Length.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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&, CSSPixels line_height);
  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(int value, Type type);
  76. Length(double value, Type type);
  77. ~Length();
  78. static Length make_auto();
  79. static Length make_px(CSSPixels value);
  80. Length percentage_of(Percentage const&) const;
  81. bool is_auto() const { return m_type == Type::Auto; }
  82. bool is_px() const { return m_type == Type::Px; }
  83. bool is_absolute() const
  84. {
  85. return m_type == Type::Cm
  86. || m_type == Type::Mm
  87. || m_type == Type::Q
  88. || m_type == Type::In
  89. || m_type == Type::Pt
  90. || m_type == Type::Pc
  91. || m_type == Type::Px;
  92. }
  93. bool is_font_relative() const
  94. {
  95. return m_type == Type::Em
  96. || m_type == Type::Rem
  97. || m_type == Type::Ex
  98. || m_type == Type::Rex
  99. || m_type == Type::Cap
  100. || m_type == Type::Rcap
  101. || m_type == Type::Ch
  102. || m_type == Type::Rch
  103. || m_type == Type::Ic
  104. || m_type == Type::Ric
  105. || m_type == Type::Lh
  106. || m_type == Type::Rlh;
  107. }
  108. bool is_viewport_relative() const
  109. {
  110. return m_type == Type::Vw
  111. || m_type == Type::Svw
  112. || m_type == Type::Lvw
  113. || m_type == Type::Dvw
  114. || m_type == Type::Vh
  115. || m_type == Type::Svh
  116. || m_type == Type::Lvh
  117. || m_type == Type::Dvh
  118. || m_type == Type::Vi
  119. || m_type == Type::Svi
  120. || m_type == Type::Lvi
  121. || m_type == Type::Dvi
  122. || m_type == Type::Vb
  123. || m_type == Type::Svb
  124. || m_type == Type::Lvb
  125. || m_type == Type::Dvb
  126. || m_type == Type::Vmin
  127. || m_type == Type::Svmin
  128. || m_type == Type::Lvmin
  129. || m_type == Type::Dvmin
  130. || m_type == Type::Vmax
  131. || m_type == Type::Svmax
  132. || m_type == Type::Lvmax
  133. || m_type == Type::Dvmax;
  134. }
  135. bool is_relative() const
  136. {
  137. return is_font_relative() || is_viewport_relative();
  138. }
  139. Type type() const { return m_type; }
  140. double raw_value() const { return m_value; }
  141. struct ResolutionContext {
  142. [[nodiscard]] static Length::ResolutionContext for_layout_node(Layout::Node const&);
  143. CSSPixelRect viewport_rect;
  144. FontMetrics font_metrics;
  145. FontMetrics root_font_metrics;
  146. };
  147. [[nodiscard]] CSSPixels to_px(ResolutionContext const&) const;
  148. CSSPixels to_px(Layout::Node const&) const;
  149. ALWAYS_INLINE CSSPixels to_px(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  150. {
  151. if (is_auto())
  152. return 0;
  153. if (is_font_relative())
  154. return font_relative_length_to_px(font_metrics, root_font_metrics);
  155. if (is_viewport_relative())
  156. return viewport_relative_length_to_px(viewport_rect);
  157. return absolute_length_to_px();
  158. }
  159. ALWAYS_INLINE CSSPixels absolute_length_to_px() const
  160. {
  161. constexpr double inch_pixels = 96.0;
  162. constexpr double centimeter_pixels = (inch_pixels / 2.54);
  163. switch (m_type) {
  164. case Type::Cm:
  165. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  166. case Type::In:
  167. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  168. case Type::Px:
  169. return m_value; // 1px = 1/96th of 1in
  170. case Type::Pt:
  171. return m_value * ((1.0 / 72.0) * inch_pixels); // 1pt = 1/72th of 1in
  172. case Type::Pc:
  173. return m_value * ((1.0 / 6.0) * inch_pixels); // 1pc = 1/6th of 1in
  174. case Type::Mm:
  175. return m_value * ((1.0 / 10.0) * centimeter_pixels); // 1mm = 1/10th of 1cm
  176. case Type::Q:
  177. return m_value * ((1.0 / 40.0) * centimeter_pixels); // 1Q = 1/40th of 1cm
  178. default:
  179. VERIFY_NOT_REACHED();
  180. }
  181. }
  182. ErrorOr<String> to_string() const;
  183. bool operator==(Length const& other) const
  184. {
  185. return m_type == other.m_type && m_value == other.m_value;
  186. }
  187. CSSPixels font_relative_length_to_px(FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  188. CSSPixels viewport_relative_length_to_px(CSSPixelRect const& viewport_rect) const;
  189. // Returns empty optional if it's already absolute.
  190. Optional<Length> absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  191. Length absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  192. private:
  193. char const* unit_name() const;
  194. Type m_type;
  195. double m_value { 0 };
  196. };
  197. }
  198. template<>
  199. struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
  200. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
  201. {
  202. return Formatter<StringView>::format(builder, TRY(length.to_string()));
  203. }
  204. };