| Flutter Impeller |

text_frame_skia.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/typographer/backends/skia/text_frame_skia.h"
6
7 #include <vector>
8
9 #include "flutter/display_list/geometry/dl_path.h"
10 #include "flutter/fml/logging.h"
11 #include "fml/status.h"
12 #include "impeller/typographer/backends/skia/typeface_skia.h"
13 #include "impeller/typographer/font.h"
14 #include "impeller/typographer/glyph.h"
15 #include "third_party/skia/include/core/SkFont.h"
16 #include "third_party/skia/include/core/SkFontMetrics.h"
17 #include "third_party/skia/include/core/SkPaint.h"
18 #include "third_party/skia/include/core/SkRect.h"
19 #include "third_party/skia/modules/skparagraph/include/Paragraph.h" // nogncheck
20 #include "third_party/skia/src/core/SkStrikeSpec.h" // nogncheck
21 #include "third_party/skia/src/core/SkTextBlobPriv.h" // nogncheck
22
23 namespace impeller {
24
25 static Font ToFont(const SkTextBlobRunIterator& run, AxisAlignment alignment) {
26 auto& font = run.font();
27 auto typeface = std::make_shared<TypefaceSkia>(font.refTypeface());
28
29 SkFontMetrics sk_metrics;
30 font.getMetrics(&sk_metrics);
31
32 Font::Metrics metrics;
33 metrics.point_size = font.getSize();
34 metrics.embolden = font.isEmbolden();
35 metrics.skewX = font.getSkewX();
36 metrics.scaleX = font.getScaleX();
37
38 return Font{std::move(typeface), metrics, alignment};
39 }
40
41 static Rect ToRect(const SkRect& rect) {
42 return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
43 }
44
45 std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
46 const sk_sp<SkTextBlob>& blob) {
47 bool has_color = false;
48 std::vector<TextRun> runs;
49 for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) {
50 SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(run.font());
51 SkBulkGlyphMetricsAndPaths paths{strikeSpec};
52 SkSpan<const SkGlyph*> glyphs =
53 paths.glyphs(SkSpan(run.glyphs(), run.glyphCount()));
54
55 for (const auto& glyph : glyphs) {
56 has_color |= glyph->isColor();
57 }
58
59 AxisAlignment alignment = AxisAlignment::kNone;
60 if (run.font().isSubpixel() && run.font().isBaselineSnap() && !has_color) {
61 alignment = AxisAlignment::kX;
62 }
63
64 switch (run.positioning()) {
65 case SkTextBlobRunIterator::kFull_Positioning: {
66 std::vector<TextRun::GlyphPosition> positions;
67 positions.reserve(run.glyphCount());
68 for (auto i = 0u; i < run.glyphCount(); i++) {
69 // kFull_Positioning has two scalars per glyph.
70 const SkPoint* glyph_points = run.points();
71 const SkPoint* point = glyph_points + i;
72 Glyph::Type type =
73 glyphs[i]->isColor() ? Glyph::Type::kBitmap : Glyph::Type::kPath;
74 positions.emplace_back(TextRun::GlyphPosition{
75 Glyph{glyphs[i]->getGlyphID(), type}, Point{
76 point->x(),
77 point->y(),
78 }});
79 }
80 TextRun text_run(ToFont(run, alignment), positions);
81 runs.emplace_back(text_run);
82 break;
83 }
84 default:
85 FML_DLOG(ERROR) << "Unimplemented.";
86 continue;
87 }
88 }
89 return std::make_shared<TextFrame>(
90 runs, ToRect(blob->bounds()), has_color,
91 [blob]() -> fml::StatusOr<flutter::DlPath> {
92 SkPath path = skia::textlayout::Paragraph::GetPath(blob.get());
93 if (path.isEmpty()) {
94 return fml::Status(fml::StatusCode::kCancelled, "No path available");
95 }
96 SkPath transformed = path.makeTransform(
97 SkMatrix::Translate(blob->bounds().left(), blob->bounds().top()));
98 return flutter::DlPath(transformed);
99 });
100 }
101
102 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:153
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
impeller::TextRun
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
font.h
glyph.h
impeller
Definition: allocation.cc:12
impeller::ToFont
static Font ToFont(const SkTextBlobRunIterator &run, AxisAlignment alignment)
Definition: text_frame_skia.cc:25
impeller::ToRect
static Rect ToRect(const SkRect &rect)
Definition: text_frame_skia.cc:41
impeller::AxisAlignment
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
impeller::AxisAlignment::kX
@ kX
impeller::AxisAlignment::kNone
@ kNone
impeller::DlPath
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29
impeller::MakeTextFrameFromTextBlobSkia
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
Definition: text_frame_skia.cc:45
impeller::Font::Metrics
Describes the modifications made to the intrinsic properties of a typeface.
Definition: font.h:44
impeller::Font::Metrics::embolden
bool embolden
Definition: font.h:49
impeller::Font::Metrics::skewX
Scalar skewX
Definition: font.h:50
impeller::Font::Metrics::scaleX
Scalar scaleX
Definition: font.h:51
impeller::Font::Metrics::point_size
Scalar point_size
Definition: font.h:48
impeller::Glyph
The glyph index in the typeface.
Definition: glyph.h:16
impeller::Glyph::Type
Type
Definition: glyph.h:17
impeller::Glyph::Type::kPath
@ kPath
impeller::Glyph::Type::kBitmap
@ kBitmap
impeller::TPoint< Scalar >
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::TRect< Scalar >
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
impeller::TextRun::GlyphPosition
Definition: text_run.h:22
text_frame_skia.h
typeface_skia.h