StyleSheetSpecification.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  29. #include "PropertyParserNumber.h"
  30. #include "PropertyParserAnimation.h"
  31. #include "PropertyParserColour.h"
  32. #include "PropertyParserKeyword.h"
  33. #include "PropertyParserString.h"
  34. #include "PropertyParserTransform.h"
  35. namespace Rocket {
  36. namespace Core {
  37. static StyleSheetSpecification* instance = NULL;
  38. StyleSheetSpecification::StyleSheetSpecification()
  39. {
  40. ROCKET_ASSERT(instance == NULL);
  41. instance = this;
  42. }
  43. StyleSheetSpecification::~StyleSheetSpecification()
  44. {
  45. ROCKET_ASSERT(instance == this);
  46. instance = NULL;
  47. }
  48. bool StyleSheetSpecification::Initialise()
  49. {
  50. if (instance == NULL)
  51. {
  52. new StyleSheetSpecification();
  53. instance->RegisterDefaultParsers();
  54. instance->RegisterDefaultProperties();
  55. }
  56. return true;
  57. }
  58. void StyleSheetSpecification::Shutdown()
  59. {
  60. if (instance != NULL)
  61. {
  62. for (ParserMap::iterator iterator = instance->parsers.begin(); iterator != instance->parsers.end(); ++iterator)
  63. (*iterator).second->Release();
  64. delete instance;
  65. }
  66. }
  67. // Registers a parser for use in property definitions.
  68. bool StyleSheetSpecification::RegisterParser(const String& parser_name, PropertyParser* parser)
  69. {
  70. ParserMap::iterator iterator = instance->parsers.find(parser_name);
  71. if (iterator != instance->parsers.end())
  72. (*iterator).second->Release();
  73. instance->parsers[parser_name] = parser;
  74. return true;
  75. }
  76. // Returns the parser registered with a specific name.
  77. PropertyParser* StyleSheetSpecification::GetParser(const String& parser_name)
  78. {
  79. ParserMap::iterator iterator = instance->parsers.find(parser_name);
  80. if (iterator == instance->parsers.end())
  81. return NULL;
  82. return (*iterator).second;
  83. }
  84. // Registers a property with a new definition.
  85. PropertyDefinition& StyleSheetSpecification::RegisterProperty(const String& property_name, const String& default_value, bool inherited, bool forces_layout)
  86. {
  87. return instance->properties.RegisterProperty(property_name, default_value, inherited, forces_layout);
  88. }
  89. // Returns a property definition.
  90. const PropertyDefinition* StyleSheetSpecification::GetProperty(const String& property_name)
  91. {
  92. return instance->properties.GetProperty(property_name);
  93. }
  94. // Fetches a list of the names of all registered property definitions.
  95. const PropertyNameList& StyleSheetSpecification::GetRegisteredProperties()
  96. {
  97. return instance->properties.GetRegisteredProperties();
  98. }
  99. const PropertyNameList & StyleSheetSpecification::GetRegisteredInheritedProperties()
  100. {
  101. return instance->properties.GetRegisteredInheritedProperties();
  102. }
  103. // Registers a shorthand property definition.
  104. bool StyleSheetSpecification::RegisterShorthand(const String& shorthand_name, const String& property_names, PropertySpecification::ShorthandType type)
  105. {
  106. return instance->properties.RegisterShorthand(shorthand_name, property_names, type);
  107. }
  108. // Returns a shorthand definition.
  109. const PropertyShorthandDefinition* StyleSheetSpecification::GetShorthand(const String& shorthand_name)
  110. {
  111. return instance->properties.GetShorthand(shorthand_name);
  112. }
  113. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  114. bool StyleSheetSpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value, const String& source_file, int source_line_number)
  115. {
  116. return instance->properties.ParsePropertyDeclaration(dictionary, property_name, property_value, source_file, source_line_number);
  117. }
  118. // Registers Rocket's default parsers.
  119. void StyleSheetSpecification::RegisterDefaultParsers()
  120. {
  121. RegisterParser("number", new PropertyParserNumber(Property::NUMBER));
  122. RegisterParser("length", new PropertyParserNumber(Property::LENGTH, Property::PX));
  123. RegisterParser("length_percent", new PropertyParserNumber(Property::LENGTH_PERCENT, Property::PX));
  124. RegisterParser("number_length_percent", new PropertyParserNumber(Property::NUMBER_LENGTH_PERCENT, Property::PX));
  125. RegisterParser("angle", new PropertyParserNumber(Property::ANGLE, Property::RAD));
  126. RegisterParser("keyword", new PropertyParserKeyword());
  127. RegisterParser("string", new PropertyParserString());
  128. RegisterParser(ANIMATION, new PropertyParserAnimation(PropertyParserAnimation::ANIMATION_PARSER));
  129. RegisterParser(TRANSITION, new PropertyParserAnimation(PropertyParserAnimation::TRANSITION_PARSER));
  130. RegisterParser(COLOR, new PropertyParserColour());
  131. RegisterParser(TRANSFORM, new PropertyParserTransform());
  132. }
  133. namespace RCSS
  134. {
  135. struct LengthPercentageAuto {
  136. enum Type { Length, Percentage, Auto } type = Length;
  137. float value = 0;
  138. LengthPercentageAuto() {}
  139. LengthPercentageAuto(Type type, float value = 0) : type(type), value(value) {}
  140. };
  141. struct LengthPercentage {
  142. enum Type { Length, Percentage } type = Length;
  143. float value = 0;
  144. };
  145. struct NumberAuto {
  146. enum Type { Number, Auto } type = Number;
  147. float value = 0;
  148. NumberAuto() {}
  149. NumberAuto(Type type, float value = 0) : type(type), value(value) {}
  150. };
  151. enum class Display { None, Block, Inline, InlineBlock };
  152. enum class Position { Static, Relative, Absolute, Fixed };
  153. enum class Float { None, Left, Right };
  154. enum class Clear { None, Left, Right, Both };
  155. struct VerticalAlign {
  156. enum Type { Baseline, Middle, Sub, Super, TextTop, TextBottom, Top, Bottom, Length } type = Baseline;
  157. float value = 0; // For length type
  158. };
  159. enum class Overflow { Visible, Hidden, Auto, Scroll };
  160. struct Clip {
  161. enum Type { Auto, Number, None } type = Auto;
  162. float value;
  163. };
  164. enum class Visibility { Visible, Hidden };
  165. enum class FontStyle { Normal, Italic };
  166. enum class FontWeight { Normal, Bold };
  167. enum class TextAlign { Left, Right, Center, Justify };
  168. enum class TextDecoration { None, Underline };
  169. enum class TextTransform { None, Capitalize, Uppercase, Lowercase };
  170. enum class WhiteSpace { Normal, Pre, Nowrap, Prewrap, Preline };
  171. enum class Drag { None, Drag, DragDrop, Block, Clone };
  172. enum class TabIndex { None, Auto };
  173. enum class Focus { None, Auto };
  174. enum class PointerEvents { None, Auto };
  175. struct ComputedValues
  176. {
  177. LengthPercentageAuto margin_top, margin_right, margin_bottom, margin_left;
  178. LengthPercentage padding_top, padding_right, padding_bottom, padding_left;
  179. float border_top_width = 0, border_right_width = 0, border_bottom_width = 0, border_left_width = 0;
  180. Colourb border_top_color{ 255, 255, 255 }, border_right_color{ 255, 255, 255 }, border_bottom_color{ 255, 255, 255 }, border_left_color{ 255, 255, 255 };
  181. Display display = Display::Inline;
  182. Position position = Position::Static;
  183. LengthPercentageAuto top{ LengthPercentageAuto::Auto }, right{ LengthPercentageAuto::Auto }, bottom{ LengthPercentageAuto::Auto }, left{ LengthPercentageAuto::Auto };
  184. Float _float = Float::None;
  185. Clear clear = Clear::None;
  186. NumberAuto z_index = { NumberAuto::Auto };
  187. LengthPercentageAuto width = { LengthPercentageAuto::Auto };
  188. LengthPercentage min_width, max_width;
  189. LengthPercentageAuto height = { LengthPercentageAuto::Auto };
  190. LengthPercentage min_height, max_height;
  191. float line_height = 1.2f;
  192. VerticalAlign vertical_align;
  193. Overflow overflow_x = Overflow::Visible, overflow_y = Overflow::Visible;
  194. Clip clip;
  195. Visibility visibility = Visibility::Visible;
  196. Colourb background_color = Colourb(255, 255, 255, 0);
  197. Colourb color = Colourb(255, 255, 255);
  198. Colourb image_color = Colourb(255, 255, 255);
  199. float opacity = 1;
  200. String font_family;
  201. String font_charset; // empty is same as "U+0020-007E"
  202. FontStyle font_style = FontStyle::Normal;
  203. FontWeight font_weight = FontWeight::Normal;
  204. LengthPercentage font_size;
  205. TextAlign text_align = TextAlign::Left;
  206. TextDecoration text_decoration = TextDecoration::None;
  207. TextTransform text_transform = TextTransform::None;
  208. WhiteSpace white_space = WhiteSpace::Normal;
  209. String cursor;
  210. Drag drag = Drag::None;
  211. TabIndex tab_index = TabIndex::None;
  212. Focus focus = Focus::Auto;
  213. float scrollbar_margin = 0;
  214. PointerEvents pointer_events = PointerEvents::Auto;
  215. float perspective = 0;
  216. float perspective_origin_x = 0.5f, perspective_origin_y = 0.5f; // Relative
  217. String transform;
  218. float transform_origin_x = 0.5f, transform_origin_y = 0.5f, transform_origin_z = 0.f;
  219. String transition; // empty is same as "none"
  220. String animation; // empty is same as "none"
  221. };
  222. }
  223. // Registers Rocket's default style properties.
  224. void StyleSheetSpecification::RegisterDefaultProperties()
  225. {
  226. // Style property specifications (ala RCSS).
  227. RegisterProperty(MARGIN_TOP, "0px", false, true)
  228. .AddParser("keyword", "auto")
  229. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  230. RegisterProperty(MARGIN_RIGHT, "0px", false, true)
  231. .AddParser("keyword", "auto")
  232. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  233. RegisterProperty(MARGIN_BOTTOM, "0px", false, true)
  234. .AddParser("keyword", "auto")
  235. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  236. RegisterProperty(MARGIN_LEFT, "0px", false, true)
  237. .AddParser("keyword", "auto")
  238. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  239. RegisterShorthand(MARGIN, "margin-top, margin-right, margin-bottom, margin-left");
  240. RegisterProperty(PADDING_TOP, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  241. RegisterProperty(PADDING_RIGHT, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  242. RegisterProperty(PADDING_BOTTOM, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  243. RegisterProperty(PADDING_LEFT, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  244. RegisterShorthand(PADDING, "padding-top, padding-right, padding-bottom, padding-left");
  245. RegisterProperty(BORDER_TOP_WIDTH, "0px", false, true).AddParser("length");
  246. RegisterProperty(BORDER_RIGHT_WIDTH, "0px", false, true).AddParser("length");
  247. RegisterProperty(BORDER_BOTTOM_WIDTH, "0px", false, true).AddParser("length");
  248. RegisterProperty(BORDER_LEFT_WIDTH, "0px", false, true).AddParser("length");
  249. RegisterShorthand(BORDER_WIDTH, "border-top-width, border-right-width, border-bottom-width, border-left-width");
  250. RegisterProperty(BORDER_TOP_COLOR, "black", false, false).AddParser(COLOR);
  251. RegisterProperty(BORDER_RIGHT_COLOR, "black", false, false).AddParser(COLOR);
  252. RegisterProperty(BORDER_BOTTOM_COLOR, "black", false, false).AddParser(COLOR);
  253. RegisterProperty(BORDER_LEFT_COLOR, "black", false, false).AddParser(COLOR);
  254. RegisterShorthand(BORDER_COLOR, "border-top-color, border-right-color, border-bottom-color, border-left-color");
  255. RegisterShorthand(BORDER_TOP, "border-top-width, border-top-color");
  256. RegisterShorthand(BORDER_RIGHT, "border-right-width, border-right-color");
  257. RegisterShorthand(BORDER_BOTTOM, "border-bottom-width, border-bottom-color");
  258. RegisterShorthand(BORDER_LEFT, "border-left-width, border-left-color");
  259. RegisterShorthand(BORDER, "border-top, border-right, border-bottom, border-left", PropertySpecification::RECURSIVE);
  260. RegisterProperty(DISPLAY, "inline", false, true).AddParser("keyword", "none, block, inline, inline-block");
  261. RegisterProperty(POSITION, "static", false, true).AddParser("keyword", "static, relative, absolute, fixed");
  262. RegisterProperty(TOP, "auto", false, false)
  263. .AddParser("keyword", "auto")
  264. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  265. RegisterProperty(RIGHT, "auto", false, false)
  266. .AddParser("keyword", "auto")
  267. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  268. RegisterProperty(BOTTOM, "auto", false, false)
  269. .AddParser("keyword", "auto")
  270. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  271. RegisterProperty(LEFT, "auto", false, false)
  272. .AddParser("keyword", "auto")
  273. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  274. RegisterProperty(FLOAT, "none", false, true).AddParser("keyword", "none, left, right");
  275. RegisterProperty(CLEAR, "none", false, true).AddParser("keyword", "none, left, right, both");
  276. RegisterProperty(Z_INDEX, "auto", false, false)
  277. .AddParser("keyword", "auto, top, bottom")
  278. .AddParser("number");
  279. RegisterProperty(WIDTH, "auto", false, true)
  280. .AddParser("keyword", "auto")
  281. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  282. RegisterProperty(MIN_WIDTH, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  283. RegisterProperty(MAX_WIDTH, "-1px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  284. RegisterProperty(HEIGHT, "auto", false, true)
  285. .AddParser("keyword", "auto")
  286. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  287. RegisterProperty(MIN_HEIGHT, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  288. RegisterProperty(MAX_HEIGHT, "-1px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  289. RegisterProperty(LINE_HEIGHT, "1.2", true, true).AddParser("number_length_percent").SetRelativeTarget(RelativeTarget::FontSize);
  290. RegisterProperty(VERTICAL_ALIGN, "baseline", false, true)
  291. .AddParser("keyword", "baseline, middle, sub, super, text-top, text-bottom, top, bottom")
  292. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::LineHeight);
  293. RegisterProperty(OVERFLOW_X, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  294. RegisterProperty(OVERFLOW_Y, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  295. RegisterShorthand("overflow", "overflow-x, overflow-y", PropertySpecification::REPLICATE);
  296. RegisterProperty(CLIP, "auto", true, false).AddParser("keyword", "auto, none").AddParser("number");
  297. RegisterProperty(VISIBILITY, "visible", false, false).AddParser("keyword", "visible, hidden");
  298. // Need some work on this if we are to include images.
  299. RegisterProperty(BACKGROUND_COLOR, "transparent", false, false).AddParser(COLOR);
  300. RegisterShorthand(BACKGROUND, BACKGROUND_COLOR);
  301. RegisterProperty(COLOR, "white", true, false).AddParser(COLOR);
  302. RegisterProperty(IMAGE_COLOR, "white", false, false).AddParser(COLOR);
  303. RegisterProperty(OPACITY, "1", true, false).AddParser("number");
  304. RegisterProperty(FONT_FAMILY, "", true, true).AddParser("string");
  305. RegisterProperty(FONT_CHARSET, "U+0020-007E", true, false).AddParser("string");
  306. RegisterProperty(FONT_STYLE, "normal", true, true).AddParser("keyword", "normal, italic");
  307. RegisterProperty(FONT_WEIGHT, "normal", true, true).AddParser("keyword", "normal, bold");
  308. RegisterProperty(FONT_SIZE, "12px", true, true).AddParser("length").AddParser("length_percent").SetRelativeTarget(RelativeTarget::ParentFontSize);
  309. RegisterShorthand(FONT, "font-style, font-weight, font-size, font-family, font-charset");
  310. RegisterProperty(TEXT_ALIGN, LEFT, true, true).AddParser("keyword", "left, right, center, justify");
  311. RegisterProperty(TEXT_DECORATION, "none", true, false).AddParser("keyword", "none, underline"/*"none, underline, overline, line-through"*/);
  312. RegisterProperty(TEXT_TRANSFORM, "none", true, true).AddParser("keyword", "none, capitalize, uppercase, lowercase");
  313. RegisterProperty(WHITE_SPACE, "normal", true, true).AddParser("keyword", "normal, pre, nowrap, pre-wrap, pre-line");
  314. RegisterProperty(CURSOR, "auto", true, false).AddParser("keyword", "auto").AddParser("string");
  315. // Functional property specifications.
  316. RegisterProperty(DRAG, "none", false, false).AddParser("keyword", "none, drag, drag-drop, block, clone");
  317. RegisterProperty(TAB_INDEX, "none", false, false).AddParser("keyword", "none, auto");
  318. RegisterProperty(FOCUS, "auto", true, false).AddParser("keyword", "none, auto");
  319. RegisterProperty(SCROLLBAR_MARGIN, "0", false, false).AddParser("length");
  320. RegisterProperty(POINTER_EVENTS, "auto", true, false).AddParser("keyword", "auto, none");
  321. // Perspective and Transform specifications
  322. RegisterProperty(PERSPECTIVE, "none", false, false).AddParser("keyword", "none").AddParser("length");
  323. RegisterProperty(PERSPECTIVE_ORIGIN_X, "50%", false, false).AddParser("keyword", "left, center, right").AddParser("length_percent");
  324. RegisterProperty(PERSPECTIVE_ORIGIN_Y, "50%", false, false).AddParser("keyword", "top, center, bottom").AddParser("length_percent");
  325. RegisterShorthand(PERSPECTIVE_ORIGIN, "perspective-origin-x, perspective-origin-y");
  326. RegisterProperty(TRANSFORM, "none", false, false).AddParser(TRANSFORM);
  327. RegisterProperty(TRANSFORM_ORIGIN_X, "50%", false, false).AddParser("keyword", "left, center, right").AddParser("length_percent");
  328. RegisterProperty(TRANSFORM_ORIGIN_Y, "50%", false, false).AddParser("keyword", "top, center, bottom").AddParser("length_percent");
  329. RegisterProperty(TRANSFORM_ORIGIN_Z, "0", false, false).AddParser("length");
  330. RegisterShorthand(TRANSFORM_ORIGIN, "transform-origin-x, transform-origin-y, transform-origin-z");
  331. RegisterProperty(TRANSITION, "none", false, false).AddParser(TRANSITION);
  332. RegisterProperty(ANIMATION, "none", false, false).AddParser(ANIMATION);
  333. }
  334. }
  335. }