blob: 73ea2058ce79c618022334b970b6d702035e3f67 [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"
bungemanf93d7112016-09-16 06:24:20 -07009#include "SkMakeUnique.h"
djsollen@google.com97145162012-05-31 19:55:08 +000010#include "SkStream.h"
mtkleinac83d622015-02-11 09:24:19 -080011#include "SkData.h"
djsollen@google.com97145162012-05-31 19:55:08 +000012
13enum {
reed@google.com3b700f62012-05-31 21:16:48 +000014 // these must match the sfnt 'name' enums
djsollen@google.com97145162012-05-31 19:55:08 +000015 kFontFamilyName = 0x01,
reed@google.com3b700f62012-05-31 21:16:48 +000016 kFullName = 0x04,
17 kPostscriptName = 0x06,
rmistry@google.comd6176b02012-08-23 18:14:13 +000018
reed@google.com3b700f62012-05-31 21:16:48 +000019 // These count backwards from 0xFF, so as not to collide with the SFNT
20 // defines for names in its 'name' table.
bungeman41868fe2015-05-20 09:21:04 -070021 kFontAxes = 0xFC,
bungemand71b7572014-09-18 10:55:32 -070022 kFontIndex = 0xFD,
bungeman9d911d52015-04-17 11:00:06 -070023 kFontFileName = 0xFE, // Remove when MIN_PICTURE_VERSION > 41
reed@google.com3b700f62012-05-31 21:16:48 +000024 kSentinel = 0xFF,
djsollen@google.com97145162012-05-31 19:55:08 +000025};
26
bungemanb8113782016-07-25 16:54:59 -070027SkFontDescriptor::SkFontDescriptor() { }
djsollen@google.com97145162012-05-31 19:55:08 +000028
reed@google.com3b700f62012-05-31 21:16:48 +000029static void read_string(SkStream* stream, SkString* string) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +000030 const uint32_t length = SkToU32(stream->readPackedUInt());
djsollen@google.com97145162012-05-31 19:55:08 +000031 if (length > 0) {
reed@google.com3b700f62012-05-31 21:16:48 +000032 string->resize(length);
33 stream->read(string->writable_str(), length);
djsollen@google.com97145162012-05-31 19:55:08 +000034 }
djsollen@google.com97145162012-05-31 19:55:08 +000035}
36
bungeman9d911d52015-04-17 11:00:06 -070037// Remove when MIN_PICTURE_VERSION > 41
38static void skip_string(SkStream* stream) {
39 const uint32_t length = SkToU32(stream->readPackedUInt());
40 if (length > 0) {
41 stream->skip(length);
42 }
43}
44
bungeman41868fe2015-05-20 09:21:04 -070045static void write_string(SkWStream* stream, const SkString& string, uint32_t id) {
djsollen@google.com97145162012-05-31 19:55:08 +000046 if (!string.isEmpty()) {
47 stream->writePackedUInt(id);
48 stream->writePackedUInt(string.size());
49 stream->write(string.c_str(), string.size());
50 }
51}
52
bungemand71b7572014-09-18 10:55:32 -070053static size_t read_uint(SkStream* stream) {
54 return stream->readPackedUInt();
55}
56
57static void write_uint(SkWStream* stream, size_t n, uint32_t id) {
58 stream->writePackedUInt(id);
59 stream->writePackedUInt(n);
60}
61
robertphillips3552ba12016-02-25 10:58:49 -080062bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
bungemanb8113782016-07-25 16:54:59 -070063 size_t styleBits = stream->readPackedUInt();
64 if (styleBits <= 2) {
65 // Remove this branch when MIN_PICTURE_VERSION > 45
66 result->fStyle = SkFontStyle::FromOldStyle(styleBits);
67 } else {
68 result->fStyle = SkFontStyle((styleBits >> 16) & 0xFFFF,
69 (styleBits >> 8 ) & 0xFF,
70 static_cast<SkFontStyle::Slant>(styleBits & 0xFF));
71 }
djsollen@google.com97145162012-05-31 19:55:08 +000072
bungeman41868fe2015-05-20 09:21:04 -070073 SkAutoSTMalloc<4, SkFixed> axis;
74 size_t axisCount = 0;
75 size_t index = 0;
bungemand71b7572014-09-18 10:55:32 -070076 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) {
77 switch (id) {
reed@google.com3b700f62012-05-31 21:16:48 +000078 case kFontFamilyName:
robertphillips3552ba12016-02-25 10:58:49 -080079 read_string(stream, &result->fFamilyName);
reed@google.com3b700f62012-05-31 21:16:48 +000080 break;
81 case kFullName:
robertphillips3552ba12016-02-25 10:58:49 -080082 read_string(stream, &result->fFullName);
reed@google.com3b700f62012-05-31 21:16:48 +000083 break;
84 case kPostscriptName:
robertphillips3552ba12016-02-25 10:58:49 -080085 read_string(stream, &result->fPostscriptName);
reed@google.com3b700f62012-05-31 21:16:48 +000086 break;
bungeman41868fe2015-05-20 09:21:04 -070087 case kFontAxes:
88 axisCount = read_uint(stream);
89 axis.reset(axisCount);
90 for (size_t i = 0; i < axisCount; ++i) {
91 axis[i] = read_uint(stream);
92 }
93 break;
bungemand71b7572014-09-18 10:55:32 -070094 case kFontIndex:
bungeman41868fe2015-05-20 09:21:04 -070095 index = read_uint(stream);
bungemand71b7572014-09-18 10:55:32 -070096 break;
bungeman9d911d52015-04-17 11:00:06 -070097 case kFontFileName: // Remove when MIN_PICTURE_VERSION > 41
98 skip_string(stream);
reed@google.com3b700f62012-05-31 21:16:48 +000099 break;
djsollen@google.com97145162012-05-31 19:55:08 +0000100 default:
101 SkDEBUGFAIL("Unknown id used by a font descriptor");
robertphillips3552ba12016-02-25 10:58:49 -0800102 return false;
djsollen@google.com97145162012-05-31 19:55:08 +0000103 }
djsollen@google.com97145162012-05-31 19:55:08 +0000104 }
bungemand71b7572014-09-18 10:55:32 -0700105
106 size_t length = stream->readPackedUInt();
107 if (length > 0) {
reedfde05112016-03-11 13:02:28 -0800108 sk_sp<SkData> data(SkData::MakeUninitialized(length));
bungemand71b7572014-09-18 10:55:32 -0700109 if (stream->read(data->writable_data(), length) == length) {
bungemanf93d7112016-09-16 06:24:20 -0700110 result->fFontData = skstd::make_unique<SkFontData>(
111 skstd::make_unique<SkMemoryStream>(data), index, axis, axisCount);
robertphillips3552ba12016-02-25 10:58:49 -0800112 } else {
113 SkDEBUGFAIL("Could not read font data");
114 return false;
bungemand71b7572014-09-18 10:55:32 -0700115 }
116 }
robertphillips3552ba12016-02-25 10:58:49 -0800117 return true;
djsollen@google.com97145162012-05-31 19:55:08 +0000118}
119
120void SkFontDescriptor::serialize(SkWStream* stream) {
bungemanb8113782016-07-25 16:54:59 -0700121 uint32_t styleBits = (fStyle.weight() << 16) | (fStyle.width() << 8) | (fStyle.slant());
122 stream->writePackedUInt(styleBits);
djsollen@google.com97145162012-05-31 19:55:08 +0000123
reed@google.com3b700f62012-05-31 21:16:48 +0000124 write_string(stream, fFamilyName, kFontFamilyName);
125 write_string(stream, fFullName, kFullName);
126 write_string(stream, fPostscriptName, kPostscriptName);
bungeman41868fe2015-05-20 09:21:04 -0700127 if (fFontData.get()) {
128 if (fFontData->getIndex()) {
129 write_uint(stream, fFontData->getIndex(), kFontIndex);
130 }
131 if (fFontData->getAxisCount()) {
132 write_uint(stream, fFontData->getAxisCount(), kFontAxes);
133 for (int i = 0; i < fFontData->getAxisCount(); ++i) {
134 stream->writePackedUInt(fFontData->getAxis()[i]);
135 }
136 }
bungemand71b7572014-09-18 10:55:32 -0700137 }
djsollen@google.com97145162012-05-31 19:55:08 +0000138
139 stream->writePackedUInt(kSentinel);
bungemand71b7572014-09-18 10:55:32 -0700140
bungeman41868fe2015-05-20 09:21:04 -0700141 if (fFontData.get() && fFontData->hasStream()) {
bungemanf93d7112016-09-16 06:24:20 -0700142 std::unique_ptr<SkStreamAsset> fontStream = fFontData->detachStream();
143 size_t length = fontStream->getLength();
bungemand71b7572014-09-18 10:55:32 -0700144 stream->writePackedUInt(length);
bungemanf93d7112016-09-16 06:24:20 -0700145 stream->writeStream(fontStream.get(), length);
bungemand71b7572014-09-18 10:55:32 -0700146 } else {
147 stream->writePackedUInt(0);
148 }
djsollen@google.com97145162012-05-31 19:55:08 +0000149}