diff options
author | robertphillips <robertphillips@google.com> | 2016-07-19 07:59:22 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-07-19 07:59:22 -0700 |
commit | e34f17d23699abfc672289f51319b37294b3c257 (patch) | |
tree | eef74fb1356491c4a23c31e32ead9a275a8fa09c | |
parent | b117ff194ff888ef9107a4797aad053b0d76be30 (diff) |
Make SkFont a bit more useable
Split out of https://codereview.chromium.org/2163483002/ (Use SkFont in GrStencilAndCoverTextContext)
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2163633002
Review-Url: https://codereview.chromium.org/2163633002
-rw-r--r-- | include/core/SkFont.h | 9 | ||||
-rw-r--r-- | include/core/SkPaint.h | 4 | ||||
-rw-r--r-- | src/core/SkDraw.cpp | 4 | ||||
-rw-r--r-- | src/core/SkFont.cpp | 16 | ||||
-rw-r--r-- | src/core/SkPaint.cpp | 24 | ||||
-rw-r--r-- | src/gpu/text/GrStencilAndCoverTextContext.cpp | 8 | ||||
-rw-r--r-- | src/gpu/text/GrTextUtils.cpp | 12 | ||||
-rw-r--r-- | src/pdf/SkPDFDevice.cpp | 8 |
8 files changed, 63 insertions, 22 deletions
diff --git a/include/core/SkFont.h b/include/core/SkFont.h index 6c231c963a..6e8a71501f 100644 --- a/include/core/SkFont.h +++ b/include/core/SkFont.h @@ -126,6 +126,10 @@ public: * If size is not supported (e.g. <= 0 or non-finite) NULL will be returned. */ sk_sp<SkFont> makeWithSize(SkScalar size) const; + /** + * Return a font with the same attributes of this font, but with the flags. + */ + sk_sp<SkFont> makeWithFlags(uint32_t newFlags) const; SkTypeface* getTypeface() const { return fTypeface.get(); } SkScalar getSize() const { return fSize; } @@ -139,10 +143,15 @@ public: bool isEnableAutoHints() const { return SkToBool(fFlags & kEnableAutoHints_Flag); } bool isEnableByteCodeHints() const { return SkToBool(fFlags & kEnableByteCodeHints_Flag); } bool isUseNonLinearMetrics() const { return SkToBool(fFlags & kUseNonlinearMetrics_Flag); } + bool isDevKern() const { return SkToBool(fFlags & kDevKern_Flag); } int textToGlyphs(const void* text, size_t byteLength, SkTextEncoding, uint16_t glyphs[], int maxGlyphCount) const; + int countText(const void* text, size_t byteLength, SkTextEncoding encoding) { + return this->textToGlyphs(text, byteLength, encoding, nullptr, 0); + } + SkScalar measureText(const void* text, size_t byteLength, SkTextEncoding) const; static sk_sp<SkFont> Testing_CreateFromPaint(const SkPaint&); diff --git a/include/core/SkPaint.h b/include/core/SkPaint.h index c0f1f3eec8..caa185608a 100644 --- a/include/core/SkPaint.h +++ b/include/core/SkPaint.h @@ -1085,7 +1085,9 @@ private: uint32_t fBitfieldsUInt; }; - GlyphCacheProc getGlyphCacheProc(bool needFullMetrics) const; + static GlyphCacheProc GetGlyphCacheProc(TextEncoding encoding, + bool isDevKern, + bool needFullMetrics); SkScalar measure_text(SkGlyphCache*, const char* text, size_t length, int* count, SkRect* bounds) const; diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp index 32fe4bbe76..7787a28249 100644 --- a/src/core/SkDraw.cpp +++ b/src/core/SkDraw.cpp @@ -1638,7 +1638,9 @@ void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength, paint.setStyle(SkPaint::kFill_Style); paint.setPathEffect(nullptr); - SkPaint::GlyphCacheProc glyphCacheProc = paint.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(), + paint.isDevKernText(), + true); SkAutoGlyphCache cache(paint, &fDevice->surfaceProps(), this->scalerContextFlags(), nullptr); const char* stop = text + byteLength; diff --git a/src/core/SkFont.cpp b/src/core/SkFont.cpp index 1300011ec4..aa7fe366c8 100644 --- a/src/core/SkFont.cpp +++ b/src/core/SkFont.cpp @@ -48,6 +48,10 @@ sk_sp<SkFont> SkFont::makeWithSize(SkScalar newSize) const { this->getSkewX(), this->getMaskType(), this->getFlags()); } +sk_sp<SkFont> SkFont::makeWithFlags(uint32_t newFlags) const { + return SkFont::Make(sk_ref_sp(this->getTypeface()), this->getSize(), this->getScaleX(), + this->getSkewX(), this->getMaskType(), newFlags); +} /////////////////////////////////////////////////////////////////////////////////////////////////// int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding encoding, @@ -74,21 +78,21 @@ int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding enc count = SkToInt(byteLength >> 1); break; } - if (nullptr == glyphs) { + if (!glyphs) { return count; } // TODO: unify/eliminate SkTypeface::Encoding with SkTextEncoding - SkTypeface::Encoding typeface_encoding; + SkTypeface::Encoding typefaceEncoding; switch (encoding) { case kUTF8_SkTextEncoding: - typeface_encoding = SkTypeface::kUTF8_Encoding; + typefaceEncoding = SkTypeface::kUTF8_Encoding; break; case kUTF16_SkTextEncoding: - typeface_encoding = SkTypeface::kUTF16_Encoding; + typefaceEncoding = SkTypeface::kUTF16_Encoding; break; case kUTF32_SkTextEncoding: - typeface_encoding = SkTypeface::kUTF32_Encoding; + typefaceEncoding = SkTypeface::kUTF32_Encoding; break; default: SkASSERT(kGlyphID_SkTextEncoding == encoding); @@ -97,7 +101,7 @@ int SkFont::textToGlyphs(const void* text, size_t byteLength, SkTextEncoding enc return count; } - (void)fTypeface->charsToGlyphs(text, typeface_encoding, glyphs, count); + (void)fTypeface->charsToGlyphs(text, typefaceEncoding, glyphs, count); return count; } diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp index 25b6aec1ec..528b1d0404 100644 --- a/src/core/SkPaint.cpp +++ b/src/core/SkPaint.cpp @@ -661,7 +661,9 @@ static const SkGlyph& sk_getAdvance_glyph_next(SkGlyphCache* cache, return cache->getGlyphIDAdvance(glyphID); } -SkPaint::GlyphCacheProc SkPaint::getGlyphCacheProc(bool needFullMetrics) const { +SkPaint::GlyphCacheProc SkPaint::GetGlyphCacheProc(TextEncoding encoding, + bool isDevKern, + bool needFullMetrics) { static const GlyphCacheProc gGlyphCacheProcs[] = { sk_getMetrics_utf8_next, sk_getMetrics_utf16_next, @@ -674,9 +676,9 @@ SkPaint::GlyphCacheProc SkPaint::getGlyphCacheProc(bool needFullMetrics) const { sk_getAdvance_glyph_next, }; - unsigned index = this->getTextEncoding(); + unsigned index = encoding; - if (!needFullMetrics && !this->isDevKernText()) { + if (!needFullMetrics && !isDevKern) { index += 4; } @@ -774,7 +776,9 @@ SkScalar SkPaint::measure_text(SkGlyphCache* cache, return 0; } - GlyphCacheProc glyphCacheProc = this->getGlyphCacheProc(nullptr != bounds); + GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(this->getTextEncoding(), + this->isDevKernText(), + nullptr != bounds); int xyIndex; JoinBoundsProc joinBoundsProc; @@ -892,7 +896,9 @@ size_t SkPaint::breakText(const void* textD, size_t length, SkScalar maxWidth, SkAutoGlyphCache autoCache(paint, nullptr, nullptr); SkGlyphCache* cache = autoCache.getCache(); - GlyphCacheProc glyphCacheProc = paint.getGlyphCacheProc(false); + GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(), + paint.isDevKernText(), + false); const int xyIndex = paint.isVerticalText() ? 1 : 0; SkScalar width = 0; @@ -1005,7 +1011,9 @@ int SkPaint::getTextWidths(const void* textData, size_t byteLength, SkAutoGlyphCache autoCache(paint, nullptr, nullptr); SkGlyphCache* cache = autoCache.getCache(); - GlyphCacheProc glyphCacheProc = paint.getGlyphCacheProc(nullptr != bounds); + GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(), + paint.isDevKernText(), + nullptr != bounds); const char* text = (const char*)textData; const char* stop = text + byteLength; @@ -2182,7 +2190,9 @@ SkTextBaseIter::SkTextBaseIter(const char text[], size_t length, const SkPaint& paint, bool applyStrokeAndPathEffects) : fPaint(paint) { - fGlyphCacheProc = paint.getGlyphCacheProc(true); + fGlyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(), + paint.isDevKernText(), + true); fPaint.setLinearText(true); fPaint.setMaskFilter(nullptr); // don't want this affecting our path-cache lookup diff --git a/src/gpu/text/GrStencilAndCoverTextContext.cpp b/src/gpu/text/GrStencilAndCoverTextContext.cpp index dd93025526..632bdcdaf1 100644 --- a/src/gpu/text/GrStencilAndCoverTextContext.cpp +++ b/src/gpu/text/GrStencilAndCoverTextContext.cpp @@ -469,7 +469,9 @@ void GrStencilAndCoverTextContext::TextRun::setText(const char text[], size_t by SkASSERT(byteLength == 0 || text != nullptr); SkGlyphCache* glyphCache = this->getGlyphCache(); - SkPaint::GlyphCacheProc glyphCacheProc = fFont.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(fFont.getTextEncoding(), + fFont.isDevKernText(), + true); fTotalGlyphCount = fFont.countText(text, byteLength); fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, @@ -529,7 +531,9 @@ void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); SkGlyphCache* glyphCache = this->getGlyphCache(); - SkPaint::GlyphCacheProc glyphCacheProc = fFont.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(fFont.getTextEncoding(), + fFont.isDevKernText(), + true); fTotalGlyphCount = fFont.countText(text, byteLength); fInstanceData.reset(InstanceData::Alloc(GrPathRendering::kTranslate_PathTransformType, diff --git a/src/gpu/text/GrTextUtils.cpp b/src/gpu/text/GrTextUtils.cpp index 9205d2fd7c..56d4ec1c00 100644 --- a/src/gpu/text/GrTextUtils.cpp +++ b/src/gpu/text/GrTextUtils.cpp @@ -253,7 +253,9 @@ void GrTextUtils::DrawDFText(GrAtlasTextBlob* blob, int runIndex, return; } - SkPaint::GlyphCacheProc glyphCacheProc = skPaint.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(skPaint.getTextEncoding(), + skPaint.isDevKernText(), + true); SkAutoDescriptor desc; SkScalerContextEffects effects; // We apply the fake-gamma by altering the distance in the shader, so we ignore the @@ -345,7 +347,9 @@ void GrTextUtils::DrawDFPosText(GrAtlasTextBlob* blob, int runIndex, // passed-in scaler context flags. (It's only used when we fall-back to bitmap text). SkGlyphCache* cache = blob->setupCache(runIndex, props, SkPaint::kNone_ScalerContextFlags, dfPaint, nullptr); - SkPaint::GlyphCacheProc glyphCacheProc = dfPaint.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(dfPaint.getTextEncoding(), + dfPaint.isDevKernText(), + true); const char* stop = text + byteLength; @@ -508,7 +512,9 @@ void GrTextUtils::DrawPosTextAsPath(GrContext* context, paint.setStyle(SkPaint::kFill_Style); paint.setPathEffect(nullptr); - SkPaint::GlyphCacheProc glyphCacheProc = paint.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(), + paint.isDevKernText(), + true); SkAutoGlyphCache autoCache(paint, &props, nullptr); SkGlyphCache* cache = autoCache.getCache(); diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp index 33ae8a49d8..01e8c82526 100644 --- a/src/pdf/SkPDFDevice.cpp +++ b/src/pdf/SkPDFDevice.cpp @@ -1216,7 +1216,9 @@ void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len, int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs); textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); - SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(textPaint.getTextEncoding(), + textPaint.isDevKernText(), + true); align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y); content.entry()->fContent.writeText("BT\n"); set_text_transform(x, y, textPaint.getTextSkewX(), @@ -1293,7 +1295,9 @@ void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len, size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs); textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); - SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true); + SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(textPaint.getTextEncoding(), + textPaint.isDevKernText(), + true); content.entry()->fContent.writeText("BT\n"); this->updateFont(textPaint, glyphIDs[0], content.entry()); GlyphPositioner glyphPositioner(&content.entry()->fContent, |