Length.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <LibWeb/Forward.h>
  11. #include <LibWeb/PixelUnits.h>
  12. namespace Web::CSS {
  13. class Length {
  14. public:
  15. enum class Type {
  16. // Font-relative
  17. Em,
  18. Rem,
  19. Ex,
  20. Rex,
  21. Cap,
  22. Rcap,
  23. Ch,
  24. Rch,
  25. Ic,
  26. Ric,
  27. Lh,
  28. Rlh,
  29. // Viewport-relative
  30. Vw,
  31. Svw,
  32. Lvw,
  33. Dvw,
  34. Vh,
  35. Svh,
  36. Lvh,
  37. Dvh,
  38. Vi,
  39. Svi,
  40. Lvi,
  41. Dvi,
  42. Vb,
  43. Svb,
  44. Lvb,
  45. Dvb,
  46. Vmin,
  47. Svmin,
  48. Lvmin,
  49. Dvmin,
  50. Vmax,
  51. Svmax,
  52. Lvmax,
  53. Dvmax,
  54. // Absolute
  55. Cm,
  56. Mm,
  57. Q,
  58. In,
  59. Pt,
  60. Pc,
  61. Px,
  62. // FIXME: Remove auto somehow
  63. Auto,
  64. };
  65. struct FontMetrics {
  66. FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const&, CSSPixels line_height);
  67. CSSPixels font_size;
  68. CSSPixels x_height;
  69. CSSPixels cap_height;
  70. CSSPixels zero_advance;
  71. CSSPixels line_height;
  72. };
  73. static Optional<Type> unit_from_name(StringView);
  74. Length(int value, Type type);
  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. CSSPixels to_px(Layout::Node const&) const;
  141. ALWAYS_INLINE CSSPixels to_px(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  142. {
  143. if (is_auto())
  144. return 0;
  145. if (is_font_relative())
  146. return font_relative_length_to_px(font_metrics, root_font_metrics);
  147. if (is_viewport_relative())
  148. return viewport_relative_length_to_px(viewport_rect);
  149. return absolute_length_to_px();
  150. }
  151. ALWAYS_INLINE CSSPixels absolute_length_to_px() const
  152. {
  153. constexpr double inch_pixels = 96.0;
  154. constexpr double centimeter_pixels = (inch_pixels / 2.54);
  155. switch (m_type) {
  156. case Type::Cm:
  157. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  158. case Type::In:
  159. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  160. case Type::Px:
  161. return m_value; // 1px = 1/96th of 1in
  162. case Type::Pt:
  163. return m_value * ((1.0 / 72.0) * inch_pixels); // 1pt = 1/72th of 1in
  164. case Type::Pc:
  165. return m_value * ((1.0 / 6.0) * inch_pixels); // 1pc = 1/6th of 1in
  166. case Type::Mm:
  167. return m_value * ((1.0 / 10.0) * centimeter_pixels); // 1mm = 1/10th of 1cm
  168. case Type::Q:
  169. return m_value * ((1.0 / 40.0) * centimeter_pixels); // 1Q = 1/40th of 1cm
  170. default:
  171. VERIFY_NOT_REACHED();
  172. }
  173. }
  174. ErrorOr<String> to_string() const;
  175. bool operator==(Length const& other) const
  176. {
  177. return m_type == other.m_type && m_value == other.m_value;
  178. }
  179. CSSPixels font_relative_length_to_px(FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  180. CSSPixels viewport_relative_length_to_px(CSSPixelRect const& viewport_rect) const;
  181. // Returns empty optional if it's already absolute.
  182. Optional<Length> absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  183. Length absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  184. private:
  185. char const* unit_name() const;
  186. Type m_type;
  187. double m_value { 0 };
  188. };
  189. }
  190. template<>
  191. struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
  192. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
  193. {
  194. return Formatter<StringView>::format(builder, TRY(length.to_string()));
  195. }
  196. };