StyleSheetFactory.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "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 = NULL;
  47. StyleSheetFactory::StyleSheetFactory()
  48. {
  49. RMLUI_ASSERT(instance == NULL);
  50. instance = this;
  51. }
  52. StyleSheetFactory::~StyleSheetFactory()
  53. {
  54. instance = NULL;
  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 != NULL)
  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. 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. (*itr).second->AddReference();
  89. return (*itr).second;
  90. }
  91. // Don't currently have the sheet, attempt to load it
  92. StyleSheet* sheet = instance->LoadStyleSheet(sheet_name);
  93. if (sheet == NULL)
  94. return NULL;
  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. sheet->AddReference();
  98. return sheet;
  99. }
  100. StyleSheet* StyleSheetFactory::GetStyleSheet(const StringList& sheets)
  101. {
  102. // Generate a unique key for these sheets
  103. String combined_key;
  104. for (size_t i = 0; i < sheets.size(); i++)
  105. {
  106. URL path(sheets[i]);
  107. combined_key += path.GetFileName();
  108. }
  109. // Look up the sheet definition in the cache.
  110. StyleSheets::iterator itr = instance->stylesheet_cache.find(combined_key);
  111. if (itr != instance->stylesheet_cache.end())
  112. {
  113. (*itr).second->AddReference();
  114. return (*itr).second;
  115. }
  116. // Load and combine the sheets.
  117. StyleSheet* sheet = NULL;
  118. for (size_t i = 0; i < sheets.size(); i++)
  119. {
  120. StyleSheet* sub_sheet = GetStyleSheet(sheets[i]);
  121. if (sub_sheet)
  122. {
  123. if (sheet)
  124. {
  125. StyleSheet* new_sheet = sheet->CombineStyleSheet(sub_sheet);
  126. sheet->RemoveReference();
  127. sub_sheet->RemoveReference();
  128. sheet = new_sheet;
  129. }
  130. else
  131. sheet = sub_sheet;
  132. }
  133. else
  134. Log::Message(Log::LT_ERROR, "Failed to load style sheet %s.", sheets[i].c_str());
  135. }
  136. if (sheet == NULL)
  137. return NULL;
  138. // Add to cache, and a reference to the sheet to hold it in the cache.
  139. instance->stylesheet_cache[combined_key] = sheet;
  140. sheet->AddReference();
  141. return sheet;
  142. }
  143. // Clear the style sheet cache.
  144. void StyleSheetFactory::ClearStyleSheetCache()
  145. {
  146. for (StyleSheets::iterator i = instance->stylesheets.begin(); i != instance->stylesheets.end(); ++i)
  147. (*i).second->RemoveReference();
  148. for (StyleSheets::iterator i = instance->stylesheet_cache.begin(); i != instance->stylesheet_cache.end(); ++i)
  149. (*i).second->RemoveReference();
  150. instance->stylesheets.clear();
  151. instance->stylesheet_cache.clear();
  152. }
  153. // Returns one of the available node selectors.
  154. StyleSheetNodeSelector* StyleSheetFactory::GetSelector(const String& name)
  155. {
  156. size_t index = name.find("(");
  157. SelectorMap::iterator i = instance->selectors.find(name.substr(0, index));
  158. if (i == instance->selectors.end())
  159. return NULL;
  160. return (*i).second;
  161. }
  162. StyleSheet* StyleSheetFactory::LoadStyleSheet(const String& sheet)
  163. {
  164. StyleSheet* new_style_sheet = NULL;
  165. // Open stream, construct new sheet and pass the stream into the sheet
  166. // TODO: Make this support ASYNC
  167. StreamFile* stream = new StreamFile();
  168. if (stream->Open(sheet))
  169. {
  170. new_style_sheet = new StyleSheet();
  171. if (!new_style_sheet->LoadStyleSheet(stream))
  172. {
  173. new_style_sheet->RemoveReference();
  174. new_style_sheet = NULL;
  175. }
  176. }
  177. stream->RemoveReference();
  178. return new_style_sheet;
  179. }
  180. }
  181. }