blob: a6780b0c7e2ce20688e316cf37244414085de14d [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"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000039#include "SkRRect.h"
Hal Canaryd12a6762017-05-26 17:01:16 -040040#include "SkRasterClip.h"
halcanary4871f222016-08-26 13:17:44 -070041#include "SkScopeExit.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000042#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070043#include "SkSurface.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000044#include "SkTemplates.h"
Florin Malitaab54e732018-07-27 09:47:15 -040045#include "SkTextBlob.h"
halcanarye06ca962016-09-09 05:34:55 -070046#include "SkTextFormatParams.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040047#include "SkTo.h"
Hal Canaryea60b952018-08-21 11:45:46 -040048#include "SkUTF.h"
halcanarya6814332015-05-27 08:53:36 -070049#include "SkXfermodeInterpretation.h"
Hal Canaryd12a6762017-05-26 17:01:16 -040050
Hal Canary9e41c212018-09-03 12:00:23 -040051#include <vector>
52
Hal Canaryd12a6762017-05-26 17:01:16 -040053#ifndef SK_PDF_MASK_QUALITY
54 // If MASK_QUALITY is in [0,100], will be used for JpegEncoder.
55 // Otherwise, just encode masks losslessly.
56 #define SK_PDF_MASK_QUALITY 50
57 // Since these masks are used for blurry shadows, we shouldn't need
58 // high quality. Raise this value if your shadows have visible JPEG
59 // artifacts.
60 // If SkJpegEncoder::Encode fails, we will fall back to the lossless
61 // encoding.
62#endif
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000063
64// Utility functions
65
Hal Canaryd791a6f2018-09-28 12:14:08 -040066static SkPath to_path(const SkRect& r) {
67 SkPath p;
68 p.addRect(r);
69 return p;
70}
71
Hal Canary51329c92017-06-27 14:28:37 -040072// This function destroys the mask and either frees or takes the pixels.
73sk_sp<SkImage> mask_to_greyscale_image(SkMask* mask) {
74 sk_sp<SkImage> img;
75 SkPixmap pm(SkImageInfo::Make(mask->fBounds.width(), mask->fBounds.height(),
76 kGray_8_SkColorType, kOpaque_SkAlphaType),
77 mask->fImage, mask->fRowBytes);
78 const int imgQuality = SK_PDF_MASK_QUALITY;
79 if (imgQuality <= 100 && imgQuality >= 0) {
80 SkDynamicMemoryWStream buffer;
81 SkJpegEncoder::Options jpegOptions;
82 jpegOptions.fQuality = imgQuality;
83 if (SkJpegEncoder::Encode(&buffer, pm, jpegOptions)) {
84 img = SkImage::MakeFromEncoded(buffer.detachAsData());
85 SkASSERT(img);
86 if (img) {
87 SkMask::FreeImage(mask->fImage);
88 }
89 }
90 }
91 if (!img) {
92 img = SkImage::MakeFromRaster(pm, [](const void* p, void*) { SkMask::FreeImage((void*)p); },
93 nullptr);
94 }
95 *mask = SkMask(); // destructive;
96 return img;
97}
98
Hal Canaryd425a1d2017-07-12 13:13:51 -040099sk_sp<SkImage> alpha_image_to_greyscale_image(const SkImage* mask) {
100 int w = mask->width(), h = mask->height();
101 SkBitmap greyBitmap;
102 greyBitmap.allocPixels(SkImageInfo::Make(w, h, kGray_8_SkColorType, kOpaque_SkAlphaType));
103 if (!mask->readPixels(SkImageInfo::MakeA8(w, h),
104 greyBitmap.getPixels(), greyBitmap.rowBytes(), 0, 0)) {
105 return nullptr;
106 }
107 return SkImage::MakeFromBitmap(greyBitmap);
108}
109
Hal Canary9a3f5542018-12-10 19:59:07 -0500110static int add_resource(SkTHashSet<SkPDFIndirectReference>& resources, SkPDFIndirectReference ref) {
111 resources.add(ref);
112 return ref.fValue;
113}
114
Mike Reeda1361362017-03-07 09:37:29 -0500115static void draw_points(SkCanvas::PointMode mode,
116 size_t count,
117 const SkPoint* points,
118 const SkPaint& paint,
119 const SkIRect& bounds,
120 const SkMatrix& ctm,
121 SkBaseDevice* device) {
122 SkRasterClip rc(bounds);
123 SkDraw draw;
124 draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(bounds.right(), bounds.bottom()), nullptr, 0);
125 draw.fMatrix = &ctm;
126 draw.fRC = &rc;
127 draw.drawPoints(mode, count, points, paint, device);
128}
129
reed374772b2016-10-05 17:33:02 -0700130// If the paint will definitely draw opaquely, replace kSrc with
131// kSrcOver. http://crbug.com/473572
halcanarya6814332015-05-27 08:53:36 -0700132static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
reed374772b2016-10-05 17:33:02 -0700133 if (kSrcOver_SkXfermodeInterpretation == SkInterpretXfermode(*paint, false)) {
134 paint->setBlendMode(SkBlendMode::kSrcOver);
halcanarya6814332015-05-27 08:53:36 -0700135 }
136}
137
Hal Canaryd12a6762017-05-26 17:01:16 -0400138// A shader's matrix is: CTMM x LocalMatrix x WrappingLocalMatrix. We want to
139// switch to device space, where CTM = I, while keeping the original behavior.
140//
141// I * LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix
142// LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix
143// InvLocalMatrix * LocalMatrix * NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix
144// NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix
145//
146static void transform_shader(SkPaint* paint, const SkMatrix& ctm) {
147 SkMatrix lm = SkPDFUtils::GetShaderLocalMatrix(paint->getShader());
148 SkMatrix lmInv;
149 if (lm.invert(&lmInv)) {
150 SkMatrix m = SkMatrix::Concat(SkMatrix::Concat(lmInv, ctm), lm);
151 paint->setShader(paint->getShader()->makeWithLocalMatrix(m));
152 }
153}
154
Hal Canary04ac4612018-10-10 13:09:43 -0400155static void emit_pdf_color(SkColor4f color, SkWStream* result) {
156 SkASSERT(color.fA == 1); // We handle alpha elsewhere.
157 SkPDFUtils::AppendColorComponentF(color.fR, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000158 result->writeText(" ");
Hal Canary04ac4612018-10-10 13:09:43 -0400159 SkPDFUtils::AppendColorComponentF(color.fG, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000160 result->writeText(" ");
Hal Canary04ac4612018-10-10 13:09:43 -0400161 SkPDFUtils::AppendColorComponentF(color.fB, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000162 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000163}
164
Hal Canaryd12a6762017-05-26 17:01:16 -0400165// If the paint has a color filter, apply the color filter to the shader or the
166// paint color. Remove the color filter.
167void remove_color_filter(SkPaint* paint) {
168 if (SkColorFilter* cf = paint->getColorFilter()) {
169 if (SkShader* shader = paint->getShader()) {
170 paint->setShader(shader->makeWithColorFilter(paint->refColorFilter()));
171 } else {
Hal Canary04ac4612018-10-10 13:09:43 -0400172 paint->setColor4f(cf->filterColor4f(paint->getColor4f(), nullptr), nullptr);
Hal Canaryd12a6762017-05-26 17:01:16 -0400173 }
174 paint->setColorFilter(nullptr);
175 }
176}
177
Hal Canary42137de2018-10-08 16:00:37 -0400178SkPDFDevice::GraphicStackState::GraphicStackState(SkDynamicMemoryWStream* s) : fContentStream(s) {
179}
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000180
Hal Canaryb400d4d2018-09-26 16:33:52 -0400181void SkPDFDevice::GraphicStackState::drainStack() {
182 if (fContentStream) {
183 while (fStackDepth) {
Hal Canaryb5b72792018-09-28 08:49:33 -0400184 this->pop();
Hal Canaryb400d4d2018-09-26 16:33:52 -0400185 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000186 }
Hal Canaryb400d4d2018-09-26 16:33:52 -0400187 SkASSERT(fStackDepth == 0);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000188}
189
Hal Canaryb400d4d2018-09-26 16:33:52 -0400190void SkPDFDevice::GraphicStackState::push() {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000191 SkASSERT(fStackDepth < kMaxStackDepth);
192 fContentStream->writeText("q\n");
193 fStackDepth++;
194 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
195}
196
Hal Canaryb400d4d2018-09-26 16:33:52 -0400197void SkPDFDevice::GraphicStackState::pop() {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000198 SkASSERT(fStackDepth > 0);
199 fContentStream->writeText("Q\n");
Hal Canaryb400d4d2018-09-26 16:33:52 -0400200 fEntries[fStackDepth] = SkPDFDevice::GraphicStateEntry();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000201 fStackDepth--;
202}
203
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000204/* Calculate an inverted path's equivalent non-inverted path, given the
205 * canvas bounds.
206 * outPath may alias with invPath (since this is supported by PathOps).
207 */
208static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
209 SkPath* outPath) {
210 SkASSERT(invPath.isInverseFillType());
Hal Canaryd791a6f2018-09-28 12:14:08 -0400211 return Op(to_path(bounds), invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000212}
213
Hal Canaryd00ef062018-06-05 11:53:58 -0400214static SkRect rect_intersect(SkRect u, SkRect v) {
215 if (u.isEmpty() || v.isEmpty()) { return {0, 0, 0, 0}; }
216 return u.intersect(v) ? u : SkRect{0, 0, 0, 0};
217}
218
219// Test to see if the clipstack is a simple rect, If so, we can avoid all PathOps code
220// and speed thing up.
221static bool is_rect(const SkClipStack& clipStack, const SkRect& bounds, SkRect* dst) {
222 SkRect currentClip = bounds;
223 SkClipStack::Iter iter(clipStack, SkClipStack::Iter::kBottom_IterStart);
224 while (const SkClipStack::Element* element = iter.next()) {
225 SkRect elementRect{0, 0, 0, 0};
226 switch (element->getDeviceSpaceType()) {
227 case SkClipStack::Element::DeviceSpaceType::kEmpty:
228 break;
229 case SkClipStack::Element::DeviceSpaceType::kRect:
230 elementRect = element->getDeviceSpaceRect();
231 break;
232 default:
233 return false;
234 }
235 switch (element->getOp()) {
236 case kReplace_SkClipOp:
237 currentClip = rect_intersect(bounds, elementRect);
238 break;
239 case SkClipOp::kIntersect:
240 currentClip = rect_intersect(currentClip, elementRect);
241 break;
242 default:
243 return false;
244 }
245 }
246 *dst = currentClip;
247 return true;
248}
249
250static void append_clip(const SkClipStack& clipStack,
251 const SkIRect& bounds,
252 SkWStream* wStream) {
253 // The bounds are slightly outset to ensure this is correct in the
254 // face of floating-point accuracy and possible SkRegion bitmap
255 // approximations.
256 SkRect outsetBounds = SkRect::Make(bounds.makeOutset(1, 1));
257
258 SkRect clipStackRect;
259 if (is_rect(clipStack, outsetBounds, &clipStackRect)) {
260 SkPDFUtils::AppendRectangle(clipStackRect, wStream);
261 wStream->writeText("W* n\n");
262 return;
263 }
264
265 SkPath clipPath;
266 (void)clipStack.asPath(&clipPath);
267
Hal Canaryd791a6f2018-09-28 12:14:08 -0400268 if (Op(clipPath, to_path(outsetBounds), kIntersect_SkPathOp, &clipPath)) {
Hal Canaryd00ef062018-06-05 11:53:58 -0400269 SkPDFUtils::EmitPath(clipPath, SkPaint::kFill_Style, wStream);
270 SkPath::FillType clipFill = clipPath.getFillType();
271 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
272 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
273 if (clipFill == SkPath::kEvenOdd_FillType) {
274 wStream->writeText("W* n\n");
275 } else {
276 wStream->writeText("W n\n");
277 }
278 }
279 // If Op() fails (pathological case; e.g. input values are
280 // extremely large or NaN), emit no clip at all.
281}
282
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000283// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000284// graphic state stack, and the fact that we can know all the clips used
285// on the page to optimize this.
Hal Canaryb400d4d2018-09-26 16:33:52 -0400286void SkPDFDevice::GraphicStackState::updateClip(const SkClipStack* clipStack,
287 const SkIRect& bounds) {
288 uint32_t clipStackGenID = clipStack ? clipStack->getTopmostGenID()
289 : SkClipStack::kWideOpenGenID;
290 if (clipStackGenID == currentEntry()->fClipStackGenID) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000291 return;
292 }
293
294 while (fStackDepth > 0) {
Hal Canaryb5b72792018-09-28 08:49:33 -0400295 this->pop();
Hal Canaryb400d4d2018-09-26 16:33:52 -0400296 if (clipStackGenID == currentEntry()->fClipStackGenID) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000297 return;
298 }
299 }
Hal Canaryb5b72792018-09-28 08:49:33 -0400300 SkASSERT(currentEntry()->fClipStackGenID == SkClipStack::kWideOpenGenID);
Hal Canaryb400d4d2018-09-26 16:33:52 -0400301 if (clipStackGenID != SkClipStack::kWideOpenGenID) {
302 SkASSERT(clipStack);
Hal Canaryb5b72792018-09-28 08:49:33 -0400303 this->push();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000304
Hal Canaryb400d4d2018-09-26 16:33:52 -0400305 currentEntry()->fClipStackGenID = clipStackGenID;
306 append_clip(*clipStack, bounds, fContentStream);
307 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000308}
309
Hal Canarye650b852018-09-12 09:12:36 -0400310static void append_transform(const SkMatrix& matrix, SkWStream* content) {
311 SkScalar values[6];
312 if (!matrix.asAffine(values)) {
313 SkMatrix::SetAffineIdentity(values);
314 }
315 for (SkScalar v : values) {
316 SkPDFUtils::AppendScalar(v, content);
317 content->writeText(" ");
318 }
319 content->writeText("cm\n");
320}
321
Hal Canaryb400d4d2018-09-26 16:33:52 -0400322void SkPDFDevice::GraphicStackState::updateMatrix(const SkMatrix& matrix) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000323 if (matrix == currentEntry()->fMatrix) {
324 return;
325 }
326
327 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
328 SkASSERT(fStackDepth > 0);
Hal Canaryb400d4d2018-09-26 16:33:52 -0400329 SkASSERT(fEntries[fStackDepth].fClipStackGenID ==
330 fEntries[fStackDepth -1].fClipStackGenID);
Hal Canaryb5b72792018-09-28 08:49:33 -0400331 this->pop();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000332
333 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
334 }
335 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
336 return;
337 }
338
Hal Canaryb5b72792018-09-28 08:49:33 -0400339 this->push();
Hal Canarye650b852018-09-12 09:12:36 -0400340 append_transform(matrix, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000341 currentEntry()->fMatrix = matrix;
342}
343
Hal Canaryb400d4d2018-09-26 16:33:52 -0400344void SkPDFDevice::GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000345 // PDF treats a shader as a color, so we only set one or the other.
346 if (state.fShaderIndex >= 0) {
347 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000348 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000349 currentEntry()->fShaderIndex = state.fShaderIndex;
350 }
351 } else {
352 if (state.fColor != currentEntry()->fColor ||
353 currentEntry()->fShaderIndex >= 0) {
354 emit_pdf_color(state.fColor, fContentStream);
355 fContentStream->writeText("RG ");
356 emit_pdf_color(state.fColor, fContentStream);
357 fContentStream->writeText("rg\n");
358 currentEntry()->fColor = state.fColor;
359 currentEntry()->fShaderIndex = -1;
360 }
361 }
362
363 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000364 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000365 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
366 }
367
368 if (state.fTextScaleX) {
369 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
Hal Canary9b491b22017-11-28 15:10:13 -0500370 SkScalar pdfScale = state.fTextScaleX * 100;
halcanarybc4696b2015-05-06 10:56:04 -0700371 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000372 fContentStream->writeText(" Tz\n");
373 currentEntry()->fTextScaleX = state.fTextScaleX;
374 }
375 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700376 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
377 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
378 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
Cary Clark60ebf142018-09-06 12:22:33 +0000379 fContentStream->writeDecAsText(state.fTextFill);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000380 fContentStream->writeText(" Tr\n");
381 currentEntry()->fTextFill = state.fTextFill;
382 }
383 }
384}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000385
reed76033be2015-03-14 10:54:31 -0700386static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700387 // PDF does not support image filters, so render them on CPU.
388 // Note that this rendering is done at "screen" resolution (100dpi), not
389 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700390 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800391 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700392
393 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700394 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700395}
396
397SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
reedcd4051e2016-07-15 09:41:26 -0700398 if (layerPaint && not_supported_for_layers(*layerPaint)) {
reed7503d602016-07-15 14:23:29 -0700399 // need to return a raster device, which we will detect in drawDevice()
400 return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
senorblancob0e89dc2014-10-20 14:03:12 -0700401 }
Hal Canary22b2d8c2017-07-19 14:46:12 -0400402 return new SkPDFDevice(cinfo.fInfo.dimensions(), fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000403}
404
halcanary989da4a2016-03-21 14:33:17 -0700405SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
406
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000407// A helper class to automatically finish a ContentEntry at the end of a
408// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000409class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000410public:
Mike Reeda1361362017-03-07 09:37:29 -0500411 ScopedContentEntry(SkPDFDevice* device,
Hal Canaryd892a9d2018-09-24 21:20:47 -0400412 const SkClipStack* clipStack,
Mike Reed27d07f02017-03-04 21:47:47 +0000413 const SkMatrix& matrix,
Mike Reeda1361362017-03-07 09:37:29 -0500414 const SkPaint& paint,
415 bool hasText = false)
416 : fDevice(device)
Mike Reeda1361362017-03-07 09:37:29 -0500417 , fBlendMode(SkBlendMode::kSrcOver)
Hal Canaryb400d4d2018-09-26 16:33:52 -0400418 , fClipStack(clipStack)
Mike Reeda1361362017-03-07 09:37:29 -0500419 {
420 if (matrix.hasPerspective()) {
421 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
422 return;
423 }
424 fBlendMode = paint.getBlendMode();
Hal Canary42137de2018-10-08 16:00:37 -0400425 fContentStream =
Mike Reeda1361362017-03-07 09:37:29 -0500426 fDevice->setUpContentEntry(clipStack, matrix, paint, hasText, &fDstFormXObject);
Mike Reed27d07f02017-03-04 21:47:47 +0000427 }
Mike Reeda1361362017-03-07 09:37:29 -0500428 ScopedContentEntry(SkPDFDevice* dev, const SkPaint& paint, bool hasText = false)
Hal Canaryd892a9d2018-09-24 21:20:47 -0400429 : ScopedContentEntry(dev, &dev->cs(), dev->ctm(), paint, hasText) {}
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000430
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000431 ~ScopedContentEntry() {
Hal Canary42137de2018-10-08 16:00:37 -0400432 if (fContentStream) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000433 SkPath* shape = &fShape;
434 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700435 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000436 }
Hal Canary9a3f5542018-12-10 19:59:07 -0500437 fDevice->finishContentEntry(fClipStack, fBlendMode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000438 }
439 }
440
Hal Canary42137de2018-10-08 16:00:37 -0400441 explicit operator bool() const { return fContentStream != nullptr; }
442 SkDynamicMemoryWStream* stream() { return fContentStream; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000443
444 /* Returns true when we explicitly need the shape of the drawing. */
445 bool needShape() {
reed374772b2016-10-05 17:33:02 -0700446 switch (fBlendMode) {
447 case SkBlendMode::kClear:
448 case SkBlendMode::kSrc:
449 case SkBlendMode::kSrcIn:
450 case SkBlendMode::kSrcOut:
451 case SkBlendMode::kDstIn:
452 case SkBlendMode::kDstOut:
453 case SkBlendMode::kSrcATop:
454 case SkBlendMode::kDstATop:
455 case SkBlendMode::kModulate:
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000456 return true;
457 default:
458 return false;
459 }
460 }
461
462 /* Returns true unless we only need the shape of the drawing. */
463 bool needSource() {
reed374772b2016-10-05 17:33:02 -0700464 if (fBlendMode == SkBlendMode::kClear) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000465 return false;
466 }
467 return true;
468 }
469
470 /* If the shape is different than the alpha component of the content, then
471 * setShape should be called with the shape. In particular, images and
472 * devices have rectangular shape.
473 */
474 void setShape(const SkPath& shape) {
475 fShape = shape;
476 }
477
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000478private:
Hal Canary42137de2018-10-08 16:00:37 -0400479 SkPDFDevice* fDevice = nullptr;
480 SkDynamicMemoryWStream* fContentStream = nullptr;
reed374772b2016-10-05 17:33:02 -0700481 SkBlendMode fBlendMode;
Hal Canary9a3f5542018-12-10 19:59:07 -0500482 SkPDFIndirectReference fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000483 SkPath fShape;
Hal Canaryb400d4d2018-09-26 16:33:52 -0400484 const SkClipStack* fClipStack;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000485};
486
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000487////////////////////////////////////////////////////////////////////////////////
488
Hal Canary7d06ab22018-09-10 14:39:13 -0400489SkPDFDevice::SkPDFDevice(SkISize pageSize, SkPDFDocument* doc, const SkMatrix& transform)
reed589a39e2016-08-20 07:59:19 -0700490 : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()),
491 SkSurfaceProps(0, kUnknown_SkPixelGeometry))
Hal Canary7d06ab22018-09-10 14:39:13 -0400492 , fInitialTransform(transform)
Dominic Mazzoni656cefe2018-09-25 20:29:15 -0700493 , fNodeId(0)
Hal Canarya0622582017-06-29 18:51:35 -0400494 , fDocument(doc)
495{
Hal Canary22b2d8c2017-07-19 14:46:12 -0400496 SkASSERT(!pageSize.isEmpty());
Hal Canarya0622582017-06-29 18:51:35 -0400497}
robertphillips1f3923e2016-07-21 07:17:54 -0700498
Hal Canary9e41c212018-09-03 12:00:23 -0400499SkPDFDevice::~SkPDFDevice() = default;
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000500
Hal Canary9e41c212018-09-03 12:00:23 -0400501void SkPDFDevice::reset() {
Hal Canary9e41c212018-09-03 12:00:23 -0400502 fLinkToURLs = std::vector<RectWithData>();
503 fLinkToDestinations = std::vector<RectWithData>();
504 fNamedDestinations = std::vector<NamedDestination>();
Hal Canary9a3f5542018-12-10 19:59:07 -0500505 fGraphicStateResources.reset();
506 fXObjectResources.reset();
507 fShaderResources.reset();
Hal Canaryb10f92e2018-11-16 17:01:50 -0500508 fFontResources.reset();
Hal Canary42137de2018-10-08 16:00:37 -0400509 fContent.reset();
Hal Canary813b5ac2018-09-28 12:30:37 -0400510 fActiveStackState = GraphicStackState();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000511}
512
Mike Reeda1361362017-03-07 09:37:29 -0500513void SkPDFDevice::drawAnnotation(const SkRect& rect, const char key[], SkData* value) {
Hal Canary9cd21682017-02-22 15:55:06 -0500514 if (!value) {
515 return;
516 }
517 if (rect.isEmpty()) {
Dominic Mazzoni656cefe2018-09-25 20:29:15 -0700518 if (!strcmp(key, SkPDFGetNodeIdKey())) {
519 int nodeID;
520 if (value->size() != sizeof(nodeID)) { return; }
521 memcpy(&nodeID, value->data(), sizeof(nodeID));
522 fNodeId = nodeID;
523 return;
524 }
Mike Reeda1361362017-03-07 09:37:29 -0500525 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
526 SkPoint transformedPoint;
527 this->ctm().mapXY(rect.x(), rect.y(), &transformedPoint);
Hal Canary5c1b3602017-04-17 16:30:06 -0400528 fNamedDestinations.emplace_back(NamedDestination{sk_ref_sp(value), transformedPoint});
Mike Reeda1361362017-03-07 09:37:29 -0500529 }
530 return;
531 }
532 // Convert to path to handle non-90-degree rotations.
Hal Canaryd791a6f2018-09-28 12:14:08 -0400533 SkPath path = to_path(rect);
Mike Reeda1361362017-03-07 09:37:29 -0500534 path.transform(this->ctm(), &path);
535 SkPath clip;
536 (void)this->cs().asPath(&clip);
537 Op(clip, path, kIntersect_SkPathOp, &path);
538 // PDF wants a rectangle only.
539 SkRect transformedRect = path.getBounds();
540 if (transformedRect.isEmpty()) {
541 return;
542 }
543 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
Hal Canary5c1b3602017-04-17 16:30:06 -0400544 fLinkToURLs.emplace_back(RectWithData{transformedRect, sk_ref_sp(value)});
Mike Reeda1361362017-03-07 09:37:29 -0500545 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
Hal Canary5c1b3602017-04-17 16:30:06 -0400546 fLinkToDestinations.emplace_back(RectWithData{transformedRect, sk_ref_sp(value)});
reedf70b5312016-03-04 16:36:20 -0800547 }
548}
549
Hal Canaryd12a6762017-05-26 17:01:16 -0400550void SkPDFDevice::drawPaint(const SkPaint& srcPaint) {
Hal Canaryabf8e412018-09-24 11:37:23 -0400551 SkMatrix inverse;
552 if (!this->ctm().invert(&inverse)) {
553 return;
554 }
555 SkRect bbox = this->cs().bounds(this->bounds());
556 inverse.mapRect(&bbox);
557 bbox.roundOut(&bbox);
Hal Canaryb4e528d2018-03-09 16:02:15 -0500558 if (this->hasEmptyClip()) {
559 return;
560 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400561 SkPaint newPaint = srcPaint;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000562 newPaint.setStyle(SkPaint::kFill_Style);
Hal Canaryabf8e412018-09-24 11:37:23 -0400563 this->drawRect(bbox, newPaint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000564}
565
Mike Reeda1361362017-03-07 09:37:29 -0500566void SkPDFDevice::drawPoints(SkCanvas::PointMode mode,
halcanarya6814332015-05-27 08:53:36 -0700567 size_t count,
568 const SkPoint* points,
569 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500570 if (this->hasEmptyClip()) {
571 return;
572 }
halcanarya6814332015-05-27 08:53:36 -0700573 SkPaint passedPaint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400574 remove_color_filter(&passedPaint);
halcanarya6814332015-05-27 08:53:36 -0700575 replace_srcmode_on_opaque_paint(&passedPaint);
Hal Canary80fa7ce2017-06-28 16:04:20 -0400576 if (SkCanvas::kPoints_PointMode != mode) {
577 passedPaint.setStyle(SkPaint::kStroke_Style);
578 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000579 if (count == 0) {
580 return;
581 }
582
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000583 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
584 // We only use this when there's a path effect because of the overhead
585 // of multiple calls to setUpContentEntry it causes.
586 if (passedPaint.getPathEffect()) {
Mike Reeda1361362017-03-07 09:37:29 -0500587 draw_points(mode, count, points, passedPaint,
588 this->devClipBounds(), this->ctm(), this);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000589 return;
590 }
591
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000592 const SkPaint* paint = &passedPaint;
593 SkPaint modifiedPaint;
594
595 if (mode == SkCanvas::kPoints_PointMode &&
596 paint->getStrokeCap() != SkPaint::kRound_Cap) {
597 modifiedPaint = *paint;
598 paint = &modifiedPaint;
599 if (paint->getStrokeWidth()) {
600 // PDF won't draw a single point with square/butt caps because the
601 // orientation is ambiguous. Draw a rectangle instead.
602 modifiedPaint.setStyle(SkPaint::kFill_Style);
603 SkScalar strokeWidth = paint->getStrokeWidth();
604 SkScalar halfStroke = SkScalarHalf(strokeWidth);
605 for (size_t i = 0; i < count; i++) {
606 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
607 r.inset(-halfStroke, -halfStroke);
Mike Reeda1361362017-03-07 09:37:29 -0500608 this->drawRect(r, modifiedPaint);
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000609 }
610 return;
611 } else {
612 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
613 }
614 }
615
Mike Reeda1361362017-03-07 09:37:29 -0500616 ScopedContentEntry content(this, *paint);
Hal Canary42137de2018-10-08 16:00:37 -0400617 if (!content) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000618 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000619 }
Hal Canary51329c92017-06-27 14:28:37 -0400620 SkDynamicMemoryWStream* contentStream = content.stream();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000621 switch (mode) {
622 case SkCanvas::kPolygon_PointMode:
Hal Canary51329c92017-06-27 14:28:37 -0400623 SkPDFUtils::MoveTo(points[0].fX, points[0].fY, contentStream);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000624 for (size_t i = 1; i < count; i++) {
Hal Canary51329c92017-06-27 14:28:37 -0400625 SkPDFUtils::AppendLine(points[i].fX, points[i].fY, contentStream);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000626 }
Hal Canary51329c92017-06-27 14:28:37 -0400627 SkPDFUtils::StrokePath(contentStream);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000628 break;
629 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000630 for (size_t i = 0; i < count/2; i++) {
Hal Canary51329c92017-06-27 14:28:37 -0400631 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, contentStream);
632 SkPDFUtils::AppendLine(points[i * 2 + 1].fX, points[i * 2 + 1].fY, contentStream);
633 SkPDFUtils::StrokePath(contentStream);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000634 }
635 break;
636 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000637 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
638 for (size_t i = 0; i < count; i++) {
Hal Canary51329c92017-06-27 14:28:37 -0400639 SkPDFUtils::MoveTo(points[i].fX, points[i].fY, contentStream);
640 SkPDFUtils::ClosePath(contentStream);
641 SkPDFUtils::StrokePath(contentStream);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000642 }
643 break;
644 default:
645 SkASSERT(false);
646 }
647}
648
Hal Canary74801582018-12-18 16:30:41 -0500649static std::unique_ptr<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
650 auto annotation = SkPDFMakeDict("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700651 annotation->insertName("Subtype", "Link");
halcanary488165e2016-04-22 06:10:21 -0700652 annotation->insertInt("F", 4); // required by ISO 19005
Hal Canarye650b852018-09-12 09:12:36 -0400653 // Border: 0 = Horizontal corner radius.
654 // 0 = Vertical corner radius.
655 // 0 = Width, 0 = no border.
656 annotation->insertObject("Border", SkPDFMakeArray(0, 0, 0));
wangxianzhud76665d2015-07-17 17:23:15 -0700657
Hal Canarye650b852018-09-12 09:12:36 -0400658 annotation->insertObject("Rect", SkPDFMakeArray(translatedRect.fLeft,
659 translatedRect.fTop,
660 translatedRect.fRight,
661 translatedRect.fBottom));
halcanary8103a342016-03-08 15:10:16 -0800662 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700663}
664
Hal Canary74801582018-12-18 16:30:41 -0500665static std::unique_ptr<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
666 std::unique_ptr<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700667 SkString url(static_cast<const char *>(urlData->data()),
668 urlData->size() - 1);
Hal Canary74801582018-12-18 16:30:41 -0500669 auto action = SkPDFMakeDict("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700670 action->insertName("S", "URI");
671 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800672 annotation->insertObject("A", std::move(action));
673 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700674}
675
Hal Canary74801582018-12-18 16:30:41 -0500676static std::unique_ptr<SkPDFDict> create_link_named_dest(const SkData* nameData,
677 const SkRect& r) {
678 std::unique_ptr<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700679 SkString name(static_cast<const char *>(nameData->data()),
680 nameData->size() - 1);
681 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800682 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700683}
684
Mike Reeda1361362017-03-07 09:37:29 -0500685void SkPDFDevice::drawRect(const SkRect& rect,
halcanarya6814332015-05-27 08:53:36 -0700686 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500687 if (this->hasEmptyClip()) {
688 return;
689 }
halcanarya6814332015-05-27 08:53:36 -0700690 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400691 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700692 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000693 SkRect r = rect;
694 r.sort();
695
Hal Canary8b354dc2018-09-20 12:51:22 -0400696 if (paint.getPathEffect() || paint.getMaskFilter() || this->ctm().hasPerspective()) {
Hal Canaryd791a6f2018-09-28 12:14:08 -0400697 this->drawPath(to_path(r), paint, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000698 return;
699 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000700
Mike Reeda1361362017-03-07 09:37:29 -0500701 ScopedContentEntry content(this, paint);
Hal Canary42137de2018-10-08 16:00:37 -0400702 if (!content) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000703 return;
704 }
Hal Canary51329c92017-06-27 14:28:37 -0400705 SkPDFUtils::AppendRectangle(r, content.stream());
706 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000707}
708
Mike Reeda1361362017-03-07 09:37:29 -0500709void SkPDFDevice::drawRRect(const SkRRect& rrect,
halcanarya6814332015-05-27 08:53:36 -0700710 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500711 if (this->hasEmptyClip()) {
712 return;
713 }
halcanarya6814332015-05-27 08:53:36 -0700714 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400715 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700716 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000717 SkPath path;
718 path.addRRect(rrect);
Robert Phillips137ca522018-08-15 10:14:33 -0400719 this->drawPath(path, paint, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000720}
721
Mike Reeda1361362017-03-07 09:37:29 -0500722void SkPDFDevice::drawOval(const SkRect& oval,
halcanarya6814332015-05-27 08:53:36 -0700723 const SkPaint& srcPaint) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500724 if (this->hasEmptyClip()) {
725 return;
726 }
halcanarya6814332015-05-27 08:53:36 -0700727 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400728 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700729 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700730 SkPath path;
731 path.addOval(oval);
Robert Phillips137ca522018-08-15 10:14:33 -0400732 this->drawPath(path, paint, true);
reed89443ab2014-06-27 11:34:19 -0700733}
734
Mike Reeda1361362017-03-07 09:37:29 -0500735void SkPDFDevice::drawPath(const SkPath& origPath,
halcanarya6814332015-05-27 08:53:36 -0700736 const SkPaint& srcPaint,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000737 bool pathIsMutable) {
Robert Phillips137ca522018-08-15 10:14:33 -0400738 this->internalDrawPath(this->cs(), this->ctm(), origPath, srcPaint, pathIsMutable);
Mike Reeda1361362017-03-07 09:37:29 -0500739}
740
Hal Canaryd12a6762017-05-26 17:01:16 -0400741void SkPDFDevice::internalDrawPathWithFilter(const SkClipStack& clipStack,
742 const SkMatrix& ctm,
743 const SkPath& origPath,
Robert Phillips137ca522018-08-15 10:14:33 -0400744 const SkPaint& origPaint) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400745 SkASSERT(origPaint.getMaskFilter());
746 SkPath path(origPath);
747 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
Robert Phillips137ca522018-08-15 10:14:33 -0400748
Hal Canaryd12a6762017-05-26 17:01:16 -0400749 SkStrokeRec::InitStyle initStyle = paint->getFillPath(path, &path)
750 ? SkStrokeRec::kFill_InitStyle
751 : SkStrokeRec::kHairline_InitStyle;
752 path.transform(ctm, &path);
753
Hal Canary22b2d8c2017-07-19 14:46:12 -0400754 SkIRect bounds = clipStack.bounds(this->bounds()).roundOut();
Hal Canaryd12a6762017-05-26 17:01:16 -0400755 SkMask sourceMask;
756 if (!SkDraw::DrawToMask(path, &bounds, paint->getMaskFilter(), &SkMatrix::I(),
757 &sourceMask, SkMask::kComputeBoundsAndRenderImage_CreateMode,
758 initStyle)) {
759 return;
760 }
761 SkAutoMaskFreeImage srcAutoMaskFreeImage(sourceMask.fImage);
762 SkMask dstMask;
763 SkIPoint margin;
Mike Reed80747ef2018-01-23 15:29:32 -0500764 if (!as_MFB(paint->getMaskFilter())->filterMask(&dstMask, sourceMask, ctm, &margin)) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400765 return;
766 }
Hal Canary51329c92017-06-27 14:28:37 -0400767 SkIRect dstMaskBounds = dstMask.fBounds;
768 sk_sp<SkImage> mask = mask_to_greyscale_image(&dstMask);
Hal Canaryd12a6762017-05-26 17:01:16 -0400769 // PDF doesn't seem to allow masking vector graphics with an Image XObject.
770 // Must mask with a Form XObject.
Hal Canary51329c92017-06-27 14:28:37 -0400771 sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice();
Hal Canaryd12a6762017-05-26 17:01:16 -0400772 {
Herb Derbyefe39bc2018-05-01 17:06:20 -0400773 SkCanvas canvas(maskDevice);
Hal Canary51329c92017-06-27 14:28:37 -0400774 canvas.drawImage(mask, dstMaskBounds.x(), dstMaskBounds.y());
Hal Canaryd12a6762017-05-26 17:01:16 -0400775 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400776 if (!ctm.isIdentity() && paint->getShader()) {
777 transform_shader(paint.writable(), ctm); // Since we are using identity matrix.
778 }
Hal Canaryd892a9d2018-09-24 21:20:47 -0400779 ScopedContentEntry content(this, &clipStack, SkMatrix::I(), *paint);
Hal Canary42137de2018-10-08 16:00:37 -0400780 if (!content) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400781 return;
782 }
Hal Canary51329c92017-06-27 14:28:37 -0400783 this->addSMaskGraphicState(std::move(maskDevice), content.stream());
784 SkPDFUtils::AppendRectangle(SkRect::Make(dstMaskBounds), content.stream());
785 SkPDFUtils::PaintPath(SkPaint::kFill_Style, path.getFillType(), content.stream());
786 this->clearMaskOnGraphicState(content.stream());
787}
Hal Canaryd12a6762017-05-26 17:01:16 -0400788
Hal Canary9a3f5542018-12-10 19:59:07 -0500789void SkPDFDevice::setGraphicState(SkPDFIndirectReference gs, SkDynamicMemoryWStream* content) {
790 SkPDFUtils::ApplyGraphicState(add_resource(fGraphicStateResources, gs), content);
Hal Canary3b8b11e2018-09-29 22:31:34 -0400791}
792
Hal Canary51329c92017-06-27 14:28:37 -0400793void SkPDFDevice::addSMaskGraphicState(sk_sp<SkPDFDevice> maskDevice,
794 SkDynamicMemoryWStream* contentStream) {
Hal Canary3b8b11e2018-09-29 22:31:34 -0400795 this->setGraphicState(SkPDFGraphicState::GetSMaskGraphicState(
Hal Canaryb4bd5ef2017-07-26 09:16:01 -0400796 maskDevice->makeFormXObjectFromDevice(true), false,
Hal Canary9a3f5542018-12-10 19:59:07 -0500797 SkPDFGraphicState::kLuminosity_SMaskMode, fDocument), contentStream);
Hal Canary51329c92017-06-27 14:28:37 -0400798}
Hal Canaryd12a6762017-05-26 17:01:16 -0400799
Hal Canary51329c92017-06-27 14:28:37 -0400800void SkPDFDevice::clearMaskOnGraphicState(SkDynamicMemoryWStream* contentStream) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400801 // The no-softmask graphic state is used to "turn off" the mask for later draw calls.
Hal Canary9a3f5542018-12-10 19:59:07 -0500802 SkPDFIndirectReference& noSMaskGS = this->getCanon()->fNoSmaskGraphicState;
Hal Canaryc02de0b2017-06-28 13:14:03 -0400803 if (!noSMaskGS) {
Hal Canary9a3f5542018-12-10 19:59:07 -0500804 SkPDFDict tmp("ExtGState");
805 tmp.insertName("SMask", "None");
806 noSMaskGS = fDocument->emit(tmp);
Hal Canaryc02de0b2017-06-28 13:14:03 -0400807 }
Hal Canary3b8b11e2018-09-29 22:31:34 -0400808 this->setGraphicState(noSMaskGS, contentStream);
Hal Canaryd12a6762017-05-26 17:01:16 -0400809}
810
Mike Reeda1361362017-03-07 09:37:29 -0500811void SkPDFDevice::internalDrawPath(const SkClipStack& clipStack,
812 const SkMatrix& ctm,
813 const SkPath& origPath,
814 const SkPaint& srcPaint,
Mike Reeda1361362017-03-07 09:37:29 -0500815 bool pathIsMutable) {
Hal Canaryb4e528d2018-03-09 16:02:15 -0500816 if (clipStack.isEmpty(this->bounds())) {
817 return;
818 }
halcanarya6814332015-05-27 08:53:36 -0700819 SkPaint paint = srcPaint;
Hal Canaryd12a6762017-05-26 17:01:16 -0400820 remove_color_filter(&paint);
halcanarya6814332015-05-27 08:53:36 -0700821 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800822 SkPath modifiedPath;
823 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000824
Hal Canaryd12a6762017-05-26 17:01:16 -0400825 if (paint.getMaskFilter()) {
Robert Phillips137ca522018-08-15 10:14:33 -0400826 this->internalDrawPathWithFilter(clipStack, ctm, origPath, paint);
Hal Canaryd12a6762017-05-26 17:01:16 -0400827 return;
828 }
829
Mike Reeda1361362017-03-07 09:37:29 -0500830 SkMatrix matrix = ctm;
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000831
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000832 if (paint.getPathEffect()) {
Hal Canary22b2d8c2017-07-19 14:46:12 -0400833 if (clipStack.isEmpty(this->bounds())) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000834 return;
835 }
halcanary682ee012016-01-28 10:59:34 -0800836 if (!pathIsMutable) {
Hal Canaryd12a6762017-05-26 17:01:16 -0400837 modifiedPath = origPath;
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000838 pathPtr = &modifiedPath;
839 pathIsMutable = true;
840 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400841 if (paint.getFillPath(*pathPtr, pathPtr)) {
842 paint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000843 } else {
Hal Canaryd12a6762017-05-26 17:01:16 -0400844 paint.setStyle(SkPaint::kStroke_Style);
845 paint.setStrokeWidth(0);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000846 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400847 paint.setPathEffect(nullptr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000848 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000849
Robert Phillips137ca522018-08-15 10:14:33 -0400850 if (this->handleInversePath(*pathPtr, paint, pathIsMutable)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000851 return;
852 }
Hal Canaryd12a6762017-05-26 17:01:16 -0400853 if (matrix.getType() & SkMatrix::kPerspective_Mask) {
854 if (!pathIsMutable) {
855 modifiedPath = origPath;
856 pathPtr = &modifiedPath;
857 pathIsMutable = true;
858 }
859 pathPtr->transform(matrix);
860 if (paint.getShader()) {
861 transform_shader(&paint, matrix);
862 }
863 matrix = SkMatrix::I();
864 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000865
Hal Canaryd892a9d2018-09-24 21:20:47 -0400866 ScopedContentEntry content(this, &clipStack, matrix, paint);
Hal Canary42137de2018-10-08 16:00:37 -0400867 if (!content) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000868 return;
869 }
Hal Canarydfaa0572017-11-27 09:33:44 -0500870 constexpr SkScalar kToleranceScale = 0.0625f; // smaller = better conics (circles).
Hal Canary385468f2017-02-13 11:03:23 -0500871 SkScalar matrixScale = matrix.mapRadius(1.0f);
Hal Canarydfaa0572017-11-27 09:33:44 -0500872 SkScalar tolerance = matrixScale > 0.0f ? kToleranceScale / matrixScale : kToleranceScale;
halcanary8b2bc252015-10-06 09:41:47 -0700873 bool consumeDegeratePathSegments =
874 paint.getStyle() == SkPaint::kFill_Style ||
875 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
876 paint.getStrokeCap() != SkPaint::kSquare_Cap);
Hal Canary51329c92017-06-27 14:28:37 -0400877 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(), consumeDegeratePathSegments, content.stream(),
Hal Canary385468f2017-02-13 11:03:23 -0500878 tolerance);
Hal Canary51329c92017-06-27 14:28:37 -0400879 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(), content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000880}
881
Hal Canary7cbf5e32017-07-12 13:10:23 -0400882////////////////////////////////////////////////////////////////////////////////
Hal Canaryf50ff392016-09-30 10:25:39 -0400883
Mike Reeda1361362017-03-07 09:37:29 -0500884void SkPDFDevice::drawImageRect(const SkImage* image,
Hal Canaryf50ff392016-09-30 10:25:39 -0400885 const SkRect* src,
886 const SkRect& dst,
Hal Canary7cbf5e32017-07-12 13:10:23 -0400887 const SkPaint& paint,
Hal Canaryf50ff392016-09-30 10:25:39 -0400888 SkCanvas::SrcRectConstraint) {
Hal Canary7cbf5e32017-07-12 13:10:23 -0400889 SkASSERT(image);
890 this->internalDrawImageRect(SkKeyedImage(sk_ref_sp(const_cast<SkImage*>(image))),
891 src, dst, paint, this->ctm());
Hal Canaryf50ff392016-09-30 10:25:39 -0400892}
893
Hal Canary7cbf5e32017-07-12 13:10:23 -0400894void SkPDFDevice::drawBitmapRect(const SkBitmap& bm,
halcanary7a14b312015-10-01 07:28:13 -0700895 const SkRect* src,
896 const SkRect& dst,
Hal Canary7cbf5e32017-07-12 13:10:23 -0400897 const SkPaint& paint,
Mike Reeda1361362017-03-07 09:37:29 -0500898 SkCanvas::SrcRectConstraint) {
Hal Canary7cbf5e32017-07-12 13:10:23 -0400899 SkASSERT(!bm.drawsNothing());
900 this->internalDrawImageRect(SkKeyedImage(bm), src, dst, paint, this->ctm());
halcanary7a14b312015-10-01 07:28:13 -0700901}
902
Hal Canary7cbf5e32017-07-12 13:10:23 -0400903void SkPDFDevice::drawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y, const SkPaint& paint) {
904 SkASSERT(!bm.drawsNothing());
905 auto r = SkRect::MakeXYWH(x, y, bm.width(), bm.height());
906 this->internalDrawImageRect(SkKeyedImage(bm), nullptr, r, paint, this->ctm());
halcanary7a14b312015-10-01 07:28:13 -0700907}
908
Hal Canary7cbf5e32017-07-12 13:10:23 -0400909void SkPDFDevice::drawSprite(const SkBitmap& bm, int x, int y, const SkPaint& paint) {
910 SkASSERT(!bm.drawsNothing());
911 auto r = SkRect::MakeXYWH(x, y, bm.width(), bm.height());
912 this->internalDrawImageRect(SkKeyedImage(bm), nullptr, r, paint, SkMatrix::I());
halcanary7a14b312015-10-01 07:28:13 -0700913}
914
Hal Canary7cbf5e32017-07-12 13:10:23 -0400915void SkPDFDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) {
916 SkASSERT(image);
917 auto r = SkRect::MakeXYWH(x, y, image->width(), image->height());
918 this->internalDrawImageRect(SkKeyedImage(sk_ref_sp(const_cast<SkImage*>(image))),
919 nullptr, r, paint, this->ctm());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000920}
921
Hal Canary7cbf5e32017-07-12 13:10:23 -0400922////////////////////////////////////////////////////////////////////////////////
923
halcanaryf0c30f52016-07-15 13:35:45 -0700924namespace {
925class GlyphPositioner {
926public:
927 GlyphPositioner(SkDynamicMemoryWStream* content,
928 SkScalar textSkewX,
halcanary4ed2f012016-08-15 18:40:07 -0700929 SkPoint origin)
halcanaryf0c30f52016-07-15 13:35:45 -0700930 : fContent(content)
halcanaryc2f9ec12016-09-12 08:55:29 -0700931 , fCurrentMatrixOrigin(origin)
Hal Canary8ef78ea2018-10-01 13:38:30 -0400932 , fTextSkewX(textSkewX) {
halcanaryf0c30f52016-07-15 13:35:45 -0700933 }
halcanary4871f222016-08-26 13:17:44 -0700934 ~GlyphPositioner() { this->flush(); }
halcanaryf0c30f52016-07-15 13:35:45 -0700935 void flush() {
936 if (fInText) {
937 fContent->writeText("> Tj\n");
938 fInText = false;
939 }
940 }
Hal Canary8ef78ea2018-10-01 13:38:30 -0400941 void setWideChars(bool wide) {
942 this->flush();
943 fWideChars = wide;
944 }
halcanary4871f222016-08-26 13:17:44 -0700945 void writeGlyph(SkPoint xy,
halcanaryf0c30f52016-07-15 13:35:45 -0700946 SkScalar advanceWidth,
947 uint16_t glyph) {
halcanaryc2f9ec12016-09-12 08:55:29 -0700948 if (!fInitialized) {
949 // Flip the text about the x-axis to account for origin swap and include
950 // the passed parameters.
951 fContent->writeText("1 0 ");
952 SkPDFUtils::AppendScalar(-fTextSkewX, fContent);
953 fContent->writeText(" -1 ");
954 SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.x(), fContent);
955 fContent->writeText(" ");
956 SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.y(), fContent);
957 fContent->writeText(" Tm\n");
958 fCurrentMatrixOrigin.set(0.0f, 0.0f);
959 fInitialized = true;
960 }
Hal Canary98caedd2018-07-23 10:50:49 -0400961 SkPoint position = xy - fCurrentMatrixOrigin;
962 if (position != SkPoint{fXAdvance, 0}) {
963 this->flush();
964 SkPDFUtils::AppendScalar(position.x() - position.y() * fTextSkewX, fContent);
965 fContent->writeText(" ");
966 SkPDFUtils::AppendScalar(-position.y(), fContent);
967 fContent->writeText(" Td ");
968 fCurrentMatrixOrigin = xy;
969 fXAdvance = 0;
halcanaryf0c30f52016-07-15 13:35:45 -0700970 }
Hal Canary98caedd2018-07-23 10:50:49 -0400971 fXAdvance += advanceWidth;
halcanaryf0c30f52016-07-15 13:35:45 -0700972 if (!fInText) {
973 fContent->writeText("<");
974 fInText = true;
975 }
976 if (fWideChars) {
977 SkPDFUtils::WriteUInt16BE(fContent, glyph);
978 } else {
979 SkASSERT(0 == glyph >> 8);
980 SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph));
981 }
halcanaryf0c30f52016-07-15 13:35:45 -0700982 }
983
984private:
985 SkDynamicMemoryWStream* fContent;
halcanary4871f222016-08-26 13:17:44 -0700986 SkPoint fCurrentMatrixOrigin;
halcanaryc2f9ec12016-09-12 08:55:29 -0700987 SkScalar fXAdvance = 0.0f;
988 SkScalar fTextSkewX;
Hal Canary8ef78ea2018-10-01 13:38:30 -0400989 bool fWideChars = true;
halcanaryc2f9ec12016-09-12 08:55:29 -0700990 bool fInText = false;
991 bool fInitialized = false;
halcanaryf0c30f52016-07-15 13:35:45 -0700992};
993} // namespace
994
Hal Canary46cc3da2018-05-09 11:50:34 -0400995static SkUnichar map_glyph(const std::vector<SkUnichar>& glyphToUnicode, SkGlyphID glyph) {
996 return glyph < glyphToUnicode.size() ? glyphToUnicode[SkToInt(glyph)] : -1;
halcanaryf59d18a2016-09-16 14:44:57 -0700997}
998
Hal Canary575be302018-09-28 15:01:14 -0400999namespace {
1000struct PositionedGlyph {
1001 SkPoint fPos;
1002 SkGlyphID fGlyph;
1003};
Hal Canaryd12a6762017-05-26 17:01:16 -04001004}
1005
Hal Canary699b8732017-06-13 12:13:29 -04001006static SkRect get_glyph_bounds_device_space(SkGlyphID gid, SkGlyphCache* cache,
1007 SkScalar xScale, SkScalar yScale,
1008 SkPoint xy, const SkMatrix& ctm) {
1009 const SkGlyph& glyph = cache->getGlyphIDMetrics(gid);
1010 SkRect glyphBounds = {glyph.fLeft * xScale,
1011 glyph.fTop * yScale,
1012 (glyph.fLeft + glyph.fWidth) * xScale,
1013 (glyph.fTop + glyph.fHeight) * yScale};
1014 glyphBounds.offset(xy);
1015 ctm.mapRect(&glyphBounds); // now in dev space.
1016 return glyphBounds;
1017}
1018
1019static bool contains(const SkRect& r, SkPoint p) {
1020 return r.left() <= p.x() && p.x() <= r.right() &&
1021 r.top() <= p.y() && p.y() <= r.bottom();
1022}
1023
Herb Derby95e17602018-12-06 17:11:43 -05001024void SkPDFDevice::drawGlyphRunAsPath(
1025 const SkGlyphRun& glyphRun, SkPoint offset, const SkPaint& runPaint) {
1026 SkPaint paint{runPaint};
1027 glyphRun.font().LEGACY_applyToPaint(&paint);
1028 SkFont font = glyphRun.font();
Hal Canary575be302018-09-28 15:01:14 -04001029 SkPath path;
Herb Derby4fce0832018-10-02 11:04:29 -04001030
Mike Reedab8f2972018-12-05 13:20:29 -05001031 struct Rec {
1032 SkPath* fPath;
1033 SkPoint fOffset;
1034 const SkPoint* fPos;
1035 } rec = {&path, offset, glyphRun.positions().data()};
1036
1037 font.getPaths(glyphRun.glyphsIDs().data(), glyphRun.glyphsIDs().size(),
1038 [](const SkPath* path, const SkMatrix& mx, void* ctx) {
1039 Rec* rec = reinterpret_cast<Rec*>(ctx);
1040 if (path) {
1041 SkMatrix total = mx;
1042 total.postTranslate(rec->fPos->fX + rec->fOffset.fX,
1043 rec->fPos->fY + rec->fOffset.fY);
1044 rec->fPath->addPath(*path, total);
1045 }
1046 rec->fPos += 1; // move to the next glyph's position
1047 }, &rec);
Hal Canary575be302018-09-28 15:01:14 -04001048 this->drawPath(path, paint, true);
1049
Herb Derby95e17602018-12-06 17:11:43 -05001050 SkFont transparentFont = glyphRun.font();
1051 transparentFont.setEmbolden(false); // Stop Recursion
1052 SkGlyphRun tmpGlyphRun(glyphRun, transparentFont);
1053
Herb Derby9bb6b7e2018-11-02 13:57:58 -04001054 SkPaint transparent;
Herb Derby9bb6b7e2018-11-02 13:57:58 -04001055 transparent.setColor(SK_ColorTRANSPARENT);
Herb Derby9bb6b7e2018-11-02 13:57:58 -04001056
Hal Canary575be302018-09-28 15:01:14 -04001057 if (this->ctm().hasPerspective()) {
1058 SkMatrix prevCTM = this->ctm();
1059 this->setCTM(SkMatrix::I());
Herb Derby95e17602018-12-06 17:11:43 -05001060 this->internalDrawGlyphRun(tmpGlyphRun, offset, transparent);
Hal Canary575be302018-09-28 15:01:14 -04001061 this->setCTM(prevCTM);
1062 } else {
Herb Derby95e17602018-12-06 17:11:43 -05001063 this->internalDrawGlyphRun(tmpGlyphRun, offset, transparent);
Hal Canary575be302018-09-28 15:01:14 -04001064 }
Hal Canary575be302018-09-28 15:01:14 -04001065}
1066
Hal Canary688afdd2018-10-17 14:57:53 -04001067static bool needs_new_font(SkPDFFont* font, SkGlyphID gid, SkGlyphCache* cache,
1068 SkAdvancedTypefaceMetrics::FontType fontType) {
1069 if (!font || !font->hasGlyph(gid)) {
1070 return true;
1071 }
1072 if (fontType == SkAdvancedTypefaceMetrics::kOther_Font) {
1073 return false;
1074 }
1075 const SkGlyph& glyph = cache->getGlyphIDMetrics(gid);
1076 if (glyph.isEmpty()) {
1077 return false;
1078 }
1079
1080 bool bitmapOnly = nullptr == cache->findPath(glyph);
1081 bool convertedToType3 = (font->getType() == SkAdvancedTypefaceMetrics::kOther_Font);
1082 return convertedToType3 != bitmapOnly;
1083}
1084
Herb Derby95e17602018-12-06 17:11:43 -05001085void SkPDFDevice::internalDrawGlyphRun(
1086 const SkGlyphRun& glyphRun, SkPoint offset, const SkPaint& runPaint) {
Hal Canary98caedd2018-07-23 10:50:49 -04001087
Herb Derbyaedc9d22018-10-25 12:27:07 -04001088 const SkGlyphID* glyphs = glyphRun.glyphsIDs().data();
1089 uint32_t glyphCount = SkToU32(glyphRun.glyphsIDs().size());
Herb Derby95e17602018-12-06 17:11:43 -05001090 SkPaint srcPaint{runPaint};
1091 glyphRun.font().LEGACY_applyToPaint(&srcPaint);
Mike Reed97f3cc22018-12-03 09:45:17 -05001092 srcPaint.setTextEncoding(kGlyphID_SkTextEncoding);
Herb Derby4fce0832018-10-02 11:04:29 -04001093
Hal Canary98caedd2018-07-23 10:50:49 -04001094 if (!glyphCount || !glyphs || srcPaint.getTextSize() <= 0 || this->hasEmptyClip()) {
Hal Canaryd12a6762017-05-26 17:01:16 -04001095 return;
1096 }
Hal Canaryd12a6762017-05-26 17:01:16 -04001097 if (srcPaint.getPathEffect()
Hal Canary98caedd2018-07-23 10:50:49 -04001098 || srcPaint.getMaskFilter()
Hal Canary575be302018-09-28 15:01:14 -04001099 || srcPaint.isFakeBoldText()
Hal Canary92c500b2018-09-19 16:19:45 -04001100 || this->ctm().hasPerspective()
Hal Canary98caedd2018-07-23 10:50:49 -04001101 || SkPaint::kFill_Style != srcPaint.getStyle()) {
Hal Canaryd12a6762017-05-26 17:01:16 -04001102 // Stroked Text doesn't work well with Type3 fonts.
Herb Derby95e17602018-12-06 17:11:43 -05001103 this->drawGlyphRunAsPath(glyphRun, offset, runPaint);
Hal Canarye9a29c42018-12-07 13:44:26 -05001104 return;
halcanarye06ca962016-09-09 05:34:55 -07001105 }
Hal Canary575be302018-09-28 15:01:14 -04001106 SkPaint paint(srcPaint);
Hal Canaryd12a6762017-05-26 17:01:16 -04001107 remove_color_filter(&paint);
halcanary4ed2f012016-08-15 18:40:07 -07001108 replace_srcmode_on_opaque_paint(&paint);
Mike Reed9edbf422018-11-07 19:54:33 -05001109 paint.setHinting(kNo_SkFontHinting);
halcanary4ed2f012016-08-15 18:40:07 -07001110 if (!paint.getTypeface()) {
1111 paint.setTypeface(SkTypeface::MakeDefault());
1112 }
1113 SkTypeface* typeface = paint.getTypeface();
1114 if (!typeface) {
1115 SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n");
1116 return;
1117 }
Hal Canary98caedd2018-07-23 10:50:49 -04001118 const SkAdvancedTypefaceMetrics* metrics = SkPDFFont::GetMetrics(typeface, fDocument->canon());
halcanary4871f222016-08-26 13:17:44 -07001119 if (!metrics) {
halcanary4ed2f012016-08-15 18:40:07 -07001120 return;
1121 }
Hal Canary688afdd2018-10-17 14:57:53 -04001122 SkAdvancedTypefaceMetrics::FontType fontType = SkPDFFont::FontType(*metrics);
1123
Hal Canary46cc3da2018-05-09 11:50:34 -04001124 const std::vector<SkUnichar>& glyphToUnicode = SkPDFFont::GetUnicodeMap(
1125 typeface, fDocument->canon());
1126
Hal Canary98caedd2018-07-23 10:50:49 -04001127 SkClusterator clusterator(glyphRun);
Hal Canaryaa3af7b2017-03-06 16:18:49 -05001128
1129 int emSize;
Herb Derby1a605cd2018-03-22 11:16:25 -04001130 auto glyphCache = SkPDFFont::MakeVectorCache(typeface, &emSize);
Hal Canaryaa3af7b2017-03-06 16:18:49 -05001131
1132 SkScalar textSize = paint.getTextSize();
1133 SkScalar advanceScale = textSize * paint.getTextScaleX() / emSize;
halcanary4ed2f012016-08-15 18:40:07 -07001134
Hal Canary699b8732017-06-13 12:13:29 -04001135 // textScaleX and textScaleY are used to get a conservative bounding box for glyphs.
1136 SkScalar textScaleY = textSize / emSize;
1137 SkScalar textScaleX = advanceScale + paint.getTextSkewX() * textScaleY;
1138
Hal Canary22b2d8c2017-07-19 14:46:12 -04001139 SkRect clipStackBounds = this->cs().bounds(this->bounds());
Hal Canary9b9510a2017-07-18 09:39:00 -04001140 {
1141 ScopedContentEntry content(this, paint, true);
Hal Canary42137de2018-10-08 16:00:37 -04001142 if (!content) {
Hal Canary9b9510a2017-07-18 09:39:00 -04001143 return;
robertphillips5ba165e2016-08-15 15:36:58 -07001144 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001145 SkDynamicMemoryWStream* out = content.stream();
Hal Canary9b9510a2017-07-18 09:39:00 -04001146
1147 out->writeText("BT\n");
Dominic Mazzoni656cefe2018-09-25 20:29:15 -07001148
1149 int markId = -1;
1150 if (fNodeId) {
1151 markId = fDocument->getMarkIdForNodeId(fNodeId);
1152 }
1153
1154 if (markId != -1) {
1155 out->writeText("/P <</MCID ");
1156 out->writeDecAsText(markId);
1157 out->writeText(" >>BDC\n");
1158 }
1159 SK_AT_SCOPE_EXIT(if (markId != -1) out->writeText("EMC\n"));
1160
Hal Canary9b9510a2017-07-18 09:39:00 -04001161 SK_AT_SCOPE_EXIT(out->writeText("ET\n"));
1162
1163 const SkGlyphID maxGlyphID = SkToU16(typeface->countGlyphs() - 1);
1164
Hal Canary9b9510a2017-07-18 09:39:00 -04001165 if (clusterator.reversedChars()) {
1166 out->writeText("/ReversedChars BMC\n");
1167 }
1168 SK_AT_SCOPE_EXIT(if (clusterator.reversedChars()) { out->writeText("EMC\n"); } );
Hal Canary8ef78ea2018-10-01 13:38:30 -04001169 GlyphPositioner glyphPositioner(out, paint.getTextSkewX(), offset);
Hal Canary9b9510a2017-07-18 09:39:00 -04001170 SkPDFFont* font = nullptr;
1171
Hal Canary1521c8a2018-03-28 09:51:00 -07001172 while (SkClusterator::Cluster c = clusterator.next()) {
Hal Canary9b9510a2017-07-18 09:39:00 -04001173 int index = c.fGlyphIndex;
1174 int glyphLimit = index + c.fGlyphCount;
1175
1176 bool actualText = false;
1177 SK_AT_SCOPE_EXIT(if (actualText) {
1178 glyphPositioner.flush();
1179 out->writeText("EMC\n");
1180 });
1181 if (c.fUtf8Text) { // real cluster
1182 // Check if `/ActualText` needed.
1183 const char* textPtr = c.fUtf8Text;
1184 const char* textEnd = c.fUtf8Text + c.fTextByteLength;
Hal Canaryf107a2f2018-07-25 16:52:48 -04001185 SkUnichar unichar = SkUTF::NextUTF8(&textPtr, textEnd);
Hal Canary9b9510a2017-07-18 09:39:00 -04001186 if (unichar < 0) {
halcanaryf59d18a2016-09-16 14:44:57 -07001187 return;
1188 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001189 if (textPtr < textEnd || // more characters left
1190 glyphLimit > index + 1 || // toUnicode wouldn't work
1191 unichar != map_glyph(glyphToUnicode, glyphs[index])) // test single Unichar map
1192 {
1193 glyphPositioner.flush();
1194 out->writeText("/Span<</ActualText <");
1195 SkPDFUtils::WriteUTF16beHex(out, 0xFEFF); // U+FEFF = BYTE ORDER MARK
1196 // the BOM marks this text as UTF-16BE, not PDFDocEncoding.
1197 SkPDFUtils::WriteUTF16beHex(out, unichar); // first char
1198 while (textPtr < textEnd) {
Hal Canaryf107a2f2018-07-25 16:52:48 -04001199 unichar = SkUTF::NextUTF8(&textPtr, textEnd);
Hal Canary9b9510a2017-07-18 09:39:00 -04001200 if (unichar < 0) {
1201 break;
1202 }
1203 SkPDFUtils::WriteUTF16beHex(out, unichar);
1204 }
1205 out->writeText("> >> BDC\n"); // begin marked-content sequence
1206 // with an associated property list.
1207 actualText = true;
halcanaryf59d18a2016-09-16 14:44:57 -07001208 }
halcanaryf59d18a2016-09-16 14:44:57 -07001209 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001210 for (; index < glyphLimit; ++index) {
1211 SkGlyphID gid = glyphs[index];
1212 if (gid > maxGlyphID) {
1213 continue;
halcanaryf59d18a2016-09-16 14:44:57 -07001214 }
Hal Canary688afdd2018-10-17 14:57:53 -04001215 SkPoint xy = glyphRun.positions()[index];
1216 // Do a glyph-by-glyph bounds-reject if positions are absolute.
1217 SkRect glyphBounds = get_glyph_bounds_device_space(
1218 gid, glyphCache.get(), textScaleX, textScaleY,
1219 xy + offset, this->ctm());
1220 if (glyphBounds.isEmpty()) {
1221 if (!contains(clipStackBounds, {glyphBounds.x(), glyphBounds.y()})) {
1222 continue;
1223 }
1224 } else {
1225 if (!clipStackBounds.intersects(glyphBounds)) {
1226 continue; // reject glyphs as out of bounds
1227 }
1228 }
1229 if (needs_new_font(font, gid, glyphCache.get(), fontType)) {
Hal Canary9b9510a2017-07-18 09:39:00 -04001230 // Not yet specified font or need to switch font.
Hal Canaryb10f92e2018-11-16 17:01:50 -05001231 font = SkPDFFont::GetFontResource(fDocument, glyphCache.get(), typeface, gid);
1232 SkASSERT(font); // All preconditions for SkPDFFont::GetFontResource are met.
Hal Canary9b9510a2017-07-18 09:39:00 -04001233 glyphPositioner.flush();
Hal Canary8ef78ea2018-10-01 13:38:30 -04001234 glyphPositioner.setWideChars(font->multiByteGlyphs());
Hal Canary9a3f5542018-12-10 19:59:07 -05001235 SkPDFWriteResourceName(out, SkPDFResourceType::kFont,
1236 add_resource(fFontResources, font->indirectReference()));
Hal Canarye650b852018-09-12 09:12:36 -04001237 out->writeText(" ");
1238 SkPDFUtils::AppendScalar(textSize, out);
1239 out->writeText(" Tf\n");
1240
Hal Canary9b9510a2017-07-18 09:39:00 -04001241 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001242 font->noteGlyphUsage(gid);
Hal Canary8ef78ea2018-10-01 13:38:30 -04001243 SkGlyphID encodedGlyph = font->multiByteGlyphs()
1244 ? gid : font->glyphToPDFFontEncoding(gid);
Hal Canary98caedd2018-07-23 10:50:49 -04001245 SkScalar advance = advanceScale * glyphCache->getGlyphIDAdvance(gid).fAdvanceX;
Hal Canary9b9510a2017-07-18 09:39:00 -04001246 glyphPositioner.writeGlyph(xy, advance, encodedGlyph);
halcanaryf59d18a2016-09-16 14:44:57 -07001247 }
Hal Canary9b9510a2017-07-18 09:39:00 -04001248 }
1249 }
halcanary4ed2f012016-08-15 18:40:07 -07001250}
1251
Herb Derbyb935cf82018-07-26 16:54:18 -04001252void SkPDFDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
1253 for (const SkGlyphRun& glyphRun : glyphRunList) {
Herb Derby95e17602018-12-06 17:11:43 -05001254 this->internalDrawGlyphRun(glyphRun, glyphRunList.origin(), glyphRunList.paint());
halcanarye06ca962016-09-09 05:34:55 -07001255 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001256}
1257
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001258void SkPDFDevice::drawVertices(const SkVertices*, const SkVertices::Bone[], int, SkBlendMode,
Ruiqi Maof5101492018-06-29 14:32:21 -04001259 const SkPaint&) {
Hal Canaryb4e528d2018-03-09 16:02:15 -05001260 if (this->hasEmptyClip()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001261 return;
1262 }
reed@google.com85e143c2013-12-30 15:51:25 +00001263 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001264}
1265
Hal Canary9a3f5542018-12-10 19:59:07 -05001266void SkPDFDevice::drawFormXObject(SkPDFIndirectReference xObject, SkDynamicMemoryWStream* content) {
1267 SkASSERT(xObject);
Hal Canary3b8b11e2018-09-29 22:31:34 -04001268 SkPDFWriteResourceName(content, SkPDFResourceType::kXObject,
Hal Canary9a3f5542018-12-10 19:59:07 -05001269 add_resource(fXObjectResources, xObject));
Hal Canarye650b852018-09-12 09:12:36 -04001270 content->writeText(" Do\n");
1271}
1272
Mike Reeda1361362017-03-07 09:37:29 -05001273void SkPDFDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001274 SkASSERT(!paint.getImageFilter());
1275
reed7503d602016-07-15 14:23:29 -07001276 // Check if the source device is really a bitmapdevice (because that's what we returned
1277 // from createDevice (likely due to an imagefilter)
1278 SkPixmap pmap;
1279 if (device->peekPixels(&pmap)) {
1280 SkBitmap bitmap;
1281 bitmap.installPixels(pmap);
Mike Reeda1361362017-03-07 09:37:29 -05001282 this->drawSprite(bitmap, x, y, paint);
reed7503d602016-07-15 14:23:29 -07001283 return;
1284 }
1285
fmalita6987dca2014-11-13 08:33:37 -08001286 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001287 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001288
1289 SkScalar scalarX = SkIntToScalar(x);
1290 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001291 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1292 SkRect r = l.rect.makeOffset(scalarX, scalarY);
Hal Canary5c1b3602017-04-17 16:30:06 -04001293 fLinkToURLs.emplace_back(RectWithData{r, l.data});
wangxianzhuef6c50a2015-09-17 20:38:02 -07001294 }
halcanary91fcb3e2016-03-04 13:53:22 -08001295 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1296 SkRect r = l.rect.makeOffset(scalarX, scalarY);
Hal Canary5c1b3602017-04-17 16:30:06 -04001297 fLinkToDestinations.emplace_back(RectWithData{r, l.data});
wangxianzhuef6c50a2015-09-17 20:38:02 -07001298 }
halcanary91fcb3e2016-03-04 13:53:22 -08001299 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1300 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
Hal Canary5c1b3602017-04-17 16:30:06 -04001301 fNamedDestinations.emplace_back(NamedDestination{d.nameData, p});
wangxianzhuef6c50a2015-09-17 20:38:02 -07001302 }
1303
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001304 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001305 return;
1306 }
1307
Mike Reeda1361362017-03-07 09:37:29 -05001308 SkMatrix matrix = SkMatrix::MakeTrans(SkIntToScalar(x), SkIntToScalar(y));
Hal Canaryd892a9d2018-09-24 21:20:47 -04001309 ScopedContentEntry content(this, &this->cs(), matrix, paint);
Hal Canary42137de2018-10-08 16:00:37 -04001310 if (!content) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001311 return;
1312 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001313 if (content.needShape()) {
Hal Canaryd791a6f2018-09-28 12:14:08 -04001314 SkISize dim = device->imageInfo().dimensions();
1315 content.setShape(to_path(SkRect::Make(SkIRect::MakeXYWH(x, y, dim.width(), dim.height()))));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001316 }
1317 if (!content.needSource()) {
1318 return;
1319 }
Hal Canary3b8b11e2018-09-29 22:31:34 -04001320 this->drawFormXObject(pdfDevice->makeFormXObjectFromDevice(), content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001321}
1322
reede8f30622016-03-23 18:59:25 -07001323sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1324 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001325}
1326
Hal Canary9a3f5542018-12-10 19:59:07 -05001327static std::vector<SkPDFIndirectReference> sort(const SkTHashSet<SkPDFIndirectReference>& src) {
1328 std::vector<SkPDFIndirectReference> dst;
1329 dst.reserve(src.count());
1330 src.foreach([&dst](SkPDFIndirectReference ref) { dst.push_back(ref); } );
1331 std::sort(dst.begin(), dst.end(),
1332 [](SkPDFIndirectReference a, SkPDFIndirectReference b) { return a.fValue < b.fValue; });
1333 return dst;
1334}
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001335
Hal Canary74801582018-12-18 16:30:41 -05001336std::unique_ptr<SkPDFDict> SkPDFDevice::makeResourceDict() {
Hal Canary9a3f5542018-12-10 19:59:07 -05001337 return SkPDFMakeResourceDict(sort(fGraphicStateResources),
1338 sort(fShaderResources),
1339 sort(fXObjectResources),
1340 sort(fFontResources));
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001341}
1342
Hal Canaryb400d4d2018-09-26 16:33:52 -04001343std::unique_ptr<SkStreamAsset> SkPDFDevice::content() {
1344 if (fActiveStackState.fContentStream) {
1345 fActiveStackState.drainStack();
1346 fActiveStackState = GraphicStackState();
1347 }
Hal Canary42137de2018-10-08 16:00:37 -04001348 if (fContent.bytesWritten() == 0) {
1349 return skstd::make_unique<SkMemoryStream>();
1350 }
halcanary334fcbc2015-02-24 12:56:16 -08001351 SkDynamicMemoryWStream buffer;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001352 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
Hal Canarye650b852018-09-12 09:12:36 -04001353 append_transform(fInitialTransform, &buffer);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001354 }
Hal Canary42137de2018-10-08 16:00:37 -04001355 if (fNeedsExtraSave) {
1356 buffer.writeText("q\n");
halcanary2be7e012016-03-28 11:58:08 -07001357 }
Hal Canary42137de2018-10-08 16:00:37 -04001358 fContent.writeToAndReset(&buffer);
1359 if (fNeedsExtraSave) {
1360 buffer.writeText("Q\n");
halcanary022c2bd2016-09-02 11:29:46 -07001361 }
Hal Canary42137de2018-10-08 16:00:37 -04001362 fNeedsExtraSave = false;
1363 return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001364}
1365
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001366/* Draws an inverse filled path by using Path Ops to compute the positive
1367 * inverse using the current clip as the inverse bounds.
1368 * Return true if this was an inverse path and was properly handled,
1369 * otherwise returns false and the normal drawing routine should continue,
1370 * either as a (incorrect) fallback or because the path was not inverse
1371 * in the first place.
1372 */
Mike Reeda1361362017-03-07 09:37:29 -05001373bool SkPDFDevice::handleInversePath(const SkPath& origPath,
Robert Phillips137ca522018-08-15 10:14:33 -04001374 const SkPaint& paint,
1375 bool pathIsMutable) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001376 if (!origPath.isInverseFillType()) {
1377 return false;
1378 }
1379
Hal Canaryb4e528d2018-03-09 16:02:15 -05001380 if (this->hasEmptyClip()) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001381 return false;
1382 }
1383
1384 SkPath modifiedPath;
1385 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1386 SkPaint noInversePaint(paint);
1387
1388 // Merge stroking operations into final path.
1389 if (SkPaint::kStroke_Style == paint.getStyle() ||
1390 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1391 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1392 if (doFillPath) {
1393 noInversePaint.setStyle(SkPaint::kFill_Style);
1394 noInversePaint.setStrokeWidth(0);
1395 pathPtr = &modifiedPath;
1396 } else {
1397 // To be consistent with the raster output, hairline strokes
1398 // are rendered as non-inverted.
1399 modifiedPath.toggleInverseFillType();
Robert Phillips137ca522018-08-15 10:14:33 -04001400 this->drawPath(modifiedPath, paint, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001401 return true;
1402 }
1403 }
1404
1405 // Get bounds of clip in current transform space
1406 // (clip bounds are given in device space).
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001407 SkMatrix transformInverse;
Mike Reeda1361362017-03-07 09:37:29 -05001408 SkMatrix totalMatrix = this->ctm();
Robert Phillips137ca522018-08-15 10:14:33 -04001409
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001410 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001411 return false;
1412 }
Hal Canary22b2d8c2017-07-19 14:46:12 -04001413 SkRect bounds = this->cs().bounds(this->bounds());
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001414 transformInverse.mapRect(&bounds);
1415
1416 // Extend the bounds by the line width (plus some padding)
1417 // so the edge doesn't cause a visible stroke.
1418 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1419 paint.getStrokeWidth() + SK_Scalar1);
1420
1421 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1422 return false;
1423 }
1424
Robert Phillips137ca522018-08-15 10:14:33 -04001425 this->drawPath(modifiedPath, noInversePaint, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001426 return true;
1427}
1428
Hal Canary74801582018-12-18 16:30:41 -05001429std::unique_ptr<SkPDFArray> SkPDFDevice::getAnnotations() {
1430 std::unique_ptr<SkPDFArray> array;
Hal Canary9e41c212018-09-03 12:00:23 -04001431 size_t count = fLinkToURLs.size() + fLinkToDestinations.size();
1432 if (0 == count) {
1433 return array;
1434 }
Hal Canary74801582018-12-18 16:30:41 -05001435 array = SkPDFMakeArray();
Hal Canary9e41c212018-09-03 12:00:23 -04001436 array->reserve(count);
halcanary91fcb3e2016-03-04 13:53:22 -08001437 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001438 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001439 fInitialTransform.mapRect(&r, rectWithURL.rect);
Hal Canary9a3f5542018-12-10 19:59:07 -05001440 array->appendRef(fDocument->emit(*create_link_to_url(rectWithURL.data.get(), r)));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001441 }
halcanary91fcb3e2016-03-04 13:53:22 -08001442 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001443 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001444 fInitialTransform.mapRect(&r, linkToDestination.rect);
Hal Canary9a3f5542018-12-10 19:59:07 -05001445 array->appendRef(
1446 fDocument->emit(*create_link_named_dest(linkToDestination.data.get(), r)));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001447 }
Hal Canary9e41c212018-09-03 12:00:23 -04001448 return array;
wangxianzhuef6c50a2015-09-17 20:38:02 -07001449}
epoger@google.comb58772f2013-03-08 09:09:10 +00001450
Hal Canaryfba5b6c2018-12-18 12:55:18 -05001451void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFIndirectReference page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001452 for (const NamedDestination& dest : fNamedDestinations) {
Hal Canaryfba5b6c2018-12-18 12:55:18 -05001453 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
Hal Canary74801582018-12-18 16:30:41 -05001454 auto pdfDest = SkPDFMakeArray();
epoger@google.comb58772f2013-03-08 09:09:10 +00001455 pdfDest->reserve(5);
Hal Canaryfba5b6c2018-12-18 12:55:18 -05001456 pdfDest->appendRef(page);
epoger@google.comb58772f2013-03-08 09:09:10 +00001457 pdfDest->appendName("XYZ");
wangxianzhuef6c50a2015-09-17 20:38:02 -07001458 pdfDest->appendScalar(p.x());
1459 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001460 pdfDest->appendInt(0); // Leave zoom unchanged
Hal Canaryfba5b6c2018-12-18 12:55:18 -05001461 dict->insertObject(SkString(static_cast<const char*>(dest.nameData->data())),
1462 std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001463 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001464}
1465
Hal Canary9a3f5542018-12-10 19:59:07 -05001466SkPDFIndirectReference SkPDFDevice::makeFormXObjectFromDevice(bool alpha) {
halcanary5abbb442016-07-29 08:41:33 -07001467 SkMatrix inverseTransform = SkMatrix::I();
halcanaryafdc1772016-08-23 09:02:12 -07001468 if (!fInitialTransform.isIdentity()) {
1469 if (!fInitialTransform.invert(&inverseTransform)) {
halcanary5abbb442016-07-29 08:41:33 -07001470 SkDEBUGFAIL("Layer initial transform should be invertible.");
1471 inverseTransform.reset();
1472 }
1473 }
Hal Canaryb4bd5ef2017-07-26 09:16:01 -04001474 const char* colorSpace = alpha ? "DeviceGray" : nullptr;
Hal Canarye650b852018-09-12 09:12:36 -04001475
Hal Canary9a3f5542018-12-10 19:59:07 -05001476 SkPDFIndirectReference xobject =
1477 SkPDFMakeFormXObject(fDocument, this->content(),
Hal Canarye650b852018-09-12 09:12:36 -04001478 SkPDFMakeArray(0, 0, this->width(), this->height()),
Hal Canaryb4bd5ef2017-07-26 09:16:01 -04001479 this->makeResourceDict(), inverseTransform, colorSpace);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001480 // We always draw the form xobjects that we create back into the device, so
1481 // we simply preserve the font usage instead of pulling it out and merging
1482 // it back in later.
Hal Canary9e41c212018-09-03 12:00:23 -04001483 this->reset();
reed@google.comfc641d02012-09-20 17:52:20 +00001484 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001485}
1486
Hal Canary9a3f5542018-12-10 19:59:07 -05001487void SkPDFDevice::drawFormXObjectWithMask(SkPDFIndirectReference xObject,
1488 SkPDFIndirectReference sMask,
reed374772b2016-10-05 17:33:02 -07001489 SkBlendMode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001490 bool invertClip) {
Hal Canary9a3f5542018-12-10 19:59:07 -05001491 SkASSERT(sMask);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001492 SkPaint paint;
reed374772b2016-10-05 17:33:02 -07001493 paint.setBlendMode(mode);
Hal Canaryd892a9d2018-09-24 21:20:47 -04001494 ScopedContentEntry content(this, nullptr, SkMatrix::I(), paint);
Hal Canary42137de2018-10-08 16:00:37 -04001495 if (!content) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001496 return;
1497 }
Hal Canary3b8b11e2018-09-29 22:31:34 -04001498 this->setGraphicState(SkPDFGraphicState::GetSMaskGraphicState(
Hal Canary9a3f5542018-12-10 19:59:07 -05001499 sMask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode,
1500 fDocument), content.stream());
1501 this->drawFormXObject(xObject, content.stream());
Hal Canaryc02de0b2017-06-28 13:14:03 -04001502 this->clearMaskOnGraphicState(content.stream());
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001503}
1504
Hal Canaryb400d4d2018-09-26 16:33:52 -04001505
1506static bool treat_as_regular_pdf_blend_mode(SkBlendMode blendMode) {
1507 return nullptr != SkPDFUtils::BlendModeName(blendMode);
1508}
1509
1510SkDynamicMemoryWStream* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
1511 const SkMatrix& matrix,
1512 const SkPaint& paint,
1513 bool hasText,
Hal Canary9a3f5542018-12-10 19:59:07 -05001514 SkPDFIndirectReference* dst) {
1515 SkASSERT(!*dst);
reed374772b2016-10-05 17:33:02 -07001516 SkBlendMode blendMode = paint.getBlendMode();
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001517
Hal Canaryb400d4d2018-09-26 16:33:52 -04001518 // Dst xfer mode doesn't draw source at all.
1519 if (blendMode == SkBlendMode::kDst) {
1520 return nullptr;
1521 }
1522
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001523 // For the following modes, we want to handle source and destination
1524 // separately, so make an object of what's already there.
Hal Canaryb400d4d2018-09-26 16:33:52 -04001525 if (!treat_as_regular_pdf_blend_mode(blendMode) && blendMode != SkBlendMode::kDstOver) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001526 if (!isContentEmpty()) {
halcanarydabd4f02016-08-03 11:16:56 -07001527 *dst = this->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001528 SkASSERT(isContentEmpty());
reed374772b2016-10-05 17:33:02 -07001529 } else if (blendMode != SkBlendMode::kSrc &&
1530 blendMode != SkBlendMode::kSrcOut) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001531 // Except for Src and SrcOut, if there isn't anything already there,
1532 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001533 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001534 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001535 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001536 // TODO(vandebo): Figure out how/if we can handle the following modes:
Hal Canaryb400d4d2018-09-26 16:33:52 -04001537 // Xor, Plus. For now, we treat them as SrcOver/Normal.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001538
Hal Canaryb400d4d2018-09-26 16:33:52 -04001539 if (treat_as_regular_pdf_blend_mode(blendMode)) {
1540 if (!fActiveStackState.fContentStream) {
Hal Canary42137de2018-10-08 16:00:37 -04001541 if (fContent.bytesWritten() != 0) {
1542 fContent.writeText("Q\nq\n");
1543 fNeedsExtraSave = true;
1544 }
1545 fActiveStackState = GraphicStackState(&fContent);
1546 } else {
1547 SkASSERT(fActiveStackState.fContentStream = &fContent);
Hal Canaryb400d4d2018-09-26 16:33:52 -04001548 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001549 } else {
Hal Canaryb400d4d2018-09-26 16:33:52 -04001550 fActiveStackState.drainStack();
Hal Canary42137de2018-10-08 16:00:37 -04001551 fActiveStackState = GraphicStackState(&fContentBuffer);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001552 }
Hal Canaryb400d4d2018-09-26 16:33:52 -04001553 SkASSERT(fActiveStackState.fContentStream);
1554 GraphicStateEntry entry;
1555 this->populateGraphicStateEntryFromPaint(matrix, clipStack, paint, hasText, &entry);
1556 fActiveStackState.updateClip(clipStack, this->bounds());
1557 fActiveStackState.updateMatrix(entry.fMatrix);
1558 fActiveStackState.updateDrawingState(entry);
1559
1560 return fActiveStackState.fContentStream;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001561}
1562
Hal Canaryb400d4d2018-09-26 16:33:52 -04001563void SkPDFDevice::finishContentEntry(const SkClipStack* clipStack,
1564 SkBlendMode blendMode,
Hal Canary9a3f5542018-12-10 19:59:07 -05001565 SkPDFIndirectReference dst,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001566 SkPath* shape) {
Hal Canaryb400d4d2018-09-26 16:33:52 -04001567 SkASSERT(blendMode != SkBlendMode::kDst);
1568 if (treat_as_regular_pdf_blend_mode(blendMode)) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001569 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001570 return;
1571 }
Hal Canaryb400d4d2018-09-26 16:33:52 -04001572
1573 SkASSERT(fActiveStackState.fContentStream);
1574
1575 fActiveStackState.drainStack();
1576 fActiveStackState = GraphicStackState();
1577
reed374772b2016-10-05 17:33:02 -07001578 if (blendMode == SkBlendMode::kDstOver) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001579 SkASSERT(!dst);
Hal Canary42137de2018-10-08 16:00:37 -04001580 if (fContentBuffer.bytesWritten() != 0) {
1581 if (fContent.bytesWritten() != 0) {
1582 fContentBuffer.writeText("Q\nq\n");
1583 fNeedsExtraSave = true;
1584 }
1585 fContentBuffer.prependToAndReset(&fContent);
1586 SkASSERT(fContentBuffer.bytesWritten() == 0);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001587 }
1588 return;
1589 }
Hal Canary42137de2018-10-08 16:00:37 -04001590 if (fContentBuffer.bytesWritten() != 0) {
1591 if (fContent.bytesWritten() != 0) {
1592 fContent.writeText("Q\nq\n");
1593 fNeedsExtraSave = true;
1594 }
1595 fContentBuffer.writeToAndReset(&fContent);
1596 SkASSERT(fContentBuffer.bytesWritten() == 0);
1597 }
1598
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001599 if (!dst) {
reed374772b2016-10-05 17:33:02 -07001600 SkASSERT(blendMode == SkBlendMode::kSrc ||
1601 blendMode == SkBlendMode::kSrcOut);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001602 return;
1603 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001604
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001605 SkASSERT(dst);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001606 // Changing the current content into a form-xobject will destroy the clip
1607 // objects which is fine since the xobject will already be clipped. However
1608 // if source has shape, we need to clip it too, so a copy of the clip is
1609 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001610
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001611 SkPaint stockPaint;
1612
Hal Canary9a3f5542018-12-10 19:59:07 -05001613 SkPDFIndirectReference srcFormXObject;
Hal Canary9e41c212018-09-03 12:00:23 -04001614 if (this->isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001615 // If nothing was drawn and there's no shape, then the draw was a
1616 // no-op, but dst needs to be restored for that to be true.
1617 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1618 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1619 // reduces to Dst.
reed374772b2016-10-05 17:33:02 -07001620 if (shape == nullptr || blendMode == SkBlendMode::kDstOut ||
1621 blendMode == SkBlendMode::kSrcATop) {
Hal Canaryd892a9d2018-09-24 21:20:47 -04001622 ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint);
Hal Canary9a3f5542018-12-10 19:59:07 -05001623 this->drawFormXObject(dst, content.stream());
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001624 return;
1625 } else {
reed374772b2016-10-05 17:33:02 -07001626 blendMode = SkBlendMode::kClear;
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001627 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001628 } else {
halcanary4b1e17e2016-07-27 14:49:46 -07001629 srcFormXObject = this->makeFormXObjectFromDevice();
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001630 }
1631
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001632 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1633 // without alpha.
reed374772b2016-10-05 17:33:02 -07001634 if (blendMode == SkBlendMode::kSrcATop) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001635 // TODO(vandebo): In order to properly support SrcATop we have to track
1636 // the shape of what's been drawn at all times. It's the intersection of
1637 // the non-transparent parts of the device and the outlines (shape) of
1638 // all images and devices drawn.
Hal Canaryc4c41c92018-09-28 13:30:45 -04001639 this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001640 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001641 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001642 // Draw shape into a form-xobject.
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001643 SkPaint filledPaint;
1644 filledPaint.setColor(SK_ColorBLACK);
1645 filledPaint.setStyle(SkPaint::kFill_Style);
Hal Canaryb400d4d2018-09-26 16:33:52 -04001646 SkClipStack empty;
Hal Canary813b5ac2018-09-28 12:30:37 -04001647 SkPDFDevice shapeDev(this->size(), fDocument, fInitialTransform);
1648 shapeDev.internalDrawPath(clipStack ? *clipStack : empty,
1649 SkMatrix::I(), *shape, filledPaint, true);
Hal Canaryc4c41c92018-09-28 13:30:45 -04001650 this->drawFormXObjectWithMask(dst, shapeDev.makeFormXObjectFromDevice(),
Mike Reeda1361362017-03-07 09:37:29 -05001651 SkBlendMode::kSrcOver, true);
halcanarydabd4f02016-08-03 11:16:56 -07001652 } else {
Hal Canaryc4c41c92018-09-28 13:30:45 -04001653 this->drawFormXObjectWithMask(dst, srcFormXObject, SkBlendMode::kSrcOver, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001654 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001655 }
1656
reed374772b2016-10-05 17:33:02 -07001657 if (blendMode == SkBlendMode::kClear) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001658 return;
reed374772b2016-10-05 17:33:02 -07001659 } else if (blendMode == SkBlendMode::kSrc ||
1660 blendMode == SkBlendMode::kDstATop) {
Hal Canaryd892a9d2018-09-24 21:20:47 -04001661 ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint);
Hal Canary42137de2018-10-08 16:00:37 -04001662 if (content) {
Hal Canary3b8b11e2018-09-29 22:31:34 -04001663 this->drawFormXObject(srcFormXObject, content.stream());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001664 }
reed374772b2016-10-05 17:33:02 -07001665 if (blendMode == SkBlendMode::kSrc) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001666 return;
1667 }
reed374772b2016-10-05 17:33:02 -07001668 } else if (blendMode == SkBlendMode::kSrcATop) {
Hal Canaryd892a9d2018-09-24 21:20:47 -04001669 ScopedContentEntry content(this, nullptr, SkMatrix::I(), stockPaint);
Hal Canary42137de2018-10-08 16:00:37 -04001670 if (content) {
Hal Canary3b8b11e2018-09-29 22:31:34 -04001671 this->drawFormXObject(dst, content.stream());
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001672 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001673 }
1674
reed374772b2016-10-05 17:33:02 -07001675 SkASSERT(blendMode == SkBlendMode::kSrcIn ||
1676 blendMode == SkBlendMode::kDstIn ||
1677 blendMode == SkBlendMode::kSrcOut ||
1678 blendMode == SkBlendMode::kDstOut ||
1679 blendMode == SkBlendMode::kSrcATop ||
1680 blendMode == SkBlendMode::kDstATop ||
1681 blendMode == SkBlendMode::kModulate);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001682
reed374772b2016-10-05 17:33:02 -07001683 if (blendMode == SkBlendMode::kSrcIn ||
1684 blendMode == SkBlendMode::kSrcOut ||
1685 blendMode == SkBlendMode::kSrcATop) {
Hal Canary9a3f5542018-12-10 19:59:07 -05001686 this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver,
1687 blendMode == SkBlendMode::kSrcOut);
halcanarydabd4f02016-08-03 11:16:56 -07001688 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001689 } else {
reed374772b2016-10-05 17:33:02 -07001690 SkBlendMode mode = SkBlendMode::kSrcOver;
reed374772b2016-10-05 17:33:02 -07001691 if (blendMode == SkBlendMode::kModulate) {
Hal Canaryc4c41c92018-09-28 13:30:45 -04001692 this->drawFormXObjectWithMask(srcFormXObject, dst, SkBlendMode::kSrcOver, false);
reed374772b2016-10-05 17:33:02 -07001693 mode = SkBlendMode::kMultiply;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001694 }
Hal Canary9a3f5542018-12-10 19:59:07 -05001695 this->drawFormXObjectWithMask(dst, srcFormXObject, mode, blendMode == SkBlendMode::kDstOut);
halcanarydabd4f02016-08-03 11:16:56 -07001696 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001697 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001698}
1699
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001700bool SkPDFDevice::isContentEmpty() {
Hal Canary42137de2018-10-08 16:00:37 -04001701 return fContent.bytesWritten() == 0 && fContentBuffer.bytesWritten() == 0;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001702}
1703
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001704void SkPDFDevice::populateGraphicStateEntryFromPaint(
1705 const SkMatrix& matrix,
Hal Canaryd892a9d2018-09-24 21:20:47 -04001706 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001707 const SkPaint& paint,
1708 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001709 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001710 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1711 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1712 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001713
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001714 entry->fMatrix = matrix;
Hal Canaryb400d4d2018-09-26 16:33:52 -04001715 entry->fClipStackGenID = clipStack ? clipStack->getTopmostGenID()
1716 : SkClipStack::kWideOpenGenID;
Hal Canary04ac4612018-10-10 13:09:43 -04001717 SkColor4f color = paint.getColor4f();
1718 entry->fColor = {color.fR, color.fG, color.fB, 1};
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001719 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001720
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001721 // PDF treats a shader as a color, so we only set one or the other.
reedfe630452016-03-25 09:08:00 -07001722 SkShader* shader = paint.getShader();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001723 if (shader) {
Hal Canaryc8f91802017-02-12 20:29:12 -05001724 if (SkShader::kColor_GradientType == shader->asAGradient(nullptr)) {
1725 // We don't have to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001726 SkShader::GradientInfo gradientInfo;
Hal Canaryc8f91802017-02-12 20:29:12 -05001727 SkColor gradientColor = SK_ColorBLACK;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001728 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001729 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001730 gradientInfo.fColorCount = 1;
Hal Canaryc8f91802017-02-12 20:29:12 -05001731 SkAssertResult(shader->asAGradient(&gradientInfo) == SkShader::kColor_GradientType);
Hal Canary04ac4612018-10-10 13:09:43 -04001732 color = SkColor4f::FromColor(gradientColor);
1733 entry->fColor ={color.fR, color.fG, color.fB, 1};
1734
Hal Canaryc8f91802017-02-12 20:29:12 -05001735 } else {
1736 // PDF positions patterns relative to the initial transform, so
1737 // we need to apply the current transform to the shader parameters.
1738 SkMatrix transform = matrix;
1739 transform.postConcat(fInitialTransform);
1740
1741 // PDF doesn't support kClamp_TileMode, so we simulate it by making
1742 // a pattern the size of the current clip.
Hal Canaryb400d4d2018-09-26 16:33:52 -04001743 SkRect clipStackBounds = clipStack ? clipStack->bounds(this->bounds())
1744 : SkRect::Make(this->bounds());
Hal Canaryc8f91802017-02-12 20:29:12 -05001745
1746 // We need to apply the initial transform to bounds in order to get
1747 // bounds in a consistent coordinate system.
Hal Canarya41c2aa2017-02-22 16:32:34 -05001748 fInitialTransform.mapRect(&clipStackBounds);
1749 SkIRect bounds;
1750 clipStackBounds.roundOut(&bounds);
Hal Canaryc8f91802017-02-12 20:29:12 -05001751
Hal Canary9a3f5542018-12-10 19:59:07 -05001752 SkPDFIndirectReference pdfShader
1753 = SkPDFMakeShader(fDocument, shader, transform, bounds, paint.getColor());
Hal Canaryc8f91802017-02-12 20:29:12 -05001754
Hal Canary9e41c212018-09-03 12:00:23 -04001755 if (pdfShader) {
1756 // pdfShader has been canonicalized so we can directly compare pointers.
Hal Canary9a3f5542018-12-10 19:59:07 -05001757 entry->fShaderIndex = add_resource(fShaderResources, pdfShader);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001758 }
1759 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001760 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001761
Hal Canary9a3f5542018-12-10 19:59:07 -05001762 SkPDFIndirectReference newGraphicState;
Hal Canary04ac4612018-10-10 13:09:43 -04001763 if (color == paint.getColor4f()) {
Hal Canary9a3f5542018-12-10 19:59:07 -05001764 newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(fDocument, paint);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001765 } else {
1766 SkPaint newPaint = paint;
Hal Canary04ac4612018-10-10 13:09:43 -04001767 newPaint.setColor4f(color, nullptr);
Hal Canary9a3f5542018-12-10 19:59:07 -05001768 newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(fDocument, newPaint);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001769 }
Hal Canary9a3f5542018-12-10 19:59:07 -05001770 entry->fGraphicStateIndex = add_resource(fGraphicStateResources, std::move(newGraphicState));
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001771
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001772 if (hasText) {
1773 entry->fTextScaleX = paint.getTextScaleX();
1774 entry->fTextFill = paint.getStyle();
1775 } else {
1776 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001777 }
1778}
1779
Hal Canaryfafe1352017-04-11 12:12:02 -04001780static SkSize rect_to_size(const SkRect& r) { return {r.width(), r.height()}; }
halcanary7a14b312015-10-01 07:28:13 -07001781
Hal Canary7cbf5e32017-07-12 13:10:23 -04001782static sk_sp<SkImage> color_filter(const SkImage* image,
halcanarya50151d2016-03-25 11:57:49 -07001783 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07001784 auto surface =
Hal Canary7cbf5e32017-07-12 13:10:23 -04001785 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(image->dimensions()));
halcanarya50151d2016-03-25 11:57:49 -07001786 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07001787 SkCanvas* canvas = surface->getCanvas();
1788 canvas->clear(SK_ColorTRANSPARENT);
1789 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07001790 paint.setColorFilter(sk_ref_sp(colorFilter));
Hal Canary7cbf5e32017-07-12 13:10:23 -04001791 canvas->drawImage(image, 0, 0, &paint);
halcanarya50151d2016-03-25 11:57:49 -07001792 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07001793}
1794
1795////////////////////////////////////////////////////////////////////////////////
Hal Canary7cbf5e32017-07-12 13:10:23 -04001796
1797static bool is_integer(SkScalar x) {
1798 return x == SkScalarTruncToScalar(x);
1799}
1800
1801static bool is_integral(const SkRect& r) {
1802 return is_integer(r.left()) &&
1803 is_integer(r.top()) &&
1804 is_integer(r.right()) &&
1805 is_integer(r.bottom());
1806}
1807
1808void SkPDFDevice::internalDrawImageRect(SkKeyedImage imageSubset,
1809 const SkRect* src,
1810 const SkRect& dst,
1811 const SkPaint& srcPaint,
1812 const SkMatrix& ctm) {
Hal Canaryb4e528d2018-03-09 16:02:15 -05001813 if (this->hasEmptyClip()) {
1814 return;
1815 }
Hal Canary7cbf5e32017-07-12 13:10:23 -04001816 if (!imageSubset) {
halcanarya50151d2016-03-25 11:57:49 -07001817 return;
1818 }
Hal Canary7cbf5e32017-07-12 13:10:23 -04001819
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001820 // First, figure out the src->dst transform and subset the image if needed.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001821 SkIRect bounds = imageSubset.image()->bounds();
Hal Canary7cbf5e32017-07-12 13:10:23 -04001822 SkRect srcRect = src ? *src : SkRect::Make(bounds);
1823 SkMatrix transform;
1824 transform.setRectToRect(srcRect, dst, SkMatrix::kFill_ScaleToFit);
1825 if (src && *src != SkRect::Make(bounds)) {
1826 if (!srcRect.intersect(SkRect::Make(bounds))) {
1827 return;
1828 }
1829 srcRect.roundOut(&bounds);
1830 transform.preTranslate(SkIntToScalar(bounds.x()),
1831 SkIntToScalar(bounds.y()));
1832 if (bounds != imageSubset.image()->bounds()) {
1833 imageSubset = imageSubset.subset(bounds);
1834 }
1835 if (!imageSubset) {
1836 return;
1837 }
1838 }
1839
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001840 // If the image is opaque and the paint's alpha is too, replace
1841 // kSrc blendmode with kSrcOver.
1842 SkPaint paint = srcPaint;
1843 if (imageSubset.image()->isOpaque()) {
1844 replace_srcmode_on_opaque_paint(&paint);
Hal Canaryd425a1d2017-07-12 13:13:51 -04001845 }
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001846
1847 // Alpha-only images need to get their color from the shader, before
1848 // applying the colorfilter.
1849 if (imageSubset.image()->isAlphaOnly() && paint.getColorFilter()) {
1850 // must blend alpha image and shader before applying colorfilter.
1851 auto surface =
1852 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageSubset.image()->dimensions()));
1853 SkCanvas* canvas = surface->getCanvas();
1854 SkPaint tmpPaint;
1855 // In the case of alpha images with shaders, the shader's coordinate
1856 // system is the image's coordiantes.
1857 tmpPaint.setShader(sk_ref_sp(paint.getShader()));
Hal Canary04ac4612018-10-10 13:09:43 -04001858 tmpPaint.setColor4f(paint.getColor4f(), nullptr);
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001859 canvas->clear(0x00000000);
1860 canvas->drawImage(imageSubset.image().get(), 0, 0, &tmpPaint);
1861 paint.setShader(nullptr);
1862 imageSubset = SkKeyedImage(surface->makeImageSnapshot());
1863 SkASSERT(!imageSubset.image()->isAlphaOnly());
1864 }
1865
1866 if (imageSubset.image()->isAlphaOnly()) {
1867 // The ColorFilter applies to the paint color/shader, not the alpha layer.
1868 SkASSERT(nullptr == paint.getColorFilter());
1869
Hal Canaryd425a1d2017-07-12 13:13:51 -04001870 sk_sp<SkImage> mask = alpha_image_to_greyscale_image(imageSubset.image().get());
1871 if (!mask) {
1872 return;
1873 }
1874 // PDF doesn't seem to allow masking vector graphics with an Image XObject.
1875 // Must mask with a Form XObject.
1876 sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice();
1877 {
Herb Derbyefe39bc2018-05-01 17:06:20 -04001878 SkCanvas canvas(maskDevice);
Hal Canaryf0f4c0c2017-07-19 15:48:38 -04001879 if (paint.getMaskFilter()) {
1880 // This clip prevents the mask image shader from covering
1881 // entire device if unnecessary.
1882 canvas.clipRect(this->cs().bounds(this->bounds()));
1883 canvas.concat(ctm);
1884 SkPaint tmpPaint;
1885 tmpPaint.setShader(mask->makeShader(&transform));
1886 tmpPaint.setMaskFilter(sk_ref_sp(paint.getMaskFilter()));
1887 canvas.drawRect(dst, tmpPaint);
1888 } else {
1889 canvas.concat(ctm);
1890 if (src && !is_integral(*src)) {
1891 canvas.clipRect(dst);
1892 }
1893 canvas.concat(transform);
1894 canvas.drawImage(mask, 0, 0);
1895 }
Hal Canaryd425a1d2017-07-12 13:13:51 -04001896 }
Hal Canaryd425a1d2017-07-12 13:13:51 -04001897 if (!ctm.isIdentity() && paint.getShader()) {
1898 transform_shader(&paint, ctm); // Since we are using identity matrix.
1899 }
Hal Canaryd892a9d2018-09-24 21:20:47 -04001900 ScopedContentEntry content(this, &this->cs(), SkMatrix::I(), paint);
Hal Canary42137de2018-10-08 16:00:37 -04001901 if (!content) {
Hal Canaryd425a1d2017-07-12 13:13:51 -04001902 return;
1903 }
1904 this->addSMaskGraphicState(std::move(maskDevice), content.stream());
Hal Canary813b5ac2018-09-28 12:30:37 -04001905 SkPDFUtils::AppendRectangle(SkRect::Make(this->size()), content.stream());
Hal Canaryd425a1d2017-07-12 13:13:51 -04001906 SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPath::kWinding_FillType, content.stream());
1907 this->clearMaskOnGraphicState(content.stream());
1908 return;
1909 }
Hal Canary7cbf5e32017-07-12 13:10:23 -04001910 if (paint.getMaskFilter()) {
1911 paint.setShader(imageSubset.image()->makeShader(&transform));
Hal Canaryd791a6f2018-09-28 12:14:08 -04001912 SkPath path = to_path(dst); // handles non-integral clipping.
Robert Phillips137ca522018-08-15 10:14:33 -04001913 this->internalDrawPath(this->cs(), this->ctm(), path, paint, true);
Hal Canary7cbf5e32017-07-12 13:10:23 -04001914 return;
1915 }
1916 transform.postConcat(ctm);
1917
1918 bool needToRestore = false;
1919 if (src && !is_integral(*src)) {
1920 // Need sub-pixel clipping to fix https://bug.skia.org/4374
1921 this->cs().save();
1922 this->cs().clipRect(dst, ctm, SkClipOp::kIntersect, true);
1923 needToRestore = true;
1924 }
1925 SK_AT_SCOPE_EXIT(if (needToRestore) { this->cs().restore(); });
1926
halcanary7a14b312015-10-01 07:28:13 -07001927 #ifdef SK_PDF_IMAGE_STATS
1928 gDrawImageCalls.fetch_add(1);
1929 #endif
Hal Canary7cbf5e32017-07-12 13:10:23 -04001930 SkMatrix matrix = transform;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001931
1932 // Rasterize the bitmap using perspective in a new bitmap.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001933 if (transform.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001934 // Transform the bitmap in the new space, without taking into
1935 // account the initial transform.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001936 SkRect imageBounds = SkRect::Make(imageSubset.image()->bounds());
Hal Canaryd791a6f2018-09-28 12:14:08 -04001937 SkPath perspectiveOutline = to_path(imageBounds);
Hal Canary7cbf5e32017-07-12 13:10:23 -04001938 perspectiveOutline.transform(transform);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001939
1940 // TODO(edisonn): perf - use current clip too.
1941 // Retrieve the bounds of the new shape.
1942 SkRect bounds = perspectiveOutline.getBounds();
1943
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001944 // Transform the bitmap in the new space, taking into
1945 // account the initial transform.
Hal Canary7cbf5e32017-07-12 13:10:23 -04001946 SkMatrix total = transform;
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001947 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07001948
Hal Canaryd791a6f2018-09-28 12:14:08 -04001949 SkPath physicalPerspectiveOutline = to_path(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001950 physicalPerspectiveOutline.transform(total);
1951
halcanary7a14b312015-10-01 07:28:13 -07001952 SkRect physicalPerspectiveBounds =
1953 physicalPerspectiveOutline.getBounds();
1954 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
1955 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001956
1957 // TODO(edisonn): A better approach would be to use a bitmap shader
1958 // (in clamp mode) and draw a rect over the entire bounding box. Then
1959 // intersect perspectiveOutline to the clip. That will avoid introducing
1960 // alpha to the image while still giving good behavior at the edge of
1961 // the image. Avoiding alpha will reduce the pdf size and generation
1962 // CPU time some.
1963
halcanary7a14b312015-10-01 07:28:13 -07001964 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
1965
Hal Canaryf50ff392016-09-30 10:25:39 -04001966 auto surface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh));
halcanary7a14b312015-10-01 07:28:13 -07001967 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00001968 return;
1969 }
halcanary7a14b312015-10-01 07:28:13 -07001970 SkCanvas* canvas = surface->getCanvas();
1971 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001972
1973 SkScalar deltaX = bounds.left();
1974 SkScalar deltaY = bounds.top();
1975
Hal Canary7cbf5e32017-07-12 13:10:23 -04001976 SkMatrix offsetMatrix = transform;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001977 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001978 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001979
1980 // Translate the draw in the new canvas, so we perfectly fit the
1981 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07001982 canvas->setMatrix(offsetMatrix);
Hal Canary7cbf5e32017-07-12 13:10:23 -04001983 canvas->drawImage(imageSubset.image(), 0, 0);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001984 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07001985 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001986
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001987 // In the new space, we use the identity matrix translated
1988 // and scaled to reflect DPI.
1989 matrix.setScale(1 / scaleX, 1 / scaleY);
1990 matrix.postTranslate(deltaX, deltaY);
1991
Hal Canary7cbf5e32017-07-12 13:10:23 -04001992 imageSubset = SkKeyedImage(surface->makeImageSnapshot());
1993 if (!imageSubset) {
1994 return;
1995 }
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001996 }
1997
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001998 SkMatrix scaled;
1999 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002000 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2001 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002002 // Scale the image up from 1x1 to WxH.
Hal Canary7cbf5e32017-07-12 13:10:23 -04002003 SkIRect subset = imageSubset.image()->bounds();
2004 scaled.postScale(SkIntToScalar(subset.width()),
2005 SkIntToScalar(subset.height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002006 scaled.postConcat(matrix);
Hal Canaryd892a9d2018-09-24 21:20:47 -04002007 ScopedContentEntry content(this, &this->cs(), scaled, paint);
Hal Canary42137de2018-10-08 16:00:37 -04002008 if (!content) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002009 return;
2010 }
2011 if (content.needShape()) {
Hal Canaryd791a6f2018-09-28 12:14:08 -04002012 SkPath shape = to_path(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002013 shape.transform(matrix);
2014 content.setShape(shape);
2015 }
2016 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002017 return;
2018 }
2019
halcanary287d22d2015-09-24 10:20:05 -07002020 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
Hal Canary7cbf5e32017-07-12 13:10:23 -04002021 sk_sp<SkImage> img = color_filter(imageSubset.image().get(), colorFilter);
2022 imageSubset = SkKeyedImage(std::move(img));
2023 if (!imageSubset) {
2024 return;
2025 }
halcanary7a14b312015-10-01 07:28:13 -07002026 // TODO(halcanary): de-dupe this by caching filtered images.
2027 // (maybe in the resource cache?)
2028 }
halcanarya50151d2016-03-25 11:57:49 -07002029
Hal Canary7cbf5e32017-07-12 13:10:23 -04002030 SkBitmapKey key = imageSubset.key();
Hal Canary9a3f5542018-12-10 19:59:07 -05002031 SkPDFIndirectReference* pdfimagePtr = fDocument->canon()->fPDFBitmapMap.find(key);
2032 SkPDFIndirectReference pdfimage = pdfimagePtr ? *pdfimagePtr : SkPDFIndirectReference();
2033 if (!pdfimagePtr) {
Hal Canary7cbf5e32017-07-12 13:10:23 -04002034 SkASSERT(imageSubset);
Hal Canary9a3f5542018-12-10 19:59:07 -05002035 pdfimage = SkPDFSerializeImage(imageSubset.image().get(), fDocument,
Hal Canarya1211832018-11-13 16:45:14 -05002036 fDocument->metadata().fEncodingQuality);
Hal Canary7cbf5e32017-07-12 13:10:23 -04002037 SkASSERT((key != SkBitmapKey{{0, 0, 0, 0}, 0}));
Hal Canary5c1b3602017-04-17 16:30:06 -04002038 fDocument->canon()->fPDFBitmapMap.set(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002039 }
Hal Canary9a3f5542018-12-10 19:59:07 -05002040 SkASSERT(pdfimage != SkPDFIndirectReference());
2041 this->drawFormXObject(pdfimage, content.stream());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002042}
reede51c3562016-07-19 14:33:20 -07002043
2044///////////////////////////////////////////////////////////////////////////////////////////////////
2045
2046#include "SkSpecialImage.h"
2047#include "SkImageFilter.h"
2048
Florin Malita53f77bd2017-04-28 13:48:37 -04002049void SkPDFDevice::drawSpecial(SkSpecialImage* srcImg, int x, int y, const SkPaint& paint,
2050 SkImage* clipImage, const SkMatrix& clipMatrix) {
Hal Canaryb4e528d2018-03-09 16:02:15 -05002051 if (this->hasEmptyClip()) {
2052 return;
2053 }
reede51c3562016-07-19 14:33:20 -07002054 SkASSERT(!srcImg->isTextureBacked());
2055
Florin Malita53f77bd2017-04-28 13:48:37 -04002056 //TODO: clipImage support
2057
reede51c3562016-07-19 14:33:20 -07002058 SkBitmap resultBM;
2059
2060 SkImageFilter* filter = paint.getImageFilter();
2061 if (filter) {
2062 SkIPoint offset = SkIPoint::Make(0, 0);
Mike Reeda1361362017-03-07 09:37:29 -05002063 SkMatrix matrix = this->ctm();
reede51c3562016-07-19 14:33:20 -07002064 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
Hal Canaryf3ee34f2017-02-07 16:58:28 -05002065 const SkIRect clipBounds =
Hal Canary22b2d8c2017-07-19 14:46:12 -04002066 this->cs().bounds(this->bounds()).roundOut().makeOffset(-x, -y);
Hal Canary67b39de2016-11-07 11:47:44 -05002067 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
Brian Osmana50205f2018-07-06 13:57:01 -04002068 // TODO: Should PDF be operating in a specified color type/space? For now, run the filter
brianosman2a75e5d2016-09-22 07:15:37 -07002069 // in the same color space as the source (this is different from all other backends).
Brian Osmana50205f2018-07-06 13:57:01 -04002070 SkImageFilter::OutputProperties outputProperties(kN32_SkColorType, srcImg->getColorSpace());
brianosman2a75e5d2016-09-22 07:15:37 -07002071 SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties);
reede51c3562016-07-19 14:33:20 -07002072
2073 sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset));
2074 if (resultImg) {
2075 SkPaint tmpUnfiltered(paint);
2076 tmpUnfiltered.setImageFilter(nullptr);
2077 if (resultImg->getROPixels(&resultBM)) {
Mike Reeda1361362017-03-07 09:37:29 -05002078 this->drawSprite(resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered);
reede51c3562016-07-19 14:33:20 -07002079 }
2080 }
2081 } else {
2082 if (srcImg->getROPixels(&resultBM)) {
Mike Reeda1361362017-03-07 09:37:29 -05002083 this->drawSprite(resultBM, x, y, paint);
reede51c3562016-07-19 14:33:20 -07002084 }
2085 }
2086}
2087
2088sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) {
2089 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
2090}
2091
2092sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) {
Brian Osmanb9c49782018-10-12 12:01:22 -04002093 return SkSpecialImage::MakeFromImage(image->bounds(), image->makeNonTextureImage());
reede51c3562016-07-19 14:33:20 -07002094}
2095
2096sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() {
reede51c3562016-07-19 14:33:20 -07002097 return nullptr;
2098}
brianosman04a44d02016-09-21 09:46:57 -07002099
2100SkImageFilterCache* SkPDFDevice::getImageFilterCache() {
2101 // We always return a transient cache, so it is freed after each
2102 // filter traversal.
2103 return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize);
2104}