OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 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 <ctype.h> | 8 #include <ctype.h> |
9 | 9 |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } else if (c <= '9') { | 145 } else if (c <= '9') { |
146 return c - '0'; | 146 return c - '0'; |
147 } else if (c <= 'F') { | 147 } else if (c <= 'F') { |
148 return c - 'A' + 10; | 148 return c - 'A' + 10; |
149 } else if (c <= 'f') { | 149 } else if (c <= 'f') { |
150 return c - 'a' + 10; | 150 return c - 'a' + 10; |
151 } | 151 } |
152 return -1; | 152 return -1; |
153 } | 153 } |
154 | 154 |
155 static SkData* handle_type1_stream(SkStream* srcStream, size_t* headerLen, | 155 static sk_sp<SkData> handle_type1_stream(SkStream* srcStream, size_t* headerLen, |
156 size_t* dataLen, size_t* trailerLen) { | 156 size_t* dataLen, size_t* trailerLen) { |
157 // srcStream may be backed by a file or a unseekable fd, so we may not be | 157 // srcStream may be backed by a file or a unseekable fd, so we may not be |
158 // able to use skip(), rewind(), or getMemoryBase(). read()ing through | 158 // able to use skip(), rewind(), or getMemoryBase(). read()ing through |
159 // the input only once is doable, but very ugly. Furthermore, it'd be nice | 159 // the input only once is doable, but very ugly. Furthermore, it'd be nice |
160 // if the data was NUL terminated so that we can use strstr() to search it. | 160 // if the data was NUL terminated so that we can use strstr() to search it. |
161 // Make as few copies as possible given these constraints. | 161 // Make as few copies as possible given these constraints. |
162 SkDynamicMemoryWStream dynamicStream; | 162 SkDynamicMemoryWStream dynamicStream; |
163 std::unique_ptr<SkMemoryStream> staticStream; | 163 std::unique_ptr<SkMemoryStream> staticStream; |
164 SkData* data = nullptr; | 164 sk_sp<SkData> data; |
165 const uint8_t* src; | 165 const uint8_t* src; |
166 size_t srcLen; | 166 size_t srcLen; |
167 if ((srcLen = srcStream->getLength()) > 0) { | 167 if ((srcLen = srcStream->getLength()) > 0) { |
168 staticStream.reset(new SkMemoryStream(srcLen + 1)); | 168 staticStream.reset(new SkMemoryStream(srcLen + 1)); |
169 src = (const uint8_t*)staticStream->getMemoryBase(); | 169 src = (const uint8_t*)staticStream->getMemoryBase(); |
170 if (srcStream->getMemoryBase() != nullptr) { | 170 if (srcStream->getMemoryBase() != nullptr) { |
171 memcpy((void *)src, srcStream->getMemoryBase(), srcLen); | 171 memcpy((void *)src, srcStream->getMemoryBase(), srcLen); |
172 } else { | 172 } else { |
173 size_t read = 0; | 173 size_t read = 0; |
174 while (read < srcLen) { | 174 while (read < srcLen) { |
175 size_t got = srcStream->read((void *)staticStream->getAtPos(), | 175 size_t got = srcStream->read((void *)staticStream->getAtPos(), |
176 srcLen - read); | 176 srcLen - read); |
177 if (got == 0) { | 177 if (got == 0) { |
178 return nullptr; | 178 return nullptr; |
179 } | 179 } |
180 read += got; | 180 read += got; |
181 staticStream->seek(read); | 181 staticStream->seek(read); |
182 } | 182 } |
183 } | 183 } |
184 ((uint8_t *)src)[srcLen] = 0; | 184 ((uint8_t *)src)[srcLen] = 0; |
185 } else { | 185 } else { |
186 static const size_t kBufSize = 4096; | 186 static const size_t kBufSize = 4096; |
187 uint8_t buf[kBufSize]; | 187 uint8_t buf[kBufSize]; |
188 size_t amount; | 188 size_t amount; |
189 while ((amount = srcStream->read(buf, kBufSize)) > 0) { | 189 while ((amount = srcStream->read(buf, kBufSize)) > 0) { |
190 dynamicStream.write(buf, amount); | 190 dynamicStream.write(buf, amount); |
191 } | 191 } |
192 amount = 0; | 192 amount = 0; |
193 dynamicStream.write(&amount, 1); // nullptr terminator. | 193 dynamicStream.write(&amount, 1); // nullptr terminator. |
194 data = dynamicStream.copyToData(); | 194 data.reset(dynamicStream.copyToData()); |
195 src = data->bytes(); | 195 src = data->bytes(); |
196 srcLen = data->size() - 1; | 196 srcLen = data->size() - 1; |
197 } | 197 } |
198 | 198 |
199 // this handles releasing the data we may have gotten from dynamicStream. | |
200 // if data is null, it is a no-op | |
201 SkAutoDataUnref aud(data); | |
202 | |
203 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) { | 199 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) { |
204 static const int kPFBSectionHeaderLength = 6; | 200 static const int kPFBSectionHeaderLength = 6; |
205 const size_t length = *headerLen + *dataLen + *trailerLen; | 201 const size_t length = *headerLen + *dataLen + *trailerLen; |
206 SkASSERT(length > 0); | 202 SkASSERT(length > 0); |
207 SkASSERT(length + (2 * kPFBSectionHeaderLength) <= srcLen); | 203 SkASSERT(length + (2 * kPFBSectionHeaderLength) <= srcLen); |
208 | 204 |
209 SkData* data = SkData::NewUninitialized(length); | 205 sk_sp<SkData> data(SkData::MakeUninitialized(length)); |
210 | 206 |
211 const uint8_t* const srcHeader = src + kPFBSectionHeaderLength; | 207 const uint8_t* const srcHeader = src + kPFBSectionHeaderLength; |
212 // There is a six-byte section header before header and data | 208 // There is a six-byte section header before header and data |
213 // (but not trailer) that we're not going to copy. | 209 // (but not trailer) that we're not going to copy. |
214 const uint8_t* const srcData = srcHeader + *headerLen + kPFBSectionHeade
rLength; | 210 const uint8_t* const srcData = srcHeader + *headerLen + kPFBSectionHeade
rLength; |
215 const uint8_t* const srcTrailer = srcData + *headerLen; | 211 const uint8_t* const srcTrailer = srcData + *headerLen; |
216 | 212 |
217 uint8_t* const resultHeader = (uint8_t*)data->writable_data(); | 213 uint8_t* const resultHeader = (uint8_t*)data->writable_data(); |
218 uint8_t* const resultData = resultHeader + *headerLen; | 214 uint8_t* const resultData = resultHeader + *headerLen; |
219 uint8_t* const resultTrailer = resultData + *dataLen; | 215 uint8_t* const resultTrailer = resultData + *dataLen; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 } | 254 } |
259 } | 255 } |
260 if (!highNibble) { | 256 if (!highNibble) { |
261 resultData[outputOffset++] = dataByte; | 257 resultData[outputOffset++] = dataByte; |
262 } | 258 } |
263 SkASSERT(outputOffset == *dataLen); | 259 SkASSERT(outputOffset == *dataLen); |
264 | 260 |
265 uint8_t* const resultTrailer = &(buffer[SkToInt(*headerLen + outputOffse
t)]); | 261 uint8_t* const resultTrailer = &(buffer[SkToInt(*headerLen + outputOffse
t)]); |
266 memcpy(resultTrailer, src + *headerLen + hexDataLen, *trailerLen); | 262 memcpy(resultTrailer, src + *headerLen + hexDataLen, *trailerLen); |
267 | 263 |
268 return SkData::NewFromMalloc(buffer.release(), length); | 264 return SkData::MakeFromMalloc(buffer.release(), length); |
269 } | 265 } |
270 return nullptr; | 266 return nullptr; |
271 } | 267 } |
272 | 268 |
273 // scale from em-units to base-1000, returning as a SkScalar | 269 // scale from em-units to base-1000, returning as a SkScalar |
274 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { | 270 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
275 SkScalar scaled = SkIntToScalar(val); | 271 SkScalar scaled = SkIntToScalar(val); |
276 if (emSize == 1000) { | 272 if (emSize == 1000) { |
277 return scaled; | 273 return scaled; |
278 } else { | 274 } else { |
(...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1184 int ttcIndex; | 1180 int ttcIndex; |
1185 size_t header SK_INIT_TO_AVOID_WARNING; | 1181 size_t header SK_INIT_TO_AVOID_WARNING; |
1186 size_t data SK_INIT_TO_AVOID_WARNING; | 1182 size_t data SK_INIT_TO_AVOID_WARNING; |
1187 size_t trailer SK_INIT_TO_AVOID_WARNING; | 1183 size_t trailer SK_INIT_TO_AVOID_WARNING; |
1188 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex)
); | 1184 std::unique_ptr<SkStreamAsset> rawFontData(typeface()->openStream(&ttcIndex)
); |
1189 SkASSERT(rawFontData); | 1185 SkASSERT(rawFontData); |
1190 SkASSERT(rawFontData->getLength() > 0); | 1186 SkASSERT(rawFontData->getLength() > 0); |
1191 if (!rawFontData || 0 == rawFontData->getLength()) { | 1187 if (!rawFontData || 0 == rawFontData->getLength()) { |
1192 return false; | 1188 return false; |
1193 } | 1189 } |
1194 sk_sp<SkData> fontData(handle_type1_stream(rawFontData.get(), &header, | 1190 sk_sp<SkData> fontData(handle_type1_stream(rawFontData.get(), &header, &data
, &trailer)); |
1195 &data, &trailer)); | |
1196 if (fontData.get() == nullptr) { | 1191 if (fontData.get() == nullptr) { |
1197 return false; | 1192 return false; |
1198 } | 1193 } |
1199 SkASSERT(this->canEmbed()); | 1194 SkASSERT(this->canEmbed()); |
1200 auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); | 1195 auto fontStream = sk_make_sp<SkPDFStream>(std::move(fontData)); |
1201 fontStream->dict()->insertInt("Length1", header); | 1196 fontStream->dict()->insertInt("Length1", header); |
1202 fontStream->dict()->insertInt("Length2", data); | 1197 fontStream->dict()->insertInt("Length2", data); |
1203 fontStream->dict()->insertInt("Length3", trailer); | 1198 fontStream->dict()->insertInt("Length3", trailer); |
1204 descriptor->insertObjRef("FontFile", std::move(fontStream)); | 1199 descriptor->insertObjRef("FontFile", std::move(fontStream)); |
1205 | 1200 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1405 } | 1400 } |
1406 return *canon->fCanEmbedTypeface.set(id, canEmbed); | 1401 return *canon->fCanEmbedTypeface.set(id, canEmbed); |
1407 } | 1402 } |
1408 | 1403 |
1409 void SkPDFFont::drop() { | 1404 void SkPDFFont::drop() { |
1410 fTypeface = nullptr; | 1405 fTypeface = nullptr; |
1411 fFontInfo = nullptr; | 1406 fFontInfo = nullptr; |
1412 fDescriptor = nullptr; | 1407 fDescriptor = nullptr; |
1413 this->SkPDFDict::drop(); | 1408 this->SkPDFDict::drop(); |
1414 } | 1409 } |
OLD | NEW |