blob: d3abd78312caecdcfff651cc6d7698b28abc6149 [file] [log] [blame]
halcanary34422612015-10-12 10:11:18 -07001/*
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
halcanary488165e2016-04-22 06:10:21 -07008#include "SkMD5.h"
halcanaryffe54002016-03-29 09:09:29 -07009#include "SkMilestone.h"
halcanary34422612015-10-12 10:11:18 -070010#include "SkPDFMetadata.h"
11#include "SkPDFTypes.h"
bungeman221524d2016-01-05 14:59:40 -080012#include <utility>
halcanary34422612015-10-12 10:11:18 -070013
halcanary34422612015-10-12 10:11:18 -070014static SkString pdf_date(const SkTime::DateTime& dt) {
15 int timeZoneMinutes = SkToInt(dt.fTimeZoneMinutes);
16 char timezoneSign = timeZoneMinutes >= 0 ? '+' : '-';
17 int timeZoneHours = SkTAbs(timeZoneMinutes) / 60;
18 timeZoneMinutes = SkTAbs(timeZoneMinutes) % 60;
19 return SkStringPrintf(
20 "D:%04u%02u%02u%02u%02u%02u%c%02d'%02d'",
21 static_cast<unsigned>(dt.fYear), static_cast<unsigned>(dt.fMonth),
22 static_cast<unsigned>(dt.fDay), static_cast<unsigned>(dt.fHour),
23 static_cast<unsigned>(dt.fMinute),
24 static_cast<unsigned>(dt.fSecond), timezoneSign, timeZoneHours,
25 timeZoneMinutes);
26}
27
halcanaryffe54002016-03-29 09:09:29 -070028#define SKPDF_STRING(X) SKPDF_STRING_IMPL(X)
29#define SKPDF_STRING_IMPL(X) #X
30
halcanary34422612015-10-12 10:11:18 -070031SkPDFObject* SkPDFMetadata::createDocumentInformationDict() const {
halcanaryece83922016-03-08 08:32:12 -080032 auto dict = sk_make_sp<SkPDFDict>();
halcanary34422612015-10-12 10:11:18 -070033 static const char* keys[] = {
34 "Title", "Author", "Subject", "Keywords", "Creator"};
35 for (const char* key : keys) {
36 for (const SkDocument::Attribute& keyValue : fInfo) {
37 if (keyValue.fKey.equals(key)) {
38 dict->insertString(key, keyValue.fValue);
39 }
40 }
41 }
halcanaryffe54002016-03-29 09:09:29 -070042 dict->insertString("Producer", "Skia/PDF m" SKPDF_STRING(SK_MILESTONE));
halcanary34422612015-10-12 10:11:18 -070043 if (fCreation) {
44 dict->insertString("CreationDate", pdf_date(*fCreation.get()));
45 }
46 if (fModified) {
47 dict->insertString("ModDate", pdf_date(*fModified.get()));
48 }
halcanaryfcad44b2016-03-06 14:47:10 -080049 return dict.release();
halcanary34422612015-10-12 10:11:18 -070050}
51
halcanary34422612015-10-12 10:11:18 -070052SkPDFMetadata::UUID SkPDFMetadata::uuid() const {
53 // The main requirement is for the UUID to be unique; the exact
54 // format of the data that will be hashed is not important.
55 SkMD5 md5;
56 const char uuidNamespace[] = "org.skia.pdf\n";
57 md5.write(uuidNamespace, strlen(uuidNamespace));
benjaminwagnerec4d4d72016-03-25 12:59:53 -070058 double msec = SkTime::GetMSecs();
halcanary34422612015-10-12 10:11:18 -070059 md5.write(&msec, sizeof(msec));
60 SkTime::DateTime dateTime;
61 SkTime::GetDateTime(&dateTime);
62 md5.write(&dateTime, sizeof(dateTime));
63 if (fCreation) {
64 md5.write(fCreation.get(), sizeof(fCreation));
65 }
66 if (fModified) {
67 md5.write(fModified.get(), sizeof(fModified));
68 }
69 for (const auto& kv : fInfo) {
70 md5.write(kv.fKey.c_str(), kv.fKey.size());
71 md5.write("\037", 1);
72 md5.write(kv.fValue.c_str(), kv.fValue.size());
73 md5.write("\036", 1);
74 }
75 SkMD5::Digest digest;
76 md5.finish(digest);
77 // See RFC 4122, page 6-7.
78 digest.data[6] = (digest.data[6] & 0x0F) | 0x30;
79 digest.data[8] = (digest.data[6] & 0x3F) | 0x80;
80 static_assert(sizeof(digest) == sizeof(UUID), "uuid_size");
81 SkPDFMetadata::UUID uuid;
82 memcpy(&uuid, &digest, sizeof(digest));
83 return uuid;
84}
85
86SkPDFObject* SkPDFMetadata::CreatePdfId(const UUID& doc, const UUID& instance) {
87 // /ID [ <81b14aafa313db63dbd6f981e49f94f4>
88 // <81b14aafa313db63dbd6f981e49f94f4> ]
halcanaryece83922016-03-08 08:32:12 -080089 auto array = sk_make_sp<SkPDFArray>();
halcanary34422612015-10-12 10:11:18 -070090 static_assert(sizeof(UUID) == 16, "uuid_size");
91 array->appendString(
92 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID)));
93 array->appendString(
94 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID)));
halcanaryfcad44b2016-03-06 14:47:10 -080095 return array.release();
halcanary34422612015-10-12 10:11:18 -070096}
97
halcanary34422612015-10-12 10:11:18 -070098static 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}
107
108#define HEXIFY(INPUT_PTR, OUTPUT_PTR, HEX_STRING, BYTE_COUNT) \
109 do { \
110 for (int i = 0; i < (BYTE_COUNT); ++i) { \
111 uint8_t value = *(INPUT_PTR)++; \
112 *(OUTPUT_PTR)++ = (HEX_STRING)[value >> 4]; \
113 *(OUTPUT_PTR)++ = (HEX_STRING)[value & 0xF]; \
114 } \
115 } while (false)
116static SkString uuid_to_string(const SkPDFMetadata::UUID& uuid) {
117 // 8-4-4-4-12
118 char buffer[36]; // [32 + 4]
119 static const char gHex[] = "0123456789abcdef";
120 SkASSERT(strlen(gHex) == 16);
121 char* ptr = buffer;
122 const uint8_t* data = uuid.fData;
123 HEXIFY(data, ptr, gHex, 4);
124 *ptr++ = '-';
125 HEXIFY(data, ptr, gHex, 2);
126 *ptr++ = '-';
127 HEXIFY(data, ptr, gHex, 2);
128 *ptr++ = '-';
129 HEXIFY(data, ptr, gHex, 2);
130 *ptr++ = '-';
131 HEXIFY(data, ptr, gHex, 6);
132 SkASSERT(ptr == buffer + 36);
133 SkASSERT(data == uuid.fData + 16);
134 return SkString(buffer, 36);
135}
136#undef HEXIFY
137
138namespace {
halcanary70d15542015-11-22 12:55:04 -0800139class PDFXMLObject final : public SkPDFObject {
halcanary34422612015-10-12 10:11:18 -0700140public:
bungeman221524d2016-01-05 14:59:40 -0800141 PDFXMLObject(SkString xml) : fXML(std::move(xml)) {}
halcanary34422612015-10-12 10:11:18 -0700142 void emitObject(SkWStream* stream,
143 const SkPDFObjNumMap& omap,
144 const SkPDFSubstituteMap& smap) const override {
145 SkPDFDict dict("Metadata");
146 dict.insertName("Subtype", "XML");
147 dict.insertInt("Length", fXML.size());
148 dict.emitObject(stream, omap, smap);
149 static const char streamBegin[] = " stream\n";
150 stream->write(streamBegin, strlen(streamBegin));
151 // Do not compress this. The standard requires that a
152 // program that does not understand PDF can grep for
153 // "<?xpacket" and extracť the entire XML.
154 stream->write(fXML.c_str(), fXML.size());
155 static const char streamEnd[] = "\nendstream";
156 stream->write(streamEnd, strlen(streamEnd));
157 }
158
159private:
160 const SkString fXML;
161};
162} // namespace
163
164static int count_xml_escape_size(const SkString& input) {
165 int extra = 0;
166 for (size_t i = 0; i < input.size(); ++i) {
167 if (input[i] == '&') {
168 extra += 4; // strlen("&amp;") - strlen("&")
169 } else if (input[i] == '<') {
170 extra += 3; // strlen("&lt;") - strlen("<")
171 }
172 }
173 return extra;
174}
175
176const SkString escape_xml(const SkString& input,
177 const char* before = nullptr,
178 const char* after = nullptr) {
179 if (input.size() == 0) {
180 return input;
181 }
182 // "&" --> "&amp;" and "<" --> "&lt;"
183 // text is assumed to be in UTF-8
184 // all strings are xml content, not attribute values.
185 size_t beforeLen = before ? strlen(before) : 0;
186 size_t afterLen = after ? strlen(after) : 0;
187 int extra = count_xml_escape_size(input);
188 SkString output(input.size() + extra + beforeLen + afterLen);
189 char* out = output.writable_str();
190 if (before) {
191 strncpy(out, before, beforeLen);
192 out += beforeLen;
193 }
194 static const char kAmp[] = "&amp;";
195 static const char kLt[] = "&lt;";
196 for (size_t i = 0; i < input.size(); ++i) {
197 if (input[i] == '&') {
198 strncpy(out, kAmp, strlen(kAmp));
199 out += strlen(kAmp);
200 } else if (input[i] == '<') {
201 strncpy(out, kLt, strlen(kLt));
202 out += strlen(kLt);
203 } else {
204 *out++ = input[i];
205 }
206 }
207 if (after) {
208 strncpy(out, after, afterLen);
209 out += afterLen;
210 }
211 // Validate that we haven't written outside of our string.
212 SkASSERT(out == &output.writable_str()[output.size()]);
213 *out = '\0';
halcanary78daeff2016-04-07 12:34:59 -0700214 return output;
halcanary34422612015-10-12 10:11:18 -0700215}
216
217SkPDFObject* SkPDFMetadata::createXMPObject(const UUID& doc,
218 const UUID& instance) const {
219 static const char templateString[] =
220 "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n"
221 "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n"
222 " x:xmptk=\"Adobe XMP Core 5.4-c005 78.147326, "
223 "2012/08/23-13:03:03\">\n"
224 "<rdf:RDF "
225 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n"
226 "<rdf:Description rdf:about=\"\"\n"
227 " xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n"
228 " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n"
229 " xmlns:xmpMM=\"http://ns.adobe.com/xap/1.0/mm/\"\n"
230 " xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\"\n"
231 " xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\">\n"
232 "<pdfaid:part>2</pdfaid:part>\n"
233 "<pdfaid:conformance>B</pdfaid:conformance>\n"
234 "%s" // ModifyDate
235 "%s" // CreateDate
halcanary34422612015-10-12 10:11:18 -0700236 "%s" // xmp:CreatorTool
237 "<dc:format>application/pdf</dc:format>\n"
238 "%s" // dc:title
239 "%s" // dc:description
240 "%s" // author
241 "%s" // keywords
242 "<xmpMM:DocumentID>uuid:%s</xmpMM:DocumentID>\n"
243 "<xmpMM:InstanceID>uuid:%s</xmpMM:InstanceID>\n"
halcanary8cd4a242016-04-07 08:56:15 -0700244 "<pdf:Producer>Skia/PDF m" SKPDF_STRING(SK_MILESTONE) "</pdf:Producer>\n"
halcanary34422612015-10-12 10:11:18 -0700245 "%s" // pdf:Keywords
246 "</rdf:Description>\n"
247 "</rdf:RDF>\n"
248 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding.
249 "<?xpacket end=\"w\"?>\n";
250
251 SkString creationDate;
252 SkString modificationDate;
halcanary34422612015-10-12 10:11:18 -0700253 if (fCreation) {
254 SkString tmp;
255 fCreation->toISO8601(&tmp);
256 SkASSERT(0 == count_xml_escape_size(tmp));
257 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape
halcanaryd51bdae2016-04-25 09:25:35 -0700258 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n",
259 tmp.c_str());
halcanary34422612015-10-12 10:11:18 -0700260 }
261 if (fModified) {
262 SkString tmp;
263 fModified->toISO8601(&tmp);
264 SkASSERT(0 == count_xml_escape_size(tmp));
halcanaryd51bdae2016-04-25 09:25:35 -0700265 modificationDate = SkStringPrintf(
halcanary34422612015-10-12 10:11:18 -0700266 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str());
halcanary34422612015-10-12 10:11:18 -0700267 }
halcanary78daeff2016-04-07 12:34:59 -0700268 SkString title = escape_xml(
269 get(fInfo, "Title"),
270 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">",
271 "</rdf:li></rdf:Alt></dc:title>\n");
272 SkString author = escape_xml(
273 get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>",
274 "</rdf:li></rdf:Bag></dc:creator>\n");
halcanary34422612015-10-12 10:11:18 -0700275 // TODO: in theory, XMP can support multiple authors. Split on a delimiter?
halcanary78daeff2016-04-07 12:34:59 -0700276 SkString subject = escape_xml(
277 get(fInfo, "Subject"),
278 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">",
279 "</rdf:li></rdf:Alt></dc:description>\n");
280 SkString keywords1 = escape_xml(
281 get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>",
282 "</rdf:li></rdf:Bag></dc:subject>\n");
283 SkString keywords2 = escape_xml(
284 get(fInfo, "Keywords"), "<pdf:Keywords>",
285 "</pdf:Keywords>\n");
halcanary34422612015-10-12 10:11:18 -0700286
287 // TODO: in theory, keywords can be a list too.
288 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>",
289 "</xmp:CreatorTool>\n");
290 SkString documentID = uuid_to_string(doc); // no need to escape
291 SkASSERT(0 == count_xml_escape_size(documentID));
292 SkString instanceID = uuid_to_string(instance);
293 SkASSERT(0 == count_xml_escape_size(instanceID));
halcanaryd51bdae2016-04-25 09:25:35 -0700294 return new PDFXMLObject(SkStringPrintf(
halcanary34422612015-10-12 10:11:18 -0700295 templateString, modificationDate.c_str(), creationDate.c_str(),
halcanary78daeff2016-04-07 12:34:59 -0700296 creator.c_str(), title.c_str(),
halcanary34422612015-10-12 10:11:18 -0700297 subject.c_str(), author.c_str(), keywords1.c_str(),
298 documentID.c_str(), instanceID.c_str(), keywords2.c_str()));
299}
300
halcanary8cd4a242016-04-07 08:56:15 -0700301#undef SKPDF_STRING
302#undef SKPDF_STRING_IMPL
303