StyleSheetContainer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/PropertyDictionary.h"
  29. #include "../../Include/RmlUi/Core/StyleSheetContainer.h"
  30. #include "../../Include/RmlUi/Core/StyleSheet.h"
  31. #include "ComputeProperty.h"
  32. #include "StyleSheetParser.h"
  33. #include "Utilities.h"
  34. namespace Rml {
  35. StyleSheetContainer::StyleSheetContainer()
  36. {
  37. }
  38. StyleSheetContainer::~StyleSheetContainer()
  39. {
  40. }
  41. bool StyleSheetContainer::LoadStyleSheetContainer(Stream* stream, int begin_line_number)
  42. {
  43. StyleSheetParser parser;
  44. int rule_count = parser.Parse(media_blocks, stream, begin_line_number);
  45. return rule_count >= 0;
  46. }
  47. StyleSheet* StyleSheetContainer::GetCompiledStyleSheet(float dp_ratio, Vector2f vp_dimensions)
  48. {
  49. if(compiled_style_sheet && vp_dimensions == current_dimensions && dp_ratio == current_density_ratio)
  50. return compiled_style_sheet.get();
  51. UniquePtr<StyleSheet> new_sheet = UniquePtr<StyleSheet>(new StyleSheet());
  52. float font_size = DefaultComputedValues.font_size;
  53. for(auto const& media_block : media_blocks)
  54. {
  55. bool all_match = true;
  56. for(auto const& property : media_block.properties.GetProperties())
  57. {
  58. switch(static_cast<MediaQueryId>(property.first))
  59. {
  60. case MediaQueryId::Width:
  61. if(vp_dimensions.x != ComputeLength(&property.second, font_size, font_size, dp_ratio, vp_dimensions))
  62. all_match = false;
  63. break;
  64. case MediaQueryId::MinWidth:
  65. if(vp_dimensions.x < ComputeLength(&property.second, font_size, font_size, dp_ratio, vp_dimensions))
  66. all_match = false;
  67. break;
  68. case MediaQueryId::MaxWidth:
  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::Height:
  73. if(vp_dimensions.y != ComputeLength(&property.second, font_size, font_size, dp_ratio, vp_dimensions))
  74. all_match = false;
  75. break;
  76. case MediaQueryId::MinHeight:
  77. if(vp_dimensions.y < ComputeLength(&property.second, font_size, font_size, dp_ratio, vp_dimensions))
  78. all_match = false;
  79. break;
  80. case MediaQueryId::MaxHeight:
  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::AspectRatio:
  85. if((vp_dimensions.x / vp_dimensions.y) != property.second.Get<float>())
  86. all_match = false;
  87. break;
  88. case MediaQueryId::MinAspectRatio:
  89. if((vp_dimensions.x / vp_dimensions.y) < property.second.Get<float>())
  90. all_match = false;
  91. break;
  92. case MediaQueryId::MaxAspectRatio:
  93. if((vp_dimensions.x / vp_dimensions.y) > property.second.Get<float>())
  94. all_match = false;
  95. break;
  96. case MediaQueryId::Resolution:
  97. if(dp_ratio != property.second.Get<float>())
  98. all_match = false;
  99. break;
  100. case MediaQueryId::MinResolution:
  101. if(dp_ratio < property.second.Get<float>())
  102. all_match = false;
  103. break;
  104. case MediaQueryId::MaxResolution:
  105. if(dp_ratio > property.second.Get<float>())
  106. all_match = false;
  107. break;
  108. case MediaQueryId::Orientation:
  109. // Landscape (x > y) = 0
  110. // Portrait (x <= y) = 1
  111. if((vp_dimensions.x <= vp_dimensions.y) != property.second.Get<bool>())
  112. all_match = false;
  113. break;
  114. // Invalid properties
  115. case MediaQueryId::Invalid:
  116. case MediaQueryId::NumDefinedIds:
  117. case MediaQueryId::MaxNumIds:
  118. break;
  119. }
  120. if(!all_match)
  121. break;
  122. }
  123. if(all_match)
  124. {
  125. new_sheet = new_sheet->CombineStyleSheet(*media_block.stylesheet);
  126. }
  127. }
  128. new_sheet->BuildNodeIndex();
  129. new_sheet->OptimizeNodeProperties();
  130. compiled_style_sheet = std::move(new_sheet);
  131. current_dimensions = vp_dimensions;
  132. current_density_ratio = dp_ratio;
  133. return compiled_style_sheet.get();
  134. }
  135. /// Combines this style sheet container with another one, producing a new sheet container.
  136. SharedPtr<StyleSheetContainer> StyleSheetContainer::CombineStyleSheetContainer(const StyleSheetContainer& container) const
  137. {
  138. SharedPtr<StyleSheetContainer> new_sheet = MakeShared<StyleSheetContainer>();
  139. for(auto const& pair : media_blocks)
  140. {
  141. PropertyDictionary dict;
  142. dict.Import(pair.properties);
  143. new_sheet->media_blocks.emplace_back(dict, pair.stylesheet->Clone());
  144. }
  145. for(auto const& pair : container.media_blocks)
  146. {
  147. bool block_found = false;
  148. for(auto& media_block : new_sheet->media_blocks)
  149. {
  150. if(pair.properties.GetProperties() == media_block.properties.GetProperties())
  151. {
  152. media_block.stylesheet = media_block.stylesheet->CombineStyleSheet(*pair.stylesheet);
  153. block_found = true;
  154. break;
  155. }
  156. }
  157. if (!block_found)
  158. {
  159. PropertyDictionary dict;
  160. dict.Import(pair.properties);
  161. new_sheet->media_blocks.emplace_back(dict, pair.stylesheet->Clone());
  162. }
  163. }
  164. return new_sheet;
  165. }
  166. } // namespace Rml