SkFontConfigInterface.h revision 027fd204ad09fe3160c7319b1e55c4ae44747947
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 SkFontConfigInterface_DEFINED
9#define SkFontConfigInterface_DEFINED
10
11#include "SkDataTable.h"
12#include "SkFontStyle.h"
13#include "SkRefCnt.h"
14#include "SkTArray.h"
15#include "SkTypeface.h"
16
17/**
18 *  \class SkFontConfigInterface
19 *
20 *  Provides SkFontHost clients with access to fontconfig services. They will
21 *  access the global instance found in RefGlobal().
22 */
23class SK_API SkFontConfigInterface : public SkRefCnt {
24public:
25    /**
26     *  Returns the global SkFontConfigInterface instance, and if it is not
27     *  NULL, calls ref() on it. The caller must balance this with a call to
28     *  unref().
29     */
30    static SkFontConfigInterface* RefGlobal();
31
32    /**
33     *  Replace the current global instance with the specified one, safely
34     *  ref'ing the new instance, and unref'ing the previous. Returns its
35     *  parameter (the new global instance).
36     */
37    static SkFontConfigInterface* SetGlobal(SkFontConfigInterface*);
38
39    /**
40     *  This should be treated as private to the impl of SkFontConfigInterface.
41     *  Callers should not change or expect any particular values. It is meant
42     *  to be a union of possible storage types to aid the impl.
43     */
44    struct FontIdentity {
45        FontIdentity() : fID(0), fTTCIndex(0) {}
46
47        bool operator==(const FontIdentity& other) const {
48            return fID == other.fID &&
49                   fTTCIndex == other.fTTCIndex &&
50                   fString == other.fString;
51        }
52
53        uint32_t    fID;
54        int32_t     fTTCIndex;
55        SkString    fString;
56        SkFontStyle fStyle;
57    };
58
59    /**
60     *  Given a familyName and style, find the best match.
61     *
62     *  If a match is found, return true and set its outFontIdentifier.
63     *      If outFamilyName is not null, assign the found familyName to it
64     *          (which may differ from the requested familyName).
65     *      If outStyle is not null, assign the found style to it
66     *          (which may differ from the requested style).
67     *
68     *  If a match is not found, return false, and ignore all out parameters.
69     */
70    virtual bool matchFamilyName(const char familyName[],
71                                 SkTypeface::Style requested,
72                                 FontIdentity* outFontIdentifier,
73                                 SkString* outFamilyName,
74                                 SkTypeface::Style* outStyle) = 0;
75
76    /**
77     *  Given a FontRef, open a stream to access its data, or return null
78     *  if the FontRef's data is not available. The caller is responsible for
79     *  calling stream->unref() when it is done accessing the data.
80     */
81    virtual SkStream* openStream(const FontIdentity&) = 0;
82
83    /**
84     *  Return a singleton instance of a direct subclass that calls into
85     *  libfontconfig. This does not affect the refcnt of the returned instance.
86     */
87    static SkFontConfigInterface* GetSingletonDirectInterface();
88
89    // New APIS, which have default impls for now (which do nothing)
90
91    virtual SkDataTable* getFamilyNames() { return SkDataTable::NewEmpty(); }
92    virtual bool matchFamilySet(const char inFamilyName[],
93                                SkString* outFamilyName,
94                                SkTArray<FontIdentity>*) {
95        return false;
96    }
97};
98
99#endif
100