Length.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. 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::make_calculated(NonnullRefPtr<CalculatedStyleValue> calculated_style_value)
  38. {
  39. Length length { 0, Type::Calculated };
  40. length.m_calculated_style = move(calculated_style_value);
  41. return length;
  42. }
  43. Length Length::percentage_of(Percentage const& percentage) const
  44. {
  45. VERIFY(!is_calculated());
  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_calculated())
  55. return m_calculated_style->resolve_length(layout_node).release_value();
  56. if (is_relative())
  57. return make_px(to_px(layout_node));
  58. if (!isfinite(m_value))
  59. return make_auto();
  60. return *this;
  61. }
  62. 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
  63. {
  64. switch (m_type) {
  65. case Type::Ex:
  66. return m_value * font_metrics.x_height;
  67. case Type::Em:
  68. return m_value * font_size;
  69. case Type::Ch:
  70. // FIXME: Use layout_node.font().pixel_size() when writing-mode is not horizontal-tb (it has to be implemented first)
  71. return m_value * (font_metrics.advance_of_ascii_zero + font_metrics.glyph_spacing);
  72. case Type::Rem:
  73. return m_value * root_font_size;
  74. case Type::Vw:
  75. return viewport_rect.width() * (m_value / 100);
  76. case Type::Vh:
  77. return viewport_rect.height() * (m_value / 100);
  78. case Type::Vmin:
  79. return min(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  80. case Type::Vmax:
  81. return max(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  82. case Type::Lh:
  83. return m_value * line_height;
  84. case Type::Rlh:
  85. return m_value * root_line_height;
  86. default:
  87. VERIFY_NOT_REACHED();
  88. }
  89. }
  90. CSSPixels Length::to_px(Layout::Node const& layout_node) const
  91. {
  92. if (is_calculated())
  93. return m_calculated_style->resolve_length(layout_node)->to_px(layout_node);
  94. if (is_absolute())
  95. return absolute_length_to_px();
  96. if (!layout_node.document().browsing_context())
  97. return 0;
  98. auto const& viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  99. auto* root_element = layout_node.document().document_element();
  100. if (!root_element || !root_element->layout_node())
  101. return 0;
  102. 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());
  103. }
  104. ErrorOr<String> Length::to_string() const
  105. {
  106. if (is_calculated())
  107. return m_calculated_style->to_string();
  108. if (is_auto())
  109. return "auto"_string;
  110. return String::formatted("{}{}", m_value, unit_name());
  111. }
  112. char const* Length::unit_name() const
  113. {
  114. switch (m_type) {
  115. case Type::Cm:
  116. return "cm";
  117. case Type::In:
  118. return "in";
  119. case Type::Px:
  120. return "px";
  121. case Type::Pt:
  122. return "pt";
  123. case Type::Mm:
  124. return "mm";
  125. case Type::Q:
  126. return "Q";
  127. case Type::Pc:
  128. return "pc";
  129. case Type::Ex:
  130. return "ex";
  131. case Type::Em:
  132. return "em";
  133. case Type::Ch:
  134. return "ch";
  135. case Type::Rem:
  136. return "rem";
  137. case Type::Auto:
  138. return "auto";
  139. case Type::Vh:
  140. return "vh";
  141. case Type::Vw:
  142. return "vw";
  143. case Type::Vmax:
  144. return "vmax";
  145. case Type::Vmin:
  146. return "vmin";
  147. case Type::Lh:
  148. return "lh";
  149. case Type::Rlh:
  150. return "rlh";
  151. case Type::Calculated:
  152. return "calculated";
  153. }
  154. VERIFY_NOT_REACHED();
  155. }
  156. Optional<Length::Type> Length::unit_from_name(StringView name)
  157. {
  158. if (name.equals_ignoring_ascii_case("px"sv)) {
  159. return Length::Type::Px;
  160. } else if (name.equals_ignoring_ascii_case("pt"sv)) {
  161. return Length::Type::Pt;
  162. } else if (name.equals_ignoring_ascii_case("pc"sv)) {
  163. return Length::Type::Pc;
  164. } else if (name.equals_ignoring_ascii_case("mm"sv)) {
  165. return Length::Type::Mm;
  166. } else if (name.equals_ignoring_ascii_case("rem"sv)) {
  167. return Length::Type::Rem;
  168. } else if (name.equals_ignoring_ascii_case("em"sv)) {
  169. return Length::Type::Em;
  170. } else if (name.equals_ignoring_ascii_case("ex"sv)) {
  171. return Length::Type::Ex;
  172. } else if (name.equals_ignoring_ascii_case("ch"sv)) {
  173. return Length::Type::Ch;
  174. } else if (name.equals_ignoring_ascii_case("vw"sv)) {
  175. return Length::Type::Vw;
  176. } else if (name.equals_ignoring_ascii_case("vh"sv)) {
  177. return Length::Type::Vh;
  178. } else if (name.equals_ignoring_ascii_case("vmax"sv)) {
  179. return Length::Type::Vmax;
  180. } else if (name.equals_ignoring_ascii_case("vmin"sv)) {
  181. return Length::Type::Vmin;
  182. } else if (name.equals_ignoring_ascii_case("cm"sv)) {
  183. return Length::Type::Cm;
  184. } else if (name.equals_ignoring_ascii_case("in"sv)) {
  185. return Length::Type::In;
  186. } else if (name.equals_ignoring_ascii_case("Q"sv)) {
  187. return Length::Type::Q;
  188. } else if (name.equals_ignoring_ascii_case("lh"sv)) {
  189. return Length::Type::Lh;
  190. } else if (name.equals_ignoring_ascii_case("rlh"sv)) {
  191. return Length::Type::Rlh;
  192. }
  193. return {};
  194. }
  195. NonnullRefPtr<CalculatedStyleValue> Length::calculated_style_value() const
  196. {
  197. VERIFY(!m_calculated_style.is_null());
  198. return *m_calculated_style;
  199. }
  200. bool Length::operator==(Length const& other) const
  201. {
  202. if (is_calculated())
  203. return m_calculated_style == other.m_calculated_style;
  204. return m_type == other.m_type && m_value == other.m_value;
  205. }
  206. }