epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +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. |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
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" |
reed@google.com | a44ea51 | 2011-07-27 18:24:25 +0000 | [diff] [blame] | 11 | #include "SkGeometry.h" |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 12 | #include "SkPaint.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkPDFUtils.h" |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 15 | #include "SkStream.h" |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 16 | #include "SkString.h" |
| 17 | #include "SkPDFTypes.h" |
| 18 | |
| 19 | // static |
| 20 | SkPDFArray* SkPDFUtils::MatrixToArray(const SkMatrix& matrix) { |
| 21 | SkScalar values[6]; |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 22 | if (!matrix.asAffine(values)) { |
| 23 | SkMatrix::SetAffineIdentity(values); |
| 24 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 25 | |
| 26 | SkPDFArray* result = new SkPDFArray; |
| 27 | result->reserve(6); |
| 28 | for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) { |
reed@google.com | c789cf1 | 2011-07-20 12:14:33 +0000 | [diff] [blame] | 29 | result->appendScalar(values[i]); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 30 | } |
| 31 | return result; |
| 32 | } |
| 33 | |
| 34 | // static |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 35 | void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) { |
| 36 | SkScalar values[6]; |
bungeman@google.com | 1ddd7c3 | 2011-07-13 19:41:55 +0000 | [diff] [blame] | 37 | if (!matrix.asAffine(values)) { |
| 38 | SkMatrix::SetAffineIdentity(values); |
| 39 | } |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 40 | for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) { |
| 41 | SkPDFScalar::Append(values[i], content); |
| 42 | content->writeText(" "); |
| 43 | } |
| 44 | content->writeText("cm\n"); |
| 45 | } |
| 46 | |
| 47 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 48 | void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 49 | SkPDFScalar::Append(x, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 50 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 51 | SkPDFScalar::Append(y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 52 | content->writeText(" m\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 56 | void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 57 | SkPDFScalar::Append(x, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 58 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 59 | SkPDFScalar::Append(y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 60 | content->writeText(" l\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // static |
| 64 | void SkPDFUtils::AppendCubic(SkScalar ctl1X, SkScalar ctl1Y, |
| 65 | SkScalar ctl2X, SkScalar ctl2Y, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 66 | SkScalar dstX, SkScalar dstY, SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 67 | SkString cmd("y\n"); |
| 68 | SkPDFScalar::Append(ctl1X, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 69 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 70 | SkPDFScalar::Append(ctl1Y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 71 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 72 | if (ctl2X != dstX || ctl2Y != dstY) { |
| 73 | cmd.set("c\n"); |
| 74 | SkPDFScalar::Append(ctl2X, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 75 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 76 | SkPDFScalar::Append(ctl2Y, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 77 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 78 | } |
| 79 | SkPDFScalar::Append(dstX, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 80 | content->writeText(" "); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 81 | SkPDFScalar::Append(dstY, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 82 | content->writeText(" "); |
| 83 | content->writeText(cmd.c_str()); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // static |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 87 | void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) { |
| 88 | // Skia has 0,0 at top left, pdf at bottom left. Do the right thing. |
| 89 | SkScalar bottom = SkMinScalar(rect.fBottom, rect.fTop); |
| 90 | |
| 91 | SkPDFScalar::Append(rect.fLeft, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 92 | content->writeText(" "); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 93 | SkPDFScalar::Append(bottom, content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 94 | content->writeText(" "); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 95 | SkPDFScalar::Append(rect.width(), content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 96 | content->writeText(" "); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 97 | SkPDFScalar::Append(rect.height(), content); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 98 | content->writeText(" re\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 99 | } |
| 100 | |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 101 | // static |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame^] | 102 | void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle, |
| 103 | SkWStream* content) { |
| 104 | // Filling a path with no area results in a drawing in PDF renderers but |
| 105 | // Chrome expects to be able to draw some such entities with no visible |
| 106 | // result, so we detect those cases and discard the drawing for them. |
| 107 | // Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y). |
| 108 | enum SkipFillState { |
| 109 | kEmpty_SkipFillState = 0, |
| 110 | kSingleLine_SkipFillState = 1, |
| 111 | kNonSingleLine_SkipFillState = 2, |
| 112 | }; |
| 113 | SkipFillState fillState = kEmpty_SkipFillState; |
| 114 | if (paintStyle != SkPaint::kFill_Style) { |
| 115 | fillState = kNonSingleLine_SkipFillState; |
| 116 | } |
| 117 | SkPoint lastMovePt; |
| 118 | SkDynamicMemoryWStream currentSegment; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 119 | SkPoint args[4]; |
| 120 | SkPath::Iter iter(path, false); |
| 121 | for (SkPath::Verb verb = iter.next(args); |
| 122 | verb != SkPath::kDone_Verb; |
| 123 | verb = iter.next(args)) { |
| 124 | // args gets all the points, even the implicit first point. |
| 125 | switch (verb) { |
| 126 | case SkPath::kMove_Verb: |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame^] | 127 | MoveTo(args[0].fX, args[0].fY, ¤tSegment); |
| 128 | lastMovePt = args[0]; |
| 129 | fillState = kEmpty_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 130 | break; |
| 131 | case SkPath::kLine_Verb: |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame^] | 132 | AppendLine(args[1].fX, args[1].fY, ¤tSegment); |
| 133 | if (fillState == kEmpty_SkipFillState) { |
| 134 | if (args[0] != lastMovePt) { |
| 135 | fillState = kSingleLine_SkipFillState; |
| 136 | } |
| 137 | } else if (fillState == kSingleLine_SkipFillState) { |
| 138 | fillState = kNonSingleLine_SkipFillState; |
| 139 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 140 | break; |
| 141 | case SkPath::kQuad_Verb: { |
reed@google.com | a44ea51 | 2011-07-27 18:24:25 +0000 | [diff] [blame] | 142 | SkPoint cubic[4]; |
| 143 | SkConvertQuadToCubic(args, cubic); |
| 144 | AppendCubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY, |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame^] | 145 | cubic[3].fX, cubic[3].fY, ¤tSegment); |
| 146 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | case SkPath::kCubic_Verb: |
| 150 | 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^] | 151 | args[3].fX, args[3].fY, ¤tSegment); |
| 152 | fillState = kNonSingleLine_SkipFillState; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 153 | break; |
| 154 | case SkPath::kClose_Verb: |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame^] | 155 | if (fillState != kSingleLine_SkipFillState) { |
| 156 | ClosePath(¤tSegment); |
| 157 | SkData* data = currentSegment.copyToData(); |
| 158 | content->write(data->data(), data->size()); |
| 159 | data->unref(); |
| 160 | } |
| 161 | currentSegment.reset(); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 162 | break; |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 163 | default: |
| 164 | SkASSERT(false); |
| 165 | break; |
| 166 | } |
| 167 | } |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame^] | 168 | if (currentSegment.bytesWritten() > 0) { |
| 169 | SkData* data = currentSegment.copyToData(); |
| 170 | content->write(data->data(), data->size()); |
| 171 | data->unref(); |
| 172 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 176 | void SkPDFUtils::ClosePath(SkWStream* content) { |
| 177 | content->writeText("h\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // static |
| 181 | void SkPDFUtils::PaintPath(SkPaint::Style style, SkPath::FillType fill, |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 182 | SkWStream* content) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 183 | if (style == SkPaint::kFill_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 184 | content->writeText("f"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 185 | } else if (style == SkPaint::kStrokeAndFill_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 186 | content->writeText("B"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 187 | } else if (style == SkPaint::kStroke_Style) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 188 | content->writeText("S"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 189 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 190 | |
| 191 | if (style != SkPaint::kStroke_Style) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 192 | NOT_IMPLEMENTED(fill == SkPath::kInverseEvenOdd_FillType, false); |
| 193 | NOT_IMPLEMENTED(fill == SkPath::kInverseWinding_FillType, false); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 194 | if (fill == SkPath::kEvenOdd_FillType) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 195 | content->writeText("*"); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 196 | } |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 197 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 198 | content->writeText("\n"); |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // static |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 202 | void SkPDFUtils::StrokePath(SkWStream* content) { |
ctguil@chromium.org | f966fd3 | 2011-03-04 21:47:04 +0000 | [diff] [blame] | 203 | SkPDFUtils::PaintPath( |
| 204 | SkPaint::kStroke_Style, SkPath::kWinding_FillType, content); |
| 205 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 206 | |
| 207 | // static |
| 208 | void SkPDFUtils::DrawFormXObject(int objectIndex, SkWStream* content) { |
| 209 | content->writeText("/X"); |
| 210 | content->writeDecAsText(objectIndex); |
| 211 | content->writeText(" Do\n"); |
| 212 | } |
| 213 | |
| 214 | // static |
| 215 | void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) { |
| 216 | content->writeText("/G"); |
| 217 | content->writeDecAsText(objectIndex); |
| 218 | content->writeText(" gs\n"); |
| 219 | } |