Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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));
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);
27 ascent = std::round(ascent * scale);
28 descent = std::round(descent * scale);
29
30 float x = 0;
31 for (size_t i = 0; i < text.size(); i++) {
32 int glyph_index =
33 stbtt_FindGlyphIndex(typeface_stb->GetFontInfo(), text[i]);
34
35 int x0, y0, x1, y1;
36 stbtt_GetGlyphBitmapBox(typeface_stb->GetFontInfo(), glyph_index, scale,
37 scale, &x0, &y0, &x1, &y1);
38 float y = y0;
39
40 int advance_width;
41 int left_side_bearing;
42 stbtt_GetGlyphHMetrics(typeface_stb->GetFontInfo(), glyph_index,
43 &advance_width, &left_side_bearing);
44
45 Glyph glyph(glyph_index, Glyph::Type::kPath,
46 Rect::MakeXYWH(0, 0, x1 - x0, y1 - y0));
47 run.AddGlyph(glyph, {x + (left_side_bearing * scale), y});
48
49 if (i + 1 < text.size()) {
50 int kerning = stbtt_GetCodepointKernAdvance(typeface_stb->GetFontInfo(),
51 text[i], text[i + 1]);
52 x += std::round((advance_width + kerning) * scale);
53 }
54 }
55
56 std::optional<Rect> result;
57 for (const auto& glyph_position : run.GetGlyphPositions()) {
58 Rect glyph_rect = Rect::MakeOriginSize(
59 glyph_position.position + glyph_position.glyph.bounds.GetOrigin(),
60 glyph_position.glyph.bounds.GetSize());
61 result = result.has_value() ? result->Union(glyph_rect) : glyph_rect;
62 }
63
64 std::vector<TextRun> runs = {run};
65 return std::make_shared<TextFrame>(
66 runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false);
67}
68
69} // namespace impeller
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
static constexpr float kPointsToPixels
GAsyncResult * result
std::u16string text
double y
double x
std::shared_ptr< TextFrame > MakeTextFrameSTB(const std::shared_ptr< TypefaceSTB > &typeface_stb, Font::Metrics metrics, const std::string &text)
Definition run.py:1
const Scalar scale
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
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