AbstractImageStyleValue.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #pragma once
  10. #include <LibWeb/CSS/Enums.h>
  11. #include <LibWeb/CSS/Serialize.h>
  12. #include <LibWeb/CSS/StyleValue.h>
  13. namespace Web::CSS {
  14. class AbstractImageStyleValue : public StyleValue {
  15. public:
  16. using StyleValue::StyleValue;
  17. virtual Optional<CSSPixels> natural_width() const { return {}; }
  18. virtual Optional<CSSPixels> natural_height() const { return {}; }
  19. virtual void load_any_resources(DOM::Document&) {};
  20. virtual void resolve_for_size(Layout::Node const&, CSSPixelSize) const {};
  21. virtual bool is_paintable() const = 0;
  22. virtual void paint(PaintContext& context, DevicePixelRect const& dest_rect, ImageRendering) const = 0;
  23. };
  24. // And now, some gradient related things. Maybe these should live somewhere else.
  25. enum class GradientRepeating {
  26. Yes,
  27. No
  28. };
  29. template<typename TPosition>
  30. struct ColorStopListElement {
  31. using PositionType = TPosition;
  32. struct ColorHint {
  33. TPosition value;
  34. inline bool operator==(ColorHint const&) const = default;
  35. };
  36. Optional<ColorHint> transition_hint;
  37. struct ColorStop {
  38. Color color;
  39. Optional<TPosition> position;
  40. Optional<TPosition> second_position = {};
  41. inline bool operator==(ColorStop const&) const = default;
  42. } color_stop;
  43. inline bool operator==(ColorStopListElement const&) const = default;
  44. };
  45. using LinearColorStopListElement = ColorStopListElement<LengthPercentage>;
  46. using AngularColorStopListElement = ColorStopListElement<AnglePercentage>;
  47. static ErrorOr<void> serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list)
  48. {
  49. bool first = true;
  50. for (auto const& element : color_stop_list) {
  51. if (!first)
  52. TRY(builder.try_append(", "sv));
  53. if (element.transition_hint.has_value())
  54. TRY(builder.try_appendff("{}, "sv, TRY(element.transition_hint->value.to_string())));
  55. TRY(serialize_a_srgb_value(builder, element.color_stop.color));
  56. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  57. if (position->has_value())
  58. TRY(builder.try_appendff(" {}"sv, TRY((*position)->to_string())));
  59. }
  60. first = false;
  61. }
  62. return {};
  63. }
  64. }