LengthStyleValue.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.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<StyleValue 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. }