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 <bungeman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
This commit is contained in:
Herb Derby 2018-03-05 14:36:45 -05:00 committed by Skia Commit-Bot
parent 7487ec86b9
commit eb3f674d6d
10 changed files with 34 additions and 33 deletions

View File

@ -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<SkAdvancedTypefaceMetrics> 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;

View File

@ -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);
}

View File

@ -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;

View File

@ -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<SkTypeface> RefTypefaceOrDefault(const SkPaint& paint) {
return paint.getTypeface() ? paint.refTypeface() : SkTypeface::MakeDefault();
}
};
#endif

View File

@ -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();

View File

@ -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())

View File

@ -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());

View File

@ -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<SkScalar> positions;

View File

@ -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<SkString> familySet;
sk_sp<SkTypeface> tface(paint.getTypeface() ? paint.refTypeface() : SkTypeface::MakeDefault());
sk_sp<SkTypeface> tface = SkPaintPriv::RefTypefaceOrDefault(paint);
SkASSERT(tface);
SkFontStyle style = tface->fontStyle();

View File

@ -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<SkTypeface> typeface(paint.getTypeface() ? paint.refTypeface()
: SkTypeface::MakeDefault());
sk_sp<SkTypeface> typeface = SkPaintPriv::RefTypefaceOrDefault(paint);
int ttcIndex;
std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
if (!fontData) {