Skip to content

Commit 3d05e51

Browse files
[vector_graphics_compiler] Ignore unrecognized font-weight values (flutter#12199)
Fixes flutter#186933 `SvgParser.parseFontWeight` throws on any value outside the recognized CSS keywords and numeric weights. That makes the whole SVG fail to compile for non-standard values such as `font-weight="regular"`. This change treats an unrecognized value as an invalid declaration: it emits a warning and returns no parsed value, so normal style inheritance still applies. An unstyled root therefore keeps the initial normal weight, while an invalid child declaration no longer overrides a bold parent. Strict `warningsAsErrors` builds continue to throw. Tests cover an unrecognized root value, inheritance from a bold parent, and the warnings-as-errors path. `flutter analyze` and the full 333-test `vector_graphics_compiler` suite pass locally. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent cd39027 commit 3d05e51

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎packages/vector_graphics_compiler/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 1.3.0
2+
3+
* An unrecognized `font-weight` value is now ignored with a warning (and still
4+
throws when `warningsAsErrors` is set) instead of always throwing. This
5+
preserves an inherited weight, or the initial normal weight at the root, and
6+
lets the compiler tolerate non-standard values such as `regular` emitted by
7+
some SVG generators.
8+
19
## 1.2.6
210

311
* Fixes `linux-arm64` host support by selecting the Flutter engine

‎packages/vector_graphics_compiler/lib/src/svg/parser.dart‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,10 @@ class SvgParser {
12951295
}
12961296

12971297
/// Parse the raw font weight string.
1298+
///
1299+
/// An unrecognized value is ignored with a warning so that inheritance can
1300+
/// determine the effective weight, or throws a [StateError] when warnings are
1301+
/// treated as errors.
12981302
FontWeight? parseFontWeight(String? fontWeight) {
12991303
if (fontWeight == null) {
13001304
return null;
@@ -1325,7 +1329,12 @@ class SvgParser {
13251329
return FontWeight.w900;
13261330
}
13271331

1328-
throw StateError('Invalid "font-weight": $fontWeight');
1332+
final errorMessage = 'Invalid "font-weight": $fontWeight';
1333+
if (_warningsAsErrors) {
1334+
throw StateError(errorMessage);
1335+
}
1336+
print('Warning: $errorMessage. Ignoring invalid value.');
1337+
return null;
13291338
}
13301339

13311340
/// Converts a SVG Color String (either a # prefixed color string or a named color) to a [Color].

‎packages/vector_graphics_compiler/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: vector_graphics_compiler
22
description: A compiler to convert SVGs to the binary format used by `package:vector_graphics`.
33
repository: https://github.com/flutter/packages/tree/main/packages/vector_graphics_compiler
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+vector_graphics%22
5-
version: 1.2.6
5+
version: 1.3.0
66

77
executables:
88
vector_graphics_compiler:

‎packages/vector_graphics_compiler/test/parser_test.dart‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,45 @@ ${[for (var i = 2; i <= 30; i++) ' <pattern id="lvl$i" width="10" height="10"
414414
]);
415415
});
416416

417+
test('Non-standard root font-weight="regular" uses initial weight', () {
418+
final VectorInstructions instructions = parseWithoutOptimizers('''
419+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
420+
<text x="10" y="20" font-size="12" font-weight="regular">Regular text</text>
421+
</svg>''');
422+
423+
expect(instructions.text.single.fontWeight, FontWeight.w400);
424+
});
425+
426+
test('Unrecognized root font-weight uses initial weight', () {
427+
final VectorInstructions instructions = parseWithoutOptimizers('''
428+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
429+
<text x="10" y="20" font-size="12" font-weight="wobbly">Some text</text>
430+
</svg>''');
431+
432+
expect(instructions.text.single.fontWeight, FontWeight.w400);
433+
});
434+
435+
test('Unrecognized font-weight preserves inherited weight', () {
436+
final VectorInstructions instructions = parseWithoutOptimizers('''
437+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
438+
<g font-weight="bold">
439+
<text x="10" y="20" font-size="12" font-weight="wobbly">Some text</text>
440+
</g>
441+
</svg>''');
442+
443+
expect(instructions.text.single.fontWeight, FontWeight.w700);
444+
});
445+
446+
test('Unrecognized font-weight throws when warnings are errors', () {
447+
expect(
448+
() => parseWithoutOptimizers('''
449+
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
450+
<text x="10" y="20" font-size="12" font-weight="wobbly">Some text</text>
451+
</svg>''', warningsAsErrors: true),
452+
throwsStateError,
453+
);
454+
});
455+
417456
test('Fill rule inheritence', () {
418457
final VectorInstructions instructions = parseWithoutOptimizers(inheritFillRule);
419458

0 commit comments

Comments
 (0)