Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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"
11#include "include/core/SkRect.h"
14#include "third_party/skia/src/core/SkStrikeSpec.h" // nogncheck
16
17namespace impeller {
18
20 auto& font = run.font();
21 auto typeface = std::make_shared<TypefaceSkia>(font.refTypeface());
22
23 SkFontMetrics sk_metrics;
24 font.getMetrics(&sk_metrics);
25
26 Font::Metrics metrics;
27 metrics.point_size = font.getSize();
28 metrics.embolden = font.isEmbolden();
29 metrics.skewX = font.getSkewX();
30 metrics.scaleX = font.getScaleX();
31
32 return Font{std::move(typeface), metrics};
33}
34
35static Rect ToRect(const SkRect& rect) {
36 return Rect::MakeLTRB(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom);
37}
38
39static constexpr Scalar kScaleSize = 100000.0f;
40
41std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
42 const sk_sp<SkTextBlob>& blob) {
43 bool has_color = false;
44 std::vector<TextRun> runs;
45 for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) {
46 // TODO(jonahwilliams): ask Skia for a public API to look this up.
47 // https://github.com/flutter/flutter/issues/112005
49 SkBulkGlyphMetricsAndPaths paths{strikeSpec};
50
51 const auto glyph_count = run.glyphCount();
52 const auto* glyphs = run.glyphs();
53 switch (run.positioning()) {
55 std::vector<SkRect> glyph_bounds;
56 glyph_bounds.resize(glyph_count);
57 SkFont font = run.font();
58 auto font_size = font.getSize();
59 // For some platforms (including Android), `SkFont::getBounds()` snaps
60 // the computed bounds to integers. And so we scale up the font size
61 // prior to fetching the bounds to ensure that the returned bounds are
62 // always precise enough.
63 font.setSize(kScaleSize);
64 font.getBounds(glyphs, glyph_count, glyph_bounds.data(), nullptr);
65
66 std::vector<TextRun::GlyphPosition> positions;
67 positions.reserve(glyph_count);
68 for (auto i = 0u; i < glyph_count; i++) {
69 // kFull_Positioning has two scalars per glyph.
70 const SkPoint* glyph_points = run.points();
71 const auto* point = glyph_points + i;
72 Glyph::Type type = paths.glyph(glyphs[i])->isColor()
73 ? Glyph::Type::kBitmap
74 : Glyph::Type::kPath;
75 has_color |= type == Glyph::Type::kBitmap;
76
77 positions.emplace_back(TextRun::GlyphPosition{
78 Glyph{glyphs[i], type,
79 ToRect(glyph_bounds[i]).Scale(font_size / kScaleSize)},
80 Point{point->x(), point->y()}});
81 }
82 TextRun text_run(ToFont(run), positions);
83 runs.emplace_back(text_run);
84 break;
85 }
86 default:
87 FML_DLOG(ERROR) << "Unimplemented.";
88 continue;
89 }
90 }
91 return std::make_shared<TextFrame>(runs, ToRect(blob->bounds()), has_color);
92}
93
94} // namespace impeller
uint16_t glyphs[5]
static SkStrikeSpec MakeWithNoDevice(const SkFont &font, const SkPaint *paint=nullptr)
Describes a typeface along with any modifications to its intrinsic properties.
Definition font.h:22
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
static Rect ToRect(const SkRect &rect)
float Scalar
Definition scalar.h:18
static Font ToFont(const SkTextBlobRunIterator &run)
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
static constexpr Scalar kScaleSize
Definition run.py:1
Describes the modifications made to the intrinsic properties of a typeface.
Definition font.h:31
The glyph index in the typeface.
Definition glyph.h:20
constexpr TRect Scale(Type scale) const
Definition rect.h:188
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition rect.h:129
#define ERROR(message)