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

Side by Side Diff: src/pdf/SkPDFDocument.cpp

Issue 1849453004: SkPDF: Make magic make sense (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-03-30 (Wednesday) 16:17:14 EDT Created 4 years, 8 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 | « no previous file | no next file » | 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 "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
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
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 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698