halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 8 | #include "SkPDFMetadata.h" |
| 9 | |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 10 | #include "SkMD5.h" |
halcanary | ffe5400 | 2016-03-29 09:09:29 -0700 | [diff] [blame] | 11 | #include "SkMilestone.h" |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 12 | #include "SkPDFTypes.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 13 | #include "SkTo.h" |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 14 | #include "SkUtils.h" |
| 15 | |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 16 | #include <utility> |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 17 | |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 18 | #define SKPDF_STRING(X) SKPDF_STRING_IMPL(X) |
| 19 | #define SKPDF_STRING_IMPL(X) #X |
| 20 | #define SKPDF_PRODUCER "Skia/PDF m" SKPDF_STRING(SK_MILESTONE) |
| 21 | #define SKPDF_CUSTOM_PRODUCER_KEY "ProductionLibrary" |
| 22 | |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 23 | static SkString pdf_date(const SkTime::DateTime& dt) { |
| 24 | int timeZoneMinutes = SkToInt(dt.fTimeZoneMinutes); |
| 25 | char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; |
| 26 | int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; |
| 27 | timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; |
| 28 | return SkStringPrintf( |
| 29 | "D:%04u%02u%02u%02u%02u%02u%c%02d'%02d'", |
| 30 | static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth), |
| 31 | static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour), |
| 32 | static_cast<unsigned>(dt.fMinute), |
| 33 | static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours, |
| 34 | timeZoneMinutes); |
| 35 | } |
| 36 | |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 37 | static bool utf8_is_pdfdocencoding(const char* src, size_t len) { |
| 38 | const uint8_t* end = (const uint8_t*)src + len; |
| 39 | for (const uint8_t* ptr = (const uint8_t*)src; ptr < end; ++ptr) { |
| 40 | uint8_t v = *ptr; |
| 41 | // See Table D.2 (PDFDocEncoding Character Set) in the PDF3200_2008 spec. |
| 42 | if ((v > 23 && v < 32) || v > 126) { |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | void write_utf16be(char** ptr, uint16_t value) { |
| 50 | *(*ptr)++ = (value >> 8); |
| 51 | *(*ptr)++ = (value & 0xFF); |
| 52 | } |
| 53 | |
| 54 | // Please Note: This "abuses" the SkString, which "should" only hold UTF8. |
| 55 | // But the SkString is written as if it is really just a ref-counted array of |
| 56 | // chars, so this works, as long as we handle endiness and conversions ourselves. |
| 57 | // |
| 58 | // Input: UTF-8 |
| 59 | // Output UTF-16-BE |
| 60 | static SkString to_utf16be(const char* src, size_t len) { |
| 61 | SkString ret; |
| 62 | const char* const end = src + len; |
| 63 | size_t n = 1; // BOM |
| 64 | for (const char* ptr = src; ptr < end;) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 65 | SkUnichar u = SkUTF::NextUTF8(&ptr, end); |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 66 | if (u < 0) { |
| 67 | break; |
| 68 | } |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 69 | n += SkUTF::ToUTF16(u); |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 70 | } |
| 71 | ret.resize(2 * n); |
| 72 | char* out = ret.writable_str(); |
| 73 | write_utf16be(&out, 0xFEFF); // BOM |
| 74 | for (const char* ptr = src; ptr < end;) { |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 75 | SkUnichar u = SkUTF::NextUTF8(&ptr, end); |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 76 | if (u < 0) { |
| 77 | break; |
| 78 | } |
| 79 | uint16_t utf16[2]; |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 80 | size_t l = SkUTF::ToUTF16(u, utf16); |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 81 | write_utf16be(&out, utf16[0]); |
| 82 | if (l == 2) { |
| 83 | write_utf16be(&out, utf16[1]); |
| 84 | } |
| 85 | } |
| 86 | SkASSERT(out == ret.writable_str() + 2 * n); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | // Input: UTF-8 |
| 91 | // Output UTF-16-BE OR PDFDocEncoding (if that encoding is identical to ASCII encoding). |
| 92 | // |
| 93 | // See sections 14.3.3 (Document Information Dictionary) and 7.9.2.2 (Text String Type) |
| 94 | // of the PDF32000_2008 spec. |
| 95 | static SkString convert(const SkString& s) { |
| 96 | return utf8_is_pdfdocencoding(s.c_str(), s.size()) ? s : to_utf16be(s.c_str(), s.size()); |
| 97 | } |
| 98 | static SkString convert(const char* src) { |
| 99 | size_t len = strlen(src); |
| 100 | return utf8_is_pdfdocencoding(src, len) ? SkString(src, len) : to_utf16be(src, len); |
| 101 | } |
| 102 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 103 | namespace { |
| 104 | static const struct { |
| 105 | const char* const key; |
| 106 | SkString SkDocument::PDFMetadata::*const valuePtr; |
| 107 | } gMetadataKeys[] = { |
| 108 | {"Title", &SkDocument::PDFMetadata::fTitle}, |
| 109 | {"Author", &SkDocument::PDFMetadata::fAuthor}, |
| 110 | {"Subject", &SkDocument::PDFMetadata::fSubject}, |
| 111 | {"Keywords", &SkDocument::PDFMetadata::fKeywords}, |
| 112 | {"Creator", &SkDocument::PDFMetadata::fCreator}, |
| 113 | }; |
| 114 | } // namespace |
| 115 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 116 | sk_sp<SkPDFObject> SkPDFMetadata::MakeDocumentInformationDict( |
| 117 | const SkDocument::PDFMetadata& metadata) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 118 | auto dict = sk_make_sp<SkPDFDict>(); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 119 | for (const auto keyValuePtr : gMetadataKeys) { |
| 120 | const SkString& value = metadata.*(keyValuePtr.valuePtr); |
| 121 | if (value.size() > 0) { |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 122 | dict->insertString(keyValuePtr.key, convert(value)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 125 | if (metadata.fProducer.isEmpty()) { |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 126 | dict->insertString("Producer", convert(SKPDF_PRODUCER)); |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 127 | } else { |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 128 | dict->insertString("Producer", convert(metadata.fProducer)); |
| 129 | dict->insertString(SKPDF_CUSTOM_PRODUCER_KEY, convert(SKPDF_PRODUCER)); |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 130 | } |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 131 | if (metadata.fCreation.fEnabled) { |
Hal Canary | 691fd1b | 2018-02-28 14:10:42 -0500 | [diff] [blame] | 132 | dict->insertString("CreationDate", pdf_date(metadata.fCreation.fDateTime)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 133 | } |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 134 | if (metadata.fModified.fEnabled) { |
| 135 | dict->insertString("ModDate", pdf_date(metadata.fModified.fDateTime)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 136 | } |
Kevin Lubick | f7621cb | 2018-04-16 15:51:44 -0400 | [diff] [blame] | 137 | return std::move(dict); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 138 | } |
| 139 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 140 | SkPDFMetadata::UUID SkPDFMetadata::CreateUUID( |
| 141 | const SkDocument::PDFMetadata& metadata) { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 142 | // The main requirement is for the UUID to be unique; the exact |
| 143 | // format of the data that will be hashed is not important. |
| 144 | SkMD5 md5; |
| 145 | const char uuidNamespace[] = "org.skia.pdf\n"; |
Hal Canary | c172f9b | 2017-05-27 20:29:44 -0400 | [diff] [blame] | 146 | md5.writeText(uuidNamespace); |
benjaminwagner | ec4d4d7 | 2016-03-25 12:59:53 -0700 | [diff] [blame] | 147 | double msec = SkTime::GetMSecs(); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 148 | md5.write(&msec, sizeof(msec)); |
| 149 | SkTime::DateTime dateTime; |
| 150 | SkTime::GetDateTime(&dateTime); |
| 151 | md5.write(&dateTime, sizeof(dateTime)); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 152 | if (metadata.fCreation.fEnabled) { |
| 153 | md5.write(&metadata.fCreation.fDateTime, |
| 154 | sizeof(metadata.fCreation.fDateTime)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 155 | } |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 156 | if (metadata.fModified.fEnabled) { |
| 157 | md5.write(&metadata.fModified.fDateTime, |
| 158 | sizeof(metadata.fModified.fDateTime)); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 159 | } |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 160 | |
| 161 | for (const auto keyValuePtr : gMetadataKeys) { |
Hal Canary | c172f9b | 2017-05-27 20:29:44 -0400 | [diff] [blame] | 162 | md5.writeText(keyValuePtr.key); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 163 | md5.write("\037", 1); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 164 | const SkString& value = metadata.*(keyValuePtr.valuePtr); |
| 165 | md5.write(value.c_str(), value.size()); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 166 | md5.write("\036", 1); |
| 167 | } |
| 168 | SkMD5::Digest digest; |
| 169 | md5.finish(digest); |
| 170 | // See RFC 4122, page 6-7. |
| 171 | digest.data[6] = (digest.data[6] & 0x0F) | 0x30; |
| 172 | digest.data[8] = (digest.data[6] & 0x3F) | 0x80; |
| 173 | static_assert(sizeof(digest) == sizeof(UUID), "uuid_size"); |
| 174 | SkPDFMetadata::UUID uuid; |
| 175 | memcpy(&uuid, &digest, sizeof(digest)); |
| 176 | return uuid; |
| 177 | } |
| 178 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 179 | sk_sp<SkPDFObject> SkPDFMetadata::MakePdfId(const UUID& doc, |
| 180 | const UUID& instance) { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 181 | // /ID [ <81b14aafa313db63dbd6f981e49f94f4> |
| 182 | // <81b14aafa313db63dbd6f981e49f94f4> ] |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 183 | auto array = sk_make_sp<SkPDFArray>(); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 184 | static_assert(sizeof(SkPDFMetadata::UUID) == 16, "uuid_size"); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 185 | array->appendString( |
| 186 | SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); |
| 187 | array->appendString( |
| 188 | SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); |
Kevin Lubick | f7621cb | 2018-04-16 15:51:44 -0400 | [diff] [blame] | 189 | return std::move(array); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 192 | // Convert a block of memory to hexadecimal. Input and output pointers will be |
| 193 | // moved to end of the range. |
| 194 | static void hexify(const uint8_t** inputPtr, char** outputPtr, int count) { |
| 195 | SkASSERT(inputPtr && *inputPtr); |
| 196 | SkASSERT(outputPtr && *outputPtr); |
| 197 | while (count-- > 0) { |
| 198 | uint8_t value = *(*inputPtr)++; |
| 199 | *(*outputPtr)++ = SkHexadecimalDigits::gLower[value >> 4]; |
| 200 | *(*outputPtr)++ = SkHexadecimalDigits::gLower[value & 0xF]; |
| 201 | } |
| 202 | } |
| 203 | |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 204 | static SkString uuid_to_string(const SkPDFMetadata::UUID& uuid) { |
| 205 | // 8-4-4-4-12 |
| 206 | char buffer[36]; // [32 + 4] |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 207 | char* ptr = buffer; |
| 208 | const uint8_t* data = uuid.fData; |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 209 | hexify(&data, &ptr, 4); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 210 | *ptr++ = '-'; |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 211 | hexify(&data, &ptr, 2); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 212 | *ptr++ = '-'; |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 213 | hexify(&data, &ptr, 2); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 214 | *ptr++ = '-'; |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 215 | hexify(&data, &ptr, 2); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 216 | *ptr++ = '-'; |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 217 | hexify(&data, &ptr, 6); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 218 | SkASSERT(ptr == buffer + 36); |
| 219 | SkASSERT(data == uuid.fData + 16); |
| 220 | return SkString(buffer, 36); |
| 221 | } |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 222 | |
| 223 | namespace { |
halcanary | 70d1554 | 2015-11-22 12:55:04 -0800 | [diff] [blame] | 224 | class PDFXMLObject final : public SkPDFObject { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 225 | public: |
bungeman | 221524d | 2016-01-05 14:59:40 -0800 | [diff] [blame] | 226 | PDFXMLObject(SkString xml) : fXML(std::move(xml)) {} |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 227 | void emitObject(SkWStream* stream, |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 228 | const SkPDFObjNumMap& omap) const override { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 229 | SkPDFDict dict("Metadata"); |
| 230 | dict.insertName("Subtype", "XML"); |
| 231 | dict.insertInt("Length", fXML.size()); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 232 | dict.emitObject(stream, omap); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 233 | static const char streamBegin[] = " stream\n"; |
Hal Canary | c172f9b | 2017-05-27 20:29:44 -0400 | [diff] [blame] | 234 | stream->writeText(streamBegin); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 235 | // Do not compress this. The standard requires that a |
| 236 | // program that does not understand PDF can grep for |
Hal Canary | 55325b7 | 2017-01-03 10:36:17 -0500 | [diff] [blame] | 237 | // "<?xpacket" and extract the entire XML. |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 238 | stream->write(fXML.c_str(), fXML.size()); |
| 239 | static const char streamEnd[] = "\nendstream"; |
Hal Canary | c172f9b | 2017-05-27 20:29:44 -0400 | [diff] [blame] | 240 | stream->writeText(streamEnd); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | private: |
| 244 | const SkString fXML; |
| 245 | }; |
| 246 | } // namespace |
| 247 | |
| 248 | static int count_xml_escape_size(const SkString& input) { |
| 249 | int extra = 0; |
| 250 | for (size_t i = 0; i < input.size(); ++i) { |
| 251 | if (input[i] == '&') { |
| 252 | extra += 4; // strlen("&") - strlen("&") |
| 253 | } else if (input[i] == '<') { |
| 254 | extra += 3; // strlen("<") - strlen("<") |
| 255 | } |
| 256 | } |
| 257 | return extra; |
| 258 | } |
| 259 | |
| 260 | const SkString escape_xml(const SkString& input, |
| 261 | const char* before = nullptr, |
| 262 | const char* after = nullptr) { |
| 263 | if (input.size() == 0) { |
| 264 | return input; |
| 265 | } |
| 266 | // "&" --> "&" and "<" --> "<" |
| 267 | // text is assumed to be in UTF-8 |
| 268 | // all strings are xml content, not attribute values. |
| 269 | size_t beforeLen = before ? strlen(before) : 0; |
| 270 | size_t afterLen = after ? strlen(after) : 0; |
| 271 | int extra = count_xml_escape_size(input); |
| 272 | SkString output(input.size() + extra + beforeLen + afterLen); |
| 273 | char* out = output.writable_str(); |
| 274 | if (before) { |
| 275 | strncpy(out, before, beforeLen); |
| 276 | out += beforeLen; |
| 277 | } |
| 278 | static const char kAmp[] = "&"; |
| 279 | static const char kLt[] = "<"; |
| 280 | for (size_t i = 0; i < input.size(); ++i) { |
| 281 | if (input[i] == '&') { |
| 282 | strncpy(out, kAmp, strlen(kAmp)); |
| 283 | out += strlen(kAmp); |
| 284 | } else if (input[i] == '<') { |
| 285 | strncpy(out, kLt, strlen(kLt)); |
| 286 | out += strlen(kLt); |
| 287 | } else { |
| 288 | *out++ = input[i]; |
| 289 | } |
| 290 | } |
| 291 | if (after) { |
| 292 | strncpy(out, after, afterLen); |
| 293 | out += afterLen; |
| 294 | } |
| 295 | // Validate that we haven't written outside of our string. |
| 296 | SkASSERT(out == &output.writable_str()[output.size()]); |
| 297 | *out = '\0'; |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 298 | return output; |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 299 | } |
| 300 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 301 | sk_sp<SkPDFObject> SkPDFMetadata::MakeXMPObject( |
| 302 | const SkDocument::PDFMetadata& metadata, |
| 303 | const UUID& doc, |
| 304 | const UUID& instance) { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 305 | static const char templateString[] = |
| 306 | "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" |
| 307 | "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" |
| 308 | " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, " |
| 309 | "2012/08/23-13:03:03\">\n" |
| 310 | "<rdf:RDF " |
| 311 | "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" |
| 312 | "<rdf:Description rdf:about=\"\"\n" |
| 313 | " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n" |
| 314 | " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" |
| 315 | " xmlns:xmpMM=\"http://ns.adobe.com/xap/1.0/mm/\"\n" |
| 316 | " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"\n" |
| 317 | " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n" |
| 318 | "<pdfaid:part>2</pdfaid:part>\n" |
| 319 | "<pdfaid:conformance>B</pdfaid:conformance>\n" |
| 320 | "%s" // ModifyDate |
| 321 | "%s" // CreateDate |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 322 | "%s" // xmp:CreatorTool |
| 323 | "<dc:format>application/pdf</dc:format>\n" |
| 324 | "%s" // dc:title |
| 325 | "%s" // dc:description |
| 326 | "%s" // author |
| 327 | "%s" // keywords |
| 328 | "<xmpMM:DocumentID>uuid:%s</xmpMM:DocumentID>\n" |
| 329 | "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n" |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 330 | "%s" // pdf:Producer |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 331 | "%s" // pdf:Keywords |
| 332 | "</rdf:Description>\n" |
| 333 | "</rdf:RDF>\n" |
| 334 | "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. |
| 335 | "<?xpacket end=\"w\"?>\n"; |
| 336 | |
| 337 | SkString creationDate; |
| 338 | SkString modificationDate; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 339 | if (metadata.fCreation.fEnabled) { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 340 | SkString tmp; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 341 | metadata.fCreation.fDateTime.toISO8601(&tmp); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 342 | SkASSERT(0 == count_xml_escape_size(tmp)); |
| 343 | // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape |
halcanary | d51bdae | 2016-04-25 09:25:35 -0700 | [diff] [blame] | 344 | creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n", |
| 345 | tmp.c_str()); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 346 | } |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 347 | if (metadata.fModified.fEnabled) { |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 348 | SkString tmp; |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 349 | metadata.fModified.fDateTime.toISO8601(&tmp); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 350 | SkASSERT(0 == count_xml_escape_size(tmp)); |
halcanary | d51bdae | 2016-04-25 09:25:35 -0700 | [diff] [blame] | 351 | modificationDate = SkStringPrintf( |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 352 | "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 353 | } |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 354 | SkString title = |
| 355 | escape_xml(metadata.fTitle, |
| 356 | "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
| 357 | "</rdf:li></rdf:Alt></dc:title>\n"); |
| 358 | SkString author = |
| 359 | escape_xml(metadata.fAuthor, "<dc:creator><rdf:Bag><rdf:li>", |
| 360 | "</rdf:li></rdf:Bag></dc:creator>\n"); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 361 | // TODO: in theory, XMP can support multiple authors. Split on a delimiter? |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 362 | SkString subject = escape_xml( |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 363 | metadata.fSubject, |
halcanary | 78daeff | 2016-04-07 12:34:59 -0700 | [diff] [blame] | 364 | "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
| 365 | "</rdf:li></rdf:Alt></dc:description>\n"); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 366 | SkString keywords1 = |
| 367 | escape_xml(metadata.fKeywords, "<dc:subject><rdf:Bag><rdf:li>", |
| 368 | "</rdf:li></rdf:Bag></dc:subject>\n"); |
| 369 | SkString keywords2 = escape_xml(metadata.fKeywords, "<pdf:Keywords>", |
| 370 | "</pdf:Keywords>\n"); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 371 | // TODO: in theory, keywords can be a list too. |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 372 | |
halcanary | 492d6b5 | 2016-07-07 12:28:30 -0700 | [diff] [blame] | 373 | SkString producer("<pdf:Producer>" SKPDF_PRODUCER "</pdf:Producer>\n"); |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 374 | if (!metadata.fProducer.isEmpty()) { |
| 375 | // TODO: register a developer prefix to make |
| 376 | // <skia:SKPDF_CUSTOM_PRODUCER_KEY> a real XML tag. |
| 377 | producer = escape_xml( |
| 378 | metadata.fProducer, "<pdf:Producer>", |
| 379 | "</pdf:Producer>\n<!-- <skia:" SKPDF_CUSTOM_PRODUCER_KEY ">" |
| 380 | SKPDF_PRODUCER "</skia:" SKPDF_CUSTOM_PRODUCER_KEY "> -->\n"); |
| 381 | } |
| 382 | |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 383 | SkString creator = escape_xml(metadata.fCreator, "<xmp:CreatorTool>", |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 384 | "</xmp:CreatorTool>\n"); |
| 385 | SkString documentID = uuid_to_string(doc); // no need to escape |
| 386 | SkASSERT(0 == count_xml_escape_size(documentID)); |
| 387 | SkString instanceID = uuid_to_string(instance); |
| 388 | SkASSERT(0 == count_xml_escape_size(instanceID)); |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 389 | return sk_make_sp<PDFXMLObject>(SkStringPrintf( |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 390 | templateString, modificationDate.c_str(), creationDate.c_str(), |
halcanary | 4b65666 | 2016-04-27 07:45:18 -0700 | [diff] [blame] | 391 | creator.c_str(), title.c_str(), subject.c_str(), author.c_str(), |
| 392 | keywords1.c_str(), documentID.c_str(), instanceID.c_str(), |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 393 | producer.c_str(), keywords2.c_str())); |
halcanary | 3442261 | 2015-10-12 10:11:18 -0700 | [diff] [blame] | 394 | } |
| 395 | |
halcanary | 9f4b332 | 2016-06-30 08:22:04 -0700 | [diff] [blame] | 396 | #undef SKPDF_CUSTOM_PRODUCER_KEY |
| 397 | #undef SKPDF_PRODUCER |
halcanary | 8cd4a24 | 2016-04-07 08:56:15 -0700 | [diff] [blame] | 398 | #undef SKPDF_STRING |
| 399 | #undef SKPDF_STRING_IMPL |