@@ -11,6 +11,44 @@ class ImageRenderer extends Renderer<ImageElement> {
1111 this .ownsImage = true ,
1212 ]);
1313
14+ Offset get _signedSize {
15+ final constraints = element.constraints;
16+ var width = element.width;
17+ var height = element.height;
18+ if (constraints is ScaledElementConstraints ) {
19+ width *= constraints.scaleX == 0 ? 1 : constraints.scaleX;
20+ height *= constraints.scaleY == 0 ? 1 : constraints.scaleY;
21+ } else if (constraints is FixedElementConstraints ) {
22+ width = constraints.width == 0 ? width : constraints.width;
23+ height = constraints.height == 0 ? height : constraints.height;
24+ } else if (constraints is DynamicElementConstraints ) {
25+ width = constraints.width;
26+ height = constraints.height;
27+ final ratio = constraints.aspectRatio;
28+ if (ratio != 0 ) {
29+ if (width == 0 ) width = height * ratio;
30+ if (height == 0 ) height = width / ratio;
31+ }
32+ if (constraints.includeArea) {
33+ final areaRect = area? .rect;
34+ if (areaRect == null ) {
35+ width = element.width;
36+ height = element.height;
37+ } else {
38+ final right = element.position.x + element.width;
39+ final areaWidth = min (areaRect.right, right) - element.position.x;
40+ width = areaWidth <= 0 ? element.width : areaWidth;
41+ final bottom = element.position.y + element.height;
42+ final areaHeight = min (areaRect.bottom, bottom) - element.position.y;
43+ height = areaHeight <= 0 ? element.height : areaHeight;
44+ }
45+ }
46+ if (width == 0 ) width = element.width;
47+ if (height == 0 ) height = element.height;
48+ }
49+ return Offset (width, height);
50+ }
51+
1452 @override
1553 bool onAssetUpdate (
1654 NoteData document,
@@ -54,12 +92,17 @@ class ImageRenderer extends Renderer<ImageElement> {
5492 ..filterQuality = FilterQuality .medium
5593 ..isAntiAlias = true ;
5694
95+ final signedSize = _signedSize;
96+ canvas.save ();
97+ canvas.translate (element.position.x, element.position.y);
98+ canvas.scale (signedSize.dx < 0 ? - 1 : 1 , signedSize.dy < 0 ? - 1 : 1 );
5799 canvas.drawImageRect (
58100 image! ,
59- Rect .fromLTWH (0 , 0 , element.width. toDouble () , element.height. toDouble () ),
60- rect ,
101+ Rect .fromLTWH (0 , 0 , element.width, element.height),
102+ Rect . fromLTWH ( 0 , 0 , signedSize.dx. abs (), signedSize.dy. abs ()) ,
61103 paint,
62104 );
105+ canvas.restore ();
63106 }
64107
65108 @override
@@ -72,6 +115,14 @@ class ImageRenderer extends Renderer<ImageElement> {
72115 if (! rect.overlaps (viewportRect)) return ;
73116 // Create data url
74117 final data = element.getUriData (document, 'image/png' ).toString ();
118+ final signedSize = _signedSize;
119+ final flipX = signedSize.dx < 0 ;
120+ final flipY = signedSize.dy < 0 ;
121+ final svgTransform = flipX || flipY
122+ ? 'translate(${flipX ? rect .left + rect .right : 0 } '
123+ '${flipY ? rect .top + rect .bottom : 0 }) '
124+ 'scale(${flipX ? -1 : 1 } ${flipY ? -1 : 1 })'
125+ : null ;
75126 // Create image
76127 xml
77128 .getElement ('svg' )
@@ -83,6 +134,7 @@ class ImageRenderer extends Renderer<ImageElement> {
83134 'width' : '${rect .width }px' ,
84135 'height' : '${rect .height }px' ,
85136 'xlink:href' : data,
137+ 'transform' : ? svgTransform,
86138 },
87139 );
88140 }
@@ -119,60 +171,8 @@ class ImageRenderer extends Renderer<ImageElement> {
119171
120172 @override
121173 Rect get rect {
122- final constraints = element.constraints;
123- if (constraints is ScaledElementConstraints ) {
124- final scaleX = constraints.scaleX <= 0 ? 1 : constraints.scaleX;
125- final scaleY = constraints.scaleY <= 0 ? 1 : constraints.scaleY;
126- return Rect .fromLTWH (
127- element.position.x,
128- element.position.y,
129- (element.width * scaleX).toDouble (),
130- (element.height * scaleY).toDouble (),
131- );
132- } else if (constraints is FixedElementConstraints ) {
133- var height = constraints.height;
134- var width = constraints.width;
135- if (height <= 0 ) height = element.height.toDouble ();
136- if (width <= 0 ) width = element.width.toDouble ();
137- return Rect .fromLTWH (
138- element.position.x,
139- element.position.y,
140- width,
141- height,
142- );
143- } else if (constraints is DynamicElementConstraints ) {
144- var width = constraints.width;
145- var height = constraints.height;
146- final ratio = constraints.aspectRatio;
147- if (ratio != 0 ) {
148- if (width <= 0 ) width = height * ratio;
149- if (height <= 0 ) height = width / ratio;
150- }
151- if (constraints.includeArea) {
152- final areaRect = area? .rect;
153- final rightArea = areaRect? .right ?? 0 ;
154- final right = element.position.x + element.width;
155- width = min (rightArea, right) - element.position.x;
156- final bottomArea = areaRect? .bottom ?? 0 ;
157- final bottom = element.position.y + element.height;
158- height = min (bottomArea, bottom) - element.position.y;
159- }
160- if (height <= 0 ) height = element.height.toDouble ();
161- if (width <= 0 ) width = element.width.toDouble ();
162- return Rect .fromLTWH (
163- element.position.x,
164- element.position.y,
165- width,
166- height,
167- );
168- } else {
169- return Rect .fromLTWH (
170- element.position.x,
171- element.position.y,
172- element.width.toDouble (),
173- element.height.toDouble (),
174- );
175- }
174+ final position = element.position.toOffset ();
175+ return Rect .fromPoints (position, position + _signedSize);
176176 }
177177
178178 @override
0 commit comments