Length.h 4.6 KB

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