StyleSheetContainer.cpp 8.1 KB

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