SkPDFDocument.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include "include/core/SkDocument.h"
  6. #include "include/core/SkScalar.h"
  7. #include "include/core/SkString.h"
  8. #include "include/core/SkTime.h"
  9. class SkExecutor;
  10. namespace SkPDF {
  11. /** Table 333 in PDF 32000-1:2008 §14.8.4.2
  12. */
  13. enum class DocumentStructureType {
  14. kDocument, //!< Document
  15. kPart, //!< Part
  16. kArt, //!< Article
  17. kSect, //!< Section
  18. kDiv, //!< Division
  19. kBlockQuote, //!< Block quotation
  20. kCaption, //!< Caption
  21. kTOC, //!< Table of Contents
  22. kTOCI, //!< Table of Contents Item
  23. kIndex, //!< Index
  24. kNonStruct, //!< Nonstructural element
  25. kPrivate, //!< Private element
  26. kH, //!< Heading
  27. kH1, //!< Heading level 1
  28. kH2, //!< Heading level 2
  29. kH3, //!< Heading level 3
  30. kH4, //!< Heading level 4
  31. kH5, //!< Heading level 5
  32. kH6, //!< Heading level 6
  33. kP, //!< Paragraph
  34. kL, //!< List
  35. kLI, //!< List item
  36. kLbl, //!< List item label
  37. kLBody, //!< List item body
  38. kTable, //!< Table
  39. kTR, //!< Table row
  40. kTH, //!< Table header cell
  41. kTD, //!< Table data cell
  42. kTHead, //!< Table header row group
  43. kTBody, //!< Table body row group
  44. kTFoot, //!< table footer row group
  45. kSpan, //!< Span
  46. kQuote, //!< Quotation
  47. kNote, //!< Note
  48. kReference, //!< Reference
  49. kBibEntry, //!< Bibliography entry
  50. kCode, //!< Code
  51. kLink, //!< Link
  52. kAnnot, //!< Annotation
  53. kRuby, //!< Ruby annotation
  54. kRB, //!< Ruby base text
  55. kRT, //!< Ruby annotation text
  56. kRP, //!< Ruby punctuation
  57. kWarichu, //!< Warichu annotation
  58. kWT, //!< Warichu text
  59. kWP, //!< Warichu punctuation
  60. kFigure, //!< Figure
  61. kFormula, //!< Formula
  62. kForm, //!< Form control (not like an HTML FORM element)
  63. };
  64. /** A node in a PDF structure tree, giving a semantic representation
  65. of the content. Each node ID is associated with content
  66. by passing the SkCanvas and node ID to SkPDF::SetNodeId() when drawing.
  67. NodeIDs should be unique within each tree.
  68. */
  69. struct StructureElementNode {
  70. const StructureElementNode* fChildren = nullptr;
  71. size_t fChildCount;
  72. int fNodeId;
  73. DocumentStructureType fType;
  74. };
  75. /** Optional metadata to be passed into the PDF factory function.
  76. */
  77. struct Metadata {
  78. /** The document's title.
  79. */
  80. SkString fTitle;
  81. /** The name of the person who created the document.
  82. */
  83. SkString fAuthor;
  84. /** The subject of the document.
  85. */
  86. SkString fSubject;
  87. /** Keywords associated with the document. Commas may be used to delineate
  88. keywords within the string.
  89. */
  90. SkString fKeywords;
  91. /** If the document was converted to PDF from another format,
  92. the name of the conforming product that created the
  93. original document from which it was converted.
  94. */
  95. SkString fCreator;
  96. /** The product that is converting this document to PDF.
  97. Leave fProducer empty to get the default, correct value.
  98. */
  99. SkString fProducer;
  100. /** The date and time the document was created.
  101. The zero default value represents an unknown/unset time.
  102. */
  103. SkTime::DateTime fCreation = {0, 0, 0, 0, 0, 0, 0, 0};
  104. /** The date and time the document was most recently modified.
  105. The zero default value represents an unknown/unset time.
  106. */
  107. SkTime::DateTime fModified = {0, 0, 0, 0, 0, 0, 0, 0};
  108. /** The DPI (pixels-per-inch) at which features without native PDF support
  109. will be rasterized (e.g. draw image with perspective, draw text with
  110. perspective, ...) A larger DPI would create a PDF that reflects the
  111. original intent with better fidelity, but it can make for larger PDF
  112. files too, which would use more memory while rendering, and it would be
  113. slower to be processed or sent online or to printer.
  114. */
  115. SkScalar fRasterDPI = SK_ScalarDefaultRasterDPI;
  116. /** If true, include XMP metadata, a document UUID, and sRGB output intent
  117. information. This adds length to the document and makes it
  118. non-reproducable, but are necessary features for PDF/A-2b conformance
  119. */
  120. bool fPDFA = false;
  121. /** Encoding quality controls the trade-off between size and quality. By
  122. default this is set to 101 percent, which corresponds to lossless
  123. encoding. If this value is set to a value <= 100, and the image is
  124. opaque, it will be encoded (using JPEG) with that quality setting.
  125. */
  126. int fEncodingQuality = 101;
  127. /** An optional tree of structured document tags that provide
  128. a semantic representation of the content. The caller
  129. should retain ownership.
  130. */
  131. const StructureElementNode* fStructureElementTreeRoot = nullptr;
  132. /** Executor to handle threaded work within PDF Backend. If this is nullptr,
  133. then all work will be done serially on the main thread. To have worker
  134. threads assist with various tasks, set this to a valid SkExecutor
  135. instance. Currently used for executing Deflate algorithm in parallel.
  136. If set, the PDF output will be non-reproducible in the order and
  137. internal numbering of objects, but should render the same.
  138. Experimental.
  139. */
  140. SkExecutor* fExecutor = nullptr;
  141. /** Preferred Subsetter. Only respected if both are compiled in.
  142. Experimental.
  143. */
  144. enum Subsetter {
  145. kHarfbuzz_Subsetter,
  146. kSfntly_Subsetter,
  147. } fSubsetter = kHarfbuzz_Subsetter;
  148. };
  149. /** Associate a node ID with subsequent drawing commands in an
  150. SkCanvas. The same node ID can appear in a StructureElementNode
  151. in order to associate a document's structure element tree with
  152. its content.
  153. A node ID of zero indicates no node ID.
  154. @param canvas The canvas used to draw to the PDF.
  155. @param nodeId The node ID for subsequent drawing commands.
  156. */
  157. SK_API void SetNodeId(SkCanvas* dst, int nodeID);
  158. /** Create a PDF-backed document, writing the results into a SkWStream.
  159. PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm.
  160. @param stream A PDF document will be written to this stream. The document may write
  161. to the stream at anytime during its lifetime, until either close() is
  162. called or the document is deleted.
  163. @param metadata a PDFmetadata object. Any fields may be left empty.
  164. @returns NULL if there is an error, otherwise a newly created PDF-backed SkDocument.
  165. */
  166. SK_API sk_sp<SkDocument> MakeDocument(SkWStream* stream, const Metadata& metadata);
  167. static inline sk_sp<SkDocument> MakeDocument(SkWStream* stream) {
  168. return MakeDocument(stream, Metadata());
  169. }
  170. } // namespace SkPDF
  171. #endif // SkPDFDocument_DEFINED