blob: 15e147961d097f50f30a749c4f1fbbdffa9e8c59 [file] [log] [blame]
halcanary23f4d4d2016-03-12 05:59:39 -08001/*
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"
halcanaryd11c7262016-03-25 05:52:57 -070011#include "SkPDFCanon.h"
halcanary50e82e62016-03-21 13:45:05 -070012#include "SkPDFMetadata.h"
halcanarycc77c122016-03-23 06:26:31 -070013#include "SkPDFFont.h"
halcanary23f4d4d2016-03-12 05:59:39 -080014
halcanary989da4a2016-03-21 14:33:17 -070015class SkPDFDevice;
16
halcanary4b656662016-04-27 07:45:18 -070017sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
18 void (*doneProc)(SkWStream*, bool),
19 SkScalar rasterDpi,
20 const SkDocument::PDFMetadata&,
21 sk_sp<SkPixelSerializer>,
22 bool pdfa);
halcanary23f4d4d2016-03-12 05:59:39 -080023
halcanary50e82e62016-03-21 13:45:05 -070024// Logically part of SkPDFDocument (like SkPDFCanon), but separate to
25// keep similar functionality together.
26struct SkPDFObjectSerializer : SkNoncopyable {
27 SkPDFObjNumMap fObjNumMap;
halcanary50e82e62016-03-21 13:45:05 -070028 SkTDArray<int32_t> fOffsets;
29 sk_sp<SkPDFObject> fInfoDict;
30 size_t fBaseOffset;
31 int32_t fNextToBeSerialized; // index in fObjNumMap
32
33 SkPDFObjectSerializer();
halcanarya50151d2016-03-25 11:57:49 -070034 ~SkPDFObjectSerializer();
halcanary50e82e62016-03-21 13:45:05 -070035 void addObjectRecursively(const sk_sp<SkPDFObject>&);
halcanary4b656662016-04-27 07:45:18 -070036 void serializeHeader(SkWStream*, const SkDocument::PDFMetadata&);
halcanary50e82e62016-03-21 13:45:05 -070037 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. */
45class SkPDFDocument : public SkDocument {
46public:
47 SkPDFDocument(SkWStream*,
48 void (*)(SkWStream*, bool),
49 SkScalar,
halcanary4b656662016-04-27 07:45:18 -070050 const SkDocument::PDFMetadata&,
51 sk_sp<SkPixelSerializer>,
halcanary488165e2016-04-22 06:10:21 -070052 bool);
halcanary50e82e62016-03-21 13:45:05 -070053 virtual ~SkPDFDocument();
54 SkCanvas* onBeginPage(SkScalar, SkScalar, const SkRect&) override;
55 void onEndPage() override;
reedd14df7c2016-09-22 14:12:46 -070056 void onClose(SkWStream*) override;
halcanary50e82e62016-03-21 13:45:05 -070057 void onAbort() override;
halcanarye86134f2016-09-06 09:32:13 -070058
halcanary50e82e62016-03-21 13:45:05 -070059 /**
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.
halcanary50e82e62016-03-21 13:45:05 -070067 */
68 void serialize(const sk_sp<SkPDFObject>&);
69 SkPDFCanon* canon() { return &fCanon; }
halcanary530032a2016-08-18 14:22:52 -070070 void registerFont(SkPDFFont* f) { fFonts.add(f); }
halcanary50e82e62016-03-21 13:45:05 -070071
72private:
73 SkPDFObjectSerializer fObjectSerializer;
74 SkPDFCanon fCanon;
halcanarycc77c122016-03-23 06:26:31 -070075 SkTArray<sk_sp<SkPDFDict>> fPages;
halcanary530032a2016-08-18 14:22:52 -070076 SkTHashSet<SkPDFFont*> fFonts;
halcanarycc77c122016-03-23 06:26:31 -070077 sk_sp<SkPDFDict> fDests;
78 sk_sp<SkPDFDevice> fPageDevice;
Mike Reed5df49342016-11-12 08:06:55 -060079 std::unique_ptr<SkCanvas> fCanvas;
halcanary8cd4a242016-04-07 08:56:15 -070080 sk_sp<SkPDFObject> fID;
81 sk_sp<SkPDFObject> fXMP;
halcanary50e82e62016-03-21 13:45:05 -070082 SkScalar fRasterDpi;
halcanary4b656662016-04-27 07:45:18 -070083 SkDocument::PDFMetadata fMetadata;
halcanary488165e2016-04-22 06:10:21 -070084 bool fPDFA;
halcanary530032a2016-08-18 14:22:52 -070085
86 void reset();
halcanary50e82e62016-03-21 13:45:05 -070087};
88
halcanary23f4d4d2016-03-12 05:59:39 -080089#endif // SkPDFDocument_DEFINED