// Copyright (c) 2005, Rodrigo Braz Monteiro // Copyright (c) 2010, Thomas Goyne // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither the name of the Aegisub Group nor the names of its contributors // may be used to endorse or promote products derived from this software // without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. // // Aegisub Project http://www.aegisub.org/ /// @file ass_override.cpp /// @brief Parse and modify ASSA style overrides /// @ingroup subs_storage /// #include "config.h" #include "ass_dialogue.h" #include #include "compat.h" #include "utils.h" #include #include #include #include #include #include using namespace boost::adaptors; AssOverrideParameter::AssOverrideParameter(VariableDataType type, AssParameterClass classification) : type(type) , classification(classification) { } AssOverrideParameter::AssOverrideParameter(AssOverrideParameter&& o) : value(std::move(o.value)) , block(std::move(o.block)) , type(o.type) , classification(o.classification) { } AssOverrideParameter& AssOverrideParameter::operator=(AssOverrideParameter&& rhs) { value = std::move(rhs.value); block = std::move(rhs.block); type = rhs.type; classification = rhs.classification; return *this; } AssOverrideParameter::~AssOverrideParameter() { } template<> wxString AssOverrideParameter::Get() const { if (omitted) throw agi::InternalError("AssOverrideParameter::Get() called on omitted parameter", 0); if (block.get()) { wxString str(block->GetText()); str.Replace("{", ""); str.Replace("}", ""); return str; } return value; } template<> int AssOverrideParameter::Get() const { long v = 0; Get().ToLong(&v); return v; } template<> double AssOverrideParameter::Get() const { double v = 0; Get().ToDouble(&v); return v; } template<> float AssOverrideParameter::Get() const { return Get(); } template<> bool AssOverrideParameter::Get() const { return Get() != 0; } template<> agi::Color AssOverrideParameter::Get() const { return from_wx(Get()); } template<> AssDialogueBlockOverride *AssOverrideParameter::Get() const { if (!block.get()) { block.reset(new AssDialogueBlockOverride(Get())); block->ParseTags(); } return block.get(); } template<> void AssOverrideParameter::Set(wxString new_value) { omitted = false; value = new_value; block.reset(); } template<> void AssOverrideParameter::Set(int new_value) { Set(wxString::Format("%d", new_value)); } template<> void AssOverrideParameter::Set(double new_value) { Set(wxString::Format("%g", new_value)); } template<> void AssOverrideParameter::Set(bool new_value) { Set(new_value); } namespace { /// The parameter is absent unless the total number of parameters is the /// indicated number. Note that only arguments not at the end need to be marked /// as optional; this is just to know which parameters to skip when there are /// earlier optional arguments enum AssParameterOptional { NOT_OPTIONAL = 0xFF, OPTIONAL_1 = 0x01, OPTIONAL_2 = 0x02, OPTIONAL_3 = 0x04, OPTIONAL_4 = 0x08, OPTIONAL_5 = 0x10, OPTIONAL_6 = 0x20, OPTIONAL_7 = 0x40 }; /// Prototype of a single override parameter struct AssOverrideParamProto { /// ASS_ParameterOptional int optional; /// Type of this parameter VariableDataType type; /// Semantic type of this parameter AssParameterClass classification; AssOverrideParamProto (VariableDataType type, int opt=NOT_OPTIONAL, AssParameterClass classi=PARCLASS_NORMAL); }; struct AssOverrideTagProto { /// Name of the tag, with slash wxString name; /// Parameters to this tag std::vector params; typedef std::vector::iterator iterator; /// @brief Add a parameter to this tag prototype /// @param type Data type of the parameter /// @param classi Semantic type of the parameter /// @param opt Situations in which this parameter is present void AddParam(VariableDataType type, AssParameterClass classi = PARCLASS_NORMAL, int opt = NOT_OPTIONAL); /// @brief Convenience function for single-argument tags /// @param name Name of the tag, with slash /// @param type Data type of the parameter /// @param classi Semantic type of the parameter /// @param opt Situations in which this parameter is present void Set(wxString name, VariableDataType type, AssParameterClass classi = PARCLASS_NORMAL, int opt = NOT_OPTIONAL); }; AssOverrideParamProto::AssOverrideParamProto(VariableDataType type, int opt, AssParameterClass classi) : optional(opt) , type(type) , classification(classi) { } void AssOverrideTagProto::AddParam(VariableDataType type, AssParameterClass classi, int opt) { params.emplace_back(type, opt, classi); } void AssOverrideTagProto::Set(wxString newName, VariableDataType type, AssParameterClass classi, int opt) { name = newName; params.emplace_back(type, opt, classi); } static std::vector proto; static void load_protos() { static bool loaded = false; if (loaded) return; loaded = true; proto.resize(56); int i = 0; // Longer tag names must appear before shorter tag names proto[0].Set("\\alpha", VARDATA_TEXT); // \alpha proto[++i].Set("\\bord", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \bord proto[++i].Set("\\xbord", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \xbord proto[++i].Set("\\ybord", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \ybord proto[++i].Set("\\shad", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \shad proto[++i].Set("\\xshad", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \xshad proto[++i].Set("\\yshad", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \yshad // \fade(,,,,,,) i++; proto[i].name = "\\fade"; proto[i].AddParam(VARDATA_INT); proto[i].AddParam(VARDATA_INT); proto[i].AddParam(VARDATA_INT); proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_START); proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_START); proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_START); proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_START); // \move(,,,[,,]) i++; proto[i].name = "\\move"; proto[i].AddParam(VARDATA_FLOAT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_FLOAT,PARCLASS_ABSOLUTE_POS_Y); proto[i].AddParam(VARDATA_FLOAT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_FLOAT,PARCLASS_ABSOLUTE_POS_Y); proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_START); proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_START); // If these are rearranged, keep rect clip and vector clip adjacent in this order // \clip(,,,) i++; proto[i].name = "\\clip"; proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_Y); proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_Y); // \clip([,]) i++; proto[i].name = "\\clip"; proto[i].AddParam(VARDATA_INT,PARCLASS_NORMAL,OPTIONAL_2); proto[i].AddParam(VARDATA_TEXT,PARCLASS_DRAWING); // \iclip(,,,) i++; proto[i].name = "\\iclip"; proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_Y); proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_Y); // \iclip([,]) i++; proto[i].name = "\\iclip"; proto[i].AddParam(VARDATA_INT,PARCLASS_NORMAL,OPTIONAL_2); proto[i].AddParam(VARDATA_TEXT,PARCLASS_DRAWING); proto[++i].Set("\\fscx", VARDATA_FLOAT,PARCLASS_RELATIVE_SIZE_X); // \fscx proto[++i].Set("\\fscy", VARDATA_FLOAT,PARCLASS_RELATIVE_SIZE_Y); // \fscy // \pos(,) i++; proto[i].name = "\\pos"; proto[i].AddParam(VARDATA_FLOAT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_FLOAT,PARCLASS_ABSOLUTE_POS_Y); // \org(,) i++; proto[i].name = "\\org"; proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_X); proto[i].AddParam(VARDATA_INT,PARCLASS_ABSOLUTE_POS_Y); proto[++i].Set("\\pbo", VARDATA_INT,PARCLASS_ABSOLUTE_POS_Y); // \pbo // \fad(,) i++; proto[i].name = "\\fad"; proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_START); proto[i].AddParam(VARDATA_INT,PARCLASS_RELATIVE_TIME_END); proto[++i].Set("\\fsp", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \fsp proto[++i].Set("\\frx", VARDATA_FLOAT); // \frx proto[++i].Set("\\fry", VARDATA_FLOAT); // \fry proto[++i].Set("\\frz", VARDATA_FLOAT); // \frz proto[++i].Set("\\fr", VARDATA_FLOAT); // \fr proto[++i].Set("\\fax", VARDATA_FLOAT); // \fax proto[++i].Set("\\fay", VARDATA_FLOAT); // \fay proto[++i].Set("\\1c", VARDATA_TEXT); // \1c&H& proto[++i].Set("\\2c", VARDATA_TEXT); // \2c&H& proto[++i].Set("\\3c", VARDATA_TEXT); // \3c&H& proto[++i].Set("\\4c", VARDATA_TEXT); // \4c&H& proto[++i].Set("\\1a", VARDATA_TEXT); // \1a&H& proto[++i].Set("\\2a", VARDATA_TEXT); // \2a&H& proto[++i].Set("\\3a", VARDATA_TEXT); // \3a&H& proto[++i].Set("\\4a", VARDATA_TEXT); // \4a&H& proto[++i].Set("\\fe", VARDATA_TEXT); // \fe proto[++i].Set("\\ko", VARDATA_INT,PARCLASS_KARAOKE); // \ko proto[++i].Set("\\kf", VARDATA_INT,PARCLASS_KARAOKE); // \kf proto[++i].Set("\\be", VARDATA_INT); // \be proto[++i].Set("\\blur", VARDATA_FLOAT); // \blur proto[++i].Set("\\fn", VARDATA_TEXT); // \fn proto[++i].Set("\\fs+", VARDATA_FLOAT); // \fs+ proto[++i].Set("\\fs-", VARDATA_FLOAT); // \fs- proto[++i].Set("\\fs", VARDATA_FLOAT,PARCLASS_ABSOLUTE_SIZE); // \fs proto[++i].Set("\\an", VARDATA_INT); // \an proto[++i].Set("\\c", VARDATA_TEXT); // \c&H& proto[++i].Set("\\b", VARDATA_INT); // \b<0/1/weight> proto[++i].Set("\\i", VARDATA_BOOL); // \i<0/1> proto[++i].Set("\\u", VARDATA_BOOL); // \u<0/1> proto[++i].Set("\\s", VARDATA_BOOL); // \s<0/1> proto[++i].Set("\\a", VARDATA_INT); // \a proto[++i].Set("\\k", VARDATA_INT,PARCLASS_KARAOKE); // \k proto[++i].Set("\\K", VARDATA_INT,PARCLASS_KARAOKE); // \K proto[++i].Set("\\q", VARDATA_INT); // \q<0-3> proto[++i].Set("\\p", VARDATA_INT); // \p proto[++i].Set("\\r", VARDATA_TEXT); // \r[] // \t([,,][,]