From eb3f674d6ddd88e4a31570b26f61238b4b82f490 Mon Sep 17 00:00:00 2001 From: Herb Derby Date: Mon, 5 Mar 2018 14:36:45 -0500 Subject: Add GetTypefaceOrDefault to SkPaintPriv Remove most uses of GetDefaultTypeface. SkTypeface has fewer friends. BUG=skia:7515 Change-Id: Iedec5b39b9ef8c638772be4971075491b59b740b Reviewed-on: https://skia-review.googlesource.com/112300 Reviewed-by: Ben Wagner Commit-Queue: Herb Derby --- include/core/SkTypeface.h | 8 ++------ src/core/SkGlyphCache.cpp | 7 +++---- src/core/SkPaint.cpp | 18 ++++++------------ src/core/SkPaintPriv.h | 11 ++++++++++- src/core/SkScalerContext.cpp | 6 ++---- src/core/SkTextBlob.cpp | 3 ++- src/gpu/text/GrAtlasTextBlob.cpp | 3 ++- src/gpu/text/GrAtlasTextContext.cpp | 4 +++- src/svg/SkSVGDevice.cpp | 3 ++- tests/FontHostStreamTest.cpp | 4 ++-- 10 files changed, 34 insertions(+), 33 deletions(-) diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index fb2c64a200..b55ac4ecb3 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -309,6 +309,7 @@ protected: virtual SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, const SkDescriptor*) const = 0; virtual void onFilterRec(SkScalerContextRec*) const = 0; + friend class SkScalerContext; // onFilterRec // Subclasses *must* override this method to work with the PDF backend. virtual std::unique_ptr onGetAdvancedMetrics() const; @@ -364,12 +365,7 @@ private: }; static SkFontStyle FromOldStyle(Style oldStyle); static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal); - static SkTypeface* NormalizeTypeface(SkTypeface* typeface) { - return typeface != nullptr ? typeface : SkTypeface::GetDefaultTypeface(); - } - friend class SkGlyphCache; // GetDefaultTypeface - friend class SkPaint; // GetDefaultTypeface - friend class SkScalerContext; // GetDefaultTypeface + friend class SkPaintPriv; // GetDefaultTypeface private: SkFontID fUniqueID; diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp index 087fb523cf..9bb43604a6 100644 --- a/src/core/SkGlyphCache.cpp +++ b/src/core/SkGlyphCache.cpp @@ -9,6 +9,7 @@ #include "SkGlyphCache.h" #include "SkGraphics.h" #include "SkOnce.h" +#include "SkPaintPriv.h" #include "SkPath.h" #include "SkTemplates.h" #include "SkTraceMemoryDump.h" @@ -788,9 +789,7 @@ void SkGraphics::SetTLSFontCacheLimit(size_t bytes) { } SkGlyphCache* SkGlyphCache::DetachCache( SkTypeface* typeface, const SkScalerContextEffects& effects, const SkDescriptor* desc) { - - auto cache = FindOrCreateStrikeExclusive( - *desc, effects, *SkTypeface::NormalizeTypeface(typeface)); + auto cache = FindOrCreateStrikeExclusive(*desc, effects, *typeface); return cache.release(); } @@ -804,5 +803,5 @@ SkGlyphCache* SkGlyphCache::DetachCacheUsingPaint(const SkPaint& paint, auto desc = SkScalerContext::CreateDescriptorAndEffectsUsingPaint( paint, surfaceProps, scalerContextFlags, deviceMatrix, &ad, &effects); - return SkGlyphCache::DetachCache(paint.getTypeface(), effects, desc); + return SkGlyphCache::DetachCache(SkPaintPriv::GetTypefaceOrDefault(paint), effects, desc); } diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp index 76f50ce1a1..e3680713b5 100644 --- a/src/core/SkPaint.cpp +++ b/src/core/SkPaint.cpp @@ -928,7 +928,7 @@ SkScalar SkPaint::getFontMetrics(FontMetrics* metrics, SkScalar zoom) const { paint, nullptr, SkScalerContextFlags::kNone, zoomPtr, &ad, &effects); { - auto typeface = SkTypeface::NormalizeTypeface(paint.getTypeface()); + auto typeface = SkPaintPriv::GetTypefaceOrDefault(paint); auto cache = SkGlyphCache::FindOrCreateStrikeExclusive(*desc, effects, *typeface); *metrics = cache->getFontMetrics(); } @@ -1201,10 +1201,7 @@ SkRect SkPaint::getFontBounds() const { m.setScale(fTextSize * fTextScaleX, fTextSize); m.postSkew(fTextSkewX, 0); - SkTypeface* typeface = this->getTypeface(); - if (nullptr == typeface) { - typeface = SkTypeface::GetDefaultTypeface(); - } + SkTypeface* typeface = SkPaintPriv::GetTypefaceOrDefault(*this); SkRect bounds; m.mapRect(&bounds, typeface->getBounds()); @@ -1386,13 +1383,10 @@ static FlatFlags unpack_paint_flags(SkPaint* paint, uint32_t packed) { it if there are not tricky elements like shaders, etc. */ void SkPaint::flatten(SkWriteBuffer& buffer) const { - SkTypeface* tf = this->getTypeface(); - if (!tf) { - // We force recording our typeface, even if its "default" since the receiver process - // may have a different notion of default. - tf = SkTypeface::GetDefaultTypeface(); - SkASSERT(tf); - } + // We force recording our typeface, even if its "default" since the receiver process + // may have a different notion of default. + SkTypeface* tf = SkPaintPriv::GetTypefaceOrDefault(*this); + SkASSERT(tf); uint8_t flatFlags = kHasTypeface_FlatFlag; diff --git a/src/core/SkPaintPriv.h b/src/core/SkPaintPriv.h index 08f80bf93a..c60c232ad4 100644 --- a/src/core/SkPaintPriv.h +++ b/src/core/SkPaintPriv.h @@ -9,8 +9,9 @@ #define SkPaintPriv_DEFINED #include "SkImageInfo.h" -#include "SkPaint.h" #include "SkMatrix.h" +#include "SkPaint.h" +#include "SkTypeface.h" class SkBitmap; class SkImage; @@ -67,6 +68,14 @@ public: // returns 0 if buffer is invalid for specified encoding static int ValidCountText(const void* text, size_t length, SkPaint::TextEncoding); + + static SkTypeface* GetTypefaceOrDefault(const SkPaint& paint) { + return paint.getTypeface() ? paint.getTypeface() : SkTypeface::GetDefaultTypeface(); + } + + static sk_sp RefTypefaceOrDefault(const SkPaint& paint) { + return paint.getTypeface() ? paint.refTypeface() : SkTypeface::MakeDefault(); + } }; #endif diff --git a/src/core/SkScalerContext.cpp b/src/core/SkScalerContext.cpp index a821ec2f5b..e0bded01ad 100644 --- a/src/core/SkScalerContext.cpp +++ b/src/core/SkScalerContext.cpp @@ -898,10 +898,8 @@ void SkScalerContext::MakeRecAndEffects(const SkPaint& paint, SkScalerContextEffects* effects) { SkASSERT(deviceMatrix == nullptr || !deviceMatrix->hasPerspective()); - SkTypeface* typeface = paint.getTypeface(); - if (nullptr == typeface) { - typeface = SkTypeface::GetDefaultTypeface(); - } + SkTypeface* typeface = SkPaintPriv::GetTypefaceOrDefault(paint); + rec->fFontID = typeface->uniqueID(); rec->fTextSize = paint.getTextSize(); rec->fPreScaleX = paint.getTextScaleX(); diff --git a/src/core/SkTextBlob.cpp b/src/core/SkTextBlob.cpp index 6d89e6f135..f686f29e83 100644 --- a/src/core/SkTextBlob.cpp +++ b/src/core/SkTextBlob.cpp @@ -7,6 +7,7 @@ #include "SkTextBlobRunIterator.h" +#include "SkPaintPriv.h" #include "SkReadBuffer.h" #include "SkSafeMath.h" #include "SkTypeface.h" @@ -26,7 +27,7 @@ public: RunFont(const SkPaint& paint) : fSize(paint.getTextSize()) , fScaleX(paint.getTextScaleX()) - , fTypeface(SkSafeRef(paint.getTypeface())) + , fTypeface(SkPaintPriv::RefTypefaceOrDefault(paint)) , fSkewX(paint.getTextSkewX()) , fAlign(paint.getTextAlign()) , fHinting(paint.getHinting()) diff --git a/src/gpu/text/GrAtlasTextBlob.cpp b/src/gpu/text/GrAtlasTextBlob.cpp index bd3d76814f..b4404c3ef9 100644 --- a/src/gpu/text/GrAtlasTextBlob.cpp +++ b/src/gpu/text/GrAtlasTextBlob.cpp @@ -14,6 +14,7 @@ #include "SkDrawFilter.h" #include "SkGlyphCache.h" #include "SkMaskFilterBase.h" +#include "SkPaintPriv.h" #include "SkTextBlobRunIterator.h" #include "SkTextToPathIter.h" #include "ops/GrAtlasTextOp.h" @@ -67,7 +68,7 @@ SkGlyphCache* GrAtlasTextBlob::setupCache(int runIndex, SkScalerContextEffects effects; SkScalerContext::CreateDescriptorAndEffectsUsingPaint( skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects); - run->fTypeface.reset(SkSafeRef(skPaint.getTypeface())); + run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint); run->fPathEffect = sk_ref_sp(effects.fPathEffect); run->fMaskFilter = sk_ref_sp(effects.fMaskFilter); return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc()); diff --git a/src/gpu/text/GrAtlasTextContext.cpp b/src/gpu/text/GrAtlasTextContext.cpp index 6ebd02cc15..ad708d7d7f 100644 --- a/src/gpu/text/GrAtlasTextContext.cpp +++ b/src/gpu/text/GrAtlasTextContext.cpp @@ -17,6 +17,7 @@ #include "SkGraphics.h" #include "SkMakeUnique.h" #include "SkMaskFilterBase.h" +#include "SkPaintPriv.h" #include "SkTextMapStateProc.h" #include "ops/GrMeshDrawOp.h" @@ -692,8 +693,9 @@ void GrAtlasTextContext::drawDFText(GrAtlasTextBlob* blob, int runIndex, // passed-in scaler context flags. (It's only used when we fall-back to bitmap text). SkScalerContext::CreateDescriptorAndEffectsUsingPaint( skPaint, &props, SkScalerContextFlags::kNone, nullptr, &desc, &effects); + auto typeface = SkPaintPriv::GetTypefaceOrDefault(skPaint); SkGlyphCache* origPaintCache = - SkGlyphCache::DetachCache(skPaint.getTypeface(), effects, desc.getDesc()); + SkGlyphCache::DetachCache(typeface, effects, desc.getDesc()); SkTArray positions; diff --git a/src/svg/SkSVGDevice.cpp b/src/svg/SkSVGDevice.cpp index 6bb971d764..7206709e5c 100644 --- a/src/svg/SkSVGDevice.cpp +++ b/src/svg/SkSVGDevice.cpp @@ -17,6 +17,7 @@ #include "SkDraw.h" #include "SkImageEncoder.h" #include "SkPaint.h" +#include "SkPaintPriv.h" #include "SkParsePath.h" #include "SkShader.h" #include "SkStream.h" @@ -544,7 +545,7 @@ void SkSVGDevice::AutoElement::addTextAttributes(const SkPaint& paint) { SkString familyName; SkTHashSet familySet; - sk_sp tface(paint.getTypeface() ? paint.refTypeface() : SkTypeface::MakeDefault()); + sk_sp tface = SkPaintPriv::RefTypefaceOrDefault(paint); SkASSERT(tface); SkFontStyle style = tface->fontStyle(); diff --git a/tests/FontHostStreamTest.cpp b/tests/FontHostStreamTest.cpp index e97ea0dcc2..3a5f52895f 100644 --- a/tests/FontHostStreamTest.cpp +++ b/tests/FontHostStreamTest.cpp @@ -11,6 +11,7 @@ #include "SkFontDescriptor.h" #include "SkGraphics.h" #include "SkPaint.h" +#include "SkPaintPriv.h" #include "SkPoint.h" #include "SkRect.h" #include "SkStream.h" @@ -84,8 +85,7 @@ DEF_TEST(FontHostStream, reporter) { drawBG(&origCanvas); origCanvas.drawString("A", point.fX, point.fY, paint); - sk_sp typeface(paint.getTypeface() ? paint.refTypeface() - : SkTypeface::MakeDefault()); + sk_sp typeface = SkPaintPriv::RefTypefaceOrDefault(paint); int ttcIndex; std::unique_ptr fontData(typeface->openStream(&ttcIndex)); if (!fontData) { -- cgit v1.2.3