StyleSheetContainer.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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-2023 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 "../../Include/RmlUi/Core/StyleSheetContainer.h"
  29. #include "../../Include/RmlUi/Core/ComputedValues.h"
  30. #include "../../Include/RmlUi/Core/Context.h"
  31. #include "../../Include/RmlUi/Core/Profiling.h"
  32. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  33. #include "../../Include/RmlUi/Core/StyleSheet.h"
  34. #include "../../Include/RmlUi/Core/Utilities.h"
  35. #include "ComputeProperty.h"
  36. #include "StyleSheetParser.h"
  37. namespace Rml {
  38. StyleSheetContainer::StyleSheetContainer() {}
  39. StyleSheetContainer::~StyleSheetContainer() {}
  40. bool StyleSheetContainer::LoadStyleSheetContainer(Stream* stream, int begin_line_number)
  41. {
  42. StyleSheetParser parser;
  43. bool result = parser.Parse(media_blocks, stream, begin_line_number);
  44. return result;
  45. }
  46. bool StyleSheetContainer::UpdateCompiledStyleSheet(const Context* context)
  47. {
  48. RMLUI_ZoneScoped;
  49. const float dp_ratio = context->GetDensityIndependentPixelRatio();
  50. const Vector2i vp_dimensions_i(context->GetDimensions());
  51. const Vector2f vp_dimensions(vp_dimensions_i);
  52. Vector<int> new_active_media_block_indices;
  53. const float font_size = DefaultComputedValues().font_size();
  54. for (int media_block_index = 0; media_block_index < (int)media_blocks.size(); media_block_index++)
  55. {
  56. const MediaBlock& media_block = media_blocks[media_block_index];
  57. bool all_match = true;
  58. bool expected_match_value = media_block.modifier == MediaQueryModifier::Not ? false : true;
  59. for (const auto& property : media_block.properties.GetProperties())
  60. {
  61. const MediaQueryId id = static_cast<MediaQueryId>(property.first);
  62. Vector2i ratio;
  63. switch (id)
  64. {
  65. case MediaQueryId::Width:
  66. if (vp_dimensions.x != ComputeLength(property.second.GetNumericValue(), font_size, font_size, dp_ratio, vp_dimensions))
  67. all_match = false;
  68. break;
  69. case MediaQueryId::MinWidth:
  70. if (vp_dimensions.x < ComputeLength(property.second.GetNumericValue(), font_size, font_size, dp_ratio, vp_dimensions))
  71. all_match = false;
  72. break;
  73. case MediaQueryId::MaxWidth:
  74. if (vp_dimensions.x > ComputeLength(property.second.GetNumericValue(), font_size, font_size, dp_ratio, vp_dimensions))
  75. all_match = false;
  76. break;
  77. case MediaQueryId::Height:
  78. if (vp_dimensions.y != ComputeLength(property.second.GetNumericValue(), font_size, font_size, dp_ratio, vp_dimensions))
  79. all_match = false;
  80. break;
  81. case MediaQueryId::MinHeight:
  82. if (vp_dimensions.y < ComputeLength(property.second.GetNumericValue(), font_size, font_size, dp_ratio, vp_dimensions))
  83. all_match = false;
  84. break;
  85. case MediaQueryId::MaxHeight:
  86. if (vp_dimensions.y > ComputeLength(property.second.GetNumericValue(), font_size, font_size, dp_ratio, vp_dimensions))
  87. all_match = false;
  88. break;
  89. case MediaQueryId::AspectRatio:
  90. ratio = Vector2i(property.second.Get<Vector2f>());
  91. if (vp_dimensions_i.x * ratio.y != vp_dimensions_i.y * ratio.x)
  92. all_match = false;
  93. break;
  94. case MediaQueryId::MinAspectRatio:
  95. ratio = Vector2i(property.second.Get<Vector2f>());
  96. if (vp_dimensions_i.x * ratio.y < vp_dimensions_i.y * ratio.x)
  97. all_match = false;
  98. break;
  99. case MediaQueryId::MaxAspectRatio:
  100. ratio = Vector2i(property.second.Get<Vector2f>());
  101. if (vp_dimensions_i.x * ratio.y > vp_dimensions_i.y * ratio.x)
  102. all_match = false;
  103. break;
  104. case MediaQueryId::Resolution:
  105. if (dp_ratio != property.second.Get<float>())
  106. all_match = false;
  107. break;
  108. case MediaQueryId::MinResolution:
  109. if (dp_ratio < property.second.Get<float>())
  110. all_match = false;
  111. break;
  112. case MediaQueryId::MaxResolution:
  113. if (dp_ratio > property.second.Get<float>())
  114. all_match = false;
  115. break;
  116. case MediaQueryId::Orientation:
  117. // Landscape (x > y) = 0
  118. // Portrait (x <= y) = 1
  119. if ((vp_dimensions.x <= vp_dimensions.y) != property.second.Get<bool>())
  120. all_match = false;
  121. break;
  122. case MediaQueryId::Theme:
  123. if (!context->IsThemeActive(property.second.Get<String>()))
  124. all_match = false;
  125. break;
  126. // Invalid properties
  127. case MediaQueryId::Invalid:
  128. case MediaQueryId::NumDefinedIds: break;
  129. }
  130. if (all_match != expected_match_value)
  131. break;
  132. }
  133. if (all_match == expected_match_value)
  134. new_active_media_block_indices.push_back(media_block_index);
  135. }
  136. const bool style_sheet_changed = (new_active_media_block_indices != active_media_block_indices || !compiled_style_sheet);
  137. if (style_sheet_changed)
  138. {
  139. StyleSheet* first_sheet = nullptr;
  140. UniquePtr<StyleSheet> new_sheet;
  141. for (int index : new_active_media_block_indices)
  142. {
  143. MediaBlock& media_block = media_blocks[index];
  144. if (!first_sheet)
  145. first_sheet = media_block.stylesheet.get();
  146. else if (!new_sheet)
  147. new_sheet = first_sheet->CombineStyleSheet(*media_block.stylesheet);
  148. else
  149. new_sheet->MergeStyleSheet(*media_block.stylesheet);
  150. }
  151. if (!first_sheet)
  152. {
  153. new_sheet.reset(new StyleSheet);
  154. first_sheet = new_sheet.get();
  155. }
  156. compiled_style_sheet = (new_sheet ? new_sheet.get() : first_sheet);
  157. combined_compiled_style_sheet = std::move(new_sheet);
  158. compiled_style_sheet->BuildNodeIndex();
  159. }
  160. active_media_block_indices = std::move(new_active_media_block_indices);
  161. return style_sheet_changed;
  162. }
  163. StyleSheet* StyleSheetContainer::GetCompiledStyleSheet()
  164. {
  165. return compiled_style_sheet;
  166. }
  167. SharedPtr<StyleSheetContainer> StyleSheetContainer::CombineStyleSheetContainer(const StyleSheetContainer& container) const
  168. {
  169. RMLUI_ZoneScoped;
  170. SharedPtr<StyleSheetContainer> new_sheet = MakeShared<StyleSheetContainer>();
  171. for (const MediaBlock& media_block : media_blocks)
  172. {
  173. new_sheet->media_blocks.emplace_back(media_block.properties, media_block.stylesheet, media_block.modifier);
  174. }
  175. new_sheet->MergeStyleSheetContainer(container);
  176. return new_sheet;
  177. }
  178. void StyleSheetContainer::MergeStyleSheetContainer(const StyleSheetContainer& other)
  179. {
  180. RMLUI_ZoneScoped;
  181. // Style sheet container must not be merged after it's been compiled. This will invalidate references to the compiled style sheet.
  182. RMLUI_ASSERT(!compiled_style_sheet);
  183. auto it_other_begin = other.media_blocks.begin();
  184. #if 0
  185. // If the last block here has the same media requirements as the first block in other, we can safely merge them
  186. // while retaining correct specificity of all properties. This is essentially an optimization to avoid more
  187. // style sheet merging later on.
  188. if (!media_blocks.empty() && !other.media_blocks.empty())
  189. {
  190. MediaBlock& block_local = media_blocks.back();
  191. const MediaBlock& block_other = other.media_blocks.front();
  192. if (block_local.properties.GetProperties() == block_other.properties.GetProperties())
  193. {
  194. // Now we can safely merge the two style sheets.
  195. block_local.stylesheet = block_local.stylesheet->CombineStyleSheet(*block_other.stylesheet);
  196. // And we need to skip the first media block in the 'other' style sheet, since we merged it just now.
  197. ++it_other_begin;
  198. }
  199. }
  200. #endif
  201. // Add all the other blocks into ours.
  202. for (auto it = it_other_begin; it != other.media_blocks.end(); ++it)
  203. {
  204. const MediaBlock& block_other = *it;
  205. media_blocks.emplace_back(block_other.properties, block_other.stylesheet, block_other.modifier);
  206. }
  207. }
  208. } // namespace Rml