blob: 85629efa7ec640bf53d5a6d9533b0916c8d3afd7 [file] [log] [blame]
djsollen@google.com97145162012-05-31 19:55:08 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkFontDescriptor.h"
9#include "SkStream.h"
mtkleinac83d622015-02-11 09:24:19 -080010#include "SkData.h"
djsollen@google.com97145162012-05-31 19:55:08 +000011
12enum {
reed@google.com3b700f62012-05-31 21:16:48 +000013 // these must match the sfnt 'name' enums
djsollen@google.com97145162012-05-31 19:55:08 +000014 kFontFamilyName = 0x01,
reed@google.com3b700f62012-05-31 21:16:48 +000015 kFullName = 0x04,
16 kPostscriptName = 0x06,
rmistry@google.comd6176b02012-08-23 18:14:13 +000017
reed@google.com3b700f62012-05-31 21:16:48 +000018 // These count backwards from 0xFF, so as not to collide with the SFNT
19 // defines for names in its 'name' table.
bungeman41868fe2015-05-20 09:21:04 -070020 kFontAxes = 0xFC,
bungemand71b7572014-09-18 10:55:32 -070021 kFontIndex = 0xFD,
bungeman9d911d52015-04-17 11:00:06 -070022 kFontFileName = 0xFE, // Remove when MIN_PICTURE_VERSION > 41
reed@google.com3b700f62012-05-31 21:16:48 +000023 kSentinel = 0xFF,
djsollen@google.com97145162012-05-31 19:55:08 +000024};
25
bungemanb8113782016-07-25 16:54:59 -070026SkFontDescriptor::SkFontDescriptor() { }
djsollen@google.com97145162012-05-31 19:55:08 +000027
reed@google.com3b700f62012-05-31 21:16:48 +000028static void read_string(SkStream* stream, SkString* string) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +000029 const uint32_t length = SkToU32(stream->readPackedUInt());
djsollen@google.com97145162012-05-31 19:55:08 +000030 if (length > 0) {
reed@google.com3b700f62012-05-31 21:16:48 +000031 string->resize(length);
32 stream->read(string->writable_str(), length);
djsollen@google.com97145162012-05-31 19:55:08 +000033 }
djsollen@google.com97145162012-05-31 19:55:08 +000034}
35
bungeman9d911d52015-04-17 11:00:06 -070036// Remove when MIN_PICTURE_VERSION > 41
37static void skip_string(SkStream* stream) {
38 const uint32_t length = SkToU32(stream->readPackedUInt());
39 if (length > 0) {
40 stream->skip(length);
41 }
42}
43
bungeman41868fe2015-05-20 09:21:04 -070044static void write_string(SkWStream* stream, const SkString& string, uint32_t id) {
djsollen@google.com97145162012-05-31 19:55:08 +000045 if (!string.isEmpty()) {
46 stream->writePackedUInt(id);
47 stream->writePackedUInt(string.size());
48 stream->write(string.c_str(), string.size());
49 }
50}
51
bungemand71b7572014-09-18 10:55:32 -070052static size_t read_uint(SkStream* stream) {
53 return stream->readPackedUInt();
54}
55
56static void write_uint(SkWStream* stream, size_t n, uint32_t id) {
57 stream->writePackedUInt(id);
58 stream->writePackedUInt(n);
59}
60
robertphillips3552ba12016-02-25 10:58:49 -080061bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
bungemanb8113782016-07-25 16:54:59 -070062 size_t styleBits = 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 }
djsollen@google.com97145162012-05-31 19:55:08 +000071
bungeman41868fe2015-05-20 09:21:04 -070072 SkAutoSTMalloc<4, SkFixed> axis;
73 size_t axisCount = 0;
74 size_t index = 0;
bungemand71b7572014-09-18 10:55:32 -070075 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) {
76 switch (id) {
reed@google.com3b700f62012-05-31 21:16:48 +000077 case kFontFamilyName:
robertphillips3552ba12016-02-25 10:58:49 -080078 read_string(stream, &result->fFamilyName);
reed@google.com3b700f62012-05-31 21:16:48 +000079 break;
80 case kFullName:
robertphillips3552ba12016-02-25 10:58:49 -080081 read_string(stream, &result->fFullName);
reed@google.com3b700f62012-05-31 21:16:48 +000082 break;
83 case kPostscriptName:
robertphillips3552ba12016-02-25 10:58:49 -080084 read_string(stream, &result->fPostscriptName);
reed@google.com3b700f62012-05-31 21:16:48 +000085 break;
bungeman41868fe2015-05-20 09:21:04 -070086 case kFontAxes:
87 axisCount = read_uint(stream);
88 axis.reset(axisCount);
89 for (size_t i = 0; i < axisCount; ++i) {
90 axis[i] = read_uint(stream);
91 }
92 break;
bungemand71b7572014-09-18 10:55:32 -070093 case kFontIndex:
bungeman41868fe2015-05-20 09:21:04 -070094 index = read_uint(stream);
bungemand71b7572014-09-18 10:55:32 -070095 break;
bungeman9d911d52015-04-17 11:00:06 -070096 case kFontFileName: // Remove when MIN_PICTURE_VERSION > 41
97 skip_string(stream);
reed@google.com3b700f62012-05-31 21:16:48 +000098 break;
djsollen@google.com97145162012-05-31 19:55:08 +000099 default:
100 SkDEBUGFAIL("Unknown id used by a font descriptor");
robertphillips3552ba12016-02-25 10:58:49 -0800101 return false;
djsollen@google.com97145162012-05-31 19:55:08 +0000102 }
djsollen@google.com97145162012-05-31 19:55:08 +0000103 }
bungemand71b7572014-09-18 10:55:32 -0700104
105 size_t length = stream->readPackedUInt();
106 if (length > 0) {
reedfde05112016-03-11 13:02:28 -0800107 sk_sp<SkData> data(SkData::MakeUninitialized(length));
bungemand71b7572014-09-18 10:55:32 -0700108 if (stream->read(data->writable_data(), length) == length) {
robertphillips3552ba12016-02-25 10:58:49 -0800109 result->fFontData.reset(new SkFontData(new SkMemoryStream(data),
110 index, axis, axisCount));
111 } else {
112 SkDEBUGFAIL("Could not read font data");
113 return false;
bungemand71b7572014-09-18 10:55:32 -0700114 }
115 }
robertphillips3552ba12016-02-25 10:58:49 -0800116 return true;
djsollen@google.com97145162012-05-31 19:55:08 +0000117}
118
119void SkFontDescriptor::serialize(SkWStream* stream) {
bungemanb8113782016-07-25 16:54:59 -0700120 uint32_t styleBits = (fStyle.weight() << 16) | (fStyle.width() << 8) | (fStyle.slant());
121 stream->writePackedUInt(styleBits);
djsollen@google.com97145162012-05-31 19:55:08 +0000122
reed@google.com3b700f62012-05-31 21:16:48 +0000123 write_string(stream, fFamilyName, kFontFamilyName);
124 write_string(stream, fFullName, kFullName);
125 write_string(stream, fPostscriptName, kPostscriptName);
bungeman41868fe2015-05-20 09:21:04 -0700126 if (fFontData.get()) {
127 if (fFontData->getIndex()) {
128 write_uint(stream, fFontData->getIndex(), kFontIndex);
129 }
130 if (fFontData->getAxisCount()) {
131 write_uint(stream, fFontData->getAxisCount(), kFontAxes);
132 for (int i = 0; i < fFontData->getAxisCount(); ++i) {
133 stream->writePackedUInt(fFontData->getAxis()[i]);
134 }
135 }
bungemand71b7572014-09-18 10:55:32 -0700136 }
djsollen@google.com97145162012-05-31 19:55:08 +0000137
138 stream->writePackedUInt(kSentinel);
bungemand71b7572014-09-18 10:55:32 -0700139
bungeman41868fe2015-05-20 09:21:04 -0700140 if (fFontData.get() && fFontData->hasStream()) {
141 SkAutoTDelete<SkStreamAsset> fontData(fFontData->detachStream());
142 size_t length = fontData->getLength();
bungemand71b7572014-09-18 10:55:32 -0700143 stream->writePackedUInt(length);
bungeman41868fe2015-05-20 09:21:04 -0700144 stream->writeStream(fontData, length);
bungemand71b7572014-09-18 10:55:32 -0700145 } else {
146 stream->writePackedUInt(0);
147 }
djsollen@google.com97145162012-05-31 19:55:08 +0000148}