blob: 3e3a50ae835ebc688ba9f0e488bfd1a165e81f4b [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
Hal Canarya0622582017-06-29 18:51:35 -040017/* @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 */
halcanary4b656662016-04-27 07:45:18 -070027sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
28 void (*doneProc)(SkWStream*, bool),
29 SkScalar rasterDpi,
30 const SkDocument::PDFMetadata&,
31 sk_sp<SkPixelSerializer>,
32 bool pdfa);
halcanary23f4d4d2016-03-12 05:59:39 -080033
halcanary50e82e62016-03-21 13:45:05 -070034// Logically part of SkPDFDocument (like SkPDFCanon), but separate to
35// keep similar functionality together.
36struct SkPDFObjectSerializer : SkNoncopyable {
37 SkPDFObjNumMap fObjNumMap;
halcanary50e82e62016-03-21 13:45:05 -070038 SkTDArray<int32_t> fOffsets;
39 sk_sp<SkPDFObject> fInfoDict;
40 size_t fBaseOffset;
41 int32_t fNextToBeSerialized; // index in fObjNumMap
42
43 SkPDFObjectSerializer();
halcanarya50151d2016-03-25 11:57:49 -070044 ~SkPDFObjectSerializer();
halcanary50e82e62016-03-21 13:45:05 -070045 void addObjectRecursively(const sk_sp<SkPDFObject>&);
halcanary4b656662016-04-27 07:45:18 -070046 void serializeHeader(SkWStream*, const SkDocument::PDFMetadata&);
halcanary50e82e62016-03-21 13:45:05 -070047 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. */
55class SkPDFDocument : public SkDocument {
56public:
57 SkPDFDocument(SkWStream*,
58 void (*)(SkWStream*, bool),
59 SkScalar,
halcanary4b656662016-04-27 07:45:18 -070060 const SkDocument::PDFMetadata&,
61 sk_sp<SkPixelSerializer>,
halcanary488165e2016-04-22 06:10:21 -070062 bool);
Brian Salomond3b65972017-03-22 12:05:03 -040063 ~SkPDFDocument() override;
Hal Canaryb1de5f92017-07-01 22:17:15 -040064 SkCanvas* onBeginPage(SkScalar, SkScalar) override;
halcanary50e82e62016-03-21 13:45:05 -070065 void onEndPage() override;
reedd14df7c2016-09-22 14:12:46 -070066 void onClose(SkWStream*) override;
halcanary50e82e62016-03-21 13:45:05 -070067 void onAbort() override;
halcanarye86134f2016-09-06 09:32:13 -070068
halcanary50e82e62016-03-21 13:45:05 -070069 /**
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.
halcanary50e82e62016-03-21 13:45:05 -070077 */
78 void serialize(const sk_sp<SkPDFObject>&);
79 SkPDFCanon* canon() { return &fCanon; }
Hal Canarya0622582017-06-29 18:51:35 -040080 SkScalar rasterDpi() const { return fRasterDpi; }
halcanary530032a2016-08-18 14:22:52 -070081 void registerFont(SkPDFFont* f) { fFonts.add(f); }
halcanary50e82e62016-03-21 13:45:05 -070082
83private:
84 SkPDFObjectSerializer fObjectSerializer;
85 SkPDFCanon fCanon;
halcanarycc77c122016-03-23 06:26:31 -070086 SkTArray<sk_sp<SkPDFDict>> fPages;
halcanary530032a2016-08-18 14:22:52 -070087 SkTHashSet<SkPDFFont*> fFonts;
halcanarycc77c122016-03-23 06:26:31 -070088 sk_sp<SkPDFDict> fDests;
89 sk_sp<SkPDFDevice> fPageDevice;
Mike Reed5df49342016-11-12 08:06:55 -060090 std::unique_ptr<SkCanvas> fCanvas;
halcanary8cd4a242016-04-07 08:56:15 -070091 sk_sp<SkPDFObject> fID;
92 sk_sp<SkPDFObject> fXMP;
halcanary50e82e62016-03-21 13:45:05 -070093 SkScalar fRasterDpi;
halcanary4b656662016-04-27 07:45:18 -070094 SkDocument::PDFMetadata fMetadata;
halcanary488165e2016-04-22 06:10:21 -070095 bool fPDFA;
halcanary530032a2016-08-18 14:22:52 -070096
97 void reset();
halcanary50e82e62016-03-21 13:45:05 -070098};
99
halcanary23f4d4d2016-03-12 05:59:39 -0800100#endif // SkPDFDocument_DEFINED