Length.h 4.7 KB

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