Skip to content

Commit ddf18dc

Browse files
authored
Fix fill type for applyOp (#134)
* Fix fill type for applyOp * format * fix incorrect tests
1 parent 2fcc58a commit ddf18dc

File tree

5 files changed

+107
-48
lines changed

5 files changed

+107
-48
lines changed

packages/vector_graphics/example/pubspec.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ dev_dependencies:
2121

2222
flutter:
2323
uses-material-design: true
24-
assets:
25-
- assets/tiger.bin # ghostscript tiger
26-
2724

2825
# Comment out before publishing
2926
dependency_overrides:

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ late final String _dylibPath;
2222
/// [dispose] has been called, this class must not be used again.
2323
class Path implements PathProxy {
2424
/// Creates an empty path object with the specified fill type.
25-
Path([this.fillType = FillType.nonZero])
25+
Path([FillType fillType = FillType.nonZero])
2626
: _path = _createPathFn(fillType.index);
2727

2828
/// Creates a copy of this path.
@@ -33,7 +33,10 @@ class Path implements PathProxy {
3333
}
3434

3535
/// The [FillType] of this path.
36-
final FillType fillType;
36+
FillType get fillType {
37+
assert(_path != null);
38+
return FillType.values[_getFillTypeFn(_path!)];
39+
}
3740

3841
ffi.Pointer<_SkPath>? _path;
3942
ffi.Pointer<_PathData>? _pathData;
@@ -295,3 +298,9 @@ typedef _destroy_data_type = ffi.Void Function(ffi.Pointer<_PathData>);
295298

296299
final _DestroyDataType _destroyDataFn =
297300
_dylib.lookupFunction<_destroy_data_type, _DestroyDataType>('DestroyData');
301+
302+
typedef _GetFillTypeType = int Function(ffi.Pointer<_SkPath>);
303+
typedef _get_fill_type_type = ffi.Int32 Function(ffi.Pointer<_SkPath>);
304+
305+
final _GetFillTypeType _getFillTypeFn =
306+
_dylib.lookupFunction<_get_fill_type_type, _GetFillTypeType>('GetFillType');

packages/vector_graphics_compiler/test/clipping_optimizer_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,24 @@ void main() {
161161
DrawCommand(DrawCommandType.restore),
162162
]);
163163
});
164+
165+
test('Preserves fill type changes', () async {
166+
const String svg = '''
167+
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
168+
<g clip-path="url(#a)">
169+
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.99 0C4.47 0 0 4.48 0 10s4.47 10 9.99 10C15.52 20 20 15.52 20 10S15.52 0 9.99 0zM11 11V5H9v6h2zm0 4v-2H9v2h2zm-9-5c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8-8 3.58-8 8z" fill="black" />
170+
</g>
171+
<defs>
172+
<clipPath id="a">
173+
<path fill="#fff" d="M0 0h20v20H0z" />
174+
</clipPath>
175+
</defs>
176+
</svg>''';
177+
final VectorInstructions instructions = await parse(svg);
178+
179+
expect(
180+
instructions.paths.single.fillType,
181+
PathFillType.evenOdd,
182+
);
183+
});
164184
}

packages/vector_graphics_compiler/test/overdraw_optimizer_test.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void main() {
9292

9393
expect(instructions.paths, <Path>[
9494
Path(
95+
fillType: PathFillType.evenOdd,
9596
commands: const <PathCommand>[
9697
MoveToCommand(367.0, 221.5),
9798
LineToCommand(99.0, 221.5),
@@ -139,6 +140,7 @@ void main() {
139140

140141
expect(instructions.paths, <Path>[
141142
Path(
143+
fillType: PathFillType.evenOdd,
142144
commands: const <PathCommand>[
143145
MoveToCommand(343.0, 240.5),
144146
LineToCommand(88.0, 240.5),
@@ -155,6 +157,7 @@ void main() {
155157
],
156158
),
157159
Path(
160+
fillType: PathFillType.evenOdd,
158161
commands: const <PathCommand>[
159162
MoveToCommand(484.0, 63.5),
160163
LineToCommand(343.0, 63.5),
@@ -171,6 +174,7 @@ void main() {
171174
],
172175
),
173176
Path(
177+
fillType: PathFillType.evenOdd,
174178
commands: const <PathCommand>[
175179
MoveToCommand(343.0, 240.5),
176180
LineToCommand(484.0, 240.5),
@@ -201,6 +205,7 @@ void main() {
201205

202206
expect(instructions.paths, <Path>[
203207
Path(
208+
fillType: PathFillType.evenOdd,
204209
commands: const <PathCommand>[
205210
MoveToCommand(343.0, 240.5),
206211
LineToCommand(88.0, 240.5),
@@ -248,6 +253,7 @@ void main() {
248253

249254
expect(instructions.paths, <Path>[
250255
Path(
256+
fillType: PathFillType.evenOdd,
251257
commands: const <PathCommand>[
252258
MoveToCommand(343.0, 240.5),
253259
LineToCommand(88.0, 240.5),
@@ -264,6 +270,7 @@ void main() {
264270
],
265271
),
266272
Path(
273+
fillType: PathFillType.evenOdd,
267274
commands: const <PathCommand>[
268275
MoveToCommand(484.0, 63.5),
269276
LineToCommand(343.0, 63.5),
@@ -280,6 +287,7 @@ void main() {
280287
],
281288
),
282289
Path(
290+
fillType: PathFillType.evenOdd,
283291
commands: const <PathCommand>[
284292
MoveToCommand(343.0, 240.5),
285293
LineToCommand(484.0, 240.5),
@@ -337,6 +345,7 @@ void main() {
337345
],
338346
),
339347
Path(
348+
fillType: PathFillType.evenOdd,
340349
commands: const <PathCommand>[
341350
MoveToCommand(150.0, 100.0),
342351
LineToCommand(100.0, 100.0),
@@ -352,6 +361,7 @@ void main() {
352361
],
353362
),
354363
Path(
364+
fillType: PathFillType.evenOdd,
355365
commands: const <PathCommand>[
356366
MoveToCommand(200.0, 50.0),
357367
CubicToCommand(
@@ -366,6 +376,7 @@ void main() {
366376
],
367377
),
368378
Path(
379+
fillType: PathFillType.evenOdd,
369380
commands: const <PathCommand>[
370381
MoveToCommand(400.0, 50.0),
371382
CubicToCommand(
@@ -379,8 +390,11 @@ void main() {
379390
CloseCommand()
380391
],
381392
),
382-
Path(),
383393
Path(
394+
fillType: PathFillType.evenOdd,
395+
),
396+
Path(
397+
fillType: PathFillType.evenOdd,
384398
commands: const <PathCommand>[
385399
MoveToCommand(600.0, 50.0),
386400
CubicToCommand(
@@ -395,6 +409,7 @@ void main() {
395409
],
396410
),
397411
Path(
412+
fillType: PathFillType.evenOdd,
398413
commands: const <PathCommand>[
399414
MoveToCommand(800.0, 50.0),
400415
CubicToCommand(
@@ -409,6 +424,7 @@ void main() {
409424
],
410425
),
411426
Path(
427+
fillType: PathFillType.evenOdd,
412428
commands: const <PathCommand>[
413429
MoveToCommand(1000.0, 50.0),
414430
CubicToCommand(
@@ -423,6 +439,7 @@ void main() {
423439
],
424440
),
425441
Path(
442+
fillType: PathFillType.evenOdd,
426443
commands: const <PathCommand>[
427444
MoveToCommand(200.0000457763672, 203.1529998779297),
428445
CubicToCommand(194.55233764648438, 201.1146697998047,
@@ -441,6 +458,7 @@ void main() {
441458
],
442459
),
443460
Path(
461+
fillType: PathFillType.evenOdd,
444462
commands: const <PathCommand>[
445463
MoveToCommand(217.5, 200.0),
446464
CubicToCommand(
@@ -455,6 +473,7 @@ void main() {
455473
],
456474
),
457475
Path(
476+
fillType: PathFillType.evenOdd,
458477
commands: const <PathCommand>[
459478
MoveToCommand(382.5, 200.0),
460479
CubicToCommand(410.09576416015625, 200.0, 432.5, 222.4042510986328,
@@ -469,6 +488,7 @@ void main() {
469488
],
470489
),
471490
Path(
491+
fillType: PathFillType.evenOdd,
472492
commands: const <PathCommand>[
473493
MoveToCommand(417.5, 200.0),
474494
CubicToCommand(445.09576416015625, 200.0, 467.5, 222.4042510986328,
@@ -483,6 +503,7 @@ void main() {
483503
],
484504
),
485505
Path(
506+
fillType: PathFillType.evenOdd,
486507
commands: const <PathCommand>[
487508
MoveToCommand(582.5, 200.0),
488509
CubicToCommand(
@@ -497,6 +518,7 @@ void main() {
497518
],
498519
),
499520
Path(
521+
fillType: PathFillType.evenOdd,
500522
commands: const <PathCommand>[
501523
MoveToCommand(617.5, 200.0),
502524
CubicToCommand(
@@ -511,6 +533,7 @@ void main() {
511533
],
512534
),
513535
Path(
536+
fillType: PathFillType.evenOdd,
514537
commands: const <PathCommand>[
515538
MoveToCommand(817.5, 200.0),
516539
CubicToCommand(
@@ -525,6 +548,7 @@ void main() {
525548
],
526549
),
527550
Path(
551+
fillType: PathFillType.evenOdd,
528552
commands: const <PathCommand>[
529553
MoveToCommand(782.5, 200.0),
530554
CubicToCommand(
@@ -539,6 +563,7 @@ void main() {
539563
],
540564
),
541565
Path(
566+
fillType: PathFillType.evenOdd,
542567
commands: const <PathCommand>[
543568
MoveToCommand(982.5, 200.0),
544569
CubicToCommand(1010.0957641601562, 200.0, 1032.5, 222.4042510986328,

packages/vector_graphics_compiler/test/test_svg_strings.dart

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,51 +1220,59 @@ List<Path> groupMaskForMaskingOptimizer = <Path>[
12201220

12211221
/// Excpected groupMask result when [MaskingOptimizer] is applied
12221222
List<Path> blendsAndMasksForMaskingOptimizer = <Path>[
1223-
Path(commands: const <PathCommand>[
1224-
MoveToCommand(50.0, 0.0),
1225-
CubicToCommand(77.5957512247, 0.0, 100.0, 22.4042487753, 100.0, 50.0),
1226-
CubicToCommand(100.0, 77.5957512247, 77.5957512247, 100.0, 50.0, 100.0),
1227-
CubicToCommand(22.4042487753, 100.0, 0.0, 77.5957512247, 0.0, 50.0),
1228-
CubicToCommand(0.0, 22.4042487753, 22.4042487753, 0.0, 50.0, 0.0),
1229-
CloseCommand()
1230-
]),
1231-
Path(commands: const <PathCommand>[
1232-
MoveToCommand(90.0, 50.0),
1233-
CubicToCommand(
1234-
90.0, 27.923398971557617, 72.07659912109375, 10.0, 50.0, 10.0),
1235-
CubicToCommand(
1236-
27.923398971557617, 10.0, 10.0, 27.923398971557617, 10.0, 50.0),
1237-
CubicToCommand(
1238-
10.0, 72.07659912109375, 27.923398971557617, 90.0, 50.0, 90.0),
1239-
CubicToCommand(
1240-
72.07659912109375, 90.0, 90.0, 72.07659912109375, 90.0, 50.0),
1241-
CloseCommand()
1242-
]),
1223+
Path(
1224+
commands: const <PathCommand>[
1225+
MoveToCommand(50.0, 0.0),
1226+
CubicToCommand(77.5957512247, 0.0, 100.0, 22.4042487753, 100.0, 50.0),
1227+
CubicToCommand(100.0, 77.5957512247, 77.5957512247, 100.0, 50.0, 100.0),
1228+
CubicToCommand(22.4042487753, 100.0, 0.0, 77.5957512247, 0.0, 50.0),
1229+
CubicToCommand(0.0, 22.4042487753, 22.4042487753, 0.0, 50.0, 0.0),
1230+
CloseCommand(),
1231+
],
1232+
),
1233+
Path(
1234+
fillType: PathFillType.evenOdd,
1235+
commands: const <PathCommand>[
1236+
MoveToCommand(90.0, 50.0),
1237+
CubicToCommand(
1238+
90.0, 27.923398971557617, 72.07659912109375, 10.0, 50.0, 10.0),
1239+
CubicToCommand(
1240+
27.923398971557617, 10.0, 10.0, 27.923398971557617, 10.0, 50.0),
1241+
CubicToCommand(
1242+
10.0, 72.07659912109375, 27.923398971557617, 90.0, 50.0, 90.0),
1243+
CubicToCommand(
1244+
72.07659912109375, 90.0, 90.0, 72.07659912109375, 90.0, 50.0),
1245+
CloseCommand(),
1246+
],
1247+
),
12431248
];
12441249

12451250
/// Expected basicClips result when [Clipping Optimizer] is applied
12461251
12471252
List<Path> basicClipsForClippingOptimzer = <Path>[
1248-
Path(commands: const <PathCommand>[
1249-
MoveToCommand(50.0, 30.0),
1250-
CubicToCommand(
1251-
50.0, 18.961700439453125, 41.038299560546875, 10.0, 30.0, 10.0),
1252-
CubicToCommand(
1253-
18.961700439453125, 10.0, 10.0, 18.961700439453125, 10.0, 30.0),
1254-
CubicToCommand(
1255-
10.0, 41.038299560546875, 18.961700439453125, 50.0, 30.0, 50.0),
1256-
CubicToCommand(
1257-
41.038299560546875, 50.0, 50.0, 41.038299560546875, 50.0, 30.0),
1258-
CloseCommand(),
1259-
MoveToCommand(90.0, 70.0),
1260-
CubicToCommand(
1261-
90.0, 58.961700439453125, 81.03829956054688, 50.0, 70.0, 50.0),
1262-
CubicToCommand(
1263-
58.961700439453125, 50.0, 50.0, 58.961700439453125, 50.0, 70.0),
1264-
CubicToCommand(
1265-
50.0, 81.03829956054688, 58.961700439453125, 90.0, 70.0, 90.0),
1266-
CubicToCommand(
1267-
81.03829956054688, 90.0, 90.0, 81.03829956054688, 90.0, 70.0),
1268-
CloseCommand()
1269-
]),
1253+
Path(
1254+
fillType: PathFillType.evenOdd,
1255+
commands: const <PathCommand>[
1256+
MoveToCommand(50.0, 30.0),
1257+
CubicToCommand(
1258+
50.0, 18.961700439453125, 41.038299560546875, 10.0, 30.0, 10.0),
1259+
CubicToCommand(
1260+
18.961700439453125, 10.0, 10.0, 18.961700439453125, 10.0, 30.0),
1261+
CubicToCommand(
1262+
10.0, 41.038299560546875, 18.961700439453125, 50.0, 30.0, 50.0),
1263+
CubicToCommand(
1264+
41.038299560546875, 50.0, 50.0, 41.038299560546875, 50.0, 30.0),
1265+
CloseCommand(),
1266+
MoveToCommand(90.0, 70.0),
1267+
CubicToCommand(
1268+
90.0, 58.961700439453125, 81.03829956054688, 50.0, 70.0, 50.0),
1269+
CubicToCommand(
1270+
58.961700439453125, 50.0, 50.0, 58.961700439453125, 50.0, 70.0),
1271+
CubicToCommand(
1272+
50.0, 81.03829956054688, 58.961700439453125, 90.0, 70.0, 90.0),
1273+
CubicToCommand(
1274+
81.03829956054688, 90.0, 90.0, 81.03829956054688, 90.0, 70.0),
1275+
CloseCommand()
1276+
],
1277+
),
12701278
];

0 commit comments

Comments
 (0)