blob: 4adc4dae4ab5aca50edb08daa6c7e6a3112c1a74 [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
fmalita632e92f2015-04-22 15:02:03 -0700323#ifdef SK_PDF_USE_PATHOPS_CLIPPING
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000324// Sanity check the numerical values of the SkRegion ops and PathOps ops
325// enums so region_op_to_pathops_op can do a straight passthrough cast.
326// If these are failing, it may be necessary to make region_op_to_pathops_op
327// do more.
bungeman99fe8222015-08-20 07:57:51 -0700328static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
329static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
330static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
331static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
332static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
333 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000334
335static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
336 SkASSERT(op >= 0);
337 SkASSERT(op <= SkRegion::kReverseDifference_Op);
338 return (SkPathOp)op;
339}
340
341/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
342 * Returns true if successful, or false if not successful.
343 * If successful, the resulting clip is stored in outClipPath.
344 * If not successful, outClipPath is undefined, and a fallback method
345 * should be used.
346 */
347static bool get_clip_stack_path(const SkMatrix& transform,
348 const SkClipStack& clipStack,
349 const SkRegion& clipRegion,
350 SkPath* outClipPath) {
351 outClipPath->reset();
352 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
353
354 const SkClipStack::Element* clipEntry;
355 SkClipStack::Iter iter;
356 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
357 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
358 SkPath entryPath;
359 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
360 outClipPath->reset();
361 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
362 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000363 } else {
364 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000365 }
366 entryPath.transform(transform);
367
368 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
369 *outClipPath = entryPath;
370 } else {
371 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
372 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
373 return false;
374 }
375 }
376 }
377
378 if (outClipPath->isInverseFillType()) {
379 // The bounds are slightly outset to ensure this is correct in the
380 // face of floating-point accuracy and possible SkRegion bitmap
381 // approximations.
382 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
383 clipBounds.outset(SK_Scalar1, SK_Scalar1);
384 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
385 return false;
386 }
387 }
388 return true;
389}
390#endif
391
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000392// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000393// graphic state stack, and the fact that we can know all the clips used
394// on the page to optimize this.
395void GraphicStackState::updateClip(const SkClipStack& clipStack,
396 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000397 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000398 if (clipStack == currentEntry()->fClipStack) {
399 return;
400 }
401
402 while (fStackDepth > 0) {
403 pop();
404 if (clipStack == currentEntry()->fClipStack) {
405 return;
406 }
407 }
408 push();
409
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000410 currentEntry()->fClipStack = clipStack;
411 currentEntry()->fClipRegion = clipRegion;
412
413 SkMatrix transform;
414 transform.setTranslate(translation.fX, translation.fY);
415
fmalita632e92f2015-04-22 15:02:03 -0700416#ifdef SK_PDF_USE_PATHOPS_CLIPPING
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000417 SkPath clipPath;
418 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700419 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000420 return;
421 }
422#endif
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000423 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
424 // already been applied. (If this is a top level device, then it specifies
425 // a clip to the content area. If this is a layer, then it specifies
426 // the clip in effect when the layer was created.) There's no need to
427 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
428 // initial clip on the parent layer. (This means there's a bug if the user
429 // expands the clip and then uses any xfer mode that uses dst:
430 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000431 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000432 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
433
434 // If the clip stack does anything other than intersect or if it uses
435 // an inverse fill type, we have to fall back to the clip region.
436 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000437 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000438 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000439 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
440 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000441 needRegion = true;
442 break;
443 }
444 }
445
446 if (needRegion) {
447 SkPath clipPath;
448 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700449 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000450 } else {
451 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000452 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000453 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000454 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
455 switch (clipEntry->getType()) {
456 case SkClipStack::Element::kRect_Type: {
457 SkRect translatedClip;
458 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700459 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000460 break;
461 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000462 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000463 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000464 clipEntry->asPath(&translatedPath);
465 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700466 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000467 break;
468 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000469 }
470 }
471 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000472}
473
474void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
475 if (matrix == currentEntry()->fMatrix) {
476 return;
477 }
478
479 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
480 SkASSERT(fStackDepth > 0);
481 SkASSERT(fEntries[fStackDepth].fClipStack ==
482 fEntries[fStackDepth -1].fClipStack);
483 pop();
484
485 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
486 }
487 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
488 return;
489 }
490
491 push();
492 SkPDFUtils::AppendTransform(matrix, fContentStream);
493 currentEntry()->fMatrix = matrix;
494}
495
halcanary2be7e012016-03-28 11:58:08 -0700496void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000497 // PDF treats a shader as a color, so we only set one or the other.
498 if (state.fShaderIndex >= 0) {
499 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000500 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000501 currentEntry()->fShaderIndex = state.fShaderIndex;
502 }
503 } else {
504 if (state.fColor != currentEntry()->fColor ||
505 currentEntry()->fShaderIndex >= 0) {
506 emit_pdf_color(state.fColor, fContentStream);
507 fContentStream->writeText("RG ");
508 emit_pdf_color(state.fColor, fContentStream);
509 fContentStream->writeText("rg\n");
510 currentEntry()->fColor = state.fColor;
511 currentEntry()->fShaderIndex = -1;
512 }
513 }
514
515 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000516 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000517 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
518 }
519
520 if (state.fTextScaleX) {
521 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
522 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
523 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700524 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000525 fContentStream->writeText(" Tz\n");
526 currentEntry()->fTextScaleX = state.fTextScaleX;
527 }
528 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700529 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
530 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
531 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000532 fContentStream->writeDecAsText(state.fTextFill);
533 fContentStream->writeText(" Tr\n");
534 currentEntry()->fTextFill = state.fTextFill;
535 }
536 }
537}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000538
reed76033be2015-03-14 10:54:31 -0700539static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700540 // PDF does not support image filters, so render them on CPU.
541 // Note that this rendering is done at "screen" resolution (100dpi), not
542 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700543 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800544 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700545
546 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700547 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700548}
549
550SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
halcanary00b7e5e2015-04-15 13:05:18 -0700551 if (cinfo.fForImageFilter ||
552 (layerPaint && not_supported_for_layers(*layerPaint))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700553 return nullptr;
senorblancob0e89dc2014-10-20 14:03:12 -0700554 }
fmalita6987dca2014-11-13 08:33:37 -0800555 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700556 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000557}
558
halcanary989da4a2016-03-21 14:33:17 -0700559SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
560
bsalomon@google.come97f0852011-06-17 13:10:25 +0000561
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000562
563// A helper class to automatically finish a ContentEntry at the end of a
564// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000565class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000566public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000567 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
568 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000569 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700570 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000571 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700572 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000573 init(draw.fClipStack, *draw.fClip, *draw.fMatrix, paint, hasText);
574 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000575 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
576 const SkRegion& clipRegion, const SkMatrix& matrix,
577 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000578 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700579 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000580 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700581 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000582 init(clipStack, clipRegion, matrix, paint, hasText);
583 }
584
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000585 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000586 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000587 SkPath* shape = &fShape;
588 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700589 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000590 }
591 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000592 }
reed@google.comfc641d02012-09-20 17:52:20 +0000593 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000594 }
595
halcanary2be7e012016-03-28 11:58:08 -0700596 SkPDFDevice::ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000597
598 /* Returns true when we explicitly need the shape of the drawing. */
599 bool needShape() {
600 switch (fXfermode) {
601 case SkXfermode::kClear_Mode:
602 case SkXfermode::kSrc_Mode:
603 case SkXfermode::kSrcIn_Mode:
604 case SkXfermode::kSrcOut_Mode:
605 case SkXfermode::kDstIn_Mode:
606 case SkXfermode::kDstOut_Mode:
607 case SkXfermode::kSrcATop_Mode:
608 case SkXfermode::kDstATop_Mode:
609 case SkXfermode::kModulate_Mode:
610 return true;
611 default:
612 return false;
613 }
614 }
615
616 /* Returns true unless we only need the shape of the drawing. */
617 bool needSource() {
618 if (fXfermode == SkXfermode::kClear_Mode) {
619 return false;
620 }
621 return true;
622 }
623
624 /* If the shape is different than the alpha component of the content, then
625 * setShape should be called with the shape. In particular, images and
626 * devices have rectangular shape.
627 */
628 void setShape(const SkPath& shape) {
629 fShape = shape;
630 }
631
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000632private:
633 SkPDFDevice* fDevice;
halcanary2be7e012016-03-28 11:58:08 -0700634 SkPDFDevice::ContentEntry* fContentEntry;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000635 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000636 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000637 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000638
639 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
640 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000641 // Shape has to be flatten before we get here.
642 if (matrix.hasPerspective()) {
643 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000644 return;
645 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000646 if (paint.getXfermode()) {
647 paint.getXfermode()->asMode(&fXfermode);
648 }
649 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
650 matrix, paint, hasText,
651 &fDstFormXObject);
652 }
653};
654
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000655////////////////////////////////////////////////////////////////////////////////
656
halcanary989da4a2016-03-21 14:33:17 -0700657SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
robertphillips9a53fd72015-06-22 09:46:59 -0700658 : INHERITED(SkSurfaceProps(0, kUnknown_SkPixelGeometry))
659 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800660 , fContentSize(pageSize)
661 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanary96fcdcc2015-08-27 07:41:13 -0700662 , fClipStack(nullptr)
halcanary385fe4d2015-08-26 13:07:48 -0700663 , fFontGlyphUsage(new SkPDFGlyphSetMap)
halcanarya1f1ee92015-02-20 06:17:26 -0800664 , fRasterDpi(rasterDpi)
halcanary989da4a2016-03-21 14:33:17 -0700665 , fDocument(doc) {
halcanarya1f1ee92015-02-20 06:17:26 -0800666 SkASSERT(pageSize.width() > 0);
667 SkASSERT(pageSize.height() > 0);
668 fLegacyBitmap.setInfo(
669 SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()));
670 if (flip) {
671 // Skia generally uses the top left as the origin but PDF
672 // natively has the origin at the bottom left. This matrix
673 // corrects for that. But that only needs to be done once, we
674 // don't do it when layering.
675 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
676 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
677 } else {
678 fInitialTransform.setIdentity();
679 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000680}
681
682SkPDFDevice::~SkPDFDevice() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000683 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000684}
685
686void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700687 fContentEntries.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700688 if (fFontGlyphUsage.get() == nullptr) {
halcanary385fe4d2015-08-26 13:07:48 -0700689 fFontGlyphUsage.reset(new SkPDFGlyphSetMap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000690 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000691}
692
vandebo@chromium.org98594282011-07-25 22:34:12 +0000693void SkPDFDevice::cleanUp(bool clearFontUsage) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000694 fGraphicStateResources.unrefAll();
695 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000696 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000697 fShaderResources.unrefAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000698
vandebo@chromium.org98594282011-07-25 22:34:12 +0000699 if (clearFontUsage) {
700 fFontGlyphUsage->reset();
701 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000702}
703
reedf70b5312016-03-04 16:36:20 -0800704void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
705 SkData* value) {
706 if (0 == rect.width() && 0 == rect.height()) {
707 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
708 } else {
709 SkPath path;
710 path.addRect(rect);
711 handlePathAnnotation(path, d, key, value);
712 }
713}
714
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000715void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000716 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700717 replace_srcmode_on_opaque_paint(&newPaint);
718
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000719 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000720 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000721 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000722}
723
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000724void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
halcanary2be7e012016-03-28 11:58:08 -0700725 SkPDFDevice::ContentEntry* contentEntry) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000726 if (!contentEntry) {
727 return;
728 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000729 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
730 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000731 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000732 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000733 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000734 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000735 inverse.mapRect(&bbox);
736
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000737 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000738 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000739 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000740}
741
halcanary682ee012016-01-28 10:59:34 -0800742void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700743 SkCanvas::PointMode mode,
744 size_t count,
745 const SkPoint* points,
746 const SkPaint& srcPaint) {
747 SkPaint passedPaint = srcPaint;
748 replace_srcmode_on_opaque_paint(&passedPaint);
749
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000750 if (count == 0) {
751 return;
752 }
753
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000754 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
755 // We only use this when there's a path effect because of the overhead
756 // of multiple calls to setUpContentEntry it causes.
757 if (passedPaint.getPathEffect()) {
758 if (d.fClip->isEmpty()) {
759 return;
760 }
761 SkDraw pointDraw(d);
762 pointDraw.fDevice = this;
763 pointDraw.drawPoints(mode, count, points, passedPaint, true);
764 return;
765 }
766
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000767 const SkPaint* paint = &passedPaint;
768 SkPaint modifiedPaint;
769
770 if (mode == SkCanvas::kPoints_PointMode &&
771 paint->getStrokeCap() != SkPaint::kRound_Cap) {
772 modifiedPaint = *paint;
773 paint = &modifiedPaint;
774 if (paint->getStrokeWidth()) {
775 // PDF won't draw a single point with square/butt caps because the
776 // orientation is ambiguous. Draw a rectangle instead.
777 modifiedPaint.setStyle(SkPaint::kFill_Style);
778 SkScalar strokeWidth = paint->getStrokeWidth();
779 SkScalar halfStroke = SkScalarHalf(strokeWidth);
780 for (size_t i = 0; i < count; i++) {
781 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
782 r.inset(-halfStroke, -halfStroke);
783 drawRect(d, r, modifiedPaint);
784 }
785 return;
786 } else {
787 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
788 }
789 }
790
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000791 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000792 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000793 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000794 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000795
796 switch (mode) {
797 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000798 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000799 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000800 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000801 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000802 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000803 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000804 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000805 break;
806 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000807 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000808 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000809 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000810 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000811 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000812 &content.entry()->fContent);
813 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000814 }
815 break;
816 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000817 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
818 for (size_t i = 0; i < count; i++) {
819 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000820 &content.entry()->fContent);
821 SkPDFUtils::ClosePath(&content.entry()->fContent);
822 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000823 }
824 break;
825 default:
826 SkASSERT(false);
827 }
828}
829
halcanary8103a342016-03-08 15:10:16 -0800830static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800831 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700832 annotation->insertName("Subtype", "Link");
833
halcanaryece83922016-03-08 08:32:12 -0800834 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700835 border->reserve(3);
836 border->appendInt(0); // Horizontal corner radius.
837 border->appendInt(0); // Vertical corner radius.
838 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800839 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700840
halcanaryece83922016-03-08 08:32:12 -0800841 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700842 rect->reserve(4);
843 rect->appendScalar(translatedRect.fLeft);
844 rect->appendScalar(translatedRect.fTop);
845 rect->appendScalar(translatedRect.fRight);
846 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800847 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700848
halcanary8103a342016-03-08 15:10:16 -0800849 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700850}
851
halcanary8103a342016-03-08 15:10:16 -0800852static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
853 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700854 SkString url(static_cast<const char *>(urlData->data()),
855 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800856 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700857 action->insertName("S", "URI");
858 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800859 annotation->insertObject("A", std::move(action));
860 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700861}
862
halcanary8103a342016-03-08 15:10:16 -0800863static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
864 const SkRect& r) {
865 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700866 SkString name(static_cast<const char *>(nameData->data()),
867 nameData->size() - 1);
868 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800869 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700870}
871
halcanary682ee012016-01-28 10:59:34 -0800872void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700873 const SkRect& rect,
874 const SkPaint& srcPaint) {
875 SkPaint paint = srcPaint;
876 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000877 SkRect r = rect;
878 r.sort();
879
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000880 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000881 if (d.fClip->isEmpty()) {
882 return;
883 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000884 SkPath path;
885 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700886 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000887 return;
888 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000889
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000890 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000891 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000892 return;
893 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000894 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000895 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000896 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000897}
898
halcanarya6814332015-05-27 08:53:36 -0700899void SkPDFDevice::drawRRect(const SkDraw& draw,
900 const SkRRect& rrect,
901 const SkPaint& srcPaint) {
902 SkPaint paint = srcPaint;
903 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000904 SkPath path;
905 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700906 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000907}
908
halcanarya6814332015-05-27 08:53:36 -0700909void SkPDFDevice::drawOval(const SkDraw& draw,
910 const SkRect& oval,
911 const SkPaint& srcPaint) {
912 SkPaint paint = srcPaint;
913 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700914 SkPath path;
915 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700916 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700917}
918
halcanary682ee012016-01-28 10:59:34 -0800919void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700920 const SkPath& origPath,
921 const SkPaint& srcPaint,
922 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000923 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700924 SkPaint paint = srcPaint;
925 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800926 SkPath modifiedPath;
927 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000928
929 SkMatrix matrix = *d.fMatrix;
930 if (prePathMatrix) {
931 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800932 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000933 pathPtr = &modifiedPath;
934 pathIsMutable = true;
935 }
halcanary682ee012016-01-28 10:59:34 -0800936 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000937 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000938 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000939 }
940 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000941
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000942 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000943 if (d.fClip->isEmpty()) {
944 return;
945 }
halcanary682ee012016-01-28 10:59:34 -0800946 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000947 pathPtr = &modifiedPath;
948 pathIsMutable = true;
949 }
halcanary682ee012016-01-28 10:59:34 -0800950 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000951
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000952 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700953 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000954 if (fill) {
955 noEffectPaint.setStyle(SkPaint::kFill_Style);
956 } else {
957 noEffectPaint.setStyle(SkPaint::kStroke_Style);
958 noEffectPaint.setStrokeWidth(0);
959 }
halcanary96fcdcc2015-08-27 07:41:13 -0700960 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000961 return;
962 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000963
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000964 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000965 return;
966 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000967
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000968 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000969 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000970 return;
971 }
halcanary8b2bc252015-10-06 09:41:47 -0700972 bool consumeDegeratePathSegments =
973 paint.getStyle() == SkPaint::kFill_Style ||
974 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
975 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000976 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -0700977 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000978 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000979 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000980 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000981}
982
halcanary7a14b312015-10-01 07:28:13 -0700983void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
984 const SkBitmap& bitmap,
985 const SkRect* src,
986 const SkRect& dst,
987 const SkPaint& srcPaint,
988 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700989 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -0700990}
991
992void SkPDFDevice::drawBitmap(const SkDraw& d,
993 const SkBitmap& bitmap,
994 const SkMatrix& matrix,
995 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -0700996 SkPaint paint = srcPaint;
997 if (bitmap.isOpaque()) {
998 replace_srcmode_on_opaque_paint(&paint);
999 }
1000
halcanary7a14b312015-10-01 07:28:13 -07001001 if (d.fClip->isEmpty()) {
1002 return;
1003 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001004
halcanary7a14b312015-10-01 07:28:13 -07001005 SkMatrix transform = matrix;
1006 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001007 SkImageBitmap imageBitmap(bitmap);
1008 this->internalDrawImage(
1009 transform, d.fClipStack, *d.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001010}
1011
1012void SkPDFDevice::drawSprite(const SkDraw& d,
1013 const SkBitmap& bitmap,
1014 int x,
1015 int y,
1016 const SkPaint& srcPaint) {
1017 SkPaint paint = srcPaint;
1018 if (bitmap.isOpaque()) {
1019 replace_srcmode_on_opaque_paint(&paint);
1020 }
1021
1022 if (d.fClip->isEmpty()) {
1023 return;
1024 }
1025
1026 SkMatrix matrix;
1027 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -07001028 SkImageBitmap imageBitmap(bitmap);
1029 this->internalDrawImage(
1030 matrix, d.fClipStack, *d.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001031}
1032
1033void SkPDFDevice::drawImage(const SkDraw& draw,
1034 const SkImage* image,
1035 SkScalar x,
1036 SkScalar y,
1037 const SkPaint& srcPaint) {
1038 SkPaint paint = srcPaint;
1039 if (!image) {
1040 return;
1041 }
1042 if (image->isOpaque()) {
1043 replace_srcmode_on_opaque_paint(&paint);
1044 }
1045 if (draw.fClip->isEmpty()) {
1046 return;
1047 }
1048 SkMatrix transform = SkMatrix::MakeTrans(x, y);
1049 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001050 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
1051 this->internalDrawImage(
1052 transform, draw.fClipStack, *draw.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001053}
1054
1055void SkPDFDevice::drawImageRect(const SkDraw& draw,
1056 const SkImage* image,
1057 const SkRect* src,
1058 const SkRect& dst,
1059 const SkPaint& srcPaint,
1060 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001061 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001062}
1063
halcanarybb264b72015-04-07 10:40:03 -07001064// Create a PDF string. Maximum length (in bytes) is 65,535.
1065// @param input A string value.
1066// @param len The length of the input array.
1067// @param wideChars True iff the upper byte in each uint16_t is
1068// significant and should be encoded and not
1069// discarded. If true, the upper byte is encoded
1070// first. Otherwise, we assert the upper byte is
1071// zero.
1072static SkString format_wide_string(const uint16_t* input,
1073 size_t len,
1074 bool wideChars) {
1075 if (wideChars) {
1076 SkASSERT(2 * len < 65535);
1077 static const char gHex[] = "0123456789ABCDEF";
1078 SkString result(4 * len + 2);
1079 result[0] = '<';
1080 for (size_t i = 0; i < len; i++) {
1081 result[4 * i + 1] = gHex[(input[i] >> 12) & 0xF];
1082 result[4 * i + 2] = gHex[(input[i] >> 8) & 0xF];
1083 result[4 * i + 3] = gHex[(input[i] >> 4) & 0xF];
1084 result[4 * i + 4] = gHex[(input[i] ) & 0xF];
1085 }
1086 result[4 * len + 1] = '>';
1087 return result;
1088 } else {
1089 SkASSERT(len <= 65535);
1090 SkString tmp(len);
1091 for (size_t i = 0; i < len; i++) {
1092 SkASSERT(0 == input[i] >> 8);
1093 tmp[i] = static_cast<uint8_t>(input[i]);
1094 }
halcanarybc4696b2015-05-06 10:56:04 -07001095 return SkPDFUtils::FormatString(tmp.c_str(), tmp.size());
halcanarybb264b72015-04-07 10:40:03 -07001096 }
1097}
1098
halcanary66a82f32015-10-12 13:05:04 -07001099static void draw_transparent_text(SkPDFDevice* device,
1100 const SkDraw& d,
1101 const void* text, size_t len,
1102 SkScalar x, SkScalar y,
1103 const SkPaint& srcPaint) {
1104
1105 SkPaint transparent;
1106 if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(),
1107 device->getCanon())) {
1108 SkDEBUGFAIL("default typeface should be embeddable");
1109 return; // Avoid infinite loop in release.
1110 }
1111 transparent.setTextSize(srcPaint.getTextSize());
1112 transparent.setColor(SK_ColorTRANSPARENT);
1113 switch (srcPaint.getTextEncoding()) {
1114 case SkPaint::kGlyphID_TextEncoding: {
1115 // Since a glyphId<->Unicode mapping is typeface-specific,
1116 // map back to Unicode first.
1117 size_t glyphCount = len / 2;
1118 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1119 srcPaint.glyphsToUnichars(
1120 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1121 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
1122 device->drawText(d, &unichars[0],
1123 glyphCount * sizeof(SkUnichar),
1124 x, y, transparent);
1125 break;
1126 }
1127 case SkPaint::kUTF8_TextEncoding:
1128 case SkPaint::kUTF16_TextEncoding:
1129 case SkPaint::kUTF32_TextEncoding:
1130 transparent.setTextEncoding(srcPaint.getTextEncoding());
1131 device->drawText(d, text, len, x, y, transparent);
1132 break;
1133 default:
1134 SkFAIL("unknown text encoding");
1135 }
1136}
1137
1138
halcanary682ee012016-01-28 10:59:34 -08001139void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
halcanarya6814332015-05-27 08:53:36 -07001140 SkScalar x, SkScalar y, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001141 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary6950de62015-11-07 05:29:00 -08001142 // https://bug.skia.org/3866
halcanary66a82f32015-10-12 13:05:04 -07001143 SkPath path;
1144 srcPaint.getTextPath(text, len, x, y, &path);
1145 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1146 // Draw text transparently to make it copyable/searchable/accessable.
1147 draw_transparent_text(this, d, text, len, x, y, srcPaint);
1148 return;
1149 }
halcanarya6814332015-05-27 08:53:36 -07001150 SkPaint paint = srcPaint;
1151 replace_srcmode_on_opaque_paint(&paint);
1152
halcanary96fcdcc2015-08-27 07:41:13 -07001153 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1154 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001155 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1156 // making text unreadable (e.g. same text twice when using CSS shadows).
1157 return;
1158 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001159 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001160 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001161 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001162 return;
1163 }
1164
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001165 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001166 const uint16_t* glyphIDs = nullptr;
reed@google.comaec40662014-04-18 19:29:07 +00001167 int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001168 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001169
benjaminwagnerd936f632016-02-23 10:44:31 -08001170 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001171 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001172 content.entry()->fContent.writeText("BT\n");
1173 set_text_transform(x, y, textPaint.getTextSkewX(),
1174 &content.entry()->fContent);
reed@google.comaec40662014-04-18 19:29:07 +00001175 int consumedGlyphCount = 0;
halcanary2f912f32014-10-16 09:53:20 -07001176
1177 SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs);
1178
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001179 while (numGlyphs > consumedGlyphCount) {
robertphillips8e0c1502015-07-07 10:28:43 -07001180 this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001181 SkPDFFont* font = content.entry()->fState.fFont;
halcanary2f912f32014-10-16 09:53:20 -07001182
1183 int availableGlyphs = font->glyphsToPDFFontEncoding(
1184 glyphIDsCopy.begin() + consumedGlyphCount,
1185 numGlyphs - consumedGlyphCount);
1186 fFontGlyphUsage->noteGlyphUsage(
1187 font, glyphIDsCopy.begin() + consumedGlyphCount,
1188 availableGlyphs);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001189 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001190 format_wide_string(glyphIDsCopy.begin() + consumedGlyphCount,
1191 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001192 content.entry()->fContent.writeText(encodedString.c_str());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001193 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001194 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001195 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001196 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001197}
1198
halcanary682ee012016-01-28 10:59:34 -08001199void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -07001200 const SkScalar pos[], int scalarsPerPos,
halcanary682ee012016-01-28 10:59:34 -08001201 const SkPoint& offset, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001202 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary66a82f32015-10-12 13:05:04 -07001203 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos);
1204 SkAutoTMalloc<SkPoint> positionsBuffer;
1205 if (2 != scalarsPerPos) {
1206 int glyphCount = srcPaint.textToGlyphs(text, len, NULL);
1207 positionsBuffer.reset(glyphCount);
1208 for (int i = 0; i < glyphCount; ++i) {
1209 positionsBuffer[i].set(pos[i], 0.0f);
1210 }
1211 positions = &positionsBuffer[0];
1212 }
1213 SkPath path;
1214 srcPaint.getPosTextPath(text, len, positions, &path);
1215 SkMatrix matrix;
1216 matrix.setTranslate(offset);
1217 this->drawPath(d, path, srcPaint, &matrix, true);
1218 // Draw text transparently to make it copyable/searchable/accessable.
1219 draw_transparent_text(
1220 this, d, text, len, offset.x() + positions[0].x(),
1221 offset.y() + positions[0].y(), srcPaint);
1222 return;
1223 }
1224
halcanarya6814332015-05-27 08:53:36 -07001225 SkPaint paint = srcPaint;
1226 replace_srcmode_on_opaque_paint(&paint);
1227
halcanary96fcdcc2015-08-27 07:41:13 -07001228 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1229 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001230 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1231 // making text unreadable (e.g. same text twice when using CSS shadows).
1232 return;
1233 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001234 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001235 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001236 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001237 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001238 return;
1239 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001240
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001241 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001242 const uint16_t* glyphIDs = nullptr;
bungeman22edc832014-10-03 07:55:58 -07001243 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001244 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001245
benjaminwagnerd936f632016-02-23 10:44:31 -08001246 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001247 content.entry()->fContent.writeText("BT\n");
robertphillips8e0c1502015-07-07 10:28:43 -07001248 this->updateFont(textPaint, glyphIDs[0], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001249 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001250 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001251 uint16_t encodedValue = glyphIDs[i];
1252 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
bungeman22edc832014-10-03 07:55:58 -07001253 // The current pdf font cannot encode the current glyph.
1254 // Try to get a pdf font which can encode the current glyph.
robertphillips8e0c1502015-07-07 10:28:43 -07001255 this->updateFont(textPaint, glyphIDs[i], content.entry());
bungeman22edc832014-10-03 07:55:58 -07001256 font = content.entry()->fState.fFont;
1257 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
1258 SkDEBUGFAIL("PDF could not encode glyph.");
1259 continue;
1260 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001261 }
bungeman22edc832014-10-03 07:55:58 -07001262
vandebo@chromium.org98594282011-07-25 22:34:12 +00001263 fFontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
fmalita05c4a432014-09-29 06:29:53 -07001264 SkScalar x = offset.x() + pos[i * scalarsPerPos];
1265 SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0);
1266
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001267 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
bungeman22edc832014-10-03 07:55:58 -07001268 set_text_transform(x, y, textPaint.getTextSkewX(), &content.entry()->fContent);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001269 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001270 format_wide_string(&encodedValue, 1, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001271 content.entry()->fContent.writeText(encodedString.c_str());
1272 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001273 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001274 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001275}
1276
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001277void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001278 int vertexCount, const SkPoint verts[],
1279 const SkPoint texs[], const SkColor colors[],
1280 SkXfermode* xmode, const uint16_t indices[],
1281 int indexCount, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001282 if (d.fClip->isEmpty()) {
1283 return;
1284 }
reed@google.com85e143c2013-12-30 15:51:25 +00001285 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001286}
1287
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001288void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1289 int x, int y, const SkPaint& paint) {
fmalita6987dca2014-11-13 08:33:37 -08001290 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001291 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001292
1293 SkScalar scalarX = SkIntToScalar(x);
1294 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001295 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1296 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001297 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001298 }
halcanary91fcb3e2016-03-04 13:53:22 -08001299 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1300 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001301 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001302 }
halcanary91fcb3e2016-03-04 13:53:22 -08001303 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1304 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001305 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001306 }
1307
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001308 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001309 return;
1310 }
1311
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001312 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001313 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001314 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001315 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001316 return;
1317 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001318 if (content.needShape()) {
1319 SkPath shape;
1320 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001321 SkIntToScalar(device->width()),
1322 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001323 content.setShape(shape);
1324 }
1325 if (!content.needSource()) {
1326 return;
1327 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001328
halcanaryece83922016-03-08 08:32:12 -08001329 auto xObject = sk_make_sp<SkPDFFormXObject>(pdfDevice);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001330 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001331 &content.entry()->fContent);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001332
1333 // Merge glyph sets from the drawn device.
1334 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001335}
1336
reed89443ab2014-06-27 11:34:19 -07001337SkImageInfo SkPDFDevice::imageInfo() const {
1338 return fLegacyBitmap.info();
1339}
1340
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001341void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1342 INHERITED::onAttachToCanvas(canvas);
1343
1344 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1345 fClipStack = canvas->getClipStack();
1346}
1347
1348void SkPDFDevice::onDetachFromCanvas() {
1349 INHERITED::onDetachFromCanvas();
1350
halcanary96fcdcc2015-08-27 07:41:13 -07001351 fClipStack = nullptr;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001352}
1353
reede8f30622016-03-23 18:59:25 -07001354sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1355 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001356}
1357
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001358
halcanary8103a342016-03-08 15:10:16 -08001359sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001360 SkTDArray<SkPDFObject*> fonts;
1361 fonts.setReserve(fFontResources.count());
1362 for (SkPDFFont* font : fFontResources) {
1363 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001364 }
halcanary8103a342016-03-08 15:10:16 -08001365 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001366 &fGraphicStateResources,
1367 &fShaderResources,
1368 &fXObjectResources,
1369 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001370}
1371
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001372const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1373 return fFontResources;
1374}
1375
halcanary8103a342016-03-08 15:10:16 -08001376sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001377 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001378 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001379 mediaBox->appendInt(0);
1380 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001381 mediaBox->appendInt(fPageSize.width());
1382 mediaBox->appendInt(fPageSize.height());
1383 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001384}
1385
mtklein5f939ab2016-03-16 10:28:35 -07001386std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001387 SkDynamicMemoryWStream buffer;
1388 this->writeContent(&buffer);
mtklein5f939ab2016-03-16 10:28:35 -07001389 return std::unique_ptr<SkStreamAsset>(
halcanary8103a342016-03-08 15:10:16 -08001390 buffer.bytesWritten() > 0
1391 ? buffer.detachAsStream()
1392 : new SkMemoryStream);
reed@google.com5667afc2011-06-27 14:42:15 +00001393}
1394
halcanary334fcbc2015-02-24 12:56:16 -08001395void SkPDFDevice::writeContent(SkWStream* out) const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001396 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanary334fcbc2015-02-24 12:56:16 -08001397 SkPDFUtils::AppendTransform(fInitialTransform, out);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001398 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001399
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001400 // If the content area is the entire page, then we don't need to clip
1401 // the content area (PDF area clips to the page size). Otherwise,
1402 // we have to clip to the content area; we've already applied the
1403 // initial transform, so just clip to the device size.
1404 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001405 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1406 SkIntToScalar(this->height()));
halcanary96fcdcc2015-08-27 07:41:13 -07001407 emit_clip(nullptr, &r, out);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001408 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001409
halcanary2be7e012016-03-28 11:58:08 -07001410 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, out);
1411 for (const auto& entry : fContentEntries) {
1412 SkPoint translation;
1413 translation.iset(this->getOrigin());
1414 translation.negate();
1415 gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion,
1416 translation);
1417 gsState.updateMatrix(entry.fState.fMatrix);
1418 gsState.updateDrawingState(entry.fState);
1419
1420 entry.fContent.writeToStream(out);
1421 }
1422 gsState.drainStack();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001423}
1424
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001425/* Draws an inverse filled path by using Path Ops to compute the positive
1426 * inverse using the current clip as the inverse bounds.
1427 * Return true if this was an inverse path and was properly handled,
1428 * otherwise returns false and the normal drawing routine should continue,
1429 * either as a (incorrect) fallback or because the path was not inverse
1430 * in the first place.
1431 */
1432bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001433 const SkPaint& paint, bool pathIsMutable,
1434 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001435 if (!origPath.isInverseFillType()) {
1436 return false;
1437 }
1438
1439 if (d.fClip->isEmpty()) {
1440 return false;
1441 }
1442
1443 SkPath modifiedPath;
1444 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1445 SkPaint noInversePaint(paint);
1446
1447 // Merge stroking operations into final path.
1448 if (SkPaint::kStroke_Style == paint.getStyle() ||
1449 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1450 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1451 if (doFillPath) {
1452 noInversePaint.setStyle(SkPaint::kFill_Style);
1453 noInversePaint.setStrokeWidth(0);
1454 pathPtr = &modifiedPath;
1455 } else {
1456 // To be consistent with the raster output, hairline strokes
1457 // are rendered as non-inverted.
1458 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001459 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001460 return true;
1461 }
1462 }
1463
1464 // Get bounds of clip in current transform space
1465 // (clip bounds are given in device space).
1466 SkRect bounds;
1467 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001468 SkMatrix totalMatrix = *d.fMatrix;
1469 if (prePathMatrix) {
1470 totalMatrix.preConcat(*prePathMatrix);
1471 }
1472 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001473 return false;
1474 }
1475 bounds.set(d.fClip->getBounds());
1476 transformInverse.mapRect(&bounds);
1477
1478 // Extend the bounds by the line width (plus some padding)
1479 // so the edge doesn't cause a visible stroke.
1480 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1481 paint.getStrokeWidth() + SK_Scalar1);
1482
1483 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1484 return false;
1485 }
1486
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001487 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001488 return true;
1489}
1490
reedf70b5312016-03-04 16:36:20 -08001491void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001492 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001493 const char key[], SkData* value) {
1494 if (!value) {
1495 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001496 }
reedf70b5312016-03-04 16:36:20 -08001497
1498 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1499 SkPoint transformedPoint;
1500 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1501 fNamedDestinations.emplace_back(value, transformedPoint);
1502 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001503}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001504
reedf70b5312016-03-04 16:36:20 -08001505void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001506 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001507 const char key[], SkData* value) {
1508 if (!value) {
1509 return;
1510 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001511
1512 SkPath transformedPath = path;
1513 transformedPath.transform(*d.fMatrix);
1514 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001515 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1516 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001517 SkRect transformedRect = SkRect::Make(clip.getBounds());
1518
reedf70b5312016-03-04 16:36:20 -08001519 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001520 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001521 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001522 }
reedf70b5312016-03-04 16:36:20 -08001523 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001524 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001525 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001526 }
reed16108352016-03-03 09:14:36 -08001527 }
halcanary438de492015-04-28 06:21:01 -07001528}
1529
wangxianzhuef6c50a2015-09-17 20:38:02 -07001530void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1531 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001532 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001533 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001534 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001535 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001536 }
halcanary91fcb3e2016-03-04 13:53:22 -08001537 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001538 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001539 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001540 array->appendObject(
1541 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001542 }
1543}
epoger@google.comb58772f2013-03-08 09:09:10 +00001544
halcanary6d622702015-03-25 08:45:42 -07001545void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001546 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001547 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001548 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001549 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001550 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001551 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001552 pdfDest->appendScalar(p.x());
1553 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001554 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001555 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001556 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001557 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001558}
1559
reed@google.comfc641d02012-09-20 17:52:20 +00001560SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
halcanary385fe4d2015-08-26 13:07:48 -07001561 SkPDFFormXObject* xobject = new SkPDFFormXObject(this);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001562 // We always draw the form xobjects that we create back into the device, so
1563 // we simply preserve the font usage instead of pulling it out and merging
1564 // it back in later.
1565 cleanUp(false); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001566 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001567 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001568}
1569
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001570void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
1571 SkPDFFormXObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001572 const SkClipStack* clipStack,
1573 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001574 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001575 bool invertClip) {
1576 if (clipRegion.isEmpty() && !invertClip) {
1577 return;
1578 }
1579
halcanary1437c1e2016-03-13 18:30:24 -07001580 auto sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanary989da4a2016-03-21 14:33:17 -07001581 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001582
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001583 SkMatrix identity;
1584 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001585 SkPaint paint;
1586 paint.setXfermodeMode(mode);
1587 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001588 if (!content.entry()) {
1589 return;
1590 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001591 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001592 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001593 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001594
halcanary1437c1e2016-03-13 18:30:24 -07001595 // Call makeNoSmaskGraphicState() instead of
1596 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1597 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001598 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001599 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001600 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001601}
1602
halcanary2be7e012016-03-28 11:58:08 -07001603SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001604 const SkRegion& clipRegion,
1605 const SkMatrix& matrix,
1606 const SkPaint& paint,
1607 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001608 SkPDFFormXObject** dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001609 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001610 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001611 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001612 }
1613
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001614 // The clip stack can come from an SkDraw where it is technically optional.
1615 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001616 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001617 if (clipRegion == fExistingClipRegion) {
1618 clipStack = &fExistingClipStack;
1619 } else {
1620 // GraphicStackState::updateClip expects the clip stack to have
1621 // fExistingClip as a prefix, so start there, then set the clip
1622 // to the passed region.
1623 synthesizedClipStack = fExistingClipStack;
1624 SkPath clipPath;
1625 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001626 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1627 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001628 clipStack = &synthesizedClipStack;
1629 }
1630 }
1631
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001632 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1633 if (paint.getXfermode()) {
1634 paint.getXfermode()->asMode(&xfermode);
1635 }
1636
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001637 // For the following modes, we want to handle source and destination
1638 // separately, so make an object of what's already there.
1639 if (xfermode == SkXfermode::kClear_Mode ||
1640 xfermode == SkXfermode::kSrc_Mode ||
1641 xfermode == SkXfermode::kSrcIn_Mode ||
1642 xfermode == SkXfermode::kDstIn_Mode ||
1643 xfermode == SkXfermode::kSrcOut_Mode ||
1644 xfermode == SkXfermode::kDstOut_Mode ||
1645 xfermode == SkXfermode::kSrcATop_Mode ||
1646 xfermode == SkXfermode::kDstATop_Mode ||
1647 xfermode == SkXfermode::kModulate_Mode) {
1648 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001649 *dst = createFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001650 SkASSERT(isContentEmpty());
1651 } else if (xfermode != SkXfermode::kSrc_Mode &&
1652 xfermode != SkXfermode::kSrcOut_Mode) {
1653 // Except for Src and SrcOut, if there isn't anything already there,
1654 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001655 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001656 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001657 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001658 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001659 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001660
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001661 // Dst xfer mode doesn't draw source at all.
1662 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001663 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001664 }
1665
halcanary2be7e012016-03-28 11:58:08 -07001666 SkPDFDevice::ContentEntry* entry;
1667 if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) {
1668 entry = fContentEntries.back();
1669 } else if (xfermode != SkXfermode::kDstOver_Mode) {
1670 entry = fContentEntries.emplace_back();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001671 } else {
halcanary2be7e012016-03-28 11:58:08 -07001672 entry = fContentEntries.emplace_front();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001673 }
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001674 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001675 hasText, &entry->fState);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001676 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001677}
1678
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001679void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001680 SkPDFFormXObject* dst,
1681 SkPath* shape) {
1682 if (xfermode != SkXfermode::kClear_Mode &&
1683 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001684 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001685 xfermode != SkXfermode::kSrcIn_Mode &&
1686 xfermode != SkXfermode::kDstIn_Mode &&
1687 xfermode != SkXfermode::kSrcOut_Mode &&
1688 xfermode != SkXfermode::kDstOut_Mode &&
1689 xfermode != SkXfermode::kSrcATop_Mode &&
1690 xfermode != SkXfermode::kDstATop_Mode &&
1691 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001692 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001693 return;
1694 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001695 if (xfermode == SkXfermode::kDstOver_Mode) {
1696 SkASSERT(!dst);
halcanary2be7e012016-03-28 11:58:08 -07001697 if (fContentEntries.front()->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001698 // For DstOver, an empty content entry was inserted before the rest
1699 // of the content entries. If nothing was drawn, it needs to be
1700 // removed.
halcanary2be7e012016-03-28 11:58:08 -07001701 fContentEntries.pop_front();
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001702 }
1703 return;
1704 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001705 if (!dst) {
1706 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1707 xfermode == SkXfermode::kSrcOut_Mode);
1708 return;
1709 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001710
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001711 SkASSERT(dst);
halcanary2be7e012016-03-28 11:58:08 -07001712 SkASSERT(fContentEntries.count() == 1);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001713 // Changing the current content into a form-xobject will destroy the clip
1714 // objects which is fine since the xobject will already be clipped. However
1715 // if source has shape, we need to clip it too, so a copy of the clip is
1716 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001717
1718 SkClipStack clipStack = fContentEntries.front()->fState.fClipStack;
1719 SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001720
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001721 SkMatrix identity;
1722 identity.reset();
1723 SkPaint stockPaint;
1724
halcanary48810a02016-03-07 14:57:50 -08001725 sk_sp<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001726 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001727 // If nothing was drawn and there's no shape, then the draw was a
1728 // no-op, but dst needs to be restored for that to be true.
1729 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1730 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1731 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001732 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001733 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001734 ScopedContentEntry content(this, &fExistingClipStack,
1735 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001736 stockPaint);
1737 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1738 &content.entry()->fContent);
1739 return;
1740 } else {
1741 xfermode = SkXfermode::kClear_Mode;
1742 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001743 } else {
halcanary2be7e012016-03-28 11:58:08 -07001744 SkASSERT(fContentEntries.count() == 1);
reed@google.comfc641d02012-09-20 17:52:20 +00001745 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001746 }
1747
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001748 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1749 // without alpha.
1750 if (xfermode == SkXfermode::kSrcATop_Mode) {
1751 // TODO(vandebo): In order to properly support SrcATop we have to track
1752 // the shape of what's been drawn at all times. It's the intersection of
1753 // the non-transparent parts of the device and the outlines (shape) of
1754 // all images and devices drawn.
1755 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001756 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001757 SkXfermode::kSrcOver_Mode, true);
1758 } else {
halcanary48810a02016-03-07 14:57:50 -08001759 sk_sp<SkPDFFormXObject> dstMaskStorage;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001760 SkPDFFormXObject* dstMask = srcFormXObject.get();
halcanary96fcdcc2015-08-27 07:41:13 -07001761 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001762 // Draw shape into a form-xobject.
1763 SkDraw d;
1764 d.fMatrix = &identity;
1765 d.fClip = &clipRegion;
1766 d.fClipStack = &clipStack;
1767 SkPaint filledPaint;
1768 filledPaint.setColor(SK_ColorBLACK);
1769 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001770 this->drawPath(d, *shape, filledPaint, nullptr, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001771
1772 dstMaskStorage.reset(createFormXObjectFromDevice());
1773 dstMask = dstMaskStorage.get();
1774 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001775 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1776 &fExistingClipStack, fExistingClipRegion,
1777 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001778 }
1779
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001780 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001781 return;
1782 } else if (xfermode == SkXfermode::kSrc_Mode ||
1783 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001784 ScopedContentEntry content(this, &fExistingClipStack,
1785 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001786 if (content.entry()) {
1787 SkPDFUtils::DrawFormXObject(
1788 this->addXObjectResource(srcFormXObject.get()),
1789 &content.entry()->fContent);
1790 }
1791 if (xfermode == SkXfermode::kSrc_Mode) {
1792 return;
1793 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001794 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001795 ScopedContentEntry content(this, &fExistingClipStack,
1796 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001797 if (content.entry()) {
1798 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1799 &content.entry()->fContent);
1800 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001801 }
1802
1803 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1804 xfermode == SkXfermode::kDstIn_Mode ||
1805 xfermode == SkXfermode::kSrcOut_Mode ||
1806 xfermode == SkXfermode::kDstOut_Mode ||
1807 xfermode == SkXfermode::kSrcATop_Mode ||
1808 xfermode == SkXfermode::kDstATop_Mode ||
1809 xfermode == SkXfermode::kModulate_Mode);
1810
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001811 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001812 xfermode == SkXfermode::kSrcOut_Mode ||
1813 xfermode == SkXfermode::kSrcATop_Mode) {
1814 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001815 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001816 SkXfermode::kSrcOver_Mode,
1817 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001818 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001819 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
1820 if (xfermode == SkXfermode::kModulate_Mode) {
1821 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001822 dst, &fExistingClipStack,
1823 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001824 SkXfermode::kSrcOver_Mode, false);
1825 mode = SkXfermode::kMultiply_Mode;
1826 }
1827 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001828 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001829 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001830 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001831}
1832
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001833bool SkPDFDevice::isContentEmpty() {
halcanary2be7e012016-03-28 11:58:08 -07001834 if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) {
1835 SkASSERT(fContentEntries.count() <= 1);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001836 return true;
1837 }
1838 return false;
1839}
1840
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001841void SkPDFDevice::populateGraphicStateEntryFromPaint(
1842 const SkMatrix& matrix,
1843 const SkClipStack& clipStack,
1844 const SkRegion& clipRegion,
1845 const SkPaint& paint,
1846 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001847 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001848 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1849 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1850 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001851
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001852 entry->fMatrix = matrix;
1853 entry->fClipStack = clipStack;
1854 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001855 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1856 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001857
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001858 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001859 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001860 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001861 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001862 if (shader) {
1863 // PDF positions patterns relative to the initial transform, so
1864 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001865 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001866 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001867
1868 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001869 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001870 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001871
1872 // We need to apply the initial transform to bounds in order to get
1873 // bounds in a consistent coordinate system.
1874 SkRect boundsTemp;
1875 boundsTemp.set(bounds);
1876 fInitialTransform.mapRect(&boundsTemp);
1877 boundsTemp.roundOut(&bounds);
1878
halcanary792c80f2015-02-20 07:21:05 -08001879 SkScalar rasterScale =
1880 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
1881 pdfShader.reset(SkPDFShader::GetPDFShader(
reedfe630452016-03-25 09:08:00 -07001882 fDocument, fRasterDpi, shader, transform, bounds, rasterScale));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001883
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001884 if (pdfShader.get()) {
1885 // pdfShader has been canonicalized so we can directly compare
1886 // pointers.
1887 int resourceIndex = fShaderResources.find(pdfShader.get());
1888 if (resourceIndex < 0) {
1889 resourceIndex = fShaderResources.count();
1890 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001891 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001892 }
1893 entry->fShaderIndex = resourceIndex;
1894 } else {
1895 // A color shader is treated as an invalid shader so we don't have
1896 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001897 SkShader::GradientInfo gradientInfo;
1898 SkColor gradientColor;
1899 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001900 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001901 gradientInfo.fColorCount = 1;
1902 if (shader->asAGradient(&gradientInfo) ==
1903 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001904 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1905 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001906 }
1907 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001908 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001909
halcanary48810a02016-03-07 14:57:50 -08001910 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001911 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001912 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001913 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001914 } else {
1915 SkPaint newPaint = paint;
1916 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001917 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001918 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001919 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001920 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001921 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001922
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001923 if (hasText) {
1924 entry->fTextScaleX = paint.getTextScaleX();
1925 entry->fTextFill = paint.getStyle();
1926 } else {
1927 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001928 }
1929}
1930
halcanarybe27a112015-04-01 13:31:19 -07001931int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001932 // Assumes that gs has been canonicalized (so we can directly compare
1933 // pointers).
1934 int result = fGraphicStateResources.find(gs);
1935 if (result < 0) {
1936 result = fGraphicStateResources.count();
1937 fGraphicStateResources.push(gs);
1938 gs->ref();
1939 }
1940 return result;
1941}
1942
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001943int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
1944 // Assumes that xobject has been canonicalized (so we can directly compare
1945 // pointers).
1946 int result = fXObjectResources.find(xObject);
1947 if (result < 0) {
1948 result = fXObjectResources.count();
1949 fXObjectResources.push(xObject);
1950 xObject->ref();
1951 }
1952 return result;
1953}
1954
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001955void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
halcanary2be7e012016-03-28 11:58:08 -07001956 SkPDFDevice::ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001957 SkTypeface* typeface = paint.getTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -07001958 if (contentEntry->fState.fFont == nullptr ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001959 contentEntry->fState.fTextSize != paint.getTextSize() ||
1960 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001961 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001962 contentEntry->fContent.writeText("/");
1963 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
1964 SkPDFResourceDict::kFont_ResourceType,
1965 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001966 contentEntry->fContent.writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -07001967 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001968 contentEntry->fContent.writeText(" Tf\n");
1969 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001970 }
1971}
1972
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001973int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08001974 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07001975 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001976 int resourceIndex = fFontResources.find(newFont.get());
1977 if (resourceIndex < 0) {
1978 resourceIndex = fFontResources.count();
1979 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001980 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001981 }
1982 return resourceIndex;
1983}
1984
halcanary7a14b312015-10-01 07:28:13 -07001985static SkSize rect_to_size(const SkRect& r) {
1986 return SkSize::Make(r.width(), r.height());
1987}
1988
halcanarya50151d2016-03-25 11:57:49 -07001989static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
1990 SkColorFilter* colorFilter) {
1991 auto surface =
1992 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
1993 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07001994 SkCanvas* canvas = surface->getCanvas();
1995 canvas->clear(SK_ColorTRANSPARENT);
1996 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07001997 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07001998 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07001999 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07002000 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07002001}
2002
2003////////////////////////////////////////////////////////////////////////////////
2004void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
2005 const SkClipStack* clipStack,
2006 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07002007 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07002008 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07002009 if (imageBitmap.dimensions().isZero()) {
2010 return;
2011 }
halcanary7a14b312015-10-01 07:28:13 -07002012 #ifdef SK_PDF_IMAGE_STATS
2013 gDrawImageCalls.fetch_add(1);
2014 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002015 SkMatrix matrix = origMatrix;
2016 SkRegion perspectiveBounds;
2017 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07002018 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002019
2020 // Rasterize the bitmap using perspective in a new bitmap.
2021 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002022 if (fRasterDpi == 0) {
2023 return;
2024 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002025 // Transform the bitmap in the new space, without taking into
2026 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002027 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07002028 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07002029 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002030 perspectiveOutline.transform(origMatrix);
2031
2032 // TODO(edisonn): perf - use current clip too.
2033 // Retrieve the bounds of the new shape.
2034 SkRect bounds = perspectiveOutline.getBounds();
2035
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002036 // Transform the bitmap in the new space, taking into
2037 // account the initial transform.
2038 SkMatrix total = origMatrix;
2039 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07002040 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
2041 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2042 total.postScale(dpiScale, dpiScale);
2043
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002044 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002045 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002046 physicalPerspectiveOutline.transform(total);
2047
halcanary7a14b312015-10-01 07:28:13 -07002048 SkRect physicalPerspectiveBounds =
2049 physicalPerspectiveOutline.getBounds();
2050 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2051 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002052
2053 // TODO(edisonn): A better approach would be to use a bitmap shader
2054 // (in clamp mode) and draw a rect over the entire bounding box. Then
2055 // intersect perspectiveOutline to the clip. That will avoid introducing
2056 // alpha to the image while still giving good behavior at the edge of
2057 // the image. Avoiding alpha will reduce the pdf size and generation
2058 // CPU time some.
2059
halcanary7a14b312015-10-01 07:28:13 -07002060 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2061
reede8f30622016-03-23 18:59:25 -07002062 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002063 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002064 return;
2065 }
halcanary7a14b312015-10-01 07:28:13 -07002066 SkCanvas* canvas = surface->getCanvas();
2067 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002068
2069 SkScalar deltaX = bounds.left();
2070 SkScalar deltaY = bounds.top();
2071
2072 SkMatrix offsetMatrix = origMatrix;
2073 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002074 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002075
2076 // Translate the draw in the new canvas, so we perfectly fit the
2077 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002078 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002079 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002080 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002081 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002082
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002083 // In the new space, we use the identity matrix translated
2084 // and scaled to reflect DPI.
2085 matrix.setScale(1 / scaleX, 1 / scaleY);
2086 matrix.postTranslate(deltaX, deltaY);
2087
halcanary7a14b312015-10-01 07:28:13 -07002088 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002089 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002090
halcanarya50151d2016-03-25 11:57:49 -07002091 autoImageUnref = surface->makeImageSnapshot();
2092 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002093 }
2094
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002095 SkMatrix scaled;
2096 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002097 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2098 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002099 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002100 SkIRect subset = imageBitmap.bounds();
2101 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2102 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002103 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002104 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002105 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002106 return;
2107 }
2108 if (content.needShape()) {
2109 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002110 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002111 shape.transform(matrix);
2112 content.setShape(shape);
2113 }
2114 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002115 return;
2116 }
2117
halcanary287d22d2015-09-24 10:20:05 -07002118 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002119 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002120 // draw calls. This code here works for all
2121 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2122 // rasterize a layer on this backend). Fortuanely, this seems
2123 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002124 autoImageUnref = color_filter(imageBitmap, colorFilter);
2125 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002126 // TODO(halcanary): de-dupe this by caching filtered images.
2127 // (maybe in the resource cache?)
2128 }
halcanarya50151d2016-03-25 11:57:49 -07002129
2130 SkBitmapKey key = imageBitmap.getKey();
2131 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002132 if (!pdfimage) {
halcanarya50151d2016-03-25 11:57:49 -07002133 auto img = imageBitmap.makeImage();
2134 if (!img) {
2135 return;
2136 }
2137 pdfimage = SkPDFCreateBitmapObject(
2138 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002139 if (!pdfimage) {
2140 return;
halcanary287d22d2015-09-24 10:20:05 -07002141 }
halcanarya50151d2016-03-25 11:57:49 -07002142 fDocument->serialize(pdfimage); // serialize images early.
2143 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002144 }
halcanarya50151d2016-03-25 11:57:49 -07002145 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002146 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002147 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002148}