SkFontMgr.h revision aae71baa73a55f91446fcac22f8ccffcbfc6adea
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 SK_API SkFontStyleSet : public SkRefCnt {
19public:
20    virtual int count() = 0;
21    virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
22    virtual SkTypeface* createTypeface(int index) = 0;
23    virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
24
25    static SkFontStyleSet* CreateEmpty();
26};
27
28class SK_API SkFontMgr : public SkRefCnt {
29public:
30    int countFamilies();
31    void getFamilyName(int index, SkString* familyName);
32    SkFontStyleSet* createStyleSet(int index);
33
34    SkFontStyleSet* matchFamily(const char familyName[]);
35
36    /**
37     *  Find the closest matching typeface to the specified familyName and style
38     *  and return a ref to it. The caller must call unref() on the returned
39     *  object. Will never return NULL, as it will return the default font if
40     *  no matching font is found.
41     */
42    SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&);
43
44    SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&);
45
46    /**
47     *  Create a typeface for the specified data and TTC index (pass 0 for none)
48     *  or NULL if the data is not recognized. The caller must call unref() on
49     *  the returned object if it is not null.
50     */
51    SkTypeface* createFromData(SkData*, int ttcIndex = 0);
52
53    /**
54     *  Create a typeface for the specified stream and TTC index
55     *  (pass 0 for none) or NULL if the stream is not recognized. The caller
56     *  must call unref() on the returned object if it is not null.
57     */
58    SkTypeface* createFromStream(SkStream*, int ttcIndex = 0);
59
60    /**
61     *  Create a typeface for the specified fileName and TTC index
62     *  (pass 0 for none) or NULL if the file is not found, or its contents are
63     *  not recognized. The caller must call unref() on the returned object
64     *  if it is not null.
65     */
66    SkTypeface* createFromFile(const char path[], int ttcIndex = 0);
67
68    /**
69     *  Return a ref to the default fontmgr. The caller must call unref() on
70     *  the returned object.
71     */
72    static SkFontMgr* RefDefault();
73
74protected:
75    virtual int onCountFamilies() = 0;
76    virtual void onGetFamilyName(int index, SkString* familyName) = 0;
77    virtual SkFontStyleSet* onCreateStyleSet(int index) = 0;
78
79    virtual SkFontStyleSet* onMatchFamily(const char familyName[]) = 0;
80
81    virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
82                                           const SkFontStyle&) = 0;
83    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
84                                         const SkFontStyle&) = 0;
85
86    virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) = 0;
87    virtual SkTypeface* onCreateFromStream(SkStream*, int ttcIndex) = 0;
88    virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) = 0;
89
90private:
91    static SkFontMgr* Factory();    // implemented by porting layer
92    static SkMutex* Mutex();        // implemented by porting layer
93
94    typedef SkRefCnt INHERITED;
95};
96
97#endif
98