blob: 052bab2d07c03d48fff5c26c805f5036f52711a4 [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00006 */
7
8#include "SkPDFDevice.h"
halcanarye06ca962016-09-09 05:34:55 -07009
halcanaryf59d18a2016-09-16 14:44:57 -070010#include "SkAdvancedTypefaceMetrics.h"
reedf70b5312016-03-04 16:36:20 -080011#include "SkAnnotationKeys.h"
Mike Reed986480a2017-01-13 22:43:16 +000012#include "SkBitmapDevice.h"
martina.kollarovab8d6af12016-06-29 05:12:31 -070013#include "SkBitmapKey.h"
Hal Canary7cbf5e32017-07-12 13:10:23 -040014#include "SkCanvas.h"
Hal Canaryd12a6762017-05-26 17:01:16 -040015#include "SkClipOpPriv.h"
Hal Canary1521c8a2018-03-28 09:51:00 -070016#include "SkClusterator.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000017#include "SkColor.h"
halcanary287d22d2015-09-24 10:20:05 -070018#include "SkColorFilter.h"
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000019#include "SkDraw.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000020#include "SkGlyphCache.h"
Herb Derby736db102018-07-19 12:52:16 -040021#include "SkGlyphRun.h"
brianosman04a44d02016-09-21 09:46:57 -070022#include "SkImageFilterCache.h"
Hal Canaryd12a6762017-05-26 17:01:16 -040023#include "SkJpegEncoder.h"
halcanary022c2bd2016-09-02 11:29:46 -070024#include "SkMakeUnique.h"
Mike Reed80747ef2018-01-23 15:29:32 -050025#include "SkMaskFilterBase.h"
halcanarydb0dcc72015-03-20 12:31:52 -070026#include "SkPDFBitmap.h"
halcanary7a14b312015-10-01 07:28:13 -070027#include "SkPDFCanon.h"
Hal Canary23564b92018-09-07 14:33:14 -040028#include "SkPDFDocumentPriv.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000029#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000030#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000031#include "SkPDFGraphicState.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000032#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000033#include "SkPDFShader.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000034#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000035#include "SkPDFUtils.h"
Hal Canaryd12a6762017-05-26 17:01:16 -040036#include "SkPath.h"
37#include "SkPathEffect.h"
38#include "SkPathOps.h"
Hal Canaryf50ff392016-09-30 10:25:39 -040039#include "SkPixelRef.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000040#include "SkRRect.h"
Hal Canaryd12a6762017-05-26 17:01:16 -040041#include "SkRasterClip.h"
halcanary4871f222016-08-26 13:17:44 -070042#include "SkScopeExit.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000043#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070044#include "SkSurface.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000045#include "SkTemplates.h"
Florin Malitaab54e732018-07-27 09:47:15 -040046#include "SkTextBlob.h"
halcanarye06ca962016-09-09 05:34:55 -070047#include "SkTextFormatParams.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040048#include "SkTo.h"
Hal Canaryea60b952018-08-21 11:45:46 -040049#include "SkUTF.h"
halcanarya6814332015-05-27 08:53:36 -070050#include "SkXfermodeInterpretation.h"
Hal Canaryd12a6762017-05-26 17:01:16 -040051
Hal Canary9e41c212018-09-03 12:00:23 -040052#include <vector>
53
Hal Canaryd12a6762017-05-26 17:01:16 -040054#ifndef SK_PDF_MASK_QUALITY
55 // If MASK_QUALITY is in [0,100], will be used for JpegEncoder.
56 // Otherwise, just encode masks losslessly.
57 #define SK_PDF_MASK_QUALITY 50
58 // Since these masks are used for blurry shadows, we shouldn't need
59 // high quality. Raise this value if your shadows have visible JPEG
60 // artifacts.
61 // If SkJpegEncoder::Encode fails, we will fall back to the lossless
62 // encoding.
63#endif
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000064
65// Utility functions
66
Hal Canaryd791a6f2018-09-28 12:14:08 -040067static SkPath to_path(const SkRect& r) {
68 SkPath p;
69 p.addRect(r);
70 return p;
71}
72
Hal Canary51329c92017-06-27 14:28:37 -040073// This function destroys the mask and either frees or takes the pixels.
74sk_sp<SkImage> mask_to_greyscale_image(SkMask* mask) {
75 sk_sp<SkImage> img;
76 SkPixmap pm(SkImageInfo::Make(mask->fBounds.width(), mask->fBounds.height(),
77 kGray_8_SkColorType, kOpaque_SkAlphaType),
78 mask->fImage, mask->fRowBytes);
79 const int imgQuality = SK_PDF_MASK_QUALITY;
80 if (imgQuality <= 100 && imgQuality >= 0) {
81 SkDynamicMemoryWStream buffer;
82 SkJpegEncoder::Options jpegOptions;
83 jpegOptions.fQuality = imgQuality;
84 if (SkJpegEncoder::Encode(&buffer, pm, jpegOptions)) {
85 img = SkImage::MakeFromEncoded(buffer.detachAsData());
86 SkASSERT(img);
87 if (img) {
88 SkMask::FreeImage(mask->fImage);
89 }
90 }
91 }
92 if (!img) {
93 img = SkImage::MakeFromRaster(pm, [](const void* p, void*) { SkMask::FreeImage((void*)p); },
94 nullptr);
95 }
96 *mask = SkMask(); // destructive;
97 return img;
98}
99
Hal Canaryd425a1d2017-07-12 13:13:51 -0400100sk_sp<SkImage> alpha_image_to_greyscale_image(const SkImage* mask) {
101 int w = mask->width(), h = mask->height();
102 SkBitmap greyBitmap;
103 greyBitmap.allocPixels(SkImageInfo::Make(w, h, kGray_8_SkColorType, kOpaque_SkAlphaType));
104 if (!mask->readPixels(SkImageInfo::MakeA8(w, h),
105 greyBitmap.getPixels(), greyBitmap.rowBytes(), 0, 0)) {
106 return nullptr;
107 }
108 return SkImage::MakeFromBitmap(greyBitmap);
109}
110
Mike Reeda1361362017-03-07 09:37:29 -0500111static void draw_points(SkCanvas::PointMode mode,
112 size_t count,
113 const SkPoint* points,
114 const SkPaint& paint,
115 const SkIRect& bounds,
116 const SkMatrix& ctm,
117 SkBaseDevice* device) {
118 SkRasterClip rc(bounds);
119 SkDraw draw;
120 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(bounds.right(), bounds.bottom()), nullptr, 0);
121 draw.fMatrix = &ctm;
122 draw.fRC = &rc;
123 draw.drawPoints(mode, count, points, paint, device);
124}
125
reed374772b2016-10-05 17:33:02 -0700126// If the paint will definitely draw opaquely, replace kSrc with
127// kSrcOver. http://crbug.com/473572
halcanarya6814332015-05-27 08:53:36 -0700128static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
reed374772b2016-10-05 17:33:02 -0700129 if (kSrcOver_SkXfermodeInterpretation == SkInterpretXfermode(*paint, false)) {
130 paint->setBlendMode(SkBlendMode::kSrcOver);
halcanarya6814332015-05-27 08:53:36 -0700131 }
132}
133
Hal Canaryd12a6762017-05-26 17:01:16 -0400134// A shader's matrix is: CTMM x LocalMatrix x WrappingLocalMatrix. We want to
135// switch to device space, where CTM = I, while keeping the original behavior.
136//
137// I * LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix
138// LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix
139// InvLocalMatrix * LocalMatrix * NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix
140// NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix
141//
142static void transform_shader(SkPaint* paint, const SkMatrix& ctm) {
143 SkMatrix lm = SkPDFUtils::GetShaderLocalMatrix(paint->getShader());
144 SkMatrix lmInv;
145 if (lm.invert(&lmInv)) {
146 SkMatrix m = SkMatrix::Concat(SkMatrix::Concat(lmInv, ctm), lm);
147 paint->setShader(paint->getShader()->makeWithLocalMatrix(m));
148 }
149}
150
Hal Canary04ac4612018-10-10 13:09:43 -0400151static void emit_pdf_color(SkColor4f color, SkWStream* result) {
152 SkASSERT(color.fA == 1); // We handle alpha elsewhere.
153 SkPDFUtils::AppendColorComponentF(color.fR, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000154 result->writeText(" ");
Hal Canary04ac4612018-10-10 13:09:43 -0400155 SkPDFUtils::AppendColorComponentF(color.fG, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000156 result->writeText(" ");
Hal Canary04ac4612018-10-10 13:09:43 -0400157 SkPDFUtils::AppendColorComponentF(color.fB, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000158 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000159}
160
Hal Canaryd12a6762017-05-26 17:01:16 -0400161// If the paint has a color filter, apply the color filter to the shader or the
162// paint color. Remove the color filter.
163void remove_color_filter(SkPaint* paint) {
164 if (SkColorFilter* cf = paint->getColorFilter()) {
165 if (SkShader* shader = paint->getShader()) {
166 paint->setShader(shader->makeWithColorFilter(paint->refColorFilter()));
167 } else {
Hal Canary04ac4612018-10-10 13:09:43 -0400168 paint->setColor4f(cf->filterColor4f(paint->getColor4f(), nullptr), nullptr);
Hal Canaryd12a6762017-05-26 17:01:16 -0400169 }
170 paint->setColorFilter(nullptr);
171 }
172}
173
Hal Canary42137de2018-10-08 16:00:37 -0400174SkPDFDevice::GraphicStackState::GraphicStackState(SkDynamicMemoryWStream* s) : fContentStream(s) {
175}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000176
Hal Canaryb400d4d2018-09-26 16:33:52 -0400177void SkPDFDevice::GraphicStackState::drainStack() {
178 if (fContentStream) {
179 while (fStackDepth) {
Hal Canaryb5b72792018-09-28 08:49:33 -0400180 this->pop();
Hal Canaryb400d4d2018-09-26 16:33:52 -0400181 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000182 }
Hal Canaryb400d4d2018-09-26 16:33:52 -0400183 SkASSERT(fStackDepth == 0);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000184}
185
Hal Canaryb400d4d2018-09-26 16:33:52 -0400186void SkPDFDevice::GraphicStackState::push() {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000187 SkASSERT(fStackDepth < kMaxStackDepth);
188 fContentStream->writeText("q\n");
189 fStackDepth++;
190 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
191}
192
Hal Canaryb400d4d2018-09-26 16:33:52 -0400193void SkPDFDevice::GraphicStackState::pop() {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000194 SkASSERT(fStackDepth > 0);
195 fContentStream->writeText("Q\n");
Hal Canaryb400d4d2018-09-26 16:33:52 -0400196 fEntries[fStackDepth] = SkPDFDevice::GraphicStateEntry();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000197 fStackDepth--;
198}
199
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000200/* Calculate an inverted path's equivalent non-inverted path, given the
201 * canvas bounds.
202 * outPath may alias with invPath (since this is supported by PathOps).
203 */
204static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
205 SkPath* outPath) {
206 SkASSERT(invPath.isInverseFillType());
Hal Canaryd791a6f2018-09-28 12:14:08 -0400207 return Op(to_path(bounds), invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000208}
209
Hal Canaryd00ef062018-06-05 11:53:58 -0400210static SkRect rect_intersect(SkRect u, SkRect v) {
211 if (u.isEmpty() || v.isEmpty()) { return {0, 0, 0, 0}; }
212 return u.intersect(v) ? u : SkRect{0, 0, 0, 0};
213}
214
215// Test to see if the clipstack is a simple rect, If so, we can avoid all PathOps code
216// and speed thing up.
217static bool is_rect(const SkClipStack& clipStack, const SkRect& bounds, SkRect* dst) {
218 SkRect currentClip = bounds;
219 SkClipStack::Iter iter(clipStack, SkClipStack::Iter::kBottom_IterStart);
220 while (const SkClipStack::Element* element = iter.next()) {
221 SkRect elementRect{0, 0, 0, 0};
222 switch (element->getDeviceSpaceType()) {
223 case SkClipStack::Element::DeviceSpaceType::kEmpty:
224 break;
225 case SkClipStack::Element::DeviceSpaceType::kRect:
226 elementRect = element->getDeviceSpaceRect();
227 break;
228 default:
229 return false;
230 }
231 switch (element->getOp()) {
232 case kReplace_SkClipOp:
233 currentClip = rect_intersect(bounds, elementRect);
234 break;
235 case SkClipOp::kIntersect:
236 currentClip = rect_intersect(currentClip, elementRect);
237 break;
238 default:
239 return false;
240 }
241 }
242 *dst = currentClip;
243 return true;
244}
245
246static void append_clip(const SkClipStack& clipStack,
247 const SkIRect& bounds,
248 SkWStream* wStream) {
249 // The bounds are slightly outset to ensure this is correct in the
250 // face of floating-point accuracy and possible SkRegion bitmap
251 // approximations.
252 SkRect outsetBounds = SkRect::Make(bounds.makeOutset(1, 1));
253
254 SkRect clipStackRect;
255 if (is_rect(clipStack, outsetBounds, &clipStackRect)) {
256 SkPDFUtils::AppendRectangle(clipStackRect, wStream);
257 wStream->writeText("W* n\n");
258 return;
259 }
260
261 SkPath clipPath;
262 (void)clipStack.asPath(&clipPath);
263
Hal Canaryd791a6f2018-09-28 12:14:08 -0400264 if (Op(clipPath, to_path(outsetBounds), kIntersect_SkPathOp, &clipPath)) {
Hal Canaryd00ef062018-06-05 11:53:58 -0400265 SkPDFUtils::EmitPath(clipPath, SkPaint::kFill_Style, wStream);
266 SkPath::FillType clipFill = clipPath.getFillType();
267 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
268 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
269 if (clipFill == SkPath::kEvenOdd_FillType) {
270 wStream->writeText("W* n\n");
271 } else {
272 wStream->writeText("W n\n");
273 }
274 }
275 // If Op() fails (pathological case; e.g. input values are
276 // extremely large or NaN), emit no clip at all.
277}
278
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000279// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000280// graphic state stack, and the fact that we can know all the clips used
281// on the page to optimize this.
Hal Canaryb400d4d2018-09-26 16:33:52 -0400282void SkPDFDevice::GraphicStackState::updateClip(const SkClipStack* clipStack,
283 const SkIRect& bounds) {
284 uint32_t clipStackGenID = clipStack ? clipStack->getTopmostGenID()
285 : SkClipStack::kWideOpenGenID;
286 if (clipStackGenID == currentEntry()->fClipStackGenID) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000287 return;
288 }
289
290 while (fStackDepth > 0) {
Hal Canaryb5b72792018-09-28 08:49:33 -0400291 this->pop();
Hal Canaryb400d4d2018-09-26 16:33:52 -0400292 if (clipStackGenID == currentEntry()->fClipStackGenID) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000293 return;
294 }
295 }
Hal Canaryb5b72792018-09-28 08:49:33 -0400296 SkASSERT(currentEntry()->fClipStackGenID == SkClipStack::kWideOpenGenID);
Hal Canaryb400d4d2018-09-26 16:33:52 -0400297 if (clipStackGenID != SkClipStack::kWideOpenGenID) {
298 SkASSERT(clipStack);
Hal Canaryb5b72792018-09-28 08:49:33 -0400299 this->push();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000300
Hal Canaryb400d4d2018-09-26 16:33:52 -0400301 currentEntry()->fClipStackGenID = clipStackGenID;
302 append_clip(*clipStack, bounds, fContentStream);
303 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000304}
305
Hal Canarye650b852018-09-12 09:12:36 -0400306static void append_transform(const SkMatrix& matrix, SkWStream* content) {
307 SkScalar values[6];
308 if (!matrix.asAffine(values)) {
309 SkMatrix::SetAffineIdentity(values);
310 }
311 for (SkScalar v : values) {
312 SkPDFUtils::AppendScalar(v, content);
313 content->writeText(" ");
314 }
315 content->writeText("cm\n");
316}
317
Hal Canaryb400d4d2018-09-26 16:33:52 -0400318void SkPDFDevice::GraphicStackState::updateMatrix(const SkMatrix& matrix) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000319 if (matrix == currentEntry()->fMatrix) {
320 return;
321 }
322
323 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
324 SkASSERT(fStackDepth > 0);
Hal Canaryb400d4d2018-09-26 16:33:52 -0400325 SkASSERT(fEntries[fStackDepth].fClipStackGenID ==
326 fEntries[fStackDepth -1].fClipStackGenID);
Hal Canaryb5b72792018-09-28 08:49:33 -0400327 this->pop();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000328
329 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
330 }
331 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
332 return;
333 }
334
Hal Canaryb5b72792018-09-28 08:49:33 -0400335 this->push();
Hal Canarye650b852018-09-12 09:12:36 -0400336 append_transform(matrix, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000337 currentEntry()->fMatrix = matrix;
338}
339
Hal Canaryb400d4d2018-09-26 16:33:52 -0400340void SkPDFDevice::GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000341 // PDF treats a shader as a color, so we only set one or the other.
342 if (state.fShaderIndex >= 0) {
343 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000344 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000345 currentEntry()->fShaderIndex = state.fShaderIndex;
346 }
347 } else {
348 if (state.fColor != currentEntry()->fColor ||
349 currentEntry()->fShaderIndex >= 0) {
350 emit_pdf_color(state.fColor, fContentStream);
351 fContentStream->writeText("RG ");
352 emit_pdf_color(state.fColor, fContentStream);
353 fContentStream->writeText("rg\n");
354 currentEntry()->fColor = state.fColor;
355 currentEntry()->fShaderIndex = -1;
356 }
357 }
358
359 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000360 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000361 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
362 }
363
364 if (state.fTextScaleX) {
365 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
Hal Canary9b491b22017-11-28 15:10:13 -0500366 SkScalar pdfScale = state.fTextScaleX * 100;
halcanarybc4696b2015-05-06 10:56:04 -0700367 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000368 fContentStream->writeText(" Tz\n");
369 currentEntry()->fTextScaleX = state.fTextScaleX;
370 }
371 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700372 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
373 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
374 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
Cary Clark60ebf142018-09-06 12:22:33 +0000375 fContentStream->writeDecAsText(state.fTextFill);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000376 fContentStream->writeText(" Tr\n");
377 currentEntry()->fTextFill = state.fTextFill;
378 }
379 }
380}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000381
reed76033be2015-03-14 10:54:31 -0700382static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700383 // PDF does not support image filters, so render them on CPU.
384 // Note that this rendering is done at "screen" resolution (100dpi), not
385 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700386 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800387 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700388
389 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700390 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700391}
392
393SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
reedcd4051e2016-07-15 09:41:26 -0700394 if (layerPaint && not_supported_for_layers(*layerPaint)) {
reed7503d602016-07-15 14:23:29 -0700395 // need to return a raster device, which we will detect in drawDevice()
396 return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
senorblancob0e89dc2014-10-20 14:03:12 -0700397 }
Hal Canary22b2d8c2017-07-19 14:46:12 -0400398 return new SkPDFDevice(cinfo.fInfo.dimensions(), fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000399}
400
halcanary989da4a2016-03-21 14:33:17 -0700401SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
402
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000403// A helper class to automatically finish a ContentEntry at the end of a
404// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000405class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000406public:
Mike Reeda1361362017-03-07 09:37:29 -0500407 ScopedContentEntry(SkPDFDevice* device,
Hal Canaryd892a9d2018-09-24 21:20:47 -0400408 const SkClipStack* clipStack,
Mike Reed27d07f02017-03-04 21:47:47 +0000409 const SkMatrix& matrix,
Mike Reeda1361362017-03-07 09:37:29 -0500410 const SkPaint& paint,
411 bool hasText = false)
412 : fDevice(device)
Mike Reeda1361362017-03-07 09:37:29 -0500413 , fBlendMode(SkBlendMode::kSrcOver)
Hal Canaryb400d4d2018-09-26 16:33:52 -0400414 , fClipStack(clipStack)
Mike Reeda1361362017-03-07 09:37:29 -0500415 {
416 if (matrix.hasPerspective()) {
417 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
418 return;
419 }
420 fBlendMode = paint.getBlendMode();
Hal Canary42137de2018-10-08 16:00:37 -0400421 fContentStream =
Mike Reeda1361362017-03-07 09:37:29 -0500422 fDevice->setUpContentEntry(clipStack, matrix, paint, hasText, &fDstFormXObject);
Mike Reed27d07f02017-03-04 21:47:47 +0000423 }
Mike Reeda1361362017-03-07 09:37:29 -0500424 ScopedContentEntry(SkPDFDevice* dev, const SkPaint& paint, bool hasText = false)
Hal Canaryd892a9d2018-09-24 21:20:47 -0400425 : ScopedContentEntry(dev, &dev->cs(), dev->ctm(), paint, hasText) {}
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000426
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000427 ~ScopedContentEntry() {
Hal Canary42137de2018-10-08 16:00:37 -0400428 if (fContentStream) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000429 SkPath* shape = &fShape;
430 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700431 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000432 }
Hal Canaryb400d4d2018-09-26 16:33:52 -0400433 fDevice->finishContentEntry(fClipStack, fBlendMode, std::move(fDstFormXObject), shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000434 }
435 }
436
Hal Canary42137de2018-10-08 16:00:37 -0400437 explicit operator bool() const { return fContentStream != nullptr; }
438 SkDynamicMemoryWStream* stream() { return fContentStream; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000439
440 /* Returns true when we explicitly need the shape of the drawing. */
441 bool needShape() {
reed374772b2016-10-05 17:33:02 -0700442 switch (fBlendMode) {
443 case SkBlendMode::kClear:
444 case SkBlendMode::kSrc:
445 case SkBlendMode::kSrcIn:
446 case SkBlendMode::kSrcOut:
447 case SkBlendMode::kDstIn:
448 case SkBlendMode::kDstOut:
449 case SkBlendMode::kSrcATop:
450 case SkBlendMode::kDstATop:
451 case SkBlendMode::kModulate:
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000452 return true;
453 default:
454 return false;
455 }
456 }
457
458 /* Returns true unless we only need the shape of the drawing. */
459 bool needSource() {
reed374772b2016-10-05 17:33:02 -0700460 if (fBlendMode == SkBlendMode::kClear) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000461 return false;
462 }
463 return true;
464 }
465
466 /* If the shape is different than the alpha component of the content, then
467 * setShape should be called with the shape. In particular, images and
468 * devices have rectangular shape.
469 */
470 void setShape(const SkPath& shape) {
471 fShape = shape;
472 }
473
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000474private:
Hal Canary42137de2018-10-08 16:00:37 -0400475 SkPDFDevice* fDevice = nullptr;
476 SkDynamicMemoryWStream* fContentStream = nullptr;
reed374772b2016-10-05 17:33:02 -0700477 SkBlendMode fBlendMode;
halcanarydabd4f02016-08-03 11:16:56 -0700478 sk_sp<SkPDFObject> fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000479 SkPath fShape;
Hal Canaryb400d4d2018-09-26 16:33:52 -0400480 const SkClipStack* fClipStack;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000481};
482
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000483////////////////////////////////////////////////////////////////////////////////
484
Hal Canary7d06ab22018-09-10 14:39:13 -0400485SkPDFDevice::SkPDFDevice(SkISize pageSize, SkPDFDocument* doc, const SkMatrix& transform)
reed589a39e2016-08-20 07:59:19 -0700486 : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()),
487 SkSurfaceProps(0, kUnknown_SkPixelGeometry))
Hal Canary7d06ab22018-09-10 14:39:13 -0400488 , fInitialTransform(transform)
Dominic Mazzoni656cefe2018-09-25 20:29:15 -0700489 , fNodeId(0)
Hal Canarya0622582017-06-29 18:51:35 -0400490 , fDocument(doc)
491{
Hal Canary22b2d8c2017-07-19 14:46:12 -0400492 SkASSERT(!pageSize.isEmpty());
Hal Canarya0622582017-06-29 18:51:35 -0400493}
robertphillips1f3923e2016-07-21 07:17:54 -0700494
Hal Canary9e41c212018-09-03 12:00:23 -0400495SkPDFDevice::~SkPDFDevice() = default;
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000496
Hal Canary9e41c212018-09-03 12:00:23 -0400497void SkPDFDevice::reset() {
Hal Canary9e41c212018-09-03 12:00:23 -0400498 fLinkToURLs = std::vector<RectWithData>();
499 fLinkToDestinations = std::vector<RectWithData>();
500 fNamedDestinations = std::vector<NamedDestination>();
501 fGraphicStateResources = std::vector<sk_sp<SkPDFObject>>();
502 fXObjectResources = std::vector<sk_sp<SkPDFObject>>();
503 fShaderResources = std::vector<sk_sp<SkPDFObject>>();
504 fFontResources = std::vector<sk_sp<SkPDFFont>>();
Hal Canary42137de2018-10-08 16:00:37 -0400505 fContent.reset();
Hal Canary813b5ac2018-09-28 12:30:37 -0400506 fActiveStackState = GraphicStackState();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000507}
508
Mike Reeda1361362017-03-07 09:37:29 -0500509void SkPDFDevice::drawAnnotation(const SkRect& rect, const char key[], SkData* value) {
Hal Canary9cd21682017-02-22 15:55:06 -0500510 if (!value) {
511 return;
512 }
513 if (rect.isEmpty()) {
Dominic Mazzoni656cefe2018-09-25 20:29:15 -0700514 if (!strcmp(key, SkPDFGetNodeIdKey())) {
515 int nodeID;
516 if (value->size() != sizeof(nodeID)) { return; }
517 memcpy(&nodeID, value->data(), sizeof(nodeID));
518 fNodeId = nodeID;
519 return;
520 }
Mike Reeda1361362017-03-07 09:37:29 -0500521 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
522 SkPoint transformedPoint;
523 this->ctm().mapXY(rect.x(), rect.y(), &transformedPoint);
Hal Canary5c1b3602017-04-17 16:30:06 -0400524 fNamedDestinations.emplace_back(NamedDestination{sk_ref_sp(value), transformedPoint});
Mike Reeda1361362017-03-07 09:37:29 -0500525 }
526 return;
527 }
528 // Convert to path to handle non-90-degree rotations.
Hal Canaryd791a6f2018-09-28 12:14:08 -0400529 SkPath path = to_path(rect);
Mike Reeda1361362017-03-07 09:37:29 -0500530 path.transform(this->ctm(), &path);
531 SkPath clip;
532 (void)this->cs().asPath(&clip);
533 Op(clip, path, kIntersect_SkPathOp, &path);
534 // PDF wants a rectangle only.
535 SkRect transformedRect = path.getBounds();
536 if (transformedRect.isEmpty()) {
537 return;
538 }
539 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
Hal Canary5c1b3602017-04-17 16:30:06 -0400540 fLinkToURLs.emplace_back(RectWithData{transformedRect, sk_ref_sp(value)});
Mike Reeda1361362017-03-07 09:37:29 -0500541 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
Hal Canary5c1b3602017-04-17 16:30:06 -0400542 fLinkToDestinations.emplace_back(RectWithData{transformedRect, sk_ref_sp(value)});
reedf70b5312016-03-04 16:36:20 -0800543 }
544}
545
Hal Canaryd12a6762017-05-26 17:01:16 -0400546void SkPDFDevice::drawPaint(const SkPaint& srcPaint) {
Hal Canaryabf8e412018-09-24 11:37:23 -0400547 SkMatrix inverse;
548 if (!this->ctm().invert(&inverse)) {
549 return;
550 }
551 SkRect bbox = this->cs().bounds(this->bounds());
552 inverse.mapRect(&bbox);
553 bbox.roundOut(&bbox);
Hal Canaryb4e528d2018-03-09 16:02:15 -0500554 if (this->hasEmptyClip()) {
555 return;
556 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400557 SkPaint newPaint = srcPaint;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000558 newPaint.setStyle(SkPaint::kFill_Style);
Hal Canaryabf8e412018-09-24 11:37:23 -0400559 this->drawRect(bbox, newPaint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000560}
561
Mike Reeda1361362017-03-07 09:37:29 -0500562void SkPDFDevice::drawPoints(SkCanvas::PointMode mode,
halcanarya6814332015-05-27 08:53:36 -0700563 size_t count,
564 const SkPoint* points,
565 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500566 if (this->hasEmptyClip()) {
567 return;
568 }
halcanarya6814332015-05-27 08:53:36 -0700569 SkPaint passedPaint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400570 remove_color_filter(&passedPaint);
halcanarya6814332015-05-27 08:53:36 -0700571 replace_srcmode_on_opaque_paint(&passedPaint);
Hal Canary80fa7ce2017-06-28 16:04:20 -0400572 if (SkCanvas::kPoints_PointMode != mode) {
573 passedPaint.setStyle(SkPaint::kStroke_Style);
574 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000575 if (count == 0) {
576 return;
577 }
578
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000579 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
580 // We only use this when there's a path effect because of the overhead
581 // of multiple calls to setUpContentEntry it causes.
582 if (passedPaint.getPathEffect()) {
Mike Reeda1361362017-03-07 09:37:29 -0500583 draw_points(mode, count, points, passedPaint,
584 this->devClipBounds(), this->ctm(), this);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000585 return;
586 }
587
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000588 const SkPaint* paint = &passedPaint;
589 SkPaint modifiedPaint;
590
591 if (mode == SkCanvas::kPoints_PointMode &&
592 paint->getStrokeCap() != SkPaint::kRound_Cap) {
593 modifiedPaint = *paint;
594 paint = &modifiedPaint;
595 if (paint->getStrokeWidth()) {
596 // PDF won't draw a single point with square/butt caps because the
597 // orientation is ambiguous. Draw a rectangle instead.
598 modifiedPaint.setStyle(SkPaint::kFill_Style);
599 SkScalar strokeWidth = paint->getStrokeWidth();
600 SkScalar halfStroke = SkScalarHalf(strokeWidth);
601 for (size_t i = 0; i < count; i++) {
602 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
603 r.inset(-halfStroke, -halfStroke);
Mike Reeda1361362017-03-07 09:37:29 -0500604 this->drawRect(r, modifiedPaint);
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000605 }
606 return;
607 } else {
608 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
609 }
610 }
611
Mike Reeda1361362017-03-07 09:37:29 -0500612 ScopedContentEntry content(this, *paint);
Hal Canary42137de2018-10-08 16:00:37 -0400613 if (!content) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000614 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000615 }
Hal Canary51329c92017-06-27 14:28:37 -0400616 SkDynamicMemoryWStream* contentStream = content.stream();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000617 switch (mode) {
618 case SkCanvas::kPolygon_PointMode:
Hal Canary51329c92017-06-27 14:28:37 -0400619 SkPDFUtils::MoveTo(points[0].fX, points[0].fY, contentStream);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000620 for (size_t i = 1; i < count; i++) {
Hal Canary51329c92017-06-27 14:28:37 -0400621 SkPDFUtils::AppendLine(points[i].fX, points[i].fY, contentStream);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000622 }
Hal Canary51329c92017-06-27 14:28:37 -0400623 SkPDFUtils::StrokePath(contentStream);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000624 break;
625 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000626 for (size_t i = 0; i < count/2; i++) {
Hal Canary51329c92017-06-27 14:28:37 -0400627 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, contentStream);
628 SkPDFUtils::AppendLine(points[i * 2 + 1].fX, points[i * 2 + 1].fY, contentStream);
629 SkPDFUtils::StrokePath(contentStream);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000630 }
631 break;
632 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000633 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
634 for (size_t i = 0; i < count; i++) {
Hal Canary51329c92017-06-27 14:28:37 -0400635 SkPDFUtils::MoveTo(points[i].fX, points[i].fY, contentStream);
636 SkPDFUtils::ClosePath(contentStream);
637 SkPDFUtils::StrokePath(contentStream);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000638 }
639 break;
640 default:
641 SkASSERT(false);
642 }
643}
644
halcanary8103a342016-03-08 15:10:16 -0800645static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800646 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700647 annotation->insertName("Subtype", "Link");
halcanary488165e2016-04-22 06:10:21 -0700648 annotation->insertInt("F", 4); // required by ISO 19005
Hal Canarye650b852018-09-12 09:12:36 -0400649 // Border: 0 = Horizontal corner radius.
650 // 0 = Vertical corner radius.
651 // 0 = Width, 0 = no border.
652 annotation->insertObject("Border", SkPDFMakeArray(0, 0, 0));
wangxianzhud76665d2015-07-17 17:23:15 -0700653
Hal Canarye650b852018-09-12 09:12:36 -0400654 annotation->insertObject("Rect", SkPDFMakeArray(translatedRect.fLeft,
655 translatedRect.fTop,
656 translatedRect.fRight,
657 translatedRect.fBottom));
halcanary8103a342016-03-08 15:10:16 -0800658 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700659}
660
halcanary8103a342016-03-08 15:10:16 -0800661static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700662 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700663 SkString url(static_cast<const char *>(urlData->data()),
664 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800665 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700666 action->insertName("S", "URI");
667 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800668 annotation->insertObject("A", std::move(action));
669 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700670}
671
halcanary8103a342016-03-08 15:10:16 -0800672static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
673 const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700674 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700675 SkString name(static_cast<const char *>(nameData->data()),
676 nameData->size() - 1);
677 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800678 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700679}
680
Mike Reeda1361362017-03-07 09:37:29 -0500681void SkPDFDevice::drawRect(const SkRect& rect,
halcanarya6814332015-05-27 08:53:36 -0700682 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500683 if (this->hasEmptyClip()) {
684 return;
685 }
halcanarya6814332015-05-27 08:53:36 -0700686 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400687 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700688 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000689 SkRect r = rect;
690 r.sort();
691
Hal Canary8b354dc2018-09-20 12:51:22 -0400692 if (paint.getPathEffect() || paint.getMaskFilter() || this->ctm().hasPerspective()) {
Hal Canaryd791a6f2018-09-28 12:14:08 -0400693 this->drawPath(to_path(r), paint, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000694 return;
695 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000696
Mike Reeda1361362017-03-07 09:37:29 -0500697 ScopedContentEntry content(this, paint);
Hal Canary42137de2018-10-08 16:00:37 -0400698 if (!content) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000699 return;
700 }
Hal Canary51329c92017-06-27 14:28:37 -0400701 SkPDFUtils::AppendRectangle(r, content.stream());
702 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000703}
704
Mike Reeda1361362017-03-07 09:37:29 -0500705void SkPDFDevice::drawRRect(const SkRRect& rrect,
halcanarya6814332015-05-27 08:53:36 -0700706 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500707 if (this->hasEmptyClip()) {
708 return;
709 }
halcanarya6814332015-05-27 08:53:36 -0700710 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400711 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700712 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000713 SkPath path;
714 path.addRRect(rrect);
Robert Phillips137ca522018-08-15 10:14:33 -0400715 this->drawPath(path, paint, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000716}
717
Mike Reeda1361362017-03-07 09:37:29 -0500718void SkPDFDevice::drawOval(const SkRect& oval,
halcanarya6814332015-05-27 08:53:36 -0700719 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500720 if (this->hasEmptyClip()) {
721 return;
722 }
halcanarya6814332015-05-27 08:53:36 -0700723 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400724 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700725 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700726 SkPath path;
727 path.addOval(oval);
Robert Phillips137ca522018-08-15 10:14:33 -0400728 this->drawPath(path, paint, true);
reed89443ab2014-06-27 11:34:19 -0700729}
730
Mike Reeda1361362017-03-07 09:37:29 -0500731void SkPDFDevice::drawPath(const SkPath& origPath,
halcanarya6814332015-05-27 08:53:36 -0700732 const SkPaint& srcPaint,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000733 bool pathIsMutable) {
Robert Phillips137ca522018-08-15 10:14:33 -0400734 this->internalDrawPath(this->cs(), this->ctm(), origPath, srcPaint, pathIsMutable);
Mike Reeda1361362017-03-07 09:37:29 -0500735}
736
Hal Canaryd12a6762017-05-26 17:01:16 -0400737void SkPDFDevice::internalDrawPathWithFilter(const SkClipStack& clipStack,
738 const SkMatrix& ctm,
739 const SkPath& origPath,
Robert Phillips137ca522018-08-15 10:14:33 -0400740 const SkPaint& origPaint) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400741 SkASSERT(origPaint.getMaskFilter());
742 SkPath path(origPath);
743 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
Robert Phillips137ca522018-08-15 10:14:33 -0400744
Hal Canaryd12a6762017-05-26 17:01:16 -0400745 SkStrokeRec::InitStyle initStyle = paint->getFillPath(path, &path)
746 ? SkStrokeRec::kFill_InitStyle
747 : SkStrokeRec::kHairline_InitStyle;
748 path.transform(ctm, &path);
749
Hal Canary22b2d8c2017-07-19 14:46:12 -0400750 SkIRect bounds = clipStack.bounds(this->bounds()).roundOut();
Hal Canaryd12a6762017-05-26 17:01:16 -0400751 SkMask sourceMask;
752 if (!SkDraw::DrawToMask(path, &bounds, paint->getMaskFilter(), &SkMatrix::I(),
753 &sourceMask, SkMask::kComputeBoundsAndRenderImage_CreateMode,
754 initStyle)) {
755 return;
756 }
757 SkAutoMaskFreeImage srcAutoMaskFreeImage(sourceMask.fImage);
758 SkMask dstMask;
759 SkIPoint margin;
Mike Reed80747ef2018-01-23 15:29:32 -0500760 if (!as_MFB(paint->getMaskFilter())->filterMask(&dstMask, sourceMask, ctm, &margin)) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400761 return;
762 }
Hal Canary51329c92017-06-27 14:28:37 -0400763 SkIRect dstMaskBounds = dstMask.fBounds;
764 sk_sp<SkImage> mask = mask_to_greyscale_image(&dstMask);
Hal Canaryd12a6762017-05-26 17:01:16 -0400765 // PDF doesn't seem to allow masking vector graphics with an Image XObject.
766 // Must mask with a Form XObject.
Hal Canary51329c92017-06-27 14:28:37 -0400767 sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice();
Hal Canaryd12a6762017-05-26 17:01:16 -0400768 {
Herb Derbyefe39bc2018-05-01 17:06:20 -0400769 SkCanvas canvas(maskDevice);
Hal Canary51329c92017-06-27 14:28:37 -0400770 canvas.drawImage(mask, dstMaskBounds.x(), dstMaskBounds.y());
Hal Canaryd12a6762017-05-26 17:01:16 -0400771 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400772 if (!ctm.isIdentity() && paint->getShader()) {
773 transform_shader(paint.writable(), ctm); // Since we are using identity matrix.
774 }
Hal Canaryd892a9d2018-09-24 21:20:47 -0400775 ScopedContentEntry content(this, &clipStack, SkMatrix::I(), *paint);
Hal Canary42137de2018-10-08 16:00:37 -0400776 if (!content) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400777 return;
778 }
Hal Canary51329c92017-06-27 14:28:37 -0400779 this->addSMaskGraphicState(std::move(maskDevice), content.stream());
780 SkPDFUtils::AppendRectangle(SkRect::Make(dstMaskBounds), content.stream());
781 SkPDFUtils::PaintPath(SkPaint::kFill_Style, path.getFillType(), content.stream());
782 this->clearMaskOnGraphicState(content.stream());
783}
Hal Canaryd12a6762017-05-26 17:01:16 -0400784
Hal Canary9e41c212018-09-03 12:00:23 -0400785template <typename T,
786 typename U,
787 typename = typename std::enable_if<std::is_convertible<U*, T*>::value>::type>
788static int find_or_add(std::vector<sk_sp<T>>* vec, sk_sp<U> object) {
789 SkASSERT(vec);
790 SkASSERT(object);
791 for (size_t i = 0; i < vec->size(); ++i) {
792 if ((*vec)[i].get() == object.get()) {
793 return SkToInt(i);
794 }
795 }
796 int index = SkToInt(vec->size());
797 vec->push_back(sk_sp<T>(std::move(object)));
798 return index;
799}
800
Hal Canary3b8b11e2018-09-29 22:31:34 -0400801void SkPDFDevice::setGraphicState(sk_sp<SkPDFObject> gs, SkDynamicMemoryWStream* content) {
802 SkPDFUtils::ApplyGraphicState(find_or_add(&fGraphicStateResources, std::move(gs)), content);
803}
804
Hal Canary51329c92017-06-27 14:28:37 -0400805void SkPDFDevice::addSMaskGraphicState(sk_sp<SkPDFDevice> maskDevice,
806 SkDynamicMemoryWStream* contentStream) {
Hal Canary3b8b11e2018-09-29 22:31:34 -0400807 this->setGraphicState(SkPDFGraphicState::GetSMaskGraphicState(
Hal Canaryb4bd5ef2017-07-26 09:16:01 -0400808 maskDevice->makeFormXObjectFromDevice(true), false,
Hal Canary3b8b11e2018-09-29 22:31:34 -0400809 SkPDFGraphicState::kLuminosity_SMaskMode, this->getCanon()), contentStream);
Hal Canary51329c92017-06-27 14:28:37 -0400810}
Hal Canaryd12a6762017-05-26 17:01:16 -0400811
Hal Canary51329c92017-06-27 14:28:37 -0400812void SkPDFDevice::clearMaskOnGraphicState(SkDynamicMemoryWStream* contentStream) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400813 // The no-softmask graphic state is used to "turn off" the mask for later draw calls.
Hal Canaryc02de0b2017-06-28 13:14:03 -0400814 sk_sp<SkPDFDict>& noSMaskGS = this->getCanon()->fNoSmaskGraphicState;
815 if (!noSMaskGS) {
816 noSMaskGS = sk_make_sp<SkPDFDict>("ExtGState");
817 noSMaskGS->insertName("SMask", "None");
818 }
Hal Canary3b8b11e2018-09-29 22:31:34 -0400819 this->setGraphicState(noSMaskGS, contentStream);
Hal Canaryd12a6762017-05-26 17:01:16 -0400820}
821
Mike Reeda1361362017-03-07 09:37:29 -0500822void SkPDFDevice::internalDrawPath(const SkClipStack& clipStack,
823 const SkMatrix& ctm,
824 const SkPath& origPath,
825 const SkPaint& srcPaint,
Mike Reeda1361362017-03-07 09:37:29 -0500826 bool pathIsMutable) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500827 if (clipStack.isEmpty(this->bounds())) {
828 return;
829 }
halcanarya6814332015-05-27 08:53:36 -0700830 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400831 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700832 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800833 SkPath modifiedPath;
834 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000835
Hal Canaryd12a6762017-05-26 17:01:16 -0400836 if (paint.getMaskFilter()) {
Robert Phillips137ca522018-08-15 10:14:33 -0400837 this->internalDrawPathWithFilter(clipStack, ctm, origPath, paint);
Hal Canaryd12a6762017-05-26 17:01:16 -0400838 return;
839 }
840
Mike Reeda1361362017-03-07 09:37:29 -0500841 SkMatrix matrix = ctm;
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000842
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000843 if (paint.getPathEffect()) {
Hal Canary22b2d8c2017-07-19 14:46:12 -0400844 if (clipStack.isEmpty(this->bounds())) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000845 return;
846 }
halcanary682ee012016-01-28 10:59:34 -0800847 if (!pathIsMutable) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400848 modifiedPath = origPath;
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000849 pathPtr = &modifiedPath;
850 pathIsMutable = true;
851 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400852 if (paint.getFillPath(*pathPtr, pathPtr)) {
853 paint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000854 } else {
Hal Canaryd12a6762017-05-26 17:01:16 -0400855 paint.setStyle(SkPaint::kStroke_Style);
856 paint.setStrokeWidth(0);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000857 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400858 paint.setPathEffect(nullptr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000859 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000860
Robert Phillips137ca522018-08-15 10:14:33 -0400861 if (this->handleInversePath(*pathPtr, paint, pathIsMutable)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000862 return;
863 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400864 if (matrix.getType() & SkMatrix::kPerspective_Mask) {
865 if (!pathIsMutable) {
866 modifiedPath = origPath;
867 pathPtr = &modifiedPath;
868 pathIsMutable = true;
869 }
870 pathPtr->transform(matrix);
871 if (paint.getShader()) {
872 transform_shader(&paint, matrix);
873 }
874 matrix = SkMatrix::I();
875 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000876
Hal Canaryd892a9d2018-09-24 21:20:47 -0400877 ScopedContentEntry content(this, &clipStack, matrix, paint);
Hal Canary42137de2018-10-08 16:00:37 -0400878 if (!content) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000879 return;
880 }
Hal Canarydfaa0572017-11-27 09:33:44 -0500881 constexpr SkScalar kToleranceScale = 0.0625f; // smaller = better conics (circles).
Hal Canary385468f2017-02-13 11:03:23 -0500882 SkScalar matrixScale = matrix.mapRadius(1.0f);
Hal Canarydfaa0572017-11-27 09:33:44 -0500883 SkScalar tolerance = matrixScale > 0.0f ? kToleranceScale / matrixScale : kToleranceScale;
halcanary8b2bc252015-10-06 09:41:47 -0700884 bool consumeDegeratePathSegments =
885 paint.getStyle() == SkPaint::kFill_Style ||
886 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
887 paint.getStrokeCap() != SkPaint::kSquare_Cap);
Hal Canary51329c92017-06-27 14:28:37 -0400888 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(), consumeDegeratePathSegments, content.stream(),
Hal Canary385468f2017-02-13 11:03:23 -0500889 tolerance);
Hal Canary51329c92017-06-27 14:28:37 -0400890 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(), content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000891}
892
Hal Canary7cbf5e32017-07-12 13:10:23 -0400893////////////////////////////////////////////////////////////////////////////////
Hal Canaryf50ff392016-09-30 10:25:39 -0400894
Mike Reeda1361362017-03-07 09:37:29 -0500895void SkPDFDevice::drawImageRect(const SkImage* image,
Hal Canaryf50ff392016-09-30 10:25:39 -0400896 const SkRect* src,
897 const SkRect& dst,
Hal Canary7cbf5e32017-07-12 13:10:23 -0400898 const SkPaint& paint,
Hal Canaryf50ff392016-09-30 10:25:39 -0400899 SkCanvas::SrcRectConstraint) {
Hal Canary7cbf5e32017-07-12 13:10:23 -0400900 SkASSERT(image);
901 this->internalDrawImageRect(SkKeyedImage(sk_ref_sp(const_cast<SkImage*>(image))),
902 src, dst, paint, this->ctm());
Hal Canaryf50ff392016-09-30 10:25:39 -0400903}
904
Hal Canary7cbf5e32017-07-12 13:10:23 -0400905void SkPDFDevice::drawBitmapRect(const SkBitmap& bm,
halcanary7a14b312015-10-01 07:28:13 -0700906 const SkRect* src,
907 const SkRect& dst,
Hal Canary7cbf5e32017-07-12 13:10:23 -0400908 const SkPaint& paint,
Mike Reeda1361362017-03-07 09:37:29 -0500909 SkCanvas::SrcRectConstraint) {
Hal Canary7cbf5e32017-07-12 13:10:23 -0400910 SkASSERT(!bm.drawsNothing());
911 this->internalDrawImageRect(SkKeyedImage(bm), src, dst, paint, this->ctm());
halcanary7a14b312015-10-01 07:28:13 -0700912}
913
Hal Canary7cbf5e32017-07-12 13:10:23 -0400914void SkPDFDevice::drawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y, const SkPaint& paint) {
915 SkASSERT(!bm.drawsNothing());
916 auto r = SkRect::MakeXYWH(x, y, bm.width(), bm.height());
917 this->internalDrawImageRect(SkKeyedImage(bm), nullptr, r, paint, this->ctm());
halcanary7a14b312015-10-01 07:28:13 -0700918}
919
Hal Canary7cbf5e32017-07-12 13:10:23 -0400920void SkPDFDevice::drawSprite(const SkBitmap& bm, int x, int y, const SkPaint& paint) {
921 SkASSERT(!bm.drawsNothing());
922 auto r = SkRect::MakeXYWH(x, y, bm.width(), bm.height());
923 this->internalDrawImageRect(SkKeyedImage(bm), nullptr, r, paint, SkMatrix::I());
halcanary7a14b312015-10-01 07:28:13 -0700924}
925
Hal Canary7cbf5e32017-07-12 13:10:23 -0400926void SkPDFDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
927 SkASSERT(image);
928 auto r = SkRect::MakeXYWH(x, y, image->width(), image->height());
929 this->internalDrawImageRect(SkKeyedImage(sk_ref_sp(const_cast<SkImage*>(image))),
930 nullptr, r, paint, this->ctm());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000931}
932
Hal Canary7cbf5e32017-07-12 13:10:23 -0400933////////////////////////////////////////////////////////////////////////////////
934
halcanaryf0c30f52016-07-15 13:35:45 -0700935namespace {
936class GlyphPositioner {
937public:
938 GlyphPositioner(SkDynamicMemoryWStream* content,
939 SkScalar textSkewX,
halcanary4ed2f012016-08-15 18:40:07 -0700940 SkPoint origin)
halcanaryf0c30f52016-07-15 13:35:45 -0700941 : fContent(content)
halcanaryc2f9ec12016-09-12 08:55:29 -0700942 , fCurrentMatrixOrigin(origin)
Hal Canary8ef78ea2018-10-01 13:38:30 -0400943 , fTextSkewX(textSkewX) {
halcanaryf0c30f52016-07-15 13:35:45 -0700944 }
halcanary4871f222016-08-26 13:17:44 -0700945 ~GlyphPositioner() { this->flush(); }
halcanaryf0c30f52016-07-15 13:35:45 -0700946 void flush() {
947 if (fInText) {
948 fContent->writeText("> Tj\n");
949 fInText = false;
950 }
951 }
Hal Canary8ef78ea2018-10-01 13:38:30 -0400952 void setWideChars(bool wide) {
953 this->flush();
954 fWideChars = wide;
955 }
halcanary4871f222016-08-26 13:17:44 -0700956 void writeGlyph(SkPoint xy,
halcanaryf0c30f52016-07-15 13:35:45 -0700957 SkScalar advanceWidth,
958 uint16_t glyph) {
halcanaryc2f9ec12016-09-12 08:55:29 -0700959 if (!fInitialized) {
960 // Flip the text about the x-axis to account for origin swap and include
961 // the passed parameters.
962 fContent->writeText("1 0 ");
963 SkPDFUtils::AppendScalar(-fTextSkewX, fContent);
964 fContent->writeText(" -1 ");
965 SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.x(), fContent);
966 fContent->writeText(" ");
967 SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.y(), fContent);
968 fContent->writeText(" Tm\n");
969 fCurrentMatrixOrigin.set(0.0f, 0.0f);
970 fInitialized = true;
971 }
Hal Canary98caedd2018-07-23 10:50:49 -0400972 SkPoint position = xy - fCurrentMatrixOrigin;
973 if (position != SkPoint{fXAdvance, 0}) {
974 this->flush();
975 SkPDFUtils::AppendScalar(position.x() - position.y() * fTextSkewX, fContent);
976 fContent->writeText(" ");
977 SkPDFUtils::AppendScalar(-position.y(), fContent);
978 fContent->writeText(" Td ");
979 fCurrentMatrixOrigin = xy;
980 fXAdvance = 0;
halcanaryf0c30f52016-07-15 13:35:45 -0700981 }
Hal Canary98caedd2018-07-23 10:50:49 -0400982 fXAdvance += advanceWidth;
halcanaryf0c30f52016-07-15 13:35:45 -0700983 if (!fInText) {
984 fContent->writeText("<");
985 fInText = true;
986 }
987 if (fWideChars) {
988 SkPDFUtils::WriteUInt16BE(fContent, glyph);
989 } else {
990 SkASSERT(0 == glyph >> 8);
991 SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph));
992 }
halcanaryf0c30f52016-07-15 13:35:45 -0700993 }
994
995private:
996 SkDynamicMemoryWStream* fContent;
halcanary4871f222016-08-26 13:17:44 -0700997 SkPoint fCurrentMatrixOrigin;
halcanaryc2f9ec12016-09-12 08:55:29 -0700998 SkScalar fXAdvance = 0.0f;
999 SkScalar fTextSkewX;
Hal Canary8ef78ea2018-10-01 13:38:30 -04001000 bool fWideChars = true;
halcanaryc2f9ec12016-09-12 08:55:29 -07001001 bool fInText = false;
1002 bool fInitialized = false;
halcanaryf0c30f52016-07-15 13:35:45 -07001003};
1004} // namespace
1005
Hal Canary46cc3da2018-05-09 11:50:34 -04001006static SkUnichar map_glyph(const std::vector<SkUnichar>& glyphToUnicode, SkGlyphID glyph) {
1007 return glyph < glyphToUnicode.size() ? glyphToUnicode[SkToInt(glyph)] : -1;
halcanaryf59d18a2016-09-16 14:44:57 -07001008}
1009
Hal Canary575be302018-09-28 15:01:14 -04001010namespace {
1011struct PositionedGlyph {
1012 SkPoint fPos;
1013 SkGlyphID fGlyph;
1014};
Hal Canaryd12a6762017-05-26 17:01:16 -04001015}
1016
Hal Canary699b8732017-06-13 12:13:29 -04001017static SkRect get_glyph_bounds_device_space(SkGlyphID gid, SkGlyphCache* cache,
1018 SkScalar xScale, SkScalar yScale,
1019 SkPoint xy, const SkMatrix& ctm) {
1020 const SkGlyph& glyph = cache->getGlyphIDMetrics(gid);
1021 SkRect glyphBounds = {glyph.fLeft * xScale,
1022 glyph.fTop * yScale,
1023 (glyph.fLeft + glyph.fWidth) * xScale,
1024 (glyph.fTop + glyph.fHeight) * yScale};
1025 glyphBounds.offset(xy);
1026 ctm.mapRect(&glyphBounds); // now in dev space.
1027 return glyphBounds;
1028}
1029
1030static bool contains(const SkRect& r, SkPoint p) {
1031 return r.left() <= p.x() && p.x() <= r.right() &&
1032 r.top() <= p.y() && p.y() <= r.bottom();
1033}
1034
Hal Canary575be302018-09-28 15:01:14 -04001035void SkPDFDevice::drawGlyphRunAsPath(const SkGlyphRun& glyphRun, SkPoint offset) {
Herb Derby4fce0832018-10-02 11:04:29 -04001036 SkPaint paint{glyphRun.paint()};
1037 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1038 paint.setTextAlign(SkPaint::kLeft_Align);
Hal Canary575be302018-09-28 15:01:14 -04001039 SkPath path;
1040 SkASSERT(paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
Herb Derby4fce0832018-10-02 11:04:29 -04001041
Hal Canary575be302018-09-28 15:01:14 -04001042 paint.getPosTextPath(glyphRun.shuntGlyphsIDs().data(),
1043 glyphRun.shuntGlyphsIDs().size() * sizeof(SkGlyphID),
1044 glyphRun.positions().data(),
1045 &path);
1046 path.offset(offset.x(), offset.y());
1047 this->drawPath(path, paint, true);
1048
1049 SkGlyphRun tmp(glyphRun);
1050 {
1051 SkPaint transparent;
1052 transparent.setTypeface(paint.getTypeface() ? paint.refTypeface()
1053 : SkTypeface::MakeDefault());
1054 transparent.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1055 transparent.setColor(SK_ColorTRANSPARENT);
1056 transparent.setTextSize(paint.getTextSize());
1057 transparent.setTextAlign(paint.getTextAlign());
1058 transparent.setTextScaleX(paint.getTextScaleX());
1059 transparent.setTextSkewX(paint.getTextSkewX());
1060 *tmp.mutablePaint() = std::move(transparent);
1061 }
1062 if (this->ctm().hasPerspective()) {
1063 SkMatrix prevCTM = this->ctm();
1064 this->setCTM(SkMatrix::I());
1065 this->internalDrawGlyphRun(tmp, offset);
1066 this->setCTM(prevCTM);
1067 } else {
1068 this->internalDrawGlyphRun(tmp, offset);
1069 }
Hal Canary575be302018-09-28 15:01:14 -04001070}
1071
Hal Canary98caedd2018-07-23 10:50:49 -04001072void SkPDFDevice::internalDrawGlyphRun(const SkGlyphRun& glyphRun, SkPoint offset) {
1073
1074 const SkGlyphID* glyphs = glyphRun.shuntGlyphsIDs().data();
1075 uint32_t glyphCount = SkToU32(glyphRun.shuntGlyphsIDs().size());
Herb Derby4fce0832018-10-02 11:04:29 -04001076 SkPaint srcPaint{glyphRun.paint()};
1077 srcPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1078 srcPaint.setTextAlign(SkPaint::kLeft_Align);
1079
Hal Canary98caedd2018-07-23 10:50:49 -04001080 if (!glyphCount || !glyphs || srcPaint.getTextSize() <= 0 || this->hasEmptyClip()) {
Hal Canaryd12a6762017-05-26 17:01:16 -04001081 return;
1082 }
Hal Canaryd12a6762017-05-26 17:01:16 -04001083 if (srcPaint.getPathEffect()
Hal Canary98caedd2018-07-23 10:50:49 -04001084 || srcPaint.getMaskFilter()
1085 || srcPaint.isVerticalText()
Hal Canary575be302018-09-28 15:01:14 -04001086 || srcPaint.isFakeBoldText()
Hal Canary92c500b2018-09-19 16:19:45 -04001087 || this->ctm().hasPerspective()
Hal Canary98caedd2018-07-23 10:50:49 -04001088 || SkPaint::kFill_Style != srcPaint.getStyle()) {
Hal Canaryd12a6762017-05-26 17:01:16 -04001089 // Stroked Text doesn't work well with Type3 fonts.
Hal Canary575be302018-09-28 15:01:14 -04001090 this->drawGlyphRunAsPath(glyphRun, offset);
halcanarye06ca962016-09-09 05:34:55 -07001091 }
Hal Canary575be302018-09-28 15:01:14 -04001092 SkPaint paint(srcPaint);
Hal Canaryd12a6762017-05-26 17:01:16 -04001093 remove_color_filter(&paint);
halcanary4ed2f012016-08-15 18:40:07 -07001094 replace_srcmode_on_opaque_paint(&paint);
Hal Canary98caedd2018-07-23 10:50:49 -04001095 paint.setHinting(SkPaint::kNo_Hinting);
halcanary4ed2f012016-08-15 18:40:07 -07001096 if (!paint.getTypeface()) {
1097 paint.setTypeface(SkTypeface::MakeDefault());
1098 }
1099 SkTypeface* typeface = paint.getTypeface();
1100 if (!typeface) {
1101 SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n");
1102 return;
1103 }
Hal Canary98caedd2018-07-23 10:50:49 -04001104 const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, fDocument->canon());
halcanary4871f222016-08-26 13:17:44 -07001105 if (!metrics) {
halcanary4ed2f012016-08-15 18:40:07 -07001106 return;
1107 }
Hal Canary46cc3da2018-05-09 11:50:34 -04001108 const std::vector<SkUnichar>& glyphToUnicode = SkPDFFont::GetUnicodeMap(
1109 typeface, fDocument->canon());
1110
Hal Canary98caedd2018-07-23 10:50:49 -04001111 SkClusterator clusterator(glyphRun);
Hal Canaryaa3af7b2017-03-06 16:18:49 -05001112
1113 int emSize;
Herb Derby1a605cd2018-03-22 11:16:25 -04001114 auto glyphCache = SkPDFFont::MakeVectorCache(typeface, &emSize);
Hal Canaryaa3af7b2017-03-06 16:18:49 -05001115
1116 SkScalar textSize = paint.getTextSize();
1117 SkScalar advanceScale = textSize * paint.getTextScaleX() / emSize;
halcanary4ed2f012016-08-15 18:40:07 -07001118
Hal Canary699b8732017-06-13 12:13:29 -04001119 // textScaleX and textScaleY are used to get a conservative bounding box for glyphs.
1120 SkScalar textScaleY = textSize / emSize;
1121 SkScalar textScaleX = advanceScale + paint.getTextSkewX() * textScaleY;
1122
Hal Canary98caedd2018-07-23 10:50:49 -04001123 SkASSERT(paint.getTextAlign() == SkPaint::kLeft_Align);
Hal Canary22b2d8c2017-07-19 14:46:12 -04001124 SkRect clipStackBounds = this->cs().bounds(this->bounds());
Hal Canary9b9510a2017-07-18 09:39:00 -04001125 {
1126 ScopedContentEntry content(this, paint, true);
Hal Canary42137de2018-10-08 16:00:37 -04001127 if (!content) {
Hal Canary9b9510a2017-07-18 09:39:00 -04001128 return;
robertphillips5ba165e2016-08-15 15:36:58 -07001129 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001130 SkDynamicMemoryWStream* out = content.stream();
Hal Canary9b9510a2017-07-18 09:39:00 -04001131
1132 out->writeText("BT\n");
Dominic Mazzoni656cefe2018-09-25 20:29:15 -07001133
1134 int markId = -1;
1135 if (fNodeId) {
1136 markId = fDocument->getMarkIdForNodeId(fNodeId);
1137 }
1138
1139 if (markId != -1) {
1140 out->writeText("/P <</MCID ");
1141 out->writeDecAsText(markId);
1142 out->writeText(" >>BDC\n");
1143 }
1144 SK_AT_SCOPE_EXIT(if (markId != -1) out->writeText("EMC\n"));
1145
Hal Canary9b9510a2017-07-18 09:39:00 -04001146 SK_AT_SCOPE_EXIT(out->writeText("ET\n"));
1147
1148 const SkGlyphID maxGlyphID = SkToU16(typeface->countGlyphs() - 1);
1149
Hal Canary9b9510a2017-07-18 09:39:00 -04001150 if (clusterator.reversedChars()) {
1151 out->writeText("/ReversedChars BMC\n");
1152 }
1153 SK_AT_SCOPE_EXIT(if (clusterator.reversedChars()) { out->writeText("EMC\n"); } );
Hal Canary8ef78ea2018-10-01 13:38:30 -04001154 GlyphPositioner glyphPositioner(out, paint.getTextSkewX(), offset);
Hal Canary9b9510a2017-07-18 09:39:00 -04001155 SkPDFFont* font = nullptr;
1156
Hal Canary1521c8a2018-03-28 09:51:00 -07001157 while (SkClusterator::Cluster c = clusterator.next()) {
Hal Canary9b9510a2017-07-18 09:39:00 -04001158 int index = c.fGlyphIndex;
1159 int glyphLimit = index + c.fGlyphCount;
1160
1161 bool actualText = false;
1162 SK_AT_SCOPE_EXIT(if (actualText) {
1163 glyphPositioner.flush();
1164 out->writeText("EMC\n");
1165 });
1166 if (c.fUtf8Text) { // real cluster
1167 // Check if `/ActualText` needed.
1168 const char* textPtr = c.fUtf8Text;
1169 const char* textEnd = c.fUtf8Text + c.fTextByteLength;
Hal Canaryf107a2f2018-07-25 16:52:48 -04001170 SkUnichar unichar = SkUTF::NextUTF8(&textPtr, textEnd);
Hal Canary9b9510a2017-07-18 09:39:00 -04001171 if (unichar < 0) {
halcanaryf59d18a2016-09-16 14:44:57 -07001172 return;
1173 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001174 if (textPtr < textEnd || // more characters left
1175 glyphLimit > index + 1 || // toUnicode wouldn't work
1176 unichar != map_glyph(glyphToUnicode, glyphs[index])) // test single Unichar map
1177 {
1178 glyphPositioner.flush();
1179 out->writeText("/Span<</ActualText <");
1180 SkPDFUtils::WriteUTF16beHex(out, 0xFEFF); // U+FEFF = BYTE ORDER MARK
1181 // the BOM marks this text as UTF-16BE, not PDFDocEncoding.
1182 SkPDFUtils::WriteUTF16beHex(out, unichar); // first char
1183 while (textPtr < textEnd) {
Hal Canaryf107a2f2018-07-25 16:52:48 -04001184 unichar = SkUTF::NextUTF8(&textPtr, textEnd);
Hal Canary9b9510a2017-07-18 09:39:00 -04001185 if (unichar < 0) {
1186 break;
1187 }
1188 SkPDFUtils::WriteUTF16beHex(out, unichar);
1189 }
1190 out->writeText("> >> BDC\n"); // begin marked-content sequence
1191 // with an associated property list.
1192 actualText = true;
halcanaryf59d18a2016-09-16 14:44:57 -07001193 }
halcanaryf59d18a2016-09-16 14:44:57 -07001194 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001195 for (; index < glyphLimit; ++index) {
1196 SkGlyphID gid = glyphs[index];
1197 if (gid > maxGlyphID) {
1198 continue;
halcanaryf59d18a2016-09-16 14:44:57 -07001199 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001200 if (!font || !font->hasGlyph(gid)) {
1201 // Not yet specified font or need to switch font.
Hal Canary9e41c212018-09-03 12:00:23 -04001202 sk_sp<SkPDFFont> newFont =
Hal Canary8ef78ea2018-10-01 13:38:30 -04001203 SkPDFFont::GetFontResource(
1204 fDocument->canon(), glyphCache.get(), typeface, gid);
Hal Canary9e41c212018-09-03 12:00:23 -04001205 SkASSERT(newFont); // All preconditions for SkPDFFont::GetFontResource are met.
1206 if (!newFont) {
Hal Canary9b9510a2017-07-18 09:39:00 -04001207 return;
1208 }
Hal Canary9e41c212018-09-03 12:00:23 -04001209 font = newFont.get();
1210 fDocument->registerFont(font);
1211 int fontIndex = find_or_add(&fFontResources, std::move(newFont));
1212
Hal Canary9b9510a2017-07-18 09:39:00 -04001213 glyphPositioner.flush();
Hal Canary8ef78ea2018-10-01 13:38:30 -04001214 glyphPositioner.setWideChars(font->multiByteGlyphs());
Hal Canarye650b852018-09-12 09:12:36 -04001215 SkPDFWriteResourceName(out, SkPDFResourceType::kFont, fontIndex);
1216 out->writeText(" ");
1217 SkPDFUtils::AppendScalar(textSize, out);
1218 out->writeText(" Tf\n");
1219
Hal Canary9b9510a2017-07-18 09:39:00 -04001220 }
Hal Canary98caedd2018-07-23 10:50:49 -04001221 SkPoint xy = glyphRun.positions()[index];
1222 // Do a glyph-by-glyph bounds-reject if positions are absolute.
1223 SkRect glyphBounds = get_glyph_bounds_device_space(
1224 gid, glyphCache.get(), textScaleX, textScaleY,
1225 xy + offset, this->ctm());
1226 if (glyphBounds.isEmpty()) {
1227 if (!contains(clipStackBounds, {glyphBounds.x(), glyphBounds.y()})) {
1228 continue;
Hal Canary699b8732017-06-13 12:13:29 -04001229 }
1230 } else {
Hal Canary98caedd2018-07-23 10:50:49 -04001231 if (!clipStackBounds.intersects(glyphBounds)) {
1232 continue; // reject glyphs as out of bounds
Hal Canary699b8732017-06-13 12:13:29 -04001233 }
1234 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001235 font->noteGlyphUsage(gid);
1236
Hal Canary8ef78ea2018-10-01 13:38:30 -04001237 SkGlyphID encodedGlyph = font->multiByteGlyphs()
1238 ? gid : font->glyphToPDFFontEncoding(gid);
Hal Canary98caedd2018-07-23 10:50:49 -04001239 SkScalar advance = advanceScale * glyphCache->getGlyphIDAdvance(gid).fAdvanceX;
Hal Canary9b9510a2017-07-18 09:39:00 -04001240 glyphPositioner.writeGlyph(xy, advance, encodedGlyph);
halcanaryf59d18a2016-09-16 14:44:57 -07001241 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001242 }
1243 }
halcanary4ed2f012016-08-15 18:40:07 -07001244}
1245
Herb Derbyb935cf82018-07-26 16:54:18 -04001246void SkPDFDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
1247 for (const SkGlyphRun& glyphRun : glyphRunList) {
1248 this->internalDrawGlyphRun(glyphRun, glyphRunList.origin());
halcanarye06ca962016-09-09 05:34:55 -07001249 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001250}
1251
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001252void SkPDFDevice::drawVertices(const SkVertices*, const SkVertices::Bone[], int, SkBlendMode,
Ruiqi Maof5101492018-06-29 14:32:21 -04001253 const SkPaint&) {
Hal Canaryb4e528d2018-03-09 16:02:15 -05001254 if (this->hasEmptyClip()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001255 return;
1256 }
reed@google.com85e143c2013-12-30 15:51:25 +00001257 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001258}
1259
Hal Canary3b8b11e2018-09-29 22:31:34 -04001260void SkPDFDevice::drawFormXObject(sk_sp<SkPDFObject> xObject, SkDynamicMemoryWStream* content) {
1261 SkPDFWriteResourceName(content, SkPDFResourceType::kXObject,
1262 find_or_add(&fXObjectResources, std::move(xObject)));
Hal Canarye650b852018-09-12 09:12:36 -04001263 content->writeText(" Do\n");
1264}
1265
Mike Reeda1361362017-03-07 09:37:29 -05001266void SkPDFDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001267 SkASSERT(!paint.getImageFilter());
1268
reed7503d602016-07-15 14:23:29 -07001269 // Check if the source device is really a bitmapdevice (because that's what we returned
1270 // from createDevice (likely due to an imagefilter)
1271 SkPixmap pmap;
1272 if (device->peekPixels(&pmap)) {
1273 SkBitmap bitmap;
1274 bitmap.installPixels(pmap);
Mike Reeda1361362017-03-07 09:37:29 -05001275 this->drawSprite(bitmap, x, y, paint);
reed7503d602016-07-15 14:23:29 -07001276 return;
1277 }
1278
fmalita6987dca2014-11-13 08:33:37 -08001279 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001280 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001281
1282 SkScalar scalarX = SkIntToScalar(x);
1283 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001284 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1285 SkRect r = l.rect.makeOffset(scalarX, scalarY);
Hal Canary5c1b3602017-04-17 16:30:06 -04001286 fLinkToURLs.emplace_back(RectWithData{r, l.data});
wangxianzhuef6c50a2015-09-17 20:38:02 -07001287 }
halcanary91fcb3e2016-03-04 13:53:22 -08001288 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1289 SkRect r = l.rect.makeOffset(scalarX, scalarY);
Hal Canary5c1b3602017-04-17 16:30:06 -04001290 fLinkToDestinations.emplace_back(RectWithData{r, l.data});
wangxianzhuef6c50a2015-09-17 20:38:02 -07001291 }
halcanary91fcb3e2016-03-04 13:53:22 -08001292 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1293 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
Hal Canary5c1b3602017-04-17 16:30:06 -04001294 fNamedDestinations.emplace_back(NamedDestination{d.nameData, p});
wangxianzhuef6c50a2015-09-17 20:38:02 -07001295 }
1296
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001297 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001298 return;
1299 }
1300
Mike Reeda1361362017-03-07 09:37:29 -05001301 SkMatrix matrix = SkMatrix::MakeTrans(SkIntToScalar(x), SkIntToScalar(y));
Hal Canaryd892a9d2018-09-24 21:20:47 -04001302 ScopedContentEntry content(this, &this->cs(), matrix, paint);
Hal Canary42137de2018-10-08 16:00:37 -04001303 if (!content) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001304 return;
1305 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001306 if (content.needShape()) {
Hal Canaryd791a6f2018-09-28 12:14:08 -04001307 SkISize dim = device->imageInfo().dimensions();
1308 content.setShape(to_path(SkRect::Make(SkIRect::MakeXYWH(x, y, dim.width(), dim.height()))));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001309 }
1310 if (!content.needSource()) {
1311 return;
1312 }
Hal Canary3b8b11e2018-09-29 22:31:34 -04001313 this->drawFormXObject(pdfDevice->makeFormXObjectFromDevice(), content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001314}
1315
reede8f30622016-03-23 18:59:25 -07001316sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1317 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001318}
1319
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001320
Hal Canary9e41c212018-09-03 12:00:23 -04001321sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() {
Hal Canarye650b852018-09-12 09:12:36 -04001322 return SkPDFMakeResourceDict(std::move(fGraphicStateResources),
Hal Canary9e41c212018-09-03 12:00:23 -04001323 std::move(fShaderResources),
1324 std::move(fXObjectResources),
1325 std::move(fFontResources));
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001326}
1327
Hal Canaryb400d4d2018-09-26 16:33:52 -04001328std::unique_ptr<SkStreamAsset> SkPDFDevice::content() {
1329 if (fActiveStackState.fContentStream) {
1330 fActiveStackState.drainStack();
1331 fActiveStackState = GraphicStackState();
1332 }
Hal Canary42137de2018-10-08 16:00:37 -04001333 if (fContent.bytesWritten() == 0) {
1334 return skstd::make_unique<SkMemoryStream>();
1335 }
halcanary334fcbc2015-02-24 12:56:16 -08001336 SkDynamicMemoryWStream buffer;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001337 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
Hal Canarye650b852018-09-12 09:12:36 -04001338 append_transform(fInitialTransform, &buffer);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001339 }
Hal Canary42137de2018-10-08 16:00:37 -04001340 if (fNeedsExtraSave) {
1341 buffer.writeText("q\n");
halcanary2be7e012016-03-28 11:58:08 -07001342 }
Hal Canary42137de2018-10-08 16:00:37 -04001343 fContent.writeToAndReset(&buffer);
1344 if (fNeedsExtraSave) {
1345 buffer.writeText("Q\n");
halcanary022c2bd2016-09-02 11:29:46 -07001346 }
Hal Canary42137de2018-10-08 16:00:37 -04001347 fNeedsExtraSave = false;
1348 return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001349}
1350
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001351/* Draws an inverse filled path by using Path Ops to compute the positive
1352 * inverse using the current clip as the inverse bounds.
1353 * Return true if this was an inverse path and was properly handled,
1354 * otherwise returns false and the normal drawing routine should continue,
1355 * either as a (incorrect) fallback or because the path was not inverse
1356 * in the first place.
1357 */
Mike Reeda1361362017-03-07 09:37:29 -05001358bool SkPDFDevice::handleInversePath(const SkPath& origPath,
Robert Phillips137ca522018-08-15 10:14:33 -04001359 const SkPaint& paint,
1360 bool pathIsMutable) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001361 if (!origPath.isInverseFillType()) {
1362 return false;
1363 }
1364
Hal Canaryb4e528d2018-03-09 16:02:15 -05001365 if (this->hasEmptyClip()) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001366 return false;
1367 }
1368
1369 SkPath modifiedPath;
1370 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1371 SkPaint noInversePaint(paint);
1372
1373 // Merge stroking operations into final path.
1374 if (SkPaint::kStroke_Style == paint.getStyle() ||
1375 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1376 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1377 if (doFillPath) {
1378 noInversePaint.setStyle(SkPaint::kFill_Style);
1379 noInversePaint.setStrokeWidth(0);
1380 pathPtr = &modifiedPath;
1381 } else {
1382 // To be consistent with the raster output, hairline strokes
1383 // are rendered as non-inverted.
1384 modifiedPath.toggleInverseFillType();
Robert Phillips137ca522018-08-15 10:14:33 -04001385 this->drawPath(modifiedPath, paint, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001386 return true;
1387 }
1388 }
1389
1390 // Get bounds of clip in current transform space
1391 // (clip bounds are given in device space).
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001392 SkMatrix transformInverse;
Mike Reeda1361362017-03-07 09:37:29 -05001393 SkMatrix totalMatrix = this->ctm();
Robert Phillips137ca522018-08-15 10:14:33 -04001394
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001395 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001396 return false;
1397 }
Hal Canary22b2d8c2017-07-19 14:46:12 -04001398 SkRect bounds = this->cs().bounds(this->bounds());
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001399 transformInverse.mapRect(&bounds);
1400
1401 // Extend the bounds by the line width (plus some padding)
1402 // so the edge doesn't cause a visible stroke.
1403 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1404 paint.getStrokeWidth() + SK_Scalar1);
1405
1406 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1407 return false;
1408 }
1409
Robert Phillips137ca522018-08-15 10:14:33 -04001410 this->drawPath(modifiedPath, noInversePaint, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001411 return true;
1412}
1413
Hal Canary9e41c212018-09-03 12:00:23 -04001414sk_sp<SkPDFArray> SkPDFDevice::getAnnotations() {
1415 sk_sp<SkPDFArray> array;
1416 size_t count = fLinkToURLs.size() + fLinkToDestinations.size();
1417 if (0 == count) {
1418 return array;
1419 }
1420 array = sk_make_sp<SkPDFArray>();
1421 array->reserve(count);
halcanary91fcb3e2016-03-04 13:53:22 -08001422 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001423 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001424 fInitialTransform.mapRect(&r, rectWithURL.rect);
Hal Canary7675b362018-06-15 15:32:14 -04001425 array->appendObjRef(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001426 }
halcanary91fcb3e2016-03-04 13:53:22 -08001427 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001428 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001429 fInitialTransform.mapRect(&r, linkToDestination.rect);
Hal Canary7675b362018-06-15 15:32:14 -04001430 array->appendObjRef(
halcanaryd7b28852016-03-07 12:39:14 -08001431 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001432 }
Hal Canary9e41c212018-09-03 12:00:23 -04001433 return array;
wangxianzhuef6c50a2015-09-17 20:38:02 -07001434}
epoger@google.comb58772f2013-03-08 09:09:10 +00001435
halcanary6d622702015-03-25 08:45:42 -07001436void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001437 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001438 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001439 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001440 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001441 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001442 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001443 pdfDest->appendScalar(p.x());
1444 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001445 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001446 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001447 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001448 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001449}
1450
Hal Canaryb4bd5ef2017-07-26 09:16:01 -04001451sk_sp<SkPDFObject> SkPDFDevice::makeFormXObjectFromDevice(bool alpha) {
halcanary5abbb442016-07-29 08:41:33 -07001452 SkMatrix inverseTransform = SkMatrix::I();
halcanaryafdc1772016-08-23 09:02:12 -07001453 if (!fInitialTransform.isIdentity()) {
1454 if (!fInitialTransform.invert(&inverseTransform)) {
halcanary5abbb442016-07-29 08:41:33 -07001455 SkDEBUGFAIL("Layer initial transform should be invertible.");
1456 inverseTransform.reset();
1457 }
1458 }
Hal Canaryb4bd5ef2017-07-26 09:16:01 -04001459 const char* colorSpace = alpha ? "DeviceGray" : nullptr;
Hal Canarye650b852018-09-12 09:12:36 -04001460
halcanary4b1e17e2016-07-27 14:49:46 -07001461 sk_sp<SkPDFObject> xobject =
Hal Canarye650b852018-09-12 09:12:36 -04001462 SkPDFMakeFormXObject(this->content(),
1463 SkPDFMakeArray(0, 0, this->width(), this->height()),
Hal Canaryb4bd5ef2017-07-26 09:16:01 -04001464 this->makeResourceDict(), inverseTransform, colorSpace);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001465 // We always draw the form xobjects that we create back into the device, so
1466 // we simply preserve the font usage instead of pulling it out and merging
1467 // it back in later.
Hal Canary9e41c212018-09-03 12:00:23 -04001468 this->reset();
reed@google.comfc641d02012-09-20 17:52:20 +00001469 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001470}
1471
Hal Canaryc4c41c92018-09-28 13:30:45 -04001472void SkPDFDevice::drawFormXObjectWithMask(sk_sp<SkPDFObject> xObject,
halcanarydabd4f02016-08-03 11:16:56 -07001473 sk_sp<SkPDFObject> mask,
reed374772b2016-10-05 17:33:02 -07001474 SkBlendMode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001475 bool invertClip) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001476 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001477 paint.setBlendMode(mode);
Hal Canaryd892a9d2018-09-24 21:20:47 -04001478 ScopedContentEntry content(this, nullptr, SkMatrix::I(), paint);
Hal Canary42137de2018-10-08 16:00:37 -04001479 if (!content) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001480 return;
1481 }
Hal Canary3b8b11e2018-09-29 22:31:34 -04001482 this->setGraphicState(SkPDFGraphicState::GetSMaskGraphicState(
1483 std::move(mask), invertClip, SkPDFGraphicState::kAlpha_SMaskMode,
1484 fDocument->canon()), content.stream());
1485 this->drawFormXObject(std::move(xObject), content.stream());
Hal Canaryc02de0b2017-06-28 13:14:03 -04001486 this->clearMaskOnGraphicState(content.stream());
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001487}
1488
Hal Canaryb400d4d2018-09-26 16:33:52 -04001489
1490static bool treat_as_regular_pdf_blend_mode(SkBlendMode blendMode) {
1491 return nullptr != SkPDFUtils::BlendModeName(blendMode);
1492}
1493
1494SkDynamicMemoryWStream* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
1495 const SkMatrix& matrix,
1496 const SkPaint& paint,
1497 bool hasText,
1498 sk_sp<SkPDFObject>* dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001499 *dst = nullptr;
reed374772b2016-10-05 17:33:02 -07001500 SkBlendMode blendMode = paint.getBlendMode();
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001501
Hal Canaryb400d4d2018-09-26 16:33:52 -04001502 // Dst xfer mode doesn't draw source at all.
1503 if (blendMode == SkBlendMode::kDst) {
1504 return nullptr;
1505 }
1506
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001507 // For the following modes, we want to handle source and destination
1508 // separately, so make an object of what's already there.
Hal Canaryb400d4d2018-09-26 16:33:52 -04001509 if (!treat_as_regular_pdf_blend_mode(blendMode) && blendMode != SkBlendMode::kDstOver) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001510 if (!isContentEmpty()) {
halcanarydabd4f02016-08-03 11:16:56 -07001511 *dst = this->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001512 SkASSERT(isContentEmpty());
reed374772b2016-10-05 17:33:02 -07001513 } else if (blendMode != SkBlendMode::kSrc &&
1514 blendMode != SkBlendMode::kSrcOut) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001515 // Except for Src and SrcOut, if there isn't anything already there,
1516 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001517 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001518 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001519 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001520 // TODO(vandebo): Figure out how/if we can handle the following modes:
Hal Canaryb400d4d2018-09-26 16:33:52 -04001521 // Xor, Plus. For now, we treat them as SrcOver/Normal.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001522
Hal Canaryb400d4d2018-09-26 16:33:52 -04001523 if (treat_as_regular_pdf_blend_mode(blendMode)) {
1524 if (!fActiveStackState.fContentStream) {
Hal Canary42137de2018-10-08 16:00:37 -04001525 if (fContent.bytesWritten() != 0) {
1526 fContent.writeText("Q\nq\n");
1527 fNeedsExtraSave = true;
1528 }
1529 fActiveStackState = GraphicStackState(&fContent);
1530 } else {
1531 SkASSERT(fActiveStackState.fContentStream = &fContent);
Hal Canaryb400d4d2018-09-26 16:33:52 -04001532 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001533 } else {
Hal Canaryb400d4d2018-09-26 16:33:52 -04001534 fActiveStackState.drainStack();
Hal Canary42137de2018-10-08 16:00:37 -04001535 fActiveStackState = GraphicStackState(&fContentBuffer);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001536 }
Hal Canaryb400d4d2018-09-26 16:33:52 -04001537 SkASSERT(fActiveStackState.fContentStream);
1538 GraphicStateEntry entry;
1539 this->populateGraphicStateEntryFromPaint(matrix, clipStack, paint, hasText, &entry);
1540 fActiveStackState.updateClip(clipStack, this->bounds());
1541 fActiveStackState.updateMatrix(entry.fMatrix);
1542 fActiveStackState.updateDrawingState(entry);
1543
1544 return fActiveStackState.fContentStream;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001545}
1546
Hal Canaryb400d4d2018-09-26 16:33:52 -04001547void SkPDFDevice::finishContentEntry(const SkClipStack* clipStack,
1548 SkBlendMode blendMode,
halcanarydabd4f02016-08-03 11:16:56 -07001549 sk_sp<SkPDFObject> dst,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001550 SkPath* shape) {
Hal Canaryb400d4d2018-09-26 16:33:52 -04001551 SkASSERT(blendMode != SkBlendMode::kDst);
1552 if (treat_as_regular_pdf_blend_mode(blendMode)) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001553 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001554 return;
1555 }
Hal Canaryb400d4d2018-09-26 16:33:52 -04001556
1557 SkASSERT(fActiveStackState.fContentStream);
1558
1559 fActiveStackState.drainStack();
1560 fActiveStackState = GraphicStackState();
1561
reed374772b2016-10-05 17:33:02 -07001562 if (blendMode == SkBlendMode::kDstOver) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001563 SkASSERT(!dst);
Hal Canary42137de2018-10-08 16:00:37 -04001564 if (fContentBuffer.bytesWritten() != 0) {
1565 if (fContent.bytesWritten() != 0) {
1566 fContentBuffer.writeText("Q\nq\n");
1567 fNeedsExtraSave = true;
1568 }
1569 fContentBuffer.prependToAndReset(&fContent);
1570 SkASSERT(fContentBuffer.bytesWritten() == 0);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001571 }
1572 return;
1573 }
Hal Canary42137de2018-10-08 16:00:37 -04001574 if (fContentBuffer.bytesWritten() != 0) {
1575 if (fContent.bytesWritten() != 0) {
1576 fContent.writeText("Q\nq\n");
1577 fNeedsExtraSave = true;
1578 }
1579 fContentBuffer.writeToAndReset(&fContent);
1580 SkASSERT(fContentBuffer.bytesWritten() == 0);
1581 }
1582
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001583 if (!dst) {
reed374772b2016-10-05 17:33:02 -07001584 SkASSERT(blendMode == SkBlendMode::kSrc ||
1585 blendMode == SkBlendMode::kSrcOut);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001586 return;
1587 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001588
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001589 SkASSERT(dst);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001590 // Changing the current content into a form-xobject will destroy the clip
1591 // objects which is fine since the xobject will already be clipped. However
1592 // if source has shape, we need to clip it too, so a copy of the clip is
1593 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001594
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001595 SkPaint stockPaint;
1596
halcanary4b1e17e2016-07-27 14:49:46 -07001597 sk_sp<SkPDFObject> srcFormXObject;
Hal Canary9e41c212018-09-03 12:00:23 -04001598 if (this->isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001599 // If nothing was drawn and there's no shape, then the draw was a
1600 // no-op, but dst needs to be restored for that to be true.
1601 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1602 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1603 // reduces to Dst.
reed374772b2016-10-05 17:33:02 -07001604 if (shape == nullptr || blendMode == SkBlendMode::kDstOut ||
1605 blendMode == SkBlendMode::kSrcATop) {
Hal Canaryd892a9d2018-09-24 21:20:47 -04001606 ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint);
Hal Canary3b8b11e2018-09-29 22:31:34 -04001607 this->drawFormXObject(std::move(dst), content.stream());
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001608 return;
1609 } else {
reed374772b2016-10-05 17:33:02 -07001610 blendMode = SkBlendMode::kClear;
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001611 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001612 } else {
halcanary4b1e17e2016-07-27 14:49:46 -07001613 srcFormXObject = this->makeFormXObjectFromDevice();
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001614 }
1615
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001616 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1617 // without alpha.
reed374772b2016-10-05 17:33:02 -07001618 if (blendMode == SkBlendMode::kSrcATop) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001619 // TODO(vandebo): In order to properly support SrcATop we have to track
1620 // the shape of what's been drawn at all times. It's the intersection of
1621 // the non-transparent parts of the device and the outlines (shape) of
1622 // all images and devices drawn.
Hal Canaryc4c41c92018-09-28 13:30:45 -04001623 this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001624 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001625 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001626 // Draw shape into a form-xobject.
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001627 SkPaint filledPaint;
1628 filledPaint.setColor(SK_ColorBLACK);
1629 filledPaint.setStyle(SkPaint::kFill_Style);
Hal Canaryb400d4d2018-09-26 16:33:52 -04001630 SkClipStack empty;
Hal Canary813b5ac2018-09-28 12:30:37 -04001631 SkPDFDevice shapeDev(this->size(), fDocument, fInitialTransform);
1632 shapeDev.internalDrawPath(clipStack ? *clipStack : empty,
1633 SkMatrix::I(), *shape, filledPaint, true);
Hal Canaryc4c41c92018-09-28 13:30:45 -04001634 this->drawFormXObjectWithMask(dst, shapeDev.makeFormXObjectFromDevice(),
Mike Reeda1361362017-03-07 09:37:29 -05001635 SkBlendMode::kSrcOver, true);
halcanarydabd4f02016-08-03 11:16:56 -07001636 } else {
Hal Canaryc4c41c92018-09-28 13:30:45 -04001637 this->drawFormXObjectWithMask(dst, srcFormXObject, SkBlendMode::kSrcOver, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001638 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001639 }
1640
reed374772b2016-10-05 17:33:02 -07001641 if (blendMode == SkBlendMode::kClear) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001642 return;
reed374772b2016-10-05 17:33:02 -07001643 } else if (blendMode == SkBlendMode::kSrc ||
1644 blendMode == SkBlendMode::kDstATop) {
Hal Canaryd892a9d2018-09-24 21:20:47 -04001645 ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint);
Hal Canary42137de2018-10-08 16:00:37 -04001646 if (content) {
Hal Canary3b8b11e2018-09-29 22:31:34 -04001647 this->drawFormXObject(srcFormXObject, content.stream());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001648 }
reed374772b2016-10-05 17:33:02 -07001649 if (blendMode == SkBlendMode::kSrc) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001650 return;
1651 }
reed374772b2016-10-05 17:33:02 -07001652 } else if (blendMode == SkBlendMode::kSrcATop) {
Hal Canaryd892a9d2018-09-24 21:20:47 -04001653 ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint);
Hal Canary42137de2018-10-08 16:00:37 -04001654 if (content) {
Hal Canary3b8b11e2018-09-29 22:31:34 -04001655 this->drawFormXObject(dst, content.stream());
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001656 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001657 }
1658
reed374772b2016-10-05 17:33:02 -07001659 SkASSERT(blendMode == SkBlendMode::kSrcIn ||
1660 blendMode == SkBlendMode::kDstIn ||
1661 blendMode == SkBlendMode::kSrcOut ||
1662 blendMode == SkBlendMode::kDstOut ||
1663 blendMode == SkBlendMode::kSrcATop ||
1664 blendMode == SkBlendMode::kDstATop ||
1665 blendMode == SkBlendMode::kModulate);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001666
reed374772b2016-10-05 17:33:02 -07001667 if (blendMode == SkBlendMode::kSrcIn ||
1668 blendMode == SkBlendMode::kSrcOut ||
1669 blendMode == SkBlendMode::kSrcATop) {
Hal Canaryc4c41c92018-09-28 13:30:45 -04001670 this->drawFormXObjectWithMask(std::move(srcFormXObject), std::move(dst),
1671 SkBlendMode::kSrcOver, blendMode == SkBlendMode::kSrcOut);
halcanarydabd4f02016-08-03 11:16:56 -07001672 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001673 } else {
reed374772b2016-10-05 17:33:02 -07001674 SkBlendMode mode = SkBlendMode::kSrcOver;
reed374772b2016-10-05 17:33:02 -07001675 if (blendMode == SkBlendMode::kModulate) {
Hal Canaryc4c41c92018-09-28 13:30:45 -04001676 this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver, false);
reed374772b2016-10-05 17:33:02 -07001677 mode = SkBlendMode::kMultiply;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001678 }
Hal Canaryc4c41c92018-09-28 13:30:45 -04001679 this->drawFormXObjectWithMask(std::move(dst), std::move(srcFormXObject), mode,
1680 blendMode == SkBlendMode::kDstOut);
halcanarydabd4f02016-08-03 11:16:56 -07001681 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001682 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001683}
1684
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001685bool SkPDFDevice::isContentEmpty() {
Hal Canary42137de2018-10-08 16:00:37 -04001686 return fContent.bytesWritten() == 0 && fContentBuffer.bytesWritten() == 0;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001687}
1688
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001689void SkPDFDevice::populateGraphicStateEntryFromPaint(
1690 const SkMatrix& matrix,
Hal Canaryd892a9d2018-09-24 21:20:47 -04001691 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001692 const SkPaint& paint,
1693 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001694 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001695 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1696 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1697 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001698
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001699 entry->fMatrix = matrix;
Hal Canaryb400d4d2018-09-26 16:33:52 -04001700 entry->fClipStackGenID = clipStack ? clipStack->getTopmostGenID()
1701 : SkClipStack::kWideOpenGenID;
Hal Canary04ac4612018-10-10 13:09:43 -04001702 SkColor4f color = paint.getColor4f();
1703 entry->fColor = {color.fR, color.fG, color.fB, 1};
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001704 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001705
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001706 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001707 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001708 SkShader* shader = paint.getShader();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001709 if (shader) {
Hal Canaryc8f91802017-02-12 20:29:12 -05001710 if (SkShader::kColor_GradientType == shader->asAGradient(nullptr)) {
1711 // We don't have to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001712 SkShader::GradientInfo gradientInfo;
Hal Canaryc8f91802017-02-12 20:29:12 -05001713 SkColor gradientColor = SK_ColorBLACK;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001714 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001715 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001716 gradientInfo.fColorCount = 1;
Hal Canaryc8f91802017-02-12 20:29:12 -05001717 SkAssertResult(shader->asAGradient(&gradientInfo) == SkShader::kColor_GradientType);
Hal Canary04ac4612018-10-10 13:09:43 -04001718 color = SkColor4f::FromColor(gradientColor);
1719 entry->fColor ={color.fR, color.fG, color.fB, 1};
1720
Hal Canaryc8f91802017-02-12 20:29:12 -05001721 } else {
1722 // PDF positions patterns relative to the initial transform, so
1723 // we need to apply the current transform to the shader parameters.
1724 SkMatrix transform = matrix;
1725 transform.postConcat(fInitialTransform);
1726
1727 // PDF doesn't support kClamp_TileMode, so we simulate it by making
1728 // a pattern the size of the current clip.
Hal Canaryb400d4d2018-09-26 16:33:52 -04001729 SkRect clipStackBounds = clipStack ? clipStack->bounds(this->bounds())
1730 : SkRect::Make(this->bounds());
Hal Canaryc8f91802017-02-12 20:29:12 -05001731
1732 // We need to apply the initial transform to bounds in order to get
1733 // bounds in a consistent coordinate system.
Hal Canarya41c2aa2017-02-22 16:32:34 -05001734 fInitialTransform.mapRect(&clipStackBounds);
1735 SkIRect bounds;
1736 clipStackBounds.roundOut(&bounds);
Hal Canaryc8f91802017-02-12 20:29:12 -05001737
Hal Canary7e872ca2017-07-19 15:51:18 -04001738 pdfShader = SkPDFMakeShader(fDocument, shader, transform, bounds, paint.getColor());
Hal Canaryc8f91802017-02-12 20:29:12 -05001739
Hal Canary9e41c212018-09-03 12:00:23 -04001740 if (pdfShader) {
1741 // pdfShader has been canonicalized so we can directly compare pointers.
1742 entry->fShaderIndex = find_or_add(&fShaderResources, std::move(pdfShader));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001743 }
1744 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001745 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001746
Hal Canary80fa7ce2017-06-28 16:04:20 -04001747 sk_sp<SkPDFDict> newGraphicState;
Hal Canary04ac4612018-10-10 13:09:43 -04001748 if (color == paint.getColor4f()) {
Hal Canary5c1b3602017-04-17 16:30:06 -04001749 newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001750 } else {
1751 SkPaint newPaint = paint;
Hal Canary04ac4612018-10-10 13:09:43 -04001752 newPaint.setColor4f(color, nullptr);
Hal Canary5c1b3602017-04-17 16:30:06 -04001753 newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001754 }
Hal Canary9e41c212018-09-03 12:00:23 -04001755 entry->fGraphicStateIndex = find_or_add(&fGraphicStateResources, std::move(newGraphicState));
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001756
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001757 if (hasText) {
1758 entry->fTextScaleX = paint.getTextScaleX();
1759 entry->fTextFill = paint.getStyle();
1760 } else {
1761 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001762 }
1763}
1764
Hal Canaryfafe1352017-04-11 12:12:02 -04001765static SkSize rect_to_size(const SkRect& r) { return {r.width(), r.height()}; }
halcanary7a14b312015-10-01 07:28:13 -07001766
Hal Canary7cbf5e32017-07-12 13:10:23 -04001767static sk_sp<SkImage> color_filter(const SkImage* image,
halcanarya50151d2016-03-25 11:57:49 -07001768 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07001769 auto surface =
Hal Canary7cbf5e32017-07-12 13:10:23 -04001770 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(image->dimensions()));
halcanarya50151d2016-03-25 11:57:49 -07001771 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07001772 SkCanvas* canvas = surface->getCanvas();
1773 canvas->clear(SK_ColorTRANSPARENT);
1774 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07001775 paint.setColorFilter(sk_ref_sp(colorFilter));
Hal Canary7cbf5e32017-07-12 13:10:23 -04001776 canvas->drawImage(image, 0, 0, &paint);
halcanarya50151d2016-03-25 11:57:49 -07001777 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07001778}
1779
1780////////////////////////////////////////////////////////////////////////////////
Hal Canary7cbf5e32017-07-12 13:10:23 -04001781
1782static bool is_integer(SkScalar x) {
1783 return x == SkScalarTruncToScalar(x);
1784}
1785
1786static bool is_integral(const SkRect& r) {
1787 return is_integer(r.left()) &&
1788 is_integer(r.top()) &&
1789 is_integer(r.right()) &&
1790 is_integer(r.bottom());
1791}
1792
1793void SkPDFDevice::internalDrawImageRect(SkKeyedImage imageSubset,
1794 const SkRect* src,
1795 const SkRect& dst,
1796 const SkPaint& srcPaint,
1797 const SkMatrix& ctm) {
Hal Canaryb4e528d2018-03-09 16:02:15 -05001798 if (this->hasEmptyClip()) {
1799 return;
1800 }
Hal Canary7cbf5e32017-07-12 13:10:23 -04001801 if (!imageSubset) {
halcanarya50151d2016-03-25 11:57:49 -07001802 return;
1803 }
Hal Canary7cbf5e32017-07-12 13:10:23 -04001804
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001805 // First, figure out the src->dst transform and subset the image if needed.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001806 SkIRect bounds = imageSubset.image()->bounds();
Hal Canary7cbf5e32017-07-12 13:10:23 -04001807 SkRect srcRect = src ? *src : SkRect::Make(bounds);
1808 SkMatrix transform;
1809 transform.setRectToRect(srcRect, dst, SkMatrix::kFill_ScaleToFit);
1810 if (src && *src != SkRect::Make(bounds)) {
1811 if (!srcRect.intersect(SkRect::Make(bounds))) {
1812 return;
1813 }
1814 srcRect.roundOut(&bounds);
1815 transform.preTranslate(SkIntToScalar(bounds.x()),
1816 SkIntToScalar(bounds.y()));
1817 if (bounds != imageSubset.image()->bounds()) {
1818 imageSubset = imageSubset.subset(bounds);
1819 }
1820 if (!imageSubset) {
1821 return;
1822 }
1823 }
1824
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001825 // If the image is opaque and the paint's alpha is too, replace
1826 // kSrc blendmode with kSrcOver.
1827 SkPaint paint = srcPaint;
1828 if (imageSubset.image()->isOpaque()) {
1829 replace_srcmode_on_opaque_paint(&paint);
Hal Canaryd425a1d2017-07-12 13:13:51 -04001830 }
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001831
1832 // Alpha-only images need to get their color from the shader, before
1833 // applying the colorfilter.
1834 if (imageSubset.image()->isAlphaOnly() && paint.getColorFilter()) {
1835 // must blend alpha image and shader before applying colorfilter.
1836 auto surface =
1837 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageSubset.image()->dimensions()));
1838 SkCanvas* canvas = surface->getCanvas();
1839 SkPaint tmpPaint;
1840 // In the case of alpha images with shaders, the shader's coordinate
1841 // system is the image's coordiantes.
1842 tmpPaint.setShader(sk_ref_sp(paint.getShader()));
Hal Canary04ac4612018-10-10 13:09:43 -04001843 tmpPaint.setColor4f(paint.getColor4f(), nullptr);
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001844 canvas->clear(0x00000000);
1845 canvas->drawImage(imageSubset.image().get(), 0, 0, &tmpPaint);
1846 paint.setShader(nullptr);
1847 imageSubset = SkKeyedImage(surface->makeImageSnapshot());
1848 SkASSERT(!imageSubset.image()->isAlphaOnly());
1849 }
1850
1851 if (imageSubset.image()->isAlphaOnly()) {
1852 // The ColorFilter applies to the paint color/shader, not the alpha layer.
1853 SkASSERT(nullptr == paint.getColorFilter());
1854
Hal Canaryd425a1d2017-07-12 13:13:51 -04001855 sk_sp<SkImage> mask = alpha_image_to_greyscale_image(imageSubset.image().get());
1856 if (!mask) {
1857 return;
1858 }
1859 // PDF doesn't seem to allow masking vector graphics with an Image XObject.
1860 // Must mask with a Form XObject.
1861 sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice();
1862 {
Herb Derbyefe39bc2018-05-01 17:06:20 -04001863 SkCanvas canvas(maskDevice);
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001864 if (paint.getMaskFilter()) {
1865 // This clip prevents the mask image shader from covering
1866 // entire device if unnecessary.
1867 canvas.clipRect(this->cs().bounds(this->bounds()));
1868 canvas.concat(ctm);
1869 SkPaint tmpPaint;
1870 tmpPaint.setShader(mask->makeShader(&transform));
1871 tmpPaint.setMaskFilter(sk_ref_sp(paint.getMaskFilter()));
1872 canvas.drawRect(dst, tmpPaint);
1873 } else {
1874 canvas.concat(ctm);
1875 if (src && !is_integral(*src)) {
1876 canvas.clipRect(dst);
1877 }
1878 canvas.concat(transform);
1879 canvas.drawImage(mask, 0, 0);
1880 }
Hal Canaryd425a1d2017-07-12 13:13:51 -04001881 }
Hal Canaryd425a1d2017-07-12 13:13:51 -04001882 if (!ctm.isIdentity() && paint.getShader()) {
1883 transform_shader(&paint, ctm); // Since we are using identity matrix.
1884 }
Hal Canaryd892a9d2018-09-24 21:20:47 -04001885 ScopedContentEntry content(this, &this->cs(), SkMatrix::I(), paint);
Hal Canary42137de2018-10-08 16:00:37 -04001886 if (!content) {
Hal Canaryd425a1d2017-07-12 13:13:51 -04001887 return;
1888 }
1889 this->addSMaskGraphicState(std::move(maskDevice), content.stream());
Hal Canary813b5ac2018-09-28 12:30:37 -04001890 SkPDFUtils::AppendRectangle(SkRect::Make(this->size()), content.stream());
Hal Canaryd425a1d2017-07-12 13:13:51 -04001891 SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPath::kWinding_FillType, content.stream());
1892 this->clearMaskOnGraphicState(content.stream());
1893 return;
1894 }
Hal Canary7cbf5e32017-07-12 13:10:23 -04001895 if (paint.getMaskFilter()) {
1896 paint.setShader(imageSubset.image()->makeShader(&transform));
Hal Canaryd791a6f2018-09-28 12:14:08 -04001897 SkPath path = to_path(dst); // handles non-integral clipping.
Robert Phillips137ca522018-08-15 10:14:33 -04001898 this->internalDrawPath(this->cs(), this->ctm(), path, paint, true);
Hal Canary7cbf5e32017-07-12 13:10:23 -04001899 return;
1900 }
1901 transform.postConcat(ctm);
1902
1903 bool needToRestore = false;
1904 if (src && !is_integral(*src)) {
1905 // Need sub-pixel clipping to fix https://bug.skia.org/4374
1906 this->cs().save();
1907 this->cs().clipRect(dst, ctm, SkClipOp::kIntersect, true);
1908 needToRestore = true;
1909 }
1910 SK_AT_SCOPE_EXIT(if (needToRestore) { this->cs().restore(); });
1911
halcanary7a14b312015-10-01 07:28:13 -07001912 #ifdef SK_PDF_IMAGE_STATS
1913 gDrawImageCalls.fetch_add(1);
1914 #endif
Hal Canary7cbf5e32017-07-12 13:10:23 -04001915 SkMatrix matrix = transform;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001916
1917 // Rasterize the bitmap using perspective in a new bitmap.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001918 if (transform.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001919 // Transform the bitmap in the new space, without taking into
1920 // account the initial transform.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001921 SkRect imageBounds = SkRect::Make(imageSubset.image()->bounds());
Hal Canaryd791a6f2018-09-28 12:14:08 -04001922 SkPath perspectiveOutline = to_path(imageBounds);
Hal Canary7cbf5e32017-07-12 13:10:23 -04001923 perspectiveOutline.transform(transform);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001924
1925 // TODO(edisonn): perf - use current clip too.
1926 // Retrieve the bounds of the new shape.
1927 SkRect bounds = perspectiveOutline.getBounds();
1928
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001929 // Transform the bitmap in the new space, taking into
1930 // account the initial transform.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001931 SkMatrix total = transform;
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001932 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07001933
Hal Canaryd791a6f2018-09-28 12:14:08 -04001934 SkPath physicalPerspectiveOutline = to_path(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001935 physicalPerspectiveOutline.transform(total);
1936
halcanary7a14b312015-10-01 07:28:13 -07001937 SkRect physicalPerspectiveBounds =
1938 physicalPerspectiveOutline.getBounds();
1939 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
1940 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001941
1942 // TODO(edisonn): A better approach would be to use a bitmap shader
1943 // (in clamp mode) and draw a rect over the entire bounding box. Then
1944 // intersect perspectiveOutline to the clip. That will avoid introducing
1945 // alpha to the image while still giving good behavior at the edge of
1946 // the image. Avoiding alpha will reduce the pdf size and generation
1947 // CPU time some.
1948
halcanary7a14b312015-10-01 07:28:13 -07001949 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
1950
Hal Canaryf50ff392016-09-30 10:25:39 -04001951 auto surface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh));
halcanary7a14b312015-10-01 07:28:13 -07001952 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00001953 return;
1954 }
halcanary7a14b312015-10-01 07:28:13 -07001955 SkCanvas* canvas = surface->getCanvas();
1956 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001957
1958 SkScalar deltaX = bounds.left();
1959 SkScalar deltaY = bounds.top();
1960
Hal Canary7cbf5e32017-07-12 13:10:23 -04001961 SkMatrix offsetMatrix = transform;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001962 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001963 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001964
1965 // Translate the draw in the new canvas, so we perfectly fit the
1966 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07001967 canvas->setMatrix(offsetMatrix);
Hal Canary7cbf5e32017-07-12 13:10:23 -04001968 canvas->drawImage(imageSubset.image(), 0, 0);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001969 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07001970 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001971
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001972 // In the new space, we use the identity matrix translated
1973 // and scaled to reflect DPI.
1974 matrix.setScale(1 / scaleX, 1 / scaleY);
1975 matrix.postTranslate(deltaX, deltaY);
1976
Hal Canary7cbf5e32017-07-12 13:10:23 -04001977 imageSubset = SkKeyedImage(surface->makeImageSnapshot());
1978 if (!imageSubset) {
1979 return;
1980 }
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001981 }
1982
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001983 SkMatrix scaled;
1984 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00001985 scaled.setScale(SK_Scalar1, -SK_Scalar1);
1986 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001987 // Scale the image up from 1x1 to WxH.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001988 SkIRect subset = imageSubset.image()->bounds();
1989 scaled.postScale(SkIntToScalar(subset.width()),
1990 SkIntToScalar(subset.height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001991 scaled.postConcat(matrix);
Hal Canaryd892a9d2018-09-24 21:20:47 -04001992 ScopedContentEntry content(this, &this->cs(), scaled, paint);
Hal Canary42137de2018-10-08 16:00:37 -04001993 if (!content) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001994 return;
1995 }
1996 if (content.needShape()) {
Hal Canaryd791a6f2018-09-28 12:14:08 -04001997 SkPath shape = to_path(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001998 shape.transform(matrix);
1999 content.setShape(shape);
2000 }
2001 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002002 return;
2003 }
2004
halcanary287d22d2015-09-24 10:20:05 -07002005 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
Hal Canary7cbf5e32017-07-12 13:10:23 -04002006 sk_sp<SkImage> img = color_filter(imageSubset.image().get(), colorFilter);
2007 imageSubset = SkKeyedImage(std::move(img));
2008 if (!imageSubset) {
2009 return;
2010 }
halcanary7a14b312015-10-01 07:28:13 -07002011 // TODO(halcanary): de-dupe this by caching filtered images.
2012 // (maybe in the resource cache?)
2013 }
halcanarya50151d2016-03-25 11:57:49 -07002014
Hal Canary7cbf5e32017-07-12 13:10:23 -04002015 SkBitmapKey key = imageSubset.key();
Hal Canary5c1b3602017-04-17 16:30:06 -04002016 sk_sp<SkPDFObject>* pdfimagePtr = fDocument->canon()->fPDFBitmapMap.find(key);
2017 sk_sp<SkPDFObject> pdfimage = pdfimagePtr ? *pdfimagePtr : nullptr;
halcanary7a14b312015-10-01 07:28:13 -07002018 if (!pdfimage) {
Hal Canary7cbf5e32017-07-12 13:10:23 -04002019 SkASSERT(imageSubset);
2020 pdfimage = SkPDFCreateBitmapObject(imageSubset.release(),
Mike Reeda4daf192017-12-14 13:25:04 -05002021 fDocument->metadata().fEncodingQuality);
halcanary7a14b312015-10-01 07:28:13 -07002022 if (!pdfimage) {
2023 return;
halcanary287d22d2015-09-24 10:20:05 -07002024 }
halcanarya50151d2016-03-25 11:57:49 -07002025 fDocument->serialize(pdfimage); // serialize images early.
Hal Canary7cbf5e32017-07-12 13:10:23 -04002026 SkASSERT((key != SkBitmapKey{{0, 0, 0, 0}, 0}));
Hal Canary5c1b3602017-04-17 16:30:06 -04002027 fDocument->canon()->fPDFBitmapMap.set(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002028 }
Hal Canary3b8b11e2018-09-29 22:31:34 -04002029 this->drawFormXObject(std::move(pdfimage), content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002030}
reede51c3562016-07-19 14:33:20 -07002031
2032///////////////////////////////////////////////////////////////////////////////////////////////////
2033
2034#include "SkSpecialImage.h"
2035#include "SkImageFilter.h"
2036
Florin Malita53f77bd2017-04-28 13:48:37 -04002037void SkPDFDevice::drawSpecial(SkSpecialImage* srcImg, int x, int y, const SkPaint& paint,
2038 SkImage* clipImage, const SkMatrix& clipMatrix) {
Hal Canaryb4e528d2018-03-09 16:02:15 -05002039 if (this->hasEmptyClip()) {
2040 return;
2041 }
reede51c3562016-07-19 14:33:20 -07002042 SkASSERT(!srcImg->isTextureBacked());
2043
Florin Malita53f77bd2017-04-28 13:48:37 -04002044 //TODO: clipImage support
2045
reede51c3562016-07-19 14:33:20 -07002046 SkBitmap resultBM;
2047
2048 SkImageFilter* filter = paint.getImageFilter();
2049 if (filter) {
2050 SkIPoint offset = SkIPoint::Make(0, 0);
Mike Reeda1361362017-03-07 09:37:29 -05002051 SkMatrix matrix = this->ctm();
reede51c3562016-07-19 14:33:20 -07002052 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
Hal Canaryf3ee34f2017-02-07 16:58:28 -05002053 const SkIRect clipBounds =
Hal Canary22b2d8c2017-07-19 14:46:12 -04002054 this->cs().bounds(this->bounds()).roundOut().makeOffset(-x, -y);
Hal Canary67b39de2016-11-07 11:47:44 -05002055 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -04002056 // TODO: Should PDF be operating in a specified color type/space? For now, run the filter
brianosman2a75e5d2016-09-22 07:15:37 -07002057 // in the same color space as the source (this is different from all other backends).
Brian Osmana50205f2018-07-06 13:57:01 -04002058 SkImageFilter::OutputProperties outputProperties(kN32_SkColorType, srcImg->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -07002059 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
reede51c3562016-07-19 14:33:20 -07002060
2061 sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset));
2062 if (resultImg) {
2063 SkPaint tmpUnfiltered(paint);
2064 tmpUnfiltered.setImageFilter(nullptr);
2065 if (resultImg->getROPixels(&resultBM)) {
Mike Reeda1361362017-03-07 09:37:29 -05002066 this->drawSprite(resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered);
reede51c3562016-07-19 14:33:20 -07002067 }
2068 }
2069 } else {
2070 if (srcImg->getROPixels(&resultBM)) {
Mike Reeda1361362017-03-07 09:37:29 -05002071 this->drawSprite(resultBM, x, y, paint);
reede51c3562016-07-19 14:33:20 -07002072 }
2073 }
2074}
2075
2076sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) {
2077 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
2078}
2079
2080sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) {
Brian Osmanb9c49782018-10-12 12:01:22 -04002081 return SkSpecialImage::MakeFromImage(image->bounds(), image->makeNonTextureImage());
reede51c3562016-07-19 14:33:20 -07002082}
2083
2084sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() {
reede51c3562016-07-19 14:33:20 -07002085 return nullptr;
2086}
brianosman04a44d02016-09-21 09:46:57 -07002087
2088SkImageFilterCache* SkPDFDevice::getImageFilterCache() {
2089 // We always return a transient cache, so it is freed after each
2090 // filter traversal.
2091 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
2092}