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