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