Length.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/String.h>
  28. #include <LibWeb/Forward.h>
  29. namespace Web::CSS {
  30. class Length {
  31. public:
  32. enum class Type {
  33. Undefined,
  34. Percentage,
  35. Auto,
  36. Cm,
  37. In,
  38. Mm,
  39. Q,
  40. Px,
  41. Pt,
  42. Pc,
  43. Ex,
  44. Em,
  45. Rem,
  46. Vh,
  47. Vw,
  48. Vmax,
  49. Vmin,
  50. };
  51. Length() { }
  52. Length(int value, Type type)
  53. : m_type(type)
  54. , m_value(value)
  55. {
  56. }
  57. Length(float value, Type type)
  58. : m_type(type)
  59. , m_value(value)
  60. {
  61. }
  62. static Length make_auto() { return Length(0, Type::Auto); }
  63. static Length make_px(float value) { return Length(value, Type::Px); }
  64. Length resolved(const Length& fallback_for_undefined, const Layout::Node& layout_node, float reference_for_percent) const
  65. {
  66. if (is_undefined())
  67. return fallback_for_undefined;
  68. if (is_percentage())
  69. return make_px(raw_value() / 100.0f * reference_for_percent);
  70. if (is_relative())
  71. return make_px(to_px(layout_node));
  72. return *this;
  73. }
  74. Length resolved_or_auto(const Layout::Node& layout_node, float reference_for_percent) const
  75. {
  76. return resolved(make_auto(), layout_node, reference_for_percent);
  77. }
  78. Length resolved_or_zero(const Layout::Node& layout_node, float reference_for_percent) const
  79. {
  80. return resolved(make_px(0), layout_node, reference_for_percent);
  81. }
  82. bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; }
  83. bool is_undefined() const { return m_type == Type::Undefined; }
  84. bool is_percentage() const { return m_type == Type::Percentage; }
  85. bool is_auto() const { return m_type == Type::Auto; }
  86. bool is_absolute() const
  87. {
  88. return m_type == Type::Cm
  89. || m_type == Type::In
  90. || m_type == Type::Mm
  91. || m_type == Type::Px
  92. || m_type == Type::Pt
  93. || m_type == Type::Pc
  94. || m_type == Type::Q;
  95. }
  96. bool is_relative() const
  97. {
  98. return m_type == Type::Ex
  99. || m_type == Type::Em
  100. || m_type == Type::Rem
  101. || m_type == Type::Vh
  102. || m_type == Type::Vw
  103. || m_type == Type::Vmax
  104. || m_type == Type::Vmin;
  105. }
  106. float raw_value() const { return m_value; }
  107. ALWAYS_INLINE float to_px(const Layout::Node& layout_node) const
  108. {
  109. if (is_relative())
  110. return relative_length_to_px(layout_node);
  111. constexpr float inch_pixels = 96.0f;
  112. constexpr float centimeter_pixels = (inch_pixels / 2.54f);
  113. switch (m_type) {
  114. case Type::Auto:
  115. return 0;
  116. case Type::Cm:
  117. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  118. case Type::In:
  119. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  120. case Type::Px:
  121. return m_value; // 1px = 1/96th of 1in
  122. case Type::Pt:
  123. return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
  124. case Type::Pc:
  125. return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
  126. case Type::Mm:
  127. return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
  128. case Type::Q:
  129. return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
  130. case Type::Undefined:
  131. case Type::Percentage:
  132. default:
  133. VERIFY_NOT_REACHED();
  134. }
  135. }
  136. String to_string() const
  137. {
  138. if (is_auto())
  139. return "[auto]";
  140. return String::formatted("[{} {}]", m_value, unit_name());
  141. }
  142. bool operator==(const Length& other) const
  143. {
  144. return m_type == other.m_type && m_value == other.m_value;
  145. }
  146. bool operator!=(const Length& other) const
  147. {
  148. return !(*this == other);
  149. }
  150. private:
  151. float relative_length_to_px(const Layout::Node&) const;
  152. const char* unit_name() const;
  153. Type m_type { Type::Undefined };
  154. float m_value { 0 };
  155. };
  156. }