| Flutter Impeller |

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
5 #include "impeller/typographer/backends/stb/text_frame_stb.h"
6
7 #include "impeller/typographer/font.h"
8
9 namespace impeller {
10
11 std::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_ScaleForPixelHeight(
21 typeface_stb->GetFontInfo(),
22 metrics.point_size * TypefaceSTB::kPointsToPixels);
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 =
59 Rect(glyph_position.position + glyph_position.glyph.bounds.origin,
60 glyph_position.glyph.bounds.size);
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
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:21
impeller::TRect< Scalar >::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:47
impeller::TextRun
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:19
impeller::MakeTextFrameSTB
std::shared_ptr< TextFrame > MakeTextFrameSTB(const std::shared_ptr< TypefaceSTB > &typeface_stb, Font::Metrics metrics, const std::string &text)
Definition: text_frame_stb.cc:11
impeller::Glyph
The glyph index in the typeface.
Definition: glyph.h:19
font.h
text_frame_stb.h
impeller::Font::Metrics::point_size
Scalar point_size
Definition: font.h:34
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
impeller::TextRun::AddGlyph
bool AddGlyph(Glyph glyph, Point position)
Add a glyph at the specified position to the run.
Definition: text_run.cc:26
impeller::TypefaceSTB::kPointsToPixels
static constexpr float kPointsToPixels
Definition: typeface_stb.h:20
impeller::TextRun::GetGlyphPositions
const std::vector< GlyphPosition > & GetGlyphPositions() const
Get a reference to all the glyph positions within the run.
Definition: text_run.cc:35
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:40
impeller::Glyph::Type::kPath
@ kPath
impeller
Definition: aiks_context.cc:10
impeller::Font::Metrics
Describes the modifications made to the intrinsic properties of a typeface.
Definition: font.h:30
impeller::TRect
Definition: rect.h:20