Length.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <AK/Variant.h>
  10. #include <LibGfx/Font.h>
  11. #include <LibGfx/Rect.h>
  12. #include <LibWeb/CSS/Length.h>
  13. #include <LibWeb/CSS/Percentage.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/HTML/BrowsingContext.h>
  16. #include <LibWeb/HTML/HTMLHtmlElement.h>
  17. namespace Web::CSS {
  18. Length::Length() = default;
  19. Length::Length(int value, Type type)
  20. : m_type(type)
  21. , m_value(value)
  22. {
  23. }
  24. Length::Length(float value, Type type)
  25. : m_type(type)
  26. , m_value(value)
  27. {
  28. }
  29. Length Length::make_auto()
  30. {
  31. return Length(0, Type::Auto);
  32. }
  33. Length Length::make_px(float value)
  34. {
  35. return Length(value, Type::Px);
  36. }
  37. Length Length::percentage_of(Percentage const& percentage) const
  38. {
  39. if (is_undefined_or_auto()) {
  40. dbgln("Attempting to get percentage of an undefined or auto length, this seems wrong? But for now we just return the original length.");
  41. return *this;
  42. }
  43. return Length { percentage.as_fraction() * raw_value(), m_type };
  44. }
  45. Length Length::resolved(Length const& fallback_for_undefined, Layout::Node const& layout_node) const
  46. {
  47. if (is_undefined())
  48. return fallback_for_undefined;
  49. if (is_calculated())
  50. return Length(resolve_calculated_value(layout_node), Type::Px);
  51. if (is_relative())
  52. return make_px(to_px(layout_node));
  53. return *this;
  54. }
  55. Length Length::resolved_or_auto(Layout::Node const& layout_node) const
  56. {
  57. return resolved(make_auto(), layout_node);
  58. }
  59. Length Length::resolved_or_zero(Layout::Node const& layout_node) const
  60. {
  61. return resolved(make_px(0), layout_node);
  62. }
  63. void Length::set_calculated_style(CalculatedStyleValue* value)
  64. {
  65. m_calculated_style = value;
  66. }
  67. float Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const
  68. {
  69. switch (m_type) {
  70. case Type::Ex:
  71. return m_value * font_metrics.x_height;
  72. case Type::Em:
  73. return m_value * font_metrics.size;
  74. case Type::Ch:
  75. // FIXME: Use layout_node.font().glyph_height() when writing-mode is not horizontal-tb (it has to be implemented first)
  76. return m_value * (font_metrics.glyph_width + font_metrics.glyph_spacing);
  77. case Type::Rem:
  78. return m_value * root_font_size;
  79. case Type::Vw:
  80. return viewport_rect.width() * (m_value / 100);
  81. case Type::Vh:
  82. return viewport_rect.height() * (m_value / 100);
  83. case Type::Vmin:
  84. return min(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  85. case Type::Vmax:
  86. return max(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  87. default:
  88. VERIFY_NOT_REACHED();
  89. }
  90. }
  91. float Length::to_px(Layout::Node const& layout_node) const
  92. {
  93. if (!layout_node.document().browsing_context())
  94. return 0;
  95. auto viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  96. auto* root_element = layout_node.document().document_element();
  97. if (!root_element || !root_element->layout_node())
  98. return 0;
  99. return to_px(viewport_rect, layout_node.font().metrics('M'), root_element->layout_node()->font().presentation_size());
  100. }
  101. static float resolve_calc_value(CalculatedStyleValue::CalcValue const& calc_value, Layout::Node const& layout_node);
  102. static float resolve_calc_number_value(CalculatedStyleValue::CalcNumberValue const&);
  103. static float resolve_calc_product(NonnullOwnPtr<CalculatedStyleValue::CalcProduct> const& calc_product, Layout::Node const& layout_node);
  104. static float resolve_calc_sum(NonnullOwnPtr<CalculatedStyleValue::CalcSum> const& calc_sum, Layout::Node const& layout_node);
  105. static float resolve_calc_number_sum(NonnullOwnPtr<CalculatedStyleValue::CalcNumberSum> const&);
  106. static float resolve_calc_number_product(NonnullOwnPtr<CalculatedStyleValue::CalcNumberProduct> const&);
  107. float Length::resolve_calculated_value(Layout::Node const& layout_node) const
  108. {
  109. if (!m_calculated_style)
  110. return 0.0f;
  111. auto& expression = m_calculated_style->expression();
  112. auto length = resolve_calc_sum(expression, layout_node);
  113. return length;
  114. };
  115. static float resolve_calc_value(CalculatedStyleValue::CalcValue const& calc_value, Layout::Node const& layout_node)
  116. {
  117. return calc_value.visit(
  118. [](float value) { return value; },
  119. [&](Length const& length) {
  120. return length.resolved_or_zero(layout_node).to_px(layout_node);
  121. },
  122. [&](NonnullOwnPtr<CalculatedStyleValue::CalcSum> const& calc_sum) {
  123. return resolve_calc_sum(calc_sum, layout_node);
  124. },
  125. [](auto&) {
  126. VERIFY_NOT_REACHED();
  127. return 0.0f;
  128. });
  129. }
  130. static float resolve_calc_number_product(NonnullOwnPtr<CalculatedStyleValue::CalcNumberProduct> const& calc_number_product)
  131. {
  132. auto value = resolve_calc_number_value(calc_number_product->first_calc_number_value);
  133. for (auto& additional_number_value : calc_number_product->zero_or_more_additional_calc_number_values) {
  134. auto additional_value = resolve_calc_number_value(additional_number_value.value);
  135. if (additional_number_value.op == CalculatedStyleValue::CalcNumberProductPartWithOperator::Multiply)
  136. value *= additional_value;
  137. else if (additional_number_value.op == CalculatedStyleValue::CalcNumberProductPartWithOperator::Divide)
  138. value /= additional_value;
  139. else
  140. VERIFY_NOT_REACHED();
  141. }
  142. return value;
  143. }
  144. static float resolve_calc_number_sum(NonnullOwnPtr<CalculatedStyleValue::CalcNumberSum> const& calc_number_sum)
  145. {
  146. auto value = resolve_calc_number_product(calc_number_sum->first_calc_number_product);
  147. for (auto& additional_product : calc_number_sum->zero_or_more_additional_calc_number_products) {
  148. auto additional_value = resolve_calc_number_product(additional_product.calc_number_product);
  149. if (additional_product.op == CSS::CalculatedStyleValue::CalcNumberSumPartWithOperator::Add)
  150. value += additional_value;
  151. else if (additional_product.op == CSS::CalculatedStyleValue::CalcNumberSumPartWithOperator::Subtract)
  152. value -= additional_value;
  153. else
  154. VERIFY_NOT_REACHED();
  155. }
  156. return value;
  157. }
  158. static float resolve_calc_number_value(CalculatedStyleValue::CalcNumberValue const& number_value)
  159. {
  160. return number_value.visit(
  161. [](float number) { return number; },
  162. [](NonnullOwnPtr<CalculatedStyleValue::CalcNumberSum> const& calc_number_sum) {
  163. return resolve_calc_number_sum(calc_number_sum);
  164. });
  165. }
  166. static float resolve_calc_product(NonnullOwnPtr<CalculatedStyleValue::CalcProduct> const& calc_product, Layout::Node const& layout_node)
  167. {
  168. auto value = resolve_calc_value(calc_product->first_calc_value, layout_node);
  169. for (auto& additional_value : calc_product->zero_or_more_additional_calc_values) {
  170. additional_value.value.visit(
  171. [&](CalculatedStyleValue::CalcValue const& calc_value) {
  172. if (additional_value.op != CalculatedStyleValue::CalcProductPartWithOperator::Multiply)
  173. VERIFY_NOT_REACHED();
  174. auto resolved_value = resolve_calc_value(calc_value, layout_node);
  175. value *= resolved_value;
  176. },
  177. [&](CalculatedStyleValue::CalcNumberValue const& calc_number_value) {
  178. if (additional_value.op != CalculatedStyleValue::CalcProductPartWithOperator::Divide)
  179. VERIFY_NOT_REACHED();
  180. auto resolved_calc_number_value = resolve_calc_number_value(calc_number_value);
  181. value /= resolved_calc_number_value;
  182. });
  183. }
  184. return value;
  185. }
  186. static float resolve_calc_sum(NonnullOwnPtr<CalculatedStyleValue::CalcSum> const& calc_sum, Layout::Node const& layout_node)
  187. {
  188. auto value = resolve_calc_product(calc_sum->first_calc_product, layout_node);
  189. for (auto& additional_product : calc_sum->zero_or_more_additional_calc_products) {
  190. auto additional_value = resolve_calc_product(additional_product.calc_product, layout_node);
  191. if (additional_product.op == CalculatedStyleValue::CalcSumPartWithOperator::Operation::Add)
  192. value += additional_value;
  193. else if (additional_product.op == CalculatedStyleValue::CalcSumPartWithOperator::Operation::Subtract)
  194. value -= additional_value;
  195. else
  196. VERIFY_NOT_REACHED();
  197. }
  198. return value;
  199. }
  200. const char* Length::unit_name() const
  201. {
  202. switch (m_type) {
  203. case Type::Cm:
  204. return "cm";
  205. case Type::In:
  206. return "in";
  207. case Type::Px:
  208. return "px";
  209. case Type::Pt:
  210. return "pt";
  211. case Type::Mm:
  212. return "mm";
  213. case Type::Q:
  214. return "Q";
  215. case Type::Pc:
  216. return "pc";
  217. case Type::Ex:
  218. return "ex";
  219. case Type::Em:
  220. return "em";
  221. case Type::Ch:
  222. return "ch";
  223. case Type::Rem:
  224. return "rem";
  225. case Type::Auto:
  226. return "auto";
  227. case Type::Undefined:
  228. return "undefined";
  229. case Type::Vh:
  230. return "vh";
  231. case Type::Vw:
  232. return "vw";
  233. case Type::Vmax:
  234. return "vmax";
  235. case Type::Vmin:
  236. return "vmin";
  237. case Type::Calculated:
  238. return "calculated";
  239. }
  240. VERIFY_NOT_REACHED();
  241. }
  242. }