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