Length.h 4.1 KB

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