blob: e92990317c0d301a8bbdb47522e9563bb6ce61bb [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"
halcanary022c2bd2016-09-02 11:29:46 -070016#include "SkMakeUnique.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000017#include "SkPath.h"
reeda4393342016-03-18 11:22:57 -070018#include "SkPathEffect.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000019#include "SkPathOps.h"
halcanarydb0dcc72015-03-20 12:31:52 -070020#include "SkPDFBitmap.h"
halcanary7a14b312015-10-01 07:28:13 -070021#include "SkPDFCanon.h"
halcanary989da4a2016-03-21 14:33:17 -070022#include "SkPDFDocument.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000023#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000024#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000025#include "SkPDFGraphicState.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000026#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000027#include "SkPDFShader.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000028#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000029#include "SkPDFUtils.h"
wangxianzhuef6c50a2015-09-17 20:38:02 -070030#include "SkRasterClip.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000031#include "SkRRect.h"
halcanary4871f222016-08-26 13:17:44 -070032#include "SkScopeExit.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000033#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070034#include "SkSurface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000035#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000036#include "SkTemplates.h"
halcanarya6814332015-05-27 08:53:36 -070037#include "SkXfermodeInterpretation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000038
edisonn@google.com73a7ea32013-11-11 20:55:15 +000039#define DPI_FOR_RASTER_SCALE_ONE 72
40
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000041// Utility functions
42
halcanarya6814332015-05-27 08:53:36 -070043// If the paint will definitely draw opaquely, replace kSrc_Mode with
44// kSrcOver_Mode. http://crbug.com/473572
45static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
46 if (kSrcOver_SkXfermodeInterpretation
47 == SkInterpretXfermode(*paint, false)) {
halcanary96fcdcc2015-08-27 07:41:13 -070048 paint->setXfermode(nullptr);
halcanarya6814332015-05-27 08:53:36 -070049 }
50}
51
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000052static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000053 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
halcanaryeb92cb32016-07-15 13:41:27 -070054 SkPDFUtils::AppendColorComponent(SkColorGetR(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000055 result->writeText(" ");
halcanaryeb92cb32016-07-15 13:41:27 -070056 SkPDFUtils::AppendColorComponent(SkColorGetG(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000057 result->writeText(" ");
halcanaryeb92cb32016-07-15 13:41:27 -070058 SkPDFUtils::AppendColorComponent(SkColorGetB(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000059 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000060}
61
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000062static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000063 SkPaint result = paint;
64 if (result.isFakeBoldText()) {
65 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
66 kStdFakeBoldInterpKeys,
67 kStdFakeBoldInterpValues,
68 kStdFakeBoldInterpLength);
69 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000070 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000071 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000072 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000073 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000074 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000075 result.setStrokeWidth(width);
76 }
77 return result;
78}
79
halcanary2be7e012016-03-28 11:58:08 -070080SkPDFDevice::GraphicStateEntry::GraphicStateEntry()
81 : fColor(SK_ColorBLACK)
82 , fTextScaleX(SK_Scalar1)
83 , fTextFill(SkPaint::kFill_Style)
84 , fShaderIndex(-1)
85 , fGraphicStateIndex(-1)
86 , fFont(nullptr)
87 , fTextSize(SK_ScalarNaN) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000088 fMatrix.reset();
89}
90
halcanary2be7e012016-03-28 11:58:08 -070091bool SkPDFDevice::GraphicStateEntry::compareInitialState(
92 const GraphicStateEntry& cur) {
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +000093 return fColor == cur.fColor &&
94 fShaderIndex == cur.fShaderIndex &&
95 fGraphicStateIndex == cur.fGraphicStateIndex &&
96 fMatrix == cur.fMatrix &&
97 fClipStack == cur.fClipStack &&
98 (fTextScaleX == 0 ||
99 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000100}
101
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000102class GraphicStackState {
103public:
104 GraphicStackState(const SkClipStack& existingClipStack,
105 const SkRegion& existingClipRegion,
106 SkWStream* contentStream)
107 : fStackDepth(0),
108 fContentStream(contentStream) {
109 fEntries[0].fClipStack = existingClipStack;
110 fEntries[0].fClipRegion = existingClipRegion;
111 }
112
113 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000114 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000115 void updateMatrix(const SkMatrix& matrix);
halcanary2be7e012016-03-28 11:58:08 -0700116 void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000117
118 void drainStack();
119
120private:
121 void push();
122 void pop();
halcanary2be7e012016-03-28 11:58:08 -0700123 SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000124
125 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
126 static const int kMaxStackDepth = 12;
halcanary2be7e012016-03-28 11:58:08 -0700127 SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1];
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000128 int fStackDepth;
129 SkWStream* fContentStream;
130};
131
132void GraphicStackState::drainStack() {
133 while (fStackDepth) {
134 pop();
135 }
136}
137
138void GraphicStackState::push() {
139 SkASSERT(fStackDepth < kMaxStackDepth);
140 fContentStream->writeText("q\n");
141 fStackDepth++;
142 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
143}
144
145void GraphicStackState::pop() {
146 SkASSERT(fStackDepth > 0);
147 fContentStream->writeText("Q\n");
148 fStackDepth--;
149}
150
robertphillips@google.com80214e22012-07-20 15:33:18 +0000151// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000152// and then skips over the leading entries as specified in prefix. It requires
153// and asserts that "prefix" will be a prefix to "stack."
154static void skip_clip_stack_prefix(const SkClipStack& prefix,
155 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000156 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000157 SkClipStack::B2TIter prefixIter(prefix);
158 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000159
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000160 const SkClipStack::Element* prefixEntry;
161 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000162
163 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000164 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000165 iterEntry = iter->next();
166 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000167 // Because of SkClipStack does internal intersection, the last clip
168 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000169 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000170 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
171 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
172 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000173 // back up the iterator by one
174 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000175 prefixEntry = prefixIter.next();
176 break;
177 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000178 }
179
halcanary96fcdcc2015-08-27 07:41:13 -0700180 SkASSERT(prefixEntry == nullptr);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000181}
182
183static void emit_clip(SkPath* clipPath, SkRect* clipRect,
184 SkWStream* contentStream) {
185 SkASSERT(clipPath || clipRect);
186
187 SkPath::FillType clipFill;
188 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000189 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000190 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000191 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000192 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
193 clipFill = SkPath::kWinding_FillType;
194 }
195
196 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
197 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
198 if (clipFill == SkPath::kEvenOdd_FillType) {
199 contentStream->writeText("W* n\n");
200 } else {
201 contentStream->writeText("W n\n");
202 }
203}
204
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000205/* Calculate an inverted path's equivalent non-inverted path, given the
206 * canvas bounds.
207 * outPath may alias with invPath (since this is supported by PathOps).
208 */
209static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
210 SkPath* outPath) {
211 SkASSERT(invPath.isInverseFillType());
212
213 SkPath clipPath;
214 clipPath.addRect(bounds);
215
reedcdb42bb2015-06-26 10:23:07 -0700216 return Op(clipPath, invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000217}
218
219// Sanity check the numerical values of the SkRegion ops and PathOps ops
220// enums so region_op_to_pathops_op can do a straight passthrough cast.
221// If these are failing, it may be necessary to make region_op_to_pathops_op
222// do more.
bungeman99fe8222015-08-20 07:57:51 -0700223static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
224static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
225static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
226static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
227static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
228 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000229
230static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
231 SkASSERT(op >= 0);
232 SkASSERT(op <= SkRegion::kReverseDifference_Op);
233 return (SkPathOp)op;
234}
235
236/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
237 * Returns true if successful, or false if not successful.
238 * If successful, the resulting clip is stored in outClipPath.
239 * If not successful, outClipPath is undefined, and a fallback method
240 * should be used.
241 */
242static bool get_clip_stack_path(const SkMatrix& transform,
243 const SkClipStack& clipStack,
244 const SkRegion& clipRegion,
245 SkPath* outClipPath) {
246 outClipPath->reset();
247 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
248
249 const SkClipStack::Element* clipEntry;
250 SkClipStack::Iter iter;
251 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
252 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
253 SkPath entryPath;
254 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
255 outClipPath->reset();
256 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
257 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000258 } else {
259 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000260 }
261 entryPath.transform(transform);
262
263 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
264 *outClipPath = entryPath;
265 } else {
266 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
267 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
268 return false;
269 }
270 }
271 }
272
273 if (outClipPath->isInverseFillType()) {
274 // The bounds are slightly outset to ensure this is correct in the
275 // face of floating-point accuracy and possible SkRegion bitmap
276 // approximations.
277 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
278 clipBounds.outset(SK_Scalar1, SK_Scalar1);
279 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
280 return false;
281 }
282 }
283 return true;
284}
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000285
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000286// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000287// graphic state stack, and the fact that we can know all the clips used
288// on the page to optimize this.
289void GraphicStackState::updateClip(const SkClipStack& clipStack,
290 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000291 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000292 if (clipStack == currentEntry()->fClipStack) {
293 return;
294 }
295
296 while (fStackDepth > 0) {
297 pop();
298 if (clipStack == currentEntry()->fClipStack) {
299 return;
300 }
301 }
302 push();
303
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000304 currentEntry()->fClipStack = clipStack;
305 currentEntry()->fClipRegion = clipRegion;
306
307 SkMatrix transform;
308 transform.setTranslate(translation.fX, translation.fY);
309
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000310 SkPath clipPath;
311 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700312 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000313 return;
314 }
halcanarydda239e2016-03-31 07:33:57 -0700315
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000316 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
317 // already been applied. (If this is a top level device, then it specifies
318 // a clip to the content area. If this is a layer, then it specifies
319 // the clip in effect when the layer was created.) There's no need to
320 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
321 // initial clip on the parent layer. (This means there's a bug if the user
322 // expands the clip and then uses any xfer mode that uses dst:
323 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000324 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000325 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
326
327 // If the clip stack does anything other than intersect or if it uses
328 // an inverse fill type, we have to fall back to the clip region.
329 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000330 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000331 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000332 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
333 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000334 needRegion = true;
335 break;
336 }
337 }
338
339 if (needRegion) {
340 SkPath clipPath;
341 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700342 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000343 } else {
344 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000345 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000346 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000347 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
348 switch (clipEntry->getType()) {
349 case SkClipStack::Element::kRect_Type: {
350 SkRect translatedClip;
351 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700352 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000353 break;
354 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000355 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000356 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000357 clipEntry->asPath(&translatedPath);
358 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700359 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000360 break;
361 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000362 }
363 }
364 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000365}
366
367void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
368 if (matrix == currentEntry()->fMatrix) {
369 return;
370 }
371
372 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
373 SkASSERT(fStackDepth > 0);
374 SkASSERT(fEntries[fStackDepth].fClipStack ==
375 fEntries[fStackDepth -1].fClipStack);
376 pop();
377
378 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
379 }
380 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
381 return;
382 }
383
384 push();
385 SkPDFUtils::AppendTransform(matrix, fContentStream);
386 currentEntry()->fMatrix = matrix;
387}
388
halcanary2be7e012016-03-28 11:58:08 -0700389void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000390 // PDF treats a shader as a color, so we only set one or the other.
391 if (state.fShaderIndex >= 0) {
392 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000393 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000394 currentEntry()->fShaderIndex = state.fShaderIndex;
395 }
396 } else {
397 if (state.fColor != currentEntry()->fColor ||
398 currentEntry()->fShaderIndex >= 0) {
399 emit_pdf_color(state.fColor, fContentStream);
400 fContentStream->writeText("RG ");
401 emit_pdf_color(state.fColor, fContentStream);
402 fContentStream->writeText("rg\n");
403 currentEntry()->fColor = state.fColor;
404 currentEntry()->fShaderIndex = -1;
405 }
406 }
407
408 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000409 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000410 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
411 }
412
413 if (state.fTextScaleX) {
414 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
415 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
416 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700417 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000418 fContentStream->writeText(" Tz\n");
419 currentEntry()->fTextScaleX = state.fTextScaleX;
420 }
421 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700422 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
423 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
424 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000425 fContentStream->writeDecAsText(state.fTextFill);
426 fContentStream->writeText(" Tr\n");
427 currentEntry()->fTextFill = state.fTextFill;
428 }
429 }
430}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000431
reed76033be2015-03-14 10:54:31 -0700432static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700433 // PDF does not support image filters, so render them on CPU.
434 // Note that this rendering is done at "screen" resolution (100dpi), not
435 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700436 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800437 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700438
439 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700440 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700441}
442
443SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
reedcd4051e2016-07-15 09:41:26 -0700444 if (layerPaint && not_supported_for_layers(*layerPaint)) {
reed7503d602016-07-15 14:23:29 -0700445 // need to return a raster device, which we will detect in drawDevice()
446 return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
senorblancob0e89dc2014-10-20 14:03:12 -0700447 }
fmalita6987dca2014-11-13 08:33:37 -0800448 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700449 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000450}
451
halcanary989da4a2016-03-21 14:33:17 -0700452SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
453
bsalomon@google.come97f0852011-06-17 13:10:25 +0000454
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000455
456// A helper class to automatically finish a ContentEntry at the end of a
457// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000458class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000459public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000460 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
461 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000462 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700463 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000464 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700465 fDstFormXObject(nullptr) {
reed1e7f5e72016-04-27 07:49:17 -0700466 init(draw.fClipStack, draw.fRC->bwRgn(), *draw.fMatrix, paint, hasText);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000467 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000468 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
469 const SkRegion& clipRegion, const SkMatrix& matrix,
470 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000471 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700472 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000473 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700474 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000475 init(clipStack, clipRegion, matrix, paint, hasText);
476 }
477
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000478 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000479 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000480 SkPath* shape = &fShape;
481 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700482 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000483 }
halcanarydabd4f02016-08-03 11:16:56 -0700484 fDevice->finishContentEntry(fXfermode, std::move(fDstFormXObject), shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000485 }
486 }
487
halcanary2be7e012016-03-28 11:58:08 -0700488 SkPDFDevice::ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000489
490 /* Returns true when we explicitly need the shape of the drawing. */
491 bool needShape() {
492 switch (fXfermode) {
493 case SkXfermode::kClear_Mode:
494 case SkXfermode::kSrc_Mode:
495 case SkXfermode::kSrcIn_Mode:
496 case SkXfermode::kSrcOut_Mode:
497 case SkXfermode::kDstIn_Mode:
498 case SkXfermode::kDstOut_Mode:
499 case SkXfermode::kSrcATop_Mode:
500 case SkXfermode::kDstATop_Mode:
501 case SkXfermode::kModulate_Mode:
502 return true;
503 default:
504 return false;
505 }
506 }
507
508 /* Returns true unless we only need the shape of the drawing. */
509 bool needSource() {
510 if (fXfermode == SkXfermode::kClear_Mode) {
511 return false;
512 }
513 return true;
514 }
515
516 /* If the shape is different than the alpha component of the content, then
517 * setShape should be called with the shape. In particular, images and
518 * devices have rectangular shape.
519 */
520 void setShape(const SkPath& shape) {
521 fShape = shape;
522 }
523
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000524private:
525 SkPDFDevice* fDevice;
halcanary2be7e012016-03-28 11:58:08 -0700526 SkPDFDevice::ContentEntry* fContentEntry;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000527 SkXfermode::Mode fXfermode;
halcanarydabd4f02016-08-03 11:16:56 -0700528 sk_sp<SkPDFObject> fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000529 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000530
531 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
532 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000533 // Shape has to be flatten before we get here.
534 if (matrix.hasPerspective()) {
535 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000536 return;
537 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000538 if (paint.getXfermode()) {
539 paint.getXfermode()->asMode(&fXfermode);
540 }
541 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
542 matrix, paint, hasText,
543 &fDstFormXObject);
544 }
545};
546
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000547////////////////////////////////////////////////////////////////////////////////
548
halcanary989da4a2016-03-21 14:33:17 -0700549SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
reed589a39e2016-08-20 07:59:19 -0700550 : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()),
551 SkSurfaceProps(0, kUnknown_SkPixelGeometry))
robertphillips9a53fd72015-06-22 09:46:59 -0700552 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800553 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanarya1f1ee92015-02-20 06:17:26 -0800554 , fRasterDpi(rasterDpi)
halcanary989da4a2016-03-21 14:33:17 -0700555 , fDocument(doc) {
halcanarya1f1ee92015-02-20 06:17:26 -0800556 SkASSERT(pageSize.width() > 0);
557 SkASSERT(pageSize.height() > 0);
robertphillips1f3923e2016-07-21 07:17:54 -0700558
halcanarya1f1ee92015-02-20 06:17:26 -0800559 if (flip) {
560 // Skia generally uses the top left as the origin but PDF
561 // natively has the origin at the bottom left. This matrix
562 // corrects for that. But that only needs to be done once, we
563 // don't do it when layering.
564 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
565 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
566 } else {
567 fInitialTransform.setIdentity();
568 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000569}
570
571SkPDFDevice::~SkPDFDevice() {
halcanary3c35fb32016-06-30 11:55:07 -0700572 this->cleanUp();
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000573}
574
575void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700576 fContentEntries.reset();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000577}
578
halcanary3c35fb32016-06-30 11:55:07 -0700579void SkPDFDevice::cleanUp() {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000580 fGraphicStateResources.unrefAll();
581 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000582 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000583 fShaderResources.unrefAll();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000584}
585
reedf70b5312016-03-04 16:36:20 -0800586void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
587 SkData* value) {
588 if (0 == rect.width() && 0 == rect.height()) {
589 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
590 } else {
591 SkPath path;
592 path.addRect(rect);
593 handlePathAnnotation(path, d, key, value);
594 }
595}
596
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000597void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000598 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700599 replace_srcmode_on_opaque_paint(&newPaint);
600
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000601 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000602 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000603 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000604}
605
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000606void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
halcanary2be7e012016-03-28 11:58:08 -0700607 SkPDFDevice::ContentEntry* contentEntry) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000608 if (!contentEntry) {
609 return;
610 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000611 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
612 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000613 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000614 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000615 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000616 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000617 inverse.mapRect(&bbox);
618
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000619 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000620 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000621 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000622}
623
halcanary682ee012016-01-28 10:59:34 -0800624void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700625 SkCanvas::PointMode mode,
626 size_t count,
627 const SkPoint* points,
628 const SkPaint& srcPaint) {
629 SkPaint passedPaint = srcPaint;
630 replace_srcmode_on_opaque_paint(&passedPaint);
631
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000632 if (count == 0) {
633 return;
634 }
635
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000636 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
637 // We only use this when there's a path effect because of the overhead
638 // of multiple calls to setUpContentEntry it causes.
639 if (passedPaint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700640 if (d.fRC->isEmpty()) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000641 return;
642 }
643 SkDraw pointDraw(d);
644 pointDraw.fDevice = this;
645 pointDraw.drawPoints(mode, count, points, passedPaint, true);
646 return;
647 }
648
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000649 const SkPaint* paint = &passedPaint;
650 SkPaint modifiedPaint;
651
652 if (mode == SkCanvas::kPoints_PointMode &&
653 paint->getStrokeCap() != SkPaint::kRound_Cap) {
654 modifiedPaint = *paint;
655 paint = &modifiedPaint;
656 if (paint->getStrokeWidth()) {
657 // PDF won't draw a single point with square/butt caps because the
658 // orientation is ambiguous. Draw a rectangle instead.
659 modifiedPaint.setStyle(SkPaint::kFill_Style);
660 SkScalar strokeWidth = paint->getStrokeWidth();
661 SkScalar halfStroke = SkScalarHalf(strokeWidth);
662 for (size_t i = 0; i < count; i++) {
663 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
664 r.inset(-halfStroke, -halfStroke);
665 drawRect(d, r, modifiedPaint);
666 }
667 return;
668 } else {
669 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
670 }
671 }
672
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000673 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000674 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000675 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000676 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000677
678 switch (mode) {
679 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000680 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000681 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000682 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000683 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000684 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000685 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000686 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000687 break;
688 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000689 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000690 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000691 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000692 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000693 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000694 &content.entry()->fContent);
695 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000696 }
697 break;
698 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000699 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
700 for (size_t i = 0; i < count; i++) {
701 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000702 &content.entry()->fContent);
703 SkPDFUtils::ClosePath(&content.entry()->fContent);
704 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000705 }
706 break;
707 default:
708 SkASSERT(false);
709 }
710}
711
halcanary8103a342016-03-08 15:10:16 -0800712static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800713 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700714 annotation->insertName("Subtype", "Link");
halcanary488165e2016-04-22 06:10:21 -0700715 annotation->insertInt("F", 4); // required by ISO 19005
wangxianzhud76665d2015-07-17 17:23:15 -0700716
halcanaryece83922016-03-08 08:32:12 -0800717 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700718 border->reserve(3);
719 border->appendInt(0); // Horizontal corner radius.
720 border->appendInt(0); // Vertical corner radius.
721 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800722 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700723
halcanaryece83922016-03-08 08:32:12 -0800724 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700725 rect->reserve(4);
726 rect->appendScalar(translatedRect.fLeft);
727 rect->appendScalar(translatedRect.fTop);
728 rect->appendScalar(translatedRect.fRight);
729 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800730 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700731
halcanary8103a342016-03-08 15:10:16 -0800732 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700733}
734
halcanary8103a342016-03-08 15:10:16 -0800735static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700736 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700737 SkString url(static_cast<const char *>(urlData->data()),
738 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800739 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700740 action->insertName("S", "URI");
741 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800742 annotation->insertObject("A", std::move(action));
743 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700744}
745
halcanary8103a342016-03-08 15:10:16 -0800746static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
747 const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700748 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700749 SkString name(static_cast<const char *>(nameData->data()),
750 nameData->size() - 1);
751 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800752 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700753}
754
halcanary682ee012016-01-28 10:59:34 -0800755void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700756 const SkRect& rect,
757 const SkPaint& srcPaint) {
758 SkPaint paint = srcPaint;
759 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000760 SkRect r = rect;
761 r.sort();
762
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000763 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700764 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000765 return;
766 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000767 SkPath path;
768 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700769 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000770 return;
771 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000772
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000773 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000774 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000775 return;
776 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000777 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000778 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000779 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000780}
781
halcanarya6814332015-05-27 08:53:36 -0700782void SkPDFDevice::drawRRect(const SkDraw& draw,
783 const SkRRect& rrect,
784 const SkPaint& srcPaint) {
785 SkPaint paint = srcPaint;
786 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000787 SkPath path;
788 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700789 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000790}
791
halcanarya6814332015-05-27 08:53:36 -0700792void SkPDFDevice::drawOval(const SkDraw& draw,
793 const SkRect& oval,
794 const SkPaint& srcPaint) {
795 SkPaint paint = srcPaint;
796 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700797 SkPath path;
798 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700799 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700800}
801
halcanary682ee012016-01-28 10:59:34 -0800802void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700803 const SkPath& origPath,
804 const SkPaint& srcPaint,
805 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000806 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700807 SkPaint paint = srcPaint;
808 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800809 SkPath modifiedPath;
810 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000811
812 SkMatrix matrix = *d.fMatrix;
813 if (prePathMatrix) {
814 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800815 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000816 pathPtr = &modifiedPath;
817 pathIsMutable = true;
818 }
halcanary682ee012016-01-28 10:59:34 -0800819 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000820 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000821 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000822 }
823 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000824
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000825 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700826 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000827 return;
828 }
halcanary682ee012016-01-28 10:59:34 -0800829 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000830 pathPtr = &modifiedPath;
831 pathIsMutable = true;
832 }
halcanary682ee012016-01-28 10:59:34 -0800833 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000834
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000835 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700836 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000837 if (fill) {
838 noEffectPaint.setStyle(SkPaint::kFill_Style);
839 } else {
840 noEffectPaint.setStyle(SkPaint::kStroke_Style);
841 noEffectPaint.setStrokeWidth(0);
842 }
halcanary96fcdcc2015-08-27 07:41:13 -0700843 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000844 return;
845 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000846
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000847 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000848 return;
849 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000850
reed1e7f5e72016-04-27 07:49:17 -0700851 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000852 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000853 return;
854 }
halcanary8b2bc252015-10-06 09:41:47 -0700855 bool consumeDegeratePathSegments =
856 paint.getStyle() == SkPaint::kFill_Style ||
857 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
858 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000859 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -0700860 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000861 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000862 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000863 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000864}
865
halcanary7a14b312015-10-01 07:28:13 -0700866void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
867 const SkBitmap& bitmap,
868 const SkRect* src,
869 const SkRect& dst,
870 const SkPaint& srcPaint,
871 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700872 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -0700873}
874
875void SkPDFDevice::drawBitmap(const SkDraw& d,
876 const SkBitmap& bitmap,
877 const SkMatrix& matrix,
878 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -0700879 SkPaint paint = srcPaint;
880 if (bitmap.isOpaque()) {
881 replace_srcmode_on_opaque_paint(&paint);
882 }
883
reed1e7f5e72016-04-27 07:49:17 -0700884 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700885 return;
886 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +0000887
halcanary7a14b312015-10-01 07:28:13 -0700888 SkMatrix transform = matrix;
889 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -0700890 SkImageBitmap imageBitmap(bitmap);
891 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700892 transform, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700893}
894
895void SkPDFDevice::drawSprite(const SkDraw& d,
896 const SkBitmap& bitmap,
897 int x,
898 int y,
899 const SkPaint& srcPaint) {
900 SkPaint paint = srcPaint;
901 if (bitmap.isOpaque()) {
902 replace_srcmode_on_opaque_paint(&paint);
903 }
904
reed1e7f5e72016-04-27 07:49:17 -0700905 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700906 return;
907 }
908
909 SkMatrix matrix;
910 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -0700911 SkImageBitmap imageBitmap(bitmap);
912 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700913 matrix, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700914}
915
916void SkPDFDevice::drawImage(const SkDraw& draw,
917 const SkImage* image,
918 SkScalar x,
919 SkScalar y,
920 const SkPaint& srcPaint) {
921 SkPaint paint = srcPaint;
922 if (!image) {
923 return;
924 }
925 if (image->isOpaque()) {
926 replace_srcmode_on_opaque_paint(&paint);
927 }
reed1e7f5e72016-04-27 07:49:17 -0700928 if (draw.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700929 return;
930 }
931 SkMatrix transform = SkMatrix::MakeTrans(x, y);
932 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -0700933 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
934 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700935 transform, draw.fClipStack, draw.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700936}
937
938void SkPDFDevice::drawImageRect(const SkDraw& draw,
939 const SkImage* image,
940 const SkRect* src,
941 const SkRect& dst,
942 const SkPaint& srcPaint,
943 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700944 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000945}
946
halcanaryf0c30f52016-07-15 13:35:45 -0700947namespace {
948class GlyphPositioner {
949public:
950 GlyphPositioner(SkDynamicMemoryWStream* content,
951 SkScalar textSkewX,
halcanary4ed2f012016-08-15 18:40:07 -0700952 bool wideChars,
953 bool defaultPositioning,
954 SkPoint origin)
halcanaryf0c30f52016-07-15 13:35:45 -0700955 : fContent(content)
halcanary4871f222016-08-26 13:17:44 -0700956 , fCurrentMatrixOrigin{0.0f, 0.0f}
halcanaryf0c30f52016-07-15 13:35:45 -0700957 , fXAdvance(0.0f)
958 , fWideChars(wideChars)
halcanary4ed2f012016-08-15 18:40:07 -0700959 , fInText(false)
960 , fDefaultPositioning(defaultPositioning) {
halcanary4871f222016-08-26 13:17:44 -0700961 // Flip the text about the x-axis to account for origin swap and include
962 // the passed parameters.
963 fContent->writeText("1 0 ");
964 SkPDFUtils::AppendScalar(0 - textSkewX, fContent);
965 fContent->writeText(" -1 ");
966 SkPDFUtils::AppendScalar(origin.x(), fContent);
967 fContent->writeText(" ");
968 SkPDFUtils::AppendScalar(origin.y(), fContent);
969 fContent->writeText(" Tm\n");
halcanaryf0c30f52016-07-15 13:35:45 -0700970 }
halcanary4871f222016-08-26 13:17:44 -0700971 ~GlyphPositioner() { this->flush(); }
halcanaryf0c30f52016-07-15 13:35:45 -0700972 void flush() {
973 if (fInText) {
974 fContent->writeText("> Tj\n");
975 fInText = false;
976 }
977 }
978 void setWideChars(bool wideChars) {
979 if (fWideChars != wideChars) {
980 SkASSERT(!fInText);
halcanary4ed2f012016-08-15 18:40:07 -0700981 SkASSERT(fWideChars == wideChars);
halcanaryf0c30f52016-07-15 13:35:45 -0700982 fWideChars = wideChars;
983 }
984 }
halcanary4871f222016-08-26 13:17:44 -0700985 void writeGlyph(SkPoint xy,
halcanaryf0c30f52016-07-15 13:35:45 -0700986 SkScalar advanceWidth,
987 uint16_t glyph) {
halcanary4ed2f012016-08-15 18:40:07 -0700988 if (!fDefaultPositioning) {
halcanary4871f222016-08-26 13:17:44 -0700989 SkPoint position = xy - fCurrentMatrixOrigin;
990 if (position != SkPoint{fXAdvance, 0}) {
halcanary4ed2f012016-08-15 18:40:07 -0700991 this->flush();
halcanary4871f222016-08-26 13:17:44 -0700992 SkPDFUtils::AppendScalar(position.x(), fContent);
halcanary4ed2f012016-08-15 18:40:07 -0700993 fContent->writeText(" ");
halcanary4871f222016-08-26 13:17:44 -0700994 SkPDFUtils::AppendScalar(-position.y(), fContent);
halcanary4ed2f012016-08-15 18:40:07 -0700995 fContent->writeText(" Td ");
halcanary4871f222016-08-26 13:17:44 -0700996 fCurrentMatrixOrigin = xy;
halcanary4ed2f012016-08-15 18:40:07 -0700997 fXAdvance = 0;
998 }
999 fXAdvance += advanceWidth;
halcanaryf0c30f52016-07-15 13:35:45 -07001000 }
1001 if (!fInText) {
1002 fContent->writeText("<");
1003 fInText = true;
1004 }
1005 if (fWideChars) {
1006 SkPDFUtils::WriteUInt16BE(fContent, glyph);
1007 } else {
1008 SkASSERT(0 == glyph >> 8);
1009 SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph));
1010 }
halcanaryf0c30f52016-07-15 13:35:45 -07001011 }
1012
1013private:
1014 SkDynamicMemoryWStream* fContent;
halcanary4871f222016-08-26 13:17:44 -07001015 SkPoint fCurrentMatrixOrigin;
halcanaryf0c30f52016-07-15 13:35:45 -07001016 SkScalar fXAdvance;
1017 bool fWideChars;
1018 bool fInText;
halcanary4ed2f012016-08-15 18:40:07 -07001019 const bool fDefaultPositioning;
halcanaryf0c30f52016-07-15 13:35:45 -07001020};
1021} // namespace
1022
halcanary66a82f32015-10-12 13:05:04 -07001023static void draw_transparent_text(SkPDFDevice* device,
1024 const SkDraw& d,
1025 const void* text, size_t len,
1026 SkScalar x, SkScalar y,
1027 const SkPaint& srcPaint) {
halcanary4871f222016-08-26 13:17:44 -07001028 sk_sp<SkTypeface> defaultFace = SkTypeface::MakeDefault();
1029 if (!SkPDFFont::CanEmbedTypeface(defaultFace.get(), device->getCanon())) {
halcanary4ed2f012016-08-15 18:40:07 -07001030 SkDebugf("SkPDF: default typeface should be embeddable");
halcanary66a82f32015-10-12 13:05:04 -07001031 return; // Avoid infinite loop in release.
1032 }
halcanary4871f222016-08-26 13:17:44 -07001033 SkPaint transparent;
1034 transparent.setTypeface(std::move(defaultFace));
halcanary66a82f32015-10-12 13:05:04 -07001035 transparent.setTextSize(srcPaint.getTextSize());
1036 transparent.setColor(SK_ColorTRANSPARENT);
1037 switch (srcPaint.getTextEncoding()) {
1038 case SkPaint::kGlyphID_TextEncoding: {
1039 // Since a glyphId<->Unicode mapping is typeface-specific,
1040 // map back to Unicode first.
1041 size_t glyphCount = len / 2;
1042 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1043 srcPaint.glyphsToUnichars(
1044 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1045 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
halcanary4ed2f012016-08-15 18:40:07 -07001046 // TODO(halcanary): deal with case where default typeface
1047 // does not have glyphs for these unicode code points.
halcanary66a82f32015-10-12 13:05:04 -07001048 device->drawText(d, &unichars[0],
1049 glyphCount * sizeof(SkUnichar),
1050 x, y, transparent);
1051 break;
1052 }
1053 case SkPaint::kUTF8_TextEncoding:
1054 case SkPaint::kUTF16_TextEncoding:
1055 case SkPaint::kUTF32_TextEncoding:
1056 transparent.setTextEncoding(srcPaint.getTextEncoding());
1057 device->drawText(d, text, len, x, y, transparent);
1058 break;
1059 default:
1060 SkFAIL("unknown text encoding");
1061 }
1062}
1063
halcanary4ed2f012016-08-15 18:40:07 -07001064void SkPDFDevice::internalDrawText(
1065 const SkDraw& d, const void* sourceText, size_t sourceByteCount,
1066 const SkScalar pos[], SkTextBlob::GlyphPositioning positioning,
1067 SkPoint offset, const SkPaint& srcPaint) {
1068 NOT_IMPLEMENTED(srcPaint.getMaskFilter() != nullptr, false);
1069 if (srcPaint.getMaskFilter() != nullptr) {
1070 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1071 // making text unreadable (e.g. same text twice when using CSS shadows).
1072 return;
1073 }
halcanaryea17dfe2016-08-24 09:20:57 -07001074 NOT_IMPLEMENTED(srcPaint.isVerticalText(), false);
1075 if (srcPaint.isVerticalText()) {
1076 // Don't pretend we support drawing vertical text. It is not
1077 // clear to me how to switch to "vertical writing" mode in PDF.
1078 // Currently neither Chromium or Android set this flag.
1079 // https://bug.skia.org/5665
1080 return;
1081 }
halcanary4ed2f012016-08-15 18:40:07 -07001082 SkPaint paint = calculate_text_paint(srcPaint);
1083 replace_srcmode_on_opaque_paint(&paint);
1084 if (!paint.getTypeface()) {
1085 paint.setTypeface(SkTypeface::MakeDefault());
1086 }
1087 SkTypeface* typeface = paint.getTypeface();
1088 if (!typeface) {
1089 SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n");
1090 return;
1091 }
halcanary4871f222016-08-26 13:17:44 -07001092
1093 const SkAdvancedTypefaceMetrics* metrics =
1094 SkPDFFont::GetMetrics(typeface, fDocument->canon());
1095 if (!metrics) {
halcanary4ed2f012016-08-15 18:40:07 -07001096 return;
1097 }
halcanary4871f222016-08-26 13:17:44 -07001098 // TODO(halcanary): use metrics->fGlyphToUnicode to check Unicode mapping.
1099 const SkGlyphID maxGlyphID = metrics->fLastGlyphID;
halcanary4ed2f012016-08-15 18:40:07 -07001100 if (!SkPDFFont::CanEmbedTypeface(typeface, fDocument->canon())) {
1101 SkPath path; // https://bug.skia.org/3866
1102 paint.getTextPath(sourceText, sourceByteCount,
1103 offset.x(), offset.y(), &path);
robertphillips5ba165e2016-08-15 15:36:58 -07001104 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1105 // Draw text transparently to make it copyable/searchable/accessable.
halcanary4ed2f012016-08-15 18:40:07 -07001106 draw_transparent_text(this, d, sourceText, sourceByteCount,
1107 offset.x(), offset.y(), paint);
robertphillips5ba165e2016-08-15 15:36:58 -07001108 return;
1109 }
halcanary4ed2f012016-08-15 18:40:07 -07001110 int glyphCount = paint.textToGlyphs(sourceText, sourceByteCount, nullptr);
halcanary4871f222016-08-26 13:17:44 -07001111 if (glyphCount <= 0) {
1112 return;
1113 }
1114 SkAutoSTMalloc<128, SkGlyphID> glyphStorage;
1115 const SkGlyphID* glyphs = nullptr;
halcanary4ed2f012016-08-15 18:40:07 -07001116 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
halcanary4871f222016-08-26 13:17:44 -07001117 glyphs = (const SkGlyphID*)sourceText;
1118 // validate input later.
halcanary4ed2f012016-08-15 18:40:07 -07001119 } else {
halcanary4871f222016-08-26 13:17:44 -07001120 glyphStorage.reset(SkToSizeT(glyphCount));
1121 (void)paint.textToGlyphs(sourceText, sourceByteCount, glyphStorage.get());
1122 glyphs = glyphStorage.get();
halcanary4ed2f012016-08-15 18:40:07 -07001123 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1124 }
robertphillips5ba165e2016-08-15 15:36:58 -07001125
halcanary4ed2f012016-08-15 18:40:07 -07001126 bool defaultPositioning = (positioning == SkTextBlob::kDefault_Positioning);
halcanary9df5a4c2016-08-24 10:08:13 -07001127 paint.setHinting(SkPaint::kNo_Hinting);
halcanary4ed2f012016-08-15 18:40:07 -07001128 SkAutoGlyphCache glyphCache(paint, nullptr, nullptr);
1129
1130 SkPaint::Align alignment = paint.getTextAlign();
halcanary4871f222016-08-26 13:17:44 -07001131 float alignmentFactor = SkPaint::kLeft_Align == alignment ? 0.0f :
1132 SkPaint::kCenter_Align == alignment ? -0.5f :
1133 /* SkPaint::kRight_Align */ -1.0f;
halcanary4ed2f012016-08-15 18:40:07 -07001134 if (defaultPositioning && alignment != SkPaint::kLeft_Align) {
halcanary4871f222016-08-26 13:17:44 -07001135 SkScalar advance = 0;
halcanary4ed2f012016-08-15 18:40:07 -07001136 for (int i = 0; i < glyphCount; ++i) {
1137 advance += glyphCache->getGlyphIDAdvance(glyphs[i]).fAdvanceX;
1138 }
halcanary4871f222016-08-26 13:17:44 -07001139 offset.offset(alignmentFactor * advance, 0);
halcanary6059dc32016-08-15 11:45:36 -07001140 }
halcanary4ed2f012016-08-15 18:40:07 -07001141 ScopedContentEntry content(this, d, paint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001142 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001143 return;
1144 }
halcanary4ed2f012016-08-15 18:40:07 -07001145 SkDynamicMemoryWStream* out = &content.entry()->fContent;
halcanary4871f222016-08-26 13:17:44 -07001146 SkScalar textSize = paint.getTextSize();
1147
1148 int index = 0;
1149 while (glyphs[index] > maxGlyphID) { // Invalid glyphID for this font.
1150 ++index; // Skip this glyphID
1151 if (index == glyphCount) {
1152 return; // all glyphIDs were bad.
1153 }
robertphillips5ba165e2016-08-15 15:36:58 -07001154 }
halcanary4871f222016-08-26 13:17:44 -07001155
1156 out->writeText("BT\n");
1157 SK_AT_SCOPE_EXIT(out->writeText("ET\n"));
1158
1159 SkPDFFont* font = this->updateFont(
1160 typeface, textSize, glyphs[index], content.entry());
1161 SkASSERT(font); // All preconditions for SkPDFFont::GetFontResource are met.
1162 if (!font) { return; }
1163
halcanary4ed2f012016-08-15 18:40:07 -07001164 GlyphPositioner glyphPositioner(out,
1165 paint.getTextSkewX(),
1166 font->multiByteGlyphs(),
1167 defaultPositioning,
1168 offset);
halcanary4ed2f012016-08-15 18:40:07 -07001169
halcanary4871f222016-08-26 13:17:44 -07001170 while (index < glyphCount) {
1171 int stretch = font->countStretch(&glyphs[index], glyphCount - index, maxGlyphID);
1172 SkASSERT(index + stretch <= glyphCount);
halcanary4ed2f012016-08-15 18:40:07 -07001173 if (stretch < 1) {
halcanary4ed2f012016-08-15 18:40:07 -07001174 // The current pdf font cannot encode the next glyph.
1175 // Try to get a pdf font which can encode the next glyph.
robertphillips5ba165e2016-08-15 15:36:58 -07001176 glyphPositioner.flush();
halcanary4871f222016-08-26 13:17:44 -07001177 // first, validate the next glyph
1178 while (glyphs[index] > maxGlyphID) {
1179 ++index; // Skip this glyphID
1180 if (index == glyphCount) {
1181 return; // all remainng glyphIDs were bad.
1182 }
robertphillips5ba165e2016-08-15 15:36:58 -07001183 }
halcanary4871f222016-08-26 13:17:44 -07001184 SkASSERT(index < glyphCount);
1185 font = this->updateFont(typeface, textSize, glyphs[index], content.entry());
1186 SkASSERT(font); // preconditions for SkPDFFont::GetFontResource met.
1187 if (!font) { return; }
robertphillips5ba165e2016-08-15 15:36:58 -07001188 glyphPositioner.setWideChars(font->multiByteGlyphs());
halcanary4871f222016-08-26 13:17:44 -07001189 // Get stretch for this new font.
1190 stretch = font->countStretch(&glyphs[index], glyphCount - index, maxGlyphID);
halcanary4ed2f012016-08-15 18:40:07 -07001191 if (stretch < 1) {
robertphillips5ba165e2016-08-15 15:36:58 -07001192 SkDEBUGFAIL("PDF could not encode glyph.");
halcanary4ed2f012016-08-15 18:40:07 -07001193 return;
robertphillips5ba165e2016-08-15 15:36:58 -07001194 }
1195 }
halcanary4871f222016-08-26 13:17:44 -07001196 while (stretch-- > 0) {
1197 SkGlyphID gid = glyphs[index];
1198 if (gid <= maxGlyphID) {
1199 font->noteGlyphUsage(gid);
1200 SkGlyphID encodedGlyph = font->glyphToPDFFontEncoding(gid);
1201 if (defaultPositioning) {
1202 glyphPositioner.writeGlyph(SkPoint{0, 0}, 0, encodedGlyph);
1203 } else {
1204 SkScalar advance = glyphCache->getGlyphIDAdvance(gid).fAdvanceX;
1205 SkPoint xy = SkTextBlob::kFull_Positioning == positioning
1206 ? SkPoint{pos[2 * index], pos[2 * index + 1]}
1207 : SkPoint{pos[index], 0};
1208 if (alignment != SkPaint::kLeft_Align) {
1209 xy.offset(alignmentFactor * advance, 0);
1210 }
1211 glyphPositioner.writeGlyph(xy, advance, encodedGlyph);
halcanary4ed2f012016-08-15 18:40:07 -07001212 }
halcanary4ed2f012016-08-15 18:40:07 -07001213 }
halcanary4871f222016-08-26 13:17:44 -07001214 ++index;
halcanary4ed2f012016-08-15 18:40:07 -07001215 }
robertphillips5ba165e2016-08-15 15:36:58 -07001216 }
halcanary4ed2f012016-08-15 18:40:07 -07001217}
1218
1219void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
1220 SkScalar x, SkScalar y, const SkPaint& paint) {
1221 this->internalDrawText(d, text, len, nullptr, SkTextBlob::kDefault_Positioning,
1222 SkPoint{x, y}, paint);
1223}
1224
1225void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
1226 const SkScalar pos[], int scalarsPerPos,
1227 const SkPoint& offset, const SkPaint& paint) {
1228 this->internalDrawText(d, text, len, pos, (SkTextBlob::GlyphPositioning)scalarsPerPos,
1229 offset, paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001230}
1231
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001232void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001233 int vertexCount, const SkPoint verts[],
1234 const SkPoint texs[], const SkColor colors[],
1235 SkXfermode* xmode, const uint16_t indices[],
1236 int indexCount, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001237 if (d.fRC->isEmpty()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001238 return;
1239 }
reed@google.com85e143c2013-12-30 15:51:25 +00001240 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001241}
1242
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001243void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1244 int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001245 SkASSERT(!paint.getImageFilter());
1246
reed7503d602016-07-15 14:23:29 -07001247 // Check if the source device is really a bitmapdevice (because that's what we returned
1248 // from createDevice (likely due to an imagefilter)
1249 SkPixmap pmap;
1250 if (device->peekPixels(&pmap)) {
1251 SkBitmap bitmap;
1252 bitmap.installPixels(pmap);
reedcf5c8462016-07-20 12:28:40 -07001253 this->drawSprite(d, bitmap, x, y, paint);
reed7503d602016-07-15 14:23:29 -07001254 return;
1255 }
1256
fmalita6987dca2014-11-13 08:33:37 -08001257 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001258 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001259
1260 SkScalar scalarX = SkIntToScalar(x);
1261 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001262 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1263 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001264 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001265 }
halcanary91fcb3e2016-03-04 13:53:22 -08001266 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1267 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001268 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001269 }
halcanary91fcb3e2016-03-04 13:53:22 -08001270 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1271 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001272 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001273 }
1274
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001275 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001276 return;
1277 }
1278
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001279 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001280 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
reed1e7f5e72016-04-27 07:49:17 -07001281 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001282 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001283 return;
1284 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001285 if (content.needShape()) {
1286 SkPath shape;
1287 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001288 SkIntToScalar(device->width()),
1289 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001290 content.setShape(shape);
1291 }
1292 if (!content.needSource()) {
1293 return;
1294 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001295
halcanary4b1e17e2016-07-27 14:49:46 -07001296 sk_sp<SkPDFObject> xObject = pdfDevice->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001297 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001298 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001299}
1300
reede8f30622016-03-23 18:59:25 -07001301sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1302 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001303}
1304
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001305
halcanary8103a342016-03-08 15:10:16 -08001306sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001307 SkTDArray<SkPDFObject*> fonts;
1308 fonts.setReserve(fFontResources.count());
1309 for (SkPDFFont* font : fFontResources) {
1310 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001311 }
halcanary8103a342016-03-08 15:10:16 -08001312 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001313 &fGraphicStateResources,
1314 &fShaderResources,
1315 &fXObjectResources,
1316 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001317}
1318
halcanary8103a342016-03-08 15:10:16 -08001319sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001320 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001321 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001322 mediaBox->appendInt(0);
1323 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001324 mediaBox->appendInt(fPageSize.width());
1325 mediaBox->appendInt(fPageSize.height());
1326 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001327}
1328
mtklein5f939ab2016-03-16 10:28:35 -07001329std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001330 SkDynamicMemoryWStream buffer;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001331 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanaryafdc1772016-08-23 09:02:12 -07001332 SkPDFUtils::AppendTransform(fInitialTransform, &buffer);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001333 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001334
halcanaryafdc1772016-08-23 09:02:12 -07001335 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, &buffer);
halcanary2be7e012016-03-28 11:58:08 -07001336 for (const auto& entry : fContentEntries) {
1337 SkPoint translation;
1338 translation.iset(this->getOrigin());
1339 translation.negate();
1340 gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion,
1341 translation);
1342 gsState.updateMatrix(entry.fState.fMatrix);
1343 gsState.updateDrawingState(entry.fState);
1344
halcanaryafdc1772016-08-23 09:02:12 -07001345 entry.fContent.writeToStream(&buffer);
halcanary2be7e012016-03-28 11:58:08 -07001346 }
1347 gsState.drainStack();
halcanary022c2bd2016-09-02 11:29:46 -07001348 if (buffer.bytesWritten() > 0) {
1349 return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream());
1350 } else {
1351 return skstd::make_unique<SkMemoryStream>();
1352 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001353}
1354
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001355/* Draws an inverse filled path by using Path Ops to compute the positive
1356 * inverse using the current clip as the inverse bounds.
1357 * Return true if this was an inverse path and was properly handled,
1358 * otherwise returns false and the normal drawing routine should continue,
1359 * either as a (incorrect) fallback or because the path was not inverse
1360 * in the first place.
1361 */
1362bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001363 const SkPaint& paint, bool pathIsMutable,
1364 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001365 if (!origPath.isInverseFillType()) {
1366 return false;
1367 }
1368
reed1e7f5e72016-04-27 07:49:17 -07001369 if (d.fRC->isEmpty()) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001370 return false;
1371 }
1372
1373 SkPath modifiedPath;
1374 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1375 SkPaint noInversePaint(paint);
1376
1377 // Merge stroking operations into final path.
1378 if (SkPaint::kStroke_Style == paint.getStyle() ||
1379 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1380 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1381 if (doFillPath) {
1382 noInversePaint.setStyle(SkPaint::kFill_Style);
1383 noInversePaint.setStrokeWidth(0);
1384 pathPtr = &modifiedPath;
1385 } else {
1386 // To be consistent with the raster output, hairline strokes
1387 // are rendered as non-inverted.
1388 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001389 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001390 return true;
1391 }
1392 }
1393
1394 // Get bounds of clip in current transform space
1395 // (clip bounds are given in device space).
1396 SkRect bounds;
1397 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001398 SkMatrix totalMatrix = *d.fMatrix;
1399 if (prePathMatrix) {
1400 totalMatrix.preConcat(*prePathMatrix);
1401 }
1402 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001403 return false;
1404 }
reed1e7f5e72016-04-27 07:49:17 -07001405 bounds.set(d.fRC->getBounds());
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001406 transformInverse.mapRect(&bounds);
1407
1408 // Extend the bounds by the line width (plus some padding)
1409 // so the edge doesn't cause a visible stroke.
1410 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1411 paint.getStrokeWidth() + SK_Scalar1);
1412
1413 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1414 return false;
1415 }
1416
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001417 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001418 return true;
1419}
1420
reedf70b5312016-03-04 16:36:20 -08001421void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001422 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001423 const char key[], SkData* value) {
1424 if (!value) {
1425 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001426 }
reedf70b5312016-03-04 16:36:20 -08001427
1428 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1429 SkPoint transformedPoint;
1430 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1431 fNamedDestinations.emplace_back(value, transformedPoint);
1432 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001433}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001434
reedf70b5312016-03-04 16:36:20 -08001435void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001436 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001437 const char key[], SkData* value) {
1438 if (!value) {
1439 return;
1440 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001441
1442 SkPath transformedPath = path;
1443 transformedPath.transform(*d.fMatrix);
1444 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001445 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1446 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001447 SkRect transformedRect = SkRect::Make(clip.getBounds());
1448
reedf70b5312016-03-04 16:36:20 -08001449 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001450 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001451 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001452 }
reedf70b5312016-03-04 16:36:20 -08001453 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001454 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001455 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001456 }
reed16108352016-03-03 09:14:36 -08001457 }
halcanary438de492015-04-28 06:21:01 -07001458}
1459
wangxianzhuef6c50a2015-09-17 20:38:02 -07001460void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1461 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001462 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001463 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001464 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001465 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001466 }
halcanary91fcb3e2016-03-04 13:53:22 -08001467 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001468 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001469 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001470 array->appendObject(
1471 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001472 }
1473}
epoger@google.comb58772f2013-03-08 09:09:10 +00001474
halcanary6d622702015-03-25 08:45:42 -07001475void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001476 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001477 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001478 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001479 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001480 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001481 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001482 pdfDest->appendScalar(p.x());
1483 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001484 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001485 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001486 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001487 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001488}
1489
halcanary4b1e17e2016-07-27 14:49:46 -07001490sk_sp<SkPDFObject> SkPDFDevice::makeFormXObjectFromDevice() {
halcanary5abbb442016-07-29 08:41:33 -07001491 SkMatrix inverseTransform = SkMatrix::I();
halcanaryafdc1772016-08-23 09:02:12 -07001492 if (!fInitialTransform.isIdentity()) {
1493 if (!fInitialTransform.invert(&inverseTransform)) {
halcanary5abbb442016-07-29 08:41:33 -07001494 SkDEBUGFAIL("Layer initial transform should be invertible.");
1495 inverseTransform.reset();
1496 }
1497 }
halcanary4b1e17e2016-07-27 14:49:46 -07001498 sk_sp<SkPDFObject> xobject =
1499 SkPDFMakeFormXObject(this->content(), this->copyMediaBox(),
halcanary5abbb442016-07-29 08:41:33 -07001500 this->makeResourceDict(), inverseTransform, nullptr);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001501 // We always draw the form xobjects that we create back into the device, so
1502 // we simply preserve the font usage instead of pulling it out and merging
1503 // it back in later.
halcanary4b1e17e2016-07-27 14:49:46 -07001504 this->cleanUp(); // Reset this device to have no content.
1505 this->init();
reed@google.comfc641d02012-09-20 17:52:20 +00001506 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001507}
1508
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001509void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
halcanarydabd4f02016-08-03 11:16:56 -07001510 sk_sp<SkPDFObject> mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001511 const SkClipStack* clipStack,
1512 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001513 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001514 bool invertClip) {
1515 if (clipRegion.isEmpty() && !invertClip) {
1516 return;
1517 }
1518
halcanary4b1e17e2016-07-27 14:49:46 -07001519 sk_sp<SkPDFDict> sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanarydabd4f02016-08-03 11:16:56 -07001520 std::move(mask), invertClip,
1521 SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001522
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001523 SkMatrix identity;
1524 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001525 SkPaint paint;
1526 paint.setXfermodeMode(mode);
1527 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001528 if (!content.entry()) {
1529 return;
1530 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001531 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001532 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001533 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001534
halcanary1437c1e2016-03-13 18:30:24 -07001535 // Call makeNoSmaskGraphicState() instead of
1536 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1537 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001538 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001539 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001540 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001541}
1542
halcanary2be7e012016-03-28 11:58:08 -07001543SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001544 const SkRegion& clipRegion,
1545 const SkMatrix& matrix,
1546 const SkPaint& paint,
1547 bool hasText,
halcanarydabd4f02016-08-03 11:16:56 -07001548 sk_sp<SkPDFObject>* dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001549 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001550 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001551 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001552 }
1553
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001554 // The clip stack can come from an SkDraw where it is technically optional.
1555 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001556 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001557 if (clipRegion == fExistingClipRegion) {
1558 clipStack = &fExistingClipStack;
1559 } else {
1560 // GraphicStackState::updateClip expects the clip stack to have
1561 // fExistingClip as a prefix, so start there, then set the clip
1562 // to the passed region.
1563 synthesizedClipStack = fExistingClipStack;
1564 SkPath clipPath;
1565 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001566 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1567 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001568 clipStack = &synthesizedClipStack;
1569 }
1570 }
1571
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001572 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1573 if (paint.getXfermode()) {
1574 paint.getXfermode()->asMode(&xfermode);
1575 }
1576
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001577 // For the following modes, we want to handle source and destination
1578 // separately, so make an object of what's already there.
1579 if (xfermode == SkXfermode::kClear_Mode ||
1580 xfermode == SkXfermode::kSrc_Mode ||
1581 xfermode == SkXfermode::kSrcIn_Mode ||
1582 xfermode == SkXfermode::kDstIn_Mode ||
1583 xfermode == SkXfermode::kSrcOut_Mode ||
1584 xfermode == SkXfermode::kDstOut_Mode ||
1585 xfermode == SkXfermode::kSrcATop_Mode ||
1586 xfermode == SkXfermode::kDstATop_Mode ||
1587 xfermode == SkXfermode::kModulate_Mode) {
1588 if (!isContentEmpty()) {
halcanarydabd4f02016-08-03 11:16:56 -07001589 *dst = this->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001590 SkASSERT(isContentEmpty());
1591 } else if (xfermode != SkXfermode::kSrc_Mode &&
1592 xfermode != SkXfermode::kSrcOut_Mode) {
1593 // Except for Src and SrcOut, if there isn't anything already there,
1594 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001595 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001596 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001597 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001598 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001599 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001600
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001601 // Dst xfer mode doesn't draw source at all.
1602 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001603 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001604 }
1605
halcanary2be7e012016-03-28 11:58:08 -07001606 SkPDFDevice::ContentEntry* entry;
1607 if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) {
1608 entry = fContentEntries.back();
1609 } else if (xfermode != SkXfermode::kDstOver_Mode) {
1610 entry = fContentEntries.emplace_back();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001611 } else {
halcanary2be7e012016-03-28 11:58:08 -07001612 entry = fContentEntries.emplace_front();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001613 }
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001614 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001615 hasText, &entry->fState);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001616 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001617}
1618
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001619void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
halcanarydabd4f02016-08-03 11:16:56 -07001620 sk_sp<SkPDFObject> dst,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001621 SkPath* shape) {
1622 if (xfermode != SkXfermode::kClear_Mode &&
1623 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001624 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001625 xfermode != SkXfermode::kSrcIn_Mode &&
1626 xfermode != SkXfermode::kDstIn_Mode &&
1627 xfermode != SkXfermode::kSrcOut_Mode &&
1628 xfermode != SkXfermode::kDstOut_Mode &&
1629 xfermode != SkXfermode::kSrcATop_Mode &&
1630 xfermode != SkXfermode::kDstATop_Mode &&
1631 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001632 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001633 return;
1634 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001635 if (xfermode == SkXfermode::kDstOver_Mode) {
1636 SkASSERT(!dst);
halcanary2be7e012016-03-28 11:58:08 -07001637 if (fContentEntries.front()->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001638 // For DstOver, an empty content entry was inserted before the rest
1639 // of the content entries. If nothing was drawn, it needs to be
1640 // removed.
halcanary2be7e012016-03-28 11:58:08 -07001641 fContentEntries.pop_front();
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001642 }
1643 return;
1644 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001645 if (!dst) {
1646 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1647 xfermode == SkXfermode::kSrcOut_Mode);
1648 return;
1649 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001650
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001651 SkASSERT(dst);
halcanary2be7e012016-03-28 11:58:08 -07001652 SkASSERT(fContentEntries.count() == 1);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001653 // Changing the current content into a form-xobject will destroy the clip
1654 // objects which is fine since the xobject will already be clipped. However
1655 // if source has shape, we need to clip it too, so a copy of the clip is
1656 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001657
1658 SkClipStack clipStack = fContentEntries.front()->fState.fClipStack;
1659 SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001660
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001661 SkMatrix identity;
1662 identity.reset();
1663 SkPaint stockPaint;
1664
halcanary4b1e17e2016-07-27 14:49:46 -07001665 sk_sp<SkPDFObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001666 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001667 // If nothing was drawn and there's no shape, then the draw was a
1668 // no-op, but dst needs to be restored for that to be true.
1669 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1670 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1671 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001672 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001673 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001674 ScopedContentEntry content(this, &fExistingClipStack,
1675 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001676 stockPaint);
halcanarydabd4f02016-08-03 11:16:56 -07001677 // TODO: addXObjectResource take sk_sp
1678 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()),
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001679 &content.entry()->fContent);
1680 return;
1681 } else {
1682 xfermode = SkXfermode::kClear_Mode;
1683 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001684 } else {
halcanary2be7e012016-03-28 11:58:08 -07001685 SkASSERT(fContentEntries.count() == 1);
halcanary4b1e17e2016-07-27 14:49:46 -07001686 srcFormXObject = this->makeFormXObjectFromDevice();
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001687 }
1688
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001689 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1690 // without alpha.
1691 if (xfermode == SkXfermode::kSrcATop_Mode) {
1692 // TODO(vandebo): In order to properly support SrcATop we have to track
1693 // the shape of what's been drawn at all times. It's the intersection of
1694 // the non-transparent parts of the device and the outlines (shape) of
1695 // all images and devices drawn.
1696 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001697 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001698 SkXfermode::kSrcOver_Mode, true);
1699 } else {
halcanary96fcdcc2015-08-27 07:41:13 -07001700 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001701 // Draw shape into a form-xobject.
reed1e7f5e72016-04-27 07:49:17 -07001702 SkRasterClip rc(clipRegion);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001703 SkDraw d;
1704 d.fMatrix = &identity;
reed1e7f5e72016-04-27 07:49:17 -07001705 d.fRC = &rc;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001706 d.fClipStack = &clipStack;
1707 SkPaint filledPaint;
1708 filledPaint.setColor(SK_ColorBLACK);
1709 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001710 this->drawPath(d, *shape, filledPaint, nullptr, true);
halcanarydabd4f02016-08-03 11:16:56 -07001711 drawFormXObjectWithMask(addXObjectResource(dst.get()),
1712 this->makeFormXObjectFromDevice(),
1713 &fExistingClipStack, fExistingClipRegion,
1714 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001715
halcanarydabd4f02016-08-03 11:16:56 -07001716 } else {
1717 drawFormXObjectWithMask(addXObjectResource(dst.get()), srcFormXObject,
1718 &fExistingClipStack, fExistingClipRegion,
1719 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001720 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001721 }
1722
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001723 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001724 return;
1725 } else if (xfermode == SkXfermode::kSrc_Mode ||
1726 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001727 ScopedContentEntry content(this, &fExistingClipStack,
1728 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001729 if (content.entry()) {
1730 SkPDFUtils::DrawFormXObject(
1731 this->addXObjectResource(srcFormXObject.get()),
1732 &content.entry()->fContent);
1733 }
1734 if (xfermode == SkXfermode::kSrc_Mode) {
1735 return;
1736 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001737 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001738 ScopedContentEntry content(this, &fExistingClipStack,
1739 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001740 if (content.entry()) {
halcanarydabd4f02016-08-03 11:16:56 -07001741 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()),
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001742 &content.entry()->fContent);
1743 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001744 }
1745
1746 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1747 xfermode == SkXfermode::kDstIn_Mode ||
1748 xfermode == SkXfermode::kSrcOut_Mode ||
1749 xfermode == SkXfermode::kDstOut_Mode ||
1750 xfermode == SkXfermode::kSrcATop_Mode ||
1751 xfermode == SkXfermode::kDstATop_Mode ||
1752 xfermode == SkXfermode::kModulate_Mode);
1753
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001754 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001755 xfermode == SkXfermode::kSrcOut_Mode ||
1756 xfermode == SkXfermode::kSrcATop_Mode) {
halcanarydabd4f02016-08-03 11:16:56 -07001757 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
1758 std::move(dst),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001759 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001760 SkXfermode::kSrcOver_Mode,
1761 xfermode == SkXfermode::kSrcOut_Mode);
halcanarydabd4f02016-08-03 11:16:56 -07001762 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001763 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001764 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
halcanarydabd4f02016-08-03 11:16:56 -07001765 int resourceID = addXObjectResource(dst.get());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001766 if (xfermode == SkXfermode::kModulate_Mode) {
1767 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
halcanarydabd4f02016-08-03 11:16:56 -07001768 std::move(dst), &fExistingClipStack,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001769 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001770 SkXfermode::kSrcOver_Mode, false);
1771 mode = SkXfermode::kMultiply_Mode;
1772 }
halcanarydabd4f02016-08-03 11:16:56 -07001773 drawFormXObjectWithMask(resourceID, std::move(srcFormXObject),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001774 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001775 xfermode == SkXfermode::kDstOut_Mode);
halcanarydabd4f02016-08-03 11:16:56 -07001776 return;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001777 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001778}
1779
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001780bool SkPDFDevice::isContentEmpty() {
halcanary2be7e012016-03-28 11:58:08 -07001781 if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) {
1782 SkASSERT(fContentEntries.count() <= 1);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001783 return true;
1784 }
1785 return false;
1786}
1787
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001788void SkPDFDevice::populateGraphicStateEntryFromPaint(
1789 const SkMatrix& matrix,
1790 const SkClipStack& clipStack,
1791 const SkRegion& clipRegion,
1792 const SkPaint& paint,
1793 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001794 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001795 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1796 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1797 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001798
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001799 entry->fMatrix = matrix;
1800 entry->fClipStack = clipStack;
1801 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001802 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1803 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001804
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001805 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001806 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001807 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001808 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001809 if (shader) {
1810 // PDF positions patterns relative to the initial transform, so
1811 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001812 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001813 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001814
1815 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001816 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001817 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001818
1819 // We need to apply the initial transform to bounds in order to get
1820 // bounds in a consistent coordinate system.
1821 SkRect boundsTemp;
1822 boundsTemp.set(bounds);
1823 fInitialTransform.mapRect(&boundsTemp);
1824 boundsTemp.roundOut(&bounds);
1825
halcanary792c80f2015-02-20 07:21:05 -08001826 SkScalar rasterScale =
1827 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
halcanarydabd4f02016-08-03 11:16:56 -07001828 pdfShader = SkPDFShader::GetPDFShader(
1829 fDocument, fRasterDpi, shader, transform, bounds, rasterScale);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001830
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001831 if (pdfShader.get()) {
1832 // pdfShader has been canonicalized so we can directly compare
1833 // pointers.
1834 int resourceIndex = fShaderResources.find(pdfShader.get());
1835 if (resourceIndex < 0) {
1836 resourceIndex = fShaderResources.count();
1837 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001838 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001839 }
1840 entry->fShaderIndex = resourceIndex;
1841 } else {
1842 // A color shader is treated as an invalid shader so we don't have
1843 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001844 SkShader::GradientInfo gradientInfo;
1845 SkColor gradientColor;
1846 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001847 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001848 gradientInfo.fColorCount = 1;
1849 if (shader->asAGradient(&gradientInfo) ==
1850 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001851 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1852 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001853 }
1854 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001855 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001856
halcanary48810a02016-03-07 14:57:50 -08001857 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001858 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001859 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001860 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001861 } else {
1862 SkPaint newPaint = paint;
1863 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001864 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001865 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001866 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001867 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001868 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001869
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001870 if (hasText) {
1871 entry->fTextScaleX = paint.getTextScaleX();
1872 entry->fTextFill = paint.getStyle();
1873 } else {
1874 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001875 }
1876}
1877
halcanarybe27a112015-04-01 13:31:19 -07001878int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001879 // Assumes that gs has been canonicalized (so we can directly compare
1880 // pointers).
1881 int result = fGraphicStateResources.find(gs);
1882 if (result < 0) {
1883 result = fGraphicStateResources.count();
1884 fGraphicStateResources.push(gs);
1885 gs->ref();
1886 }
1887 return result;
1888}
1889
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001890int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
halcanarydabd4f02016-08-03 11:16:56 -07001891 // TODO(halcanary): make this take a sk_sp<SkPDFObject>
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001892 // Assumes that xobject has been canonicalized (so we can directly compare
1893 // pointers).
1894 int result = fXObjectResources.find(xObject);
1895 if (result < 0) {
1896 result = fXObjectResources.count();
halcanarydabd4f02016-08-03 11:16:56 -07001897 fXObjectResources.push(SkRef(xObject));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001898 }
1899 return result;
1900}
1901
halcanary4871f222016-08-26 13:17:44 -07001902SkPDFFont* SkPDFDevice::updateFont(SkTypeface* typeface,
1903 SkScalar textSize,
1904 uint16_t glyphID,
1905 SkPDFDevice::ContentEntry* contentEntry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001906 if (contentEntry->fState.fFont == nullptr ||
halcanary4871f222016-08-26 13:17:44 -07001907 contentEntry->fState.fTextSize != textSize ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001908 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001909 int fontIndex = getFontResourceIndex(typeface, glyphID);
halcanary7e8d5d32016-08-12 07:59:38 -07001910 if (fontIndex < 0) {
halcanary4871f222016-08-26 13:17:44 -07001911 SkDebugf("SkPDF: Font error.");
1912 return nullptr;
halcanary7e8d5d32016-08-12 07:59:38 -07001913 }
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001914 contentEntry->fContent.writeText("/");
1915 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
1916 SkPDFResourceDict::kFont_ResourceType,
1917 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001918 contentEntry->fContent.writeText(" ");
halcanary4871f222016-08-26 13:17:44 -07001919 SkPDFUtils::AppendScalar(textSize, &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001920 contentEntry->fContent.writeText(" Tf\n");
1921 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001922 }
halcanary4871f222016-08-26 13:17:44 -07001923 return contentEntry->fState.fFont;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001924}
1925
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001926int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08001927 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07001928 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
halcanary7e8d5d32016-08-12 07:59:38 -07001929 if (!newFont) {
1930 return -1;
1931 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001932 int resourceIndex = fFontResources.find(newFont.get());
1933 if (resourceIndex < 0) {
halcanary530032a2016-08-18 14:22:52 -07001934 fDocument->registerFont(newFont.get());
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001935 resourceIndex = fFontResources.count();
halcanary530032a2016-08-18 14:22:52 -07001936 fFontResources.push(newFont.release());
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001937 }
1938 return resourceIndex;
1939}
1940
halcanary7a14b312015-10-01 07:28:13 -07001941static SkSize rect_to_size(const SkRect& r) {
1942 return SkSize::Make(r.width(), r.height());
1943}
1944
halcanarya50151d2016-03-25 11:57:49 -07001945static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
1946 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07001947 auto surface =
halcanarya50151d2016-03-25 11:57:49 -07001948 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
1949 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07001950 SkCanvas* canvas = surface->getCanvas();
1951 canvas->clear(SK_ColorTRANSPARENT);
1952 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07001953 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07001954 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07001955 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07001956 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07001957}
1958
1959////////////////////////////////////////////////////////////////////////////////
1960void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
1961 const SkClipStack* clipStack,
1962 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07001963 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07001964 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07001965 if (imageBitmap.dimensions().isZero()) {
1966 return;
1967 }
halcanary7a14b312015-10-01 07:28:13 -07001968 #ifdef SK_PDF_IMAGE_STATS
1969 gDrawImageCalls.fetch_add(1);
1970 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001971 SkMatrix matrix = origMatrix;
1972 SkRegion perspectiveBounds;
1973 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07001974 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001975
1976 // Rasterize the bitmap using perspective in a new bitmap.
1977 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001978 if (fRasterDpi == 0) {
1979 return;
1980 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001981 // Transform the bitmap in the new space, without taking into
1982 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001983 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07001984 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07001985 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001986 perspectiveOutline.transform(origMatrix);
1987
1988 // TODO(edisonn): perf - use current clip too.
1989 // Retrieve the bounds of the new shape.
1990 SkRect bounds = perspectiveOutline.getBounds();
1991
edisonn@google.com73a7ea32013-11-11 20:55:15 +00001992 // Transform the bitmap in the new space, taking into
1993 // account the initial transform.
1994 SkMatrix total = origMatrix;
1995 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07001996 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
1997 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
1998 total.postScale(dpiScale, dpiScale);
1999
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002000 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002001 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002002 physicalPerspectiveOutline.transform(total);
2003
halcanary7a14b312015-10-01 07:28:13 -07002004 SkRect physicalPerspectiveBounds =
2005 physicalPerspectiveOutline.getBounds();
2006 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2007 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002008
2009 // TODO(edisonn): A better approach would be to use a bitmap shader
2010 // (in clamp mode) and draw a rect over the entire bounding box. Then
2011 // intersect perspectiveOutline to the clip. That will avoid introducing
2012 // alpha to the image while still giving good behavior at the edge of
2013 // the image. Avoiding alpha will reduce the pdf size and generation
2014 // CPU time some.
2015
halcanary7a14b312015-10-01 07:28:13 -07002016 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2017
reede8f30622016-03-23 18:59:25 -07002018 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002019 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002020 return;
2021 }
halcanary7a14b312015-10-01 07:28:13 -07002022 SkCanvas* canvas = surface->getCanvas();
2023 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002024
2025 SkScalar deltaX = bounds.left();
2026 SkScalar deltaY = bounds.top();
2027
2028 SkMatrix offsetMatrix = origMatrix;
2029 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002030 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002031
2032 // Translate the draw in the new canvas, so we perfectly fit the
2033 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002034 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002035 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002036 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002037 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002038
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002039 // In the new space, we use the identity matrix translated
2040 // and scaled to reflect DPI.
2041 matrix.setScale(1 / scaleX, 1 / scaleY);
2042 matrix.postTranslate(deltaX, deltaY);
2043
halcanary7a14b312015-10-01 07:28:13 -07002044 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002045 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002046
halcanarya50151d2016-03-25 11:57:49 -07002047 autoImageUnref = surface->makeImageSnapshot();
2048 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002049 }
2050
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002051 SkMatrix scaled;
2052 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002053 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2054 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002055 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002056 SkIRect subset = imageBitmap.bounds();
2057 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2058 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002059 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002060 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002061 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002062 return;
2063 }
2064 if (content.needShape()) {
2065 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002066 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002067 shape.transform(matrix);
2068 content.setShape(shape);
2069 }
2070 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002071 return;
2072 }
2073
halcanary287d22d2015-09-24 10:20:05 -07002074 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002075 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002076 // draw calls. This code here works for all
2077 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2078 // rasterize a layer on this backend). Fortuanely, this seems
2079 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002080 autoImageUnref = color_filter(imageBitmap, colorFilter);
2081 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002082 // TODO(halcanary): de-dupe this by caching filtered images.
2083 // (maybe in the resource cache?)
2084 }
halcanarya50151d2016-03-25 11:57:49 -07002085
2086 SkBitmapKey key = imageBitmap.getKey();
2087 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002088 if (!pdfimage) {
halcanary4b1e17e2016-07-27 14:49:46 -07002089 sk_sp<SkImage> img = imageBitmap.makeImage();
halcanarya50151d2016-03-25 11:57:49 -07002090 if (!img) {
2091 return;
2092 }
2093 pdfimage = SkPDFCreateBitmapObject(
2094 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002095 if (!pdfimage) {
2096 return;
halcanary287d22d2015-09-24 10:20:05 -07002097 }
halcanarya50151d2016-03-25 11:57:49 -07002098 fDocument->serialize(pdfimage); // serialize images early.
2099 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002100 }
halcanarya50151d2016-03-25 11:57:49 -07002101 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002102 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002103 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002104}
reede51c3562016-07-19 14:33:20 -07002105
2106///////////////////////////////////////////////////////////////////////////////////////////////////
2107
2108#include "SkSpecialImage.h"
2109#include "SkImageFilter.h"
2110
2111void SkPDFDevice::drawSpecial(const SkDraw& draw, SkSpecialImage* srcImg, int x, int y,
2112 const SkPaint& paint) {
2113 SkASSERT(!srcImg->isTextureBacked());
2114
2115 SkBitmap resultBM;
2116
2117 SkImageFilter* filter = paint.getImageFilter();
2118 if (filter) {
2119 SkIPoint offset = SkIPoint::Make(0, 0);
2120 SkMatrix matrix = *draw.fMatrix;
2121 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
2122 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-x, -y);
2123// SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
2124 SkImageFilter::Context ctx(matrix, clipBounds, nullptr /*cache.get()*/);
2125
2126 sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset));
2127 if (resultImg) {
2128 SkPaint tmpUnfiltered(paint);
2129 tmpUnfiltered.setImageFilter(nullptr);
2130 if (resultImg->getROPixels(&resultBM)) {
2131 this->drawSprite(draw, resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered);
2132 }
2133 }
2134 } else {
2135 if (srcImg->getROPixels(&resultBM)) {
2136 this->drawSprite(draw, resultBM, x, y, paint);
2137 }
2138 }
2139}
2140
2141sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) {
2142 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
2143}
2144
2145sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) {
2146 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()),
2147 image->makeNonTextureImage());
2148}
2149
2150sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() {
reede51c3562016-07-19 14:33:20 -07002151 return nullptr;
2152}