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 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 10 | #include "SkPDFCatalog.h" |
| 11 | #include "SkPDFTypes.h" |
| 12 | #include "SkStream.h" |
| 13 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 14 | #ifdef SK_BUILD_FOR_WIN |
| 15 | #define SNPRINTF _snprintf |
| 16 | #else |
| 17 | #define SNPRINTF snprintf |
| 18 | #endif |
| 19 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 20 | //////////////////////////////////////////////////////////////////////////////// |
| 21 | |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 22 | SkPDFObjRef::SkPDFObjRef(SkPDFObject* obj) : fObj(obj) { |
| 23 | SkSafeRef(obj); |
| 24 | } |
| 25 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 26 | SkPDFObjRef::~SkPDFObjRef() {} |
| 27 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 28 | void SkPDFObjRef::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame^] | 29 | stream->writeDecAsText(catalog->getObjectNumber(fObj.get())); |
| 30 | stream->writeText(" 0 R"); // Generation number is always 0. |
| 31 | } |
| 32 | |
| 33 | void SkPDFObjRef::addResources(SkTSet<SkPDFObject*>* resourceSet, |
| 34 | SkPDFCatalog* catalog) const { |
| 35 | SkPDFObject* obj = catalog->getSubstituteObject(fObj); |
| 36 | SkASSERT(obj); |
| 37 | if (resourceSet->add(obj)) { |
| 38 | obj->addResources(resourceSet, catalog); |
| 39 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 40 | } |
| 41 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 42 | //////////////////////////////////////////////////////////////////////////////// |
| 43 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 44 | SkPDFInt::SkPDFInt(int32_t value) : fValue(value) {} |
| 45 | SkPDFInt::~SkPDFInt() {} |
| 46 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 47 | void SkPDFInt::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 48 | stream->writeDecAsText(fValue); |
| 49 | } |
| 50 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 51 | //////////////////////////////////////////////////////////////////////////////// |
| 52 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 53 | SkPDFBool::SkPDFBool(bool value) : fValue(value) {} |
| 54 | SkPDFBool::~SkPDFBool() {} |
| 55 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 56 | void SkPDFBool::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 57 | if (fValue) { |
| 58 | stream->writeText("true"); |
| 59 | } else { |
| 60 | stream->writeText("false"); |
| 61 | } |
| 62 | } |
| 63 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 64 | //////////////////////////////////////////////////////////////////////////////// |
| 65 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 66 | SkPDFScalar::SkPDFScalar(SkScalar value) : fValue(value) {} |
| 67 | SkPDFScalar::~SkPDFScalar() {} |
| 68 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 69 | void SkPDFScalar::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 70 | Append(fValue, stream); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 74 | void SkPDFScalar::Append(SkScalar value, SkWStream* stream) { |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 75 | // The range of reals in PDF/A is the same as SkFixed: +/- 32,767 and |
| 76 | // +/- 1/65,536 (though integers can range from 2^31 - 1 to -2^31). |
| 77 | // When using floats that are outside the whole value range, we can use |
| 78 | // integers instead. |
| 79 | |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 80 | #if !defined(SK_ALLOW_LARGE_PDF_SCALARS) |
| 81 | if (value > 32767 || value < -32767) { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 82 | stream->writeDecAsText(SkScalarRoundToInt(value)); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 83 | return; |
| 84 | } |
| 85 | |
| 86 | char buffer[SkStrAppendScalar_MaxSize]; |
| 87 | char* end = SkStrAppendFixed(buffer, SkScalarToFixed(value)); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 88 | stream->write(buffer, end - buffer); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 89 | return; |
| 90 | #endif // !SK_ALLOW_LARGE_PDF_SCALARS |
| 91 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 92 | #if defined(SK_ALLOW_LARGE_PDF_SCALARS) |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 93 | // Floats have 24bits of significance, so anything outside that range is |
| 94 | // no more precise than an int. (Plus PDF doesn't support scientific |
| 95 | // notation, so this clamps to SK_Max/MinS32). |
| 96 | if (value > (1 << 24) || value < -(1 << 24)) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 97 | stream->writeDecAsText(value); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | // Continue to enforce the PDF limits for small floats. |
| 101 | if (value < 1.0f/65536 && value > -1.0f/65536) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 102 | stream->writeDecAsText(0); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | // SkStrAppendFloat might still use scientific notation, so use snprintf |
| 106 | // directly.. |
| 107 | static const int kFloat_MaxSize = 19; |
| 108 | char buffer[kFloat_MaxSize]; |
| 109 | int len = SNPRINTF(buffer, kFloat_MaxSize, "%#.8f", value); |
| 110 | // %f always prints trailing 0s, so strip them. |
| 111 | for (; buffer[len - 1] == '0' && len > 0; len--) { |
| 112 | buffer[len - 1] = '\0'; |
| 113 | } |
| 114 | if (buffer[len - 1] == '.') { |
| 115 | buffer[len - 1] = '\0'; |
| 116 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 117 | stream->writeText(buffer); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 118 | return; |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 119 | #endif // SK_ALLOW_LARGE_PDF_SCALARS |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 120 | } |
| 121 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 122 | //////////////////////////////////////////////////////////////////////////////// |
| 123 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 124 | SkPDFString::SkPDFString(const char value[]) |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 125 | : fValue(FormatString(value, strlen(value))) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | SkPDFString::SkPDFString(const SkString& value) |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 129 | : fValue(FormatString(value.c_str(), value.size())) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | SkPDFString::SkPDFString(const uint16_t* value, size_t len, bool wideChars) |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 133 | : fValue(FormatString(value, len, wideChars)) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 134 | } |
| 135 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 136 | SkPDFString::~SkPDFString() {} |
| 137 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 138 | void SkPDFString::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 139 | stream->write(fValue.c_str(), fValue.size()); |
| 140 | } |
| 141 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 142 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 143 | SkString SkPDFString::FormatString(const char* input, size_t len) { |
| 144 | return DoFormatString(input, len, false, false); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 145 | } |
| 146 | |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 147 | SkString SkPDFString::FormatString(const uint16_t* input, size_t len, |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 148 | bool wideChars) { |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 149 | return DoFormatString(input, len, true, wideChars); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 153 | SkString SkPDFString::DoFormatString(const void* input, size_t len, |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 154 | bool wideInput, bool wideOutput) { |
| 155 | SkASSERT(len <= kMaxLen); |
| 156 | const uint16_t* win = (const uint16_t*) input; |
| 157 | const char* cin = (const char*) input; |
| 158 | |
| 159 | if (wideOutput) { |
| 160 | SkASSERT(wideInput); |
| 161 | SkString result; |
| 162 | result.append("<"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 163 | for (size_t i = 0; i < len; i++) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 164 | result.appendHex(win[i], 4); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 165 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 166 | result.append(">"); |
| 167 | return result; |
| 168 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 169 | |
| 170 | // 7-bit clean is a heuristic to decide what string format to use; |
| 171 | // a 7-bit clean string should require little escaping. |
| 172 | bool sevenBitClean = true; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 173 | for (size_t i = 0; i < len; i++) { |
| 174 | SkASSERT(!wideInput || !(win[i] & ~0xFF)); |
| 175 | char val = wideInput ? win[i] : cin[i]; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 176 | if (val > '~' || val < ' ') { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 177 | sevenBitClean = false; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | SkString result; |
| 183 | if (sevenBitClean) { |
| 184 | result.append("("); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 185 | for (size_t i = 0; i < len; i++) { |
| 186 | SkASSERT(!wideInput || !(win[i] & ~0xFF)); |
| 187 | char val = wideInput ? win[i] : cin[i]; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 188 | if (val == '\\' || val == '(' || val == ')') { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 189 | result.append("\\"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 190 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 191 | result.append(&val, 1); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 192 | } |
| 193 | result.append(")"); |
| 194 | } else { |
| 195 | result.append("<"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 196 | for (size_t i = 0; i < len; i++) { |
| 197 | SkASSERT(!wideInput || !(win[i] & ~0xFF)); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 198 | unsigned char val = wideInput ? win[i] : cin[i]; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 199 | result.appendHex(val, 2); |
| 200 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 201 | result.append(">"); |
| 202 | } |
| 203 | |
| 204 | return result; |
| 205 | } |
| 206 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 207 | //////////////////////////////////////////////////////////////////////////////// |
| 208 | |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 209 | SkPDFName::SkPDFName(const char name[]) : fValue(FormatName(SkString(name))) {} |
| 210 | SkPDFName::SkPDFName(const SkString& name) : fValue(FormatName(name)) {} |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 211 | SkPDFName::~SkPDFName() {} |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 212 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 213 | bool SkPDFName::operator==(const SkPDFName& b) const { |
| 214 | return fValue == b.fValue; |
| 215 | } |
| 216 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 217 | void SkPDFName::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 218 | stream->write(fValue.c_str(), fValue.size()); |
| 219 | } |
| 220 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 221 | // static |
reed@google.com | f6c3ebd | 2011-07-20 17:20:28 +0000 | [diff] [blame] | 222 | SkString SkPDFName::FormatName(const SkString& input) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 223 | SkASSERT(input.size() <= kMaxLen); |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 224 | // TODO(vandebo) If more escaping is needed, improve the linear scan. |
| 225 | static const char escaped[] = "#/%()<>[]{}"; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 226 | |
| 227 | SkString result("/"); |
| 228 | for (size_t i = 0; i < input.size(); i++) { |
vandebo@chromium.org | 251a766 | 2012-09-21 17:50:50 +0000 | [diff] [blame] | 229 | if (input[i] & 0x80 || input[i] < '!' || strchr(escaped, input[i])) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 230 | result.append("#"); |
vandebo@chromium.org | c0376fe | 2012-03-05 18:44:33 +0000 | [diff] [blame] | 231 | // Mask with 0xFF to avoid sign extension. i.e. #FFFFFF81 |
| 232 | result.appendHex(input[i] & 0xFF, 2); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 233 | } else { |
| 234 | result.append(input.c_str() + i, 1); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return result; |
| 239 | } |
| 240 | |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 241 | //////////////////////////////////////////////////////////////////////////////// |
| 242 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 243 | SkPDFArray::SkPDFArray() {} |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 244 | SkPDFArray::~SkPDFArray() { |
reed@google.com | 2812b80 | 2011-07-19 15:46:40 +0000 | [diff] [blame] | 245 | fValue.unrefAll(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 246 | } |
| 247 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 248 | void SkPDFArray::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 249 | stream->writeText("["); |
| 250 | for (int i = 0; i < fValue.count(); i++) { |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 251 | catalog->getSubstituteObject(fValue[i])->emitObject(stream, catalog); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 252 | if (i + 1 < fValue.count()) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 253 | stream->writeText(" "); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 254 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 255 | } |
| 256 | stream->writeText("]"); |
| 257 | } |
| 258 | |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame^] | 259 | void SkPDFArray::addResources(SkTSet<SkPDFObject*>* resourceSet, |
| 260 | SkPDFCatalog* catalog) const { |
| 261 | for (int i = 0; i < fValue.count(); i++) { |
| 262 | catalog->getSubstituteObject(fValue[i]) |
| 263 | ->addResources(resourceSet, catalog); |
| 264 | } |
| 265 | } |
| 266 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 267 | void SkPDFArray::reserve(int length) { |
| 268 | SkASSERT(length <= kMaxLen); |
| 269 | fValue.setReserve(length); |
| 270 | } |
| 271 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 272 | SkPDFObject* SkPDFArray::setAt(int offset, SkPDFObject* value) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 273 | SkASSERT(offset < fValue.count()); |
reed@google.com | 2812b80 | 2011-07-19 15:46:40 +0000 | [diff] [blame] | 274 | value->ref(); |
| 275 | fValue[offset]->unref(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 276 | fValue[offset] = value; |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 277 | return value; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 278 | } |
| 279 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 280 | SkPDFObject* SkPDFArray::append(SkPDFObject* value) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 281 | SkASSERT(fValue.count() < kMaxLen); |
reed@google.com | 2812b80 | 2011-07-19 15:46:40 +0000 | [diff] [blame] | 282 | value->ref(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 283 | fValue.push(value); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 284 | return value; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 285 | } |
| 286 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 287 | void SkPDFArray::appendInt(int32_t value) { |
| 288 | SkASSERT(fValue.count() < kMaxLen); |
| 289 | fValue.push(new SkPDFInt(value)); |
| 290 | } |
| 291 | |
| 292 | void SkPDFArray::appendScalar(SkScalar value) { |
| 293 | SkASSERT(fValue.count() < kMaxLen); |
| 294 | fValue.push(new SkPDFScalar(value)); |
| 295 | } |
| 296 | |
| 297 | void SkPDFArray::appendName(const char name[]) { |
| 298 | SkASSERT(fValue.count() < kMaxLen); |
| 299 | fValue.push(new SkPDFName(name)); |
| 300 | } |
| 301 | |
| 302 | /////////////////////////////////////////////////////////////////////////////// |
| 303 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 304 | SkPDFDict::SkPDFDict() {} |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 305 | |
| 306 | SkPDFDict::SkPDFDict(const char type[]) { |
vandebo@chromium.org | 06f7f40 | 2011-07-20 18:39:20 +0000 | [diff] [blame] | 307 | insertName("Type", type); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 308 | } |
| 309 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 310 | SkPDFDict::~SkPDFDict() { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 311 | clear(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 312 | } |
| 313 | |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 314 | int SkPDFDict::size() const { |
| 315 | SkAutoMutexAcquire lock(fMutex); |
| 316 | return fValue.count(); |
| 317 | } |
| 318 | |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 319 | void SkPDFDict::emitObject(SkWStream* stream, SkPDFCatalog* catalog) { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 320 | SkAutoMutexAcquire lock(fMutex); // If another thread triggers a |
| 321 | // resize while this thread is in |
| 322 | // the for-loop, we can be left |
| 323 | // with a bad fValue[i] reference. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 324 | stream->writeText("<<"); |
| 325 | for (int i = 0; i < fValue.count(); i++) { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 326 | SkASSERT(fValue[i].key); |
| 327 | SkASSERT(fValue[i].value); |
halcanary | 4fc48af | 2015-01-12 10:07:50 -0800 | [diff] [blame] | 328 | fValue[i].key->emitObject(stream, catalog); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 329 | stream->writeText(" "); |
halcanary | 6a14434 | 2015-01-23 11:45:10 -0800 | [diff] [blame] | 330 | catalog->getSubstituteObject(fValue[i].value) |
| 331 | ->emitObject(stream, catalog); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 332 | stream->writeText("\n"); |
| 333 | } |
| 334 | stream->writeText(">>"); |
| 335 | } |
| 336 | |
halcanary | bf799cd | 2015-02-10 13:32:09 -0800 | [diff] [blame^] | 337 | void SkPDFDict::addResources(SkTSet<SkPDFObject*>* resourceSet, |
| 338 | SkPDFCatalog* catalog) const { |
| 339 | for (int i = 0; i < fValue.count(); i++) { |
| 340 | SkASSERT(fValue[i].key); |
| 341 | SkASSERT(fValue[i].value); |
| 342 | fValue[i].key->addResources(resourceSet, catalog); |
| 343 | catalog->getSubstituteObject(fValue[i].value) |
| 344 | ->addResources(resourceSet, catalog); |
| 345 | } |
| 346 | } |
| 347 | |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 348 | SkPDFObject* SkPDFDict::append(SkPDFName* key, SkPDFObject* value) { |
| 349 | SkASSERT(key); |
| 350 | SkASSERT(value); |
| 351 | SkAutoMutexAcquire lock(fMutex); // If the SkTDArray resizes while |
| 352 | // two threads access array, one |
| 353 | // is left with a bad pointer. |
| 354 | *(fValue.append()) = Rec(key, value); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 355 | return value; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 356 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 357 | |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 358 | SkPDFObject* SkPDFDict::insert(SkPDFName* key, SkPDFObject* value) { |
| 359 | return this->append(SkRef(key), SkRef(value)); |
| 360 | } |
| 361 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 362 | SkPDFObject* SkPDFDict::insert(const char key[], SkPDFObject* value) { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 363 | return this->append(new SkPDFName(key), SkRef(value)); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 364 | } |
| 365 | |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 366 | void SkPDFDict::insertInt(const char key[], int32_t value) { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 367 | (void)this->append(new SkPDFName(key), new SkPDFInt(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void SkPDFDict::insertScalar(const char key[], SkScalar value) { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 371 | (void)this->append(new SkPDFName(key), new SkPDFScalar(value)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void SkPDFDict::insertName(const char key[], const char name[]) { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 375 | (void)this->append(new SkPDFName(key), new SkPDFName(name)); |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 376 | } |
| 377 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 378 | void SkPDFDict::clear() { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 379 | SkAutoMutexAcquire lock(fMutex); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 380 | for (int i = 0; i < fValue.count(); i++) { |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 381 | SkASSERT(fValue[i].key); |
| 382 | SkASSERT(fValue[i].value); |
reed@google.com | 2812b80 | 2011-07-19 15:46:40 +0000 | [diff] [blame] | 383 | fValue[i].key->unref(); |
| 384 | fValue[i].value->unref(); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 385 | } |
| 386 | fValue.reset(); |
| 387 | } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 388 | |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 389 | void SkPDFDict::remove(const char key[]) { |
| 390 | SkASSERT(key); |
| 391 | SkPDFName name(key); |
| 392 | SkAutoMutexAcquire lock(fMutex); |
| 393 | for (int i = 0; i < fValue.count(); i++) { |
| 394 | SkASSERT(fValue[i].key); |
| 395 | if (*(fValue[i].key) == name) { |
| 396 | fValue[i].key->unref(); |
| 397 | SkASSERT(fValue[i].value); |
| 398 | fValue[i].value->unref(); |
| 399 | fValue.removeShuffle(i); |
| 400 | return; |
| 401 | } |
| 402 | } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 403 | } |
| 404 | |
halcanary | 1f8ed02 | 2014-06-27 10:37:27 -0700 | [diff] [blame] | 405 | void SkPDFDict::mergeFrom(const SkPDFDict& other) { |
| 406 | SkAutoMutexAcquire lockOther(other.fMutex); |
| 407 | SkTDArray<Rec> copy(other.fValue); |
| 408 | lockOther.release(); // Do not hold both mutexes at once. |
| 409 | |
| 410 | SkAutoMutexAcquire lock(fMutex); |
| 411 | for (int i = 0; i < copy.count(); i++) { |
| 412 | *(fValue.append()) = Rec(SkRef(copy[i].key), SkRef(copy[i].value)); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 413 | } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 414 | } |