StyleSheetContainer.cpp 7.5 KB

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