ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 9 | #ifndef SkPDFUtils_DEFINED |
| 10 | #define SkPDFUtils_DEFINED |
| 11 | |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" |
vandebo@chromium.org | f71b210 | 2011-04-04 19:46:31 +0000 | [diff] [blame] | 13 | #include "SkPath.h" |
halcanary | a76a10b7 | 2016-07-07 12:31:55 -0700 | [diff] [blame] | 14 | #include "SkStream.h" |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 15 | |
| 16 | class SkMatrix; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 17 | class SkPDFArray; |
vandebo@chromium.org | 8989d31 | 2011-05-18 22:10:09 +0000 | [diff] [blame] | 18 | struct SkRect; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 19 | |
vandebo@chromium.org | f71b210 | 2011-04-04 19:46:31 +0000 | [diff] [blame] | 20 | #if 0 |
| 21 | #define PRINT_NOT_IMPL(str) fprintf(stderr, str) |
| 22 | #else |
| 23 | #define PRINT_NOT_IMPL(str) |
| 24 | #endif |
| 25 | |
| 26 | #define NOT_IMPLEMENTED(condition, assert) \ |
| 27 | do { \ |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 28 | if ((bool)(condition)) { \ |
vandebo@chromium.org | f71b210 | 2011-04-04 19:46:31 +0000 | [diff] [blame] | 29 | PRINT_NOT_IMPL("NOT_IMPLEMENTED: " #condition "\n"); \ |
| 30 | SkDEBUGCODE(SkASSERT(!assert);) \ |
| 31 | } \ |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 32 | } while (0) |
vandebo@chromium.org | f71b210 | 2011-04-04 19:46:31 +0000 | [diff] [blame] | 33 | |
halcanary | a76a10b7 | 2016-07-07 12:31:55 -0700 | [diff] [blame] | 34 | namespace SkPDFUtils { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 35 | |
halcanary | a76a10b7 | 2016-07-07 12:31:55 -0700 | [diff] [blame] | 36 | sk_sp<SkPDFArray> RectToArray(const SkRect& rect); |
| 37 | sk_sp<SkPDFArray> MatrixToArray(const SkMatrix& matrix); |
| 38 | void AppendTransform(const SkMatrix& matrix, SkWStream* content); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 39 | |
halcanary | a76a10b7 | 2016-07-07 12:31:55 -0700 | [diff] [blame] | 40 | void MoveTo(SkScalar x, SkScalar y, SkWStream* content); |
| 41 | void AppendLine(SkScalar x, SkScalar y, SkWStream* content); |
| 42 | void AppendCubic(SkScalar ctl1X, SkScalar ctl1Y, |
| 43 | SkScalar ctl2X, SkScalar ctl2Y, |
| 44 | SkScalar dstX, SkScalar dstY, SkWStream* content); |
| 45 | void AppendRectangle(const SkRect& rect, SkWStream* content); |
| 46 | void EmitPath(const SkPath& path, SkPaint::Style paintStyle, |
| 47 | bool doConsumeDegerates, SkWStream* content); |
| 48 | inline void EmitPath(const SkPath& path, SkPaint::Style paintStyle, |
| 49 | SkWStream* content) { |
| 50 | SkPDFUtils::EmitPath(path, paintStyle, true, content); |
| 51 | } |
| 52 | void ClosePath(SkWStream* content); |
| 53 | void PaintPath(SkPaint::Style style, SkPath::FillType fill, |
| 54 | SkWStream* content); |
| 55 | void StrokePath(SkWStream* content); |
| 56 | void DrawFormXObject(int objectIndex, SkWStream* content); |
| 57 | void ApplyGraphicState(int objectIndex, SkWStream* content); |
| 58 | void ApplyPattern(int objectIndex, SkWStream* content); |
| 59 | |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame^] | 60 | // Converts (value / 255.0) with three significant digits of accuracy. |
| 61 | // Writes value as string into result. Returns strlen() of result. |
| 62 | size_t ColorToDecimal(uint8_t value, char result[5]); |
| 63 | inline void AppendColorComponent(uint8_t value, SkWStream* wStream) { |
| 64 | char buffer[5]; |
| 65 | size_t len = SkPDFUtils::ColorToDecimal(value, buffer); |
| 66 | wStream->write(buffer, len); |
| 67 | } |
| 68 | |
halcanary | a76a10b7 | 2016-07-07 12:31:55 -0700 | [diff] [blame] | 69 | // 3 = '-', '.', and '\0' characters. |
| 70 | // 9 = number of significant digits |
| 71 | // abs(FLT_MIN_10_EXP) = number of zeros in FLT_MIN |
| 72 | const size_t kMaximumFloatDecimalLength = 3 + 9 - FLT_MIN_10_EXP; |
| 73 | // FloatToDecimal is exposed for unit tests. |
| 74 | size_t FloatToDecimal(float value, |
| 75 | char output[kMaximumFloatDecimalLength]); |
| 76 | void AppendScalar(SkScalar value, SkWStream* stream); |
| 77 | void WriteString(SkWStream* wStream, const char* input, size_t len); |
| 78 | |
| 79 | inline void WriteUInt16BE(SkDynamicMemoryWStream* wStream, uint16_t value) { |
| 80 | static const char gHex[] = "0123456789ABCDEF"; |
| 81 | char result[4]; |
| 82 | result[0] = gHex[ value >> 12 ]; |
| 83 | result[1] = gHex[0xF & (value >> 8 )]; |
| 84 | result[2] = gHex[0xF & (value >> 4 )]; |
| 85 | result[3] = gHex[0xF & (value )]; |
| 86 | wStream->write(result, 4); |
| 87 | } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 88 | inline void WriteUInt8(SkDynamicMemoryWStream* wStream, uint8_t value) { |
| 89 | static const char gHex[] = "0123456789ABCDEF"; |
| 90 | char result[2]; |
| 91 | result[0] = gHex[value >> 4 ]; |
| 92 | result[1] = gHex[0xF & value]; |
| 93 | wStream->write(result, 2); |
| 94 | } |
halcanary | a76a10b7 | 2016-07-07 12:31:55 -0700 | [diff] [blame] | 95 | |
| 96 | } // namespace SkPDFUtils |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 97 | |
| 98 | #endif |