Length.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Ch,
  22. Rch,
  23. Lh,
  24. Rlh,
  25. // Viewport-relative
  26. Vw,
  27. Vh,
  28. Vmin,
  29. Vmax,
  30. // Absolute
  31. Cm,
  32. Mm,
  33. Q,
  34. In,
  35. Pt,
  36. Pc,
  37. Px,
  38. // FIXME: Remove auto somehow
  39. Auto,
  40. };
  41. struct FontMetrics {
  42. FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const&, CSSPixels line_height);
  43. CSSPixels font_size;
  44. CSSPixels x_height;
  45. CSSPixels zero_advance;
  46. CSSPixels line_height;
  47. };
  48. static Optional<Type> unit_from_name(StringView);
  49. Length(int value, Type type);
  50. Length(float value, Type type);
  51. ~Length();
  52. static Length make_auto();
  53. static Length make_px(CSSPixels value);
  54. Length percentage_of(Percentage const&) const;
  55. Length resolved(Layout::Node const& layout_node) const;
  56. bool is_auto() const { return m_type == Type::Auto; }
  57. bool is_px() const { return m_type == Type::Px; }
  58. bool is_absolute() const
  59. {
  60. return m_type == Type::Cm
  61. || m_type == Type::Mm
  62. || m_type == Type::Q
  63. || m_type == Type::In
  64. || m_type == Type::Pt
  65. || m_type == Type::Pc
  66. || m_type == Type::Px;
  67. }
  68. bool is_font_relative() const
  69. {
  70. return m_type == Type::Em
  71. || m_type == Type::Rem
  72. || m_type == Type::Ex
  73. || m_type == Type::Rex
  74. || m_type == Type::Ch
  75. || m_type == Type::Rch
  76. || m_type == Type::Lh
  77. || m_type == Type::Rlh;
  78. }
  79. bool is_viewport_relative() const
  80. {
  81. return m_type == Type::Vw
  82. || m_type == Type::Vh
  83. || m_type == Type::Vmin
  84. || m_type == Type::Vmax;
  85. }
  86. bool is_relative() const
  87. {
  88. return is_font_relative() || is_viewport_relative();
  89. }
  90. Type type() const { return m_type; }
  91. float raw_value() const { return m_value; }
  92. CSSPixels to_px(Layout::Node const&) const;
  93. ALWAYS_INLINE CSSPixels to_px(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const
  94. {
  95. if (is_auto())
  96. return 0;
  97. if (is_relative())
  98. return relative_length_to_px(viewport_rect, font_metrics, root_font_metrics);
  99. return absolute_length_to_px();
  100. }
  101. ALWAYS_INLINE CSSPixels absolute_length_to_px() const
  102. {
  103. constexpr float inch_pixels = 96.0f;
  104. constexpr float centimeter_pixels = (inch_pixels / 2.54f);
  105. switch (m_type) {
  106. case Type::Cm:
  107. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  108. case Type::In:
  109. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  110. case Type::Px:
  111. return m_value; // 1px = 1/96th of 1in
  112. case Type::Pt:
  113. return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
  114. case Type::Pc:
  115. return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
  116. case Type::Mm:
  117. return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
  118. case Type::Q:
  119. return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
  120. default:
  121. VERIFY_NOT_REACHED();
  122. }
  123. }
  124. ErrorOr<String> to_string() const;
  125. bool operator==(Length const& other) const
  126. {
  127. return m_type == other.m_type && m_value == other.m_value;
  128. }
  129. CSSPixels relative_length_to_px(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  130. // Returns empty optional if it's already absolute.
  131. Optional<Length> absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  132. Length absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
  133. private:
  134. char const* unit_name() const;
  135. Type m_type;
  136. float m_value { 0 };
  137. };
  138. }
  139. template<>
  140. struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
  141. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
  142. {
  143. return Formatter<StringView>::format(builder, TRY(length.to_string()));
  144. }
  145. };