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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 // <81b14aafa313db63dbd6f981e49f94f4> ] | 88 // <81b14aafa313db63dbd6f981e49f94f4> ] |
89 auto array = sk_make_sp<SkPDFArray>(); | 89 auto array = sk_make_sp<SkPDFArray>(); |
90 static_assert(sizeof(UUID) == 16, "uuid_size"); | 90 static_assert(sizeof(UUID) == 16, "uuid_size"); |
91 array->appendString( | 91 array->appendString( |
92 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); | 92 SkString(reinterpret_cast<const char*>(&doc), sizeof(UUID))); |
93 array->appendString( | 93 array->appendString( |
94 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); | 94 SkString(reinterpret_cast<const char*>(&instance), sizeof(UUID))); |
95 return array.release(); | 95 return array.release(); |
96 } | 96 } |
97 | 97 |
| 98 // Improvement on SkStringPrintf to allow for arbitrarily long output. |
| 99 // TODO: replace SkStringPrintf. |
| 100 static SkString sk_string_printf(const char* format, ...) { |
| 101 #ifdef SK_BUILD_FOR_WIN |
| 102 va_list args; |
| 103 va_start(args, format); |
| 104 char buffer[1024]; |
| 105 int length = _vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, args); |
| 106 va_end(args); |
| 107 if (length >= 0 && length < (int)sizeof(buffer)) { |
| 108 return SkString(buffer, length); |
| 109 } |
| 110 va_start(args, format); |
| 111 length = _vscprintf(format, args); |
| 112 va_end(args); |
| 113 |
| 114 SkString string((size_t)length); |
| 115 va_start(args, format); |
| 116 SkDEBUGCODE(int check = ) _vsnprintf_s(string.writable_str(), length + 1, |
| 117 _TRUNCATE, format, args); |
| 118 va_end(args); |
| 119 SkASSERT(check == length); |
| 120 SkASSERT(string[length] == '\0'); |
| 121 return string; |
| 122 #else // C99/C++11 standard vsnprintf |
| 123 // TODO: When all compilers support this, remove windows-specific code. |
| 124 va_list args; |
| 125 va_start(args, format); |
| 126 char buffer[1024]; |
| 127 int length = vsnprintf(buffer, sizeof(buffer), format, args); |
| 128 va_end(args); |
| 129 if (length < 0) { |
| 130 return SkString(); |
| 131 } |
| 132 if (length < (int)sizeof(buffer)) { |
| 133 return SkString(buffer, length); |
| 134 } |
| 135 SkString string((size_t)length); |
| 136 va_start(args, format); |
| 137 SkDEBUGCODE(int check = ) |
| 138 vsnprintf(string.writable_str(), length + 1, format, args); |
| 139 va_end(args); |
| 140 SkASSERT(check == length); |
| 141 SkASSERT(string[length] == '\0'); |
| 142 return string; |
| 143 #endif |
| 144 } |
| 145 |
98 static const SkString get(const SkTArray<SkDocument::Attribute>& info, | 146 static const SkString get(const SkTArray<SkDocument::Attribute>& info, |
99 const char* key) { | 147 const char* key) { |
100 for (const auto& keyValue : info) { | 148 for (const auto& keyValue : info) { |
101 if (keyValue.fKey.equals(key)) { | 149 if (keyValue.fKey.equals(key)) { |
102 return keyValue.fValue; | 150 return keyValue.fValue; |
103 } | 151 } |
104 } | 152 } |
105 return SkString(); | 153 return SkString(); |
106 } | 154 } |
107 | 155 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. | 296 "</x:xmpmeta>\n" // Note: the standard suggests 4k of padding. |
249 "<?xpacket end=\"w\"?>\n"; | 297 "<?xpacket end=\"w\"?>\n"; |
250 | 298 |
251 SkString creationDate; | 299 SkString creationDate; |
252 SkString modificationDate; | 300 SkString modificationDate; |
253 if (fCreation) { | 301 if (fCreation) { |
254 SkString tmp; | 302 SkString tmp; |
255 fCreation->toISO8601(&tmp); | 303 fCreation->toISO8601(&tmp); |
256 SkASSERT(0 == count_xml_escape_size(tmp)); | 304 SkASSERT(0 == count_xml_escape_size(tmp)); |
257 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape | 305 // YYYY-mm-ddTHH:MM:SS[+|-]ZZ:ZZ; no need to escape |
258 creationDate = SkStringPrintf("<xmp:CreateDate>%s</xmp:CreateDate>\n", | 306 creationDate = sk_string_printf("<xmp:CreateDate>%s</xmp:CreateDate>\n", |
259 tmp.c_str()); | 307 tmp.c_str()); |
260 } | 308 } |
261 if (fModified) { | 309 if (fModified) { |
262 SkString tmp; | 310 SkString tmp; |
263 fModified->toISO8601(&tmp); | 311 fModified->toISO8601(&tmp); |
264 SkASSERT(0 == count_xml_escape_size(tmp)); | 312 SkASSERT(0 == count_xml_escape_size(tmp)); |
265 modificationDate = SkStringPrintf( | 313 modificationDate = sk_string_printf( |
266 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); | 314 "<xmp:ModifyDate>%s</xmp:ModifyDate>\n", tmp.c_str()); |
267 } | 315 } |
268 SkString title = escape_xml( | 316 SkString title = escape_xml( |
269 get(fInfo, "Title"), | 317 get(fInfo, "Title"), |
270 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", | 318 "<dc:title><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
271 "</rdf:li></rdf:Alt></dc:title>\n"); | 319 "</rdf:li></rdf:Alt></dc:title>\n"); |
272 SkString author = escape_xml( | 320 SkString author = escape_xml( |
273 get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", | 321 get(fInfo, "Author"), "<dc:creator><rdf:Bag><rdf:li>", |
274 "</rdf:li></rdf:Bag></dc:creator>\n"); | 322 "</rdf:li></rdf:Bag></dc:creator>\n"); |
275 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? | 323 // TODO: in theory, XMP can support multiple authors. Split on a delimiter? |
276 SkString subject = escape_xml( | 324 SkString subject = escape_xml( |
277 get(fInfo, "Subject"), | 325 get(fInfo, "Subject"), |
278 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", | 326 "<dc:description><rdf:Alt><rdf:li xml:lang=\"x-default\">", |
279 "</rdf:li></rdf:Alt></dc:description>\n"); | 327 "</rdf:li></rdf:Alt></dc:description>\n"); |
280 SkString keywords1 = escape_xml( | 328 SkString keywords1 = escape_xml( |
281 get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", | 329 get(fInfo, "Keywords"), "<dc:subject><rdf:Bag><rdf:li>", |
282 "</rdf:li></rdf:Bag></dc:subject>\n"); | 330 "</rdf:li></rdf:Bag></dc:subject>\n"); |
283 SkString keywords2 = escape_xml( | 331 SkString keywords2 = escape_xml( |
284 get(fInfo, "Keywords"), "<pdf:Keywords>", | 332 get(fInfo, "Keywords"), "<pdf:Keywords>", |
285 "</pdf:Keywords>\n"); | 333 "</pdf:Keywords>\n"); |
286 | 334 |
287 // TODO: in theory, keywords can be a list too. | 335 // TODO: in theory, keywords can be a list too. |
288 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", | 336 SkString creator = escape_xml(get(fInfo, "Creator"), "<xmp:CreatorTool>", |
289 "</xmp:CreatorTool>\n"); | 337 "</xmp:CreatorTool>\n"); |
290 SkString documentID = uuid_to_string(doc); // no need to escape | 338 SkString documentID = uuid_to_string(doc); // no need to escape |
291 SkASSERT(0 == count_xml_escape_size(documentID)); | 339 SkASSERT(0 == count_xml_escape_size(documentID)); |
292 SkString instanceID = uuid_to_string(instance); | 340 SkString instanceID = uuid_to_string(instance); |
293 SkASSERT(0 == count_xml_escape_size(instanceID)); | 341 SkASSERT(0 == count_xml_escape_size(instanceID)); |
294 return new PDFXMLObject(SkStringPrintf( | 342 return new PDFXMLObject(sk_string_printf( |
295 templateString, modificationDate.c_str(), creationDate.c_str(), | 343 templateString, modificationDate.c_str(), creationDate.c_str(), |
296 creator.c_str(), title.c_str(), | 344 creator.c_str(), title.c_str(), |
297 subject.c_str(), author.c_str(), keywords1.c_str(), | 345 subject.c_str(), author.c_str(), keywords1.c_str(), |
298 documentID.c_str(), instanceID.c_str(), keywords2.c_str())); | 346 documentID.c_str(), instanceID.c_str(), keywords2.c_str())); |
299 } | 347 } |
300 | 348 |
301 #undef SKPDF_STRING | 349 #undef SKPDF_STRING |
302 #undef SKPDF_STRING_IMPL | 350 #undef SKPDF_STRING_IMPL |
303 | 351 |
OLD | NEW |