4
4
5
5
import 'dart:typed_data' ;
6
6
7
+ import 'package:meta/meta.dart' ;
8
+
7
9
import '../geometry/basic_types.dart' ;
8
10
import '../geometry/matrix.dart' ;
9
11
import '../geometry/path.dart' ;
@@ -57,10 +59,30 @@ class _EmptyNode extends Node {
57
59
void visitChildren (NodeCallback visitor) {}
58
60
}
59
61
62
+ /// A node that contains a transform operation in the tree of graphics
63
+ /// operations.
64
+ abstract class TransformableNode extends Node {
65
+ /// Constructs a new tree node with [transform] .
66
+ TransformableNode (this .transform);
67
+
68
+ /// The descendant child's transform
69
+ final AffineMatrix transform;
70
+
71
+ @override
72
+ @mustCallSuper
73
+ AffineMatrix concatTransform (AffineMatrix currentTransform) {
74
+ if (transform == AffineMatrix .identity) {
75
+ return currentTransform;
76
+ }
77
+ return currentTransform.multiplied (transform);
78
+ }
79
+ }
80
+
60
81
/// A node that has attributes in the tree of graphics operations.
61
- abstract class AttributedNode extends Node {
62
- /// Constructs a new tree node with [id] and [paint] .
63
- AttributedNode (this .attributes);
82
+ abstract class AttributedNode extends TransformableNode {
83
+ /// Constructs a new tree node with [attributes] .
84
+ AttributedNode (this .attributes, {AffineMatrix ? precalculatedTransform})
85
+ : super (precalculatedTransform ?? attributes.transform);
64
86
65
87
/// A collection of painting attributes.
66
88
///
@@ -116,13 +138,9 @@ class ParentNode extends AttributedNode {
116
138
/// Creates a new [ParentNode] .
117
139
ParentNode (
118
140
super .attributes, {
119
- AffineMatrix ? precalculatedTransform,
141
+ super . precalculatedTransform,
120
142
List <Node >? children,
121
- }) : transform = precalculatedTransform ?? attributes.transform,
122
- _children = children ?? < Node > [];
123
-
124
- /// The transform to apply to this subtree, if any.
125
- final AffineMatrix transform;
143
+ }) : _children = children ?? < Node > [];
126
144
127
145
/// The child nodes of this node.
128
146
final List <Node > _children;
@@ -177,14 +195,6 @@ class ParentNode extends AttributedNode {
177
195
_children.add (wrappedChild);
178
196
}
179
197
180
- @override
181
- AffineMatrix concatTransform (AffineMatrix currentTransform) {
182
- if (transform == AffineMatrix .identity) {
183
- return currentTransform;
184
- }
185
- return currentTransform.multiplied (transform);
186
- }
187
-
188
198
@override
189
199
AttributedNode applyAttributes (SvgAttributes newAttributes) {
190
200
return ParentNode (
@@ -237,15 +247,15 @@ class SaveLayerNode extends ParentNode {
237
247
}
238
248
239
249
/// A parent node that applies a clip to its children.
240
- class ClipNode extends Node {
250
+ class ClipNode extends TransformableNode {
241
251
/// Creates a new clip node that applies clip paths to [child] .
242
252
ClipNode ({
243
253
required this .resolver,
244
254
required this .child,
245
255
required this .clipId,
246
- required this . transform,
256
+ required AffineMatrix transform,
247
257
String ? id,
248
- });
258
+ }) : super (transform) ;
249
259
250
260
/// Called by visitors to resolve [clipId] to a list of paths.
251
261
final Resolver <List <Path >> resolver;
@@ -260,17 +270,6 @@ class ClipNode extends Node {
260
270
/// The child to clip.
261
271
final Node child;
262
272
263
- /// The descendant child's transform
264
- final AffineMatrix transform;
265
-
266
- @override
267
- AffineMatrix concatTransform (AffineMatrix currentTransform) {
268
- if (transform == AffineMatrix .identity) {
269
- return currentTransform;
270
- }
271
- return currentTransform.multiplied (transform);
272
- }
273
-
274
273
@override
275
274
void visitChildren (NodeCallback visitor) {
276
275
visitor (child);
@@ -283,15 +282,15 @@ class ClipNode extends Node {
283
282
}
284
283
285
284
/// A parent node that applies a mask to its child.
286
- class MaskNode extends Node {
285
+ class MaskNode extends TransformableNode {
287
286
/// Creates a new mask node that applies [mask] to [child] using [blendMode] .
288
287
MaskNode ({
289
288
required this .child,
290
289
required this .maskId,
291
290
this .blendMode,
292
291
required this .resolver,
293
- required this . transform,
294
- });
292
+ required AffineMatrix transform,
293
+ }) : super (transform) ;
295
294
296
295
/// The mask to apply.
297
296
final String maskId;
@@ -302,20 +301,9 @@ class MaskNode extends Node {
302
301
/// The blend mode to apply when saving a layer for the mask, if any.
303
302
final BlendMode ? blendMode;
304
303
305
- /// The descendant child's transform
306
- final AffineMatrix transform;
307
-
308
304
/// Called by visitors to resolve [maskId] to an [AttributedNode] .
309
305
final Resolver <AttributedNode ?> resolver;
310
306
311
- @override
312
- AffineMatrix concatTransform (AffineMatrix currentTransform) {
313
- if (transform == AffineMatrix .identity) {
314
- return currentTransform;
315
- }
316
- return currentTransform.multiplied (transform);
317
- }
318
-
319
307
@override
320
308
void visitChildren (NodeCallback visitor) {
321
309
visitor (child);
@@ -522,35 +510,24 @@ class ImageNode extends AttributedNode {
522
510
}
523
511
524
512
/// A leaf node in the tree that reprents an patterned-node.
525
- class PatternNode extends Node {
513
+ class PatternNode extends TransformableNode {
526
514
/// Creates a new pattern node that aaples [pattern] to [child] .
527
515
PatternNode ({
528
516
required this .child,
529
517
required this .patternId,
530
518
required this .resolver,
531
- required this . transform,
532
- });
519
+ required AffineMatrix transform,
520
+ }) : super (transform) ;
533
521
534
522
/// This id will match any path or text element that has a non-null patternId.
535
523
final String patternId;
536
524
537
525
/// The child(ren) to apply the pattern to.
538
526
final Node child;
539
527
540
- /// The descendant child's transform.
541
- final AffineMatrix transform;
542
-
543
528
/// Called by visitors to resolve [patternId] to an [AttributedNode] .
544
529
final Resolver <AttributedNode ?> resolver;
545
530
546
- @override
547
- AffineMatrix concatTransform (AffineMatrix currentTransform) {
548
- if (transform == AffineMatrix .identity) {
549
- return currentTransform;
550
- }
551
- return currentTransform.multiplied (transform);
552
- }
553
-
554
531
@override
555
532
void visitChildren (NodeCallback visitor) {
556
533
visitor (child);
0 commit comments