StyleSheetFactory.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 UniquePtr<StyleSheetFactory> instance;
  46. StyleSheetFactory::StyleSheetFactory()
  47. {}
  48. StyleSheetFactory::~StyleSheetFactory()
  49. {}
  50. bool StyleSheetFactory::Initialise()
  51. {
  52. RMLUI_ASSERT(instance == nullptr);
  53. instance = UniquePtr<StyleSheetFactory>(new StyleSheetFactory);
  54. instance->selectors["nth-child"] = MakeUnique<StyleSheetNodeSelectorNthChild>();
  55. instance->selectors["nth-last-child"] = MakeUnique<StyleSheetNodeSelectorNthLastChild>();
  56. instance->selectors["nth-of-type"] = MakeUnique<StyleSheetNodeSelectorNthOfType>();
  57. instance->selectors["nth-last-of-type"] = MakeUnique<StyleSheetNodeSelectorNthLastOfType>();
  58. instance->selectors["first-child"] = MakeUnique<StyleSheetNodeSelectorFirstChild>();
  59. instance->selectors["last-child"] = MakeUnique<StyleSheetNodeSelectorLastChild>();
  60. instance->selectors["first-of-type"] = MakeUnique<StyleSheetNodeSelectorFirstOfType>();
  61. instance->selectors["last-of-type"] = MakeUnique<StyleSheetNodeSelectorLastOfType>();
  62. instance->selectors["only-child"] = MakeUnique<StyleSheetNodeSelectorOnlyChild>();
  63. instance->selectors["only-of-type"] = MakeUnique<StyleSheetNodeSelectorOnlyOfType>();
  64. instance->selectors["empty"] = MakeUnique<StyleSheetNodeSelectorEmpty>();
  65. return true;
  66. }
  67. void StyleSheetFactory::Shutdown()
  68. {
  69. instance.reset();
  70. }
  71. const StyleSheetContainer* StyleSheetFactory::GetStyleSheetContainer(const String& sheet_name)
  72. {
  73. // Look up the sheet definition in the cache
  74. auto it = instance->stylesheets.find(sheet_name);
  75. if (it != instance->stylesheets.end())
  76. return it->second.get();
  77. // Don't currently have the sheet, attempt to load it
  78. UniquePtr<const StyleSheetContainer> sheet = instance->LoadStyleSheetContainer(sheet_name);
  79. if (!sheet)
  80. return nullptr;
  81. const StyleSheetContainer* result = sheet.get();
  82. // Add it to the cache.
  83. instance->stylesheets[sheet_name] = std::move(sheet);
  84. return result;
  85. }
  86. // Clear the style sheet cache.
  87. void StyleSheetFactory::ClearStyleSheetCache()
  88. {
  89. instance->stylesheets.clear();
  90. }
  91. // Returns one of the available node selectors.
  92. StructuralSelector StyleSheetFactory::GetSelector(const String& name)
  93. {
  94. SelectorMap::const_iterator it;
  95. const size_t parameter_start = name.find('(');
  96. if (parameter_start == String::npos)
  97. it = instance->selectors.find(name);
  98. else
  99. it = instance->selectors.find(name.substr(0, parameter_start));
  100. if (it == instance->selectors.end())
  101. return StructuralSelector(nullptr, 0, 0);
  102. // Parse the 'a' and 'b' values.
  103. int a = 1;
  104. int b = 0;
  105. const size_t parameter_end = name.find(')', parameter_start + 1);
  106. if (parameter_start != String::npos &&
  107. parameter_end != String::npos)
  108. {
  109. String parameters = StringUtilities::StripWhitespace(name.substr(parameter_start + 1, parameter_end - (parameter_start + 1)));
  110. // Check for 'even' or 'odd' first.
  111. if (parameters == "even")
  112. {
  113. a = 2;
  114. b = 0;
  115. }
  116. else if (parameters == "odd")
  117. {
  118. a = 2;
  119. b = 1;
  120. }
  121. else
  122. {
  123. // Alrighty; we've got an equation in the form of [[+/-]an][(+/-)b]. So, foist up, we split on 'n'.
  124. const size_t n_index = parameters.find('n');
  125. if (n_index == String::npos)
  126. {
  127. // The equation is 0n + b. So a = 0, and we only have to parse b.
  128. a = 0;
  129. b = atoi(parameters.c_str());
  130. }
  131. else
  132. {
  133. if (n_index == 0)
  134. a = 1;
  135. else
  136. {
  137. const String a_parameter = parameters.substr(0, n_index);
  138. if (StringUtilities::StripWhitespace(a_parameter) == "-")
  139. a = -1;
  140. else
  141. a = atoi(a_parameter.c_str());
  142. }
  143. size_t pm_index = parameters.find('+', n_index + 1);
  144. if (pm_index != String::npos)
  145. b = 1;
  146. else
  147. {
  148. pm_index = parameters.find('-', n_index + 1);
  149. if (pm_index != String::npos)
  150. b = -1;
  151. }
  152. if (n_index == parameters.size() - 1 || pm_index == String::npos)
  153. b = 0;
  154. else
  155. b = b * atoi(parameters.data() + pm_index + 1);
  156. }
  157. }
  158. }
  159. return StructuralSelector(it->second.get(), a, b);
  160. }
  161. UniquePtr<const StyleSheetContainer> StyleSheetFactory::LoadStyleSheetContainer(const String& sheet)
  162. {
  163. UniquePtr<StyleSheetContainer> new_style_sheet;
  164. // Open stream, construct new sheet and pass the stream into the sheet
  165. auto stream = MakeUnique<StreamFile>();
  166. if (stream->Open(sheet))
  167. {
  168. new_style_sheet = MakeUnique<StyleSheetContainer>();
  169. if (!new_style_sheet->LoadStyleSheetContainer(stream.get()))
  170. {
  171. new_style_sheet.reset();
  172. }
  173. }
  174. return new_style_sheet;
  175. }
  176. } // namespace Rml