SkPDF: Optionally output PDF/A-2b  archive format.

Note: this format does not yet pass validation tests.

Add skia_pdf_generate_pdfa GYP flag.  Default to off for now.
PDF/A files are not reproducable, so they make correctness
testing harder.

Turn the Metadata struct into te SkPDFMetadata struct.  This
splits out a lot of functionality around both kinds of metadata.

When PDF/A is used, add an ID entry to the trailer.

Add SkPDFObjNumMap::addObjectRecursively.

Test with

    GYP_DEFINES=skia_pdf_generate_pdfa=1 bin/sync-and-gyp
    ninja -C out/Release dm
    out/Release/dm --config pdf --src skp gm -w /tmp/dm

With skia_pdf_generate_pdfa=0, all PDFs generated from GMs and
SKPs are identical.  With skia_pdf_generate_pdfa=1, all PDFs
generated from GMs and SKPs render identically in Pdfium.

BUG=skia:3110

Review URL: https://codereview.chromium.org/1394263003
diff --git a/gyp/common_variables.gypi b/gyp/common_variables.gypi
index 57fad1d..c84a8e5 100644
--- a/gyp/common_variables.gypi
+++ b/gyp/common_variables.gypi
@@ -225,6 +225,7 @@
     'skia_win_ltcg%': 1,
     'skia_osx_deployment_target%': '<(skia_osx_deployment_target)',
     'skia_pdf%': '<(skia_pdf)',
+    'skia_pdf_generate_pdfa%': 0,  # emit larger PDF/A-2b file
     'skia_profile_enabled%': '<(skia_profile_enabled)',
     'skia_shared_lib%': '<(skia_shared_lib)',
     'skia_opencl%': '<(skia_opencl)',
diff --git a/gyp/pdf.gyp b/gyp/pdf.gyp
index ee30488..77d1922 100644
--- a/gyp/pdf.gyp
+++ b/gyp/pdf.gyp
@@ -40,6 +40,7 @@
            skia_os in ["win", "android", "linux", "chromeos", "mac"]',
           { 'dependencies': [ 'sfntly.gyp:sfntly' ] }
         ],
+        [ 'skia_pdf_generate_pdfa', { 'defines': ['SK_PDF_GENERATE_PDFA'] } ],
         [ 'skia_android_framework', {
             # Add SFTNLY support for PDF (which in turns depends on ICU)
             'include_dirs': [
diff --git a/gyp/pdf.gypi b/gyp/pdf.gypi
index f114fa7..888613e 100644
--- a/gyp/pdf.gypi
+++ b/gyp/pdf.gypi
@@ -29,6 +29,8 @@
         '<(skia_src_path)/pdf/SkPDFFormXObject.h',
         '<(skia_src_path)/pdf/SkPDFGraphicState.cpp',
         '<(skia_src_path)/pdf/SkPDFGraphicState.h',
+        '<(skia_src_path)/pdf/SkPDFMetadata.cpp',
+        '<(skia_src_path)/pdf/SkPDFMetadata.h',
         '<(skia_src_path)/pdf/SkPDFResourceDict.cpp',
         '<(skia_src_path)/pdf/SkPDFResourceDict.h',
         '<(skia_src_path)/pdf/SkPDFShader.cpp',
diff --git a/src/doc/SkDocument_PDF.cpp b/src/doc/SkDocument_PDF.cpp
index fb22f18..ff7a038 100644
--- a/src/doc/SkDocument_PDF.cpp
+++ b/src/doc/SkDocument_PDF.cpp
@@ -13,6 +13,7 @@
 #include "SkPDFTypes.h"
 #include "SkPDFUtils.h"
 #include "SkStream.h"
+#include "SkPDFMetadata.h"
 
 class SkPDFDict;
 
@@ -30,15 +31,18 @@
                             SkPDFObject* docCatalog,
                             int64_t objCount,
                             int32_t xRefFileOffset,
-                            SkPDFDict* info) {
+                            SkPDFObject* info /* take ownership */,
+                            SkPDFObject* id /* take ownership */) {
     SkPDFDict trailerDict;
-    // TODO(vandebo): Linearized format will take a Prev entry too.
-    // TODO(vandebo): PDF/A requires an ID entry.
+    // TODO(http://crbug.com/80908): Linearized format will take a
+    //                               Prev entry too.
     trailerDict.insertInt("Size", int(objCount));
     trailerDict.insertObjRef("Root", SkRef(docCatalog));
     SkASSERT(info);
-    trailerDict.insertObjRef("Info", SkRef(info));
-
+    trailerDict.insertObjRef("Info", info);
+    if (id) {
+        trailerDict.insertObject("ID", id);
+    }
     stream->writeText("trailer\n");
     trailerDict.emitObject(stream, objNumMap, substitutes);
     stream->writeText("\nstartxref\n");
@@ -162,49 +166,8 @@
     }
 }
 
-struct Metadata {
-    SkTArray<SkDocument::Attribute> fInfo;
-    SkAutoTDelete<const SkTime::DateTime> fCreation;
-    SkAutoTDelete<const SkTime::DateTime> fModified;
-};
-
-static SkString pdf_date(const SkTime::DateTime& dt) {
-    int timeZoneMinutes = SkToInt(dt.fTimeZoneMinutes);
-    char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-';
-    int timeZoneHours = SkTAbs(timeZoneMinutes) / 60;
-    timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60;
-    return SkStringPrintf(
-            "D:%04u%02u%02u%02u%02u%02u%c%02d'%02d'",
-            static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth),
-            static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour),
-            static_cast<unsigned>(dt.fMinute),
-            static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours,
-            timeZoneMinutes);
-}
-
-SkPDFDict* create_document_information_dict(const Metadata& metadata) {
-    SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
-    static const char* keys[] = {
-        "Title", "Author", "Subject", "Keywords", "Creator" };
-    for (const char* key : keys) {
-        for (const SkDocument::Attribute& keyValue : metadata.fInfo) {
-            if (keyValue.fKey.equals(key)) {
-                dict->insertString(key, keyValue.fValue);
-            }
-        }
-    }
-    dict->insertString("Producer", "Skia/PDF");
-    if (metadata.fCreation) {
-        dict->insertString("CreationDate", pdf_date(*metadata.fCreation.get()));
-    }
-    if (metadata.fModified) {
-        dict->insertString("ModDate", pdf_date(*metadata.fModified.get()));
-    }
-    return dict.detach();
-}
-
 static bool emit_pdf_document(const SkTDArray<const SkPDFDevice*>& pageDevices,
-                              const Metadata& metadata,
+                              const SkPDFMetadata& metadata,
                               SkWStream* stream) {
     if (pageDevices.isEmpty()) {
         return false;
@@ -222,9 +185,37 @@
         pages.push(page.detach());
     }
 
-    SkTDArray<SkPDFDict*> pageTree;
     SkAutoTUnref<SkPDFDict> docCatalog(new SkPDFDict("Catalog"));
 
+    SkAutoTUnref<SkPDFObject> infoDict(
+            metadata.createDocumentInformationDict());
+
+    SkAutoTUnref<SkPDFObject> id, xmp;
+#ifdef SK_PDF_GENERATE_PDFA
+    SkPDFMetadata::UUID uuid = metadata.uuid();
+    // We use the same UUID for Document ID and Instance ID since this
+    // is the first revision of this document (and Skia does not
+    // support revising existing PDF documents).
+    // If we are not in PDF/A mode, don't use a UUID since testing
+    // works best with reproducible outputs.
+    id.reset(SkPDFMetadata::CreatePdfId(uuid, uuid));
+    xmp.reset(metadata.createXMPObject(uuid, uuid));
+    docCatalog->insertObjRef("Metadata", xmp.detach());
+
+    // sRGB is specified by HTML, CSS, and SVG.
+    SkAutoTUnref<SkPDFDict> outputIntent(new SkPDFDict("OutputIntent"));
+    outputIntent->insertName("S", "GTS_PDFA1");
+    outputIntent->insertString("RegistryName", "http://www.color.org");
+    outputIntent->insertString("OutputConditionIdentifier",
+                               "sRGB IEC61966-2.1");
+    SkAutoTUnref<SkPDFArray> intentArray(new SkPDFArray);
+    intentArray->appendObject(outputIntent.detach());
+    // Don't specify OutputIntents if we are not in PDF/A mode since
+    // no one has ever asked for this feature.
+    docCatalog->insertObject("OutputIntents", intentArray.detach());
+#endif
+
+    SkTDArray<SkPDFDict*> pageTree;
     SkPDFDict* pageTreeRoot;
     generate_page_tree(pages, &pageTree, &pageTreeRoot);
     docCatalog->insertObjRef("Pages", SkRef(pageTreeRoot));
@@ -233,28 +224,13 @@
         docCatalog->insertObjRef("Dests", dests.detach());
     }
 
-    /* TODO(vandebo): output intent
-    SkAutoTUnref<SkPDFDict> outputIntent = new SkPDFDict("OutputIntent");
-    outputIntent->insertName("S", "GTS_PDFA1");
-    outputIntent->insertString("OutputConditionIdentifier", "sRGB");
-    SkAutoTUnref<SkPDFArray> intentArray(new SkPDFArray);
-    intentArray->appendObject(SkRef(outputIntent.get()));
-    docCatalog->insertObject("OutputIntent", intentArray.detach());
-    */
-
     // Build font subsetting info before proceeding.
     SkPDFSubstituteMap substitutes;
     perform_font_subsetting(pageDevices, &substitutes);
 
-    SkAutoTUnref<SkPDFDict> infoDict(
-            create_document_information_dict(metadata));
     SkPDFObjNumMap objNumMap;
-    if (objNumMap.addObject(infoDict)) {
-        infoDict->addResources(&objNumMap, substitutes);
-    }
-    if (objNumMap.addObject(docCatalog.get())) {
-        docCatalog->addResources(&objNumMap, substitutes);
-    }
+    objNumMap.addObjectRecursively(infoDict, substitutes);
+    objNumMap.addObjectRecursively(docCatalog.get(), substitutes);
     size_t baseOffset = stream->bytesWritten();
     emit_pdf_header(stream);
     SkTDArray<int32_t> offsets;
@@ -286,7 +262,7 @@
         stream->writeText(" 00000 n \n");
     }
     emit_pdf_footer(stream, objNumMap, substitutes, docCatalog.get(), objCount,
-                    xRefFileOffset, infoDict);
+                    xRefFileOffset, infoDict.detach(), id.detach());
 
     // The page tree has both child and parent pointers, so it creates a
     // reference cycle.  We must clear that cycle to properly reclaim memory.
@@ -404,7 +380,7 @@
     SkTDArray<const SkPDFDevice*> fPageDevices;
     SkAutoTUnref<SkCanvas> fCanvas;
     SkScalar fRasterDpi;
-    Metadata fMetadata;
+    SkPDFMetadata fMetadata;
 };
 }  // namespace
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/pdf/SkPDFBitmap.cpp b/src/pdf/SkPDFBitmap.cpp
index 1978040..52b65c0 100644
--- a/src/pdf/SkPDFBitmap.cpp
+++ b/src/pdf/SkPDFBitmap.cpp
@@ -364,19 +364,17 @@
 namespace {
 class PDFDefaultBitmap : public SkPDFObject {
 public:
-    void emitObject(SkWStream*  stream,
+    void emitObject(SkWStream* stream,
                     const SkPDFObjNumMap& objNumMap,
-                    const SkPDFSubstituteMap& substitutes) const override {
-        emit_image_xobject(stream, fImage, false, fSMask, objNumMap, substitutes);
+                    const SkPDFSubstituteMap& subs) const override {
+        emit_image_xobject(stream, fImage, false, fSMask, objNumMap, subs);
     }
     void addResources(SkPDFObjNumMap* catalog,
-                      const SkPDFSubstituteMap& substitutes) const override {
+                      const SkPDFSubstituteMap& subs) const override {
         if (fSMask.get()) {
-            SkPDFObject* obj = substitutes.getSubstitute(fSMask.get());
+            SkPDFObject* obj = subs.getSubstitute(fSMask.get());
             SkASSERT(obj);
-            if (catalog->addObject(obj)) {
-                obj->addResources(catalog, substitutes);
-            }
+            catalog->addObjectRecursively(obj, subs);
         }
     }
     PDFDefaultBitmap(const SkImage* image, SkPDFObject* smask)
diff --git a/src/pdf/SkPDFMetadata.cpp b/src/pdf/SkPDFMetadata.cpp
new file mode 100644
index 0000000..6a68a48
--- /dev/null
+++ b/src/pdf/SkPDFMetadata.cpp
@@ -0,0 +1,347 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkPDFMetadata.h"
+#include "SkMD5.h"
+#include "SkPDFTypes.h"
+
+SkPDFMetadata::UUID SkPDFMetadata::uuid() const {
+    // The main requirement is for the UUID to be unique; the exact
+    // format of the data that will be hashed is not important.
+    SkMD5 md5;
+    const char uuidNamespace[] = "org.skia.pdf\n";
+    md5.write(uuidNamespace, strlen(uuidNamespace));
+    SkMSec msec = SkTime::GetMSecs();
+    md5.write(&msec, sizeof(msec));
+    SkTime::DateTime dateTime;
+    SkTime::GetDateTime(&dateTime);
+    md5.write(&dateTime, sizeof(dateTime));
+    if (fCreation) {
+        md5.write(fCreation.get(), sizeof(fCreation));
+    }
+    if (fModified) {
+        md5.write(fModified.get(), sizeof(fModified));
+    }
+    for (const auto& kv : fInfo) {
+        md5.write(kv.fKey.c_str(), kv.fKey.size());
+        md5.write("\037", 1);
+        md5.write(kv.fValue.c_str(), kv.fValue.size());
+        md5.write("\036", 1);
+    }
+    SkMD5::Digest digest;
+    md5.finish(digest);
+    // See RFC 4122, page 6-7.
+    digest.data[6] = (digest.data[6] & 0x0F) | 0x30;
+    digest.data[8] = (digest.data[6] & 0x3F) | 0x80;
+    static_assert(sizeof(digest) == sizeof(UUID), "uuid_size");
+    SkPDFMetadata::UUID uuid;
+    memcpy(&uuid, &digest, sizeof(digest));
+    return uuid;
+}
+
+SkPDFObject* SkPDFMetadata::CreatePdfId(const UUID& doc, const UUID& instance) {
+    // /ID [ <81b14aafa313db63dbd6f981e49f94f4>
+    //       <81b14aafa313db63dbd6f981e49f94f4> ]
+    SkAutoTUnref<SkPDFArray> array(new SkPDFArray);
+    static_assert(sizeof(UUID) == 16, "uuid_size");
+    array->appendString(
+            SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID)));
+    array->appendString(
+            SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID)));
+    return array.detach();
+}
+
+static SkString pdf_date(const SkTime::DateTime& dt) {
+    int timeZoneMinutes = SkToInt(dt.fTimeZoneMinutes);
+    char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-';
+    int timeZoneHours = SkTAbs(timeZoneMinutes) / 60;
+    timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60;
+    return SkStringPrintf(
+            "D:%04u%02u%02u%02u%02u%02u%c%02d'%02d'",
+            static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth),
+            static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour),
+            static_cast<unsigned>(dt.fMinute),
+            static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours,
+            timeZoneMinutes);
+}
+
+SkPDFObject* SkPDFMetadata::createDocumentInformationDict() const {
+    SkAutoTUnref<SkPDFDict> dict(new SkPDFDict);
+    static const char* keys[] = {
+            "Title", "Author", "Subject", "Keywords", "Creator"};
+    for (const char* key : keys) {
+        for (const SkDocument::Attribute& keyValue : fInfo) {
+            if (keyValue.fKey.equals(key)) {
+                dict->insertString(key, keyValue.fValue);
+            }
+        }
+    }
+    dict->insertString("Producer", "Skia/PDF");
+    if (fCreation) {
+        dict->insertString("CreationDate", pdf_date(*fCreation.get()));
+    }
+    if (fModified) {
+        dict->insertString("ModDate", pdf_date(*fModified.get()));
+    }
+    return dict.detach();
+}
+
+#ifdef SK_PDF_GENERATE_PDFA
+// Improvement on SkStringPrintf to allow for arbitrarily long output.
+// TODO: replace SkStringPrintf.
+static SkString sk_string_printf(const char* format, ...) {
+#ifdef SK_BUILD_FOR_WIN
+    va_list args;
+    va_start(args, format);
+    char buffer[1024];
+    int length = _vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, args);
+    va_end(args);
+    if (length >= 0 && length < (int)sizeof(buffer)) {
+        return SkString(buffer, length);
+    }
+    va_start(args, format);
+    length = _vscprintf(format, args);
+    va_end(args);
+
+    SkString string((size_t)length);
+    va_start(args, format);
+    SkDEBUGCODE(int check = ) _vsnprintf_s(string.writable_str(), length + 1,
+                                           _TRUNCATE, format, args);
+    va_end(args);
+    SkASSERT(check == length);
+    SkASSERT(string[length] == '\0');
+    return skstd::move(string);
+#else  // C99/C++11 standard vsnprintf
+    // TODO: When all compilers support this, remove windows-specific code.
+    va_list args;
+    va_start(args, format);
+    char buffer[1024];
+    int length = vsnprintf(buffer, sizeof(buffer), format, args);
+    va_end(args);
+    if (length < 0) {
+        return SkString();
+    }
+    if (length < (int)sizeof(buffer)) {
+        return SkString(buffer, length);
+    }
+    SkString string((size_t)length);
+    va_start(args, format);
+    SkDEBUGCODE(int check = )
+            vsnprintf(string.writable_str(), length + 1, format, args);
+    va_end(args);
+    SkASSERT(check == length);
+    SkASSERT(string[length] == '\0');
+    return skstd::move(string);
+#endif
+}
+
+static const SkString get(const SkTArray<SkDocument::Attribute>& info,
+                          const char* key) {
+    for (const auto& keyValue : info) {
+        if (keyValue.fKey.equals(key)) {
+            return keyValue.fValue;
+        }
+    }
+    return SkString();
+}
+
+#define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \
+    do {                                                      \
+        for (int i = 0; i < (BYTE_COUNT); ++i) {              \
+            uint8_t value = *(INPUT_PTR)++;                   \
+            *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4];       \
+            *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF];      \
+        }                                                     \
+    } while (false)
+static SkString uuid_to_string(const SkPDFMetadata::UUID& uuid) {
+    //  8-4-4-4-12
+    char buffer[36];  // [32 + 4]
+    static const char gHex[] = "0123456789abcdef";
+    SkASSERT(strlen(gHex) == 16);
+    char* ptr = buffer;
+    const uint8_t* data = uuid.fData;
+    HEXIFY(data, ptr, gHex, 4);
+    *ptr++ = '-';
+    HEXIFY(data, ptr, gHex, 2);
+    *ptr++ = '-';
+    HEXIFY(data, ptr, gHex, 2);
+    *ptr++ = '-';
+    HEXIFY(data, ptr, gHex, 2);
+    *ptr++ = '-';
+    HEXIFY(data, ptr, gHex, 6);
+    SkASSERT(ptr == buffer + 36);
+    SkASSERT(data == uuid.fData + 16);
+    return SkString(buffer, 36);
+}
+#undef HEXIFY
+
+namespace {
+class PDFXMLObject : public SkPDFObject {
+public:
+    PDFXMLObject(SkString xml) : fXML(skstd::move(xml)) {}
+    void emitObject(SkWStream* stream,
+                    const SkPDFObjNumMap& omap,
+                    const SkPDFSubstituteMap& smap) const override {
+        SkPDFDict dict("Metadata");
+        dict.insertName("Subtype", "XML");
+        dict.insertInt("Length", fXML.size());
+        dict.emitObject(stream, omap, smap);
+        static const char streamBegin[] = " stream\n";
+        stream->write(streamBegin, strlen(streamBegin));
+        // Do not compress this.  The standard requires that a
+        // program that does not understand PDF can grep for
+        // "<?xpacket" and extracť the entire XML.
+        stream->write(fXML.c_str(), fXML.size());
+        static const char streamEnd[] = "\nendstream";
+        stream->write(streamEnd, strlen(streamEnd));
+    }
+
+private:
+    const SkString fXML;
+};
+}  // namespace
+
+static int count_xml_escape_size(const SkString& input) {
+    int extra = 0;
+    for (size_t i = 0; i < input.size(); ++i) {
+        if (input[i] == '&') {
+            extra += 4;  // strlen("&amp;") - strlen("&")
+        } else if (input[i] == '<') {
+            extra += 3;  // strlen("&lt;") - strlen("<")
+        }
+    }
+    return extra;
+}
+
+const SkString escape_xml(const SkString& input,
+                          const char* before = nullptr,
+                          const char* after = nullptr) {
+    if (input.size() == 0) {
+        return input;
+    }
+    // "&" --> "&amp;" and  "<" --> "&lt;"
+    // text is assumed to be in UTF-8
+    // all strings are xml content, not attribute values.
+    size_t beforeLen = before ? strlen(before) : 0;
+    size_t afterLen = after ? strlen(after) : 0;
+    int extra = count_xml_escape_size(input);
+    SkString output(input.size() + extra + beforeLen + afterLen);
+    char* out = output.writable_str();
+    if (before) {
+        strncpy(out, before, beforeLen);
+        out += beforeLen;
+    }
+    static const char kAmp[] = "&amp;";
+    static const char kLt[] = "&lt;";
+    for (size_t i = 0; i < input.size(); ++i) {
+        if (input[i] == '&') {
+            strncpy(out, kAmp, strlen(kAmp));
+            out += strlen(kAmp);
+        } else if (input[i] == '<') {
+            strncpy(out, kLt, strlen(kLt));
+            out += strlen(kLt);
+        } else {
+            *out++ = input[i];
+        }
+    }
+    if (after) {
+        strncpy(out, after, afterLen);
+        out += afterLen;
+    }
+    // Validate that we haven't written outside of our string.
+    SkASSERT(out == &output.writable_str()[output.size()]);
+    *out = '\0';
+    return skstd::move(output);
+}
+
+SkPDFObject* SkPDFMetadata::createXMPObject(const UUID& doc,
+                                            const UUID& instance) const {
+    static const char templateString[] =
+            "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
+            "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n"
+            " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, "
+            "2012/08/23-13:03:03\">\n"
+            "<rdf:RDF "
+            "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
+            "<rdf:Description rdf:about=\"\"\n"
+            " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n"
+            " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n"
+            " xmlns:xmpMM=\"http://ns.adobe.com/xap/1.0/mm/\"\n"
+            " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"\n"
+            " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
+            "<pdfaid:part>2</pdfaid:part>\n"
+            "<pdfaid:conformance>B</pdfaid:conformance>\n"
+            "%s"  // ModifyDate
+            "%s"  // CreateDate
+            "%s"  // MetadataDate
+            "%s"  // xmp:CreatorTool
+            "<dc:format>application/pdf</dc:format>\n"
+            "%s"  // dc:title
+            "%s"  // dc:description
+            "%s"  // author
+            "%s"  // keywords
+            "<xmpMM:DocumentID>uuid:%s</xmpMM:DocumentID>\n"
+            "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n"
+            "<pdf:Producer>Skia/PDF</pdf:Producer>\n"
+            "%s"  // pdf:Keywords
+            "</rdf:Description>\n"
+            "</rdf:RDF>\n"
+            "</x:xmpmeta>\n"  // Note:  the standard suggests 4k of padding.
+            "<?xpacket end=\"w\"?>\n";
+
+    SkString creationDate;
+    SkString modificationDate;
+    SkString metadataDate;
+    if (fCreation) {
+        SkString tmp;
+        fCreation->toISO8601(&tmp);
+        SkASSERT(0 == count_xml_escape_size(tmp));
+        // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape
+        creationDate = sk_string_printf("<xmp:CreateDate>%s</xmp:CreateDate>\n",
+                                        tmp.c_str());
+    }
+    if (fModified) {
+        SkString tmp;
+        fModified->toISO8601(&tmp);
+        SkASSERT(0 == count_xml_escape_size(tmp));
+        modificationDate = sk_string_printf(
+                "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str());
+        metadataDate = sk_string_printf(
+                "<xmp:MetadataDate>%s</xmp:MetadataDate>\n", tmp.c_str());
+    }
+
+    SkString title =
+            escape_xml(get(fInfo, "Title"), "<dc:title><rdf:Alt><rdf:li>",
+                       "</rdf:li></rdf:Alt></dc:title>\n");
+    SkString author =
+            escape_xml(get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>",
+                       "</rdf:li></rdf:Bag></dc:creator>\n");
+    // TODO: in theory, XMP can support multiple authors.  Split on a delimiter?
+    SkString subject = escape_xml(get(fInfo, "Subject"),
+                                  "<dc:description><rdf:Alt><rdf:li>",
+                                  "</rdf:li></rdf:Alt></dc:description>\n");
+    SkString keywords1 =
+            escape_xml(get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>",
+                       "</rdf:li></rdf:Bag></dc:subject>\n");
+    SkString keywords2 = escape_xml(get(fInfo, "Keywords"), "<pdf:Keywords>",
+                                    "</pdf:Keywords>\n");
+
+    // TODO: in theory, keywords can be a list too.
+    SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>",
+                                  "</xmp:CreatorTool>\n");
+    SkString documentID = uuid_to_string(doc);  // no need to escape
+    SkASSERT(0 == count_xml_escape_size(documentID));
+    SkString instanceID = uuid_to_string(instance);
+    SkASSERT(0 == count_xml_escape_size(instanceID));
+    return new PDFXMLObject(sk_string_printf(
+            templateString, modificationDate.c_str(), creationDate.c_str(),
+            metadataDate.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()));
+}
+
+#endif  // SK_PDF_GENERATE_PDFA
diff --git a/src/pdf/SkPDFMetadata.h b/src/pdf/SkPDFMetadata.h
new file mode 100644
index 0000000..3475ac2
--- /dev/null
+++ b/src/pdf/SkPDFMetadata.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkPDFMetadata_DEFINED
+#define SkPDFMetadata_DEFINED
+
+#include "SkDocument.h"
+#include "SkTime.h"
+
+class SkPDFObject;
+
+struct SkPDFMetadata {
+    SkTArray<SkDocument::Attribute> fInfo;
+    SkAutoTDelete<const SkTime::DateTime> fCreation;
+    SkAutoTDelete<const SkTime::DateTime> fModified;
+
+    SkPDFObject* createDocumentInformationDict() const;
+
+    struct UUID {
+        uint8_t fData[16];
+    };
+    UUID uuid() const;
+    static SkPDFObject* CreatePdfId(const UUID& doc, const UUID& instance);
+
+#ifdef SK_PDF_GENERATE_PDFA
+    SkPDFObject* createXMPObject(const UUID& doc, const UUID& instance) const;
+#endif  // SK_PDF_GENERATE_PDFA
+};
+
+#endif  // SkPDFMetadata_DEFINED
diff --git a/src/pdf/SkPDFTypes.cpp b/src/pdf/SkPDFTypes.cpp
index 4cd48f4..faa0837 100644
--- a/src/pdf/SkPDFTypes.cpp
+++ b/src/pdf/SkPDFTypes.cpp
@@ -177,9 +177,7 @@
             return;  // These have no resources.
         case Type::kObjRef: {
             SkPDFObject* obj = substituteMap.getSubstitute(fObject);
-            if (objNumMap->addObject(obj)) {
-                obj->addResources(objNumMap, substituteMap);
-            }
+            objNumMap->addObjectRecursively(obj, substituteMap);
             return;
         }
         case Type::kObject:
@@ -500,6 +498,13 @@
     return true;
 }
 
+void SkPDFObjNumMap::addObjectRecursively(SkPDFObject* obj,
+                                          const SkPDFSubstituteMap& subs) {
+    if (obj && this->addObject(obj)) {
+        obj->addResources(this, subs);
+    }
+}
+
 int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const {
     int32_t* objectNumberFound = fObjectNumbers.find(obj);
     SkASSERT(objectNumberFound);
diff --git a/src/pdf/SkPDFTypes.h b/src/pdf/SkPDFTypes.h
index ec527fc..0f02998 100644
--- a/src/pdf/SkPDFTypes.h
+++ b/src/pdf/SkPDFTypes.h
@@ -346,6 +346,12 @@
      */
     bool addObject(SkPDFObject* obj);
 
+    /** Add the passed object to the catalog, as well as all its dependencies.
+     *  @param obj   The object to add.  If nullptr, this is a noop.
+     *  @param subs  Will be passed to obj->addResources().
+     */
+    void addObjectRecursively(SkPDFObject* obj, const SkPDFSubstituteMap& subs);
+
     /** Get the object number for the passed object.
      *  @param obj         The object of interest.
      */