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 /* @param rasterDpi the DPI at which features without native PDF 18 * support will be rasterized (e.g. draw image with 19 * perspective, draw text with perspective, ...). A 20 * larger DPI would create a PDF that reflects the 21 * original intent with better fidelity, but it can make 22 * for larger PDF files too, which would use more memory 23 * while rendering, and it would be slower to be processed 24 * or sent online or to printer. A good choice is 25 * SK_ScalarDefaultRasterDPI(72.0f). 26 */ 27 sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream, 28 void (*doneProc)(SkWStream*, bool), 29 SkScalar rasterDpi, 30 const SkDocument::PDFMetadata&, 31 sk_sp<SkPixelSerializer>, 32 bool pdfa); 33 34 // Logically part of SkPDFDocument (like SkPDFCanon), but separate to 35 // keep similar functionality together. 36 struct SkPDFObjectSerializer : SkNoncopyable { 37 SkPDFObjNumMap fObjNumMap; 38 SkTDArray<int32_t> fOffsets; 39 sk_sp<SkPDFObject> fInfoDict; 40 size_t fBaseOffset; 41 int32_t fNextToBeSerialized; // index in fObjNumMap 42 43 SkPDFObjectSerializer(); 44 ~SkPDFObjectSerializer(); 45 void addObjectRecursively(const sk_sp<SkPDFObject>&); 46 void serializeHeader(SkWStream*, const SkDocument::PDFMetadata&); 47 void serializeObjects(SkWStream*); 48 void serializeFooter(SkWStream*, const sk_sp<SkPDFObject>, sk_sp<SkPDFObject>); 49 int32_t offset(SkWStream*); 50 }; 51 52 /** Concrete implementation of SkDocument that creates PDF files. This 53 class does not produced linearized or optimized PDFs; instead it 54 it attempts to use a minimum amount of RAM. */ 55 class SkPDFDocument : public SkDocument { 56 public: 57 SkPDFDocument(SkWStream*, 58 void (*)(SkWStream*, bool), 59 SkScalar, 60 const SkDocument::PDFMetadata&, 61 sk_sp<SkPixelSerializer>, 62 bool); 63 ~SkPDFDocument() override; 64 SkCanvas* onBeginPage(SkScalar, SkScalar) override; 65 void onEndPage() override; 66 void onClose(SkWStream*) override; 67 void onAbort() override; 68 69 /** 70 Serialize the object, as well as any other objects it 71 indirectly refers to. If any any other objects have been added 72 to the SkPDFObjNumMap without serializing them, they will be 73 serialized as well. 74 75 It might go without saying that objects should not be changed 76 after calling serialize, since those changes will be too late. 77 */ 78 void serialize(const sk_sp<SkPDFObject>&); canon()79 SkPDFCanon* canon() { return &fCanon; } rasterDpi()80 SkScalar rasterDpi() const { return fRasterDpi; } registerFont(SkPDFFont * f)81 void registerFont(SkPDFFont* f) { fFonts.add(f); } 82 83 private: 84 SkPDFObjectSerializer fObjectSerializer; 85 SkPDFCanon fCanon; 86 SkTArray<sk_sp<SkPDFDict>> fPages; 87 SkTHashSet<SkPDFFont*> fFonts; 88 sk_sp<SkPDFDict> fDests; 89 sk_sp<SkPDFDevice> fPageDevice; 90 std::unique_ptr<SkCanvas> fCanvas; 91 sk_sp<SkPDFObject> fID; 92 sk_sp<SkPDFObject> fXMP; 93 SkScalar fRasterDpi; 94 SkDocument::PDFMetadata fMetadata; 95 bool fPDFA; 96 97 void reset(); 98 }; 99 100 #endif // SkPDFDocument_DEFINED 101