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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/pdf/SkPDFDevice.h" |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorFilter.h" |
| 13 | #include "include/core/SkPath.h" |
| 14 | #include "include/core/SkPathEffect.h" |
| 15 | #include "include/core/SkRRect.h" |
| 16 | #include "include/core/SkString.h" |
| 17 | #include "include/core/SkSurface.h" |
| 18 | #include "include/core/SkTextBlob.h" |
| 19 | #include "include/docs/SkPDFDocument.h" |
| 20 | #include "include/encode/SkJpegEncoder.h" |
| 21 | #include "include/pathops/SkPathOps.h" |
| 22 | #include "include/private/SkTemplates.h" |
| 23 | #include "include/private/SkTo.h" |
| 24 | #include "src/core/SkAdvancedTypefaceMetrics.h" |
| 25 | #include "src/core/SkAnnotationKeys.h" |
| 26 | #include "src/core/SkBitmapDevice.h" |
Mike Reed | bd84330 | 2019-09-27 21:05:24 -0400 | [diff] [blame] | 27 | #include "src/core/SkColorSpacePriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/core/SkDraw.h" |
| 29 | #include "src/core/SkGlyphRun.h" |
| 30 | #include "src/core/SkImageFilterCache.h" |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 31 | #include "src/core/SkImageFilter_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "src/core/SkMaskFilterBase.h" |
| 33 | #include "src/core/SkRasterClip.h" |
Herb Derby | 81e84a6 | 2020-02-14 11:47:35 -0500 | [diff] [blame] | 34 | #include "src/core/SkScalerCache.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/core/SkScopeExit.h" |
Herb Derby | 2b35b7d | 2019-05-22 16:11:13 -0400 | [diff] [blame] | 36 | #include "src/core/SkStrikeSpec.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "src/core/SkTextFormatParams.h" |
| 38 | #include "src/core/SkXfermodeInterpretation.h" |
| 39 | #include "src/pdf/SkBitmapKey.h" |
| 40 | #include "src/pdf/SkClusterator.h" |
| 41 | #include "src/pdf/SkPDFBitmap.h" |
| 42 | #include "src/pdf/SkPDFDocumentPriv.h" |
| 43 | #include "src/pdf/SkPDFFont.h" |
| 44 | #include "src/pdf/SkPDFFormXObject.h" |
| 45 | #include "src/pdf/SkPDFGraphicState.h" |
| 46 | #include "src/pdf/SkPDFResourceDict.h" |
| 47 | #include "src/pdf/SkPDFShader.h" |
| 48 | #include "src/pdf/SkPDFTypes.h" |
| 49 | #include "src/pdf/SkPDFUtils.h" |
Mike Reed | c15afe4 | 2019-12-20 14:00:41 -0500 | [diff] [blame] | 50 | #include "src/utils/SkClipStackUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 51 | #include "src/utils/SkUTF.h" |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 52 | |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 53 | #include <vector> |
| 54 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 55 | #ifndef SK_PDF_MASK_QUALITY |
| 56 | // If MASK_QUALITY is in [0,100], will be used for JpegEncoder. |
| 57 | // Otherwise, just encode masks losslessly. |
| 58 | #define SK_PDF_MASK_QUALITY 50 |
| 59 | // Since these masks are used for blurry shadows, we shouldn't need |
| 60 | // high quality. Raise this value if your shadows have visible JPEG |
| 61 | // artifacts. |
| 62 | // If SkJpegEncoder::Encode fails, we will fall back to the lossless |
| 63 | // encoding. |
| 64 | #endif |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 65 | |
Dominic Mazzoni | 8f233fc | 2020-03-06 11:20:45 -0800 | [diff] [blame] | 66 | namespace { |
| 67 | |
| 68 | // If nodeId is not zero, outputs the tags to begin a marked-content sequence |
| 69 | // for the given node ID, and then closes those tags when this object goes |
| 70 | // out of scope. |
| 71 | class ScopedOutputMarkedContentTags { |
| 72 | public: |
| 73 | ScopedOutputMarkedContentTags(int nodeId, SkPDFDocument* document, SkDynamicMemoryWStream* out) |
| 74 | : fOut(out) |
| 75 | , fMarkId(-1) { |
| 76 | if (nodeId) { |
Dominic Mazzoni | 473c9f4 | 2020-07-07 14:55:35 -0700 | [diff] [blame] | 77 | fMarkId = document->createMarkIdForNodeId(nodeId); |
Dominic Mazzoni | 8f233fc | 2020-03-06 11:20:45 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (fMarkId != -1) { |
| 81 | fOut->writeText("/P <</MCID "); |
| 82 | fOut->writeDecAsText(fMarkId); |
| 83 | fOut->writeText(" >>BDC\n"); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | ~ScopedOutputMarkedContentTags() { |
| 88 | if (fMarkId != -1) { |
| 89 | fOut->writeText("EMC\n"); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | SkDynamicMemoryWStream* fOut; |
| 95 | int fMarkId; |
| 96 | }; |
| 97 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 98 | } // namespace |
Dominic Mazzoni | 8f233fc | 2020-03-06 11:20:45 -0800 | [diff] [blame] | 99 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 100 | // Utility functions |
| 101 | |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 102 | // This function destroys the mask and either frees or takes the pixels. |
| 103 | sk_sp<SkImage> mask_to_greyscale_image(SkMask* mask) { |
| 104 | sk_sp<SkImage> img; |
| 105 | SkPixmap pm(SkImageInfo::Make(mask->fBounds.width(), mask->fBounds.height(), |
| 106 | kGray_8_SkColorType, kOpaque_SkAlphaType), |
| 107 | mask->fImage, mask->fRowBytes); |
| 108 | const int imgQuality = SK_PDF_MASK_QUALITY; |
| 109 | if (imgQuality <= 100 && imgQuality >= 0) { |
| 110 | SkDynamicMemoryWStream buffer; |
| 111 | SkJpegEncoder::Options jpegOptions; |
| 112 | jpegOptions.fQuality = imgQuality; |
| 113 | if (SkJpegEncoder::Encode(&buffer, pm, jpegOptions)) { |
| 114 | img = SkImage::MakeFromEncoded(buffer.detachAsData()); |
| 115 | SkASSERT(img); |
| 116 | if (img) { |
| 117 | SkMask::FreeImage(mask->fImage); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | if (!img) { |
| 122 | img = SkImage::MakeFromRaster(pm, [](const void* p, void*) { SkMask::FreeImage((void*)p); }, |
| 123 | nullptr); |
| 124 | } |
| 125 | *mask = SkMask(); // destructive; |
| 126 | return img; |
| 127 | } |
| 128 | |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 129 | sk_sp<SkImage> alpha_image_to_greyscale_image(const SkImage* mask) { |
| 130 | int w = mask->width(), h = mask->height(); |
| 131 | SkBitmap greyBitmap; |
| 132 | greyBitmap.allocPixels(SkImageInfo::Make(w, h, kGray_8_SkColorType, kOpaque_SkAlphaType)); |
Adlai Holler | bcfc554 | 2020-08-27 12:44:07 -0400 | [diff] [blame] | 133 | // TODO: support gpu images in pdf |
| 134 | if (!mask->readPixels(nullptr, SkImageInfo::MakeA8(w, h), |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 135 | greyBitmap.getPixels(), greyBitmap.rowBytes(), 0, 0)) { |
| 136 | return nullptr; |
| 137 | } |
Mike Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame] | 138 | greyBitmap.setImmutable(); |
| 139 | return greyBitmap.asImage(); |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 142 | static int add_resource(SkTHashSet<SkPDFIndirectReference>& resources, SkPDFIndirectReference ref) { |
| 143 | resources.add(ref); |
| 144 | return ref.fValue; |
| 145 | } |
| 146 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 147 | static void draw_points(SkCanvas::PointMode mode, |
| 148 | size_t count, |
| 149 | const SkPoint* points, |
| 150 | const SkPaint& paint, |
| 151 | const SkIRect& bounds, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 152 | SkBaseDevice* device) { |
| 153 | SkRasterClip rc(bounds); |
| 154 | SkDraw draw; |
| 155 | draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(bounds.right(), bounds.bottom()), nullptr, 0); |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 156 | draw.fMatrixProvider = device; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 157 | draw.fRC = &rc; |
| 158 | draw.drawPoints(mode, count, points, paint, device); |
| 159 | } |
| 160 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 161 | // A shader's matrix is: CTMM x LocalMatrix x WrappingLocalMatrix. We want to |
| 162 | // switch to device space, where CTM = I, while keeping the original behavior. |
| 163 | // |
| 164 | // I * LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix |
| 165 | // LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix |
| 166 | // InvLocalMatrix * LocalMatrix * NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix |
| 167 | // NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix |
| 168 | // |
| 169 | static void transform_shader(SkPaint* paint, const SkMatrix& ctm) { |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 170 | SkASSERT(!ctm.isIdentity()); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 171 | SkMatrix lm = SkPDFUtils::GetShaderLocalMatrix(paint->getShader()); |
| 172 | SkMatrix lmInv; |
| 173 | if (lm.invert(&lmInv)) { |
| 174 | SkMatrix m = SkMatrix::Concat(SkMatrix::Concat(lmInv, ctm), lm); |
| 175 | paint->setShader(paint->getShader()->makeWithLocalMatrix(m)); |
| 176 | } |
| 177 | } |
| 178 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 179 | static SkTCopyOnFirstWrite<SkPaint> clean_paint(const SkPaint& srcPaint) { |
| 180 | SkTCopyOnFirstWrite<SkPaint> paint(srcPaint); |
| 181 | // If the paint will definitely draw opaquely, replace kSrc with |
| 182 | // kSrcOver. http://crbug.com/473572 |
Mike Reed | 90f4e9f | 2021-07-07 16:05:25 -0400 | [diff] [blame] | 183 | if (!paint->isSrcOver() && |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 184 | kSrcOver_SkXfermodeInterpretation == SkInterpretXfermode(*paint, false)) |
| 185 | { |
| 186 | paint.writable()->setBlendMode(SkBlendMode::kSrcOver); |
| 187 | } |
Mike Klein | 96bce8f | 2019-10-23 11:26:46 -0500 | [diff] [blame] | 188 | if (paint->getColorFilter()) { |
| 189 | // We assume here that PDFs all draw in sRGB. |
| 190 | SkPaintPriv::RemoveColorFilter(paint.writable(), sk_srgb_singleton()); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 191 | } |
Mike Klein | 96bce8f | 2019-10-23 11:26:46 -0500 | [diff] [blame] | 192 | SkASSERT(!paint->getColorFilter()); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 193 | return paint; |
| 194 | } |
| 195 | |
| 196 | static void set_style(SkTCopyOnFirstWrite<SkPaint>* paint, SkPaint::Style style) { |
| 197 | if (paint->get()->getStyle() != style) { |
| 198 | paint->writable()->setStyle(style); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 202 | /* Calculate an inverted path's equivalent non-inverted path, given the |
| 203 | * canvas bounds. |
| 204 | * outPath may alias with invPath (since this is supported by PathOps). |
| 205 | */ |
| 206 | static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath, |
| 207 | SkPath* outPath) { |
| 208 | SkASSERT(invPath.isInverseFillType()); |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 209 | return Op(SkPath::Rect(bounds), invPath, kIntersect_SkPathOp, outPath); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Hal Canary | 8cb7376 | 2019-01-09 09:46:43 -0500 | [diff] [blame] | 212 | SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 213 | // PDF does not support image filters, so render them on CPU. |
| 214 | // Note that this rendering is done at "screen" resolution (100dpi), not |
| 215 | // printer resolution. |
Hal Canary | 8cb7376 | 2019-01-09 09:46:43 -0500 | [diff] [blame] | 216 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 217 | // TODO: It may be possible to express some filters natively using PDF |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 218 | // to improve quality and file size (https://bug.skia.org/3043) |
Hal Canary | 8cb7376 | 2019-01-09 09:46:43 -0500 | [diff] [blame] | 219 | if (layerPaint && (layerPaint->getImageFilter() || layerPaint->getColorFilter())) { |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 220 | // need to return a raster device, which we will detect in drawDevice() |
| 221 | return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 222 | } |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 223 | return new SkPDFDevice(cinfo.fInfo.dimensions(), fDocument); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 226 | // A helper class to automatically finish a ContentEntry at the end of a |
| 227 | // 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] | 228 | class ScopedContentEntry { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 229 | public: |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 230 | ScopedContentEntry(SkPDFDevice* device, |
Hal Canary | d892a9d | 2018-09-24 21:20:47 -0400 | [diff] [blame] | 231 | const SkClipStack* clipStack, |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 232 | const SkMatrix& matrix, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 233 | const SkPaint& paint, |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 234 | SkScalar textScale = 0) |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 235 | : fDevice(device) |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 236 | , fBlendMode(SkBlendMode::kSrcOver) |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 237 | , fClipStack(clipStack) |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 238 | { |
| 239 | if (matrix.hasPerspective()) { |
| 240 | NOT_IMPLEMENTED(!matrix.hasPerspective(), false); |
| 241 | return; |
| 242 | } |
Mike Reed | 90f4e9f | 2021-07-07 16:05:25 -0400 | [diff] [blame] | 243 | fBlendMode = paint.getBlendMode_or(SkBlendMode::kSrcOver); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 244 | fContentStream = |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 245 | fDevice->setUpContentEntry(clipStack, matrix, paint, textScale, &fDstFormXObject); |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 246 | } |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 247 | ScopedContentEntry(SkPDFDevice* dev, const SkPaint& paint, SkScalar textScale = 0) |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 248 | : ScopedContentEntry(dev, &dev->cs(), dev->localToDevice(), paint, textScale) {} |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 249 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 250 | ~ScopedContentEntry() { |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 251 | if (fContentStream) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 252 | SkPath* shape = &fShape; |
| 253 | if (shape->isEmpty()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 254 | shape = nullptr; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 255 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 256 | fDevice->finishContentEntry(fClipStack, fBlendMode, fDstFormXObject, shape); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 260 | explicit operator bool() const { return fContentStream != nullptr; } |
| 261 | SkDynamicMemoryWStream* stream() { return fContentStream; } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 262 | |
| 263 | /* Returns true when we explicitly need the shape of the drawing. */ |
| 264 | bool needShape() { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 265 | switch (fBlendMode) { |
| 266 | case SkBlendMode::kClear: |
| 267 | case SkBlendMode::kSrc: |
| 268 | case SkBlendMode::kSrcIn: |
| 269 | case SkBlendMode::kSrcOut: |
| 270 | case SkBlendMode::kDstIn: |
| 271 | case SkBlendMode::kDstOut: |
| 272 | case SkBlendMode::kSrcATop: |
| 273 | case SkBlendMode::kDstATop: |
| 274 | case SkBlendMode::kModulate: |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 275 | return true; |
| 276 | default: |
| 277 | return false; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* Returns true unless we only need the shape of the drawing. */ |
| 282 | bool needSource() { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 283 | if (fBlendMode == SkBlendMode::kClear) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | /* If the shape is different than the alpha component of the content, then |
| 290 | * setShape should be called with the shape. In particular, images and |
| 291 | * devices have rectangular shape. |
| 292 | */ |
| 293 | void setShape(const SkPath& shape) { |
| 294 | fShape = shape; |
| 295 | } |
| 296 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 297 | private: |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 298 | SkPDFDevice* fDevice = nullptr; |
| 299 | SkDynamicMemoryWStream* fContentStream = nullptr; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 300 | SkBlendMode fBlendMode; |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 301 | SkPDFIndirectReference fDstFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 302 | SkPath fShape; |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 303 | const SkClipStack* fClipStack; |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 306 | //////////////////////////////////////////////////////////////////////////////// |
| 307 | |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 308 | SkPDFDevice::SkPDFDevice(SkISize pageSize, SkPDFDocument* doc, const SkMatrix& transform) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 309 | : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()), |
| 310 | SkSurfaceProps(0, kUnknown_SkPixelGeometry)) |
Hal Canary | 7d06ab2 | 2018-09-10 14:39:13 -0400 | [diff] [blame] | 311 | , fInitialTransform(transform) |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 312 | , fNodeId(0) |
Hal Canary | a062258 | 2017-06-29 18:51:35 -0400 | [diff] [blame] | 313 | , fDocument(doc) |
| 314 | { |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 315 | SkASSERT(!pageSize.isEmpty()); |
Hal Canary | a062258 | 2017-06-29 18:51:35 -0400 | [diff] [blame] | 316 | } |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 317 | |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 318 | SkPDFDevice::~SkPDFDevice() = default; |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 319 | |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 320 | void SkPDFDevice::reset() { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 321 | fGraphicStateResources.reset(); |
| 322 | fXObjectResources.reset(); |
| 323 | fShaderResources.reset(); |
Hal Canary | b10f92e | 2018-11-16 17:01:50 -0500 | [diff] [blame] | 324 | fFontResources.reset(); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 325 | fContent.reset(); |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 326 | fActiveStackState = SkPDFGraphicStackState(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 329 | void SkPDFDevice::drawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame] | 330 | if (!value) { |
| 331 | return; |
| 332 | } |
Michael Ludwig | fb3f302 | 2020-02-20 13:23:58 -0500 | [diff] [blame] | 333 | // Annotations are specified in absolute coordinates, so the page xform maps from device space |
| 334 | // to the global space, and applies the document transform. |
Michael Ludwig | b32708a | 2021-05-06 10:23:24 -0400 | [diff] [blame] | 335 | SkMatrix pageXform = this->deviceToGlobal().asM33(); |
Michael Ludwig | fb3f302 | 2020-02-20 13:23:58 -0500 | [diff] [blame] | 336 | pageXform.postConcat(fDocument->currentPageTransform()); |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame] | 337 | if (rect.isEmpty()) { |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 338 | if (!strcmp(key, SkPDFGetNodeIdKey())) { |
| 339 | int nodeID; |
| 340 | if (value->size() != sizeof(nodeID)) { return; } |
| 341 | memcpy(&nodeID, value->data(), sizeof(nodeID)); |
| 342 | fNodeId = nodeID; |
| 343 | return; |
| 344 | } |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 345 | if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) { |
Michael Ludwig | fb3f302 | 2020-02-20 13:23:58 -0500 | [diff] [blame] | 346 | SkPoint p = this->localToDevice().mapXY(rect.x(), rect.y()); |
Hal Canary | 7742278 | 2019-04-09 12:57:46 -0400 | [diff] [blame] | 347 | pageXform.mapPoints(&p, 1); |
| 348 | auto pg = fDocument->currentPage(); |
| 349 | fDocument->fNamedDestinations.push_back(SkPDFNamedDestination{sk_ref_sp(value), p, pg}); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 350 | } |
| 351 | return; |
| 352 | } |
| 353 | // Convert to path to handle non-90-degree rotations. |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 354 | SkPath path = SkPath::Rect(rect).makeTransform(this->localToDevice()); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 355 | SkPath clip; |
Mike Reed | c15afe4 | 2019-12-20 14:00:41 -0500 | [diff] [blame] | 356 | SkClipStack_AsPath(this->cs(), &clip); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 357 | Op(clip, path, kIntersect_SkPathOp, &path); |
| 358 | // PDF wants a rectangle only. |
Michael Ludwig | fb3f302 | 2020-02-20 13:23:58 -0500 | [diff] [blame] | 359 | SkRect transformedRect = pageXform.mapRect(path.getBounds()); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 360 | if (transformedRect.isEmpty()) { |
| 361 | return; |
| 362 | } |
Dominic Mazzoni | c227f4a | 2020-03-24 08:27:54 -0700 | [diff] [blame] | 363 | |
| 364 | SkPDFLink::Type linkType = SkPDFLink::Type::kNone; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 365 | if (!strcmp(SkAnnotationKeys::URL_Key(), key)) { |
Dominic Mazzoni | c227f4a | 2020-03-24 08:27:54 -0700 | [diff] [blame] | 366 | linkType = SkPDFLink::Type::kUrl; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 367 | } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) { |
Dominic Mazzoni | c227f4a | 2020-03-24 08:27:54 -0700 | [diff] [blame] | 368 | linkType = SkPDFLink::Type::kNamedDestination; |
| 369 | } |
| 370 | |
| 371 | if (linkType != SkPDFLink::Type::kNone) { |
| 372 | std::unique_ptr<SkPDFLink> link = std::make_unique<SkPDFLink>( |
| 373 | linkType, value, transformedRect, fNodeId); |
| 374 | fDocument->fCurrentPageLinks.push_back(std::move(link)); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 378 | void SkPDFDevice::drawPaint(const SkPaint& srcPaint) { |
Hal Canary | abf8e41 | 2018-09-24 11:37:23 -0400 | [diff] [blame] | 379 | SkMatrix inverse; |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 380 | if (!this->localToDevice().invert(&inverse)) { |
Hal Canary | abf8e41 | 2018-09-24 11:37:23 -0400 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | SkRect bbox = this->cs().bounds(this->bounds()); |
| 384 | inverse.mapRect(&bbox); |
| 385 | bbox.roundOut(&bbox); |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 386 | if (this->hasEmptyClip()) { |
| 387 | return; |
| 388 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 389 | SkPaint newPaint = srcPaint; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 390 | newPaint.setStyle(SkPaint::kFill_Style); |
Hal Canary | abf8e41 | 2018-09-24 11:37:23 -0400 | [diff] [blame] | 391 | this->drawRect(bbox, newPaint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 394 | void SkPDFDevice::drawPoints(SkCanvas::PointMode mode, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 395 | size_t count, |
| 396 | const SkPoint* points, |
| 397 | const SkPaint& srcPaint) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 398 | if (this->hasEmptyClip()) { |
| 399 | return; |
| 400 | } |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 401 | if (count == 0) { |
| 402 | return; |
| 403 | } |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 404 | SkTCopyOnFirstWrite<SkPaint> paint(clean_paint(srcPaint)); |
| 405 | |
| 406 | |
| 407 | |
| 408 | if (SkCanvas::kPoints_PointMode != mode) { |
| 409 | set_style(&paint, SkPaint::kStroke_Style); |
| 410 | } |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 411 | |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 412 | // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath. |
| 413 | // We only use this when there's a path effect because of the overhead |
| 414 | // of multiple calls to setUpContentEntry it causes. |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 415 | if (paint->getPathEffect()) { |
Brian Osman | 9aaec36 | 2020-05-08 14:54:37 -0400 | [diff] [blame] | 416 | draw_points(mode, count, points, *paint, this->devClipBounds(), this); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 420 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 421 | if (mode == SkCanvas::kPoints_PointMode && paint->getStrokeCap() != SkPaint::kRound_Cap) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 422 | if (paint->getStrokeWidth()) { |
| 423 | // PDF won't draw a single point with square/butt caps because the |
| 424 | // orientation is ambiguous. Draw a rectangle instead. |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 425 | set_style(&paint, SkPaint::kFill_Style); |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 426 | SkScalar strokeWidth = paint->getStrokeWidth(); |
| 427 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 428 | for (size_t i = 0; i < count; i++) { |
| 429 | SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0); |
| 430 | r.inset(-halfStroke, -halfStroke); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 431 | this->drawRect(r, *paint); |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 432 | } |
| 433 | return; |
| 434 | } else { |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 435 | if (paint->getStrokeCap() != SkPaint::kRound_Cap) { |
| 436 | paint.writable()->setStrokeCap(SkPaint::kRound_Cap); |
| 437 | } |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 441 | ScopedContentEntry content(this, *paint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 442 | if (!content) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 443 | return; |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 444 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 445 | SkDynamicMemoryWStream* contentStream = content.stream(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 446 | switch (mode) { |
| 447 | case SkCanvas::kPolygon_PointMode: |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 448 | SkPDFUtils::MoveTo(points[0].fX, points[0].fY, contentStream); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 449 | for (size_t i = 1; i < count; i++) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 450 | SkPDFUtils::AppendLine(points[i].fX, points[i].fY, contentStream); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 451 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 452 | SkPDFUtils::StrokePath(contentStream); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 453 | break; |
| 454 | case SkCanvas::kLines_PointMode: |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 455 | for (size_t i = 0; i < count/2; i++) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 456 | SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, contentStream); |
| 457 | SkPDFUtils::AppendLine(points[i * 2 + 1].fX, points[i * 2 + 1].fY, contentStream); |
| 458 | SkPDFUtils::StrokePath(contentStream); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 459 | } |
| 460 | break; |
| 461 | case SkCanvas::kPoints_PointMode: |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 462 | SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap); |
| 463 | for (size_t i = 0; i < count; i++) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 464 | SkPDFUtils::MoveTo(points[i].fX, points[i].fY, contentStream); |
| 465 | SkPDFUtils::ClosePath(contentStream); |
| 466 | SkPDFUtils::StrokePath(contentStream); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 467 | } |
| 468 | break; |
| 469 | default: |
| 470 | SkASSERT(false); |
| 471 | } |
| 472 | } |
| 473 | |
Hal Canary | b4719cc | 2019-04-10 09:04:34 -0400 | [diff] [blame] | 474 | void SkPDFDevice::drawRect(const SkRect& rect, const SkPaint& paint) { |
commit-bot@chromium.org | 969fd6a | 2013-05-14 18:16:40 +0000 | [diff] [blame] | 475 | SkRect r = rect; |
| 476 | r.sort(); |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 477 | this->internalDrawPath(this->cs(), this->localToDevice(), SkPath::Rect(r), paint, true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Hal Canary | b4719cc | 2019-04-10 09:04:34 -0400 | [diff] [blame] | 480 | void SkPDFDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 481 | this->internalDrawPath(this->cs(), this->localToDevice(), SkPath::RRect(rrect), paint, true); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Hal Canary | b4719cc | 2019-04-10 09:04:34 -0400 | [diff] [blame] | 484 | void SkPDFDevice::drawOval(const SkRect& oval, const SkPaint& paint) { |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 485 | this->internalDrawPath(this->cs(), this->localToDevice(), SkPath::Oval(oval), paint, true); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Hal Canary | b4719cc | 2019-04-10 09:04:34 -0400 | [diff] [blame] | 488 | void SkPDFDevice::drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) { |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 489 | this->internalDrawPath(this->cs(), this->localToDevice(), path, paint, pathIsMutable); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 492 | void SkPDFDevice::internalDrawPathWithFilter(const SkClipStack& clipStack, |
| 493 | const SkMatrix& ctm, |
| 494 | const SkPath& origPath, |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 495 | const SkPaint& origPaint) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 496 | SkASSERT(origPaint.getMaskFilter()); |
| 497 | SkPath path(origPath); |
| 498 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 499 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 500 | SkStrokeRec::InitStyle initStyle = paint->getFillPath(path, &path) |
| 501 | ? SkStrokeRec::kFill_InitStyle |
| 502 | : SkStrokeRec::kHairline_InitStyle; |
| 503 | path.transform(ctm, &path); |
| 504 | |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 505 | SkIRect bounds = clipStack.bounds(this->bounds()).roundOut(); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 506 | SkMask sourceMask; |
| 507 | if (!SkDraw::DrawToMask(path, &bounds, paint->getMaskFilter(), &SkMatrix::I(), |
| 508 | &sourceMask, SkMask::kComputeBoundsAndRenderImage_CreateMode, |
| 509 | initStyle)) { |
| 510 | return; |
| 511 | } |
| 512 | SkAutoMaskFreeImage srcAutoMaskFreeImage(sourceMask.fImage); |
| 513 | SkMask dstMask; |
| 514 | SkIPoint margin; |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 515 | if (!as_MFB(paint->getMaskFilter())->filterMask(&dstMask, sourceMask, ctm, &margin)) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 516 | return; |
| 517 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 518 | SkIRect dstMaskBounds = dstMask.fBounds; |
| 519 | sk_sp<SkImage> mask = mask_to_greyscale_image(&dstMask); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 520 | // PDF doesn't seem to allow masking vector graphics with an Image XObject. |
| 521 | // Must mask with a Form XObject. |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 522 | sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice(); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 523 | { |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 524 | SkCanvas canvas(maskDevice); |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 525 | canvas.drawImage(mask, dstMaskBounds.x(), dstMaskBounds.y()); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 526 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 527 | if (!ctm.isIdentity() && paint->getShader()) { |
| 528 | transform_shader(paint.writable(), ctm); // Since we are using identity matrix. |
| 529 | } |
Hal Canary | d892a9d | 2018-09-24 21:20:47 -0400 | [diff] [blame] | 530 | ScopedContentEntry content(this, &clipStack, SkMatrix::I(), *paint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 531 | if (!content) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 532 | return; |
| 533 | } |
Hal Canary | 6028fa7 | 2019-04-10 15:33:06 -0400 | [diff] [blame] | 534 | this->setGraphicState(SkPDFGraphicState::GetSMaskGraphicState( |
| 535 | maskDevice->makeFormXObjectFromDevice(dstMaskBounds, true), false, |
| 536 | SkPDFGraphicState::kLuminosity_SMaskMode, fDocument), content.stream()); |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 537 | SkPDFUtils::AppendRectangle(SkRect::Make(dstMaskBounds), content.stream()); |
Mike Reed | cf0e3c6 | 2019-12-03 16:26:15 -0500 | [diff] [blame] | 538 | SkPDFUtils::PaintPath(SkPaint::kFill_Style, path.getFillType(), content.stream()); |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 539 | this->clearMaskOnGraphicState(content.stream()); |
| 540 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 541 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 542 | void SkPDFDevice::setGraphicState(SkPDFIndirectReference gs, SkDynamicMemoryWStream* content) { |
| 543 | SkPDFUtils::ApplyGraphicState(add_resource(fGraphicStateResources, gs), content); |
Hal Canary | 3b8b11e | 2018-09-29 22:31:34 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 546 | void SkPDFDevice::clearMaskOnGraphicState(SkDynamicMemoryWStream* contentStream) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 547 | // The no-softmask graphic state is used to "turn off" the mask for later draw calls. |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 548 | SkPDFIndirectReference& noSMaskGS = fDocument->fNoSmaskGraphicState; |
Hal Canary | c02de0b | 2017-06-28 13:14:03 -0400 | [diff] [blame] | 549 | if (!noSMaskGS) { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 550 | SkPDFDict tmp("ExtGState"); |
| 551 | tmp.insertName("SMask", "None"); |
| 552 | noSMaskGS = fDocument->emit(tmp); |
Hal Canary | c02de0b | 2017-06-28 13:14:03 -0400 | [diff] [blame] | 553 | } |
Hal Canary | 3b8b11e | 2018-09-29 22:31:34 -0400 | [diff] [blame] | 554 | this->setGraphicState(noSMaskGS, contentStream); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 555 | } |
| 556 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 557 | void SkPDFDevice::internalDrawPath(const SkClipStack& clipStack, |
| 558 | const SkMatrix& ctm, |
| 559 | const SkPath& origPath, |
| 560 | const SkPaint& srcPaint, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 561 | bool pathIsMutable) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 562 | if (clipStack.isEmpty(this->bounds())) { |
| 563 | return; |
| 564 | } |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 565 | SkTCopyOnFirstWrite<SkPaint> paint(clean_paint(srcPaint)); |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 566 | SkPath modifiedPath; |
| 567 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 568 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 569 | if (paint->getMaskFilter()) { |
| 570 | this->internalDrawPathWithFilter(clipStack, ctm, origPath, *paint); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 574 | SkMatrix matrix = ctm; |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 575 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 576 | if (paint->getPathEffect()) { |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 577 | if (clipStack.isEmpty(this->bounds())) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 578 | return; |
| 579 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 580 | if (!pathIsMutable) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 581 | modifiedPath = origPath; |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 582 | pathPtr = &modifiedPath; |
| 583 | pathIsMutable = true; |
| 584 | } |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 585 | if (paint->getFillPath(*pathPtr, pathPtr)) { |
| 586 | set_style(&paint, SkPaint::kFill_Style); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 587 | } else { |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 588 | set_style(&paint, SkPaint::kStroke_Style); |
| 589 | if (paint->getStrokeWidth() != 0) { |
| 590 | paint.writable()->setStrokeWidth(0); |
| 591 | } |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 592 | } |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 593 | paint.writable()->setPathEffect(nullptr); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 594 | } |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 595 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 596 | if (this->handleInversePath(*pathPtr, *paint, pathIsMutable)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 597 | return; |
| 598 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 599 | if (matrix.getType() & SkMatrix::kPerspective_Mask) { |
| 600 | if (!pathIsMutable) { |
| 601 | modifiedPath = origPath; |
| 602 | pathPtr = &modifiedPath; |
| 603 | pathIsMutable = true; |
| 604 | } |
| 605 | pathPtr->transform(matrix); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 606 | if (paint->getShader()) { |
| 607 | transform_shader(paint.writable(), matrix); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 608 | } |
| 609 | matrix = SkMatrix::I(); |
| 610 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 611 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 612 | ScopedContentEntry content(this, &clipStack, matrix, *paint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 613 | if (!content) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 614 | return; |
| 615 | } |
Hal Canary | dfaa057 | 2017-11-27 09:33:44 -0500 | [diff] [blame] | 616 | constexpr SkScalar kToleranceScale = 0.0625f; // smaller = better conics (circles). |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 617 | SkScalar matrixScale = matrix.mapRadius(1.0f); |
Hal Canary | dfaa057 | 2017-11-27 09:33:44 -0500 | [diff] [blame] | 618 | SkScalar tolerance = matrixScale > 0.0f ? kToleranceScale / matrixScale : kToleranceScale; |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 619 | bool consumeDegeratePathSegments = |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 620 | paint->getStyle() == SkPaint::kFill_Style || |
| 621 | (paint->getStrokeCap() != SkPaint::kRound_Cap && |
| 622 | paint->getStrokeCap() != SkPaint::kSquare_Cap); |
| 623 | SkPDFUtils::EmitPath(*pathPtr, paint->getStyle(), consumeDegeratePathSegments, content.stream(), |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 624 | tolerance); |
Mike Reed | cf0e3c6 | 2019-12-03 16:26:15 -0500 | [diff] [blame] | 625 | SkPDFUtils::PaintPath(paint->getStyle(), pathPtr->getFillType(), content.stream()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 628 | //////////////////////////////////////////////////////////////////////////////// |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 629 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 630 | void SkPDFDevice::drawImageRect(const SkImage* image, |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 631 | const SkRect* src, |
| 632 | const SkRect& dst, |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 633 | const SkSamplingOptions& sampling, |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 634 | const SkPaint& paint, |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 635 | SkCanvas::SrcRectConstraint) { |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 636 | SkASSERT(image); |
| 637 | this->internalDrawImageRect(SkKeyedImage(sk_ref_sp(const_cast<SkImage*>(image))), |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 638 | src, dst, sampling, paint, this->localToDevice()); |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 639 | } |
| 640 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 641 | void SkPDFDevice::drawSprite(const SkBitmap& bm, int x, int y, const SkPaint& paint) { |
| 642 | SkASSERT(!bm.drawsNothing()); |
| 643 | auto r = SkRect::MakeXYWH(x, y, bm.width(), bm.height()); |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 644 | this->internalDrawImageRect(SkKeyedImage(bm), nullptr, r, SkSamplingOptions(), paint, |
| 645 | SkMatrix::I()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 648 | //////////////////////////////////////////////////////////////////////////////// |
| 649 | |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 650 | namespace { |
| 651 | class GlyphPositioner { |
| 652 | public: |
| 653 | GlyphPositioner(SkDynamicMemoryWStream* content, |
| 654 | SkScalar textSkewX, |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 655 | SkPoint origin) |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 656 | : fContent(content) |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 657 | , fCurrentMatrixOrigin(origin) |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 658 | , fTextSkewX(textSkewX) { |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 659 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 660 | ~GlyphPositioner() { this->flush(); } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 661 | void flush() { |
| 662 | if (fInText) { |
| 663 | fContent->writeText("> Tj\n"); |
| 664 | fInText = false; |
| 665 | } |
| 666 | } |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 667 | void setFont(SkPDFFont* pdfFont) { |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 668 | this->flush(); |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 669 | fPDFFont = pdfFont; |
| 670 | // Reader 2020.013.20064 incorrectly advances some Type3 fonts https://crbug.com/1226960 |
| 671 | bool convertedToType3 = fPDFFont->getType() == SkAdvancedTypefaceMetrics::kOther_Font; |
| 672 | bool thousandEM = fPDFFont->typeface()->getUnitsPerEm() == 1000; |
| 673 | fViewersAgreeOnAdvancesInFont = thousandEM || !convertedToType3; |
Hal Canary | 8ef78ea | 2018-10-01 13:38:30 -0400 | [diff] [blame] | 674 | } |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 675 | void writeGlyph(uint16_t glyph, SkScalar advanceWidth, SkPoint xy) { |
| 676 | SkASSERT(fPDFFont); |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 677 | if (!fInitialized) { |
| 678 | // Flip the text about the x-axis to account for origin swap and include |
| 679 | // the passed parameters. |
| 680 | fContent->writeText("1 0 "); |
| 681 | SkPDFUtils::AppendScalar(-fTextSkewX, fContent); |
| 682 | fContent->writeText(" -1 "); |
| 683 | SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.x(), fContent); |
| 684 | fContent->writeText(" "); |
| 685 | SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.y(), fContent); |
| 686 | fContent->writeText(" Tm\n"); |
| 687 | fCurrentMatrixOrigin.set(0.0f, 0.0f); |
| 688 | fInitialized = true; |
| 689 | } |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 690 | SkPoint position = xy - fCurrentMatrixOrigin; |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 691 | if (!fViewersAgreeOnXAdvance || position != SkPoint{fXAdvance, 0}) { |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 692 | this->flush(); |
| 693 | SkPDFUtils::AppendScalar(position.x() - position.y() * fTextSkewX, fContent); |
| 694 | fContent->writeText(" "); |
| 695 | SkPDFUtils::AppendScalar(-position.y(), fContent); |
| 696 | fContent->writeText(" Td "); |
| 697 | fCurrentMatrixOrigin = xy; |
| 698 | fXAdvance = 0; |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 699 | fViewersAgreeOnXAdvance = true; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 700 | } |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 701 | fXAdvance += advanceWidth; |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 702 | if (!fViewersAgreeOnAdvancesInFont) { |
| 703 | fViewersAgreeOnXAdvance = false; |
| 704 | } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 705 | if (!fInText) { |
| 706 | fContent->writeText("<"); |
| 707 | fInText = true; |
| 708 | } |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 709 | if (fPDFFont->multiByteGlyphs()) { |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 710 | SkPDFUtils::WriteUInt16BE(fContent, glyph); |
| 711 | } else { |
| 712 | SkASSERT(0 == glyph >> 8); |
| 713 | SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph)); |
| 714 | } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | private: |
| 718 | SkDynamicMemoryWStream* fContent; |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 719 | SkPDFFont* fPDFFont = nullptr; |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 720 | SkPoint fCurrentMatrixOrigin; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 721 | SkScalar fXAdvance = 0.0f; |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 722 | bool fViewersAgreeOnAdvancesInFont = true; |
| 723 | bool fViewersAgreeOnXAdvance = true; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 724 | SkScalar fTextSkewX; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 725 | bool fInText = false; |
| 726 | bool fInitialized = false; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 727 | }; |
| 728 | } // namespace |
| 729 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 730 | static SkUnichar map_glyph(const std::vector<SkUnichar>& glyphToUnicode, SkGlyphID glyph) { |
| 731 | return glyph < glyphToUnicode.size() ? glyphToUnicode[SkToInt(glyph)] : -1; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Hal Canary | 575be30 | 2018-09-28 15:01:14 -0400 | [diff] [blame] | 734 | namespace { |
| 735 | struct PositionedGlyph { |
| 736 | SkPoint fPos; |
| 737 | SkGlyphID fGlyph; |
| 738 | }; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 739 | } // namespace |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 740 | |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 741 | static SkRect get_glyph_bounds_device_space(const SkGlyph* glyph, |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 742 | SkScalar xScale, SkScalar yScale, |
| 743 | SkPoint xy, const SkMatrix& ctm) { |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 744 | SkRect glyphBounds = SkMatrix::Scale(xScale, yScale).mapRect(glyph->rect()); |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 745 | glyphBounds.offset(xy); |
| 746 | ctm.mapRect(&glyphBounds); // now in dev space. |
| 747 | return glyphBounds; |
| 748 | } |
| 749 | |
| 750 | static bool contains(const SkRect& r, SkPoint p) { |
| 751 | return r.left() <= p.x() && p.x() <= r.right() && |
| 752 | r.top() <= p.y() && p.y() <= r.bottom(); |
| 753 | } |
| 754 | |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame] | 755 | void SkPDFDevice::drawGlyphRunAsPath( |
| 756 | const SkGlyphRun& glyphRun, SkPoint offset, const SkPaint& runPaint) { |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 757 | const SkFont& font = glyphRun.font(); |
Hal Canary | 575be30 | 2018-09-28 15:01:14 -0400 | [diff] [blame] | 758 | SkPath path; |
Herb Derby | 4fce083 | 2018-10-02 11:04:29 -0400 | [diff] [blame] | 759 | |
Mike Reed | ab8f297 | 2018-12-05 13:20:29 -0500 | [diff] [blame] | 760 | struct Rec { |
| 761 | SkPath* fPath; |
| 762 | SkPoint fOffset; |
| 763 | const SkPoint* fPos; |
| 764 | } rec = {&path, offset, glyphRun.positions().data()}; |
| 765 | |
| 766 | font.getPaths(glyphRun.glyphsIDs().data(), glyphRun.glyphsIDs().size(), |
| 767 | [](const SkPath* path, const SkMatrix& mx, void* ctx) { |
| 768 | Rec* rec = reinterpret_cast<Rec*>(ctx); |
| 769 | if (path) { |
| 770 | SkMatrix total = mx; |
| 771 | total.postTranslate(rec->fPos->fX + rec->fOffset.fX, |
| 772 | rec->fPos->fY + rec->fOffset.fY); |
| 773 | rec->fPath->addPath(*path, total); |
| 774 | } |
| 775 | rec->fPos += 1; // move to the next glyph's position |
| 776 | }, &rec); |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 777 | this->internalDrawPath(this->cs(), this->localToDevice(), path, runPaint, true); |
Hal Canary | 575be30 | 2018-09-28 15:01:14 -0400 | [diff] [blame] | 778 | |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame] | 779 | SkFont transparentFont = glyphRun.font(); |
| 780 | transparentFont.setEmbolden(false); // Stop Recursion |
| 781 | SkGlyphRun tmpGlyphRun(glyphRun, transparentFont); |
| 782 | |
Herb Derby | 9bb6b7e | 2018-11-02 13:57:58 -0400 | [diff] [blame] | 783 | SkPaint transparent; |
Herb Derby | 9bb6b7e | 2018-11-02 13:57:58 -0400 | [diff] [blame] | 784 | transparent.setColor(SK_ColorTRANSPARENT); |
Herb Derby | 9bb6b7e | 2018-11-02 13:57:58 -0400 | [diff] [blame] | 785 | |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 786 | if (this->localToDevice().hasPerspective()) { |
| 787 | SkAutoDeviceTransformRestore adr(this, SkMatrix::I()); |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame] | 788 | this->internalDrawGlyphRun(tmpGlyphRun, offset, transparent); |
Hal Canary | 575be30 | 2018-09-28 15:01:14 -0400 | [diff] [blame] | 789 | } else { |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame] | 790 | this->internalDrawGlyphRun(tmpGlyphRun, offset, transparent); |
Hal Canary | 575be30 | 2018-09-28 15:01:14 -0400 | [diff] [blame] | 791 | } |
Hal Canary | 575be30 | 2018-09-28 15:01:14 -0400 | [diff] [blame] | 792 | } |
| 793 | |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 794 | static bool needs_new_font(SkPDFFont* font, const SkGlyph* glyph, |
Hal Canary | 688afdd | 2018-10-17 14:57:53 -0400 | [diff] [blame] | 795 | SkAdvancedTypefaceMetrics::FontType fontType) { |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 796 | if (!font || !font->hasGlyph(glyph->getGlyphID())) { |
Hal Canary | 688afdd | 2018-10-17 14:57:53 -0400 | [diff] [blame] | 797 | return true; |
| 798 | } |
| 799 | if (fontType == SkAdvancedTypefaceMetrics::kOther_Font) { |
| 800 | return false; |
| 801 | } |
Herb Derby | f0e7581 | 2019-06-12 11:14:50 -0400 | [diff] [blame] | 802 | if (glyph->isEmpty()) { |
Hal Canary | 688afdd | 2018-10-17 14:57:53 -0400 | [diff] [blame] | 803 | return false; |
| 804 | } |
| 805 | |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 806 | bool bitmapOnly = nullptr == glyph->path(); |
Hal Canary | 688afdd | 2018-10-17 14:57:53 -0400 | [diff] [blame] | 807 | bool convertedToType3 = (font->getType() == SkAdvancedTypefaceMetrics::kOther_Font); |
| 808 | return convertedToType3 != bitmapOnly; |
| 809 | } |
| 810 | |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame] | 811 | void SkPDFDevice::internalDrawGlyphRun( |
| 812 | const SkGlyphRun& glyphRun, SkPoint offset, const SkPaint& runPaint) { |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 813 | |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 814 | const SkGlyphID* glyphIDs = glyphRun.glyphsIDs().data(); |
Herb Derby | aedc9d2 | 2018-10-25 12:27:07 -0400 | [diff] [blame] | 815 | uint32_t glyphCount = SkToU32(glyphRun.glyphsIDs().size()); |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 816 | const SkFont& glyphRunFont = glyphRun.font(); |
Herb Derby | 4fce083 | 2018-10-02 11:04:29 -0400 | [diff] [blame] | 817 | |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 818 | if (!glyphCount || !glyphIDs || glyphRunFont.getSize() <= 0 || this->hasEmptyClip()) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 819 | return; |
| 820 | } |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 821 | if (runPaint.getPathEffect() |
| 822 | || runPaint.getMaskFilter() |
| 823 | || glyphRunFont.isEmbolden() |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 824 | || this->localToDevice().hasPerspective() |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 825 | || SkPaint::kFill_Style != runPaint.getStyle()) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 826 | // Stroked Text doesn't work well with Type3 fonts. |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame] | 827 | this->drawGlyphRunAsPath(glyphRun, offset, runPaint); |
Hal Canary | e9a29c4 | 2018-12-07 13:44:26 -0500 | [diff] [blame] | 828 | return; |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 829 | } |
Herb Derby | 087fad7 | 2019-01-22 14:45:16 -0500 | [diff] [blame] | 830 | SkTypeface* typeface = glyphRunFont.getTypefaceOrDefault(); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 831 | if (!typeface) { |
| 832 | SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n"); |
| 833 | return; |
| 834 | } |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 835 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 836 | const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, fDocument); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 837 | if (!metrics) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 838 | return; |
| 839 | } |
Hal Canary | 688afdd | 2018-10-17 14:57:53 -0400 | [diff] [blame] | 840 | SkAdvancedTypefaceMetrics::FontType fontType = SkPDFFont::FontType(*metrics); |
| 841 | |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 842 | const std::vector<SkUnichar>& glyphToUnicode = SkPDFFont::GetUnicodeMap(typeface, fDocument); |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 843 | |
Hal Canary | 98caedd | 2018-07-23 10:50:49 -0400 | [diff] [blame] | 844 | SkClusterator clusterator(glyphRun); |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 845 | |
| 846 | int emSize; |
Herb Derby | 36a54c1 | 2019-06-06 10:50:56 -0400 | [diff] [blame] | 847 | SkStrikeSpec strikeSpec = SkStrikeSpec::MakePDFVector(*typeface, &emSize); |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 848 | |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 849 | SkScalar textSize = glyphRunFont.getSize(); |
| 850 | SkScalar advanceScale = textSize * glyphRunFont.getScaleX() / emSize; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 851 | |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 852 | // textScaleX and textScaleY are used to get a conservative bounding box for glyphs. |
| 853 | SkScalar textScaleY = textSize / emSize; |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 854 | SkScalar textScaleX = advanceScale + glyphRunFont.getSkewX() * textScaleY; |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 855 | |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 856 | SkRect clipStackBounds = this->cs().bounds(this->bounds()); |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 857 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 858 | SkTCopyOnFirstWrite<SkPaint> paint(clean_paint(runPaint)); |
| 859 | ScopedContentEntry content(this, *paint, glyphRunFont.getScaleX()); |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 860 | if (!content) { |
| 861 | return; |
| 862 | } |
| 863 | SkDynamicMemoryWStream* out = content.stream(); |
Dominic Mazzoni | 656cefe | 2018-09-25 20:29:15 -0700 | [diff] [blame] | 864 | |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 865 | out->writeText("BT\n"); |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 866 | SK_AT_SCOPE_EXIT(out->writeText("ET\n")); |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 867 | |
Dominic Mazzoni | 8f233fc | 2020-03-06 11:20:45 -0800 | [diff] [blame] | 868 | ScopedOutputMarkedContentTags mark(fNodeId, fDocument, out); |
| 869 | |
Ben Wagner | c19459b | 2020-04-30 21:13:51 -0400 | [diff] [blame] | 870 | const int numGlyphs = typeface->countGlyphs(); |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 871 | |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 872 | if (clusterator.reversedChars()) { |
| 873 | out->writeText("/ReversedChars BMC\n"); |
| 874 | } |
| 875 | SK_AT_SCOPE_EXIT(if (clusterator.reversedChars()) { out->writeText("EMC\n"); } ); |
| 876 | GlyphPositioner glyphPositioner(out, glyphRunFont.getSkewX(), offset); |
| 877 | SkPDFFont* font = nullptr; |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 878 | |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 879 | SkBulkGlyphMetricsAndPaths paths{strikeSpec}; |
| 880 | auto glyphs = paths.glyphs(glyphRun.glyphsIDs()); |
Herb Derby | ea41c7b | 2019-05-22 16:40:58 -0400 | [diff] [blame] | 881 | |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 882 | while (SkClusterator::Cluster c = clusterator.next()) { |
| 883 | int index = c.fGlyphIndex; |
| 884 | int glyphLimit = index + c.fGlyphCount; |
| 885 | |
| 886 | bool actualText = false; |
| 887 | SK_AT_SCOPE_EXIT(if (actualText) { |
| 888 | glyphPositioner.flush(); |
| 889 | out->writeText("EMC\n"); |
| 890 | }); |
| 891 | if (c.fUtf8Text) { // real cluster |
| 892 | // Check if `/ActualText` needed. |
| 893 | const char* textPtr = c.fUtf8Text; |
| 894 | const char* textEnd = c.fUtf8Text + c.fTextByteLength; |
| 895 | SkUnichar unichar = SkUTF::NextUTF8(&textPtr, textEnd); |
| 896 | if (unichar < 0) { |
| 897 | return; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 898 | } |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 899 | if (textPtr < textEnd || // more characters left |
| 900 | glyphLimit > index + 1 || // toUnicode wouldn't work |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 901 | unichar != map_glyph(glyphToUnicode, glyphIDs[index])) // test single Unichar map |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 902 | { |
| 903 | glyphPositioner.flush(); |
| 904 | out->writeText("/Span<</ActualText <"); |
| 905 | SkPDFUtils::WriteUTF16beHex(out, 0xFEFF); // U+FEFF = BYTE ORDER MARK |
| 906 | // the BOM marks this text as UTF-16BE, not PDFDocEncoding. |
| 907 | SkPDFUtils::WriteUTF16beHex(out, unichar); // first char |
| 908 | while (textPtr < textEnd) { |
| 909 | unichar = SkUTF::NextUTF8(&textPtr, textEnd); |
| 910 | if (unichar < 0) { |
| 911 | break; |
| 912 | } |
| 913 | SkPDFUtils::WriteUTF16beHex(out, unichar); |
| 914 | } |
| 915 | out->writeText("> >> BDC\n"); // begin marked-content sequence |
| 916 | // with an associated property list. |
| 917 | actualText = true; |
| 918 | } |
| 919 | } |
| 920 | for (; index < glyphLimit; ++index) { |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 921 | SkGlyphID gid = glyphIDs[index]; |
Ben Wagner | c19459b | 2020-04-30 21:13:51 -0400 | [diff] [blame] | 922 | if (numGlyphs <= gid) { |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 923 | continue; |
| 924 | } |
| 925 | SkPoint xy = glyphRun.positions()[index]; |
| 926 | // Do a glyph-by-glyph bounds-reject if positions are absolute. |
| 927 | SkRect glyphBounds = get_glyph_bounds_device_space( |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 928 | glyphs[index], textScaleX, textScaleY, |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 929 | xy + offset, this->localToDevice()); |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 930 | if (glyphBounds.isEmpty()) { |
| 931 | if (!contains(clipStackBounds, {glyphBounds.x(), glyphBounds.y()})) { |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 932 | continue; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 933 | } |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 934 | } else { |
| 935 | if (!clipStackBounds.intersects(glyphBounds)) { |
| 936 | continue; // reject glyphs as out of bounds |
Hal Canary | 688afdd | 2018-10-17 14:57:53 -0400 | [diff] [blame] | 937 | } |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 938 | } |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 939 | if (needs_new_font(font, glyphs[index], fontType)) { |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 940 | // Not yet specified font or need to switch font. |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 941 | font = SkPDFFont::GetFontResource(fDocument, glyphs[index], typeface); |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 942 | SkASSERT(font); // All preconditions for SkPDFFont::GetFontResource are met. |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 943 | glyphPositioner.setFont(font); |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 944 | SkPDFWriteResourceName(out, SkPDFResourceType::kFont, |
| 945 | add_resource(fFontResources, font->indirectReference())); |
| 946 | out->writeText(" "); |
| 947 | SkPDFUtils::AppendScalar(textSize, out); |
| 948 | out->writeText(" Tf\n"); |
| 949 | |
| 950 | } |
| 951 | font->noteGlyphUsage(gid); |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 952 | SkGlyphID encodedGlyph = font->glyphToPDFFontEncoding(gid); |
Herb Derby | 02e9150 | 2019-07-03 10:57:33 -0400 | [diff] [blame] | 953 | SkScalar advance = advanceScale * glyphs[index]->advanceX(); |
Ben Wagner | a4625e2 | 2021-08-13 15:45:22 -0400 | [diff] [blame] | 954 | glyphPositioner.writeGlyph(encodedGlyph, advance, xy); |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 955 | } |
| 956 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Herb Derby | 97abbee | 2021-04-09 15:06:21 -0400 | [diff] [blame] | 959 | void SkPDFDevice::onDrawGlyphRunList(const SkGlyphRunList& glyphRunList, const SkPaint& paint) { |
| 960 | SkASSERT(!glyphRunList.hasRSXForm()); |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 961 | for (const SkGlyphRun& glyphRun : glyphRunList) { |
Herb Derby | 0da2c14 | 2021-03-22 15:28:23 -0400 | [diff] [blame] | 962 | this->internalDrawGlyphRun(glyphRun, glyphRunList.origin(), paint); |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 963 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Mike Reed | 5caf935 | 2020-03-02 14:57:09 -0500 | [diff] [blame] | 966 | void SkPDFDevice::drawVertices(const SkVertices*, SkBlendMode, const SkPaint&) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 967 | if (this->hasEmptyClip()) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 968 | return; |
| 969 | } |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 970 | // TODO: implement drawVertices |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 973 | void SkPDFDevice::drawFormXObject(SkPDFIndirectReference xObject, SkDynamicMemoryWStream* content) { |
Dominic Mazzoni | 8f233fc | 2020-03-06 11:20:45 -0800 | [diff] [blame] | 974 | ScopedOutputMarkedContentTags mark(fNodeId, fDocument, content); |
| 975 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 976 | SkASSERT(xObject); |
Hal Canary | 3b8b11e | 2018-09-29 22:31:34 -0400 | [diff] [blame] | 977 | SkPDFWriteResourceName(content, SkPDFResourceType::kXObject, |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 978 | add_resource(fXObjectResources, xObject)); |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 979 | content->writeText(" Do\n"); |
| 980 | } |
| 981 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 982 | sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 983 | return SkSurface::MakeRaster(info, &props); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 986 | static std::vector<SkPDFIndirectReference> sort(const SkTHashSet<SkPDFIndirectReference>& src) { |
| 987 | std::vector<SkPDFIndirectReference> dst; |
| 988 | dst.reserve(src.count()); |
John Stiles | 65b4827 | 2020-12-22 17:18:34 -0500 | [diff] [blame] | 989 | for (SkPDFIndirectReference ref : src) { |
| 990 | dst.push_back(ref); |
| 991 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 992 | std::sort(dst.begin(), dst.end(), |
| 993 | [](SkPDFIndirectReference a, SkPDFIndirectReference b) { return a.fValue < b.fValue; }); |
| 994 | return dst; |
| 995 | } |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 996 | |
Hal Canary | 7480158 | 2018-12-18 16:30:41 -0500 | [diff] [blame] | 997 | std::unique_ptr<SkPDFDict> SkPDFDevice::makeResourceDict() { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 998 | return SkPDFMakeResourceDict(sort(fGraphicStateResources), |
| 999 | sort(fShaderResources), |
| 1000 | sort(fXObjectResources), |
| 1001 | sort(fFontResources)); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1004 | std::unique_ptr<SkStreamAsset> SkPDFDevice::content() { |
| 1005 | if (fActiveStackState.fContentStream) { |
| 1006 | fActiveStackState.drainStack(); |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 1007 | fActiveStackState = SkPDFGraphicStackState(); |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1008 | } |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1009 | if (fContent.bytesWritten() == 0) { |
Mike Klein | f46d5ca | 2019-12-11 10:45:01 -0500 | [diff] [blame] | 1010 | return std::make_unique<SkMemoryStream>(); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1011 | } |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1012 | SkDynamicMemoryWStream buffer; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1013 | if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) { |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 1014 | SkPDFUtils::AppendTransform(fInitialTransform, &buffer); |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 1015 | } |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1016 | if (fNeedsExtraSave) { |
| 1017 | buffer.writeText("q\n"); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1018 | } |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1019 | fContent.writeToAndReset(&buffer); |
| 1020 | if (fNeedsExtraSave) { |
| 1021 | buffer.writeText("Q\n"); |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 1022 | } |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1023 | fNeedsExtraSave = false; |
| 1024 | return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1027 | /* Draws an inverse filled path by using Path Ops to compute the positive |
| 1028 | * inverse using the current clip as the inverse bounds. |
| 1029 | * Return true if this was an inverse path and was properly handled, |
| 1030 | * otherwise returns false and the normal drawing routine should continue, |
| 1031 | * either as a (incorrect) fallback or because the path was not inverse |
| 1032 | * in the first place. |
| 1033 | */ |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1034 | bool SkPDFDevice::handleInversePath(const SkPath& origPath, |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 1035 | const SkPaint& paint, |
| 1036 | bool pathIsMutable) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1037 | if (!origPath.isInverseFillType()) { |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 1041 | if (this->hasEmptyClip()) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1042 | return false; |
| 1043 | } |
| 1044 | |
| 1045 | SkPath modifiedPath; |
| 1046 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
| 1047 | SkPaint noInversePaint(paint); |
| 1048 | |
| 1049 | // Merge stroking operations into final path. |
| 1050 | if (SkPaint::kStroke_Style == paint.getStyle() || |
| 1051 | SkPaint::kStrokeAndFill_Style == paint.getStyle()) { |
| 1052 | bool doFillPath = paint.getFillPath(origPath, &modifiedPath); |
| 1053 | if (doFillPath) { |
| 1054 | noInversePaint.setStyle(SkPaint::kFill_Style); |
| 1055 | noInversePaint.setStrokeWidth(0); |
| 1056 | pathPtr = &modifiedPath; |
| 1057 | } else { |
| 1058 | // To be consistent with the raster output, hairline strokes |
| 1059 | // are rendered as non-inverted. |
| 1060 | modifiedPath.toggleInverseFillType(); |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 1061 | this->internalDrawPath(this->cs(), this->localToDevice(), modifiedPath, paint, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1062 | return true; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | // Get bounds of clip in current transform space |
| 1067 | // (clip bounds are given in device space). |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1068 | SkMatrix transformInverse; |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 1069 | SkMatrix totalMatrix = this->localToDevice(); |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 1070 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1071 | if (!totalMatrix.invert(&transformInverse)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1072 | return false; |
| 1073 | } |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 1074 | SkRect bounds = this->cs().bounds(this->bounds()); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1075 | transformInverse.mapRect(&bounds); |
| 1076 | |
| 1077 | // Extend the bounds by the line width (plus some padding) |
| 1078 | // so the edge doesn't cause a visible stroke. |
| 1079 | bounds.outset(paint.getStrokeWidth() + SK_Scalar1, |
| 1080 | paint.getStrokeWidth() + SK_Scalar1); |
| 1081 | |
| 1082 | if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) { |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 1086 | this->internalDrawPath(this->cs(), this->localToDevice(), modifiedPath, noInversePaint, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1087 | return true; |
| 1088 | } |
| 1089 | |
Hal Canary | 6028fa7 | 2019-04-10 15:33:06 -0400 | [diff] [blame] | 1090 | SkPDFIndirectReference SkPDFDevice::makeFormXObjectFromDevice(SkIRect bounds, bool alpha) { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1091 | SkMatrix inverseTransform = SkMatrix::I(); |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1092 | if (!fInitialTransform.isIdentity()) { |
| 1093 | if (!fInitialTransform.invert(&inverseTransform)) { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1094 | SkDEBUGFAIL("Layer initial transform should be invertible."); |
| 1095 | inverseTransform.reset(); |
| 1096 | } |
| 1097 | } |
Hal Canary | b4bd5ef | 2017-07-26 09:16:01 -0400 | [diff] [blame] | 1098 | const char* colorSpace = alpha ? "DeviceGray" : nullptr; |
Hal Canary | e650b85 | 2018-09-12 09:12:36 -0400 | [diff] [blame] | 1099 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1100 | SkPDFIndirectReference xobject = |
| 1101 | SkPDFMakeFormXObject(fDocument, this->content(), |
Hal Canary | 6028fa7 | 2019-04-10 15:33:06 -0400 | [diff] [blame] | 1102 | SkPDFMakeArray(bounds.left(), bounds.top(), |
| 1103 | bounds.right(), bounds.bottom()), |
Hal Canary | b4bd5ef | 2017-07-26 09:16:01 -0400 | [diff] [blame] | 1104 | this->makeResourceDict(), inverseTransform, colorSpace); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1105 | // We always draw the form xobjects that we create back into the device, so |
| 1106 | // we simply preserve the font usage instead of pulling it out and merging |
| 1107 | // it back in later. |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 1108 | this->reset(); |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1109 | return xobject; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Hal Canary | 6028fa7 | 2019-04-10 15:33:06 -0400 | [diff] [blame] | 1112 | SkPDFIndirectReference SkPDFDevice::makeFormXObjectFromDevice(bool alpha) { |
| 1113 | return this->makeFormXObjectFromDevice(SkIRect{0, 0, this->width(), this->height()}, alpha); |
| 1114 | } |
| 1115 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1116 | void SkPDFDevice::drawFormXObjectWithMask(SkPDFIndirectReference xObject, |
| 1117 | SkPDFIndirectReference sMask, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1118 | SkBlendMode mode, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1119 | bool invertClip) { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1120 | SkASSERT(sMask); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1121 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1122 | paint.setBlendMode(mode); |
Hal Canary | d892a9d | 2018-09-24 21:20:47 -0400 | [diff] [blame] | 1123 | ScopedContentEntry content(this, nullptr, SkMatrix::I(), paint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1124 | if (!content) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
Hal Canary | 3b8b11e | 2018-09-29 22:31:34 -0400 | [diff] [blame] | 1127 | this->setGraphicState(SkPDFGraphicState::GetSMaskGraphicState( |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1128 | sMask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, |
| 1129 | fDocument), content.stream()); |
| 1130 | this->drawFormXObject(xObject, content.stream()); |
Hal Canary | c02de0b | 2017-06-28 13:14:03 -0400 | [diff] [blame] | 1131 | this->clearMaskOnGraphicState(content.stream()); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1134 | |
| 1135 | static bool treat_as_regular_pdf_blend_mode(SkBlendMode blendMode) { |
| 1136 | return nullptr != SkPDFUtils::BlendModeName(blendMode); |
| 1137 | } |
| 1138 | |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 1139 | static void populate_graphic_state_entry_from_paint( |
| 1140 | SkPDFDocument* doc, |
| 1141 | const SkMatrix& matrix, |
| 1142 | const SkClipStack* clipStack, |
| 1143 | SkIRect deviceBounds, |
| 1144 | const SkPaint& paint, |
| 1145 | const SkMatrix& initialTransform, |
| 1146 | SkScalar textScale, |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 1147 | SkPDFGraphicStackState::Entry* entry, |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 1148 | SkTHashSet<SkPDFIndirectReference>* shaderResources, |
| 1149 | SkTHashSet<SkPDFIndirectReference>* graphicStateResources) { |
| 1150 | NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false); |
| 1151 | NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false); |
| 1152 | NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false); |
| 1153 | |
| 1154 | entry->fMatrix = matrix; |
| 1155 | entry->fClipStackGenID = clipStack ? clipStack->getTopmostGenID() |
| 1156 | : SkClipStack::kWideOpenGenID; |
| 1157 | SkColor4f color = paint.getColor4f(); |
| 1158 | entry->fColor = {color.fR, color.fG, color.fB, 1}; |
| 1159 | entry->fShaderIndex = -1; |
| 1160 | |
| 1161 | // PDF treats a shader as a color, so we only set one or the other. |
| 1162 | SkShader* shader = paint.getShader(); |
| 1163 | if (shader) { |
Mike Reed | b9c9829 | 2021-06-15 09:37:20 -0400 | [diff] [blame] | 1164 | // note: we always present the alpha as 1 for the shader, knowing that it will be |
| 1165 | // accounted for when we create our newGraphicsState (below) |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 1166 | if (SkShader::kColor_GradientType == shader->asAGradient(nullptr)) { |
| 1167 | // We don't have to set a shader just for a color. |
| 1168 | SkShader::GradientInfo gradientInfo; |
| 1169 | SkColor gradientColor = SK_ColorBLACK; |
| 1170 | gradientInfo.fColors = &gradientColor; |
| 1171 | gradientInfo.fColorOffsets = nullptr; |
| 1172 | gradientInfo.fColorCount = 1; |
| 1173 | SkAssertResult(shader->asAGradient(&gradientInfo) == SkShader::kColor_GradientType); |
| 1174 | color = SkColor4f::FromColor(gradientColor); |
| 1175 | entry->fColor ={color.fR, color.fG, color.fB, 1}; |
| 1176 | |
| 1177 | } else { |
| 1178 | // PDF positions patterns relative to the initial transform, so |
| 1179 | // we need to apply the current transform to the shader parameters. |
| 1180 | SkMatrix transform = matrix; |
| 1181 | transform.postConcat(initialTransform); |
| 1182 | |
| 1183 | // PDF doesn't support kClamp_TileMode, so we simulate it by making |
| 1184 | // a pattern the size of the current clip. |
| 1185 | SkRect clipStackBounds = clipStack ? clipStack->bounds(deviceBounds) |
| 1186 | : SkRect::Make(deviceBounds); |
| 1187 | |
| 1188 | // We need to apply the initial transform to bounds in order to get |
| 1189 | // bounds in a consistent coordinate system. |
| 1190 | initialTransform.mapRect(&clipStackBounds); |
| 1191 | SkIRect bounds; |
| 1192 | clipStackBounds.roundOut(&bounds); |
| 1193 | |
Mike Reed | b9c9829 | 2021-06-15 09:37:20 -0400 | [diff] [blame] | 1194 | auto c = paint.getColor4f(); |
| 1195 | SkPDFIndirectReference pdfShader = SkPDFMakeShader(doc, shader, transform, bounds, |
| 1196 | {c.fR, c.fG, c.fB, 1.0f}); |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 1197 | |
| 1198 | if (pdfShader) { |
| 1199 | // pdfShader has been canonicalized so we can directly compare pointers. |
| 1200 | entry->fShaderIndex = add_resource(*shaderResources, pdfShader); |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | SkPDFIndirectReference newGraphicState; |
| 1206 | if (color == paint.getColor4f()) { |
| 1207 | newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(doc, paint); |
| 1208 | } else { |
| 1209 | SkPaint newPaint = paint; |
| 1210 | newPaint.setColor4f(color, nullptr); |
| 1211 | newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(doc, newPaint); |
| 1212 | } |
| 1213 | entry->fGraphicStateIndex = add_resource(*graphicStateResources, newGraphicState); |
| 1214 | entry->fTextScaleX = textScale; |
| 1215 | } |
| 1216 | |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1217 | SkDynamicMemoryWStream* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack, |
| 1218 | const SkMatrix& matrix, |
| 1219 | const SkPaint& paint, |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 1220 | SkScalar textScale, |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1221 | SkPDFIndirectReference* dst) { |
| 1222 | SkASSERT(!*dst); |
Mike Reed | 90f4e9f | 2021-07-07 16:05:25 -0400 | [diff] [blame] | 1223 | SkBlendMode blendMode = paint.getBlendMode_or(SkBlendMode::kSrcOver); |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1224 | |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1225 | // Dst xfer mode doesn't draw source at all. |
| 1226 | if (blendMode == SkBlendMode::kDst) { |
| 1227 | return nullptr; |
| 1228 | } |
| 1229 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1230 | // For the following modes, we want to handle source and destination |
| 1231 | // separately, so make an object of what's already there. |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1232 | if (!treat_as_regular_pdf_blend_mode(blendMode) && blendMode != SkBlendMode::kDstOver) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1233 | if (!isContentEmpty()) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1234 | *dst = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1235 | SkASSERT(isContentEmpty()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1236 | } else if (blendMode != SkBlendMode::kSrc && |
| 1237 | blendMode != SkBlendMode::kSrcOut) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1238 | // Except for Src and SrcOut, if there isn't anything already there, |
| 1239 | // then we're done. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1240 | return nullptr; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1241 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1242 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1243 | // TODO(vandebo): Figure out how/if we can handle the following modes: |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1244 | // Xor, Plus. For now, we treat them as SrcOver/Normal. |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1245 | |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1246 | if (treat_as_regular_pdf_blend_mode(blendMode)) { |
| 1247 | if (!fActiveStackState.fContentStream) { |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1248 | if (fContent.bytesWritten() != 0) { |
| 1249 | fContent.writeText("Q\nq\n"); |
| 1250 | fNeedsExtraSave = true; |
| 1251 | } |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 1252 | fActiveStackState = SkPDFGraphicStackState(&fContent); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1253 | } else { |
| 1254 | SkASSERT(fActiveStackState.fContentStream = &fContent); |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1255 | } |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1256 | } else { |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1257 | fActiveStackState.drainStack(); |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 1258 | fActiveStackState = SkPDFGraphicStackState(&fContentBuffer); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1259 | } |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1260 | SkASSERT(fActiveStackState.fContentStream); |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 1261 | SkPDFGraphicStackState::Entry entry; |
Hal Canary | 8f37ce5 | 2018-12-28 11:40:10 -0500 | [diff] [blame] | 1262 | populate_graphic_state_entry_from_paint( |
| 1263 | fDocument, |
| 1264 | matrix, |
| 1265 | clipStack, |
| 1266 | this->bounds(), |
| 1267 | paint, |
| 1268 | fInitialTransform, |
| 1269 | textScale, |
| 1270 | &entry, |
| 1271 | &fShaderResources, |
| 1272 | &fGraphicStateResources); |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1273 | fActiveStackState.updateClip(clipStack, this->bounds()); |
| 1274 | fActiveStackState.updateMatrix(entry.fMatrix); |
| 1275 | fActiveStackState.updateDrawingState(entry); |
| 1276 | |
| 1277 | return fActiveStackState.fContentStream; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1280 | void SkPDFDevice::finishContentEntry(const SkClipStack* clipStack, |
| 1281 | SkBlendMode blendMode, |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1282 | SkPDFIndirectReference dst, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1283 | SkPath* shape) { |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1284 | SkASSERT(blendMode != SkBlendMode::kDst); |
| 1285 | if (treat_as_regular_pdf_blend_mode(blendMode)) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1286 | SkASSERT(!dst); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1287 | return; |
| 1288 | } |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1289 | |
| 1290 | SkASSERT(fActiveStackState.fContentStream); |
| 1291 | |
| 1292 | fActiveStackState.drainStack(); |
Hal Canary | f5edf36 | 2019-04-10 11:00:01 -0400 | [diff] [blame] | 1293 | fActiveStackState = SkPDFGraphicStackState(); |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1294 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1295 | if (blendMode == SkBlendMode::kDstOver) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1296 | SkASSERT(!dst); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1297 | if (fContentBuffer.bytesWritten() != 0) { |
| 1298 | if (fContent.bytesWritten() != 0) { |
| 1299 | fContentBuffer.writeText("Q\nq\n"); |
| 1300 | fNeedsExtraSave = true; |
| 1301 | } |
| 1302 | fContentBuffer.prependToAndReset(&fContent); |
| 1303 | SkASSERT(fContentBuffer.bytesWritten() == 0); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1304 | } |
| 1305 | return; |
| 1306 | } |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1307 | if (fContentBuffer.bytesWritten() != 0) { |
| 1308 | if (fContent.bytesWritten() != 0) { |
| 1309 | fContent.writeText("Q\nq\n"); |
| 1310 | fNeedsExtraSave = true; |
| 1311 | } |
| 1312 | fContentBuffer.writeToAndReset(&fContent); |
| 1313 | SkASSERT(fContentBuffer.bytesWritten() == 0); |
| 1314 | } |
| 1315 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1316 | if (!dst) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1317 | SkASSERT(blendMode == SkBlendMode::kSrc || |
| 1318 | blendMode == SkBlendMode::kSrcOut); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1319 | return; |
| 1320 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1321 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1322 | SkASSERT(dst); |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1323 | // Changing the current content into a form-xobject will destroy the clip |
| 1324 | // objects which is fine since the xobject will already be clipped. However |
| 1325 | // if source has shape, we need to clip it too, so a copy of the clip is |
| 1326 | // saved. |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1327 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1328 | SkPaint stockPaint; |
| 1329 | |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1330 | SkPDFIndirectReference srcFormXObject; |
Hal Canary | 9e41c21 | 2018-09-03 12:00:23 -0400 | [diff] [blame] | 1331 | if (this->isContentEmpty()) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1332 | // If nothing was drawn and there's no shape, then the draw was a |
| 1333 | // no-op, but dst needs to be restored for that to be true. |
| 1334 | // If there is shape, then an empty source with Src, SrcIn, SrcOut, |
| 1335 | // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop |
| 1336 | // reduces to Dst. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1337 | if (shape == nullptr || blendMode == SkBlendMode::kDstOut || |
| 1338 | blendMode == SkBlendMode::kSrcATop) { |
Hal Canary | d892a9d | 2018-09-24 21:20:47 -0400 | [diff] [blame] | 1339 | ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1340 | this->drawFormXObject(dst, content.stream()); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1341 | return; |
| 1342 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1343 | blendMode = SkBlendMode::kClear; |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1344 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1345 | } else { |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1346 | srcFormXObject = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1349 | // TODO(vandebo) srcFormXObject may contain alpha, but here we want it |
| 1350 | // without alpha. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1351 | if (blendMode == SkBlendMode::kSrcATop) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1352 | // TODO(vandebo): In order to properly support SrcATop we have to track |
| 1353 | // the shape of what's been drawn at all times. It's the intersection of |
| 1354 | // the non-transparent parts of the device and the outlines (shape) of |
| 1355 | // all images and devices drawn. |
Hal Canary | c4c41c9 | 2018-09-28 13:30:45 -0400 | [diff] [blame] | 1356 | this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1357 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1358 | if (shape != nullptr) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1359 | // Draw shape into a form-xobject. |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1360 | SkPaint filledPaint; |
| 1361 | filledPaint.setColor(SK_ColorBLACK); |
| 1362 | filledPaint.setStyle(SkPaint::kFill_Style); |
Hal Canary | b400d4d | 2018-09-26 16:33:52 -0400 | [diff] [blame] | 1363 | SkClipStack empty; |
Hal Canary | 813b5ac | 2018-09-28 12:30:37 -0400 | [diff] [blame] | 1364 | SkPDFDevice shapeDev(this->size(), fDocument, fInitialTransform); |
| 1365 | shapeDev.internalDrawPath(clipStack ? *clipStack : empty, |
| 1366 | SkMatrix::I(), *shape, filledPaint, true); |
Hal Canary | c4c41c9 | 2018-09-28 13:30:45 -0400 | [diff] [blame] | 1367 | this->drawFormXObjectWithMask(dst, shapeDev.makeFormXObjectFromDevice(), |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1368 | SkBlendMode::kSrcOver, true); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1369 | } else { |
Hal Canary | c4c41c9 | 2018-09-28 13:30:45 -0400 | [diff] [blame] | 1370 | this->drawFormXObjectWithMask(dst, srcFormXObject, SkBlendMode::kSrcOver, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1371 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1374 | if (blendMode == SkBlendMode::kClear) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1375 | return; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1376 | } else if (blendMode == SkBlendMode::kSrc || |
| 1377 | blendMode == SkBlendMode::kDstATop) { |
Hal Canary | d892a9d | 2018-09-24 21:20:47 -0400 | [diff] [blame] | 1378 | ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1379 | if (content) { |
Hal Canary | 3b8b11e | 2018-09-29 22:31:34 -0400 | [diff] [blame] | 1380 | this->drawFormXObject(srcFormXObject, content.stream()); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1381 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1382 | if (blendMode == SkBlendMode::kSrc) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1383 | return; |
| 1384 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1385 | } else if (blendMode == SkBlendMode::kSrcATop) { |
Hal Canary | d892a9d | 2018-09-24 21:20:47 -0400 | [diff] [blame] | 1386 | ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1387 | if (content) { |
Hal Canary | 3b8b11e | 2018-09-29 22:31:34 -0400 | [diff] [blame] | 1388 | this->drawFormXObject(dst, content.stream()); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1389 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1392 | SkASSERT(blendMode == SkBlendMode::kSrcIn || |
| 1393 | blendMode == SkBlendMode::kDstIn || |
| 1394 | blendMode == SkBlendMode::kSrcOut || |
| 1395 | blendMode == SkBlendMode::kDstOut || |
| 1396 | blendMode == SkBlendMode::kSrcATop || |
| 1397 | blendMode == SkBlendMode::kDstATop || |
| 1398 | blendMode == SkBlendMode::kModulate); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1399 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1400 | if (blendMode == SkBlendMode::kSrcIn || |
| 1401 | blendMode == SkBlendMode::kSrcOut || |
| 1402 | blendMode == SkBlendMode::kSrcATop) { |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1403 | this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver, |
| 1404 | blendMode == SkBlendMode::kSrcOut); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1405 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1406 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1407 | SkBlendMode mode = SkBlendMode::kSrcOver; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1408 | if (blendMode == SkBlendMode::kModulate) { |
Hal Canary | c4c41c9 | 2018-09-28 13:30:45 -0400 | [diff] [blame] | 1409 | this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver, false); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1410 | mode = SkBlendMode::kMultiply; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1411 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1412 | this->drawFormXObjectWithMask(dst, srcFormXObject, mode, blendMode == SkBlendMode::kDstOut); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1413 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1414 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1417 | bool SkPDFDevice::isContentEmpty() { |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1418 | return fContent.bytesWritten() == 0 && fContentBuffer.bytesWritten() == 0; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 1421 | static SkSize rect_to_size(const SkRect& r) { return {r.width(), r.height()}; } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1422 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1423 | static sk_sp<SkImage> color_filter(const SkImage* image, |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1424 | SkColorFilter* colorFilter) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1425 | auto surface = |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1426 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(image->dimensions())); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1427 | SkASSERT(surface); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1428 | SkCanvas* canvas = surface->getCanvas(); |
| 1429 | canvas->clear(SK_ColorTRANSPARENT); |
| 1430 | SkPaint paint; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 1431 | paint.setColorFilter(sk_ref_sp(colorFilter)); |
Mike Reed | 34c56a5 | 2021-01-22 15:26:41 -0500 | [diff] [blame] | 1432 | canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1433 | return surface->makeImageSnapshot(); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | //////////////////////////////////////////////////////////////////////////////// |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1437 | |
| 1438 | static bool is_integer(SkScalar x) { |
| 1439 | return x == SkScalarTruncToScalar(x); |
| 1440 | } |
| 1441 | |
| 1442 | static bool is_integral(const SkRect& r) { |
| 1443 | return is_integer(r.left()) && |
| 1444 | is_integer(r.top()) && |
| 1445 | is_integer(r.right()) && |
| 1446 | is_integer(r.bottom()); |
| 1447 | } |
| 1448 | |
| 1449 | void SkPDFDevice::internalDrawImageRect(SkKeyedImage imageSubset, |
| 1450 | const SkRect* src, |
| 1451 | const SkRect& dst, |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 1452 | const SkSamplingOptions& sampling, |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1453 | const SkPaint& srcPaint, |
| 1454 | const SkMatrix& ctm) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 1455 | if (this->hasEmptyClip()) { |
| 1456 | return; |
| 1457 | } |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1458 | if (!imageSubset) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1459 | return; |
| 1460 | } |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1461 | |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1462 | // 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] | 1463 | SkIRect bounds = imageSubset.image()->bounds(); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1464 | SkRect srcRect = src ? *src : SkRect::Make(bounds); |
Mike Reed | 2ac6ce8 | 2021-01-15 12:26:22 -0500 | [diff] [blame] | 1465 | SkMatrix transform = SkMatrix::RectToRect(srcRect, dst); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1466 | if (src && *src != SkRect::Make(bounds)) { |
| 1467 | if (!srcRect.intersect(SkRect::Make(bounds))) { |
| 1468 | return; |
| 1469 | } |
| 1470 | srcRect.roundOut(&bounds); |
| 1471 | transform.preTranslate(SkIntToScalar(bounds.x()), |
| 1472 | SkIntToScalar(bounds.y())); |
| 1473 | if (bounds != imageSubset.image()->bounds()) { |
| 1474 | imageSubset = imageSubset.subset(bounds); |
| 1475 | } |
| 1476 | if (!imageSubset) { |
| 1477 | return; |
| 1478 | } |
| 1479 | } |
| 1480 | |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1481 | // If the image is opaque and the paint's alpha is too, replace |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1482 | // kSrc blendmode with kSrcOver. http://crbug.com/473572 |
| 1483 | SkTCopyOnFirstWrite<SkPaint> paint(srcPaint); |
Mike Reed | 90f4e9f | 2021-07-07 16:05:25 -0400 | [diff] [blame] | 1484 | if (!paint->isSrcOver() && |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1485 | imageSubset.image()->isOpaque() && |
| 1486 | kSrcOver_SkXfermodeInterpretation == SkInterpretXfermode(*paint, false)) |
| 1487 | { |
| 1488 | paint.writable()->setBlendMode(SkBlendMode::kSrcOver); |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 1489 | } |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1490 | |
| 1491 | // Alpha-only images need to get their color from the shader, before |
| 1492 | // applying the colorfilter. |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1493 | if (imageSubset.image()->isAlphaOnly() && paint->getColorFilter()) { |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1494 | // must blend alpha image and shader before applying colorfilter. |
| 1495 | auto surface = |
| 1496 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageSubset.image()->dimensions())); |
| 1497 | SkCanvas* canvas = surface->getCanvas(); |
| 1498 | SkPaint tmpPaint; |
| 1499 | // In the case of alpha images with shaders, the shader's coordinate |
| 1500 | // system is the image's coordiantes. |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1501 | tmpPaint.setShader(sk_ref_sp(paint->getShader())); |
| 1502 | tmpPaint.setColor4f(paint->getColor4f(), nullptr); |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1503 | canvas->clear(0x00000000); |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 1504 | canvas->drawImage(imageSubset.image().get(), 0, 0, sampling, &tmpPaint); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1505 | if (paint->getShader() != nullptr) { |
| 1506 | paint.writable()->setShader(nullptr); |
| 1507 | } |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1508 | imageSubset = SkKeyedImage(surface->makeImageSnapshot()); |
| 1509 | SkASSERT(!imageSubset.image()->isAlphaOnly()); |
| 1510 | } |
| 1511 | |
| 1512 | if (imageSubset.image()->isAlphaOnly()) { |
| 1513 | // The ColorFilter applies to the paint color/shader, not the alpha layer. |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1514 | SkASSERT(nullptr == paint->getColorFilter()); |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1515 | |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 1516 | sk_sp<SkImage> mask = alpha_image_to_greyscale_image(imageSubset.image().get()); |
| 1517 | if (!mask) { |
| 1518 | return; |
| 1519 | } |
| 1520 | // PDF doesn't seem to allow masking vector graphics with an Image XObject. |
| 1521 | // Must mask with a Form XObject. |
| 1522 | sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice(); |
| 1523 | { |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 1524 | SkCanvas canvas(maskDevice); |
Hal Canary | 6028fa7 | 2019-04-10 15:33:06 -0400 | [diff] [blame] | 1525 | // This clip prevents the mask image shader from covering |
| 1526 | // entire device if unnecessary. |
| 1527 | canvas.clipRect(this->cs().bounds(this->bounds())); |
| 1528 | canvas.concat(ctm); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1529 | if (paint->getMaskFilter()) { |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1530 | SkPaint tmpPaint; |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 1531 | tmpPaint.setShader(mask->makeShader(SkSamplingOptions(), transform)); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1532 | tmpPaint.setMaskFilter(sk_ref_sp(paint->getMaskFilter())); |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1533 | canvas.drawRect(dst, tmpPaint); |
| 1534 | } else { |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 1535 | if (src && !is_integral(*src)) { |
| 1536 | canvas.clipRect(dst); |
| 1537 | } |
| 1538 | canvas.concat(transform); |
| 1539 | canvas.drawImage(mask, 0, 0); |
| 1540 | } |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 1541 | } |
Hal Canary | 6028fa7 | 2019-04-10 15:33:06 -0400 | [diff] [blame] | 1542 | SkIRect maskDeviceBounds = maskDevice->cs().bounds(maskDevice->bounds()).roundOut(); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1543 | if (!ctm.isIdentity() && paint->getShader()) { |
| 1544 | transform_shader(paint.writable(), ctm); // Since we are using identity matrix. |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 1545 | } |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1546 | ScopedContentEntry content(this, &this->cs(), SkMatrix::I(), *paint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1547 | if (!content) { |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 1548 | return; |
| 1549 | } |
Hal Canary | 6028fa7 | 2019-04-10 15:33:06 -0400 | [diff] [blame] | 1550 | this->setGraphicState(SkPDFGraphicState::GetSMaskGraphicState( |
| 1551 | maskDevice->makeFormXObjectFromDevice(maskDeviceBounds, true), false, |
| 1552 | SkPDFGraphicState::kLuminosity_SMaskMode, fDocument), content.stream()); |
Hal Canary | 813b5ac | 2018-09-28 12:30:37 -0400 | [diff] [blame] | 1553 | SkPDFUtils::AppendRectangle(SkRect::Make(this->size()), content.stream()); |
Mike Reed | 7d34dc7 | 2019-11-26 12:17:17 -0500 | [diff] [blame] | 1554 | SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPathFillType::kWinding, content.stream()); |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 1555 | this->clearMaskOnGraphicState(content.stream()); |
| 1556 | return; |
| 1557 | } |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1558 | if (paint->getMaskFilter()) { |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 1559 | paint.writable()->setShader(imageSubset.image()->makeShader(SkSamplingOptions(), |
| 1560 | transform)); |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 1561 | SkPath path = SkPath::Rect(dst); // handles non-integral clipping. |
Michael Ludwig | c89d1b5 | 2019-10-18 11:32:56 -0400 | [diff] [blame] | 1562 | this->internalDrawPath(this->cs(), this->localToDevice(), path, *paint, true); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1563 | return; |
| 1564 | } |
| 1565 | transform.postConcat(ctm); |
| 1566 | |
| 1567 | bool needToRestore = false; |
| 1568 | if (src && !is_integral(*src)) { |
| 1569 | // Need sub-pixel clipping to fix https://bug.skia.org/4374 |
| 1570 | this->cs().save(); |
| 1571 | this->cs().clipRect(dst, ctm, SkClipOp::kIntersect, true); |
| 1572 | needToRestore = true; |
| 1573 | } |
| 1574 | SK_AT_SCOPE_EXIT(if (needToRestore) { this->cs().restore(); }); |
| 1575 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1576 | SkMatrix matrix = transform; |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1577 | |
| 1578 | // Rasterize the bitmap using perspective in a new bitmap. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1579 | if (transform.hasPerspective()) { |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 1580 | // Transform the bitmap in the new space, without taking into |
| 1581 | // account the initial transform. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1582 | SkRect imageBounds = SkRect::Make(imageSubset.image()->bounds()); |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 1583 | SkPath perspectiveOutline = SkPath::Rect(imageBounds).makeTransform(transform); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1584 | |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1585 | // Retrieve the bounds of the new shape. |
John Stiles | 0e15769 | 2021-08-02 15:49:20 -0400 | [diff] [blame] | 1586 | SkRect outlineBounds = perspectiveOutline.getBounds(); |
| 1587 | if (!outlineBounds.intersect(SkRect::Make(this->devClipBounds()))) { |
Michael Ludwig | 3037838 | 2020-10-08 16:29:14 -0400 | [diff] [blame] | 1588 | return; |
| 1589 | } |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1590 | |
Michael Ludwig | 3037838 | 2020-10-08 16:29:14 -0400 | [diff] [blame] | 1591 | // Transform the bitmap in the new space to the final space, to account for DPI |
John Stiles | 0e15769 | 2021-08-02 15:49:20 -0400 | [diff] [blame] | 1592 | SkRect physicalBounds = fInitialTransform.mapRect(outlineBounds); |
| 1593 | SkScalar scaleX = physicalBounds.width() / outlineBounds.width(); |
| 1594 | SkScalar scaleY = physicalBounds.height() / outlineBounds.height(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1595 | |
| 1596 | // TODO(edisonn): A better approach would be to use a bitmap shader |
| 1597 | // (in clamp mode) and draw a rect over the entire bounding box. Then |
| 1598 | // intersect perspectiveOutline to the clip. That will avoid introducing |
| 1599 | // alpha to the image while still giving good behavior at the edge of |
| 1600 | // the image. Avoiding alpha will reduce the pdf size and generation |
| 1601 | // CPU time some. |
| 1602 | |
Michael Ludwig | 3037838 | 2020-10-08 16:29:14 -0400 | [diff] [blame] | 1603 | SkISize wh = rect_to_size(physicalBounds).toCeil(); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1604 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 1605 | auto surface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1606 | if (!surface) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 1607 | return; |
| 1608 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1609 | SkCanvas* canvas = surface->getCanvas(); |
| 1610 | canvas->clear(SK_ColorTRANSPARENT); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1611 | |
John Stiles | 0e15769 | 2021-08-02 15:49:20 -0400 | [diff] [blame] | 1612 | SkScalar deltaX = outlineBounds.left(); |
| 1613 | SkScalar deltaY = outlineBounds.top(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1614 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1615 | SkMatrix offsetMatrix = transform; |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1616 | offsetMatrix.postTranslate(-deltaX, -deltaY); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 1617 | offsetMatrix.postScale(scaleX, scaleY); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1618 | |
| 1619 | // Translate the draw in the new canvas, so we perfectly fit the |
| 1620 | // shape in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1621 | canvas->setMatrix(offsetMatrix); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1622 | canvas->drawImage(imageSubset.image(), 0, 0); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1623 | // Make sure the final bits are in the bitmap. |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 1624 | surface->flushAndSubmit(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1625 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 1626 | // In the new space, we use the identity matrix translated |
| 1627 | // and scaled to reflect DPI. |
| 1628 | matrix.setScale(1 / scaleX, 1 / scaleY); |
| 1629 | matrix.postTranslate(deltaX, deltaY); |
| 1630 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1631 | imageSubset = SkKeyedImage(surface->makeImageSnapshot()); |
| 1632 | if (!imageSubset) { |
| 1633 | return; |
| 1634 | } |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 1637 | SkMatrix scaled; |
| 1638 | // Adjust for origin flip. |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 1639 | scaled.setScale(SK_Scalar1, -SK_Scalar1); |
| 1640 | scaled.postTranslate(0, SK_Scalar1); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 1641 | // Scale the image up from 1x1 to WxH. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1642 | SkIRect subset = imageSubset.image()->bounds(); |
| 1643 | scaled.postScale(SkIntToScalar(subset.width()), |
| 1644 | SkIntToScalar(subset.height())); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 1645 | scaled.postConcat(matrix); |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1646 | ScopedContentEntry content(this, &this->cs(), scaled, *paint); |
Hal Canary | 42137de | 2018-10-08 16:00:37 -0400 | [diff] [blame] | 1647 | if (!content) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1648 | return; |
| 1649 | } |
| 1650 | if (content.needShape()) { |
Mike Reed | 58cc97a | 2020-08-24 15:15:01 -0400 | [diff] [blame] | 1651 | SkPath shape = SkPath::Rect(SkRect::Make(subset)).makeTransform(matrix); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1652 | content.setShape(shape); |
| 1653 | } |
| 1654 | if (!content.needSource()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1655 | return; |
| 1656 | } |
| 1657 | |
Hal Canary | cfb0e02 | 2019-04-08 11:52:55 -0400 | [diff] [blame] | 1658 | if (SkColorFilter* colorFilter = paint->getColorFilter()) { |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1659 | sk_sp<SkImage> img = color_filter(imageSubset.image().get(), colorFilter); |
| 1660 | imageSubset = SkKeyedImage(std::move(img)); |
| 1661 | if (!imageSubset) { |
| 1662 | return; |
| 1663 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1664 | // TODO(halcanary): de-dupe this by caching filtered images. |
| 1665 | // (maybe in the resource cache?) |
| 1666 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1667 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1668 | SkBitmapKey key = imageSubset.key(); |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 1669 | SkPDFIndirectReference* pdfimagePtr = fDocument->fPDFBitmapMap.find(key); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1670 | SkPDFIndirectReference pdfimage = pdfimagePtr ? *pdfimagePtr : SkPDFIndirectReference(); |
| 1671 | if (!pdfimagePtr) { |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1672 | SkASSERT(imageSubset); |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1673 | pdfimage = SkPDFSerializeImage(imageSubset.image().get(), fDocument, |
Hal Canary | a121183 | 2018-11-13 16:45:14 -0500 | [diff] [blame] | 1674 | fDocument->metadata().fEncodingQuality); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1675 | SkASSERT((key != SkBitmapKey{{0, 0, 0, 0}, 0})); |
Hal Canary | 4ca9fa3 | 2018-12-21 16:15:01 -0500 | [diff] [blame] | 1676 | fDocument->fPDFBitmapMap.set(key, pdfimage); |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 1677 | } |
Hal Canary | 9a3f554 | 2018-12-10 19:59:07 -0500 | [diff] [blame] | 1678 | SkASSERT(pdfimage != SkPDFIndirectReference()); |
| 1679 | this->drawFormXObject(pdfimage, content.stream()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1680 | } |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1681 | |
| 1682 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1683 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1684 | |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 1685 | void SkPDFDevice::drawDevice(SkBaseDevice* device, const SkSamplingOptions& sampling, |
| 1686 | const SkPaint& paint) { |
Michael Ludwig | 278263d | 2020-10-12 19:49:24 +0000 | [diff] [blame] | 1687 | SkASSERT(!paint.getImageFilter()); |
| 1688 | SkASSERT(!paint.getMaskFilter()); |
| 1689 | |
| 1690 | // Check if the source device is really a bitmapdevice (because that's what we returned |
| 1691 | // from createDevice (an image filter would go through drawSpecial, but createDevice uses |
| 1692 | // a raster device to apply color filters, too). |
| 1693 | SkPixmap pmap; |
| 1694 | if (device->peekPixels(&pmap)) { |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 1695 | this->INHERITED::drawDevice(device, sampling, paint); |
Michael Ludwig | 278263d | 2020-10-12 19:49:24 +0000 | [diff] [blame] | 1696 | return; |
| 1697 | } |
| 1698 | |
| 1699 | // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses. |
| 1700 | SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device); |
| 1701 | |
| 1702 | if (pdfDevice->isContentEmpty()) { |
| 1703 | return; |
| 1704 | } |
| 1705 | |
| 1706 | SkMatrix matrix = device->getRelativeTransform(*this); |
| 1707 | ScopedContentEntry content(this, &this->cs(), matrix, paint); |
| 1708 | if (!content) { |
| 1709 | return; |
| 1710 | } |
| 1711 | if (content.needShape()) { |
| 1712 | SkPath shape = SkPath::Rect(SkRect::Make(device->imageInfo().dimensions())); |
| 1713 | shape.transform(matrix); |
| 1714 | content.setShape(shape); |
| 1715 | } |
| 1716 | if (!content.needSource()) { |
| 1717 | return; |
| 1718 | } |
| 1719 | this->drawFormXObject(pdfDevice->makeFormXObjectFromDevice(), content.stream()); |
| 1720 | } |
| 1721 | |
| 1722 | void SkPDFDevice::drawSpecial(SkSpecialImage* srcImg, const SkMatrix& localToDevice, |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 1723 | const SkSamplingOptions& sampling, const SkPaint& paint) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 1724 | if (this->hasEmptyClip()) { |
| 1725 | return; |
| 1726 | } |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1727 | SkASSERT(!srcImg->isTextureBacked()); |
Michael Ludwig | 16d5b0a | 2020-08-31 13:07:16 -0400 | [diff] [blame] | 1728 | SkASSERT(!paint.getMaskFilter() && !paint.getImageFilter()); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1729 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1730 | SkBitmap resultBM; |
Michael Ludwig | 16d5b0a | 2020-08-31 13:07:16 -0400 | [diff] [blame] | 1731 | if (srcImg->getROPixels(&resultBM)) { |
Michael Ludwig | 278263d | 2020-10-12 19:49:24 +0000 | [diff] [blame] | 1732 | auto r = SkRect::MakeWH(resultBM.width(), resultBM.height()); |
Mike Reed | d8ff313 | 2021-01-02 12:57:04 -0500 | [diff] [blame] | 1733 | this->internalDrawImageRect(SkKeyedImage(resultBM), nullptr, r, sampling, paint, |
| 1734 | localToDevice); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) { |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 1739 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap, this->surfaceProps()); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) { |
Chris Dalton | f5b87f9 | 2021-04-19 17:27:09 -0600 | [diff] [blame] | 1743 | return SkSpecialImage::MakeFromImage(nullptr, image->bounds(), image->makeNonTextureImage(), |
| 1744 | this->surfaceProps()); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 1747 | SkImageFilterCache* SkPDFDevice::getImageFilterCache() { |
| 1748 | // We always return a transient cache, so it is freed after each |
| 1749 | // filter traversal. |
| 1750 | return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize); |
| 1751 | } |