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

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

Powered by Google App Engine
This is Rietveld 408576698