Skip to content

Commit 542f437

Browse files
authored
Add some missing tests, fix bugs (#9)
* Add some missing tests, fix bugs * format
1 parent 5422149 commit 542f437

File tree

8 files changed

+329
-42
lines changed

8 files changed

+329
-42
lines changed

packages/vector_graphics_compiler/lib/src/geometry/matrix.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class AffineMatrix {
1818
this.d,
1919
this.e,
2020
this.f, [
21-
this._m4_10 = 1.0,
22-
]);
21+
double? m4_10,
22+
]) : _m4_10 = m4_10 ?? (1.0 * a);
2323

2424
/// The identity affine matrix.
2525
static const AffineMatrix identity = AffineMatrix(1, 0, 0, 1, 0, 0);
@@ -95,6 +95,32 @@ class AffineMatrix {
9595
);
9696
}
9797

98+
/// Creates a new affine matrix, skewed along the x axis.
99+
AffineMatrix xSkewed(double x) {
100+
return multiplied(AffineMatrix(
101+
identity.a,
102+
identity.b,
103+
math.tan(x),
104+
identity.d,
105+
identity.e,
106+
identity.f,
107+
identity._m4_10,
108+
));
109+
}
110+
111+
/// Creates a new affine matrix, skewed along the y axis.
112+
AffineMatrix ySkewed(double y) {
113+
return multiplied(AffineMatrix(
114+
identity.a,
115+
math.tan(y),
116+
identity.c,
117+
identity.d,
118+
identity.e,
119+
identity.f,
120+
identity._m4_10,
121+
));
122+
}
123+
98124
/// Creates a new affine matrix of this concatenated with `other`.
99125
AffineMatrix multiplied(AffineMatrix other) {
100126
return AffineMatrix(
@@ -104,7 +130,7 @@ class AffineMatrix {
104130
(b * other.c) + (d * other.d),
105131
(a * other.e) + (c * other.f) + e,
106132
(b * other.e) + (d * other.f) + f,
107-
_m4_10,
133+
_m4_10 * other._m4_10,
108134
);
109135
}
110136

@@ -188,6 +214,6 @@ class AffineMatrix {
188214
String toString() => '''
189215
[ $a, $c, $e ]
190216
[ $b, $d, $f ]
191-
[ 0.0, 0.0, 1.0 ]
217+
[ 0.0, 0.0, 1.0 ] // _m4_10 = $_m4_10
192218
''';
193219
}

packages/vector_graphics_compiler/lib/src/paint.dart

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,8 @@ class Color {
7676
/// A shading program to apply to a [Paint]. Implemented in [LinearGradient] and
7777
/// [RadialGradient].
7878
abstract class Shader {
79-
/// Allows subclasses to be const, and specifies the human-readable type name
80-
/// for this shader.
81-
const Shader(this.typeName);
82-
83-
/// A human readable name for this shader, i.e. 'linearGradient'.
84-
final String typeName;
79+
/// Allows subclasses to be const.
80+
const Shader();
8581
}
8682

8783
/// A [Shader] that describes a linear gradient from [from] to [to].
@@ -112,7 +108,7 @@ class LinearGradient extends Shader {
112108
required this.tileMode,
113109
this.unitMode = GradientUnitMode.objectBoundingBox,
114110
this.transform,
115-
}) : super('linearGradient');
111+
});
116112

117113
/// The start point of the gradient, as specified by [tileMode].
118114
final Point from;
@@ -155,7 +151,7 @@ class LinearGradient extends Shader {
155151
@override
156152
String toString() {
157153
return '''
158-
final $typeName$hashCode = Gradient.linear(
154+
Gradient.linear(
159155
const Offset(${from.x}, ${from.y}),
160156
const Offset(${to.x}, ${to.y}),
161157
$colors,
@@ -215,7 +211,7 @@ class RadialGradient extends Shader {
215211
this.transform,
216212
this.focalPoint,
217213
this.unitMode = GradientUnitMode.objectBoundingBox,
218-
}) : super('radialGradient');
214+
});
219215

220216
/// The central point of the gradient.
221217
final Point center;
@@ -271,7 +267,7 @@ class RadialGradient extends Shader {
271267
@override
272268
String toString() {
273269
return '''
274-
final $typeName$hashCode = Gradient.radial(
270+
Gradient.radial(
275271
const Offset(${center.x}, ${center.y}),
276272
$radius,
277273
$colors,
@@ -454,20 +450,21 @@ class Stroke {
454450

455451
/// Creates a string with the dart:ui code to represent this stroke and any
456452
/// shaders it contains as a ui.Paint.
457-
String toFlutterPaintString([BlendMode? blendMode]) {
453+
String toFlutterPaintString(String shaderName, String paintName,
454+
[BlendMode? blendMode]) {
458455
final StringBuffer buffer = StringBuffer();
459456
if (shader != null) {
460-
buffer.writeln(shader?.toString());
457+
buffer.writeln('final $shaderName = $shader');
461458
}
462-
buffer.write('Paint()');
459+
buffer.write('final $paintName = Paint()');
463460
if ((blendMode ?? BlendMode.srcOver) != BlendMode.srcOver) {
464461
buffer.write('\n ..blendMode = $blendMode');
465462
}
466463
if ((color ?? Color.opaqueBlack) != Color.opaqueBlack) {
467464
buffer.write('\n ..color = $color');
468465
}
469466
if (shader != null) {
470-
buffer.write('\n ..shader = shader${shader.hashCode}}');
467+
buffer.write('\n ..shader = $shaderName');
471468
}
472469
if ((cap ?? StrokeCap.butt) != StrokeCap.butt) {
473470
buffer.write('\n ..strokeCap = $cap');
@@ -574,20 +571,21 @@ class Fill {
574571

575572
/// Creates a string with the dart:ui code to represent this fill and any
576573
/// shaders it contains as a ui.Paint.
577-
String toFlutterPaintString([BlendMode? blendMode]) {
574+
String toFlutterPaintString(String shaderName, String paintName,
575+
[BlendMode? blendMode]) {
578576
final StringBuffer buffer = StringBuffer();
579577
if (shader != null) {
580-
buffer.writeln(shader?.toString());
578+
buffer.writeln('final $shaderName = $shader');
581579
}
582-
buffer.write('Paint()');
580+
buffer.write('final $paintName = Paint()');
583581
if ((blendMode ?? BlendMode.srcOver) != BlendMode.srcOver) {
584582
buffer.write('\n ..blendMode = $blendMode');
585583
}
586584
if ((color ?? Color.opaqueBlack) != Color.opaqueBlack) {
587585
buffer.write('\n ..color = $color');
588586
}
589587
if (shader != null) {
590-
buffer.write('\n ..shader = shader${shader.hashCode}}');
588+
buffer.write('\n ..shader = $shaderName');
591589
}
592590
buffer.writeln(';');
593591
return buffer.toString();

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ import '../geometry/matrix.dart';
44
import '../geometry/path.dart';
55
import 'numbers.dart';
66

7-
// /// Parses a `text-anchor` attribute.
8-
// DrawableTextAnchorPosition? parseTextAnchor(String? raw) {
9-
// switch (raw) {
10-
// case 'inherit':
11-
// return null;
12-
// case 'middle':
13-
// return DrawableTextAnchorPosition.middle;
14-
// case 'end':
15-
// return DrawableTextAnchorPosition.end;
16-
// case 'start':
17-
// default:
18-
// return DrawableTextAnchorPosition.start;
19-
// }
20-
// }
21-
227
const String _transformCommandAtom = ' *,?([^(]+)\\(([^)]*)\\)';
238
final RegExp _transformValidator = RegExp('^($_transformCommandAtom)*\$');
249
final RegExp _transformCommand = RegExp(_transformCommandAtom);
@@ -115,8 +100,7 @@ AffineMatrix _parseSvgRotate(String? paramsStr, AffineMatrix current) {
115100
assert(params.length <= 3);
116101
final double a = radians(parseDouble(params[0])!);
117102

118-
final AffineMatrix rotate =
119-
AffineMatrix(cos(a), sin(a), -sin(a), cos(a), 0.0, 0.0);
103+
final AffineMatrix rotate = AffineMatrix.identity.rotated(a);
120104

121105
if (params.length > 1) {
122106
final double x = parseDouble(params[1])!;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ class SvgTheme {
4444
int get hashCode => Object.hash(currentColor, fontSize, xHeight);
4545

4646
@override
47-
String toString() {
48-
return 'SvgTheme(currentColor: $currentColor, fontSize: $fontSize, xHeight: $xHeight)';
49-
}
47+
String toString() =>
48+
'SvgTheme(currentColor: $currentColor, fontSize: $fontSize, xHeight: $xHeight)';
5049
}

packages/vector_graphics_compiler/test/matrix_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ void main() {
4949
);
5050
});
5151

52+
test('Multiply handles the extra matrix4 scale value', () {
53+
final AffineMatrix matrix1 = AffineMatrix.identity.scaled(2, 3);
54+
final AffineMatrix matrix2 = AffineMatrix.identity.multiplied(matrix1);
55+
56+
expect(matrix1, matrix2);
57+
});
58+
5259
test('Translate', () {
5360
const AffineMatrix matrix1 = AffineMatrix(2, 2, 3, 4, 5, 6);
5461

packages/vector_graphics_compiler/test/paint_test.dart

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,87 @@ void main() {
8080
expect(const Paint().isEmpty, true);
8181
expect(Paint.empty.isEmpty, true);
8282
});
83+
84+
test('Paint toFlutterString', () {
85+
const Paint paint = Paint(
86+
blendMode: BlendMode.screen,
87+
stroke: Stroke(
88+
color: Color(0x87654321),
89+
width: 2.0,
90+
cap: StrokeCap.round,
91+
join: StrokeJoin.bevel,
92+
miterLimit: 10.0,
93+
shader: LinearGradient(
94+
from: Point(0, 0),
95+
to: Point(10, 10),
96+
colors: <Color>[Color.opaqueBlack, Color(0xFFABCDEF)],
97+
tileMode: TileMode.mirror,
98+
offsets: <double>[0.0, 1.0],
99+
transform: AffineMatrix.identity,
100+
unitMode: GradientUnitMode.userSpaceOnUse,
101+
)),
102+
fill: Fill(
103+
color: Color(0x12345678),
104+
shader: RadialGradient(
105+
center: Point(50, 50),
106+
radius: 10,
107+
colors: <Color>[Color(0xFFFFFFAA), Color(0xFFABCDEF)],
108+
tileMode: TileMode.clamp,
109+
transform: AffineMatrix.identity,
110+
focalPoint: Point(5, 50),
111+
offsets: <double>[.1, .9],
112+
),
113+
),
114+
pathFillType: PathFillType.evenOdd,
115+
);
116+
117+
expect(
118+
paint.fill!.toFlutterPaintString(
119+
'shader1',
120+
'fillPaint',
121+
paint.blendMode,
122+
),
123+
'final shader1 = Gradient.radial(\n'
124+
' const Offset(50.0, 50.0),\n'
125+
' 10.0,\n'
126+
' [Color(0xffffffaa), Color(0xffabcdef)],\n'
127+
' [0.1, 0.9],\n'
128+
' TileMode.clamp,\n'
129+
' Float64List.fromList(<double>[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]),\n'
130+
' const Offset(5.0, 50.0),\n'
131+
' 0.0,\n'
132+
');\n'
133+
'\n'
134+
'final fillPaint = Paint()\n'
135+
' ..blendMode = BlendMode.screen\n'
136+
' ..color = Color(0x12345678)\n'
137+
' ..shader = shader1;\n',
138+
);
139+
140+
expect(
141+
paint.stroke!.toFlutterPaintString(
142+
'shader2',
143+
'strokePaint',
144+
paint.blendMode,
145+
),
146+
'final shader2 = Gradient.linear(\n'
147+
' const Offset(0.0, 0.0),\n'
148+
' const Offset(10.0, 10.0),\n'
149+
' [Color(0xff000000), Color(0xffabcdef)],\n'
150+
' [0.0, 1.0],\n'
151+
' TileMode.mirror,\n'
152+
' Float64List.fromList([1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]),\n'
153+
');\n'
154+
'\n'
155+
'final strokePaint = Paint()\n'
156+
' ..blendMode = BlendMode.screen\n'
157+
' ..color = Color(0x87654321)\n'
158+
' ..shader = shader2\n'
159+
' ..strokeCap = StrokeCap.round\n'
160+
' ..strokeJoin = StrokeJoin.bevel\n'
161+
' ..strokeMiterLimit = 10.0\n'
162+
' ..strokeWidth = 2.0\n'
163+
' ..style = PaintingStyle.stroke;\n',
164+
);
165+
});
83166
}

0 commit comments

Comments
 (0)