OLD | NEW |
(Empty) | |
| 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 |
| 8 #include "SkPDFMetadata.h" |
| 9 #include "SkPDFTypes.h" |
| 10 |
| 11 #ifdef SK_PDF_GENERATE_PDFA |
| 12 #include "SkMD5.h" |
| 13 #endif |
| 14 |
| 15 static SkString pdf_date(const SkTime::DateTime& dt) { |
| 16 int timeZoneMinutes = SkToInt(dt.fTimeZoneMinutes); |
| 17 char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-'; |
| 18 int timeZoneHours = SkTAbs(timeZoneMinutes) / 60; |
| 19 timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60; |
| 20 return SkStringPrintf( |
| 21 "D:%04u%02u%02u%02u%02u%02u%c%02d'%02d'", |
| 22 static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth), |
| 23 static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour), |
| 24 static_cast<unsigned>(dt.fMinute), |
| 25 static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours, |
| 26 timeZoneMinutes); |
| 27 } |
| 28 |
| 29 SkPDFObject* SkPDFMetadata::createDocumentInformationDict() const { |
| 30 SkAutoTUnref<SkPDFDict> dict(new SkPDFDict); |
| 31 static const char* keys[] = { |
| 32 "Title", "Author", "Subject", "Keywords", "Creator"}; |
| 33 for (const char* key : keys) { |
| 34 for (const SkDocument::Attribute& keyValue : fInfo) { |
| 35 if (keyValue.fKey.equals(key)) { |
| 36 dict->insertString(key, keyValue.fValue); |
| 37 } |
| 38 } |
| 39 } |
| 40 dict->insertString("Producer", "Skia/PDF"); |
| 41 if (fCreation) { |
| 42 dict->insertString("CreationDate", pdf_date(*fCreation.get())); |
| 43 } |
| 44 if (fModified) { |
| 45 dict->insertString("ModDate", pdf_date(*fModified.get())); |
| 46 } |
| 47 return dict.detach(); |
| 48 } |
| 49 |
| 50 #ifdef SK_PDF_GENERATE_PDFA |
| 51 SkPDFMetadata::UUID SkPDFMetadata::uuid() const { |
| 52 // The main requirement is for the UUID to be unique; the exact |
| 53 // format of the data that will be hashed is not important. |
| 54 SkMD5 md5; |
| 55 const char uuidNamespace[] = "org.skia.pdf\n"; |
| 56 md5.write(uuidNamespace, strlen(uuidNamespace)); |
| 57 SkMSec msec = SkTime::GetMSecs(); |
| 58 md5.write(&msec, sizeof(msec)); |
| 59 SkTime::DateTime dateTime; |
| 60 SkTime::GetDateTime(&dateTime); |
| 61 md5.write(&dateTime, sizeof(dateTime)); |
| 62 if (fCreation) { |
| 63 md5.write(fCreation.get(), sizeof(fCreation)); |
| 64 } |
| 65 if (fModified) { |
| 66 md5.write(fModified.get(), sizeof(fModified)); |
| 67 } |
| 68 for (const auto& kv : fInfo) { |
| 69 md5.write(kv.fKey.c_str(), kv.fKey.size()); |
| 70 md5.write("\037", 1); |
| 71 md5.write(kv.fValue.c_str(), kv.fValue.size()); |
| 72 md5.write("\036", 1); |
| 73 } |
| 74 SkMD5::Digest digest; |
| 75 md5.finish(digest); |
| 76 // See RFC 4122, page 6-7. |
| 77 digest.data[6] = (digest.data[6] & 0x0F) | 0x30; |
| 78 digest.data[8] = (digest.data[6] & 0x3F) | 0x80; |
| 79 static_assert(sizeof(digest) == sizeof(UUID), "uuid_size"); |
| 80 SkPDFMetadata::UUID uuid; |
| 81 memcpy(&uuid, &digest, sizeof(digest)); |
| 82 return uuid; |
| 83 } |
| 84 |
| 85 SkPDFObject* SkPDFMetadata::CreatePdfId(const UUID& doc, const UUID& instance) { |
| 86 // /ID [ <81b14aafa313db63dbd6f981e49f94f4> |
| 87 // <81b14aafa313db63dbd6f981e49f94f4> ] |
| 88 SkAutoTUnref<SkPDFArray> array(new SkPDFArray); |
| 89 static_assert(sizeof(UUID) == 16, "uuid_size"); |
| 90 array->appendString( |
| 91 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); |
| 92 array->appendString( |
| 93 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); |
| 94 return array.detach(); |
| 95 } |
| 96 |
| 97 // Improvement on SkStringPrintf to allow for arbitrarily long output. |
| 98 // TODO: replace SkStringPrintf. |
| 99 static SkString sk_string_printf(const char* format, ...) { |
| 100 #ifdef SK_BUILD_FOR_WIN |
| 101 va_list args; |
| 102 va_start(args, format); |
| 103 char buffer[1024]; |
| 104 int length = _vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, args); |
| 105 va_end(args); |
| 106 if (length >= 0 && length < (int)sizeof(buffer)) { |
| 107 return SkString(buffer, length); |
| 108 } |
| 109 va_start(args, format); |
| 110 length = _vscprintf(format, args); |
| 111 va_end(args); |
| 112 |
| 113 SkString string((size_t)length); |
| 114 va_start(args, format); |
| 115 SkDEBUGCODE(int check = ) _vsnprintf_s(string.writable_str(), length + 1, |
| 116 _TRUNCATE, format, args); |
| 117 va_end(args); |
| 118 SkASSERT(check == length); |
| 119 SkASSERT(string[length] == '\0'); |
| 120 return skstd::move(string); |
| 121 #else // C99/C++11 standard vsnprintf |
| 122 // TODO: When all compilers support this, remove windows-specific code. |
| 123 va_list args; |
| 124 va_start(args, format); |
| 125 char buffer[1024]; |
| 126 int length = vsnprintf(buffer, sizeof(buffer), format, args); |
| 127 va_end(args); |
| 128 if (length < 0) { |
| 129 return SkString(); |
| 130 } |
| 131 if (length < (int)sizeof(buffer)) { |
| 132 return SkString(buffer, length); |
| 133 } |
| 134 SkString string((size_t)length); |
| 135 va_start(args, format); |
| 136 SkDEBUGCODE(int check = ) |
| 137 vsnprintf(string.writable_str(), length + 1, format, args); |
| 138 va_end(args); |
| 139 SkASSERT(check == length); |
| 140 SkASSERT(string[length] == '\0'); |
| 141 return skstd::move(string); |
| 142 #endif |
| 143 } |
| 144 |
| 145 static const SkString get(const SkTArray<SkDocument::Attribute>& info, |
| 146 const char* key) { |
| 147 for (const auto& keyValue : info) { |
| 148 if (keyValue.fKey.equals(key)) { |
| 149 return keyValue.fValue; |
| 150 } |
| 151 } |
| 152 return SkString(); |
| 153 } |
| 154 |
| 155 #define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \ |
| 156 do { \ |
| 157 for (int i = 0; i < (BYTE_COUNT); ++i) { \ |
| 158 uint8_t value = *(INPUT_PTR)++; \ |
| 159 *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4]; \ |
| 160 *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF]; \ |
| 161 } \ |
| 162 } while (false) |
| 163 static SkString uuid_to_string(const SkPDFMetadata::UUID& uuid) { |
| 164 // 8-4-4-4-12 |
| 165 char buffer[36]; // [32 + 4] |
| 166 static const char gHex[] = "0123456789abcdef"; |
| 167 SkASSERT(strlen(gHex) == 16); |
| 168 char* ptr = buffer; |
| 169 const uint8_t* data = uuid.fData; |
| 170 HEXIFY(data, ptr, gHex, 4); |
| 171 *ptr++ = '-'; |
| 172 HEXIFY(data, ptr, gHex, 2); |
| 173 *ptr++ = '-'; |
| 174 HEXIFY(data, ptr, gHex, 2); |
| 175 *ptr++ = '-'; |
| 176 HEXIFY(data, ptr, gHex, 2); |
| 177 *ptr++ = '-'; |
| 178 HEXIFY(data, ptr, gHex, 6); |
| 179 SkASSERT(ptr == buffer + 36); |
| 180 SkASSERT(data == uuid.fData + 16); |
| 181 return SkString(buffer, 36); |
| 182 } |
| 183 #undef HEXIFY |
| 184 |
| 185 namespace { |
| 186 class PDFXMLObject : public SkPDFObject { |
| 187 public: |
| 188 PDFXMLObject(SkString xml) : fXML(skstd::move(xml)) {} |
| 189 void emitObject(SkWStream* stream, |
| 190 const SkPDFObjNumMap& omap, |
| 191 const SkPDFSubstituteMap& smap) const override { |
| 192 SkPDFDict dict("Metadata"); |
| 193 dict.insertName("Subtype", "XML"); |
| 194 dict.insertInt("Length", fXML.size()); |
| 195 dict.emitObject(stream, omap, smap); |
| 196 static const char streamBegin[] = " stream\n"; |
| 197 stream->write(streamBegin, strlen(streamBegin)); |
| 198 // Do not compress this. The standard requires that a |
| 199 // program that does not understand PDF can grep for |
| 200 // "<?xpacket" and extracť the entire XML. |
| 201 stream->write(fXML.c_str(), fXML.size()); |
| 202 static const char streamEnd[] = "\nendstream"; |
| 203 stream->write(streamEnd, strlen(streamEnd)); |
| 204 } |
| 205 |
| 206 private: |
| 207 const SkString fXML; |
| 208 }; |
| 209 } // namespace |
| 210 |
| 211 static int count_xml_escape_size(const SkString& input) { |
| 212 int extra = 0; |
| 213 for (size_t i = 0; i < input.size(); ++i) { |
| 214 if (input[i] == '&') { |
| 215 extra += 4; // strlen("&") - strlen("&") |
| 216 } else if (input[i] == '<') { |
| 217 extra += 3; // strlen("<") - strlen("<") |
| 218 } |
| 219 } |
| 220 return extra; |
| 221 } |
| 222 |
| 223 const SkString escape_xml(const SkString& input, |
| 224 const char* before = nullptr, |
| 225 const char* after = nullptr) { |
| 226 if (input.size() == 0) { |
| 227 return input; |
| 228 } |
| 229 // "&" --> "&" and "<" --> "<" |
| 230 // text is assumed to be in UTF-8 |
| 231 // all strings are xml content, not attribute values. |
| 232 size_t beforeLen = before ? strlen(before) : 0; |
| 233 size_t afterLen = after ? strlen(after) : 0; |
| 234 int extra = count_xml_escape_size(input); |
| 235 SkString output(input.size() + extra + beforeLen + afterLen); |
| 236 char* out = output.writable_str(); |
| 237 if (before) { |
| 238 strncpy(out, before, beforeLen); |
| 239 out += beforeLen; |
| 240 } |
| 241 static const char kAmp[] = "&"; |
| 242 static const char kLt[] = "<"; |
| 243 for (size_t i = 0; i < input.size(); ++i) { |
| 244 if (input[i] == '&') { |
| 245 strncpy(out, kAmp, strlen(kAmp)); |
| 246 out += strlen(kAmp); |
| 247 } else if (input[i] == '<') { |
| 248 strncpy(out, kLt, strlen(kLt)); |
| 249 out += strlen(kLt); |
| 250 } else { |
| 251 *out++ = input[i]; |
| 252 } |
| 253 } |
| 254 if (after) { |
| 255 strncpy(out, after, afterLen); |
| 256 out += afterLen; |
| 257 } |
| 258 // Validate that we haven't written outside of our string. |
| 259 SkASSERT(out == &output.writable_str()[output.size()]); |
| 260 *out = '\0'; |
| 261 return skstd::move(output); |
| 262 } |
| 263 |
| 264 SkPDFObject* SkPDFMetadata::createXMPObject(const UUID& doc, |
| 265 const UUID& instance) const { |
| 266 static const char templateString[] = |
| 267 "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" |
| 268 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" |
| 269 " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, " |
| 270 "2012/08/23-13:03:03\">\n" |
| 271 "<rdf:RDF " |
| 272 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" |
| 273 "<rdf:Description rdf:about=\"\"\n" |
| 274 " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n" |
| 275 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" |
| 276 " xmlns:xmpMM=\"http://ns.adobe.com/xap/1.0/mm/\"\n" |
| 277 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"\n" |
| 278 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n" |
| 279 "<pdfaid:part>2</pdfaid:part>\n" |
| 280 "<pdfaid:conformance>B</pdfaid:conformance>\n" |
| 281 "%s" // ModifyDate |
| 282 "%s" // CreateDate |
| 283 "%s" // MetadataDate |
| 284 "%s" // xmp:CreatorTool |
| 285 "<dc:format>application/pdf</dc:format>\n" |
| 286 "%s" // dc:title |
| 287 "%s" // dc:description |
| 288 "%s" // author |
| 289 "%s" // keywords |
| 290 "<xmpMM:DocumentID>uuid:%s</xmpMM:DocumentID>\n" |
| 291 "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n" |
| 292 "<pdf:Producer>Skia/PDF</pdf:Producer>\n" |
| 293 "%s" // pdf:Keywords |
| 294 "</rdf:Description>\n" |
| 295 "</rdf:RDF>\n" |
| 296 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. |
| 297 "<?xpacket end=\"w\"?>\n"; |
| 298 |
| 299 SkString creationDate; |
| 300 SkString modificationDate; |
| 301 SkString metadataDate; |
| 302 if (fCreation) { |
| 303 SkString tmp; |
| 304 fCreation->toISO8601(&tmp); |
| 305 SkASSERT(0 == count_xml_escape_size(tmp)); |
| 306 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape |
| 307 creationDate = sk_string_printf("<xmp:CreateDate>%s</xmp:CreateDate>\n", |
| 308 tmp.c_str()); |
| 309 } |
| 310 if (fModified) { |
| 311 SkString tmp; |
| 312 fModified->toISO8601(&tmp); |
| 313 SkASSERT(0 == count_xml_escape_size(tmp)); |
| 314 modificationDate = sk_string_printf( |
| 315 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); |
| 316 metadataDate = sk_string_printf( |
| 317 "<xmp:MetadataDate>%s</xmp:MetadataDate>\n", tmp.c_str()); |
| 318 } |
| 319 |
| 320 SkString title = |
| 321 escape_xml(get(fInfo, "Title"), "<dc:title><rdf:Alt><rdf:li>", |
| 322 "</rdf:li></rdf:Alt></dc:title>\n"); |
| 323 SkString author = |
| 324 escape_xml(get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", |
| 325 "</rdf:li></rdf:Bag></dc:creator>\n"); |
| 326 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? |
| 327 SkString subject = escape_xml(get(fInfo, "Subject"), |
| 328 "<dc:description><rdf:Alt><rdf:li>", |
| 329 "</rdf:li></rdf:Alt></dc:description>\n"); |
| 330 SkString keywords1 = |
| 331 escape_xml(get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", |
| 332 "</rdf:li></rdf:Bag></dc:subject>\n"); |
| 333 SkString keywords2 = escape_xml(get(fInfo, "Keywords"), "<pdf:Keywords>", |
| 334 "</pdf:Keywords>\n"); |
| 335 |
| 336 // TODO: in theory, keywords can be a list too. |
| 337 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", |
| 338 "</xmp:CreatorTool>\n"); |
| 339 SkString documentID = uuid_to_string(doc); // no need to escape |
| 340 SkASSERT(0 == count_xml_escape_size(documentID)); |
| 341 SkString instanceID = uuid_to_string(instance); |
| 342 SkASSERT(0 == count_xml_escape_size(instanceID)); |
| 343 return new PDFXMLObject(sk_string_printf( |
| 344 templateString, modificationDate.c_str(), creationDate.c_str(), |
| 345 metadataDate.c_str(), creator.c_str(), title.c_str(), |
| 346 subject.c_str(), author.c_str(), keywords1.c_str(), |
| 347 documentID.c_str(), instanceID.c_str(), keywords2.c_str())); |
| 348 } |
| 349 |
| 350 #endif // SK_PDF_GENERATE_PDFA |
OLD | NEW |