Skip to content

Commit e71b3fd

Browse files
authored
[vector_graphics_compiler] Handle coordinate lists on text and tspan (#13287)
SVG allows `x`, `y`, `dx` and `dy` on `text` and `tspan` to be a list, one value per character. The compiler parsed them as a single number, so a list like `x="0 4.86 10.01"` threw a confusing `FormatException: Invalid double`. Lists aren't supported, so it now throws an `UnimplementedError` that names the attribute and value. Single values work as before. Fixes flutter/flutter#165490 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^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 b48550d commit e71b3fd

4 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎packages/vector_graphics_compiler/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.3.3
2+
3+
* Throws an `UnimplementedError` instead of a `FormatException` when `text` or
4+
`tspan` has a list of `x`, `y`, `dx` or `dy` values, which isn't supported.
5+
16
## 1.3.2
27

38
* Fixes a `tspan` without its own position being drawn with its parent group

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,17 +1570,32 @@ class SvgParser {
15701570
return attributeMap['display'] != 'none' && attributeMap['visibility'] != 'hidden';
15711571
}
15721572

1573+
static final RegExp _listSeparator = RegExp(r'[\s,]');
1574+
1575+
// Text and tspan can list one coordinate per character, which isn't
1576+
// supported. Fail with a clear message instead of a FormatException.
1577+
String? _singleCoordinate(String name, String? raw) {
1578+
if (raw == null) {
1579+
return null;
1580+
}
1581+
final String trimmed = raw.trim();
1582+
if (trimmed.contains(_listSeparator)) {
1583+
throw UnimplementedError('A list of values for "$name" is not supported: "$trimmed"');
1584+
}
1585+
return trimmed;
1586+
}
1587+
15731588
SvgAttributes _createSvgAttributes(Map<String, String> attributeMap, {Color? currentColor}) {
15741589
final String? id = attributeMap['id'];
15751590
final double? opacity = parseDouble(attributeMap['opacity'])?.clamp(0.0, 1.0);
15761591
final Color? color =
15771592
parseColor(attributeMap['color'], attributeName: 'color', id: id) ?? currentColor;
15781593

1579-
final String? rawX = attributeMap['x'];
1580-
final String? rawY = attributeMap['y'];
1594+
final String? rawX = _singleCoordinate('x', attributeMap['x']);
1595+
final String? rawY = _singleCoordinate('y', attributeMap['y']);
15811596

1582-
final String? rawDx = attributeMap['dx'];
1583-
final String? rawDy = attributeMap['dy'];
1597+
final String? rawDx = _singleCoordinate('dx', attributeMap['dx']);
1598+
final String? rawDy = _singleCoordinate('dy', attributeMap['dy']);
15841599

15851600
return SvgAttributes._(
15861601
raw: attributeMap,

‎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.3.2
5+
version: 1.3.3
66

77
executables:
88
vector_graphics_compiler:

‎packages/vector_graphics_compiler/test/parser_test.dart‎

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

417+
test('text and tspan with coordinate lists throw an UnimplementedError', () {
418+
for (final attributes in <String>['x="10 20 30" y="40"', 'x="10" y="40, 50"']) {
419+
expect(
420+
() => parseWithoutOptimizers(
421+
'<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><text $attributes>ABC</text></svg>',
422+
),
423+
throwsA(isA<UnimplementedError>()),
424+
);
425+
}
426+
for (final attributes in <String>['dx="1 2"', 'dy="3,4"']) {
427+
expect(
428+
() => parseWithoutOptimizers(
429+
'<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><text x="10" y="40">ABC<tspan $attributes>DE</tspan></text></svg>',
430+
),
431+
throwsA(
432+
isA<UnimplementedError>().having(
433+
(UnimplementedError e) => e.message,
434+
'message',
435+
contains('is not supported'),
436+
),
437+
),
438+
);
439+
}
440+
});
441+
442+
test('text with single coordinates surrounded by whitespace still parses', () {
443+
final VectorInstructions instructions = parseWithoutOptimizers('''
444+
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
445+
<text x=" 10 " y="40">ABC</text>
446+
</svg>
447+
''');
448+
449+
expect(instructions.textPositions, const <TextPosition>[
450+
TextPosition(x: 10, y: 40, reset: true),
451+
]);
452+
});
453+
417454
test('tspan without position does not apply the group transform twice', () {
418455
final VectorInstructions instructions = parseWithoutOptimizers('''
419456
<svg width="400" height="100" xmlns="http://www.w3.org/2000/svg">

0 commit comments

Comments
 (0)