From f2e4a039df33d9246bd0ec68602d4d06e6199553 Mon Sep 17 00:00:00 2001 From: Herb Derby Date: Mon, 18 Jun 2018 19:13:37 -0400 Subject: Add SkGlyphRunList Extend the glyph run system with a glyph run list. This allows the processing of text blobs. Add original text an cluster to runs for PDF. Change-Id: If4867d000e45f8975a30e982fc8fdbe104ef4332 Reviewed-on: https://skia-review.googlesource.com/135627 Reviewed-by: Ben Wagner Commit-Queue: Herb Derby --- src/core/SkGlyphRun.cpp | 241 ++++++++++++++++++++++++++++++++++++------------ src/core/SkGlyphRun.h | 98 +++++++++++++------- 2 files changed, 246 insertions(+), 93 deletions(-) (limited to 'src') diff --git a/src/core/SkGlyphRun.cpp b/src/core/SkGlyphRun.cpp index 29c1dfdb78..191dc75952 100644 --- a/src/core/SkGlyphRun.cpp +++ b/src/core/SkGlyphRun.cpp @@ -19,6 +19,8 @@ #include "SkPaint.h" #include "SkPaintPriv.h" #include "SkStrikeCache.h" +#include "SkTextBlob.h" +#include "SkTextBlobRunIterator.h" #include "SkTo.h" #include "SkUtils.h" @@ -58,15 +60,9 @@ void SkGlyphSet::reuse(uint32_t glyphUniverseSize, std::vector* uniqu SkASSERT(glyphUniverseSize <= (1 << 16)); fUniverseSize = glyphUniverseSize; fUniqueGlyphIDs = uniqueGlyphIDs; - // If we're hanging onto these arrays for a long time, we don't want their size to drift - // endlessly upwards. It's unusual to see more than 256 unique glyphs used in a run, - // or a typeface with more than 4096 possible glyphs. - if (fUniqueGlyphIDs->size() > 256) { - fUniqueGlyphIDs->resize(256); - fUniqueGlyphIDs->shrink_to_fit(); - } - fUniqueGlyphIDs->clear(); + // If we're hanging onto these arrays for a long time, we don't want their size to drift + // endlessly upwards. It's unusual to see a typeface with more than 4096 possible glyphs. if (glyphUniverseSize < 4096 && fIndices.size() > 4096) { fIndices.resize(4096); fIndices.shrink_to_fit(); @@ -77,6 +73,21 @@ void SkGlyphSet::reuse(uint32_t glyphUniverseSize, std::vector* uniqu } // -- SkGlyphRun ----------------------------------------------------------------------------------- +SkGlyphRun::SkGlyphRun(SkSpan denseIndex, + SkSpan positions, + SkSpan scratchGlyphs, + SkSpan uniqueGlyphIDs, + SkSpan text, + SkSpan clusters) + : fDenseIndex{denseIndex}, fPositions{positions} + , fTemporaryShuntGlyphIDs{scratchGlyphs} + , fUniqueGlyphIDs{uniqueGlyphIDs} + , fText{text} + , fClusters{clusters} { + SkASSERT(denseIndex.size() == positions.size()); + SkASSERT(denseIndex.size() == scratchGlyphs.size()); +} + void SkGlyphRun::temporaryShuntToDrawPosText(const SkPaint& paint, SkBaseDevice* device) { @@ -93,78 +104,110 @@ void SkGlyphRun::temporaryShuntToCallback(TemporaryShuntCallback callback) { callback(this->runSize(), bytes, pos); } +// -- SkGlyphRunList ------------------------------------------------------------------------------- +SkGlyphRunList::SkGlyphRunList(SkSpan glyphRuns, uint64_t uniqueID) + : fUniqueID{uniqueID} + , fGlyphRuns{glyphRuns} { } // -- SkGlyphRunBuilder ---------------------------------------------------------------------------- void SkGlyphRunBuilder::prepareDrawText( const SkPaint& paint, const void* bytes, size_t byteLength, SkPoint origin) { - - this->initializeDenseAndUnique(paint, bytes, byteLength); - - fScratchAdvances.resize(this->uniqueSize()); - { - auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(paint); - cache->getAdvances(SkSpan{fUniqueGlyphs}, fScratchAdvances.data()); - } - - SkPoint endOfLastGlyph = origin; - - for (size_t i = 0; i < this->runSize(); i++) { - fPositions.push_back(endOfLastGlyph); - endOfLastGlyph += fScratchAdvances[fDenseIndex[i]]; - } - - if (paint.getTextAlign() != SkPaint::kLeft_Align) { - SkVector len = endOfLastGlyph - origin; - if (paint.getTextAlign() == SkPaint::kCenter_Align) { - len.scale(SK_ScalarHalf); - } - for (size_t i = 0; i < this->runSize(); i++) { - fPositions[i] -= len; - } + this->initialize(); + SkSpan originalText((const char*)bytes, byteLength); + if (paint.getTextEncoding() != SkPaint::kUTF8_TextEncoding) { + originalText = SkSpan(); } - + this->drawText(paint, bytes, byteLength, origin, originalText, SkSpan()); } void SkGlyphRunBuilder::prepareDrawPosTextH(const SkPaint& paint, const void* bytes, size_t byteLength, const SkScalar* xpos, SkScalar constY) { - - this->initializeDenseAndUnique(paint, bytes, byteLength); - - for (size_t i = 0; i < runSize(); i++) { - fPositions.push_back(SkPoint::Make(xpos[i], constY)); - } + this->initialize(); + this->drawPosTextH( + paint, bytes, byteLength, xpos, constY, SkSpan(), SkSpan()); } void SkGlyphRunBuilder::prepareDrawPosText(const SkPaint& paint, const void* bytes, size_t byteLength, const SkPoint* pos) { - this->initializeDenseAndUnique(paint, bytes, byteLength); + this->initialize(); + this->drawPosText(paint, bytes, byteLength, pos, SkSpan(), SkSpan()); +} - for (size_t i = 0; i < runSize(); i++) { - fPositions.push_back(pos[i]); +void SkGlyphRunBuilder::prepareTextBlob( + const SkPaint& paint, const SkTextBlob& blob, SkPoint origin) { + this->initialize(); + fUniqueID = blob.uniqueID(); + + SkPaint runPaint = paint; + + SkTextBlobRunIterator it(&blob); + for (;!it.done(); it.next()) { + size_t glyphLen = it.glyphCount() * sizeof(uint16_t); + const SkPoint& offset = it.offset(); + + // applyFontToPaint() always overwrites the exact same attributes, + // so it is safe to not re-seed the paint for this reason. + it.applyFontToPaint(&runPaint); + + // These better be glyphs + SkASSERT(runPaint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding); + + auto text = SkSpan(it.text(), it.textSize()); + auto clusters = SkSpan(it.clusters(), it.glyphCount()); + + switch (it.positioning()) { + case SkTextBlob::kDefault_Positioning: { + auto dtOrigin = origin + offset; + this->drawText(runPaint, it.glyphs(), glyphLen, dtOrigin, text, clusters); + } + break; + case SkTextBlob::kHorizontal_Positioning: { + auto constY = origin.y() + offset.y(); + this->drawPosTextH( + runPaint, it.glyphs(), glyphLen, it.pos(), constY, text, clusters); + } + break; + case SkTextBlob::kFull_Positioning: + this->drawPosText( + runPaint, it.glyphs(), glyphLen, (const SkPoint*)it.pos(), text, clusters); + break; + default: + SK_ABORT("unhandled positioning mode"); + } } } SkGlyphRun* SkGlyphRunBuilder::useGlyphRun() { - fScratchGlyphRun.~SkGlyphRun(); - new ((void*)&fScratchGlyphRun) SkGlyphRun{SkSpan(fDenseIndex), - SkSpan(fPositions), - SkSpan( - fTemporaryShuntGlyphIDs, fDenseIndex.size()), - SkSpan(fUniqueGlyphs)}; - return &fScratchGlyphRun; + auto glyphRunList = this->useGlyphRunList(); + SkASSERT(glyphRunList->size() == 1); + return &(*glyphRunList)[0]; } -void SkGlyphRunBuilder::initializeDenseAndUnique( - const SkPaint& paint, const void* bytes, size_t byteLength) { +SkGlyphRunList* SkGlyphRunBuilder::useGlyphRunList() { + new ((void*)&fScratchGlyphRunList) SkGlyphRunList{SkSpan(fGlyphRuns), fUniqueID}; + return &fScratchGlyphRunList; +} + +size_t SkGlyphRunBuilder::runSize() const { return fDenseIndex.size(); } + +size_t SkGlyphRunBuilder::uniqueSize() const { return fUniqueGlyphs.size(); } +void SkGlyphRunBuilder::initialize() { + fUniqueID = 0; fDenseIndex.clear(); fPositions.clear(); fUniqueGlyphs.clear(); - fTemporaryShuntGlyphIDs = nullptr; + fGlyphRuns.clear(); + fLastDenseIndex = 0; + fLastUniqueIndex = 0; +} + +SkGlyphID* SkGlyphRunBuilder::addDenseAndUnique( + const SkPaint& paint, const void* bytes, size_t byteLength) { size_t runSize = 0; - const SkGlyphID* glyphIDs = nullptr; + SkGlyphID* glyphIDs = nullptr; auto encoding = paint.getTextEncoding(); auto typeface = SkPaintPriv::GetTypefaceOrDefault(paint); if (encoding != SkPaint::kGlyphID_TextEncoding) { @@ -178,18 +221,98 @@ void SkGlyphRunBuilder::initializeDenseAndUnique( } } else { runSize = byteLength / 2; - glyphIDs = (const SkGlyphID*)bytes; + glyphIDs = (SkGlyphID*)bytes; } SkASSERT(glyphIDs != nullptr); - if (runSize == 0) { return; } - fTemporaryShuntGlyphIDs = glyphIDs; + if (runSize > 0) { + fGlyphSet.reuse(typeface->countGlyphs(), &fUniqueGlyphs); + for (size_t i = 0; i < runSize; i++) { + fDenseIndex.push_back(fGlyphSet.add(glyphIDs[i])); + } + } + + return glyphIDs; +} + +void SkGlyphRunBuilder::addGlyphRunToList( + SkGlyphID* temporaryShuntGlyphIDs, SkSpan text, SkSpan clusters) { + + // Ignore empty runs. + if (fDenseIndex.size() != fLastDenseIndex) { + auto runSize = fDenseIndex.size() - fLastDenseIndex; + auto uniqueSize = fUniqueGlyphs.size() - fLastUniqueIndex; + + fGlyphRuns.emplace_back( + SkSpan(&fDenseIndex[fLastDenseIndex], runSize), + SkSpan(&fPositions[fLastDenseIndex], runSize), + SkSpan(temporaryShuntGlyphIDs, runSize), + SkSpan(&fUniqueGlyphs[fLastDenseIndex], uniqueSize), + text, + clusters); + + fLastDenseIndex = fDenseIndex.size(); + fLastUniqueIndex = fUniqueGlyphs.size(); + } +} + +void SkGlyphRunBuilder::drawText( + const SkPaint& paint, const void* bytes, size_t byteLength, SkPoint origin, + SkSpan text, SkSpan clusters) { + + SkGlyphID* temporaryShuntGlyphIDs = this->addDenseAndUnique(paint, bytes, byteLength); + + fScratchAdvances.resize(this->uniqueSize()); + { + auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(paint); + cache->getAdvances(SkSpan{fUniqueGlyphs}, fScratchAdvances.data()); + } + + SkPoint endOfLastGlyph = origin; + + for (size_t i = 0; i < this->runSize(); i++) { + fPositions.push_back(endOfLastGlyph); + endOfLastGlyph += fScratchAdvances[fDenseIndex[i]]; + } + + if (paint.getTextAlign() != SkPaint::kLeft_Align) { + SkVector len = endOfLastGlyph - origin; + if (paint.getTextAlign() == SkPaint::kCenter_Align) { + len.scale(SK_ScalarHalf); + } + for (size_t i = fLastDenseIndex; i < this->runSize(); i++) { + fPositions[i] -= len; + } + } + + this->addGlyphRunToList(temporaryShuntGlyphIDs, text, clusters); +} + +void SkGlyphRunBuilder::drawPosTextH(const SkPaint& paint, const void* bytes, + size_t byteLength, const SkScalar* xpos, + SkScalar constY, + SkSpan text, SkSpan clusters) { - fGlyphSet.reuse(typeface->countGlyphs(), &fUniqueGlyphs); - for (size_t i = 0; i < runSize; i++) { - fDenseIndex.push_back(fGlyphSet.add(glyphIDs[i])); + SkGlyphID* temporaryShuntGlyphIDs = this->addDenseAndUnique(paint, bytes, byteLength); + + for (size_t i = 0; i < runSize(); i++) { + fPositions.push_back(SkPoint::Make(xpos[i], constY)); } + + this->addGlyphRunToList(temporaryShuntGlyphIDs, text, clusters); +} + +void SkGlyphRunBuilder::drawPosText(const SkPaint& paint, const void* bytes, + size_t byteLength, const SkPoint* pos, + SkSpan text, SkSpan clusters) { + SkGlyphID* temporaryShuntGlyphIDs = this->addDenseAndUnique(paint, bytes, byteLength); + + for (size_t i = 0; i < runSize(); i++) { + fPositions.push_back(pos[i]); + } + + this->addGlyphRunToList(temporaryShuntGlyphIDs, text, clusters); } diff --git a/src/core/SkGlyphRun.h b/src/core/SkGlyphRun.h index 243d5d121c..e4676d22df 100644 --- a/src/core/SkGlyphRun.h +++ b/src/core/SkGlyphRun.h @@ -24,36 +24,32 @@ template class SkSpan { public: SkSpan() : fPtr{nullptr}, fSize{0} {} - SkSpan(const T* ptr, size_t size) : fPtr{ptr}, fSize{size} {} - explicit SkSpan(const std::vector& v) : fPtr{v.data()}, fSize{v.size()} {} - const T& operator [] (ptrdiff_t i) const { return fPtr[i]; } + SkSpan(T* ptr, size_t size) : fPtr{ptr}, fSize{size} {} + explicit SkSpan(std::vector& v) : fPtr{v.data()}, fSize{v.size()} {} + SkSpan& operator=( const SkSpan& other ) = default; + T& operator [] (ptrdiff_t i) const { return fPtr[i]; } T* begin() const { return fPtr; } T* end() const { return fPtr + fSize; } const T* cbegin() const { return fPtr; } const T* cend() const { return fPtr + fSize; } - const T* data() const { return fPtr; } + T* data() const { return fPtr; } ptrdiff_t size() const { return fSize; } bool empty() const { return fSize == 0; } private: - const T* fPtr; + T* fPtr; size_t fSize; }; class SkGlyphRun { public: SkGlyphRun() = default; - SkGlyphRun(SkSpan denseIndex, - SkSpan positions, - SkSpan scratchGlyphs, - SkSpan uniqueGlyphIDs) - : fDenseIndex{denseIndex} - , fPositions{positions} - , fTemporaryShuntGlyphIDs{scratchGlyphs} - , fUniqueGlyphIDs{uniqueGlyphIDs} { - SkASSERT(denseIndex.size() == positions.size()); - SkASSERT(denseIndex.size() == scratchGlyphs.size()); - } + SkGlyphRun(SkSpan denseIndex, + SkSpan positions, + SkSpan scratchGlyphs, + SkSpan uniqueGlyphIDs, + SkSpan text, + SkSpan clusters); // The temporaryShunt calls are to allow inter-operating with existing code while glyph runs // are developed. @@ -67,13 +63,33 @@ public: private: // Indices into the unique glyph IDs. On for each original glyph. - const SkSpan fDenseIndex; + const SkSpan fDenseIndex; // The base line position of all the glyphs in source space. - const SkSpan fPositions; + const SkSpan fPositions; // This is temporary while converting from the old per glyph code to the bulk code. - const SkSpan fTemporaryShuntGlyphIDs; + const SkSpan fTemporaryShuntGlyphIDs; // The set of unique glyphs in the run. - const SkSpan fUniqueGlyphIDs; + const SkSpan fUniqueGlyphIDs; + // Original text from SkTextBlob if present. Will be empty of not present. + const SkSpan fText; + // Original clusters from SkTextBlob if present. Will be empty if not present. + const SkSpan fClusters; +}; + +class SkGlyphRunList { + const uint64_t fUniqueID{0}; + SkSpan fGlyphRuns; + +public: + SkGlyphRunList() = default; + SkGlyphRunList(SkSpan glyphRuns, uint64_t uniqueID); + + uint64_t uniqueID() const { return fUniqueID; } + + auto begin() -> decltype(fGlyphRuns.begin()) { return fGlyphRuns.begin(); } + auto end() -> decltype(fGlyphRuns.end()) { return fGlyphRuns.end(); } + auto size() -> decltype(fGlyphRuns.size()) { return fGlyphRuns.size(); } + auto operator [] (ptrdiff_t i) -> decltype(fGlyphRuns[i]) { return fGlyphRuns[i]; } }; // A faster set implementation that does not need any initialization, and reading the set items @@ -92,10 +108,6 @@ private: std::vector* fUniqueGlyphIDs{nullptr}; }; -// Currently the old code is passing around SkGlyphRunBuilder because it facilitates working in the -// old single glyph lookup style with the cache. When the lower level code is transitioned over to -// the bulk glyph cache style, then the builder will only be used in the canvas, and only runs will -// be passed around. class SkGlyphRunBuilder { public: SkGlyphRunBuilder() = default; @@ -106,32 +118,50 @@ public: const SkScalar xpos[], SkScalar constY); void prepareDrawPosText( const SkPaint& paint, const void* bytes, size_t byteLength, const SkPoint pos[]); + void prepareTextBlob(const SkPaint& paint, const SkTextBlob& blob, SkPoint origin); - size_t runSize() const {return fDenseIndex.size();} - size_t uniqueSize() const {return fUniqueGlyphs.size();} - + SkGlyphRunList* useGlyphRunList(); SkGlyphRun* useGlyphRun(); private: - void initializeDenseAndUnique(const SkPaint& paint, const void* bytes, size_t byteLength); + size_t runSize() const; + size_t uniqueSize() const; + void initialize(); + SkGlyphID* addDenseAndUnique(const SkPaint& paint, const void* bytes, size_t byteLength); + void addGlyphRunToList( + SkGlyphID* temporaryShuntGlyphIDs, SkSpan text, SkSpan clusters); + + void drawText( + const SkPaint& paint, const void* bytes, size_t byteLength, SkPoint origin, + SkSpan text, SkSpan clusters); + void drawPosTextH( + const SkPaint& paint, const void* bytes, size_t byteLength, + const SkScalar* xpos, SkScalar constY, + SkSpan text, SkSpan clusters); + void drawPosText( + const SkPaint& paint, const void* bytes, size_t byteLength, const SkPoint* pos, + SkSpan text, SkSpan clusters); + + uint64_t fUniqueID{0}; std::vector fDenseIndex; std::vector fPositions; std::vector fUniqueGlyphs; + size_t fLastDenseIndex{0}; + size_t fLastUniqueIndex{0}; + // Used as a temporary for preparing using utfN text. std::vector fScratchGlyphIDs; // Used as temporary storage for calculating positions for drawText. std::vector fScratchAdvances; - // Used to temporarily use of a glyph run for bulk cache API calls (just an experiment at - // this point). - SkGlyphRun fScratchGlyphRun; + // Vector for accumulating runs. This is later deposited in fScratchGlyphRunList; + std::vector fGlyphRuns; - // Used as an aid to shunt from glyph runs to drawPosText. It will either be fScratchIDs or - // the bytes passed in. - const SkGlyphID* fTemporaryShuntGlyphIDs{nullptr}; + // Used as temporary glyph run for the rest of the Text stack. + SkGlyphRunList fScratchGlyphRunList; // Used for collecting the set of unique glyphs. SkGlyphSet fGlyphSet; -- cgit v1.2.3