StyleSheetFactory.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. 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. StyleSheetNodeSelector* StyleSheetFactory::GetSelector(const String& name)
  145. {
  146. size_t index = name.find("(");
  147. SelectorMap::iterator i = instance->selectors.find(name.substr(0, index));
  148. if (i == instance->selectors.end())
  149. return NULL;
  150. return (*i).second;
  151. }
  152. SharedPtr<StyleSheet> StyleSheetFactory::LoadStyleSheet(const String& sheet)
  153. {
  154. SharedPtr<StyleSheet> new_style_sheet;
  155. // Open stream, construct new sheet and pass the stream into the sheet
  156. // TODO: Make this support ASYNC
  157. StreamFile* stream = new StreamFile();
  158. if (stream->Open(sheet))
  159. {
  160. new_style_sheet = std::make_shared<StyleSheet>();
  161. if (!new_style_sheet->LoadStyleSheet(stream))
  162. {
  163. new_style_sheet = nullptr;
  164. }
  165. }
  166. stream->RemoveReference();
  167. return new_style_sheet;
  168. }
  169. }
  170. }