• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "include/core/SkData.h"
9 #include "include/core/SkStream.h"
10 #include "src/core/SkFontDescriptor.h"
11 
12 enum {
13     kInvalid        = 0x00,
14 
15     // these must match the sfnt 'name' enums
16     kFontFamilyName = 0x01,
17     kFullName       = 0x04,
18     kPostscriptName = 0x06,
19 
20     // These count backwards from 0xFF, so as not to collide with the SFNT
21     // defines for names in its 'name' table.
22     kFontVariation  = 0xFA,
23     kFontIndex      = 0xFD,
24     kSentinel       = 0xFF,
25 };
26 
SkFontDescriptor()27 SkFontDescriptor::SkFontDescriptor() { }
28 
read_string(SkStream * stream,SkString * string)29 static bool SK_WARN_UNUSED_RESULT read_string(SkStream* stream, SkString* string) {
30     size_t length;
31     if (!stream->readPackedUInt(&length)) { return false; }
32     if (length > 0) {
33         string->resize(length);
34         if (stream->read(string->writable_str(), length) != length) { return false; }
35     }
36     return true;
37 }
38 
write_string(SkWStream * stream,const SkString & string,uint32_t id)39 static bool write_string(SkWStream* stream, const SkString& string, uint32_t id) {
40     if (string.isEmpty()) { return true; }
41     return stream->writePackedUInt(id) &&
42            stream->writePackedUInt(string.size()) &&
43            stream->write(string.c_str(), string.size());
44 }
45 
write_uint(SkWStream * stream,size_t n,uint32_t id)46 static bool write_uint(SkWStream* stream, size_t n, uint32_t id) {
47     return stream->writePackedUInt(id) &&
48            stream->writePackedUInt(n);
49 }
50 
read_id(SkStream * stream)51 static size_t SK_WARN_UNUSED_RESULT read_id(SkStream* stream) {
52     size_t i;
53     if (!stream->readPackedUInt(&i)) { return kInvalid; }
54     return i;
55 }
56 
Deserialize(SkStream * stream,SkFontDescriptor * result)57 bool SkFontDescriptor::Deserialize(SkStream* stream, SkFontDescriptor* result) {
58     size_t styleBits;
59     if (!stream->readPackedUInt(&styleBits)) { return false; }
60     result->fStyle = SkFontStyle((styleBits >> 16) & 0xFFFF,
61                                  (styleBits >> 8 ) & 0xFF,
62                                  static_cast<SkFontStyle::Slant>(styleBits & 0xFF));
63 
64     size_t coordinateCount;
65     using CoordinateCountType = decltype(result->fCoordinateCount);
66 
67     size_t index;
68     using CollectionIndexType = decltype(result->fCollectionIndex);
69 
70     for (size_t id; (id = read_id(stream)) != kSentinel;) {
71         switch (id) {
72             case kFontFamilyName:
73                 if (!read_string(stream, &result->fFamilyName)) { return false; }
74                 break;
75             case kFullName:
76                 if (!read_string(stream, &result->fFullName)) { return false; }
77                 break;
78             case kPostscriptName:
79                 if (!read_string(stream, &result->fPostscriptName)) { return false; }
80                 break;
81             case kFontVariation:
82                 if (!stream->readPackedUInt(&coordinateCount)) { return false; }
83                 if (!SkTFitsIn<CoordinateCountType>(coordinateCount)) { return false; }
84                 result->fCoordinateCount = SkTo<CoordinateCountType>(coordinateCount);
85 
86                 result->fVariation.reset(coordinateCount);
87                 for (size_t i = 0; i < coordinateCount; ++i) {
88                     if (!stream->readU32(&result->fVariation[i].axis)) { return false; }
89                     if (!stream->readScalar(&result->fVariation[i].value)) { return false; }
90                 }
91                 break;
92             case kFontIndex:
93                 if (!stream->readPackedUInt(&index)) { return false; }
94                 if (!SkTFitsIn<CollectionIndexType>(index)) { return false; }
95                 result->fCollectionIndex = SkTo<CollectionIndexType>(index);
96                 break;
97             default:
98                 SkDEBUGFAIL("Unknown id used by a font descriptor");
99                 return false;
100         }
101     }
102 
103     size_t length;
104     if (!stream->readPackedUInt(&length)) { return false; }
105     if (length > 0) {
106         sk_sp<SkData> data(SkData::MakeUninitialized(length));
107         if (stream->read(data->writable_data(), length) != length) {
108             SkDEBUGFAIL("Could not read font data");
109             return false;
110         }
111         result->fStream = SkMemoryStream::Make(std::move(data));
112     }
113     return true;
114 }
115 
serialize(SkWStream * stream) const116 void SkFontDescriptor::serialize(SkWStream* stream) const {
117     uint32_t styleBits = (fStyle.weight() << 16) | (fStyle.width() << 8) | (fStyle.slant());
118     stream->writePackedUInt(styleBits);
119 
120     write_string(stream, fFamilyName, kFontFamilyName);
121     write_string(stream, fFullName, kFullName);
122     write_string(stream, fPostscriptName, kPostscriptName);
123 
124     if (fCollectionIndex) {
125         write_uint(stream, fCollectionIndex, kFontIndex);
126     }
127     if (fCoordinateCount) {
128         write_uint(stream, fCoordinateCount, kFontVariation);
129         for (int i = 0; i < fCoordinateCount; ++i) {
130             stream->write32(fVariation[i].axis);
131             stream->writeScalar(fVariation[i].value);
132         }
133     }
134 
135     stream->writePackedUInt(kSentinel);
136 
137     if (fStream) {
138         std::unique_ptr<SkStreamAsset> fontStream = fStream->duplicate();
139         size_t length = fontStream->getLength();
140         stream->writePackedUInt(length);
141         stream->writeStream(fontStream.get(), length);
142     } else {
143         stream->writePackedUInt(0);
144     }
145 }
146