Length.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. Length percentage_of(Percentage const&) const;
  41. Length resolved(Length const& fallback_for_undefined, Layout::Node const& layout_node) const;
  42. Length resolved_or_auto(Layout::Node const& layout_node) const;
  43. Length resolved_or_zero(Layout::Node const& layout_node) const;
  44. bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; }
  45. bool is_undefined() const { return m_type == Type::Undefined; }
  46. bool is_auto() const { return m_type == Type::Auto; }
  47. bool is_calculated() const { return m_type == Type::Calculated; }
  48. bool is_absolute() const
  49. {
  50. return m_type == Type::Cm
  51. || m_type == Type::In
  52. || m_type == Type::Mm
  53. || m_type == Type::Px
  54. || m_type == Type::Pt
  55. || m_type == Type::Pc
  56. || m_type == Type::Q;
  57. }
  58. bool is_relative() const
  59. {
  60. return m_type == Type::Ex
  61. || m_type == Type::Em
  62. || m_type == Type::Ch
  63. || m_type == Type::Rem
  64. || m_type == Type::Vh
  65. || m_type == Type::Vw
  66. || m_type == Type::Vmax
  67. || m_type == Type::Vmin;
  68. }
  69. float raw_value() const { return m_value; }
  70. float to_px(Layout::Node const&) const;
  71. ALWAYS_INLINE float to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const
  72. {
  73. if (is_auto())
  74. return 0;
  75. if (is_relative())
  76. return relative_length_to_px(viewport_rect, font_metrics, root_font_size);
  77. return absolute_length_to_px();
  78. }
  79. ALWAYS_INLINE float absolute_length_to_px() const
  80. {
  81. constexpr float inch_pixels = 96.0f;
  82. constexpr float centimeter_pixels = (inch_pixels / 2.54f);
  83. switch (m_type) {
  84. case Type::Cm:
  85. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  86. case Type::In:
  87. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  88. case Type::Px:
  89. return m_value; // 1px = 1/96th of 1in
  90. case Type::Pt:
  91. return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
  92. case Type::Pc:
  93. return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
  94. case Type::Mm:
  95. return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
  96. case Type::Q:
  97. return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
  98. default:
  99. VERIFY_NOT_REACHED();
  100. }
  101. }
  102. String to_string() const
  103. {
  104. if (is_auto())
  105. return "auto";
  106. return String::formatted("{}{}", m_value, unit_name());
  107. }
  108. bool operator==(const Length& other) const
  109. {
  110. return m_type == other.m_type && m_value == other.m_value;
  111. }
  112. bool operator!=(const Length& other) const
  113. {
  114. return !(*this == other);
  115. }
  116. void set_calculated_style(CalculatedStyleValue* value);
  117. float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const;
  118. private:
  119. float resolve_calculated_value(Layout::Node const& layout_node) const;
  120. const char* unit_name() const;
  121. Type m_type { Type::Undefined };
  122. float m_value { 0 };
  123. RefPtr<CalculatedStyleValue> m_calculated_style;
  124. };
  125. }
  126. template<>
  127. struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
  128. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
  129. {
  130. return Formatter<StringView>::format(builder, length.to_string());
  131. }
  132. };