Length.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/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. #include <LibWeb/PixelUnits.h>
  18. namespace Web::CSS {
  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(CSSPixels value, Type type)
  30. : m_type(type)
  31. , m_value(value.value())
  32. {
  33. }
  34. Length::~Length() = default;
  35. Length Length::make_auto()
  36. {
  37. return Length(0, Type::Auto);
  38. }
  39. Length Length::make_px(float value)
  40. {
  41. return Length(value, Type::Px);
  42. }
  43. Length Length::make_px(CSSPixels value)
  44. {
  45. return Length(value.value(), Type::Px);
  46. }
  47. Length Length::make_calculated(NonnullRefPtr<CalculatedStyleValue> calculated_style_value)
  48. {
  49. Length length { 0, Type::Calculated };
  50. length.m_calculated_style = move(calculated_style_value);
  51. return length;
  52. }
  53. Length Length::percentage_of(Percentage const& percentage) const
  54. {
  55. VERIFY(!is_calculated());
  56. if (is_auto()) {
  57. dbgln("Attempting to get percentage of an auto length, this seems wrong? But for now we just return the original length.");
  58. return *this;
  59. }
  60. return Length { percentage.as_fraction() * raw_value(), m_type };
  61. }
  62. Length Length::resolved(Layout::Node const& layout_node) const
  63. {
  64. if (is_calculated())
  65. return m_calculated_style->resolve_length(layout_node).release_value();
  66. if (is_relative())
  67. return make_px(to_px(layout_node));
  68. if (!isfinite(m_value))
  69. return make_auto();
  70. return *this;
  71. }
  72. float Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float root_font_size) const
  73. {
  74. switch (m_type) {
  75. case Type::Ex:
  76. return m_value * font_metrics.x_height;
  77. case Type::Em:
  78. return m_value * font_size;
  79. case Type::Ch:
  80. // FIXME: Use layout_node.font().glyph_height() when writing-mode is not horizontal-tb (it has to be implemented first)
  81. return m_value * (font_metrics.advance_of_ascii_zero + font_metrics.glyph_spacing);
  82. case Type::Rem:
  83. return m_value * root_font_size;
  84. case Type::Vw:
  85. return viewport_rect.width() * (m_value / 100);
  86. case Type::Vh:
  87. return viewport_rect.height() * (m_value / 100);
  88. case Type::Vmin:
  89. return min(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  90. case Type::Vmax:
  91. return max(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  92. default:
  93. VERIFY_NOT_REACHED();
  94. }
  95. }
  96. float Length::to_px(Layout::Node const& layout_node) const
  97. {
  98. if (is_calculated())
  99. return m_calculated_style->resolve_length(layout_node)->to_px(layout_node);
  100. if (is_absolute())
  101. return absolute_length_to_px();
  102. if (!layout_node.document().browsing_context())
  103. return 0;
  104. auto const& viewport_rect = layout_node.document().browsing_context()->viewport_rect().to_type<float>().to_type<int>();
  105. auto* root_element = layout_node.document().document_element();
  106. if (!root_element || !root_element->layout_node())
  107. return 0;
  108. return to_px(viewport_rect, layout_node.font().pixel_metrics(), layout_node.computed_values().font_size(), root_element->layout_node()->computed_values().font_size());
  109. }
  110. DeprecatedString Length::to_deprecated_string() const
  111. {
  112. if (is_calculated())
  113. return m_calculated_style->to_deprecated_string();
  114. if (is_auto())
  115. return "auto";
  116. return DeprecatedString::formatted("{}{}", m_value, unit_name());
  117. }
  118. char const* Length::unit_name() const
  119. {
  120. switch (m_type) {
  121. case Type::Cm:
  122. return "cm";
  123. case Type::In:
  124. return "in";
  125. case Type::Px:
  126. return "px";
  127. case Type::Pt:
  128. return "pt";
  129. case Type::Mm:
  130. return "mm";
  131. case Type::Q:
  132. return "Q";
  133. case Type::Pc:
  134. return "pc";
  135. case Type::Ex:
  136. return "ex";
  137. case Type::Em:
  138. return "em";
  139. case Type::Ch:
  140. return "ch";
  141. case Type::Rem:
  142. return "rem";
  143. case Type::Auto:
  144. return "auto";
  145. case Type::Vh:
  146. return "vh";
  147. case Type::Vw:
  148. return "vw";
  149. case Type::Vmax:
  150. return "vmax";
  151. case Type::Vmin:
  152. return "vmin";
  153. case Type::Calculated:
  154. return "calculated";
  155. }
  156. VERIFY_NOT_REACHED();
  157. }
  158. Optional<Length::Type> Length::unit_from_name(StringView name)
  159. {
  160. if (name.equals_ignoring_case("px"sv)) {
  161. return Length::Type::Px;
  162. } else if (name.equals_ignoring_case("pt"sv)) {
  163. return Length::Type::Pt;
  164. } else if (name.equals_ignoring_case("pc"sv)) {
  165. return Length::Type::Pc;
  166. } else if (name.equals_ignoring_case("mm"sv)) {
  167. return Length::Type::Mm;
  168. } else if (name.equals_ignoring_case("rem"sv)) {
  169. return Length::Type::Rem;
  170. } else if (name.equals_ignoring_case("em"sv)) {
  171. return Length::Type::Em;
  172. } else if (name.equals_ignoring_case("ex"sv)) {
  173. return Length::Type::Ex;
  174. } else if (name.equals_ignoring_case("ch"sv)) {
  175. return Length::Type::Ch;
  176. } else if (name.equals_ignoring_case("vw"sv)) {
  177. return Length::Type::Vw;
  178. } else if (name.equals_ignoring_case("vh"sv)) {
  179. return Length::Type::Vh;
  180. } else if (name.equals_ignoring_case("vmax"sv)) {
  181. return Length::Type::Vmax;
  182. } else if (name.equals_ignoring_case("vmin"sv)) {
  183. return Length::Type::Vmin;
  184. } else if (name.equals_ignoring_case("cm"sv)) {
  185. return Length::Type::Cm;
  186. } else if (name.equals_ignoring_case("in"sv)) {
  187. return Length::Type::In;
  188. } else if (name.equals_ignoring_case("Q"sv)) {
  189. return Length::Type::Q;
  190. }
  191. return {};
  192. }
  193. NonnullRefPtr<CalculatedStyleValue> Length::calculated_style_value() const
  194. {
  195. VERIFY(!m_calculated_style.is_null());
  196. return *m_calculated_style;
  197. }
  198. bool Length::operator==(Length const& other) const
  199. {
  200. if (is_calculated())
  201. return m_calculated_style == other.m_calculated_style;
  202. return m_type == other.m_type && m_value == other.m_value;
  203. }
  204. }