This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Couldn't load subscription status.
- Fork 6k
[web] Rich text painting on bitmap canvas #23136
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| repository: https://github.com/flutter/goldens.git | ||
| revision: c808c28c81b6c3143ae969e8c49bed4a6d49aabb | ||
| revision: 4946ab2de031c14d30502efcaf51220e0be4d1f1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.12 | ||
| part of engine; | ||
|
|
||
| /// Responsible for painting a [CanvasParagraph] on a [BitmapCanvas]. | ||
| class TextPaintService { | ||
ferhatb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TextPaintService(this.paragraph); | ||
|
|
||
| final CanvasParagraph paragraph; | ||
|
|
||
| void paint(BitmapCanvas canvas, ui.Offset offset) { | ||
| // Loop through all the lines, for each line, loop through all the boxes and | ||
| // paint them. The boxes have enough information so they can be painted | ||
| // individually. | ||
| final List<EngineLineMetrics> lines = paragraph.computeLineMetrics(); | ||
|
|
||
| for (final EngineLineMetrics line in lines) { | ||
| for (final RangeBox box in line.boxes!) { | ||
| _paintBox(canvas, offset, line, box); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void _paintBox( | ||
| BitmapCanvas canvas, | ||
| ui.Offset offset, | ||
| EngineLineMetrics line, | ||
| RangeBox box, | ||
| ) { | ||
| final ParagraphSpan span = box.span; | ||
|
|
||
| // Placeholder spans don't need any painting. Their boxes should remain | ||
| // empty so that their underlying widgets do their own painting. | ||
| if (span is FlatTextSpan) { | ||
| // Paint the background of the box, if the span has a background. | ||
| final SurfacePaint? background = span.style._background as SurfacePaint?; | ||
| if (background != null) { | ||
| canvas.drawRect( | ||
| box.toTextBox(line).toRect().shift(offset), | ||
| background.paintData, | ||
| ); | ||
| } | ||
|
|
||
| // Paint the actual text. | ||
| _applySpanStyleToCanvas(span, canvas); | ||
| final double x = offset.dx + line.left + box.left; | ||
| final double y = offset.dy + line.baseline; | ||
| final String text = paragraph.toPlainText().substring( | ||
| box.start.index, | ||
| box.end.indexWithoutTrailingNewlines, | ||
| ); | ||
| canvas.fillText(text, x, y); | ||
|
|
||
| // Paint the ellipsis using the same span styles. | ||
| final String? ellipsis = line.ellipsis; | ||
| if (ellipsis != null && box == line.boxes!.last) { | ||
| final double x = offset.dx + line.left + box.right; | ||
| canvas.fillText(ellipsis, x, y); | ||
| } | ||
|
|
||
| canvas._tearDownPaint(); | ||
| } | ||
| } | ||
|
|
||
| void _applySpanStyleToCanvas(FlatTextSpan span, BitmapCanvas canvas) { | ||
| final SurfacePaint? paint; | ||
| final ui.Paint? foreground = span.style._foreground; | ||
| if (foreground != null) { | ||
| paint = foreground as SurfacePaint; | ||
| } else { | ||
| paint = (ui.Paint()..color = span.style._color!) as SurfacePaint; | ||
| } | ||
|
|
||
| canvas.setCssFont(span.style.cssFontString); | ||
| canvas._setUpPaint(paint.paintData, null); | ||
| } | ||
| } | ||
168 changes: 168 additions & 0 deletions
168
lib/web_ui/test/golden_tests/engine/canvas_paragraph/general_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.6 | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:test/bootstrap/browser.dart'; | ||
| import 'package:test/test.dart'; | ||
| import 'package:ui/ui.dart' hide window; | ||
| import 'package:ui/src/engine.dart'; | ||
|
|
||
| import '../scuba.dart'; | ||
| import 'helper.dart'; | ||
|
|
||
| typedef CanvasTest = FutureOr<void> Function(EngineCanvas canvas); | ||
|
|
||
| const Rect bounds = Rect.fromLTWH(0, 0, 800, 600); | ||
|
|
||
| const Color white = Color(0xFFFFFFFF); | ||
| const Color black = Color(0xFF000000); | ||
| const Color red = Color(0xFFFF0000); | ||
| const Color green = Color(0xFF00FF00); | ||
| const Color blue = Color(0xFF0000FF); | ||
|
|
||
| ParagraphConstraints constrain(double width) { | ||
| return ParagraphConstraints(width: width); | ||
| } | ||
|
|
||
| CanvasParagraph rich( | ||
| EngineParagraphStyle style, | ||
| void Function(CanvasParagraphBuilder) callback, | ||
| ) { | ||
| final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style); | ||
| callback(builder); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| void main() { | ||
| internalBootstrapBrowserTest(() => testMain); | ||
| } | ||
|
|
||
| void testMain() async { | ||
| setUpStableTestFonts(); | ||
|
|
||
| test('paints spans and lines correctly', () { | ||
| final canvas = BitmapCanvas(bounds, RenderStrategy()); | ||
|
|
||
| Offset offset = Offset.zero; | ||
| CanvasParagraph paragraph; | ||
|
|
||
| // Single-line multi-span. | ||
| paragraph = rich(ParagraphStyle(fontFamily: 'Roboto'), (builder) { | ||
| builder.pushStyle(EngineTextStyle.only(color: blue)); | ||
| builder.addText('Lorem '); | ||
| builder.pushStyle(EngineTextStyle.only( | ||
| color: green, | ||
| background: Paint()..color = red, | ||
| )); | ||
| builder.addText('ipsum '); | ||
| builder.pop(); | ||
| builder.addText('.'); | ||
| }) | ||
| ..layout(constrain(double.infinity)); | ||
| canvas.drawParagraph(paragraph, offset); | ||
| offset = offset.translate(0, paragraph.height + 10); | ||
|
|
||
| // Multi-line single-span. | ||
| paragraph = rich(ParagraphStyle(fontFamily: 'Roboto'), (builder) { | ||
| builder.addText('Lorem ipsum dolor sit'); | ||
| }) | ||
| ..layout(constrain(90.0)); | ||
| canvas.drawParagraph(paragraph, offset); | ||
| offset = offset.translate(0, paragraph.height + 10); | ||
|
|
||
| // Multi-line multi-span. | ||
| paragraph = rich(ParagraphStyle(fontFamily: 'Roboto'), (builder) { | ||
| builder.pushStyle(EngineTextStyle.only(color: blue)); | ||
| builder.addText('Lorem ipsum '); | ||
| builder.pushStyle(EngineTextStyle.only(background: Paint()..color = red)); | ||
| builder.pushStyle(EngineTextStyle.only(color: green)); | ||
| builder.addText('dolor '); | ||
| builder.pop(); | ||
| builder.addText('sit'); | ||
| }) | ||
| ..layout(constrain(90.0)); | ||
| canvas.drawParagraph(paragraph, offset); | ||
| offset = offset.translate(0, paragraph.height + 10); | ||
|
|
||
| return takeScreenshot(canvas, bounds, 'canvas_paragraph_general'); | ||
| }); | ||
|
|
||
| test('respects alignment', () { | ||
| final canvas = BitmapCanvas(bounds, RenderStrategy()); | ||
|
|
||
| Offset offset = Offset.zero; | ||
| CanvasParagraph paragraph; | ||
|
|
||
| void build(CanvasParagraphBuilder builder) { | ||
| builder.pushStyle(EngineTextStyle.only(color: black)); | ||
| builder.addText('Lorem '); | ||
| builder.pushStyle(EngineTextStyle.only(color: blue)); | ||
| builder.addText('ipsum '); | ||
| builder.pushStyle(EngineTextStyle.only(color: green)); | ||
| builder.addText('dolor '); | ||
| builder.pushStyle(EngineTextStyle.only(color: red)); | ||
| builder.addText('sit'); | ||
| } | ||
|
|
||
| paragraph = rich( | ||
| ParagraphStyle(fontFamily: 'Roboto', textAlign: TextAlign.left), | ||
| build, | ||
| )..layout(constrain(100.0)); | ||
| canvas.drawParagraph(paragraph, offset); | ||
| offset = offset.translate(0, paragraph.height + 10); | ||
|
|
||
| paragraph = rich( | ||
| ParagraphStyle(fontFamily: 'Roboto', textAlign: TextAlign.center), | ||
| build, | ||
| )..layout(constrain(100.0)); | ||
| canvas.drawParagraph(paragraph, offset); | ||
| offset = offset.translate(0, paragraph.height + 10); | ||
|
|
||
| paragraph = rich( | ||
| ParagraphStyle(fontFamily: 'Roboto', textAlign: TextAlign.right), | ||
| build, | ||
| )..layout(constrain(100.0)); | ||
| canvas.drawParagraph(paragraph, offset); | ||
| offset = offset.translate(0, paragraph.height + 10); | ||
|
|
||
| return takeScreenshot(canvas, bounds, 'canvas_paragraph_align'); | ||
| }); | ||
|
|
||
| test('paints spans with varying heights/baselines', () { | ||
| final canvas = BitmapCanvas(bounds, RenderStrategy()); | ||
|
|
||
| final CanvasParagraph paragraph = rich( | ||
| ParagraphStyle(fontFamily: 'Roboto'), | ||
| (builder) { | ||
| builder.pushStyle(EngineTextStyle.only(fontSize: 20.0)); | ||
| builder.addText('Lorem '); | ||
| builder.pushStyle(EngineTextStyle.only( | ||
| fontSize: 40.0, | ||
| background: Paint()..color = green, | ||
| )); | ||
| builder.addText('ipsum '); | ||
| builder.pushStyle(EngineTextStyle.only( | ||
| fontSize: 10.0, | ||
| color: white, | ||
| background: Paint()..color = black, | ||
| )); | ||
| builder.addText('dolor '); | ||
| builder.pushStyle(EngineTextStyle.only(fontSize: 30.0)); | ||
| builder.addText('sit '); | ||
| builder.pop(); | ||
| builder.pop(); | ||
| builder.pushStyle(EngineTextStyle.only( | ||
| fontSize: 20.0, | ||
| background: Paint()..color = blue, | ||
| )); | ||
| builder.addText('amet'); | ||
| }, | ||
| )..layout(constrain(220.0)); | ||
| canvas.drawParagraph(paragraph, Offset.zero); | ||
|
|
||
| return takeScreenshot(canvas, bounds, 'canvas_paragraph_varying_heights'); | ||
| }); | ||
| } |
34 changes: 34 additions & 0 deletions
34
lib/web_ui/test/golden_tests/engine/canvas_paragraph/helper.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // @dart = 2.12 | ||
| import 'dart:html' as html; | ||
|
|
||
| import 'package:ui/src/engine.dart'; | ||
| import 'package:ui/ui.dart'; | ||
| import 'package:web_engine_tester/golden_tester.dart'; | ||
|
|
||
| Future<void> takeScreenshot( | ||
| EngineCanvas canvas, | ||
| Rect region, | ||
| String fileName, { | ||
| bool write = false, | ||
| double? maxDiffRatePercent, | ||
| }) async { | ||
| final html.Element sceneElement = html.Element.tag('flt-scene'); | ||
| try { | ||
| sceneElement.append(canvas.rootElement); | ||
| html.document.body!.append(sceneElement); | ||
| await matchGoldenFile( | ||
| '$fileName.png', | ||
| region: region, | ||
| maxDiffRatePercent: maxDiffRatePercent, | ||
| write: write, | ||
| ); | ||
| } finally { | ||
| // The page is reused across tests, so remove the element after taking the | ||
| // Scuba screenshot. | ||
| sceneElement.remove(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
document
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.