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 
6 
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, 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);
27  ascent = std::round(ascent * scale);
28  descent = std::round(descent * scale);
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
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
impeller::TRect< Scalar >::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
impeller::TextRun
Represents a collection of positioned glyphs from a specific font.
Definition: text_run.h:20
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:16
font.h
text_frame_stb.h
impeller::TRect< Scalar >::MakeOriginSize
constexpr static TRect MakeOriginSize(const TPoint< Type > &origin, const TSize< Type > &size)
Definition: rect.h:144
impeller::Font::Metrics::point_size
Scalar point_size
Definition: font.h:48
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::GlyphPosition
Definition: text_run.h:22
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
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:129
impeller::Glyph::Type::kPath
@ kPath
impeller::AxisAlignment::kNone
@ kNone
impeller
Definition: allocation.cc:12
impeller::Font::Metrics
Describes the modifications made to the intrinsic properties of a typeface.
Definition: font.h:44
impeller::TRect
Definition: rect.h:122
impeller::TextRun::GlyphPosition::position
Point position
Definition: text_run.h:24