SkPDFDocument.h 5.2 KB

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