Length.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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::FontMetrics::FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const& pixel_metrics, CSSPixels line_height)
  19. : font_size(font_size)
  20. , x_height(pixel_metrics.x_height)
  21. , zero_advance(pixel_metrics.advance_of_ascii_zero + pixel_metrics.glyph_spacing)
  22. , line_height(line_height)
  23. {
  24. }
  25. Length::Length(int value, Type type)
  26. : m_type(type)
  27. , m_value(value)
  28. {
  29. }
  30. Length::Length(float value, Type type)
  31. : m_type(type)
  32. , m_value(value)
  33. {
  34. }
  35. Length::~Length() = default;
  36. Length Length::make_auto()
  37. {
  38. return Length(0, Type::Auto);
  39. }
  40. Length Length::make_px(CSSPixels value)
  41. {
  42. return Length(value.value(), Type::Px);
  43. }
  44. Length Length::percentage_of(Percentage const& percentage) const
  45. {
  46. if (is_auto()) {
  47. dbgln("Attempting to get percentage of an auto length, this seems wrong? But for now we just return the original length.");
  48. return *this;
  49. }
  50. return Length { percentage.as_fraction() * raw_value(), m_type };
  51. }
  52. Length Length::resolved(Layout::Node const& layout_node) const
  53. {
  54. if (is_relative())
  55. return make_px(to_px(layout_node));
  56. if (!isfinite(m_value))
  57. return make_auto();
  58. return *this;
  59. }
  60. CSSPixels Length::relative_length_to_px(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  61. {
  62. switch (m_type) {
  63. case Type::Em:
  64. return m_value * font_metrics.font_size;
  65. case Type::Rem:
  66. return m_value * root_font_metrics.font_size;
  67. case Type::Ex:
  68. return m_value * font_metrics.x_height;
  69. case Type::Ch:
  70. return m_value * font_metrics.zero_advance;
  71. case Type::Lh:
  72. return m_value * font_metrics.line_height;
  73. case Type::Rlh:
  74. return m_value * root_font_metrics.line_height;
  75. case Type::Vw:
  76. return viewport_rect.width() * (m_value / 100);
  77. case Type::Vh:
  78. return viewport_rect.height() * (m_value / 100);
  79. case Type::Vmin:
  80. return min(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  81. case Type::Vmax:
  82. return max(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  83. default:
  84. VERIFY_NOT_REACHED();
  85. }
  86. }
  87. CSSPixels Length::to_px(Layout::Node const& layout_node) const
  88. {
  89. if (is_absolute())
  90. return absolute_length_to_px();
  91. if (!layout_node.document().browsing_context())
  92. return 0;
  93. auto const& viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  94. auto* root_element = layout_node.document().document_element();
  95. if (!root_element || !root_element->layout_node())
  96. return 0;
  97. FontMetrics font_metrics {
  98. layout_node.computed_values().font_size(),
  99. layout_node.font().pixel_metrics(),
  100. layout_node.line_height()
  101. };
  102. FontMetrics root_font_metrics {
  103. root_element->layout_node()->computed_values().font_size(),
  104. root_element->layout_node()->font().pixel_metrics(),
  105. root_element->layout_node()->line_height()
  106. };
  107. return to_px(viewport_rect, font_metrics, root_font_metrics);
  108. }
  109. ErrorOr<String> Length::to_string() const
  110. {
  111. if (is_auto())
  112. return "auto"_string;
  113. return String::formatted("{}{}", m_value, unit_name());
  114. }
  115. char const* Length::unit_name() const
  116. {
  117. switch (m_type) {
  118. case Type::Em:
  119. return "em";
  120. case Type::Rem:
  121. return "rem";
  122. case Type::Ex:
  123. return "ex";
  124. case Type::Ch:
  125. return "ch";
  126. case Type::Lh:
  127. return "lh";
  128. case Type::Rlh:
  129. return "rlh";
  130. case Type::Vw:
  131. return "vw";
  132. case Type::Vh:
  133. return "vh";
  134. case Type::Vmin:
  135. return "vmin";
  136. case Type::Vmax:
  137. return "vmax";
  138. case Type::Cm:
  139. return "cm";
  140. case Type::Mm:
  141. return "mm";
  142. case Type::Q:
  143. return "Q";
  144. case Type::In:
  145. return "in";
  146. case Type::Pt:
  147. return "pt";
  148. case Type::Pc:
  149. return "pc";
  150. case Type::Px:
  151. return "px";
  152. case Type::Auto:
  153. return "auto";
  154. }
  155. VERIFY_NOT_REACHED();
  156. }
  157. Optional<Length::Type> Length::unit_from_name(StringView name)
  158. {
  159. if (name.equals_ignoring_ascii_case("em"sv)) {
  160. return Length::Type::Em;
  161. } else if (name.equals_ignoring_ascii_case("rem"sv)) {
  162. return Length::Type::Rem;
  163. } else if (name.equals_ignoring_ascii_case("ex"sv)) {
  164. return Length::Type::Ex;
  165. } else if (name.equals_ignoring_ascii_case("ch"sv)) {
  166. return Length::Type::Ch;
  167. } else if (name.equals_ignoring_ascii_case("lh"sv)) {
  168. return Length::Type::Lh;
  169. } else if (name.equals_ignoring_ascii_case("rlh"sv)) {
  170. return Length::Type::Rlh;
  171. } else if (name.equals_ignoring_ascii_case("vw"sv)) {
  172. return Length::Type::Vw;
  173. } else if (name.equals_ignoring_ascii_case("vh"sv)) {
  174. return Length::Type::Vh;
  175. } else if (name.equals_ignoring_ascii_case("vmin"sv)) {
  176. return Length::Type::Vmin;
  177. } else if (name.equals_ignoring_ascii_case("vmax"sv)) {
  178. return Length::Type::Vmax;
  179. } else if (name.equals_ignoring_ascii_case("cm"sv)) {
  180. return Length::Type::Cm;
  181. } else if (name.equals_ignoring_ascii_case("mm"sv)) {
  182. return Length::Type::Mm;
  183. } else if (name.equals_ignoring_ascii_case("Q"sv)) {
  184. return Length::Type::Q;
  185. } else if (name.equals_ignoring_ascii_case("in"sv)) {
  186. return Length::Type::In;
  187. } else if (name.equals_ignoring_ascii_case("pt"sv)) {
  188. return Length::Type::Pt;
  189. } else if (name.equals_ignoring_ascii_case("pc"sv)) {
  190. return Length::Type::Pc;
  191. } else if (name.equals_ignoring_ascii_case("px"sv)) {
  192. return Length::Type::Px;
  193. }
  194. return {};
  195. }
  196. Optional<Length> Length::absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  197. {
  198. if (is_px())
  199. return {};
  200. if (is_absolute() || is_relative()) {
  201. auto px = to_px(viewport_rect, font_metrics, root_font_metrics);
  202. return CSS::Length::make_px(px);
  203. }
  204. return {};
  205. }
  206. Length Length::absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  207. {
  208. return absolutize(viewport_rect, font_metrics, root_font_metrics).value_or(*this);
  209. }
  210. }