blob: 24df879a9d1b27b6318feb96ece87ace73aa3563 [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"
reedf70b5312016-03-04 16:36:20 -08009#include "SkAnnotationKeys.h"
reed7503d602016-07-15 14:23:29 -070010#include "SkBitmapDevice.h"
martina.kollarovab8d6af12016-06-29 05:12:31 -070011#include "SkBitmapKey.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000012#include "SkColor.h"
halcanary287d22d2015-09-24 10:20:05 -070013#include "SkColorFilter.h"
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000014#include "SkDraw.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkGlyphCache.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000016#include "SkPath.h"
reeda4393342016-03-18 11:22:57 -070017#include "SkPathEffect.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000018#include "SkPathOps.h"
halcanarydb0dcc72015-03-20 12:31:52 -070019#include "SkPDFBitmap.h"
halcanary7a14b312015-10-01 07:28:13 -070020#include "SkPDFCanon.h"
halcanary989da4a2016-03-21 14:33:17 -070021#include "SkPDFDocument.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000022#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000023#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000024#include "SkPDFGraphicState.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000025#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000026#include "SkPDFShader.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000027#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000028#include "SkPDFUtils.h"
wangxianzhuef6c50a2015-09-17 20:38:02 -070029#include "SkRasterClip.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000030#include "SkRRect.h"
halcanary4871f222016-08-26 13:17:44 -070031#include "SkScopeExit.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000032#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070033#include "SkSurface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000034#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000035#include "SkTemplates.h"
halcanarya6814332015-05-27 08:53:36 -070036#include "SkXfermodeInterpretation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000037
edisonn@google.com73a7ea32013-11-11 20:55:15 +000038#define DPI_FOR_RASTER_SCALE_ONE 72
39
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000040// Utility functions
41
halcanarya6814332015-05-27 08:53:36 -070042// If the paint will definitely draw opaquely, replace kSrc_Mode with
43// kSrcOver_Mode. http://crbug.com/473572
44static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
45 if (kSrcOver_SkXfermodeInterpretation
46 == SkInterpretXfermode(*paint, false)) {
halcanary96fcdcc2015-08-27 07:41:13 -070047 paint->setXfermode(nullptr);
halcanarya6814332015-05-27 08:53:36 -070048 }
49}
50
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000051static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000052 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
halcanaryeb92cb32016-07-15 13:41:27 -070053 SkPDFUtils::AppendColorComponent(SkColorGetR(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000054 result->writeText(" ");
halcanaryeb92cb32016-07-15 13:41:27 -070055 SkPDFUtils::AppendColorComponent(SkColorGetG(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000056 result->writeText(" ");
halcanaryeb92cb32016-07-15 13:41:27 -070057 SkPDFUtils::AppendColorComponent(SkColorGetB(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000058 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000059}
60
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000061static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000062 SkPaint result = paint;
63 if (result.isFakeBoldText()) {
64 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
65 kStdFakeBoldInterpKeys,
66 kStdFakeBoldInterpValues,
67 kStdFakeBoldInterpLength);
68 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000069 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000070 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000071 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000072 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000073 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000074 result.setStrokeWidth(width);
75 }
76 return result;
77}
78
halcanary2be7e012016-03-28 11:58:08 -070079SkPDFDevice::GraphicStateEntry::GraphicStateEntry()
80 : fColor(SK_ColorBLACK)
81 , fTextScaleX(SK_Scalar1)
82 , fTextFill(SkPaint::kFill_Style)
83 , fShaderIndex(-1)
84 , fGraphicStateIndex(-1)
85 , fFont(nullptr)
86 , fTextSize(SK_ScalarNaN) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000087 fMatrix.reset();
88}
89
halcanary2be7e012016-03-28 11:58:08 -070090bool SkPDFDevice::GraphicStateEntry::compareInitialState(
91 const GraphicStateEntry& cur) {
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +000092 return fColor == cur.fColor &&
93 fShaderIndex == cur.fShaderIndex &&
94 fGraphicStateIndex == cur.fGraphicStateIndex &&
95 fMatrix == cur.fMatrix &&
96 fClipStack == cur.fClipStack &&
97 (fTextScaleX == 0 ||
98 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000099}
100
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000101class GraphicStackState {
102public:
103 GraphicStackState(const SkClipStack& existingClipStack,
104 const SkRegion& existingClipRegion,
105 SkWStream* contentStream)
106 : fStackDepth(0),
107 fContentStream(contentStream) {
108 fEntries[0].fClipStack = existingClipStack;
109 fEntries[0].fClipRegion = existingClipRegion;
110 }
111
112 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000113 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000114 void updateMatrix(const SkMatrix& matrix);
halcanary2be7e012016-03-28 11:58:08 -0700115 void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000116
117 void drainStack();
118
119private:
120 void push();
121 void pop();
halcanary2be7e012016-03-28 11:58:08 -0700122 SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000123
124 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
125 static const int kMaxStackDepth = 12;
halcanary2be7e012016-03-28 11:58:08 -0700126 SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1];
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000127 int fStackDepth;
128 SkWStream* fContentStream;
129};
130
131void GraphicStackState::drainStack() {
132 while (fStackDepth) {
133 pop();
134 }
135}
136
137void GraphicStackState::push() {
138 SkASSERT(fStackDepth < kMaxStackDepth);
139 fContentStream->writeText("q\n");
140 fStackDepth++;
141 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
142}
143
144void GraphicStackState::pop() {
145 SkASSERT(fStackDepth > 0);
146 fContentStream->writeText("Q\n");
147 fStackDepth--;
148}
149
robertphillips@google.com80214e22012-07-20 15:33:18 +0000150// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000151// and then skips over the leading entries as specified in prefix. It requires
152// and asserts that "prefix" will be a prefix to "stack."
153static void skip_clip_stack_prefix(const SkClipStack& prefix,
154 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000155 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000156 SkClipStack::B2TIter prefixIter(prefix);
157 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000158
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000159 const SkClipStack::Element* prefixEntry;
160 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000161
162 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000163 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000164 iterEntry = iter->next();
165 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000166 // Because of SkClipStack does internal intersection, the last clip
167 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000168 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000169 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
170 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
171 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000172 // back up the iterator by one
173 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000174 prefixEntry = prefixIter.next();
175 break;
176 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000177 }
178
halcanary96fcdcc2015-08-27 07:41:13 -0700179 SkASSERT(prefixEntry == nullptr);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000180}
181
182static void emit_clip(SkPath* clipPath, SkRect* clipRect,
183 SkWStream* contentStream) {
184 SkASSERT(clipPath || clipRect);
185
186 SkPath::FillType clipFill;
187 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000188 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000189 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000190 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000191 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
192 clipFill = SkPath::kWinding_FillType;
193 }
194
195 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
196 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
197 if (clipFill == SkPath::kEvenOdd_FillType) {
198 contentStream->writeText("W* n\n");
199 } else {
200 contentStream->writeText("W n\n");
201 }
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());
211
212 SkPath clipPath;
213 clipPath.addRect(bounds);
214
reedcdb42bb2015-06-26 10:23:07 -0700215 return Op(clipPath, invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000216}
217
218// Sanity check the numerical values of the SkRegion ops and PathOps ops
219// enums so region_op_to_pathops_op can do a straight passthrough cast.
220// If these are failing, it may be necessary to make region_op_to_pathops_op
221// do more.
bungeman99fe8222015-08-20 07:57:51 -0700222static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
223static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
224static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
225static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
226static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
227 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000228
229static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
230 SkASSERT(op >= 0);
231 SkASSERT(op <= SkRegion::kReverseDifference_Op);
232 return (SkPathOp)op;
233}
234
235/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
236 * Returns true if successful, or false if not successful.
237 * If successful, the resulting clip is stored in outClipPath.
238 * If not successful, outClipPath is undefined, and a fallback method
239 * should be used.
240 */
241static bool get_clip_stack_path(const SkMatrix& transform,
242 const SkClipStack& clipStack,
243 const SkRegion& clipRegion,
244 SkPath* outClipPath) {
245 outClipPath->reset();
246 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
247
248 const SkClipStack::Element* clipEntry;
249 SkClipStack::Iter iter;
250 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
251 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
252 SkPath entryPath;
253 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
254 outClipPath->reset();
255 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
256 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000257 } else {
258 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000259 }
260 entryPath.transform(transform);
261
262 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
263 *outClipPath = entryPath;
264 } else {
265 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
266 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
267 return false;
268 }
269 }
270 }
271
272 if (outClipPath->isInverseFillType()) {
273 // The bounds are slightly outset to ensure this is correct in the
274 // face of floating-point accuracy and possible SkRegion bitmap
275 // approximations.
276 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
277 clipBounds.outset(SK_Scalar1, SK_Scalar1);
278 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
279 return false;
280 }
281 }
282 return true;
283}
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000284
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000285// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000286// graphic state stack, and the fact that we can know all the clips used
287// on the page to optimize this.
288void GraphicStackState::updateClip(const SkClipStack& clipStack,
289 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000290 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000291 if (clipStack == currentEntry()->fClipStack) {
292 return;
293 }
294
295 while (fStackDepth > 0) {
296 pop();
297 if (clipStack == currentEntry()->fClipStack) {
298 return;
299 }
300 }
301 push();
302
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000303 currentEntry()->fClipStack = clipStack;
304 currentEntry()->fClipRegion = clipRegion;
305
306 SkMatrix transform;
307 transform.setTranslate(translation.fX, translation.fY);
308
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000309 SkPath clipPath;
310 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700311 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000312 return;
313 }
halcanarydda239e2016-03-31 07:33:57 -0700314
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000315 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
316 // already been applied. (If this is a top level device, then it specifies
317 // a clip to the content area. If this is a layer, then it specifies
318 // the clip in effect when the layer was created.) There's no need to
319 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
320 // initial clip on the parent layer. (This means there's a bug if the user
321 // expands the clip and then uses any xfer mode that uses dst:
322 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000323 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000324 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
325
326 // If the clip stack does anything other than intersect or if it uses
327 // an inverse fill type, we have to fall back to the clip region.
328 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000329 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000330 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000331 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
332 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000333 needRegion = true;
334 break;
335 }
336 }
337
338 if (needRegion) {
339 SkPath clipPath;
340 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700341 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000342 } else {
343 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000344 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000345 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000346 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
347 switch (clipEntry->getType()) {
348 case SkClipStack::Element::kRect_Type: {
349 SkRect translatedClip;
350 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700351 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000352 break;
353 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000354 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000355 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000356 clipEntry->asPath(&translatedPath);
357 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700358 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000359 break;
360 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000361 }
362 }
363 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000364}
365
366void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
367 if (matrix == currentEntry()->fMatrix) {
368 return;
369 }
370
371 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
372 SkASSERT(fStackDepth > 0);
373 SkASSERT(fEntries[fStackDepth].fClipStack ==
374 fEntries[fStackDepth -1].fClipStack);
375 pop();
376
377 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
378 }
379 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
380 return;
381 }
382
383 push();
384 SkPDFUtils::AppendTransform(matrix, fContentStream);
385 currentEntry()->fMatrix = matrix;
386}
387
halcanary2be7e012016-03-28 11:58:08 -0700388void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000389 // PDF treats a shader as a color, so we only set one or the other.
390 if (state.fShaderIndex >= 0) {
391 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000392 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000393 currentEntry()->fShaderIndex = state.fShaderIndex;
394 }
395 } else {
396 if (state.fColor != currentEntry()->fColor ||
397 currentEntry()->fShaderIndex >= 0) {
398 emit_pdf_color(state.fColor, fContentStream);
399 fContentStream->writeText("RG ");
400 emit_pdf_color(state.fColor, fContentStream);
401 fContentStream->writeText("rg\n");
402 currentEntry()->fColor = state.fColor;
403 currentEntry()->fShaderIndex = -1;
404 }
405 }
406
407 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000408 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000409 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
410 }
411
412 if (state.fTextScaleX) {
413 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
414 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
415 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700416 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000417 fContentStream->writeText(" Tz\n");
418 currentEntry()->fTextScaleX = state.fTextScaleX;
419 }
420 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700421 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
422 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
423 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000424 fContentStream->writeDecAsText(state.fTextFill);
425 fContentStream->writeText(" Tr\n");
426 currentEntry()->fTextFill = state.fTextFill;
427 }
428 }
429}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000430
reed76033be2015-03-14 10:54:31 -0700431static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700432 // PDF does not support image filters, so render them on CPU.
433 // Note that this rendering is done at "screen" resolution (100dpi), not
434 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700435 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800436 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700437
438 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700439 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700440}
441
442SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
reedcd4051e2016-07-15 09:41:26 -0700443 if (layerPaint && not_supported_for_layers(*layerPaint)) {
reed7503d602016-07-15 14:23:29 -0700444 // need to return a raster device, which we will detect in drawDevice()
445 return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
senorblancob0e89dc2014-10-20 14:03:12 -0700446 }
fmalita6987dca2014-11-13 08:33:37 -0800447 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700448 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000449}
450
halcanary989da4a2016-03-21 14:33:17 -0700451SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
452
bsalomon@google.come97f0852011-06-17 13:10:25 +0000453
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000454
455// A helper class to automatically finish a ContentEntry at the end of a
456// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000457class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000458public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000459 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
460 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000461 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700462 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000463 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700464 fDstFormXObject(nullptr) {
reed1e7f5e72016-04-27 07:49:17 -0700465 init(draw.fClipStack, draw.fRC->bwRgn(), *draw.fMatrix, paint, hasText);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000466 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000467 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
468 const SkRegion& clipRegion, const SkMatrix& matrix,
469 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000470 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700471 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000472 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700473 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000474 init(clipStack, clipRegion, matrix, paint, hasText);
475 }
476
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000477 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000478 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000479 SkPath* shape = &fShape;
480 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700481 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000482 }
halcanarydabd4f02016-08-03 11:16:56 -0700483 fDevice->finishContentEntry(fXfermode, std::move(fDstFormXObject), shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000484 }
485 }
486
halcanary2be7e012016-03-28 11:58:08 -0700487 SkPDFDevice::ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000488
489 /* Returns true when we explicitly need the shape of the drawing. */
490 bool needShape() {
491 switch (fXfermode) {
492 case SkXfermode::kClear_Mode:
493 case SkXfermode::kSrc_Mode:
494 case SkXfermode::kSrcIn_Mode:
495 case SkXfermode::kSrcOut_Mode:
496 case SkXfermode::kDstIn_Mode:
497 case SkXfermode::kDstOut_Mode:
498 case SkXfermode::kSrcATop_Mode:
499 case SkXfermode::kDstATop_Mode:
500 case SkXfermode::kModulate_Mode:
501 return true;
502 default:
503 return false;
504 }
505 }
506
507 /* Returns true unless we only need the shape of the drawing. */
508 bool needSource() {
509 if (fXfermode == SkXfermode::kClear_Mode) {
510 return false;
511 }
512 return true;
513 }
514
515 /* If the shape is different than the alpha component of the content, then
516 * setShape should be called with the shape. In particular, images and
517 * devices have rectangular shape.
518 */
519 void setShape(const SkPath& shape) {
520 fShape = shape;
521 }
522
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000523private:
524 SkPDFDevice* fDevice;
halcanary2be7e012016-03-28 11:58:08 -0700525 SkPDFDevice::ContentEntry* fContentEntry;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000526 SkXfermode::Mode fXfermode;
halcanarydabd4f02016-08-03 11:16:56 -0700527 sk_sp<SkPDFObject> fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000528 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000529
530 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
531 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000532 // Shape has to be flatten before we get here.
533 if (matrix.hasPerspective()) {
534 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000535 return;
536 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000537 if (paint.getXfermode()) {
538 paint.getXfermode()->asMode(&fXfermode);
539 }
540 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
541 matrix, paint, hasText,
542 &fDstFormXObject);
543 }
544};
545
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000546////////////////////////////////////////////////////////////////////////////////
547
halcanary989da4a2016-03-21 14:33:17 -0700548SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
reed589a39e2016-08-20 07:59:19 -0700549 : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()),
550 SkSurfaceProps(0, kUnknown_SkPixelGeometry))
robertphillips9a53fd72015-06-22 09:46:59 -0700551 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800552 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanarya1f1ee92015-02-20 06:17:26 -0800553 , fRasterDpi(rasterDpi)
halcanary989da4a2016-03-21 14:33:17 -0700554 , fDocument(doc) {
halcanarya1f1ee92015-02-20 06:17:26 -0800555 SkASSERT(pageSize.width() > 0);
556 SkASSERT(pageSize.height() > 0);
robertphillips1f3923e2016-07-21 07:17:54 -0700557
halcanarya1f1ee92015-02-20 06:17:26 -0800558 if (flip) {
559 // Skia generally uses the top left as the origin but PDF
560 // natively has the origin at the bottom left. This matrix
561 // corrects for that. But that only needs to be done once, we
562 // don't do it when layering.
563 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
564 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
565 } else {
566 fInitialTransform.setIdentity();
567 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000568}
569
570SkPDFDevice::~SkPDFDevice() {
halcanary3c35fb32016-06-30 11:55:07 -0700571 this->cleanUp();
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000572}
573
574void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700575 fContentEntries.reset();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000576}
577
halcanary3c35fb32016-06-30 11:55:07 -0700578void SkPDFDevice::cleanUp() {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000579 fGraphicStateResources.unrefAll();
580 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000581 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000582 fShaderResources.unrefAll();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000583}
584
reedf70b5312016-03-04 16:36:20 -0800585void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
586 SkData* value) {
587 if (0 == rect.width() && 0 == rect.height()) {
588 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
589 } else {
590 SkPath path;
591 path.addRect(rect);
592 handlePathAnnotation(path, d, key, value);
593 }
594}
595
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000596void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000597 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700598 replace_srcmode_on_opaque_paint(&newPaint);
599
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000600 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000601 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000602 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000603}
604
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000605void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
halcanary2be7e012016-03-28 11:58:08 -0700606 SkPDFDevice::ContentEntry* contentEntry) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000607 if (!contentEntry) {
608 return;
609 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000610 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
611 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000612 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000613 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000614 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000615 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000616 inverse.mapRect(&bbox);
617
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000618 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000619 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000620 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000621}
622
halcanary682ee012016-01-28 10:59:34 -0800623void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700624 SkCanvas::PointMode mode,
625 size_t count,
626 const SkPoint* points,
627 const SkPaint& srcPaint) {
628 SkPaint passedPaint = srcPaint;
629 replace_srcmode_on_opaque_paint(&passedPaint);
630
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000631 if (count == 0) {
632 return;
633 }
634
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000635 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
636 // We only use this when there's a path effect because of the overhead
637 // of multiple calls to setUpContentEntry it causes.
638 if (passedPaint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700639 if (d.fRC->isEmpty()) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000640 return;
641 }
642 SkDraw pointDraw(d);
643 pointDraw.fDevice = this;
644 pointDraw.drawPoints(mode, count, points, passedPaint, true);
645 return;
646 }
647
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000648 const SkPaint* paint = &passedPaint;
649 SkPaint modifiedPaint;
650
651 if (mode == SkCanvas::kPoints_PointMode &&
652 paint->getStrokeCap() != SkPaint::kRound_Cap) {
653 modifiedPaint = *paint;
654 paint = &modifiedPaint;
655 if (paint->getStrokeWidth()) {
656 // PDF won't draw a single point with square/butt caps because the
657 // orientation is ambiguous. Draw a rectangle instead.
658 modifiedPaint.setStyle(SkPaint::kFill_Style);
659 SkScalar strokeWidth = paint->getStrokeWidth();
660 SkScalar halfStroke = SkScalarHalf(strokeWidth);
661 for (size_t i = 0; i < count; i++) {
662 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
663 r.inset(-halfStroke, -halfStroke);
664 drawRect(d, r, modifiedPaint);
665 }
666 return;
667 } else {
668 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
669 }
670 }
671
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000672 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000673 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000674 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000675 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000676
677 switch (mode) {
678 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000679 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000680 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000681 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000682 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000683 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000684 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000685 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000686 break;
687 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000688 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000689 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000690 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000691 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000692 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000693 &content.entry()->fContent);
694 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000695 }
696 break;
697 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000698 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
699 for (size_t i = 0; i < count; i++) {
700 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000701 &content.entry()->fContent);
702 SkPDFUtils::ClosePath(&content.entry()->fContent);
703 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000704 }
705 break;
706 default:
707 SkASSERT(false);
708 }
709}
710
halcanary8103a342016-03-08 15:10:16 -0800711static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800712 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700713 annotation->insertName("Subtype", "Link");
halcanary488165e2016-04-22 06:10:21 -0700714 annotation->insertInt("F", 4); // required by ISO 19005
wangxianzhud76665d2015-07-17 17:23:15 -0700715
halcanaryece83922016-03-08 08:32:12 -0800716 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700717 border->reserve(3);
718 border->appendInt(0); // Horizontal corner radius.
719 border->appendInt(0); // Vertical corner radius.
720 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800721 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700722
halcanaryece83922016-03-08 08:32:12 -0800723 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700724 rect->reserve(4);
725 rect->appendScalar(translatedRect.fLeft);
726 rect->appendScalar(translatedRect.fTop);
727 rect->appendScalar(translatedRect.fRight);
728 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800729 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700730
halcanary8103a342016-03-08 15:10:16 -0800731 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700732}
733
halcanary8103a342016-03-08 15:10:16 -0800734static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700735 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700736 SkString url(static_cast<const char *>(urlData->data()),
737 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800738 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700739 action->insertName("S", "URI");
740 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800741 annotation->insertObject("A", std::move(action));
742 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700743}
744
halcanary8103a342016-03-08 15:10:16 -0800745static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
746 const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700747 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700748 SkString name(static_cast<const char *>(nameData->data()),
749 nameData->size() - 1);
750 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800751 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700752}
753
halcanary682ee012016-01-28 10:59:34 -0800754void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700755 const SkRect& rect,
756 const SkPaint& srcPaint) {
757 SkPaint paint = srcPaint;
758 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000759 SkRect r = rect;
760 r.sort();
761
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000762 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700763 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000764 return;
765 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000766 SkPath path;
767 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700768 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000769 return;
770 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000771
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000772 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000773 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000774 return;
775 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000776 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000777 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000778 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000779}
780
halcanarya6814332015-05-27 08:53:36 -0700781void SkPDFDevice::drawRRect(const SkDraw& draw,
782 const SkRRect& rrect,
783 const SkPaint& srcPaint) {
784 SkPaint paint = srcPaint;
785 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000786 SkPath path;
787 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700788 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000789}
790
halcanarya6814332015-05-27 08:53:36 -0700791void SkPDFDevice::drawOval(const SkDraw& draw,
792 const SkRect& oval,
793 const SkPaint& srcPaint) {
794 SkPaint paint = srcPaint;
795 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700796 SkPath path;
797 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700798 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700799}
800
halcanary682ee012016-01-28 10:59:34 -0800801void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700802 const SkPath& origPath,
803 const SkPaint& srcPaint,
804 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000805 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700806 SkPaint paint = srcPaint;
807 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800808 SkPath modifiedPath;
809 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000810
811 SkMatrix matrix = *d.fMatrix;
812 if (prePathMatrix) {
813 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800814 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000815 pathPtr = &modifiedPath;
816 pathIsMutable = true;
817 }
halcanary682ee012016-01-28 10:59:34 -0800818 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000819 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000820 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000821 }
822 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000823
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000824 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700825 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000826 return;
827 }
halcanary682ee012016-01-28 10:59:34 -0800828 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000829 pathPtr = &modifiedPath;
830 pathIsMutable = true;
831 }
halcanary682ee012016-01-28 10:59:34 -0800832 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000833
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000834 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700835 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000836 if (fill) {
837 noEffectPaint.setStyle(SkPaint::kFill_Style);
838 } else {
839 noEffectPaint.setStyle(SkPaint::kStroke_Style);
840 noEffectPaint.setStrokeWidth(0);
841 }
halcanary96fcdcc2015-08-27 07:41:13 -0700842 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000843 return;
844 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000845
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000846 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000847 return;
848 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000849
reed1e7f5e72016-04-27 07:49:17 -0700850 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000851 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000852 return;
853 }
halcanary8b2bc252015-10-06 09:41:47 -0700854 bool consumeDegeratePathSegments =
855 paint.getStyle() == SkPaint::kFill_Style ||
856 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
857 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000858 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -0700859 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000860 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000861 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000862 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000863}
864
halcanary7a14b312015-10-01 07:28:13 -0700865void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
866 const SkBitmap& bitmap,
867 const SkRect* src,
868 const SkRect& dst,
869 const SkPaint& srcPaint,
870 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700871 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -0700872}
873
874void SkPDFDevice::drawBitmap(const SkDraw& d,
875 const SkBitmap& bitmap,
876 const SkMatrix& matrix,
877 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -0700878 SkPaint paint = srcPaint;
879 if (bitmap.isOpaque()) {
880 replace_srcmode_on_opaque_paint(&paint);
881 }
882
reed1e7f5e72016-04-27 07:49:17 -0700883 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700884 return;
885 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +0000886
halcanary7a14b312015-10-01 07:28:13 -0700887 SkMatrix transform = matrix;
888 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -0700889 SkImageBitmap imageBitmap(bitmap);
890 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700891 transform, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700892}
893
894void SkPDFDevice::drawSprite(const SkDraw& d,
895 const SkBitmap& bitmap,
896 int x,
897 int y,
898 const SkPaint& srcPaint) {
899 SkPaint paint = srcPaint;
900 if (bitmap.isOpaque()) {
901 replace_srcmode_on_opaque_paint(&paint);
902 }
903
reed1e7f5e72016-04-27 07:49:17 -0700904 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700905 return;
906 }
907
908 SkMatrix matrix;
909 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -0700910 SkImageBitmap imageBitmap(bitmap);
911 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700912 matrix, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700913}
914
915void SkPDFDevice::drawImage(const SkDraw& draw,
916 const SkImage* image,
917 SkScalar x,
918 SkScalar y,
919 const SkPaint& srcPaint) {
920 SkPaint paint = srcPaint;
921 if (!image) {
922 return;
923 }
924 if (image->isOpaque()) {
925 replace_srcmode_on_opaque_paint(&paint);
926 }
reed1e7f5e72016-04-27 07:49:17 -0700927 if (draw.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700928 return;
929 }
930 SkMatrix transform = SkMatrix::MakeTrans(x, y);
931 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -0700932 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
933 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700934 transform, draw.fClipStack, draw.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700935}
936
937void SkPDFDevice::drawImageRect(const SkDraw& draw,
938 const SkImage* image,
939 const SkRect* src,
940 const SkRect& dst,
941 const SkPaint& srcPaint,
942 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700943 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000944}
945
halcanaryf0c30f52016-07-15 13:35:45 -0700946namespace {
947class GlyphPositioner {
948public:
949 GlyphPositioner(SkDynamicMemoryWStream* content,
950 SkScalar textSkewX,
halcanary4ed2f012016-08-15 18:40:07 -0700951 bool wideChars,
952 bool defaultPositioning,
953 SkPoint origin)
halcanaryf0c30f52016-07-15 13:35:45 -0700954 : fContent(content)
halcanary4871f222016-08-26 13:17:44 -0700955 , fCurrentMatrixOrigin{0.0f, 0.0f}
halcanaryf0c30f52016-07-15 13:35:45 -0700956 , fXAdvance(0.0f)
957 , fWideChars(wideChars)
halcanary4ed2f012016-08-15 18:40:07 -0700958 , fInText(false)
959 , fDefaultPositioning(defaultPositioning) {
halcanary4871f222016-08-26 13:17:44 -0700960 // Flip the text about the x-axis to account for origin swap and include
961 // the passed parameters.
962 fContent->writeText("1 0 ");
963 SkPDFUtils::AppendScalar(0 - textSkewX, fContent);
964 fContent->writeText(" -1 ");
965 SkPDFUtils::AppendScalar(origin.x(), fContent);
966 fContent->writeText(" ");
967 SkPDFUtils::AppendScalar(origin.y(), fContent);
968 fContent->writeText(" Tm\n");
halcanaryf0c30f52016-07-15 13:35:45 -0700969 }
halcanary4871f222016-08-26 13:17:44 -0700970 ~GlyphPositioner() { this->flush(); }
halcanaryf0c30f52016-07-15 13:35:45 -0700971 void flush() {
972 if (fInText) {
973 fContent->writeText("> Tj\n");
974 fInText = false;
975 }
976 }
977 void setWideChars(bool wideChars) {
978 if (fWideChars != wideChars) {
979 SkASSERT(!fInText);
halcanary4ed2f012016-08-15 18:40:07 -0700980 SkASSERT(fWideChars == wideChars);
halcanaryf0c30f52016-07-15 13:35:45 -0700981 fWideChars = wideChars;
982 }
983 }
halcanary4871f222016-08-26 13:17:44 -0700984 void writeGlyph(SkPoint xy,
halcanaryf0c30f52016-07-15 13:35:45 -0700985 SkScalar advanceWidth,
986 uint16_t glyph) {
halcanary4ed2f012016-08-15 18:40:07 -0700987 if (!fDefaultPositioning) {
halcanary4871f222016-08-26 13:17:44 -0700988 SkPoint position = xy - fCurrentMatrixOrigin;
989 if (position != SkPoint{fXAdvance, 0}) {
halcanary4ed2f012016-08-15 18:40:07 -0700990 this->flush();
halcanary4871f222016-08-26 13:17:44 -0700991 SkPDFUtils::AppendScalar(position.x(), fContent);
halcanary4ed2f012016-08-15 18:40:07 -0700992 fContent->writeText(" ");
halcanary4871f222016-08-26 13:17:44 -0700993 SkPDFUtils::AppendScalar(-position.y(), fContent);
halcanary4ed2f012016-08-15 18:40:07 -0700994 fContent->writeText(" Td ");
halcanary4871f222016-08-26 13:17:44 -0700995 fCurrentMatrixOrigin = xy;
halcanary4ed2f012016-08-15 18:40:07 -0700996 fXAdvance = 0;
997 }
998 fXAdvance += advanceWidth;
halcanaryf0c30f52016-07-15 13:35:45 -0700999 }
1000 if (!fInText) {
1001 fContent->writeText("<");
1002 fInText = true;
1003 }
1004 if (fWideChars) {
1005 SkPDFUtils::WriteUInt16BE(fContent, glyph);
1006 } else {
1007 SkASSERT(0 == glyph >> 8);
1008 SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph));
1009 }
halcanaryf0c30f52016-07-15 13:35:45 -07001010 }
1011
1012private:
1013 SkDynamicMemoryWStream* fContent;
halcanary4871f222016-08-26 13:17:44 -07001014 SkPoint fCurrentMatrixOrigin;
halcanaryf0c30f52016-07-15 13:35:45 -07001015 SkScalar fXAdvance;
1016 bool fWideChars;
1017 bool fInText;
halcanary4ed2f012016-08-15 18:40:07 -07001018 const bool fDefaultPositioning;
halcanaryf0c30f52016-07-15 13:35:45 -07001019};
1020} // namespace
1021
halcanary66a82f32015-10-12 13:05:04 -07001022static void draw_transparent_text(SkPDFDevice* device,
1023 const SkDraw& d,
1024 const void* text, size_t len,
1025 SkScalar x, SkScalar y,
1026 const SkPaint& srcPaint) {
halcanary4871f222016-08-26 13:17:44 -07001027 sk_sp<SkTypeface> defaultFace = SkTypeface::MakeDefault();
1028 if (!SkPDFFont::CanEmbedTypeface(defaultFace.get(), device->getCanon())) {
halcanary4ed2f012016-08-15 18:40:07 -07001029 SkDebugf("SkPDF: default typeface should be embeddable");
halcanary66a82f32015-10-12 13:05:04 -07001030 return; // Avoid infinite loop in release.
1031 }
halcanary4871f222016-08-26 13:17:44 -07001032 SkPaint transparent;
1033 transparent.setTypeface(std::move(defaultFace));
halcanary66a82f32015-10-12 13:05:04 -07001034 transparent.setTextSize(srcPaint.getTextSize());
1035 transparent.setColor(SK_ColorTRANSPARENT);
1036 switch (srcPaint.getTextEncoding()) {
1037 case SkPaint::kGlyphID_TextEncoding: {
1038 // Since a glyphId<->Unicode mapping is typeface-specific,
1039 // map back to Unicode first.
1040 size_t glyphCount = len / 2;
1041 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1042 srcPaint.glyphsToUnichars(
1043 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1044 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
halcanary4ed2f012016-08-15 18:40:07 -07001045 // TODO(halcanary): deal with case where default typeface
1046 // does not have glyphs for these unicode code points.
halcanary66a82f32015-10-12 13:05:04 -07001047 device->drawText(d, &unichars[0],
1048 glyphCount * sizeof(SkUnichar),
1049 x, y, transparent);
1050 break;
1051 }
1052 case SkPaint::kUTF8_TextEncoding:
1053 case SkPaint::kUTF16_TextEncoding:
1054 case SkPaint::kUTF32_TextEncoding:
1055 transparent.setTextEncoding(srcPaint.getTextEncoding());
1056 device->drawText(d, text, len, x, y, transparent);
1057 break;
1058 default:
1059 SkFAIL("unknown text encoding");
1060 }
1061}
1062
halcanary4ed2f012016-08-15 18:40:07 -07001063void SkPDFDevice::internalDrawText(
1064 const SkDraw& d, const void* sourceText, size_t sourceByteCount,
1065 const SkScalar pos[], SkTextBlob::GlyphPositioning positioning,
1066 SkPoint offset, const SkPaint& srcPaint) {
1067 NOT_IMPLEMENTED(srcPaint.getMaskFilter() != nullptr, false);
1068 if (srcPaint.getMaskFilter() != nullptr) {
1069 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1070 // making text unreadable (e.g. same text twice when using CSS shadows).
1071 return;
1072 }
halcanaryea17dfe2016-08-24 09:20:57 -07001073 NOT_IMPLEMENTED(srcPaint.isVerticalText(), false);
1074 if (srcPaint.isVerticalText()) {
1075 // Don't pretend we support drawing vertical text. It is not
1076 // clear to me how to switch to "vertical writing" mode in PDF.
1077 // Currently neither Chromium or Android set this flag.
1078 // https://bug.skia.org/5665
1079 return;
1080 }
halcanary4ed2f012016-08-15 18:40:07 -07001081 SkPaint paint = calculate_text_paint(srcPaint);
1082 replace_srcmode_on_opaque_paint(&paint);
1083 if (!paint.getTypeface()) {
1084 paint.setTypeface(SkTypeface::MakeDefault());
1085 }
1086 SkTypeface* typeface = paint.getTypeface();
1087 if (!typeface) {
1088 SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n");
1089 return;
1090 }
halcanary4871f222016-08-26 13:17:44 -07001091
1092 const SkAdvancedTypefaceMetrics* metrics =
1093 SkPDFFont::GetMetrics(typeface, fDocument->canon());
1094 if (!metrics) {
halcanary4ed2f012016-08-15 18:40:07 -07001095 return;
1096 }
halcanary4871f222016-08-26 13:17:44 -07001097 // TODO(halcanary): use metrics->fGlyphToUnicode to check Unicode mapping.
1098 const SkGlyphID maxGlyphID = metrics->fLastGlyphID;
halcanary4ed2f012016-08-15 18:40:07 -07001099 if (!SkPDFFont::CanEmbedTypeface(typeface, fDocument->canon())) {
1100 SkPath path; // https://bug.skia.org/3866
1101 paint.getTextPath(sourceText, sourceByteCount,
1102 offset.x(), offset.y(), &path);
robertphillips5ba165e2016-08-15 15:36:58 -07001103 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1104 // Draw text transparently to make it copyable/searchable/accessable.
halcanary4ed2f012016-08-15 18:40:07 -07001105 draw_transparent_text(this, d, sourceText, sourceByteCount,
1106 offset.x(), offset.y(), paint);
robertphillips5ba165e2016-08-15 15:36:58 -07001107 return;
1108 }
halcanary4ed2f012016-08-15 18:40:07 -07001109 int glyphCount = paint.textToGlyphs(sourceText, sourceByteCount, nullptr);
halcanary4871f222016-08-26 13:17:44 -07001110 if (glyphCount <= 0) {
1111 return;
1112 }
1113 SkAutoSTMalloc<128, SkGlyphID> glyphStorage;
1114 const SkGlyphID* glyphs = nullptr;
halcanary4ed2f012016-08-15 18:40:07 -07001115 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
halcanary4871f222016-08-26 13:17:44 -07001116 glyphs = (const SkGlyphID*)sourceText;
1117 // validate input later.
halcanary4ed2f012016-08-15 18:40:07 -07001118 } else {
halcanary4871f222016-08-26 13:17:44 -07001119 glyphStorage.reset(SkToSizeT(glyphCount));
1120 (void)paint.textToGlyphs(sourceText, sourceByteCount, glyphStorage.get());
1121 glyphs = glyphStorage.get();
halcanary4ed2f012016-08-15 18:40:07 -07001122 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1123 }
robertphillips5ba165e2016-08-15 15:36:58 -07001124
halcanary4ed2f012016-08-15 18:40:07 -07001125 bool defaultPositioning = (positioning == SkTextBlob::kDefault_Positioning);
halcanary9df5a4c2016-08-24 10:08:13 -07001126 paint.setHinting(SkPaint::kNo_Hinting);
halcanary4ed2f012016-08-15 18:40:07 -07001127 SkAutoGlyphCache glyphCache(paint, nullptr, nullptr);
1128
1129 SkPaint::Align alignment = paint.getTextAlign();
halcanary4871f222016-08-26 13:17:44 -07001130 float alignmentFactor = SkPaint::kLeft_Align == alignment ? 0.0f :
1131 SkPaint::kCenter_Align == alignment ? -0.5f :
1132 /* SkPaint::kRight_Align */ -1.0f;
halcanary4ed2f012016-08-15 18:40:07 -07001133 if (defaultPositioning && alignment != SkPaint::kLeft_Align) {
halcanary4871f222016-08-26 13:17:44 -07001134 SkScalar advance = 0;
halcanary4ed2f012016-08-15 18:40:07 -07001135 for (int i = 0; i < glyphCount; ++i) {
1136 advance += glyphCache->getGlyphIDAdvance(glyphs[i]).fAdvanceX;
1137 }
halcanary4871f222016-08-26 13:17:44 -07001138 offset.offset(alignmentFactor * advance, 0);
halcanary6059dc32016-08-15 11:45:36 -07001139 }
halcanary4ed2f012016-08-15 18:40:07 -07001140 ScopedContentEntry content(this, d, paint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001141 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001142 return;
1143 }
halcanary4ed2f012016-08-15 18:40:07 -07001144 SkDynamicMemoryWStream* out = &content.entry()->fContent;
halcanary4871f222016-08-26 13:17:44 -07001145 SkScalar textSize = paint.getTextSize();
1146
1147 int index = 0;
1148 while (glyphs[index] > maxGlyphID) { // Invalid glyphID for this font.
1149 ++index; // Skip this glyphID
1150 if (index == glyphCount) {
1151 return; // all glyphIDs were bad.
1152 }
robertphillips5ba165e2016-08-15 15:36:58 -07001153 }
halcanary4871f222016-08-26 13:17:44 -07001154
1155 out->writeText("BT\n");
1156 SK_AT_SCOPE_EXIT(out->writeText("ET\n"));
1157
1158 SkPDFFont* font = this->updateFont(
1159 typeface, textSize, glyphs[index], content.entry());
1160 SkASSERT(font); // All preconditions for SkPDFFont::GetFontResource are met.
1161 if (!font) { return; }
1162
halcanary4ed2f012016-08-15 18:40:07 -07001163 GlyphPositioner glyphPositioner(out,
1164 paint.getTextSkewX(),
1165 font->multiByteGlyphs(),
1166 defaultPositioning,
1167 offset);
halcanary4ed2f012016-08-15 18:40:07 -07001168
halcanary4871f222016-08-26 13:17:44 -07001169 while (index < glyphCount) {
1170 int stretch = font->countStretch(&glyphs[index], glyphCount - index, maxGlyphID);
1171 SkASSERT(index + stretch <= glyphCount);
halcanary4ed2f012016-08-15 18:40:07 -07001172 if (stretch < 1) {
halcanary4ed2f012016-08-15 18:40:07 -07001173 // The current pdf font cannot encode the next glyph.
1174 // Try to get a pdf font which can encode the next glyph.
robertphillips5ba165e2016-08-15 15:36:58 -07001175 glyphPositioner.flush();
halcanary4871f222016-08-26 13:17:44 -07001176 // first, validate the next glyph
1177 while (glyphs[index] > maxGlyphID) {
1178 ++index; // Skip this glyphID
1179 if (index == glyphCount) {
1180 return; // all remainng glyphIDs were bad.
1181 }
robertphillips5ba165e2016-08-15 15:36:58 -07001182 }
halcanary4871f222016-08-26 13:17:44 -07001183 SkASSERT(index < glyphCount);
1184 font = this->updateFont(typeface, textSize, glyphs[index], content.entry());
1185 SkASSERT(font); // preconditions for SkPDFFont::GetFontResource met.
1186 if (!font) { return; }
robertphillips5ba165e2016-08-15 15:36:58 -07001187 glyphPositioner.setWideChars(font->multiByteGlyphs());
halcanary4871f222016-08-26 13:17:44 -07001188 // Get stretch for this new font.
1189 stretch = font->countStretch(&glyphs[index], glyphCount - index, maxGlyphID);
halcanary4ed2f012016-08-15 18:40:07 -07001190 if (stretch < 1) {
robertphillips5ba165e2016-08-15 15:36:58 -07001191 SkDEBUGFAIL("PDF could not encode glyph.");
halcanary4ed2f012016-08-15 18:40:07 -07001192 return;
robertphillips5ba165e2016-08-15 15:36:58 -07001193 }
1194 }
halcanary4871f222016-08-26 13:17:44 -07001195 while (stretch-- > 0) {
1196 SkGlyphID gid = glyphs[index];
1197 if (gid <= maxGlyphID) {
1198 font->noteGlyphUsage(gid);
1199 SkGlyphID encodedGlyph = font->glyphToPDFFontEncoding(gid);
1200 if (defaultPositioning) {
1201 glyphPositioner.writeGlyph(SkPoint{0, 0}, 0, encodedGlyph);
1202 } else {
1203 SkScalar advance = glyphCache->getGlyphIDAdvance(gid).fAdvanceX;
1204 SkPoint xy = SkTextBlob::kFull_Positioning == positioning
1205 ? SkPoint{pos[2 * index], pos[2 * index + 1]}
1206 : SkPoint{pos[index], 0};
1207 if (alignment != SkPaint::kLeft_Align) {
1208 xy.offset(alignmentFactor * advance, 0);
1209 }
1210 glyphPositioner.writeGlyph(xy, advance, encodedGlyph);
halcanary4ed2f012016-08-15 18:40:07 -07001211 }
halcanary4ed2f012016-08-15 18:40:07 -07001212 }
halcanary4871f222016-08-26 13:17:44 -07001213 ++index;
halcanary4ed2f012016-08-15 18:40:07 -07001214 }
robertphillips5ba165e2016-08-15 15:36:58 -07001215 }
halcanary4ed2f012016-08-15 18:40:07 -07001216}
1217
1218void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
1219 SkScalar x, SkScalar y, const SkPaint& paint) {
1220 this->internalDrawText(d, text, len, nullptr, SkTextBlob::kDefault_Positioning,
1221 SkPoint{x, y}, paint);
1222}
1223
1224void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
1225 const SkScalar pos[], int scalarsPerPos,
1226 const SkPoint& offset, const SkPaint& paint) {
1227 this->internalDrawText(d, text, len, pos, (SkTextBlob::GlyphPositioning)scalarsPerPos,
1228 offset, paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001229}
1230
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001231void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001232 int vertexCount, const SkPoint verts[],
1233 const SkPoint texs[], const SkColor colors[],
1234 SkXfermode* xmode, const uint16_t indices[],
1235 int indexCount, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001236 if (d.fRC->isEmpty()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001237 return;
1238 }
reed@google.com85e143c2013-12-30 15:51:25 +00001239 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001240}
1241
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001242void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1243 int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001244 SkASSERT(!paint.getImageFilter());
1245
reed7503d602016-07-15 14:23:29 -07001246 // Check if the source device is really a bitmapdevice (because that's what we returned
1247 // from createDevice (likely due to an imagefilter)
1248 SkPixmap pmap;
1249 if (device->peekPixels(&pmap)) {
1250 SkBitmap bitmap;
1251 bitmap.installPixels(pmap);
reedcf5c8462016-07-20 12:28:40 -07001252 this->drawSprite(d, bitmap, x, y, paint);
reed7503d602016-07-15 14:23:29 -07001253 return;
1254 }
1255
fmalita6987dca2014-11-13 08:33:37 -08001256 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001257 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001258
1259 SkScalar scalarX = SkIntToScalar(x);
1260 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001261 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1262 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001263 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001264 }
halcanary91fcb3e2016-03-04 13:53:22 -08001265 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1266 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001267 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001268 }
halcanary91fcb3e2016-03-04 13:53:22 -08001269 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1270 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001271 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001272 }
1273
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001274 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001275 return;
1276 }
1277
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001278 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001279 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
reed1e7f5e72016-04-27 07:49:17 -07001280 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001281 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001282 return;
1283 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001284 if (content.needShape()) {
1285 SkPath shape;
1286 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001287 SkIntToScalar(device->width()),
1288 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001289 content.setShape(shape);
1290 }
1291 if (!content.needSource()) {
1292 return;
1293 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001294
halcanary4b1e17e2016-07-27 14:49:46 -07001295 sk_sp<SkPDFObject> xObject = pdfDevice->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001296 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001297 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001298}
1299
reede8f30622016-03-23 18:59:25 -07001300sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1301 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001302}
1303
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001304
halcanary8103a342016-03-08 15:10:16 -08001305sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001306 SkTDArray<SkPDFObject*> fonts;
1307 fonts.setReserve(fFontResources.count());
1308 for (SkPDFFont* font : fFontResources) {
1309 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001310 }
halcanary8103a342016-03-08 15:10:16 -08001311 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001312 &fGraphicStateResources,
1313 &fShaderResources,
1314 &fXObjectResources,
1315 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001316}
1317
halcanary8103a342016-03-08 15:10:16 -08001318sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001319 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001320 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001321 mediaBox->appendInt(0);
1322 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001323 mediaBox->appendInt(fPageSize.width());
1324 mediaBox->appendInt(fPageSize.height());
1325 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001326}
1327
mtklein5f939ab2016-03-16 10:28:35 -07001328std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001329 SkDynamicMemoryWStream buffer;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001330 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanaryafdc1772016-08-23 09:02:12 -07001331 SkPDFUtils::AppendTransform(fInitialTransform, &buffer);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001332 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001333
halcanaryafdc1772016-08-23 09:02:12 -07001334 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, &buffer);
halcanary2be7e012016-03-28 11:58:08 -07001335 for (const auto& entry : fContentEntries) {
1336 SkPoint translation;
1337 translation.iset(this->getOrigin());
1338 translation.negate();
1339 gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion,
1340 translation);
1341 gsState.updateMatrix(entry.fState.fMatrix);
1342 gsState.updateDrawingState(entry.fState);
1343
halcanaryafdc1772016-08-23 09:02:12 -07001344 entry.fContent.writeToStream(&buffer);
halcanary2be7e012016-03-28 11:58:08 -07001345 }
1346 gsState.drainStack();
halcanaryafdc1772016-08-23 09:02:12 -07001347
1348 return std::unique_ptr<SkStreamAsset>(
1349 buffer.bytesWritten() > 0
1350 ? buffer.detachAsStream()
1351 : new SkMemoryStream);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001352}
1353
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001354/* Draws an inverse filled path by using Path Ops to compute the positive
1355 * inverse using the current clip as the inverse bounds.
1356 * Return true if this was an inverse path and was properly handled,
1357 * otherwise returns false and the normal drawing routine should continue,
1358 * either as a (incorrect) fallback or because the path was not inverse
1359 * in the first place.
1360 */
1361bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001362 const SkPaint& paint, bool pathIsMutable,
1363 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001364 if (!origPath.isInverseFillType()) {
1365 return false;
1366 }
1367
reed1e7f5e72016-04-27 07:49:17 -07001368 if (d.fRC->isEmpty()) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001369 return false;
1370 }
1371
1372 SkPath modifiedPath;
1373 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1374 SkPaint noInversePaint(paint);
1375
1376 // Merge stroking operations into final path.
1377 if (SkPaint::kStroke_Style == paint.getStyle() ||
1378 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1379 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1380 if (doFillPath) {
1381 noInversePaint.setStyle(SkPaint::kFill_Style);
1382 noInversePaint.setStrokeWidth(0);
1383 pathPtr = &modifiedPath;
1384 } else {
1385 // To be consistent with the raster output, hairline strokes
1386 // are rendered as non-inverted.
1387 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001388 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001389 return true;
1390 }
1391 }
1392
1393 // Get bounds of clip in current transform space
1394 // (clip bounds are given in device space).
1395 SkRect bounds;
1396 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001397 SkMatrix totalMatrix = *d.fMatrix;
1398 if (prePathMatrix) {
1399 totalMatrix.preConcat(*prePathMatrix);
1400 }
1401 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001402 return false;
1403 }
reed1e7f5e72016-04-27 07:49:17 -07001404 bounds.set(d.fRC->getBounds());
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001405 transformInverse.mapRect(&bounds);
1406
1407 // Extend the bounds by the line width (plus some padding)
1408 // so the edge doesn't cause a visible stroke.
1409 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1410 paint.getStrokeWidth() + SK_Scalar1);
1411
1412 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1413 return false;
1414 }
1415
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001416 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001417 return true;
1418}
1419
reedf70b5312016-03-04 16:36:20 -08001420void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001421 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001422 const char key[], SkData* value) {
1423 if (!value) {
1424 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001425 }
reedf70b5312016-03-04 16:36:20 -08001426
1427 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1428 SkPoint transformedPoint;
1429 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1430 fNamedDestinations.emplace_back(value, transformedPoint);
1431 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001432}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001433
reedf70b5312016-03-04 16:36:20 -08001434void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001435 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001436 const char key[], SkData* value) {
1437 if (!value) {
1438 return;
1439 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001440
1441 SkPath transformedPath = path;
1442 transformedPath.transform(*d.fMatrix);
1443 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001444 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1445 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001446 SkRect transformedRect = SkRect::Make(clip.getBounds());
1447
reedf70b5312016-03-04 16:36:20 -08001448 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001449 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001450 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001451 }
reedf70b5312016-03-04 16:36:20 -08001452 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001453 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001454 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001455 }
reed16108352016-03-03 09:14:36 -08001456 }
halcanary438de492015-04-28 06:21:01 -07001457}
1458
wangxianzhuef6c50a2015-09-17 20:38:02 -07001459void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1460 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001461 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001462 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001463 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001464 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001465 }
halcanary91fcb3e2016-03-04 13:53:22 -08001466 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001467 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001468 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001469 array->appendObject(
1470 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001471 }
1472}
epoger@google.comb58772f2013-03-08 09:09:10 +00001473
halcanary6d622702015-03-25 08:45:42 -07001474void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001475 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001476 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001477 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001478 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001479 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001480 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001481 pdfDest->appendScalar(p.x());
1482 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001483 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001484 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001485 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001486 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001487}
1488
halcanary4b1e17e2016-07-27 14:49:46 -07001489sk_sp<SkPDFObject> SkPDFDevice::makeFormXObjectFromDevice() {
halcanary5abbb442016-07-29 08:41:33 -07001490 SkMatrix inverseTransform = SkMatrix::I();
halcanaryafdc1772016-08-23 09:02:12 -07001491 if (!fInitialTransform.isIdentity()) {
1492 if (!fInitialTransform.invert(&inverseTransform)) {
halcanary5abbb442016-07-29 08:41:33 -07001493 SkDEBUGFAIL("Layer initial transform should be invertible.");
1494 inverseTransform.reset();
1495 }
1496 }
halcanary4b1e17e2016-07-27 14:49:46 -07001497 sk_sp<SkPDFObject> xobject =
1498 SkPDFMakeFormXObject(this->content(), this->copyMediaBox(),
halcanary5abbb442016-07-29 08:41:33 -07001499 this->makeResourceDict(), inverseTransform, nullptr);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001500 // We always draw the form xobjects that we create back into the device, so
1501 // we simply preserve the font usage instead of pulling it out and merging
1502 // it back in later.
halcanary4b1e17e2016-07-27 14:49:46 -07001503 this->cleanUp(); // Reset this device to have no content.
1504 this->init();
reed@google.comfc641d02012-09-20 17:52:20 +00001505 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001506}
1507
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001508void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
halcanarydabd4f02016-08-03 11:16:56 -07001509 sk_sp<SkPDFObject> mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001510 const SkClipStack* clipStack,
1511 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001512 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001513 bool invertClip) {
1514 if (clipRegion.isEmpty() && !invertClip) {
1515 return;
1516 }
1517
halcanary4b1e17e2016-07-27 14:49:46 -07001518 sk_sp<SkPDFDict> sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanarydabd4f02016-08-03 11:16:56 -07001519 std::move(mask), invertClip,
1520 SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001521
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001522 SkMatrix identity;
1523 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001524 SkPaint paint;
1525 paint.setXfermodeMode(mode);
1526 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001527 if (!content.entry()) {
1528 return;
1529 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001530 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001531 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001532 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001533
halcanary1437c1e2016-03-13 18:30:24 -07001534 // Call makeNoSmaskGraphicState() instead of
1535 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1536 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001537 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001538 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001539 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001540}
1541
halcanary2be7e012016-03-28 11:58:08 -07001542SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001543 const SkRegion& clipRegion,
1544 const SkMatrix& matrix,
1545 const SkPaint& paint,
1546 bool hasText,
halcanarydabd4f02016-08-03 11:16:56 -07001547 sk_sp<SkPDFObject>* dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001548 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001549 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001550 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001551 }
1552
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001553 // The clip stack can come from an SkDraw where it is technically optional.
1554 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001555 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001556 if (clipRegion == fExistingClipRegion) {
1557 clipStack = &fExistingClipStack;
1558 } else {
1559 // GraphicStackState::updateClip expects the clip stack to have
1560 // fExistingClip as a prefix, so start there, then set the clip
1561 // to the passed region.
1562 synthesizedClipStack = fExistingClipStack;
1563 SkPath clipPath;
1564 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001565 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1566 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001567 clipStack = &synthesizedClipStack;
1568 }
1569 }
1570
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001571 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1572 if (paint.getXfermode()) {
1573 paint.getXfermode()->asMode(&xfermode);
1574 }
1575
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001576 // For the following modes, we want to handle source and destination
1577 // separately, so make an object of what's already there.
1578 if (xfermode == SkXfermode::kClear_Mode ||
1579 xfermode == SkXfermode::kSrc_Mode ||
1580 xfermode == SkXfermode::kSrcIn_Mode ||
1581 xfermode == SkXfermode::kDstIn_Mode ||
1582 xfermode == SkXfermode::kSrcOut_Mode ||
1583 xfermode == SkXfermode::kDstOut_Mode ||
1584 xfermode == SkXfermode::kSrcATop_Mode ||
1585 xfermode == SkXfermode::kDstATop_Mode ||
1586 xfermode == SkXfermode::kModulate_Mode) {
1587 if (!isContentEmpty()) {
halcanarydabd4f02016-08-03 11:16:56 -07001588 *dst = this->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001589 SkASSERT(isContentEmpty());
1590 } else if (xfermode != SkXfermode::kSrc_Mode &&
1591 xfermode != SkXfermode::kSrcOut_Mode) {
1592 // Except for Src and SrcOut, if there isn't anything already there,
1593 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001594 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001595 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001596 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001597 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001598 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001599
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001600 // Dst xfer mode doesn't draw source at all.
1601 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001602 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001603 }
1604
halcanary2be7e012016-03-28 11:58:08 -07001605 SkPDFDevice::ContentEntry* entry;
1606 if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) {
1607 entry = fContentEntries.back();
1608 } else if (xfermode != SkXfermode::kDstOver_Mode) {
1609 entry = fContentEntries.emplace_back();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001610 } else {
halcanary2be7e012016-03-28 11:58:08 -07001611 entry = fContentEntries.emplace_front();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001612 }
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001613 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001614 hasText, &entry->fState);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001615 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001616}
1617
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001618void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
halcanarydabd4f02016-08-03 11:16:56 -07001619 sk_sp<SkPDFObject> dst,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001620 SkPath* shape) {
1621 if (xfermode != SkXfermode::kClear_Mode &&
1622 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001623 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001624 xfermode != SkXfermode::kSrcIn_Mode &&
1625 xfermode != SkXfermode::kDstIn_Mode &&
1626 xfermode != SkXfermode::kSrcOut_Mode &&
1627 xfermode != SkXfermode::kDstOut_Mode &&
1628 xfermode != SkXfermode::kSrcATop_Mode &&
1629 xfermode != SkXfermode::kDstATop_Mode &&
1630 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001631 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001632 return;
1633 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001634 if (xfermode == SkXfermode::kDstOver_Mode) {
1635 SkASSERT(!dst);
halcanary2be7e012016-03-28 11:58:08 -07001636 if (fContentEntries.front()->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001637 // For DstOver, an empty content entry was inserted before the rest
1638 // of the content entries. If nothing was drawn, it needs to be
1639 // removed.
halcanary2be7e012016-03-28 11:58:08 -07001640 fContentEntries.pop_front();
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001641 }
1642 return;
1643 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001644 if (!dst) {
1645 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1646 xfermode == SkXfermode::kSrcOut_Mode);
1647 return;
1648 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001649
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001650 SkASSERT(dst);
halcanary2be7e012016-03-28 11:58:08 -07001651 SkASSERT(fContentEntries.count() == 1);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001652 // Changing the current content into a form-xobject will destroy the clip
1653 // objects which is fine since the xobject will already be clipped. However
1654 // if source has shape, we need to clip it too, so a copy of the clip is
1655 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001656
1657 SkClipStack clipStack = fContentEntries.front()->fState.fClipStack;
1658 SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001659
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001660 SkMatrix identity;
1661 identity.reset();
1662 SkPaint stockPaint;
1663
halcanary4b1e17e2016-07-27 14:49:46 -07001664 sk_sp<SkPDFObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001665 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001666 // If nothing was drawn and there's no shape, then the draw was a
1667 // no-op, but dst needs to be restored for that to be true.
1668 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1669 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1670 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001671 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001672 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001673 ScopedContentEntry content(this, &fExistingClipStack,
1674 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001675 stockPaint);
halcanarydabd4f02016-08-03 11:16:56 -07001676 // TODO: addXObjectResource take sk_sp
1677 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()),
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001678 &content.entry()->fContent);
1679 return;
1680 } else {
1681 xfermode = SkXfermode::kClear_Mode;
1682 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001683 } else {
halcanary2be7e012016-03-28 11:58:08 -07001684 SkASSERT(fContentEntries.count() == 1);
halcanary4b1e17e2016-07-27 14:49:46 -07001685 srcFormXObject = this->makeFormXObjectFromDevice();
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001686 }
1687
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001688 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1689 // without alpha.
1690 if (xfermode == SkXfermode::kSrcATop_Mode) {
1691 // TODO(vandebo): In order to properly support SrcATop we have to track
1692 // the shape of what's been drawn at all times. It's the intersection of
1693 // the non-transparent parts of the device and the outlines (shape) of
1694 // all images and devices drawn.
1695 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001696 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001697 SkXfermode::kSrcOver_Mode, true);
1698 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001699 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001700 // Draw shape into a form-xobject.
reed1e7f5e72016-04-27 07:49:17 -07001701 SkRasterClip rc(clipRegion);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001702 SkDraw d;
1703 d.fMatrix = &identity;
reed1e7f5e72016-04-27 07:49:17 -07001704 d.fRC = &rc;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001705 d.fClipStack = &clipStack;
1706 SkPaint filledPaint;
1707 filledPaint.setColor(SK_ColorBLACK);
1708 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001709 this->drawPath(d, *shape, filledPaint, nullptr, true);
halcanarydabd4f02016-08-03 11:16:56 -07001710 drawFormXObjectWithMask(addXObjectResource(dst.get()),
1711 this->makeFormXObjectFromDevice(),
1712 &fExistingClipStack, fExistingClipRegion,
1713 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001714
halcanarydabd4f02016-08-03 11:16:56 -07001715 } else {
1716 drawFormXObjectWithMask(addXObjectResource(dst.get()), srcFormXObject,
1717 &fExistingClipStack, fExistingClipRegion,
1718 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001719 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001720 }
1721
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001722 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001723 return;
1724 } else if (xfermode == SkXfermode::kSrc_Mode ||
1725 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001726 ScopedContentEntry content(this, &fExistingClipStack,
1727 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001728 if (content.entry()) {
1729 SkPDFUtils::DrawFormXObject(
1730 this->addXObjectResource(srcFormXObject.get()),
1731 &content.entry()->fContent);
1732 }
1733 if (xfermode == SkXfermode::kSrc_Mode) {
1734 return;
1735 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001736 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001737 ScopedContentEntry content(this, &fExistingClipStack,
1738 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001739 if (content.entry()) {
halcanarydabd4f02016-08-03 11:16:56 -07001740 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()),
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001741 &content.entry()->fContent);
1742 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001743 }
1744
1745 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1746 xfermode == SkXfermode::kDstIn_Mode ||
1747 xfermode == SkXfermode::kSrcOut_Mode ||
1748 xfermode == SkXfermode::kDstOut_Mode ||
1749 xfermode == SkXfermode::kSrcATop_Mode ||
1750 xfermode == SkXfermode::kDstATop_Mode ||
1751 xfermode == SkXfermode::kModulate_Mode);
1752
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001753 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001754 xfermode == SkXfermode::kSrcOut_Mode ||
1755 xfermode == SkXfermode::kSrcATop_Mode) {
halcanarydabd4f02016-08-03 11:16:56 -07001756 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
1757 std::move(dst),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001758 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001759 SkXfermode::kSrcOver_Mode,
1760 xfermode == SkXfermode::kSrcOut_Mode);
halcanarydabd4f02016-08-03 11:16:56 -07001761 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001762 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001763 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
halcanarydabd4f02016-08-03 11:16:56 -07001764 int resourceID = addXObjectResource(dst.get());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001765 if (xfermode == SkXfermode::kModulate_Mode) {
1766 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
halcanarydabd4f02016-08-03 11:16:56 -07001767 std::move(dst), &fExistingClipStack,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001768 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001769 SkXfermode::kSrcOver_Mode, false);
1770 mode = SkXfermode::kMultiply_Mode;
1771 }
halcanarydabd4f02016-08-03 11:16:56 -07001772 drawFormXObjectWithMask(resourceID, std::move(srcFormXObject),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001773 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001774 xfermode == SkXfermode::kDstOut_Mode);
halcanarydabd4f02016-08-03 11:16:56 -07001775 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001776 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001777}
1778
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001779bool SkPDFDevice::isContentEmpty() {
halcanary2be7e012016-03-28 11:58:08 -07001780 if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) {
1781 SkASSERT(fContentEntries.count() <= 1);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001782 return true;
1783 }
1784 return false;
1785}
1786
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001787void SkPDFDevice::populateGraphicStateEntryFromPaint(
1788 const SkMatrix& matrix,
1789 const SkClipStack& clipStack,
1790 const SkRegion& clipRegion,
1791 const SkPaint& paint,
1792 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001793 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001794 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1795 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1796 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001797
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001798 entry->fMatrix = matrix;
1799 entry->fClipStack = clipStack;
1800 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001801 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1802 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001803
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001804 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001805 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001806 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001807 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001808 if (shader) {
1809 // PDF positions patterns relative to the initial transform, so
1810 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001811 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001812 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001813
1814 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001815 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001816 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001817
1818 // We need to apply the initial transform to bounds in order to get
1819 // bounds in a consistent coordinate system.
1820 SkRect boundsTemp;
1821 boundsTemp.set(bounds);
1822 fInitialTransform.mapRect(&boundsTemp);
1823 boundsTemp.roundOut(&bounds);
1824
halcanary792c80f2015-02-20 07:21:05 -08001825 SkScalar rasterScale =
1826 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
halcanarydabd4f02016-08-03 11:16:56 -07001827 pdfShader = SkPDFShader::GetPDFShader(
1828 fDocument, fRasterDpi, shader, transform, bounds, rasterScale);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001829
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001830 if (pdfShader.get()) {
1831 // pdfShader has been canonicalized so we can directly compare
1832 // pointers.
1833 int resourceIndex = fShaderResources.find(pdfShader.get());
1834 if (resourceIndex < 0) {
1835 resourceIndex = fShaderResources.count();
1836 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001837 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001838 }
1839 entry->fShaderIndex = resourceIndex;
1840 } else {
1841 // A color shader is treated as an invalid shader so we don't have
1842 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001843 SkShader::GradientInfo gradientInfo;
1844 SkColor gradientColor;
1845 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001846 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001847 gradientInfo.fColorCount = 1;
1848 if (shader->asAGradient(&gradientInfo) ==
1849 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001850 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1851 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001852 }
1853 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001854 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001855
halcanary48810a02016-03-07 14:57:50 -08001856 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001857 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001858 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001859 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001860 } else {
1861 SkPaint newPaint = paint;
1862 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001863 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001864 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001865 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001866 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001867 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001868
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001869 if (hasText) {
1870 entry->fTextScaleX = paint.getTextScaleX();
1871 entry->fTextFill = paint.getStyle();
1872 } else {
1873 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001874 }
1875}
1876
halcanarybe27a112015-04-01 13:31:19 -07001877int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001878 // Assumes that gs has been canonicalized (so we can directly compare
1879 // pointers).
1880 int result = fGraphicStateResources.find(gs);
1881 if (result < 0) {
1882 result = fGraphicStateResources.count();
1883 fGraphicStateResources.push(gs);
1884 gs->ref();
1885 }
1886 return result;
1887}
1888
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001889int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
halcanarydabd4f02016-08-03 11:16:56 -07001890 // TODO(halcanary): make this take a sk_sp<SkPDFObject>
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001891 // Assumes that xobject has been canonicalized (so we can directly compare
1892 // pointers).
1893 int result = fXObjectResources.find(xObject);
1894 if (result < 0) {
1895 result = fXObjectResources.count();
halcanarydabd4f02016-08-03 11:16:56 -07001896 fXObjectResources.push(SkRef(xObject));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001897 }
1898 return result;
1899}
1900
halcanary4871f222016-08-26 13:17:44 -07001901SkPDFFont* SkPDFDevice::updateFont(SkTypeface* typeface,
1902 SkScalar textSize,
1903 uint16_t glyphID,
1904 SkPDFDevice::ContentEntry* contentEntry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001905 if (contentEntry->fState.fFont == nullptr ||
halcanary4871f222016-08-26 13:17:44 -07001906 contentEntry->fState.fTextSize != textSize ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001907 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001908 int fontIndex = getFontResourceIndex(typeface, glyphID);
halcanary7e8d5d32016-08-12 07:59:38 -07001909 if (fontIndex < 0) {
halcanary4871f222016-08-26 13:17:44 -07001910 SkDebugf("SkPDF: Font error.");
1911 return nullptr;
halcanary7e8d5d32016-08-12 07:59:38 -07001912 }
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001913 contentEntry->fContent.writeText("/");
1914 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
1915 SkPDFResourceDict::kFont_ResourceType,
1916 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001917 contentEntry->fContent.writeText(" ");
halcanary4871f222016-08-26 13:17:44 -07001918 SkPDFUtils::AppendScalar(textSize, &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001919 contentEntry->fContent.writeText(" Tf\n");
1920 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001921 }
halcanary4871f222016-08-26 13:17:44 -07001922 return contentEntry->fState.fFont;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001923}
1924
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001925int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08001926 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07001927 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
halcanary7e8d5d32016-08-12 07:59:38 -07001928 if (!newFont) {
1929 return -1;
1930 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001931 int resourceIndex = fFontResources.find(newFont.get());
1932 if (resourceIndex < 0) {
halcanary530032a2016-08-18 14:22:52 -07001933 fDocument->registerFont(newFont.get());
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001934 resourceIndex = fFontResources.count();
halcanary530032a2016-08-18 14:22:52 -07001935 fFontResources.push(newFont.release());
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001936 }
1937 return resourceIndex;
1938}
1939
halcanary7a14b312015-10-01 07:28:13 -07001940static SkSize rect_to_size(const SkRect& r) {
1941 return SkSize::Make(r.width(), r.height());
1942}
1943
halcanarya50151d2016-03-25 11:57:49 -07001944static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
1945 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07001946 auto surface =
halcanarya50151d2016-03-25 11:57:49 -07001947 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
1948 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07001949 SkCanvas* canvas = surface->getCanvas();
1950 canvas->clear(SK_ColorTRANSPARENT);
1951 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07001952 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07001953 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07001954 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07001955 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07001956}
1957
1958////////////////////////////////////////////////////////////////////////////////
1959void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
1960 const SkClipStack* clipStack,
1961 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07001962 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07001963 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07001964 if (imageBitmap.dimensions().isZero()) {
1965 return;
1966 }
halcanary7a14b312015-10-01 07:28:13 -07001967 #ifdef SK_PDF_IMAGE_STATS
1968 gDrawImageCalls.fetch_add(1);
1969 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001970 SkMatrix matrix = origMatrix;
1971 SkRegion perspectiveBounds;
1972 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07001973 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001974
1975 // Rasterize the bitmap using perspective in a new bitmap.
1976 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001977 if (fRasterDpi == 0) {
1978 return;
1979 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001980 // Transform the bitmap in the new space, without taking into
1981 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001982 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07001983 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07001984 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001985 perspectiveOutline.transform(origMatrix);
1986
1987 // TODO(edisonn): perf - use current clip too.
1988 // Retrieve the bounds of the new shape.
1989 SkRect bounds = perspectiveOutline.getBounds();
1990
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001991 // Transform the bitmap in the new space, taking into
1992 // account the initial transform.
1993 SkMatrix total = origMatrix;
1994 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07001995 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
1996 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
1997 total.postScale(dpiScale, dpiScale);
1998
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001999 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002000 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002001 physicalPerspectiveOutline.transform(total);
2002
halcanary7a14b312015-10-01 07:28:13 -07002003 SkRect physicalPerspectiveBounds =
2004 physicalPerspectiveOutline.getBounds();
2005 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2006 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002007
2008 // TODO(edisonn): A better approach would be to use a bitmap shader
2009 // (in clamp mode) and draw a rect over the entire bounding box. Then
2010 // intersect perspectiveOutline to the clip. That will avoid introducing
2011 // alpha to the image while still giving good behavior at the edge of
2012 // the image. Avoiding alpha will reduce the pdf size and generation
2013 // CPU time some.
2014
halcanary7a14b312015-10-01 07:28:13 -07002015 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2016
reede8f30622016-03-23 18:59:25 -07002017 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002018 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002019 return;
2020 }
halcanary7a14b312015-10-01 07:28:13 -07002021 SkCanvas* canvas = surface->getCanvas();
2022 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002023
2024 SkScalar deltaX = bounds.left();
2025 SkScalar deltaY = bounds.top();
2026
2027 SkMatrix offsetMatrix = origMatrix;
2028 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002029 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002030
2031 // Translate the draw in the new canvas, so we perfectly fit the
2032 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002033 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002034 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002035 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002036 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002037
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002038 // In the new space, we use the identity matrix translated
2039 // and scaled to reflect DPI.
2040 matrix.setScale(1 / scaleX, 1 / scaleY);
2041 matrix.postTranslate(deltaX, deltaY);
2042
halcanary7a14b312015-10-01 07:28:13 -07002043 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002044 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002045
halcanarya50151d2016-03-25 11:57:49 -07002046 autoImageUnref = surface->makeImageSnapshot();
2047 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002048 }
2049
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002050 SkMatrix scaled;
2051 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002052 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2053 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002054 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002055 SkIRect subset = imageBitmap.bounds();
2056 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2057 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002058 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002059 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002060 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002061 return;
2062 }
2063 if (content.needShape()) {
2064 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002065 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002066 shape.transform(matrix);
2067 content.setShape(shape);
2068 }
2069 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002070 return;
2071 }
2072
halcanary287d22d2015-09-24 10:20:05 -07002073 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002074 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002075 // draw calls. This code here works for all
2076 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2077 // rasterize a layer on this backend). Fortuanely, this seems
2078 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002079 autoImageUnref = color_filter(imageBitmap, colorFilter);
2080 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002081 // TODO(halcanary): de-dupe this by caching filtered images.
2082 // (maybe in the resource cache?)
2083 }
halcanarya50151d2016-03-25 11:57:49 -07002084
2085 SkBitmapKey key = imageBitmap.getKey();
2086 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002087 if (!pdfimage) {
halcanary4b1e17e2016-07-27 14:49:46 -07002088 sk_sp<SkImage> img = imageBitmap.makeImage();
halcanarya50151d2016-03-25 11:57:49 -07002089 if (!img) {
2090 return;
2091 }
2092 pdfimage = SkPDFCreateBitmapObject(
2093 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002094 if (!pdfimage) {
2095 return;
halcanary287d22d2015-09-24 10:20:05 -07002096 }
halcanarya50151d2016-03-25 11:57:49 -07002097 fDocument->serialize(pdfimage); // serialize images early.
2098 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002099 }
halcanarya50151d2016-03-25 11:57:49 -07002100 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002101 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002102 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002103}
reede51c3562016-07-19 14:33:20 -07002104
2105///////////////////////////////////////////////////////////////////////////////////////////////////
2106
2107#include "SkSpecialImage.h"
2108#include "SkImageFilter.h"
2109
2110void SkPDFDevice::drawSpecial(const SkDraw& draw, SkSpecialImage* srcImg, int x, int y,
2111 const SkPaint& paint) {
2112 SkASSERT(!srcImg->isTextureBacked());
2113
2114 SkBitmap resultBM;
2115
2116 SkImageFilter* filter = paint.getImageFilter();
2117 if (filter) {
2118 SkIPoint offset = SkIPoint::Make(0, 0);
2119 SkMatrix matrix = *draw.fMatrix;
2120 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
2121 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-x, -y);
2122// SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
2123 SkImageFilter::Context ctx(matrix, clipBounds, nullptr /*cache.get()*/);
2124
2125 sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset));
2126 if (resultImg) {
2127 SkPaint tmpUnfiltered(paint);
2128 tmpUnfiltered.setImageFilter(nullptr);
2129 if (resultImg->getROPixels(&resultBM)) {
2130 this->drawSprite(draw, resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered);
2131 }
2132 }
2133 } else {
2134 if (srcImg->getROPixels(&resultBM)) {
2135 this->drawSprite(draw, resultBM, x, y, paint);
2136 }
2137 }
2138}
2139
2140sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) {
2141 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
2142}
2143
2144sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) {
2145 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()),
2146 image->makeNonTextureImage());
2147}
2148
2149sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() {
reede51c3562016-07-19 14:33:20 -07002150 return nullptr;
2151}