| 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, | |
| 21 kFontIndex = 0xFD, | 20 kFontIndex = 0xFD, |
| 22 kFontFileName = 0xFE, // Remove when MIN_PICTURE_VERSION > 41 | 21 kFontFileName = 0xFE, // Remove when MIN_PICTURE_VERSION > 41 |
| 23 kSentinel = 0xFF, | 22 kSentinel = 0xFF, |
| 24 }; | 23 }; |
| 25 | 24 |
| 26 SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) : fStyle(style) { } | 25 SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) : fFontIndex(0), fSt
yle(style) { } |
| 27 | 26 |
| 28 static void read_string(SkStream* stream, SkString* string) { | 27 static void read_string(SkStream* stream, SkString* string) { |
| 29 const uint32_t length = SkToU32(stream->readPackedUInt()); | 28 const uint32_t length = SkToU32(stream->readPackedUInt()); |
| 30 if (length > 0) { | 29 if (length > 0) { |
| 31 string->resize(length); | 30 string->resize(length); |
| 32 stream->read(string->writable_str(), length); | 31 stream->read(string->writable_str(), length); |
| 33 } | 32 } |
| 34 } | 33 } |
| 35 | 34 |
| 36 // Remove when MIN_PICTURE_VERSION > 41 | 35 // Remove when MIN_PICTURE_VERSION > 41 |
| 37 static void skip_string(SkStream* stream) { | 36 static void skip_string(SkStream* stream) { |
| 38 const uint32_t length = SkToU32(stream->readPackedUInt()); | 37 const uint32_t length = SkToU32(stream->readPackedUInt()); |
| 39 if (length > 0) { | 38 if (length > 0) { |
| 40 stream->skip(length); | 39 stream->skip(length); |
| 41 } | 40 } |
| 42 } | 41 } |
| 43 | 42 |
| 44 static void write_string(SkWStream* stream, const SkString& string, uint32_t id)
{ | 43 static void write_string(SkWStream* stream, const SkString& string, |
| 44 uint32_t id) { |
| 45 if (!string.isEmpty()) { | 45 if (!string.isEmpty()) { |
| 46 stream->writePackedUInt(id); | 46 stream->writePackedUInt(id); |
| 47 stream->writePackedUInt(string.size()); | 47 stream->writePackedUInt(string.size()); |
| 48 stream->write(string.c_str(), string.size()); | 48 stream->write(string.c_str(), string.size()); |
| 49 } | 49 } |
| 50 } | 50 } |
| 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 SkFontDescriptor::SkFontDescriptor(SkStream* stream) : fFontIndex(0) { |
| 62 fStyle = (SkTypeface::Style)stream->readPackedUInt(); | 62 fStyle = (SkTypeface::Style)stream->readPackedUInt(); |
| 63 | 63 |
| 64 SkAutoSTMalloc<4, SkFixed> axis; | |
| 65 size_t axisCount = 0; | |
| 66 size_t index = 0; | |
| 67 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { | 64 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { |
| 68 switch (id) { | 65 switch (id) { |
| 69 case kFontFamilyName: | 66 case kFontFamilyName: |
| 70 read_string(stream, &fFamilyName); | 67 read_string(stream, &fFamilyName); |
| 71 break; | 68 break; |
| 72 case kFullName: | 69 case kFullName: |
| 73 read_string(stream, &fFullName); | 70 read_string(stream, &fFullName); |
| 74 break; | 71 break; |
| 75 case kPostscriptName: | 72 case kPostscriptName: |
| 76 read_string(stream, &fPostscriptName); | 73 read_string(stream, &fPostscriptName); |
| 77 break; | 74 break; |
| 78 case kFontAxes: | |
| 79 axisCount = read_uint(stream); | |
| 80 axis.reset(axisCount); | |
| 81 for (size_t i = 0; i < axisCount; ++i) { | |
| 82 axis[i] = read_uint(stream); | |
| 83 } | |
| 84 break; | |
| 85 case kFontIndex: | 75 case kFontIndex: |
| 86 index = read_uint(stream); | 76 fFontIndex = read_uint(stream); |
| 87 break; | 77 break; |
| 88 case kFontFileName: // Remove when MIN_PICTURE_VERSION > 41 | 78 case kFontFileName: // Remove when MIN_PICTURE_VERSION > 41 |
| 89 skip_string(stream); | 79 skip_string(stream); |
| 90 break; | 80 break; |
| 91 default: | 81 default: |
| 92 SkDEBUGFAIL("Unknown id used by a font descriptor"); | 82 SkDEBUGFAIL("Unknown id used by a font descriptor"); |
| 93 return; | 83 return; |
| 94 } | 84 } |
| 95 } | 85 } |
| 96 | 86 |
| 97 size_t length = stream->readPackedUInt(); | 87 size_t length = stream->readPackedUInt(); |
| 98 if (length > 0) { | 88 if (length > 0) { |
| 99 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); | 89 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); |
| 100 if (stream->read(data->writable_data(), length) == length) { | 90 if (stream->read(data->writable_data(), length) == length) { |
| 101 fFontData.reset(new SkFontData(SkNEW_ARGS(SkMemoryStream, (data)), i
ndex, | 91 fFontData.reset(SkNEW_ARGS(SkMemoryStream, (data))); |
| 102 axis, axisCount)); | |
| 103 } | 92 } |
| 104 } | 93 } |
| 105 } | 94 } |
| 106 | 95 |
| 107 void SkFontDescriptor::serialize(SkWStream* stream) { | 96 void SkFontDescriptor::serialize(SkWStream* stream) { |
| 108 stream->writePackedUInt(fStyle); | 97 stream->writePackedUInt(fStyle); |
| 109 | 98 |
| 110 write_string(stream, fFamilyName, kFontFamilyName); | 99 write_string(stream, fFamilyName, kFontFamilyName); |
| 111 write_string(stream, fFullName, kFullName); | 100 write_string(stream, fFullName, kFullName); |
| 112 write_string(stream, fPostscriptName, kPostscriptName); | 101 write_string(stream, fPostscriptName, kPostscriptName); |
| 113 if (fFontData.get()) { | 102 if (fFontIndex) { |
| 114 if (fFontData->getIndex()) { | 103 write_uint(stream, fFontIndex, kFontIndex); |
| 115 write_uint(stream, fFontData->getIndex(), kFontIndex); | |
| 116 } | |
| 117 if (fFontData->getAxisCount()) { | |
| 118 write_uint(stream, fFontData->getAxisCount(), kFontAxes); | |
| 119 for (int i = 0; i < fFontData->getAxisCount(); ++i) { | |
| 120 stream->writePackedUInt(fFontData->getAxis()[i]); | |
| 121 } | |
| 122 } | |
| 123 } | 104 } |
| 124 | 105 |
| 125 stream->writePackedUInt(kSentinel); | 106 stream->writePackedUInt(kSentinel); |
| 126 | 107 |
| 127 if (fFontData.get() && fFontData->hasStream()) { | 108 if (fFontData) { |
| 128 SkAutoTDelete<SkStreamAsset> fontData(fFontData->detachStream()); | 109 size_t length = fFontData->getLength(); |
| 129 size_t length = fontData->getLength(); | |
| 130 stream->writePackedUInt(length); | 110 stream->writePackedUInt(length); |
| 131 stream->writeStream(fontData, length); | 111 stream->writeStream(fFontData, length); |
| 132 } else { | 112 } else { |
| 133 stream->writePackedUInt(0); | 113 stream->writePackedUInt(0); |
| 134 } | 114 } |
| 135 } | 115 } |
| OLD | NEW |