StyleSheetContainer.cpp 8.2 KB

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