Skip to content

Commit 90f26f5

Browse files
Rusinomdebbar
andauthored
Fixing zoom, dropping subpixel shift (#177460)
Also, fixed decoration color and the cursor in TextEditField Part of flutter/flutter#172561 --------- Co-authored-by: Mouad Debbar <[email protected]>
1 parent 4f0a8f4 commit 90f26f5

9 files changed

Lines changed: 209 additions & 57 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/debug.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class WebParagraphDebug {
1717

1818
static void apiTrace(String arg) {
1919
assert(() {
20-
if (apiLogging) {
20+
if (apiLogging || logging) {
2121
print(arg);
2222
}
2323
return true;

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/layout.dart‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,17 @@ class TextLayout {
498498
assert((line.advance.height - (bottom - top).abs() < epsilon));
499499
case ui.BoxHeightStyle.strut:
500500
if (paragraph.paragraphStyle.strutStyle == null) {
501-
top = firstRect.top;
502-
bottom = firstRect.bottom;
501+
top = firstRect.top + line.advance.top;
502+
bottom = firstRect.top + line.advance.bottom;
503503
break;
504504
}
505505
final WebStrutStyle strutStyle = paragraph.paragraphStyle.strutStyle!;
506-
top = line.fontBoundingBoxAscent - strutStyle.strutAscent;
507-
bottom = top + strutStyle.strutDescent;
506+
top =
507+
firstRect.top +
508+
line.advance.top +
509+
line.fontBoundingBoxAscent -
510+
strutStyle.strutAscent;
511+
bottom = top + strutStyle.strutAscent + strutStyle.strutDescent;
508512
case ui.BoxHeightStyle.includeLineSpacingMiddle:
509513
top =
510514
line.advance.top +
@@ -1183,7 +1187,7 @@ class TextLine {
11831187
height: advance.height,
11841188
width: advance.width,
11851189
left: advance.left,
1186-
baseline: 0.0,
1190+
baseline: advance.top + fontBoundingBoxAscent,
11871191
lineNumber: lineNumber,
11881192
);
11891193
}

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/paint.dart‎

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
'\nsourceRect: $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}\nsource: $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);

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/painter.dart‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ import 'debug.dart';
1414
import 'layout.dart';
1515
import 'paragraph.dart';
1616

17+
// TODO(mdebbar): Discuss it: we use this canvas for painting the entire block (entire line)
18+
// so we need to make sure it's big enough to hold the biggest line.
19+
// Also, we use it to paint shadows (with vertical shifts) so we need to make it tall enough as well.
20+
const int _paintWidth = 1000;
21+
const int _paintHeight = 500;
22+
double? currentDevicePixelRatio;
23+
final DomOffscreenCanvas _paintCanvas = createDomOffscreenCanvas(_paintWidth, _paintHeight);
24+
final paintContext = _paintCanvas.getContext('2d')! as DomCanvasRenderingContext2D;
25+
1726
/// Abstracts the interface for painting text clusters, shadows, and decorations.
1827
abstract class Painter {
1928
Painter();
@@ -38,13 +47,32 @@ abstract class Painter {
3847

3948
/// Paints the decorations previously filled by [fillDecorations].
4049
void paintDecorations(ui.Canvas canvas, ui.Rect sourceRect, ui.Rect targetRect);
41-
}
4250

43-
// TODO(jlavrova): precalculate the size of the canvas based on the text to be painted
44-
// (including shadows, decorations, etc) and make sure it does not exceed the maximum canvas size
45-
// supported by the browser.
46-
final DomOffscreenCanvas _paintCanvas = createDomOffscreenCanvas(500, 500);
47-
final paintContext = _paintCanvas.getContext('2d')! as DomCanvasRenderingContext2D;
51+
/// Adjust the _paintCanvas scale based on device pixel ratio
52+
void resizePaintCanvas(double devicePixelRatio) {
53+
if (currentDevicePixelRatio == devicePixelRatio) {
54+
// Nothing changed
55+
return;
56+
}
57+
58+
// Since the output canvas is zoomed by device pixel ratio,
59+
// we need to adjust our offscreen canvas accordingly to avoid pixelation
60+
// that would happen if didn't resize it.
61+
if (currentDevicePixelRatio != null) {
62+
paintContext.restore(); // Restore to unscaled state
63+
}
64+
_paintCanvas.width = (_paintWidth * devicePixelRatio).ceilToDouble();
65+
_paintCanvas.height = (_paintHeight * devicePixelRatio).ceilToDouble();
66+
paintContext.scale(devicePixelRatio, devicePixelRatio);
67+
paintContext.save();
68+
69+
currentDevicePixelRatio = devicePixelRatio;
70+
71+
WebParagraphDebug.log(
72+
'resizePaintCanvas: ${_paintCanvas.width}x${_paintCanvas.height} @ $devicePixelRatio',
73+
);
74+
}
75+
}
4876

4977
class CanvasKitPainter extends Painter {
5078
@override
@@ -224,7 +252,7 @@ class CanvasKitPainter extends Painter {
224252
}
225253

226254
double calculateThickness(WebTextStyle textStyle) {
227-
return (textStyle.fontSize! / 14.0) * textStyle.decorationThickness!;
255+
return (textStyle.fontSize! / 14.0) * (textStyle.decorationThickness ?? 1.0);
228256
}
229257

230258
double calculatePosition(

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/paragraph.dart‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ class WebTextStyle implements ui.TextStyle {
228228
final ui.Paint? background;
229229
final List<ui.Shadow>? shadows;
230230
final ui.TextDecoration? decoration;
231-
final ui.Color? decorationColor;
232-
final ui.TextDecorationStyle? decorationStyle;
233-
final double? decorationThickness;
231+
final ui.Color? decorationColor; // Defaults to foreground color
232+
final ui.TextDecorationStyle? decorationStyle; // Defaults to none
233+
final double? decorationThickness; // Defaults to 1
234234
final double? letterSpacing;
235235
final double? wordSpacing;
236236
final double? height;
@@ -336,8 +336,6 @@ class WebTextStyle implements ui.TextStyle {
336336
}
337337

338338
ui.Color getForegroundColor() {
339-
//print('foreground: ${foreground == null ? 'null' : foreground!.color.toCssString()}');
340-
//print('color: ${color == null ? 'null' : color!.toCssString()}');
341339
return foreground != null
342340
? foreground!.color
343341
: (color != null ? color! : const ui.Color(0xFFFFFFFF));
@@ -469,11 +467,17 @@ class WebTextStyle implements ui.TextStyle {
469467
bool hasElement(StyleElements element) {
470468
switch (element) {
471469
case StyleElements.background:
472-
return background != null;
470+
// Transparent background is equivalent to no background
471+
// We do not check for transparency in other paints (like foreground) because
472+
// it seems unnatural to have a transparent paint on them
473+
return background != null && background!.color.a != 0;
473474
case StyleElements.shadows:
474475
return shadows != null && shadows!.isNotEmpty;
475476
case StyleElements.decorations:
476-
return decoration != null && decoration! != ui.TextDecoration.none;
477+
return decoration != null &&
478+
decoration! != ui.TextDecoration.none &&
479+
decorationStyle != null &&
480+
decorationColor != null;
477481
case StyleElements.text:
478482
return true;
479483
}
@@ -625,7 +629,6 @@ class TextSpan extends ParagraphSpan {
625629
DomTextMetrics _getMetrics() {
626630
// TODO(jlavrova): Is this necessary?
627631
// layoutContext.direction = isDefaultLtr ? 'ltr' : 'rtl';
628-
629632
style.applyToContext(layoutContext);
630633
return layoutContext.measureText(text);
631634
}
@@ -950,6 +953,7 @@ class WebParagraph implements ui.Paragraph {
950953
}
951954

952955
void paint(ui.Canvas canvas, ui.Offset offset) {
956+
_paint.painter.resizePaintCanvas(ui.window.devicePixelRatio);
953957
for (final line in _layout.lines) {
954958
_paint.paintLine(canvas, _layout, line, offset.dx, offset.dy);
955959
}

0 commit comments

Comments
 (0)