Length.h 4.2 KB

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