Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply a clip rectangle if a color filter is used #229

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ class RenderPictureVectorGraphic extends RenderBox {
context.canvas.translate(offset.dx, offset.dy);
}
if (_opacityValue != 1.0 || colorFilter != null) {
context.canvas.clipRect(Offset.zero & size);
context.canvas.saveLayer(Offset.zero & size, colorPaint);
}
context.canvas.drawPicture(pictureInfo.picture);
Expand Down
35 changes: 35 additions & 0 deletions packages/vector_graphics/test/render_vector_graphics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,28 @@ void main() {

expect(data.dispose, returnsNormally);
});

test('Color filter applies clip', () async {
final RenderPictureVectorGraphic render = RenderPictureVectorGraphic(
pictureInfo,
const ui.ColorFilter.mode(Colors.green, ui.BlendMode.difference),
null,
);
render.layout(BoxConstraints.tight(const Size(50, 50)));
final FakePaintingContext context = FakePaintingContext();
render.paint(context, Offset.zero);

expect(context.canvas.lastClipRect,
equals(const ui.Rect.fromLTRB(0, 0, 50, 50)));
});
}

class FakeCanvas extends Fake implements Canvas {
ui.Image? lastImage;
Rect? lastSrc;
Rect? lastDst;
Paint? lastPaint;
Rect? lastClipRect;

@override
void drawImageRect(ui.Image image, Rect src, Rect dst, Paint paint) {
Expand All @@ -423,6 +438,26 @@ class FakeCanvas extends Fake implements Canvas {
lastDst = dst;
lastPaint = paint;
}

@override
void drawPicture(ui.Picture picture) {}

@override
int getSaveCount() {
return 0;
}

@override
void restoreToCount(int count) {}

@override
void saveLayer(Rect? bounds, Paint paint) {}

@override
void clipRect(ui.Rect rect,
{ui.ClipOp clipOp = ui.ClipOp.intersect, bool doAntiAlias = true}) {
lastClipRect = rect;
}
}

class FakeHistoryCanvas extends Fake implements Canvas {
Expand Down
Loading