OLD | NEW |
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 2190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2201 bestMetric = metric; | 2201 bestMetric = metric; |
2202 bestDesc = desc; | 2202 bestDesc = desc; |
2203 } | 2203 } |
2204 } | 2204 } |
2205 SkASSERT(bestDesc); | 2205 SkASSERT(bestDesc); |
2206 return bestDesc; | 2206 return bestDesc; |
2207 } | 2207 } |
2208 }; | 2208 }; |
2209 | 2209 |
2210 class SkFontMgr_Mac : public SkFontMgr { | 2210 class SkFontMgr_Mac : public SkFontMgr { |
| 2211 CFArrayRef fNames; |
2211 int fCount; | 2212 int fCount; |
2212 CFArrayRef fNames; | |
2213 | 2213 |
2214 CFStringRef stringAt(int index) const { | 2214 CFStringRef stringAt(int index) const { |
2215 SkASSERT((unsigned)index < (unsigned)fCount); | 2215 SkASSERT((unsigned)index < (unsigned)fCount); |
2216 return (CFStringRef)CFArrayGetValueAtIndex(fNames, index); | 2216 return (CFStringRef)CFArrayGetValueAtIndex(fNames, index); |
2217 } | 2217 } |
2218 | 2218 |
2219 void lazyInit() { | |
2220 if (NULL == fNames) { | |
2221 fNames = SkCTFontManagerCopyAvailableFontFamilyNames(); | |
2222 fCount = fNames ? SkToInt(CFArrayGetCount(fNames)) : 0; | |
2223 } | |
2224 } | |
2225 | |
2226 static SkFontStyleSet* CreateSet(CFStringRef cfFamilyName) { | 2219 static SkFontStyleSet* CreateSet(CFStringRef cfFamilyName) { |
2227 AutoCFRelease<CFMutableDictionaryRef> cfAttr( | 2220 AutoCFRelease<CFMutableDictionaryRef> cfAttr( |
2228 CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | 2221 CFDictionaryCreateMutable(kCFAllocatorDefault, 0, |
2229 &kCFTypeDictionaryKeyCallBacks, | 2222 &kCFTypeDictionaryKeyCallBacks, |
2230 &kCFTypeDictionaryValueCallBacks)); | 2223 &kCFTypeDictionaryValueCallBacks)); |
2231 | 2224 |
2232 CFDictionaryAddValue(cfAttr, kCTFontFamilyNameAttribute, cfFamilyName); | 2225 CFDictionaryAddValue(cfAttr, kCTFontFamilyNameAttribute, cfFamilyName); |
2233 | 2226 |
2234 AutoCFRelease<CTFontDescriptorRef> desc( | 2227 AutoCFRelease<CTFontDescriptorRef> desc( |
2235 CTFontDescriptorCreateWithAttributes(cfAttr)); | 2228 CTFontDescriptorCreateWithAttributes(cfAttr)); |
2236 return SkNEW_ARGS(SkFontStyleSet_Mac, (cfFamilyName, desc)); | 2229 return SkNEW_ARGS(SkFontStyleSet_Mac, (cfFamilyName, desc)); |
2237 } | 2230 } |
2238 | 2231 |
2239 public: | 2232 public: |
2240 SkFontMgr_Mac() : fCount(0), fNames(NULL) {} | 2233 SkFontMgr_Mac() |
| 2234 : fNames(SkCTFontManagerCopyAvailableFontFamilyNames()) |
| 2235 , fCount(fNames ? SkToInt(CFArrayGetCount(fNames)) : 0) {} |
2241 | 2236 |
2242 virtual ~SkFontMgr_Mac() { | 2237 virtual ~SkFontMgr_Mac() { |
2243 CFSafeRelease(fNames); | 2238 CFSafeRelease(fNames); |
2244 } | 2239 } |
2245 | 2240 |
2246 protected: | 2241 protected: |
2247 virtual int onCountFamilies() SK_OVERRIDE { | 2242 virtual int onCountFamilies() const SK_OVERRIDE { |
2248 this->lazyInit(); | |
2249 return fCount; | 2243 return fCount; |
2250 } | 2244 } |
2251 | 2245 |
2252 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE { | 2246 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERR
IDE { |
2253 this->lazyInit(); | |
2254 if ((unsigned)index < (unsigned)fCount) { | 2247 if ((unsigned)index < (unsigned)fCount) { |
2255 CFStringToSkString(this->stringAt(index), familyName); | 2248 CFStringToSkString(this->stringAt(index), familyName); |
2256 } else { | 2249 } else { |
2257 familyName->reset(); | 2250 familyName->reset(); |
2258 } | 2251 } |
2259 } | 2252 } |
2260 | 2253 |
2261 virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE { | 2254 virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE { |
2262 this->lazyInit(); | |
2263 if ((unsigned)index >= (unsigned)fCount) { | 2255 if ((unsigned)index >= (unsigned)fCount) { |
2264 return NULL; | 2256 return NULL; |
2265 } | 2257 } |
2266 return CreateSet(this->stringAt(index)); | 2258 return CreateSet(this->stringAt(index)); |
2267 } | 2259 } |
2268 | 2260 |
2269 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) SK_OVERRIDE { | 2261 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const SK_OVER
RIDE { |
2270 AutoCFRelease<CFStringRef> cfName(make_CFString(familyName)); | 2262 AutoCFRelease<CFStringRef> cfName(make_CFString(familyName)); |
2271 return CreateSet(cfName); | 2263 return CreateSet(cfName); |
2272 } | 2264 } |
2273 | 2265 |
2274 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], | 2266 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
2275 const SkFontStyle&) SK_OVERRIDE { | 2267 const SkFontStyle&) const SK_OVERRIDE
{ |
2276 return NULL; | 2268 return NULL; |
2277 } | 2269 } |
2278 | 2270 |
2279 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, | 2271 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember, |
2280 const SkFontStyle&) SK_OVERRIDE { | 2272 const SkFontStyle&) const SK_OVERRIDE { |
2281 return NULL; | 2273 return NULL; |
2282 } | 2274 } |
2283 | 2275 |
2284 virtual SkTypeface* onCreateFromData(SkData* data, | 2276 virtual SkTypeface* onCreateFromData(SkData* data, |
2285 int ttcIndex) SK_OVERRIDE { | 2277 int ttcIndex) const SK_OVERRIDE { |
2286 AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromData(data)); | 2278 AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromData(data)); |
2287 if (NULL == pr) { | 2279 if (NULL == pr) { |
2288 return NULL; | 2280 return NULL; |
2289 } | 2281 } |
2290 return create_from_dataProvider(pr); | 2282 return create_from_dataProvider(pr); |
2291 } | 2283 } |
2292 | 2284 |
2293 virtual SkTypeface* onCreateFromStream(SkStream* stream, | 2285 virtual SkTypeface* onCreateFromStream(SkStream* stream, |
2294 int ttcIndex) SK_OVERRIDE { | 2286 int ttcIndex) const SK_OVERRIDE { |
2295 AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromStream(strea
m)); | 2287 AutoCFRelease<CGDataProviderRef> pr(SkCreateDataProviderFromStream(strea
m)); |
2296 if (NULL == pr) { | 2288 if (NULL == pr) { |
2297 return NULL; | 2289 return NULL; |
2298 } | 2290 } |
2299 return create_from_dataProvider(pr); | 2291 return create_from_dataProvider(pr); |
2300 } | 2292 } |
2301 | 2293 |
2302 virtual SkTypeface* onCreateFromFile(const char path[], | 2294 virtual SkTypeface* onCreateFromFile(const char path[], |
2303 int ttcIndex) SK_OVERRIDE { | 2295 int ttcIndex) const SK_OVERRIDE { |
2304 AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(pat
h)); | 2296 AutoCFRelease<CGDataProviderRef> pr(CGDataProviderCreateWithFilename(pat
h)); |
2305 if (NULL == pr) { | 2297 if (NULL == pr) { |
2306 return NULL; | 2298 return NULL; |
2307 } | 2299 } |
2308 return create_from_dataProvider(pr); | 2300 return create_from_dataProvider(pr); |
2309 } | 2301 } |
2310 | 2302 |
2311 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 2303 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
2312 unsigned styleBits) SK_OVERRIDE { | 2304 unsigned styleBits) const SK_OVER
RIDE { |
2313 return create_typeface(NULL, familyName, (SkTypeface::Style)styleBits); | 2305 return create_typeface(NULL, familyName, (SkTypeface::Style)styleBits); |
2314 } | 2306 } |
2315 }; | 2307 }; |
2316 | 2308 |
2317 /////////////////////////////////////////////////////////////////////////////// | 2309 /////////////////////////////////////////////////////////////////////////////// |
2318 | 2310 |
2319 SkFontMgr* SkFontMgr::Factory() { | 2311 SkFontMgr* SkFontMgr::Factory() { |
2320 return SkNEW(SkFontMgr_Mac); | 2312 return SkNEW(SkFontMgr_Mac); |
2321 } | 2313 } |
2322 #endif | 2314 #endif |
OLD | NEW |