StyleSheetContainer.cpp 8.0 KB

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