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 | 002653e | 2015-05-05 11:28:55 -0700 | [diff] [blame^] | 273 | #if 0 // Enable if needed. |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 274 | void SkPDFAtom::emitObject(SkWStream* stream, |
| 275 | const SkPDFObjNumMap& objNumMap, |
| 276 | const SkPDFSubstituteMap& substitutes) { |
| 277 | fValue.emitObject(stream, objNumMap, substitutes); |
| 278 | } |
| 279 | void SkPDFAtom::addResources(SkPDFObjNumMap* map, |
| 280 | const SkPDFSubstituteMap& substitutes) const { |
| 281 | fValue.addResources(map, substitutes); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 282 | } |
halcanary | 002653e | 2015-05-05 11:28:55 -0700 | [diff] [blame^] | 283 | #endif // 0 |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 284 | //////////////////////////////////////////////////////////////////////////////// |
| 285 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 286 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 287 | void SkPDFScalar::Append(SkScalar value, SkWStream* stream) { |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 288 | // The range of reals in PDF/A is the same as SkFixed: +/- 32,767 and |
| 289 | // +/- 1/65,536 (though integers can range from 2^31 - 1 to -2^31). |
| 290 | // When using floats that are outside the whole value range, we can use |
| 291 | // integers instead. |
| 292 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 293 | #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) |
| 294 | if (value > 32767 || value < -32767) { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 295 | stream->writeDecAsText(SkScalarRoundToInt(value)); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | |
| 299 | char buffer[SkStrAppendScalar_MaxSize]; |
| 300 | char* end = SkStrAppendFixed(buffer, SkScalarToFixed(value)); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 301 | stream->write(buffer, end - buffer); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 302 | return; |
| 303 | #endif // !SK_ALLOW_LARGE_PDF_SCALARS |
| 304 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 305 | #if defined(SK_ALLOW_LARGE_PDF_SCALARS) |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 306 | // Floats have 24bits of significance, so anything outside that range is |
| 307 | // no more precise than an int. (Plus PDF doesn't support scientific |
| 308 | // notation, so this clamps to SK_Max/MinS32). |
| 309 | if (value > (1 << 24) || value < -(1 << 24)) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 310 | stream->writeDecAsText(value); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | // Continue to enforce the PDF limits for small floats. |
| 314 | if (value < 1.0f/65536 && value > -1.0f/65536) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 315 | stream->writeDecAsText(0); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | // SkStrAppendFloat might still use scientific notation, so use snprintf |
| 319 | // directly.. |
| 320 | static const int kFloat_MaxSize = 19; |
| 321 | char buffer[kFloat_MaxSize]; |
| 322 | int len = SNPRINTF(buffer, kFloat_MaxSize, "%#.8f", value); |
| 323 | // %f always prints trailing 0s, so strip them. |
| 324 | for (; buffer[len - 1] == '0' && len > 0; len--) { |
| 325 | buffer[len - 1] = '\0'; |
| 326 | } |
| 327 | if (buffer[len - 1] == '.') { |
| 328 | buffer[len - 1] = '\0'; |
| 329 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 330 | stream->writeText(buffer); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 331 | return; |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 332 | #endif // SK_ALLOW_LARGE_PDF_SCALARS |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 333 | } |
| 334 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 335 | //////////////////////////////////////////////////////////////////////////////// |
| 336 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 337 | // static |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 338 | |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 339 | SkString SkPDFString::FormatString(const char* cin, size_t len) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 340 | SkASSERT(len <= kMaxLen); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 341 | |
| 342 | // 7-bit clean is a heuristic to decide what string format to use; |
| 343 | // a 7-bit clean string should require little escaping. |
| 344 | bool sevenBitClean = true; |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 345 | size_t characterCount = 2 + len; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 346 | for (size_t i = 0; i < len; i++) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 347 | if (cin[i] > '~' || cin[i] < ' ') { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 348 | sevenBitClean = false; |
| 349 | break; |
| 350 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 351 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
| 352 | ++characterCount; |
| 353 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 354 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 355 | SkString result; |
| 356 | if (sevenBitClean) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 357 | result.resize(characterCount); |
| 358 | char* str = result.writable_str(); |
| 359 | *str++ = '('; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 360 | for (size_t i = 0; i < len; i++) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 361 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
| 362 | *str++ = '\\'; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 363 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 364 | *str++ = cin[i]; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 365 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 366 | *str++ = ')'; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 367 | } else { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 368 | result.resize(2 * len + 2); |
| 369 | char* str = result.writable_str(); |
| 370 | *str++ = '<'; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 371 | for (size_t i = 0; i < len; i++) { |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 372 | uint8_t c = static_cast<uint8_t>(cin[i]); |
| 373 | static const char gHex[] = "0123456789ABCDEF"; |
| 374 | *str++ = gHex[(c >> 4) & 0xF]; |
| 375 | *str++ = gHex[(c ) & 0xF]; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 376 | } |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 377 | *str++ = '>'; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 378 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 379 | return result; |
| 380 | } |
| 381 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 382 | //////////////////////////////////////////////////////////////////////////////// |
| 383 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 384 | SkPDFArray::SkPDFArray() {} |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 385 | SkPDFArray::~SkPDFArray() { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 386 | for (SkPDFUnion& value : fValues) { |
| 387 | value.~SkPDFUnion(); |
| 388 | } |
| 389 | fValues.reset(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 390 | } |
| 391 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 392 | int SkPDFArray::size() const { return fValues.count(); } |
| 393 | |
| 394 | void SkPDFArray::reserve(int length) { fValues.setReserve(length); } |
| 395 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 396 | void SkPDFArray::emitObject(SkWStream* stream, |
| 397 | const SkPDFObjNumMap& objNumMap, |
| 398 | const SkPDFSubstituteMap& substitutes) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 399 | stream->writeText("["); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 400 | for (int i = 0; i < fValues.count(); i++) { |
| 401 | fValues[i].emitObject(stream, objNumMap, substitutes); |
| 402 | if (i + 1 < fValues.count()) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 403 | stream->writeText(" "); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 404 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 405 | } |
| 406 | stream->writeText("]"); |
| 407 | } |
| 408 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 409 | void SkPDFArray::addResources(SkPDFObjNumMap* catalog, |
| 410 | const SkPDFSubstituteMap& substitutes) const { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 411 | for (const SkPDFUnion& value : fValues) { |
| 412 | value.addResources(catalog, substitutes); |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 416 | void SkPDFArray::append(SkPDFUnion&& value) { |
| 417 | SkNEW_PLACEMENT_ARGS(fValues.append(), SkPDFUnion, (value.move())); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 418 | } |
| 419 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 420 | void SkPDFArray::appendInt(int32_t value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 421 | this->append(SkPDFUnion::Int(value)); |
| 422 | } |
| 423 | |
| 424 | void SkPDFArray::appendBool(bool value) { |
| 425 | this->append(SkPDFUnion::Bool(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void SkPDFArray::appendScalar(SkScalar value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 429 | this->append(SkPDFUnion::Scalar(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void SkPDFArray::appendName(const char name[]) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 433 | this->append(SkPDFUnion::Name(SkString(name))); |
| 434 | } |
| 435 | |
| 436 | void SkPDFArray::appendName(const SkString& name) { |
| 437 | this->append(SkPDFUnion::Name(name)); |
| 438 | } |
| 439 | |
| 440 | void SkPDFArray::appendString(const SkString& value) { |
| 441 | this->append(SkPDFUnion::String(value)); |
| 442 | } |
| 443 | |
| 444 | void SkPDFArray::appendString(const char value[]) { |
| 445 | this->append(SkPDFUnion::String(value)); |
| 446 | } |
| 447 | |
| 448 | void SkPDFArray::appendObject(SkPDFObject* value) { |
| 449 | this->append(SkPDFUnion::Object(value)); |
| 450 | } |
| 451 | |
| 452 | void SkPDFArray::appendObjRef(SkPDFObject* value) { |
| 453 | this->append(SkPDFUnion::ObjRef(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /////////////////////////////////////////////////////////////////////////////// |
| 457 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 458 | SkPDFDict::SkPDFDict() {} |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 459 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 460 | SkPDFDict::~SkPDFDict() { this->clear(); } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 461 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 462 | SkPDFDict::SkPDFDict(const char type[]) { this->insertName("Type", type); } |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 463 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 464 | void SkPDFDict::emitObject(SkWStream* stream, |
| 465 | const SkPDFObjNumMap& objNumMap, |
| 466 | const SkPDFSubstituteMap& substitutes) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 467 | stream->writeText("<<"); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 468 | for (int i = 0; i < fRecords.count(); i++) { |
| 469 | fRecords[i].fKey.emitObject(stream, objNumMap, substitutes); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 470 | stream->writeText(" "); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 471 | fRecords[i].fValue.emitObject(stream, objNumMap, substitutes); |
| 472 | if (i + 1 < fRecords.count()) { |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 473 | stream->writeText("\n"); |
| 474 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 475 | } |
| 476 | stream->writeText(">>"); |
| 477 | } |
| 478 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 479 | void SkPDFDict::addResources(SkPDFObjNumMap* catalog, |
| 480 | const SkPDFSubstituteMap& substitutes) const { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 481 | for (int i = 0; i < fRecords.count(); i++) { |
| 482 | fRecords[i].fKey.addResources(catalog, substitutes); |
| 483 | fRecords[i].fValue.addResources(catalog, substitutes); |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 487 | void SkPDFDict::set(SkPDFUnion&& name, SkPDFUnion&& value) { |
| 488 | Record* rec = fRecords.append(); |
| 489 | SkASSERT(name.isName()); |
| 490 | SkNEW_PLACEMENT_ARGS(&rec->fKey, SkPDFUnion, (name.move())); |
| 491 | SkNEW_PLACEMENT_ARGS(&rec->fValue, SkPDFUnion, (value.move())); |
| 492 | } |
| 493 | |
| 494 | int SkPDFDict::size() const { return fRecords.count(); } |
| 495 | |
| 496 | void SkPDFDict::insertObjRef(const char key[], SkPDFObject* value) { |
| 497 | this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(value)); |
| 498 | } |
| 499 | void SkPDFDict::insertObjRef(const SkString& key, SkPDFObject* value) { |
| 500 | this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(value)); |
| 501 | } |
| 502 | |
| 503 | void SkPDFDict::insertObject(const char key[], SkPDFObject* value) { |
| 504 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(value)); |
| 505 | } |
| 506 | void SkPDFDict::insertObject(const SkString& key, SkPDFObject* value) { |
| 507 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(value)); |
| 508 | } |
| 509 | |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 510 | void SkPDFDict::insertBool(const char key[], bool value) { |
| 511 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Bool(value)); |
| 512 | } |
| 513 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 514 | void SkPDFDict::insertInt(const char key[], int32_t value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 515 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Int(value)); |
| 516 | } |
| 517 | |
| 518 | void SkPDFDict::insertInt(const char key[], size_t value) { |
| 519 | this->insertInt(key, SkToS32(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void SkPDFDict::insertScalar(const char key[], SkScalar value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 523 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Scalar(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | void SkPDFDict::insertName(const char key[], const char name[]) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 527 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); |
| 528 | } |
| 529 | |
| 530 | void SkPDFDict::insertName(const char key[], const SkString& name) { |
| 531 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); |
| 532 | } |
| 533 | |
| 534 | void SkPDFDict::insertString(const char key[], const char value[]) { |
| 535 | this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); |
| 536 | } |
| 537 | |
| 538 | void SkPDFDict::insertString(const char key[], const SkString& value) { |
| 539 | this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 540 | } |
| 541 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 542 | void SkPDFDict::clear() { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 543 | for (Record& rec : fRecords) { |
| 544 | rec.fKey.~SkPDFUnion(); |
| 545 | rec.fValue.~SkPDFUnion(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 546 | } |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 547 | fRecords.reset(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 548 | } |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 549 | |
| 550 | //////////////////////////////////////////////////////////////////////////////// |
| 551 | |
| 552 | SkPDFSubstituteMap::~SkPDFSubstituteMap() { |
| 553 | fSubstituteMap.foreach( |
| 554 | [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); |
| 555 | } |
| 556 | |
| 557 | void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, |
| 558 | SkPDFObject* substitute) { |
| 559 | SkASSERT(original != substitute); |
| 560 | SkASSERT(!fSubstituteMap.find(original)); |
| 561 | fSubstituteMap.set(original, SkRef(substitute)); |
| 562 | } |
| 563 | |
| 564 | SkPDFObject* SkPDFSubstituteMap::getSubstitute(SkPDFObject* object) const { |
| 565 | SkPDFObject** found = fSubstituteMap.find(object); |
| 566 | return found ? *found : object; |
| 567 | } |
| 568 | |
| 569 | //////////////////////////////////////////////////////////////////////////////// |
| 570 | |
| 571 | bool SkPDFObjNumMap::addObject(SkPDFObject* obj) { |
| 572 | if (fObjectNumbers.find(obj)) { |
| 573 | return false; |
| 574 | } |
| 575 | fObjectNumbers.set(obj, fObjectNumbers.count() + 1); |
| 576 | fObjects.push(obj); |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const { |
| 581 | int32_t* objectNumberFound = fObjectNumbers.find(obj); |
| 582 | SkASSERT(objectNumberFound); |
| 583 | return *objectNumberFound; |
| 584 | } |
| 585 | |