OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
| 9 #include "SkDeflate.h" |
9 #include "SkPDFTypes.h" | 10 #include "SkPDFTypes.h" |
10 #include "SkPDFUtils.h" | 11 #include "SkPDFUtils.h" |
11 #include "SkStream.h" | 12 #include "SkStreamPriv.h" |
12 | |
13 #ifdef SK_BUILD_FOR_WIN | |
14 #define SNPRINTF _snprintf | |
15 #else | |
16 #define SNPRINTF snprintf | |
17 #endif | |
18 | 13 |
19 //////////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////////// |
20 | 15 |
21 SkString* pun(char* x) { return reinterpret_cast<SkString*>(x); } | 16 SkString* pun(char* x) { return reinterpret_cast<SkString*>(x); } |
22 const SkString* pun(const char* x) { | 17 const SkString* pun(const char* x) { |
23 return reinterpret_cast<const SkString*>(x); | 18 return reinterpret_cast<const SkString*>(x); |
24 } | 19 } |
25 | 20 |
26 SkPDFUnion::SkPDFUnion(Type t) : fType(t) {} | 21 SkPDFUnion::SkPDFUnion(Type t) : fType(t) {} |
27 | 22 |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 SkPDFDict::SkPDFDict() {} | 343 SkPDFDict::SkPDFDict() {} |
349 | 344 |
350 SkPDFDict::~SkPDFDict() { this->clear(); } | 345 SkPDFDict::~SkPDFDict() { this->clear(); } |
351 | 346 |
352 SkPDFDict::SkPDFDict(const char type[]) { this->insertName("Type", type); } | 347 SkPDFDict::SkPDFDict(const char type[]) { this->insertName("Type", type); } |
353 | 348 |
354 void SkPDFDict::emitObject(SkWStream* stream, | 349 void SkPDFDict::emitObject(SkWStream* stream, |
355 const SkPDFObjNumMap& objNumMap, | 350 const SkPDFObjNumMap& objNumMap, |
356 const SkPDFSubstituteMap& substitutes) { | 351 const SkPDFSubstituteMap& substitutes) { |
357 stream->writeText("<<"); | 352 stream->writeText("<<"); |
| 353 this->emitAll(stream, objNumMap, substitutes); |
| 354 stream->writeText(">>"); |
| 355 } |
| 356 |
| 357 void SkPDFDict::emitAll(SkWStream* stream, |
| 358 const SkPDFObjNumMap& objNumMap, |
| 359 const SkPDFSubstituteMap& substitutes) { |
358 for (int i = 0; i < fRecords.count(); i++) { | 360 for (int i = 0; i < fRecords.count(); i++) { |
359 fRecords[i].fKey.emitObject(stream, objNumMap, substitutes); | 361 fRecords[i].fKey.emitObject(stream, objNumMap, substitutes); |
360 stream->writeText(" "); | 362 stream->writeText(" "); |
361 fRecords[i].fValue.emitObject(stream, objNumMap, substitutes); | 363 fRecords[i].fValue.emitObject(stream, objNumMap, substitutes); |
362 if (i + 1 < fRecords.count()) { | 364 if (i + 1 < fRecords.count()) { |
363 stream->writeText("\n"); | 365 stream->writeText("\n"); |
364 } | 366 } |
365 } | 367 } |
366 stream->writeText(">>"); | |
367 } | 368 } |
368 | 369 |
369 void SkPDFDict::addResources(SkPDFObjNumMap* catalog, | 370 void SkPDFDict::addResources(SkPDFObjNumMap* catalog, |
370 const SkPDFSubstituteMap& substitutes) const { | 371 const SkPDFSubstituteMap& substitutes) const { |
371 for (int i = 0; i < fRecords.count(); i++) { | 372 for (int i = 0; i < fRecords.count(); i++) { |
372 fRecords[i].fKey.addResources(catalog, substitutes); | 373 fRecords[i].fKey.addResources(catalog, substitutes); |
373 fRecords[i].fValue.addResources(catalog, substitutes); | 374 fRecords[i].fValue.addResources(catalog, substitutes); |
374 } | 375 } |
375 } | 376 } |
376 | 377 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 void SkPDFDict::clear() { | 433 void SkPDFDict::clear() { |
433 for (Record& rec : fRecords) { | 434 for (Record& rec : fRecords) { |
434 rec.fKey.~SkPDFUnion(); | 435 rec.fKey.~SkPDFUnion(); |
435 rec.fValue.~SkPDFUnion(); | 436 rec.fValue.~SkPDFUnion(); |
436 } | 437 } |
437 fRecords.reset(); | 438 fRecords.reset(); |
438 } | 439 } |
439 | 440 |
440 //////////////////////////////////////////////////////////////////////////////// | 441 //////////////////////////////////////////////////////////////////////////////// |
441 | 442 |
| 443 void SkPDFSharedStream::emitObject(SkWStream* stream, |
| 444 const SkPDFObjNumMap& objNumMap, |
| 445 const SkPDFSubstituteMap& substitutes) { |
| 446 SkDynamicMemoryWStream buffer; |
| 447 SkDeflateWStream deflateWStream(&buffer); |
| 448 // Note: emitObject isn't marked const, but could be in the future |
| 449 // This is why this function doesn't change the dictionary. |
| 450 SkAutoTDelete<SkStreamAsset> dup(fAsset->duplicate()); // Cheap copy |
| 451 SkStreamCopy(&deflateWStream, dup.get()); |
| 452 deflateWStream.finalize(); |
| 453 size_t length = buffer.bytesWritten(); |
| 454 stream->writeText("<<"); |
| 455 fDict->emitAll(stream, objNumMap, substitutes); |
| 456 stream->writeText("\n"); |
| 457 SkPDFUnion::Name("Length").emitObject(stream, objNumMap, substitutes); |
| 458 stream->writeText(" "); |
| 459 SkPDFUnion::Int(length).emitObject(stream, objNumMap, substitutes); |
| 460 stream->writeText("\n"); |
| 461 SkPDFUnion::Name("Filter").emitObject(stream, objNumMap, substitutes); |
| 462 stream->writeText(" "); |
| 463 SkPDFUnion::Name("FlateDecode").emitObject(stream, objNumMap, substitutes); |
| 464 stream->writeText(">>"); |
| 465 stream->writeText(" stream\n"); |
| 466 buffer.writeToStream(stream); |
| 467 stream->writeText("\nendstream"); |
| 468 } |
| 469 |
| 470 void SkPDFSharedStream::addResources( |
| 471 SkPDFObjNumMap* catalog, const SkPDFSubstituteMap& substitutes) const { |
| 472 fDict->addResources(catalog, substitutes); |
| 473 } |
| 474 |
| 475 //////////////////////////////////////////////////////////////////////////////// |
| 476 |
442 SkPDFSubstituteMap::~SkPDFSubstituteMap() { | 477 SkPDFSubstituteMap::~SkPDFSubstituteMap() { |
443 fSubstituteMap.foreach( | 478 fSubstituteMap.foreach( |
444 [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); | 479 [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); |
445 } | 480 } |
446 | 481 |
447 void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, | 482 void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, |
448 SkPDFObject* substitute) { | 483 SkPDFObject* substitute) { |
449 SkASSERT(original != substitute); | 484 SkASSERT(original != substitute); |
450 SkASSERT(!fSubstituteMap.find(original)); | 485 SkASSERT(!fSubstituteMap.find(original)); |
451 fSubstituteMap.set(original, SkRef(substitute)); | 486 fSubstituteMap.set(original, SkRef(substitute)); |
(...skipping 14 matching lines...) Expand all Loading... |
466 fObjects.push(obj); | 501 fObjects.push(obj); |
467 return true; | 502 return true; |
468 } | 503 } |
469 | 504 |
470 int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const { | 505 int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const { |
471 int32_t* objectNumberFound = fObjectNumbers.find(obj); | 506 int32_t* objectNumberFound = fObjectNumbers.find(obj); |
472 SkASSERT(objectNumberFound); | 507 SkASSERT(objectNumberFound); |
473 return *objectNumberFound; | 508 return *objectNumberFound; |
474 } | 509 } |
475 | 510 |
OLD | NEW |