Length.h 4.6 KB

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