Skip to content

Commit bc18995

Browse files
authored
[vector_graphics_compiler] fix: handle parsing stroke-width with an invalid value (flutter#8004)
There is some issue while calling parseDouble from the original repo of this package [issue link](dnfield/vector_graphics#209 (comment)). I suggest a fix to change the return of **parseDouble** to always double.tryParse so that it will return `null` instead of throwing an error if it parses an invalid String Fixes flutter#158819 Issue: [link](dnfield/vector_graphics#209 (comment))
1 parent 46aeb2b commit bc18995

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

packages/vector_graphics_compiler/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.16
2+
3+
* Sets stroke-width to 1 by default when an invalid value is parsed instead of throwing an exception.
4+
15
## 1.1.15
26

37
* Fixes a bug where empty tags caused the parser to crash.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ class SvgParser {
15871587
cap: _parseCap(rawStrokeCap, null),
15881588
join: _parseJoin(rawLineJoin, null),
15891589
miterLimit: parseDouble(rawMiterLimit),
1590-
width: parseDoubleWithUnits(rawStrokeWidth),
1590+
width: parseDoubleWithUnits(rawStrokeWidth, tryParse: true),
15911591
dashArray: _parseDashArray(rawStrokeDashArray),
15921592
dashOffset: _parseDashOffset(rawStrokeDashOffset),
15931593
hasPattern: hasPattern,

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.1.15
5+
version: 1.1.16
66

77
executables:
88
vector_graphics_compiler:

packages/vector_graphics_compiler/test/parser_test.dart

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,87 @@ void main() {
704704
]);
705705
});
706706

707+
test('stroke-width with invalid value', () {
708+
const String svg =
709+
'<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"><path d="M100 10 H180 V90 H100 Z" fill="#ff0000" stroke="#0000ff" stroke-width="invalid"/></svg>';
710+
711+
final VectorInstructions instructions = parseWithoutOptimizers(svg);
712+
713+
expect(instructions.paints, const <Paint>[
714+
Paint(
715+
stroke: Stroke(color: Color(0xff0000ff)),
716+
fill: Fill(color: Color(0xffff0000))),
717+
]);
718+
719+
expect(instructions.paths, <Path>[
720+
Path(
721+
commands: const <PathCommand>[
722+
MoveToCommand(100.0, 10.0),
723+
LineToCommand(180.0, 10.0),
724+
LineToCommand(180.0, 90.0),
725+
LineToCommand(100.0, 90.0),
726+
CloseCommand(),
727+
],
728+
),
729+
]);
730+
});
731+
732+
test('stroke-width with unit value', () {
733+
const SvgTheme theme = SvgTheme();
734+
const double ptConversionFactor = 96 / 72;
735+
736+
const String svg_px =
737+
'<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"><path d="M100 10 H180 V90 H100 Z" fill="#ff0000" stroke="#0000ff" stroke-width="1px"/></svg>';
738+
const String svg_pt =
739+
'<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"><path d="M100 10 H180 V90 H100 Z" fill="#ff0000" stroke="#0000ff" stroke-width="1pt"/></svg>';
740+
const String svg_ex =
741+
'<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"><path d="M100 10 H180 V90 H100 Z" fill="#ff0000" stroke="#0000ff" stroke-width="1ex"/></svg>';
742+
const String svg_em =
743+
'<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"><path d="M100 10 H180 V90 H100 Z" fill="#ff0000" stroke="#0000ff" stroke-width="1em"/></svg>';
744+
const String svg_rem =
745+
'<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg"><path d="M100 10 H180 V90 H100 Z" fill="#ff0000" stroke="#0000ff" stroke-width="1rem"/></svg>';
746+
747+
final VectorInstructions instructionsPx = parseWithoutOptimizers(svg_px);
748+
final VectorInstructions instructionsPt = parseWithoutOptimizers(svg_pt);
749+
final VectorInstructions instructionsEx = parseWithoutOptimizers(svg_ex);
750+
final VectorInstructions instructionsEm = parseWithoutOptimizers(svg_em);
751+
final VectorInstructions instructionsRem = parseWithoutOptimizers(svg_rem);
752+
753+
expect(instructionsPx.paints, <Paint>[
754+
const Paint(
755+
stroke: Stroke(color: Color(0xff0000ff), width: 1.0),
756+
fill: Fill(color: Color(0xffff0000))),
757+
]);
758+
759+
expect(instructionsPt.paints, <Paint>[
760+
const Paint(
761+
stroke:
762+
Stroke(color: Color(0xff0000ff), width: 1 * ptConversionFactor),
763+
fill: Fill(color: Color(0xffff0000))),
764+
]);
765+
766+
expect(instructionsEx.paints, <Paint>[
767+
Paint(
768+
stroke: Stroke(
769+
color: const Color(0xff0000ff), width: 1.0 * theme.xHeight),
770+
fill: const Fill(color: Color(0xffff0000))),
771+
]);
772+
773+
expect(instructionsEm.paints, <Paint>[
774+
Paint(
775+
stroke: Stroke(
776+
color: const Color(0xff0000ff), width: 1.0 * theme.fontSize),
777+
fill: const Fill(color: Color(0xffff0000))),
778+
]);
779+
780+
expect(instructionsRem.paints, <Paint>[
781+
Paint(
782+
stroke: Stroke(
783+
color: const Color(0xff0000ff), width: 1.0 * theme.fontSize),
784+
fill: const Fill(color: Color(0xffff0000))),
785+
]);
786+
});
787+
707788
test('Dashed path', () {
708789
final VectorInstructions instructions = parseWithoutOptimizers(
709790
'''

0 commit comments

Comments
 (0)