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 | 11 |
11 enum { | 12 enum { |
12 // these must match the sfnt 'name' enums | 13 // these must match the sfnt 'name' enums |
13 kFontFamilyName = 0x01, | 14 kFontFamilyName = 0x01, |
14 kFullName = 0x04, | 15 kFullName = 0x04, |
15 kPostscriptName = 0x06, | 16 kPostscriptName = 0x06, |
16 | 17 |
17 // 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 |
18 // defines for names in its 'name' table. | 19 // defines for names in its 'name' table. |
| 20 kFontIndex = 0xFD, |
19 kFontFileName = 0xFE, | 21 kFontFileName = 0xFE, |
20 kSentinel = 0xFF, | 22 kSentinel = 0xFF, |
21 }; | 23 }; |
22 | 24 |
23 SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) { | 25 SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) : fFontIndex(0), fSt
yle(style) { } |
24 fStyle = style; | |
25 } | |
26 | 26 |
27 static void read_string(SkStream* stream, SkString* string) { | 27 static void read_string(SkStream* stream, SkString* string) { |
28 const uint32_t length = SkToU32(stream->readPackedUInt()); | 28 const uint32_t length = SkToU32(stream->readPackedUInt()); |
29 if (length > 0) { | 29 if (length > 0) { |
30 string->resize(length); | 30 string->resize(length); |
31 stream->read(string->writable_str(), length); | 31 stream->read(string->writable_str(), length); |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 static void write_string(SkWStream* stream, const SkString& string, | 35 static void write_string(SkWStream* stream, const SkString& string, |
36 uint32_t id) { | 36 uint32_t id) { |
37 if (!string.isEmpty()) { | 37 if (!string.isEmpty()) { |
38 stream->writePackedUInt(id); | 38 stream->writePackedUInt(id); |
39 stream->writePackedUInt(string.size()); | 39 stream->writePackedUInt(string.size()); |
40 stream->write(string.c_str(), string.size()); | 40 stream->write(string.c_str(), string.size()); |
41 } | 41 } |
42 } | 42 } |
43 | 43 |
44 SkFontDescriptor::SkFontDescriptor(SkStream* stream) { | 44 static size_t read_uint(SkStream* stream) { |
| 45 return stream->readPackedUInt(); |
| 46 } |
| 47 |
| 48 static void write_uint(SkWStream* stream, size_t n, uint32_t id) { |
| 49 stream->writePackedUInt(id); |
| 50 stream->writePackedUInt(n); |
| 51 } |
| 52 |
| 53 SkFontDescriptor::SkFontDescriptor(SkStream* stream) : fFontIndex(0) { |
45 fStyle = (SkTypeface::Style)stream->readPackedUInt(); | 54 fStyle = (SkTypeface::Style)stream->readPackedUInt(); |
46 | 55 |
47 for (;;) { | 56 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { |
48 switch (stream->readPackedUInt()) { | 57 switch (id) { |
49 case kFontFamilyName: | 58 case kFontFamilyName: |
50 read_string(stream, &fFamilyName); | 59 read_string(stream, &fFamilyName); |
51 break; | 60 break; |
52 case kFullName: | 61 case kFullName: |
53 read_string(stream, &fFullName); | 62 read_string(stream, &fFullName); |
54 break; | 63 break; |
55 case kPostscriptName: | 64 case kPostscriptName: |
56 read_string(stream, &fPostscriptName); | 65 read_string(stream, &fPostscriptName); |
57 break; | 66 break; |
| 67 case kFontIndex: |
| 68 fFontIndex = read_uint(stream); |
| 69 break; |
58 case kFontFileName: | 70 case kFontFileName: |
59 read_string(stream, &fFontFileName); | 71 read_string(stream, &fFontFileName); |
60 break; | 72 break; |
61 case kSentinel: | |
62 return; | |
63 default: | 73 default: |
64 SkDEBUGFAIL("Unknown id used by a font descriptor"); | 74 SkDEBUGFAIL("Unknown id used by a font descriptor"); |
65 return; | 75 return; |
66 } | 76 } |
67 } | 77 } |
| 78 |
| 79 size_t length = stream->readPackedUInt(); |
| 80 if (length > 0) { |
| 81 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); |
| 82 if (stream->read(data->writable_data(), length) == length) { |
| 83 fFontData.reset(SkNEW_ARGS(SkMemoryStream, (data))); |
| 84 } |
| 85 } |
68 } | 86 } |
69 | 87 |
70 void SkFontDescriptor::serialize(SkWStream* stream) { | 88 void SkFontDescriptor::serialize(SkWStream* stream) { |
71 stream->writePackedUInt(fStyle); | 89 stream->writePackedUInt(fStyle); |
72 | 90 |
73 write_string(stream, fFamilyName, kFontFamilyName); | 91 write_string(stream, fFamilyName, kFontFamilyName); |
74 write_string(stream, fFullName, kFullName); | 92 write_string(stream, fFullName, kFullName); |
75 write_string(stream, fPostscriptName, kPostscriptName); | 93 write_string(stream, fPostscriptName, kPostscriptName); |
76 write_string(stream, fFontFileName, kFontFileName); | 94 write_string(stream, fFontFileName, kFontFileName); |
| 95 if (fFontIndex) { |
| 96 write_uint(stream, fFontIndex, kFontIndex); |
| 97 } |
77 | 98 |
78 stream->writePackedUInt(kSentinel); | 99 stream->writePackedUInt(kSentinel); |
| 100 |
| 101 if (fFontData) { |
| 102 size_t length = fFontData->getLength(); |
| 103 stream->writePackedUInt(length); |
| 104 stream->writeStream(fFontData, length); |
| 105 } else { |
| 106 stream->writePackedUInt(0); |
| 107 } |
79 } | 108 } |
OLD | NEW |