AbstractImageStyleValue.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 Optional<CSSPixelFraction> natural_aspect_ratio() const
  21. {
  22. auto width = natural_width();
  23. auto height = natural_height();
  24. if (width.has_value() && height.has_value())
  25. return *width / *height;
  26. return {};
  27. }
  28. virtual void load_any_resources(DOM::Document&) {};
  29. virtual void resolve_for_size(Layout::NodeWithStyleAndBoxModelMetrics const&, CSSPixelSize) const {};
  30. virtual bool is_paintable() const = 0;
  31. virtual void paint(PaintContext& context, DevicePixelRect const& dest_rect, ImageRendering) const = 0;
  32. };
  33. // And now, some gradient related things. Maybe these should live somewhere else.
  34. enum class GradientRepeating {
  35. Yes,
  36. No
  37. };
  38. template<typename TPosition>
  39. struct ColorStopListElement {
  40. using PositionType = TPosition;
  41. struct ColorHint {
  42. TPosition value;
  43. inline bool operator==(ColorHint const&) const = default;
  44. };
  45. Optional<ColorHint> transition_hint;
  46. struct ColorStop {
  47. RefPtr<StyleValue> color;
  48. Optional<TPosition> position;
  49. Optional<TPosition> second_position = {};
  50. inline bool operator==(ColorStop const&) const = default;
  51. } color_stop;
  52. inline bool operator==(ColorStopListElement const&) const = default;
  53. };
  54. using LinearColorStopListElement = ColorStopListElement<LengthPercentage>;
  55. using AngularColorStopListElement = ColorStopListElement<AnglePercentage>;
  56. static void serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list)
  57. {
  58. bool first = true;
  59. for (auto const& element : color_stop_list) {
  60. if (!first)
  61. builder.append(", "sv);
  62. if (element.transition_hint.has_value())
  63. builder.appendff("{}, "sv, element.transition_hint->value.to_string());
  64. builder.append(element.color_stop.color->to_string());
  65. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  66. if (position->has_value())
  67. builder.appendff(" {}"sv, (*position)->to_string());
  68. }
  69. first = false;
  70. }
  71. }
  72. }