Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vector_graphics_compiler] fix: handle parsing stroke-width with an invalid value #8004

Merged
merged 20 commits into from
Nov 25, 2024
Merged
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
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
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