flutter/examples/layers/raw/text.dart
Ben Konyi 6412f35c5e
Manual engine roll with fixes: (#26252)
7112b72cc2...e5ec3cf3ea

git log
7112b72cc229e05d36716c3d7739885d3ffa72e6..e5ec3cf3ea5ce78c6c3116271601f60d30a799d4
--no-merges --oneline
e5ec3cf3e Dart SDK roll for 2019-01-08
08c95d27a Roll src/third_party/skia 55ff5d3ba881..1337f5b85978 (10
commits) (flutter/engine#7407)
e385f5cbb Roll src/third_party/skia 26d173fee72b..55ff5d3ba881 (12
commits) (flutter/engine#7406)
0f8273b0c Dart SDK roll for 2019-01-07
4036b260a Reset ParagraphBuilder after build() (flutter/engine#7401)
4820cbec4 Dart SDK roll for 2019-01-07
8eccb860d Add onStart hook to FlutterFragmentActivity
(flutter/engine#6719)
f2ea838b3 Roll src/third_party/skia b2fdcbf3033f..26d173fee72b (10
commits) (flutter/engine#7400)
5ca8aadaa Announce in/out of list (flutter/engine#6918)
4487d392d Replace Java code with equivalent, more concise code.
(flutter/engine#7398)
395b7852d Roll src/third_party/skia 46ee3f7a8ff5..b2fdcbf3033f (11
commits) (flutter/engine#7394)
5965f9084 Make `ParagraphConstraints` have const constructor
(flutter/engine#7346)
e02dd416a Roll src/third_party/skia a47eb455360f..46ee3f7a8ff5 (2
commits) (flutter/engine#7390)
f0038b3cc Roll src/third_party/skia 3ac3a4053f86..a47eb455360f (2
commits) (flutter/engine#7389)
2019-01-08 17:59:16 -08:00

93 lines
3.8 KiB
Dart

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This example shows how to draw some bi-directional text using the raw
// interface to the engine.
import 'dart:typed_data';
import 'dart:ui' as ui;
// A paragraph represents a rectangular region that contains some text.
ui.Paragraph paragraph;
ui.Picture paint(ui.Rect paintBounds) {
final ui.PictureRecorder recorder = ui.PictureRecorder();
final ui.Canvas canvas = ui.Canvas(recorder, paintBounds);
final double devicePixelRatio = ui.window.devicePixelRatio;
final ui.Size logicalSize = ui.window.physicalSize / devicePixelRatio;
canvas.translate(logicalSize.width / 2.0, logicalSize.height / 2.0);
canvas.drawRect(ui.Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0),
ui.Paint()..color = const ui.Color.fromARGB(255, 0, 255, 0));
// The paint method of Paragraph draws the contents of the paragraph onto the
// given canvas.
canvas.drawParagraph(paragraph, ui.Offset(-paragraph.width / 2.0, (paragraph.width / 2.0) - 125.0));
return recorder.endRecording();
}
ui.Scene composite(ui.Picture picture, ui.Rect paintBounds) {
final double devicePixelRatio = ui.window.devicePixelRatio;
final Float64List deviceTransform = Float64List(16)
..[0] = devicePixelRatio
..[5] = devicePixelRatio
..[10] = 1.0
..[15] = 1.0;
final ui.SceneBuilder sceneBuilder = ui.SceneBuilder()
..pushTransform(deviceTransform)
..addPicture(ui.Offset.zero, picture)
..pop();
return sceneBuilder.build();
}
void beginFrame(Duration timeStamp) {
final ui.Rect paintBounds = ui.Offset.zero & (ui.window.physicalSize / ui.window.devicePixelRatio);
final ui.Picture picture = paint(paintBounds);
final ui.Scene scene = composite(picture, paintBounds);
ui.window.render(scene);
}
void main() {
// To create a paragraph of text, we use ParagraphBuilder.
final ui.ParagraphBuilder builder = ui.ParagraphBuilder(
// The text below has a primary direction of left-to-right.
// The embedded text has other directions.
// If this was TextDirection.rtl, the "Hello, world" text would end up on
// the other side of the right-to-left text.
ui.ParagraphStyle(textDirection: ui.TextDirection.ltr),
)
// We first push a style that turns the text blue.
..pushStyle(ui.TextStyle(color: const ui.Color(0xFF0000FF)))
..addText('Hello, ')
// The next run of text will be bold.
..pushStyle(ui.TextStyle(fontWeight: ui.FontWeight.bold))
..addText('world. ')
// The pop() command signals the end of the bold styling.
..pop()
// We add text to the paragraph in logical order. The paragraph object
// understands bi-directional text and will compute the visual ordering
// during layout.
..addText('هذا هو قليلا طويلة من النص الذي يجب التفاف .')
// The second pop() removes the blue color.
..pop()
// We can add more text with the default styling.
..addText(' و أكثر قليلا لجعله أطول. ')
..addText('สวัสดี');
// When we're done adding styles and text, we build the Paragraph object, at
// which time we can apply styling that affects the entire paragraph, such as
// left, right, or center alignment. Once built, the contents of the paragraph
// cannot be altered, but sizing and positioning information can be updated.
paragraph = builder.build()
// Next, we supply a width that the text is permitted to occupy and we ask
// the paragraph to the visual position of each its glyphs as well as its
// overall size, subject to its sizing constraints.
..layout(const ui.ParagraphConstraints(width: 180.0));
// Finally, we register our beginFrame callback and kick off the first frame.
ui.window.onBeginFrame = beginFrame;
ui.window.scheduleFrame();
}