Length.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/NonnullOwnPtr.h>
  9. #include <AK/Variant.h>
  10. #include <LibGfx/Font.h>
  11. #include <LibGfx/Rect.h>
  12. #include <LibWeb/CSS/Length.h>
  13. #include <LibWeb/CSS/Percentage.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/HTML/BrowsingContext.h>
  16. #include <LibWeb/HTML/HTMLHtmlElement.h>
  17. namespace Web::CSS {
  18. Length::Length() = default;
  19. Length::Length(int value, Type type)
  20. : m_type(type)
  21. , m_value(value)
  22. {
  23. }
  24. Length::Length(float value, Type type)
  25. : m_type(type)
  26. , m_value(value)
  27. {
  28. }
  29. Length Length::make_auto()
  30. {
  31. return Length(0, Type::Auto);
  32. }
  33. Length Length::make_px(float value)
  34. {
  35. return Length(value, Type::Px);
  36. }
  37. Length Length::make_calculated(NonnullRefPtr<CalculatedStyleValue> calculated_style_value)
  38. {
  39. Length length { 0, Type::Calculated };
  40. length.m_calculated_style = move(calculated_style_value);
  41. return length;
  42. }
  43. Length Length::percentage_of(Percentage const& percentage) const
  44. {
  45. if (is_undefined_or_auto()) {
  46. dbgln("Attempting to get percentage of an undefined or auto length, this seems wrong? But for now we just return the original length.");
  47. return *this;
  48. }
  49. return Length { percentage.as_fraction() * raw_value(), m_type };
  50. }
  51. Length Length::resolved(Length const& fallback_for_undefined, Layout::Node const& layout_node) const
  52. {
  53. if (is_undefined())
  54. return fallback_for_undefined;
  55. if (is_calculated())
  56. return m_calculated_style->resolve_length(layout_node).release_value();
  57. if (is_relative())
  58. return make_px(to_px(layout_node));
  59. return *this;
  60. }
  61. Length Length::resolved_or_auto(Layout::Node const& layout_node) const
  62. {
  63. return resolved(make_auto(), layout_node);
  64. }
  65. Length Length::resolved_or_zero(Layout::Node const& layout_node) const
  66. {
  67. return resolved(make_px(0), layout_node);
  68. }
  69. float Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const
  70. {
  71. switch (m_type) {
  72. case Type::Ex:
  73. return m_value * font_metrics.x_height;
  74. case Type::Em:
  75. return m_value * font_metrics.size;
  76. case Type::Ch:
  77. // FIXME: Use layout_node.font().glyph_height() when writing-mode is not horizontal-tb (it has to be implemented first)
  78. return m_value * (font_metrics.glyph_width + font_metrics.glyph_spacing);
  79. case Type::Rem:
  80. return m_value * root_font_size;
  81. case Type::Vw:
  82. return viewport_rect.width() * (m_value / 100);
  83. case Type::Vh:
  84. return viewport_rect.height() * (m_value / 100);
  85. case Type::Vmin:
  86. return min(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  87. case Type::Vmax:
  88. return max(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  89. default:
  90. VERIFY_NOT_REACHED();
  91. }
  92. }
  93. float Length::to_px(Layout::Node const& layout_node) const
  94. {
  95. if (!layout_node.document().browsing_context())
  96. return 0;
  97. auto viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  98. auto* root_element = layout_node.document().document_element();
  99. if (!root_element || !root_element->layout_node())
  100. return 0;
  101. return to_px(viewport_rect, layout_node.font().metrics('M'), root_element->layout_node()->font().presentation_size());
  102. }
  103. const char* Length::unit_name() const
  104. {
  105. switch (m_type) {
  106. case Type::Cm:
  107. return "cm";
  108. case Type::In:
  109. return "in";
  110. case Type::Px:
  111. return "px";
  112. case Type::Pt:
  113. return "pt";
  114. case Type::Mm:
  115. return "mm";
  116. case Type::Q:
  117. return "Q";
  118. case Type::Pc:
  119. return "pc";
  120. case Type::Ex:
  121. return "ex";
  122. case Type::Em:
  123. return "em";
  124. case Type::Ch:
  125. return "ch";
  126. case Type::Rem:
  127. return "rem";
  128. case Type::Auto:
  129. return "auto";
  130. case Type::Undefined:
  131. return "undefined";
  132. case Type::Vh:
  133. return "vh";
  134. case Type::Vw:
  135. return "vw";
  136. case Type::Vmax:
  137. return "vmax";
  138. case Type::Vmin:
  139. return "vmin";
  140. case Type::Calculated:
  141. return "calculated";
  142. }
  143. VERIFY_NOT_REACHED();
  144. }
  145. }