/
bitmap_data_load_info.dart
37 lines (32 loc) · 1.35 KB
/
bitmap_data_load_info.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
part of '../display.dart';
/// The BitmapDataLoadInfo creates information about the best matching image
/// file based on the specified image url, the pixelRatios and the current
/// display configuration.
class BitmapDataLoadInfo {
final String _sourceUrl;
String _loaderUrl;
double _pixelRatio = 1;
BitmapDataLoadInfo(String url, List<double> pixelRatios)
: _sourceUrl = url,
_loaderUrl = url {
final pixelRatioRegexp = RegExp(r'@(\d+(.\d+)?)x');
final pixelRatioMatch = pixelRatioRegexp.firstMatch(sourceUrl);
if (pixelRatioMatch != null) {
final match = pixelRatioMatch;
final originPixelRatioFractions = (match.group(2) ?? '.').length - 1;
final originPixelRatio = double.parse(match.group(1)!);
final devicePixelRatio = env.devicePixelRatio;
final loaderPixelRatio = pixelRatios.fold<num>(0.0, (a, b) {
final aDelta = (a - devicePixelRatio).abs();
final bDelta = (b - devicePixelRatio).abs();
return aDelta < bDelta && a > 0.0 ? a : b;
});
final name = loaderPixelRatio.toStringAsFixed(originPixelRatioFractions);
_loaderUrl = url.replaceRange(match.start + 1, match.end - 1, name);
_pixelRatio = loaderPixelRatio / originPixelRatio;
}
}
String get sourceUrl => _sourceUrl;
String get loaderUrl => _loaderUrl;
double get pixelRatio => _pixelRatio;
}