blob: 38e6d1f021506390204125f84c106132559d9f2f [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"
9
reedf70b5312016-03-04 16:36:20 -080010#include "SkAnnotationKeys.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000011#include "SkColor.h"
halcanary287d22d2015-09-24 10:20:05 -070012#include "SkColorFilter.h"
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000013#include "SkClipStack.h"
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000014#include "SkDraw.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkGlyphCache.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000016#include "SkPaint.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.org9b49dc02010-10-20 22:23:29 +000028#include "SkPDFStream.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000029#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000030#include "SkPDFUtils.h"
wangxianzhuef6c50a2015-09-17 20:38:02 -070031#include "SkRasterClip.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000032#include "SkRect.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000033#include "SkRRect.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000034#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070035#include "SkSurface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000036#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000037#include "SkTemplates.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000038#include "SkTypefacePriv.h"
halcanarya6814332015-05-27 08:53:36 -070039#include "SkXfermodeInterpretation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000040
edisonn@google.com73a7ea32013-11-11 20:55:15 +000041#define DPI_FOR_RASTER_SCALE_ONE 72
42
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000043// Utility functions
44
halcanarya6814332015-05-27 08:53:36 -070045// If the paint will definitely draw opaquely, replace kSrc_Mode with
46// kSrcOver_Mode. http://crbug.com/473572
47static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
48 if (kSrcOver_SkXfermodeInterpretation
49 == SkInterpretXfermode(*paint, false)) {
halcanary96fcdcc2015-08-27 07:41:13 -070050 paint->setXfermode(nullptr);
halcanarya6814332015-05-27 08:53:36 -070051 }
52}
53
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000054static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000055 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
reed80ea19c2015-05-12 10:37:34 -070056 SkScalar colorScale = SkScalarInvert(0xFF);
57 SkPDFUtils::AppendScalar(SkColorGetR(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000058 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070059 SkPDFUtils::AppendScalar(SkColorGetG(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000060 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070061 SkPDFUtils::AppendScalar(SkColorGetB(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000062 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000063}
64
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000065static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000066 SkPaint result = paint;
67 if (result.isFakeBoldText()) {
68 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
69 kStdFakeBoldInterpKeys,
70 kStdFakeBoldInterpValues,
71 kStdFakeBoldInterpLength);
72 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000073 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000074 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000075 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000076 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000077 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000078 result.setStrokeWidth(width);
79 }
80 return result;
81}
82
83// Stolen from measure_text in SkDraw.cpp and then tweaked.
benjaminwagnerd936f632016-02-23 10:44:31 -080084static void align_text(SkPaint::GlyphCacheProc glyphCacheProc, const SkPaint& paint,
bungeman@google.com9a87cee2011-08-23 17:02:18 +000085 const uint16_t* glyphs, size_t len,
86 SkScalar* x, SkScalar* y) {
87 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000088 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000089 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000090
91 SkMatrix ident;
92 ident.reset();
halcanary96fcdcc2015-08-27 07:41:13 -070093 SkAutoGlyphCache autoCache(paint, nullptr, &ident);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000094 SkGlyphCache* cache = autoCache.getCache();
95
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +000096 const char* start = reinterpret_cast<const char*>(glyphs);
97 const char* stop = reinterpret_cast<const char*>(glyphs + len);
benjaminwagner6b3eacb2016-03-24 19:07:58 -070098 SkScalar xAdv = 0, yAdv = 0;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000099
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000100 // TODO(vandebo): This probably needs to take kerning into account.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000101 while (start < stop) {
benjaminwagnerd936f632016-02-23 10:44:31 -0800102 const SkGlyph& glyph = glyphCacheProc(cache, &start);
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700103 xAdv += SkFloatToScalar(glyph.fAdvanceX);
104 yAdv += SkFloatToScalar(glyph.fAdvanceY);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000105 };
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000106 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000107 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000108 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000109
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000110 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700111 xAdv = SkScalarHalf(xAdv);
112 yAdv = SkScalarHalf(yAdv);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000113 }
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700114 *x = *x - xAdv;
115 *y = *y - yAdv;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000116}
117
robertphillips@google.coma4662862013-11-21 14:24:16 +0000118static int max_glyphid_for_typeface(SkTypeface* typeface) {
reed@google.comfed86bd2013-03-14 15:04:57 +0000119 SkAutoResolveDefaultTypeface autoResolve(typeface);
120 typeface = autoResolve.get();
commit-bot@chromium.org6a4ba5b2013-07-17 21:55:08 +0000121 return typeface->countGlyphs() - 1;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000122}
123
124typedef SkAutoSTMalloc<128, uint16_t> SkGlyphStorage;
125
reed@google.comaec40662014-04-18 19:29:07 +0000126static int force_glyph_encoding(const SkPaint& paint, const void* text,
127 size_t len, SkGlyphStorage* storage,
bungeman22edc832014-10-03 07:55:58 -0700128 const uint16_t** glyphIDs) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000129 // Make sure we have a glyph id encoding.
130 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
halcanary96fcdcc2015-08-27 07:41:13 -0700131 int numGlyphs = paint.textToGlyphs(text, len, nullptr);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000132 storage->reset(numGlyphs);
133 paint.textToGlyphs(text, len, storage->get());
134 *glyphIDs = storage->get();
135 return numGlyphs;
136 }
137
138 // For user supplied glyph ids we need to validate them.
139 SkASSERT((len & 1) == 0);
reed@google.comaec40662014-04-18 19:29:07 +0000140 int numGlyphs = SkToInt(len / 2);
bungeman22edc832014-10-03 07:55:58 -0700141 const uint16_t* input = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000142
143 int maxGlyphID = max_glyphid_for_typeface(paint.getTypeface());
reed@google.comaec40662014-04-18 19:29:07 +0000144 int validated;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000145 for (validated = 0; validated < numGlyphs; ++validated) {
146 if (input[validated] > maxGlyphID) {
147 break;
148 }
149 }
150 if (validated >= numGlyphs) {
bungeman22edc832014-10-03 07:55:58 -0700151 *glyphIDs = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000152 return numGlyphs;
153 }
154
155 // Silently drop anything out of range.
156 storage->reset(numGlyphs);
157 if (validated > 0) {
158 memcpy(storage->get(), input, validated * sizeof(uint16_t));
159 }
160
reed@google.comaec40662014-04-18 19:29:07 +0000161 for (int i = validated; i < numGlyphs; ++i) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000162 storage->get()[i] = input[i];
163 if (input[i] > maxGlyphID) {
164 storage->get()[i] = 0;
165 }
166 }
167 *glyphIDs = storage->get();
168 return numGlyphs;
169}
170
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000171static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX,
172 SkWStream* content) {
173 // Flip the text about the x-axis to account for origin swap and include
174 // the passed parameters.
175 content->writeText("1 0 ");
halcanarybc4696b2015-05-06 10:56:04 -0700176 SkPDFUtils::AppendScalar(0 - textSkewX, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000177 content->writeText(" -1 ");
halcanarybc4696b2015-05-06 10:56:04 -0700178 SkPDFUtils::AppendScalar(x, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000179 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700180 SkPDFUtils::AppendScalar(y, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000181 content->writeText(" Tm\n");
182}
183
halcanary2be7e012016-03-28 11:58:08 -0700184SkPDFDevice::GraphicStateEntry::GraphicStateEntry()
185 : fColor(SK_ColorBLACK)
186 , fTextScaleX(SK_Scalar1)
187 , fTextFill(SkPaint::kFill_Style)
188 , fShaderIndex(-1)
189 , fGraphicStateIndex(-1)
190 , fFont(nullptr)
191 , fTextSize(SK_ScalarNaN) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000192 fMatrix.reset();
193}
194
halcanary2be7e012016-03-28 11:58:08 -0700195bool SkPDFDevice::GraphicStateEntry::compareInitialState(
196 const GraphicStateEntry& cur) {
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +0000197 return fColor == cur.fColor &&
198 fShaderIndex == cur.fShaderIndex &&
199 fGraphicStateIndex == cur.fGraphicStateIndex &&
200 fMatrix == cur.fMatrix &&
201 fClipStack == cur.fClipStack &&
202 (fTextScaleX == 0 ||
203 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000204}
205
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000206class GraphicStackState {
207public:
208 GraphicStackState(const SkClipStack& existingClipStack,
209 const SkRegion& existingClipRegion,
210 SkWStream* contentStream)
211 : fStackDepth(0),
212 fContentStream(contentStream) {
213 fEntries[0].fClipStack = existingClipStack;
214 fEntries[0].fClipRegion = existingClipRegion;
215 }
216
217 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000218 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000219 void updateMatrix(const SkMatrix& matrix);
halcanary2be7e012016-03-28 11:58:08 -0700220 void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000221
222 void drainStack();
223
224private:
225 void push();
226 void pop();
halcanary2be7e012016-03-28 11:58:08 -0700227 SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000228
229 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
230 static const int kMaxStackDepth = 12;
halcanary2be7e012016-03-28 11:58:08 -0700231 SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1];
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000232 int fStackDepth;
233 SkWStream* fContentStream;
234};
235
236void GraphicStackState::drainStack() {
237 while (fStackDepth) {
238 pop();
239 }
240}
241
242void GraphicStackState::push() {
243 SkASSERT(fStackDepth < kMaxStackDepth);
244 fContentStream->writeText("q\n");
245 fStackDepth++;
246 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
247}
248
249void GraphicStackState::pop() {
250 SkASSERT(fStackDepth > 0);
251 fContentStream->writeText("Q\n");
252 fStackDepth--;
253}
254
robertphillips@google.com80214e22012-07-20 15:33:18 +0000255// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000256// and then skips over the leading entries as specified in prefix. It requires
257// and asserts that "prefix" will be a prefix to "stack."
258static void skip_clip_stack_prefix(const SkClipStack& prefix,
259 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000260 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000261 SkClipStack::B2TIter prefixIter(prefix);
262 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000263
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000264 const SkClipStack::Element* prefixEntry;
265 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000266
267 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000268 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000269 iterEntry = iter->next();
270 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000271 // Because of SkClipStack does internal intersection, the last clip
272 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000273 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000274 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
275 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
276 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000277 // back up the iterator by one
278 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000279 prefixEntry = prefixIter.next();
280 break;
281 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000282 }
283
halcanary96fcdcc2015-08-27 07:41:13 -0700284 SkASSERT(prefixEntry == nullptr);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000285}
286
287static void emit_clip(SkPath* clipPath, SkRect* clipRect,
288 SkWStream* contentStream) {
289 SkASSERT(clipPath || clipRect);
290
291 SkPath::FillType clipFill;
292 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000293 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000294 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000295 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000296 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
297 clipFill = SkPath::kWinding_FillType;
298 }
299
300 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
301 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
302 if (clipFill == SkPath::kEvenOdd_FillType) {
303 contentStream->writeText("W* n\n");
304 } else {
305 contentStream->writeText("W n\n");
306 }
307}
308
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000309/* Calculate an inverted path's equivalent non-inverted path, given the
310 * canvas bounds.
311 * outPath may alias with invPath (since this is supported by PathOps).
312 */
313static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
314 SkPath* outPath) {
315 SkASSERT(invPath.isInverseFillType());
316
317 SkPath clipPath;
318 clipPath.addRect(bounds);
319
reedcdb42bb2015-06-26 10:23:07 -0700320 return Op(clipPath, invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000321}
322
323// Sanity check the numerical values of the SkRegion ops and PathOps ops
324// enums so region_op_to_pathops_op can do a straight passthrough cast.
325// If these are failing, it may be necessary to make region_op_to_pathops_op
326// do more.
bungeman99fe8222015-08-20 07:57:51 -0700327static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
328static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
329static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
330static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
331static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
332 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000333
334static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
335 SkASSERT(op >= 0);
336 SkASSERT(op <= SkRegion::kReverseDifference_Op);
337 return (SkPathOp)op;
338}
339
340/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
341 * Returns true if successful, or false if not successful.
342 * If successful, the resulting clip is stored in outClipPath.
343 * If not successful, outClipPath is undefined, and a fallback method
344 * should be used.
345 */
346static bool get_clip_stack_path(const SkMatrix& transform,
347 const SkClipStack& clipStack,
348 const SkRegion& clipRegion,
349 SkPath* outClipPath) {
350 outClipPath->reset();
351 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
352
353 const SkClipStack::Element* clipEntry;
354 SkClipStack::Iter iter;
355 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
356 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
357 SkPath entryPath;
358 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
359 outClipPath->reset();
360 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
361 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000362 } else {
363 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000364 }
365 entryPath.transform(transform);
366
367 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
368 *outClipPath = entryPath;
369 } else {
370 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
371 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
372 return false;
373 }
374 }
375 }
376
377 if (outClipPath->isInverseFillType()) {
378 // The bounds are slightly outset to ensure this is correct in the
379 // face of floating-point accuracy and possible SkRegion bitmap
380 // approximations.
381 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
382 clipBounds.outset(SK_Scalar1, SK_Scalar1);
383 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
384 return false;
385 }
386 }
387 return true;
388}
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000389
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000390// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000391// graphic state stack, and the fact that we can know all the clips used
392// on the page to optimize this.
393void GraphicStackState::updateClip(const SkClipStack& clipStack,
394 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000395 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000396 if (clipStack == currentEntry()->fClipStack) {
397 return;
398 }
399
400 while (fStackDepth > 0) {
401 pop();
402 if (clipStack == currentEntry()->fClipStack) {
403 return;
404 }
405 }
406 push();
407
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000408 currentEntry()->fClipStack = clipStack;
409 currentEntry()->fClipRegion = clipRegion;
410
411 SkMatrix transform;
412 transform.setTranslate(translation.fX, translation.fY);
413
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000414 SkPath clipPath;
415 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700416 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000417 return;
418 }
halcanarydda239e2016-03-31 07:33:57 -0700419
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000420 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
421 // already been applied. (If this is a top level device, then it specifies
422 // a clip to the content area. If this is a layer, then it specifies
423 // the clip in effect when the layer was created.) There's no need to
424 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
425 // initial clip on the parent layer. (This means there's a bug if the user
426 // expands the clip and then uses any xfer mode that uses dst:
427 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000428 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000429 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
430
431 // If the clip stack does anything other than intersect or if it uses
432 // an inverse fill type, we have to fall back to the clip region.
433 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000434 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000435 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000436 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
437 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000438 needRegion = true;
439 break;
440 }
441 }
442
443 if (needRegion) {
444 SkPath clipPath;
445 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700446 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000447 } else {
448 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000449 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000450 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000451 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
452 switch (clipEntry->getType()) {
453 case SkClipStack::Element::kRect_Type: {
454 SkRect translatedClip;
455 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700456 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000457 break;
458 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000459 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000460 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000461 clipEntry->asPath(&translatedPath);
462 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700463 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000464 break;
465 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000466 }
467 }
468 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000469}
470
471void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
472 if (matrix == currentEntry()->fMatrix) {
473 return;
474 }
475
476 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
477 SkASSERT(fStackDepth > 0);
478 SkASSERT(fEntries[fStackDepth].fClipStack ==
479 fEntries[fStackDepth -1].fClipStack);
480 pop();
481
482 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
483 }
484 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
485 return;
486 }
487
488 push();
489 SkPDFUtils::AppendTransform(matrix, fContentStream);
490 currentEntry()->fMatrix = matrix;
491}
492
halcanary2be7e012016-03-28 11:58:08 -0700493void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000494 // PDF treats a shader as a color, so we only set one or the other.
495 if (state.fShaderIndex >= 0) {
496 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000497 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000498 currentEntry()->fShaderIndex = state.fShaderIndex;
499 }
500 } else {
501 if (state.fColor != currentEntry()->fColor ||
502 currentEntry()->fShaderIndex >= 0) {
503 emit_pdf_color(state.fColor, fContentStream);
504 fContentStream->writeText("RG ");
505 emit_pdf_color(state.fColor, fContentStream);
506 fContentStream->writeText("rg\n");
507 currentEntry()->fColor = state.fColor;
508 currentEntry()->fShaderIndex = -1;
509 }
510 }
511
512 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000513 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000514 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
515 }
516
517 if (state.fTextScaleX) {
518 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
519 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
520 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700521 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000522 fContentStream->writeText(" Tz\n");
523 currentEntry()->fTextScaleX = state.fTextScaleX;
524 }
525 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700526 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
527 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
528 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000529 fContentStream->writeDecAsText(state.fTextFill);
530 fContentStream->writeText(" Tr\n");
531 currentEntry()->fTextFill = state.fTextFill;
532 }
533 }
534}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000535
reed76033be2015-03-14 10:54:31 -0700536static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700537 // PDF does not support image filters, so render them on CPU.
538 // Note that this rendering is done at "screen" resolution (100dpi), not
539 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700540 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800541 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700542
543 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700544 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700545}
546
547SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
halcanary00b7e5e2015-04-15 13:05:18 -0700548 if (cinfo.fForImageFilter ||
549 (layerPaint && not_supported_for_layers(*layerPaint))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700550 return nullptr;
senorblancob0e89dc2014-10-20 14:03:12 -0700551 }
fmalita6987dca2014-11-13 08:33:37 -0800552 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700553 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000554}
555
halcanary989da4a2016-03-21 14:33:17 -0700556SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
557
bsalomon@google.come97f0852011-06-17 13:10:25 +0000558
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000559
560// A helper class to automatically finish a ContentEntry at the end of a
561// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000562class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000563public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000564 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
565 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000566 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700567 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000568 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700569 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000570 init(draw.fClipStack, *draw.fClip, *draw.fMatrix, paint, hasText);
571 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000572 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
573 const SkRegion& clipRegion, const SkMatrix& matrix,
574 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000575 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700576 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000577 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700578 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000579 init(clipStack, clipRegion, matrix, paint, hasText);
580 }
581
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000582 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000583 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000584 SkPath* shape = &fShape;
585 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700586 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000587 }
588 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000589 }
reed@google.comfc641d02012-09-20 17:52:20 +0000590 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000591 }
592
halcanary2be7e012016-03-28 11:58:08 -0700593 SkPDFDevice::ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000594
595 /* Returns true when we explicitly need the shape of the drawing. */
596 bool needShape() {
597 switch (fXfermode) {
598 case SkXfermode::kClear_Mode:
599 case SkXfermode::kSrc_Mode:
600 case SkXfermode::kSrcIn_Mode:
601 case SkXfermode::kSrcOut_Mode:
602 case SkXfermode::kDstIn_Mode:
603 case SkXfermode::kDstOut_Mode:
604 case SkXfermode::kSrcATop_Mode:
605 case SkXfermode::kDstATop_Mode:
606 case SkXfermode::kModulate_Mode:
607 return true;
608 default:
609 return false;
610 }
611 }
612
613 /* Returns true unless we only need the shape of the drawing. */
614 bool needSource() {
615 if (fXfermode == SkXfermode::kClear_Mode) {
616 return false;
617 }
618 return true;
619 }
620
621 /* If the shape is different than the alpha component of the content, then
622 * setShape should be called with the shape. In particular, images and
623 * devices have rectangular shape.
624 */
625 void setShape(const SkPath& shape) {
626 fShape = shape;
627 }
628
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000629private:
630 SkPDFDevice* fDevice;
halcanary2be7e012016-03-28 11:58:08 -0700631 SkPDFDevice::ContentEntry* fContentEntry;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000632 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000633 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000634 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000635
636 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
637 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000638 // Shape has to be flatten before we get here.
639 if (matrix.hasPerspective()) {
640 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000641 return;
642 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000643 if (paint.getXfermode()) {
644 paint.getXfermode()->asMode(&fXfermode);
645 }
646 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
647 matrix, paint, hasText,
648 &fDstFormXObject);
649 }
650};
651
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000652////////////////////////////////////////////////////////////////////////////////
653
halcanary989da4a2016-03-21 14:33:17 -0700654SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
robertphillips9a53fd72015-06-22 09:46:59 -0700655 : INHERITED(SkSurfaceProps(0, kUnknown_SkPixelGeometry))
656 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800657 , fContentSize(pageSize)
658 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanary96fcdcc2015-08-27 07:41:13 -0700659 , fClipStack(nullptr)
halcanary385fe4d2015-08-26 13:07:48 -0700660 , fFontGlyphUsage(new SkPDFGlyphSetMap)
halcanarya1f1ee92015-02-20 06:17:26 -0800661 , fRasterDpi(rasterDpi)
halcanary989da4a2016-03-21 14:33:17 -0700662 , fDocument(doc) {
halcanarya1f1ee92015-02-20 06:17:26 -0800663 SkASSERT(pageSize.width() > 0);
664 SkASSERT(pageSize.height() > 0);
665 fLegacyBitmap.setInfo(
666 SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()));
667 if (flip) {
668 // Skia generally uses the top left as the origin but PDF
669 // natively has the origin at the bottom left. This matrix
670 // corrects for that. But that only needs to be done once, we
671 // don't do it when layering.
672 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
673 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
674 } else {
675 fInitialTransform.setIdentity();
676 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000677}
678
679SkPDFDevice::~SkPDFDevice() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000680 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000681}
682
683void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700684 fContentEntries.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700685 if (fFontGlyphUsage.get() == nullptr) {
halcanary385fe4d2015-08-26 13:07:48 -0700686 fFontGlyphUsage.reset(new SkPDFGlyphSetMap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000687 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000688}
689
vandebo@chromium.org98594282011-07-25 22:34:12 +0000690void SkPDFDevice::cleanUp(bool clearFontUsage) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000691 fGraphicStateResources.unrefAll();
692 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000693 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000694 fShaderResources.unrefAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000695
vandebo@chromium.org98594282011-07-25 22:34:12 +0000696 if (clearFontUsage) {
697 fFontGlyphUsage->reset();
698 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000699}
700
reedf70b5312016-03-04 16:36:20 -0800701void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
702 SkData* value) {
703 if (0 == rect.width() && 0 == rect.height()) {
704 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
705 } else {
706 SkPath path;
707 path.addRect(rect);
708 handlePathAnnotation(path, d, key, value);
709 }
710}
711
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000712void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000713 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700714 replace_srcmode_on_opaque_paint(&newPaint);
715
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000716 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000717 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000718 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000719}
720
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000721void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
halcanary2be7e012016-03-28 11:58:08 -0700722 SkPDFDevice::ContentEntry* contentEntry) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000723 if (!contentEntry) {
724 return;
725 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000726 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
727 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000728 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000729 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000730 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000731 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000732 inverse.mapRect(&bbox);
733
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000734 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000735 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000736 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000737}
738
halcanary682ee012016-01-28 10:59:34 -0800739void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700740 SkCanvas::PointMode mode,
741 size_t count,
742 const SkPoint* points,
743 const SkPaint& srcPaint) {
744 SkPaint passedPaint = srcPaint;
745 replace_srcmode_on_opaque_paint(&passedPaint);
746
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000747 if (count == 0) {
748 return;
749 }
750
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000751 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
752 // We only use this when there's a path effect because of the overhead
753 // of multiple calls to setUpContentEntry it causes.
754 if (passedPaint.getPathEffect()) {
755 if (d.fClip->isEmpty()) {
756 return;
757 }
758 SkDraw pointDraw(d);
759 pointDraw.fDevice = this;
760 pointDraw.drawPoints(mode, count, points, passedPaint, true);
761 return;
762 }
763
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000764 const SkPaint* paint = &passedPaint;
765 SkPaint modifiedPaint;
766
767 if (mode == SkCanvas::kPoints_PointMode &&
768 paint->getStrokeCap() != SkPaint::kRound_Cap) {
769 modifiedPaint = *paint;
770 paint = &modifiedPaint;
771 if (paint->getStrokeWidth()) {
772 // PDF won't draw a single point with square/butt caps because the
773 // orientation is ambiguous. Draw a rectangle instead.
774 modifiedPaint.setStyle(SkPaint::kFill_Style);
775 SkScalar strokeWidth = paint->getStrokeWidth();
776 SkScalar halfStroke = SkScalarHalf(strokeWidth);
777 for (size_t i = 0; i < count; i++) {
778 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
779 r.inset(-halfStroke, -halfStroke);
780 drawRect(d, r, modifiedPaint);
781 }
782 return;
783 } else {
784 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
785 }
786 }
787
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000788 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000789 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000790 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000791 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000792
793 switch (mode) {
794 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000795 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000796 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000797 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000798 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000799 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000800 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000801 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000802 break;
803 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000804 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000805 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000806 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000807 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000808 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000809 &content.entry()->fContent);
810 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000811 }
812 break;
813 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000814 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
815 for (size_t i = 0; i < count; i++) {
816 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000817 &content.entry()->fContent);
818 SkPDFUtils::ClosePath(&content.entry()->fContent);
819 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000820 }
821 break;
822 default:
823 SkASSERT(false);
824 }
825}
826
halcanary8103a342016-03-08 15:10:16 -0800827static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800828 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700829 annotation->insertName("Subtype", "Link");
830
halcanaryece83922016-03-08 08:32:12 -0800831 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700832 border->reserve(3);
833 border->appendInt(0); // Horizontal corner radius.
834 border->appendInt(0); // Vertical corner radius.
835 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800836 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700837
halcanaryece83922016-03-08 08:32:12 -0800838 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700839 rect->reserve(4);
840 rect->appendScalar(translatedRect.fLeft);
841 rect->appendScalar(translatedRect.fTop);
842 rect->appendScalar(translatedRect.fRight);
843 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800844 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700845
halcanary8103a342016-03-08 15:10:16 -0800846 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700847}
848
halcanary8103a342016-03-08 15:10:16 -0800849static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
850 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700851 SkString url(static_cast<const char *>(urlData->data()),
852 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800853 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700854 action->insertName("S", "URI");
855 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800856 annotation->insertObject("A", std::move(action));
857 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700858}
859
halcanary8103a342016-03-08 15:10:16 -0800860static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
861 const SkRect& r) {
862 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700863 SkString name(static_cast<const char *>(nameData->data()),
864 nameData->size() - 1);
865 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800866 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700867}
868
halcanary682ee012016-01-28 10:59:34 -0800869void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700870 const SkRect& rect,
871 const SkPaint& srcPaint) {
872 SkPaint paint = srcPaint;
873 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000874 SkRect r = rect;
875 r.sort();
876
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000877 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000878 if (d.fClip->isEmpty()) {
879 return;
880 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000881 SkPath path;
882 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700883 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000884 return;
885 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000886
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000887 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000888 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000889 return;
890 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000891 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000892 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000893 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000894}
895
halcanarya6814332015-05-27 08:53:36 -0700896void SkPDFDevice::drawRRect(const SkDraw& draw,
897 const SkRRect& rrect,
898 const SkPaint& srcPaint) {
899 SkPaint paint = srcPaint;
900 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000901 SkPath path;
902 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700903 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000904}
905
halcanarya6814332015-05-27 08:53:36 -0700906void SkPDFDevice::drawOval(const SkDraw& draw,
907 const SkRect& oval,
908 const SkPaint& srcPaint) {
909 SkPaint paint = srcPaint;
910 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700911 SkPath path;
912 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700913 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700914}
915
halcanary682ee012016-01-28 10:59:34 -0800916void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700917 const SkPath& origPath,
918 const SkPaint& srcPaint,
919 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000920 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700921 SkPaint paint = srcPaint;
922 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800923 SkPath modifiedPath;
924 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000925
926 SkMatrix matrix = *d.fMatrix;
927 if (prePathMatrix) {
928 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800929 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000930 pathPtr = &modifiedPath;
931 pathIsMutable = true;
932 }
halcanary682ee012016-01-28 10:59:34 -0800933 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000934 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000935 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000936 }
937 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000938
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000939 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000940 if (d.fClip->isEmpty()) {
941 return;
942 }
halcanary682ee012016-01-28 10:59:34 -0800943 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000944 pathPtr = &modifiedPath;
945 pathIsMutable = true;
946 }
halcanary682ee012016-01-28 10:59:34 -0800947 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000948
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000949 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700950 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000951 if (fill) {
952 noEffectPaint.setStyle(SkPaint::kFill_Style);
953 } else {
954 noEffectPaint.setStyle(SkPaint::kStroke_Style);
955 noEffectPaint.setStrokeWidth(0);
956 }
halcanary96fcdcc2015-08-27 07:41:13 -0700957 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000958 return;
959 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000960
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000961 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000962 return;
963 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000964
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000965 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000966 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000967 return;
968 }
halcanary8b2bc252015-10-06 09:41:47 -0700969 bool consumeDegeratePathSegments =
970 paint.getStyle() == SkPaint::kFill_Style ||
971 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
972 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000973 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -0700974 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000975 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000976 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000977 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000978}
979
halcanary7a14b312015-10-01 07:28:13 -0700980void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
981 const SkBitmap& bitmap,
982 const SkRect* src,
983 const SkRect& dst,
984 const SkPaint& srcPaint,
985 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700986 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -0700987}
988
989void SkPDFDevice::drawBitmap(const SkDraw& d,
990 const SkBitmap& bitmap,
991 const SkMatrix& matrix,
992 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -0700993 SkPaint paint = srcPaint;
994 if (bitmap.isOpaque()) {
995 replace_srcmode_on_opaque_paint(&paint);
996 }
997
halcanary7a14b312015-10-01 07:28:13 -0700998 if (d.fClip->isEmpty()) {
999 return;
1000 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001001
halcanary7a14b312015-10-01 07:28:13 -07001002 SkMatrix transform = matrix;
1003 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001004 SkImageBitmap imageBitmap(bitmap);
1005 this->internalDrawImage(
1006 transform, d.fClipStack, *d.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001007}
1008
1009void SkPDFDevice::drawSprite(const SkDraw& d,
1010 const SkBitmap& bitmap,
1011 int x,
1012 int y,
1013 const SkPaint& srcPaint) {
1014 SkPaint paint = srcPaint;
1015 if (bitmap.isOpaque()) {
1016 replace_srcmode_on_opaque_paint(&paint);
1017 }
1018
1019 if (d.fClip->isEmpty()) {
1020 return;
1021 }
1022
1023 SkMatrix matrix;
1024 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -07001025 SkImageBitmap imageBitmap(bitmap);
1026 this->internalDrawImage(
1027 matrix, d.fClipStack, *d.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001028}
1029
1030void SkPDFDevice::drawImage(const SkDraw& draw,
1031 const SkImage* image,
1032 SkScalar x,
1033 SkScalar y,
1034 const SkPaint& srcPaint) {
1035 SkPaint paint = srcPaint;
1036 if (!image) {
1037 return;
1038 }
1039 if (image->isOpaque()) {
1040 replace_srcmode_on_opaque_paint(&paint);
1041 }
1042 if (draw.fClip->isEmpty()) {
1043 return;
1044 }
1045 SkMatrix transform = SkMatrix::MakeTrans(x, y);
1046 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001047 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
1048 this->internalDrawImage(
1049 transform, draw.fClipStack, *draw.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001050}
1051
1052void SkPDFDevice::drawImageRect(const SkDraw& draw,
1053 const SkImage* image,
1054 const SkRect* src,
1055 const SkRect& dst,
1056 const SkPaint& srcPaint,
1057 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001058 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001059}
1060
halcanarybb264b72015-04-07 10:40:03 -07001061// Create a PDF string. Maximum length (in bytes) is 65,535.
1062// @param input A string value.
1063// @param len The length of the input array.
1064// @param wideChars True iff the upper byte in each uint16_t is
1065// significant and should be encoded and not
1066// discarded. If true, the upper byte is encoded
1067// first. Otherwise, we assert the upper byte is
1068// zero.
1069static SkString format_wide_string(const uint16_t* input,
1070 size_t len,
1071 bool wideChars) {
1072 if (wideChars) {
1073 SkASSERT(2 * len < 65535);
1074 static const char gHex[] = "0123456789ABCDEF";
1075 SkString result(4 * len + 2);
1076 result[0] = '<';
1077 for (size_t i = 0; i < len; i++) {
1078 result[4 * i + 1] = gHex[(input[i] >> 12) & 0xF];
1079 result[4 * i + 2] = gHex[(input[i] >> 8) & 0xF];
1080 result[4 * i + 3] = gHex[(input[i] >> 4) & 0xF];
1081 result[4 * i + 4] = gHex[(input[i] ) & 0xF];
1082 }
1083 result[4 * len + 1] = '>';
1084 return result;
1085 } else {
1086 SkASSERT(len <= 65535);
1087 SkString tmp(len);
1088 for (size_t i = 0; i < len; i++) {
1089 SkASSERT(0 == input[i] >> 8);
1090 tmp[i] = static_cast<uint8_t>(input[i]);
1091 }
halcanarybc4696b2015-05-06 10:56:04 -07001092 return SkPDFUtils::FormatString(tmp.c_str(), tmp.size());
halcanarybb264b72015-04-07 10:40:03 -07001093 }
1094}
1095
halcanary66a82f32015-10-12 13:05:04 -07001096static void draw_transparent_text(SkPDFDevice* device,
1097 const SkDraw& d,
1098 const void* text, size_t len,
1099 SkScalar x, SkScalar y,
1100 const SkPaint& srcPaint) {
1101
1102 SkPaint transparent;
1103 if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(),
1104 device->getCanon())) {
1105 SkDEBUGFAIL("default typeface should be embeddable");
1106 return; // Avoid infinite loop in release.
1107 }
1108 transparent.setTextSize(srcPaint.getTextSize());
1109 transparent.setColor(SK_ColorTRANSPARENT);
1110 switch (srcPaint.getTextEncoding()) {
1111 case SkPaint::kGlyphID_TextEncoding: {
1112 // Since a glyphId<->Unicode mapping is typeface-specific,
1113 // map back to Unicode first.
1114 size_t glyphCount = len / 2;
1115 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1116 srcPaint.glyphsToUnichars(
1117 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1118 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
1119 device->drawText(d, &unichars[0],
1120 glyphCount * sizeof(SkUnichar),
1121 x, y, transparent);
1122 break;
1123 }
1124 case SkPaint::kUTF8_TextEncoding:
1125 case SkPaint::kUTF16_TextEncoding:
1126 case SkPaint::kUTF32_TextEncoding:
1127 transparent.setTextEncoding(srcPaint.getTextEncoding());
1128 device->drawText(d, text, len, x, y, transparent);
1129 break;
1130 default:
1131 SkFAIL("unknown text encoding");
1132 }
1133}
1134
1135
halcanary682ee012016-01-28 10:59:34 -08001136void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
halcanarya6814332015-05-27 08:53:36 -07001137 SkScalar x, SkScalar y, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001138 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary6950de62015-11-07 05:29:00 -08001139 // https://bug.skia.org/3866
halcanary66a82f32015-10-12 13:05:04 -07001140 SkPath path;
1141 srcPaint.getTextPath(text, len, x, y, &path);
1142 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1143 // Draw text transparently to make it copyable/searchable/accessable.
1144 draw_transparent_text(this, d, text, len, x, y, srcPaint);
1145 return;
1146 }
halcanarya6814332015-05-27 08:53:36 -07001147 SkPaint paint = srcPaint;
1148 replace_srcmode_on_opaque_paint(&paint);
1149
halcanary96fcdcc2015-08-27 07:41:13 -07001150 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1151 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001152 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1153 // making text unreadable (e.g. same text twice when using CSS shadows).
1154 return;
1155 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001156 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001157 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001158 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001159 return;
1160 }
1161
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001162 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001163 const uint16_t* glyphIDs = nullptr;
reed@google.comaec40662014-04-18 19:29:07 +00001164 int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001165 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001166
benjaminwagnerd936f632016-02-23 10:44:31 -08001167 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001168 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001169 content.entry()->fContent.writeText("BT\n");
1170 set_text_transform(x, y, textPaint.getTextSkewX(),
1171 &content.entry()->fContent);
reed@google.comaec40662014-04-18 19:29:07 +00001172 int consumedGlyphCount = 0;
halcanary2f912f32014-10-16 09:53:20 -07001173
1174 SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs);
1175
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001176 while (numGlyphs > consumedGlyphCount) {
robertphillips8e0c1502015-07-07 10:28:43 -07001177 this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001178 SkPDFFont* font = content.entry()->fState.fFont;
halcanary2f912f32014-10-16 09:53:20 -07001179
1180 int availableGlyphs = font->glyphsToPDFFontEncoding(
1181 glyphIDsCopy.begin() + consumedGlyphCount,
1182 numGlyphs - consumedGlyphCount);
1183 fFontGlyphUsage->noteGlyphUsage(
1184 font, glyphIDsCopy.begin() + consumedGlyphCount,
1185 availableGlyphs);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001186 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001187 format_wide_string(glyphIDsCopy.begin() + consumedGlyphCount,
1188 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001189 content.entry()->fContent.writeText(encodedString.c_str());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001190 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001191 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001192 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001193 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001194}
1195
halcanary682ee012016-01-28 10:59:34 -08001196void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -07001197 const SkScalar pos[], int scalarsPerPos,
halcanary682ee012016-01-28 10:59:34 -08001198 const SkPoint& offset, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001199 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary66a82f32015-10-12 13:05:04 -07001200 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos);
1201 SkAutoTMalloc<SkPoint> positionsBuffer;
1202 if (2 != scalarsPerPos) {
1203 int glyphCount = srcPaint.textToGlyphs(text, len, NULL);
1204 positionsBuffer.reset(glyphCount);
1205 for (int i = 0; i < glyphCount; ++i) {
1206 positionsBuffer[i].set(pos[i], 0.0f);
1207 }
1208 positions = &positionsBuffer[0];
1209 }
1210 SkPath path;
1211 srcPaint.getPosTextPath(text, len, positions, &path);
1212 SkMatrix matrix;
1213 matrix.setTranslate(offset);
1214 this->drawPath(d, path, srcPaint, &matrix, true);
1215 // Draw text transparently to make it copyable/searchable/accessable.
1216 draw_transparent_text(
1217 this, d, text, len, offset.x() + positions[0].x(),
1218 offset.y() + positions[0].y(), srcPaint);
1219 return;
1220 }
1221
halcanarya6814332015-05-27 08:53:36 -07001222 SkPaint paint = srcPaint;
1223 replace_srcmode_on_opaque_paint(&paint);
1224
halcanary96fcdcc2015-08-27 07:41:13 -07001225 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1226 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001227 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1228 // making text unreadable (e.g. same text twice when using CSS shadows).
1229 return;
1230 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001231 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001232 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001233 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001234 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001235 return;
1236 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001237
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001238 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001239 const uint16_t* glyphIDs = nullptr;
bungeman22edc832014-10-03 07:55:58 -07001240 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001241 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001242
benjaminwagnerd936f632016-02-23 10:44:31 -08001243 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001244 content.entry()->fContent.writeText("BT\n");
robertphillips8e0c1502015-07-07 10:28:43 -07001245 this->updateFont(textPaint, glyphIDs[0], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001246 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001247 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001248 uint16_t encodedValue = glyphIDs[i];
1249 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
bungeman22edc832014-10-03 07:55:58 -07001250 // The current pdf font cannot encode the current glyph.
1251 // Try to get a pdf font which can encode the current glyph.
robertphillips8e0c1502015-07-07 10:28:43 -07001252 this->updateFont(textPaint, glyphIDs[i], content.entry());
bungeman22edc832014-10-03 07:55:58 -07001253 font = content.entry()->fState.fFont;
1254 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
1255 SkDEBUGFAIL("PDF could not encode glyph.");
1256 continue;
1257 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001258 }
bungeman22edc832014-10-03 07:55:58 -07001259
vandebo@chromium.org98594282011-07-25 22:34:12 +00001260 fFontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
fmalita05c4a432014-09-29 06:29:53 -07001261 SkScalar x = offset.x() + pos[i * scalarsPerPos];
1262 SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0);
1263
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001264 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
bungeman22edc832014-10-03 07:55:58 -07001265 set_text_transform(x, y, textPaint.getTextSkewX(), &content.entry()->fContent);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001266 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001267 format_wide_string(&encodedValue, 1, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001268 content.entry()->fContent.writeText(encodedString.c_str());
1269 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001270 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001271 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001272}
1273
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001274void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001275 int vertexCount, const SkPoint verts[],
1276 const SkPoint texs[], const SkColor colors[],
1277 SkXfermode* xmode, const uint16_t indices[],
1278 int indexCount, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001279 if (d.fClip->isEmpty()) {
1280 return;
1281 }
reed@google.com85e143c2013-12-30 15:51:25 +00001282 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001283}
1284
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001285void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1286 int x, int y, const SkPaint& paint) {
fmalita6987dca2014-11-13 08:33:37 -08001287 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001288 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001289
1290 SkScalar scalarX = SkIntToScalar(x);
1291 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001292 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1293 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001294 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001295 }
halcanary91fcb3e2016-03-04 13:53:22 -08001296 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1297 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001298 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001299 }
halcanary91fcb3e2016-03-04 13:53:22 -08001300 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1301 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001302 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001303 }
1304
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001305 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001306 return;
1307 }
1308
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001309 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001310 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001311 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001312 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001313 return;
1314 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001315 if (content.needShape()) {
1316 SkPath shape;
1317 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001318 SkIntToScalar(device->width()),
1319 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001320 content.setShape(shape);
1321 }
1322 if (!content.needSource()) {
1323 return;
1324 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001325
halcanaryece83922016-03-08 08:32:12 -08001326 auto xObject = sk_make_sp<SkPDFFormXObject>(pdfDevice);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001327 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001328 &content.entry()->fContent);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001329
1330 // Merge glyph sets from the drawn device.
1331 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001332}
1333
reed89443ab2014-06-27 11:34:19 -07001334SkImageInfo SkPDFDevice::imageInfo() const {
1335 return fLegacyBitmap.info();
1336}
1337
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001338void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1339 INHERITED::onAttachToCanvas(canvas);
1340
1341 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1342 fClipStack = canvas->getClipStack();
1343}
1344
1345void SkPDFDevice::onDetachFromCanvas() {
1346 INHERITED::onDetachFromCanvas();
1347
halcanary96fcdcc2015-08-27 07:41:13 -07001348 fClipStack = nullptr;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001349}
1350
reede8f30622016-03-23 18:59:25 -07001351sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1352 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001353}
1354
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001355
halcanary8103a342016-03-08 15:10:16 -08001356sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001357 SkTDArray<SkPDFObject*> fonts;
1358 fonts.setReserve(fFontResources.count());
1359 for (SkPDFFont* font : fFontResources) {
1360 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001361 }
halcanary8103a342016-03-08 15:10:16 -08001362 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001363 &fGraphicStateResources,
1364 &fShaderResources,
1365 &fXObjectResources,
1366 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001367}
1368
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001369const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1370 return fFontResources;
1371}
1372
halcanary8103a342016-03-08 15:10:16 -08001373sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001374 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001375 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001376 mediaBox->appendInt(0);
1377 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001378 mediaBox->appendInt(fPageSize.width());
1379 mediaBox->appendInt(fPageSize.height());
1380 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001381}
1382
mtklein5f939ab2016-03-16 10:28:35 -07001383std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001384 SkDynamicMemoryWStream buffer;
1385 this->writeContent(&buffer);
mtklein5f939ab2016-03-16 10:28:35 -07001386 return std::unique_ptr<SkStreamAsset>(
halcanary8103a342016-03-08 15:10:16 -08001387 buffer.bytesWritten() > 0
1388 ? buffer.detachAsStream()
1389 : new SkMemoryStream);
reed@google.com5667afc2011-06-27 14:42:15 +00001390}
1391
halcanary334fcbc2015-02-24 12:56:16 -08001392void SkPDFDevice::writeContent(SkWStream* out) const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001393 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanary334fcbc2015-02-24 12:56:16 -08001394 SkPDFUtils::AppendTransform(fInitialTransform, out);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001395 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001396
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001397 // If the content area is the entire page, then we don't need to clip
1398 // the content area (PDF area clips to the page size). Otherwise,
1399 // we have to clip to the content area; we've already applied the
1400 // initial transform, so just clip to the device size.
1401 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001402 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1403 SkIntToScalar(this->height()));
halcanary96fcdcc2015-08-27 07:41:13 -07001404 emit_clip(nullptr, &r, out);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001405 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001406
halcanary2be7e012016-03-28 11:58:08 -07001407 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, out);
1408 for (const auto& entry : fContentEntries) {
1409 SkPoint translation;
1410 translation.iset(this->getOrigin());
1411 translation.negate();
1412 gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion,
1413 translation);
1414 gsState.updateMatrix(entry.fState.fMatrix);
1415 gsState.updateDrawingState(entry.fState);
1416
1417 entry.fContent.writeToStream(out);
1418 }
1419 gsState.drainStack();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001420}
1421
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001422/* Draws an inverse filled path by using Path Ops to compute the positive
1423 * inverse using the current clip as the inverse bounds.
1424 * Return true if this was an inverse path and was properly handled,
1425 * otherwise returns false and the normal drawing routine should continue,
1426 * either as a (incorrect) fallback or because the path was not inverse
1427 * in the first place.
1428 */
1429bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001430 const SkPaint& paint, bool pathIsMutable,
1431 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001432 if (!origPath.isInverseFillType()) {
1433 return false;
1434 }
1435
1436 if (d.fClip->isEmpty()) {
1437 return false;
1438 }
1439
1440 SkPath modifiedPath;
1441 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1442 SkPaint noInversePaint(paint);
1443
1444 // Merge stroking operations into final path.
1445 if (SkPaint::kStroke_Style == paint.getStyle() ||
1446 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1447 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1448 if (doFillPath) {
1449 noInversePaint.setStyle(SkPaint::kFill_Style);
1450 noInversePaint.setStrokeWidth(0);
1451 pathPtr = &modifiedPath;
1452 } else {
1453 // To be consistent with the raster output, hairline strokes
1454 // are rendered as non-inverted.
1455 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001456 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001457 return true;
1458 }
1459 }
1460
1461 // Get bounds of clip in current transform space
1462 // (clip bounds are given in device space).
1463 SkRect bounds;
1464 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001465 SkMatrix totalMatrix = *d.fMatrix;
1466 if (prePathMatrix) {
1467 totalMatrix.preConcat(*prePathMatrix);
1468 }
1469 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001470 return false;
1471 }
1472 bounds.set(d.fClip->getBounds());
1473 transformInverse.mapRect(&bounds);
1474
1475 // Extend the bounds by the line width (plus some padding)
1476 // so the edge doesn't cause a visible stroke.
1477 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1478 paint.getStrokeWidth() + SK_Scalar1);
1479
1480 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1481 return false;
1482 }
1483
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001484 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001485 return true;
1486}
1487
reedf70b5312016-03-04 16:36:20 -08001488void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001489 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001490 const char key[], SkData* value) {
1491 if (!value) {
1492 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001493 }
reedf70b5312016-03-04 16:36:20 -08001494
1495 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1496 SkPoint transformedPoint;
1497 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1498 fNamedDestinations.emplace_back(value, transformedPoint);
1499 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001500}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001501
reedf70b5312016-03-04 16:36:20 -08001502void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001503 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001504 const char key[], SkData* value) {
1505 if (!value) {
1506 return;
1507 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001508
1509 SkPath transformedPath = path;
1510 transformedPath.transform(*d.fMatrix);
1511 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001512 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1513 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001514 SkRect transformedRect = SkRect::Make(clip.getBounds());
1515
reedf70b5312016-03-04 16:36:20 -08001516 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001517 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001518 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001519 }
reedf70b5312016-03-04 16:36:20 -08001520 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001521 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001522 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001523 }
reed16108352016-03-03 09:14:36 -08001524 }
halcanary438de492015-04-28 06:21:01 -07001525}
1526
wangxianzhuef6c50a2015-09-17 20:38:02 -07001527void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1528 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001529 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001530 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001531 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001532 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001533 }
halcanary91fcb3e2016-03-04 13:53:22 -08001534 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001535 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001536 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001537 array->appendObject(
1538 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001539 }
1540}
epoger@google.comb58772f2013-03-08 09:09:10 +00001541
halcanary6d622702015-03-25 08:45:42 -07001542void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001543 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001544 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001545 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001546 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001547 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001548 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001549 pdfDest->appendScalar(p.x());
1550 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001551 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001552 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001553 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001554 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001555}
1556
reed@google.comfc641d02012-09-20 17:52:20 +00001557SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
halcanary385fe4d2015-08-26 13:07:48 -07001558 SkPDFFormXObject* xobject = new SkPDFFormXObject(this);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001559 // We always draw the form xobjects that we create back into the device, so
1560 // we simply preserve the font usage instead of pulling it out and merging
1561 // it back in later.
1562 cleanUp(false); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001563 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001564 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001565}
1566
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001567void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
1568 SkPDFFormXObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001569 const SkClipStack* clipStack,
1570 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001571 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001572 bool invertClip) {
1573 if (clipRegion.isEmpty() && !invertClip) {
1574 return;
1575 }
1576
halcanary1437c1e2016-03-13 18:30:24 -07001577 auto sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanary989da4a2016-03-21 14:33:17 -07001578 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001579
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001580 SkMatrix identity;
1581 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001582 SkPaint paint;
1583 paint.setXfermodeMode(mode);
1584 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001585 if (!content.entry()) {
1586 return;
1587 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001588 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001589 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001590 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001591
halcanary1437c1e2016-03-13 18:30:24 -07001592 // Call makeNoSmaskGraphicState() instead of
1593 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1594 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001595 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001596 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001597 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001598}
1599
halcanary2be7e012016-03-28 11:58:08 -07001600SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001601 const SkRegion& clipRegion,
1602 const SkMatrix& matrix,
1603 const SkPaint& paint,
1604 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001605 SkPDFFormXObject** dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001606 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001607 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001608 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001609 }
1610
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001611 // The clip stack can come from an SkDraw where it is technically optional.
1612 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001613 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001614 if (clipRegion == fExistingClipRegion) {
1615 clipStack = &fExistingClipStack;
1616 } else {
1617 // GraphicStackState::updateClip expects the clip stack to have
1618 // fExistingClip as a prefix, so start there, then set the clip
1619 // to the passed region.
1620 synthesizedClipStack = fExistingClipStack;
1621 SkPath clipPath;
1622 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001623 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1624 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001625 clipStack = &synthesizedClipStack;
1626 }
1627 }
1628
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001629 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1630 if (paint.getXfermode()) {
1631 paint.getXfermode()->asMode(&xfermode);
1632 }
1633
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001634 // For the following modes, we want to handle source and destination
1635 // separately, so make an object of what's already there.
1636 if (xfermode == SkXfermode::kClear_Mode ||
1637 xfermode == SkXfermode::kSrc_Mode ||
1638 xfermode == SkXfermode::kSrcIn_Mode ||
1639 xfermode == SkXfermode::kDstIn_Mode ||
1640 xfermode == SkXfermode::kSrcOut_Mode ||
1641 xfermode == SkXfermode::kDstOut_Mode ||
1642 xfermode == SkXfermode::kSrcATop_Mode ||
1643 xfermode == SkXfermode::kDstATop_Mode ||
1644 xfermode == SkXfermode::kModulate_Mode) {
1645 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001646 *dst = createFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001647 SkASSERT(isContentEmpty());
1648 } else if (xfermode != SkXfermode::kSrc_Mode &&
1649 xfermode != SkXfermode::kSrcOut_Mode) {
1650 // Except for Src and SrcOut, if there isn't anything already there,
1651 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001652 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001653 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001654 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001655 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001656 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001657
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001658 // Dst xfer mode doesn't draw source at all.
1659 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001660 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001661 }
1662
halcanary2be7e012016-03-28 11:58:08 -07001663 SkPDFDevice::ContentEntry* entry;
1664 if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) {
1665 entry = fContentEntries.back();
1666 } else if (xfermode != SkXfermode::kDstOver_Mode) {
1667 entry = fContentEntries.emplace_back();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001668 } else {
halcanary2be7e012016-03-28 11:58:08 -07001669 entry = fContentEntries.emplace_front();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001670 }
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001671 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001672 hasText, &entry->fState);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001673 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001674}
1675
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001676void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001677 SkPDFFormXObject* dst,
1678 SkPath* shape) {
1679 if (xfermode != SkXfermode::kClear_Mode &&
1680 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001681 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001682 xfermode != SkXfermode::kSrcIn_Mode &&
1683 xfermode != SkXfermode::kDstIn_Mode &&
1684 xfermode != SkXfermode::kSrcOut_Mode &&
1685 xfermode != SkXfermode::kDstOut_Mode &&
1686 xfermode != SkXfermode::kSrcATop_Mode &&
1687 xfermode != SkXfermode::kDstATop_Mode &&
1688 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001689 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001690 return;
1691 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001692 if (xfermode == SkXfermode::kDstOver_Mode) {
1693 SkASSERT(!dst);
halcanary2be7e012016-03-28 11:58:08 -07001694 if (fContentEntries.front()->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001695 // For DstOver, an empty content entry was inserted before the rest
1696 // of the content entries. If nothing was drawn, it needs to be
1697 // removed.
halcanary2be7e012016-03-28 11:58:08 -07001698 fContentEntries.pop_front();
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001699 }
1700 return;
1701 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001702 if (!dst) {
1703 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1704 xfermode == SkXfermode::kSrcOut_Mode);
1705 return;
1706 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001707
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001708 SkASSERT(dst);
halcanary2be7e012016-03-28 11:58:08 -07001709 SkASSERT(fContentEntries.count() == 1);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001710 // Changing the current content into a form-xobject will destroy the clip
1711 // objects which is fine since the xobject will already be clipped. However
1712 // if source has shape, we need to clip it too, so a copy of the clip is
1713 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001714
1715 SkClipStack clipStack = fContentEntries.front()->fState.fClipStack;
1716 SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001717
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001718 SkMatrix identity;
1719 identity.reset();
1720 SkPaint stockPaint;
1721
halcanary48810a02016-03-07 14:57:50 -08001722 sk_sp<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001723 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001724 // If nothing was drawn and there's no shape, then the draw was a
1725 // no-op, but dst needs to be restored for that to be true.
1726 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1727 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1728 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001729 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001730 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001731 ScopedContentEntry content(this, &fExistingClipStack,
1732 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001733 stockPaint);
1734 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1735 &content.entry()->fContent);
1736 return;
1737 } else {
1738 xfermode = SkXfermode::kClear_Mode;
1739 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001740 } else {
halcanary2be7e012016-03-28 11:58:08 -07001741 SkASSERT(fContentEntries.count() == 1);
reed@google.comfc641d02012-09-20 17:52:20 +00001742 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001743 }
1744
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001745 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1746 // without alpha.
1747 if (xfermode == SkXfermode::kSrcATop_Mode) {
1748 // TODO(vandebo): In order to properly support SrcATop we have to track
1749 // the shape of what's been drawn at all times. It's the intersection of
1750 // the non-transparent parts of the device and the outlines (shape) of
1751 // all images and devices drawn.
1752 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001753 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001754 SkXfermode::kSrcOver_Mode, true);
1755 } else {
halcanary48810a02016-03-07 14:57:50 -08001756 sk_sp<SkPDFFormXObject> dstMaskStorage;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001757 SkPDFFormXObject* dstMask = srcFormXObject.get();
halcanary96fcdcc2015-08-27 07:41:13 -07001758 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001759 // Draw shape into a form-xobject.
1760 SkDraw d;
1761 d.fMatrix = &identity;
1762 d.fClip = &clipRegion;
1763 d.fClipStack = &clipStack;
1764 SkPaint filledPaint;
1765 filledPaint.setColor(SK_ColorBLACK);
1766 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001767 this->drawPath(d, *shape, filledPaint, nullptr, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001768
1769 dstMaskStorage.reset(createFormXObjectFromDevice());
1770 dstMask = dstMaskStorage.get();
1771 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001772 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1773 &fExistingClipStack, fExistingClipRegion,
1774 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001775 }
1776
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001777 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001778 return;
1779 } else if (xfermode == SkXfermode::kSrc_Mode ||
1780 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001781 ScopedContentEntry content(this, &fExistingClipStack,
1782 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001783 if (content.entry()) {
1784 SkPDFUtils::DrawFormXObject(
1785 this->addXObjectResource(srcFormXObject.get()),
1786 &content.entry()->fContent);
1787 }
1788 if (xfermode == SkXfermode::kSrc_Mode) {
1789 return;
1790 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001791 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001792 ScopedContentEntry content(this, &fExistingClipStack,
1793 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001794 if (content.entry()) {
1795 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1796 &content.entry()->fContent);
1797 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001798 }
1799
1800 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1801 xfermode == SkXfermode::kDstIn_Mode ||
1802 xfermode == SkXfermode::kSrcOut_Mode ||
1803 xfermode == SkXfermode::kDstOut_Mode ||
1804 xfermode == SkXfermode::kSrcATop_Mode ||
1805 xfermode == SkXfermode::kDstATop_Mode ||
1806 xfermode == SkXfermode::kModulate_Mode);
1807
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001808 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001809 xfermode == SkXfermode::kSrcOut_Mode ||
1810 xfermode == SkXfermode::kSrcATop_Mode) {
1811 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001812 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001813 SkXfermode::kSrcOver_Mode,
1814 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001815 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001816 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
1817 if (xfermode == SkXfermode::kModulate_Mode) {
1818 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001819 dst, &fExistingClipStack,
1820 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001821 SkXfermode::kSrcOver_Mode, false);
1822 mode = SkXfermode::kMultiply_Mode;
1823 }
1824 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001825 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001826 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001827 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001828}
1829
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001830bool SkPDFDevice::isContentEmpty() {
halcanary2be7e012016-03-28 11:58:08 -07001831 if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) {
1832 SkASSERT(fContentEntries.count() <= 1);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001833 return true;
1834 }
1835 return false;
1836}
1837
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001838void SkPDFDevice::populateGraphicStateEntryFromPaint(
1839 const SkMatrix& matrix,
1840 const SkClipStack& clipStack,
1841 const SkRegion& clipRegion,
1842 const SkPaint& paint,
1843 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001844 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001845 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1846 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1847 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001848
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001849 entry->fMatrix = matrix;
1850 entry->fClipStack = clipStack;
1851 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001852 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1853 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001854
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001855 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001856 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001857 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001858 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001859 if (shader) {
1860 // PDF positions patterns relative to the initial transform, so
1861 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001862 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001863 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001864
1865 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001866 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001867 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001868
1869 // We need to apply the initial transform to bounds in order to get
1870 // bounds in a consistent coordinate system.
1871 SkRect boundsTemp;
1872 boundsTemp.set(bounds);
1873 fInitialTransform.mapRect(&boundsTemp);
1874 boundsTemp.roundOut(&bounds);
1875
halcanary792c80f2015-02-20 07:21:05 -08001876 SkScalar rasterScale =
1877 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
1878 pdfShader.reset(SkPDFShader::GetPDFShader(
reedfe630452016-03-25 09:08:00 -07001879 fDocument, fRasterDpi, shader, transform, bounds, rasterScale));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001880
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001881 if (pdfShader.get()) {
1882 // pdfShader has been canonicalized so we can directly compare
1883 // pointers.
1884 int resourceIndex = fShaderResources.find(pdfShader.get());
1885 if (resourceIndex < 0) {
1886 resourceIndex = fShaderResources.count();
1887 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001888 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001889 }
1890 entry->fShaderIndex = resourceIndex;
1891 } else {
1892 // A color shader is treated as an invalid shader so we don't have
1893 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001894 SkShader::GradientInfo gradientInfo;
1895 SkColor gradientColor;
1896 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001897 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001898 gradientInfo.fColorCount = 1;
1899 if (shader->asAGradient(&gradientInfo) ==
1900 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001901 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1902 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001903 }
1904 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001905 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001906
halcanary48810a02016-03-07 14:57:50 -08001907 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001908 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001909 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001910 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001911 } else {
1912 SkPaint newPaint = paint;
1913 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001914 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001915 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001916 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001917 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001918 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001919
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001920 if (hasText) {
1921 entry->fTextScaleX = paint.getTextScaleX();
1922 entry->fTextFill = paint.getStyle();
1923 } else {
1924 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001925 }
1926}
1927
halcanarybe27a112015-04-01 13:31:19 -07001928int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001929 // Assumes that gs has been canonicalized (so we can directly compare
1930 // pointers).
1931 int result = fGraphicStateResources.find(gs);
1932 if (result < 0) {
1933 result = fGraphicStateResources.count();
1934 fGraphicStateResources.push(gs);
1935 gs->ref();
1936 }
1937 return result;
1938}
1939
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001940int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
1941 // Assumes that xobject has been canonicalized (so we can directly compare
1942 // pointers).
1943 int result = fXObjectResources.find(xObject);
1944 if (result < 0) {
1945 result = fXObjectResources.count();
1946 fXObjectResources.push(xObject);
1947 xObject->ref();
1948 }
1949 return result;
1950}
1951
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001952void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
halcanary2be7e012016-03-28 11:58:08 -07001953 SkPDFDevice::ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001954 SkTypeface* typeface = paint.getTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -07001955 if (contentEntry->fState.fFont == nullptr ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001956 contentEntry->fState.fTextSize != paint.getTextSize() ||
1957 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001958 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001959 contentEntry->fContent.writeText("/");
1960 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
1961 SkPDFResourceDict::kFont_ResourceType,
1962 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001963 contentEntry->fContent.writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -07001964 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001965 contentEntry->fContent.writeText(" Tf\n");
1966 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001967 }
1968}
1969
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001970int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08001971 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07001972 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001973 int resourceIndex = fFontResources.find(newFont.get());
1974 if (resourceIndex < 0) {
1975 resourceIndex = fFontResources.count();
1976 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001977 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001978 }
1979 return resourceIndex;
1980}
1981
halcanary7a14b312015-10-01 07:28:13 -07001982static SkSize rect_to_size(const SkRect& r) {
1983 return SkSize::Make(r.width(), r.height());
1984}
1985
halcanarya50151d2016-03-25 11:57:49 -07001986static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
1987 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07001988 auto surface =
halcanarya50151d2016-03-25 11:57:49 -07001989 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
1990 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07001991 SkCanvas* canvas = surface->getCanvas();
1992 canvas->clear(SK_ColorTRANSPARENT);
1993 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07001994 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07001995 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07001996 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07001997 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07001998}
1999
2000////////////////////////////////////////////////////////////////////////////////
2001void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
2002 const SkClipStack* clipStack,
2003 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07002004 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07002005 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07002006 if (imageBitmap.dimensions().isZero()) {
2007 return;
2008 }
halcanary7a14b312015-10-01 07:28:13 -07002009 #ifdef SK_PDF_IMAGE_STATS
2010 gDrawImageCalls.fetch_add(1);
2011 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002012 SkMatrix matrix = origMatrix;
2013 SkRegion perspectiveBounds;
2014 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07002015 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002016
2017 // Rasterize the bitmap using perspective in a new bitmap.
2018 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002019 if (fRasterDpi == 0) {
2020 return;
2021 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002022 // Transform the bitmap in the new space, without taking into
2023 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002024 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07002025 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07002026 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002027 perspectiveOutline.transform(origMatrix);
2028
2029 // TODO(edisonn): perf - use current clip too.
2030 // Retrieve the bounds of the new shape.
2031 SkRect bounds = perspectiveOutline.getBounds();
2032
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002033 // Transform the bitmap in the new space, taking into
2034 // account the initial transform.
2035 SkMatrix total = origMatrix;
2036 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07002037 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
2038 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2039 total.postScale(dpiScale, dpiScale);
2040
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002041 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002042 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002043 physicalPerspectiveOutline.transform(total);
2044
halcanary7a14b312015-10-01 07:28:13 -07002045 SkRect physicalPerspectiveBounds =
2046 physicalPerspectiveOutline.getBounds();
2047 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2048 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002049
2050 // TODO(edisonn): A better approach would be to use a bitmap shader
2051 // (in clamp mode) and draw a rect over the entire bounding box. Then
2052 // intersect perspectiveOutline to the clip. That will avoid introducing
2053 // alpha to the image while still giving good behavior at the edge of
2054 // the image. Avoiding alpha will reduce the pdf size and generation
2055 // CPU time some.
2056
halcanary7a14b312015-10-01 07:28:13 -07002057 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2058
reede8f30622016-03-23 18:59:25 -07002059 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002060 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002061 return;
2062 }
halcanary7a14b312015-10-01 07:28:13 -07002063 SkCanvas* canvas = surface->getCanvas();
2064 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002065
2066 SkScalar deltaX = bounds.left();
2067 SkScalar deltaY = bounds.top();
2068
2069 SkMatrix offsetMatrix = origMatrix;
2070 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002071 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002072
2073 // Translate the draw in the new canvas, so we perfectly fit the
2074 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002075 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002076 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002077 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002078 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002079
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002080 // In the new space, we use the identity matrix translated
2081 // and scaled to reflect DPI.
2082 matrix.setScale(1 / scaleX, 1 / scaleY);
2083 matrix.postTranslate(deltaX, deltaY);
2084
halcanary7a14b312015-10-01 07:28:13 -07002085 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002086 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002087
halcanarya50151d2016-03-25 11:57:49 -07002088 autoImageUnref = surface->makeImageSnapshot();
2089 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002090 }
2091
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002092 SkMatrix scaled;
2093 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002094 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2095 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002096 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002097 SkIRect subset = imageBitmap.bounds();
2098 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2099 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002100 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002101 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002102 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002103 return;
2104 }
2105 if (content.needShape()) {
2106 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002107 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002108 shape.transform(matrix);
2109 content.setShape(shape);
2110 }
2111 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002112 return;
2113 }
2114
halcanary287d22d2015-09-24 10:20:05 -07002115 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002116 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002117 // draw calls. This code here works for all
2118 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2119 // rasterize a layer on this backend). Fortuanely, this seems
2120 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002121 autoImageUnref = color_filter(imageBitmap, colorFilter);
2122 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002123 // TODO(halcanary): de-dupe this by caching filtered images.
2124 // (maybe in the resource cache?)
2125 }
halcanarya50151d2016-03-25 11:57:49 -07002126
2127 SkBitmapKey key = imageBitmap.getKey();
2128 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002129 if (!pdfimage) {
halcanarya50151d2016-03-25 11:57:49 -07002130 auto img = imageBitmap.makeImage();
2131 if (!img) {
2132 return;
2133 }
2134 pdfimage = SkPDFCreateBitmapObject(
2135 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002136 if (!pdfimage) {
2137 return;
halcanary287d22d2015-09-24 10:20:05 -07002138 }
halcanarya50151d2016-03-25 11:57:49 -07002139 fDocument->serialize(pdfimage); // serialize images early.
2140 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002141 }
halcanarya50151d2016-03-25 11:57:49 -07002142 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002143 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002144 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002145}