Skip to content

Commit

Permalink
Changed the SvgIntegration to parse metadata, and include the image's…
Browse files Browse the repository at this point in the history
… width/height in the generated assets.gen.dart.
  • Loading branch information
bramp committed Jan 12, 2024
1 parent dd1c950 commit e7e515c
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 9 deletions.
49 changes: 43 additions & 6 deletions packages/core/lib/generators/integrations/svg_integration.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import 'dart:io';

import 'package:flutter_gen_core/generators/integrations/integration.dart';
import 'package:flutter_gen_core/settings/asset_type.dart';
import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';

class SvgIntegration extends Integration {
SvgIntegration(String packageName) : super(packageName);
SvgIntegration(String packageName, {this.parseMetadata = false})
: super(packageName);

String get packageExpression => isPackage ? ' = package' : '';

final bool parseMetadata;

@override
List<String> get requiredImports => [
'package:flutter/widgets.dart',
Expand All @@ -17,11 +23,11 @@ class SvgIntegration extends Integration {
String get classOutput => _classDefinition;

String get _classDefinition => '''class SvgGenImage {
const SvgGenImage(this._assetName);
const SvgGenImage(this._assetName, {this.size = null});
final String _assetName;
${isPackage ? "static const String package = '$packageName';" : ''}
final Size? size;
${isPackage ? "\n static const String package = '$packageName';" : ''}
SvgPicture svg({
Key? key,
Expand Down Expand Up @@ -76,12 +82,43 @@ class SvgIntegration extends Integration {
String get className => 'SvgGenImage';

@override
String classInstantiate(AssetType asset) =>
'SvgGenImage(\'${asset.posixStylePath}\')';
String classInstantiate(AssetType asset) {
// Query extra information about the SVG
SvgInfo? info = parseMetadata ? _getMetadata(asset) : null;

return 'SvgGenImage(\'${asset.posixStylePath}\''
'${(info != null) ? ', size: Size(${info.width}, ${info.height})' : ''}'
')';
}

SvgInfo? _getMetadata(AssetType asset) {
try {
// The SVG file is read fully, then parsed with the vector_graphics
// library. This is quite a heavy way to extract just the dimenions, but
// it's also the same way it will be eventually rendered by Flutter.
final svg = File(asset.fullPath).readAsStringSync();
final vec = parseWithoutOptimizers(svg);
return SvgInfo(vec.width, vec.height);
} catch (e) {
stderr.writeln(
'[WARNING] Failed to parse SVG \'${asset.path}\' metadata: $e');
}

return null;
}

@override
bool isSupport(AssetType asset) => asset.mime == 'image/svg+xml';

@override
bool get isConstConstructor => true;
}

/// Useful metadata about the a parsed SVG file.
/// Currently only contains the width and height.
class SvgInfo {
final double width;
final double height;

SvgInfo(this.width, this.height);
}
1 change: 1 addition & 0 deletions packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
dart_style: ^2.2.4
args: ^2.0.0
pub_semver: ^2.0.0
vector_graphics_compiler: ^1.1.9

dev_dependencies:
lints: any # Ignoring the version to allow editing across SDK versions.
Expand Down
8 changes: 8 additions & 0 deletions packages/core/test/assets_gen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ void main() {

await expectedAssetsGen(pubspec, generated, fact);
});

test('Assets with parse metadata enabled', () async {
const pubspec = 'test_resources/pubspec_assets_parse_metadata.yaml';
const fact = 'test_resources/actual_data/assets_parse_metadata.gen.dart';
const generated = 'test_resources/lib/gen/assets_parse_metadata.gen.dart';

await expectedAssetsGen(pubspec, generated, fact);
});
});

group('Test generatePackageNameForConfig', () {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test_resources/actual_data/assets.gen.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e7e515c

Please sign in to comment.