| 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/fml/logging.h"
10 #include "impeller/typographer/backends/skia/typeface_skia.h"
11 #include "impeller/typographer/font.h"
12 #include "impeller/typographer/glyph.h"
13 #include "third_party/skia/include/core/SkFont.h"
14 #include "third_party/skia/include/core/SkFontMetrics.h"
15 #include "third_party/skia/include/core/SkPaint.h"
16 #include "third_party/skia/include/core/SkRect.h"
17 #include "third_party/skia/src/core/SkStrikeSpec.h" // nogncheck
18 #include "third_party/skia/src/core/SkTextBlobPriv.h" // nogncheck
19
20 namespace impeller {
21
22 static Font ToFont(const SkTextBlobRunIterator& run, AxisAlignment alignment) {
23 auto& font = run.font();
24 auto typeface = std::make_shared<TypefaceSkia>(font.refTypeface());
25
26 SkFontMetrics sk_metrics;
27 font.getMetrics(&sk_metrics);
28
29 Font::Metrics metrics;
30 metrics.point_size = font.getSize();
31 metrics.embolden = font.isEmbolden();
32 metrics.skewX = font.getSkewX();
33 metrics.scaleX = font.getScaleX();
34
35 return Font{std::move(typeface), metrics, alignment};
36 }
37
38 static Rect ToRect(const SkRect& rect) {
39 return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
40 }
41
42 std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
43 const sk_sp<SkTextBlob>& blob) {
44 bool has_color = false;
45 std::vector<TextRun> runs;
46 for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) {
47 SkStrikeSpec strikeSpec = SkStrikeSpec::MakeWithNoDevice(run.font());
48 SkBulkGlyphMetricsAndPaths paths{strikeSpec};
49 SkSpan<const SkGlyph*> glyphs =
50 paths.glyphs(SkSpan(run.glyphs(), run.glyphCount()));
51
52 for (const auto& glyph : glyphs) {
53 has_color |= glyph->isColor();
54 }
55
56 AxisAlignment alignment = AxisAlignment::kNone;
57 if (run.font().isSubpixel() && run.font().isBaselineSnap() && !has_color) {
58 alignment = AxisAlignment::kX;
59 }
60
61 switch (run.positioning()) {
62 case SkTextBlobRunIterator::kFull_Positioning: {
63 std::vector<TextRun::GlyphPosition> positions;
64 positions.reserve(run.glyphCount());
65 for (auto i = 0u; i < run.glyphCount(); i++) {
66 // kFull_Positioning has two scalars per glyph.
67 const SkPoint* glyph_points = run.points();
68 const SkPoint* point = glyph_points + i;
69 Glyph::Type type =
70 glyphs[i]->isColor() ? Glyph::Type::kBitmap : Glyph::Type::kPath;
71 positions.emplace_back(TextRun::GlyphPosition{
72 Glyph{glyphs[i]->getGlyphID(), type}, Point{
73 point->x(),
74 point->y(),
75 }});
76 }
77 TextRun text_run(ToFont(run, alignment), positions);
78 runs.emplace_back(text_run);
79 break;
80 }
81 default:
82 FML_DLOG(ERROR) << "Unimplemented.";
83 continue;
84 }
85 }
86 return std::make_shared<TextFrame>(runs, ToRect(blob->bounds()), has_color);
87 }
88
89 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:151
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:22
impeller::ToRect
static Rect ToRect(const SkRect &rect)
Definition: text_frame_skia.cc:38
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::MakeTextFrameFromTextBlobSkia
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
Definition: text_frame_skia.cc:42
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