From 4b3a5152a3cdb25d1f19fd3a04b3248f4462e05e Mon Sep 17 00:00:00 2001 From: Herb Derby Date: Tue, 17 Jul 2018 16:10:30 -0400 Subject: MakeAsDrawText for SkTextBlob Change-Id: I04ebca6b318e0654cc1e598aa323cfb9a21e5a5e Reviewed-on: https://skia-review.googlesource.com/141960 Reviewed-by: Mike Reed Reviewed-by: Cary Clark Reviewed-by: Florin Malita Commit-Queue: Herb Derby --- src/core/SkGlyphRun.cpp | 5 +++++ src/core/SkGlyphRun.h | 4 +++- src/core/SkTextBlob.cpp | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/SkGlyphRun.cpp b/src/core/SkGlyphRun.cpp index 549dce8ee8..3548dfa203 100644 --- a/src/core/SkGlyphRun.cpp +++ b/src/core/SkGlyphRun.cpp @@ -68,6 +68,11 @@ void SkGlyphRun::temporaryShuntToCallback(TemporaryShuntCallback callback) { callback(fTemporaryShuntGlyphIDs.size(), bytes, pos); } +void SkGlyphRun::filloutGlyphsAndPositions(SkGlyphID* glyphIDs, SkPoint* positions) { + memcpy(glyphIDs, fTemporaryShuntGlyphIDs.data(), fTemporaryShuntGlyphIDs.size_bytes()); + memcpy(positions, fPositions.data(), fPositions.size_bytes()); +} + // -- SkGlyphRunList ------------------------------------------------------------------------------- SkGlyphRunList::SkGlyphRunList( const SkPaint& paint, diff --git a/src/core/SkGlyphRun.h b/src/core/SkGlyphRun.h index c220a26424..001c3c4548 100644 --- a/src/core/SkGlyphRun.h +++ b/src/core/SkGlyphRun.h @@ -37,6 +37,7 @@ public: T* data() const { return fPtr; } size_t size() const { return fSize; } bool empty() const { return fSize == 0; } + size_t size_bytes() const { return fSize * sizeof(T); } private: T* fPtr; @@ -59,8 +60,9 @@ public: void temporaryShuntToDrawPosText(SkBaseDevice* device, SkPoint origin); using TemporaryShuntCallback = std::function; void temporaryShuntToCallback(TemporaryShuntCallback callback); + void filloutGlyphsAndPositions(SkGlyphID* glyphIDs, SkPoint* positions); - size_t runSize() const { return fUniqueGlyphIDIndices.size(); } + size_t runSize() const { return fTemporaryShuntGlyphIDs.size(); } SkSpan positions() const { return fPositions; } const SkPaint& paint() const { return fRunPaint; } diff --git a/src/core/SkTextBlob.cpp b/src/core/SkTextBlob.cpp index dd2f7bd1dd..fec7c527a2 100644 --- a/src/core/SkTextBlob.cpp +++ b/src/core/SkTextBlob.cpp @@ -7,6 +7,7 @@ #include "SkTextBlobRunIterator.h" +#include "SkGlyphRun.h" #include "SkPaintPriv.h" #include "SkReadBuffer.h" #include "SkSafeMath.h" @@ -886,6 +887,26 @@ sk_sp SkTextBlobPriv::MakeFromBuffer(SkReadBuffer& reader) { return blobBuilder.make(); } +sk_sp SkTextBlob::MakeAsDrawText( + const void* text, size_t byteLength, const SkPaint& paint) { + SkGlyphRunBuilder runBuilder; + + runBuilder.drawText(paint, text, byteLength, SkPoint::Make(0, 0)); + + auto list = runBuilder.useGlyphRunList(); + SkTextBlobBuilder blobBuilder; + if (!list->empty()) { + auto run = (*list)[0]; + SkPaint blobPaint(paint); + blobPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + + auto runData = blobBuilder.allocRunPos(blobPaint, run.runSize()); + run.filloutGlyphsAndPositions(runData.glyphs, (SkPoint *)runData.pos); + } + + return blobBuilder.make(); +} + sk_sp SkTextBlob::serialize(const SkSerialProcs& procs) const { SkBinaryWriteBuffer buffer; buffer.setSerialProcs(procs); -- cgit v1.2.3