Skip to content

Commit c54e10e

Browse files
mdebbarJsouLiang
authored andcommitted
[web] Remove EngineParagraph and ParagraphGeometricStyle (flutter#30766)
1 parent 53eaca2 commit c54e10e

File tree

10 files changed

+28
-198
lines changed

10 files changed

+28
-198
lines changed

lib/web_ui/lib/src/engine/engine_canvas.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:ui/ui.dart' as ui;
1212

1313
import 'html/painting.dart';
1414
import 'html/render_vertices.dart';
15-
import 'text/paragraph.dart';
15+
import 'text/canvas_paragraph.dart';
1616
import 'util.dart';
1717
import 'vector_math.dart';
1818

@@ -76,7 +76,7 @@ abstract class EngineCanvas {
7676
void drawImageRect(
7777
ui.Image image, ui.Rect src, ui.Rect dst, SurfacePaintData paint);
7878

79-
void drawParagraph(EngineParagraph paragraph, ui.Offset offset);
79+
void drawParagraph(CanvasParagraph paragraph, ui.Offset offset);
8080

8181
void drawVertices(
8282
SurfaceVertices vertices, ui.BlendMode blendMode, SurfacePaintData paint);
@@ -258,7 +258,7 @@ mixin SaveStackTracking on EngineCanvas {
258258
}
259259

260260
html.Element drawParagraphElement(
261-
EngineParagraph paragraph,
261+
CanvasParagraph paragraph,
262262
ui.Offset offset, {
263263
Matrix4? transform,
264264
}) {

lib/web_ui/lib/src/engine/html/bitmap_canvas.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import '../engine_canvas.dart';
1616
import '../frame_reference.dart';
1717
import '../html_image_codec.dart';
1818
import '../platform_dispatcher.dart';
19-
import '../text/paragraph.dart';
19+
import '../text/canvas_paragraph.dart';
2020
import '../util.dart';
2121
import '../vector_math.dart';
2222
import '../window.dart';
@@ -936,7 +936,7 @@ class BitmapCanvas extends EngineCanvas {
936936
}
937937

938938
@override
939-
void drawParagraph(EngineParagraph paragraph, ui.Offset offset) {
939+
void drawParagraph(CanvasParagraph paragraph, ui.Offset offset) {
940940
assert(paragraph.isLaidOut);
941941

942942
/// - paragraph.drawOnCanvas checks that the text styling doesn't include

lib/web_ui/lib/src/engine/html/dom_canvas.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:ui/ui.dart' as ui;
1212
import '../browser_detection.dart';
1313
import '../engine_canvas.dart';
1414
import '../html_image_codec.dart';
15-
import '../text/paragraph.dart';
15+
import '../text/canvas_paragraph.dart';
1616
import '../util.dart';
1717
import '../vector_math.dart';
1818
import 'painting.dart';
@@ -127,7 +127,7 @@ class DomCanvas extends EngineCanvas with SaveElementStackTracking {
127127
@override
128128
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
129129
final html.Element paragraphElement = drawParagraphElement(
130-
paragraph as EngineParagraph, offset,
130+
paragraph as CanvasParagraph, offset,
131131
transform: currentTransform);
132132
currentElement.append(paragraphElement);
133133
}

lib/web_ui/lib/src/engine/html/recording_canvas.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import '../engine_canvas.dart';
1111
import '../picture.dart';
1212
import '../rrect_renderer.dart';
1313
import '../shadow.dart';
14-
import '../text/paragraph.dart';
14+
import '../text/canvas_paragraph.dart';
1515
import '../util.dart';
1616
import '../vector_math.dart';
1717
import 'bitmap_canvas.dart';
@@ -584,7 +584,7 @@ class RecordingCanvas {
584584

585585
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
586586
assert(!_recordingEnded);
587-
final EngineParagraph engineParagraph = paragraph as EngineParagraph;
587+
final CanvasParagraph engineParagraph = paragraph as CanvasParagraph;
588588
if (!engineParagraph.isLaidOut) {
589589
// Ignore non-laid out paragraphs. This matches Flutter's behavior.
590590
return;
@@ -1238,7 +1238,7 @@ class PaintDrawImageRect extends DrawCommand {
12381238
}
12391239

12401240
class PaintDrawParagraph extends DrawCommand {
1241-
final EngineParagraph paragraph;
1241+
final CanvasParagraph paragraph;
12421242
final ui.Offset offset;
12431243

12441244
PaintDrawParagraph(this.paragraph, this.offset);

lib/web_ui/lib/src/engine/text/canvas_paragraph.dart

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const ui.Color _defaultTextColor = ui.Color(0xFFFF0000);
2222
/// [CanvasParagraph] doesn't use a DOM element to represent the structure of
2323
/// its spans and styles. Instead it uses a flat list of [ParagraphSpan]
2424
/// objects.
25-
class CanvasParagraph implements EngineParagraph {
25+
class CanvasParagraph implements ui.Paragraph {
2626
/// This class is created by the engine, and should not be instantiated
2727
/// or extended directly.
2828
///
@@ -47,7 +47,7 @@ class CanvasParagraph implements EngineParagraph {
4747
/// The number of placeholders in this paragraph.
4848
final int placeholderCount;
4949

50-
@override
50+
/// Whether this paragraph can be drawn on a bitmap canvas.
5151
final bool drawOnCanvas;
5252

5353
@override
@@ -74,7 +74,7 @@ class CanvasParagraph implements EngineParagraph {
7474
@override
7575
bool get didExceedMaxLines => _layoutService.didExceedMaxLines;
7676

77-
@override
77+
/// Whether this paragraph has been laid out or not.
7878
bool isLaidOut = false;
7979

8080
bool get isRtl => paragraphStyle.effectiveTextDirection == ui.TextDirection.rtl;
@@ -119,20 +119,24 @@ class CanvasParagraph implements EngineParagraph {
119119

120120
// TODO(mdebbar): Returning true means we always require a bitmap canvas. Revisit
121121
// this decision once `CanvasParagraph` is fully implemented.
122-
@override
122+
/// Whether this paragraph is doing arbitrary paint operations that require
123+
/// a bitmap canvas, and can't be expressed in a DOM canvas.
123124
bool get hasArbitraryPaint => true;
124125

125-
@override
126+
/// Paints this paragraph instance on a [canvas] at the given [offset].
126127
void paint(BitmapCanvas canvas, ui.Offset offset) {
127128
_paintService.paint(canvas, offset);
128129
}
129130

130-
@override
131+
/// Generates a flat string computed from all the spans of the paragraph.
131132
String toPlainText() => plainText;
132133

133134
html.HtmlElement? _cachedDomElement;
134135

135-
@override
136+
/// Returns a DOM element that represents the entire paragraph and its
137+
/// children.
138+
///
139+
/// Generates a new DOM element on every invocation.
136140
html.HtmlElement toDomElement() {
137141
assert(isLaidOut);
138142
final html.HtmlElement? domElement = _cachedDomElement;

lib/web_ui/lib/src/engine/text/paragraph.dart

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'package:ui/ui.dart' as ui;
99

1010
import '../browser_detection.dart';
1111
import '../embedder.dart';
12-
import '../html/bitmap_canvas.dart';
1312
import '../util.dart';
1413
import 'layout_service.dart';
1514
import 'ruler.dart';
@@ -182,31 +181,6 @@ class EngineLineMetrics implements ui.LineMetrics {
182181
}
183182
}
184183

185-
/// Common interface for all the implementations of [ui.Paragraph] in the web
186-
/// engine.
187-
abstract class EngineParagraph implements ui.Paragraph {
188-
/// Whether this paragraph has been laid out or not.
189-
bool get isLaidOut;
190-
191-
/// Whether this paragraph can be drawn on a bitmap canvas.
192-
bool get drawOnCanvas;
193-
194-
/// Whether this paragraph is doing arbitrary paint operations that require
195-
/// a bitmap canvas, and can't be expressed in a DOM canvas.
196-
bool get hasArbitraryPaint;
197-
198-
void paint(BitmapCanvas canvas, ui.Offset offset);
199-
200-
/// Generates a flat string computed from all the spans of the paragraph.
201-
String toPlainText();
202-
203-
/// Returns a DOM element that represents the entire paragraph and its
204-
/// children.
205-
///
206-
/// Generates a new DOM element on every invocation.
207-
html.HtmlElement toDomElement();
208-
}
209-
210184
/// The web implementation of [ui.ParagraphStyle].
211185
class EngineParagraphStyle implements ui.ParagraphStyle {
212186
/// Creates a new instance of [EngineParagraphStyle].

lib/web_ui/lib/src/engine/text/ruler.dart

Lines changed: 1 addition & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -47,157 +47,9 @@ String buildCssFontString({
4747
return result.toString();
4848
}
4949

50-
/// Contains the subset of [ui.ParagraphStyle] properties that affect layout.
51-
class ParagraphGeometricStyle {
52-
ParagraphGeometricStyle({
53-
required this.textDirection,
54-
required this.textAlign,
55-
this.fontWeight,
56-
this.fontStyle,
57-
this.fontFamily,
58-
this.fontSize,
59-
this.lineHeight,
60-
this.maxLines,
61-
this.letterSpacing,
62-
this.wordSpacing,
63-
this.decoration,
64-
this.ellipsis,
65-
this.shadows,
66-
});
67-
68-
final ui.TextDirection textDirection;
69-
final ui.TextAlign textAlign;
70-
final ui.FontWeight? fontWeight;
71-
final ui.FontStyle? fontStyle;
72-
final String? fontFamily;
73-
final double? fontSize;
74-
final double? lineHeight;
75-
final int? maxLines;
76-
final double? letterSpacing;
77-
final double? wordSpacing;
78-
final String? decoration;
79-
final String? ellipsis;
80-
final List<ui.Shadow>? shadows;
81-
82-
// Since all fields above are primitives, cache hashcode since ruler lookups
83-
// use this style as key.
84-
int? _cachedHashCode;
85-
86-
/// Returns the font-family that should be used to style the paragraph. It may
87-
/// or may not be different from [fontFamily]:
88-
///
89-
/// - Always returns "Ahem" in tests.
90-
/// - Provides correct defaults when [fontFamily] doesn't have a value.
91-
String get effectiveFontFamily {
92-
if (assertionsEnabled) {
93-
// In widget tests we use a predictable-size font "Ahem". This makes
94-
// widget tests predictable and less flaky.
95-
if (ui.debugEmulateFlutterTesterEnvironment) {
96-
return 'Ahem';
97-
}
98-
}
99-
final String? localFontFamily = fontFamily;
100-
if (localFontFamily == null || localFontFamily.isEmpty) {
101-
return FlutterViewEmbedder.defaultFontFamily;
102-
}
103-
return localFontFamily;
104-
}
105-
106-
String? _cssFontString;
107-
108-
/// Cached font string that can be used in CSS.
109-
///
110-
/// See <https://developer.mozilla.org/en-US/docs/Web/CSS/font>.
111-
String get cssFontString {
112-
return _cssFontString ??= buildCssFontString(
113-
fontStyle: fontStyle,
114-
fontWeight: fontWeight,
115-
fontSize: fontSize,
116-
fontFamily: effectiveFontFamily,
117-
);
118-
}
119-
120-
TextHeightStyle? _cachedHeightStyle;
121-
122-
TextHeightStyle get textHeightStyle {
123-
TextHeightStyle? style = _cachedHeightStyle;
124-
if (style == null) {
125-
style = TextHeightStyle(
126-
fontFamily: effectiveFontFamily,
127-
fontSize: fontSize ?? FlutterViewEmbedder.defaultFontSize,
128-
height: lineHeight,
129-
// TODO(mdebbar): Pass the actual value when font features become supported
130-
// https://github.com/flutter/flutter/issues/64595
131-
fontFeatures: null,
132-
);
133-
_cachedHeightStyle = style;
134-
}
135-
return style;
136-
}
137-
138-
@override
139-
bool operator ==(Object other) {
140-
if (identical(this, other)) {
141-
return true;
142-
}
143-
if (other.runtimeType != runtimeType) {
144-
return false;
145-
}
146-
return other is ParagraphGeometricStyle
147-
&& other.textDirection == textDirection
148-
&& other.textAlign == textAlign
149-
&& other.fontWeight == fontWeight
150-
&& other.fontStyle == fontStyle
151-
&& other.fontFamily == fontFamily
152-
&& other.fontSize == fontSize
153-
&& other.lineHeight == lineHeight
154-
&& other.maxLines == maxLines
155-
&& other.letterSpacing == letterSpacing
156-
&& other.wordSpacing == wordSpacing
157-
&& other.decoration == decoration
158-
&& other.ellipsis == ellipsis;
159-
}
160-
161-
@override
162-
int get hashCode => _cachedHashCode ??= ui.hashValues(
163-
textDirection,
164-
textAlign,
165-
fontWeight,
166-
fontStyle,
167-
fontFamily,
168-
fontSize,
169-
lineHeight,
170-
maxLines,
171-
letterSpacing,
172-
wordSpacing,
173-
decoration,
174-
ellipsis,
175-
);
176-
177-
@override
178-
String toString() {
179-
if (assertionsEnabled) {
180-
return '$runtimeType(textDirection: $textDirection, textAlign: $textAlign,'
181-
' fontWeight: $fontWeight,'
182-
' fontStyle: $fontStyle,'
183-
' fontFamily: $fontFamily, fontSize: $fontSize,'
184-
' lineHeight: $lineHeight,'
185-
' maxLines: $maxLines,'
186-
' letterSpacing: $letterSpacing,'
187-
' wordSpacing: $wordSpacing,'
188-
' decoration: $decoration,'
189-
' ellipsis: $ellipsis,'
190-
')';
191-
} else {
192-
return super.toString();
193-
}
194-
}
195-
}
196-
19750
/// Contains all styles that have an effect on the height of text.
19851
///
199-
/// This is useful as a cache key for [TextHeightRuler]. It's more efficient
200-
/// than using the entire [ParagraphGeometricStyle] as a cache key.
52+
/// This is useful as a cache key for [TextHeightRuler].
20153
class TextHeightStyle {
20254
TextHeightStyle({
20355
required this.fontFamily,

lib/web_ui/test/html/bitmap_canvas_golden_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ Future<void> testMain() async {
147147
//
148148
// More details: https://github.com/flutter/flutter/issues/32274
149149
test('renders clipped DOM text with high quality', () async {
150-
final EngineParagraph paragraph =
150+
final CanvasParagraph paragraph =
151151
(ParagraphBuilder(ParagraphStyle(fontFamily: 'Roboto'))
152-
..addText('Am I blurry?')).build() as EngineParagraph;
152+
..addText('Am I blurry?')).build() as CanvasParagraph;
153153
paragraph.layout(const ParagraphConstraints(width: 1000));
154154

155155
final Rect canvasSize = Rect.fromLTRB(
@@ -199,7 +199,7 @@ Future<void> testMain() async {
199199
'breaks into multiple lines.';
200200
builder.addText(text);
201201

202-
final EngineParagraph paragraph = builder.build() as EngineParagraph;
202+
final CanvasParagraph paragraph = builder.build() as CanvasParagraph;
203203
paragraph.layout(const ParagraphConstraints(width: 100));
204204

205205
final Rect canvasSize = Offset.zero & const Size(500, 500);

lib/web_ui/test/html/paragraph/text_overflow_golden_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Future<void> testMain() async {
3030

3131
testEachCanvas('maxLines clipping', (EngineCanvas canvas) {
3232
Offset offset = Offset.zero;
33-
EngineParagraph p;
33+
CanvasParagraph p;
3434

3535
// All three lines are rendered.
3636
p = paragraph(threeLines);

lib/web_ui/test/html/paragraph/text_scuba.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ final ui.TextStyle _defaultTextStyle = ui.TextStyle(
108108
fontSize: 14,
109109
);
110110

111-
EngineParagraph paragraph(
111+
CanvasParagraph paragraph(
112112
String text, {
113113
ui.ParagraphStyle? paragraphStyle,
114114
ui.TextStyle? textStyle,
@@ -119,7 +119,7 @@ EngineParagraph paragraph(
119119
builder.pushStyle(textStyle ?? _defaultTextStyle);
120120
builder.addText(text);
121121
builder.pop();
122-
final EngineParagraph paragraph = builder.build() as EngineParagraph;
122+
final CanvasParagraph paragraph = builder.build() as CanvasParagraph;
123123
paragraph.layout(ui.ParagraphConstraints(width: maxWidth));
124124
return paragraph;
125125
}

0 commit comments

Comments
 (0)