Skip to content

Commit

Permalink
Fix parsing transforms with scientific notation (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
skreborn authored Oct 13, 2023
1 parent 6106ede commit d944f2c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/vector_graphics_compiler/lib/src/svg/parsers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,26 @@ List<double> _parseTransformParams(String params) {
final List<double> result = <double>[];
String current = '';
for (int i = 0; i < params.length; i += 1) {
if (params[i] == ' ' || params[i] == '-' || params[i] == ',') {
final String char = params[i];
final bool isSeparator = char == ' ' || char == '-' || char == ',';
final bool isExponent = i > 0 && params[i - 1] == 'e';
if (isSeparator && !isExponent) {
if (current != '') {
result.add(parseDouble(current)!);
}
if (params[i] == '-') {
if (char == '-') {
current = '-';
} else {
current = '';
}
} else {
if (params[i] == '.') {
if (char == '.') {
if (current.contains('.')) {
result.add(parseDouble(current)!);
current = '';
}
}
current += params[i];
current += char;
}
}
if (current.isNotEmpty) {
Expand Down
7 changes: 7 additions & 0 deletions packages/vector_graphics_compiler/test/parsers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ void main() {
expect(parseDoubleWithUnits('1pt', theme: const SvgTheme()), 1 + 1 / 3);
});

test('Parse a transform with scientific notation', () {
expect(
parseTransform('translate(9e-6,6.5e-4)'),
AffineMatrix.identity.translated(9e-6, 6.5e-4),
);
});

test('Parse a transform with a missing space', () {
expect(
parseTransform('translate(0-70)'),
Expand Down

0 comments on commit d944f2c

Please sign in to comment.