StyleSheetSpecification.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "PropertyParserColour.h"
  31. #include "PropertyParserKeyword.h"
  32. #include "PropertyParserString.h"
  33. namespace Rocket {
  34. namespace Core {
  35. static StyleSheetSpecification* instance = NULL;
  36. StyleSheetSpecification::StyleSheetSpecification()
  37. {
  38. ROCKET_ASSERT(instance == NULL);
  39. instance = this;
  40. }
  41. StyleSheetSpecification::~StyleSheetSpecification()
  42. {
  43. ROCKET_ASSERT(instance == this);
  44. instance = NULL;
  45. }
  46. bool StyleSheetSpecification::Initialise()
  47. {
  48. if (instance == NULL)
  49. {
  50. new StyleSheetSpecification();
  51. instance->RegisterDefaultParsers();
  52. instance->RegisterDefaultProperties();
  53. }
  54. return true;
  55. }
  56. void StyleSheetSpecification::Shutdown()
  57. {
  58. if (instance != NULL)
  59. {
  60. for (ParserMap::iterator iterator = instance->parsers.begin(); iterator != instance->parsers.end(); iterator++)
  61. (*iterator).second->Release();
  62. delete instance;
  63. }
  64. }
  65. // Registers a parser for use in property definitions.
  66. bool StyleSheetSpecification::RegisterParser(const String& parser_name, PropertyParser* parser)
  67. {
  68. ParserMap::iterator iterator = instance->parsers.find(parser_name);
  69. if (iterator != instance->parsers.end())
  70. (*iterator).second->Release();
  71. instance->parsers[parser_name] = parser;
  72. return true;
  73. }
  74. // Returns the parser registered with a specific name.
  75. PropertyParser* StyleSheetSpecification::GetParser(const String& parser_name)
  76. {
  77. ParserMap::iterator iterator = instance->parsers.find(parser_name);
  78. if (iterator == instance->parsers.end())
  79. return NULL;
  80. return (*iterator).second;
  81. }
  82. // Registers a property with a new definition.
  83. PropertyDefinition& StyleSheetSpecification::RegisterProperty(const String& property_name, const String& default_value, bool inherited, bool forces_layout)
  84. {
  85. return instance->properties.RegisterProperty(property_name, default_value, inherited, forces_layout);
  86. }
  87. // Returns a property definition.
  88. const PropertyDefinition* StyleSheetSpecification::GetProperty(const String& property_name)
  89. {
  90. return instance->properties.GetProperty(property_name);
  91. }
  92. // Fetches a list of the names of all registered property definitions.
  93. const PropertyNameList& StyleSheetSpecification::GetRegisteredProperties()
  94. {
  95. return instance->properties.GetRegisteredProperties();
  96. }
  97. const PropertyNameList & StyleSheetSpecification::GetRegisteredInheritedProperties()
  98. {
  99. return instance->properties.GetRegisteredInheritedProperties();
  100. }
  101. // Registers a shorthand property definition.
  102. bool StyleSheetSpecification::RegisterShorthand(const String& shorthand_name, const String& property_names, PropertySpecification::ShorthandType type)
  103. {
  104. return instance->properties.RegisterShorthand(shorthand_name, property_names, type);
  105. }
  106. // Returns a shorthand definition.
  107. const PropertyShorthandDefinition* StyleSheetSpecification::GetShorthand(const String& shorthand_name)
  108. {
  109. return instance->properties.GetShorthand(shorthand_name);
  110. }
  111. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  112. bool StyleSheetSpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value, const String& source_file, int source_line_number)
  113. {
  114. return instance->properties.ParsePropertyDeclaration(dictionary, property_name, property_value, source_file, source_line_number);
  115. }
  116. // Registers Rocket's default parsers.
  117. void StyleSheetSpecification::RegisterDefaultParsers()
  118. {
  119. RegisterParser("number", new PropertyParserNumber());
  120. RegisterParser("keyword", new PropertyParserKeyword());
  121. RegisterParser("string", new PropertyParserString());
  122. RegisterParser(COLOR, new PropertyParserColour());
  123. }
  124. // Registers Rocket's default style properties.
  125. void StyleSheetSpecification::RegisterDefaultProperties()
  126. {
  127. // Style property specifications (ala RCSS).
  128. RegisterProperty(MARGIN_TOP, "0px", false, true)
  129. .AddParser("keyword", "auto")
  130. .AddParser("number");
  131. RegisterProperty(MARGIN_RIGHT, "0px", false, true)
  132. .AddParser("keyword", "auto")
  133. .AddParser("number");
  134. RegisterProperty(MARGIN_BOTTOM, "0px", false, true)
  135. .AddParser("keyword", "auto")
  136. .AddParser("number");
  137. RegisterProperty(MARGIN_LEFT, "0px", false, true)
  138. .AddParser("keyword", "auto")
  139. .AddParser("number");
  140. RegisterShorthand(MARGIN, "margin-top, margin-right, margin-bottom, margin-left");
  141. RegisterProperty(PADDING_TOP, "0px", false, true).AddParser("number");
  142. RegisterProperty(PADDING_RIGHT, "0px", false, true).AddParser("number");
  143. RegisterProperty(PADDING_BOTTOM, "0px", false, true).AddParser("number");
  144. RegisterProperty(PADDING_LEFT, "0px", false, true).AddParser("number");
  145. RegisterShorthand(PADDING, "padding-top, padding-right, padding-bottom, padding-left");
  146. RegisterProperty(BORDER_TOP_WIDTH, "0px", false, true).AddParser("number");
  147. RegisterProperty(BORDER_RIGHT_WIDTH, "0px", false, true).AddParser("number");
  148. RegisterProperty(BORDER_BOTTOM_WIDTH, "0px", false, true).AddParser("number");
  149. RegisterProperty(BORDER_LEFT_WIDTH, "0px", false, true).AddParser("number");
  150. RegisterShorthand(BORDER_WIDTH, "border-top-width, border-right-width, border-bottom-width, border-left-width");
  151. RegisterProperty(BORDER_TOP_COLOR, "black", false, false).AddParser(COLOR);
  152. RegisterProperty(BORDER_RIGHT_COLOR, "black", false, false).AddParser(COLOR);
  153. RegisterProperty(BORDER_BOTTOM_COLOR, "black", false, false).AddParser(COLOR);
  154. RegisterProperty(BORDER_LEFT_COLOR, "black", false, false).AddParser(COLOR);
  155. RegisterShorthand(BORDER_COLOR, "border-top-color, border-right-color, border-bottom-color, border-left-color");
  156. RegisterShorthand(BORDER_TOP, "border-top-width, border-top-color");
  157. RegisterShorthand(BORDER_RIGHT, "border-right-width, border-right-color");
  158. RegisterShorthand(BORDER_BOTTOM, "border-bottom-width, border-bottom-color");
  159. RegisterShorthand(BORDER_LEFT, "border-left-width, border-left-color");
  160. RegisterProperty(DISPLAY, "inline", false, true).AddParser("keyword", "none, block, inline, inline-block");
  161. RegisterProperty(POSITION, "static", false, true).AddParser("keyword", "static, relative, absolute, fixed");
  162. RegisterProperty(TOP, "auto", false, false)
  163. .AddParser("keyword", "auto")
  164. .AddParser("number");
  165. RegisterProperty(RIGHT, "auto", false, false)
  166. .AddParser("keyword", "auto")
  167. .AddParser("number");
  168. RegisterProperty(BOTTOM, "auto", false, false)
  169. .AddParser("keyword", "auto")
  170. .AddParser("number");
  171. RegisterProperty(LEFT, "auto", false, false)
  172. .AddParser("keyword", "auto")
  173. .AddParser("number");
  174. RegisterProperty(FLOAT, "none", false, true).AddParser("keyword", "none, left, right");
  175. RegisterProperty(CLEAR, "none", false, true).AddParser("keyword", "none, left, right, both");
  176. RegisterProperty(Z_INDEX, "auto", false, false)
  177. .AddParser("keyword", "auto, top, bottom")
  178. .AddParser("number");
  179. RegisterProperty(WIDTH, "auto", false, true)
  180. .AddParser("keyword", "auto")
  181. .AddParser("number");
  182. RegisterProperty(MIN_WIDTH, "0px", false, true).AddParser("number");
  183. RegisterProperty(MAX_WIDTH, "-1", false, true).AddParser("number");
  184. RegisterProperty(HEIGHT, "auto", false, true)
  185. .AddParser("keyword", "auto")
  186. .AddParser("number");
  187. RegisterProperty(MIN_HEIGHT, "0px", false, true).AddParser("number");
  188. RegisterProperty(MAX_HEIGHT, "-1", false, true).AddParser("number");
  189. RegisterProperty(LINE_HEIGHT, "1.2", true, true).AddParser("number");
  190. RegisterProperty(VERTICAL_ALIGN, "baseline", false, true)
  191. .AddParser("keyword", "baseline, middle, sub, super, text-top, text-bottom, top, bottom")
  192. .AddParser("number");
  193. RegisterProperty(OVERFLOW_X, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  194. RegisterProperty(OVERFLOW_Y, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  195. RegisterShorthand("overflow", "overflow-x, overflow-y", PropertySpecification::REPLICATE);
  196. RegisterProperty(CLIP, "auto", true, false)
  197. .AddParser("keyword", "auto, none")
  198. .AddParser("number");
  199. RegisterProperty(VISIBILITY, "visible", false, false).AddParser("keyword", "visible, hidden");
  200. // Need some work on this if we are to include images.
  201. RegisterProperty(BACKGROUND_COLOR, "transparent", false, false).AddParser(COLOR);
  202. RegisterShorthand(BACKGROUND, BACKGROUND_COLOR);
  203. RegisterProperty(COLOR, "white", true, false).AddParser(COLOR);
  204. RegisterProperty(FONT_FAMILY, "", true, true).AddParser("string");
  205. RegisterProperty(FONT_CHARSET, "U+0020-007E", true, false).AddParser("string");
  206. RegisterProperty(FONT_STYLE, "normal", true, true).AddParser("keyword", "normal, italic");
  207. RegisterProperty(FONT_WEIGHT, "normal", true, true).AddParser("keyword", "normal, bold");
  208. RegisterProperty(FONT_SIZE, "12", true, true).AddParser("number");
  209. RegisterShorthand(FONT, "font-style, font-weight, font-size, font-family, font-charset");
  210. RegisterProperty(TEXT_ALIGN, LEFT, true, true).AddParser("keyword", "left, right, center, justify");
  211. RegisterProperty(TEXT_DECORATION, "none", true, false).AddParser("keyword", "none, underline"/*"none, underline, overline, line-through"*/);
  212. RegisterProperty(TEXT_TRANSFORM, "none", true, true).AddParser("keyword", "none, capitalize, uppercase, lowercase");
  213. RegisterProperty(WHITE_SPACE, "normal", true, true).AddParser("keyword", "normal, pre, nowrap, pre-wrap, pre-line");
  214. RegisterProperty(CURSOR, "auto", true, false)
  215. .AddParser("keyword", "auto")
  216. .AddParser("string");
  217. // Functional property specifications.
  218. RegisterProperty(DRAG, "none", false, false).AddParser("keyword", "none, drag, drag-drop, block, clone");
  219. RegisterProperty(TAB_INDEX, "none", false, false).AddParser("keyword", "none, auto");
  220. RegisterProperty(FOCUS, "auto", true, false).AddParser("keyword", "none, auto");
  221. RegisterProperty(SCROLLBAR_MARGIN, "0", false, false).AddParser("number");
  222. }
  223. }
  224. }