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 | |
Hal Canary | 4f29c20 | 2017-07-18 10:28:31 -0400 | [diff] [blame] | 8 | #include "SkPDFUtils.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 11 | #include "SkFixed.h" |
reed@google.com | a44ea51 | 2011-07-27 18:24:25 +0000 | [diff] [blame] | 12 | #include "SkGeometry.h" |
Hal Canary | 4f29c20 | 2017-07-18 10:28:31 -0400 | [diff] [blame] | 13 | #include "SkImage_Base.h" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 14 | #include "SkPDFResourceDict.h" |
Hal Canary | 4f29c20 | 2017-07-18 10:28:31 -0400 | [diff] [blame] | 15 | #include "SkPDFTypes.h" |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 17 | #include "SkString.h" |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 18 | |
halcanary | 8e9f5e3 | 2016-02-24 15:46:46 -0800 | [diff] [blame] | 19 | #include <cmath> |
| 20 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 21 | sk_sp<SkPDFArray> SkPDFUtils::RectToArray(const SkRect& rect) { |
| 22 | auto result = sk_make_sp<SkPDFArray>(); |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 23 | result->reserve(4); |
| 24 | result->appendScalar(rect.fLeft); |
| 25 | result->appendScalar(rect.fTop); |
| 26 | result->appendScalar(rect.fRight); |
| 27 | result->appendScalar(rect.fBottom); |
| 28 | return result; |
| 29 | } |
| 30 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 31 | sk_sp<SkPDFArray> SkPDFUtils::MatrixToArray(const SkMatrix& matrix) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 32 | SkScalar values[6]; |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 33 | if (!matrix.asAffine(values)) { |
| 34 | SkMatrix::SetAffineIdentity(values); |
| 35 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 36 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 37 | auto result = sk_make_sp<SkPDFArray>(); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 38 | result->reserve(6); |
| 39 | for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 40 | result->appendScalar(values[i]); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 41 | } |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | // static |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 46 | void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) { |
| 47 | SkScalar values[6]; |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 48 | if (!matrix.asAffine(values)) { |
| 49 | SkMatrix::SetAffineIdentity(values); |
| 50 | } |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 51 | for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 52 | SkPDFUtils::AppendScalar(values[i], content); |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 53 | content->writeText(" "); |
| 54 | } |
| 55 | content->writeText("cm\n"); |
| 56 | } |
| 57 | |
| 58 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 59 | void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 60 | SkPDFUtils::AppendScalar(x, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 61 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 62 | SkPDFUtils::AppendScalar(y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 63 | content->writeText(" m\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 67 | void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 68 | SkPDFUtils::AppendScalar(x, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 69 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 70 | SkPDFUtils::AppendScalar(y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 71 | content->writeText(" l\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // static |
| 75 | void SkPDFUtils::AppendCubic(SkScalar ctl1X, SkScalar ctl1Y, |
| 76 | SkScalar ctl2X, SkScalar ctl2Y, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 77 | SkScalar dstX, SkScalar dstY, SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 78 | SkString cmd("y\n"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 79 | SkPDFUtils::AppendScalar(ctl1X, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 80 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 81 | SkPDFUtils::AppendScalar(ctl1Y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 82 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 83 | if (ctl2X != dstX || ctl2Y != dstY) { |
| 84 | cmd.set("c\n"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 85 | SkPDFUtils::AppendScalar(ctl2X, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 86 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 87 | SkPDFUtils::AppendScalar(ctl2Y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 88 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 89 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 90 | SkPDFUtils::AppendScalar(dstX, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 91 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 92 | SkPDFUtils::AppendScalar(dstY, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 93 | content->writeText(" "); |
| 94 | content->writeText(cmd.c_str()); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 97 | static void append_quad(const SkPoint quad[], SkWStream* content) { |
| 98 | SkPoint cubic[4]; |
| 99 | SkConvertQuadToCubic(quad, cubic); |
| 100 | SkPDFUtils::AppendCubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY, |
| 101 | cubic[3].fX, cubic[3].fY, content); |
| 102 | } |
| 103 | |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 104 | // static |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 105 | void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) { |
| 106 | // Skia has 0,0 at top left, pdf at bottom left. Do the right thing. |
| 107 | SkScalar bottom = SkMinScalar(rect.fBottom, rect.fTop); |
| 108 | |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 109 | SkPDFUtils::AppendScalar(rect.fLeft, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 110 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 111 | SkPDFUtils::AppendScalar(bottom, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 112 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 113 | SkPDFUtils::AppendScalar(rect.width(), content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 114 | content->writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 115 | SkPDFUtils::AppendScalar(rect.height(), content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 116 | content->writeText(" re\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 117 | } |
| 118 | |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 119 | // static |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 120 | void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle, |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 121 | bool doConsumeDegerates, SkWStream* content, |
| 122 | SkScalar tolerance) { |
Hal Canary | 4e83ff1 | 2018-03-09 12:16:42 -0500 | [diff] [blame] | 123 | if (path.isEmpty() && SkPaint::kFill_Style == paintStyle) { |
| 124 | SkPDFUtils::AppendRectangle({0, 0, 0, 0}, content); |
| 125 | return; |
| 126 | } |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 127 | // Filling a path with no area results in a drawing in PDF renderers but |
| 128 | // Chrome expects to be able to draw some such entities with no visible |
| 129 | // result, so we detect those cases and discard the drawing for them. |
| 130 | // Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y). |
Hal Canary | 970225b | 2016-11-10 11:46:21 -0500 | [diff] [blame] | 131 | |
| 132 | SkRect rect; |
| 133 | bool isClosed; // Both closure and direction need to be checked. |
| 134 | SkPath::Direction direction; |
| 135 | if (path.isRect(&rect, &isClosed, &direction) && |
Hal Canary | fc3afa9 | 2017-07-19 14:19:14 -0400 | [diff] [blame] | 136 | isClosed && |
| 137 | (SkPath::kCW_Direction == direction || |
| 138 | SkPath::kEvenOdd_FillType == path.getFillType())) |
Hal Canary | 970225b | 2016-11-10 11:46:21 -0500 | [diff] [blame] | 139 | { |
| 140 | SkPDFUtils::AppendRectangle(rect, content); |
| 141 | return; |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 142 | } |
Hal Canary | 970225b | 2016-11-10 11:46:21 -0500 | [diff] [blame] | 143 | |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 144 | enum SkipFillState { |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 145 | kEmpty_SkipFillState, |
| 146 | kSingleLine_SkipFillState, |
| 147 | kNonSingleLine_SkipFillState, |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 148 | }; |
| 149 | SkipFillState fillState = kEmpty_SkipFillState; |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 150 | //if (paintStyle != SkPaint::kFill_Style) { |
| 151 | // fillState = kNonSingleLine_SkipFillState; |
| 152 | //} |
sugoi@google.com | e2e8113 | 2013-03-05 18:35:55 +0000 | [diff] [blame] | 153 | SkPoint lastMovePt = SkPoint::Make(0,0); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 154 | SkDynamicMemoryWStream currentSegment; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 155 | SkPoint args[4]; |
| 156 | SkPath::Iter iter(path, false); |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 157 | for (SkPath::Verb verb = iter.next(args, doConsumeDegerates); |
| 158 | verb != SkPath::kDone_Verb; |
| 159 | verb = iter.next(args, doConsumeDegerates)) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 160 | // args gets all the points, even the implicit first point. |
| 161 | switch (verb) { |
| 162 | case SkPath::kMove_Verb: |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 163 | MoveTo(args[0].fX, args[0].fY, ¤tSegment); |
| 164 | lastMovePt = args[0]; |
| 165 | fillState = kEmpty_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 166 | break; |
| 167 | case SkPath::kLine_Verb: |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 168 | AppendLine(args[1].fX, args[1].fY, ¤tSegment); |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 169 | if ((fillState == kEmpty_SkipFillState) && (args[0] != lastMovePt)) { |
| 170 | fillState = kSingleLine_SkipFillState; |
| 171 | break; |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 172 | } |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 173 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 174 | break; |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 175 | case SkPath::kQuad_Verb: |
| 176 | append_quad(args, ¤tSegment); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 177 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 178 | break; |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 179 | case SkPath::kConic_Verb: { |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 180 | SkAutoConicToQuads converter; |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 181 | const SkPoint* quads = converter.computeQuads(args, iter.conicWeight(), tolerance); |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 182 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 183 | append_quad(&quads[i * 2], ¤tSegment); |
| 184 | } |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 185 | fillState = kNonSingleLine_SkipFillState; |
reed | 40c85e4 | 2015-01-05 10:01:25 -0800 | [diff] [blame] | 186 | } break; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 187 | case SkPath::kCubic_Verb: |
| 188 | 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] | 189 | args[3].fX, args[3].fY, ¤tSegment); |
| 190 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 191 | break; |
| 192 | case SkPath::kClose_Verb: |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 193 | ClosePath(¤tSegment); |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 194 | currentSegment.writeToStream(content); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 195 | currentSegment.reset(); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 196 | break; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 197 | default: |
| 198 | SkASSERT(false); |
| 199 | break; |
| 200 | } |
| 201 | } |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 202 | if (currentSegment.bytesWritten() > 0) { |
halcanary | 7af2150 | 2015-02-23 12:17:59 -0800 | [diff] [blame] | 203 | currentSegment.writeToStream(content); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 204 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 208 | void SkPDFUtils::ClosePath(SkWStream* content) { |
| 209 | content->writeText("h\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // static |
| 213 | void SkPDFUtils::PaintPath(SkPaint::Style style, SkPath::FillType fill, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 214 | SkWStream* content) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 215 | if (style == SkPaint::kFill_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 216 | content->writeText("f"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 217 | } else if (style == SkPaint::kStrokeAndFill_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 218 | content->writeText("B"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 219 | } else if (style == SkPaint::kStroke_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 220 | content->writeText("S"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 221 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 222 | |
| 223 | if (style != SkPaint::kStroke_Style) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 224 | NOT_IMPLEMENTED(fill == SkPath::kInverseEvenOdd_FillType, false); |
| 225 | NOT_IMPLEMENTED(fill == SkPath::kInverseWinding_FillType, false); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 226 | if (fill == SkPath::kEvenOdd_FillType) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 227 | content->writeText("*"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 228 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 229 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 230 | content->writeText("\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 234 | void SkPDFUtils::StrokePath(SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 235 | SkPDFUtils::PaintPath( |
| 236 | SkPaint::kStroke_Style, SkPath::kWinding_FillType, content); |
| 237 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 238 | |
| 239 | // static |
| 240 | void SkPDFUtils::DrawFormXObject(int objectIndex, SkWStream* content) { |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 241 | content->writeText("/"); |
| 242 | content->writeText(SkPDFResourceDict::getResourceName( |
| 243 | SkPDFResourceDict::kXObject_ResourceType, |
| 244 | objectIndex).c_str()); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 245 | content->writeText(" Do\n"); |
| 246 | } |
| 247 | |
| 248 | // static |
| 249 | void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) { |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 250 | content->writeText("/"); |
| 251 | content->writeText(SkPDFResourceDict::getResourceName( |
| 252 | SkPDFResourceDict::kExtGState_ResourceType, |
| 253 | objectIndex).c_str()); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 254 | content->writeText(" gs\n"); |
| 255 | } |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 256 | |
| 257 | // static |
| 258 | void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) { |
| 259 | // Select Pattern color space (CS, cs) and set pattern object as current |
| 260 | // color (SCN, scn) |
| 261 | SkString resourceName = SkPDFResourceDict::getResourceName( |
| 262 | SkPDFResourceDict::kPattern_ResourceType, |
| 263 | objectIndex); |
| 264 | content->writeText("/Pattern CS/Pattern cs/"); |
| 265 | content->writeText(resourceName.c_str()); |
| 266 | content->writeText(" SCN/"); |
| 267 | content->writeText(resourceName.c_str()); |
| 268 | content->writeText(" scn\n"); |
| 269 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 270 | |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 271 | size_t SkPDFUtils::ColorToDecimal(uint8_t value, char result[5]) { |
| 272 | if (value == 255 || value == 0) { |
| 273 | result[0] = value ? '1' : '0'; |
| 274 | result[1] = '\0'; |
| 275 | return 1; |
| 276 | } |
| 277 | // int x = 0.5 + (1000.0 / 255.0) * value; |
| 278 | int x = SkFixedRoundToInt((SK_Fixed1 * 1000 / 255) * value); |
| 279 | result[0] = '.'; |
| 280 | for (int i = 3; i > 0; --i) { |
| 281 | result[i] = '0' + x % 10; |
| 282 | x /= 10; |
| 283 | } |
| 284 | int j; |
| 285 | for (j = 3; j > 1; --j) { |
| 286 | if (result[j] != '0') { |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | result[j + 1] = '\0'; |
| 291 | return j + 1; |
| 292 | } |
| 293 | |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 294 | void SkPDFUtils::WriteString(SkWStream* wStream, const char* cin, size_t len) { |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 295 | SkDEBUGCODE(static const size_t kMaxLen = 65535;) |
| 296 | SkASSERT(len <= kMaxLen); |
| 297 | |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 298 | size_t extraCharacterCount = 0; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 299 | for (size_t i = 0; i < len; i++) { |
| 300 | if (cin[i] > '~' || cin[i] < ' ') { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 301 | extraCharacterCount += 3; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 302 | } |
| 303 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 304 | ++extraCharacterCount; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 307 | if (extraCharacterCount <= len) { |
| 308 | wStream->writeText("("); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 309 | for (size_t i = 0; i < len; i++) { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 310 | if (cin[i] > '~' || cin[i] < ' ') { |
| 311 | uint8_t c = static_cast<uint8_t>(cin[i]); |
| 312 | uint8_t octal[4]; |
| 313 | octal[0] = '\\'; |
| 314 | octal[1] = '0' + ( c >> 6 ); |
| 315 | octal[2] = '0' + ((c >> 3) & 0x07); |
| 316 | octal[3] = '0' + ( c & 0x07); |
| 317 | wStream->write(octal, 4); |
| 318 | } else { |
| 319 | if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') { |
| 320 | wStream->writeText("\\"); |
| 321 | } |
| 322 | wStream->write(&cin[i], 1); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 323 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 324 | } |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 325 | wStream->writeText(")"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 326 | } else { |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 327 | wStream->writeText("<"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 328 | for (size_t i = 0; i < len; i++) { |
| 329 | uint8_t c = static_cast<uint8_t>(cin[i]); |
Hal Canary | d6e6e66 | 2017-06-17 10:38:13 -0400 | [diff] [blame] | 330 | char hexValue[2] = { SkHexadecimalDigits::gUpper[c >> 4], |
| 331 | SkHexadecimalDigits::gUpper[c & 0xF] }; |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 332 | wStream->write(hexValue, 2); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 333 | } |
halcanary | ee41b75 | 2016-06-23 14:08:11 -0700 | [diff] [blame] | 334 | wStream->writeText(">"); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 335 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 336 | } |
Hal Canary | 94fd66c | 2017-07-05 11:25:42 -0400 | [diff] [blame] | 337 | |
| 338 | bool SkPDFUtils::InverseTransformBBox(const SkMatrix& matrix, SkRect* bbox) { |
| 339 | SkMatrix inverse; |
| 340 | if (!matrix.invert(&inverse)) { |
| 341 | return false; |
| 342 | } |
| 343 | inverse.mapRect(bbox); |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | void SkPDFUtils::PopulateTilingPatternDict(SkPDFDict* pattern, |
| 348 | SkRect& bbox, |
| 349 | sk_sp<SkPDFDict> resources, |
| 350 | const SkMatrix& matrix) { |
| 351 | const int kTiling_PatternType = 1; |
| 352 | const int kColoredTilingPattern_PaintType = 1; |
| 353 | const int kConstantSpacing_TilingType = 1; |
| 354 | |
| 355 | pattern->insertName("Type", "Pattern"); |
| 356 | pattern->insertInt("PatternType", kTiling_PatternType); |
| 357 | pattern->insertInt("PaintType", kColoredTilingPattern_PaintType); |
| 358 | pattern->insertInt("TilingType", kConstantSpacing_TilingType); |
| 359 | pattern->insertObject("BBox", SkPDFUtils::RectToArray(bbox)); |
| 360 | pattern->insertScalar("XStep", bbox.width()); |
| 361 | pattern->insertScalar("YStep", bbox.height()); |
| 362 | pattern->insertObject("Resources", std::move(resources)); |
| 363 | if (!matrix.isIdentity()) { |
| 364 | pattern->insertObject("Matrix", SkPDFUtils::MatrixToArray(matrix)); |
| 365 | } |
| 366 | } |
Hal Canary | 4f29c20 | 2017-07-18 10:28:31 -0400 | [diff] [blame] | 367 | |
| 368 | bool SkPDFUtils::ToBitmap(const SkImage* img, SkBitmap* dst) { |
| 369 | SkASSERT(img); |
| 370 | SkASSERT(dst); |
| 371 | SkBitmap bitmap; |
| 372 | if(as_IB(img)->getROPixels(&bitmap, nullptr)) { |
| 373 | SkASSERT(bitmap.dimensions() == img->dimensions()); |
| 374 | SkASSERT(!bitmap.drawsNothing()); |
| 375 | *dst = std::move(bitmap); |
| 376 | return true; |
| 377 | } |
| 378 | return false; |
| 379 | } |