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 "SkFontStyle.h"
12#include "SkRefCnt.h"
13#include "SkTypeface.h"
14
15class SkFontMgr;
16
17/**
18 *  \class SkFontConfigInterface
19 *
20 *  A simple interface for remotable font management.
21 *  The global instance can be found with RefGlobal().
22 */
23class SK_API SkFontConfigInterface : public SkRefCnt {
24public:
25
26    /**
27     *  Returns the global SkFontConfigInterface instance. If it is not
28     *  nullptr, calls ref() on it. The caller must balance this with a call to
29     *  unref(). The default SkFontConfigInterface is the result of calling
30     *  GetSingletonDirectInterface.
31     */
32    static SkFontConfigInterface* RefGlobal();
33
34    /**
35     *  Replace the current global instance with the specified one, safely
36     *  ref'ing the new instance, and unref'ing the previous. Returns its
37     *  parameter (the new global instance).
38     */
39    static SkFontConfigInterface* SetGlobal(SkFontConfigInterface*);
40
41    /**
42     *  This should be treated as private to the impl of SkFontConfigInterface.
43     *  Callers should not change or expect any particular values. It is meant
44     *  to be a union of possible storage types to aid the impl.
45     */
46    struct FontIdentity {
47        FontIdentity() : fID(0), fTTCIndex(0) {}
48
49        bool operator==(const FontIdentity& other) const {
50            return fID == other.fID &&
51                   fTTCIndex == other.fTTCIndex &&
52                   fString == other.fString;
53        }
54        bool operator!=(const FontIdentity& other) const {
55            return !(*this == other);
56        }
57
58        uint32_t    fID;
59        int32_t     fTTCIndex;
60        SkString    fString;
61        SkFontStyle fStyle;
62
63        // If buffer is NULL, just return the number of bytes that would have
64        // been written. Will pad contents to a multiple of 4.
65        size_t writeToMemory(void* buffer = nullptr) const;
66
67        // Recreate from a flattened buffer, returning the number of bytes read.
68        size_t readFromMemory(const void* buffer, size_t length);
69    };
70
71    /**
72     *  Given a familyName and style, find the best match.
73     *
74     *  If a match is found, return true and set its outFontIdentifier.
75     *      If outFamilyName is not null, assign the found familyName to it
76     *          (which may differ from the requested familyName).
77     *      If outStyle is not null, assign the found style to it
78     *          (which may differ from the requested style).
79     *
80     *  If a match is not found, return false, and ignore all out parameters.
81     */
82    virtual bool matchFamilyName(const char familyName[],
83                                 SkFontStyle requested,
84                                 FontIdentity* outFontIdentifier,
85                                 SkString* outFamilyName,
86                                 SkFontStyle* outStyle) = 0;
87
88    /**
89     *  Given a FontRef, open a stream to access its data, or return null
90     *  if the FontRef's data is not available. The caller is responsible for
91     *  deleting the stream when it is done accessing the data.
92     */
93    virtual SkStreamAsset* openStream(const FontIdentity&) = 0;
94
95    /**
96     *  Return an SkTypeface for the given FontIdentity.
97     *
98     *  The default implementation simply returns a new typeface built using data obtained from
99     *  openStream(), but derived classes may implement more complex caching schemes.
100     */
101    virtual sk_sp<SkTypeface> makeTypeface(const FontIdentity& identity) {
102        return SkTypeface::MakeFromStream(this->openStream(identity), identity.fTTCIndex);
103    }
104
105    /**
106     *  Return a singleton instance of a direct subclass that calls into
107     *  libfontconfig. This does not affect the refcnt of the returned instance.
108     */
109    static SkFontConfigInterface* GetSingletonDirectInterface();
110
111    typedef SkRefCnt INHERITED;
112};
113
114#endif
115