OLD | NEW |
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 "SkPDFCanon.h" | 8 #include "SkPDFCanon.h" |
9 #include "SkPDFCanvas.h" | 9 #include "SkPDFCanvas.h" |
10 #include "SkPDFDevice.h" | 10 #include "SkPDFDevice.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 SkPDFObjectSerializer::~SkPDFObjectSerializer() { | 21 SkPDFObjectSerializer::~SkPDFObjectSerializer() { |
22 for (int i = 0; i < fObjNumMap.objects().count(); ++i) { | 22 for (int i = 0; i < fObjNumMap.objects().count(); ++i) { |
23 fObjNumMap.objects()[i]->drop(); | 23 fObjNumMap.objects()[i]->drop(); |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 void SkPDFObjectSerializer::addObjectRecursively(const sk_sp<SkPDFObject>& objec
t) { | 27 void SkPDFObjectSerializer::addObjectRecursively(const sk_sp<SkPDFObject>& objec
t) { |
28 fObjNumMap.addObjectRecursively(object.get(), fSubstituteMap); | 28 fObjNumMap.addObjectRecursively(object.get(), fSubstituteMap); |
29 } | 29 } |
30 | 30 |
| 31 #define SKPDF_MAGIC "\xD3\xEB\xE9\xE1" |
| 32 #ifndef SK_BUILD_FOR_WIN32 |
| 33 static_assert((SKPDF_MAGIC[0] & 0x7F) == "Skia"[0], ""); |
| 34 static_assert((SKPDF_MAGIC[1] & 0x7F) == "Skia"[1], ""); |
| 35 static_assert((SKPDF_MAGIC[2] & 0x7F) == "Skia"[2], ""); |
| 36 static_assert((SKPDF_MAGIC[3] & 0x7F) == "Skia"[3], ""); |
| 37 #endif |
31 void SkPDFObjectSerializer::serializeHeader(SkWStream* wStream, const SkPDFMetad
ata& md) { | 38 void SkPDFObjectSerializer::serializeHeader(SkWStream* wStream, const SkPDFMetad
ata& md) { |
32 fBaseOffset = wStream->bytesWritten(); | 39 fBaseOffset = wStream->bytesWritten(); |
33 static const char kHeader[] = "%PDF-1.4\n%\xE1\xE9\xEB\xD3\n"; | 40 static const char kHeader[] = "%PDF-1.4\n%" SKPDF_MAGIC "\n"; |
34 wStream->write(kHeader, strlen(kHeader)); | 41 wStream->write(kHeader, strlen(kHeader)); |
35 // The PDF spec recommends including a comment with four | 42 // The PDF spec recommends including a comment with four |
36 // bytes, all with their high bits set. "\xD3\xEB\xE9\xE1" is | 43 // bytes, all with their high bits set. "\xD3\xEB\xE9\xE1" is |
37 // "Skia" with the high bits set. | 44 // "Skia" with the high bits set. |
38 fInfoDict.reset(md.createDocumentInformationDict()); | 45 fInfoDict.reset(md.createDocumentInformationDict()); |
39 this->addObjectRecursively(fInfoDict); | 46 this->addObjectRecursively(fInfoDict); |
40 this->serializeObjects(wStream); | 47 this->serializeObjects(wStream); |
41 } | 48 } |
| 49 #undef SKPDF_MAGIC |
42 | 50 |
43 // Serialize all objects in the fObjNumMap that have not yet been serialized; | 51 // Serialize all objects in the fObjNumMap that have not yet been serialized; |
44 void SkPDFObjectSerializer::serializeObjects(SkWStream* wStream) { | 52 void SkPDFObjectSerializer::serializeObjects(SkWStream* wStream) { |
45 const SkTArray<sk_sp<SkPDFObject>>& objects = fObjNumMap.objects(); | 53 const SkTArray<sk_sp<SkPDFObject>>& objects = fObjNumMap.objects(); |
46 while (fNextToBeSerialized < objects.count()) { | 54 while (fNextToBeSerialized < objects.count()) { |
47 SkPDFObject* object = objects[fNextToBeSerialized].get(); | 55 SkPDFObject* object = objects[fNextToBeSerialized].get(); |
48 int32_t index = fNextToBeSerialized + 1; // Skip object 0. | 56 int32_t index = fNextToBeSerialized + 1; // Skip object 0. |
49 // "The first entry in the [XREF] table (object number 0) is | 57 // "The first entry in the [XREF] table (object number 0) is |
50 // always free and has a generation number of 65,535; it is | 58 // always free and has a generation number of 65,535; it is |
51 // the head of the linked list of free objects." | 59 // the head of the linked list of free objects." |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 return SkPDFMakeDocument(stream, nullptr, dpi, jpegEncoder).release(); | 368 return SkPDFMakeDocument(stream, nullptr, dpi, jpegEncoder).release(); |
361 } | 369 } |
362 | 370 |
363 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { | 371 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { |
364 auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; | 372 auto delete_wstream = [](SkWStream* stream, bool) { delete stream; }; |
365 std::unique_ptr<SkFILEWStream> stream(new SkFILEWStream(path)); | 373 std::unique_ptr<SkFILEWStream> stream(new SkFILEWStream(path)); |
366 return stream->isValid() | 374 return stream->isValid() |
367 ? SkPDFMakeDocument(stream.release(), delete_wstream, dpi, nullptr).rele
ase() | 375 ? SkPDFMakeDocument(stream.release(), delete_wstream, dpi, nullptr).rele
ase() |
368 : nullptr; | 376 : nullptr; |
369 } | 377 } |
OLD | NEW |