epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 9 | #include "SkPDFTypes.h" |
| 10 | #include "SkStream.h" |
| 11 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 12 | #ifdef SK_BUILD_FOR_WIN |
| 13 | #define SNPRINTF _snprintf |
| 14 | #else |
| 15 | #define SNPRINTF snprintf |
| 16 | #endif |
| 17 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 18 | //////////////////////////////////////////////////////////////////////////////// |
| 19 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 20 | SkString* pun(char* x) { return reinterpret_cast<SkString*>(x); } |
| 21 | const SkString* pun(const char* x) { |
| 22 | return reinterpret_cast<const SkString*>(x); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 23 | } |
| 24 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 25 | SkPDFUnion::SkPDFUnion(Type t) : fType(t) {} |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 26 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 27 | SkPDFUnion::~SkPDFUnion() { |
| 28 | switch (fType) { |
| 29 | case Type::kNameSkS: |
| 30 | case Type::kStringSkS: |
| 31 | pun(fSkString)->~SkString(); |
| 32 | return; |
| 33 | case Type::kObjRef: |
| 34 | case Type::kObject: |
| 35 | SkSafeUnref(fObject); |
| 36 | return; |
| 37 | default: |
| 38 | return; |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 39 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 40 | } |
| 41 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 42 | SkPDFUnion& SkPDFUnion::operator=(SkPDFUnion&& other) { |
| 43 | if (this != &other) { |
| 44 | this->~SkPDFUnion(); |
| 45 | SkNEW_PLACEMENT_ARGS(this, SkPDFUnion, (other.move())); |
| 46 | } |
| 47 | return *this; |
| 48 | } |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 49 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 50 | SkPDFUnion::SkPDFUnion(SkPDFUnion&& other) { |
| 51 | SkASSERT(this != &other); |
| 52 | memcpy(this, &other, sizeof(*this)); |
| 53 | other.fType = Type::kDestroyed; |
| 54 | } |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 55 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 56 | #if 0 |
| 57 | SkPDFUnion SkPDFUnion::copy() const { |
| 58 | SkPDFUnion u(fType); |
| 59 | memcpy(&u, this, sizeof(u)); |
| 60 | switch (fType) { |
| 61 | case Type::kNameSkS: |
| 62 | case Type::kStringSkS: |
| 63 | SkNEW_PLACEMENT_ARGS(pun(u.fSkString), SkString, |
| 64 | (*pun(fSkString))); |
| 65 | return u.move(); |
| 66 | case Type::kObjRef: |
| 67 | case Type::kObject: |
| 68 | SkRef(u.fObject); |
| 69 | return u.move(); |
| 70 | default: |
| 71 | return u.move(); |
| 72 | } |
| 73 | } |
| 74 | SkPDFUnion& SkPDFUnion::operator=(const SkPDFUnion& other) { |
| 75 | return *this = other.copy(); |
| 76 | } |
| 77 | SkPDFUnion::SkPDFUnion(const SkPDFUnion& other) { |
| 78 | *this = other.copy(); |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | bool SkPDFUnion::isName() const { |
| 83 | return Type::kName == fType || Type::kNameSkS == fType; |
| 84 | } |
| 85 | |
| 86 | #ifdef SK_DEBUG |
| 87 | // Most names need no escaping. Such names are handled as static |
| 88 | // const strings. |
| 89 | bool is_valid_name(const char* n) { |
| 90 | static const char kControlChars[] = "/%()<>[]{}"; |
| 91 | while (*n) { |
| 92 | if (*n < '!' || *n > '~' || strchr(kControlChars, *n)) { |
| 93 | return false; |
| 94 | } |
| 95 | ++n; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | #endif // SK_DEBUG |
| 100 | |
| 101 | // Given an arbitrary string, convert it to a valid name. |
| 102 | static SkString escape_name(const char* name, size_t len) { |
| 103 | static const char kToEscape[] = "#/%()<>[]{}"; |
| 104 | int count = 0; |
| 105 | const char* const end = &name[len]; |
| 106 | for (const char* n = name; n != end; ++n) { |
| 107 | if (*n < '!' || *n > '~' || strchr(kToEscape, *n)) { |
| 108 | count += 2; |
| 109 | } |
| 110 | ++count; |
| 111 | } |
| 112 | SkString result(count); |
| 113 | char* s = result.writable_str(); |
| 114 | static const char kHex[] = "0123456789ABCDEF"; |
| 115 | for (const char* n = name; n != end; ++n) { |
| 116 | if (*n < '!' || *n > '~' || strchr(kToEscape, *n)) { |
| 117 | *s++ = '#'; |
| 118 | *s++ = kHex[(*n >> 4) & 0xF]; |
| 119 | *s++ = kHex[*n & 0xF]; |
| 120 | } else { |
| 121 | *s++ = *n; |
| 122 | } |
| 123 | } |
| 124 | SkASSERT(&result.writable_str()[count] == s); // don't over-write |
| 125 | return result; // allocated space |
| 126 | } |
| 127 | |
| 128 | static SkString escape_name(const SkString& name) { |
| 129 | return escape_name(name.c_str(), name.size()); |
| 130 | } |
| 131 | |
| 132 | static void write_string(SkWStream* o, const SkString& s) { |
| 133 | o->write(s.c_str(), s.size()); |
| 134 | } |
| 135 | |
| 136 | static SkString format_string(const SkString& s) { |
| 137 | return SkPDFString::FormatString(s.c_str(), s.size()); |
| 138 | } |
| 139 | |
| 140 | static SkString format_string(const char* s) { |
| 141 | return SkPDFString::FormatString(s, strlen(s)); |
| 142 | } |
| 143 | |
| 144 | void SkPDFUnion::emitObject(SkWStream* stream, |
| 145 | const SkPDFObjNumMap& objNumMap, |
| 146 | const SkPDFSubstituteMap& substitutes) const { |
| 147 | switch (fType) { |
| 148 | case Type::kInt: |
| 149 | stream->writeDecAsText(fIntValue); |
| 150 | return; |
| 151 | case Type::kBool: |
| 152 | stream->writeText(fBoolValue ? "true" : "false"); |
| 153 | return; |
| 154 | case Type::kScalar: |
| 155 | SkPDFScalar::Append(fScalarValue, stream); |
| 156 | return; |
| 157 | case Type::kName: |
| 158 | stream->writeText("/"); |
| 159 | SkASSERT(is_valid_name(fStaticString)); |
| 160 | stream->writeText(fStaticString); |
| 161 | return; |
| 162 | case Type::kString: |
| 163 | SkASSERT(fStaticString); |
| 164 | write_string(stream, format_string(fStaticString)); |
| 165 | return; |
| 166 | case Type::kNameSkS: |
| 167 | stream->writeText("/"); |
| 168 | write_string(stream, escape_name(*pun(fSkString))); |
| 169 | return; |
| 170 | case Type::kStringSkS: |
| 171 | write_string(stream, format_string(*pun(fSkString))); |
| 172 | return; |
| 173 | case Type::kObjRef: |
| 174 | stream->writeDecAsText(objNumMap.getObjectNumber( |
| 175 | substitutes.getSubstitute(fObject))); |
| 176 | stream->writeText(" 0 R"); // Generation number is always 0. |
| 177 | return; |
| 178 | case Type::kObject: |
| 179 | fObject->emitObject(stream, objNumMap, substitutes); |
| 180 | return; |
| 181 | default: |
| 182 | SkDEBUGFAIL("SkPDFUnion::emitObject with bad type"); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void SkPDFUnion::addResources(SkPDFObjNumMap* objNumMap, |
| 187 | const SkPDFSubstituteMap& substituteMap) const { |
| 188 | switch (fType) { |
| 189 | case Type::kInt: |
| 190 | case Type::kBool: |
| 191 | case Type::kScalar: |
| 192 | case Type::kName: |
| 193 | case Type::kString: |
| 194 | case Type::kNameSkS: |
| 195 | case Type::kStringSkS: |
| 196 | return; // These have no resources. |
| 197 | case Type::kObjRef: { |
| 198 | SkPDFObject* obj = substituteMap.getSubstitute(fObject); |
| 199 | if (objNumMap->addObject(obj)) { |
| 200 | obj->addResources(objNumMap, substituteMap); |
| 201 | } |
| 202 | return; |
| 203 | } |
| 204 | case Type::kObject: |
| 205 | fObject->addResources(objNumMap, substituteMap); |
| 206 | return; |
| 207 | default: |
| 208 | SkDEBUGFAIL("SkPDFUnion::addResources with bad type"); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | SkPDFUnion SkPDFUnion::Int(int32_t value) { |
| 213 | SkPDFUnion u(Type::kInt); |
| 214 | u.fIntValue = value; |
| 215 | return u.move(); |
| 216 | } |
| 217 | |
| 218 | SkPDFUnion SkPDFUnion::Bool(bool value) { |
| 219 | SkPDFUnion u(Type::kBool); |
| 220 | u.fBoolValue = value; |
| 221 | return u.move(); |
| 222 | } |
| 223 | |
| 224 | SkPDFUnion SkPDFUnion::Scalar(SkScalar value) { |
| 225 | SkPDFUnion u(Type::kScalar); |
| 226 | u.fScalarValue = value; |
| 227 | return u.move(); |
| 228 | } |
| 229 | |
| 230 | SkPDFUnion SkPDFUnion::Name(const char* value) { |
| 231 | SkPDFUnion u(Type::kName); |
| 232 | SkASSERT(value); |
| 233 | SkASSERT(is_valid_name(value)); |
| 234 | u.fStaticString = value; |
| 235 | return u.move(); |
| 236 | } |
| 237 | |
| 238 | SkPDFUnion SkPDFUnion::String(const char* value) { |
| 239 | SkPDFUnion u(Type::kString); |
| 240 | SkASSERT(value); |
| 241 | u.fStaticString = value; |
| 242 | return u.move(); |
| 243 | } |
| 244 | |
| 245 | SkPDFUnion SkPDFUnion::Name(const SkString& s) { |
| 246 | SkPDFUnion u(Type::kNameSkS); |
| 247 | SkNEW_PLACEMENT_ARGS(pun(u.fSkString), SkString, (s)); |
| 248 | return u.move(); |
| 249 | } |
| 250 | |
| 251 | SkPDFUnion SkPDFUnion::String(const SkString& s) { |
| 252 | SkPDFUnion u(Type::kStringSkS); |
| 253 | SkNEW_PLACEMENT_ARGS(pun(u.fSkString), SkString, (s)); |
| 254 | return u.move(); |
| 255 | } |
| 256 | |
| 257 | SkPDFUnion SkPDFUnion::ObjRef(SkPDFObject* ptr) { |
| 258 | SkPDFUnion u(Type::kObjRef); |
| 259 | SkASSERT(ptr); |
| 260 | u.fObject = ptr; |
| 261 | return u.move(); |
| 262 | } |
| 263 | |
| 264 | SkPDFUnion SkPDFUnion::Object(SkPDFObject* ptr) { |
| 265 | SkPDFUnion u(Type::kObject); |
| 266 | SkASSERT(ptr); |
| 267 | u.fObject = ptr; |
| 268 | return u.move(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 269 | } |
| 270 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 271 | //////////////////////////////////////////////////////////////////////////////// |
| 272 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 273 | void SkPDFAtom::emitObject(SkWStream* stream, |
| 274 | const SkPDFObjNumMap& objNumMap, |
| 275 | const SkPDFSubstituteMap& substitutes) { |
| 276 | fValue.emitObject(stream, objNumMap, substitutes); |
| 277 | } |
| 278 | void SkPDFAtom::addResources(SkPDFObjNumMap* map, |
| 279 | const SkPDFSubstituteMap& substitutes) const { |
| 280 | fValue.addResources(map, substitutes); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 281 | } |
| 282 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 283 | //////////////////////////////////////////////////////////////////////////////// |
| 284 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 285 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 286 | void SkPDFScalar::Append(SkScalar value, SkWStream* stream) { |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 287 | // The range of reals in PDF/A is the same as SkFixed: +/- 32,767 and |
| 288 | // +/- 1/65,536 (though integers can range from 2^31 - 1 to -2^31). |
| 289 | // When using floats that are outside the whole value range, we can use |
| 290 | // integers instead. |
| 291 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 292 | #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) |
| 293 | if (value > 32767 || value < -32767) { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 294 | stream->writeDecAsText(SkScalarRoundToInt(value)); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 295 | return; |
| 296 | } |
| 297 | |
| 298 | char buffer[SkStrAppendScalar_MaxSize]; |
| 299 | char* end = SkStrAppendFixed(buffer, SkScalarToFixed(value)); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 300 | stream->write(buffer, end - buffer); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 301 | return; |
| 302 | #endif // !SK_ALLOW_LARGE_PDF_SCALARS |
| 303 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 304 | #if defined(SK_ALLOW_LARGE_PDF_SCALARS) |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 305 | // Floats have 24bits of significance, so anything outside that range is |
| 306 | // no more precise than an int. (Plus PDF doesn't support scientific |
| 307 | // notation, so this clamps to SK_Max/MinS32). |
| 308 | if (value > (1 << 24) || value < -(1 << 24)) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 309 | stream->writeDecAsText(value); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 310 | return; |
| 311 | } |
| 312 | // Continue to enforce the PDF limits for small floats. |
| 313 | if (value < 1.0f/65536 && value > -1.0f/65536) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 314 | stream->writeDecAsText(0); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | // SkStrAppendFloat might still use scientific notation, so use snprintf |
| 318 | // directly.. |
| 319 | static const int kFloat_MaxSize = 19; |
| 320 | char buffer[kFloat_MaxSize]; |
| 321 | int len = SNPRINTF(buffer, kFloat_MaxSize, "%#.8f", value); |
| 322 | // %f always prints trailing 0s, so strip them. |
| 323 | for (; buffer[len - 1] == '0' && len > 0; len--) { |
| 324 | buffer[len - 1] = '\0'; |
| 325 | } |
| 326 | if (buffer[len - 1] == '.') { |
| 327 | buffer[len - 1] = '\0'; |
| 328 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 329 | stream->writeText(buffer); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 330 | return; |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 331 | #endif // SK_ALLOW_LARGE_PDF_SCALARS |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 332 | } |
| 333 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 334 | //////////////////////////////////////////////////////////////////////////////// |
| 335 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 336 | // static |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 337 | |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 338 | SkString SkPDFString::FormatString(const char* cin, size_t len) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 339 | SkASSERT(len <= kMaxLen); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 340 | |
| 341 | // 7-bit clean is a heuristic to decide what string format to use; |
| 342 | // a 7-bit clean string should require little escaping. |
| 343 | bool sevenBitClean = true; |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 344 | size_t characterCount = 2 + len; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 345 | for (size_t i = 0; i < len; i++) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 346 | if (cin[i] > '~' || cin[i] < ' ') { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 347 | sevenBitClean = false; |
| 348 | break; |
| 349 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 350 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
| 351 | ++characterCount; |
| 352 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 353 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 354 | SkString result; |
| 355 | if (sevenBitClean) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 356 | result.resize(characterCount); |
| 357 | char* str = result.writable_str(); |
| 358 | *str++ = '('; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 359 | for (size_t i = 0; i < len; i++) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 360 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
| 361 | *str++ = '\\'; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 362 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 363 | *str++ = cin[i]; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 364 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 365 | *str++ = ')'; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 366 | } else { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 367 | result.resize(2 * len + 2); |
| 368 | char* str = result.writable_str(); |
| 369 | *str++ = '<'; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 370 | for (size_t i = 0; i < len; i++) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 371 | uint8_t c = static_cast<uint8_t>(cin[i]); |
| 372 | static const char gHex[] = "0123456789ABCDEF"; |
| 373 | *str++ = gHex[(c >> 4) & 0xF]; |
| 374 | *str++ = gHex[(c ) & 0xF]; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 375 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 376 | *str++ = '>'; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 377 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 378 | return result; |
| 379 | } |
| 380 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 381 | //////////////////////////////////////////////////////////////////////////////// |
| 382 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 383 | SkPDFArray::SkPDFArray() {} |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 384 | SkPDFArray::~SkPDFArray() { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 385 | for (SkPDFUnion& value : fValues) { |
| 386 | value.~SkPDFUnion(); |
| 387 | } |
| 388 | fValues.reset(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 389 | } |
| 390 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 391 | int SkPDFArray::size() const { return fValues.count(); } |
| 392 | |
| 393 | void SkPDFArray::reserve(int length) { fValues.setReserve(length); } |
| 394 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 395 | void SkPDFArray::emitObject(SkWStream* stream, |
| 396 | const SkPDFObjNumMap& objNumMap, |
| 397 | const SkPDFSubstituteMap& substitutes) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 398 | stream->writeText("["); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 399 | for (int i = 0; i < fValues.count(); i++) { |
| 400 | fValues[i].emitObject(stream, objNumMap, substitutes); |
| 401 | if (i + 1 < fValues.count()) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 402 | stream->writeText(" "); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 403 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 404 | } |
| 405 | stream->writeText("]"); |
| 406 | } |
| 407 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 408 | void SkPDFArray::addResources(SkPDFObjNumMap* catalog, |
| 409 | const SkPDFSubstituteMap& substitutes) const { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 410 | for (const SkPDFUnion& value : fValues) { |
| 411 | value.addResources(catalog, substitutes); |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 415 | void SkPDFArray::append(SkPDFUnion&& value) { |
| 416 | SkNEW_PLACEMENT_ARGS(fValues.append(), SkPDFUnion, (value.move())); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 417 | } |
| 418 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 419 | SkPDFObject* SkPDFArray::append(SkPDFObject* value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 420 | // DEPRECATED |
| 421 | this->append(SkPDFUnion::Object(SkRef(value))); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 422 | return value; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 423 | } |
| 424 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 425 | void SkPDFArray::appendInt(int32_t value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 426 | this->append(SkPDFUnion::Int(value)); |
| 427 | } |
| 428 | |
| 429 | void SkPDFArray::appendBool(bool value) { |
| 430 | this->append(SkPDFUnion::Bool(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void SkPDFArray::appendScalar(SkScalar value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 434 | this->append(SkPDFUnion::Scalar(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void SkPDFArray::appendName(const char name[]) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 438 | this->append(SkPDFUnion::Name(SkString(name))); |
| 439 | } |
| 440 | |
| 441 | void SkPDFArray::appendName(const SkString& name) { |
| 442 | this->append(SkPDFUnion::Name(name)); |
| 443 | } |
| 444 | |
| 445 | void SkPDFArray::appendString(const SkString& value) { |
| 446 | this->append(SkPDFUnion::String(value)); |
| 447 | } |
| 448 | |
| 449 | void SkPDFArray::appendString(const char value[]) { |
| 450 | this->append(SkPDFUnion::String(value)); |
| 451 | } |
| 452 | |
| 453 | void SkPDFArray::appendObject(SkPDFObject* value) { |
| 454 | this->append(SkPDFUnion::Object(value)); |
| 455 | } |
| 456 | |
| 457 | void SkPDFArray::appendObjRef(SkPDFObject* value) { |
| 458 | this->append(SkPDFUnion::ObjRef(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /////////////////////////////////////////////////////////////////////////////// |
| 462 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 463 | SkPDFDict::SkPDFDict() {} |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 464 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 465 | SkPDFDict::~SkPDFDict() { this->clear(); } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 466 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 467 | SkPDFDict::SkPDFDict(const char type[]) { this->insertName("Type", type); } |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 468 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 469 | void SkPDFDict::emitObject(SkWStream* stream, |
| 470 | const SkPDFObjNumMap& objNumMap, |
| 471 | const SkPDFSubstituteMap& substitutes) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 472 | stream->writeText("<<"); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 473 | for (int i = 0; i < fRecords.count(); i++) { |
| 474 | fRecords[i].fKey.emitObject(stream, objNumMap, substitutes); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 475 | stream->writeText(" "); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 476 | fRecords[i].fValue.emitObject(stream, objNumMap, substitutes); |
| 477 | if (i + 1 < fRecords.count()) { |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 478 | stream->writeText("\n"); |
| 479 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 480 | } |
| 481 | stream->writeText(">>"); |
| 482 | } |
| 483 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 484 | void SkPDFDict::addResources(SkPDFObjNumMap* catalog, |
| 485 | const SkPDFSubstituteMap& substitutes) const { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 486 | for (int i = 0; i < fRecords.count(); i++) { |
| 487 | fRecords[i].fKey.addResources(catalog, substitutes); |
| 488 | fRecords[i].fValue.addResources(catalog, substitutes); |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 492 | void SkPDFDict::set(SkPDFUnion&& name, SkPDFUnion&& value) { |
| 493 | Record* rec = fRecords.append(); |
| 494 | SkASSERT(name.isName()); |
| 495 | SkNEW_PLACEMENT_ARGS(&rec->fKey, SkPDFUnion, (name.move())); |
| 496 | SkNEW_PLACEMENT_ARGS(&rec->fValue, SkPDFUnion, (value.move())); |
| 497 | } |
| 498 | |
| 499 | int SkPDFDict::size() const { return fRecords.count(); } |
| 500 | |
| 501 | void SkPDFDict::insertObjRef(const char key[], SkPDFObject* value) { |
| 502 | this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(value)); |
| 503 | } |
| 504 | void SkPDFDict::insertObjRef(const SkString& key, SkPDFObject* value) { |
| 505 | this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(value)); |
| 506 | } |
| 507 | |
| 508 | void SkPDFDict::insertObject(const char key[], SkPDFObject* value) { |
| 509 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(value)); |
| 510 | } |
| 511 | void SkPDFDict::insertObject(const SkString& key, SkPDFObject* value) { |
| 512 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(value)); |
| 513 | } |
| 514 | |
| 515 | // DEPRECATED |
| 516 | SkPDFObject* SkPDFDict::insert(const char key[], SkPDFObject* value) { |
| 517 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(SkRef(value))); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 518 | return value; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 519 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 520 | |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame^] | 521 | void SkPDFDict::insertBool(const char key[], bool value) { |
| 522 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Bool(value)); |
| 523 | } |
| 524 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 525 | void SkPDFDict::insertInt(const char key[], int32_t value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 526 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Int(value)); |
| 527 | } |
| 528 | |
| 529 | void SkPDFDict::insertInt(const char key[], size_t value) { |
| 530 | this->insertInt(key, SkToS32(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void SkPDFDict::insertScalar(const char key[], SkScalar value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 534 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Scalar(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | void SkPDFDict::insertName(const char key[], const char name[]) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 538 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); |
| 539 | } |
| 540 | |
| 541 | void SkPDFDict::insertName(const char key[], const SkString& name) { |
| 542 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); |
| 543 | } |
| 544 | |
| 545 | void SkPDFDict::insertString(const char key[], const char value[]) { |
| 546 | this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); |
| 547 | } |
| 548 | |
| 549 | void SkPDFDict::insertString(const char key[], const SkString& value) { |
| 550 | this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 551 | } |
| 552 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 553 | void SkPDFDict::clear() { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 554 | for (Record& rec : fRecords) { |
| 555 | rec.fKey.~SkPDFUnion(); |
| 556 | rec.fValue.~SkPDFUnion(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 557 | } |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 558 | fRecords.reset(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 559 | } |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 560 | |
| 561 | //////////////////////////////////////////////////////////////////////////////// |
| 562 | |
| 563 | SkPDFSubstituteMap::~SkPDFSubstituteMap() { |
| 564 | fSubstituteMap.foreach( |
| 565 | [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); |
| 566 | } |
| 567 | |
| 568 | void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, |
| 569 | SkPDFObject* substitute) { |
| 570 | SkASSERT(original != substitute); |
| 571 | SkASSERT(!fSubstituteMap.find(original)); |
| 572 | fSubstituteMap.set(original, SkRef(substitute)); |
| 573 | } |
| 574 | |
| 575 | SkPDFObject* SkPDFSubstituteMap::getSubstitute(SkPDFObject* object) const { |
| 576 | SkPDFObject** found = fSubstituteMap.find(object); |
| 577 | return found ? *found : object; |
| 578 | } |
| 579 | |
| 580 | //////////////////////////////////////////////////////////////////////////////// |
| 581 | |
| 582 | bool SkPDFObjNumMap::addObject(SkPDFObject* obj) { |
| 583 | if (fObjectNumbers.find(obj)) { |
| 584 | return false; |
| 585 | } |
| 586 | fObjectNumbers.set(obj, fObjectNumbers.count() + 1); |
| 587 | fObjects.push(obj); |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const { |
| 592 | int32_t* objectNumberFound = fObjectNumbers.find(obj); |
| 593 | SkASSERT(objectNumberFound); |
| 594 | return *objectNumberFound; |
| 595 | } |
| 596 | |