Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: src/pdf/SkPDFMetadata.cpp

Issue 1916093002: SkDocument/PDF: new API (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-04-26 (Tuesday) 11:04:14 EDT Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 sk_sp<SkPDFObject> SkPDFMetadata::MakeDocumentInformationDict(
45 const SkDocument::PDFMetadata& metadata) {
32 auto dict = sk_make_sp<SkPDFDict>(); 46 auto dict = sk_make_sp<SkPDFDict>();
33 static const char* keys[] = { 47 for (const auto keyValuePtr : gMetadataKeys) {
34 "Title", "Author", "Subject", "Keywords", "Creator"}; 48 const SkString& value = metadata.*(keyValuePtr.valuePtr);
35 for (const char* key : keys) { 49 if (value.size() > 0) {
36 for (const SkDocument::Attribute& keyValue : fInfo) { 50 dict->insertString(keyValuePtr.key, value);
37 if (keyValue.fKey.equals(key)) {
38 dict->insertString(key, keyValue.fValue);
39 }
40 } 51 }
41 } 52 }
42 dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE)); 53 dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE));
43 if (fCreation) { 54 if (metadata.fCreation.fEnabled) {
44 dict->insertString("CreationDate", pdf_date(*fCreation.get())); 55 dict->insertString("CreationDate",
56 pdf_date(metadata.fCreation.fDateTime));
45 } 57 }
46 if (fModified) { 58 if (metadata.fModified.fEnabled) {
47 dict->insertString("ModDate", pdf_date(*fModified.get())); 59 dict->insertString("ModDate", pdf_date(metadata.fModified.fDateTime));
48 } 60 }
49 return dict.release(); 61 return dict;
50 } 62 }
51 63
52 SkPDFMetadata::UUID SkPDFMetadata::uuid() const { 64 SkPDFMetadata::UUID SkPDFMetadata::CreateUUID(
65 const SkDocument::PDFMetadata& metadata) {
53 // The main requirement is for the UUID to be unique; the exact 66 // The main requirement is for the UUID to be unique; the exact
54 // format of the data that will be hashed is not important. 67 // format of the data that will be hashed is not important.
55 SkMD5 md5; 68 SkMD5 md5;
56 const char uuidNamespace[] = "org.skia.pdf\n"; 69 const char uuidNamespace[] = "org.skia.pdf\n";
57 md5.write(uuidNamespace, strlen(uuidNamespace)); 70 md5.write(uuidNamespace, strlen(uuidNamespace));
58 double msec = SkTime::GetMSecs(); 71 double msec = SkTime::GetMSecs();
59 md5.write(&msec, sizeof(msec)); 72 md5.write(&msec, sizeof(msec));
60 SkTime::DateTime dateTime; 73 SkTime::DateTime dateTime;
61 SkTime::GetDateTime(&dateTime); 74 SkTime::GetDateTime(&dateTime);
62 md5.write(&dateTime, sizeof(dateTime)); 75 md5.write(&dateTime, sizeof(dateTime));
63 if (fCreation) { 76 if (metadata.fCreation.fEnabled) {
64 md5.write(fCreation.get(), sizeof(fCreation)); 77 md5.write(&metadata.fCreation.fDateTime,
78 sizeof(metadata.fCreation.fDateTime));
65 } 79 }
66 if (fModified) { 80 if (metadata.fModified.fEnabled) {
67 md5.write(fModified.get(), sizeof(fModified)); 81 md5.write(&metadata.fModified.fDateTime,
82 sizeof(metadata.fModified.fDateTime));
68 } 83 }
69 for (const auto& kv : fInfo) { 84
70 md5.write(kv.fKey.c_str(), kv.fKey.size()); 85 for (const auto keyValuePtr : gMetadataKeys) {
86 md5.write(keyValuePtr.key, strlen(keyValuePtr.key));
71 md5.write("\037", 1); 87 md5.write("\037", 1);
72 md5.write(kv.fValue.c_str(), kv.fValue.size()); 88 const SkString& value = metadata.*(keyValuePtr.valuePtr);
89 md5.write(value.c_str(), value.size());
73 md5.write("\036", 1); 90 md5.write("\036", 1);
74 } 91 }
75 SkMD5::Digest digest; 92 SkMD5::Digest digest;
76 md5.finish(digest); 93 md5.finish(digest);
77 // See RFC 4122, page 6-7. 94 // See RFC 4122, page 6-7.
78 digest.data[6] = (digest.data[6] & 0x0F) | 0x30; 95 digest.data[6] = (digest.data[6] & 0x0F) | 0x30;
79 digest.data[8] = (digest.data[6] & 0x3F) | 0x80; 96 digest.data[8] = (digest.data[6] & 0x3F) | 0x80;
80 static_assert(sizeof(digest) == sizeof(UUID), "uuid_size"); 97 static_assert(sizeof(digest) == sizeof(UUID), "uuid_size");
81 SkPDFMetadata::UUID uuid; 98 SkPDFMetadata::UUID uuid;
82 memcpy(&uuid, &digest, sizeof(digest)); 99 memcpy(&uuid, &digest, sizeof(digest));
83 return uuid; 100 return uuid;
84 } 101 }
85 102
86 SkPDFObject* SkPDFMetadata::CreatePdfId(const UUID& doc, const UUID& instance) { 103 sk_sp<SkPDFObject> SkPDFMetadata::MakePdfId(const UUID& doc,
104 const UUID& instance) {
87 // /ID [ <81b14aafa313db63dbd6f981e49f94f4> 105 // /ID [ <81b14aafa313db63dbd6f981e49f94f4>
88 // <81b14aafa313db63dbd6f981e49f94f4> ] 106 // <81b14aafa313db63dbd6f981e49f94f4> ]
89 auto array = sk_make_sp<SkPDFArray>(); 107 auto array = sk_make_sp<SkPDFArray>();
90 static_assert(sizeof(UUID) == 16, "uuid_size"); 108 static_assert(sizeof(SkPDFMetadata::UUID) == 16, "uuid_size");
91 array->appendString( 109 array->appendString(
92 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); 110 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID)));
93 array->appendString( 111 array->appendString(
94 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); 112 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID)));
95 return array.release(); 113 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 } 114 }
107 115
108 #define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \ 116 #define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \
109 do { \ 117 do { \
110 for (int i = 0; i < (BYTE_COUNT); ++i) { \ 118 for (int i = 0; i < (BYTE_COUNT); ++i) { \
111 uint8_t value = *(INPUT_PTR)++; \ 119 uint8_t value = *(INPUT_PTR)++; \
112 *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4]; \ 120 *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4]; \
113 *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF]; \ 121 *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF]; \
114 } \ 122 } \
115 } while (false) 123 } while (false)
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 if (after) { 215 if (after) {
208 strncpy(out, after, afterLen); 216 strncpy(out, after, afterLen);
209 out += afterLen; 217 out += afterLen;
210 } 218 }
211 // Validate that we haven't written outside of our string. 219 // Validate that we haven't written outside of our string.
212 SkASSERT(out == &output.writable_str()[output.size()]); 220 SkASSERT(out == &output.writable_str()[output.size()]);
213 *out = '\0'; 221 *out = '\0';
214 return output; 222 return output;
215 } 223 }
216 224
217 SkPDFObject* SkPDFMetadata::createXMPObject(const UUID& doc, 225 sk_sp<SkPDFObject> SkPDFMetadata::MakeXMPObject(
218 const UUID& instance) const { 226 const SkDocument::PDFMetadata& metadata,
227 const UUID& doc,
228 const UUID& instance) {
219 static const char templateString[] = 229 static const char templateString[] =
220 "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" 230 "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
221 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" 231 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n"
222 " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, " 232 " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, "
223 "2012/08/23-13:03:03\">\n" 233 "2012/08/23-13:03:03\">\n"
224 "<rdf:RDF " 234 "<rdf:RDF "
225 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" 235 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
226 "<rdf:Description rdf:about=\"\"\n" 236 "<rdf:Description rdf:about=\"\"\n"
227 " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n" 237 " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n"
228 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" 238 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n"
(...skipping 14 matching lines...) Expand all
243 "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n" 253 "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n"
244 "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Produce r>\n" 254 "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Produce r>\n"
245 "%s" // pdf:Keywords 255 "%s" // pdf:Keywords
246 "</rdf:Description>\n" 256 "</rdf:Description>\n"
247 "</rdf:RDF>\n" 257 "</rdf:RDF>\n"
248 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. 258 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding.
249 "<?xpacket end=\"w\"?>\n"; 259 "<?xpacket end=\"w\"?>\n";
250 260
251 SkString creationDate; 261 SkString creationDate;
252 SkString modificationDate; 262 SkString modificationDate;
253 if (fCreation) { 263 if (metadata.fCreation.fEnabled) {
254 SkString tmp; 264 SkString tmp;
255 fCreation->toISO8601(&tmp); 265 metadata.fCreation.fDateTime.toISO8601(&tmp);
256 SkASSERT(0 == count_xml_escape_size(tmp)); 266 SkASSERT(0 == count_xml_escape_size(tmp));
257 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape 267 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape
258 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n", 268 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n",
259 tmp.c_str()); 269 tmp.c_str());
260 } 270 }
261 if (fModified) { 271 if (metadata.fModified.fEnabled) {
262 SkString tmp; 272 SkString tmp;
263 fModified->toISO8601(&tmp); 273 metadata.fModified.fDateTime.toISO8601(&tmp);
264 SkASSERT(0 == count_xml_escape_size(tmp)); 274 SkASSERT(0 == count_xml_escape_size(tmp));
265 modificationDate = SkStringPrintf( 275 modificationDate = SkStringPrintf(
266 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); 276 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str());
267 } 277 }
268 SkString title = escape_xml( 278 SkString title =
269 get(fInfo, "Title"), 279 escape_xml(metadata.fTitle,
270 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", 280 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">",
271 "</rdf:li></rdf:Alt></dc:title>\n"); 281 "</rdf:li></rdf:Alt></dc:title>\n");
272 SkString author = escape_xml( 282 SkString author =
273 get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", 283 escape_xml(metadata.fAuthor, "<dc:creator><rdf:Bag><rdf:li>",
274 "</rdf:li></rdf:Bag></dc:creator>\n"); 284 "</rdf:li></rdf:Bag></dc:creator>\n");
275 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? 285 // TODO: in theory, XMP can support multiple authors. Split on a delimiter?
276 SkString subject = escape_xml( 286 SkString subject = escape_xml(
277 get(fInfo, "Subject"), 287 metadata.fSubject,
278 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", 288 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">",
279 "</rdf:li></rdf:Alt></dc:description>\n"); 289 "</rdf:li></rdf:Alt></dc:description>\n");
280 SkString keywords1 = escape_xml( 290 SkString keywords1 =
281 get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", 291 escape_xml(metadata.fKeywords, "<dc:subject><rdf:Bag><rdf:li>",
282 "</rdf:li></rdf:Bag></dc:subject>\n"); 292 "</rdf:li></rdf:Bag></dc:subject>\n");
283 SkString keywords2 = escape_xml( 293 SkString keywords2 = escape_xml(metadata.fKeywords, "<pdf:Keywords>",
284 get(fInfo, "Keywords"), "<pdf:Keywords>", 294 "</pdf:Keywords>\n");
285 "</pdf:Keywords>\n");
286 295
287 // TODO: in theory, keywords can be a list too. 296 // TODO: in theory, keywords can be a list too.
288 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", 297 SkString creator = escape_xml(metadata.fCreator, "<xmp:CreatorTool>",
289 "</xmp:CreatorTool>\n"); 298 "</xmp:CreatorTool>\n");
290 SkString documentID = uuid_to_string(doc); // no need to escape 299 SkString documentID = uuid_to_string(doc); // no need to escape
291 SkASSERT(0 == count_xml_escape_size(documentID)); 300 SkASSERT(0 == count_xml_escape_size(documentID));
292 SkString instanceID = uuid_to_string(instance); 301 SkString instanceID = uuid_to_string(instance);
293 SkASSERT(0 == count_xml_escape_size(instanceID)); 302 SkASSERT(0 == count_xml_escape_size(instanceID));
294 return new PDFXMLObject(SkStringPrintf( 303 return sk_make_sp<PDFXMLObject>(SkStringPrintf(
295 templateString, modificationDate.c_str(), creationDate.c_str(), 304 templateString, modificationDate.c_str(), creationDate.c_str(),
296 creator.c_str(), title.c_str(), 305 creator.c_str(), title.c_str(), subject.c_str(), author.c_str(),
297 subject.c_str(), author.c_str(), keywords1.c_str(), 306 keywords1.c_str(), documentID.c_str(), instanceID.c_str(),
298 documentID.c_str(), instanceID.c_str(), keywords2.c_str())); 307 keywords2.c_str()));
299 } 308 }
300 309
301 #undef SKPDF_STRING 310 #undef SKPDF_STRING
302 #undef SKPDF_STRING_IMPL 311 #undef SKPDF_STRING_IMPL
303
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698