Length.h 4.2 KB

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