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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 } else if (c <= '9') { | 143 } else if (c <= '9') { |
144 return c - '0'; | 144 return c - '0'; |
145 } else if (c <= 'F') { | 145 } else if (c <= 'F') { |
146 return c - 'A' + 10; | 146 return c - 'A' + 10; |
147 } else if (c <= 'f') { | 147 } else if (c <= 'f') { |
148 return c - 'a' + 10; | 148 return c - 'a' + 10; |
149 } | 149 } |
150 return -1; | 150 return -1; |
151 } | 151 } |
152 | 152 |
153 static SkData* handle_type1_stream(SkStream* srcStream, size_t* headerLen, | 153 SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen, |
154 size_t* dataLen, size_t* trailerLen) { | 154 size_t* dataLen, size_t* trailerLen) { |
155 // srcStream may be backed by a file or a unseekable fd, so we may not be | 155 // srcStream may be backed by a file or a unseekable fd, so we may not be |
156 // able to use skip(), rewind(), or getMemoryBase(). read()ing through | 156 // able to use skip(), rewind(), or getMemoryBase(). read()ing through |
157 // the input only once is doable, but very ugly. Furthermore, it'd be nice | 157 // the input only once is doable, but very ugly. Furthermore, it'd be nice |
158 // if the data was NUL terminated so that we can use strstr() to search it. | 158 // if the data was NUL terminated so that we can use strstr() to search it. |
159 // Make as few copies as possible given these constraints. | 159 // Make as few copies as possible given these constraints. |
160 SkDynamicMemoryWStream dynamicStream; | 160 SkDynamicMemoryWStream dynamicStream; |
161 SkAutoTUnref<SkMemoryStream> staticStream; | 161 SkAutoTUnref<SkMemoryStream> staticStream; |
162 SkData* data = NULL; | 162 SkData* data = NULL; |
163 const uint8_t* src; | 163 const uint8_t* src; |
164 size_t srcLen; | 164 size_t srcLen; |
(...skipping 27 matching lines...) Expand all Loading... |
192 data = dynamicStream.copyToData(); | 192 data = dynamicStream.copyToData(); |
193 src = data->bytes(); | 193 src = data->bytes(); |
194 srcLen = data->size() - 1; | 194 srcLen = data->size() - 1; |
195 } | 195 } |
196 | 196 |
197 // this handles releasing the data we may have gotten from dynamicStream. | 197 // this handles releasing the data we may have gotten from dynamicStream. |
198 // if data is null, it is a no-op | 198 // if data is null, it is a no-op |
199 SkAutoDataUnref aud(data); | 199 SkAutoDataUnref aud(data); |
200 | 200 |
201 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) { | 201 if (parsePFB(src, srcLen, headerLen, dataLen, trailerLen)) { |
202 static const int kPFBSectionHeaderLength = 6; | 202 SkMemoryStream* result = |
203 const size_t length = *headerLen + *dataLen + *trailerLen; | 203 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); |
204 SkASSERT(length > 0); | 204 memcpy((char*)result->getAtPos(), src + 6, *headerLen); |
205 SkASSERT(length + (2 * kPFBSectionHeaderLength) <= srcLen); | 205 result->seek(*headerLen); |
206 | 206 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen); |
207 SkAutoTMalloc<uint8_t> buffer(length); | 207 result->seek(*headerLen + *dataLen); |
208 | 208 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen, |
209 const uint8_t* const srcHeader = src + kPFBSectionHeaderLength; | 209 *trailerLen); |
210 // There is a six-byte section header before header and data | 210 result->rewind(); |
211 // (but not trailer) that we're not going to copy. | 211 return result; |
212 const uint8_t* const srcData | |
213 = srcHeader + *headerLen + kPFBSectionHeaderLength; | |
214 const uint8_t* const srcTrailer = srcData + *headerLen; | |
215 | |
216 uint8_t* const resultHeader = buffer.get(); | |
217 uint8_t* const resultData = resultHeader + *headerLen; | |
218 uint8_t* const resultTrailer = resultData + *dataLen; | |
219 | |
220 SkASSERT(resultTrailer + *trailerLen == resultHeader + length); | |
221 | |
222 memcpy(resultHeader, srcHeader, *headerLen); | |
223 memcpy(resultData, srcData, *dataLen); | |
224 memcpy(resultTrailer, srcTrailer, *trailerLen); | |
225 | |
226 return SkData::NewFromMalloc(buffer.detach(), length); | |
227 } | 212 } |
228 | 213 |
229 // A PFA has to be converted for PDF. | 214 // A PFA has to be converted for PDF. |
230 size_t hexDataLen; | 215 size_t hexDataLen; |
231 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen, | 216 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen, |
232 trailerLen)) { | 217 trailerLen)) { |
233 const size_t length = *headerLen + *dataLen + *trailerLen; | 218 SkMemoryStream* result = |
234 SkASSERT(length > 0); | 219 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); |
235 SkAutoTMalloc<uint8_t> buffer(length); | 220 memcpy((char*)result->getAtPos(), src, *headerLen); |
236 | 221 result->seek(*headerLen); |
237 memcpy(buffer.get(), src, *headerLen); | |
238 uint8_t* const resultData = &(buffer[*headerLen]); | |
239 | 222 |
240 const uint8_t* hexData = src + *headerLen; | 223 const uint8_t* hexData = src + *headerLen; |
241 const uint8_t* trailer = hexData + hexDataLen; | 224 const uint8_t* trailer = hexData + hexDataLen; |
242 size_t outputOffset = 0; | 225 size_t outputOffset = 0; |
243 uint8_t dataByte = 0; // To hush compiler. | 226 uint8_t dataByte = 0; // To hush compiler. |
244 bool highNibble = true; | 227 bool highNibble = true; |
245 for (; hexData < trailer; hexData++) { | 228 for (; hexData < trailer; hexData++) { |
246 int8_t curNibble = hexToBin(*hexData); | 229 int8_t curNibble = hexToBin(*hexData); |
247 if (curNibble < 0) { | 230 if (curNibble < 0) { |
248 continue; | 231 continue; |
249 } | 232 } |
250 if (highNibble) { | 233 if (highNibble) { |
251 dataByte = curNibble << 4; | 234 dataByte = curNibble << 4; |
252 highNibble = false; | 235 highNibble = false; |
253 } else { | 236 } else { |
254 dataByte |= curNibble; | 237 dataByte |= curNibble; |
255 highNibble = true; | 238 highNibble = true; |
256 resultData[outputOffset++] = dataByte; | 239 ((char *)result->getAtPos())[outputOffset++] = dataByte; |
257 } | 240 } |
258 } | 241 } |
259 if (!highNibble) { | 242 if (!highNibble) { |
260 resultData[outputOffset++] = dataByte; | 243 ((char *)result->getAtPos())[outputOffset++] = dataByte; |
261 } | 244 } |
262 SkASSERT(outputOffset == *dataLen); | 245 SkASSERT(outputOffset == *dataLen); |
| 246 result->seek(*headerLen + outputOffset); |
263 | 247 |
264 uint8_t* const resultTrailer = &(buffer[*headerLen + outputOffset]); | 248 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen, |
265 memcpy(resultTrailer, src + *headerLen + hexDataLen, *trailerLen); | 249 *trailerLen); |
| 250 result->rewind(); |
| 251 return result; |
| 252 } |
266 | 253 |
267 return SkData::NewFromMalloc(buffer.detach(), length); | |
268 } | |
269 return NULL; | 254 return NULL; |
270 } | 255 } |
271 | 256 |
272 // scale from em-units to base-1000, returning as a SkScalar | 257 // scale from em-units to base-1000, returning as a SkScalar |
273 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { | 258 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
274 SkScalar scaled = SkIntToScalar(val); | 259 SkScalar scaled = SkIntToScalar(val); |
275 if (emSize == 1000) { | 260 if (emSize == 1000) { |
276 return scaled; | 261 return scaled; |
277 } else { | 262 } else { |
278 return SkScalarMulDiv(scaled, 1000, emSize); | 263 return SkScalarMulDiv(scaled, 1000, emSize); |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 uint16_t lastGlyphID) { | 549 uint16_t lastGlyphID) { |
565 SkDynamicMemoryWStream cmap; | 550 SkDynamicMemoryWStream cmap; |
566 if (multiByteGlyphs) { | 551 if (multiByteGlyphs) { |
567 append_tounicode_header(&cmap, firstGlyphID, lastGlyphID); | 552 append_tounicode_header(&cmap, firstGlyphID, lastGlyphID); |
568 } else { | 553 } else { |
569 append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1); | 554 append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1); |
570 } | 555 } |
571 append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs, | 556 append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs, |
572 firstGlyphID, lastGlyphID); | 557 firstGlyphID, lastGlyphID); |
573 append_cmap_footer(&cmap); | 558 append_cmap_footer(&cmap); |
574 SkAutoTUnref<SkData> cmapData(cmap.copyToData()); | 559 SkAutoTUnref<SkMemoryStream> cmapStream(new SkMemoryStream()); |
575 return new SkPDFStream(cmapData.get()); | 560 cmapStream->setData(cmap.copyToData())->unref(); |
| 561 return new SkPDFStream(cmapStream.get()); |
576 } | 562 } |
577 | 563 |
578 #if defined (SK_SFNTLY_SUBSETTER) | 564 #if defined (SK_SFNTLY_SUBSETTER) |
579 static void sk_delete_array(const void* ptr, size_t, void*) { | 565 static void sk_delete_array(const void* ptr, size_t, void*) { |
580 // Use C-style cast to cast away const and cast type simultaneously. | 566 // Use C-style cast to cast away const and cast type simultaneously. |
581 delete[] (unsigned char*)ptr; | 567 delete[] (unsigned char*)ptr; |
582 } | 568 } |
583 #endif | 569 #endif |
584 | 570 |
585 static size_t get_subset_font_stream(const char* fontName, | 571 static size_t get_subset_font_stream(const char* fontName, |
586 const SkTypeface* typeface, | 572 const SkTypeface* typeface, |
587 const SkTDArray<uint32_t>& subset, | 573 const SkTDArray<uint32_t>& subset, |
588 SkPDFStream** fontStream) { | 574 SkPDFStream** fontStream) { |
589 int ttcIndex; | 575 int ttcIndex; |
590 SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex)); | 576 SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex)); |
591 SkASSERT(fontData.get()); | |
592 | 577 |
593 size_t fontSize = fontData->getLength(); | 578 size_t fontSize = fontData->getLength(); |
594 | 579 |
595 #if defined (SK_SFNTLY_SUBSETTER) | 580 #if defined (SK_SFNTLY_SUBSETTER) |
596 // Read font into buffer. | 581 // Read font into buffer. |
597 SkPDFStream* subsetFontStream = NULL; | 582 SkPDFStream* subsetFontStream = NULL; |
598 SkTDArray<unsigned char> originalFont; | 583 SkTDArray<unsigned char> originalFont; |
599 originalFont.setCount(SkToInt(fontSize)); | 584 originalFont.setCount(SkToInt(fontSize)); |
600 if (fontData->read(originalFont.begin(), fontSize) == fontSize) { | 585 if (fontData->read(originalFont.begin(), fontSize) == fontSize) { |
601 unsigned char* subsetFont = NULL; | 586 unsigned char* subsetFont = NULL; |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1303 } | 1288 } |
1304 | 1289 |
1305 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); | 1290 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); |
1306 setFontDescriptor(descriptor.get()); | 1291 setFontDescriptor(descriptor.get()); |
1307 | 1292 |
1308 int ttcIndex; | 1293 int ttcIndex; |
1309 size_t header SK_INIT_TO_AVOID_WARNING; | 1294 size_t header SK_INIT_TO_AVOID_WARNING; |
1310 size_t data SK_INIT_TO_AVOID_WARNING; | 1295 size_t data SK_INIT_TO_AVOID_WARNING; |
1311 size_t trailer SK_INIT_TO_AVOID_WARNING; | 1296 size_t trailer SK_INIT_TO_AVOID_WARNING; |
1312 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); | 1297 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); |
1313 SkData* fontData = handle_type1_stream(rawFontData.get(), &header, &data, | 1298 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, |
1314 &trailer); | 1299 &trailer); |
1315 if (fontData == NULL) { | 1300 if (fontData == NULL) { |
1316 return false; | 1301 return false; |
1317 } | 1302 } |
1318 if (canEmbed()) { | 1303 if (canEmbed()) { |
1319 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); | 1304 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); |
1320 addResource(fontStream.get()); | 1305 addResource(fontStream.get()); |
1321 fontStream->insertInt("Length1", header); | 1306 fontStream->insertInt("Length1", header); |
1322 fontStream->insertInt("Length2", data); | 1307 fontStream->insertInt("Length2", data); |
1323 fontStream->insertInt("Length3", trailer); | 1308 fontStream->insertInt("Length3", trailer); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1488 | 1473 |
1489 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); | 1474 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); |
1490 insertInt("FirstChar", 1); | 1475 insertInt("FirstChar", 1); |
1491 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1); | 1476 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1); |
1492 insert("Widths", widthArray.get()); | 1477 insert("Widths", widthArray.get()); |
1493 insertName("CIDToGIDMap", "Identity"); | 1478 insertName("CIDToGIDMap", "Identity"); |
1494 | 1479 |
1495 populateToUnicodeTable(NULL); | 1480 populateToUnicodeTable(NULL); |
1496 return true; | 1481 return true; |
1497 } | 1482 } |
OLD | NEW |