Flutter Engine
The Flutter Engine
text_frame_stb.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
8
9namespace impeller {
10
11std::shared_ptr<TextFrame> MakeTextFrameSTB(
12 const std::shared_ptr<TypefaceSTB>& typeface_stb,
13 Font::Metrics metrics,
14 const std::string& text) {
15 TextRun run(Font(typeface_stb, metrics, AxisAlignment::kNone));
16
17 // Shape the text run using STB. The glyph positions could also be resolved
18 // using a more advanced text shaper such as harfbuzz.
19
20 float scale = stbtt_ScaleForMappingEmToPixels(
21 typeface_stb->GetFontInfo(),
23
24 int ascent, descent, line_gap;
25 stbtt_GetFontVMetrics(typeface_stb->GetFontInfo(), &ascent, &descent,
26 &line_gap);
29
30 float x = 0;
31 std::vector<Rect> bounds;
32 bounds.resize(text.size());
33 for (size_t i = 0; i < text.size(); i++) {
34 int glyph_index =
35 stbtt_FindGlyphIndex(typeface_stb->GetFontInfo(), text[i]);
36
37 int x0, y0, x1, y1;
38 stbtt_GetGlyphBitmapBox(typeface_stb->GetFontInfo(), glyph_index, scale,
39 scale, &x0, &y0, &x1, &y1);
40 float y = y0;
41
42 int advance_width;
43 int left_side_bearing;
44 stbtt_GetGlyphHMetrics(typeface_stb->GetFontInfo(), glyph_index,
45 &advance_width, &left_side_bearing);
46
47 bounds.push_back(Rect::MakeXYWH(0, 0, x1 - x0, y1 - y0));
48 Glyph glyph(glyph_index, Glyph::Type::kPath);
49 run.AddGlyph(glyph, {x + (left_side_bearing * scale), y});
50
51 if (i + 1 < text.size()) {
52 int kerning = stbtt_GetCodepointKernAdvance(typeface_stb->GetFontInfo(),
53 text[i], text[i + 1]);
54 x += std::round((advance_width + kerning) * scale);
55 }
56 }
57
58 std::optional<Rect> result;
59 for (auto i = 0u; i < bounds.size(); i++) {
60 const TextRun::GlyphPosition& gp = run.GetGlyphPositions()[i];
61 Rect glyph_rect = Rect::MakeOriginSize(gp.position + bounds[i].GetOrigin(),
62 bounds[i].GetSize());
63 result = result.has_value() ? result->Union(glyph_rect) : glyph_rect;
64 }
65
66 std::vector<TextRun> runs = {run};
67 return std::make_shared<TextFrame>(
68 runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false);
69}
70
71} // namespace impeller
static void round(SkPoint *p)
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
static constexpr float kPointsToPixels
Definition: typeface_stb.h:20
GAsyncResult * result
std::u16string text
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
std::shared_ptr< TextFrame > MakeTextFrameSTB(const std::shared_ptr< TypefaceSTB > &typeface_stb, Font::Metrics metrics, const std::string &text)
Definition: run.py:1
def run(cmd)
Definition: run.py:14
@ kPath
Definition: SkGlyph.h:317
const Scalar scale
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 MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:140
static constexpr TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129