AbstractImageStyleValue.h 2.5 KB

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