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

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

Powered by Google App Engine
This is Rietveld 408576698