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" |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 10 | #include "SkPDFUtils.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 11 | #include "SkStream.h" |
| 12 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 13 | #ifdef SK_BUILD_FOR_WIN |
| 14 | #define SNPRINTF _snprintf |
| 15 | #else |
| 16 | #define SNPRINTF snprintf |
| 17 | #endif |
| 18 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 19 | //////////////////////////////////////////////////////////////////////////////// |
| 20 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 21 | SkString* pun(char* x) { return reinterpret_cast<SkString*>(x); } |
| 22 | const SkString* pun(const char* x) { |
| 23 | return reinterpret_cast<const SkString*>(x); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 24 | } |
| 25 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 26 | SkPDFUnion::SkPDFUnion(Type t) : fType(t) {} |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 27 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 28 | SkPDFUnion::~SkPDFUnion() { |
| 29 | switch (fType) { |
| 30 | case Type::kNameSkS: |
| 31 | case Type::kStringSkS: |
| 32 | pun(fSkString)->~SkString(); |
| 33 | return; |
| 34 | case Type::kObjRef: |
| 35 | case Type::kObject: |
| 36 | SkSafeUnref(fObject); |
| 37 | return; |
| 38 | default: |
| 39 | return; |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 40 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 41 | } |
| 42 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 43 | SkPDFUnion& SkPDFUnion::operator=(SkPDFUnion&& other) { |
| 44 | if (this != &other) { |
| 45 | this->~SkPDFUnion(); |
| 46 | SkNEW_PLACEMENT_ARGS(this, SkPDFUnion, (other.move())); |
| 47 | } |
| 48 | return *this; |
| 49 | } |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 50 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 51 | SkPDFUnion::SkPDFUnion(SkPDFUnion&& other) { |
| 52 | SkASSERT(this != &other); |
| 53 | memcpy(this, &other, sizeof(*this)); |
| 54 | other.fType = Type::kDestroyed; |
| 55 | } |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 56 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 57 | #if 0 |
| 58 | SkPDFUnion SkPDFUnion::copy() const { |
| 59 | SkPDFUnion u(fType); |
| 60 | memcpy(&u, this, sizeof(u)); |
| 61 | switch (fType) { |
| 62 | case Type::kNameSkS: |
| 63 | case Type::kStringSkS: |
| 64 | SkNEW_PLACEMENT_ARGS(pun(u.fSkString), SkString, |
| 65 | (*pun(fSkString))); |
| 66 | return u.move(); |
| 67 | case Type::kObjRef: |
| 68 | case Type::kObject: |
| 69 | SkRef(u.fObject); |
| 70 | return u.move(); |
| 71 | default: |
| 72 | return u.move(); |
| 73 | } |
| 74 | } |
| 75 | SkPDFUnion& SkPDFUnion::operator=(const SkPDFUnion& other) { |
| 76 | return *this = other.copy(); |
| 77 | } |
| 78 | SkPDFUnion::SkPDFUnion(const SkPDFUnion& other) { |
| 79 | *this = other.copy(); |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | bool SkPDFUnion::isName() const { |
| 84 | return Type::kName == fType || Type::kNameSkS == fType; |
| 85 | } |
| 86 | |
| 87 | #ifdef SK_DEBUG |
| 88 | // Most names need no escaping. Such names are handled as static |
| 89 | // const strings. |
| 90 | bool is_valid_name(const char* n) { |
| 91 | static const char kControlChars[] = "/%()<>[]{}"; |
| 92 | while (*n) { |
| 93 | if (*n < '!' || *n > '~' || strchr(kControlChars, *n)) { |
| 94 | return false; |
| 95 | } |
| 96 | ++n; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | #endif // SK_DEBUG |
| 101 | |
halcanary | 8ca89e1 | 2015-06-08 12:14:56 -0700 | [diff] [blame] | 102 | // Given an arbitrary string, write it as a valid name (not including |
| 103 | // leading slash). |
| 104 | static void write_name_escaped(SkWStream* o, const char* name) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 105 | static const char kToEscape[] = "#/%()<>[]{}"; |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 106 | static const char kHex[] = "0123456789ABCDEF"; |
halcanary | 8ca89e1 | 2015-06-08 12:14:56 -0700 | [diff] [blame] | 107 | for (const uint8_t* n = reinterpret_cast<const uint8_t*>(name); *n; ++n) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 108 | if (*n < '!' || *n > '~' || strchr(kToEscape, *n)) { |
halcanary | 8ca89e1 | 2015-06-08 12:14:56 -0700 | [diff] [blame] | 109 | char buffer[3] = {'#', '\0', '\0'}; |
| 110 | buffer[1] = kHex[(*n >> 4) & 0xF]; |
| 111 | buffer[2] = kHex[*n & 0xF]; |
| 112 | o->write(buffer, sizeof(buffer)); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 113 | } else { |
halcanary | 8ca89e1 | 2015-06-08 12:14:56 -0700 | [diff] [blame] | 114 | o->write(n, 1); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void write_string(SkWStream* o, const SkString& s) { |
| 120 | o->write(s.c_str(), s.size()); |
| 121 | } |
| 122 | |
| 123 | static SkString format_string(const SkString& s) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 124 | return SkPDFUtils::FormatString(s.c_str(), s.size()); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static SkString format_string(const char* s) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 128 | return SkPDFUtils::FormatString(s, strlen(s)); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void SkPDFUnion::emitObject(SkWStream* stream, |
| 132 | const SkPDFObjNumMap& objNumMap, |
| 133 | const SkPDFSubstituteMap& substitutes) const { |
| 134 | switch (fType) { |
| 135 | case Type::kInt: |
| 136 | stream->writeDecAsText(fIntValue); |
| 137 | return; |
| 138 | case Type::kBool: |
| 139 | stream->writeText(fBoolValue ? "true" : "false"); |
| 140 | return; |
| 141 | case Type::kScalar: |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 142 | SkPDFUtils::AppendScalar(fScalarValue, stream); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 143 | return; |
| 144 | case Type::kName: |
| 145 | stream->writeText("/"); |
| 146 | SkASSERT(is_valid_name(fStaticString)); |
| 147 | stream->writeText(fStaticString); |
| 148 | return; |
| 149 | case Type::kString: |
| 150 | SkASSERT(fStaticString); |
| 151 | write_string(stream, format_string(fStaticString)); |
| 152 | return; |
| 153 | case Type::kNameSkS: |
| 154 | stream->writeText("/"); |
halcanary | 8ca89e1 | 2015-06-08 12:14:56 -0700 | [diff] [blame] | 155 | write_name_escaped(stream, pun(fSkString)->c_str()); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 156 | return; |
| 157 | case Type::kStringSkS: |
| 158 | write_string(stream, format_string(*pun(fSkString))); |
| 159 | return; |
| 160 | case Type::kObjRef: |
| 161 | stream->writeDecAsText(objNumMap.getObjectNumber( |
| 162 | substitutes.getSubstitute(fObject))); |
| 163 | stream->writeText(" 0 R"); // Generation number is always 0. |
| 164 | return; |
| 165 | case Type::kObject: |
| 166 | fObject->emitObject(stream, objNumMap, substitutes); |
| 167 | return; |
| 168 | default: |
| 169 | SkDEBUGFAIL("SkPDFUnion::emitObject with bad type"); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void SkPDFUnion::addResources(SkPDFObjNumMap* objNumMap, |
| 174 | const SkPDFSubstituteMap& substituteMap) const { |
| 175 | switch (fType) { |
| 176 | case Type::kInt: |
| 177 | case Type::kBool: |
| 178 | case Type::kScalar: |
| 179 | case Type::kName: |
| 180 | case Type::kString: |
| 181 | case Type::kNameSkS: |
| 182 | case Type::kStringSkS: |
| 183 | return; // These have no resources. |
| 184 | case Type::kObjRef: { |
| 185 | SkPDFObject* obj = substituteMap.getSubstitute(fObject); |
| 186 | if (objNumMap->addObject(obj)) { |
| 187 | obj->addResources(objNumMap, substituteMap); |
| 188 | } |
| 189 | return; |
| 190 | } |
| 191 | case Type::kObject: |
| 192 | fObject->addResources(objNumMap, substituteMap); |
| 193 | return; |
| 194 | default: |
| 195 | SkDEBUGFAIL("SkPDFUnion::addResources with bad type"); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | SkPDFUnion SkPDFUnion::Int(int32_t value) { |
| 200 | SkPDFUnion u(Type::kInt); |
| 201 | u.fIntValue = value; |
| 202 | return u.move(); |
| 203 | } |
| 204 | |
| 205 | SkPDFUnion SkPDFUnion::Bool(bool value) { |
| 206 | SkPDFUnion u(Type::kBool); |
| 207 | u.fBoolValue = value; |
| 208 | return u.move(); |
| 209 | } |
| 210 | |
| 211 | SkPDFUnion SkPDFUnion::Scalar(SkScalar value) { |
| 212 | SkPDFUnion u(Type::kScalar); |
| 213 | u.fScalarValue = value; |
| 214 | return u.move(); |
| 215 | } |
| 216 | |
| 217 | SkPDFUnion SkPDFUnion::Name(const char* value) { |
| 218 | SkPDFUnion u(Type::kName); |
| 219 | SkASSERT(value); |
| 220 | SkASSERT(is_valid_name(value)); |
| 221 | u.fStaticString = value; |
| 222 | return u.move(); |
| 223 | } |
| 224 | |
| 225 | SkPDFUnion SkPDFUnion::String(const char* value) { |
| 226 | SkPDFUnion u(Type::kString); |
| 227 | SkASSERT(value); |
| 228 | u.fStaticString = value; |
| 229 | return u.move(); |
| 230 | } |
| 231 | |
| 232 | SkPDFUnion SkPDFUnion::Name(const SkString& s) { |
| 233 | SkPDFUnion u(Type::kNameSkS); |
| 234 | SkNEW_PLACEMENT_ARGS(pun(u.fSkString), SkString, (s)); |
| 235 | return u.move(); |
| 236 | } |
| 237 | |
| 238 | SkPDFUnion SkPDFUnion::String(const SkString& s) { |
| 239 | SkPDFUnion u(Type::kStringSkS); |
| 240 | SkNEW_PLACEMENT_ARGS(pun(u.fSkString), SkString, (s)); |
| 241 | return u.move(); |
| 242 | } |
| 243 | |
| 244 | SkPDFUnion SkPDFUnion::ObjRef(SkPDFObject* ptr) { |
| 245 | SkPDFUnion u(Type::kObjRef); |
| 246 | SkASSERT(ptr); |
| 247 | u.fObject = ptr; |
| 248 | return u.move(); |
| 249 | } |
| 250 | |
| 251 | SkPDFUnion SkPDFUnion::Object(SkPDFObject* ptr) { |
| 252 | SkPDFUnion u(Type::kObject); |
| 253 | SkASSERT(ptr); |
| 254 | u.fObject = ptr; |
| 255 | return u.move(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 256 | } |
| 257 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 258 | //////////////////////////////////////////////////////////////////////////////// |
| 259 | |
halcanary | 002653e | 2015-05-05 11:28:55 -0700 | [diff] [blame] | 260 | #if 0 // Enable if needed. |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 261 | void SkPDFAtom::emitObject(SkWStream* stream, |
| 262 | const SkPDFObjNumMap& objNumMap, |
| 263 | const SkPDFSubstituteMap& substitutes) { |
| 264 | fValue.emitObject(stream, objNumMap, substitutes); |
| 265 | } |
| 266 | void SkPDFAtom::addResources(SkPDFObjNumMap* map, |
| 267 | const SkPDFSubstituteMap& substitutes) const { |
| 268 | fValue.addResources(map, substitutes); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 269 | } |
halcanary | 002653e | 2015-05-05 11:28:55 -0700 | [diff] [blame] | 270 | #endif // 0 |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 271 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 272 | //////////////////////////////////////////////////////////////////////////////// |
| 273 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 274 | SkPDFArray::SkPDFArray() {} |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 275 | SkPDFArray::~SkPDFArray() { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 276 | for (SkPDFUnion& value : fValues) { |
| 277 | value.~SkPDFUnion(); |
| 278 | } |
| 279 | fValues.reset(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 280 | } |
| 281 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 282 | int SkPDFArray::size() const { return fValues.count(); } |
| 283 | |
| 284 | void SkPDFArray::reserve(int length) { fValues.setReserve(length); } |
| 285 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 286 | void SkPDFArray::emitObject(SkWStream* stream, |
| 287 | const SkPDFObjNumMap& objNumMap, |
| 288 | const SkPDFSubstituteMap& substitutes) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 289 | stream->writeText("["); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 290 | for (int i = 0; i < fValues.count(); i++) { |
| 291 | fValues[i].emitObject(stream, objNumMap, substitutes); |
| 292 | if (i + 1 < fValues.count()) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 293 | stream->writeText(" "); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 294 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 295 | } |
| 296 | stream->writeText("]"); |
| 297 | } |
| 298 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 299 | void SkPDFArray::addResources(SkPDFObjNumMap* catalog, |
| 300 | const SkPDFSubstituteMap& substitutes) const { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 301 | for (const SkPDFUnion& value : fValues) { |
| 302 | value.addResources(catalog, substitutes); |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 306 | void SkPDFArray::append(SkPDFUnion&& value) { |
| 307 | SkNEW_PLACEMENT_ARGS(fValues.append(), SkPDFUnion, (value.move())); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 308 | } |
| 309 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 310 | void SkPDFArray::appendInt(int32_t value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 311 | this->append(SkPDFUnion::Int(value)); |
| 312 | } |
| 313 | |
| 314 | void SkPDFArray::appendBool(bool value) { |
| 315 | this->append(SkPDFUnion::Bool(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void SkPDFArray::appendScalar(SkScalar value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 319 | this->append(SkPDFUnion::Scalar(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void SkPDFArray::appendName(const char name[]) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 323 | this->append(SkPDFUnion::Name(SkString(name))); |
| 324 | } |
| 325 | |
| 326 | void SkPDFArray::appendName(const SkString& name) { |
| 327 | this->append(SkPDFUnion::Name(name)); |
| 328 | } |
| 329 | |
| 330 | void SkPDFArray::appendString(const SkString& value) { |
| 331 | this->append(SkPDFUnion::String(value)); |
| 332 | } |
| 333 | |
| 334 | void SkPDFArray::appendString(const char value[]) { |
| 335 | this->append(SkPDFUnion::String(value)); |
| 336 | } |
| 337 | |
| 338 | void SkPDFArray::appendObject(SkPDFObject* value) { |
| 339 | this->append(SkPDFUnion::Object(value)); |
| 340 | } |
| 341 | |
| 342 | void SkPDFArray::appendObjRef(SkPDFObject* value) { |
| 343 | this->append(SkPDFUnion::ObjRef(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /////////////////////////////////////////////////////////////////////////////// |
| 347 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 348 | SkPDFDict::SkPDFDict() {} |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 349 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 350 | SkPDFDict::~SkPDFDict() { this->clear(); } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 351 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 352 | SkPDFDict::SkPDFDict(const char type[]) { this->insertName("Type", type); } |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 353 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 354 | void SkPDFDict::emitObject(SkWStream* stream, |
| 355 | const SkPDFObjNumMap& objNumMap, |
| 356 | const SkPDFSubstituteMap& substitutes) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 357 | stream->writeText("<<"); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 358 | for (int i = 0; i < fRecords.count(); i++) { |
| 359 | fRecords[i].fKey.emitObject(stream, objNumMap, substitutes); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 360 | stream->writeText(" "); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 361 | fRecords[i].fValue.emitObject(stream, objNumMap, substitutes); |
| 362 | if (i + 1 < fRecords.count()) { |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 363 | stream->writeText("\n"); |
| 364 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 365 | } |
| 366 | stream->writeText(">>"); |
| 367 | } |
| 368 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 369 | void SkPDFDict::addResources(SkPDFObjNumMap* catalog, |
| 370 | const SkPDFSubstituteMap& substitutes) const { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 371 | for (int i = 0; i < fRecords.count(); i++) { |
| 372 | fRecords[i].fKey.addResources(catalog, substitutes); |
| 373 | fRecords[i].fValue.addResources(catalog, substitutes); |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 377 | void SkPDFDict::set(SkPDFUnion&& name, SkPDFUnion&& value) { |
| 378 | Record* rec = fRecords.append(); |
| 379 | SkASSERT(name.isName()); |
| 380 | SkNEW_PLACEMENT_ARGS(&rec->fKey, SkPDFUnion, (name.move())); |
| 381 | SkNEW_PLACEMENT_ARGS(&rec->fValue, SkPDFUnion, (value.move())); |
| 382 | } |
| 383 | |
| 384 | int SkPDFDict::size() const { return fRecords.count(); } |
| 385 | |
| 386 | void SkPDFDict::insertObjRef(const char key[], SkPDFObject* value) { |
| 387 | this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(value)); |
| 388 | } |
| 389 | void SkPDFDict::insertObjRef(const SkString& key, SkPDFObject* value) { |
| 390 | this->set(SkPDFUnion::Name(key), SkPDFUnion::ObjRef(value)); |
| 391 | } |
| 392 | |
| 393 | void SkPDFDict::insertObject(const char key[], SkPDFObject* value) { |
| 394 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(value)); |
| 395 | } |
| 396 | void SkPDFDict::insertObject(const SkString& key, SkPDFObject* value) { |
| 397 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Object(value)); |
| 398 | } |
| 399 | |
halcanary | a25b337 | 2015-04-27 14:00:09 -0700 | [diff] [blame] | 400 | void SkPDFDict::insertBool(const char key[], bool value) { |
| 401 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Bool(value)); |
| 402 | } |
| 403 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 404 | void SkPDFDict::insertInt(const char key[], int32_t value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 405 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Int(value)); |
| 406 | } |
| 407 | |
| 408 | void SkPDFDict::insertInt(const char key[], size_t value) { |
| 409 | this->insertInt(key, SkToS32(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void SkPDFDict::insertScalar(const char key[], SkScalar value) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 413 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Scalar(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void SkPDFDict::insertName(const char key[], const char name[]) { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 417 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); |
| 418 | } |
| 419 | |
| 420 | void SkPDFDict::insertName(const char key[], const SkString& name) { |
| 421 | this->set(SkPDFUnion::Name(key), SkPDFUnion::Name(name)); |
| 422 | } |
| 423 | |
| 424 | void SkPDFDict::insertString(const char key[], const char value[]) { |
| 425 | this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); |
| 426 | } |
| 427 | |
| 428 | void SkPDFDict::insertString(const char key[], const SkString& value) { |
| 429 | this->set(SkPDFUnion::Name(key), SkPDFUnion::String(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 430 | } |
| 431 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 432 | void SkPDFDict::clear() { |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 433 | for (Record& rec : fRecords) { |
| 434 | rec.fKey.~SkPDFUnion(); |
| 435 | rec.fValue.~SkPDFUnion(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 436 | } |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 437 | fRecords.reset(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 438 | } |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 439 | |
| 440 | //////////////////////////////////////////////////////////////////////////////// |
| 441 | |
| 442 | SkPDFSubstituteMap::~SkPDFSubstituteMap() { |
| 443 | fSubstituteMap.foreach( |
| 444 | [](SkPDFObject*, SkPDFObject** v) { (*v)->unref(); }); |
| 445 | } |
| 446 | |
| 447 | void SkPDFSubstituteMap::setSubstitute(SkPDFObject* original, |
| 448 | SkPDFObject* substitute) { |
| 449 | SkASSERT(original != substitute); |
| 450 | SkASSERT(!fSubstituteMap.find(original)); |
| 451 | fSubstituteMap.set(original, SkRef(substitute)); |
| 452 | } |
| 453 | |
| 454 | SkPDFObject* SkPDFSubstituteMap::getSubstitute(SkPDFObject* object) const { |
| 455 | SkPDFObject** found = fSubstituteMap.find(object); |
| 456 | return found ? *found : object; |
| 457 | } |
| 458 | |
| 459 | //////////////////////////////////////////////////////////////////////////////// |
| 460 | |
| 461 | bool SkPDFObjNumMap::addObject(SkPDFObject* obj) { |
| 462 | if (fObjectNumbers.find(obj)) { |
| 463 | return false; |
| 464 | } |
| 465 | fObjectNumbers.set(obj, fObjectNumbers.count() + 1); |
| 466 | fObjects.push(obj); |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | int32_t SkPDFObjNumMap::getObjectNumber(SkPDFObject* obj) const { |
| 471 | int32_t* objectNumberFound = fObjectNumbers.find(obj); |
| 472 | SkASSERT(objectNumberFound); |
| 473 | return *objectNumberFound; |
| 474 | } |
| 475 | |