Length.h 4.7 KB

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