| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkMD5.h" | 8 #include "SkMD5.h" |
| 9 #include "SkMilestone.h" | 9 #include "SkMilestone.h" |
| 10 #include "SkPDFMetadata.h" | 10 #include "SkPDFMetadata.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth), | 21 static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth), |
| 22 static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour), | 22 static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour), |
| 23 static_cast<unsigned>(dt.fMinute), | 23 static_cast<unsigned>(dt.fMinute), |
| 24 static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours, | 24 static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours, |
| 25 timeZoneMinutes); | 25 timeZoneMinutes); |
| 26 } | 26 } |
| 27 | 27 |
| 28 #define SKPDF_STRING(X) SKPDF_STRING_IMPL(X) | 28 #define SKPDF_STRING(X) SKPDF_STRING_IMPL(X) |
| 29 #define SKPDF_STRING_IMPL(X) #X | 29 #define SKPDF_STRING_IMPL(X) #X |
| 30 | 30 |
| 31 SkPDFObject* SkPDFMetadata::createDocumentInformationDict() const { | 31 namespace { |
| 32 static const struct { |
| 33 const char* const key; |
| 34 SkString SkDocument::PDFMetadata::*const valuePtr; |
| 35 } gMetadataKeys[] = { |
| 36 {"Title", &SkDocument::PDFMetadata::fTitle}, |
| 37 {"Author", &SkDocument::PDFMetadata::fAuthor}, |
| 38 {"Subject", &SkDocument::PDFMetadata::fSubject}, |
| 39 {"Keywords", &SkDocument::PDFMetadata::fKeywords}, |
| 40 {"Creator", &SkDocument::PDFMetadata::fCreator}, |
| 41 }; |
| 42 } // namespace |
| 43 |
| 44 #ifdef SK_SUPPORT_LEGACY_DOCUMENT_API |
| 45 void SkPDFMetadata::SetMetadataByKey(const SkString& key, |
| 46 const SkString& value, |
| 47 SkDocument::PDFMetadata* metadata) { |
| 48 for (const auto keyValuePtr : gMetadataKeys) { |
| 49 if (key.equals(keyValuePtr.key)) { |
| 50 metadata->*(keyValuePtr.valuePtr) = value; |
| 51 } |
| 52 } |
| 53 } |
| 54 |
| 55 #endif |
| 56 |
| 57 sk_sp<SkPDFObject> SkPDFMetadata::MakeDocumentInformationDict( |
| 58 const SkDocument::PDFMetadata& metadata) { |
| 32 auto dict = sk_make_sp<SkPDFDict>(); | 59 auto dict = sk_make_sp<SkPDFDict>(); |
| 33 static const char* keys[] = { | 60 for (const auto keyValuePtr : gMetadataKeys) { |
| 34 "Title", "Author", "Subject", "Keywords", "Creator"}; | 61 const SkString& value = metadata.*(keyValuePtr.valuePtr); |
| 35 for (const char* key : keys) { | 62 if (value.size() > 0) { |
| 36 for (const SkDocument::Attribute& keyValue : fInfo) { | 63 dict->insertString(keyValuePtr.key, value); |
| 37 if (keyValue.fKey.equals(key)) { | |
| 38 dict->insertString(key, keyValue.fValue); | |
| 39 } | |
| 40 } | 64 } |
| 41 } | 65 } |
| 42 dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE)); | 66 dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE)); |
| 43 if (fCreation) { | 67 if (metadata.fCreation.fEnabled) { |
| 44 dict->insertString("CreationDate", pdf_date(*fCreation.get())); | 68 dict->insertString("CreationDate", |
| 69 pdf_date(metadata.fCreation.fDateTime)); |
| 45 } | 70 } |
| 46 if (fModified) { | 71 if (metadata.fModified.fEnabled) { |
| 47 dict->insertString("ModDate", pdf_date(*fModified.get())); | 72 dict->insertString("ModDate", pdf_date(metadata.fModified.fDateTime)); |
| 48 } | 73 } |
| 49 return dict.release(); | 74 return dict; |
| 50 } | 75 } |
| 51 | 76 |
| 52 SkPDFMetadata::UUID SkPDFMetadata::uuid() const { | 77 SkPDFMetadata::UUID SkPDFMetadata::CreateUUID( |
| 78 const SkDocument::PDFMetadata& metadata) { |
| 53 // The main requirement is for the UUID to be unique; the exact | 79 // The main requirement is for the UUID to be unique; the exact |
| 54 // format of the data that will be hashed is not important. | 80 // format of the data that will be hashed is not important. |
| 55 SkMD5 md5; | 81 SkMD5 md5; |
| 56 const char uuidNamespace[] = "org.skia.pdf\n"; | 82 const char uuidNamespace[] = "org.skia.pdf\n"; |
| 57 md5.write(uuidNamespace, strlen(uuidNamespace)); | 83 md5.write(uuidNamespace, strlen(uuidNamespace)); |
| 58 double msec = SkTime::GetMSecs(); | 84 double msec = SkTime::GetMSecs(); |
| 59 md5.write(&msec, sizeof(msec)); | 85 md5.write(&msec, sizeof(msec)); |
| 60 SkTime::DateTime dateTime; | 86 SkTime::DateTime dateTime; |
| 61 SkTime::GetDateTime(&dateTime); | 87 SkTime::GetDateTime(&dateTime); |
| 62 md5.write(&dateTime, sizeof(dateTime)); | 88 md5.write(&dateTime, sizeof(dateTime)); |
| 63 if (fCreation) { | 89 if (metadata.fCreation.fEnabled) { |
| 64 md5.write(fCreation.get(), sizeof(fCreation)); | 90 md5.write(&metadata.fCreation.fDateTime, |
| 91 sizeof(metadata.fCreation.fDateTime)); |
| 65 } | 92 } |
| 66 if (fModified) { | 93 if (metadata.fModified.fEnabled) { |
| 67 md5.write(fModified.get(), sizeof(fModified)); | 94 md5.write(&metadata.fModified.fDateTime, |
| 95 sizeof(metadata.fModified.fDateTime)); |
| 68 } | 96 } |
| 69 for (const auto& kv : fInfo) { | 97 |
| 70 md5.write(kv.fKey.c_str(), kv.fKey.size()); | 98 for (const auto keyValuePtr : gMetadataKeys) { |
| 99 md5.write(keyValuePtr.key, strlen(keyValuePtr.key)); |
| 71 md5.write("\037", 1); | 100 md5.write("\037", 1); |
| 72 md5.write(kv.fValue.c_str(), kv.fValue.size()); | 101 const SkString& value = metadata.*(keyValuePtr.valuePtr); |
| 102 md5.write(value.c_str(), value.size()); |
| 73 md5.write("\036", 1); | 103 md5.write("\036", 1); |
| 74 } | 104 } |
| 75 SkMD5::Digest digest; | 105 SkMD5::Digest digest; |
| 76 md5.finish(digest); | 106 md5.finish(digest); |
| 77 // See RFC 4122, page 6-7. | 107 // See RFC 4122, page 6-7. |
| 78 digest.data[6] = (digest.data[6] & 0x0F) | 0x30; | 108 digest.data[6] = (digest.data[6] & 0x0F) | 0x30; |
| 79 digest.data[8] = (digest.data[6] & 0x3F) | 0x80; | 109 digest.data[8] = (digest.data[6] & 0x3F) | 0x80; |
| 80 static_assert(sizeof(digest) == sizeof(UUID), "uuid_size"); | 110 static_assert(sizeof(digest) == sizeof(UUID), "uuid_size"); |
| 81 SkPDFMetadata::UUID uuid; | 111 SkPDFMetadata::UUID uuid; |
| 82 memcpy(&uuid, &digest, sizeof(digest)); | 112 memcpy(&uuid, &digest, sizeof(digest)); |
| 83 return uuid; | 113 return uuid; |
| 84 } | 114 } |
| 85 | 115 |
| 86 SkPDFObject* SkPDFMetadata::CreatePdfId(const UUID& doc, const UUID& instance) { | 116 sk_sp<SkPDFObject> SkPDFMetadata::MakePdfId(const UUID& doc, |
| 117 const UUID& instance) { |
| 87 // /ID [ <81b14aafa313db63dbd6f981e49f94f4> | 118 // /ID [ <81b14aafa313db63dbd6f981e49f94f4> |
| 88 // <81b14aafa313db63dbd6f981e49f94f4> ] | 119 // <81b14aafa313db63dbd6f981e49f94f4> ] |
| 89 auto array = sk_make_sp<SkPDFArray>(); | 120 auto array = sk_make_sp<SkPDFArray>(); |
| 90 static_assert(sizeof(UUID) == 16, "uuid_size"); | 121 static_assert(sizeof(SkPDFMetadata::UUID) == 16, "uuid_size"); |
| 91 array->appendString( | 122 array->appendString( |
| 92 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); | 123 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); |
| 93 array->appendString( | 124 array->appendString( |
| 94 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); | 125 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); |
| 95 return array.release(); | 126 return array; |
| 96 } | |
| 97 | |
| 98 static const SkString get(const SkTArray<SkDocument::Attribute>& info, | |
| 99 const char* key) { | |
| 100 for (const auto& keyValue : info) { | |
| 101 if (keyValue.fKey.equals(key)) { | |
| 102 return keyValue.fValue; | |
| 103 } | |
| 104 } | |
| 105 return SkString(); | |
| 106 } | 127 } |
| 107 | 128 |
| 108 #define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \ | 129 #define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \ |
| 109 do { \ | 130 do { \ |
| 110 for (int i = 0; i < (BYTE_COUNT); ++i) { \ | 131 for (int i = 0; i < (BYTE_COUNT); ++i) { \ |
| 111 uint8_t value = *(INPUT_PTR)++; \ | 132 uint8_t value = *(INPUT_PTR)++; \ |
| 112 *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4]; \ | 133 *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4]; \ |
| 113 *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF]; \ | 134 *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF]; \ |
| 114 } \ | 135 } \ |
| 115 } while (false) | 136 } while (false) |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 if (after) { | 228 if (after) { |
| 208 strncpy(out, after, afterLen); | 229 strncpy(out, after, afterLen); |
| 209 out += afterLen; | 230 out += afterLen; |
| 210 } | 231 } |
| 211 // Validate that we haven't written outside of our string. | 232 // Validate that we haven't written outside of our string. |
| 212 SkASSERT(out == &output.writable_str()[output.size()]); | 233 SkASSERT(out == &output.writable_str()[output.size()]); |
| 213 *out = '\0'; | 234 *out = '\0'; |
| 214 return output; | 235 return output; |
| 215 } | 236 } |
| 216 | 237 |
| 217 SkPDFObject* SkPDFMetadata::createXMPObject(const UUID& doc, | 238 sk_sp<SkPDFObject> SkPDFMetadata::MakeXMPObject( |
| 218 const UUID& instance) const { | 239 const SkDocument::PDFMetadata& metadata, |
| 240 const UUID& doc, |
| 241 const UUID& instance) { |
| 219 static const char templateString[] = | 242 static const char templateString[] = |
| 220 "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" | 243 "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" |
| 221 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" | 244 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" |
| 222 " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, " | 245 " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, " |
| 223 "2012/08/23-13:03:03\">\n" | 246 "2012/08/23-13:03:03\">\n" |
| 224 "<rdf:RDF " | 247 "<rdf:RDF " |
| 225 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" | 248 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" |
| 226 "<rdf:Description rdf:about=\"\"\n" | 249 "<rdf:Description rdf:about=\"\"\n" |
| 227 " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n" | 250 " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n" |
| 228 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" | 251 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 243 "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n" | 266 "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n" |
| 244 "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Produce
r>\n" | 267 "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Produce
r>\n" |
| 245 "%s" // pdf:Keywords | 268 "%s" // pdf:Keywords |
| 246 "</rdf:Description>\n" | 269 "</rdf:Description>\n" |
| 247 "</rdf:RDF>\n" | 270 "</rdf:RDF>\n" |
| 248 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. | 271 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. |
| 249 "<?xpacket end=\"w\"?>\n"; | 272 "<?xpacket end=\"w\"?>\n"; |
| 250 | 273 |
| 251 SkString creationDate; | 274 SkString creationDate; |
| 252 SkString modificationDate; | 275 SkString modificationDate; |
| 253 if (fCreation) { | 276 if (metadata.fCreation.fEnabled) { |
| 254 SkString tmp; | 277 SkString tmp; |
| 255 fCreation->toISO8601(&tmp); | 278 metadata.fCreation.fDateTime.toISO8601(&tmp); |
| 256 SkASSERT(0 == count_xml_escape_size(tmp)); | 279 SkASSERT(0 == count_xml_escape_size(tmp)); |
| 257 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape | 280 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape |
| 258 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n", | 281 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n", |
| 259 tmp.c_str()); | 282 tmp.c_str()); |
| 260 } | 283 } |
| 261 if (fModified) { | 284 if (metadata.fModified.fEnabled) { |
| 262 SkString tmp; | 285 SkString tmp; |
| 263 fModified->toISO8601(&tmp); | 286 metadata.fModified.fDateTime.toISO8601(&tmp); |
| 264 SkASSERT(0 == count_xml_escape_size(tmp)); | 287 SkASSERT(0 == count_xml_escape_size(tmp)); |
| 265 modificationDate = SkStringPrintf( | 288 modificationDate = SkStringPrintf( |
| 266 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); | 289 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); |
| 267 } | 290 } |
| 268 SkString title = escape_xml( | 291 SkString title = |
| 269 get(fInfo, "Title"), | 292 escape_xml(metadata.fTitle, |
| 270 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", | 293 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
| 271 "</rdf:li></rdf:Alt></dc:title>\n"); | 294 "</rdf:li></rdf:Alt></dc:title>\n"); |
| 272 SkString author = escape_xml( | 295 SkString author = |
| 273 get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", | 296 escape_xml(metadata.fAuthor, "<dc:creator><rdf:Bag><rdf:li>", |
| 274 "</rdf:li></rdf:Bag></dc:creator>\n"); | 297 "</rdf:li></rdf:Bag></dc:creator>\n"); |
| 275 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? | 298 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? |
| 276 SkString subject = escape_xml( | 299 SkString subject = escape_xml( |
| 277 get(fInfo, "Subject"), | 300 metadata.fSubject, |
| 278 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", | 301 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
| 279 "</rdf:li></rdf:Alt></dc:description>\n"); | 302 "</rdf:li></rdf:Alt></dc:description>\n"); |
| 280 SkString keywords1 = escape_xml( | 303 SkString keywords1 = |
| 281 get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", | 304 escape_xml(metadata.fKeywords, "<dc:subject><rdf:Bag><rdf:li>", |
| 282 "</rdf:li></rdf:Bag></dc:subject>\n"); | 305 "</rdf:li></rdf:Bag></dc:subject>\n"); |
| 283 SkString keywords2 = escape_xml( | 306 SkString keywords2 = escape_xml(metadata.fKeywords, "<pdf:Keywords>", |
| 284 get(fInfo, "Keywords"), "<pdf:Keywords>", | 307 "</pdf:Keywords>\n"); |
| 285 "</pdf:Keywords>\n"); | |
| 286 | 308 |
| 287 // TODO: in theory, keywords can be a list too. | 309 // TODO: in theory, keywords can be a list too. |
| 288 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", | 310 SkString creator = escape_xml(metadata.fCreator, "<xmp:CreatorTool>", |
| 289 "</xmp:CreatorTool>\n"); | 311 "</xmp:CreatorTool>\n"); |
| 290 SkString documentID = uuid_to_string(doc); // no need to escape | 312 SkString documentID = uuid_to_string(doc); // no need to escape |
| 291 SkASSERT(0 == count_xml_escape_size(documentID)); | 313 SkASSERT(0 == count_xml_escape_size(documentID)); |
| 292 SkString instanceID = uuid_to_string(instance); | 314 SkString instanceID = uuid_to_string(instance); |
| 293 SkASSERT(0 == count_xml_escape_size(instanceID)); | 315 SkASSERT(0 == count_xml_escape_size(instanceID)); |
| 294 return new PDFXMLObject(SkStringPrintf( | 316 return sk_make_sp<PDFXMLObject>(SkStringPrintf( |
| 295 templateString, modificationDate.c_str(), creationDate.c_str(), | 317 templateString, modificationDate.c_str(), creationDate.c_str(), |
| 296 creator.c_str(), title.c_str(), | 318 creator.c_str(), title.c_str(), subject.c_str(), author.c_str(), |
| 297 subject.c_str(), author.c_str(), keywords1.c_str(), | 319 keywords1.c_str(), documentID.c_str(), instanceID.c_str(), |
| 298 documentID.c_str(), instanceID.c_str(), keywords2.c_str())); | 320 keywords2.c_str())); |
| 299 } | 321 } |
| 300 | 322 |
| 301 #undef SKPDF_STRING | 323 #undef SKPDF_STRING |
| 302 #undef SKPDF_STRING_IMPL | 324 #undef SKPDF_STRING_IMPL |
| 303 | |
| OLD | NEW |