Length.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <LibGfx/Forward.h>
  10. #include <LibGfx/Rect.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/PixelUnits.h>
  13. namespace Web::CSS {
  14. class Length {
  15. public:
  16. enum class Type {
  17. // Font-relative
  18. Em,
  19. Rem,
  20. Ex,
  21. Rex,
  22. Cap,
  23. Rcap,
  24. Ch,
  25. Rch,
  26. Ic,
  27. Ric,
  28. Lh,
  29. Rlh,
  30. // Viewport-relative
  31. Vw,
  32. Svw,
  33. Lvw,
  34. Dvw,
  35. Vh,
  36. Svh,
  37. Lvh,
  38. Dvh,
  39. Vi,
  40. Svi,
  41. Lvi,
  42. Dvi,
  43. Vb,
  44. Svb,
  45. Lvb,
  46. Dvb,
  47. Vmin,
  48. Svmin,
  49. Lvmin,
  50. Dvmin,
  51. Vmax,
  52. Svmax,
  53. Lvmax,
  54. Dvmax,
  55. // Absolute
  56. Cm,
  57. Mm,
  58. Q,
  59. In,
  60. Pt,
  61. Pc,
  62. Px,
  63. // FIXME: Remove auto somehow
  64. Auto,
  65. };
  66. struct FontMetrics {
  67. FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const&);
  68. CSSPixels font_size;
  69. CSSPixels x_height;
  70. CSSPixels cap_height;
  71. CSSPixels zero_advance;
  72. CSSPixels line_height;
  73. };
  74. static Optional<Type> unit_from_name(StringView);
  75. Length(double value, Type type);
  76. ~Length();
  77. static Length make_auto();
  78. static Length make_px(CSSPixels value);
  79. Length percentage_of(Percentage const&) const;
  80. bool is_auto() const { return m_type == Type::Auto; }
  81. bool is_px() const { return m_type == Type::Px; }
  82. bool is_absolute() const
  83. {
  84. return m_type == Type::Cm
  85. || m_type == Type::Mm
  86. || m_type == Type::Q
  87. || m_type == Type::In
  88. || m_type == Type::Pt
  89. || m_type == Type::Pc
  90. || m_type == Type::Px;
  91. }
  92. bool is_font_relative() const
  93. {
  94. return m_type == Type::Em
  95. || m_type == Type::Rem
  96. || m_type == Type::Ex
  97. || m_type == Type::Rex
  98. || m_type == Type::Cap
  99. || m_type == Type::Rcap
  100. || m_type == Type::Ch
  101. || m_type == Type::Rch
  102. || m_type == Type::Ic
  103. || m_type == Type::Ric
  104. || m_type == Type::Lh
  105. || m_type == Type::Rlh;
  106. }
  107. bool is_viewport_relative() const
  108. {
  109. return m_type == Type::Vw
  110. || m_type == Type::Svw
  111. || m_type == Type::Lvw
  112. || m_type == Type::Dvw
  113. || m_type == Type::Vh
  114. || m_type == Type::Svh
  115. || m_type == Type::Lvh
  116. || m_type == Type::Dvh
  117. || m_type == Type::Vi
  118. || m_type == Type::Svi
  119. || m_type == Type::Lvi
  120. || m_type == Type::Dvi
  121. || m_type == Type::Vb
  122. || m_type == Type::Svb
  123. || m_type == Type::Lvb
  124. || m_type == Type::Dvb
  125. || m_type == Type::Vmin
  126. || m_type == Type::Svmin
  127. || m_type == Type::Lvmin
  128. || m_type == Type::Dvmin
  129. || m_type == Type::Vmax
  130. || m_type == Type::Svmax
  131. || m_type == Type::Lvmax
  132. || m_type == Type::Dvmax;
  133. }
  134. bool is_relative() const
  135. {
  136. return is_font_relative() || is_viewport_relative();
  137. }
  138. Type type() const { return m_type; }
  139. double raw_value() const { return m_value; }
  140. struct ResolutionContext {
  141. [[nodiscard]] static Length::ResolutionContext for_layout_node(Layout::Node const&);
  142. CSSPixelRect viewport_rect;
  143. FontMetrics font_metrics;
  144. FontMetrics root_font_metrics;
  145. };
  146. [[nodiscard]] CSSPixels to_px(ResolutionContext const&) const;
  147. [[nodiscard]] ALWAYS_INLINE CSSPixels to_px(Layout::Node const& node) const
  148. {
  149. if (is_absolute())
  150. return absolute_length_to_px();
  151. return to_px_slow_case(node);
  152. }
  153. ALWAYS_INLINE CSSPixels to_px(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  154. {
  155. if (is_auto())
  156. return 0;
  157. if (is_font_relative())
  158. return font_relative_length_to_px(font_metrics, root_font_metrics);
  159. if (is_viewport_relative())
  160. return viewport_relative_length_to_px(viewport_rect);
  161. return absolute_length_to_px();
  162. }
  163. ALWAYS_INLINE CSSPixels absolute_length_to_px() const
  164. {
  165. constexpr double inch_pixels = 96.0;
  166. constexpr double centimeter_pixels = (inch_pixels / 2.54);
  167. switch (m_type) {
  168. case Type::Cm:
  169. return CSSPixels::nearest_value_for(m_value * centimeter_pixels); // 1cm = 96px/2.54
  170. case Type::In:
  171. return CSSPixels::nearest_value_for(m_value * inch_pixels); // 1in = 2.54 cm = 96px
  172. case Type::Px:
  173. return CSSPixels::nearest_value_for(m_value); // 1px = 1/96th of 1in
  174. case Type::Pt:
  175. return CSSPixels::nearest_value_for(m_value * ((1.0 / 72.0) * inch_pixels)); // 1pt = 1/72th of 1in
  176. case Type::Pc:
  177. return CSSPixels::nearest_value_for(m_value * ((1.0 / 6.0) * inch_pixels)); // 1pc = 1/6th of 1in
  178. case Type::Mm:
  179. return CSSPixels::nearest_value_for(m_value * ((1.0 / 10.0) * centimeter_pixels)); // 1mm = 1/10th of 1cm
  180. case Type::Q:
  181. return CSSPixels::nearest_value_for(m_value * ((1.0 / 40.0) * centimeter_pixels)); // 1Q = 1/40th of 1cm
  182. default:
  183. VERIFY_NOT_REACHED();
  184. }
  185. }
  186. String to_string() const;
  187. bool operator==(Length const& other) const
  188. {
  189. return m_type == other.m_type && m_value == other.m_value;
  190. }
  191. CSSPixels font_relative_length_to_px(FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  192. CSSPixels viewport_relative_length_to_px(CSSPixelRect const& viewport_rect) const;
  193. // Returns empty optional if it's already absolute.
  194. Optional<Length> absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  195. Length absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  196. private:
  197. char const* unit_name() const;
  198. [[nodiscard]] CSSPixels to_px_slow_case(Layout::Node const&) const;
  199. Type m_type;
  200. double m_value { 0 };
  201. };
  202. }
  203. template<>
  204. struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
  205. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
  206. {
  207. return Formatter<StringView>::format(builder, length.to_string());
  208. }
  209. };