1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 18 #ifndef SkTypefaceCache_DEFINED 19 #define SkTypefaceCache_DEFINED 20 21 #include "SkTypeface.h" 22 #include "SkTDArray.h" 23 24 /* TODO 25 * Provide std way to cache name+requestedStyle aliases to the same typeface. 26 * 27 * The current mechanism ends up create a diff typeface for each one, even if 28 * they map to the same internal obj (e.g. CTFontRef on the mac) 29 */ 30 31 class SkTypefaceCache { 32 public: 33 typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context); 34 35 /** 36 * Helper: returns a unique fontID to pass to the constructor of 37 * your subclass of SkTypeface 38 */ 39 static SkFontID NewFontID(); 40 41 /** 42 * Add a typeface to the cache. This ref()s the typeface, so that the 43 * cache is also an owner. Later, if we need to purge the cache, it will 44 * unref() typefaces whose refcnt is 1 (meaning only the cache is an owner). 45 */ 46 static void Add(SkTypeface*, SkTypeface::Style requested); 47 48 /** 49 * Search the cache for a typeface with the specified fontID (uniqueID). 50 * If one is found, return it (its reference count is unmodified). If none 51 * is found, return NULL. 52 */ 53 static SkTypeface* FindByID(SkFontID fontID); 54 55 /** 56 * Iterate through the cache, calling proc(typeface, ctx) with each 57 * typeface. If proc returns true, then we return that typeface (its 58 * reference count is unmodified). If it never returns true, we return NULL. 59 */ 60 static SkTypeface* FindByProc(FindProc proc, void* ctx); 61 62 /** 63 * Debugging only: dumps the status of the typefaces in the cache 64 */ 65 static void Dump(); 66 67 private: 68 static SkTypefaceCache& Get(); 69 70 void add(SkTypeface*, SkTypeface::Style requested); 71 SkTypeface* findByID(SkFontID findID) const; 72 SkTypeface* findByProc(FindProc proc, void* ctx) const; 73 void purge(int count); 74 75 struct Rec { 76 SkTypeface* fFace; 77 SkTypeface::Style fRequestedStyle; 78 }; 79 SkTDArray<Rec> fArray; 80 }; 81 82 #endif 83