blob: b2622d930007849b69782e003f5bec43d3c52d0d [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.
bungemand71b7572014-09-18 10:55:32 -070020 kFontIndex = 0xFD,
reed@google.com3b700f62012-05-31 21:16:48 +000021 kFontFileName = 0xFE,
22 kSentinel = 0xFF,
djsollen@google.com97145162012-05-31 19:55:08 +000023};
24
bungemand71b7572014-09-18 10:55:32 -070025SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) : fFontIndex(0), fStyle(style) { }
djsollen@google.com97145162012-05-31 19:55:08 +000026
reed@google.com3b700f62012-05-31 21:16:48 +000027static void read_string(SkStream* stream, SkString* string) {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +000028 const uint32_t length = SkToU32(stream->readPackedUInt());
djsollen@google.com97145162012-05-31 19:55:08 +000029 if (length > 0) {
reed@google.com3b700f62012-05-31 21:16:48 +000030 string->resize(length);
31 stream->read(string->writable_str(), length);
djsollen@google.com97145162012-05-31 19:55:08 +000032 }
djsollen@google.com97145162012-05-31 19:55:08 +000033}
34
reed@google.com3b700f62012-05-31 21:16:48 +000035static void write_string(SkWStream* stream, const SkString& string,
36 uint32_t id) {
djsollen@google.com97145162012-05-31 19:55:08 +000037 if (!string.isEmpty()) {
38 stream->writePackedUInt(id);
39 stream->writePackedUInt(string.size());
40 stream->write(string.c_str(), string.size());
41 }
42}
43
bungemand71b7572014-09-18 10:55:32 -070044static size_t read_uint(SkStream* stream) {
45 return stream->readPackedUInt();
46}
47
48static void write_uint(SkWStream* stream, size_t n, uint32_t id) {
49 stream->writePackedUInt(id);
50 stream->writePackedUInt(n);
51}
52
53SkFontDescriptor::SkFontDescriptor(SkStream* stream) : fFontIndex(0) {
reed@google.com3b700f62012-05-31 21:16:48 +000054 fStyle = (SkTypeface::Style)stream->readPackedUInt();
djsollen@google.com97145162012-05-31 19:55:08 +000055
bungemand71b7572014-09-18 10:55:32 -070056 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) {
57 switch (id) {
reed@google.com3b700f62012-05-31 21:16:48 +000058 case kFontFamilyName:
59 read_string(stream, &fFamilyName);
60 break;
61 case kFullName:
62 read_string(stream, &fFullName);
63 break;
64 case kPostscriptName:
65 read_string(stream, &fPostscriptName);
66 break;
bungemand71b7572014-09-18 10:55:32 -070067 case kFontIndex:
68 fFontIndex = read_uint(stream);
69 break;
reed@google.com3b700f62012-05-31 21:16:48 +000070 case kFontFileName:
71 read_string(stream, &fFontFileName);
72 break;
djsollen@google.com97145162012-05-31 19:55:08 +000073 default:
74 SkDEBUGFAIL("Unknown id used by a font descriptor");
reed@google.com3b700f62012-05-31 21:16:48 +000075 return;
djsollen@google.com97145162012-05-31 19:55:08 +000076 }
djsollen@google.com97145162012-05-31 19:55:08 +000077 }
bungemand71b7572014-09-18 10:55:32 -070078
79 size_t length = stream->readPackedUInt();
80 if (length > 0) {
81 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length));
82 if (stream->read(data->writable_data(), length) == length) {
83 fFontData.reset(SkNEW_ARGS(SkMemoryStream, (data)));
84 }
85 }
djsollen@google.com97145162012-05-31 19:55:08 +000086}
87
88void SkFontDescriptor::serialize(SkWStream* stream) {
reed@google.com3b700f62012-05-31 21:16:48 +000089 stream->writePackedUInt(fStyle);
djsollen@google.com97145162012-05-31 19:55:08 +000090
reed@google.com3b700f62012-05-31 21:16:48 +000091 write_string(stream, fFamilyName, kFontFamilyName);
92 write_string(stream, fFullName, kFullName);
93 write_string(stream, fPostscriptName, kPostscriptName);
94 write_string(stream, fFontFileName, kFontFileName);
bungemand71b7572014-09-18 10:55:32 -070095 if (fFontIndex) {
96 write_uint(stream, fFontIndex, kFontIndex);
97 }
djsollen@google.com97145162012-05-31 19:55:08 +000098
99 stream->writePackedUInt(kSentinel);
bungemand71b7572014-09-18 10:55:32 -0700100
101 if (fFontData) {
102 size_t length = fFontData->getLength();
103 stream->writePackedUInt(length);
104 stream->writeStream(fFontData, length);
105 } else {
106 stream->writePackedUInt(0);
107 }
djsollen@google.com97145162012-05-31 19:55:08 +0000108}