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