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