Cleanup svg decoder

This commit is contained in:
2026-09-01 21:23:45 +02:00
parent 3290fd9db6
commit 91c70fc4af
+20 -11
View File
@@ -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.