CanvasPattern.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/HTML/CanvasPattern.h>
  9. #include <LibWeb/HTML/CanvasRenderingContext2D.h>
  10. namespace Web::HTML {
  11. void CanvasPatternPaintStyle::paint(Gfx::IntRect physical_bounding_box, PaintFunction paint) const
  12. {
  13. // 1. Create an infinite transparent black bitmap.
  14. // *waves magic wand 🪄*
  15. // Done!
  16. // 2. Place a copy of the image on the bitmap, anchored such that its top left corner
  17. // is at the origin of the coordinate space, with one coordinate space unit per CSS pixel of the image,
  18. // then place repeated copies of this image horizontally to the left and right, if the repetition behavior
  19. // is "repeat-x", or vertically up and down, if the repetition behavior is "repeat-y", or in all four directions
  20. // all over the bitmap, if the repetition behavior is "repeat".
  21. // FIMXE: If the original image data is a bitmap image, then the value painted at a point in the area of
  22. // the repetitions is computed by filtering the original image data. When scaling up, if the imageSmoothingEnabled
  23. // attribute is set to false, then the image must be rendered using nearest-neighbor interpolation.
  24. // Otherwise, the user agent may use any filtering algorithm (for example bilinear interpolation or nearest-neighbor).
  25. // User agents which support multiple filtering algorithms may use the value of the imageSmoothingQuality attribute
  26. // to guide the choice of filtering algorithm. When such a filtering algorithm requires a pixel value from outside
  27. // the original image data, it must instead use the value from wrapping the pixel's coordinates to the original
  28. // image's dimensions. (That is, the filter uses 'repeat' behavior, regardless of the value of the pattern's repetition behavior.)
  29. // FIXME: 3. Transform the resulting bitmap according to the pattern's transformation matrix.
  30. // FIXME: 4. Transform the resulting bitmap again, this time according to the current transformation matrix.
  31. // 5. Replace any part of the image outside the area in which the pattern is to be rendered with transparent black.
  32. // 6. The resulting bitmap is what is to be rendered, with the same origin and same scale.
  33. auto const bitmap_width = m_bitmap->width();
  34. auto const bitmap_height = m_bitmap->height();
  35. paint([=, this](auto point) {
  36. point.translate_by(physical_bounding_box.location());
  37. point = [&]() -> Gfx::IntPoint {
  38. switch (m_repetition) {
  39. case Repetition::NoRepeat: {
  40. return point;
  41. }
  42. case Repetition::Repeat: {
  43. return {
  44. point.x() % bitmap_width,
  45. point.y() % bitmap_height
  46. };
  47. }
  48. case Repetition::RepeatX: {
  49. return {
  50. point.x() % bitmap_width,
  51. point.y()
  52. };
  53. }
  54. case Repetition::RepeatY: {
  55. return {
  56. point.x(),
  57. point.y() % bitmap_height
  58. };
  59. }
  60. default:
  61. VERIFY_NOT_REACHED();
  62. }
  63. }();
  64. if (m_bitmap->rect().contains(point))
  65. return m_bitmap->get_pixel(point);
  66. return Gfx::Color();
  67. });
  68. }
  69. CanvasPattern::CanvasPattern(JS::Realm& realm, CanvasPatternPaintStyle& pattern)
  70. : PlatformObject(realm)
  71. , m_pattern(pattern)
  72. {
  73. }
  74. CanvasPattern::~CanvasPattern() = default;
  75. // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createpattern
  76. WebIDL::ExceptionOr<JS::GCPtr<CanvasPattern>> CanvasPattern::create(JS::Realm& realm, CanvasImageSource const& image, StringView repetition)
  77. {
  78. auto parse_repetition = [&](auto repetition) -> Optional<CanvasPatternPaintStyle::Repetition> {
  79. if (repetition == "repeat"sv)
  80. return CanvasPatternPaintStyle::Repetition::Repeat;
  81. if (repetition == "repeat-x"sv)
  82. return CanvasPatternPaintStyle::Repetition::RepeatX;
  83. if (repetition == "repeat-y"sv)
  84. return CanvasPatternPaintStyle::Repetition::RepeatY;
  85. if (repetition == "no-repeat"sv)
  86. return CanvasPatternPaintStyle::Repetition::NoRepeat;
  87. return {};
  88. };
  89. // 1. Let usability be the result of checking the usability of image.
  90. auto usability = TRY(check_usability_of_image(image));
  91. // 2. If usability is bad, then return null.
  92. if (usability == CanvasImageSourceUsability::Bad)
  93. return JS::GCPtr<CanvasPattern> {};
  94. // 3. Assert: usability is good.
  95. VERIFY(usability == CanvasImageSourceUsability::Good);
  96. // 4. If repetition is the empty string, then set it to "repeat".
  97. if (repetition.is_empty())
  98. repetition = "repeat"sv;
  99. // 5. If repetition is not identical to one of "repeat", "repeat-x", "repeat-y", or "no-repeat",
  100. // then throw a "SyntaxError" DOMException.
  101. auto repetition_value = parse_repetition(repetition);
  102. if (!repetition_value.has_value())
  103. return WebIDL::SyntaxError::create(realm, "Repetition value is not valid");
  104. // Note: Bitmap won't be null here, as if it were it would have "bad" usability.
  105. auto const& bitmap = *image.visit([](auto const& source) -> Gfx::Bitmap const* { return source->bitmap(); });
  106. // 6. Let pattern be a new CanvasPattern object with the image image and the repetition behavior given by repetition.
  107. auto pattern = TRY_OR_THROW_OOM(realm.vm(), CanvasPatternPaintStyle::create(bitmap, *repetition_value));
  108. // FIXME: 7. If image is not origin-clean, then mark pattern as not origin-clean.
  109. // 8. Return pattern.
  110. return MUST_OR_THROW_OOM(realm.heap().allocate<CanvasPattern>(realm, realm, *pattern));
  111. }
  112. JS::ThrowCompletionOr<void> CanvasPattern::initialize(JS::Realm& realm)
  113. {
  114. MUST_OR_THROW_OOM(Base::initialize(realm));
  115. set_prototype(&Bindings::ensure_web_prototype<Bindings::CanvasPatternPrototype>(realm, "CanvasPattern"));
  116. return {};
  117. }
  118. }