Factory methods for heap-allocated SkTypeface objects.
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 7 Apr 2014 19:34:16 +0000 (19:34 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 7 Apr 2014 19:34:16 +0000 (19:34 +0000)
This is part of an effort to ensure that all SkPaint effects can only be
allocated on the heap.

This patch makes the constructors of SkTypeface and its subclasses non-public
and instead provides factory methods for creating these objects on the heap.

BUG=skia:2187
R=scroggo@google.com, bungeman@google.com

Author: dominikg@chromium.org

Review URL: https://codereview.chromium.org/227693003

git-svn-id: http://skia.googlecode.com/svn/trunk@14080 2bbb7eff-a529-9590-31e7-b0007b416f81

src/core/SkTypeface.cpp
src/fonts/SkFontMgr_fontconfig.cpp
src/ports/SkFontConfigTypeface.h
src/ports/SkFontHost_fontconfig.cpp

index d15caeb..cd3953b 100644 (file)
@@ -37,8 +37,12 @@ SkTypeface::~SkTypeface() {
 
 class SkEmptyTypeface : public SkTypeface {
 public:
-    SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { }
+    static SkEmptyTypeface* Create() {
+        return SkNEW(SkEmptyTypeface);
+    }
 protected:
+    SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { }
+
     virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { return NULL; }
     virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE {
         return NULL;
@@ -85,7 +89,8 @@ void SkTypeface::create_default_typeface(Style style) {
         gDefaultTypefaces[style] = SkFontHost::CreateTypeface(NULL, NULL, style);
     }
     if (NULL == gDefaultTypefaces[style]) {
-        gDefaultTypefaces[style] = SkNEW(SkEmptyTypeface);
+        // FIXME: Use a singleton for SkEmptyTypeface.
+        gDefaultTypefaces[style] = SkEmptyTypeface::Create();
     }
 }
 
index 1d17111..226da96 100644 (file)
@@ -311,7 +311,7 @@ protected:
             return NULL;
         }
 
-        SkTypeface* face = SkNEW_ARGS(FontConfigTypeface, (style, isFixedWidth, stream));
+        SkTypeface* face = FontConfigTypeface::Create(style, isFixedWidth, stream);
         return face;
     }
 
index dc92f1d..b6f8797 100644 (file)
@@ -18,19 +18,14 @@ class FontConfigTypeface : public SkTypeface_FreeType {
     SkStream* fLocalStream;
 
 public:
-    FontConfigTypeface(Style style,
-                       const SkFontConfigInterface::FontIdentity& fi,
-                       const SkString& familyName)
-            : INHERITED(style, SkTypefaceCache::NewFontID(), false)
-            , fIdentity(fi)
-            , fFamilyName(familyName)
-            , fLocalStream(NULL) {}
+    static FontConfigTypeface* Create(Style style,
+                                      const SkFontConfigInterface::FontIdentity& fi,
+                                      const SkString& familyName) {
+        return SkNEW_ARGS(FontConfigTypeface, (style, fi, familyName));
+    }
 
-    FontConfigTypeface(Style style, bool fixedWidth, SkStream* localStream)
-            : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) {
-        // we default to empty fFamilyName and fIdentity
-        fLocalStream = localStream;
-        SkSafeRef(localStream);
+    static FontConfigTypeface* Create(Style style, bool fixedWidth, SkStream* localStream) {
+        return SkNEW_ARGS(FontConfigTypeface, (style, fixedWidth, localStream));
     }
 
     virtual ~FontConfigTypeface() {
@@ -55,6 +50,21 @@ public:
 protected:
     friend class SkFontHost;    // hack until we can make public versions
 
+    FontConfigTypeface(Style style,
+                       const SkFontConfigInterface::FontIdentity& fi,
+                       const SkString& familyName)
+            : INHERITED(style, SkTypefaceCache::NewFontID(), false)
+            , fIdentity(fi)
+            , fFamilyName(familyName)
+            , fLocalStream(NULL) {}
+
+    FontConfigTypeface(Style style, bool fixedWidth, SkStream* localStream)
+            : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) {
+        // we default to empty fFamilyName and fIdentity
+        fLocalStream = localStream;
+        SkSafeRef(localStream);
+    }
+
     virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE;
     virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE;
 
index 3700ed1..07bfbd0 100644 (file)
@@ -118,7 +118,7 @@ SkTypeface* FontConfigTypeface::LegacyCreateTypeface(
         return face;
     }
 
-    face = SkNEW_ARGS(FontConfigTypeface, (outStyle, indentity, outFamilyName));
+    face = FontConfigTypeface::Create(outStyle, indentity, outFamilyName);
     SkTypefaceCache::Add(face, style);
 //    SkDebugf("add face <%s> <%s> %p [%d]\n", familyName, outFamilyName.c_str(), face, face->getRefCnt());
     return face;