SkPDF: allow overriding Producer metadata
authorhalcanary <halcanary@google.com>
Thu, 30 Jun 2016 15:34:06 +0000 (08:34 -0700)
committerCommit bot <commit-bot@chromium.org>
Thu, 30 Jun 2016 15:34:06 +0000 (08:34 -0700)
Cherry-pick 9f4b332 to m53

I recommend not using this functionality.

Also, some documentation.

BUG=skia:5436

Review-Url: https://codereview.chromium.org/2074583003

NOTREECHECKS=true
NOTRY=true
NOPRESUBMIT=true

// discussed this cherry pick
TBR=reed@google.com

Review-Url: https://codereview.chromium.org/2110543007

include/core/SkDocument.h
src/pdf/SkPDFMetadata.cpp

index 9eed304a4030d45201de4b52b1ab02762c6550a1..eede42003d2c93948a303898ac4e8d5d021c1e32 100644 (file)
@@ -47,12 +47,42 @@ public:
      *  Optional metadata to be passed into the PDF factory function.
      */
     struct PDFMetadata {
+        /**
+         * The document’s title.
+         */
         SkString fTitle;
+        /**
+         * The name of the person who created the document.
+         */
         SkString fAuthor;
+        /**
+         * The subject of the document.
+         */
         SkString fSubject;
+        /**
+         * Keywords associated with the document.  Commas may be used
+         * to delineate keywords within the string.
+         */
         SkString fKeywords;
+        /**
+         * If the document was converted to PDF from another format,
+         * the name of the conforming product that created the
+         * original document from which it was converted.
+         */
         SkString fCreator;
+        /**
+         * The product that is converting this document to PDF.
+         *
+         * Leave fProducer empty to get the default, correct value.
+         */
+        SkString fProducer;
+        /**
+         * The date and time the document was created.
+         */
         OptionalTimestamp fCreation;
+        /**
+         * The date and time the document was most recently modified.
+         */
         OptionalTimestamp fModified;
     };
 
index b9aa3bb5a9515b25d408cb1148fc0b440527b41f..18c8e29bc7adeaaa171efda7e641d07fe25177b2 100644 (file)
 #include "SkPDFTypes.h"
 #include <utility>
 
+#define SKPDF_STRING(X) SKPDF_STRING_IMPL(X)
+#define SKPDF_STRING_IMPL(X) #X
+#define SKPDF_PRODUCER "Skia/PDF m" SKPDF_STRING(SK_MILESTONE)
+#define SKPDF_CUSTOM_PRODUCER_KEY "ProductionLibrary"
+
 static SkString pdf_date(const SkTime::DateTime& dt) {
     int timeZoneMinutes = SkToInt(dt.fTimeZoneMinutes);
     char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-';
@@ -25,9 +30,6 @@ static SkString pdf_date(const SkTime::DateTime& dt) {
             timeZoneMinutes);
 }
 
-#define SKPDF_STRING(X) SKPDF_STRING_IMPL(X)
-#define SKPDF_STRING_IMPL(X) #X
-
 namespace {
 static const struct {
     const char* const key;
@@ -63,7 +65,12 @@ sk_sp<SkPDFObject> SkPDFMetadata::MakeDocumentInformationDict(
             dict->insertString(keyValuePtr.key, value);
         }
     }
-    dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE));
+    if (metadata.fProducer.isEmpty()) {
+        dict->insertString("Producer", SKPDF_PRODUCER);
+    } else {
+        dict->insertString("Producer", metadata.fProducer);
+        dict->insertString(SKPDF_CUSTOM_PRODUCER_KEY, SKPDF_PRODUCER);
+    }
     if (metadata.fCreation.fEnabled) {
         dict->insertString("CreationDate",
                            pdf_date(metadata.fCreation.fDateTime));
@@ -264,7 +271,7 @@ sk_sp<SkPDFObject> SkPDFMetadata::MakeXMPObject(
             "%s"  // keywords
             "<xmpMM:DocumentID>uuid:%s</xmpMM:DocumentID>\n"
             "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n"
-            "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Producer>\n"
+            "%s"  // pdf:Producer
             "%s"  // pdf:Keywords
             "</rdf:Description>\n"
             "</rdf:RDF>\n"
@@ -305,8 +312,18 @@ sk_sp<SkPDFObject> SkPDFMetadata::MakeXMPObject(
                        "</rdf:li></rdf:Bag></dc:subject>\n");
     SkString keywords2 = escape_xml(metadata.fKeywords, "<pdf:Keywords>",
                                     "</pdf:Keywords>\n");
-
     // TODO: in theory, keywords can be a list too.
+
+    SkString producer("<pdf:Producer>SKPDF_PRODUCER</pdf:Producer>\n");
+    if (!metadata.fProducer.isEmpty()) {
+        // TODO: register a developer prefix to make
+        // <skia:SKPDF_CUSTOM_PRODUCER_KEY> a real XML tag.
+        producer = escape_xml(
+                metadata.fProducer, "<pdf:Producer>",
+                "</pdf:Producer>\n<!-- <skia:" SKPDF_CUSTOM_PRODUCER_KEY ">"
+                SKPDF_PRODUCER "</skia:" SKPDF_CUSTOM_PRODUCER_KEY "> -->\n");
+    }
+
     SkString creator = escape_xml(metadata.fCreator, "<xmp:CreatorTool>",
                                   "</xmp:CreatorTool>\n");
     SkString documentID = uuid_to_string(doc);  // no need to escape
@@ -317,8 +334,10 @@ sk_sp<SkPDFObject> SkPDFMetadata::MakeXMPObject(
             templateString, modificationDate.c_str(), creationDate.c_str(),
             creator.c_str(), title.c_str(), subject.c_str(), author.c_str(),
             keywords1.c_str(), documentID.c_str(), instanceID.c_str(),
-            keywords2.c_str()));
+            producer.c_str(), keywords2.c_str()));
 }
 
+#undef SKPDF_CUSTOM_PRODUCER_KEY
+#undef SKPDF_PRODUCER
 #undef SKPDF_STRING
 #undef SKPDF_STRING_IMPL