blob: 892489e446a5c9ec78a25a14006d74954de8100c [file] [log] [blame]
Hal Canary23564b92018-09-07 14:33:14 -04001// Copyright 2018 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3#ifndef SkPDFDocument_DEFINED
4#define SkPDFDocument_DEFINED
5
Mike Kleinc0bd9f92019-04-23 12:05:21 -05006#include "include/core/SkDocument.h"
Hal Canary23564b92018-09-07 14:33:14 -04007
Hal Canaryc056e162019-10-21 11:35:05 -04008#include "include/core/SkMilestone.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "include/core/SkScalar.h"
10#include "include/core/SkString.h"
11#include "include/core/SkTime.h"
Hal Canary23564b92018-09-07 14:33:14 -040012
Hal Canaryc056e162019-10-21 11:35:05 -040013#define SKPDF_STRING(X) SKPDF_STRING_IMPL(X)
14#define SKPDF_STRING_IMPL(X) #X
15
Hal Canary9a3f5542018-12-10 19:59:07 -050016class SkExecutor;
17
Hal Canary23564b92018-09-07 14:33:14 -040018namespace SkPDF {
19
Hal Canary279b65d2018-12-28 10:42:54 -050020/** Table 333 in PDF 32000-1:2008 ยง14.8.4.2
Dominic Mazzoni656cefe2018-09-25 20:29:15 -070021*/
22enum class DocumentStructureType {
Hal Canary279b65d2018-12-28 10:42:54 -050023 kDocument, //!< Document
24 kPart, //!< Part
25 kArt, //!< Article
26 kSect, //!< Section
27 kDiv, //!< Division
28 kBlockQuote, //!< Block quotation
29 kCaption, //!< Caption
30 kTOC, //!< Table of Contents
31 kTOCI, //!< Table of Contents Item
32 kIndex, //!< Index
33 kNonStruct, //!< Nonstructural element
34 kPrivate, //!< Private element
35 kH, //!< Heading
36 kH1, //!< Heading level 1
37 kH2, //!< Heading level 2
38 kH3, //!< Heading level 3
39 kH4, //!< Heading level 4
40 kH5, //!< Heading level 5
41 kH6, //!< Heading level 6
42 kP, //!< Paragraph
43 kL, //!< List
44 kLI, //!< List item
45 kLbl, //!< List item label
46 kLBody, //!< List item body
47 kTable, //!< Table
48 kTR, //!< Table row
49 kTH, //!< Table header cell
50 kTD, //!< Table data cell
51 kTHead, //!< Table header row group
52 kTBody, //!< Table body row group
53 kTFoot, //!< table footer row group
54 kSpan, //!< Span
55 kQuote, //!< Quotation
56 kNote, //!< Note
57 kReference, //!< Reference
58 kBibEntry, //!< Bibliography entry
59 kCode, //!< Code
60 kLink, //!< Link
61 kAnnot, //!< Annotation
62 kRuby, //!< Ruby annotation
63 kRB, //!< Ruby base text
64 kRT, //!< Ruby annotation text
65 kRP, //!< Ruby punctuation
66 kWarichu, //!< Warichu annotation
67 kWT, //!< Warichu text
68 kWP, //!< Warichu punctuation
69 kFigure, //!< Figure
70 kFormula, //!< Formula
71 kForm, //!< Form control (not like an HTML FORM element)
Dominic Mazzoni656cefe2018-09-25 20:29:15 -070072};
73
Hal Canary279b65d2018-12-28 10:42:54 -050074/** A node in a PDF structure tree, giving a semantic representation
75 of the content. Each node ID is associated with content
76 by passing the SkCanvas and node ID to SkPDF::SetNodeId() when drawing.
77 NodeIDs should be unique within each tree.
78*/
Dominic Mazzoni656cefe2018-09-25 20:29:15 -070079struct StructureElementNode {
80 const StructureElementNode* fChildren = nullptr;
81 size_t fChildCount;
82 int fNodeId;
83 DocumentStructureType fType;
84};
85
Hal Canary23564b92018-09-07 14:33:14 -040086/** Optional metadata to be passed into the PDF factory function.
87*/
88struct Metadata {
89 /** The document's title.
90 */
91 SkString fTitle;
92
93 /** The name of the person who created the document.
94 */
95 SkString fAuthor;
96
97 /** The subject of the document.
98 */
99 SkString fSubject;
100
101 /** Keywords associated with the document. Commas may be used to delineate
102 keywords within the string.
103 */
104 SkString fKeywords;
105
106 /** If the document was converted to PDF from another format,
107 the name of the conforming product that created the
108 original document from which it was converted.
109 */
110 SkString fCreator;
111
112 /** The product that is converting this document to PDF.
Hal Canary23564b92018-09-07 14:33:14 -0400113 */
Hal Canaryc056e162019-10-21 11:35:05 -0400114 SkString fProducer = SkString("Skia/PDF m" SKPDF_STRING(SK_MILESTONE));
Hal Canary23564b92018-09-07 14:33:14 -0400115
116 /** The date and time the document was created.
117 The zero default value represents an unknown/unset time.
118 */
119 SkTime::DateTime fCreation = {0, 0, 0, 0, 0, 0, 0, 0};
120
121 /** The date and time the document was most recently modified.
122 The zero default value represents an unknown/unset time.
123 */
124 SkTime::DateTime fModified = {0, 0, 0, 0, 0, 0, 0, 0};
125
126 /** The DPI (pixels-per-inch) at which features without native PDF support
127 will be rasterized (e.g. draw image with perspective, draw text with
128 perspective, ...) A larger DPI would create a PDF that reflects the
129 original intent with better fidelity, but it can make for larger PDF
130 files too, which would use more memory while rendering, and it would be
131 slower to be processed or sent online or to printer.
132 */
133 SkScalar fRasterDPI = SK_ScalarDefaultRasterDPI;
134
135 /** If true, include XMP metadata, a document UUID, and sRGB output intent
136 information. This adds length to the document and makes it
137 non-reproducable, but are necessary features for PDF/A-2b conformance
138 */
139 bool fPDFA = false;
140
141 /** Encoding quality controls the trade-off between size and quality. By
142 default this is set to 101 percent, which corresponds to lossless
143 encoding. If this value is set to a value <= 100, and the image is
144 opaque, it will be encoded (using JPEG) with that quality setting.
145 */
146 int fEncodingQuality = 101;
Dominic Mazzoni656cefe2018-09-25 20:29:15 -0700147
Hal Canary279b65d2018-12-28 10:42:54 -0500148 /** An optional tree of structured document tags that provide
149 a semantic representation of the content. The caller
150 should retain ownership.
151 */
Dominic Mazzoni656cefe2018-09-25 20:29:15 -0700152 const StructureElementNode* fStructureElementTreeRoot = nullptr;
Hal Canary9a3f5542018-12-10 19:59:07 -0500153
154 /** Executor to handle threaded work within PDF Backend. If this is nullptr,
155 then all work will be done serially on the main thread. To have worker
156 threads assist with various tasks, set this to a valid SkExecutor
157 instance. Currently used for executing Deflate algorithm in parallel.
158
Hal Canary279b65d2018-12-28 10:42:54 -0500159 If set, the PDF output will be non-reproducible in the order and
160 internal numbering of objects, but should render the same.
161
Hal Canary9a3f5542018-12-10 19:59:07 -0500162 Experimental.
163 */
164 SkExecutor* fExecutor = nullptr;
Hal Canary25dd2c92019-03-22 12:41:23 -0400165
166 /** Preferred Subsetter. Only respected if both are compiled in.
Hal Canarya1050ed2019-11-07 12:12:02 -0500167
168 The Sfntly subsetter is deprecated.
169
Hal Canary25dd2c92019-03-22 12:41:23 -0400170 Experimental.
171 */
172 enum Subsetter {
173 kHarfbuzz_Subsetter,
174 kSfntly_Subsetter,
175 } fSubsetter = kHarfbuzz_Subsetter;
Hal Canary23564b92018-09-07 14:33:14 -0400176};
177
Dominic Mazzoni656cefe2018-09-25 20:29:15 -0700178/** Associate a node ID with subsequent drawing commands in an
179 SkCanvas. The same node ID can appear in a StructureElementNode
180 in order to associate a document's structure element tree with
181 its content.
182
183 A node ID of zero indicates no node ID.
184
185 @param canvas The canvas used to draw to the PDF.
186 @param nodeId The node ID for subsequent drawing commands.
187*/
188SK_API void SetNodeId(SkCanvas* dst, int nodeID);
189
Hal Canary23564b92018-09-07 14:33:14 -0400190/** Create a PDF-backed document, writing the results into a SkWStream.
191
192 PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm.
193
194 @param stream A PDF document will be written to this stream. The document may write
195 to the stream at anytime during its lifetime, until either close() is
196 called or the document is deleted.
197 @param metadata a PDFmetadata object. Any fields may be left empty.
198
199 @returns NULL if there is an error, otherwise a newly created PDF-backed SkDocument.
200*/
Hal Canarybf79d5b2018-09-20 20:22:29 -0400201SK_API sk_sp<SkDocument> MakeDocument(SkWStream* stream, const Metadata& metadata);
Hal Canary23564b92018-09-07 14:33:14 -0400202
203static inline sk_sp<SkDocument> MakeDocument(SkWStream* stream) {
204 return MakeDocument(stream, Metadata());
205}
206
207} // namespace SkPDF
Hal Canaryc056e162019-10-21 11:35:05 -0400208
209#undef SKPDF_STRING
210#undef SKPDF_STRING_IMPL
Hal Canary23564b92018-09-07 14:33:14 -0400211#endif // SkPDFDocument_DEFINED