Skip to content

Commit a263ae2

Browse files
Apply a clip rectangle if a color filter is used (#229)
Skia's SkCanvas::saveLayer does not implicitly clip to the bounds rect. See flutter/flutter#142620
1 parent 8351705 commit a263ae2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

packages/vector_graphics/lib/src/render_vector_graphic.dart

+1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ class RenderPictureVectorGraphic extends RenderBox {
439439
context.canvas.translate(offset.dx, offset.dy);
440440
}
441441
if (_opacityValue != 1.0 || colorFilter != null) {
442+
context.canvas.clipRect(Offset.zero & size);
442443
context.canvas.saveLayer(Offset.zero & size, colorPaint);
443444
}
444445
context.canvas.drawPicture(pictureInfo.picture);

packages/vector_graphics/test/render_vector_graphics_test.dart

+35
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,28 @@ void main() {
408408

409409
expect(data.dispose, returnsNormally);
410410
});
411+
412+
test('Color filter applies clip', () async {
413+
final RenderPictureVectorGraphic render = RenderPictureVectorGraphic(
414+
pictureInfo,
415+
const ui.ColorFilter.mode(Colors.green, ui.BlendMode.difference),
416+
null,
417+
);
418+
render.layout(BoxConstraints.tight(const Size(50, 50)));
419+
final FakePaintingContext context = FakePaintingContext();
420+
render.paint(context, Offset.zero);
421+
422+
expect(context.canvas.lastClipRect,
423+
equals(const ui.Rect.fromLTRB(0, 0, 50, 50)));
424+
});
411425
}
412426

413427
class FakeCanvas extends Fake implements Canvas {
414428
ui.Image? lastImage;
415429
Rect? lastSrc;
416430
Rect? lastDst;
417431
Paint? lastPaint;
432+
Rect? lastClipRect;
418433

419434
@override
420435
void drawImageRect(ui.Image image, Rect src, Rect dst, Paint paint) {
@@ -423,6 +438,26 @@ class FakeCanvas extends Fake implements Canvas {
423438
lastDst = dst;
424439
lastPaint = paint;
425440
}
441+
442+
@override
443+
void drawPicture(ui.Picture picture) {}
444+
445+
@override
446+
int getSaveCount() {
447+
return 0;
448+
}
449+
450+
@override
451+
void restoreToCount(int count) {}
452+
453+
@override
454+
void saveLayer(Rect? bounds, Paint paint) {}
455+
456+
@override
457+
void clipRect(ui.Rect rect,
458+
{ui.ClipOp clipOp = ui.ClipOp.intersect, bool doAntiAlias = true}) {
459+
lastClipRect = rect;
460+
}
426461
}
427462

428463
class FakeHistoryCanvas extends Fake implements Canvas {

0 commit comments

Comments
 (0)