PositionStyleValue.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "PositionStyleValue.h"
  10. namespace Web::CSS {
  11. bool PositionStyleValue::is_center() const
  12. {
  13. return (edge_x()->edge() == PositionEdge::Left
  14. && edge_x()->offset().is_percentage() && edge_x()->offset().percentage() == Percentage { 50 })
  15. && (edge_y()->edge() == PositionEdge::Top
  16. && edge_y()->offset().is_percentage() && edge_y()->offset().percentage() == Percentage { 50 });
  17. }
  18. CSSPixelPoint PositionStyleValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const
  19. {
  20. // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense)
  21. CSSPixels x = m_properties.edge_x->offset().to_px(node, rect.width());
  22. CSSPixels y = m_properties.edge_y->offset().to_px(node, rect.height());
  23. if (m_properties.edge_x->edge() == PositionEdge::Right)
  24. x = rect.width() - x;
  25. if (m_properties.edge_y->edge() == PositionEdge::Bottom)
  26. y = rect.height() - y;
  27. return CSSPixelPoint { rect.x() + x, rect.y() + y };
  28. }
  29. String PositionStyleValue::to_string() const
  30. {
  31. return MUST(String::formatted("{} {}", m_properties.edge_x->to_string(), m_properties.edge_y->to_string()));
  32. }
  33. }