Length.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/RefPtr.h>
  9. #include <AK/String.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibWeb/Forward.h>
  12. #include <LibWeb/PixelUnits.h>
  13. namespace Web::CSS {
  14. class Length {
  15. public:
  16. enum class Type {
  17. Calculated,
  18. Auto,
  19. Cm,
  20. In,
  21. Mm,
  22. Q,
  23. Px,
  24. Pt,
  25. Pc,
  26. Ex,
  27. Em,
  28. Ch,
  29. Rem,
  30. Vh,
  31. Vw,
  32. Vmax,
  33. Vmin,
  34. };
  35. static Optional<Type> unit_from_name(StringView);
  36. // We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
  37. // this file already. To break the cyclic dependency, we must move all method definitions out.
  38. Length(int value, Type type);
  39. Length(float value, Type type);
  40. ~Length();
  41. static Length make_auto();
  42. static Length make_px(CSSPixels value);
  43. static Length make_calculated(NonnullRefPtr<CalculatedStyleValue>);
  44. Length percentage_of(Percentage const&) const;
  45. Length resolved(Layout::Node const& layout_node) const;
  46. bool is_auto() const { return m_type == Type::Auto; }
  47. bool is_calculated() const { return m_type == Type::Calculated; }
  48. bool is_px() const { return m_type == Type::Px; }
  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. NonnullRefPtr<CalculatedStyleValue> calculated_style_value() const;
  72. CSSPixels to_px(Layout::Node const&) const;
  73. ALWAYS_INLINE CSSPixels to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const
  74. {
  75. if (is_auto())
  76. return 0;
  77. if (is_relative())
  78. return relative_length_to_px(viewport_rect, font_metrics, font_size, root_font_size);
  79. if (is_calculated())
  80. VERIFY_NOT_REACHED(); // We can't resolve a calculated length from here. :^(
  81. return absolute_length_to_px();
  82. }
  83. ALWAYS_INLINE CSSPixels absolute_length_to_px() const
  84. {
  85. constexpr float inch_pixels = 96.0f;
  86. constexpr float centimeter_pixels = (inch_pixels / 2.54f);
  87. switch (m_type) {
  88. case Type::Cm:
  89. return m_value * centimeter_pixels; // 1cm = 96px/2.54
  90. case Type::In:
  91. return m_value * inch_pixels; // 1in = 2.54 cm = 96px
  92. case Type::Px:
  93. return m_value; // 1px = 1/96th of 1in
  94. case Type::Pt:
  95. return m_value * ((1.0f / 72.0f) * inch_pixels); // 1pt = 1/72th of 1in
  96. case Type::Pc:
  97. return m_value * ((1.0f / 6.0f) * inch_pixels); // 1pc = 1/6th of 1in
  98. case Type::Mm:
  99. return m_value * ((1.0f / 10.0f) * centimeter_pixels); // 1mm = 1/10th of 1cm
  100. case Type::Q:
  101. return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
  102. default:
  103. VERIFY_NOT_REACHED();
  104. }
  105. }
  106. ErrorOr<String> to_string() const;
  107. // We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
  108. // this file already. To break the cyclic dependency, we must move all method definitions out.
  109. bool operator==(Length const& other) const;
  110. CSSPixels relative_length_to_px(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size) const;
  111. private:
  112. char const* unit_name() const;
  113. Type m_type;
  114. float m_value { 0 };
  115. RefPtr<CalculatedStyleValue> m_calculated_style;
  116. };
  117. }
  118. template<>
  119. struct AK::Formatter<Web::CSS::Length> : Formatter<StringView> {
  120. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Length const& length)
  121. {
  122. return Formatter<StringView>::format(builder, TRY(length.to_string()));
  123. }
  124. };