Length.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. Ex,
  43. Em,
  44. Rem,
  45. Vh,
  46. Vw,
  47. Vmax,
  48. Vmin,
  49. };
  50. Length() { }
  51. Length(int value, Type type)
  52. : m_type(type)
  53. , m_value(value)
  54. {
  55. }
  56. Length(float value, Type type)
  57. : m_type(type)
  58. , m_value(value)
  59. {
  60. }
  61. static Length make_auto() { return Length(0, Type::Auto); }
  62. static Length make_px(float value) { return Length(value, Type::Px); }
  63. Length resolved(const Length& fallback_for_undefined, const LayoutNode& layout_node, float reference_for_percent) const
  64. {
  65. if (is_undefined())
  66. return fallback_for_undefined;
  67. if (is_percentage())
  68. return make_px(raw_value() / 100.0 * reference_for_percent);
  69. if (is_relative())
  70. return make_px(to_px(layout_node));
  71. return *this;
  72. }
  73. Length resolved_or_auto(const LayoutNode& layout_node, float reference_for_percent) const
  74. {
  75. return resolved(make_auto(), layout_node, reference_for_percent);
  76. }
  77. Length resolved_or_zero(const LayoutNode& layout_node, float reference_for_percent) const
  78. {
  79. return resolved(make_px(0), layout_node, reference_for_percent);
  80. }
  81. bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; }
  82. bool is_undefined() const { return m_type == Type::Undefined; }
  83. bool is_percentage() const { return m_type == Type::Percentage; }
  84. bool is_auto() const { return m_type == Type::Auto; }
  85. bool is_absolute() const
  86. {
  87. return m_type == Type::Cm
  88. || m_type == Type::In
  89. || m_type == Type::Mm
  90. || m_type == Type::Px
  91. || m_type == Type::Pt
  92. || m_type == Type::Q;
  93. }
  94. bool is_relative() const
  95. {
  96. return m_type == Type::Ex
  97. || m_type == Type::Em
  98. || m_type == Type::Rem
  99. || m_type == Type::Vh
  100. || m_type == Type::Vw
  101. || m_type == Type::Vmax
  102. || m_type == Type::Vmin;
  103. }
  104. float raw_value() const { return m_value; }
  105. ALWAYS_INLINE float to_px(const LayoutNode& layout_node) const
  106. {
  107. if (is_relative())
  108. return relative_length_to_px(layout_node);
  109. switch (m_type) {
  110. case Type::Auto:
  111. return 0;
  112. case Type::Cm:
  113. return m_value * (96.0f / 2.54f); // 1cm = 96px/2.54
  114. case Type::In:
  115. return m_value * 96.0f; // 1in = 2.54 cm = 96px
  116. case Type::Px:
  117. return m_value; // 1px = 1/96th of 1in
  118. case Type::Pt:
  119. return m_value * 1.33333333f; // 1pt = 1/72th of 1in
  120. case Type::Mm:
  121. return m_value * (0.1f * (96.0f / 2.54f)); // 1mm = 1/10th of 1cm
  122. case Type::Q:
  123. return m_value * (0.025f * (96.0f / 2.54f)); // 1Q = 1/40th of 1cm
  124. case Type::Undefined:
  125. case Type::Percentage:
  126. default:
  127. ASSERT_NOT_REACHED();
  128. }
  129. }
  130. String to_string() const
  131. {
  132. if (is_auto())
  133. return "[auto]";
  134. return String::format("[%g %s]", m_value, unit_name());
  135. }
  136. private:
  137. float relative_length_to_px(const LayoutNode&) const;
  138. const char* unit_name() const;
  139. Type m_type { Type::Undefined };
  140. float m_value { 0 };
  141. };
  142. inline const LogStream& operator<<(const LogStream& stream, const Length& value)
  143. {
  144. return stream << value.to_string();
  145. }
  146. }