| 1 |
// 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 |
|
| 6 |
#include "include/core/SkDocument.h" |
| 7 |
#include "include/core/SkMilestone.h" |
| 8 |
#include "include/core/SkRefCnt.h" |
| 9 |
#include "include/core/SkScalar.h" |
| 10 |
#include "include/core/SkSpan.h" |
| 11 |
#include "include/core/SkString.h" |
| 12 |
#include "include/private/base/SkAPI.h" |
| 13 |
#include "include/private/base/SkMacros.h" |
| 14 |
#include "include/private/base/SkNoncopyable.h" |
| 15 |
|
| 16 |
#include <cstdint> |
| 17 |
#include <memory> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
class SkCanvas; |
| 21 |
class SkCodec; |
| 22 |
class SkData; |
| 23 |
class SkExecutor; |
| 24 |
class SkPDFArray; |
| 25 |
class SkPDFStructTree; |
| 26 |
class SkPixmap; |
| 27 |
class SkWStream; |
| 28 |
|
| 29 |
namespace SkPDF { |
| 30 |
|
| 31 |
/** Attributes for nodes in the PDF tree. |
| 32 |
* |
| 33 |
* Each attribute must have an owner (e.g. "Layout", "List", "Table", etc) |
| 34 |
* and an attribute name (e.g. "BBox", "RowSpan", etc.) from PDF32000_2008 14.8.5, |
| 35 |
* and then a value of the proper type according to the spec. |
| 36 |
* |
| 37 |
* Parameters of type `const char*` will not be copied and the pointers must remain valid until |
| 38 |
* `SkDocument::close` or `SkDocument::abort` is called. |
| 39 |
* Names are expected to be constants and are taken as `const char*`. |
| 40 |
* Parameters not taken as `const char*` will be copied. |
| 41 |
*/ |
| 42 |
class SK_API AttributeList : SkNoncopyable { |
| 43 |
public: |
| 44 |
AttributeList(); |
| 45 |
~AttributeList(); |
| 46 |
|
| 47 |
void appendInt(const char* owner, const char* name, int value); |
| 48 |
void appendFloat(const char* owner, const char* name, float value); |
| 49 |
void appendName(const char* owner, const char* name, const char* value); |
| 50 |
void appendTextString(const char* owner, const char* name, const char* value); |
| 51 |
void appendTextString(const char* owner, const char* name, SkString value); |
| 52 |
void appendFloatArray(const char* owner, const char* name, SkSpan<const float> value); |
| 53 |
void appendNodeIdArray(const char* owner, const char* name, SkSpan<const int> nodeIds); |
| 54 |
|
| 55 |
private: |
| 56 |
friend class ::SkPDFStructTree; |
| 57 |
|
| 58 |
std::unique_ptr<SkPDFArray> fAttrs; |
| 59 |
std::vector<int> fElemIds; // element identifiers referenced by fAttrs |
| 60 |
}; |
| 61 |
|
| 62 |
/** A node in a PDF structure tree, giving a semantic representation |
| 63 |
of the content. Each node ID is associated with content |
| 64 |
by passing the SkCanvas and node ID to SkPDF::SetNodeId() when drawing. |
| 65 |
NodeIDs should be unique within each tree. |
| 66 |
*/ |
| 67 |
struct StructureElementNode { |
| 68 |
SkString fTypeString; |
| 69 |
std::vector<std::unique_ptr<StructureElementNode>> fChildVector; |
| 70 |
int fNodeId = 0; |
| 71 |
AttributeList fAttributes; |
| 72 |
SkString fAlt; |
| 73 |
SkString fLang; |
| 74 |
}; |
| 75 |
|
| 76 |
struct DateTime { |
| 77 |
int16_t fTimeZoneMinutes; // The number of minutes that this |
| 78 |
// is ahead of or behind UTC. |
| 79 |
uint16_t fYear; //!< e.g. 2005 |
| 80 |
uint8_t fMonth; //!< 1..12 |
| 81 |
uint8_t fDayOfWeek; //!< 0..6, 0==Sunday |
| 82 |
uint8_t fDay; //!< 1..31 |
| 83 |
uint8_t fHour; //!< 0..23 |
| 84 |
uint8_t fMinute; //!< 0..59 |
| 85 |
uint8_t fSecond; //!< 0..59 |
| 86 |
|
| 87 |
void toISO8601(SkString* dst) const; |
| 88 |
}; |
| 89 |
|
| 90 |
using DecodeJpegCallback = std::unique_ptr<SkCodec> (*)(sk_sp<const SkData>); |
| 91 |
using EncodeJpegCallback = bool (*)(SkWStream* dst, const SkPixmap& src, int quality); |
| 92 |
|
| 93 |
/** Optional metadata to be passed into the PDF factory function. |
| 94 |
*/ |
| 95 |
struct Metadata { |
| 96 |
/** The document's title. |
| 97 |
*/ |
| 98 |
SkString fTitle; |
| 99 |
|
| 100 |
/** The name of the person who created the document. |
| 101 |
*/ |
| 102 |
SkString fAuthor; |
| 103 |
|
| 104 |
/** The subject of the document. |
| 105 |
*/ |
| 106 |
SkString fSubject; |
| 107 |
|
| 108 |
/** Keywords associated with the document. Commas may be used to delineate |
| 109 |
keywords within the string. |
| 110 |
*/ |
| 111 |
SkString fKeywords; |
| 112 |
|
| 113 |
/** If the document was converted to PDF from another format, |
| 114 |
the name of the conforming product that created the |
| 115 |
original document from which it was converted. |
| 116 |
*/ |
| 117 |
SkString fCreator; |
| 118 |
|
| 119 |
/** The product that is converting this document to PDF. |
| 120 |
*/ |
| 121 |
SkString fProducer = SkString("Skia/PDF m" SK_MACRO_STRINGIFY(SK_MILESTONE)); |
| 122 |
|
| 123 |
/** The date and time the document was created. |
| 124 |
The zero default value represents an unknown/unset time. |
| 125 |
*/ |
| 126 |
DateTime fCreation = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 127 |
|
| 128 |
/** The date and time the document was most recently modified. |
| 129 |
The zero default value represents an unknown/unset time. |
| 130 |
*/ |
| 131 |
DateTime fModified = {0, 0, 0, 0, 0, 0, 0, 0}; |
| 132 |
|
| 133 |
/** The natural language of the text in the PDF. If fLang is empty, the root |
| 134 |
StructureElementNode::fLang will be used (if not empty). Text not in |
| 135 |
this language should be marked with StructureElementNode::fLang. |
| 136 |
*/ |
| 137 |
SkString fLang; |
| 138 |
|
| 139 |
/** The DPI (pixels-per-inch) at which features without native PDF support |
| 140 |
will be rasterized (e.g. draw image with perspective, draw text with |
| 141 |
perspective, ...) A larger DPI would create a PDF that reflects the |
| 142 |
original intent with better fidelity, but it can make for larger PDF |
| 143 |
files too, which would use more memory while rendering, and it would be |
| 144 |
slower to be processed or sent online or to printer. |
| 145 |
*/ |
| 146 |
SkScalar fRasterDPI = SK_ScalarDefaultRasterDPI; |
| 147 |
|
| 148 |
/** If true, include XMP metadata, a document UUID, and sRGB output intent |
| 149 |
information. This adds length to the document and makes it |
| 150 |
non-reproducable, but are necessary features for PDF/A-2b conformance |
| 151 |
*/ |
| 152 |
bool fPDFA = false; |
| 153 |
|
| 154 |
/** Encoding quality controls the trade-off between size and quality. By |
| 155 |
default this is set to 101 percent, which corresponds to lossless |
| 156 |
encoding. If this value is set to a value <= 100, and the image is |
| 157 |
opaque, it will be encoded (using JPEG) with that quality setting. |
| 158 |
*/ |
| 159 |
int fEncodingQuality = 101; |
| 160 |
|
| 161 |
/** An optional tree of structured document tags that provide |
| 162 |
a semantic representation of the content. The caller |
| 163 |
should retain ownership. |
| 164 |
*/ |
| 165 |
StructureElementNode* fStructureElementTreeRoot = nullptr; |
| 166 |
|
| 167 |
enum class Outline : int { |
| 168 |
None = 0, |
| 169 |
StructureElementHeaders = 1, |
| 170 |
StructureElements = 2, |
| 171 |
} fOutline = Outline::None; |
| 172 |
|
| 173 |
/** Executor to handle threaded work within PDF Backend. If this is nullptr, |
| 174 |
then all work will be done serially on the main thread. To have worker |
| 175 |
threads assist with various tasks, set this to a valid SkExecutor |
| 176 |
instance. Currently used for executing Deflate algorithm in parallel. |
| 177 |
|
| 178 |
If set, the PDF output will be non-reproducible in the order and |
| 179 |
internal numbering of objects, but should render the same. |
| 180 |
|
| 181 |
Experimental. |
| 182 |
*/ |
| 183 |
SkExecutor* fExecutor = nullptr; |
| 184 |
|
| 185 |
/** PDF streams may be compressed to save space. |
| 186 |
Use this to specify the desired compression vs time tradeoff. |
| 187 |
*/ |
| 188 |
enum class CompressionLevel : int { |
| 189 |
Default = -1, |
| 190 |
None = 0, |
| 191 |
LowButFast = 1, |
| 192 |
Average = 6, |
| 193 |
HighButSlow = 9, |
| 194 |
} fCompressionLevel = CompressionLevel::Default; |
| 195 |
|
| 196 |
/** Preferred Subsetter. */ |
| 197 |
enum Subsetter { |
| 198 |
kHarfbuzz_Subsetter, |
| 199 |
} fSubsetter = kHarfbuzz_Subsetter; |
| 200 |
|
| 201 |
/** Clients can provide a way to decode jpeg. To use Skia's JPEG decoder, pass in |
| 202 |
SkJpegDecoder::Decode. If not supplied, all images will need to be re-encoded |
| 203 |
as jpegs or deflated images before embedding. If supplied, Skia may be able to |
| 204 |
skip the re-encoding step. |
| 205 |
Skia's JPEG decoder can be used here. |
| 206 |
*/ |
| 207 |
SkPDF::DecodeJpegCallback jpegDecoder = nullptr; |
| 208 |
|
| 209 |
/** Clients can provide a way to encode jpeg. If not supplied, images will be embedded |
| 210 |
as a deflated image, potentially making them much larger. If clients provide |
| 211 |
their own implementation, JPEGs should be encoded to RGB (not YUV) otherwise they |
| 212 |
will have the wrong surrounding metadata provided by Skia. |
| 213 |
Skia's JPEG encoder can be used here. |
| 214 |
*/ |
| 215 |
SkPDF::EncodeJpegCallback jpegEncoder = nullptr; |
| 216 |
|
| 217 |
// Skia's PDF support depends on having both a jpeg encoder and decoder for writing |
| 218 |
// compact PDFs. It will technically work, but produce larger than optimal PDFs |
| 219 |
// if either the decoder or encoder are left as nullptr. If clients will be creating |
| 220 |
// PDFs that don't use images or otherwise want to incur this cost (with the upside |
| 221 |
// of not having a jpeg library), they should set this to true to avoid an internal |
| 222 |
// assert from firing. |
| 223 |
bool allowNoJpegs = false; |
| 224 |
}; |
| 225 |
|
| 226 |
namespace NodeID { |
| 227 |
static const constexpr int Nothing = 0; |
| 228 |
static const constexpr int OtherArtifact = -1; |
| 229 |
static const constexpr int PaginationArtifact = -2; |
| 230 |
static const constexpr int PaginationHeaderArtifact = -3; |
| 231 |
static const constexpr int PaginationFooterArtifact = -4; |
| 232 |
static const constexpr int PaginationWatermarkArtifact = -5; |
| 233 |
static const constexpr int LayoutArtifact = -6; |
| 234 |
static const constexpr int PageArtifact = -7; |
| 235 |
static const constexpr int BackgroundArtifact = -8; |
| 236 |
} // namespace NodeID |
| 237 |
|
| 238 |
/** Associate a node ID with subsequent drawing commands in an |
| 239 |
SkCanvas. The same node ID can appear in a StructureElementNode |
| 240 |
in order to associate a document's structure element tree with |
| 241 |
its content. |
| 242 |
|
| 243 |
A node ID of zero indicates no node ID. Negative node IDs are reserved. |
| 244 |
|
| 245 |
@param canvas The canvas used to draw to the PDF. |
| 246 |
@param nodeId The node ID for subsequent drawing commands. |
| 247 |
*/ |
| 248 |
SK_API void SetNodeId(SkCanvas* dst, int nodeID); |
| 249 |
|
| 250 |
/** Create a PDF-backed document, writing the results into a SkWStream. |
| 251 |
|
| 252 |
PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm. |
| 253 |
|
| 254 |
@param stream A PDF document will be written to this stream. The document may write |
| 255 |
to the stream at anytime during its lifetime, until either close() is |
| 256 |
called or the document is deleted. |
| 257 |
@param metadata a PDFmetadata object. Some fields may be left empty. |
| 258 |
|
| 259 |
@returns NULL if there is an error, otherwise a newly created PDF-backed SkDocument. |
| 260 |
*/ |
| 261 |
SK_API sk_sp<SkDocument> MakeDocument(SkWStream* stream, const Metadata& metadata); |
| 262 |
|
| 263 |
#if !defined(SK_DISABLE_LEGACY_PDF_JPEG) |
| 264 |
static inline sk_sp<SkDocument> MakeDocument(SkWStream* stream) { |
| 265 |
return MakeDocument(stream, Metadata()); |
| 266 |
} |
| 267 |
#endif |
| 268 |
|
| 269 |
} // namespace SkPDF |
| 270 |
|
| 271 |
#endif // SkPDFDocument_DEFINED |