OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFontDescriptor.h" | 8 #include "SkFontDescriptor.h" |
9 #include "SkStream.h" | 9 #include "SkStream.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
11 | 11 |
12 enum { | 12 enum { |
13 // these must match the sfnt 'name' enums | 13 // these must match the sfnt 'name' enums |
14 kFontFamilyName = 0x01, | 14 kFontFamilyName = 0x01, |
15 kFullName = 0x04, | 15 kFullName = 0x04, |
16 kPostscriptName = 0x06, | 16 kPostscriptName = 0x06, |
17 | 17 |
18 // These count backwards from 0xFF, so as not to collide with the SFNT | 18 // These count backwards from 0xFF, so as not to collide with the SFNT |
19 // defines for names in its 'name' table. | 19 // defines for names in its 'name' table. |
20 kFontAxes = 0xFC, | 20 kFontAxes = 0xFC, |
21 kFontIndex = 0xFD, | 21 kFontIndex = 0xFD, |
22 kFontFileName = 0xFE, // Remove when MIN_PICTURE_VERSION > 41 | 22 kFontFileName = 0xFE, // Remove when MIN_PICTURE_VERSION > 41 |
23 kSentinel = 0xFF, | 23 kSentinel = 0xFF, |
24 }; | 24 }; |
25 | 25 |
26 SkFontDescriptor::SkFontDescriptor() { } | 26 SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) : fStyle(style) { } |
27 | 27 |
28 static void read_string(SkStream* stream, SkString* string) { | 28 static void read_string(SkStream* stream, SkString* string) { |
29 const uint32_t length = SkToU32(stream->readPackedUInt()); | 29 const uint32_t length = SkToU32(stream->readPackedUInt()); |
30 if (length > 0) { | 30 if (length > 0) { |
31 string->resize(length); | 31 string->resize(length); |
32 stream->read(string->writable_str(), length); | 32 stream->read(string->writable_str(), length); |
33 } | 33 } |
34 } | 34 } |
35 | 35 |
36 // Remove when MIN_PICTURE_VERSION > 41 | 36 // Remove when MIN_PICTURE_VERSION > 41 |
(...skipping 15 matching lines...) Expand all Loading... |
52 static size_t read_uint(SkStream* stream) { | 52 static size_t read_uint(SkStream* stream) { |
53 return stream->readPackedUInt(); | 53 return stream->readPackedUInt(); |
54 } | 54 } |
55 | 55 |
56 static void write_uint(SkWStream* stream, size_t n, uint32_t id) { | 56 static void write_uint(SkWStream* stream, size_t n, uint32_t id) { |
57 stream->writePackedUInt(id); | 57 stream->writePackedUInt(id); |
58 stream->writePackedUInt(n); | 58 stream->writePackedUInt(n); |
59 } | 59 } |
60 | 60 |
61 bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) { | 61 bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) { |
62 size_t styleBits = stream->readPackedUInt(); | 62 result->fStyle = (SkTypeface::Style)stream->readPackedUInt(); |
63 if (styleBits <= 2) { | |
64 // Remove this branch when MIN_PICTURE_VERSION > 45 | |
65 result->fStyle = SkFontStyle::FromOldStyle(styleBits); | |
66 } else { | |
67 result->fStyle = SkFontStyle((styleBits >> 16) & 0xFFFF, | |
68 (styleBits >> 8 ) & 0xFF, | |
69 static_cast<SkFontStyle::Slant>(styleBits &
0xFF)); | |
70 } | |
71 | 63 |
72 SkAutoSTMalloc<4, SkFixed> axis; | 64 SkAutoSTMalloc<4, SkFixed> axis; |
73 size_t axisCount = 0; | 65 size_t axisCount = 0; |
74 size_t index = 0; | 66 size_t index = 0; |
75 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { | 67 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { |
76 switch (id) { | 68 switch (id) { |
77 case kFontFamilyName: | 69 case kFontFamilyName: |
78 read_string(stream, &result->fFamilyName); | 70 read_string(stream, &result->fFamilyName); |
79 break; | 71 break; |
80 case kFullName: | 72 case kFullName: |
(...skipping 29 matching lines...) Expand all Loading... |
110 index, axis, axisCount)); | 102 index, axis, axisCount)); |
111 } else { | 103 } else { |
112 SkDEBUGFAIL("Could not read font data"); | 104 SkDEBUGFAIL("Could not read font data"); |
113 return false; | 105 return false; |
114 } | 106 } |
115 } | 107 } |
116 return true; | 108 return true; |
117 } | 109 } |
118 | 110 |
119 void SkFontDescriptor::serialize(SkWStream* stream) { | 111 void SkFontDescriptor::serialize(SkWStream* stream) { |
120 uint32_t styleBits = (fStyle.weight() << 16) | (fStyle.width() << 8) | (fSty
le.slant()); | 112 stream->writePackedUInt(fStyle); |
121 stream->writePackedUInt(styleBits); | |
122 | 113 |
123 write_string(stream, fFamilyName, kFontFamilyName); | 114 write_string(stream, fFamilyName, kFontFamilyName); |
124 write_string(stream, fFullName, kFullName); | 115 write_string(stream, fFullName, kFullName); |
125 write_string(stream, fPostscriptName, kPostscriptName); | 116 write_string(stream, fPostscriptName, kPostscriptName); |
126 if (fFontData.get()) { | 117 if (fFontData.get()) { |
127 if (fFontData->getIndex()) { | 118 if (fFontData->getIndex()) { |
128 write_uint(stream, fFontData->getIndex(), kFontIndex); | 119 write_uint(stream, fFontData->getIndex(), kFontIndex); |
129 } | 120 } |
130 if (fFontData->getAxisCount()) { | 121 if (fFontData->getAxisCount()) { |
131 write_uint(stream, fFontData->getAxisCount(), kFontAxes); | 122 write_uint(stream, fFontData->getAxisCount(), kFontAxes); |
132 for (int i = 0; i < fFontData->getAxisCount(); ++i) { | 123 for (int i = 0; i < fFontData->getAxisCount(); ++i) { |
133 stream->writePackedUInt(fFontData->getAxis()[i]); | 124 stream->writePackedUInt(fFontData->getAxis()[i]); |
134 } | 125 } |
135 } | 126 } |
136 } | 127 } |
137 | 128 |
138 stream->writePackedUInt(kSentinel); | 129 stream->writePackedUInt(kSentinel); |
139 | 130 |
140 if (fFontData.get() && fFontData->hasStream()) { | 131 if (fFontData.get() && fFontData->hasStream()) { |
141 SkAutoTDelete<SkStreamAsset> fontData(fFontData->detachStream()); | 132 SkAutoTDelete<SkStreamAsset> fontData(fFontData->detachStream()); |
142 size_t length = fontData->getLength(); | 133 size_t length = fontData->getLength(); |
143 stream->writePackedUInt(length); | 134 stream->writePackedUInt(length); |
144 stream->writeStream(fontData, length); | 135 stream->writeStream(fontData, length); |
145 } else { | 136 } else { |
146 stream->writePackedUInt(0); | 137 stream->writePackedUInt(0); |
147 } | 138 } |
148 } | 139 } |
OLD | NEW |