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

Side by Side Diff: src/pdf/SkPDFFont.cpp

Issue 340783013: Switch SkPDFStream's internal storage from SkStream to SkData (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: AnotherPatchSet Created 6 years, 5 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 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
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* handle_type1_stream(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
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 static const int pfbSectionHeaderLength = 6;
mtklein 2014/06/26 13:21:59 kPFBSectionHeaderLength? I started panicking that
hal.canary 2014/06/26 18:42:02 On 2014/06/26 13:21:59, mtklein wrote: > kPFBSecti
203 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); 203 const size_t length = *headerLen + *dataLen + *trailerLen;
204 memcpy((char*)result->getAtPos(), src + 6, *headerLen); 204 SkASSERT(length > 0);
205 result->seek(*headerLen); 205 SkASSERT(length + (2 * pfbSectionHeaderLength) <= srcLen);
206 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6, *dataLen); 206 SkAutoMalloc buffer(length);
207 result->seek(*headerLen + *dataLen); 207
208 memcpy((char*)result->getAtPos(), src + 6 + *headerLen + 6 + *dataLen, 208 const uint8_t* const srcHeader = src + pfbSectionHeaderLength;
mtklein 2014/06/26 13:21:59 I admire your attention to constness, but it's pro
mtklein 2014/06/26 13:21:59 // There is a six-byte section header before heade
hal.canary 2014/06/26 18:42:02 Done.
hal.canary 2014/06/26 18:42:02 Done.
209 *trailerLen); 209 const uint8_t* const srcData
210 result->rewind(); 210 = srcHeader + *headerLen + pfbSectionHeaderLength;
211 return result; 211 const uint8_t* const srcTrailer = srcData + *headerLen;
212
213 uint8_t* const resultHeader = static_cast<uint8_t*>(buffer.get());
214 uint8_t* const resultData = resultHeader + *headerLen;
215 uint8_t* const resultTrailer = resultData + *dataLen;
216
217 SkASSERT(resultTrailer + *trailerLen == resultHeader + length);
218
219 memcpy(resultHeader, srcHeader, *headerLen);
220 memcpy(resultData, srcData, *dataLen);
221 memcpy(resultTrailer, srcTrailer, *trailerLen);
222
223 return SkData::NewFromMalloc(buffer.detach(), length);
212 } 224 }
213 225
214 // A PFA has to be converted for PDF. 226 // A PFA has to be converted for PDF.
215 size_t hexDataLen; 227 size_t hexDataLen;
216 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen, 228 if (parsePFA((const char*)src, srcLen, headerLen, &hexDataLen, dataLen,
217 trailerLen)) { 229 trailerLen)) {
218 SkMemoryStream* result = 230 const size_t length = *headerLen + *dataLen + *trailerLen;
219 new SkMemoryStream(*headerLen + *dataLen + *trailerLen); 231 SkASSERT(length > 0);
220 memcpy((char*)result->getAtPos(), src, *headerLen); 232 SkAutoMalloc buffer(length);
221 result->seek(*headerLen); 233 char* const result = static_cast<char*>(buffer.get());
234 memcpy(result, src, *headerLen);
235 char* const resultData = &(result[*headerLen]);
222 236
223 const uint8_t* hexData = src + *headerLen; 237 const uint8_t* hexData = src + *headerLen;
224 const uint8_t* trailer = hexData + hexDataLen; 238 const uint8_t* trailer = hexData + hexDataLen;
225 size_t outputOffset = 0; 239 size_t outputOffset = 0;
226 uint8_t dataByte = 0; // To hush compiler. 240 uint8_t dataByte = 0; // To hush compiler.
227 bool highNibble = true; 241 bool highNibble = true;
228 for (; hexData < trailer; hexData++) { 242 for (; hexData < trailer; hexData++) {
229 int8_t curNibble = hexToBin(*hexData); 243 int8_t curNibble = hexToBin(*hexData);
230 if (curNibble < 0) { 244 if (curNibble < 0) {
231 continue; 245 continue;
232 } 246 }
233 if (highNibble) { 247 if (highNibble) {
234 dataByte = curNibble << 4; 248 dataByte = curNibble << 4;
235 highNibble = false; 249 highNibble = false;
236 } else { 250 } else {
237 dataByte |= curNibble; 251 dataByte |= curNibble;
238 highNibble = true; 252 highNibble = true;
239 ((char *)result->getAtPos())[outputOffset++] = dataByte; 253 resultData[outputOffset++] = dataByte;
240 } 254 }
241 } 255 }
242 if (!highNibble) { 256 if (!highNibble) {
243 ((char *)result->getAtPos())[outputOffset++] = dataByte; 257 resultData[outputOffset++] = dataByte;
244 } 258 }
245 SkASSERT(outputOffset == *dataLen); 259 SkASSERT(outputOffset == *dataLen);
246 result->seek(*headerLen + outputOffset);
247 260
248 memcpy((char *)result->getAtPos(), src + *headerLen + hexDataLen, 261 char* const resultTrailer = &(result[*headerLen + outputOffset]);
249 *trailerLen); 262 memcpy(resultTrailer, src + *headerLen + hexDataLen, *trailerLen);
250 result->rewind(); 263
251 return result; 264 return SkData::NewFromMalloc(buffer.detach(), length);
252 } 265 }
253
254 return NULL; 266 return NULL;
255 } 267 }
256 268
257 // scale from em-units to base-1000, returning as a SkScalar 269 // scale from em-units to base-1000, returning as a SkScalar
258 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) { 270 SkScalar scaleFromFontUnits(int16_t val, uint16_t emSize) {
259 SkScalar scaled = SkIntToScalar(val); 271 SkScalar scaled = SkIntToScalar(val);
260 if (emSize == 1000) { 272 if (emSize == 1000) {
261 return scaled; 273 return scaled;
262 } else { 274 } else {
263 return SkScalarMulDiv(scaled, 1000, emSize); 275 return SkScalarMulDiv(scaled, 1000, emSize);
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 uint16_t lastGlyphID) { 561 uint16_t lastGlyphID) {
550 SkDynamicMemoryWStream cmap; 562 SkDynamicMemoryWStream cmap;
551 if (multiByteGlyphs) { 563 if (multiByteGlyphs) {
552 append_tounicode_header(&cmap, firstGlyphID, lastGlyphID); 564 append_tounicode_header(&cmap, firstGlyphID, lastGlyphID);
553 } else { 565 } else {
554 append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1); 566 append_tounicode_header(&cmap, 1, lastGlyphID - firstGlyphID + 1);
555 } 567 }
556 append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs, 568 append_cmap_sections(glyphToUnicode, subset, &cmap, multiByteGlyphs,
557 firstGlyphID, lastGlyphID); 569 firstGlyphID, lastGlyphID);
558 append_cmap_footer(&cmap); 570 append_cmap_footer(&cmap);
559 SkAutoTUnref<SkMemoryStream> cmapStream(new SkMemoryStream()); 571 SkAutoTUnref<SkData> cmapData(cmap.copyToData());
560 cmapStream->setData(cmap.copyToData())->unref(); 572 return new SkPDFStream(cmapData.get());
561 return new SkPDFStream(cmapStream.get());
562 } 573 }
563 574
564 #if defined (SK_SFNTLY_SUBSETTER) 575 #if defined (SK_SFNTLY_SUBSETTER)
565 static void sk_delete_array(const void* ptr, size_t, void*) { 576 static void sk_delete_array(const void* ptr, size_t, void*) {
566 // Use C-style cast to cast away const and cast type simultaneously. 577 // Use C-style cast to cast away const and cast type simultaneously.
567 delete[] (unsigned char*)ptr; 578 delete[] (unsigned char*)ptr;
568 } 579 }
569 #endif 580 #endif
570 581
571 static size_t get_subset_font_stream(const char* fontName, 582 static size_t get_subset_font_stream(const char* fontName,
572 const SkTypeface* typeface, 583 const SkTypeface* typeface,
573 const SkTDArray<uint32_t>& subset, 584 const SkTDArray<uint32_t>& subset,
574 SkPDFStream** fontStream) { 585 SkPDFStream** fontStream) {
575 int ttcIndex; 586 int ttcIndex;
576 SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex)); 587 SkAutoTUnref<SkStream> fontData(typeface->openStream(&ttcIndex));
588 SkASSERT(fontData.get());
577 589
578 size_t fontSize = fontData->getLength(); 590 size_t fontSize = fontData->getLength();
579 591
580 #if defined (SK_SFNTLY_SUBSETTER) 592 #if defined (SK_SFNTLY_SUBSETTER)
581 // Read font into buffer. 593 // Read font into buffer.
582 SkPDFStream* subsetFontStream = NULL; 594 SkPDFStream* subsetFontStream = NULL;
583 SkTDArray<unsigned char> originalFont; 595 SkTDArray<unsigned char> originalFont;
584 originalFont.setCount(SkToInt(fontSize)); 596 originalFont.setCount(SkToInt(fontSize));
585 if (fontData->read(originalFont.begin(), fontSize) == fontSize) { 597 if (fontData->read(originalFont.begin(), fontSize) == fontSize) {
586 unsigned char* subsetFont = NULL; 598 unsigned char* subsetFont = NULL;
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after
1288 } 1300 }
1289 1301
1290 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor")); 1302 SkAutoTUnref<SkPDFDict> descriptor(new SkPDFDict("FontDescriptor"));
1291 setFontDescriptor(descriptor.get()); 1303 setFontDescriptor(descriptor.get());
1292 1304
1293 int ttcIndex; 1305 int ttcIndex;
1294 size_t header SK_INIT_TO_AVOID_WARNING; 1306 size_t header SK_INIT_TO_AVOID_WARNING;
1295 size_t data SK_INIT_TO_AVOID_WARNING; 1307 size_t data SK_INIT_TO_AVOID_WARNING;
1296 size_t trailer SK_INIT_TO_AVOID_WARNING; 1308 size_t trailer SK_INIT_TO_AVOID_WARNING;
1297 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex)); 1309 SkAutoTUnref<SkStream> rawFontData(typeface()->openStream(&ttcIndex));
1298 SkStream* fontData = handleType1Stream(rawFontData.get(), &header, &data, 1310 SkData* fontData = handle_type1_stream(rawFontData.get(), &header, &data,
1299 &trailer); 1311 &trailer);
1300 if (fontData == NULL) { 1312 if (fontData == NULL) {
1301 return false; 1313 return false;
1302 } 1314 }
1303 if (canEmbed()) { 1315 if (canEmbed()) {
1304 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData)); 1316 SkAutoTUnref<SkPDFStream> fontStream(new SkPDFStream(fontData));
1305 addResource(fontStream.get()); 1317 addResource(fontStream.get());
1306 fontStream->insertInt("Length1", header); 1318 fontStream->insertInt("Length1", header);
1307 fontStream->insertInt("Length2", data); 1319 fontStream->insertInt("Length2", data);
1308 fontStream->insertInt("Length3", trailer); 1320 fontStream->insertInt("Length3", trailer);
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1473 1485
1474 insert("FontBBox", makeFontBBox(bbox, 1000))->unref(); 1486 insert("FontBBox", makeFontBBox(bbox, 1000))->unref();
1475 insertInt("FirstChar", 1); 1487 insertInt("FirstChar", 1);
1476 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1); 1488 insertInt("LastChar", lastGlyphID() - firstGlyphID() + 1);
1477 insert("Widths", widthArray.get()); 1489 insert("Widths", widthArray.get());
1478 insertName("CIDToGIDMap", "Identity"); 1490 insertName("CIDToGIDMap", "Identity");
1479 1491
1480 populateToUnicodeTable(NULL); 1492 populateToUnicodeTable(NULL);
1481 return true; 1493 return true;
1482 } 1494 }
OLDNEW
« no previous file with comments | « gyp/pdf.gyp ('k') | src/pdf/SkPDFGraphicState.cpp » ('j') | src/pdf/SkPDFGraphicState.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698