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/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::NodeWithStyleAndBoxModelMetrics 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. RefPtr<StyleValue> 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 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. builder.append(", "sv);
  54. if (element.transition_hint.has_value())
  55. builder.appendff("{}, "sv, element.transition_hint->value.to_string());
  56. builder.append(element.color_stop.color->to_string());
  57. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  58. if (position->has_value())
  59. builder.appendff(" {}"sv, (*position)->to_string());
  60. }
  61. first = false;
  62. }
  63. }
  64. }