Replace SkTypeface::Style with SkFontStyle.
authorbungeman <bungeman@google.com>
Mon, 20 Oct 2014 19:33:59 +0000 (12:33 -0700)
committerCommit bot <commit-bot@chromium.org>
Mon, 20 Oct 2014 19:33:59 +0000 (12:33 -0700)
Review URL: https://codereview.chromium.org/488143002

25 files changed:
gyp/core.gypi
gyp/ports.gyp
include/core/SkFontStyle.h [new file with mode: 0644]
include/core/SkTypeface.h
include/ports/SkFontStyle.h [deleted file]
src/core/SkFontHost.cpp
src/core/SkTypeface.cpp
src/core/SkTypefaceCache.cpp
src/core/SkTypefaceCache.h
src/fonts/SkFontMgr_fontconfig.cpp
src/fonts/SkGScalerContext.cpp
src/fonts/SkTestScalerContext.cpp
src/fonts/SkTestScalerContext.h
src/ports/SkFontConfigTypeface.h
src/ports/SkFontHost_FreeType.cpp
src/ports/SkFontHost_FreeType_common.h
src/ports/SkFontHost_fontconfig.cpp
src/ports/SkFontHost_linux.cpp
src/ports/SkFontHost_mac.cpp
src/ports/SkFontHost_win.cpp
src/ports/SkFontMgr_android.cpp
src/ports/SkFontMgr_fontconfig.cpp
src/ports/SkFontMgr_win_dw.cpp
src/ports/SkTypeface_win_dw.h
tools/sk_tool_utils_font.cpp

index 5e811163fe60357661f2e542eb5415338a195282..4f9379f42b03eafeea37f1aee5693c17ff7dc871 100644 (file)
         '<(skia_include_path)/core/SkFloatBits.h',
         '<(skia_include_path)/core/SkFloatingPoint.h',
         '<(skia_include_path)/core/SkFontHost.h',
+        '<(skia_include_path)/core/SkFontStyle.h',
         '<(skia_include_path)/core/SkGraphics.h',
         '<(skia_include_path)/core/SkImage.h',
         '<(skia_include_path)/core/SkImageDecoder.h',
index 268f6fd4d9c69230f496d5c6b9c01442694e3c41..78d59dfe033a00dd1926538f114a9e305ac5a741 100644 (file)
@@ -60,7 +60,6 @@
         '../include/ports/SkFontConfigInterface.h',
         '../include/ports/SkFontMgr.h',
         '../include/ports/SkFontMgr_indirect.h',
-        '../include/ports/SkFontStyle.h',
         '../include/ports/SkRemotableFontMgr.h',
       ],
       'conditions': [
diff --git a/include/core/SkFontStyle.h b/include/core/SkFontStyle.h
new file mode 100644 (file)
index 0000000..f42d7dd
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkFontStyle_DEFINED
+#define SkFontStyle_DEFINED
+
+#include "SkTypes.h"
+
+class SK_API SkFontStyle {
+public:
+    enum Weight {
+        kThin_Weight        = 100,
+        kExtraLight_Weight  = 200,
+        kLight_Weight       = 300,
+        kNormal_Weight      = 400,
+        kMedium_Weight      = 500,
+        kSemiBold_Weight    = 600,
+        kBold_Weight        = 700,
+        kExtraBold_Weight   = 800,
+        kBlack_Weight       = 900
+    };
+
+    enum Width {
+        kUltraCondensed_Width   = 1,
+        kExtraCondensed_Width   = 2,
+        kCondensed_Width        = 3,
+        kSemiCondensed_Width    = 4,
+        kNormal_Width           = 5,
+        kSemiExpanded_Width     = 6,
+        kExpanded_Width         = 7,
+        kExtraExpanded_Width    = 8,
+        kUltaExpanded_Width     = 9
+    };
+
+    enum Slant {
+        kUpright_Slant,
+        kItalic_Slant,
+    };
+
+    SkFontStyle();
+    SkFontStyle(int weight, int width, Slant);
+    /** oldStyle means the style-bits in SkTypeface::Style: bold=1, italic=2 */
+    explicit SkFontStyle(unsigned oldStyle);
+
+    bool operator==(const SkFontStyle& rhs) const {
+        return fUnion.fU32 == rhs.fUnion.fU32;
+    }
+
+    int weight() const { return fUnion.fR.fWeight; }
+    int width() const { return fUnion.fR.fWidth; }
+    Slant slant() const { return (Slant)fUnion.fR.fSlant; }
+
+    bool isItalic() const {
+        return kItalic_Slant == fUnion.fR.fSlant;
+    }
+
+private:
+    union {
+        struct {
+            uint16_t fWeight;   // 100 .. 900
+            uint8_t  fWidth;    // 1 .. 9
+            uint8_t  fSlant;    // 0 .. 2
+        } fR;
+        uint32_t    fU32;
+    } fUnion;
+};
+
+#endif
index a080d84bd8c87ab9538a2c2c932fa8a6b4c171a0..f7af5d2b61cd9d18eea19d4710f9198f1eebb2b9 100644 (file)
@@ -11,6 +11,7 @@
 #define SkTypeface_DEFINED
 
 #include "SkAdvancedTypefaceMetrics.h"
+#include "SkFontStyle.h"
 #include "SkWeakRefCnt.h"
 
 class SkDescriptor;
@@ -49,17 +50,25 @@ public:
         kBoldItalic = 0x03
     };
 
-    /** Returns the typeface's intrinsic style attributes
-    */
-    Style style() const { return fStyle; }
+    /** Returns the typeface's intrinsic style attributes. */
+    SkFontStyle fontStyle() const {
+        return fStyle;
+    }
 
-    /** Returns true if getStyle() has the kBold bit set.
-    */
-    bool isBold() const { return (fStyle & kBold) != 0; }
+    /** Returns the typeface's intrinsic style attributes. 
+     *  @deprecated use fontStyle() instead.
+     */
+    Style style() const {
+        return static_cast<Style>(
+            (fStyle.weight() >= SkFontStyle::kSemiBold_Weight ? kBold : kNormal) |
+            (fStyle.slant()  != SkFontStyle::kUpright_Slant ? kItalic : kNormal));
+    }
 
-    /** Returns true if getStyle() has the kItalic bit set.
-    */
-    bool isItalic() const { return (fStyle & kItalic) != 0; }
+    /** Returns true if style() has the kBold bit set. */
+    bool isBold() const { return fStyle.weight() >= SkFontStyle::kSemiBold_Weight; }
+
+    /** Returns true if style() has the kItalic bit set. */
+    bool isItalic() const { return fStyle.slant() != SkFontStyle::kUpright_Slant; }
 
     /** Returns true if the typeface claims to be fixed-pitch.
      *  This is a style bit, advance widths may vary even if this returns true.
@@ -285,7 +294,7 @@ public:
 protected:
     /** uniqueID must be unique and non-zero
     */
-    SkTypeface(Style style, SkFontID uniqueID, bool isFixedPitch = false);
+    SkTypeface(const SkFontStyle& style, SkFontID uniqueID, bool isFixedPitch = false);
     virtual ~SkTypeface();
 
     /** Sets the fixedPitch bit. If used, must be called in the constructor. */
@@ -351,7 +360,7 @@ private:
     static void        DeleteDefault(SkTypeface*);
 
     SkFontID    fUniqueID;
-    Style       fStyle;
+    SkFontStyle fStyle;
     bool        fIsFixedPitch;
 
     friend class SkPaint;
diff --git a/include/ports/SkFontStyle.h b/include/ports/SkFontStyle.h
deleted file mode 100644 (file)
index 9d9a912..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright 2013 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkFontStyle_DEFINED
-#define SkFontStyle_DEFINED
-
-#include "SkTypes.h"
-
-class SK_API SkFontStyle {
-public:
-    enum Weight {
-        kThin_Weight        = 100,
-        kExtraLight_Weight  = 200,
-        kLight_Weight       = 300,
-        kNormal_Weight      = 400,
-        kMedium_Weight      = 500,
-        kSemiBold_Weight    = 600,
-        kBold_Weight        = 700,
-        kExtraBold_Weight   = 800,
-        kBlack_Weight       = 900
-    };
-
-    enum Width {
-        kUltraCondensed_Width   = 1,
-        kExtraCondensed_Width   = 2,
-        kCondensed_Width        = 3,
-        kSemiCondensed_Width    = 4,
-        kNormal_Width           = 5,
-        kSemiExpanded_Width     = 6,
-        kExpanded_Width         = 7,
-        kExtraExpanded_Width    = 8,
-        kUltaExpanded_Width     = 9
-    };
-
-    enum Slant {
-        kUpright_Slant,
-        kItalic_Slant,
-    };
-
-    SkFontStyle();
-    SkFontStyle(int weight, int width, Slant);
-
-    bool operator==(const SkFontStyle& rhs) const {
-        return fUnion.fU32 == rhs.fUnion.fU32;
-    }
-
-    int weight() const { return fUnion.fR.fWeight; }
-    int width() const { return fUnion.fR.fWidth; }
-    Slant slant() const { return (Slant)fUnion.fR.fSlant; }
-
-    bool isItalic() const {
-        return kItalic_Slant == fUnion.fR.fSlant;
-    }
-
-private:
-    union {
-        struct {
-            uint16_t fWeight;   // 100 .. 900
-            uint8_t  fWidth;    // 1 .. 9
-            uint8_t  fSlant;    // 0 .. 2
-        } fR;
-        uint32_t    fU32;
-    } fUnion;
-};
-
-#endif
index ce73491c7271e4fdc40a510ecf1f0671a7156eee..77b80e88219e8a26efff200a03101ed18dc126ce 100644 (file)
@@ -67,6 +67,15 @@ SkFontStyle::SkFontStyle(int weight, int width, Slant slant) {
     fUnion.fR.fSlant = SkPin32(slant, kUpright_Slant, kItalic_Slant);
 }
 
+SkFontStyle::SkFontStyle(unsigned oldStyle) {
+    fUnion.fU32 = 0;
+    fUnion.fR.fWeight = (oldStyle & SkTypeface::kBold) ? SkFontStyle::kBold_Weight
+                                                       : SkFontStyle::kNormal_Weight;
+    fUnion.fR.fWidth = SkFontStyle::kNormal_Width;
+    fUnion.fR.fSlant = (oldStyle & SkTypeface::kItalic) ? SkFontStyle::kItalic_Slant
+                                                        : SkFontStyle::kUpright_Slant;
+}
+
 #include "SkFontMgr.h"
 
 class SkEmptyFontStyleSet : public SkFontStyleSet {
index 81038bc986e510e35922c21625b519839ae84dc0..84cbdbfe6643b034374f97231604b1ce5b8c82a2 100644 (file)
 #include "SkStream.h"
 #include "SkTypeface.h"
 
-//#define TRACE_LIFECYCLE
-
-#ifdef TRACE_LIFECYCLE
-    static int32_t gTypefaceCounter;
-#endif
-
-SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedPitch)
-    : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) {
-#ifdef TRACE_LIFECYCLE
-    SkDebugf("SkTypeface: create  %p fontID %d total %d\n",
-             this, fontID, ++gTypefaceCounter);
-#endif
-}
+SkTypeface::SkTypeface(const SkFontStyle& style, SkFontID fontID, bool isFixedPitch)
+    : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) { }
 
-SkTypeface::~SkTypeface() {
-#ifdef TRACE_LIFECYCLE
-    SkDebugf("SkTypeface: destroy %p fontID %d total %d\n",
-             this, fUniqueID, --gTypefaceCounter);
-#endif
-}
+SkTypeface::~SkTypeface() { }
 
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -43,7 +27,7 @@ public:
         return SkNEW(SkEmptyTypeface);
     }
 protected:
-    SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { }
+    SkEmptyTypeface() : SkTypeface(SkFontStyle(), 0, true) { }
 
     virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { return NULL; }
     virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE {
index f864e1c9dc5105b4961eacd0253c48c7a18a67d8..cfa301ef39def299aa2abb801cec433daa3f2307 100644 (file)
@@ -29,7 +29,7 @@ SkTypefaceCache::~SkTypefaceCache() {
 }
 
 void SkTypefaceCache::add(SkTypeface* face,
-                          SkTypeface::Style requestedStyle,
+                          const SkFontStyle& requestedStyle,
                           bool strong) {
     if (fArray.count() >= TYPEFACE_CACHE_LIMIT) {
         this->purge(TYPEFACE_CACHE_LIMIT >> 2);
@@ -120,7 +120,7 @@ SkFontID SkTypefaceCache::NewFontID() {
 SK_DECLARE_STATIC_MUTEX(gMutex);
 
 void SkTypefaceCache::Add(SkTypeface* face,
-                          SkTypeface::Style requestedStyle,
+                          const SkFontStyle& requestedStyle,
                           bool strong) {
     SkAutoMutexAcquire ama(gMutex);
     Get().add(face, requestedStyle, strong);
@@ -145,9 +145,9 @@ void SkTypefaceCache::PurgeAll() {
 ///////////////////////////////////////////////////////////////////////////////
 
 #ifdef SK_DEBUG
-static bool DumpProc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
-    SkDebugf("SkTypefaceCache: face %p fontID %d style %d refcnt %d\n",
-             face, face->uniqueID(), style, face->getRefCnt());
+static bool DumpProc(SkTypeface* face, const SkFontStyle& s, void* ctx) {
+    SkDebugf("SkTypefaceCache: face %p fontID %d weight %d width %d style %d refcnt %d\n",
+             face, face->uniqueID(), s.weight(), s.width(), s.slant(), face->getRefCnt());
     return false;
 }
 #endif
index ae37ab76ec4ba6cfd4984087606eb78f40030f08..ba851ee0b6a4b2dc45c7fca456e56c0a862796ce 100644 (file)
@@ -31,7 +31,7 @@ public:
      * for the given context. The passed typeface is owned by the cache and is
      * not additionally ref()ed. The typeface may be in the disposed state.
      */
-    typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context);
+    typedef bool(*FindProc)(SkTypeface*, const SkFontStyle&, void* context);
 
     /**
      *  Add a typeface to the cache. This ref()s the typeface, so that the
@@ -39,7 +39,7 @@ public:
      *  whose refcnt is 1 (meaning only the cache is an owner) will be
      *  unref()ed.
      */
-    void add(SkTypeface*, SkTypeface::Style requested, bool strong = true);
+    void add(SkTypeface*, const SkFontStyle& requested, bool strong = true);
 
     /**
      *  Search the cache for a typeface with the specified fontID (uniqueID).
@@ -73,7 +73,7 @@ public:
     // These are static wrappers around a global instance of a cache.
 
     static void Add(SkTypeface*,
-                    SkTypeface::Style requested,
+                    const SkFontStyle& requested,
                     bool strong = true);
     static SkTypeface* FindByID(SkFontID fontID);
     static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx);
@@ -90,9 +90,9 @@ private:
     void purge(int count);
 
     struct Rec {
-        SkTypeface*         fFace;
-        bool                fStrong;
-        SkTypeface::Style   fRequestedStyle;
+        SkTypeface* fFace;
+        bool fStrong;
+        SkFontStyle fRequestedStyle;
     };
     SkTDArray<Rec> fArray;
 };
index a7f81281c32fffc53f49c52e9a794558690241b7..c56e065509fffacd0359cdaa534e27b0fa320c5b 100644 (file)
@@ -300,7 +300,7 @@ protected:
         }
 
         // TODO should the caller give us the style or should we get it from freetype?
-        SkTypeface::Style style = SkTypeface::kNormal;
+        SkFontStyle style;
         bool isFixedWidth = false;
         if (!SkTypeface_FreeType::ScanFont(stream, 0, NULL, &style, &isFixedWidth)) {
             return NULL;
index 34a788a9c99b6f9dffa2e40937cc3d89eb5aa5b0..cacc27064f93ebec17cdebaa476bfc8c00bf03cb 100644 (file)
@@ -158,7 +158,7 @@ void SkGScalerContext::generateFontMetrics(SkPaint::FontMetrics* metrics) {
 #include "SkTypefaceCache.h"
 
 SkGTypeface::SkGTypeface(SkTypeface* proxy, const SkPaint& paint)
-    : SkTypeface(proxy->style(), SkTypefaceCache::NewFontID(), false)
+    : SkTypeface(proxy->fontStyle(), SkTypefaceCache::NewFontID(), false)
     , fProxy(SkRef(proxy))
     , fPaint(paint) {}
 
index ee379f0a3852afe6463980369bccff082100240a..b148375b0944806888de2290f997e3da45ff47bc 100644 (file)
@@ -110,8 +110,8 @@ void SkTestFont::init(const SkScalar* pts, const unsigned char* verbs) {
         fPaths[index] = path;
     }
 }
-    
-SkTestTypeface::SkTestTypeface(SkTestFont* testFont, SkTypeface::Style style)
+
+SkTestTypeface::SkTestTypeface(SkTestFont* testFont, const SkFontStyle& style)
     : SkTypeface(style, SkTypefaceCache::NewFontID(), false)
     , fTestFont(testFont) {
 }
index 42f604952f2f3f9c0d7e8369911a6463eae6bb51..3e6dc97ed149f17bc3694cdc95ef01105844cd2d 100644 (file)
@@ -57,7 +57,7 @@ private:
 
 class SkTestTypeface : public SkTypeface {
 public:
-    SkTestTypeface(SkTestFont* , SkTypeface::Style style);
+    SkTestTypeface(SkTestFont*, const SkFontStyle& style);
     virtual ~SkTestTypeface() {
         SkSafeUnref(fTestFont);
     }
index f62d99d2f45acf5ce85e7b591f6a3ed01cd7091d..c7d8e26032b3b6baac0743edb283efc172f545ff 100644 (file)
@@ -18,13 +18,14 @@ class FontConfigTypeface : public SkTypeface_FreeType {
     SkStream* fLocalStream;
 
 public:
-    static FontConfigTypeface* Create(Style style,
+    static FontConfigTypeface* Create(const SkFontStyle& style,
                                       const SkFontConfigInterface::FontIdentity& fi,
                                       const SkString& familyName) {
         return SkNEW_ARGS(FontConfigTypeface, (style, fi, familyName));
     }
 
-    static FontConfigTypeface* Create(Style style, bool fixedWidth, SkStream* localStream) {
+    static FontConfigTypeface* Create(const SkFontStyle& style, bool fixedWidth,
+                                      SkStream* localStream) {
         return SkNEW_ARGS(FontConfigTypeface, (style, fixedWidth, localStream));
     }
 
@@ -50,7 +51,7 @@ public:
 protected:
     friend class SkFontHost;    // hack until we can make public versions
 
-    FontConfigTypeface(Style style,
+    FontConfigTypeface(const SkFontStyle& style,
                        const SkFontConfigInterface::FontIdentity& fi,
                        const SkString& familyName)
             : INHERITED(style, SkTypefaceCache::NewFontID(), false)
@@ -58,7 +59,7 @@ protected:
             , fFamilyName(familyName)
             , fLocalStream(NULL) {}
 
-    FontConfigTypeface(Style style, bool fixedWidth, SkStream* localStream)
+    FontConfigTypeface(const SkFontStyle& style, bool fixedWidth, SkStream* localStream)
             : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) {
         // we default to empty fFamilyName and fIdentity
         fLocalStream = localStream;
index 85f8ab94693ba3a7f0eadef6faf0184b9a3ab3b7..f3a87e52aae82fc3efb1a1122b6662f5b2cd5805 100644 (file)
@@ -1669,8 +1669,9 @@ size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
+#include "SkTSearch.h"
 /*static*/ bool SkTypeface_FreeType::ScanFont(
-    SkStream* stream, int ttcIndex, SkString* name, SkTypeface::Style* style, bool* isFixedPitch)
+    SkStream* stream, int ttcIndex, SkString* name, SkFontStyle* style, bool* isFixedPitch)
 {
     FT_Library  library;
     if (FT_Init_FreeType(&library)) {
@@ -1704,19 +1705,61 @@ size_t SkTypeface_FreeType::onGetTableData(SkFontTableTag tag, size_t offset,
         return false;
     }
 
-    int tempStyle = SkTypeface::kNormal;
+    int weight = SkFontStyle::kNormal_Weight;
+    int width = SkFontStyle::kNormal_Width;
+    SkFontStyle::Slant slant = SkFontStyle::kUpright_Slant;
     if (face->style_flags & FT_STYLE_FLAG_BOLD) {
-        tempStyle |= SkTypeface::kBold;
+        weight = SkFontStyle::kBold_Weight;
     }
     if (face->style_flags & FT_STYLE_FLAG_ITALIC) {
-        tempStyle |= SkTypeface::kItalic;
+        slant = SkFontStyle::kItalic_Slant;
+    }
+
+    PS_FontInfoRec psFontInfo;
+    TT_OS2* os2 = static_cast<TT_OS2*>(FT_Get_Sfnt_Table(face, ft_sfnt_os2));
+    if (os2 && os2->version != 0xffff) {
+        weight = os2->usWeightClass;
+        width = os2->usWidthClass;
+    } else if (0 == FT_Get_PS_Font_Info(face, &psFontInfo) && psFontInfo.weight) {
+        static const struct {
+            char const * const name;
+            int const weight;
+        } commonWeights [] = {
+            // There are probably more common names, but these are known to exist.
+            { "black", SkFontStyle::kBlack_Weight },
+            { "bold", SkFontStyle::kBold_Weight },
+            { "book", (SkFontStyle::kNormal_Weight + SkFontStyle::kLight_Weight)/2 },
+            { "demi", SkFontStyle::kSemiBold_Weight },
+            { "demibold", SkFontStyle::kSemiBold_Weight },
+            { "extrabold", SkFontStyle::kExtraBold_Weight },
+            { "extralight", SkFontStyle::kExtraLight_Weight },
+            { "heavy", SkFontStyle::kBlack_Weight },
+            { "light", SkFontStyle::kLight_Weight },
+            { "medium", SkFontStyle::kMedium_Weight },
+            { "normal", SkFontStyle::kNormal_Weight },
+            { "regular", SkFontStyle::kNormal_Weight },
+            { "semibold", SkFontStyle::kSemiBold_Weight },
+            { "thin", SkFontStyle::kThin_Weight },
+            { "ultra", SkFontStyle::kExtraBold_Weight },
+            { "ultrablack", 1000 },
+            { "ultrabold", SkFontStyle::kExtraBold_Weight },
+            { "ultraheavy", 1000 },
+            { "ultralight", SkFontStyle::kExtraLight_Weight },
+        };
+        int const index = SkStrLCSearch(&commonWeights[0].name, SK_ARRAY_COUNT(commonWeights),
+                                        psFontInfo.weight, sizeof(commonWeights));
+        if (index >= 0) {
+            weight = commonWeights[index].weight;
+        } else {
+            SkDEBUGF(("Do not know weight for: %s\n", psFontInfo.weight));
+        }
     }
 
     if (name) {
         name->set(face->family_name);
     }
     if (style) {
-        *style = (SkTypeface::Style) tempStyle;
+        *style = SkFontStyle(weight, width, slant);
     }
     if (isFixedPitch) {
         *isFixedPitch = FT_IS_FIXED_WIDTH(face);
index f093dba8a57532c5823731e6463c986f2214ba59..79e6d651631103642729c4788fc55f8b3bf2ec5b 100644 (file)
@@ -49,10 +49,10 @@ public:
      *  name and style from a stream, using FreeType's API.
      */
     static bool ScanFont(SkStream* stream, int ttcIndex,
-                         SkString* name, SkTypeface::Style* style, bool* isFixedPitch);
+                         SkString* name, SkFontStyle* style, bool* isFixedPitch);
 
 protected:
-    SkTypeface_FreeType(Style style, SkFontID uniqueID, bool isFixedPitch)
+    SkTypeface_FreeType(const SkFontStyle& style, SkFontID uniqueID, bool isFixedPitch)
         : INHERITED(style, uniqueID, isFixedPitch)
         , fGlyphCount(-1)
     {}
index b3a893e53687a1c8597a542406414b2c1131ef2a..32e9f80560cefe0ad6c31cee45ae44a3184b564a 100644 (file)
@@ -60,19 +60,20 @@ SkFontConfigInterface* SkFontHost_fontconfig_ref_global() {
 ///////////////////////////////////////////////////////////////////////////////
 
 struct FindRec {
-    FindRec(const char* name, SkTypeface::Style style)
+    FindRec(const char* name, const SkFontStyle& style)
         : fFamilyName(name)  // don't need to make a deep copy
         , fStyle(style) {}
 
     const char* fFamilyName;
-    SkTypeface::Style fStyle;
+    SkFontStyle fStyle;
 };
 
-static bool find_proc(SkTypeface* face, SkTypeface::Style style, void* ctx) {
-    FontConfigTypeface* fci = (FontConfigTypeface*)face;
-    const FindRec* rec = (const FindRec*)ctx;
+static bool find_proc(SkTypeface* cachedTypeface, const SkFontStyle& cachedStyle, void* ctx) {
+    FontConfigTypeface* cachedFCTypeface = (FontConfigTypeface*)cachedTypeface;
+    const FindRec* rec = static_cast<const FindRec*>(ctx);
 
-    return rec->fStyle == style && fci->isFamilyName(rec->fFamilyName);
+    return rec->fStyle == cachedStyle &&
+           cachedFCTypeface->isFamilyName(rec->fFamilyName);
 }
 
 SkTypeface* FontConfigTypeface::LegacyCreateTypeface(
@@ -89,34 +90,37 @@ SkTypeface* FontConfigTypeface::LegacyCreateTypeface(
         familyName = fct->getFamilyName();
     }
 
-    FindRec rec(familyName, style);
+    SkFontStyle requestedStyle(style);
+    FindRec rec(familyName, requestedStyle);
     SkTypeface* face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
     if (face) {
-//        SkDebugf("found cached face <%s> <%s> %p [%d]\n", familyName, ((FontConfigTypeface*)face)->getFamilyName(), face, face->getRefCnt());
+        //SkDebugf("found cached face <%s> <%s> %p [%d]\n",
+        //         familyName, ((FontConfigTypeface*)face)->getFamilyName(),
+        //         face, face->getRefCnt());
         return face;
     }
 
     SkFontConfigInterface::FontIdentity indentity;
-    SkString                            outFamilyName;
-    SkTypeface::Style                   outStyle;
-
-    if (!fci->matchFamilyName(familyName, style,
-                              &indentity, &outFamilyName, &outStyle)) {
+    SkString outFamilyName;
+    SkTypeface::Style outStyle;
+    if (!fci->matchFamilyName(familyName, style, &indentity, &outFamilyName, &outStyle)) {
         return NULL;
     }
 
     // check if we, in fact, already have this. perhaps fontconfig aliased the
     // requested name to some other name we actually have...
     rec.fFamilyName = outFamilyName.c_str();
-    rec.fStyle = outStyle;
+    rec.fStyle = SkFontStyle(outStyle);
     face = SkTypefaceCache::FindByProcAndRef(find_proc, &rec);
     if (face) {
         return face;
     }
 
-    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());
+    face = FontConfigTypeface::Create(SkFontStyle(outStyle), indentity, outFamilyName);
+    SkTypefaceCache::Add(face, requestedStyle);
+    //SkDebugf("add face <%s> <%s> %p [%d]\n",
+    //         familyName, outFamilyName.c_str(),
+    //         face, face->getRefCnt());
     return face;
 }
 
index a4202aabd21d9ce4b728e6719451f51bb7130c90..90141f9374eb563ecd59b8c94ad0360f22eb2541 100644 (file)
@@ -30,7 +30,7 @@
 /** The base SkTypeface implementation for the custom font manager. */
 class SkTypeface_Custom : public SkTypeface_FreeType {
 public:
-    SkTypeface_Custom(Style style, bool isFixedPitch,
+    SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
                       bool sysFont, const SkString familyName, int index)
         : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
         , fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
@@ -67,7 +67,7 @@ private:
  */
 class SkTypeface_Empty : public SkTypeface_Custom {
 public:
-    SkTypeface_Empty() : INHERITED(SkTypeface::kNormal, false, true, SkString(), 0) {}
+    SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}
 
     virtual const char* getUniqueString() const SK_OVERRIDE { return NULL; }
 
@@ -81,9 +81,9 @@ private:
 /** The stream SkTypeface implementation for the custom font manager. */
 class SkTypeface_Stream : public SkTypeface_Custom {
 public:
-    SkTypeface_Stream(Style style, bool isFixedPitch, bool sysFont, const SkString familyName,
-                      SkStream* stream, int ttcIndex)
-        : INHERITED(style, isFixedPitch, sysFont, familyName, ttcIndex)
+    SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
+                      const SkString familyName, SkStream* stream, int index)
+        : INHERITED(style, isFixedPitch, sysFont, familyName, index)
         , fStream(SkRef(stream))
     { }
 
@@ -104,8 +104,8 @@ private:
 /** The file SkTypeface implementation for the custom font manager. */
 class SkTypeface_File : public SkTypeface_Custom {
 public:
-    SkTypeface_File(Style style, bool isFixedPitch, bool sysFont, const SkString familyName,
-                    const char path[], int index)
+    SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
+                    const SkString familyName, const char path[], int index)
         : INHERITED(style, isFixedPitch, sysFont, familyName, index)
         , fPath(path)
     { }
@@ -273,7 +273,7 @@ protected:
         }
 
         bool isFixedPitch;
-        SkTypeface::Style style;
+        SkFontStyle style;
         SkString name;
         if (SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
             return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
@@ -315,7 +315,7 @@ protected:
 private:
 
     static bool get_name_and_style(const char path[], SkString* name,
-                                   SkTypeface::Style* style, bool* isFixedPitch) {
+                                   SkFontStyle* style, bool* isFixedPitch) {
         SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
         if (stream.get()) {
             return SkTypeface_FreeType::ScanFont(stream, 0, name, style, isFixedPitch);
@@ -335,8 +335,7 @@ private:
 
             bool isFixedPitch;
             SkString realname;
-            SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
-
+            SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
             if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedPitch)) {
                 SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
                 continue;
index aae7464e2516e6d8608ab0ea1ceb18aee98371f5..4e573541ca2780b2a9383a9e471a1cb294317e58 100755 (executable)
@@ -6,7 +6,6 @@
  * found in the LICENSE file.
  */
 
-#include <vector>
 #ifdef SK_BUILD_FOR_MAC
 #import <ApplicationServices/ApplicationServices.h>
 #endif
@@ -352,20 +351,70 @@ Offscreen::Offscreen() : fRGBSpace(NULL), fCG(NULL),
 
 ///////////////////////////////////////////////////////////////////////////////
 
-static SkTypeface::Style computeStyleBits(CTFontRef font, bool* isFixedPitch) {
-    unsigned style = SkTypeface::kNormal;
-    CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(font);
+static bool find_dict_traits(CFDictionaryRef dict, CTFontSymbolicTraits* traits) {
+    CFNumberRef num;
+    return CFDictionaryGetValueIfPresent(dict, kCTFontSymbolicTrait, (const void**)&num)
+    && CFNumberIsFloatType(num)
+    && CFNumberGetValue(num, kCFNumberSInt32Type, traits);
+}
+
+static bool find_dict_float(CFDictionaryRef dict, CFStringRef name, float* value) {
+    CFNumberRef num;
+    return CFDictionaryGetValueIfPresent(dict, name, (const void**)&num)
+    && CFNumberIsFloatType(num)
+    && CFNumberGetValue(num, kCFNumberFloatType, value);
+}
+
+static int unit_weight_to_fontstyle(float unit) {
+    float value;
+    if (unit < 0) {
+        value = 100 + (1 + unit) * 300;
+    } else {
+        value = 400 + unit * 500;
+    }
+    return sk_float_round2int(value);
+}
 
-    if (traits & kCTFontBoldTrait) {
-        style |= SkTypeface::kBold;
+static int unit_width_to_fontstyle(float unit) {
+    float value;
+    if (unit < 0) {
+        value = 1 + (1 + unit) * 4;
+    } else {
+        value = 5 + unit * 4;
     }
-    if (traits & kCTFontItalicTrait) {
-        style |= SkTypeface::kItalic;
+    return sk_float_round2int(value);
+}
+
+static SkFontStyle fontstyle_from_descriptor(CTFontDescriptorRef desc, bool* isFixedPitch) {
+    AutoCFRelease<CFDictionaryRef> dict(
+            (CFDictionaryRef)CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute));
+    if (NULL == dict.get()) {
+        return SkFontStyle();
+    }
+
+    CTFontSymbolicTraits traits;
+    if (!find_dict_traits(dict, &traits)) {
+        traits = 0;
     }
     if (isFixedPitch) {
-        *isFixedPitch = (traits & kCTFontMonoSpaceTrait) != 0;
+        *isFixedPitch = SkToBool(traits & kCTFontMonoSpaceTrait);
     }
-    return (SkTypeface::Style)style;
+
+    float weight, width, slant;
+    if (!find_dict_float(dict, kCTFontWeightTrait, &weight)) {
+        weight = (traits & kCTFontBoldTrait) ? 0.5f : 0;
+    }
+    if (!find_dict_float(dict, kCTFontWidthTrait, &width)) {
+        width = 0;
+    }
+    if (!find_dict_float(dict, kCTFontSlantTrait, &slant)) {
+        slant = (traits & kCTFontItalicTrait) ? 0.5f : 0;
+    }
+
+    return SkFontStyle(unit_weight_to_fontstyle(weight),
+                       unit_width_to_fontstyle(width),
+                       slant ? SkFontStyle::kItalic_Slant
+                       : SkFontStyle::kUpright_Slant);
 }
 
 static SkFontID CTFontRef_to_SkFontID(CTFontRef fontRef) {
@@ -396,48 +445,15 @@ static SkFontID CTFontRef_to_SkFontID(CTFontRef fontRef) {
     return id;
 }
 
-static SkFontStyle stylebits2fontstyle(SkTypeface::Style styleBits) {
-    return SkFontStyle((styleBits & SkTypeface::kBold)
-                           ? SkFontStyle::kBold_Weight
-                           : SkFontStyle::kNormal_Weight,
-                       SkFontStyle::kNormal_Width,
-                       (styleBits & SkTypeface::kItalic)
-                           ? SkFontStyle::kItalic_Slant
-                           : SkFontStyle::kUpright_Slant);
-}
-
 #define WEIGHT_THRESHOLD    ((SkFontStyle::kNormal_Weight + SkFontStyle::kBold_Weight)/2)
 
-static SkTypeface::Style fontstyle2stylebits(const SkFontStyle& fs) {
-    unsigned style = 0;
-    if (fs.width() >= WEIGHT_THRESHOLD) {
-        style |= SkTypeface::kBold;
-    }
-    if (fs.isItalic()) {
-        style |= SkTypeface::kItalic;
-    }
-    return (SkTypeface::Style)style;
-}
-
 class SkTypeface_Mac : public SkTypeface {
 public:
-    SkTypeface_Mac(SkTypeface::Style style, SkFontID fontID, bool isFixedPitch,
-                   CTFontRef fontRef, const char name[], bool isLocalStream)
-        : SkTypeface(style, fontID, isFixedPitch)
-        , fName(name)
-        , fFontRef(fontRef) // caller has already called CFRetain for us
-        , fFontStyle(stylebits2fontstyle(style))
-        , fIsLocalStream(isLocalStream)
-    {
-        SkASSERT(fontRef);
-    }
-
     SkTypeface_Mac(const SkFontStyle& fs, SkFontID fontID, bool isFixedPitch,
                    CTFontRef fontRef, const char name[], bool isLocalStream)
-        : SkTypeface(fontstyle2stylebits(fs), fontID, isFixedPitch)
+        : SkTypeface(fs, fontID, isFixedPitch)
         , fName(name)
         , fFontRef(fontRef) // caller has already called CFRetain for us
-        , fFontStyle(fs)
         , fIsLocalStream(isLocalStream)
     {
         SkASSERT(fontRef);
@@ -445,7 +461,6 @@ public:
 
     SkString fName;
     AutoCFRelease<CTFontRef> fFontRef;
-    SkFontStyle fFontStyle;
 
 protected:
     friend class SkFontHost;    // to access our protected members for deprecated methods
@@ -476,23 +491,26 @@ private:
 static SkTypeface* NewFromFontRef(CTFontRef fontRef, const char name[], bool isLocalStream) {
     SkASSERT(fontRef);
     bool isFixedPitch;
-    SkTypeface::Style style = computeStyleBits(fontRef, &isFixedPitch);
+    AutoCFRelease<CTFontDescriptorRef> desc(CTFontCopyFontDescriptor(fontRef));
+    SkFontStyle style = fontstyle_from_descriptor(desc, &isFixedPitch);
     SkFontID fontID = CTFontRef_to_SkFontID(fontRef);
 
     return new SkTypeface_Mac(style, fontID, isFixedPitch, fontRef, name, isLocalStream);
 }
 
-static SkTypeface* NewFromName(const char familyName[], SkTypeface::Style theStyle) {
+static SkTypeface* NewFromName(const char familyName[], const SkFontStyle& theStyle) {
     CTFontRef ctFont = NULL;
 
     CTFontSymbolicTraits ctFontTraits = 0;
-    if (theStyle & SkTypeface::kBold) {
+    if (theStyle.weight() >= SkFontStyle::kBold_Weight) {
         ctFontTraits |= kCTFontBoldTrait;
     }
-    if (theStyle & SkTypeface::kItalic) {
+    if (theStyle.slant() != SkFontStyle::kUpright_Slant) {
         ctFontTraits |= kCTFontItalicTrait;
     }
 
+    //TODO: add weight width slant
+
     // Create the font info
     AutoCFRelease<CFStringRef> cfFontName(make_CFString(familyName));
 
@@ -534,8 +552,8 @@ static SkTypeface* GetDefaultFace() {
     static SkTypeface* gDefaultFace;
 
     if (NULL == gDefaultFace) {
-        gDefaultFace = NewFromName(FONT_DEFAULT_NAME, SkTypeface::kNormal);
-        SkTypefaceCache::Add(gDefaultFace, SkTypeface::kNormal);
+        gDefaultFace = NewFromName(FONT_DEFAULT_NAME, SkFontStyle());
+        SkTypefaceCache::Add(gDefaultFace, SkFontStyle());
     }
     return gDefaultFace;
 }
@@ -558,7 +576,7 @@ SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef) {
         face->ref();
     } else {
         face = NewFromFontRef(fontRef, NULL, false);
-        SkTypefaceCache::Add(face, face->style());
+        SkTypefaceCache::Add(face, face->fontStyle());
         // NewFromFontRef doesn't retain the parameter, but the typeface it
         // creates does release it in its destructor, so we balance that with
         // a retain call here.
@@ -570,11 +588,10 @@ SkTypeface* SkCreateTypefaceFromCTFont(CTFontRef fontRef) {
 
 struct NameStyleRec {
     const char*         fName;
-    SkTypeface::Style   fStyle;
+    SkFontStyle   fStyle;
 };
 
-static bool FindByNameStyle(SkTypeface* face, SkTypeface::Style style,
-                            void* ctx) {
+static bool FindByNameStyle(SkTypeface* face, const SkFontStyle& style, void* ctx) {
     const SkTypeface_Mac* mface = reinterpret_cast<SkTypeface_Mac*>(face);
     const NameStyleRec* rec = reinterpret_cast<const NameStyleRec*>(ctx);
 
@@ -599,39 +616,6 @@ static const char* map_css_names(const char* name) {
     return name;    // no change
 }
 
-static SkTypeface* create_typeface(const SkTypeface* familyFace,
-                                   const char familyName[],
-                                   SkTypeface::Style style) {
-    if (familyName) {
-        familyName = map_css_names(familyName);
-    }
-
-    // Clone an existing typeface
-    // TODO: only clone if style matches the familyFace's style...
-    if (familyName == NULL && familyFace != NULL) {
-        familyFace->ref();
-        return const_cast<SkTypeface*>(familyFace);
-    }
-
-    if (!familyName || !*familyName) {
-        familyName = FONT_DEFAULT_NAME;
-    }
-
-    NameStyleRec rec = { familyName, style };
-    SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByNameStyle, &rec);
-
-    if (NULL == face) {
-        face = NewFromName(familyName, style);
-        if (face) {
-            SkTypefaceCache::Add(face, style);
-        } else {
-            face = GetDefaultFace();
-            face->ref();
-        }
-    }
-    return face;
-}
-
 ///////////////////////////////////////////////////////////////////////////////
 
 /** GlyphRect is in FUnits (em space, y up). */
@@ -2008,7 +1992,6 @@ int SkTypeface_Mac::onCountGlyphs() const {
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
-#if 1
 
 static bool find_desc_str(CTFontDescriptorRef desc, CFStringRef name, SkString* value) {
     AutoCFRelease<CFStringRef> ref((CFStringRef)CTFontDescriptorCopyAttribute(desc, name));
@@ -2019,35 +2002,8 @@ static bool find_desc_str(CTFontDescriptorRef desc, CFStringRef name, SkString*
     return true;
 }
 
-static bool find_dict_float(CFDictionaryRef dict, CFStringRef name, float* value) {
-    CFNumberRef num;
-    return CFDictionaryGetValueIfPresent(dict, name, (const void**)&num)
-    && CFNumberIsFloatType(num)
-    && CFNumberGetValue(num, kCFNumberFloatType, value);
-}
-
 #include "SkFontMgr.h"
 
-static int unit_weight_to_fontstyle(float unit) {
-    float value;
-    if (unit < 0) {
-        value = 100 + (1 + unit) * 300;
-    } else {
-        value = 400 + unit * 500;
-    }
-    return sk_float_round2int(value);
-}
-
-static int unit_width_to_fontstyle(float unit) {
-    float value;
-    if (unit < 0) {
-        value = 1 + (1 + unit) * 4;
-    } else {
-        value = 5 + unit * 4;
-    }
-    return sk_float_round2int(value);
-}
-
 static inline int sqr(int value) {
     SkASSERT(SkAbs32(value) < 0x7FFF);  // check for overflow
     return value * value;
@@ -2060,53 +2016,25 @@ static int compute_metric(const SkFontStyle& a, const SkFontStyle& b) {
            sqr((a.isItalic() != b.isItalic()) * 900);
 }
 
-static SkFontStyle desc2fontstyle(CTFontDescriptorRef desc) {
-    AutoCFRelease<CFDictionaryRef> dict(
-        (CFDictionaryRef)CTFontDescriptorCopyAttribute(desc,
-                                                       kCTFontTraitsAttribute));
-    if (NULL == dict.get()) {
-        return SkFontStyle();
-    }
-
-    float weight, width, slant;
-    if (!find_dict_float(dict, kCTFontWeightTrait, &weight)) {
-        weight = 0;
-    }
-    if (!find_dict_float(dict, kCTFontWidthTrait, &width)) {
-        width = 0;
-    }
-    if (!find_dict_float(dict, kCTFontSlantTrait, &slant)) {
-        slant = 0;
-    }
-
-    return SkFontStyle(unit_weight_to_fontstyle(weight),
-                       unit_width_to_fontstyle(width),
-                       slant ? SkFontStyle::kItalic_Slant
-                       : SkFontStyle::kUpright_Slant);
-}
-
 struct NameFontStyleRec {
     SkString    fFamilyName;
     SkFontStyle fFontStyle;
 };
 
-static bool nameFontStyleProc(SkTypeface* face, SkTypeface::Style,
-                              void* ctx) {
+static bool nameFontStyleProc(SkTypeface* face, const SkFontStyle&, void* ctx) {
     SkTypeface_Mac* macFace = (SkTypeface_Mac*)face;
     const NameFontStyleRec* rec = (const NameFontStyleRec*)ctx;
 
-    return macFace->fFontStyle == rec->fFontStyle &&
+    return macFace->fontStyle() == rec->fFontStyle &&
            macFace->fName == rec->fFamilyName;
 }
 
-static SkTypeface* createFromDesc(CFStringRef cfFamilyName,
-                                  CTFontDescriptorRef desc) {
+static SkTypeface* createFromDesc(CFStringRef cfFamilyName, CTFontDescriptorRef desc) {
     NameFontStyleRec rec;
     CFStringToSkString(cfFamilyName, &rec.fFamilyName);
-    rec.fFontStyle = desc2fontstyle(desc);
+    rec.fFontStyle = fontstyle_from_descriptor(desc, NULL);
 
-    SkTypeface* face = SkTypefaceCache::FindByProcAndRef(nameFontStyleProc,
-                                                         &rec);
+    SkTypeface* face = SkTypefaceCache::FindByProcAndRef(nameFontStyleProc, &rec);
     if (face) {
         return face;
     }
@@ -2127,12 +2055,13 @@ static SkTypeface* createFromDesc(CFStringRef cfFamilyName,
     CFStringToSkString(cfFamilyName, &str);
 
     bool isFixedPitch;
-    (void)computeStyleBits(ctFont, &isFixedPitch);
+    AutoCFRelease<CTFontDescriptorRef> ctFontDesc(CTFontCopyFontDescriptor(ctFont));
+    (void)fontstyle_from_descriptor(ctFontDesc, &isFixedPitch);
     SkFontID fontID = CTFontRef_to_SkFontID(ctFont);
 
     face = SkNEW_ARGS(SkTypeface_Mac, (rec.fFontStyle, fontID, isFixedPitch,
                                        ctFont, str.c_str(), false));
-    SkTypefaceCache::Add(face, face->style());
+    SkTypefaceCache::Add(face, face->fontStyle());
     return face;
 }
 
@@ -2158,12 +2087,11 @@ public:
         return fCount;
     }
 
-    virtual void getStyle(int index, SkFontStyle* style,
-                          SkString* name) SK_OVERRIDE {
+    virtual void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE {
         SkASSERT((unsigned)index < (unsigned)fCount);
         CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray, index);
         if (style) {
-            *style = desc2fontstyle(desc);
+            *style = fontstyle_from_descriptor(desc, NULL);
         }
         if (name) {
             if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) {
@@ -2197,7 +2125,7 @@ private:
 
         for (int i = 0; i < fCount; ++i) {
             CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fArray, i);
-            int metric = compute_metric(pattern, desc2fontstyle(desc));
+            int metric = compute_metric(pattern, fontstyle_from_descriptor(desc, NULL));
             if (0 == metric) {
                 return desc;
             }
@@ -2277,8 +2205,7 @@ protected:
         return NULL;
     }
 
-    virtual SkTypeface* onCreateFromData(SkData* data,
-                                         int ttcIndex) const SK_OVERRIDE {
+    virtual SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE {
         AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromData(data));
         if (NULL == pr) {
             return NULL;
@@ -2286,8 +2213,7 @@ protected:
         return create_from_dataProvider(pr);
     }
 
-    virtual SkTypeface* onCreateFromStream(SkStream* stream,
-                                           int ttcIndex) const SK_OVERRIDE {
+    virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
         AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromStream(stream));
         if (NULL == pr) {
             return NULL;
@@ -2295,8 +2221,7 @@ protected:
         return create_from_dataProvider(pr);
     }
 
-    virtual SkTypeface* onCreateFromFile(const char path[],
-                                         int ttcIndex) const SK_OVERRIDE {
+    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERRIDE {
         AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(path));
         if (NULL == pr) {
             return NULL;
@@ -2306,7 +2231,29 @@ protected:
 
     virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
                                                unsigned styleBits) const SK_OVERRIDE {
-        return create_typeface(NULL, familyName, (SkTypeface::Style)styleBits);
+
+        SkFontStyle style = SkFontStyle((SkTypeface::Style)styleBits);
+        if (familyName) {
+            familyName = map_css_names(familyName);
+        }
+
+        if (!familyName || !*familyName) {
+            familyName = FONT_DEFAULT_NAME;
+        }
+
+        NameStyleRec rec = { familyName, style };
+        SkTypeface* face = SkTypefaceCache::FindByProcAndRef(FindByNameStyle, &rec);
+
+        if (NULL == face) {
+            face = NewFromName(familyName, style);
+            if (face) {
+                SkTypefaceCache::Add(face, style);
+            } else {
+                face = GetDefaultFace();
+                face->ref();
+            }
+        }
+        return face;
     }
 };
 
@@ -2315,4 +2262,3 @@ protected:
 SkFontMgr* SkFontMgr::Factory() {
     return SkNEW(SkFontMgr_Mac);
 }
-#endif
index 1290c007a5fd5217d20f4d9fc31ee55d53d3e423..348246b89329d49fb481aafe9365ecd1139f1cdc 100755 (executable)
@@ -126,20 +126,10 @@ static void make_canonical(LOGFONT* lf) {
 //    lf->lfClipPrecision = 64;
 }
 
-static SkTypeface::Style get_style(const LOGFONT& lf) {
-    unsigned style = 0;
-    if (lf.lfWeight >= FW_BOLD) {
-        style |= SkTypeface::kBold;
-    }
-    if (lf.lfItalic) {
-        style |= SkTypeface::kItalic;
-    }
-    return static_cast<SkTypeface::Style>(style);
-}
-
-static void setStyle(LOGFONT* lf, SkTypeface::Style style) {
-    lf->lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL ;
-    lf->lfItalic = ((style & SkTypeface::kItalic) != 0);
+static SkFontStyle get_style(const LOGFONT& lf) {
+    return SkFontStyle(lf.lfWeight,
+                       lf.lfWidth,
+                       lf.lfItalic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
 }
 
 static inline FIXED SkFixedToFIXED(SkFixed x) {
@@ -217,8 +207,11 @@ static unsigned calculateUPEM(HDC hdc, const LOGFONT& lf) {
 
 class LogFontTypeface : public SkTypeface {
 public:
-    LogFontTypeface(SkTypeface::Style style, SkFontID fontID, const LOGFONT& lf, bool serializeAsStream = false) :
-        SkTypeface(style, fontID, false), fLogFont(lf), fSerializeAsStream(serializeAsStream) {
+    LogFontTypeface(const SkFontStyle& style, const LOGFONT& lf, bool serializeAsStream)
+        : SkTypeface(style, SkTypefaceCache::NewFontID(), false)
+        , fLogFont(lf)
+        , fSerializeAsStream(serializeAsStream)
+    {
 
         // If the font has cubic outlines, it will not be rendered with ClearType.
         HFONT font = CreateFontIndirect(&lf);
@@ -255,9 +248,7 @@ public:
     bool fCanBeLCD;
 
     static LogFontTypeface* Create(const LOGFONT& lf) {
-        SkTypeface::Style style = get_style(lf);
-        SkFontID fontID = SkTypefaceCache::NewFontID();
-        return new LogFontTypeface(style, fontID, lf);
+        return new LogFontTypeface(get_style(lf), lf, false);
     }
 
     static void EnsureAccessible(const SkTypeface* face) {
@@ -289,9 +280,7 @@ public:
      *  The created FontMemResourceTypeface takes ownership of fontMemResource.
      */
     static FontMemResourceTypeface* Create(const LOGFONT& lf, HANDLE fontMemResource) {
-        SkTypeface::Style style = get_style(lf);
-        SkFontID fontID = SkTypefaceCache::NewFontID();
-        return new FontMemResourceTypeface(style, fontID, lf, fontMemResource);
+        return new FontMemResourceTypeface(get_style(lf), lf, fontMemResource);
     }
 
 protected:
@@ -305,9 +294,9 @@ private:
     /**
      *  Takes ownership of fontMemResource.
      */
-    FontMemResourceTypeface(SkTypeface::Style style, SkFontID fontID, const LOGFONT& lf, HANDLE fontMemResource) :
-        LogFontTypeface(style, fontID, lf, true), fFontMemResource(fontMemResource) {
-    }
+    FontMemResourceTypeface(const SkFontStyle& style, const LOGFONT& lf, HANDLE fontMemResource)
+        : LogFontTypeface(style, lf, true), fFontMemResource(fontMemResource)
+    }
 
     HANDLE fFontMemResource;
 
@@ -319,7 +308,7 @@ static const LOGFONT& get_default_font() {
     return gDefaultFont;
 }
 
-static bool FindByLogFont(SkTypeface* face, SkTypeface::Style requestedStyle, void* ctx) {
+static bool FindByLogFont(SkTypeface* face, const SkFontStyle& requestedStyle, void* ctx) {
     LogFontTypeface* lface = static_cast<LogFontTypeface*>(face);
     const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx);
 
@@ -2457,12 +2446,6 @@ static int CALLBACK enum_family_proc(const LOGFONT* lf, const TEXTMETRIC*,
     return 1; // non-zero means continue
 }
 
-static SkFontStyle compute_fontstyle(const LOGFONT& lf) {
-    return SkFontStyle(lf.lfWeight, SkFontStyle::kNormal_Width,
-                       lf.lfItalic ? SkFontStyle::kItalic_Slant
-                                   : SkFontStyle::kUpright_Slant);
-}
-
 class SkFontStyleSetGDI : public SkFontStyleSet {
 public:
     SkFontStyleSetGDI(const TCHAR familyName[]) {
@@ -2482,7 +2465,7 @@ public:
 
     virtual void getStyle(int index, SkFontStyle* fs, SkString* styleName) SK_OVERRIDE {
         if (fs) {
-            *fs = compute_fontstyle(fArray[index].elfLogFont);
+            *fs = get_style(fArray[index].elfLogFont);
         }
         if (styleName) {
             const ENUMLOGFONTEX& ref = fArray[index];
@@ -2585,7 +2568,10 @@ protected:
         } else {
             logfont_for_name(familyName, &lf);
         }
-        setStyle(&lf, (SkTypeface::Style)styleBits);
+
+        SkTypeface::Style style = (SkTypeface::Style)styleBits;
+        lf.lfWeight = (style & SkTypeface::kBold) != 0 ? FW_BOLD : FW_NORMAL;
+        lf.lfItalic = ((style & SkTypeface::kItalic) != 0);
         return SkCreateTypefaceFromLOGFONT(lf);
     }
 
index 5e93bf8a6208522c600bedca95d02529a3f7f570..15f1d3ed58ba0da153aad7a64504c6908aeedd71 100644 (file)
@@ -42,7 +42,7 @@ static const char* gTestFontFilePrefix = NULL;
 class SkTypeface_Android : public SkTypeface_FreeType {
 public:
     SkTypeface_Android(int index,
-                       Style style,
+                       const SkFontStyle& style,
                        bool isFixedPitch,
                        const SkString familyName)
         : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
@@ -65,7 +65,7 @@ class SkTypeface_AndroidSystem : public SkTypeface_Android {
 public:
     SkTypeface_AndroidSystem(const SkString pathName,
                              int index,
-                             Style style,
+                             const SkFontStyle& style,
                              bool isFixedPitch,
                              const SkString familyName,
                              const SkLanguage& lang,
@@ -100,7 +100,7 @@ class SkTypeface_AndroidStream : public SkTypeface_Android {
 public:
     SkTypeface_AndroidStream(SkStream* stream,
                              int index,
-                             Style style,
+                             const SkFontStyle& style,
                              bool isFixedPitch,
                              const SkString familyName)
         : INHERITED(index, style, isFixedPitch, familyName)
@@ -158,7 +158,7 @@ public:
 
             const int ttcIndex = fontFile.fIndex;
             SkString familyName;
-            SkTypeface::Style style;
+            SkFontStyle style;
             bool isFixedWidth;
             if (!SkTypeface_FreeType::ScanFont(stream.get(), ttcIndex,
                                                &familyName, &style, &isFixedWidth)) {
@@ -404,7 +404,7 @@ protected:
 
     virtual SkTypeface* onCreateFromStream(SkStream* stream, int ttcIndex) const SK_OVERRIDE {
         bool isFixedPitch;
-        SkTypeface::Style style;
+        SkFontStyle style;
         SkString name;
         if (!SkTypeface_FreeType::ScanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
             return NULL;
@@ -416,14 +416,7 @@ protected:
 
     virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
                                                unsigned styleBits) const SK_OVERRIDE {
-        SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits;
-        SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold
-                                                 ? SkFontStyle::kBold_Weight
-                                                 : SkFontStyle::kNormal_Weight,
-                                        SkFontStyle::kNormal_Width,
-                                        oldStyle & SkTypeface::kItalic
-                                                 ? SkFontStyle::kItalic_Slant
-                                                 : SkFontStyle::kUpright_Slant);
+        SkFontStyle style = SkFontStyle(styleBits);
 
         if (familyName) {
             // On Android, we must return NULL when we can't find the requested
index 6fc1f28c89bc318ce2d26622b5acf94f0da295ef..dbc64b6e47655749ab8bf2919e708f01b10eacf3 100644 (file)
@@ -373,20 +373,13 @@ static void fcpattern_from_skfontstyle(SkFontStyle style, FcPattern* pattern) {
     FcPatternAddInteger(pattern, FC_SLANT, style.isItalic() ? FC_SLANT_ITALIC : FC_SLANT_ROMAN);
 }
 
-static SkTypeface::Style sktypefacestyle_from_fcpattern(FcPattern* pattern) {
-    int fcweight = get_int(pattern, FC_WEIGHT, FC_WEIGHT_REGULAR);
-    int fcslant = get_int(pattern, FC_SLANT, FC_SLANT_ROMAN);
-    return (SkTypeface::Style)((fcweight >= FC_WEIGHT_BOLD ? SkTypeface::kBold : 0) |
-                                (fcslant > FC_SLANT_ROMAN ? SkTypeface::kItalic : 0));
-}
-
 class SkTypeface_stream : public SkTypeface_FreeType {
 public:
     /** @param stream does not take ownership of the reference, does take ownership of the stream.*/
-    SkTypeface_stream(SkTypeface::Style style, bool fixedWidth, int ttcIndex, SkStreamAsset* stream)
+    SkTypeface_stream(const SkFontStyle& style, bool fixedWidth, int index, SkStreamAsset* stream)
         : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth)
         , fStream(SkRef(stream))
-        , fIndex(ttcIndex)
+        , fIndex(index)
     { };
 
     virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
@@ -447,7 +440,7 @@ public:
 private:
     /** @param pattern takes ownership of the reference. */
     SkTypeface_fontconfig(FcPattern* pattern)
-        : INHERITED(sktypefacestyle_from_fcpattern(pattern),
+        : INHERITED(skfontstyle_from_fcpattern(pattern),
                     SkTypefaceCache::NewFontID(),
                     FC_PROPORTIONAL != get_int(pattern, FC_SPACING, FC_PROPORTIONAL))
         , fPattern(pattern)
@@ -571,7 +564,7 @@ class SkFontMgr_fontconfig : public SkFontMgr {
                                           sizes.begin(), names.count());
     }
 
-    static bool FindByFcPattern(SkTypeface* cached, SkTypeface::Style, void* ctx) {
+    static bool FindByFcPattern(SkTypeface* cached, const SkFontStyle&, void* ctx) {
         SkTypeface_fontconfig* cshFace = static_cast<SkTypeface_fontconfig*>(cached);
         FcPattern* ctxPattern = static_cast<FcPattern*>(ctx);
         return FcTrue == FcPatternEqual(cshFace->fPattern, ctxPattern);
@@ -590,7 +583,7 @@ class SkFontMgr_fontconfig : public SkFontMgr {
             FcPatternReference(pattern);
             face = SkTypeface_fontconfig::Create(pattern);
             if (face) {
-                fTFCache.add(face, SkTypeface::kNormal, true);
+                fTFCache.add(face, SkFontStyle(), true);
             }
         }
         return face;
@@ -818,7 +811,7 @@ protected:
             return NULL;
         }
 
-        SkTypeface::Style style = SkTypeface::kNormal;
+        SkFontStyle style;
         bool isFixedWidth = false;
         if (!SkTypeface_FreeType::ScanFont(stream, ttcIndex, NULL, &style, &isFixedWidth)) {
             return NULL;
index ecca57ff513da78da6dcbcb63eb4785004917e4e..e9d494f51477cb892984222085935742da845cdd 100644 (file)
@@ -331,7 +331,7 @@ struct ProtoDWriteTypeface {
     IDWriteFontFamily* fDWriteFontFamily;
 };
 
-static bool FindByDWriteFont(SkTypeface* cached, SkTypeface::Style, void* ctx) {
+static bool FindByDWriteFont(SkTypeface* cached, const SkFontStyle&, void* ctx) {
     DWriteFontTypeface* cshFace = reinterpret_cast<DWriteFontTypeface*>(cached);
     ProtoDWriteTypeface* ctxFace = reinterpret_cast<ProtoDWriteTypeface*>(ctx);
     bool same;
index 531dc51a1179b907acbbd098bf04e31bcdb0e55c..7d72dfd4d649233490214a23d5929d99100b6929 100644 (file)
 class SkFontDescriptor;
 struct SkScalerContextRec;
 
-static SkTypeface::Style get_style(IDWriteFont* font) {
-    int style = SkTypeface::kNormal;
-    DWRITE_FONT_WEIGHT weight = font->GetWeight();
-    if (DWRITE_FONT_WEIGHT_DEMI_BOLD <= weight) {
-        style |= SkTypeface::kBold;
-    }
-    DWRITE_FONT_STYLE angle = font->GetStyle();
-    if (DWRITE_FONT_STYLE_OBLIQUE == angle || DWRITE_FONT_STYLE_ITALIC == angle) {
-        style |= SkTypeface::kItalic;
-    }
-    return static_cast<SkTypeface::Style>(style);
+static SkFontStyle get_style(IDWriteFont* font) {
+    DWRITE_FONT_STYLE dwStyle = font->GetStyle();
+    return SkFontStyle(font->GetWeight(),
+                       font->GetStretch(),
+                       (DWRITE_FONT_STYLE_OBLIQUE == dwStyle ||
+                        DWRITE_FONT_STYLE_ITALIC  == dwStyle)
+                                                   ? SkFontStyle::kItalic_Slant
+                                                   : SkFontStyle::kUpright_Slant);
 }
 
 class DWriteFontTypeface : public SkTypeface {
 private:
-    DWriteFontTypeface(SkTypeface::Style style, SkFontID fontID,
+    DWriteFontTypeface(const SkFontStyle& style, SkFontID fontID,
                        IDWriteFactory* factory,
                        IDWriteFontFace* fontFace,
                        IDWriteFont* font,
@@ -80,9 +77,8 @@ public:
                                       IDWriteFontFamily* fontFamily,
                                       IDWriteFontFileLoader* fontFileLoader = NULL,
                                       IDWriteFontCollectionLoader* fontCollectionLoader = NULL) {
-        SkTypeface::Style style = get_style(font);
         SkFontID fontID = SkTypefaceCache::NewFontID();
-        return SkNEW_ARGS(DWriteFontTypeface, (style, fontID,
+        return SkNEW_ARGS(DWriteFontTypeface, (get_style(font), fontID,
                                                factory, fontFace, font, fontFamily,
                                                fontFileLoader, fontCollectionLoader));
     }
index 3236f07c84e1912dc98e71c177e3b76b0b9cf6e4..7cee944b5250d70d8d5e09bc8fd9097e396fff8b 100644 (file)
@@ -63,7 +63,7 @@ SkTypeface* create_font(const char* name, SkTypeface::Style style) {
             atexit(release_portable_typefaces);
         }
     }
-    return SkNEW_ARGS(SkTestTypeface, (font, style));
+    return SkNEW_ARGS(SkTestTypeface, (font, SkFontStyle(style)));
 }