Length.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/DeprecatedString.h>
  8. #include <LibGfx/Forward.h>
  9. #include <LibWeb/Forward.h>
  10. #include <LibWeb/PixelUnits.h>
  11. namespace Web::CSS {
  12. class Length {
  13. public:
  14. enum class Type {
  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. static Optional<Type> unit_from_name(StringView);
  34. // We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
  35. // this file already. To break the cyclic dependency, we must move all method definitions out.
  36. Length(int value, Type type);
  37. Length(float value, Type type);
  38. Length(CSSPixels value, Type type);
  39. ~Length();
  40. static Length make_auto();
  41. static Length make_px(float value);
  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. float to_px(Layout::Node const&) const;
  73. ALWAYS_INLINE float to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float 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 float 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. DeprecatedString to_deprecated_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. float relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, float font_size, float 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, length.to_deprecated_string());
  123. }
  124. };