1 /* 2 * Copyright 2016 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 #ifndef SkPDFDocument_DEFINED 8 #define SkPDFDocument_DEFINED 9 10 #include "SkDocument.h" 11 #include "SkPDFCanon.h" 12 #include "SkPDFMetadata.h" 13 #include "SkPDFFont.h" 14 15 class SkPDFDevice; 16 17 sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream, 18 void (*doneProc)(SkWStream*, bool), 19 SkScalar rasterDpi, 20 const SkDocument::PDFMetadata&, 21 sk_sp<SkPixelSerializer>, 22 bool pdfa); 23 24 // Logically part of SkPDFDocument (like SkPDFCanon), but separate to 25 // keep similar functionality together. 26 struct SkPDFObjectSerializer : SkNoncopyable { 27 SkPDFObjNumMap fObjNumMap; 28 SkTDArray<int32_t> fOffsets; 29 sk_sp<SkPDFObject> fInfoDict; 30 size_t fBaseOffset; 31 int32_t fNextToBeSerialized; // index in fObjNumMap 32 33 SkPDFObjectSerializer(); 34 ~SkPDFObjectSerializer(); 35 void addObjectRecursively(const sk_sp<SkPDFObject>&); 36 void serializeHeader(SkWStream*, const SkDocument::PDFMetadata&); 37 void serializeObjects(SkWStream*); 38 void serializeFooter(SkWStream*, const sk_sp<SkPDFObject>, sk_sp<SkPDFObject>); 39 int32_t offset(SkWStream*); 40 }; 41 42 /** Concrete implementation of SkDocument that creates PDF files. This 43 class does not produced linearized or optimized PDFs; instead it 44 it attempts to use a minimum amount of RAM. */ 45 class SkPDFDocument : public SkDocument { 46 public: 47 SkPDFDocument(SkWStream*, 48 void (*)(SkWStream*, bool), 49 SkScalar, 50 const SkDocument::PDFMetadata&, 51 sk_sp<SkPixelSerializer>, 52 bool); 53 ~SkPDFDocument() override; 54 SkCanvas* onBeginPage(SkScalar, SkScalar, const SkRect&) override; 55 void onEndPage() override; 56 void onClose(SkWStream*) override; 57 void onAbort() override; 58 59 /** 60 Serialize the object, as well as any other objects it 61 indirectly refers to. If any any other objects have been added 62 to the SkPDFObjNumMap without serializing them, they will be 63 serialized as well. 64 65 It might go without saying that objects should not be changed 66 after calling serialize, since those changes will be too late. 67 */ 68 void serialize(const sk_sp<SkPDFObject>&); canon()69 SkPDFCanon* canon() { return &fCanon; } registerFont(SkPDFFont * f)70 void registerFont(SkPDFFont* f) { fFonts.add(f); } 71 72 private: 73 SkPDFObjectSerializer fObjectSerializer; 74 SkPDFCanon fCanon; 75 SkTArray<sk_sp<SkPDFDict>> fPages; 76 SkTHashSet<SkPDFFont*> fFonts; 77 sk_sp<SkPDFDict> fDests; 78 sk_sp<SkPDFDevice> fPageDevice; 79 std::unique_ptr<SkCanvas> fCanvas; 80 sk_sp<SkPDFObject> fID; 81 sk_sp<SkPDFObject> fXMP; 82 SkScalar fRasterDpi; 83 SkDocument::PDFMetadata fMetadata; 84 bool fPDFA; 85 86 void reset(); 87 }; 88 89 #endif // SkPDFDocument_DEFINED 90