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 4, 2023
1 parent 0f679b3 commit 2e0e455
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions packages/flame_svg/lib/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class Svg {
/// Creates an [Svg] with the received [pictureInfo].
/// Default [pixelRatio] is the device pixel ratio.
Svg(this.pictureInfo, {double? pixelRatio})
: pixelRatio = pixelRatio ??
PlatformDispatcher.instance.views.first.devicePixelRatio;
: pixelRatio = pixelRatio ?? PlatformDispatcher.instance.views.first.devicePixelRatio;

final MemoryCache<Size, Image> _imageCache = MemoryCache();

Expand Down Expand Up @@ -54,7 +53,6 @@ class Svg {

if (image != null) {
canvas.save();
canvas.scale(1 / pixelRatio);
final drawPaint = overridePaint ?? _paint;
canvas.drawImage(image, Offset.zero, drawPaint);
canvas.restore();
Expand All @@ -81,6 +79,7 @@ class Svg {
final recorder = PictureRecorder();
final canvas = Canvas(recorder);
_render(canvas, size);

final picture = recorder.endRecording();
picture
.toImageSafe(
Expand All @@ -100,12 +99,8 @@ class Svg {
canvas.scale(pixelRatio);

final scale = math.min(
size.width / pictureInfo.size.width,
size.height / pictureInfo.size.height,
);
canvas.translate(
(size.width - pictureInfo.size.width * scale) / 2,
(size.height - pictureInfo.size.height * scale) / 2,
size.width / pictureInfo.size.width / pixelRatio,
size.height / pictureInfo.size.height / pixelRatio,
);
canvas.scale(scale);
canvas.drawPicture(pictureInfo.picture);
Expand Down

0 comments on commit 2e0e455

Please sign in to comment.