Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: src/ports/SkFontHost_mac.cpp

Issue 13161002: add caching to fontmgr for mac (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include <vector> 9 #include <vector>
10 #ifdef SK_BUILD_FOR_MAC 10 #ifdef SK_BUILD_FOR_MAC
(...skipping 1919 matching lines...) Expand 10 before | Expand all | Expand 10 after
1930 if (!find_dict_float(dict, kCTFontSlantTrait, &slant)) { 1930 if (!find_dict_float(dict, kCTFontSlantTrait, &slant)) {
1931 slant = 0; 1931 slant = 0;
1932 } 1932 }
1933 1933
1934 return SkFontStyle(unit_weight_to_fontstyle(weight), 1934 return SkFontStyle(unit_weight_to_fontstyle(weight),
1935 unit_width_to_fontstyle(width), 1935 unit_width_to_fontstyle(width),
1936 slant ? SkFontStyle::kItalic_Slant 1936 slant ? SkFontStyle::kItalic_Slant
1937 : SkFontStyle::kUpright_Slant); 1937 : SkFontStyle::kUpright_Slant);
1938 } 1938 }
1939 1939
1940 struct NameFontStyleRec {
1941 SkString fFamilyName;
1942 SkFontStyle fFontStyle;
1943 };
1944
1945 static bool nameFontStyleProc(SkTypeface* face, SkTypeface::Style,
1946 void* ctx) {
1947 SkTypeface_Mac* macFace = (SkTypeface_Mac*)face;
1948 const NameFontStyleRec* rec = (const NameFontStyleRec*)ctx;
1949
1950 return macFace->fFontStyle == rec->fFontStyle &&
1951 macFace->fName == rec->fFamilyName;
1952 }
1953
1940 static SkTypeface* createFromDesc(CFStringRef cfFamilyName, 1954 static SkTypeface* createFromDesc(CFStringRef cfFamilyName,
1941 CTFontDescriptorRef desc) { 1955 CTFontDescriptorRef desc) {
1956 NameFontStyleRec rec;
1957 CFStringToSkString(cfFamilyName, &rec.fFamilyName);
1958 rec.fFontStyle = desc2fontstyle(desc);
1959
1960 SkTypeface* face = SkTypefaceCache::FindByProcAndRef(nameFontStyleProc,
1961 &rec);
1962 if (face) {
1963 return face;
1964 }
1965
1942 AutoCFRelease<CTFontRef> ctNamed(CTFontCreateWithName(cfFamilyName, 1, NULL) ); 1966 AutoCFRelease<CTFontRef> ctNamed(CTFontCreateWithName(cfFamilyName, 1, NULL) );
1943 CTFontRef ctFont = CTFontCreateCopyWithAttributes(ctNamed, 1, NULL, desc); 1967 CTFontRef ctFont = CTFontCreateCopyWithAttributes(ctNamed, 1, NULL, desc);
1944 if (NULL == ctFont) { 1968 if (NULL == ctFont) {
1945 return NULL; 1969 return NULL;
1946 } 1970 }
1947 1971
1948 SkString str; 1972 SkString str;
1949 CFStringToSkString(cfFamilyName, &str); 1973 CFStringToSkString(cfFamilyName, &str);
1950 1974
1951 bool isFixedPitch; 1975 bool isFixedPitch;
1952 (void)computeStyleBits(ctFont, &isFixedPitch); 1976 (void)computeStyleBits(ctFont, &isFixedPitch);
1953 SkFontID fontID = CTFontRef_to_SkFontID(ctFont); 1977 SkFontID fontID = CTFontRef_to_SkFontID(ctFont);
1954 1978
1955 return new SkTypeface_Mac(desc2fontstyle(desc), fontID, isFixedPitch, 1979 face = SkNEW_ARGS(SkTypeface_Mac, (rec.fFontStyle, fontID, isFixedPitch,
1956 ctFont, str.c_str()); 1980 ctFont, str.c_str()));
1981 SkTypefaceCache::Add(face, face->style());
1982 return face;
1957 } 1983 }
1958 1984
1959 class SkFontStyleSet_Mac : public SkFontStyleSet { 1985 class SkFontStyleSet_Mac : public SkFontStyleSet {
1960 public: 1986 public:
1961 SkFontStyleSet_Mac(CFStringRef familyName, CTFontDescriptorRef desc) 1987 SkFontStyleSet_Mac(CFStringRef familyName, CTFontDescriptorRef desc)
1962 : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL)) 1988 : fArray(CTFontDescriptorCreateMatchingFontDescriptors(desc, NULL))
1963 , fFamilyName(familyName) { 1989 , fFamilyName(familyName)
1990 , fCount(0) {
1964 CFRetain(familyName); 1991 CFRetain(familyName);
1992 if (NULL == fArray) {
1993 fArray = CFArrayCreate(NULL, NULL, 0, NULL);
1994 }
1995 fCount = CFArrayGetCount(fArray);
1965 } 1996 }
1966 1997
1967 virtual ~SkFontStyleSet_Mac() { 1998 virtual ~SkFontStyleSet_Mac() {
1968 CFSafeRelease(fArray); 1999 CFRelease(fArray);
1969 CFRelease(fFamilyName); 2000 CFRelease(fFamilyName);
1970 } 2001 }
1971 2002
1972 virtual int count() SK_OVERRIDE { 2003 virtual int count() SK_OVERRIDE {
1973 return CFArrayGetCount(fArray); 2004 return fCount;
1974 } 2005 }
1975 2006
1976 virtual void getStyle(int index, SkFontStyle* style, 2007 virtual void getStyle(int index, SkFontStyle* style,
1977 SkString* name) SK_OVERRIDE { 2008 SkString* name) SK_OVERRIDE {
1978 SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray)); 2009 SkASSERT((unsigned)index < (unsigned)fCount);
1979 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(f Array, index); 2010 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(f Array, index);
1980 if (style) { 2011 if (style) {
1981 *style = desc2fontstyle(desc); 2012 *style = desc2fontstyle(desc);
1982 } 2013 }
1983 if (name) { 2014 if (name) {
1984 if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) { 2015 if (!find_desc_str(desc, kCTFontStyleNameAttribute, name)) {
1985 name->reset(); 2016 name->reset();
1986 } 2017 }
1987 } 2018 }
1988 } 2019 }
1989 2020
1990 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { 2021 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
1991 SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray)); 2022 SkASSERT((unsigned)index < (unsigned)CFArrayGetCount(fArray));
1992 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(f Array, index); 2023 CTFontDescriptorRef desc = (CTFontDescriptorRef)CFArrayGetValueAtIndex(f Array, index);
1993 2024
1994 return createFromDesc(fFamilyName, desc); 2025 return createFromDesc(fFamilyName, desc);
1995 } 2026 }
1996 2027
1997 private: 2028 private:
1998 CFArrayRef fArray; 2029 CFArrayRef fArray;
1999 CFStringRef fFamilyName; 2030 CFStringRef fFamilyName;
2031 int fCount;
2000 }; 2032 };
2001 2033
2002 class SkFontMgr_Mac : public SkFontMgr { 2034 class SkFontMgr_Mac : public SkFontMgr {
2003 int fCount; 2035 int fCount;
2004 CFArrayRef fNames; 2036 CFArrayRef fNames;
2005 2037
2006 CFStringRef stringAt(int index) const { 2038 CFStringRef stringAt(int index) const {
2007 SkASSERT((unsigned)index < (unsigned)fCount); 2039 SkASSERT((unsigned)index < (unsigned)fCount);
2008 return (CFStringRef)CFArrayGetValueAtIndex(fNames, index); 2040 return (CFStringRef)CFArrayGetValueAtIndex(fNames, index);
2009 } 2041 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
2091 return NULL; 2123 return NULL;
2092 } 2124 }
2093 return create_from_dataProvider(pr); 2125 return create_from_dataProvider(pr);
2094 } 2126 }
2095 }; 2127 };
2096 2128
2097 SkFontMgr* SkFontMgr::Factory() { 2129 SkFontMgr* SkFontMgr::Factory() {
2098 return SkNEW(SkFontMgr_Mac); 2130 return SkNEW(SkFontMgr_Mac);
2099 } 2131 }
2100 #endif 2132 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698