StyleSheetFactory.cpp 5.6 KB

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