Skip to content

Commit

Permalink
[vector_graphics_compiler] fix: handle parsing stroke-width with an i…
Browse files Browse the repository at this point in the history
…nvalid value (#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/flutter#158819

Issue: [link](dnfield/vector_graphics#209 (comment))
  • Loading branch information
fransdhinta authored Nov 25, 2024
1 parent 46aeb2b commit bc18995
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/vector_graphics_compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.16

* Sets stroke-width to 1 by default when an invalid value is parsed instead of throwing an exception.

## 1.1.15

* Fixes a bug where empty tags caused the parser to crash.
Expand Down
2 changes: 1 addition & 1 deletion packages/vector_graphics_compiler/lib/src/svg/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ class SvgParser {
cap: _parseCap(rawStrokeCap, null),
join: _parseJoin(rawLineJoin, null),
miterLimit: parseDouble(rawMiterLimit),
width: parseDoubleWithUnits(rawStrokeWidth),
width: parseDoubleWithUnits(rawStrokeWidth, tryParse: true),
dashArray: _parseDashArray(rawStrokeDashArray),
dashOffset: _parseDashOffset(rawStrokeDashOffset),
hasPattern: hasPattern,
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.1.15
version: 1.1.16

executables:
vector_graphics_compiler:
Expand Down
81 changes: 81 additions & 0 deletions packages/vector_graphics_compiler/test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,87 @@ void main() {
]);
});

test('stroke-width with invalid value', () {
const String svg =
'<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>';

final VectorInstructions instructions = parseWithoutOptimizers(svg);

expect(instructions.paints, const <Paint>[
Paint(
stroke: Stroke(color: Color(0xff0000ff)),
fill: Fill(color: Color(0xffff0000))),
]);

expect(instructions.paths, <Path>[
Path(
commands: const <PathCommand>[
MoveToCommand(100.0, 10.0),
LineToCommand(180.0, 10.0),
LineToCommand(180.0, 90.0),
LineToCommand(100.0, 90.0),
CloseCommand(),
],
),
]);
});

test('stroke-width with unit value', () {
const SvgTheme theme = SvgTheme();
const double ptConversionFactor = 96 / 72;

const String svg_px =
'<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>';
const String svg_pt =
'<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>';
const String svg_ex =
'<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>';
const String svg_em =
'<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>';
const String svg_rem =
'<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>';

final VectorInstructions instructionsPx = parseWithoutOptimizers(svg_px);
final VectorInstructions instructionsPt = parseWithoutOptimizers(svg_pt);
final VectorInstructions instructionsEx = parseWithoutOptimizers(svg_ex);
final VectorInstructions instructionsEm = parseWithoutOptimizers(svg_em);
final VectorInstructions instructionsRem = parseWithoutOptimizers(svg_rem);

expect(instructionsPx.paints, <Paint>[
const Paint(
stroke: Stroke(color: Color(0xff0000ff), width: 1.0),
fill: Fill(color: Color(0xffff0000))),
]);

expect(instructionsPt.paints, <Paint>[
const Paint(
stroke:
Stroke(color: Color(0xff0000ff), width: 1 * ptConversionFactor),
fill: Fill(color: Color(0xffff0000))),
]);

expect(instructionsEx.paints, <Paint>[
Paint(
stroke: Stroke(
color: const Color(0xff0000ff), width: 1.0 * theme.xHeight),
fill: const Fill(color: Color(0xffff0000))),
]);

expect(instructionsEm.paints, <Paint>[
Paint(
stroke: Stroke(
color: const Color(0xff0000ff), width: 1.0 * theme.fontSize),
fill: const Fill(color: Color(0xffff0000))),
]);

expect(instructionsRem.paints, <Paint>[
Paint(
stroke: Stroke(
color: const Color(0xff0000ff), width: 1.0 * theme.fontSize),
fill: const Fill(color: Color(0xffff0000))),
]);
});

test('Dashed path', () {
final VectorInstructions instructions = parseWithoutOptimizers(
'''
Expand Down

0 comments on commit bc18995

Please sign in to comment.