12 const std::shared_ptr<TypefaceSTB>& typeface_stb,
14 const std::string& text) {
20 float scale = stbtt_ScaleForMappingEmToPixels(
21 typeface_stb->GetFontInfo(),
24 int ascent, descent, line_gap;
25 stbtt_GetFontVMetrics(typeface_stb->GetFontInfo(), &ascent, &descent,
27 ascent = std::round(ascent *
scale);
28 descent = std::round(descent *
scale);
31 std::vector<Rect> bounds;
32 bounds.resize(text.size());
33 for (
size_t i = 0; i < text.size(); i++) {
35 stbtt_FindGlyphIndex(typeface_stb->GetFontInfo(), text[i]);
38 stbtt_GetGlyphBitmapBox(typeface_stb->GetFontInfo(), glyph_index,
scale,
39 scale, &x0, &y0, &x1, &y1);
43 int left_side_bearing;
44 stbtt_GetGlyphHMetrics(typeface_stb->GetFontInfo(), glyph_index,
45 &advance_width, &left_side_bearing);
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);
58 std::optional<Rect> result;
59 for (
auto i = 0u; i < bounds.size(); i++) {
63 result = result.has_value() ? result->Union(glyph_rect) : glyph_rect;
66 std::vector<TextRun> runs = {run};
67 return std::make_shared<TextFrame>(