Length.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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) 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().glyph_height() 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. default:
  83. VERIFY_NOT_REACHED();
  84. }
  85. }
  86. CSSPixels Length::to_px(Layout::Node const& layout_node) const
  87. {
  88. if (is_calculated())
  89. return m_calculated_style->resolve_length(layout_node)->to_px(layout_node);
  90. if (is_absolute())
  91. return absolute_length_to_px();
  92. if (!layout_node.document().browsing_context())
  93. return 0;
  94. auto const& viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  95. auto* root_element = layout_node.document().document_element();
  96. if (!root_element || !root_element->layout_node())
  97. return 0;
  98. return to_px(viewport_rect, layout_node.font().pixel_metrics(), layout_node.computed_values().font_size(), root_element->layout_node()->computed_values().font_size());
  99. }
  100. DeprecatedString Length::to_deprecated_string() const
  101. {
  102. if (is_calculated())
  103. return m_calculated_style->to_deprecated_string();
  104. if (is_auto())
  105. return "auto";
  106. return DeprecatedString::formatted("{}{}", m_value, unit_name());
  107. }
  108. char const* Length::unit_name() const
  109. {
  110. switch (m_type) {
  111. case Type::Cm:
  112. return "cm";
  113. case Type::In:
  114. return "in";
  115. case Type::Px:
  116. return "px";
  117. case Type::Pt:
  118. return "pt";
  119. case Type::Mm:
  120. return "mm";
  121. case Type::Q:
  122. return "Q";
  123. case Type::Pc:
  124. return "pc";
  125. case Type::Ex:
  126. return "ex";
  127. case Type::Em:
  128. return "em";
  129. case Type::Ch:
  130. return "ch";
  131. case Type::Rem:
  132. return "rem";
  133. case Type::Auto:
  134. return "auto";
  135. case Type::Vh:
  136. return "vh";
  137. case Type::Vw:
  138. return "vw";
  139. case Type::Vmax:
  140. return "vmax";
  141. case Type::Vmin:
  142. return "vmin";
  143. case Type::Calculated:
  144. return "calculated";
  145. }
  146. VERIFY_NOT_REACHED();
  147. }
  148. Optional<Length::Type> Length::unit_from_name(StringView name)
  149. {
  150. if (name.equals_ignoring_case("px"sv)) {
  151. return Length::Type::Px;
  152. } else if (name.equals_ignoring_case("pt"sv)) {
  153. return Length::Type::Pt;
  154. } else if (name.equals_ignoring_case("pc"sv)) {
  155. return Length::Type::Pc;
  156. } else if (name.equals_ignoring_case("mm"sv)) {
  157. return Length::Type::Mm;
  158. } else if (name.equals_ignoring_case("rem"sv)) {
  159. return Length::Type::Rem;
  160. } else if (name.equals_ignoring_case("em"sv)) {
  161. return Length::Type::Em;
  162. } else if (name.equals_ignoring_case("ex"sv)) {
  163. return Length::Type::Ex;
  164. } else if (name.equals_ignoring_case("ch"sv)) {
  165. return Length::Type::Ch;
  166. } else if (name.equals_ignoring_case("vw"sv)) {
  167. return Length::Type::Vw;
  168. } else if (name.equals_ignoring_case("vh"sv)) {
  169. return Length::Type::Vh;
  170. } else if (name.equals_ignoring_case("vmax"sv)) {
  171. return Length::Type::Vmax;
  172. } else if (name.equals_ignoring_case("vmin"sv)) {
  173. return Length::Type::Vmin;
  174. } else if (name.equals_ignoring_case("cm"sv)) {
  175. return Length::Type::Cm;
  176. } else if (name.equals_ignoring_case("in"sv)) {
  177. return Length::Type::In;
  178. } else if (name.equals_ignoring_case("Q"sv)) {
  179. return Length::Type::Q;
  180. }
  181. return {};
  182. }
  183. NonnullRefPtr<CalculatedStyleValue> Length::calculated_style_value() const
  184. {
  185. VERIFY(!m_calculated_style.is_null());
  186. return *m_calculated_style;
  187. }
  188. bool Length::operator==(Length const& other) const
  189. {
  190. if (is_calculated())
  191. return m_calculated_style == other.m_calculated_style;
  192. return m_type == other.m_type && m_value == other.m_value;
  193. }
  194. }