diff --git a/packages/vector_graphics_compiler/CHANGELOG.md b/packages/vector_graphics_compiler/CHANGELOG.md index 578be6722575..aea39915b176 100644 --- a/packages/vector_graphics_compiler/CHANGELOG.md +++ b/packages/vector_graphics_compiler/CHANGELOG.md @@ -1,3 +1,11 @@ +## 1.3.0 + +* An unrecognized `font-weight` value is now ignored with a warning (and still + throws when `warningsAsErrors` is set) instead of always throwing. This + preserves an inherited weight, or the initial normal weight at the root, and + lets the compiler tolerate non-standard values such as `regular` emitted by + some SVG generators. + ## 1.2.6 * Fixes `linux-arm64` host support by selecting the Flutter engine diff --git a/packages/vector_graphics_compiler/lib/src/svg/parser.dart b/packages/vector_graphics_compiler/lib/src/svg/parser.dart index cbf90242b236..99f07f81e359 100644 --- a/packages/vector_graphics_compiler/lib/src/svg/parser.dart +++ b/packages/vector_graphics_compiler/lib/src/svg/parser.dart @@ -1295,6 +1295,10 @@ class SvgParser { } /// Parse the raw font weight string. + /// + /// An unrecognized value is ignored with a warning so that inheritance can + /// determine the effective weight, or throws a [StateError] when warnings are + /// treated as errors. FontWeight? parseFontWeight(String? fontWeight) { if (fontWeight == null) { return null; @@ -1325,7 +1329,12 @@ class SvgParser { return FontWeight.w900; } - throw StateError('Invalid "font-weight": $fontWeight'); + final errorMessage = 'Invalid "font-weight": $fontWeight'; + if (_warningsAsErrors) { + throw StateError(errorMessage); + } + print('Warning: $errorMessage. Ignoring invalid value.'); + return null; } /// Converts a SVG Color String (either a # prefixed color string or a named color) to a [Color]. diff --git a/packages/vector_graphics_compiler/pubspec.yaml b/packages/vector_graphics_compiler/pubspec.yaml index db216b088853..d841e67afdf6 100644 --- a/packages/vector_graphics_compiler/pubspec.yaml +++ b/packages/vector_graphics_compiler/pubspec.yaml @@ -2,7 +2,7 @@ name: vector_graphics_compiler description: A compiler to convert SVGs to the binary format used by `package:vector_graphics`. repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics_compiler issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22 -version: 1.2.6 +version: 1.3.0 executables: vector_graphics_compiler: diff --git a/packages/vector_graphics_compiler/test/parser_test.dart b/packages/vector_graphics_compiler/test/parser_test.dart index c0b04d07f9a1..770035269be5 100644 --- a/packages/vector_graphics_compiler/test/parser_test.dart +++ b/packages/vector_graphics_compiler/test/parser_test.dart @@ -414,6 +414,45 @@ ${[for (var i = 2; i <= 30; i++) ' + Regular text +'''); + + expect(instructions.text.single.fontWeight, FontWeight.w400); + }); + + test('Unrecognized root font-weight uses initial weight', () { + final VectorInstructions instructions = parseWithoutOptimizers(''' + + Some text +'''); + + expect(instructions.text.single.fontWeight, FontWeight.w400); + }); + + test('Unrecognized font-weight preserves inherited weight', () { + final VectorInstructions instructions = parseWithoutOptimizers(''' + + + Some text + +'''); + + expect(instructions.text.single.fontWeight, FontWeight.w700); + }); + + test('Unrecognized font-weight throws when warnings are errors', () { + expect( + () => parseWithoutOptimizers(''' + + Some text +''', warningsAsErrors: true), + throwsStateError, + ); + }); + test('Fill rule inheritence', () { final VectorInstructions instructions = parseWithoutOptimizers(inheritFillRule);