StyleSheetContainer.cpp 7.8 KB

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