From 91c70fc4aff359c981a9713d475abe7784f680fc Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Tue, 1 Sep 2026 21:23:45 +0200 Subject: [PATCH] Cleanup svg decoder --- lib/src/decoders/svg_decoder.dart | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/src/decoders/svg_decoder.dart b/lib/src/decoders/svg_decoder.dart index 83bd730..f0c40a6 100644 --- a/lib/src/decoders/svg_decoder.dart +++ b/lib/src/decoders/svg_decoder.dart @@ -35,33 +35,42 @@ class SvgDecoder implements CursorDecoder { } on Exception { throw const FormatException("invalid svg data"); } - final width = pictureInfo.size.width; - final height = pictureInfo.size.height; + + final double width = pictureInfo.size.width; + final double height = pictureInfo.size.height; if (width <= 0 || height <= 0) { pictureInfo.picture.dispose(); throw const FormatException("svg has no size"); } - final target = options.targetSize; - final ratio = target != null + final double? target = options.targetSize; + final double ratio = target != null ? (target > _maxSize ? _maxSize.toDouble() : target) / width : _shrinkRatio(width, height, options.maxSize); - final finalWidth = (width * ratio).round().clamp(1, _maxSize * 32); - final finalHeight = (height * ratio).round().clamp(1, _maxSize * 32); + final int finalWidth = (width * ratio).round().clamp(1, _maxSize * 32); + final int finalHeight = (height * ratio).round().clamp(1, _maxSize * 32); final recorder = ui.PictureRecorder(); final canvas = ui.Canvas(recorder); canvas.scale(ratio, ratio); canvas.drawPicture(pictureInfo.picture); - final picture = recorder.endRecording(); - final image = await picture.toImage(finalWidth, finalHeight); + final ui.Picture picture = recorder.endRecording(); + final ui.Image image = await picture.toImage(finalWidth, finalHeight); picture.dispose(); pictureInfo.picture.dispose(); final hotspot = options.hotspot ?? ui.Offset.zero; - final hotX = (hotspot.dx * ratio).round().clamp(0, finalWidth - 1); - final hotY = (hotspot.dy * ratio).round().clamp(0, finalHeight - 1); - return StaticCursor(Cursor(image: image, hotspotX: hotX, hotspotY: hotY)); + + final int hotX = (hotspot.dx * ratio).round().clamp(0, finalWidth - 1); + final int hotY = (hotspot.dy * ratio).round().clamp(0, finalHeight - 1); + + return StaticCursor( + Cursor( + image: image, + hotspotX: hotX, + hotspotY: hotY, + ), + ); } /// Raster-size cap for generated SVG cursors.