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 "include/core/SkStream.h"
12#include "include/core/SkString.h"
13#include "include/core/SkTypeface.h"
14#include "include/private/SkFixed.h"
15#include "include/private/SkNoncopyable.h"
16#include "include/private/SkTemplates.h"
17
18class SkFontData {
19public:
20 /** Makes a copy of the data in 'axis'. */
21 SkFontData(std::unique_ptr<SkStreamAsset> stream, int index, const SkFixed* axis, int axisCount)
22 : fStream(std::move(stream)), fIndex(index), fAxisCount(axisCount), fAxis(axisCount)
23 {
24 for (int i = 0; i < axisCount; ++i) {
25 fAxis[i] = axis[i];
26 }
27 }
28 SkFontData(std::unique_ptr<SkStreamAsset> stream, SkFontArguments args)
29 : fStream(std::move(stream)), fIndex(args.getCollectionIndex())
30 , fAxisCount(args.getVariationDesignPosition().coordinateCount)
31 , fAxis(args.getVariationDesignPosition().coordinateCount)
32 {
33 for (int i = 0; i < fAxisCount; ++i) {
34 fAxis[i] = SkFloatToFixed(args.getVariationDesignPosition().coordinates[i].value);
35 }
36 }
37 SkFontData(const SkFontData& that)
38 : fStream(that.fStream->duplicate())
39 , fIndex(that.fIndex)
40 , fAxisCount(that.fAxisCount)
41 , fAxis(fAxisCount)
42 {
43 for (int i = 0; i < fAxisCount; ++i) {
44 fAxis[i] = that.fAxis[i];
45 }
46 }
47 bool hasStream() const { return fStream.get() != nullptr; }
48 std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
49 SkStreamAsset* getStream() { return fStream.get(); }
50 SkStreamAsset const* getStream() const { return fStream.get(); }
51 int getIndex() const { return fIndex; }
52 int getAxisCount() const { return fAxisCount; }
53 const SkFixed* getAxis() const { return fAxis.get(); }
54
55private:
56 std::unique_ptr<SkStreamAsset> fStream;
57 int fIndex;
58 int fAxisCount;
59 SkAutoSTMalloc<4, SkFixed> fAxis;
60};
61
62class SkFontDescriptor : SkNoncopyable {
63public:
64 SkFontDescriptor();
65 // Does not affect ownership of SkStream.
66 static bool Deserialize(SkStream*, SkFontDescriptor* result);
67
68 void serialize(SkWStream*) const;
69
70 SkFontStyle getStyle() const { return fStyle; }
71 void setStyle(SkFontStyle style) { fStyle = style; }
72
73 const char* getFamilyName() const { return fFamilyName.c_str(); }
74 const char* getFullName() const { return fFullName.c_str(); }
75 const char* getPostscriptName() const { return fPostscriptName.c_str(); }
76
77 void setFamilyName(const char* name) { fFamilyName.set(name); }
78 void setFullName(const char* name) { fFullName.set(name); }
79 void setPostscriptName(const char* name) { fPostscriptName.set(name); }
80
81 bool hasStream() const { return bool(fStream); }
82 std::unique_ptr<SkStreamAsset> dupStream() const { return fStream->duplicate(); }
83 int getCollectionIndex() const { return fCollectionIndex; }
84 int getVariationCoordinateCount() const { return fCoordinateCount; }
85 const SkFontArguments::VariationPosition::Coordinate* getVariation() const {
86 return fVariation.get();
87 }
88
89 std::unique_ptr<SkStreamAsset> detachStream() { return std::move(fStream); }
90 void setStream(std::unique_ptr<SkStreamAsset> stream) { fStream = std::move(stream); }
91 void setCollectionIndex(int collectionIndex) { fCollectionIndex = collectionIndex; }
92 SkFontArguments::VariationPosition::Coordinate* setVariationCoordinates(int coordinateCount) {
93 fCoordinateCount = coordinateCount;
94 return fVariation.reset(coordinateCount);
95 }
96
97 std::unique_ptr<SkFontData> maybeAsSkFontData();
98
99private:
100 SkString fFamilyName;
101 SkString fFullName;
102 SkString fPostscriptName;
103 SkFontStyle fStyle;
104
105 std::unique_ptr<SkStreamAsset> fStream;
106 int fCollectionIndex = 0;
107 using Coordinates = SkAutoSTMalloc<4, SkFontArguments::VariationPosition::Coordinate>;
108 bool fVariationDataIsOldAndBad = false;
109 int fCoordinateCount = 0;
110 Coordinates fVariation;
111};
112
113#endif // SkFontDescriptor_DEFINED
114