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) const { | 351 const SkPDFSubstituteMap& substitutes) const { |
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) const { |
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( |
| 444 SkWStream* stream, |
| 445 const SkPDFObjNumMap& objNumMap, |
| 446 const SkPDFSubstituteMap& substitutes) const { |
| 447 SkDynamicMemoryWStream buffer; |
| 448 SkDeflateWStream deflateWStream(&buffer); |
| 449 // Since emitObject is const, this function doesn't change the dictionary. |
| 450 SkAutoTDelete<SkStreamAsset> dup(fAsset->duplicate()); // Cheap copy |
| 451 SkASSERT(dup); |
| 452 SkStreamCopy(&deflateWStream, dup.get()); |
| 453 deflateWStream.finalize(); |
| 454 size_t length = buffer.bytesWritten(); |
| 455 stream->writeText("<<"); |
| 456 fDict->emitAll(stream, objNumMap, substitutes); |
| 457 stream->writeText("\n"); |
| 458 SkPDFUnion::Name("Length").emitObject(stream, objNumMap, substitutes); |
| 459 stream->writeText(" "); |
| 460 SkPDFUnion::Int(length).emitObject(stream, objNumMap, substitutes); |
| 461 stream->writeText("\n"); |
| 462 SkPDFUnion::Name("Filter").emitObject(stream, objNumMap, substitutes); |
| 463 stream->writeText(" "); |
| 464 SkPDFUnion::Name("FlateDecode").emitObject(stream, objNumMap, substitutes); |
| 465 stream->writeText(">>"); |
| 466 stream->writeText(" stream\n"); |
| 467 buffer.writeToStream(stream); |
| 468 stream->writeText("\nendstream"); |
| 469 } |
| 470 |
| 471 void SkPDFSharedStream::addResources( |
| 472 SkPDFObjNumMap* catalog, const SkPDFSubstituteMap& substitutes) const { |
| 473 fDict->addResources(catalog, substitutes); |
| 474 } |
| 475 |
| 476 //////////////////////////////////////////////////////////////////////////////// |
| 477 |
442 SkPDFSubstituteMap::~SkPDFSubstituteMap() { | 478 SkPDFSubstituteMap::~SkPDFSubstituteMap() { |
443 fSubstituteMap.foreach( | 479 fSubstituteMap.foreach( |
444 [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); | 480 [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); |
445 } | 481 } |
446 | 482 |
447 void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, | 483 void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, |
448 SkPDFObject* substitute) { | 484 SkPDFObject* substitute) { |
449 SkASSERT(original != substitute); | 485 SkASSERT(original != substitute); |
450 SkASSERT(!fSubstituteMap.find(original)); | 486 SkASSERT(!fSubstituteMap.find(original)); |
451 fSubstituteMap.set(original, SkRef(substitute)); | 487 fSubstituteMap.set(original, SkRef(substitute)); |
(...skipping 14 matching lines...) Expand all Loading... |
466 fObjects.push(obj); | 502 fObjects.push(obj); |
467 return true; | 503 return true; |
468 } | 504 } |
469 | 505 |
470 int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const { | 506 int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const { |
471 int32_t* objectNumberFound = fObjectNumbers.find(obj); | 507 int32_t* objectNumberFound = fObjectNumbers.find(obj); |
472 SkASSERT(objectNumberFound); | 508 SkASSERT(objectNumberFound); |
473 return *objectNumberFound; | 509 return *objectNumberFound; |
474 } | 510 } |
475 | 511 |
OLD | NEW |