vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +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. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "SkPDFDevice.h" |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 9 | |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 10 | #include "SkAdvancedTypefaceMetrics.h" |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 11 | #include "SkAnnotationKeys.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 12 | #include "SkBitmapDevice.h" |
martina.kollarova | b8d6af1 | 2016-06-29 05:12:31 -0700 | [diff] [blame] | 13 | #include "SkBitmapKey.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 14 | #include "SkColor.h" |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 15 | #include "SkColorFilter.h" |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 16 | #include "SkDraw.h" |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 17 | #include "SkDrawFilter.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 18 | #include "SkGlyphCache.h" |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 19 | #include "SkImageFilterCache.h" |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 20 | #include "SkMakeUnique.h" |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 21 | #include "SkPath.h" |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 22 | #include "SkPathEffect.h" |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 23 | #include "SkPathOps.h" |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 24 | #include "SkPDFBitmap.h" |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 25 | #include "SkPDFCanon.h" |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 26 | #include "SkPDFDocument.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 27 | #include "SkPDFFont.h" |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 28 | #include "SkPDFFormXObject.h" |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 29 | #include "SkPDFGraphicState.h" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 30 | #include "SkPDFResourceDict.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 31 | #include "SkPDFShader.h" |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 32 | #include "SkPDFTypes.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 33 | #include "SkPDFUtils.h" |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 34 | #include "SkPixelRef.h" |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 35 | #include "SkRasterClip.h" |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 36 | #include "SkRRect.h" |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 37 | #include "SkScopeExit.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 38 | #include "SkString.h" |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 39 | #include "SkSurface.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 40 | #include "SkTemplates.h" |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 41 | #include "SkTextBlobRunIterator.h" |
| 42 | #include "SkTextFormatParams.h" |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 43 | #include "SkUtils.h" |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 44 | #include "SkXfermodeInterpretation.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 45 | #include "SkClipOpPriv.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 46 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 47 | #define DPI_FOR_RASTER_SCALE_ONE 72 |
| 48 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 49 | // Utility functions |
| 50 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 51 | // If the paint will definitely draw opaquely, replace kSrc with |
| 52 | // kSrcOver. http://crbug.com/473572 |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 53 | static void replace_srcmode_on_opaque_paint(SkPaint* paint) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 54 | if (kSrcOver_SkXfermodeInterpretation == SkInterpretXfermode(*paint, false)) { |
| 55 | paint->setBlendMode(SkBlendMode::kSrcOver); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 59 | static void emit_pdf_color(SkColor color, SkWStream* result) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 60 | SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 61 | SkPDFUtils::AppendColorComponent(SkColorGetR(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 62 | result->writeText(" "); |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 63 | SkPDFUtils::AppendColorComponent(SkColorGetG(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 64 | result->writeText(" "); |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 65 | SkPDFUtils::AppendColorComponent(SkColorGetB(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 66 | result->writeText(" "); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 67 | } |
| 68 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 69 | static SkPaint calculate_text_paint(const SkPaint& paint) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 70 | SkPaint result = paint; |
| 71 | if (result.isFakeBoldText()) { |
| 72 | SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(), |
| 73 | kStdFakeBoldInterpKeys, |
| 74 | kStdFakeBoldInterpValues, |
| 75 | kStdFakeBoldInterpLength); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 76 | SkScalar width = result.getTextSize() * fakeBoldScale; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 77 | if (result.getStyle() == SkPaint::kFill_Style) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 78 | result.setStyle(SkPaint::kStrokeAndFill_Style); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 79 | } else { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 80 | width += result.getStrokeWidth(); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 81 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 82 | result.setStrokeWidth(width); |
| 83 | } |
| 84 | return result; |
| 85 | } |
| 86 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 87 | static SkImageSubset make_image_subset(const SkBitmap& bitmap) { |
| 88 | SkASSERT(!bitmap.drawsNothing()); |
| 89 | SkIRect subset = bitmap.getSubset(); |
| 90 | SkAutoLockPixels autoLockPixels(bitmap); |
| 91 | SkASSERT(bitmap.pixelRef()); |
| 92 | SkBitmap tmp; |
| 93 | tmp.setInfo(bitmap.pixelRef()->info(), bitmap.rowBytes()); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 94 | tmp.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0); |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 95 | tmp.lockPixels(); |
| 96 | auto img = SkImage::MakeFromBitmap(tmp); |
| 97 | if (img) { |
| 98 | SkASSERT(!bitmap.isImmutable() || img->uniqueID() == bitmap.getGenerationID()); |
| 99 | SkASSERT(img->bounds().contains(subset)); |
| 100 | } |
| 101 | SkImageSubset imageSubset(std::move(img), subset); |
| 102 | // SkImage::MakeFromBitmap only preserves genID for immutable |
| 103 | // bitmaps. Use the bitmap's original ID for de-duping. |
| 104 | imageSubset.setID(bitmap.getGenerationID()); |
| 105 | return imageSubset; |
| 106 | } |
| 107 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 108 | SkPDFDevice::GraphicStateEntry::GraphicStateEntry() |
| 109 | : fColor(SK_ColorBLACK) |
| 110 | , fTextScaleX(SK_Scalar1) |
| 111 | , fTextFill(SkPaint::kFill_Style) |
| 112 | , fShaderIndex(-1) |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 113 | , fGraphicStateIndex(-1) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 114 | fMatrix.reset(); |
| 115 | } |
| 116 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 117 | bool SkPDFDevice::GraphicStateEntry::compareInitialState( |
| 118 | const GraphicStateEntry& cur) { |
commit-bot@chromium.org | b000d76 | 2014-02-07 19:39:57 +0000 | [diff] [blame] | 119 | return fColor == cur.fColor && |
| 120 | fShaderIndex == cur.fShaderIndex && |
| 121 | fGraphicStateIndex == cur.fGraphicStateIndex && |
| 122 | fMatrix == cur.fMatrix && |
| 123 | fClipStack == cur.fClipStack && |
| 124 | (fTextScaleX == 0 || |
| 125 | (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 126 | } |
| 127 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 128 | class GraphicStackState { |
| 129 | public: |
| 130 | GraphicStackState(const SkClipStack& existingClipStack, |
| 131 | const SkRegion& existingClipRegion, |
| 132 | SkWStream* contentStream) |
| 133 | : fStackDepth(0), |
| 134 | fContentStream(contentStream) { |
| 135 | fEntries[0].fClipStack = existingClipStack; |
| 136 | fEntries[0].fClipRegion = existingClipRegion; |
| 137 | } |
| 138 | |
| 139 | void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion, |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 140 | const SkPoint& translation, const SkRect& bounds); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 141 | void updateMatrix(const SkMatrix& matrix); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 142 | void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 143 | |
| 144 | void drainStack(); |
| 145 | |
| 146 | private: |
| 147 | void push(); |
| 148 | void pop(); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 149 | SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; } |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 150 | |
| 151 | // Conservative limit on save depth, see impl. notes in PDF 1.4 spec. |
| 152 | static const int kMaxStackDepth = 12; |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 153 | SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1]; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 154 | int fStackDepth; |
| 155 | SkWStream* fContentStream; |
| 156 | }; |
| 157 | |
| 158 | void GraphicStackState::drainStack() { |
| 159 | while (fStackDepth) { |
| 160 | pop(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void GraphicStackState::push() { |
| 165 | SkASSERT(fStackDepth < kMaxStackDepth); |
| 166 | fContentStream->writeText("q\n"); |
| 167 | fStackDepth++; |
| 168 | fEntries[fStackDepth] = fEntries[fStackDepth - 1]; |
| 169 | } |
| 170 | |
| 171 | void GraphicStackState::pop() { |
| 172 | SkASSERT(fStackDepth > 0); |
| 173 | fContentStream->writeText("Q\n"); |
| 174 | fStackDepth--; |
| 175 | } |
| 176 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 177 | /* Calculate an inverted path's equivalent non-inverted path, given the |
| 178 | * canvas bounds. |
| 179 | * outPath may alias with invPath (since this is supported by PathOps). |
| 180 | */ |
| 181 | static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath, |
| 182 | SkPath* outPath) { |
| 183 | SkASSERT(invPath.isInverseFillType()); |
| 184 | |
| 185 | SkPath clipPath; |
| 186 | clipPath.addRect(bounds); |
| 187 | |
reed | cdb42bb | 2015-06-26 10:23:07 -0700 | [diff] [blame] | 188 | return Op(clipPath, invPath, kIntersect_SkPathOp, outPath); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 191 | bool apply_clip(SkClipOp op, const SkPath& u, const SkPath& v, SkPath* r) { |
| 192 | switch (op) { |
| 193 | case SkClipOp::kDifference: |
| 194 | return Op(u, v, kDifference_SkPathOp, r); |
| 195 | case SkClipOp::kIntersect: |
| 196 | return Op(u, v, kIntersect_SkPathOp, r); |
| 197 | case SkClipOp::kUnion_deprecated: |
| 198 | return Op(u, v, kUnion_SkPathOp, r); |
| 199 | case SkClipOp::kXOR_deprecated: |
| 200 | return Op(u, v, kXOR_SkPathOp, r); |
| 201 | case SkClipOp::kReverseDifference_deprecated: |
| 202 | return Op(u, v, kReverseDifference_SkPathOp, r); |
| 203 | case SkClipOp::kReplace_deprecated: |
| 204 | *r = v; |
| 205 | return true; |
| 206 | default: |
| 207 | return false; |
| 208 | } |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* Uses Path Ops to calculate a vector SkPath clip from a clip stack. |
| 212 | * Returns true if successful, or false if not successful. |
| 213 | * If successful, the resulting clip is stored in outClipPath. |
| 214 | * If not successful, outClipPath is undefined, and a fallback method |
| 215 | * should be used. |
| 216 | */ |
| 217 | static bool get_clip_stack_path(const SkMatrix& transform, |
| 218 | const SkClipStack& clipStack, |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 219 | const SkRect& bounds, |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 220 | SkPath* outClipPath) { |
| 221 | outClipPath->reset(); |
| 222 | outClipPath->setFillType(SkPath::kInverseWinding_FillType); |
| 223 | |
| 224 | const SkClipStack::Element* clipEntry; |
| 225 | SkClipStack::Iter iter; |
| 226 | iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 227 | for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) { |
| 228 | SkPath entryPath; |
| 229 | if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) { |
| 230 | outClipPath->reset(); |
| 231 | outClipPath->setFillType(SkPath::kInverseWinding_FillType); |
| 232 | continue; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 233 | } else { |
| 234 | clipEntry->asPath(&entryPath); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 235 | } |
| 236 | entryPath.transform(transform); |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 237 | if (!apply_clip(clipEntry->getOp(), *outClipPath, entryPath, outClipPath)) { |
| 238 | return false; |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | if (outClipPath->isInverseFillType()) { |
| 243 | // The bounds are slightly outset to ensure this is correct in the |
| 244 | // face of floating-point accuracy and possible SkRegion bitmap |
| 245 | // approximations. |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 246 | SkRect clipBounds = bounds; |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 247 | clipBounds.outset(SK_Scalar1, SK_Scalar1); |
| 248 | if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) { |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | return true; |
| 253 | } |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 254 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 255 | // TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 256 | // graphic state stack, and the fact that we can know all the clips used |
| 257 | // on the page to optimize this. |
| 258 | void GraphicStackState::updateClip(const SkClipStack& clipStack, |
| 259 | const SkRegion& clipRegion, |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 260 | const SkPoint& translation, |
| 261 | const SkRect& bounds) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 262 | if (clipStack == currentEntry()->fClipStack) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | while (fStackDepth > 0) { |
| 267 | pop(); |
| 268 | if (clipStack == currentEntry()->fClipStack) { |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | push(); |
| 273 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 274 | currentEntry()->fClipStack = clipStack; |
| 275 | currentEntry()->fClipRegion = clipRegion; |
| 276 | |
| 277 | SkMatrix transform; |
| 278 | transform.setTranslate(translation.fX, translation.fY); |
| 279 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 280 | SkPath clipPath; |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 281 | if (get_clip_stack_path(transform, clipStack, bounds, &clipPath)) { |
halcanary | 5edf290 | 2016-09-07 09:05:25 -0700 | [diff] [blame] | 282 | SkPDFUtils::EmitPath(clipPath, SkPaint::kFill_Style, fContentStream); |
| 283 | SkPath::FillType clipFill = clipPath.getFillType(); |
| 284 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false); |
| 285 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false); |
| 286 | if (clipFill == SkPath::kEvenOdd_FillType) { |
| 287 | fContentStream->writeText("W* n\n"); |
| 288 | } else { |
| 289 | fContentStream->writeText("W n\n"); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
halcanary | 5edf290 | 2016-09-07 09:05:25 -0700 | [diff] [blame] | 292 | // If Op() fails (pathological case; e.g. input values are |
| 293 | // extremely large or NaN), emit no clip at all. |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void GraphicStackState::updateMatrix(const SkMatrix& matrix) { |
| 297 | if (matrix == currentEntry()->fMatrix) { |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) { |
| 302 | SkASSERT(fStackDepth > 0); |
| 303 | SkASSERT(fEntries[fStackDepth].fClipStack == |
| 304 | fEntries[fStackDepth -1].fClipStack); |
| 305 | pop(); |
| 306 | |
| 307 | SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask); |
| 308 | } |
| 309 | if (matrix.getType() == SkMatrix::kIdentity_Mask) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | push(); |
| 314 | SkPDFUtils::AppendTransform(matrix, fContentStream); |
| 315 | currentEntry()->fMatrix = matrix; |
| 316 | } |
| 317 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 318 | void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 319 | // PDF treats a shader as a color, so we only set one or the other. |
| 320 | if (state.fShaderIndex >= 0) { |
| 321 | if (state.fShaderIndex != currentEntry()->fShaderIndex) { |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 322 | SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 323 | currentEntry()->fShaderIndex = state.fShaderIndex; |
| 324 | } |
| 325 | } else { |
| 326 | if (state.fColor != currentEntry()->fColor || |
| 327 | currentEntry()->fShaderIndex >= 0) { |
| 328 | emit_pdf_color(state.fColor, fContentStream); |
| 329 | fContentStream->writeText("RG "); |
| 330 | emit_pdf_color(state.fColor, fContentStream); |
| 331 | fContentStream->writeText("rg\n"); |
| 332 | currentEntry()->fColor = state.fColor; |
| 333 | currentEntry()->fShaderIndex = -1; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 338 | SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 339 | currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex; |
| 340 | } |
| 341 | |
| 342 | if (state.fTextScaleX) { |
| 343 | if (state.fTextScaleX != currentEntry()->fTextScaleX) { |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 344 | SkScalar pdfScale = state.fTextScaleX * 1000; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 345 | SkPDFUtils::AppendScalar(pdfScale, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 346 | fContentStream->writeText(" Tz\n"); |
| 347 | currentEntry()->fTextScaleX = state.fTextScaleX; |
| 348 | } |
| 349 | if (state.fTextFill != currentEntry()->fTextFill) { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 350 | static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value"); |
| 351 | static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value"); |
| 352 | static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value"); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 353 | fContentStream->writeDecAsText(state.fTextFill); |
| 354 | fContentStream->writeText(" Tr\n"); |
| 355 | currentEntry()->fTextFill = state.fTextFill; |
| 356 | } |
| 357 | } |
| 358 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 359 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 360 | static bool not_supported_for_layers(const SkPaint& layerPaint) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 361 | // PDF does not support image filters, so render them on CPU. |
| 362 | // Note that this rendering is done at "screen" resolution (100dpi), not |
| 363 | // printer resolution. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 364 | // TODO: It may be possible to express some filters natively using PDF |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 365 | // to improve quality and file size (https://bug.skia.org/3043) |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 366 | |
| 367 | // TODO: should we return true if there is a colorfilter? |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 368 | return layerPaint.getImageFilter() != nullptr; |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) { |
reed | cd4051e | 2016-07-15 09:41:26 -0700 | [diff] [blame] | 372 | if (layerPaint && not_supported_for_layers(*layerPaint)) { |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 373 | // need to return a raster device, which we will detect in drawDevice() |
| 374 | return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 375 | } |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 376 | SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height()); |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 377 | return SkPDFDevice::Create(size, fRasterDpi, fDocument); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 380 | SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); } |
| 381 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 382 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 383 | |
| 384 | // A helper class to automatically finish a ContentEntry at the end of a |
| 385 | // drawing method and maintain the state needed between set up and finish. |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 386 | class ScopedContentEntry { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 387 | public: |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 388 | ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw, |
| 389 | const SkPaint& paint, bool hasText = false) |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 390 | : fDevice(device), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 391 | fContentEntry(nullptr), |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 392 | fBlendMode(SkBlendMode::kSrcOver), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 393 | fDstFormXObject(nullptr) { |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 394 | init(draw.fClipStack, draw.fRC->bwRgn(), *draw.fMatrix, paint, hasText); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 395 | } |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 396 | ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack, |
| 397 | const SkRegion& clipRegion, const SkMatrix& matrix, |
| 398 | const SkPaint& paint, bool hasText = false) |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 399 | : fDevice(device), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 400 | fContentEntry(nullptr), |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 401 | fBlendMode(SkBlendMode::kSrcOver), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 402 | fDstFormXObject(nullptr) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 403 | init(clipStack, clipRegion, matrix, paint, hasText); |
| 404 | } |
| 405 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 406 | ~ScopedContentEntry() { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 407 | if (fContentEntry) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 408 | SkPath* shape = &fShape; |
| 409 | if (shape->isEmpty()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 410 | shape = nullptr; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 411 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 412 | fDevice->finishContentEntry(fBlendMode, std::move(fDstFormXObject), shape); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 416 | SkPDFDevice::ContentEntry* entry() { return fContentEntry; } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 417 | |
| 418 | /* Returns true when we explicitly need the shape of the drawing. */ |
| 419 | bool needShape() { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 420 | switch (fBlendMode) { |
| 421 | case SkBlendMode::kClear: |
| 422 | case SkBlendMode::kSrc: |
| 423 | case SkBlendMode::kSrcIn: |
| 424 | case SkBlendMode::kSrcOut: |
| 425 | case SkBlendMode::kDstIn: |
| 426 | case SkBlendMode::kDstOut: |
| 427 | case SkBlendMode::kSrcATop: |
| 428 | case SkBlendMode::kDstATop: |
| 429 | case SkBlendMode::kModulate: |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 430 | return true; |
| 431 | default: |
| 432 | return false; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /* Returns true unless we only need the shape of the drawing. */ |
| 437 | bool needSource() { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 438 | if (fBlendMode == SkBlendMode::kClear) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 439 | return false; |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | /* If the shape is different than the alpha component of the content, then |
| 445 | * setShape should be called with the shape. In particular, images and |
| 446 | * devices have rectangular shape. |
| 447 | */ |
| 448 | void setShape(const SkPath& shape) { |
| 449 | fShape = shape; |
| 450 | } |
| 451 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 452 | private: |
| 453 | SkPDFDevice* fDevice; |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 454 | SkPDFDevice::ContentEntry* fContentEntry; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 455 | SkBlendMode fBlendMode; |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 456 | sk_sp<SkPDFObject> fDstFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 457 | SkPath fShape; |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 458 | |
| 459 | void init(const SkClipStack* clipStack, const SkRegion& clipRegion, |
| 460 | const SkMatrix& matrix, const SkPaint& paint, bool hasText) { |
edisonn@google.com | 83d8eda | 2013-10-24 13:19:28 +0000 | [diff] [blame] | 461 | // Shape has to be flatten before we get here. |
| 462 | if (matrix.hasPerspective()) { |
| 463 | NOT_IMPLEMENTED(!matrix.hasPerspective(), false); |
vandebo@chromium.org | dc37e20 | 2013-10-18 20:16:34 +0000 | [diff] [blame] | 464 | return; |
| 465 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 466 | fBlendMode = paint.getBlendMode(); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 467 | fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion, |
| 468 | matrix, paint, hasText, |
| 469 | &fDstFormXObject); |
| 470 | } |
| 471 | }; |
| 472 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 473 | //////////////////////////////////////////////////////////////////////////////// |
| 474 | |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 475 | SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 476 | : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()), |
| 477 | SkSurfaceProps(0, kUnknown_SkPixelGeometry)) |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 478 | , fPageSize(pageSize) |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 479 | , fExistingClipRegion(SkIRect::MakeSize(pageSize)) |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 480 | , fRasterDpi(rasterDpi) |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 481 | , fDocument(doc) { |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 482 | SkASSERT(pageSize.width() > 0); |
| 483 | SkASSERT(pageSize.height() > 0); |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 484 | |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 485 | if (flip) { |
| 486 | // Skia generally uses the top left as the origin but PDF |
| 487 | // natively has the origin at the bottom left. This matrix |
| 488 | // corrects for that. But that only needs to be done once, we |
| 489 | // don't do it when layering. |
| 490 | fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight)); |
| 491 | fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1); |
| 492 | } else { |
| 493 | fInitialTransform.setIdentity(); |
| 494 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | SkPDFDevice::~SkPDFDevice() { |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 498 | this->cleanUp(); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void SkPDFDevice::init() { |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 502 | fContentEntries.reset(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 503 | } |
| 504 | |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 505 | void SkPDFDevice::cleanUp() { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 506 | fGraphicStateResources.unrefAll(); |
| 507 | fXObjectResources.unrefAll(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 508 | fFontResources.unrefAll(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 509 | fShaderResources.unrefAll(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 510 | } |
| 511 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 512 | void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[], |
| 513 | SkData* value) { |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame^] | 514 | if (!value) { |
| 515 | return; |
| 516 | } |
| 517 | if (rect.isEmpty()) { |
| 518 | this->handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 519 | } else { |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame^] | 520 | this->handleRectAnnotation(rect, d, key, value); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 524 | void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 525 | SkPaint newPaint = paint; |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 526 | replace_srcmode_on_opaque_paint(&newPaint); |
| 527 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 528 | newPaint.setStyle(SkPaint::kFill_Style); |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 529 | ScopedContentEntry content(this, d, newPaint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 530 | internalDrawPaint(newPaint, content.entry()); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 531 | } |
| 532 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 533 | void SkPDFDevice::internalDrawPaint(const SkPaint& paint, |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 534 | SkPDFDevice::ContentEntry* contentEntry) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 535 | if (!contentEntry) { |
| 536 | return; |
| 537 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 538 | SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()), |
| 539 | SkIntToScalar(this->height())); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 540 | SkMatrix inverse; |
commit-bot@chromium.org | d2cfa74 | 2013-09-20 18:58:30 +0000 | [diff] [blame] | 541 | if (!contentEntry->fState.fMatrix.invert(&inverse)) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 542 | return; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 543 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 544 | inverse.mapRect(&bbox); |
| 545 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 546 | SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 547 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 548 | &contentEntry->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 549 | } |
| 550 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 551 | void SkPDFDevice::drawPoints(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 552 | SkCanvas::PointMode mode, |
| 553 | size_t count, |
| 554 | const SkPoint* points, |
| 555 | const SkPaint& srcPaint) { |
| 556 | SkPaint passedPaint = srcPaint; |
| 557 | replace_srcmode_on_opaque_paint(&passedPaint); |
| 558 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 559 | if (count == 0) { |
| 560 | return; |
| 561 | } |
| 562 | |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 563 | // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath. |
| 564 | // We only use this when there's a path effect because of the overhead |
| 565 | // of multiple calls to setUpContentEntry it causes. |
| 566 | if (passedPaint.getPathEffect()) { |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 567 | if (d.fClipStack->isEmpty(this->getGlobalBounds())) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 568 | return; |
| 569 | } |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 570 | d.drawPoints(mode, count, points, passedPaint, this); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 574 | const SkPaint* paint = &passedPaint; |
| 575 | SkPaint modifiedPaint; |
| 576 | |
| 577 | if (mode == SkCanvas::kPoints_PointMode && |
| 578 | paint->getStrokeCap() != SkPaint::kRound_Cap) { |
| 579 | modifiedPaint = *paint; |
| 580 | paint = &modifiedPaint; |
| 581 | if (paint->getStrokeWidth()) { |
| 582 | // PDF won't draw a single point with square/butt caps because the |
| 583 | // orientation is ambiguous. Draw a rectangle instead. |
| 584 | modifiedPaint.setStyle(SkPaint::kFill_Style); |
| 585 | SkScalar strokeWidth = paint->getStrokeWidth(); |
| 586 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 587 | for (size_t i = 0; i < count; i++) { |
| 588 | SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0); |
| 589 | r.inset(-halfStroke, -halfStroke); |
| 590 | drawRect(d, r, modifiedPaint); |
| 591 | } |
| 592 | return; |
| 593 | } else { |
| 594 | modifiedPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 595 | } |
| 596 | } |
| 597 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 598 | ScopedContentEntry content(this, d, *paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 599 | if (!content.entry()) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 600 | return; |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 601 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 602 | |
| 603 | switch (mode) { |
| 604 | case SkCanvas::kPolygon_PointMode: |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 605 | SkPDFUtils::MoveTo(points[0].fX, points[0].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 606 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 607 | for (size_t i = 1; i < count; i++) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 608 | SkPDFUtils::AppendLine(points[i].fX, points[i].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 609 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 610 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 611 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 612 | break; |
| 613 | case SkCanvas::kLines_PointMode: |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 614 | for (size_t i = 0; i < count/2; i++) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 615 | SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 616 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 617 | SkPDFUtils::AppendLine(points[i * 2 + 1].fX, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 618 | points[i * 2 + 1].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 619 | &content.entry()->fContent); |
| 620 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 621 | } |
| 622 | break; |
| 623 | case SkCanvas::kPoints_PointMode: |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 624 | SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap); |
| 625 | for (size_t i = 0; i < count; i++) { |
| 626 | SkPDFUtils::MoveTo(points[i].fX, points[i].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 627 | &content.entry()->fContent); |
| 628 | SkPDFUtils::ClosePath(&content.entry()->fContent); |
| 629 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 630 | } |
| 631 | break; |
| 632 | default: |
| 633 | SkASSERT(false); |
| 634 | } |
| 635 | } |
| 636 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 637 | static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 638 | auto annotation = sk_make_sp<SkPDFDict>("Annot"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 639 | annotation->insertName("Subtype", "Link"); |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 640 | annotation->insertInt("F", 4); // required by ISO 19005 |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 641 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 642 | auto border = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 643 | border->reserve(3); |
| 644 | border->appendInt(0); // Horizontal corner radius. |
| 645 | border->appendInt(0); // Vertical corner radius. |
| 646 | border->appendInt(0); // Width, 0 = no border. |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 647 | annotation->insertObject("Border", std::move(border)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 648 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 649 | auto rect = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 650 | rect->reserve(4); |
| 651 | rect->appendScalar(translatedRect.fLeft); |
| 652 | rect->appendScalar(translatedRect.fTop); |
| 653 | rect->appendScalar(translatedRect.fRight); |
| 654 | rect->appendScalar(translatedRect.fBottom); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 655 | annotation->insertObject("Rect", std::move(rect)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 656 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 657 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 658 | } |
| 659 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 660 | static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) { |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 661 | sk_sp<SkPDFDict> annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 662 | SkString url(static_cast<const char *>(urlData->data()), |
| 663 | urlData->size() - 1); |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 664 | auto action = sk_make_sp<SkPDFDict>("Action"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 665 | action->insertName("S", "URI"); |
| 666 | action->insertString("URI", url); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 667 | annotation->insertObject("A", std::move(action)); |
| 668 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 669 | } |
| 670 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 671 | static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData, |
| 672 | const SkRect& r) { |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 673 | sk_sp<SkPDFDict> annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 674 | SkString name(static_cast<const char *>(nameData->data()), |
| 675 | nameData->size() - 1); |
| 676 | annotation->insertName("Dest", name); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 677 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 678 | } |
| 679 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 680 | void SkPDFDevice::drawRect(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 681 | const SkRect& rect, |
| 682 | const SkPaint& srcPaint) { |
| 683 | SkPaint paint = srcPaint; |
| 684 | replace_srcmode_on_opaque_paint(&paint); |
commit-bot@chromium.org | 969fd6a | 2013-05-14 18:16:40 +0000 | [diff] [blame] | 685 | SkRect r = rect; |
| 686 | r.sort(); |
| 687 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 688 | if (paint.getPathEffect()) { |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 689 | if (d.fClipStack->isEmpty(this->getGlobalBounds())) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 690 | return; |
| 691 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 692 | SkPath path; |
| 693 | path.addRect(r); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 694 | drawPath(d, path, paint, nullptr, true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 695 | return; |
| 696 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 697 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 698 | ScopedContentEntry content(this, d, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 699 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 700 | return; |
| 701 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 702 | SkPDFUtils::AppendRectangle(r, &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 703 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 704 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 705 | } |
| 706 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 707 | void SkPDFDevice::drawRRect(const SkDraw& draw, |
| 708 | const SkRRect& rrect, |
| 709 | const SkPaint& srcPaint) { |
| 710 | SkPaint paint = srcPaint; |
| 711 | replace_srcmode_on_opaque_paint(&paint); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 712 | SkPath path; |
| 713 | path.addRRect(rrect); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 714 | this->drawPath(draw, path, paint, nullptr, true); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 715 | } |
| 716 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 717 | void SkPDFDevice::drawOval(const SkDraw& draw, |
| 718 | const SkRect& oval, |
| 719 | const SkPaint& srcPaint) { |
| 720 | SkPaint paint = srcPaint; |
| 721 | replace_srcmode_on_opaque_paint(&paint); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 722 | SkPath path; |
| 723 | path.addOval(oval); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 724 | this->drawPath(draw, path, paint, nullptr, true); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 725 | } |
| 726 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 727 | void SkPDFDevice::drawPath(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 728 | const SkPath& origPath, |
| 729 | const SkPaint& srcPaint, |
| 730 | const SkMatrix* prePathMatrix, |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 731 | bool pathIsMutable) { |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 732 | SkPaint paint = srcPaint; |
| 733 | replace_srcmode_on_opaque_paint(&paint); |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 734 | SkPath modifiedPath; |
| 735 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 736 | |
| 737 | SkMatrix matrix = *d.fMatrix; |
| 738 | if (prePathMatrix) { |
| 739 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 740 | if (!pathIsMutable) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 741 | pathPtr = &modifiedPath; |
| 742 | pathIsMutable = true; |
| 743 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 744 | origPath.transform(*prePathMatrix, pathPtr); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 745 | } else { |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 746 | matrix.preConcat(*prePathMatrix); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 749 | |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 750 | if (paint.getPathEffect()) { |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 751 | if (d.fClipStack->isEmpty(this->getGlobalBounds())) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 752 | return; |
| 753 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 754 | if (!pathIsMutable) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 755 | pathPtr = &modifiedPath; |
| 756 | pathIsMutable = true; |
| 757 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 758 | bool fill = paint.getFillPath(origPath, pathPtr); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 759 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 760 | SkPaint noEffectPaint(paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 761 | noEffectPaint.setPathEffect(nullptr); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 762 | if (fill) { |
| 763 | noEffectPaint.setStyle(SkPaint::kFill_Style); |
| 764 | } else { |
| 765 | noEffectPaint.setStyle(SkPaint::kStroke_Style); |
| 766 | noEffectPaint.setStrokeWidth(0); |
| 767 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 768 | drawPath(d, *pathPtr, noEffectPaint, nullptr, true); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 769 | return; |
| 770 | } |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 771 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 772 | if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 773 | return; |
| 774 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 775 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 776 | ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 777 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 778 | return; |
| 779 | } |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 780 | SkScalar matrixScale = matrix.mapRadius(1.0f); |
| 781 | SkScalar tolerance = matrixScale > 0.0f ? 0.25f / matrixScale : 0.25f; |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 782 | bool consumeDegeratePathSegments = |
| 783 | paint.getStyle() == SkPaint::kFill_Style || |
| 784 | (paint.getStrokeCap() != SkPaint::kRound_Cap && |
| 785 | paint.getStrokeCap() != SkPaint::kSquare_Cap); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 786 | SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(), |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 787 | consumeDegeratePathSegments, |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 788 | &content.entry()->fContent, |
| 789 | tolerance); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 790 | SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 791 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 794 | |
| 795 | void SkPDFDevice::drawImageRect(const SkDraw& d, |
| 796 | const SkImage* image, |
| 797 | const SkRect* src, |
| 798 | const SkRect& dst, |
| 799 | const SkPaint& srcPaint, |
| 800 | SkCanvas::SrcRectConstraint) { |
| 801 | if (!image) { |
| 802 | return; |
| 803 | } |
| 804 | SkIRect bounds = image->bounds(); |
| 805 | SkPaint paint = srcPaint; |
| 806 | if (image->isOpaque()) { |
| 807 | replace_srcmode_on_opaque_paint(&paint); |
| 808 | } |
| 809 | SkRect srcRect = src ? *src : SkRect::Make(bounds); |
| 810 | SkMatrix transform; |
| 811 | transform.setRectToRect(srcRect, dst, SkMatrix::kFill_ScaleToFit); |
| 812 | if (src) { |
| 813 | if (!srcRect.intersect(SkRect::Make(bounds))) { |
| 814 | return; |
| 815 | } |
| 816 | srcRect.roundOut(&bounds); |
| 817 | transform.preTranslate(SkIntToScalar(bounds.x()), |
| 818 | SkIntToScalar(bounds.y())); |
| 819 | } |
| 820 | SkImageSubset imageSubset(sk_ref_sp(const_cast<SkImage*>(image)), bounds); |
| 821 | if (!imageSubset.isValid()) { |
| 822 | return; |
| 823 | } |
| 824 | transform.postConcat(*d.fMatrix); |
| 825 | this->internalDrawImage(transform, d.fClipStack, d.fRC->bwRgn(), |
| 826 | std::move(imageSubset), paint); |
| 827 | } |
| 828 | |
| 829 | void SkPDFDevice::drawBitmapRect(const SkDraw& d, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 830 | const SkBitmap& bitmap, |
| 831 | const SkRect* src, |
| 832 | const SkRect& dst, |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 833 | const SkPaint& srcPaint, |
| 834 | SkCanvas::SrcRectConstraint) { |
| 835 | if (bitmap.drawsNothing()) { |
| 836 | return; |
| 837 | } |
| 838 | SkIRect bounds = bitmap.bounds(); |
| 839 | SkPaint paint = srcPaint; |
| 840 | if (bitmap.isOpaque()) { |
| 841 | replace_srcmode_on_opaque_paint(&paint); |
| 842 | } |
| 843 | SkRect srcRect = src ? *src : SkRect::Make(bounds); |
| 844 | SkMatrix transform; |
| 845 | transform.setRectToRect(srcRect, dst, SkMatrix::kFill_ScaleToFit); |
| 846 | if (src) { |
| 847 | if (!srcRect.intersect(SkRect::Make(bounds))) { |
| 848 | return; |
| 849 | } |
| 850 | srcRect.roundOut(&bounds); |
| 851 | transform.preTranslate(SkIntToScalar(bounds.x()), |
| 852 | SkIntToScalar(bounds.y())); |
| 853 | } |
| 854 | SkBitmap bitmapSubset; |
| 855 | if (!bitmap.extractSubset(&bitmapSubset, bounds)) { |
| 856 | return; |
| 857 | } |
| 858 | SkImageSubset imageSubset = make_image_subset(bitmapSubset); |
| 859 | if (!imageSubset.isValid()) { |
| 860 | return; |
| 861 | } |
| 862 | transform.postConcat(*d.fMatrix); |
| 863 | this->internalDrawImage(transform, d.fClipStack, d.fRC->bwRgn(), |
| 864 | std::move(imageSubset), paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | void SkPDFDevice::drawBitmap(const SkDraw& d, |
| 868 | const SkBitmap& bitmap, |
| 869 | const SkMatrix& matrix, |
| 870 | const SkPaint& srcPaint) { |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 871 | if (bitmap.drawsNothing() || d.fClipStack->isEmpty(this->getGlobalBounds())) { |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 872 | return; |
| 873 | } |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 874 | SkPaint paint = srcPaint; |
| 875 | if (bitmap.isOpaque()) { |
| 876 | replace_srcmode_on_opaque_paint(&paint); |
| 877 | } |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 878 | SkImageSubset imageSubset = make_image_subset(bitmap); |
| 879 | if (!imageSubset.isValid()) { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 880 | return; |
| 881 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 882 | SkMatrix transform = matrix; |
| 883 | transform.postConcat(*d.fMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 884 | this->internalDrawImage( |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 885 | transform, d.fClipStack, d.fRC->bwRgn(), std::move(imageSubset), paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | void SkPDFDevice::drawSprite(const SkDraw& d, |
| 889 | const SkBitmap& bitmap, |
| 890 | int x, |
| 891 | int y, |
| 892 | const SkPaint& srcPaint) { |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 893 | if (bitmap.drawsNothing() || d.fClipStack->isEmpty(this->getGlobalBounds())) { |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 894 | return; |
| 895 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 896 | SkPaint paint = srcPaint; |
| 897 | if (bitmap.isOpaque()) { |
| 898 | replace_srcmode_on_opaque_paint(&paint); |
| 899 | } |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 900 | SkImageSubset imageSubset = make_image_subset(bitmap); |
| 901 | if (!imageSubset.isValid()) { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 902 | return; |
| 903 | } |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 904 | SkMatrix transform = SkMatrix::MakeTrans(SkIntToScalar(x), SkIntToScalar(y)); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 905 | this->internalDrawImage( |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 906 | transform, d.fClipStack, d.fRC->bwRgn(), std::move(imageSubset), paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | void SkPDFDevice::drawImage(const SkDraw& draw, |
| 910 | const SkImage* image, |
| 911 | SkScalar x, |
| 912 | SkScalar y, |
| 913 | const SkPaint& srcPaint) { |
| 914 | SkPaint paint = srcPaint; |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 915 | if (!image) { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | if (image->isOpaque()) { |
| 919 | replace_srcmode_on_opaque_paint(&paint); |
| 920 | } |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 921 | SkImageSubset imageSubset(sk_ref_sp(const_cast<SkImage*>(image))); |
| 922 | if (!imageSubset.isValid()) { |
| 923 | return; |
| 924 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 925 | SkMatrix transform = SkMatrix::MakeTrans(x, y); |
| 926 | transform.postConcat(*draw.fMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 927 | this->internalDrawImage( |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 928 | transform, draw.fClipStack, draw.fRC->bwRgn(), std::move(imageSubset), paint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 929 | } |
| 930 | |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 931 | namespace { |
| 932 | class GlyphPositioner { |
| 933 | public: |
| 934 | GlyphPositioner(SkDynamicMemoryWStream* content, |
| 935 | SkScalar textSkewX, |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 936 | bool wideChars, |
| 937 | bool defaultPositioning, |
| 938 | SkPoint origin) |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 939 | : fContent(content) |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 940 | , fCurrentMatrixOrigin(origin) |
| 941 | , fTextSkewX(textSkewX) |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 942 | , fWideChars(wideChars) |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 943 | , fDefaultPositioning(defaultPositioning) { |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 944 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 945 | ~GlyphPositioner() { this->flush(); } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 946 | void flush() { |
| 947 | if (fInText) { |
| 948 | fContent->writeText("> Tj\n"); |
| 949 | fInText = false; |
| 950 | } |
| 951 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 952 | void writeGlyph(SkPoint xy, |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 953 | SkScalar advanceWidth, |
| 954 | uint16_t glyph) { |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 955 | if (!fInitialized) { |
| 956 | // Flip the text about the x-axis to account for origin swap and include |
| 957 | // the passed parameters. |
| 958 | fContent->writeText("1 0 "); |
| 959 | SkPDFUtils::AppendScalar(-fTextSkewX, fContent); |
| 960 | fContent->writeText(" -1 "); |
| 961 | SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.x(), fContent); |
| 962 | fContent->writeText(" "); |
| 963 | SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.y(), fContent); |
| 964 | fContent->writeText(" Tm\n"); |
| 965 | fCurrentMatrixOrigin.set(0.0f, 0.0f); |
| 966 | fInitialized = true; |
| 967 | } |
Hal Canary | 7bb9301 | 2016-10-31 12:40:49 -0400 | [diff] [blame] | 968 | #ifdef SK_BUILD_FOR_WIN |
| 969 | const bool kAlwaysPosition = true; |
| 970 | #else |
| 971 | const bool kAlwaysPosition = false; |
| 972 | #endif |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 973 | if (!fDefaultPositioning) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 974 | SkPoint position = xy - fCurrentMatrixOrigin; |
Hal Canary | 7bb9301 | 2016-10-31 12:40:49 -0400 | [diff] [blame] | 975 | if (kAlwaysPosition || position != SkPoint{fXAdvance, 0}) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 976 | this->flush(); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 977 | SkPDFUtils::AppendScalar(position.x(), fContent); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 978 | fContent->writeText(" "); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 979 | SkPDFUtils::AppendScalar(-position.y(), fContent); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 980 | fContent->writeText(" Td "); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 981 | fCurrentMatrixOrigin = xy; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 982 | fXAdvance = 0; |
| 983 | } |
| 984 | fXAdvance += advanceWidth; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 985 | } |
| 986 | if (!fInText) { |
| 987 | fContent->writeText("<"); |
| 988 | fInText = true; |
| 989 | } |
| 990 | if (fWideChars) { |
| 991 | SkPDFUtils::WriteUInt16BE(fContent, glyph); |
| 992 | } else { |
| 993 | SkASSERT(0 == glyph >> 8); |
| 994 | SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph)); |
| 995 | } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | private: |
| 999 | SkDynamicMemoryWStream* fContent; |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1000 | SkPoint fCurrentMatrixOrigin; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1001 | SkScalar fXAdvance = 0.0f; |
| 1002 | SkScalar fTextSkewX; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1003 | bool fWideChars; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1004 | bool fInText = false; |
| 1005 | bool fInitialized = false; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1006 | const bool fDefaultPositioning; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1007 | }; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1008 | |
| 1009 | /** Given the m-to-n glyph-to-character mapping data (as returned by |
| 1010 | harfbuzz), iterate over the clusters. */ |
| 1011 | class Clusterator { |
| 1012 | public: |
| 1013 | Clusterator() : fClusters(nullptr), fUtf8Text(nullptr), fGlyphCount(0), fTextByteLength(0) {} |
| 1014 | explicit Clusterator(uint32_t glyphCount) |
| 1015 | : fClusters(nullptr) |
| 1016 | , fUtf8Text(nullptr) |
| 1017 | , fGlyphCount(glyphCount) |
| 1018 | , fTextByteLength(0) {} |
| 1019 | // The clusters[] array is an array of offsets into utf8Text[], |
| 1020 | // one offset for each glyph. See SkTextBlobBuilder for more info. |
| 1021 | Clusterator(const uint32_t* clusters, |
| 1022 | const char* utf8Text, |
| 1023 | uint32_t glyphCount, |
| 1024 | uint32_t textByteLength) |
| 1025 | : fClusters(clusters) |
| 1026 | , fUtf8Text(utf8Text) |
| 1027 | , fGlyphCount(glyphCount) |
| 1028 | , fTextByteLength(textByteLength) { |
| 1029 | // This is a cheap heuristic for /ReversedChars which seems to |
| 1030 | // work for clusters produced by HarfBuzz, which either |
| 1031 | // increase from zero (LTR) or decrease to zero (RTL). |
| 1032 | // "ReversedChars" is how PDF deals with RTL text. |
| 1033 | fReversedChars = |
| 1034 | fUtf8Text && fClusters && fGlyphCount && fClusters[0] != 0; |
| 1035 | } |
| 1036 | struct Cluster { |
| 1037 | const char* fUtf8Text; |
| 1038 | uint32_t fTextByteLength; |
| 1039 | uint32_t fGlyphIndex; |
| 1040 | uint32_t fGlyphCount; |
| 1041 | explicit operator bool() const { return fGlyphCount != 0; } |
| 1042 | }; |
| 1043 | // True if this looks like right-to-left text. |
| 1044 | bool reversedChars() const { return fReversedChars; } |
| 1045 | Cluster next() { |
| 1046 | if ((!fUtf8Text || !fClusters) && fGlyphCount) { |
| 1047 | // These glyphs have no text. Treat as one "cluster". |
| 1048 | uint32_t glyphCount = fGlyphCount; |
| 1049 | fGlyphCount = 0; |
| 1050 | return Cluster{nullptr, 0, 0, glyphCount}; |
| 1051 | } |
| 1052 | if (fGlyphCount == 0 || fTextByteLength == 0) { |
| 1053 | return Cluster{nullptr, 0, 0, 0}; // empty |
| 1054 | } |
| 1055 | SkASSERT(fUtf8Text); |
| 1056 | SkASSERT(fClusters); |
| 1057 | uint32_t cluster = fClusters[0]; |
| 1058 | if (cluster >= fTextByteLength) { |
| 1059 | return Cluster{nullptr, 0, 0, 0}; // bad input. |
| 1060 | } |
| 1061 | uint32_t glyphsInCluster = 1; |
| 1062 | while (glyphsInCluster < fGlyphCount && |
| 1063 | fClusters[glyphsInCluster] == cluster) { |
| 1064 | ++glyphsInCluster; |
| 1065 | } |
| 1066 | SkASSERT(glyphsInCluster <= fGlyphCount); |
| 1067 | uint32_t textLength = 0; |
| 1068 | if (glyphsInCluster == fGlyphCount) { |
| 1069 | // consumes rest of glyphs and rest of text |
| 1070 | if (kInvalidCluster == fPreviousCluster) { // LTR text or single cluster |
| 1071 | textLength = fTextByteLength - cluster; |
| 1072 | } else { // RTL text; last cluster. |
| 1073 | SkASSERT(fPreviousCluster < fTextByteLength); |
| 1074 | if (fPreviousCluster <= cluster) { // bad input. |
| 1075 | return Cluster{nullptr, 0, 0, 0}; |
| 1076 | } |
| 1077 | textLength = fPreviousCluster - cluster; |
| 1078 | } |
| 1079 | fGlyphCount = 0; |
| 1080 | return Cluster{fUtf8Text + cluster, |
| 1081 | textLength, |
| 1082 | fGlyphIndex, |
| 1083 | glyphsInCluster}; |
| 1084 | } |
| 1085 | SkASSERT(glyphsInCluster < fGlyphCount); |
| 1086 | uint32_t nextCluster = fClusters[glyphsInCluster]; |
| 1087 | if (nextCluster >= fTextByteLength) { |
| 1088 | return Cluster{nullptr, 0, 0, 0}; // bad input. |
| 1089 | } |
| 1090 | if (nextCluster > cluster) { // LTR text |
| 1091 | if (kInvalidCluster != fPreviousCluster) { |
| 1092 | return Cluster{nullptr, 0, 0, 0}; // bad input. |
| 1093 | } |
| 1094 | textLength = nextCluster - cluster; |
| 1095 | } else { // RTL text |
| 1096 | SkASSERT(nextCluster < cluster); |
| 1097 | if (kInvalidCluster == fPreviousCluster) { // first cluster |
| 1098 | textLength = fTextByteLength - cluster; |
| 1099 | } else { // later cluster |
| 1100 | if (fPreviousCluster <= cluster) { |
| 1101 | return Cluster{nullptr, 0, 0, 0}; // bad input. |
| 1102 | } |
| 1103 | textLength = fPreviousCluster - cluster; |
| 1104 | } |
| 1105 | fPreviousCluster = cluster; |
| 1106 | } |
| 1107 | uint32_t glyphIndex = fGlyphIndex; |
| 1108 | fGlyphCount -= glyphsInCluster; |
| 1109 | fGlyphIndex += glyphsInCluster; |
| 1110 | fClusters += glyphsInCluster; |
| 1111 | return Cluster{fUtf8Text + cluster, |
| 1112 | textLength, |
| 1113 | glyphIndex, |
| 1114 | glyphsInCluster}; |
| 1115 | } |
| 1116 | |
| 1117 | private: |
| 1118 | static constexpr uint32_t kInvalidCluster = 0xFFFFFFFF; |
| 1119 | const uint32_t* fClusters; |
| 1120 | const char* fUtf8Text; |
| 1121 | uint32_t fGlyphCount; |
| 1122 | uint32_t fTextByteLength; |
| 1123 | uint32_t fGlyphIndex = 0; |
| 1124 | uint32_t fPreviousCluster = kInvalidCluster; |
| 1125 | bool fReversedChars = false; |
| 1126 | }; |
| 1127 | |
| 1128 | struct TextStorage { |
| 1129 | SkAutoTMalloc<char> fUtf8textStorage; |
| 1130 | SkAutoTMalloc<uint32_t> fClusterStorage; |
| 1131 | SkAutoTMalloc<SkGlyphID> fGlyphStorage; |
| 1132 | }; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1133 | } // namespace |
| 1134 | |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1135 | /** Given some unicode text (as passed to drawText(), convert to |
| 1136 | glyphs (via primitive shaping), while preserving |
| 1137 | glyph-to-character mapping information. */ |
| 1138 | static Clusterator make_clusterator( |
| 1139 | const void* sourceText, |
| 1140 | size_t sourceByteCount, |
| 1141 | const SkPaint& paint, |
| 1142 | TextStorage* storage, |
| 1143 | int glyphCount) { |
| 1144 | SkASSERT(SkPaint::kGlyphID_TextEncoding != paint.getTextEncoding()); |
| 1145 | SkASSERT(glyphCount == paint.textToGlyphs(sourceText, sourceByteCount, nullptr)); |
| 1146 | SkASSERT(glyphCount > 0); |
| 1147 | storage->fGlyphStorage.reset(SkToSizeT(glyphCount)); |
| 1148 | (void)paint.textToGlyphs(sourceText, sourceByteCount, storage->fGlyphStorage.get()); |
| 1149 | storage->fClusterStorage.reset(SkToSizeT(glyphCount)); |
| 1150 | uint32_t* clusters = storage->fClusterStorage.get(); |
| 1151 | uint32_t utf8ByteCount = 0; |
| 1152 | const char* utf8Text = nullptr; |
| 1153 | switch (paint.getTextEncoding()) { |
| 1154 | case SkPaint::kUTF8_TextEncoding: { |
| 1155 | const char* txtPtr = (const char*)sourceText; |
| 1156 | for (int i = 0; i < glyphCount; ++i) { |
| 1157 | clusters[i] = SkToU32(txtPtr - (const char*)sourceText); |
| 1158 | txtPtr += SkUTF8_LeadByteToCount(*(const unsigned char*)txtPtr); |
| 1159 | SkASSERT(txtPtr <= (const char*)sourceText + sourceByteCount); |
| 1160 | } |
| 1161 | SkASSERT(txtPtr == (const char*)sourceText + sourceByteCount); |
| 1162 | utf8ByteCount = SkToU32(sourceByteCount); |
| 1163 | utf8Text = (const char*)sourceText; |
| 1164 | break; |
| 1165 | } |
| 1166 | case SkPaint::kUTF16_TextEncoding: { |
| 1167 | const uint16_t* utf16ptr = (const uint16_t*)sourceText; |
| 1168 | int utf16count = SkToInt(sourceByteCount / sizeof(uint16_t)); |
| 1169 | utf8ByteCount = SkToU32(SkUTF16_ToUTF8(utf16ptr, utf16count)); |
| 1170 | storage->fUtf8textStorage.reset(utf8ByteCount); |
| 1171 | char* txtPtr = storage->fUtf8textStorage.get(); |
| 1172 | utf8Text = txtPtr; |
| 1173 | int clusterIndex = 0; |
| 1174 | while (utf16ptr < (const uint16_t*)sourceText + utf16count) { |
| 1175 | clusters[clusterIndex++] = SkToU32(txtPtr - utf8Text); |
| 1176 | SkUnichar uni = SkUTF16_NextUnichar(&utf16ptr); |
| 1177 | txtPtr += SkUTF8_FromUnichar(uni, txtPtr); |
| 1178 | } |
| 1179 | SkASSERT(clusterIndex == glyphCount); |
| 1180 | SkASSERT(txtPtr == storage->fUtf8textStorage.get() + utf8ByteCount); |
| 1181 | SkASSERT(utf16ptr == (const uint16_t*)sourceText + utf16count); |
| 1182 | break; |
| 1183 | } |
| 1184 | case SkPaint::kUTF32_TextEncoding: { |
| 1185 | const SkUnichar* utf32 = (const SkUnichar*)sourceText; |
| 1186 | int utf32count = SkToInt(sourceByteCount / sizeof(SkUnichar)); |
| 1187 | SkASSERT(glyphCount == utf32count); |
| 1188 | for (int i = 0; i < utf32count; ++i) { |
| 1189 | utf8ByteCount += SkToU32(SkUTF8_FromUnichar(utf32[i])); |
| 1190 | } |
| 1191 | storage->fUtf8textStorage.reset(SkToSizeT(utf8ByteCount)); |
| 1192 | char* txtPtr = storage->fUtf8textStorage.get(); |
| 1193 | utf8Text = txtPtr; |
| 1194 | for (int i = 0; i < utf32count; ++i) { |
| 1195 | clusters[i] = SkToU32(txtPtr - utf8Text); |
| 1196 | txtPtr += SkUTF8_FromUnichar(utf32[i], txtPtr); |
| 1197 | } |
| 1198 | break; |
| 1199 | } |
| 1200 | default: |
| 1201 | SkDEBUGFAIL(""); |
| 1202 | break; |
| 1203 | } |
| 1204 | return Clusterator(clusters, utf8Text, SkToU32(glyphCount), utf8ByteCount); |
| 1205 | } |
| 1206 | |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1207 | static SkUnichar map_glyph(const SkTDArray<SkUnichar>& glyphToUnicode, SkGlyphID glyph) { |
| 1208 | return SkToInt(glyph) < glyphToUnicode.count() ? glyphToUnicode[SkToInt(glyph)] : -1; |
| 1209 | } |
| 1210 | |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1211 | static void update_font(SkWStream* wStream, int fontIndex, SkScalar textSize) { |
| 1212 | wStream->writeText("/"); |
| 1213 | char prefix = SkPDFResourceDict::GetResourceTypePrefix(SkPDFResourceDict::kFont_ResourceType); |
| 1214 | wStream->write(&prefix, 1); |
| 1215 | wStream->writeDecAsText(fontIndex); |
| 1216 | wStream->writeText(" "); |
| 1217 | SkPDFUtils::AppendScalar(textSize, wStream); |
| 1218 | wStream->writeText(" Tf\n"); |
| 1219 | } |
| 1220 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1221 | void SkPDFDevice::internalDrawText( |
| 1222 | const SkDraw& d, const void* sourceText, size_t sourceByteCount, |
| 1223 | const SkScalar pos[], SkTextBlob::GlyphPositioning positioning, |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1224 | SkPoint offset, const SkPaint& srcPaint, const uint32_t* clusters, |
| 1225 | uint32_t textByteLength, const char* utf8Text) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1226 | NOT_IMPLEMENTED(srcPaint.getMaskFilter() != nullptr, false); |
| 1227 | if (srcPaint.getMaskFilter() != nullptr) { |
| 1228 | // Don't pretend we support drawing MaskFilters, it makes for artifacts |
| 1229 | // making text unreadable (e.g. same text twice when using CSS shadows). |
| 1230 | return; |
| 1231 | } |
halcanary | ea17dfe | 2016-08-24 09:20:57 -0700 | [diff] [blame] | 1232 | NOT_IMPLEMENTED(srcPaint.isVerticalText(), false); |
| 1233 | if (srcPaint.isVerticalText()) { |
| 1234 | // Don't pretend we support drawing vertical text. It is not |
| 1235 | // clear to me how to switch to "vertical writing" mode in PDF. |
| 1236 | // Currently neither Chromium or Android set this flag. |
| 1237 | // https://bug.skia.org/5665 |
| 1238 | return; |
| 1239 | } |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1240 | if (0 == sourceByteCount || !sourceText) { |
| 1241 | return; |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1242 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1243 | SkPaint paint = calculate_text_paint(srcPaint); |
| 1244 | replace_srcmode_on_opaque_paint(&paint); |
| 1245 | if (!paint.getTypeface()) { |
| 1246 | paint.setTypeface(SkTypeface::MakeDefault()); |
| 1247 | } |
| 1248 | SkTypeface* typeface = paint.getTypeface(); |
| 1249 | if (!typeface) { |
| 1250 | SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n"); |
| 1251 | return; |
| 1252 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1253 | |
| 1254 | const SkAdvancedTypefaceMetrics* metrics = |
| 1255 | SkPDFFont::GetMetrics(typeface, fDocument->canon()); |
| 1256 | if (!metrics) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1257 | return; |
| 1258 | } |
halcanary | 2bd295e | 2016-09-16 08:15:56 -0700 | [diff] [blame] | 1259 | int glyphCount = paint.textToGlyphs(sourceText, sourceByteCount, nullptr); |
| 1260 | if (glyphCount <= 0) { |
| 1261 | return; |
| 1262 | } |
halcanary | 3b294d5 | 2016-09-16 13:21:08 -0700 | [diff] [blame] | 1263 | |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1264 | // These three heap buffers are only used in the case where no glyphs |
| 1265 | // are passed to drawText() (most clients pass glyphs or a textblob). |
| 1266 | TextStorage storage; |
| 1267 | const SkGlyphID* glyphs = nullptr; |
| 1268 | Clusterator clusterator; |
| 1269 | if (textByteLength > 0) { |
| 1270 | SkASSERT(glyphCount == SkToInt(sourceByteCount / sizeof(SkGlyphID))); |
| 1271 | glyphs = (const SkGlyphID*)sourceText; |
| 1272 | clusterator = Clusterator(clusters, utf8Text, SkToU32(glyphCount), textByteLength); |
| 1273 | SkASSERT(clusters); |
| 1274 | SkASSERT(utf8Text); |
| 1275 | SkASSERT(srcPaint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding); |
| 1276 | SkASSERT(glyphCount == paint.textToGlyphs(sourceText, sourceByteCount, nullptr)); |
| 1277 | } else if (SkPaint::kGlyphID_TextEncoding == srcPaint.getTextEncoding()) { |
| 1278 | SkASSERT(glyphCount == SkToInt(sourceByteCount / sizeof(SkGlyphID))); |
| 1279 | glyphs = (const SkGlyphID*)sourceText; |
| 1280 | clusterator = Clusterator(SkToU32(glyphCount)); |
| 1281 | SkASSERT(glyphCount == paint.textToGlyphs(sourceText, sourceByteCount, nullptr)); |
| 1282 | SkASSERT(nullptr == clusters); |
| 1283 | SkASSERT(nullptr == utf8Text); |
| 1284 | } else { |
| 1285 | SkASSERT(nullptr == clusters); |
| 1286 | SkASSERT(nullptr == utf8Text); |
| 1287 | clusterator = make_clusterator(sourceText, sourceByteCount, srcPaint, |
| 1288 | &storage, glyphCount); |
| 1289 | glyphs = storage.fGlyphStorage; |
| 1290 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1291 | bool defaultPositioning = (positioning == SkTextBlob::kDefault_Positioning); |
halcanary | 9df5a4c | 2016-08-24 10:08:13 -0700 | [diff] [blame] | 1292 | paint.setHinting(SkPaint::kNo_Hinting); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1293 | SkAutoGlyphCache glyphCache(paint, nullptr, nullptr); |
| 1294 | |
| 1295 | SkPaint::Align alignment = paint.getTextAlign(); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1296 | float alignmentFactor = SkPaint::kLeft_Align == alignment ? 0.0f : |
| 1297 | SkPaint::kCenter_Align == alignment ? -0.5f : |
| 1298 | /* SkPaint::kRight_Align */ -1.0f; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1299 | if (defaultPositioning && alignment != SkPaint::kLeft_Align) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1300 | SkScalar advance = 0; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1301 | for (int i = 0; i < glyphCount; ++i) { |
| 1302 | advance += glyphCache->getGlyphIDAdvance(glyphs[i]).fAdvanceX; |
| 1303 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1304 | offset.offset(alignmentFactor * advance, 0); |
halcanary | 6059dc3 | 2016-08-15 11:45:36 -0700 | [diff] [blame] | 1305 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1306 | ScopedContentEntry content(this, d, paint, true); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1307 | if (!content.entry()) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1308 | return; |
| 1309 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1310 | SkDynamicMemoryWStream* out = &content.entry()->fContent; |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1311 | SkScalar textSize = paint.getTextSize(); |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1312 | const SkTDArray<SkUnichar>& glyphToUnicode = metrics->fGlyphToUnicode; |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1313 | |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1314 | out->writeText("BT\n"); |
| 1315 | SK_AT_SCOPE_EXIT(out->writeText("ET\n")); |
| 1316 | |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1317 | const SkGlyphID maxGlyphID = metrics->fLastGlyphID; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1318 | bool multiByteGlyphs = SkPDFFont::IsMultiByte(SkPDFFont::FontType(*metrics)); |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1319 | if (clusterator.reversedChars()) { |
| 1320 | out->writeText("/ReversedChars BMC\n"); |
| 1321 | } |
| 1322 | SK_AT_SCOPE_EXIT(if (clusterator.reversedChars()) { out->writeText("EMC\n"); } ); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1323 | GlyphPositioner glyphPositioner(out, |
| 1324 | paint.getTextSkewX(), |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1325 | multiByteGlyphs, |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1326 | defaultPositioning, |
| 1327 | offset); |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1328 | SkPDFFont* font = nullptr; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1329 | |
| 1330 | while (Clusterator::Cluster c = clusterator.next()) { |
| 1331 | int index = c.fGlyphIndex; |
| 1332 | int glyphLimit = index + c.fGlyphCount; |
| 1333 | |
| 1334 | bool actualText = false; |
| 1335 | SK_AT_SCOPE_EXIT(if (actualText) { glyphPositioner.flush(); out->writeText("EMC\n"); } ); |
| 1336 | if (c.fUtf8Text) { // real cluster |
| 1337 | // Check if `/ActualText` needed. |
| 1338 | const char* textPtr = c.fUtf8Text; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1339 | const char* textEnd = c.fUtf8Text + c.fTextByteLength; |
Hal Canary | d1c8e56 | 2017-01-11 15:53:25 -0500 | [diff] [blame] | 1340 | SkUnichar unichar = SkUTF8_NextUnicharWithError(&textPtr, textEnd); |
| 1341 | if (unichar < 0) { |
| 1342 | return; |
| 1343 | } |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1344 | if (textPtr < textEnd || // more characters left |
| 1345 | glyphLimit > index + 1 || // toUnicode wouldn't work |
| 1346 | unichar != map_glyph(glyphToUnicode, glyphs[index])) // test single Unichar map |
| 1347 | { |
| 1348 | glyphPositioner.flush(); |
| 1349 | out->writeText("/Span<</ActualText <"); |
| 1350 | SkPDFUtils::WriteUTF16beHex(out, 0xFEFF); // U+FEFF = BYTE ORDER MARK |
| 1351 | // the BOM marks this text as UTF-16BE, not PDFDocEncoding. |
| 1352 | SkPDFUtils::WriteUTF16beHex(out, unichar); // first char |
| 1353 | while (textPtr < textEnd) { |
Hal Canary | d1c8e56 | 2017-01-11 15:53:25 -0500 | [diff] [blame] | 1354 | unichar = SkUTF8_NextUnicharWithError(&textPtr, textEnd); |
| 1355 | if (unichar < 0) { |
| 1356 | break; |
| 1357 | } |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1358 | SkPDFUtils::WriteUTF16beHex(out, unichar); |
| 1359 | } |
| 1360 | out->writeText("> >> BDC\n"); // begin marked-content sequence |
| 1361 | // with an associated property list. |
| 1362 | actualText = true; |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1363 | } |
| 1364 | } |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1365 | for (; index < glyphLimit; ++index) { |
| 1366 | SkGlyphID gid = glyphs[index]; |
| 1367 | if (gid > maxGlyphID) { |
| 1368 | continue; |
| 1369 | } |
| 1370 | if (!font || !font->hasGlyph(gid)) { |
| 1371 | // Not yet specified font or need to switch font. |
| 1372 | int fontIndex = this->getFontResourceIndex(typeface, gid); |
| 1373 | // All preconditions for SkPDFFont::GetFontResource are met. |
| 1374 | SkASSERT(fontIndex >= 0); |
| 1375 | if (fontIndex < 0) { |
| 1376 | return; |
| 1377 | } |
| 1378 | glyphPositioner.flush(); |
| 1379 | update_font(out, fontIndex, textSize); |
| 1380 | font = fFontResources[fontIndex]; |
| 1381 | SkASSERT(font); // All preconditions for SkPDFFont::GetFontResource are met. |
| 1382 | if (!font) { |
| 1383 | return; |
| 1384 | } |
| 1385 | SkASSERT(font->multiByteGlyphs() == multiByteGlyphs); |
| 1386 | } |
| 1387 | SkPoint xy{0, 0}; |
| 1388 | SkScalar advance{0}; |
| 1389 | if (!defaultPositioning) { |
| 1390 | advance = glyphCache->getGlyphIDAdvance(gid).fAdvanceX; |
| 1391 | xy = SkTextBlob::kFull_Positioning == positioning |
| 1392 | ? SkPoint{pos[2 * index], pos[2 * index + 1]} |
| 1393 | : SkPoint{pos[index], 0}; |
| 1394 | if (alignment != SkPaint::kLeft_Align) { |
| 1395 | xy.offset(alignmentFactor * advance, 0); |
| 1396 | } |
| 1397 | } |
| 1398 | font->noteGlyphUsage(gid); |
| 1399 | SkGlyphID encodedGlyph = multiByteGlyphs ? gid : font->glyphToPDFFontEncoding(gid); |
| 1400 | glyphPositioner.writeGlyph(xy, advance, encodedGlyph); |
| 1401 | } |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1402 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len, |
| 1406 | SkScalar x, SkScalar y, const SkPaint& paint) { |
| 1407 | this->internalDrawText(d, text, len, nullptr, SkTextBlob::kDefault_Positioning, |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1408 | SkPoint{x, y}, paint, nullptr, 0, nullptr); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len, |
| 1412 | const SkScalar pos[], int scalarsPerPos, |
| 1413 | const SkPoint& offset, const SkPaint& paint) { |
| 1414 | this->internalDrawText(d, text, len, pos, (SkTextBlob::GlyphPositioning)scalarsPerPos, |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1415 | offset, paint, nullptr, 0, nullptr); |
| 1416 | } |
| 1417 | |
| 1418 | void SkPDFDevice::drawTextBlob(const SkDraw& draw, const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 1419 | const SkPaint &paint, SkDrawFilter* drawFilter) { |
| 1420 | for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) { |
| 1421 | SkPaint runPaint(paint); |
| 1422 | it.applyFontToPaint(&runPaint); |
| 1423 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 1424 | continue; |
| 1425 | } |
| 1426 | runPaint.setFlags(this->filterTextFlags(runPaint)); |
| 1427 | SkPoint offset = it.offset() + SkPoint{x, y}; |
| 1428 | this->internalDrawText(draw, it.glyphs(), sizeof(SkGlyphID) * it.glyphCount(), |
| 1429 | it.pos(), it.positioning(), offset, runPaint, |
| 1430 | it.clusters(), it.textSize(), it.text()); |
| 1431 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1434 | void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1435 | int vertexCount, const SkPoint verts[], |
| 1436 | const SkPoint texs[], const SkColor colors[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 1437 | SkBlendMode, const uint16_t indices[], |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1438 | int indexCount, const SkPaint& paint) { |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 1439 | if (d.fClipStack->isEmpty(this->getGlobalBounds())) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1440 | return; |
| 1441 | } |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1442 | // TODO: implement drawVertices |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1445 | void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device, |
| 1446 | int x, int y, const SkPaint& paint) { |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 1447 | SkASSERT(!paint.getImageFilter()); |
| 1448 | |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1449 | // Check if the source device is really a bitmapdevice (because that's what we returned |
| 1450 | // from createDevice (likely due to an imagefilter) |
| 1451 | SkPixmap pmap; |
| 1452 | if (device->peekPixels(&pmap)) { |
| 1453 | SkBitmap bitmap; |
| 1454 | bitmap.installPixels(pmap); |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 1455 | this->drawSprite(d, bitmap, x, y, paint); |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1456 | return; |
| 1457 | } |
| 1458 | |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 1459 | // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses. |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1460 | SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1461 | |
| 1462 | SkScalar scalarX = SkIntToScalar(x); |
| 1463 | SkScalar scalarY = SkIntToScalar(y); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1464 | for (const RectWithData& l : pdfDevice->fLinkToURLs) { |
| 1465 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1466 | fLinkToURLs.emplace_back(r, l.data.get()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1467 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1468 | for (const RectWithData& l : pdfDevice->fLinkToDestinations) { |
| 1469 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1470 | fLinkToDestinations.emplace_back(r, l.data.get()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1471 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1472 | for (const NamedDestination& d : pdfDevice->fNamedDestinations) { |
| 1473 | SkPoint p = d.point + SkPoint::Make(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1474 | fNamedDestinations.emplace_back(d.nameData.get(), p); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
ctguil@chromium.org | f4ff39c | 2011-05-24 19:55:05 +0000 | [diff] [blame] | 1477 | if (pdfDevice->isContentEmpty()) { |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1478 | return; |
| 1479 | } |
| 1480 | |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 1481 | SkMatrix matrix; |
reed@google.com | a6d59f6 | 2011-03-07 21:29:21 +0000 | [diff] [blame] | 1482 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1483 | ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1484 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1485 | return; |
| 1486 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1487 | if (content.needShape()) { |
| 1488 | SkPath shape; |
| 1489 | shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
vandebo@chromium.org | fd3c8c2 | 2013-10-30 21:00:47 +0000 | [diff] [blame] | 1490 | SkIntToScalar(device->width()), |
| 1491 | SkIntToScalar(device->height()))); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1492 | content.setShape(shape); |
| 1493 | } |
| 1494 | if (!content.needSource()) { |
| 1495 | return; |
| 1496 | } |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 1497 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1498 | sk_sp<SkPDFObject> xObject = pdfDevice->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1499 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1500 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1503 | sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 1504 | return SkSurface::MakeRaster(info, &props); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1507 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1508 | sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const { |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1509 | SkTDArray<SkPDFObject*> fonts; |
| 1510 | fonts.setReserve(fFontResources.count()); |
| 1511 | for (SkPDFFont* font : fFontResources) { |
| 1512 | fonts.push(font); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1513 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1514 | return SkPDFResourceDict::Make( |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1515 | &fGraphicStateResources, |
| 1516 | &fShaderResources, |
| 1517 | &fXObjectResources, |
| 1518 | &fonts); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1521 | sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1522 | auto mediaBox = sk_make_sp<SkPDFArray>(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1523 | mediaBox->reserve(4); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 1524 | mediaBox->appendInt(0); |
| 1525 | mediaBox->appendInt(0); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1526 | mediaBox->appendInt(fPageSize.width()); |
| 1527 | mediaBox->appendInt(fPageSize.height()); |
| 1528 | return mediaBox; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1531 | std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const { |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1532 | SkDynamicMemoryWStream buffer; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1533 | if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) { |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1534 | SkPDFUtils::AppendTransform(fInitialTransform, &buffer); |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 1535 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1536 | |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1537 | GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, &buffer); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1538 | for (const auto& entry : fContentEntries) { |
| 1539 | SkPoint translation; |
| 1540 | translation.iset(this->getOrigin()); |
| 1541 | translation.negate(); |
| 1542 | gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion, |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 1543 | translation, SkRect::Make(this->getGlobalBounds())); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1544 | gsState.updateMatrix(entry.fState.fMatrix); |
| 1545 | gsState.updateDrawingState(entry.fState); |
| 1546 | |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1547 | entry.fContent.writeToStream(&buffer); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1548 | } |
| 1549 | gsState.drainStack(); |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 1550 | if (buffer.bytesWritten() > 0) { |
| 1551 | return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream()); |
| 1552 | } else { |
| 1553 | return skstd::make_unique<SkMemoryStream>(); |
| 1554 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1557 | /* Draws an inverse filled path by using Path Ops to compute the positive |
| 1558 | * inverse using the current clip as the inverse bounds. |
| 1559 | * Return true if this was an inverse path and was properly handled, |
| 1560 | * otherwise returns false and the normal drawing routine should continue, |
| 1561 | * either as a (incorrect) fallback or because the path was not inverse |
| 1562 | * in the first place. |
| 1563 | */ |
| 1564 | bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath, |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1565 | const SkPaint& paint, bool pathIsMutable, |
| 1566 | const SkMatrix* prePathMatrix) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1567 | if (!origPath.isInverseFillType()) { |
| 1568 | return false; |
| 1569 | } |
| 1570 | |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 1571 | if (d.fClipStack->isEmpty(this->getGlobalBounds())) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1572 | return false; |
| 1573 | } |
| 1574 | |
| 1575 | SkPath modifiedPath; |
| 1576 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
| 1577 | SkPaint noInversePaint(paint); |
| 1578 | |
| 1579 | // Merge stroking operations into final path. |
| 1580 | if (SkPaint::kStroke_Style == paint.getStyle() || |
| 1581 | SkPaint::kStrokeAndFill_Style == paint.getStyle()) { |
| 1582 | bool doFillPath = paint.getFillPath(origPath, &modifiedPath); |
| 1583 | if (doFillPath) { |
| 1584 | noInversePaint.setStyle(SkPaint::kFill_Style); |
| 1585 | noInversePaint.setStrokeWidth(0); |
| 1586 | pathPtr = &modifiedPath; |
| 1587 | } else { |
| 1588 | // To be consistent with the raster output, hairline strokes |
| 1589 | // are rendered as non-inverted. |
| 1590 | modifiedPath.toggleInverseFillType(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1591 | drawPath(d, modifiedPath, paint, nullptr, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1592 | return true; |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | // Get bounds of clip in current transform space |
| 1597 | // (clip bounds are given in device space). |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1598 | SkMatrix transformInverse; |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1599 | SkMatrix totalMatrix = *d.fMatrix; |
| 1600 | if (prePathMatrix) { |
| 1601 | totalMatrix.preConcat(*prePathMatrix); |
| 1602 | } |
| 1603 | if (!totalMatrix.invert(&transformInverse)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1604 | return false; |
| 1605 | } |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 1606 | SkRect bounds = d.fClipStack->bounds(this->getGlobalBounds()); |
| 1607 | SkIPoint deviceOrigin = this->getOrigin(); |
| 1608 | bounds.offset(-deviceOrigin.x(), -deviceOrigin.y()); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1609 | transformInverse.mapRect(&bounds); |
| 1610 | |
| 1611 | // Extend the bounds by the line width (plus some padding) |
| 1612 | // so the edge doesn't cause a visible stroke. |
| 1613 | bounds.outset(paint.getStrokeWidth() + SK_Scalar1, |
| 1614 | paint.getStrokeWidth() + SK_Scalar1); |
| 1615 | |
| 1616 | if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) { |
| 1617 | return false; |
| 1618 | } |
| 1619 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1620 | drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1621 | return true; |
| 1622 | } |
| 1623 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1624 | void SkPDFDevice::handlePointAnnotation(const SkPoint& point, |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1625 | const SkMatrix& matrix, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1626 | const char key[], SkData* value) { |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame^] | 1627 | SkASSERT(value); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1628 | if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) { |
| 1629 | SkPoint transformedPoint; |
| 1630 | matrix.mapXY(point.x(), point.y(), &transformedPoint); |
| 1631 | fNamedDestinations.emplace_back(value, transformedPoint); |
| 1632 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1633 | } |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 1634 | |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame^] | 1635 | void SkPDFDevice::handleRectAnnotation(const SkRect& rect, |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1636 | const SkDraw& d, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1637 | const char key[], SkData* value) { |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame^] | 1638 | SkASSERT(value); |
| 1639 | // Convert to path to handle non-90-degree rotations. |
| 1640 | SkPath path; |
| 1641 | path.addRect(rect); |
| 1642 | path.transform(*d.fMatrix, &path); |
| 1643 | SkPath clip; |
| 1644 | (void)d.fClipStack->asPath(&clip); |
| 1645 | SkIPoint deviceOrigin = this->getOrigin(); |
| 1646 | clip.offset(-deviceOrigin.x(), -deviceOrigin.y()); |
| 1647 | // Offset here to make clip and path share coordinates. |
| 1648 | // We remove the offset in SkPDFDevice::drawDevice(). |
| 1649 | Op(clip, path, kIntersect_SkPathOp, &path); |
| 1650 | // PDF wants a rectangle only. |
| 1651 | SkRect transformedRect = path.getBounds(); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1652 | |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame^] | 1653 | if (!transformedRect.isEmpty()) { |
| 1654 | if (!strcmp(SkAnnotationKeys::URL_Key(), key)) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1655 | fLinkToURLs.emplace_back(transformedRect, value); |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame^] | 1656 | } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1657 | fLinkToDestinations.emplace_back(transformedRect, value); |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1658 | } |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1659 | } |
halcanary | 438de49 | 2015-04-28 06:21:01 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1662 | void SkPDFDevice::appendAnnotations(SkPDFArray* array) const { |
| 1663 | array->reserve(fLinkToURLs.count() + fLinkToDestinations.count()); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1664 | for (const RectWithData& rectWithURL : fLinkToURLs) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1665 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1666 | fInitialTransform.mapRect(&r, rectWithURL.rect); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1667 | array->appendObject(create_link_to_url(rectWithURL.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1668 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1669 | for (const RectWithData& linkToDestination : fLinkToDestinations) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1670 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1671 | fInitialTransform.mapRect(&r, linkToDestination.rect); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1672 | array->appendObject( |
| 1673 | create_link_named_dest(linkToDestination.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1674 | } |
| 1675 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1676 | |
halcanary | 6d62270 | 2015-03-25 08:45:42 -0700 | [diff] [blame] | 1677 | void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const { |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1678 | for (const NamedDestination& dest : fNamedDestinations) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1679 | auto pdfDest = sk_make_sp<SkPDFArray>(); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1680 | pdfDest->reserve(5); |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 1681 | pdfDest->appendObjRef(sk_ref_sp(page)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1682 | pdfDest->appendName("XYZ"); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1683 | SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1684 | pdfDest->appendScalar(p.x()); |
| 1685 | pdfDest->appendScalar(p.y()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1686 | pdfDest->appendInt(0); // Leave zoom unchanged |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1687 | SkString name(static_cast<const char*>(dest.nameData->data())); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1688 | dict->insertObject(name, std::move(pdfDest)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1689 | } |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1692 | sk_sp<SkPDFObject> SkPDFDevice::makeFormXObjectFromDevice() { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1693 | SkMatrix inverseTransform = SkMatrix::I(); |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1694 | if (!fInitialTransform.isIdentity()) { |
| 1695 | if (!fInitialTransform.invert(&inverseTransform)) { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1696 | SkDEBUGFAIL("Layer initial transform should be invertible."); |
| 1697 | inverseTransform.reset(); |
| 1698 | } |
| 1699 | } |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1700 | sk_sp<SkPDFObject> xobject = |
| 1701 | SkPDFMakeFormXObject(this->content(), this->copyMediaBox(), |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1702 | this->makeResourceDict(), inverseTransform, nullptr); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1703 | // We always draw the form xobjects that we create back into the device, so |
| 1704 | // we simply preserve the font usage instead of pulling it out and merging |
| 1705 | // it back in later. |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1706 | this->cleanUp(); // Reset this device to have no content. |
| 1707 | this->init(); |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1708 | return xobject; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1711 | void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex, |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1712 | sk_sp<SkPDFObject> mask, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1713 | const SkClipStack* clipStack, |
| 1714 | const SkRegion& clipRegion, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1715 | SkBlendMode mode, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1716 | bool invertClip) { |
| 1717 | if (clipRegion.isEmpty() && !invertClip) { |
| 1718 | return; |
| 1719 | } |
| 1720 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1721 | sk_sp<SkPDFDict> sMaskGS = SkPDFGraphicState::GetSMaskGraphicState( |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1722 | std::move(mask), invertClip, |
| 1723 | SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon()); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1724 | |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1725 | SkMatrix identity; |
| 1726 | identity.reset(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1727 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1728 | paint.setBlendMode(mode); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1729 | ScopedContentEntry content(this, clipStack, clipRegion, identity, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1730 | if (!content.entry()) { |
| 1731 | return; |
| 1732 | } |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1733 | SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1734 | &content.entry()->fContent); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1735 | SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1736 | |
halcanary | 1437c1e | 2016-03-13 18:30:24 -0700 | [diff] [blame] | 1737 | // Call makeNoSmaskGraphicState() instead of |
| 1738 | // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon |
| 1739 | // can deduplicate. |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1740 | sMaskGS = fDocument->canon()->makeNoSmaskGraphicState(); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1741 | SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1742 | &content.entry()->fContent); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1745 | SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack, |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 1746 | const SkRegion& clipRegion, |
| 1747 | const SkMatrix& matrix, |
| 1748 | const SkPaint& paint, |
| 1749 | bool hasText, |
| 1750 | sk_sp<SkPDFObject>* dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1751 | *dst = nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1752 | if (clipRegion.isEmpty()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1753 | return nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1754 | } |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 1755 | SkASSERT(clipStack); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1756 | SkBlendMode blendMode = paint.getBlendMode(); |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1757 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1758 | // For the following modes, we want to handle source and destination |
| 1759 | // separately, so make an object of what's already there. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1760 | if (blendMode == SkBlendMode::kClear || |
| 1761 | blendMode == SkBlendMode::kSrc || |
| 1762 | blendMode == SkBlendMode::kSrcIn || |
| 1763 | blendMode == SkBlendMode::kDstIn || |
| 1764 | blendMode == SkBlendMode::kSrcOut || |
| 1765 | blendMode == SkBlendMode::kDstOut || |
| 1766 | blendMode == SkBlendMode::kSrcATop || |
| 1767 | blendMode == SkBlendMode::kDstATop || |
| 1768 | blendMode == SkBlendMode::kModulate) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1769 | if (!isContentEmpty()) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1770 | *dst = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1771 | SkASSERT(isContentEmpty()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1772 | } else if (blendMode != SkBlendMode::kSrc && |
| 1773 | blendMode != SkBlendMode::kSrcOut) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1774 | // Except for Src and SrcOut, if there isn't anything already there, |
| 1775 | // then we're done. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1776 | return nullptr; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1777 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1778 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1779 | // TODO(vandebo): Figure out how/if we can handle the following modes: |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1780 | // Xor, Plus. |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1781 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1782 | // Dst xfer mode doesn't draw source at all. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1783 | if (blendMode == SkBlendMode::kDst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1784 | return nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1787 | SkPDFDevice::ContentEntry* entry; |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 1788 | if (fContentEntries.back() && fContentEntries.back()->fContent.bytesWritten() == 0) { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1789 | entry = fContentEntries.back(); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1790 | } else if (blendMode != SkBlendMode::kDstOver) { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1791 | entry = fContentEntries.emplace_back(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1792 | } else { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1793 | entry = fContentEntries.emplace_front(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1794 | } |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1795 | populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1796 | hasText, &entry->fState); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1797 | return entry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1800 | void SkPDFDevice::finishContentEntry(SkBlendMode blendMode, |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1801 | sk_sp<SkPDFObject> dst, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1802 | SkPath* shape) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1803 | if (blendMode != SkBlendMode::kClear && |
| 1804 | blendMode != SkBlendMode::kSrc && |
| 1805 | blendMode != SkBlendMode::kDstOver && |
| 1806 | blendMode != SkBlendMode::kSrcIn && |
| 1807 | blendMode != SkBlendMode::kDstIn && |
| 1808 | blendMode != SkBlendMode::kSrcOut && |
| 1809 | blendMode != SkBlendMode::kDstOut && |
| 1810 | blendMode != SkBlendMode::kSrcATop && |
| 1811 | blendMode != SkBlendMode::kDstATop && |
| 1812 | blendMode != SkBlendMode::kModulate) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1813 | SkASSERT(!dst); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1814 | return; |
| 1815 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1816 | if (blendMode == SkBlendMode::kDstOver) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1817 | SkASSERT(!dst); |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 1818 | if (fContentEntries.front()->fContent.bytesWritten() == 0) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1819 | // For DstOver, an empty content entry was inserted before the rest |
| 1820 | // of the content entries. If nothing was drawn, it needs to be |
| 1821 | // removed. |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1822 | fContentEntries.pop_front(); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1823 | } |
| 1824 | return; |
| 1825 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1826 | if (!dst) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1827 | SkASSERT(blendMode == SkBlendMode::kSrc || |
| 1828 | blendMode == SkBlendMode::kSrcOut); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1829 | return; |
| 1830 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1831 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1832 | SkASSERT(dst); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1833 | SkASSERT(fContentEntries.count() == 1); |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1834 | // Changing the current content into a form-xobject will destroy the clip |
| 1835 | // objects which is fine since the xobject will already be clipped. However |
| 1836 | // if source has shape, we need to clip it too, so a copy of the clip is |
| 1837 | // saved. |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1838 | |
| 1839 | SkClipStack clipStack = fContentEntries.front()->fState.fClipStack; |
| 1840 | SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1841 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1842 | SkMatrix identity; |
| 1843 | identity.reset(); |
| 1844 | SkPaint stockPaint; |
| 1845 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1846 | sk_sp<SkPDFObject> srcFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1847 | if (isContentEmpty()) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1848 | // If nothing was drawn and there's no shape, then the draw was a |
| 1849 | // no-op, but dst needs to be restored for that to be true. |
| 1850 | // If there is shape, then an empty source with Src, SrcIn, SrcOut, |
| 1851 | // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop |
| 1852 | // reduces to Dst. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1853 | if (shape == nullptr || blendMode == SkBlendMode::kDstOut || |
| 1854 | blendMode == SkBlendMode::kSrcATop) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1855 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1856 | fExistingClipRegion, identity, |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1857 | stockPaint); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1858 | // TODO: addXObjectResource take sk_sp |
| 1859 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()), |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1860 | &content.entry()->fContent); |
| 1861 | return; |
| 1862 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1863 | blendMode = SkBlendMode::kClear; |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1864 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1865 | } else { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1866 | SkASSERT(fContentEntries.count() == 1); |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1867 | srcFormXObject = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1870 | // TODO(vandebo) srcFormXObject may contain alpha, but here we want it |
| 1871 | // without alpha. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1872 | if (blendMode == SkBlendMode::kSrcATop) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1873 | // TODO(vandebo): In order to properly support SrcATop we have to track |
| 1874 | // the shape of what's been drawn at all times. It's the intersection of |
| 1875 | // the non-transparent parts of the device and the outlines (shape) of |
| 1876 | // all images and devices drawn. |
| 1877 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst, |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1878 | &fExistingClipStack, fExistingClipRegion, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1879 | SkBlendMode::kSrcOver, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1880 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1881 | if (shape != nullptr) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1882 | // Draw shape into a form-xobject. |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1883 | SkRasterClip rc(clipRegion); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1884 | SkDraw d; |
| 1885 | d.fMatrix = &identity; |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1886 | d.fRC = &rc; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1887 | d.fClipStack = &clipStack; |
| 1888 | SkPaint filledPaint; |
| 1889 | filledPaint.setColor(SK_ColorBLACK); |
| 1890 | filledPaint.setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1891 | this->drawPath(d, *shape, filledPaint, nullptr, true); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1892 | drawFormXObjectWithMask(addXObjectResource(dst.get()), |
| 1893 | this->makeFormXObjectFromDevice(), |
| 1894 | &fExistingClipStack, fExistingClipRegion, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1895 | SkBlendMode::kSrcOver, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1896 | |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1897 | } else { |
| 1898 | drawFormXObjectWithMask(addXObjectResource(dst.get()), srcFormXObject, |
| 1899 | &fExistingClipStack, fExistingClipRegion, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1900 | SkBlendMode::kSrcOver, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1901 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1904 | if (blendMode == SkBlendMode::kClear) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1905 | return; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1906 | } else if (blendMode == SkBlendMode::kSrc || |
| 1907 | blendMode == SkBlendMode::kDstATop) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1908 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1909 | fExistingClipRegion, identity, stockPaint); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1910 | if (content.entry()) { |
| 1911 | SkPDFUtils::DrawFormXObject( |
| 1912 | this->addXObjectResource(srcFormXObject.get()), |
| 1913 | &content.entry()->fContent); |
| 1914 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1915 | if (blendMode == SkBlendMode::kSrc) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1916 | return; |
| 1917 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1918 | } else if (blendMode == SkBlendMode::kSrcATop) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1919 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1920 | fExistingClipRegion, identity, stockPaint); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1921 | if (content.entry()) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1922 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()), |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1923 | &content.entry()->fContent); |
| 1924 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1927 | SkASSERT(blendMode == SkBlendMode::kSrcIn || |
| 1928 | blendMode == SkBlendMode::kDstIn || |
| 1929 | blendMode == SkBlendMode::kSrcOut || |
| 1930 | blendMode == SkBlendMode::kDstOut || |
| 1931 | blendMode == SkBlendMode::kSrcATop || |
| 1932 | blendMode == SkBlendMode::kDstATop || |
| 1933 | blendMode == SkBlendMode::kModulate); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1934 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1935 | if (blendMode == SkBlendMode::kSrcIn || |
| 1936 | blendMode == SkBlendMode::kSrcOut || |
| 1937 | blendMode == SkBlendMode::kSrcATop) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1938 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), |
| 1939 | std::move(dst), |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1940 | &fExistingClipStack, fExistingClipRegion, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1941 | SkBlendMode::kSrcOver, |
| 1942 | blendMode == SkBlendMode::kSrcOut); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1943 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1944 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1945 | SkBlendMode mode = SkBlendMode::kSrcOver; |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1946 | int resourceID = addXObjectResource(dst.get()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1947 | if (blendMode == SkBlendMode::kModulate) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1948 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1949 | std::move(dst), &fExistingClipStack, |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1950 | fExistingClipRegion, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1951 | SkBlendMode::kSrcOver, false); |
| 1952 | mode = SkBlendMode::kMultiply; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1953 | } |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1954 | drawFormXObjectWithMask(resourceID, std::move(srcFormXObject), |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1955 | &fExistingClipStack, fExistingClipRegion, mode, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1956 | blendMode == SkBlendMode::kDstOut); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1957 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1958 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1961 | bool SkPDFDevice::isContentEmpty() { |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 1962 | if (!fContentEntries.front() || fContentEntries.front()->fContent.bytesWritten() == 0) { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1963 | SkASSERT(fContentEntries.count() <= 1); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1964 | return true; |
| 1965 | } |
| 1966 | return false; |
| 1967 | } |
| 1968 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1969 | void SkPDFDevice::populateGraphicStateEntryFromPaint( |
| 1970 | const SkMatrix& matrix, |
| 1971 | const SkClipStack& clipStack, |
| 1972 | const SkRegion& clipRegion, |
| 1973 | const SkPaint& paint, |
| 1974 | bool hasText, |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1975 | SkPDFDevice::GraphicStateEntry* entry) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1976 | NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false); |
| 1977 | NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false); |
| 1978 | NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1979 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1980 | entry->fMatrix = matrix; |
| 1981 | entry->fClipStack = clipStack; |
| 1982 | entry->fClipRegion = clipRegion; |
vandebo@chromium.org | da6c569 | 2012-06-28 21:37:20 +0000 | [diff] [blame] | 1983 | entry->fColor = SkColorSetA(paint.getColor(), 0xFF); |
| 1984 | entry->fShaderIndex = -1; |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 1985 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1986 | // PDF treats a shader as a color, so we only set one or the other. |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1987 | sk_sp<SkPDFObject> pdfShader; |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1988 | SkShader* shader = paint.getShader(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1989 | SkColor color = paint.getColor(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1990 | if (shader) { |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1991 | if (SkShader::kColor_GradientType == shader->asAGradient(nullptr)) { |
| 1992 | // We don't have to set a shader just for a color. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1993 | SkShader::GradientInfo gradientInfo; |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1994 | SkColor gradientColor = SK_ColorBLACK; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1995 | gradientInfo.fColors = &gradientColor; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1996 | gradientInfo.fColorOffsets = nullptr; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1997 | gradientInfo.fColorCount = 1; |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1998 | SkAssertResult(shader->asAGradient(&gradientInfo) == SkShader::kColor_GradientType); |
| 1999 | entry->fColor = SkColorSetA(gradientColor, 0xFF); |
| 2000 | color = gradientColor; |
| 2001 | } else { |
| 2002 | // PDF positions patterns relative to the initial transform, so |
| 2003 | // we need to apply the current transform to the shader parameters. |
| 2004 | SkMatrix transform = matrix; |
| 2005 | transform.postConcat(fInitialTransform); |
| 2006 | |
| 2007 | // PDF doesn't support kClamp_TileMode, so we simulate it by making |
| 2008 | // a pattern the size of the current clip. |
Hal Canary | a41c2aa | 2017-02-22 16:32:34 -0500 | [diff] [blame] | 2009 | SkRect clipStackBounds = clipStack.bounds(this->getGlobalBounds()); |
| 2010 | SkIPoint deviceOrigin = this->getOrigin(); |
| 2011 | clipStackBounds.offset(-deviceOrigin.x(), -deviceOrigin.y()); |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 2012 | |
| 2013 | // We need to apply the initial transform to bounds in order to get |
| 2014 | // bounds in a consistent coordinate system. |
Hal Canary | a41c2aa | 2017-02-22 16:32:34 -0500 | [diff] [blame] | 2015 | fInitialTransform.mapRect(&clipStackBounds); |
| 2016 | SkIRect bounds; |
| 2017 | clipStackBounds.roundOut(&bounds); |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 2018 | |
| 2019 | SkScalar rasterScale = |
| 2020 | SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE; |
| 2021 | pdfShader = SkPDFShader::GetPDFShader( |
| 2022 | fDocument, fRasterDpi, shader, transform, bounds, rasterScale); |
| 2023 | |
| 2024 | if (pdfShader.get()) { |
| 2025 | // pdfShader has been canonicalized so we can directly compare |
| 2026 | // pointers. |
| 2027 | int resourceIndex = fShaderResources.find(pdfShader.get()); |
| 2028 | if (resourceIndex < 0) { |
| 2029 | resourceIndex = fShaderResources.count(); |
| 2030 | fShaderResources.push(pdfShader.get()); |
| 2031 | pdfShader.get()->ref(); |
| 2032 | } |
| 2033 | entry->fShaderIndex = resourceIndex; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 2034 | } |
| 2035 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 2036 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2037 | |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 2038 | sk_sp<SkPDFGraphicState> newGraphicState; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 2039 | if (color == paint.getColor()) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 2040 | newGraphicState.reset( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 2041 | SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 2042 | } else { |
| 2043 | SkPaint newPaint = paint; |
| 2044 | newPaint.setColor(color); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 2045 | newGraphicState.reset( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 2046 | SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 2047 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 2048 | int resourceIndex = addGraphicStateResource(newGraphicState.get()); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 2049 | entry->fGraphicStateIndex = resourceIndex; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2050 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 2051 | if (hasText) { |
| 2052 | entry->fTextScaleX = paint.getTextScaleX(); |
| 2053 | entry->fTextFill = paint.getStyle(); |
| 2054 | } else { |
| 2055 | entry->fTextScaleX = 0; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 2059 | int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 2060 | // Assumes that gs has been canonicalized (so we can directly compare |
| 2061 | // pointers). |
| 2062 | int result = fGraphicStateResources.find(gs); |
| 2063 | if (result < 0) { |
| 2064 | result = fGraphicStateResources.count(); |
| 2065 | fGraphicStateResources.push(gs); |
| 2066 | gs->ref(); |
| 2067 | } |
| 2068 | return result; |
| 2069 | } |
| 2070 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2071 | int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 2072 | // TODO(halcanary): make this take a sk_sp<SkPDFObject> |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2073 | // Assumes that xobject has been canonicalized (so we can directly compare |
| 2074 | // pointers). |
| 2075 | int result = fXObjectResources.find(xObject); |
| 2076 | if (result < 0) { |
| 2077 | result = fXObjectResources.count(); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 2078 | fXObjectResources.push(SkRef(xObject)); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2079 | } |
| 2080 | return result; |
| 2081 | } |
| 2082 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 2083 | int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 2084 | sk_sp<SkPDFFont> newFont( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 2085 | SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID)); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 2086 | if (!newFont) { |
| 2087 | return -1; |
| 2088 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2089 | int resourceIndex = fFontResources.find(newFont.get()); |
| 2090 | if (resourceIndex < 0) { |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 2091 | fDocument->registerFont(newFont.get()); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2092 | resourceIndex = fFontResources.count(); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 2093 | fFontResources.push(newFont.release()); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2094 | } |
| 2095 | return resourceIndex; |
| 2096 | } |
| 2097 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2098 | static SkSize rect_to_size(const SkRect& r) { |
| 2099 | return SkSize::Make(r.width(), r.height()); |
| 2100 | } |
| 2101 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2102 | static sk_sp<SkImage> color_filter(const SkImageSubset& imageSubset, |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2103 | SkColorFilter* colorFilter) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2104 | auto surface = |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2105 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageSubset.dimensions())); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2106 | SkASSERT(surface); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2107 | SkCanvas* canvas = surface->getCanvas(); |
| 2108 | canvas->clear(SK_ColorTRANSPARENT); |
| 2109 | SkPaint paint; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 2110 | paint.setColorFilter(sk_ref_sp(colorFilter)); |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2111 | imageSubset.draw(canvas, &paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2112 | canvas->flush(); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2113 | return surface->makeImageSnapshot(); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | //////////////////////////////////////////////////////////////////////////////// |
| 2117 | void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix, |
| 2118 | const SkClipStack* clipStack, |
| 2119 | const SkRegion& origClipRegion, |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2120 | SkImageSubset imageSubset, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2121 | const SkPaint& paint) { |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2122 | if (imageSubset.dimensions().isZero()) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2123 | return; |
| 2124 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2125 | #ifdef SK_PDF_IMAGE_STATS |
| 2126 | gDrawImageCalls.fetch_add(1); |
| 2127 | #endif |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2128 | SkMatrix matrix = origMatrix; |
| 2129 | SkRegion perspectiveBounds; |
| 2130 | const SkRegion* clipRegion = &origClipRegion; |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2131 | |
| 2132 | // Rasterize the bitmap using perspective in a new bitmap. |
| 2133 | if (origMatrix.hasPerspective()) { |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2134 | if (fRasterDpi == 0) { |
| 2135 | return; |
| 2136 | } |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2137 | // Transform the bitmap in the new space, without taking into |
| 2138 | // account the initial transform. |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2139 | SkPath perspectiveOutline; |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2140 | SkRect imageBounds = SkRect::Make(imageSubset.bounds()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2141 | perspectiveOutline.addRect(imageBounds); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2142 | perspectiveOutline.transform(origMatrix); |
| 2143 | |
| 2144 | // TODO(edisonn): perf - use current clip too. |
| 2145 | // Retrieve the bounds of the new shape. |
| 2146 | SkRect bounds = perspectiveOutline.getBounds(); |
| 2147 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2148 | // Transform the bitmap in the new space, taking into |
| 2149 | // account the initial transform. |
| 2150 | SkMatrix total = origMatrix; |
| 2151 | total.postConcat(fInitialTransform); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2152 | SkScalar dpiScale = SkIntToScalar(fRasterDpi) / |
| 2153 | SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE); |
| 2154 | total.postScale(dpiScale, dpiScale); |
| 2155 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2156 | SkPath physicalPerspectiveOutline; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2157 | physicalPerspectiveOutline.addRect(imageBounds); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2158 | physicalPerspectiveOutline.transform(total); |
| 2159 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2160 | SkRect physicalPerspectiveBounds = |
| 2161 | physicalPerspectiveOutline.getBounds(); |
| 2162 | SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width(); |
| 2163 | SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2164 | |
| 2165 | // TODO(edisonn): A better approach would be to use a bitmap shader |
| 2166 | // (in clamp mode) and draw a rect over the entire bounding box. Then |
| 2167 | // intersect perspectiveOutline to the clip. That will avoid introducing |
| 2168 | // alpha to the image while still giving good behavior at the edge of |
| 2169 | // the image. Avoiding alpha will reduce the pdf size and generation |
| 2170 | // CPU time some. |
| 2171 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2172 | SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil(); |
| 2173 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2174 | auto surface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2175 | if (!surface) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 2176 | return; |
| 2177 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2178 | SkCanvas* canvas = surface->getCanvas(); |
| 2179 | canvas->clear(SK_ColorTRANSPARENT); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2180 | |
| 2181 | SkScalar deltaX = bounds.left(); |
| 2182 | SkScalar deltaY = bounds.top(); |
| 2183 | |
| 2184 | SkMatrix offsetMatrix = origMatrix; |
| 2185 | offsetMatrix.postTranslate(-deltaX, -deltaY); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2186 | offsetMatrix.postScale(scaleX, scaleY); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2187 | |
| 2188 | // Translate the draw in the new canvas, so we perfectly fit the |
| 2189 | // shape in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2190 | canvas->setMatrix(offsetMatrix); |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2191 | imageSubset.draw(canvas, nullptr); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2192 | // Make sure the final bits are in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2193 | canvas->flush(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2194 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2195 | // In the new space, we use the identity matrix translated |
| 2196 | // and scaled to reflect DPI. |
| 2197 | matrix.setScale(1 / scaleX, 1 / scaleY); |
| 2198 | matrix.postTranslate(deltaX, deltaY); |
| 2199 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2200 | perspectiveBounds.setRect(bounds.roundOut()); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2201 | clipRegion = &perspectiveBounds; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2202 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2203 | imageSubset = SkImageSubset(surface->makeImageSnapshot()); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2206 | SkMatrix scaled; |
| 2207 | // Adjust for origin flip. |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 2208 | scaled.setScale(SK_Scalar1, -SK_Scalar1); |
| 2209 | scaled.postTranslate(0, SK_Scalar1); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2210 | // Scale the image up from 1x1 to WxH. |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2211 | SkIRect subset = imageSubset.bounds(); |
| 2212 | scaled.postScale(SkIntToScalar(imageSubset.dimensions().width()), |
| 2213 | SkIntToScalar(imageSubset.dimensions().height())); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2214 | scaled.postConcat(matrix); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2215 | ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2216 | if (!content.entry()) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2217 | return; |
| 2218 | } |
| 2219 | if (content.needShape()) { |
| 2220 | SkPath shape; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2221 | shape.addRect(SkRect::Make(subset)); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2222 | shape.transform(matrix); |
| 2223 | content.setShape(shape); |
| 2224 | } |
| 2225 | if (!content.needSource()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 2226 | return; |
| 2227 | } |
| 2228 | |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2229 | if (SkColorFilter* colorFilter = paint.getColorFilter()) { |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 2230 | // TODO(https://bug.skia.org/4378): implement colorfilter on other |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2231 | // draw calls. This code here works for all |
| 2232 | // drawBitmap*()/drawImage*() calls amd ImageFilters (which |
| 2233 | // rasterize a layer on this backend). Fortuanely, this seems |
| 2234 | // to be how Chromium impements most color-filters. |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2235 | sk_sp<SkImage> img = color_filter(imageSubset, colorFilter); |
| 2236 | imageSubset = SkImageSubset(std::move(img)); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2237 | // TODO(halcanary): de-dupe this by caching filtered images. |
| 2238 | // (maybe in the resource cache?) |
| 2239 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2240 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2241 | SkBitmapKey key = imageSubset.getKey(); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2242 | sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2243 | if (!pdfimage) { |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2244 | sk_sp<SkImage> img = imageSubset.makeImage(); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2245 | if (!img) { |
| 2246 | return; |
| 2247 | } |
| 2248 | pdfimage = SkPDFCreateBitmapObject( |
| 2249 | std::move(img), fDocument->canon()->getPixelSerializer()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2250 | if (!pdfimage) { |
| 2251 | return; |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2252 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2253 | fDocument->serialize(pdfimage); // serialize images early. |
| 2254 | fDocument->canon()->addPDFBitmap(key, pdfimage); |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2255 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2256 | // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject> |
halcanary | 3d8c33c | 2015-10-01 11:06:22 -0700 | [diff] [blame] | 2257 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 2258 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2259 | } |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2260 | |
| 2261 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2262 | |
| 2263 | #include "SkSpecialImage.h" |
| 2264 | #include "SkImageFilter.h" |
| 2265 | |
| 2266 | void SkPDFDevice::drawSpecial(const SkDraw& draw, SkSpecialImage* srcImg, int x, int y, |
| 2267 | const SkPaint& paint) { |
| 2268 | SkASSERT(!srcImg->isTextureBacked()); |
| 2269 | |
| 2270 | SkBitmap resultBM; |
| 2271 | |
| 2272 | SkImageFilter* filter = paint.getImageFilter(); |
| 2273 | if (filter) { |
| 2274 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 2275 | SkMatrix matrix = *draw.fMatrix; |
| 2276 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 2277 | const SkIRect clipBounds = |
| 2278 | draw.fClipStack->bounds(this->imageInfo().bounds()).roundOut().makeOffset(-x, -y); |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 2279 | sk_sp<SkImageFilterCache> cache(this->getImageFilterCache()); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 2280 | // TODO: Should PDF be operating in a specified color space? For now, run the filter |
| 2281 | // in the same color space as the source (this is different from all other backends). |
| 2282 | SkImageFilter::OutputProperties outputProperties(srcImg->getColorSpace()); |
| 2283 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2284 | |
| 2285 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset)); |
| 2286 | if (resultImg) { |
| 2287 | SkPaint tmpUnfiltered(paint); |
| 2288 | tmpUnfiltered.setImageFilter(nullptr); |
| 2289 | if (resultImg->getROPixels(&resultBM)) { |
| 2290 | this->drawSprite(draw, resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered); |
| 2291 | } |
| 2292 | } |
| 2293 | } else { |
| 2294 | if (srcImg->getROPixels(&resultBM)) { |
| 2295 | this->drawSprite(draw, resultBM, x, y, paint); |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) { |
| 2301 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap); |
| 2302 | } |
| 2303 | |
| 2304 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) { |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 2305 | // TODO: See comment above in drawSpecial. The color mode we use for decode should be driven |
| 2306 | // by the destination where we're going to draw thing thing (ie this device). But we don't have |
| 2307 | // a color space, so we always decode in legacy mode for now. |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 2308 | SkColorSpace* legacyColorSpace = nullptr; |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2309 | return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()), |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 2310 | image->makeNonTextureImage(), legacyColorSpace); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() { |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2314 | return nullptr; |
| 2315 | } |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 2316 | |
| 2317 | SkImageFilterCache* SkPDFDevice::getImageFilterCache() { |
| 2318 | // We always return a transient cache, so it is freed after each |
| 2319 | // filter traversal. |
| 2320 | return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize); |
| 2321 | } |