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" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
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 SkFontDescriptor::SkFontDescriptor(SkStream* stream) { | 61 bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) { |
62 fStyle = (SkTypeface::Style)stream->readPackedUInt(); | 62 result->fStyle = (SkTypeface::Style)stream->readPackedUInt(); |
63 | 63 |
64 SkAutoSTMalloc<4, SkFixed> axis; | 64 SkAutoSTMalloc<4, SkFixed> axis; |
65 size_t axisCount = 0; | 65 size_t axisCount = 0; |
66 size_t index = 0; | 66 size_t index = 0; |
67 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { | 67 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { |
68 switch (id) { | 68 switch (id) { |
69 case kFontFamilyName: | 69 case kFontFamilyName: |
70 read_string(stream, &fFamilyName); | 70 read_string(stream, &result->fFamilyName); |
71 break; | 71 break; |
72 case kFullName: | 72 case kFullName: |
73 read_string(stream, &fFullName); | 73 read_string(stream, &result->fFullName); |
74 break; | 74 break; |
75 case kPostscriptName: | 75 case kPostscriptName: |
76 read_string(stream, &fPostscriptName); | 76 read_string(stream, &result->fPostscriptName); |
77 break; | 77 break; |
78 case kFontAxes: | 78 case kFontAxes: |
79 axisCount = read_uint(stream); | 79 axisCount = read_uint(stream); |
80 axis.reset(axisCount); | 80 axis.reset(axisCount); |
81 for (size_t i = 0; i < axisCount; ++i) { | 81 for (size_t i = 0; i < axisCount; ++i) { |
82 axis[i] = read_uint(stream); | 82 axis[i] = read_uint(stream); |
83 } | 83 } |
84 break; | 84 break; |
85 case kFontIndex: | 85 case kFontIndex: |
86 index = read_uint(stream); | 86 index = read_uint(stream); |
87 break; | 87 break; |
88 case kFontFileName: // Remove when MIN_PICTURE_VERSION > 41 | 88 case kFontFileName: // Remove when MIN_PICTURE_VERSION > 41 |
89 skip_string(stream); | 89 skip_string(stream); |
90 break; | 90 break; |
91 default: | 91 default: |
92 SkDEBUGFAIL("Unknown id used by a font descriptor"); | 92 SkDEBUGFAIL("Unknown id used by a font descriptor"); |
93 return; | 93 return false; |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 size_t length = stream->readPackedUInt(); | 97 size_t length = stream->readPackedUInt(); |
98 if (length > 0) { | 98 if (length > 0) { |
99 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); | 99 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); |
100 if (stream->read(data->writable_data(), length) == length) { | 100 if (stream->read(data->writable_data(), length) == length) { |
101 fFontData.reset(new SkFontData(new SkMemoryStream(data), index, axis
, axisCount)); | 101 result->fFontData.reset(new SkFontData(new SkMemoryStream(data), |
| 102 index, axis, axisCount)); |
| 103 } else { |
| 104 SkDEBUGFAIL("Could not read font data"); |
| 105 return false; |
102 } | 106 } |
103 } | 107 } |
| 108 return true; |
104 } | 109 } |
105 | 110 |
106 void SkFontDescriptor::serialize(SkWStream* stream) { | 111 void SkFontDescriptor::serialize(SkWStream* stream) { |
107 stream->writePackedUInt(fStyle); | 112 stream->writePackedUInt(fStyle); |
108 | 113 |
109 write_string(stream, fFamilyName, kFontFamilyName); | 114 write_string(stream, fFamilyName, kFontFamilyName); |
110 write_string(stream, fFullName, kFullName); | 115 write_string(stream, fFullName, kFullName); |
111 write_string(stream, fPostscriptName, kPostscriptName); | 116 write_string(stream, fPostscriptName, kPostscriptName); |
112 if (fFontData.get()) { | 117 if (fFontData.get()) { |
113 if (fFontData->getIndex()) { | 118 if (fFontData->getIndex()) { |
(...skipping 11 matching lines...) Expand all Loading... |
125 | 130 |
126 if (fFontData.get() && fFontData->hasStream()) { | 131 if (fFontData.get() && fFontData->hasStream()) { |
127 SkAutoTDelete<SkStreamAsset> fontData(fFontData->detachStream()); | 132 SkAutoTDelete<SkStreamAsset> fontData(fFontData->detachStream()); |
128 size_t length = fontData->getLength(); | 133 size_t length = fontData->getLength(); |
129 stream->writePackedUInt(length); | 134 stream->writePackedUInt(length); |
130 stream->writeStream(fontData, length); | 135 stream->writeStream(fontData, length); |
131 } else { | 136 } else { |
132 stream->writePackedUInt(0); | 137 stream->writePackedUInt(0); |
133 } | 138 } |
134 } | 139 } |
OLD | NEW |