Length.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2022-2023, 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. namespace Web::CSS {
  18. Length::Length(int value, Type type)
  19. : m_type(type)
  20. , m_value(value)
  21. {
  22. }
  23. Length::Length(float value, Type type)
  24. : m_type(type)
  25. , m_value(value)
  26. {
  27. }
  28. Length::~Length() = default;
  29. Length Length::make_auto()
  30. {
  31. return Length(0, Type::Auto);
  32. }
  33. Length Length::make_px(CSSPixels value)
  34. {
  35. return Length(value.value(), Type::Px);
  36. }
  37. Length Length::percentage_of(Percentage const& percentage) const
  38. {
  39. if (is_auto()) {
  40. dbgln("Attempting to get percentage of an 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(Layout::Node const& layout_node) const
  46. {
  47. if (is_relative())
  48. return make_px(to_px(layout_node));
  49. if (!isfinite(m_value))
  50. return make_auto();
  51. return *this;
  52. }
  53. CSSPixels Length::relative_length_to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
  54. {
  55. switch (m_type) {
  56. case Type::Ex:
  57. return m_value * font_metrics.x_height;
  58. case Type::Em:
  59. return m_value * font_size;
  60. case Type::Ch:
  61. // FIXME: Use layout_node.font().pixel_size() when writing-mode is not horizontal-tb (it has to be implemented first)
  62. return m_value * (font_metrics.advance_of_ascii_zero + font_metrics.glyph_spacing);
  63. case Type::Rem:
  64. return m_value * root_font_size;
  65. case Type::Vw:
  66. return viewport_rect.width() * (m_value / 100);
  67. case Type::Vh:
  68. return viewport_rect.height() * (m_value / 100);
  69. case Type::Vmin:
  70. return min(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  71. case Type::Vmax:
  72. return max(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  73. case Type::Lh:
  74. return m_value * line_height;
  75. case Type::Rlh:
  76. return m_value * root_line_height;
  77. default:
  78. VERIFY_NOT_REACHED();
  79. }
  80. }
  81. CSSPixels Length::to_px(Layout::Node const& layout_node) const
  82. {
  83. if (is_absolute())
  84. return absolute_length_to_px();
  85. if (!layout_node.document().browsing_context())
  86. return 0;
  87. auto const& viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  88. auto* root_element = layout_node.document().document_element();
  89. if (!root_element || !root_element->layout_node())
  90. return 0;
  91. return to_px(viewport_rect, layout_node.font().pixel_metrics(), layout_node.computed_values().font_size(), root_element->layout_node()->computed_values().font_size(), layout_node.line_height(), root_element->layout_node()->line_height());
  92. }
  93. ErrorOr<String> Length::to_string() const
  94. {
  95. if (is_auto())
  96. return "auto"_string;
  97. return String::formatted("{}{}", m_value, unit_name());
  98. }
  99. char const* Length::unit_name() const
  100. {
  101. switch (m_type) {
  102. case Type::Cm:
  103. return "cm";
  104. case Type::In:
  105. return "in";
  106. case Type::Px:
  107. return "px";
  108. case Type::Pt:
  109. return "pt";
  110. case Type::Mm:
  111. return "mm";
  112. case Type::Q:
  113. return "Q";
  114. case Type::Pc:
  115. return "pc";
  116. case Type::Ex:
  117. return "ex";
  118. case Type::Em:
  119. return "em";
  120. case Type::Ch:
  121. return "ch";
  122. case Type::Rem:
  123. return "rem";
  124. case Type::Auto:
  125. return "auto";
  126. case Type::Vh:
  127. return "vh";
  128. case Type::Vw:
  129. return "vw";
  130. case Type::Vmax:
  131. return "vmax";
  132. case Type::Vmin:
  133. return "vmin";
  134. case Type::Lh:
  135. return "lh";
  136. case Type::Rlh:
  137. return "rlh";
  138. }
  139. VERIFY_NOT_REACHED();
  140. }
  141. Optional<Length::Type> Length::unit_from_name(StringView name)
  142. {
  143. if (name.equals_ignoring_ascii_case("px"sv)) {
  144. return Length::Type::Px;
  145. } else if (name.equals_ignoring_ascii_case("pt"sv)) {
  146. return Length::Type::Pt;
  147. } else if (name.equals_ignoring_ascii_case("pc"sv)) {
  148. return Length::Type::Pc;
  149. } else if (name.equals_ignoring_ascii_case("mm"sv)) {
  150. return Length::Type::Mm;
  151. } else if (name.equals_ignoring_ascii_case("rem"sv)) {
  152. return Length::Type::Rem;
  153. } else if (name.equals_ignoring_ascii_case("em"sv)) {
  154. return Length::Type::Em;
  155. } else if (name.equals_ignoring_ascii_case("ex"sv)) {
  156. return Length::Type::Ex;
  157. } else if (name.equals_ignoring_ascii_case("ch"sv)) {
  158. return Length::Type::Ch;
  159. } else if (name.equals_ignoring_ascii_case("vw"sv)) {
  160. return Length::Type::Vw;
  161. } else if (name.equals_ignoring_ascii_case("vh"sv)) {
  162. return Length::Type::Vh;
  163. } else if (name.equals_ignoring_ascii_case("vmax"sv)) {
  164. return Length::Type::Vmax;
  165. } else if (name.equals_ignoring_ascii_case("vmin"sv)) {
  166. return Length::Type::Vmin;
  167. } else if (name.equals_ignoring_ascii_case("cm"sv)) {
  168. return Length::Type::Cm;
  169. } else if (name.equals_ignoring_ascii_case("in"sv)) {
  170. return Length::Type::In;
  171. } else if (name.equals_ignoring_ascii_case("Q"sv)) {
  172. return Length::Type::Q;
  173. } else if (name.equals_ignoring_ascii_case("lh"sv)) {
  174. return Length::Type::Lh;
  175. } else if (name.equals_ignoring_ascii_case("rlh"sv)) {
  176. return Length::Type::Rlh;
  177. }
  178. return {};
  179. }
  180. Optional<Length> Length::absolutize(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
  181. {
  182. if (is_px())
  183. return {};
  184. if (is_absolute() || is_relative()) {
  185. auto px = to_px(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height);
  186. return CSS::Length::make_px(px);
  187. }
  188. return {};
  189. }
  190. Length Length::absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const
  191. {
  192. return absolutize(viewport_rect, font_metrics, font_size, root_font_size, line_height, root_line_height).value_or(*this);
  193. }
  194. }