SkTypefaceCache.h revision 80bacfeb4bda06541e8695bd502229727bccfea
180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/* 380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc. 480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * 580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be 680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file. 780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifndef SkTypefaceCache_DEFINED 1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SkTypefaceCache_DEFINED 1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTypeface.h" 1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTDArray.h" 1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/* TODO 1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Provide std way to cache name+requestedStyle aliases to the same typeface. 1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * 2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * The current mechanism ends up create a diff typeface for each one, even if 2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * they map to the same internal obj (e.g. CTFontRef on the mac) 2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass SkTypefaceCache { 2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic: 2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Callback for FindByProc. Returns true if the given typeface is a match 2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * for the given context. The passed typeface is owned by the cache and is 2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * not additionally ref()ed. The typeface may be in the disposed state. 3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context); 3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Helper: returns a unique fontID to pass to the constructor of 3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * your subclass of SkTypeface 3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static SkFontID NewFontID(); 3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Add a typeface to the cache. This ref()s the typeface, so that the 4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * cache is also an owner. Later, if we need to purge the cache, typefaces 4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * whose refcnt is 1 (meaning only the cache is an owner) will be 4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * unref()ed. 4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static void Add(SkTypeface*, 4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkTypeface::Style requested, 4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru bool strong = true); 4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Search the cache for a typeface with the specified fontID (uniqueID). 5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * If one is found, return it (its reference count is unmodified). If none 5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * is found, return NULL. The reference count is unmodified as it is 5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * assumed that the stack will contain a ref to the typeface. 5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static SkTypeface* FindByID(SkFontID fontID); 5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Iterate through the cache, calling proc(typeface, ctx) with each 5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * typeface. If proc returns true, then we return that typeface (this 6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * ref()s the typeface). If it never returns true, we return NULL. 6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx); 6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * This will unref all of the typefaces in the cache for which the cache 6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * is the only owner. Normally this is handled automatically as needed. 6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * This function is exposed for clients that explicitly want to purge the 6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * cache (e.g. to look for leaks). 6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static void PurgeAll(); 7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru /** 7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Debugging only: dumps the status of the typefaces in the cache 7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static void Dump(); 7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate: 7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru static SkTypefaceCache& Get(); 7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void add(SkTypeface*, SkTypeface::Style requested, bool strong = true); 8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkTypeface* findByID(SkFontID findID) const; 8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const; 8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void purge(int count); 8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru void purgeAll(); 8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru struct Rec { 8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkTypeface* fFace; 8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru bool fStrong; 8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkTypeface::Style fRequestedStyle; 9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru }; 9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkTDArray<Rec> fArray; 9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}; 9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif 95