StyleSheetFactory.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/StyleSheetContainer.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<StyleSheetContainer> StyleSheetFactory::GetStyleSheetContainer(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<StyleSheetContainer> sheet = instance->LoadStyleSheetContainer(sheet_name);
  91. if (!sheet)
  92. return nullptr;
  93. // Add it to the cache, and add a reference count so the cache will keep hold of it.
  94. instance->stylesheets[sheet_name] = sheet;
  95. return sheet;
  96. }
  97. SharedPtr<StyleSheetContainer> StyleSheetFactory::GetStyleSheetContainer(const StringList& sheets)
  98. {
  99. // Generate a unique key for these sheets
  100. String combined_key;
  101. for (size_t i = 0; i < sheets.size(); i++)
  102. {
  103. URL path(sheets[i]);
  104. combined_key += path.GetFileName();
  105. }
  106. // Look up the sheet definition in the cache.
  107. StyleSheets::iterator itr = instance->stylesheet_cache.find(combined_key);
  108. if (itr != instance->stylesheet_cache.end())
  109. {
  110. return (*itr).second;
  111. }
  112. // Load and combine the sheets.
  113. SharedPtr<StyleSheetContainer> sheet;
  114. for (size_t i = 0; i < sheets.size(); i++)
  115. {
  116. SharedPtr<StyleSheetContainer> sub_sheet = GetStyleSheetContainer(sheets[i]);
  117. if (sub_sheet)
  118. {
  119. if (sheet)
  120. {
  121. SharedPtr<StyleSheetContainer> new_sheet = sheet->CombineStyleSheetContainer(*sub_sheet);
  122. sheet = std::move(new_sheet);
  123. }
  124. else
  125. sheet = sub_sheet;
  126. }
  127. else
  128. Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", sheets[i].c_str());
  129. }
  130. if (!sheet)
  131. return nullptr;
  132. // Add to cache, and a reference to the sheet to hold it in the cache.
  133. instance->stylesheet_cache[combined_key] = sheet;
  134. return sheet;
  135. }
  136. // Clear the style sheet cache.
  137. void StyleSheetFactory::ClearStyleSheetCache()
  138. {
  139. instance->stylesheets.clear();
  140. instance->stylesheet_cache.clear();
  141. }
  142. // Returns one of the available node selectors.
  143. StructuralSelector StyleSheetFactory::GetSelector(const String& name)
  144. {
  145. SelectorMap::const_iterator it;
  146. const size_t parameter_start = name.find('(');
  147. if (parameter_start == String::npos)
  148. it = instance->selectors.find(name);
  149. else
  150. it = instance->selectors.find(name.substr(0, parameter_start));
  151. if (it == instance->selectors.end())
  152. return StructuralSelector(nullptr, 0, 0);
  153. // Parse the 'a' and 'b' values.
  154. int a = 1;
  155. int b = 0;
  156. const size_t parameter_end = name.find(')', parameter_start + 1);
  157. if (parameter_start != String::npos &&
  158. parameter_end != String::npos)
  159. {
  160. String parameters = StringUtilities::StripWhitespace(name.substr(parameter_start + 1, parameter_end - (parameter_start + 1)));
  161. // Check for 'even' or 'odd' first.
  162. if (parameters == "even")
  163. {
  164. a = 2;
  165. b = 0;
  166. }
  167. else if (parameters == "odd")
  168. {
  169. a = 2;
  170. b = 1;
  171. }
  172. else
  173. {
  174. // Alrighty; we've got an equation in the form of [[+/-]an][(+/-)b]. So, foist up, we split on 'n'.
  175. const size_t n_index = parameters.find('n');
  176. if (n_index == String::npos)
  177. {
  178. // The equation is 0n + b. So a = 0, and we only have to parse b.
  179. a = 0;
  180. b = atoi(parameters.c_str());
  181. }
  182. else
  183. {
  184. if (n_index == 0)
  185. a = 1;
  186. else
  187. {
  188. const String a_parameter = parameters.substr(0, n_index);
  189. if (StringUtilities::StripWhitespace(a_parameter) == "-")
  190. a = -1;
  191. else
  192. a = atoi(a_parameter.c_str());
  193. }
  194. size_t pm_index = parameters.find('+', n_index + 1);
  195. if (pm_index != String::npos)
  196. b = 1;
  197. else
  198. {
  199. pm_index = parameters.find('-', n_index + 1);
  200. if (pm_index != String::npos)
  201. b = -1;
  202. }
  203. if (n_index == parameters.size() - 1 || pm_index == String::npos)
  204. b = 0;
  205. else
  206. b = b * atoi(parameters.data() + pm_index + 1);
  207. }
  208. }
  209. }
  210. return StructuralSelector(it->second, a, b);
  211. }
  212. SharedPtr<StyleSheetContainer> StyleSheetFactory::LoadStyleSheetContainer(const String& sheet)
  213. {
  214. SharedPtr<StyleSheetContainer> new_style_sheet;
  215. // Open stream, construct new sheet and pass the stream into the sheet
  216. // TODO: Make this support ASYNC
  217. auto stream = MakeUnique<StreamFile>();
  218. if (stream->Open(sheet))
  219. {
  220. new_style_sheet = MakeShared<StyleSheetContainer>();
  221. if (!new_style_sheet->LoadStyleSheetContainer(stream.get()))
  222. {
  223. new_style_sheet = nullptr;
  224. }
  225. }
  226. return new_style_sheet;
  227. }
  228. } // namespace Rml