Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions packages/vector_graphics_compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 10 additions & 1 deletion packages/vector_graphics_compiler/lib/src/svg/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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].
Expand Down
2 changes: 1 addition & 1 deletion packages/vector_graphics_compiler/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions packages/vector_graphics_compiler/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,45 @@ ${[for (var i = 2; i <= 30; i++) ' <pattern id="lvl$i" width="10" height="10"
]);
});

test('Non-standard root font-weight="regular" uses initial weight', () {
final VectorInstructions instructions = parseWithoutOptimizers('''
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<text x="10" y="20" font-size="12" font-weight="regular">Regular text</text>
</svg>''');

expect(instructions.text.single.fontWeight, FontWeight.w400);
});

test('Unrecognized root font-weight uses initial weight', () {
final VectorInstructions instructions = parseWithoutOptimizers('''
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<text x="10" y="20" font-size="12" font-weight="wobbly">Some text</text>
</svg>''');

expect(instructions.text.single.fontWeight, FontWeight.w400);
});

test('Unrecognized font-weight preserves inherited weight', () {
final VectorInstructions instructions = parseWithoutOptimizers('''
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<g font-weight="bold">
<text x="10" y="20" font-size="12" font-weight="wobbly">Some text</text>
</g>
</svg>''');

expect(instructions.text.single.fontWeight, FontWeight.w700);
});

test('Unrecognized font-weight throws when warnings are errors', () {
expect(
() => parseWithoutOptimizers('''
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<text x="10" y="20" font-size="12" font-weight="wobbly">Some text</text>
</svg>''', warningsAsErrors: true),
throwsStateError,
);
});

test('Fill rule inheritence', () {
final VectorInstructions instructions = parseWithoutOptimizers(inheritFillRule);

Expand Down