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