StyleSheetContainer.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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
  138. new_sheet = (new_sheet ? new_sheet.get() : first_sheet)->CombineStyleSheet(*media_block.stylesheet);
  139. }
  140. if (!first_sheet)
  141. {
  142. new_sheet.reset(new StyleSheet);
  143. first_sheet = new_sheet.get();
  144. }
  145. compiled_style_sheet = (new_sheet ? new_sheet.get() : first_sheet);
  146. combined_compiled_style_sheet = std::move(new_sheet);
  147. compiled_style_sheet->BuildNodeIndex();
  148. compiled_style_sheet->OptimizeNodeProperties();
  149. }
  150. active_media_block_indices = std::move(new_active_media_block_indices);
  151. return style_sheet_changed;
  152. }
  153. StyleSheet* StyleSheetContainer::GetCompiledStyleSheet()
  154. {
  155. return compiled_style_sheet;
  156. }
  157. SharedPtr<StyleSheetContainer> StyleSheetContainer::CombineStyleSheetContainer(const StyleSheetContainer& container) const
  158. {
  159. RMLUI_ZoneScoped;
  160. SharedPtr<StyleSheetContainer> new_sheet = MakeShared<StyleSheetContainer>();
  161. for (const MediaBlock& media_block : media_blocks)
  162. {
  163. new_sheet->media_blocks.emplace_back(media_block.properties, media_block.stylesheet->Clone());
  164. }
  165. new_sheet->MergeStyleSheetContainer(container);
  166. return new_sheet;
  167. }
  168. void StyleSheetContainer::MergeStyleSheetContainer(const StyleSheetContainer& other)
  169. {
  170. RMLUI_ZoneScoped;
  171. // Style sheet container must not be merged after it's been compiled. This will invalidate references to the compiled style sheet.
  172. RMLUI_ASSERT(!compiled_style_sheet);
  173. for (const MediaBlock& block_other : other.media_blocks)
  174. {
  175. media_blocks.emplace_back(block_other.properties, block_other.stylesheet->Clone());
  176. }
  177. }
  178. void StyleSheetContainer::OptimizeNodeProperties()
  179. {
  180. for (MediaBlock& block : media_blocks)
  181. block.stylesheet->OptimizeNodeProperties();
  182. }
  183. } // namespace Rml