Length.cpp 5.6 KB

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