blob: cfdc57157013fa1c63eb71c9d196db5c023b3485 [file] [log] [blame]
djsollen@google.com97145162012-05-31 19:55:08 +00001/*
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
djsollen@google.com97145162012-05-31 19:55:08 +00008#ifndef SkFontDescriptor_DEFINED
9#define SkFontDescriptor_DEFINED
10
bungemand71b7572014-09-18 10:55:32 -070011#include "SkStream.h"
djsollen@google.com97145162012-05-31 19:55:08 +000012#include "SkString.h"
13#include "SkTypeface.h"
14
bungeman41868fe2015-05-20 09:21:04 -070015class SkFontData {
16public:
17 /** This takes ownership of 'stream'. Makes a copy of the data in 'axis'. */
18 SkFontData(SkStreamAsset* stream, int index, const SkFixed axis[], int axisCount)
19 : fStream(stream), fIndex(index), fAxisCount(axisCount), fAxis(axisCount)
20 {
21 for (int i = 0; i < axisCount; ++i) {
22 fAxis[i] = axis[i];
23 }
24 }
25 SkFontData(const SkFontData& that)
26 : fStream(that.fStream->duplicate())
27 , fIndex(that.fIndex)
28 , fAxisCount(that.fAxisCount)
29 , fAxis(fAxisCount)
30 {
31 for (int i = 0; i < fAxisCount; ++i) {
32 fAxis[i] = that.fAxis[i];
33 }
34 }
halcanary96fcdcc2015-08-27 07:41:13 -070035 bool hasStream() const { return fStream.get() != nullptr; }
bungeman41868fe2015-05-20 09:21:04 -070036 SkStreamAsset* duplicateStream() const { return fStream->duplicate(); }
mtklein18300a32016-03-16 13:53:35 -070037 SkStreamAsset* detachStream() { return fStream.release(); }
bungeman41868fe2015-05-20 09:21:04 -070038 SkStreamAsset* getStream() { return fStream.get(); }
39 int getIndex() const { return fIndex; }
40 int getAxisCount() const { return fAxisCount; }
41 const SkFixed* getAxis() const { return fAxis.get(); }
42
43private:
44 SkAutoTDelete<SkStreamAsset> fStream;
45 int fIndex;
46 int fAxisCount;
47 SkAutoSTMalloc<4, SkFixed> fAxis;
48};
49
50class SkFontDescriptor : SkNoncopyable {
djsollen@google.com97145162012-05-31 19:55:08 +000051public:
reed@google.com3b700f62012-05-31 21:16:48 +000052 SkFontDescriptor(SkTypeface::Style = SkTypeface::kNormal);
scroggoa1193e42015-01-21 12:09:53 -080053 // Does not affect ownership of SkStream.
robertphillips3552ba12016-02-25 10:58:49 -080054 static bool Deserialize(SkStream*, SkFontDescriptor* result);
djsollen@google.com97145162012-05-31 19:55:08 +000055
56 void serialize(SkWStream*);
57
reed@google.com3b700f62012-05-31 21:16:48 +000058 SkTypeface::Style getStyle() { return fStyle; }
59 void setStyle(SkTypeface::Style style) { fStyle = style; }
60
bungemand71b7572014-09-18 10:55:32 -070061 const char* getFamilyName() const { return fFamilyName.c_str(); }
62 const char* getFullName() const { return fFullName.c_str(); }
63 const char* getPostscriptName() const { return fPostscriptName.c_str(); }
halcanary96fcdcc2015-08-27 07:41:13 -070064 bool hasFontData() const { return fFontData.get() != nullptr; }
mtklein18300a32016-03-16 13:53:35 -070065 SkFontData* detachFontData() { return fFontData.release(); }
rmistry@google.comd6176b02012-08-23 18:14:13 +000066
reed@google.com3b700f62012-05-31 21:16:48 +000067 void setFamilyName(const char* name) { fFamilyName.set(name); }
68 void setFullName(const char* name) { fFullName.set(name); }
69 void setPostscriptName(const char* name) { fPostscriptName.set(name); }
bungemand71b7572014-09-18 10:55:32 -070070 /** Set the font data only if it is necessary for serialization.
bungeman41868fe2015-05-20 09:21:04 -070071 * This method takes ownership of the font data. */
72 void setFontData(SkFontData* data) { fFontData.reset(data); }
djsollen@google.com97145162012-05-31 19:55:08 +000073
djsollen@google.com97145162012-05-31 19:55:08 +000074private:
reed@google.com3b700f62012-05-31 21:16:48 +000075 SkString fFamilyName;
76 SkString fFullName;
77 SkString fPostscriptName;
bungeman41868fe2015-05-20 09:21:04 -070078 SkAutoTDelete<SkFontData> fFontData;
reed@google.com3b700f62012-05-31 21:16:48 +000079
80 SkTypeface::Style fStyle;
djsollen@google.com97145162012-05-31 19:55:08 +000081};
82
83#endif // SkFontDescriptor_DEFINED