ConicGradientStyleValue.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/CSS/StyleValues/PositionStyleValue.h>
  11. #include <LibWeb/Layout/Node.h>
  12. namespace Web::CSS {
  13. String ConicGradientStyleValue::to_string() const
  14. {
  15. StringBuilder builder;
  16. if (is_repeating())
  17. builder.append("repeating-"sv);
  18. builder.append("conic-gradient("sv);
  19. bool has_from_angle = m_properties.from_angle.to_degrees() != 0;
  20. bool has_at_position = !m_properties.position->is_center();
  21. if (has_from_angle)
  22. builder.appendff("from {}", m_properties.from_angle.to_string());
  23. if (has_at_position) {
  24. if (has_from_angle)
  25. builder.append(' ');
  26. builder.appendff("at {}"sv, m_properties.position->to_string());
  27. }
  28. if (has_from_angle || has_at_position)
  29. builder.append(", "sv);
  30. serialize_color_stop_list(builder, m_properties.color_stop_list);
  31. builder.append(')');
  32. return MUST(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, Vector<Gfx::Path> const& clip_paths) const
  41. {
  42. VERIFY(m_resolved.has_value());
  43. auto destination_rect = dest_rect.to_type<int>();
  44. auto position = context.rounded_device_point(m_resolved->position).to_type<int>();
  45. context.recording_painter().fill_rect_with_conic_gradient(destination_rect, m_resolved->data, position, clip_paths);
  46. }
  47. bool ConicGradientStyleValue::equals(StyleValue const& other) const
  48. {
  49. if (type() != other.type())
  50. return false;
  51. auto& other_gradient = other.as_conic_gradient();
  52. return m_properties == other_gradient.m_properties;
  53. }
  54. float ConicGradientStyleValue::angle_degrees() const
  55. {
  56. return m_properties.from_angle.to_degrees();
  57. }
  58. }