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 | |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 10 | #include "SkFixed.h" |
reed@google.com | a44ea51 | 2011-07-27 18:24:25 +0000 | [diff] [blame] | 11 | #include "SkGeometry.h" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 12 | #include "SkPDFResourceDict.h" |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 13 | #include "SkPDFUtils.h" |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 15 | #include "SkString.h" |
| 16 | #include "SkPDFTypes.h" |
| 17 | |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 18 | #include <cmath> |
| 19 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 20 | sk_sp<SkPDFArray> SkPDFUtils::RectToArray(const SkRect& rect) { |
| 21 | auto result = sk_make_sp<SkPDFArray>(); |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 22 | result->reserve(4); |
| 23 | result->appendScalar(rect.fLeft); |
| 24 | result->appendScalar(rect.fTop); |
| 25 | result->appendScalar(rect.fRight); |
| 26 | result->appendScalar(rect.fBottom); |
| 27 | return result; |
| 28 | } |
| 29 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 30 | sk_sp<SkPDFArray> SkPDFUtils::MatrixToArray(const SkMatrix& matrix) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 31 | SkScalar values[6]; |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 32 | if (!matrix.asAffine(values)) { |
| 33 | SkMatrix::SetAffineIdentity(values); |
| 34 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 35 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 36 | auto result = sk_make_sp<SkPDFArray>(); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 37 | result->reserve(6); |
| 38 | for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 39 | result->appendScalar(values[i]); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 40 | } |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | // static |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 45 | void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) { |
| 46 | SkScalar values[6]; |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 47 | if (!matrix.asAffine(values)) { |
| 48 | SkMatrix::SetAffineIdentity(values); |
| 49 | } |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 50 | for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 51 | SkPDFUtils::AppendScalar(values[i], content); |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 52 | content->writeText(" "); |
| 53 | } |
| 54 | content->writeText("cm\n"); |
| 55 | } |
| 56 | |
| 57 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 58 | void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 59 | SkPDFUtils::AppendScalar(x, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 60 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 61 | SkPDFUtils::AppendScalar(y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 62 | content->writeText(" m\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 66 | void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 67 | SkPDFUtils::AppendScalar(x, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 68 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 69 | SkPDFUtils::AppendScalar(y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 70 | content->writeText(" l\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // static |
| 74 | void SkPDFUtils::AppendCubic(SkScalar ctl1X, SkScalar ctl1Y, |
| 75 | SkScalar ctl2X, SkScalar ctl2Y, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 76 | SkScalar dstX, SkScalar dstY, SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 77 | SkString cmd("y\n"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 78 | SkPDFUtils::AppendScalar(ctl1X, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 79 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 80 | SkPDFUtils::AppendScalar(ctl1Y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 81 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 82 | if (ctl2X != dstX || ctl2Y != dstY) { |
| 83 | cmd.set("c\n"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 84 | SkPDFUtils::AppendScalar(ctl2X, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 85 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 86 | SkPDFUtils::AppendScalar(ctl2Y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 87 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 88 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 89 | SkPDFUtils::AppendScalar(dstX, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 90 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 91 | SkPDFUtils::AppendScalar(dstY, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 92 | content->writeText(" "); |
| 93 | content->writeText(cmd.c_str()); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 94 | } |
| 95 | |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 96 | static void append_quad(const SkPoint quad[], SkWStream* content) { |
| 97 | SkPoint cubic[4]; |
| 98 | SkConvertQuadToCubic(quad, cubic); |
| 99 | SkPDFUtils::AppendCubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY, |
| 100 | cubic[3].fX, cubic[3].fY, content); |
| 101 | } |
| 102 | |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 103 | // static |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 104 | void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) { |
| 105 | // Skia has 0,0 at top left, pdf at bottom left. Do the right thing. |
| 106 | SkScalar bottom = SkMinScalar(rect.fBottom, rect.fTop); |
| 107 | |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 108 | SkPDFUtils::AppendScalar(rect.fLeft, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 109 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 110 | SkPDFUtils::AppendScalar(bottom, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 111 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 112 | SkPDFUtils::AppendScalar(rect.width(), content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 113 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 114 | SkPDFUtils::AppendScalar(rect.height(), content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 115 | content->writeText(" re\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 116 | } |
| 117 | |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 118 | // static |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 119 | void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle, |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 120 | bool doConsumeDegerates, SkWStream* content) { |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 121 | // Filling a path with no area results in a drawing in PDF renderers but |
| 122 | // Chrome expects to be able to draw some such entities with no visible |
| 123 | // result, so we detect those cases and discard the drawing for them. |
| 124 | // Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y). |
Hal Canary | 970225b | 2016-11-10 11:46:21 -0500 | [diff] [blame] | 125 | |
| 126 | SkRect rect; |
| 127 | bool isClosed; // Both closure and direction need to be checked. |
| 128 | SkPath::Direction direction; |
| 129 | if (path.isRect(&rect, &isClosed, &direction) && |
| 130 | isClosed && SkPath::kCW_Direction == direction) |
| 131 | { |
| 132 | SkPDFUtils::AppendRectangle(rect, content); |
| 133 | return; |
| 134 | } |
| 135 | |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 136 | enum SkipFillState { |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 137 | kEmpty_SkipFillState, |
| 138 | kSingleLine_SkipFillState, |
| 139 | kNonSingleLine_SkipFillState, |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 140 | }; |
| 141 | SkipFillState fillState = kEmpty_SkipFillState; |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 142 | //if (paintStyle != SkPaint::kFill_Style) { |
| 143 | // fillState = kNonSingleLine_SkipFillState; |
| 144 | //} |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 145 | SkPoint lastMovePt = SkPoint::Make(0,0); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 146 | SkDynamicMemoryWStream currentSegment; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 147 | SkPoint args[4]; |
| 148 | SkPath::Iter iter(path, false); |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 149 | for (SkPath::Verb verb = iter.next(args, doConsumeDegerates); |
| 150 | verb != SkPath::kDone_Verb; |
| 151 | verb = iter.next(args, doConsumeDegerates)) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 152 | // args gets all the points, even the implicit first point. |
| 153 | switch (verb) { |
| 154 | case SkPath::kMove_Verb: |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 155 | MoveTo(args[0].fX, args[0].fY, ¤tSegment); |
| 156 | lastMovePt = args[0]; |
| 157 | fillState = kEmpty_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 158 | break; |
| 159 | case SkPath::kLine_Verb: |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 160 | AppendLine(args[1].fX, args[1].fY, ¤tSegment); |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 161 | if ((fillState == kEmpty_SkipFillState) && (args[0] != lastMovePt)) { |
| 162 | fillState = kSingleLine_SkipFillState; |
| 163 | break; |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 164 | } |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 165 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 166 | break; |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 167 | case SkPath::kQuad_Verb: |
| 168 | append_quad(args, ¤tSegment); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 169 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 170 | break; |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 171 | case SkPath::kConic_Verb: { |
| 172 | const SkScalar tol = SK_Scalar1 / 4; |
| 173 | SkAutoConicToQuads converter; |
| 174 | const SkPoint* quads = converter.computeQuads(args, iter.conicWeight(), tol); |
| 175 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 176 | append_quad(&quads[i * 2], ¤tSegment); |
| 177 | } |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 178 | fillState = kNonSingleLine_SkipFillState; |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 179 | } break; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 180 | case SkPath::kCubic_Verb: |
| 181 | AppendCubic(args[1].fX, args[1].fY, args[2].fX, args[2].fY, |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 182 | args[3].fX, args[3].fY, ¤tSegment); |
| 183 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 184 | break; |
| 185 | case SkPath::kClose_Verb: |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 186 | |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 187 | ClosePath(¤tSegment); |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 188 | |
| 189 | currentSegment.writeToStream(content); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 190 | currentSegment.reset(); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 191 | break; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 192 | default: |
| 193 | SkASSERT(false); |
| 194 | break; |
| 195 | } |
| 196 | } |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 197 | if (currentSegment.bytesWritten() > 0) { |
halcanary | 7af2150 | 2015-02-23 12:17:59 -0800 | [diff] [blame] | 198 | currentSegment.writeToStream(content); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 199 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 203 | void SkPDFUtils::ClosePath(SkWStream* content) { |
| 204 | content->writeText("h\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // static |
| 208 | void SkPDFUtils::PaintPath(SkPaint::Style style, SkPath::FillType fill, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 209 | SkWStream* content) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 210 | if (style == SkPaint::kFill_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 211 | content->writeText("f"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 212 | } else if (style == SkPaint::kStrokeAndFill_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 213 | content->writeText("B"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 214 | } else if (style == SkPaint::kStroke_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 215 | content->writeText("S"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 216 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 217 | |
| 218 | if (style != SkPaint::kStroke_Style) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 219 | NOT_IMPLEMENTED(fill == SkPath::kInverseEvenOdd_FillType, false); |
| 220 | NOT_IMPLEMENTED(fill == SkPath::kInverseWinding_FillType, false); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 221 | if (fill == SkPath::kEvenOdd_FillType) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 222 | content->writeText("*"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 223 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 224 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 225 | content->writeText("\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 229 | void SkPDFUtils::StrokePath(SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 230 | SkPDFUtils::PaintPath( |
| 231 | SkPaint::kStroke_Style, SkPath::kWinding_FillType, content); |
| 232 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 233 | |
| 234 | // static |
| 235 | void SkPDFUtils::DrawFormXObject(int objectIndex, SkWStream* content) { |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 236 | content->writeText("/"); |
| 237 | content->writeText(SkPDFResourceDict::getResourceName( |
| 238 | SkPDFResourceDict::kXObject_ResourceType, |
| 239 | objectIndex).c_str()); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 240 | content->writeText(" Do\n"); |
| 241 | } |
| 242 | |
| 243 | // static |
| 244 | void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) { |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 245 | content->writeText("/"); |
| 246 | content->writeText(SkPDFResourceDict::getResourceName( |
| 247 | SkPDFResourceDict::kExtGState_ResourceType, |
| 248 | objectIndex).c_str()); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 249 | content->writeText(" gs\n"); |
| 250 | } |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 251 | |
| 252 | // static |
| 253 | void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) { |
| 254 | // Select Pattern color space (CS, cs) and set pattern object as current |
| 255 | // color (SCN, scn) |
| 256 | SkString resourceName = SkPDFResourceDict::getResourceName( |
| 257 | SkPDFResourceDict::kPattern_ResourceType, |
| 258 | objectIndex); |
| 259 | content->writeText("/Pattern CS/Pattern cs/"); |
| 260 | content->writeText(resourceName.c_str()); |
| 261 | content->writeText(" SCN/"); |
| 262 | content->writeText(resourceName.c_str()); |
| 263 | content->writeText(" scn\n"); |
| 264 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 265 | |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 266 | size_t SkPDFUtils::ColorToDecimal(uint8_t value, char result[5]) { |
| 267 | if (value == 255 || value == 0) { |
| 268 | result[0] = value ? '1' : '0'; |
| 269 | result[1] = '\0'; |
| 270 | return 1; |
| 271 | } |
| 272 | // int x = 0.5 + (1000.0 / 255.0) * value; |
| 273 | int x = SkFixedRoundToInt((SK_Fixed1 * 1000 / 255) * value); |
| 274 | result[0] = '.'; |
| 275 | for (int i = 3; i > 0; --i) { |
| 276 | result[i] = '0' + x % 10; |
| 277 | x /= 10; |
| 278 | } |
| 279 | int j; |
| 280 | for (j = 3; j > 1; --j) { |
| 281 | if (result[j] != '0') { |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | result[j + 1] = '\0'; |
| 286 | return j + 1; |
| 287 | } |
| 288 | |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 289 | void SkPDFUtils::AppendScalar(SkScalar value, SkWStream* stream) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 290 | char result[kMaximumFloatDecimalLength]; |
| 291 | size_t len = SkPDFUtils::FloatToDecimal(SkScalarToFloat(value), result); |
| 292 | SkASSERT(len < kMaximumFloatDecimalLength); |
| 293 | stream->write(result, len); |
| 294 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 295 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 296 | // Return pow(10.0, e), optimized for common cases. |
| 297 | inline double pow10(int e) { |
| 298 | switch (e) { |
| 299 | case 0: return 1.0; // common cases |
| 300 | case 1: return 10.0; |
| 301 | case 2: return 100.0; |
| 302 | case 3: return 1e+03; |
| 303 | case 4: return 1e+04; |
| 304 | case 5: return 1e+05; |
| 305 | case 6: return 1e+06; |
| 306 | case 7: return 1e+07; |
| 307 | case 8: return 1e+08; |
| 308 | case 9: return 1e+09; |
| 309 | case 10: return 1e+10; |
| 310 | case 11: return 1e+11; |
| 311 | case 12: return 1e+12; |
| 312 | case 13: return 1e+13; |
| 313 | case 14: return 1e+14; |
| 314 | case 15: return 1e+15; |
| 315 | default: |
| 316 | if (e > 15) { |
| 317 | double value = 1e+15; |
| 318 | while (e-- > 15) { value *= 10.0; } |
| 319 | return value; |
| 320 | } else { |
| 321 | SkASSERT(e < 0); |
| 322 | double value = 1.0; |
| 323 | while (e++ < 0) { value /= 10.0; } |
| 324 | return value; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 329 | /** Write a string into result, includeing a terminating '\0' (for |
| 330 | unit testing). Return strlen(result) (for SkWStream::write) The |
| 331 | resulting string will be in the form /[-]?([0-9]*.)?[0-9]+/ and |
| 332 | sscanf(result, "%f", &x) will return the original value iff the |
| 333 | value is finite. This function accepts all possible input values. |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 334 | |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 335 | Motivation: "PDF does not support [numbers] in exponential format |
| 336 | (such as 6.02e23)." Otherwise, this function would rely on a |
| 337 | sprintf-type function from the standard library. */ |
| 338 | size_t SkPDFUtils::FloatToDecimal(float value, |
| 339 | char result[kMaximumFloatDecimalLength]) { |
| 340 | /* The longest result is -FLT_MIN. |
| 341 | We serialize it as "-.0000000000000000000000000000000000000117549435" |
| 342 | which has 48 characters plus a terminating '\0'. */ |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 343 | |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 344 | /* section C.1 of the PDF1.4 spec (http://goo.gl/0SCswJ) says that |
| 345 | most PDF rasterizers will use fixed-point scalars that lack the |
| 346 | dynamic range of floats. Even if this is the case, I want to |
| 347 | serialize these (uncommon) very small and very large scalar |
| 348 | values with enough precision to allow a floating-point |
| 349 | rasterizer to read them in with perfect accuracy. |
| 350 | Experimentally, rasterizers such as pdfium do seem to benefit |
| 351 | from this. Rasterizers that rely on fixed-point scalars should |
| 352 | gracefully ignore these values that they can not parse. */ |
| 353 | char* output = &result[0]; |
| 354 | const char* const end = &result[kMaximumFloatDecimalLength - 1]; |
| 355 | // subtract one to leave space for '\0'. |
| 356 | |
| 357 | /* This function is written to accept any possible input value, |
| 358 | including non-finite values such as INF and NAN. In that case, |
| 359 | we ignore value-correctness and and output a syntacticly-valid |
| 360 | number. */ |
| 361 | if (value == SK_FloatInfinity) { |
| 362 | value = FLT_MAX; // nearest finite float. |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 363 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 364 | if (value == SK_FloatNegativeInfinity) { |
| 365 | value = -FLT_MAX; // nearest finite float. |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 366 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 367 | if (!std::isfinite(value) || value == 0.0f) { |
| 368 | // NAN is unsupported in PDF. Always output a valid number. |
| 369 | // Also catch zero here, as a special case. |
| 370 | *output++ = '0'; |
| 371 | *output = '\0'; |
| 372 | return output - result; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 373 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 374 | if (value < 0.0) { |
| 375 | *output++ = '-'; |
| 376 | value = -value; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 377 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 378 | SkASSERT(value >= 0.0f); |
| 379 | |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 380 | int binaryExponent; |
| 381 | (void)std::frexp(value, &binaryExponent); |
| 382 | static const double kLog2 = 0.3010299956639812; // log10(2.0); |
| 383 | int decimalExponent = static_cast<int>(std::floor(kLog2 * binaryExponent)); |
| 384 | int decimalShift = decimalExponent - 8; |
| 385 | double power = pow10(-decimalShift); |
| 386 | int32_t d = static_cast<int32_t>(value * power + 0.5); |
| 387 | // SkASSERT(value == (float)(d * pow(10.0, decimalShift))); |
| 388 | SkASSERT(d <= 999999999); |
| 389 | if (d > 167772159) { // floor(pow(10,1+log10(1<<24))) |
| 390 | // need one fewer decimal digits for 24-bit precision. |
| 391 | decimalShift = decimalExponent - 7; |
| 392 | // SkASSERT(power * 0.1 = pow10(-decimalShift)); |
| 393 | // recalculate to get rounding right. |
| 394 | d = static_cast<int32_t>(value * (power * 0.1) + 0.5); |
| 395 | SkASSERT(d <= 99999999); |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 396 | } |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 397 | while (d % 10 == 0) { |
| 398 | d /= 10; |
| 399 | ++decimalShift; |
| 400 | } |
| 401 | SkASSERT(d > 0); |
| 402 | // SkASSERT(value == (float)(d * pow(10.0, decimalShift))); |
| 403 | uint8_t buffer[9]; // decimal value buffer. |
| 404 | int bufferIndex = 0; |
| 405 | do { |
| 406 | buffer[bufferIndex++] = d % 10; |
| 407 | d /= 10; |
| 408 | } while (d != 0); |
| 409 | SkASSERT(bufferIndex <= (int)sizeof(buffer) && bufferIndex > 0); |
| 410 | if (decimalShift >= 0) { |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 411 | do { |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 412 | --bufferIndex; |
| 413 | *output++ = '0' + buffer[bufferIndex]; |
| 414 | } while (bufferIndex); |
| 415 | for (int i = 0; i < decimalShift; ++i) { |
| 416 | *output++ = '0'; |
| 417 | } |
| 418 | } else { |
| 419 | int placesBeforeDecimal = bufferIndex + decimalShift; |
| 420 | if (placesBeforeDecimal > 0) { |
| 421 | while (placesBeforeDecimal-- > 0) { |
| 422 | --bufferIndex; |
| 423 | *output++ = '0' + buffer[bufferIndex]; |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 424 | } |
halcanary | 56586b6 | 2016-07-25 13:59:30 -0700 | [diff] [blame] | 425 | *output++ = '.'; |
| 426 | } else { |
| 427 | *output++ = '.'; |
| 428 | int placesAfterDecimal = -placesBeforeDecimal; |
| 429 | while (placesAfterDecimal-- > 0) { |
| 430 | *output++ = '0'; |
| 431 | } |
| 432 | } |
| 433 | while (bufferIndex > 0) { |
| 434 | --bufferIndex; |
| 435 | *output++ = '0' + buffer[bufferIndex]; |
| 436 | if (output == end) { |
| 437 | break; // denormalized: don't need extra precision. |
| 438 | // Note: denormalized numbers will not have the same number of |
| 439 | // significantDigits, but do not need them to round-trip. |
| 440 | } |
| 441 | } |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 442 | } |
| 443 | SkASSERT(output <= end); |
| 444 | *output = '\0'; |
| 445 | return output - result; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 446 | } |
| 447 | |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 448 | void SkPDFUtils::WriteString(SkWStream* wStream, const char* cin, size_t len) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 449 | SkDEBUGCODE(static const size_t kMaxLen = 65535;) |
| 450 | SkASSERT(len <= kMaxLen); |
| 451 | |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 452 | size_t extraCharacterCount = 0; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 453 | for (size_t i = 0; i < len; i++) { |
| 454 | if (cin[i] > '~' || cin[i] < ' ') { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 455 | extraCharacterCount += 3; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 456 | } |
| 457 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 458 | ++extraCharacterCount; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 461 | if (extraCharacterCount <= len) { |
| 462 | wStream->writeText("("); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 463 | for (size_t i = 0; i < len; i++) { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 464 | if (cin[i] > '~' || cin[i] < ' ') { |
| 465 | uint8_t c = static_cast<uint8_t>(cin[i]); |
| 466 | uint8_t octal[4]; |
| 467 | octal[0] = '\\'; |
| 468 | octal[1] = '0' + ( c >> 6 ); |
| 469 | octal[2] = '0' + ((c >> 3) & 0x07); |
| 470 | octal[3] = '0' + ( c & 0x07); |
| 471 | wStream->write(octal, 4); |
| 472 | } else { |
| 473 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
| 474 | wStream->writeText("\\"); |
| 475 | } |
| 476 | wStream->write(&cin[i], 1); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 477 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 478 | } |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 479 | wStream->writeText(")"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 480 | } else { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 481 | wStream->writeText("<"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 482 | for (size_t i = 0; i < len; i++) { |
| 483 | uint8_t c = static_cast<uint8_t>(cin[i]); |
| 484 | static const char gHex[] = "0123456789ABCDEF"; |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 485 | char hexValue[2]; |
| 486 | hexValue[0] = gHex[(c >> 4) & 0xF]; |
| 487 | hexValue[1] = gHex[ c & 0xF]; |
| 488 | wStream->write(hexValue, 2); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 489 | } |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 490 | wStream->writeText(">"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 491 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 492 | } |