diff options
author | Mike Reed <reed@google.com> | 2019-01-30 08:25:03 -0500 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2019-01-31 18:11:52 +0000 |
commit | 5b257abb87d2085b7f36fd5bc8f4a78a73942b2a (patch) | |
tree | 19419952c5af35e89a145ba73d91f4fd24db1f5e /src | |
parent | 24d861d8bf0a2ef4b37abe4d1f488d85f86f2852 (diff) | |
download | skqp-5b257abb87d2085b7f36fd5bc8f4a78a73942b2a.tar.gz |
change measureText impl to first convert to glyphs
Bug: skia:
Change-Id: Icbc53688fb4e575695abf062dfe89e3b4b3bbb53
Reviewed-on: https://skia-review.googlesource.com/c/188021
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Mike Reed <reed@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/core/SkFont.cpp | 61 |
1 files changed, 59 insertions, 2 deletions
diff --git a/src/core/SkFont.cpp b/src/core/SkFont.cpp index 80b589b92a..c9f8e5041b 100644 --- a/src/core/SkFont.cpp +++ b/src/core/SkFont.cpp @@ -321,8 +321,8 @@ static void join_bounds_x(const SkGlyph& g, SkRect* bounds, SkScalar dx) { SkIntToScalar(g.fTop + g.fHeight)); } -SkScalar SkFont::measureText(const void* textD, size_t length, SkTextEncoding encoding, - SkRect* bounds, const SkPaint* paint) const { +SkScalar SkFont::legacy_measureText(const void* textD, size_t length, SkTextEncoding encoding, + SkRect* bounds, const SkPaint* paint) const { if (length == 0) { if (bounds) { bounds->setEmpty(); @@ -368,6 +368,63 @@ SkScalar SkFont::measureText(const void* textD, size_t length, SkTextEncoding en return width; } +SkScalar SkFont::measureText(const void* text, size_t length, SkTextEncoding encoding, + SkRect* bounds, const SkPaint* paint) const { + SkCanonicalizeFont canon(*this, paint); + const SkFont& font = canon.getFont(); + const SkScalar scale = canon.getScale(); + + SkAutoToGlyphs atg(font, text, length, encoding); + const int count = atg.count(); + if (count == 0) { + if (bounds) { + bounds->setEmpty(); + } + return 0; + } + const uint16_t* glyphs = atg.glyphs(); + + auto cache = SkStrikeCache::FindOrCreateStrikeWithNoDeviceExclusive(font, canon.getPaint()); + + SkScalar width = 0; + if (bounds) { + const SkGlyph* g = &cache->getGlyphIDMetrics(glyphs[0]); + set_bounds(*g, bounds); + width = g->fAdvanceX; + for (int i = 1; i < count; ++i) { + g = &cache->getGlyphIDMetrics(glyphs[i]); + join_bounds_x(*g, bounds, width); + width += g->fAdvanceX; + } + } else { + for (int i = 0; i < count; ++i) { + width += cache->getGlyphIDAdvance(glyphs[i]).fAdvanceX; + } + } + + if (scale) { + width *= scale; + if (bounds) { + bounds->fLeft *= scale; + bounds->fTop *= scale; + bounds->fRight *= scale; + bounds->fBottom *= scale; + } + } + +#ifdef SK_DEBUG + { + SkRect b2; + SkScalar w2 = this->legacy_measureText(text, length, encoding, &b2, paint); + SkASSERT(width == w2); + if (bounds) { + SkASSERT(*bounds == b2); + } + } +#endif + return width; +} + static SkRect make_bounds(const SkGlyph& g, SkScalar scale) { return { g.fLeft * scale, |