Skip to content
Merged
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
28 changes: 3 additions & 25 deletions packages/flame_svg/lib/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import 'package:flutter_svg/flutter_svg.dart';
class Svg {
/// Creates an [Svg] with the received [pictureInfo].
/// Default [pixelRatio] is the device pixel ratio.
/// Setting [integralSize] to `true` uses integer dimensions for cache keys.
/// Setting [fixedRatio] to `true` ensures the cache uses a single entry.
/// Setting [fixedRatio] to `true` ensures the cache uses one entry
/// per rendered size/scale.
/// Default [cacheSize] is [defaultCacheSize], which is 10 as previously;
Comment on lines 13 to 17
/// specifying [unlimitedCacheSize] is the same as using a [Map] instead
/// of a [MemoryCache].
Svg(
this.pictureInfo, {
double? pixelRatio,
bool integralSize = false,
bool fixedRatio = false,
int cacheSize = defaultCacheSize,
}) : pixelRatio =
Expand All @@ -31,7 +30,6 @@ class Svg {
.views
.first
.devicePixelRatio {
_integralSize = integralSize;
_fixedRatio = fixedRatio;
_cacheSize = cacheSize;
assert(_cacheSize >= 1, 'The cache size must support at least one slot.');
Expand All @@ -46,15 +44,6 @@ class Svg {
/// The pixel ratio that this [Svg] is rendered based on.
final double pixelRatio;

/// Whether we're using integral sizes for the cache keys (default: false).
bool get integralSize => _integralSize;
set integralSize(bool integral) {
_emptyCache();
_integralSize = integral;
}

late bool _integralSize;

/// Whether we're using a fixed ratio for the cache keys (default: false).
bool get fixedRatio => _fixedRatio;
set fixedRatio(bool fixed) {
Expand Down Expand Up @@ -90,7 +79,6 @@ class Svg {
String fileName, {
AssetsCache? cache,
double? pixelRatio,
bool integralSize = false,
bool fixedRatio = false,
int cacheSize = defaultCacheSize,
String? package,
Expand All @@ -100,7 +88,6 @@ class Svg {
return Svg.loadFromString(
svgString,
pixelRatio: pixelRatio,
integralSize: integralSize,
fixedRatio: fixedRatio,
cacheSize: cacheSize,
);
Expand All @@ -110,15 +97,13 @@ class Svg {
static Future<Svg> loadFromString(
String svgString, {
double? pixelRatio,
bool integralSize = false,
bool fixedRatio = false,
int cacheSize = defaultCacheSize,
}) async {
final pictureInfo = await vg.loadPicture(SvgStringLoader(svgString), null);
return Svg(
pictureInfo,
pixelRatio: pixelRatio,
integralSize: integralSize,
fixedRatio: fixedRatio,
cacheSize: cacheSize,
);
Expand Down Expand Up @@ -177,12 +162,7 @@ class Svg {
Image _getImage(Size size, double widthRatio, double heightRatio) {
final width = size.width * widthRatio;
final height = size.height * heightRatio;
Size cacheKey;
if (fixedRatio || integralSize) {
cacheKey = Size(width.ceilToDouble(), height.ceilToDouble());
} else {
cacheKey = Size(width, height);
}
final cacheKey = Size(width.ceilToDouble(), height.ceilToDouble());
final image = _imageCache.getValue(cacheKey);
Comment on lines 162 to 166

if (image == null) {
Expand Down Expand Up @@ -239,14 +219,12 @@ extension SvgLoader on Game {
Future<Svg> loadSvg(
String fileName, {
String? package,
bool integralSize = false,
bool fixedRatio = false,
int cacheSize = Svg.defaultCacheSize,
}) => Svg.load(
fileName,
cache: assets,
package: package,
integralSize: integralSize,
fixedRatio: fixedRatio,
cacheSize: cacheSize,
);
Expand Down
Loading