From 4fa748d5801df66e46e6f4e98e07523d44d261a2 Mon Sep 17 00:00:00 2001 From: "djsollen@google.com" Date: Wed, 5 Jun 2013 14:20:25 +0000 Subject: [PATCH] Fix issues related to resolving fonts based on name. 1) non-system font files are not added to the cache. 2) We cache the default fonts for quick lookup. Review URL: https://codereview.chromium.org/16439004 git-svn-id: http://skia.googlecode.com/svn/trunk@9441 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkTypeface.h | 4 ++-- src/core/SkTypeface.cpp | 24 ++++++++++++++++-------- src/ports/SkFontHost_fontconfig.cpp | 1 - 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index a314e0fff..d34854d32 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -85,7 +85,7 @@ public: * Returns a ref() to the default typeface. The caller must call unref() * when they are done referencing the object. Never returns NULL. */ - static SkTypeface* RefDefault(); + static SkTypeface* RefDefault(Style style = SkTypeface::kNormal); /** Return a new reference to the typeface that most closely matches the requested familyName and style. Pass null as the familyName to return @@ -222,7 +222,7 @@ protected: void setIsFixedPitch(bool isFixedPitch) { fIsFixedPitch = isFixedPitch; } friend class SkScalerContext; - static SkTypeface* GetDefaultTypeface(); + static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal); virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const = 0; virtual void onFilterRec(SkScalerContextRec*) const = 0; diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp index cc60ca180..ba58c4c42 100644 --- a/src/core/SkTypeface.cpp +++ b/src/core/SkTypeface.cpp @@ -37,21 +37,26 @@ SkTypeface::~SkTypeface() { /////////////////////////////////////////////////////////////////////////////// -SkTypeface* SkTypeface::GetDefaultTypeface() { +SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { // we keep a reference to this guy for all time, since if we return its // fontID, the font cache may later on ask to resolve that back into a // typeface object. - static SkTypeface* gDefaultTypeface; + static const uint32_t FONT_STYLE_COUNT = 4; + static SkTypeface* gDefaultTypefaces[FONT_STYLE_COUNT]; + SkASSERT((unsigned)style < FONT_STYLE_COUNT); - if (NULL == gDefaultTypeface) { - gDefaultTypeface = - SkFontHost::CreateTypeface(NULL, NULL, SkTypeface::kNormal); + // mask off any other bits to avoid a crash in SK_RELEASE + style = (Style)(style & 0x03); + + if (NULL == gDefaultTypefaces[style]) { + gDefaultTypefaces[style] = + SkFontHost::CreateTypeface(NULL, NULL, style); } - return gDefaultTypeface; + return gDefaultTypefaces[style]; } -SkTypeface* SkTypeface::RefDefault() { - return SkRef(GetDefaultTypeface()); +SkTypeface* SkTypeface::RefDefault(Style style) { + return SkRef(GetDefaultTypeface(style)); } uint32_t SkTypeface::UniqueID(const SkTypeface* face) { @@ -68,6 +73,9 @@ bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { /////////////////////////////////////////////////////////////////////////////// SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { + if (NULL == name) { + return RefDefault(style); + } return SkFontHost::CreateTypeface(NULL, name, style); } diff --git a/src/ports/SkFontHost_fontconfig.cpp b/src/ports/SkFontHost_fontconfig.cpp index a5aef56f1..1cd2dd903 100644 --- a/src/ports/SkFontHost_fontconfig.cpp +++ b/src/ports/SkFontHost_fontconfig.cpp @@ -122,7 +122,6 @@ SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { // TODO should the caller give us the style? SkTypeface::Style style = SkTypeface::kNormal; SkTypeface* face = SkNEW_ARGS(FontConfigTypeface, (style, stream)); - SkTypefaceCache::Add(face, style); return face; }