Length.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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::percentage_of(Percentage const& percentage) const
  38. {
  39. if (is_undefined_or_auto()) {
  40. dbgln("Attempting to get percentage of an undefined or auto length, this seems wrong? But for now we just return the original length.");
  41. return *this;
  42. }
  43. return Length { percentage.as_fraction() * raw_value(), m_type };
  44. }
  45. Length Length::resolved(Length const& fallback_for_undefined, Layout::Node const& layout_node) const
  46. {
  47. if (is_undefined())
  48. return fallback_for_undefined;
  49. if (is_calculated())
  50. return m_calculated_style->resolve_length(layout_node).release_value();
  51. if (is_relative())
  52. return make_px(to_px(layout_node));
  53. return *this;
  54. }
  55. Length Length::resolved_or_auto(Layout::Node const& layout_node) const
  56. {
  57. return resolved(make_auto(), layout_node);
  58. }
  59. Length Length::resolved_or_zero(Layout::Node const& layout_node) const
  60. {
  61. return resolved(make_px(0), layout_node);
  62. }
  63. void Length::set_calculated_style(CalculatedStyleValue* value)
  64. {
  65. m_calculated_style = value;
  66. }
  67. float Length::relative_length_to_px(Gfx::IntRect const& viewport_rect, Gfx::FontMetrics const& font_metrics, float root_font_size) const
  68. {
  69. switch (m_type) {
  70. case Type::Ex:
  71. return m_value * font_metrics.x_height;
  72. case Type::Em:
  73. return m_value * font_metrics.size;
  74. case Type::Ch:
  75. // FIXME: Use layout_node.font().glyph_height() when writing-mode is not horizontal-tb (it has to be implemented first)
  76. return m_value * (font_metrics.glyph_width + font_metrics.glyph_spacing);
  77. case Type::Rem:
  78. return m_value * root_font_size;
  79. case Type::Vw:
  80. return viewport_rect.width() * (m_value / 100);
  81. case Type::Vh:
  82. return viewport_rect.height() * (m_value / 100);
  83. case Type::Vmin:
  84. return min(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  85. case Type::Vmax:
  86. return max(viewport_rect.width(), viewport_rect.height()) * (m_value / 100);
  87. default:
  88. VERIFY_NOT_REACHED();
  89. }
  90. }
  91. float Length::to_px(Layout::Node const& layout_node) const
  92. {
  93. if (!layout_node.document().browsing_context())
  94. return 0;
  95. auto viewport_rect = layout_node.document().browsing_context()->viewport_rect();
  96. auto* root_element = layout_node.document().document_element();
  97. if (!root_element || !root_element->layout_node())
  98. return 0;
  99. return to_px(viewport_rect, layout_node.font().metrics('M'), root_element->layout_node()->font().presentation_size());
  100. }
  101. const char* Length::unit_name() const
  102. {
  103. switch (m_type) {
  104. case Type::Cm:
  105. return "cm";
  106. case Type::In:
  107. return "in";
  108. case Type::Px:
  109. return "px";
  110. case Type::Pt:
  111. return "pt";
  112. case Type::Mm:
  113. return "mm";
  114. case Type::Q:
  115. return "Q";
  116. case Type::Pc:
  117. return "pc";
  118. case Type::Ex:
  119. return "ex";
  120. case Type::Em:
  121. return "em";
  122. case Type::Ch:
  123. return "ch";
  124. case Type::Rem:
  125. return "rem";
  126. case Type::Auto:
  127. return "auto";
  128. case Type::Undefined:
  129. return "undefined";
  130. case Type::Vh:
  131. return "vh";
  132. case Type::Vw:
  133. return "vw";
  134. case Type::Vmax:
  135. return "vmax";
  136. case Type::Vmin:
  137. return "vmin";
  138. case Type::Calculated:
  139. return "calculated";
  140. }
  141. VERIFY_NOT_REACHED();
  142. }
  143. }