Skip to content

Commit

Permalink
Do not render SVGs bigger than requested when pixelRatio > 1 and no m…
Browse files Browse the repository at this point in the history
…atch in _imageCache
  • Loading branch information
Paweł Lis committed Oct 5, 2023
1 parent e57cacc commit de985b1
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions packages/flame_svg/lib/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ class Svg {

if (image != null) {
canvas.save();
canvas.scale(1 / pixelRatio);
final drawPaint = overridePaint ?? _paint;
canvas.drawImage(image, Offset.zero, drawPaint);
canvas.restore();
} else {
_render(canvas, localSize);
_render(canvas, localSize, pixelRatio: 1);
}
}

Expand All @@ -79,8 +80,8 @@ class Svg {
_lock.add(size);
final recorder = PictureRecorder();
final canvas = Canvas(recorder);
canvas.scale(pixelRatio);
_render(canvas, size);

final picture = recorder.endRecording();
picture
.toImageSafe(
Expand All @@ -96,19 +97,15 @@ class Svg {
return image;
}

void _render(Canvas canvas, Size size) {
canvas.scale(pixelRatio);

void _render(Canvas canvas, Size size, {double? pixelRatio}) {
final scale = math.min(
size.width / pictureInfo.size.width / pixelRatio,
size.height / pictureInfo.size.height / pixelRatio,
size.width / pictureInfo.size.width,
size.height / pictureInfo.size.height,
);

canvas.translate(
(size.width / pixelRatio - pictureInfo.size.width * scale) / 2,
(size.height / pixelRatio - pictureInfo.size.height * scale) / 2,
(size.width - pictureInfo.size.width * scale) / 2,
(size.height - pictureInfo.size.height * scale) / 2,
);

canvas.scale(scale);
canvas.drawPicture(pictureInfo.picture);
}
Expand Down

0 comments on commit de985b1

Please sign in to comment.