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