StyleSheetFactory.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "precompiled.h"
  29. #include "StyleSheetFactory.h"
  30. #include "../../Include/RmlUi/Core/StyleSheet.h"
  31. #include "StyleSheetNode.h"
  32. #include "StreamFile.h"
  33. #include "StyleSheetNodeSelectorNthChild.h"
  34. #include "StyleSheetNodeSelectorNthLastChild.h"
  35. #include "StyleSheetNodeSelectorNthOfType.h"
  36. #include "StyleSheetNodeSelectorNthLastOfType.h"
  37. #include "StyleSheetNodeSelectorFirstChild.h"
  38. #include "StyleSheetNodeSelectorLastChild.h"
  39. #include "StyleSheetNodeSelectorFirstOfType.h"
  40. #include "StyleSheetNodeSelectorLastOfType.h"
  41. #include "StyleSheetNodeSelectorOnlyChild.h"
  42. #include "StyleSheetNodeSelectorOnlyOfType.h"
  43. #include "StyleSheetNodeSelectorEmpty.h"
  44. #include "../../Include/RmlUi/Core/Log.h"
  45. namespace Rml {
  46. namespace Core {
  47. static StyleSheetFactory* instance = nullptr;
  48. StyleSheetFactory::StyleSheetFactory()
  49. {
  50. RMLUI_ASSERT(instance == nullptr);
  51. instance = this;
  52. }
  53. StyleSheetFactory::~StyleSheetFactory()
  54. {
  55. instance = nullptr;
  56. }
  57. bool StyleSheetFactory::Initialise()
  58. {
  59. new StyleSheetFactory();
  60. instance->selectors["nth-child"] = new StyleSheetNodeSelectorNthChild();
  61. instance->selectors["nth-last-child"] = new StyleSheetNodeSelectorNthLastChild();
  62. instance->selectors["nth-of-type"] = new StyleSheetNodeSelectorNthOfType();
  63. instance->selectors["nth-last-of-type"] = new StyleSheetNodeSelectorNthLastOfType();
  64. instance->selectors["first-child"] = new StyleSheetNodeSelectorFirstChild();
  65. instance->selectors["last-child"] = new StyleSheetNodeSelectorLastChild();
  66. instance->selectors["first-of-type"] = new StyleSheetNodeSelectorFirstOfType();
  67. instance->selectors["last-of-type"] = new StyleSheetNodeSelectorLastOfType();
  68. instance->selectors["only-child"] = new StyleSheetNodeSelectorOnlyChild();
  69. instance->selectors["only-of-type"] = new StyleSheetNodeSelectorOnlyOfType();
  70. instance->selectors["empty"] = new StyleSheetNodeSelectorEmpty();
  71. return true;
  72. }
  73. void StyleSheetFactory::Shutdown()
  74. {
  75. if (instance != nullptr)
  76. {
  77. ClearStyleSheetCache();
  78. for (SelectorMap::iterator i = instance->selectors.begin(); i != instance->selectors.end(); ++i)
  79. delete (*i).second;
  80. delete instance;
  81. }
  82. }
  83. SharedPtr<StyleSheet> StyleSheetFactory::GetStyleSheet(const String& sheet_name)
  84. {
  85. // Look up the sheet definition in the cache
  86. StyleSheets::iterator itr = instance->stylesheets.find(sheet_name);
  87. if (itr != instance->stylesheets.end())
  88. {
  89. return (*itr).second;
  90. }
  91. // Don't currently have the sheet, attempt to load it
  92. SharedPtr<StyleSheet> sheet = instance->LoadStyleSheet(sheet_name);
  93. if (!sheet)
  94. return nullptr;
  95. // Add it to the cache, and add a reference count so the cache will keep hold of it.
  96. instance->stylesheets[sheet_name] = sheet;
  97. return sheet;
  98. }
  99. SharedPtr<StyleSheet> StyleSheetFactory::GetStyleSheet(const StringList& sheets)
  100. {
  101. // Generate a unique key for these sheets
  102. String combined_key;
  103. for (size_t i = 0; i < sheets.size(); i++)
  104. {
  105. URL path(sheets[i]);
  106. combined_key += path.GetFileName();
  107. }
  108. // Look up the sheet definition in the cache.
  109. StyleSheets::iterator itr = instance->stylesheet_cache.find(combined_key);
  110. if (itr != instance->stylesheet_cache.end())
  111. {
  112. return (*itr).second;
  113. }
  114. // Load and combine the sheets.
  115. SharedPtr<StyleSheet> sheet;
  116. for (size_t i = 0; i < sheets.size(); i++)
  117. {
  118. SharedPtr<StyleSheet> sub_sheet = GetStyleSheet(sheets[i]);
  119. if (sub_sheet)
  120. {
  121. if (sheet)
  122. {
  123. SharedPtr<StyleSheet> new_sheet = sheet->CombineStyleSheet(*sub_sheet);
  124. sheet = new_sheet;
  125. }
  126. else
  127. sheet = sub_sheet;
  128. }
  129. else
  130. Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", sheets[i].c_str());
  131. }
  132. if (!sheet)
  133. return nullptr;
  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. NodeSelector StyleSheetFactory::GetSelector(const String& name)
  146. {
  147. size_t index = name.find("(");
  148. auto it = instance->selectors.find(name.substr(0, index));
  149. if (it == instance->selectors.end())
  150. return NodeSelector(nullptr, 0, 0);
  151. // Parse the 'a' and 'b' values.
  152. int a = 1;
  153. int b = 0;
  154. size_t parameter_start = name.find("(");
  155. size_t parameter_end = name.find(")");
  156. if (parameter_start != String::npos &&
  157. parameter_end != String::npos)
  158. {
  159. String parameters = name.substr(parameter_start + 1, parameter_end - (parameter_start + 1));
  160. // Check for 'even' or 'odd' first.
  161. if (parameters == "even")
  162. {
  163. a = 2;
  164. b = 0;
  165. }
  166. else if (parameters == "odd")
  167. {
  168. a = 2;
  169. b = 1;
  170. }
  171. else
  172. {
  173. // Alrighty; we've got an equation in the form of [[+/-]an][(+/-)b]. So, foist up, we split on 'n'.
  174. size_t n_index = parameters.find('n');
  175. if (n_index != String::npos)
  176. {
  177. // The equation is 0n + b. So a = 0, and we only have to parse b.
  178. a = 0;
  179. b = atoi(parameters.c_str());
  180. }
  181. else
  182. {
  183. if (n_index == 0)
  184. a = 1;
  185. else
  186. {
  187. String a_parameter = parameters.substr(0, n_index);
  188. if (StringUtilities::StripWhitespace(a_parameter) == "-")
  189. a = -1;
  190. else
  191. a = atoi(a_parameter.c_str());
  192. }
  193. if (n_index == parameters.size() - 1)
  194. b = 0;
  195. else
  196. b = atoi(parameters.substr(n_index + 1).c_str());
  197. }
  198. }
  199. }
  200. return NodeSelector(it->second, a, b);
  201. }
  202. SharedPtr<StyleSheet> StyleSheetFactory::LoadStyleSheet(const String& sheet)
  203. {
  204. SharedPtr<StyleSheet> new_style_sheet;
  205. // Open stream, construct new sheet and pass the stream into the sheet
  206. // TODO: Make this support ASYNC
  207. auto stream = std::make_unique<StreamFile>();
  208. if (stream->Open(sheet))
  209. {
  210. new_style_sheet = std::make_shared<StyleSheet>();
  211. if (!new_style_sheet->LoadStyleSheet(stream.get()))
  212. {
  213. new_style_sheet = nullptr;
  214. }
  215. }
  216. return new_style_sheet;
  217. }
  218. }
  219. }