SkTypefaceCache.h revision 0a657bbc2c6fc9daf699942e023050536d5ec95f
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:
260a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    SkTypefaceCache();
270a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    ~SkTypefaceCache();
280a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * Callback for FindByProc. Returns true if the given typeface is a match
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * for the given context. The passed typeface is owned by the cache and is
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     * not additionally ref()ed. The typeface may be in the disposed state.
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    typedef bool (*FindProc)(SkTypeface*, SkTypeface::Style, void* context);
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Add a typeface to the cache. This ref()s the typeface, so that the
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  cache is also an owner. Later, if we need to purge the cache, typefaces
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  whose refcnt is 1 (meaning only the cache is an owner) will be
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  unref()ed.
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
420a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    void add(SkTypeface*, SkTypeface::Style requested, bool strong = true);
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Search the cache for a typeface with the specified fontID (uniqueID).
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  If one is found, return it (its reference count is unmodified). If none
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  is found, return NULL. The reference count is unmodified as it is
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  assumed that the stack will contain a ref to the typeface.
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
500a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    SkTypeface* findByID(SkFontID findID) const;
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Iterate through the cache, calling proc(typeface, ctx) with each
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  typeface. If proc returns true, then we return that typeface (this
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  ref()s the typeface). If it never returns true, we return NULL.
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
570a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    SkTypeface* findByProcAndRef(FindProc proc, void* ctx) const;
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  This will unref all of the typefaces in the cache for which the cache
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  is the only owner. Normally this is handled automatically as needed.
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  This function is exposed for clients that explicitly want to purge the
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  cache (e.g. to look for leaks).
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
650a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    void purgeAll();
660a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger
670a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    /**
680a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger     *  Helper: returns a unique fontID to pass to the constructor of
690a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger     *  your subclass of SkTypeface
700a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger     */
710a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    static SkFontID NewFontID();
720a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger
730a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    // These are static wrappers around a global instance of a cache.
740a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger
750a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    static void Add(SkTypeface*,
760a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                    SkTypeface::Style requested,
770a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                    bool strong = true);
780a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    static SkTypeface* FindByID(SkFontID fontID);
790a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    static SkTypeface* FindByProcAndRef(FindProc proc, void* ctx);
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static void PurgeAll();
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    /**
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     *  Debugging only: dumps the status of the typefaces in the cache
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru     */
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static void Dump();
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    static SkTypefaceCache& Get();
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void purge(int count);
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    struct Rec {
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkTypeface*         fFace;
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        bool                fStrong;
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkTypeface::Style   fRequestedStyle;
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    };
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkTDArray<Rec> fArray;
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
101