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
15class 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 */
27sk_sp<SkDocument> SkPDFMakeDocument(SkWStream* stream,
28                                    const SkDocument::PDFMetadata&);
29
30// Logically part of SkPDFDocument (like SkPDFCanon), but separate to
31// keep similar functionality together.
32struct SkPDFObjectSerializer : SkNoncopyable {
33    SkPDFObjNumMap fObjNumMap;
34    SkTDArray<int32_t> fOffsets;
35    sk_sp<SkPDFObject> fInfoDict;
36    size_t fBaseOffset;
37    int32_t fNextToBeSerialized;  // index in fObjNumMap
38
39    SkPDFObjectSerializer();
40    ~SkPDFObjectSerializer();
41    void addObjectRecursively(const sk_sp<SkPDFObject>&);
42    void serializeHeader(SkWStream*, const SkDocument::PDFMetadata&);
43    void serializeObjects(SkWStream*);
44    void serializeFooter(SkWStream*, const sk_sp<SkPDFObject>, sk_sp<SkPDFObject>);
45    int32_t offset(SkWStream*);
46};
47
48/** Concrete implementation of SkDocument that creates PDF files. This
49    class does not produced linearized or optimized PDFs; instead it
50    it attempts to use a minimum amount of RAM. */
51class SkPDFDocument : public SkDocument {
52public:
53    SkPDFDocument(SkWStream*,
54                  const SkDocument::PDFMetadata&);
55    ~SkPDFDocument() override;
56    SkCanvas* onBeginPage(SkScalar, SkScalar) override;
57    void onEndPage() override;
58    void onClose(SkWStream*) override;
59    void onAbort() override;
60
61    /**
62       Serialize the object, as well as any other objects it
63       indirectly refers to.  If any any other objects have been added
64       to the SkPDFObjNumMap without serializing them, they will be
65       serialized as well.
66
67       It might go without saying that objects should not be changed
68       after calling serialize, since those changes will be too late.
69     */
70    void serialize(const sk_sp<SkPDFObject>&);
71    SkPDFCanon* canon() { return &fCanon; }
72    SkScalar rasterDpi() const { return fMetadata.fRasterDPI; }
73    void registerFont(SkPDFFont* f) { fFonts.add(f); }
74    const PDFMetadata& metadata() const { return fMetadata; }
75
76private:
77    SkPDFObjectSerializer fObjectSerializer;
78    SkPDFCanon fCanon;
79    SkTArray<sk_sp<SkPDFDict>> fPages;
80    SkTHashSet<SkPDFFont*> fFonts;
81    sk_sp<SkPDFDict> fDests;
82    sk_sp<SkPDFDevice> fPageDevice;
83    std::unique_ptr<SkCanvas> fCanvas;
84    sk_sp<SkPDFObject> fID;
85    sk_sp<SkPDFObject> fXMP;
86    SkDocument::PDFMetadata fMetadata;
87
88    void reset();
89};
90
91#endif  // SkPDFDocument_DEFINED
92