LengthStyleValue.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 "LengthStyleValue.h"
  10. namespace Web::CSS {
  11. ValueComparingNonnullRefPtr<LengthStyleValue> LengthStyleValue::create(Length const& length)
  12. {
  13. VERIFY(!length.is_auto());
  14. if (length.is_px()) {
  15. if (length.raw_value() == 0) {
  16. static auto value = adopt_ref(*new (nothrow) LengthStyleValue(CSS::Length::make_px(0)));
  17. return value;
  18. }
  19. if (length.raw_value() == 1) {
  20. static auto value = adopt_ref(*new (nothrow) LengthStyleValue(CSS::Length::make_px(1)));
  21. return value;
  22. }
  23. }
  24. return adopt_ref(*new (nothrow) LengthStyleValue(length));
  25. }
  26. ValueComparingNonnullRefPtr<CSSStyleValue const> LengthStyleValue::absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
  27. {
  28. if (auto length = m_length.absolutize(viewport_rect, font_metrics, root_font_metrics); length.has_value())
  29. return LengthStyleValue::create(length.release_value());
  30. return *this;
  31. }
  32. bool LengthStyleValue::equals(CSSStyleValue const& other) const
  33. {
  34. if (type() != other.type())
  35. return false;
  36. auto const& other_length = other.as_length();
  37. return m_length == other_length.m_length;
  38. }
  39. }