1/*
2 * Copyright 2012 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 SkFontDescriptor_DEFINED
9#define SkFontDescriptor_DEFINED
10
11#include "SkFixed.h"
12#include "SkStream.h"
13#include "SkString.h"
14#include "SkTypeface.h"
15
16class SkFontData {
17public:
18    /** Makes a copy of the data in 'axis'. */
19    SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, const SkFixed axis[],int axisCount)
20        : fStream(std::move(stream)), fIndex(index), fAxisCount(axisCount), fAxis(axisCount)
21    {
22        for (int i = 0; i < axisCount; ++i) {
23            fAxis[i] = axis[i];
24        }
25    }
26    SkFontData(const SkFontData& that)
27        : fStream(that.fStream->duplicate())
28        , fIndex(that.fIndex)
29        , fAxisCount(that.fAxisCount)
30        , fAxis(fAxisCount)
31    {
32        for (int i = 0; i < fAxisCount; ++i) {
33            fAxis[i] = that.fAxis[i];
34        }
35    }
36    bool hasStream() const { return fStream.get() != nullptr; }
37    std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
38    SkStreamAsset* getStream() { return fStream.get(); }
39    SkStreamAsset const* getStream() const { return fStream.get(); }
40    int getIndex() const { return fIndex; }
41    int getAxisCount() const { return fAxisCount; }
42    const SkFixed* getAxis() const { return fAxis.get(); }
43
44private:
45    std::unique_ptr<SkStreamAsset> fStream;
46    int fIndex;
47    int fAxisCount;
48    SkAutoSTMalloc<4, SkFixed> fAxis;
49};
50
51class SkFontDescriptor : SkNoncopyable {
52public:
53    SkFontDescriptor();
54    // Does not affect ownership of SkStream.
55    static bool Deserialize(SkStream*, SkFontDescriptor* result);
56
57    void serialize(SkWStream*);
58
59    SkFontStyle getStyle() { return fStyle; }
60    void setStyle(SkFontStyle style) { fStyle = style; }
61
62    const char* getFamilyName() const { return fFamilyName.c_str(); }
63    const char* getFullName() const { return fFullName.c_str(); }
64    const char* getPostscriptName() const { return fPostscriptName.c_str(); }
65    bool hasFontData() const { return fFontData.get() != nullptr; }
66    std::unique_ptr<SkFontData> detachFontData() { return std::move(fFontData); }
67
68    void setFamilyName(const char* name) { fFamilyName.set(name); }
69    void setFullName(const char* name) { fFullName.set(name); }
70    void setPostscriptName(const char* name) { fPostscriptName.set(name); }
71    /** Set the font data only if it is necessary for serialization. */
72    void setFontData(std::unique_ptr<SkFontData> data) { fFontData = std::move(data); }
73
74private:
75    SkString fFamilyName;
76    SkString fFullName;
77    SkString fPostscriptName;
78    std::unique_ptr<SkFontData> fFontData;
79
80    SkFontStyle fStyle;
81};
82
83#endif // SkFontDescriptor_DEFINED
84