Flutter Engine
The Flutter Engine
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
6
7#include <vector>
8
9#include "flutter/fml/logging.h"
17#include "third_party/skia/src/core/SkStrikeSpec.h" // nogncheck
19
20namespace impeller {
21
22/// @brief Convert a Skia axis alignment into an Impeller alignment.
23///
24/// This does not include a case for AxisAlignment::kNone, that should
25/// be used if SkFont::isSubpixel is false.
27 switch (aligment) {
29 // Skia calls this case none, meaning alignment in both X and Y.
30 // Impeller will call it "all" since that is less confusing. "none"
31 // is reserved for no subpixel alignment.
34 return AxisAlignment::kX;
36 return AxisAlignment::kY;
37 }
39}
40
42 auto& font = run.font();
43 auto typeface = std::make_shared<TypefaceSkia>(font.refTypeface());
44
45 SkFontMetrics sk_metrics;
46 font.getMetrics(&sk_metrics);
47
48 Font::Metrics metrics;
49 metrics.point_size = font.getSize();
50 metrics.embolden = font.isEmbolden();
51 metrics.skewX = font.getSkewX();
52 metrics.scaleX = font.getScaleX();
53
54 return Font{std::move(typeface), metrics, alignment};
55}
56
57static Rect ToRect(const SkRect& rect) {
58 return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
59}
60
61std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
62 const sk_sp<SkTextBlob>& blob) {
63 bool has_color = false;
64 std::vector<TextRun> runs;
65 for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) {
66 // TODO(jonahwilliams): ask Skia for a public API to look this up.
67 // https://github.com/flutter/flutter/issues/112005
69 SkBulkGlyphMetricsAndPaths paths{strikeSpec};
71 if (run.font().isSubpixel()) {
72 alignment = ToAxisAligment(
73 strikeSpec.createScalerContext()->computeAxisAlignmentForHText());
74 }
75
76 const auto glyph_count = run.glyphCount();
77 const auto* glyphs = run.glyphs();
78 switch (run.positioning()) {
80 std::vector<TextRun::GlyphPosition> positions;
81 positions.reserve(glyph_count);
82 for (auto i = 0u; i < glyph_count; i++) {
83 // kFull_Positioning has two scalars per glyph.
84 const SkPoint* glyph_points = run.points();
85 const SkPoint* point = glyph_points + i;
86 Glyph::Type type = paths.glyph(glyphs[i])->isColor()
87 ? Glyph::Type::kBitmap
89 has_color |= type == Glyph::Type::kBitmap;
90 positions.emplace_back(
92 point->x(),
93 point->y(),
94 }});
95 }
96 TextRun text_run(ToFont(run, alignment), positions);
97 runs.emplace_back(text_run);
98 break;
99 }
100 default:
101 FML_DLOG(ERROR) << "Unimplemented.";
102 continue;
103 }
104 }
105 return std::make_shared<TextFrame>(runs, ToRect(blob->bounds()), has_color);
106}
107
108} // namespace impeller
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
SkAxisAlignment
Definition: SkGlyph.h:218
GLenum type
static SkStrikeSpec MakeWithNoDevice(const SkFont &font, const SkPaint *paint=nullptr)
std::unique_ptr< SkScalerContext > createScalerContext() const
Definition: SkStrikeSpec.h:91
const SkRect & bounds() const
Definition: SkTextBlob.h:53
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
T * get() const
Definition: SkRefCnt.h:303
#define FML_DLOG(severity)
Definition: logging.h:102
#define FML_UNREACHABLE()
Definition: logging.h:109
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
font
Font Metadata and Metrics.
static Font ToFont(const SkTextBlobRunIterator &run, AxisAlignment alignment)
static Rect ToRect(const SkRect &rect)
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
static AxisAlignment ToAxisAligment(SkAxisAlignment aligment)
Convert a Skia axis alignment into an Impeller alignment.
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
Definition: run.py:1
def run(cmd)
Definition: run.py:14
@ kPath
Definition: SkGlyph.h:317
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
Describes the modifications made to the intrinsic properties of a typeface.
Definition: font.h:44
The glyph index in the typeface.
Definition: glyph.h:16
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
#define ERROR(message)
Definition: elf_loader.cc:260