ConicGradientStyleValue.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 "ConicGradientStyleValue.h"
  10. #include <LibWeb/Layout/Node.h>
  11. namespace Web::CSS {
  12. ErrorOr<String> ConicGradientStyleValue::to_string() const
  13. {
  14. StringBuilder builder;
  15. if (is_repeating())
  16. TRY(builder.try_append("repeating-"sv));
  17. TRY(builder.try_append("conic-gradient("sv));
  18. bool has_from_angle = false;
  19. bool has_at_position = false;
  20. if ((has_from_angle = m_properties.from_angle.to_degrees() != 0))
  21. TRY(builder.try_appendff("from {}", TRY(m_properties.from_angle.to_string())));
  22. if ((has_at_position = m_properties.position != PositionValue::center())) {
  23. if (has_from_angle)
  24. TRY(builder.try_append(' '));
  25. TRY(builder.try_appendff("at "sv));
  26. TRY(m_properties.position.serialize(builder));
  27. }
  28. if (has_from_angle || has_at_position)
  29. TRY(builder.try_append(", "sv));
  30. TRY(serialize_color_stop_list(builder, m_properties.color_stop_list));
  31. TRY(builder.try_append(')'));
  32. return builder.to_string();
  33. }
  34. void ConicGradientStyleValue::resolve_for_size(Layout::NodeWithStyleAndBoxModelMetrics const& node, CSSPixelSize size) const
  35. {
  36. if (!m_resolved.has_value())
  37. m_resolved = ResolvedData { Painting::resolve_conic_gradient_data(node, *this), {} };
  38. m_resolved->position = m_properties.position.resolved(node, CSSPixelRect { { 0, 0 }, size });
  39. }
  40. void ConicGradientStyleValue::paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering) const
  41. {
  42. VERIFY(m_resolved.has_value());
  43. Painting::paint_conic_gradient(context, dest_rect, m_resolved->data, context.rounded_device_point(m_resolved->position));
  44. }
  45. bool ConicGradientStyleValue::equals(StyleValue const& other) const
  46. {
  47. if (type() != other.type())
  48. return false;
  49. auto& other_gradient = other.as_conic_gradient();
  50. return m_properties == other_gradient.m_properties;
  51. }
  52. float ConicGradientStyleValue::angle_degrees() const
  53. {
  54. return m_properties.from_angle.to_degrees();
  55. }
  56. }