aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/atlastext.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gm/atlastext.cpp')
-rw-r--r--gm/atlastext.cpp22
1 files changed, 17 insertions, 5 deletions
diff --git a/gm/atlastext.cpp b/gm/atlastext.cpp
index 8270306e5e..6ee092b49f 100644
--- a/gm/atlastext.cpp
+++ b/gm/atlastext.cpp
@@ -25,19 +25,31 @@
static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y,
uint32_t color, sk_sp<SkTypeface> typeface, float size) {
+ if (!text.size()) {
+ return x;
+ }
auto font = SkAtlasTextFont::Make(typeface, size);
int cnt = SkUTF8_CountUnichars(text.c_str());
std::unique_ptr<SkGlyphID[]> glyphs(new SkGlyphID[cnt]);
typeface->charsToGlyphs(text.c_str(), SkTypeface::Encoding::kUTF8_Encoding, glyphs.get(), cnt);
- target->drawText(glyphs.get(), cnt, x, y, color, *font);
-
- // Using a paint just to perform a measure to let the caller know how much space to skip in x.
+ // Using a paint to get the positions for each glyph.
SkPaint paint;
paint.setTextSize(size);
paint.setTypeface(std::move(typeface));
- paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
- return x + paint.measureText(text.c_str(), text.size());
+ paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
+ std::unique_ptr<SkScalar[]> widths(new SkScalar[cnt]);
+ paint.getTextWidths(glyphs.get(), cnt * sizeof(SkGlyphID), widths.get(), nullptr);
+
+ std::unique_ptr<SkPoint[]> positions(new SkPoint[cnt]);
+ positions[0] = {x, y};
+ for (int i = 1; i < cnt; ++i) {
+ positions[i] = {positions[i - 1].fX + widths[i - 1], y};
+ }
+
+ target->drawText(glyphs.get(), positions.get(), cnt, color, *font);
+
+ return positions[cnt - 1].fX + widths[cnt - 1];
}
class AtlasTextGM : public skiagm::GM {