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 #include "SkFontDescriptor.h"
9 #include "SkMakeUnique.h"
10 #include "SkStream.h"
11 #include "SkData.h"
12
13 enum {
14 kInvalid = 0x00,
15
16 // these must match the sfnt 'name' enums
17 kFontFamilyName = 0x01,
18 kFullName = 0x04,
19 kPostscriptName = 0x06,
20
21 // These count backwards from 0xFF, so as not to collide with the SFNT
22 // defines for names in its 'name' table.
23 kFontAxes = 0xFB,
24 kFontAxes_bad = 0xFC, // Broken negative axes, remove when MIN_PICTURE_VERSION > 62.
25 kFontIndex = 0xFD,
26 kSentinel = 0xFF,
27 };
28
SkFontDescriptor()29 SkFontDescriptor::SkFontDescriptor() { }
30
read_string(SkStream * stream,SkString * string)31 static bool SK_WARN_UNUSED_RESULT read_string(SkStream* stream, SkString* string) {
32 size_t length;
33 if (!stream->readPackedUInt(&length)) { return false; }
34 if (length > 0) {
35 string->resize(length);
36 if (stream->read(string->writable_str(), length) != length) { return false; }
37 }
38 return true;
39 }
40
write_string(SkWStream * stream,const SkString & string,uint32_t id)41 static bool write_string(SkWStream* stream, const SkString& string, uint32_t id) {
42 if (string.isEmpty()) { return true; }
43 return stream->writePackedUInt(id) &&
44 stream->writePackedUInt(string.size()) &&
45 stream->write(string.c_str(), string.size());
46 }
47
write_uint(SkWStream * stream,size_t n,uint32_t id)48 static bool write_uint(SkWStream* stream, size_t n, uint32_t id) {
49 return stream->writePackedUInt(id) &&
50 stream->writePackedUInt(n);
51 }
52
read_id(SkStream * stream)53 static size_t SK_WARN_UNUSED_RESULT read_id(SkStream* stream) {
54 size_t i;
55 if (!stream->readPackedUInt(&i)) { return kInvalid; }
56 return i;
57 }
58
Deserialize(SkStream * stream,SkFontDescriptor * result)59 bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
60 size_t styleBits;
61 if (!stream->readPackedUInt(&styleBits)) { return false; }
62 result->fStyle = SkFontStyle((styleBits >> 16) & 0xFFFF,
63 (styleBits >> 8 ) & 0xFF,
64 static_cast<SkFontStyle::Slant>(styleBits & 0xFF));
65
66 SkAutoSTMalloc<4, SkFixed> axis;
67 size_t axisCount = 0;
68 size_t index = 0;
69 for (size_t id; (id = read_id(stream)) != kSentinel;) {
70 switch (id) {
71 case kFontFamilyName:
72 if (!read_string(stream, &result->fFamilyName)) { return false; }
73 break;
74 case kFullName:
75 if (!read_string(stream, &result->fFullName)) { return false; }
76 break;
77 case kPostscriptName:
78 if (!read_string(stream, &result->fPostscriptName)) { return false; }
79 break;
80 case kFontAxes:
81 if (!stream->readPackedUInt(&axisCount)) { return false; }
82 axis.reset(axisCount);
83 for (size_t i = 0; i < axisCount; ++i) {
84 if (!stream->readS32(&axis[i])) { return false; }
85 }
86 break;
87 case kFontAxes_bad:
88 if (!stream->readPackedUInt(&axisCount)) { return false; }
89 axis.reset(axisCount);
90 for (size_t i = 0; i < axisCount; ++i) {
91 size_t packedAxis;
92 if (!stream->readPackedUInt(&packedAxis)) { return false; }
93 axis[i] = packedAxis;
94 }
95 break;
96 case kFontIndex:
97 if (!stream->readPackedUInt(&index)) { return false; }
98 break;
99 default:
100 SkDEBUGFAIL("Unknown id used by a font descriptor");
101 return false;
102 }
103 }
104
105 size_t length;
106 if (!stream->readPackedUInt(&length)) { return false; }
107 if (length > 0) {
108 sk_sp<SkData> data(SkData::MakeUninitialized(length));
109 if (stream->read(data->writable_data(), length) != length) {
110 SkDEBUGFAIL("Could not read font data");
111 return false;
112 }
113 result->fFontData = skstd::make_unique<SkFontData>(
114 SkMemoryStream::Make(std::move(data)), index, axis, axisCount);
115 }
116 return true;
117 }
118
serialize(SkWStream * stream) const119 void SkFontDescriptor::serialize(SkWStream* stream) const {
120 uint32_t styleBits = (fStyle.weight() << 16) | (fStyle.width() << 8) | (fStyle.slant());
121 stream->writePackedUInt(styleBits);
122
123 write_string(stream, fFamilyName, kFontFamilyName);
124 write_string(stream, fFullName, kFullName);
125 write_string(stream, fPostscriptName, kPostscriptName);
126 if (fFontData.get()) {
127 if (fFontData->getIndex()) {
128 write_uint(stream, fFontData->getIndex(), kFontIndex);
129 }
130 if (fFontData->getAxisCount()) {
131 write_uint(stream, fFontData->getAxisCount(), kFontAxes);
132 for (int i = 0; i < fFontData->getAxisCount(); ++i) {
133 stream->write32(fFontData->getAxis()[i]);
134 }
135 }
136 }
137
138 stream->writePackedUInt(kSentinel);
139
140 if (fFontData.get() && fFontData->hasStream()) {
141 std::unique_ptr<SkStreamAsset> fontStream = fFontData->detachStream();
142 size_t length = fontStream->getLength();
143 stream->writePackedUInt(length);
144 stream->writeStream(fontStream.get(), length);
145 } else {
146 stream->writePackedUInt(0);
147 }
148 }
149