@@ -38,15 +38,23 @@ class TextPaint {
3838 if (block is PlaceholderBlock ) {
3939 continue ;
4040 }
41+
4142 // Let's calculate the sizes
4243 final (ui.Rect sourceRect, ui.Rect targetRect) = calculateBlock (
4344 layout,
4445 block as TextBlock ,
4546 ui.Offset (
46- line.advance.left + line.formattingShift,
47+ line.advance.left + line.formattingShift + block.shiftFromLineStart ,
4748 line.advance.top + line.fontBoundingBoxAscent - block.rawFontBoundingBoxAscent,
4849 ),
4950 ui.Offset (x, y),
51+ ui.window.devicePixelRatio,
52+ );
53+
54+ WebParagraphDebug .log (
55+ '+_paintByBlocks: ${block .textRange } ${block .spanShiftFromLineStart } ${block .shiftFromLineStart } '
56+ '${line .advance } + ${line .formattingShift } '
57+ '\n sourceRect: $sourceRect targetRect: $targetRect ' ,
5058 );
5159 // Let's draw whatever has to be drawn
5260 switch (styleElement) {
@@ -84,6 +92,7 @@ class TextPaint {
8492 WebParagraphDebug .log (
8593 '+paintByClusters: ${block .textRange } ${block .clusterRange } ${(block as TextBlock ).clusterRangeWithoutWhitespaces } ${block .whitespacesWidth } ${block .isLtr } ${line .advance .left } + ${line .formattingShift } + ${block .shiftFromLineStart }' ,
8694 );
95+
8796 // We are painting clusters in visual order so that if they step on each other, the paint
8897 // order is correct.
8998 final int start = block.isLtr
@@ -95,6 +104,7 @@ class TextPaint {
95104 final int step = block.isLtr ? 1 : - 1 ;
96105 for (int i = start; i != end; i += step) {
97106 final clusterText = layout.allClusters[i];
107+ // We need to adjust the canvas size to fit the block in case there is scaling or zoom involved
98108 final (ui.Rect sourceRect, ui.Rect targetRect) = calculateCluster (
99109 layout,
100110 block,
@@ -105,7 +115,13 @@ class TextPaint {
105115 line.advance.top + line.fontBoundingBoxAscent - block.rawFontBoundingBoxAscent,
106116 ),
107117 ui.Offset (x, y),
118+ ui.window.devicePixelRatio,
108119 );
120+
121+ if (sourceRect.isEmpty) {
122+ // Let's skip empty clusters
123+ continue ;
124+ }
109125 switch (styleElement) {
110126 case StyleElements .shadows:
111127 paintContext.save ();
@@ -130,33 +146,44 @@ class TextPaint {
130146 WebCluster webTextCluster,
131147 ui.Offset clusterOffset,
132148 ui.Offset lineOffset,
149+ double devicePixelRatio,
133150 ) {
134151 // Define the text cluster bounds
135152 final pos = webTextCluster.bounds.left - webTextCluster.advance.left;
153+
154+ // Define the text cluster bounds
155+ // Source rect must take in account the scaling
156+ final ui.Rect sourceRect = ui.Rect .fromLTWH (
157+ pos * devicePixelRatio,
158+ 0 ,
159+ webTextCluster.bounds.width.ceilToDouble () * devicePixelRatio,
160+ webTextCluster.advance.height.ceilToDouble () * devicePixelRatio,
161+ );
162+ // Target rect will be scaled by the canvas transform, so we don't scale it here
136163 final ui.Rect zeroRect = ui.Rect .fromLTWH (
137164 pos,
138165 0 ,
139- webTextCluster.bounds.width,
140- webTextCluster.advance.height,
166+ webTextCluster.bounds.width. ceilToDouble () ,
167+ webTextCluster.advance.height. ceilToDouble () ,
141168 );
142- final ui.Rect sourceRect = zeroRect;
143169
144170 // We shift the target rect to the correct x position inside the line and
145171 // the correct y position of the line itself
146172 // (and then to the paragraph.paint x and y)
147173
148- final double left = clusterOffset.dx + webTextCluster.advance.left + lineOffset.dx;
149- final double shift = left - left.floorToDouble ();
150174 // TODO(jlavrova): Make translation in a single operation so it's actually an integer
151175 final ui.Rect targetRect = zeroRect
152176 .translate (clusterOffset.dx + webTextCluster.advance.left, clusterOffset.dy)
153- .translate (lineOffset.dx, lineOffset.dy)
154- .translate (- shift, 0 );
177+ .translate (lineOffset.dx, lineOffset.dy);
155178
156179 if (WebParagraphDebug .logging) {
157180 final String text = paragraph.getText1 (webTextCluster.start, webTextCluster.end);
181+ final double left = clusterOffset.dx + webTextCluster.advance.left + lineOffset.dx;
182+ final double shift = left - left.floorToDouble ();
158183 WebParagraphDebug .log (
159- 'calculateCluster "$text " webTextCluster.bounds.width=${webTextCluster .bounds .width } clusterOffset.dx=${clusterOffset .dx }+webTextCluster.advance.left=${webTextCluster .advance .left }+lineOffset.dx=${lineOffset .dx }\n source: $sourceRect => target: $targetRect ' ,
184+ 'calculateCluster "$text " bounds: ${webTextCluster .bounds } advance: ${webTextCluster .advance } shift $shift \n '
185+ 'clusterOffset: $clusterOffset lineOffset: $lineOffset \n '
186+ 'source: $sourceRect => target: $targetRect ' ,
160187 );
161188 }
162189
@@ -168,12 +195,20 @@ class TextPaint {
168195 TextBlock block,
169196 ui.Offset blockOffset,
170197 ui.Offset paragraphOffset,
198+ double devicePixelRatio,
171199 ) {
172200 final ui.Rect advance = block.advance;
173201
174202 // Define the text clusters rect (using advances, not selected rects)
203+ // Source rect must take in account the scaling
204+ final ui.Rect sourceRect = ui.Rect .fromLTWH (
205+ 0 ,
206+ 0 ,
207+ advance.width * devicePixelRatio,
208+ advance.height * devicePixelRatio,
209+ );
210+ // Target rect will be scaled by the canvas transform, so we don't scale it here
175211 final ui.Rect zeroRect = ui.Rect .fromLTWH (0 , 0 , advance.width, advance.height);
176- final ui.Rect sourceRect = zeroRect;
177212
178213 // We shift the target rect to the correct x position inside the line and
179214 // the correct y position of the line itself
@@ -182,7 +217,7 @@ class TextPaint {
182217 final ui.Rect targetRect = zeroRect
183218 // TODO(jlavrova): Can we use `block.advance.left` instead of the cluster? That way
184219 // we don't have to worry about LTR vs RTL to get first cluster.
185- .translate (blockOffset.dx + block.advance.left , blockOffset.dy)
220+ .translate (blockOffset.dx, blockOffset.dy)
186221 .translate (paragraphOffset.dx, paragraphOffset.dy);
187222
188223 final String text = paragraph.getText (block.textRange);
0 commit comments