Length.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <LibGfx/Forward.h>
  9. #include <LibWeb/Forward.h>
  10. namespace Web::CSS {
  11. class Length {
  12. public:
  13. enum class Type {
  14. Undefined,
  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. // We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
  34. // this file already. To break the cyclic dependency, we must move all method definitions out.
  35. Length();
  36. Length(int value, Type type);
  37. Length(float value, Type type);
  38. static Length make_auto();
  39. static Length make_px(float value);
  40. static Length make_calculated(NonnullRefPtr<CalculatedStyleValue>);
  41. Length percentage_of(Percentage const&) const;
  42. Length resolved(Length const& fallback_for_undefined, Layout::Node const& layout_node) const;
  43. Length resolved_or_auto(Layout::Node const& layout_node) const;
  44. Length resolved_or_zero(Layout::Node const& layout_node) const;
  45. bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; }
  46. bool is_undefined() const { return m_type == Type::Undefined; }
  47. bool is_auto() const { return m_type == Type::Auto; }
  48. bool is_calculated() const { return m_type == Type::Calculated; }
  49. bool is_absolute() const
  50. {
  51. return m_type == Type::Cm
  52. || m_type == Type::In
  53. || m_type == Type::Mm
  54. || m_type == Type::Px
  55. || m_type == Type::Pt
  56. || m_type == Type::Pc
  57. || m_type == Type::Q;
  58. }
  59. bool is_relative() const
  60. {
  61. return m_type == Type::Ex
  62. || m_type == Type::Em
  63. || m_type == Type::Ch
  64. || m_type == Type::Rem
  65. || m_type == Type::Vh
  66. || m_type == Type::Vw
  67. || m_type == Type::Vmax
  68. || m_type == Type::Vmin;
  69. }
  70. float raw_value() const { return m_value; }
  71. float to_px(Layout::Node const&) const;
  72. ALWAYS_INLINE float to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const
  73. {
  74. if (is_auto())
  75. return 0;
  76. if (is_relative())
  77. return relative_length_to_px(viewport_rect, font_metrics, root_font_size);
  78. return absolute_length_to_px();
  79. }
  80. ALWAYS_INLINE float absolute_length_to_px() const
  81. {
  82. constexpr float inch_pixels = 96.0f;
  83. constexpr float centimeter_pixels = (inch_pixels / 2.54f);
  84. switch (m_type) {
  85. case Type::Cm:
  86. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  87. case Type::In:
  88. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  89. case Type::Px:
  90. return m_value; // 1px = 1/96th of 1in
  91. case Type::Pt:
  92. return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
  93. case Type::Pc:
  94. return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
  95. case Type::Mm:
  96. return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
  97. case Type::Q:
  98. return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
  99. default:
  100. VERIFY_NOT_REACHED();
  101. }
  102. }
  103. String to_string() const
  104. {
  105. if (is_auto())
  106. return "auto";
  107. return String::formatted("{}{}", m_value, unit_name());
  108. }
  109. bool operator==(const Length& other) const
  110. {
  111. return m_type == other.m_type && m_value == other.m_value;
  112. }
  113. bool operator!=(const Length& other) const
  114. {
  115. return !(*this == other);
  116. }
  117. float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const;
  118. private:
  119. const char* unit_name() const;
  120. Type m_type { Type::Undefined };
  121. float m_value { 0 };
  122. RefPtr<CalculatedStyleValue> m_calculated_style;
  123. };
  124. }
  125. template<>
  126. struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
  127. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
  128. {
  129. return Formatter<StringView>::format(builder, length.to_string());
  130. }
  131. };