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 SkStream* handleType1Stream(SkStream* srcStream, size_t* headerLen, | 153 static SkData* handleType1Stream(SkStream* srcStream, size_t* headerLen, |
mtklein
2014/06/25 18:12:31
-> handle_type1_stream
hal.canary
2014/06/25 18:56:26
Done.
| |
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 SkMemoryStream* result = | 202 const size_t length = *headerLen + *dataLen + *trailerLen; |
203 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); | 203 SkASSERT(length > 0); |
204 memcpy((char*)result->getAtPos(), src + 6, *headerLen); | 204 SkAutoMalloc buffer(length); |
205 result->seek(*headerLen); | 205 char* const result = static_cast<char*>(buffer.get()); |
206 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen); | 206 memcpy(result, src + 6, *headerLen); |
mtklein
2014/06/25 18:12:31
Just pass buffer.get() into each of these?
| |
207 result->seek(*headerLen + *dataLen); | 207 memcpy(result + *headerLen, src + 6 + *headerLen + 6, *dataLen); |
208 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen, | 208 memcpy(result + (*headerLen + *dataLen), |
mtklein
2014/06/25 18:12:31
This whole area is really hard to follow. Diagra
| |
209 src + 6 + *headerLen + 6 + *dataLen, | |
209 *trailerLen); | 210 *trailerLen); |
210 result->rewind(); | 211 return SkData::NewFromMalloc(buffer.detach(), length); |
211 return result; | |
212 } | 212 } |
213 | 213 |
214 // A PFA has to be converted for PDF. | 214 // A PFA has to be converted for PDF. |
215 size_t hexDataLen; | 215 size_t hexDataLen; |
216 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen, | 216 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen, |
217 trailerLen)) { | 217 trailerLen)) { |
218 SkMemoryStream* result = | 218 const size_t length = *headerLen + *dataLen + *trailerLen; |
219 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); | 219 SkASSERT(length > 0); |
220 memcpy((char*)result->getAtPos(), src, *headerLen); | 220 SkAutoMalloc buffer(length); |
mtklein
2014/06/25 18:12:31
Just start with SkAutoTMalloc<char> if you want ch
hal.canary
2014/06/25 18:56:26
SkAutoTMalloc uses SK_MALLOC_TEMP, which is not wh
mtklein
2014/06/26 13:21:59
Let's ignore that. SK_MALLOC_TEMP doesn't do anyt
| |
221 result->seek(*headerLen); | 221 char* const result = static_cast<char*>(buffer.get()); |
222 memcpy(result, src, *headerLen); | |
223 char* const resultData = &(result[*headerLen]); | |
222 | 224 |
223 const uint8_t* hexData = src + *headerLen; | 225 const uint8_t* hexData = src + *headerLen; |
224 const uint8_t* trailer = hexData + hexDataLen; | 226 const uint8_t* trailer = hexData + hexDataLen; |
225 size_t outputOffset = 0; | 227 size_t outputOffset = 0; |
226 uint8_t dataByte = 0; // To hush compiler. | 228 uint8_t dataByte = 0; // To hush compiler. |
227 bool highNibble = true; | 229 bool highNibble = true; |
228 for (; hexData < trailer; hexData++) { | 230 for (; hexData < trailer; hexData++) { |
229 int8_t curNibble = hexToBin(*hexData); | 231 int8_t curNibble = hexToBin(*hexData); |
230 if (curNibble < 0) { | 232 if (curNibble < 0) { |
231 continue; | 233 continue; |
232 } | 234 } |
233 if (highNibble) { | 235 if (highNibble) { |
234 dataByte = curNibble << 4; | 236 dataByte = curNibble << 4; |
235 highNibble = false; | 237 highNibble = false; |
236 } else { | 238 } else { |
237 dataByte |= curNibble; | 239 dataByte |= curNibble; |
238 highNibble = true; | 240 highNibble = true; |
239 ((char *)result->getAtPos())[outputOffset++] = dataByte; | 241 resultData[outputOffset++] = dataByte; |
240 } | 242 } |
241 } | 243 } |
242 if (!highNibble) { | 244 if (!highNibble) { |
243 ((char *)result->getAtPos())[outputOffset++] = dataByte; | 245 resultData[outputOffset++] = dataByte; |
244 } | 246 } |
245 SkASSERT(outputOffset == *dataLen); | 247 SkASSERT(outputOffset == *dataLen); |
246 result->seek(*headerLen + outputOffset); | |
247 | 248 |
248 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen, | 249 char* const resultTrailer = &(result[*headerLen + outputOffset]); |
249 *trailerLen); | 250 memcpy(resultTrailer, src + *headerLen + hexDataLen, *trailerLen); |
250 result->rewind(); | 251 |
251 return result; | 252 return SkData::NewFromMalloc(buffer.detach(), length); |
252 } | 253 } |
253 | |
254 return NULL; | 254 return NULL; |
255 } | 255 } |
256 | 256 |
257 // scale from em-units to base-1000, returning as a SkScalar | 257 // scale from em-units to base-1000, returning as a SkScalar |
258 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { | 258 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { |
259 SkScalar scaled = SkIntToScalar(val); | 259 SkScalar scaled = SkIntToScalar(val); |
260 if (emSize == 1000) { | 260 if (emSize == 1000) { |
261 return scaled; | 261 return scaled; |
262 } else { | 262 } else { |
263 return SkScalarMulDiv(scaled, 1000, emSize); | 263 return SkScalarMulDiv(scaled, 1000, emSize); |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
549 uint16_t lastGlyphID) { | 549 uint16_t lastGlyphID) { |
550 SkDynamicMemoryWStream cmap; | 550 SkDynamicMemoryWStream cmap; |
551 if (multiByteGlyphs) { | 551 if (multiByteGlyphs) { |
552 append_tounicode_header(&cmap, firstGlyphID, lastGlyphID); | 552 append_tounicode_header(&cmap, firstGlyphID, lastGlyphID); |
553 } else { | 553 } else { |
554 append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1); | 554 append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1); |
555 } | 555 } |
556 append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs, | 556 append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs, |
557 firstGlyphID, lastGlyphID); | 557 firstGlyphID, lastGlyphID); |
558 append_cmap_footer(&cmap); | 558 append_cmap_footer(&cmap); |
559 SkAutoTUnref<SkMemoryStream> cmapStream(new SkMemoryStream()); | 559 SkAutoTUnref<SkData> cmapData(cmap.copyToData()); |
560 cmapStream->setData(cmap.copyToData())->unref(); | 560 return new SkPDFStream(cmapData.get()); |
mtklein
2014/06/25 18:12:31
Remind me why this is not
return new SkPDFStream(
hal.canary
2014/06/25 18:56:26
The SkPDFStream constructor refs the data.
mtklein
2014/06/26 13:21:59
Add a comment?
| |
561 return new SkPDFStream(cmapStream.get()); | |
562 } | 561 } |
563 | 562 |
564 #if defined (SK_SFNTLY_SUBSETTER) | 563 #if defined (SK_SFNTLY_SUBSETTER) |
565 static void sk_delete_array(const void* ptr, size_t, void*) { | 564 static void sk_delete_array(const void* ptr, size_t, void*) { |
566 // Use C-style cast to cast away const and cast type simultaneously. | 565 // Use C-style cast to cast away const and cast type simultaneously. |
567 delete[] (unsigned char*)ptr; | 566 delete[] (unsigned char*)ptr; |
568 } | 567 } |
569 #endif | 568 #endif |
570 | 569 |
571 static size_t get_subset_font_stream(const char* fontName, | 570 static size_t get_subset_font_stream(const char* fontName, |
572 const SkTypeface* typeface, | 571 const SkTypeface* typeface, |
573 const SkTDArray<uint32_t>& subset, | 572 const SkTDArray<uint32_t>& subset, |
574 SkPDFStream** fontStream) { | 573 SkPDFStream** fontStream) { |
575 int ttcIndex; | 574 int ttcIndex; |
576 SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex)); | 575 SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex)); |
576 SkASSERT(fontData.get()); | |
577 | 577 |
578 size_t fontSize = fontData->getLength(); | 578 size_t fontSize = fontData->getLength(); |
579 | 579 |
580 #if defined (SK_SFNTLY_SUBSETTER) | 580 #if defined (SK_SFNTLY_SUBSETTER) |
581 // Read font into buffer. | 581 // Read font into buffer. |
582 SkPDFStream* subsetFontStream = NULL; | 582 SkPDFStream* subsetFontStream = NULL; |
583 SkTDArray<unsigned char> originalFont; | 583 SkTDArray<unsigned char> originalFont; |
584 originalFont.setCount(SkToInt(fontSize)); | 584 originalFont.setCount(SkToInt(fontSize)); |
585 if (fontData->read(originalFont.begin(), fontSize) == fontSize) { | 585 if (fontData->read(originalFont.begin(), fontSize) == fontSize) { |
586 unsigned char* subsetFont = NULL; | 586 unsigned char* subsetFont = NULL; |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1288 } | 1288 } |
1289 | 1289 |
1290 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); | 1290 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); |
1291 setFontDescriptor(descriptor.get()); | 1291 setFontDescriptor(descriptor.get()); |
1292 | 1292 |
1293 int ttcIndex; | 1293 int ttcIndex; |
1294 size_t header SK_INIT_TO_AVOID_WARNING; | 1294 size_t header SK_INIT_TO_AVOID_WARNING; |
1295 size_t data SK_INIT_TO_AVOID_WARNING; | 1295 size_t data SK_INIT_TO_AVOID_WARNING; |
1296 size_t trailer SK_INIT_TO_AVOID_WARNING; | 1296 size_t trailer SK_INIT_TO_AVOID_WARNING; |
1297 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); | 1297 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); |
1298 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, | 1298 SkData* fontData = handleType1Stream(rawFontData.get(), &header, &data, |
1299 &trailer); | 1299 &trailer); |
1300 if (fontData == NULL) { | 1300 if (fontData == NULL) { |
1301 return false; | 1301 return false; |
1302 } | 1302 } |
1303 if (canEmbed()) { | 1303 if (canEmbed()) { |
1304 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); | 1304 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); |
1305 addResource(fontStream.get()); | 1305 addResource(fontStream.get()); |
1306 fontStream->insertInt("Length1", header); | 1306 fontStream->insertInt("Length1", header); |
1307 fontStream->insertInt("Length2", data); | 1307 fontStream->insertInt("Length2", data); |
1308 fontStream->insertInt("Length3", trailer); | 1308 fontStream->insertInt("Length3", trailer); |
1309 descriptor->insert("FontFile", | 1309 descriptor->insert("FontFile", |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1473 | 1473 |
1474 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); | 1474 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); |
1475 insertInt("FirstChar", 1); | 1475 insertInt("FirstChar", 1); |
1476 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1); | 1476 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1); |
1477 insert("Widths", widthArray.get()); | 1477 insert("Widths", widthArray.get()); |
1478 insertName("CIDToGIDMap", "Identity"); | 1478 insertName("CIDToGIDMap", "Identity"); |
1479 | 1479 |
1480 populateToUnicodeTable(NULL); | 1480 populateToUnicodeTable(NULL); |
1481 return true; | 1481 return true; |
1482 } | 1482 } |
OLD | NEW |