StyleSheetFactory.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. namespace Core {
  46. static StyleSheetFactory* instance = nullptr;
  47. StyleSheetFactory::StyleSheetFactory()
  48. {
  49. RMLUI_ASSERT(instance == nullptr);
  50. instance = this;
  51. }
  52. StyleSheetFactory::~StyleSheetFactory()
  53. {
  54. instance = nullptr;
  55. }
  56. bool StyleSheetFactory::Initialise()
  57. {
  58. new StyleSheetFactory();
  59. instance->selectors["nth-child"] = new StyleSheetNodeSelectorNthChild();
  60. instance->selectors["nth-last-child"] = new StyleSheetNodeSelectorNthLastChild();
  61. instance->selectors["nth-of-type"] = new StyleSheetNodeSelectorNthOfType();
  62. instance->selectors["nth-last-of-type"] = new StyleSheetNodeSelectorNthLastOfType();
  63. instance->selectors["first-child"] = new StyleSheetNodeSelectorFirstChild();
  64. instance->selectors["last-child"] = new StyleSheetNodeSelectorLastChild();
  65. instance->selectors["first-of-type"] = new StyleSheetNodeSelectorFirstOfType();
  66. instance->selectors["last-of-type"] = new StyleSheetNodeSelectorLastOfType();
  67. instance->selectors["only-child"] = new StyleSheetNodeSelectorOnlyChild();
  68. instance->selectors["only-of-type"] = new StyleSheetNodeSelectorOnlyOfType();
  69. instance->selectors["empty"] = new StyleSheetNodeSelectorEmpty();
  70. return true;
  71. }
  72. void StyleSheetFactory::Shutdown()
  73. {
  74. if (instance != nullptr)
  75. {
  76. ClearStyleSheetCache();
  77. for (SelectorMap::iterator i = instance->selectors.begin(); i != instance->selectors.end(); ++i)
  78. delete (*i).second;
  79. delete instance;
  80. }
  81. }
  82. SharedPtr<StyleSheet> StyleSheetFactory::GetStyleSheet(const String& sheet_name)
  83. {
  84. // Look up the sheet definition in the cache
  85. StyleSheets::iterator itr = instance->stylesheets.find(sheet_name);
  86. if (itr != instance->stylesheets.end())
  87. {
  88. return (*itr).second;
  89. }
  90. // Don't currently have the sheet, attempt to load it
  91. SharedPtr<StyleSheet> sheet = instance->LoadStyleSheet(sheet_name);
  92. if (!sheet)
  93. return nullptr;
  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 = 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. // Add to cache, and a reference to the sheet to hold it in the cache.
  134. instance->stylesheet_cache[combined_key] = sheet;
  135. return sheet;
  136. }
  137. // Clear the style sheet cache.
  138. void StyleSheetFactory::ClearStyleSheetCache()
  139. {
  140. instance->stylesheets.clear();
  141. instance->stylesheet_cache.clear();
  142. }
  143. // Returns one of the available node selectors.
  144. StructuralSelector StyleSheetFactory::GetSelector(const String& name)
  145. {
  146. size_t index = name.find('(');
  147. if(index == String::npos)
  148. return StructuralSelector(nullptr, 0, 0);
  149. auto it = instance->selectors.find(name.substr(0, index));
  150. if (it == instance->selectors.end())
  151. return StructuralSelector(nullptr, 0, 0);
  152. // Parse the 'a' and 'b' values.
  153. int a = 1;
  154. int b = 0;
  155. size_t parameter_start = name.find('(');
  156. size_t parameter_end = name.find(')');
  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. 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. 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<StyleSheet> StyleSheetFactory::LoadStyleSheet(const String& sheet)
  213. {
  214. SharedPtr<StyleSheet> 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 = std::make_unique<StreamFile>();
  218. if (stream->Open(sheet))
  219. {
  220. new_style_sheet = std::make_shared<StyleSheet>();
  221. if (!new_style_sheet->LoadStyleSheet(stream.get()))
  222. {
  223. new_style_sheet = nullptr;
  224. }
  225. }
  226. return new_style_sheet;
  227. }
  228. }
  229. }