the-mews/app/tool/generate_icons.dart

51 lines
2 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'dart:ui' as ui show ImageByteFormat, PictureRecorder;
import 'package:flutter/widgets.dart' show Canvas;
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_test/flutter_test.dart';
/// Rasterizes `assets/logo.svg` into the PNGs the platforms want: the
/// Android launcher icons at every density, the splash logo, and the
/// bundled logo the Linux window icon loads. Run with:
///
/// flutter test tool/generate_icons.dart
///
/// The committed PNGs are the product; this exists so a new logo can be
/// dropped in as the SVG and every platform follows in one run.
const sizes = {
'android/app/src/main/res/mipmap-mdpi/ic_launcher.png': 48,
'android/app/src/main/res/mipmap-hdpi/ic_launcher.png': 72,
'android/app/src/main/res/mipmap-xhdpi/ic_launcher.png': 96,
'android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png': 144,
'android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png': 192,
'android/app/src/main/res/drawable-nodpi/splash_logo.png': 432,
'assets/logo.png': 256,
};
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('rasterizes the logo for every platform', () async {
final svg = File('assets/logo.svg').readAsStringSync();
final info = await vg.loadPicture(SvgStringLoader(svg), null);
for (final entry in sizes.entries) {
final size = entry.value;
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder);
// The logo is square, so one scale fits both axes.
canvas.scale(size / info.size.width, size / info.size.height);
canvas.drawPicture(info.picture);
final image = await recorder.endRecording().toImage(size, size);
final bytes = await image.toByteData(format: ui.ImageByteFormat.png);
final file = File(entry.key);
await file.parent.create(recursive: true);
await file.writeAsBytes(bytes!.buffer.asUint8List());
// ignore: avoid_print
print('wrote ${file.path} (${size}x$size)');
}
});
}