StyleSheetSpecification.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // Modified by uniquejack
  28. // Implement opacity (Check https://github.com/libRocket/libRocket/pull/262)
  29. #include "precompiled.h"
  30. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  31. #include "PropertyParserNumber.h"
  32. #include "PropertyParserColour.h"
  33. #include "PropertyParserKeyword.h"
  34. #include "PropertyParserString.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());
  122. RegisterParser("keyword", new PropertyParserKeyword());
  123. RegisterParser("string", new PropertyParserString());
  124. RegisterParser(COLOR, new PropertyParserColour());
  125. }
  126. // Registers Rocket's default style properties.
  127. void StyleSheetSpecification::RegisterDefaultProperties()
  128. {
  129. // Style property specifications (ala RCSS).
  130. RegisterProperty(MARGIN_TOP, "0px", false, true)
  131. .AddParser("keyword", "auto")
  132. .AddParser("number");
  133. RegisterProperty(MARGIN_RIGHT, "0px", false, true)
  134. .AddParser("keyword", "auto")
  135. .AddParser("number");
  136. RegisterProperty(MARGIN_BOTTOM, "0px", false, true)
  137. .AddParser("keyword", "auto")
  138. .AddParser("number");
  139. RegisterProperty(MARGIN_LEFT, "0px", false, true)
  140. .AddParser("keyword", "auto")
  141. .AddParser("number");
  142. RegisterShorthand(MARGIN, "margin-top, margin-right, margin-bottom, margin-left");
  143. RegisterProperty(PADDING_TOP, "0px", false, true).AddParser("number");
  144. RegisterProperty(PADDING_RIGHT, "0px", false, true).AddParser("number");
  145. RegisterProperty(PADDING_BOTTOM, "0px", false, true).AddParser("number");
  146. RegisterProperty(PADDING_LEFT, "0px", false, true).AddParser("number");
  147. RegisterShorthand(PADDING, "padding-top, padding-right, padding-bottom, padding-left");
  148. RegisterProperty(BORDER_TOP_WIDTH, "0px", false, true).AddParser("number");
  149. RegisterProperty(BORDER_RIGHT_WIDTH, "0px", false, true).AddParser("number");
  150. RegisterProperty(BORDER_BOTTOM_WIDTH, "0px", false, true).AddParser("number");
  151. RegisterProperty(BORDER_LEFT_WIDTH, "0px", false, true).AddParser("number");
  152. RegisterShorthand(BORDER_WIDTH, "border-top-width, border-right-width, border-bottom-width, border-left-width");
  153. RegisterProperty(BORDER_TOP_COLOR, "black", false, false).AddParser(COLOR);
  154. RegisterProperty(BORDER_RIGHT_COLOR, "black", false, false).AddParser(COLOR);
  155. RegisterProperty(BORDER_BOTTOM_COLOR, "black", false, false).AddParser(COLOR);
  156. RegisterProperty(BORDER_LEFT_COLOR, "black", false, false).AddParser(COLOR);
  157. RegisterShorthand(BORDER_COLOR, "border-top-color, border-right-color, border-bottom-color, border-left-color");
  158. RegisterShorthand(BORDER_TOP, "border-top-width, border-top-color");
  159. RegisterShorthand(BORDER_RIGHT, "border-right-width, border-right-color");
  160. RegisterShorthand(BORDER_BOTTOM, "border-bottom-width, border-bottom-color");
  161. RegisterShorthand(BORDER_LEFT, "border-left-width, border-left-color");
  162. RegisterProperty(DISPLAY, "inline", false, true).AddParser("keyword", "none, block, inline, inline-block");
  163. RegisterProperty(POSITION, "static", false, true).AddParser("keyword", "static, relative, absolute, fixed");
  164. RegisterProperty(TOP, "auto", false, false)
  165. .AddParser("keyword", "auto")
  166. .AddParser("number");
  167. RegisterProperty(RIGHT, "auto", false, false)
  168. .AddParser("keyword", "auto")
  169. .AddParser("number");
  170. RegisterProperty(BOTTOM, "auto", false, false)
  171. .AddParser("keyword", "auto")
  172. .AddParser("number");
  173. RegisterProperty(LEFT, "auto", false, false)
  174. .AddParser("keyword", "auto")
  175. .AddParser("number");
  176. RegisterProperty(FLOAT, "none", false, true).AddParser("keyword", "none, left, right");
  177. RegisterProperty(CLEAR, "none", false, true).AddParser("keyword", "none, left, right, both");
  178. RegisterProperty(Z_INDEX, "auto", false, false)
  179. .AddParser("keyword", "auto, top, bottom")
  180. .AddParser("number");
  181. RegisterProperty(WIDTH, "auto", false, true)
  182. .AddParser("keyword", "auto")
  183. .AddParser("number");
  184. RegisterProperty(MIN_WIDTH, "0px", false, true).AddParser("number");
  185. RegisterProperty(MAX_WIDTH, "-1", false, true).AddParser("number");
  186. RegisterProperty(HEIGHT, "auto", false, true)
  187. .AddParser("keyword", "auto")
  188. .AddParser("number");
  189. RegisterProperty(MIN_HEIGHT, "0px", false, true).AddParser("number");
  190. RegisterProperty(MAX_HEIGHT, "-1", false, true).AddParser("number");
  191. RegisterProperty(LINE_HEIGHT, "1.2", true, true).AddParser("number");
  192. RegisterProperty(VERTICAL_ALIGN, "baseline", false, true)
  193. .AddParser("keyword", "baseline, middle, sub, super, text-top, text-bottom, top, bottom")
  194. .AddParser("number");
  195. RegisterProperty(OVERFLOW_X, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  196. RegisterProperty(OVERFLOW_Y, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  197. RegisterShorthand("overflow", "overflow-x, overflow-y", PropertySpecification::REPLICATE);
  198. RegisterProperty(CLIP, "auto", true, false)
  199. .AddParser("keyword", "auto, none")
  200. .AddParser("number");
  201. RegisterProperty(VISIBILITY, "visible", false, false).AddParser("keyword", "visible, hidden");
  202. // Need some work on this if we are to include images.
  203. RegisterProperty(BACKGROUND_COLOR, "transparent", false, false).AddParser(COLOR);
  204. RegisterShorthand(BACKGROUND, BACKGROUND_COLOR);
  205. RegisterProperty(COLOR, "white", true, false).AddParser(COLOR);
  206. RegisterProperty(IMAGE_COLOR, "white", true, false).AddParser(COLOR);
  207. RegisterProperty(FONT_FAMILY, "", true, true).AddParser("string");
  208. RegisterProperty(FONT_CHARSET, "U+0020-007E", true, false).AddParser("string");
  209. RegisterProperty(FONT_STYLE, "normal", true, true).AddParser("keyword", "normal, italic");
  210. RegisterProperty(FONT_WEIGHT, "normal", true, true).AddParser("keyword", "normal, bold");
  211. RegisterProperty(FONT_SIZE, "12", true, true).AddParser("number");
  212. RegisterShorthand(FONT, "font-style, font-weight, font-size, font-family, font-charset");
  213. RegisterProperty(TEXT_ALIGN, LEFT, true, true).AddParser("keyword", "left, right, center, justify");
  214. RegisterProperty(TEXT_DECORATION, "none", true, false).AddParser("keyword", "none, underline"/*"none, underline, overline, line-through"*/);
  215. RegisterProperty(TEXT_TRANSFORM, "none", true, true).AddParser("keyword", "none, capitalize, uppercase, lowercase");
  216. RegisterProperty(WHITE_SPACE, "normal", true, true).AddParser("keyword", "normal, pre, nowrap, pre-wrap, pre-line");
  217. RegisterProperty(CURSOR, "auto", true, false)
  218. .AddParser("keyword", "auto")
  219. .AddParser("string");
  220. // Functional property specifications.
  221. RegisterProperty(DRAG, "none", false, false).AddParser("keyword", "none, drag, drag-drop, block, clone");
  222. RegisterProperty(TAB_INDEX, "none", false, false).AddParser("keyword", "none, auto");
  223. RegisterProperty(FOCUS, "auto", true, false).AddParser("keyword", "none, auto");
  224. RegisterProperty(SCROLLBAR_MARGIN, "0", false, false).AddParser("number");
  225. RegisterProperty(OPACITY, "1", true, false).AddParser("number");
  226. RegisterProperty(POINTER_EVENTS, "auto", true, false).AddParser("keyword", "auto, none");
  227. }
  228. }
  229. }