Length.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <LibGfx/Forward.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::CSS {
  11. class Length {
  12. public:
  13. enum class Type {
  14. Undefined,
  15. Percentage,
  16. Calculated,
  17. Auto,
  18. Cm,
  19. In,
  20. Mm,
  21. Q,
  22. Px,
  23. Pt,
  24. Pc,
  25. Ex,
  26. Em,
  27. Ch,
  28. Rem,
  29. Vh,
  30. Vw,
  31. Vmax,
  32. Vmin,
  33. };
  34. Length() = default;
  35. Length(int value, Type type)
  36. : m_type(type)
  37. , m_value(value)
  38. {
  39. }
  40. Length(float value, Type type)
  41. : m_type(type)
  42. , m_value(value)
  43. {
  44. }
  45. static Length make_auto() { return Length(0, Type::Auto); }
  46. static Length make_px(float value) { return Length(value, Type::Px); }
  47. Length resolved(const Length& fallback_for_undefined, const Layout::Node& layout_node, float reference_for_percent) const
  48. {
  49. if (is_undefined())
  50. return fallback_for_undefined;
  51. if (is_calculated())
  52. return Length(resolve_calculated_value(layout_node, reference_for_percent), Type::Px);
  53. if (is_percentage())
  54. return make_px(raw_value() / 100.0f * reference_for_percent);
  55. if (is_relative())
  56. return make_px(to_px(layout_node));
  57. return *this;
  58. }
  59. Length resolved_or_auto(const Layout::Node& layout_node, float reference_for_percent) const
  60. {
  61. return resolved(make_auto(), layout_node, reference_for_percent);
  62. }
  63. Length resolved_or_zero(const Layout::Node& layout_node, float reference_for_percent) const
  64. {
  65. return resolved(make_px(0), layout_node, reference_for_percent);
  66. }
  67. bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; }
  68. bool is_undefined() const { return m_type == Type::Undefined; }
  69. bool is_percentage() const { return m_type == Type::Percentage || m_type == Type::Calculated; }
  70. bool is_auto() const { return m_type == Type::Auto; }
  71. bool is_calculated() const { return m_type == Type::Calculated; }
  72. bool is_absolute() const
  73. {
  74. return m_type == Type::Cm
  75. || m_type == Type::In
  76. || m_type == Type::Mm
  77. || m_type == Type::Px
  78. || m_type == Type::Pt
  79. || m_type == Type::Pc
  80. || m_type == Type::Q;
  81. }
  82. bool is_relative() const
  83. {
  84. return m_type == Type::Ex
  85. || m_type == Type::Em
  86. || m_type == Type::Ch
  87. || m_type == Type::Rem
  88. || m_type == Type::Vh
  89. || m_type == Type::Vw
  90. || m_type == Type::Vmax
  91. || m_type == Type::Vmin;
  92. }
  93. float raw_value() const { return m_value; }
  94. float to_px(Layout::Node const&) const;
  95. ALWAYS_INLINE float to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const
  96. {
  97. if (is_auto())
  98. return 0;
  99. if (is_relative())
  100. return relative_length_to_px(viewport_rect, font_metrics, root_font_size);
  101. return absolute_length_to_px();
  102. }
  103. ALWAYS_INLINE float absolute_length_to_px() const
  104. {
  105. constexpr float inch_pixels = 96.0f;
  106. constexpr float centimeter_pixels = (inch_pixels / 2.54f);
  107. switch (m_type) {
  108. case Type::Cm:
  109. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  110. case Type::In:
  111. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  112. case Type::Px:
  113. return m_value; // 1px = 1/96th of 1in
  114. case Type::Pt:
  115. return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
  116. case Type::Pc:
  117. return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
  118. case Type::Mm:
  119. return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
  120. case Type::Q:
  121. return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
  122. default:
  123. VERIFY_NOT_REACHED();
  124. }
  125. }
  126. String to_string() const
  127. {
  128. if (is_auto())
  129. return "auto";
  130. return String::formatted("{}{}", m_value, unit_name());
  131. }
  132. bool operator==(const Length& other) const
  133. {
  134. return m_type == other.m_type && m_value == other.m_value;
  135. }
  136. bool operator!=(const Length& other) const
  137. {
  138. return !(*this == other);
  139. }
  140. void set_calculated_style(CalculatedStyleValue* value) { m_calculated_style = value; }
  141. float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const;
  142. private:
  143. float relative_length_to_px(const Layout::Node&) const;
  144. float resolve_calculated_value(const Layout::Node& layout_node, float reference_for_percent) const;
  145. const char* unit_name() const;
  146. Type m_type { Type::Undefined };
  147. float m_value { 0 };
  148. RefPtr<CalculatedStyleValue> m_calculated_style;
  149. };
  150. }