Length.h 4.4 KB

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