diff options
author | Brian Salomon <bsalomon@google.com> | 2017-11-20 13:17:43 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-11-20 19:43:18 +0000 |
commit | a0ba714ad5ee26b3f1929aa572eb77cc71809e64 (patch) | |
tree | ef549bf038a299f1c9f6466b85aba976d4ca056c /gm/atlastext.cpp | |
parent | 32eb5ba7af4da511df2c7ec9e560018c3060f914 (diff) |
Require glyph positions in SkAtlasTextTarget::drawText
Change-Id: Idf0977befc8ed4fd9eb3b733db5e945457b2164c
Reviewed-on: https://skia-review.googlesource.com/73701
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
Diffstat (limited to 'gm/atlastext.cpp')
-rw-r--r-- | gm/atlastext.cpp | 22 |
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 { |