AbstractImageStyleValue.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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/CSSStyleValue.h>
  11. #include <LibWeb/CSS/Enums.h>
  12. #include <LibWeb/CSS/PercentageOr.h>
  13. #include <LibWeb/CSS/Serialize.h>
  14. namespace Web::CSS {
  15. class AbstractImageStyleValue : public CSSStyleValue {
  16. public:
  17. using CSSStyleValue::CSSStyleValue;
  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. virtual Optional<Gfx::Color> color_if_single_pixel_bitmap() const { return {}; }
  33. };
  34. // And now, some gradient related things. Maybe these should live somewhere else.
  35. enum class GradientRepeating {
  36. Yes,
  37. No
  38. };
  39. template<typename TPosition>
  40. struct ColorStopListElement {
  41. using PositionType = TPosition;
  42. struct ColorHint {
  43. TPosition value;
  44. inline bool operator==(ColorHint const&) const = default;
  45. };
  46. Optional<ColorHint> transition_hint;
  47. struct ColorStop {
  48. RefPtr<CSSStyleValue> color;
  49. Optional<TPosition> position;
  50. Optional<TPosition> second_position = {};
  51. inline bool operator==(ColorStop const&) const = default;
  52. } color_stop;
  53. inline bool operator==(ColorStopListElement const&) const = default;
  54. };
  55. using LinearColorStopListElement = ColorStopListElement<LengthPercentage>;
  56. using AngularColorStopListElement = ColorStopListElement<AnglePercentage>;
  57. static void serialize_color_stop_list(StringBuilder& builder, auto const& color_stop_list)
  58. {
  59. bool first = true;
  60. for (auto const& element : color_stop_list) {
  61. if (!first)
  62. builder.append(", "sv);
  63. if (element.transition_hint.has_value())
  64. builder.appendff("{}, "sv, element.transition_hint->value.to_string());
  65. builder.append(element.color_stop.color->to_string());
  66. for (auto position : Array { &element.color_stop.position, &element.color_stop.second_position }) {
  67. if (position->has_value())
  68. builder.appendff(" {}"sv, (*position)->to_string());
  69. }
  70. first = false;
  71. }
  72. }
  73. }