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#include "SkFontConfigInterface.h"
9#include "SkFontHost_FreeType_common.h"
10#include "SkStream.h"
11#include "SkTypefaceCache.h"
12
13class SkFontDescriptor;
14
15class FontConfigTypeface : public SkTypeface_FreeType {
16    SkFontConfigInterface::FontIdentity fIdentity;
17    SkString fFamilyName;
18    SkStream* fLocalStream;
19
20public:
21    static FontConfigTypeface* Create(Style style,
22                                      const SkFontConfigInterface::FontIdentity& fi,
23                                      const SkString& familyName) {
24        return SkNEW_ARGS(FontConfigTypeface, (style, fi, familyName));
25    }
26
27    static FontConfigTypeface* Create(Style style, bool fixedWidth, SkStream* localStream) {
28        return SkNEW_ARGS(FontConfigTypeface, (style, fixedWidth, localStream));
29    }
30
31    virtual ~FontConfigTypeface() {
32        SkSafeUnref(fLocalStream);
33    }
34
35    const SkFontConfigInterface::FontIdentity& getIdentity() const {
36        return fIdentity;
37    }
38
39    const char* getFamilyName() const { return fFamilyName.c_str(); }
40    SkStream*   getLocalStream() const { return fLocalStream; }
41
42    bool isFamilyName(const char* name) const {
43        return fFamilyName.equals(name);
44    }
45
46    static SkTypeface* LegacyCreateTypeface(const SkTypeface* family,
47                                            const char familyName[],
48                                            SkTypeface::Style);
49
50protected:
51    friend class SkFontHost;    // hack until we can make public versions
52
53    FontConfigTypeface(Style style,
54                       const SkFontConfigInterface::FontIdentity& fi,
55                       const SkString& familyName)
56            : INHERITED(style, SkTypefaceCache::NewFontID(), false)
57            , fIdentity(fi)
58            , fFamilyName(familyName)
59            , fLocalStream(NULL) {}
60
61    FontConfigTypeface(Style style, bool fixedWidth, SkStream* localStream)
62            : INHERITED(style, SkTypefaceCache::NewFontID(), fixedWidth) {
63        // we default to empty fFamilyName and fIdentity
64        fLocalStream = localStream;
65        SkSafeRef(localStream);
66    }
67
68    virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE;
69    virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE;
70
71private:
72    typedef SkTypeface_FreeType INHERITED;
73};
74