StyleSheetFactory.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "StyleSheetFactory.h"
  29. #include "../../Include/RmlUi/Core/StyleSheet.h"
  30. #include "StyleSheetNode.h"
  31. #include "StreamFile.h"
  32. #include "StyleSheetNodeSelectorNthChild.h"
  33. #include "StyleSheetNodeSelectorNthLastChild.h"
  34. #include "StyleSheetNodeSelectorNthOfType.h"
  35. #include "StyleSheetNodeSelectorNthLastOfType.h"
  36. #include "StyleSheetNodeSelectorFirstChild.h"
  37. #include "StyleSheetNodeSelectorLastChild.h"
  38. #include "StyleSheetNodeSelectorFirstOfType.h"
  39. #include "StyleSheetNodeSelectorLastOfType.h"
  40. #include "StyleSheetNodeSelectorOnlyChild.h"
  41. #include "StyleSheetNodeSelectorOnlyOfType.h"
  42. #include "StyleSheetNodeSelectorEmpty.h"
  43. #include "../../Include/RmlUi/Core/Log.h"
  44. namespace Rml {
  45. static StyleSheetFactory* instance = nullptr;
  46. StyleSheetFactory::StyleSheetFactory()
  47. {
  48. RMLUI_ASSERT(instance == nullptr);
  49. instance = this;
  50. }
  51. StyleSheetFactory::~StyleSheetFactory()
  52. {
  53. instance = nullptr;
  54. }
  55. bool StyleSheetFactory::Initialise()
  56. {
  57. new StyleSheetFactory();
  58. instance->selectors["nth-child"] = new StyleSheetNodeSelectorNthChild();
  59. instance->selectors["nth-last-child"] = new StyleSheetNodeSelectorNthLastChild();
  60. instance->selectors["nth-of-type"] = new StyleSheetNodeSelectorNthOfType();
  61. instance->selectors["nth-last-of-type"] = new StyleSheetNodeSelectorNthLastOfType();
  62. instance->selectors["first-child"] = new StyleSheetNodeSelectorFirstChild();
  63. instance->selectors["last-child"] = new StyleSheetNodeSelectorLastChild();
  64. instance->selectors["first-of-type"] = new StyleSheetNodeSelectorFirstOfType();
  65. instance->selectors["last-of-type"] = new StyleSheetNodeSelectorLastOfType();
  66. instance->selectors["only-child"] = new StyleSheetNodeSelectorOnlyChild();
  67. instance->selectors["only-of-type"] = new StyleSheetNodeSelectorOnlyOfType();
  68. instance->selectors["empty"] = new StyleSheetNodeSelectorEmpty();
  69. return true;
  70. }
  71. void StyleSheetFactory::Shutdown()
  72. {
  73. if (instance != nullptr)
  74. {
  75. ClearStyleSheetCache();
  76. for (SelectorMap::iterator i = instance->selectors.begin(); i != instance->selectors.end(); ++i)
  77. delete (*i).second;
  78. delete instance;
  79. }
  80. }
  81. SharedPtr<StyleSheet> StyleSheetFactory::GetStyleSheet(const String& sheet_name)
  82. {
  83. // Look up the sheet definition in the cache
  84. StyleSheets::iterator itr = instance->stylesheets.find(sheet_name);
  85. if (itr != instance->stylesheets.end())
  86. {
  87. return (*itr).second;
  88. }
  89. // Don't currently have the sheet, attempt to load it
  90. SharedPtr<StyleSheet> sheet = instance->LoadStyleSheet(sheet_name);
  91. if (!sheet)
  92. return nullptr;
  93. sheet->OptimizeNodeProperties();
  94. // Add it to the cache, and add a reference count so the cache will keep hold of it.
  95. instance->stylesheets[sheet_name] = sheet;
  96. return sheet;
  97. }
  98. SharedPtr<StyleSheet> StyleSheetFactory::GetStyleSheet(const StringList& sheets)
  99. {
  100. // Generate a unique key for these sheets
  101. String combined_key;
  102. for (size_t i = 0; i < sheets.size(); i++)
  103. {
  104. URL path(sheets[i]);
  105. combined_key += path.GetFileName();
  106. }
  107. // Look up the sheet definition in the cache.
  108. StyleSheets::iterator itr = instance->stylesheet_cache.find(combined_key);
  109. if (itr != instance->stylesheet_cache.end())
  110. {
  111. return (*itr).second;
  112. }
  113. // Load and combine the sheets.
  114. SharedPtr<StyleSheet> sheet;
  115. for (size_t i = 0; i < sheets.size(); i++)
  116. {
  117. SharedPtr<StyleSheet> sub_sheet = GetStyleSheet(sheets[i]);
  118. if (sub_sheet)
  119. {
  120. if (sheet)
  121. {
  122. SharedPtr<StyleSheet> new_sheet = sheet->CombineStyleSheet(*sub_sheet);
  123. sheet = std::move(new_sheet);
  124. }
  125. else
  126. sheet = sub_sheet;
  127. }
  128. else
  129. Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", sheets[i].c_str());
  130. }
  131. if (!sheet)
  132. return nullptr;
  133. sheet->OptimizeNodeProperties();
  134. // Add to cache, and a reference to the sheet to hold it in the cache.
  135. instance->stylesheet_cache[combined_key] = sheet;
  136. return sheet;
  137. }
  138. // Clear the style sheet cache.
  139. void StyleSheetFactory::ClearStyleSheetCache()
  140. {
  141. instance->stylesheets.clear();
  142. instance->stylesheet_cache.clear();
  143. }
  144. // Returns one of the available node selectors.
  145. StructuralSelector StyleSheetFactory::GetSelector(const String& name)
  146. {
  147. SelectorMap::const_iterator it;
  148. const size_t parameter_start = name.find('(');
  149. if (parameter_start == String::npos)
  150. it = instance->selectors.find(name);
  151. else
  152. it = instance->selectors.find(name.substr(0, parameter_start));
  153. if (it == instance->selectors.end())
  154. return StructuralSelector(nullptr, 0, 0);
  155. // Parse the 'a' and 'b' values.
  156. int a = 1;
  157. int b = 0;
  158. const size_t parameter_end = name.find(')', parameter_start + 1);
  159. if (parameter_start != String::npos &&
  160. parameter_end != String::npos)
  161. {
  162. String parameters = StringUtilities::StripWhitespace(name.substr(parameter_start + 1, parameter_end - (parameter_start + 1)));
  163. // Check for 'even' or 'odd' first.
  164. if (parameters == "even")
  165. {
  166. a = 2;
  167. b = 0;
  168. }
  169. else if (parameters == "odd")
  170. {
  171. a = 2;
  172. b = 1;
  173. }
  174. else
  175. {
  176. // Alrighty; we've got an equation in the form of [[+/-]an][(+/-)b]. So, foist up, we split on 'n'.
  177. const size_t n_index = parameters.find('n');
  178. if (n_index == String::npos)
  179. {
  180. // The equation is 0n + b. So a = 0, and we only have to parse b.
  181. a = 0;
  182. b = atoi(parameters.c_str());
  183. }
  184. else
  185. {
  186. if (n_index == 0)
  187. a = 1;
  188. else
  189. {
  190. const String a_parameter = parameters.substr(0, n_index);
  191. if (StringUtilities::StripWhitespace(a_parameter) == "-")
  192. a = -1;
  193. else
  194. a = atoi(a_parameter.c_str());
  195. }
  196. size_t pm_index = parameters.find('+', n_index + 1);
  197. if (pm_index != String::npos)
  198. b = 1;
  199. else
  200. {
  201. pm_index = parameters.find('-', n_index + 1);
  202. if (pm_index != String::npos)
  203. b = -1;
  204. }
  205. if (n_index == parameters.size() - 1 || pm_index == String::npos)
  206. b = 0;
  207. else
  208. b = b * atoi(parameters.data() + pm_index + 1);
  209. }
  210. }
  211. }
  212. return StructuralSelector(it->second, a, b);
  213. }
  214. SharedPtr<StyleSheet> StyleSheetFactory::LoadStyleSheet(const String& sheet)
  215. {
  216. SharedPtr<StyleSheet> new_style_sheet;
  217. // Open stream, construct new sheet and pass the stream into the sheet
  218. // TODO: Make this support ASYNC
  219. auto stream = MakeUnique<StreamFile>();
  220. if (stream->Open(sheet))
  221. {
  222. new_style_sheet = MakeShared<StyleSheet>();
  223. if (!new_style_sheet->LoadStyleSheet(stream.get()))
  224. {
  225. new_style_sheet = nullptr;
  226. }
  227. }
  228. return new_style_sheet;
  229. }
  230. } // namespace Rml