Length.h 4.5 KB

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