OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 | 8 |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkGeometry.h" | 10 #include "SkGeometry.h" |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 // When significantDigits == maxSignificantDigits, we can stop. | 363 // When significantDigits == maxSignificantDigits, we can stop. |
364 // when output == end, we have filed the string. | 364 // when output == end, we have filed the string. |
365 // Note: denormalized numbers will not have the same number of | 365 // Note: denormalized numbers will not have the same number of |
366 // significantDigits, but do not need them to round-trip. | 366 // significantDigits, but do not need them to round-trip. |
367 } | 367 } |
368 SkASSERT(output <= end); | 368 SkASSERT(output <= end); |
369 *output = '\0'; | 369 *output = '\0'; |
370 return output - result; | 370 return output - result; |
371 } | 371 } |
372 | 372 |
373 SkString SkPDFUtils::FormatString(const char* cin, size_t len) { | 373 void SkPDFUtils::WriteString(SkWStream* wStream, const char* cin, size_t len) { |
374 SkDEBUGCODE(static const size_t kMaxLen = 65535;) | 374 SkDEBUGCODE(static const size_t kMaxLen = 65535;) |
375 SkASSERT(len <= kMaxLen); | 375 SkASSERT(len <= kMaxLen); |
376 | 376 |
377 // 7-bit clean is a heuristic to decide what string format to use; | 377 size_t extraCharacterCount = 0; |
378 // a 7-bit clean string should require little escaping. | |
379 bool sevenBitClean = true; | |
380 size_t characterCount = 2 + len; | |
381 for (size_t i = 0; i < len; i++) { | 378 for (size_t i = 0; i < len; i++) { |
382 if (cin[i] > '~' || cin[i] < ' ') { | 379 if (cin[i] > '~' || cin[i] < ' ') { |
383 sevenBitClean = false; | 380 extraCharacterCount += 3; |
384 break; | |
385 } | 381 } |
386 if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { | 382 if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
387 ++characterCount; | 383 ++extraCharacterCount; |
388 } | 384 } |
389 } | 385 } |
390 SkString result; | 386 if (extraCharacterCount <= len) { |
391 if (sevenBitClean) { | 387 wStream->writeText("("); |
392 result.resize(characterCount); | |
393 char* str = result.writable_str(); | |
394 *str++ = '('; | |
395 for (size_t i = 0; i < len; i++) { | 388 for (size_t i = 0; i < len; i++) { |
396 if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { | 389 if (cin[i] > '~' || cin[i] < ' ') { |
397 *str++ = '\\'; | 390 uint8_t c = static_cast<uint8_t>(cin[i]); |
| 391 uint8_t octal[4]; |
| 392 octal[0] = '\\'; |
| 393 octal[1] = '0' + ( c >> 6 ); |
| 394 octal[2] = '0' + ((c >> 3) & 0x07); |
| 395 octal[3] = '0' + ( c & 0x07); |
| 396 wStream->write(octal, 4); |
| 397 } else { |
| 398 if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
| 399 wStream->writeText("\\"); |
| 400 } |
| 401 wStream->write(&cin[i], 1); |
398 } | 402 } |
399 *str++ = cin[i]; | |
400 } | 403 } |
401 *str++ = ')'; | 404 wStream->writeText(")"); |
402 } else { | 405 } else { |
403 result.resize(2 * len + 2); | 406 wStream->writeText("<"); |
404 char* str = result.writable_str(); | |
405 *str++ = '<'; | |
406 for (size_t i = 0; i < len; i++) { | 407 for (size_t i = 0; i < len; i++) { |
407 uint8_t c = static_cast<uint8_t>(cin[i]); | 408 uint8_t c = static_cast<uint8_t>(cin[i]); |
408 static const char gHex[] = "0123456789ABCDEF"; | 409 static const char gHex[] = "0123456789ABCDEF"; |
409 *str++ = gHex[(c >> 4) & 0xF]; | 410 char hexValue[2]; |
410 *str++ = gHex[(c ) & 0xF]; | 411 hexValue[0] = gHex[(c >> 4) & 0xF]; |
| 412 hexValue[1] = gHex[ c & 0xF]; |
| 413 wStream->write(hexValue, 2); |
411 } | 414 } |
412 *str++ = '>'; | 415 wStream->writeText(">"); |
413 } | 416 } |
414 return result; | |
415 } | 417 } |
OLD | NEW |