Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: src/core/SkFontDescriptor.cpp

Issue 1027373002: Font variations. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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, 22 kFontFileName = 0xFE,
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 static void write_string(SkWStream* stream, const SkString& string, 36 static void write_string(SkWStream* stream, const SkString& string, uint32_t id) {
36 uint32_t id) {
37 if (!string.isEmpty()) { 37 if (!string.isEmpty()) {
38 stream->writePackedUInt(id); 38 stream->writePackedUInt(id);
39 stream->writePackedUInt(string.size()); 39 stream->writePackedUInt(string.size());
40 stream->write(string.c_str(), string.size()); 40 stream->write(string.c_str(), string.size());
41 } 41 }
42 } 42 }
43 43
44 static size_t read_uint(SkStream* stream) { 44 static size_t read_uint(SkStream* stream) {
45 return stream->readPackedUInt(); 45 return stream->readPackedUInt();
46 } 46 }
47 47
48 static void write_uint(SkWStream* stream, size_t n, uint32_t id) { 48 static void write_uint(SkWStream* stream, size_t n, uint32_t id) {
49 stream->writePackedUInt(id); 49 stream->writePackedUInt(id);
50 stream->writePackedUInt(n); 50 stream->writePackedUInt(n);
51 } 51 }
52 52
53 SkFontDescriptor::SkFontDescriptor(SkStream* stream) : fFontIndex(0) { 53 SkFontDescriptor::SkFontDescriptor(SkStream* stream) {
54 fStyle = (SkTypeface::Style)stream->readPackedUInt(); 54 fStyle = (SkTypeface::Style)stream->readPackedUInt();
55 55
56 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { 56 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) {
57 switch (id) { 57 switch (id) {
58 case kFontFamilyName: 58 case kFontFamilyName:
59 read_string(stream, &fFamilyName); 59 read_string(stream, &fFamilyName);
60 break; 60 break;
61 case kFullName: 61 case kFullName:
62 read_string(stream, &fFullName); 62 read_string(stream, &fFullName);
63 break; 63 break;
64 case kPostscriptName: 64 case kPostscriptName:
65 read_string(stream, &fPostscriptName); 65 read_string(stream, &fPostscriptName);
66 break; 66 break;
67 case kFontAxes: {
68 size_t count = read_uint(stream);
69 fFontParameters.fAxis.reset(count);
70 for (size_t i = 0; i < count; ++i) {
71 fFontParameters.fAxis[i] = read_uint(stream);
72 }
73 break;
74 }
67 case kFontIndex: 75 case kFontIndex:
68 fFontIndex = read_uint(stream); 76 fFontParameters.fIndex = read_uint(stream);
69 break; 77 break;
70 case kFontFileName: 78 case kFontFileName:
71 read_string(stream, &fFontFileName); 79 read_string(stream, &fFontFileName);
72 break; 80 break;
73 default: 81 default:
74 SkDEBUGFAIL("Unknown id used by a font descriptor"); 82 SkDEBUGFAIL("Unknown id used by a font descriptor");
75 return; 83 return;
76 } 84 }
77 } 85 }
78 86
79 size_t length = stream->readPackedUInt(); 87 size_t length = stream->readPackedUInt();
80 if (length > 0) { 88 if (length > 0) {
81 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); 89 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length));
82 if (stream->read(data->writable_data(), length) == length) { 90 if (stream->read(data->writable_data(), length) == length) {
83 fFontData.reset(SkNEW_ARGS(SkMemoryStream, (data))); 91 fFontData.reset(SkNEW_ARGS(SkMemoryStream, (data)));
84 } 92 }
85 } 93 }
86 } 94 }
87 95
88 void SkFontDescriptor::serialize(SkWStream* stream) { 96 void SkFontDescriptor::serialize(SkWStream* stream) {
89 stream->writePackedUInt(fStyle); 97 stream->writePackedUInt(fStyle);
90 98
91 write_string(stream, fFamilyName, kFontFamilyName); 99 write_string(stream, fFamilyName, kFontFamilyName);
92 write_string(stream, fFullName, kFullName); 100 write_string(stream, fFullName, kFullName);
93 write_string(stream, fPostscriptName, kPostscriptName); 101 write_string(stream, fPostscriptName, kPostscriptName);
94 write_string(stream, fFontFileName, kFontFileName); 102 write_string(stream, fFontFileName, kFontFileName);
95 if (fFontIndex) { 103 if (fFontParameters.fIndex) {
96 write_uint(stream, fFontIndex, kFontIndex); 104 write_uint(stream, fFontParameters.fIndex, kFontIndex);
105 }
106 if (fFontParameters.fAxis.count()) {
107 write_uint(stream, fFontParameters.fAxis.count(), kFontAxes);
108 for (int i = 0; i < fFontParameters.fAxis.count(); ++i) {
109 stream->writePackedUInt(fFontParameters.fAxis[i]);
110 }
97 } 111 }
98 112
99 stream->writePackedUInt(kSentinel); 113 stream->writePackedUInt(kSentinel);
100 114
101 if (fFontData) { 115 if (fFontData) {
102 size_t length = fFontData->getLength(); 116 size_t length = fFontData->getLength();
103 stream->writePackedUInt(length); 117 stream->writePackedUInt(length);
104 stream->writeStream(fFontData, length); 118 stream->writeStream(fFontData, length);
105 } else { 119 } else {
106 stream->writePackedUInt(0); 120 stream->writePackedUInt(0);
107 } 121 }
108 } 122 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698