SkFontMgr.h revision 95625dbcb6b2960732bc27ad0dd0fc4adbb4f7c0
1/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkFontMgr_DEFINED
9#define SkFontMgr_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkFontStyle.h"
13
14class SkData;
15class SkStream;
16class SkString;
17
18class SkFontStyleSet : public SkRefCnt {
19public:
20    int count() const;
21    void getStyle(int index, SkFontStyle*) const;
22    SkTypeface* createTypeface(int index) const;
23};
24
25class SkFontMgr : public SkRefCnt {
26public:
27    int countFamilies();
28    void getFamilyName(int index, SkString* familyName);
29    SkFontStyleSet* createStyleSet(int index);
30
31    /**
32     *  Find the closest matching typeface to the specified familyName and style
33     *  and return a ref to it. The caller must call unref() on the returned
34     *  object. Will never return NULL, as it will return the default font if
35     *  no matching font is found.
36     */
37    SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&);
38
39    SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&);
40
41    /**
42     *  Create a typeface for the specified data and TTC index (pass 0 for none)
43     *  or NULL if the data is not recognized. The caller must call unref() on
44     *  the returned object if it is not null.
45     */
46    SkTypeface* createFromData(SkData*, int ttcIndex = 0);
47
48    /**
49     *  Create a typeface for the specified stream and TTC index
50     *  (pass 0 for none) or NULL if the stream is not recognized. The caller
51     *  must call unref() on the returned object if it is not null.
52     */
53    SkTypeface* createFromStream(SkStream*, int ttcIndex = 0);
54
55    /**
56     *  Create a typeface for the specified fileName and TTC index
57     *  (pass 0 for none) or NULL if the file is not found, or its contents are
58     *  not recognized. The caller must call unref() on the returned object
59     *  if it is not null.
60     */
61    SkTypeface* createFromFile(const char path[], int ttcIndex = 0);
62
63    /**
64     *  Return a ref to the default fontmgr. The caller must call unref() on
65     *  the returned object.
66     */
67    static SkFontMgr* RefDefault();
68
69protected:
70    virtual int onCountFamilies() = 0;
71    virtual void onGetFamilyName(int index, SkString* familyName) = 0;
72    virtual SkFontStyleSet* onCreateStyleSet(int index) = 0;
73
74    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
75                                           const SkFontStyle&) = 0;
76    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
77                                         const SkFontStyle&) = 0;
78
79    virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) = 0;
80    virtual SkTypeface* onCreateFromStream(SkStream*, int ttcIndex) = 0;
81    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) = 0;
82
83private:
84    static SkFontMgr* Factory();    // implemented by porting layer
85    static SkMutex* Mutex();        // implemented by porting layer
86
87    typedef SkRefCnt INHERITED;
88};
89
90#endif
91