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