blob: 34f4f17e881d020a0f6bf66ea10779224da84d0c [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"
10
11enum {
reed@google.com3b700f62012-05-31 21:16:48 +000012 // these must match the sfnt 'name' enums
djsollen@google.com97145162012-05-31 19:55:08 +000013 kFontFamilyName = 0x01,
reed@google.com3b700f62012-05-31 21:16:48 +000014 kFullName = 0x04,
15 kPostscriptName = 0x06,
16
17 // These count backwards from 0xFF, so as not to collide with the SFNT
18 // defines for names in its 'name' table.
19 kFontFileName = 0xFE,
20 kSentinel = 0xFF,
djsollen@google.com97145162012-05-31 19:55:08 +000021};
22
reed@google.com3b700f62012-05-31 21:16:48 +000023SkFontDescriptor::SkFontDescriptor(SkTypeface::Style style) {
24 fStyle = style;
djsollen@google.com97145162012-05-31 19:55:08 +000025}
26
reed@google.com3b700f62012-05-31 21:16:48 +000027static void read_string(SkStream* stream, SkString* string) {
djsollen@google.com97145162012-05-31 19:55:08 +000028 const uint32_t length = stream->readPackedUInt();
29 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
44SkFontDescriptor::SkFontDescriptor(SkStream* stream) {
reed@google.com3b700f62012-05-31 21:16:48 +000045 fStyle = (SkTypeface::Style)stream->readPackedUInt();
djsollen@google.com97145162012-05-31 19:55:08 +000046
reed@google.com3b700f62012-05-31 21:16:48 +000047 for (;;) {
48 switch (stream->readPackedUInt()) {
49 case kFontFamilyName:
50 read_string(stream, &fFamilyName);
51 break;
52 case kFullName:
53 read_string(stream, &fFullName);
54 break;
55 case kPostscriptName:
56 read_string(stream, &fPostscriptName);
57 break;
58 case kFontFileName:
59 read_string(stream, &fFontFileName);
60 break;
61 case kSentinel:
62 return;
djsollen@google.com97145162012-05-31 19:55:08 +000063 default:
64 SkDEBUGFAIL("Unknown id used by a font descriptor");
reed@google.com3b700f62012-05-31 21:16:48 +000065 return;
djsollen@google.com97145162012-05-31 19:55:08 +000066 }
djsollen@google.com97145162012-05-31 19:55:08 +000067 }
68}
69
70void SkFontDescriptor::serialize(SkWStream* stream) {
reed@google.com3b700f62012-05-31 21:16:48 +000071 stream->writePackedUInt(fStyle);
djsollen@google.com97145162012-05-31 19:55:08 +000072
reed@google.com3b700f62012-05-31 21:16:48 +000073 write_string(stream, fFamilyName, kFontFamilyName);
74 write_string(stream, fFullName, kFullName);
75 write_string(stream, fPostscriptName, kPostscriptName);
76 write_string(stream, fFontFileName, kFontFileName);
djsollen@google.com97145162012-05-31 19:55:08 +000077
78 stream->writePackedUInt(kSentinel);
79}