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