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