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

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

Issue 1027373002: Font variations. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Slightly more palatable. Created 5 years, 8 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, // Deprecated
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 SkAutoSTMalloc<4, SkFixed> axis;
bungeman-skia 2015/04/01 20:38:28 It would be nice to break up the serialization of
57 size_t axisCount = 0;
58 size_t index = 0;
56 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) { 59 for (size_t id; (id = stream->readPackedUInt()) != kSentinel;) {
57 switch (id) { 60 switch (id) {
58 case kFontFamilyName: 61 case kFontFamilyName:
59 read_string(stream, &fFamilyName); 62 read_string(stream, &fFamilyName);
60 break; 63 break;
61 case kFullName: 64 case kFullName:
62 read_string(stream, &fFullName); 65 read_string(stream, &fFullName);
63 break; 66 break;
64 case kPostscriptName: 67 case kPostscriptName:
65 read_string(stream, &fPostscriptName); 68 read_string(stream, &fPostscriptName);
66 break; 69 break;
70 case kFontAxes:
71 axisCount = read_uint(stream);
72 axis.reset(axisCount);
73 for (size_t i = 0; i < axisCount; ++i) {
74 axis[i] = read_uint(stream);
75 }
76 break;
67 case kFontIndex: 77 case kFontIndex:
68 fFontIndex = read_uint(stream); 78 index = read_uint(stream);
69 break; 79 break;
70 case kFontFileName: 80 case kFontFileName: {
71 read_string(stream, &fFontFileName); 81 SkString fontFileName;
82 read_string(stream, &fontFileName);
72 break; 83 break;
84 }
73 default: 85 default:
74 SkDEBUGFAIL("Unknown id used by a font descriptor"); 86 SkDEBUGFAIL("Unknown id used by a font descriptor");
75 return; 87 return;
76 } 88 }
77 } 89 }
78 90
79 size_t length = stream->readPackedUInt(); 91 size_t length = stream->readPackedUInt();
80 if (length > 0) { 92 if (length > 0) {
81 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length)); 93 SkAutoTUnref<SkData> data(SkData::NewUninitialized(length));
82 if (stream->read(data->writable_data(), length) == length) { 94 if (stream->read(data->writable_data(), length) == length) {
83 fFontData.reset(SkNEW_ARGS(SkMemoryStream, (data))); 95 fFontData.reset(new SkFontData(SkNEW_ARGS(SkMemoryStream, (data)), i ndex,
96 axisCount, axis));
84 } 97 }
85 } 98 }
86 } 99 }
87 100
88 void SkFontDescriptor::serialize(SkWStream* stream) { 101 void SkFontDescriptor::serialize(SkWStream* stream) {
89 stream->writePackedUInt(fStyle); 102 stream->writePackedUInt(fStyle);
90 103
91 write_string(stream, fFamilyName, kFontFamilyName); 104 write_string(stream, fFamilyName, kFontFamilyName);
92 write_string(stream, fFullName, kFullName); 105 write_string(stream, fFullName, kFullName);
93 write_string(stream, fPostscriptName, kPostscriptName); 106 write_string(stream, fPostscriptName, kPostscriptName);
94 write_string(stream, fFontFileName, kFontFileName); 107 if (fFontData->getIndex()) {
bungeman-skia 2015/04/01 20:38:28 Might not have a fontData here.
95 if (fFontIndex) { 108 write_uint(stream, fFontData->getIndex(), kFontIndex);
96 write_uint(stream, fFontIndex, kFontIndex); 109 }
110 if (fFontData->getAxisCount()) {
111 write_uint(stream, fFontData->getAxisCount(), kFontAxes);
112 for (int i = 0; i < fFontData->getAxisCount(); ++i) {
113 stream->writePackedUInt(fFontData->getAxis()[i]);
114 }
97 } 115 }
98 116
99 stream->writePackedUInt(kSentinel); 117 stream->writePackedUInt(kSentinel);
100 118
101 if (fFontData) { 119 if (fFontData->hasStream()) {
102 size_t length = fFontData->getLength(); 120 SkAutoTDelete<SkStreamAsset> fontData(fFontData->transferStream());
121 size_t length = fontData->getLength();
103 stream->writePackedUInt(length); 122 stream->writePackedUInt(length);
104 stream->writeStream(fFontData, length); 123 stream->writeStream(fontData, length);
105 } else { 124 } else {
106 stream->writePackedUInt(0); 125 stream->writePackedUInt(0);
107 } 126 }
108 } 127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698