blob: 8e417b1f529dbf9e4b41909175a9cf7107aeb940 [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"
martina.kollarovab8d6af12016-06-29 05:12:31 -070011#include "SkBitmapKey.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000012#include "SkColor.h"
halcanary287d22d2015-09-24 10:20:05 -070013#include "SkColorFilter.h"
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000014#include "SkDraw.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkGlyphCache.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000016#include "SkPath.h"
reeda4393342016-03-18 11:22:57 -070017#include "SkPathEffect.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000018#include "SkPathOps.h"
halcanarydb0dcc72015-03-20 12:31:52 -070019#include "SkPDFBitmap.h"
halcanary7a14b312015-10-01 07:28:13 -070020#include "SkPDFCanon.h"
halcanary989da4a2016-03-21 14:33:17 -070021#include "SkPDFDocument.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000022#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000023#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000024#include "SkPDFGraphicState.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000025#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000026#include "SkPDFShader.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000027#include "SkPDFStream.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000028#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000029#include "SkPDFUtils.h"
wangxianzhuef6c50a2015-09-17 20:38:02 -070030#include "SkRasterClip.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000031#include "SkRRect.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000032#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070033#include "SkSurface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000034#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000035#include "SkTemplates.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000036#include "SkTypefacePriv.h"
halcanarya6814332015-05-27 08:53:36 -070037#include "SkXfermodeInterpretation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000038
edisonn@google.com73a7ea32013-11-11 20:55:15 +000039#define DPI_FOR_RASTER_SCALE_ONE 72
40
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000041// Utility functions
42
halcanarya6814332015-05-27 08:53:36 -070043// If the paint will definitely draw opaquely, replace kSrc_Mode with
44// kSrcOver_Mode. http://crbug.com/473572
45static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
46 if (kSrcOver_SkXfermodeInterpretation
47 == SkInterpretXfermode(*paint, false)) {
halcanary96fcdcc2015-08-27 07:41:13 -070048 paint->setXfermode(nullptr);
halcanarya6814332015-05-27 08:53:36 -070049 }
50}
51
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000052static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000053 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
reed80ea19c2015-05-12 10:37:34 -070054 SkScalar colorScale = SkScalarInvert(0xFF);
55 SkPDFUtils::AppendScalar(SkColorGetR(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000056 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070057 SkPDFUtils::AppendScalar(SkColorGetG(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000058 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070059 SkPDFUtils::AppendScalar(SkColorGetB(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000060 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000061}
62
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000063static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000064 SkPaint result = paint;
65 if (result.isFakeBoldText()) {
66 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
67 kStdFakeBoldInterpKeys,
68 kStdFakeBoldInterpValues,
69 kStdFakeBoldInterpLength);
70 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000071 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000072 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000073 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000074 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000075 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000076 result.setStrokeWidth(width);
77 }
78 return result;
79}
80
81// Stolen from measure_text in SkDraw.cpp and then tweaked.
benjaminwagnerd936f632016-02-23 10:44:31 -080082static void align_text(SkPaint::GlyphCacheProc glyphCacheProc, const SkPaint& paint,
bungeman@google.com9a87cee2011-08-23 17:02:18 +000083 const uint16_t* glyphs, size_t len,
84 SkScalar* x, SkScalar* y) {
85 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000086 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000087 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000088
89 SkMatrix ident;
90 ident.reset();
halcanary96fcdcc2015-08-27 07:41:13 -070091 SkAutoGlyphCache autoCache(paint, nullptr, &ident);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000092 SkGlyphCache* cache = autoCache.getCache();
93
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +000094 const char* start = reinterpret_cast<const char*>(glyphs);
95 const char* stop = reinterpret_cast<const char*>(glyphs + len);
benjaminwagner6b3eacb2016-03-24 19:07:58 -070096 SkScalar xAdv = 0, yAdv = 0;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000097
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000098 // TODO(vandebo): This probably needs to take kerning into account.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000099 while (start < stop) {
benjaminwagnerd936f632016-02-23 10:44:31 -0800100 const SkGlyph& glyph = glyphCacheProc(cache, &start);
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700101 xAdv += SkFloatToScalar(glyph.fAdvanceX);
102 yAdv += SkFloatToScalar(glyph.fAdvanceY);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000103 };
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000104 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000105 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000106 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000107
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000108 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700109 xAdv = SkScalarHalf(xAdv);
110 yAdv = SkScalarHalf(yAdv);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000111 }
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700112 *x = *x - xAdv;
113 *y = *y - yAdv;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000114}
115
robertphillips@google.coma4662862013-11-21 14:24:16 +0000116static int max_glyphid_for_typeface(SkTypeface* typeface) {
reed@google.comfed86bd2013-03-14 15:04:57 +0000117 SkAutoResolveDefaultTypeface autoResolve(typeface);
118 typeface = autoResolve.get();
commit-bot@chromium.org6a4ba5b2013-07-17 21:55:08 +0000119 return typeface->countGlyphs() - 1;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000120}
121
122typedef SkAutoSTMalloc<128, uint16_t> SkGlyphStorage;
123
reed@google.comaec40662014-04-18 19:29:07 +0000124static int force_glyph_encoding(const SkPaint& paint, const void* text,
125 size_t len, SkGlyphStorage* storage,
bungeman22edc832014-10-03 07:55:58 -0700126 const uint16_t** glyphIDs) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000127 // Make sure we have a glyph id encoding.
128 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
halcanary96fcdcc2015-08-27 07:41:13 -0700129 int numGlyphs = paint.textToGlyphs(text, len, nullptr);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000130 storage->reset(numGlyphs);
131 paint.textToGlyphs(text, len, storage->get());
132 *glyphIDs = storage->get();
133 return numGlyphs;
134 }
135
136 // For user supplied glyph ids we need to validate them.
137 SkASSERT((len & 1) == 0);
reed@google.comaec40662014-04-18 19:29:07 +0000138 int numGlyphs = SkToInt(len / 2);
bungeman22edc832014-10-03 07:55:58 -0700139 const uint16_t* input = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000140
141 int maxGlyphID = max_glyphid_for_typeface(paint.getTypeface());
reed@google.comaec40662014-04-18 19:29:07 +0000142 int validated;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000143 for (validated = 0; validated < numGlyphs; ++validated) {
144 if (input[validated] > maxGlyphID) {
145 break;
146 }
147 }
148 if (validated >= numGlyphs) {
bungeman22edc832014-10-03 07:55:58 -0700149 *glyphIDs = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000150 return numGlyphs;
151 }
152
153 // Silently drop anything out of range.
154 storage->reset(numGlyphs);
155 if (validated > 0) {
156 memcpy(storage->get(), input, validated * sizeof(uint16_t));
157 }
158
reed@google.comaec40662014-04-18 19:29:07 +0000159 for (int i = validated; i < numGlyphs; ++i) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000160 storage->get()[i] = input[i];
161 if (input[i] > maxGlyphID) {
162 storage->get()[i] = 0;
163 }
164 }
165 *glyphIDs = storage->get();
166 return numGlyphs;
167}
168
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000169static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX,
170 SkWStream* content) {
171 // Flip the text about the x-axis to account for origin swap and include
172 // the passed parameters.
173 content->writeText("1 0 ");
halcanarybc4696b2015-05-06 10:56:04 -0700174 SkPDFUtils::AppendScalar(0 - textSkewX, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000175 content->writeText(" -1 ");
halcanarybc4696b2015-05-06 10:56:04 -0700176 SkPDFUtils::AppendScalar(x, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000177 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700178 SkPDFUtils::AppendScalar(y, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000179 content->writeText(" Tm\n");
180}
181
halcanary2be7e012016-03-28 11:58:08 -0700182SkPDFDevice::GraphicStateEntry::GraphicStateEntry()
183 : fColor(SK_ColorBLACK)
184 , fTextScaleX(SK_Scalar1)
185 , fTextFill(SkPaint::kFill_Style)
186 , fShaderIndex(-1)
187 , fGraphicStateIndex(-1)
188 , fFont(nullptr)
189 , fTextSize(SK_ScalarNaN) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000190 fMatrix.reset();
191}
192
halcanary2be7e012016-03-28 11:58:08 -0700193bool SkPDFDevice::GraphicStateEntry::compareInitialState(
194 const GraphicStateEntry& cur) {
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +0000195 return fColor == cur.fColor &&
196 fShaderIndex == cur.fShaderIndex &&
197 fGraphicStateIndex == cur.fGraphicStateIndex &&
198 fMatrix == cur.fMatrix &&
199 fClipStack == cur.fClipStack &&
200 (fTextScaleX == 0 ||
201 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000202}
203
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000204class GraphicStackState {
205public:
206 GraphicStackState(const SkClipStack& existingClipStack,
207 const SkRegion& existingClipRegion,
208 SkWStream* contentStream)
209 : fStackDepth(0),
210 fContentStream(contentStream) {
211 fEntries[0].fClipStack = existingClipStack;
212 fEntries[0].fClipRegion = existingClipRegion;
213 }
214
215 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000216 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000217 void updateMatrix(const SkMatrix& matrix);
halcanary2be7e012016-03-28 11:58:08 -0700218 void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000219
220 void drainStack();
221
222private:
223 void push();
224 void pop();
halcanary2be7e012016-03-28 11:58:08 -0700225 SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000226
227 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
228 static const int kMaxStackDepth = 12;
halcanary2be7e012016-03-28 11:58:08 -0700229 SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1];
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000230 int fStackDepth;
231 SkWStream* fContentStream;
232};
233
234void GraphicStackState::drainStack() {
235 while (fStackDepth) {
236 pop();
237 }
238}
239
240void GraphicStackState::push() {
241 SkASSERT(fStackDepth < kMaxStackDepth);
242 fContentStream->writeText("q\n");
243 fStackDepth++;
244 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
245}
246
247void GraphicStackState::pop() {
248 SkASSERT(fStackDepth > 0);
249 fContentStream->writeText("Q\n");
250 fStackDepth--;
251}
252
robertphillips@google.com80214e22012-07-20 15:33:18 +0000253// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000254// and then skips over the leading entries as specified in prefix. It requires
255// and asserts that "prefix" will be a prefix to "stack."
256static void skip_clip_stack_prefix(const SkClipStack& prefix,
257 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000258 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000259 SkClipStack::B2TIter prefixIter(prefix);
260 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000261
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000262 const SkClipStack::Element* prefixEntry;
263 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000264
265 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000266 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000267 iterEntry = iter->next();
268 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000269 // Because of SkClipStack does internal intersection, the last clip
270 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000271 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000272 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
273 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
274 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000275 // back up the iterator by one
276 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000277 prefixEntry = prefixIter.next();
278 break;
279 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000280 }
281
halcanary96fcdcc2015-08-27 07:41:13 -0700282 SkASSERT(prefixEntry == nullptr);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000283}
284
285static void emit_clip(SkPath* clipPath, SkRect* clipRect,
286 SkWStream* contentStream) {
287 SkASSERT(clipPath || clipRect);
288
289 SkPath::FillType clipFill;
290 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000291 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000292 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000293 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000294 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
295 clipFill = SkPath::kWinding_FillType;
296 }
297
298 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
299 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
300 if (clipFill == SkPath::kEvenOdd_FillType) {
301 contentStream->writeText("W* n\n");
302 } else {
303 contentStream->writeText("W n\n");
304 }
305}
306
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000307/* Calculate an inverted path's equivalent non-inverted path, given the
308 * canvas bounds.
309 * outPath may alias with invPath (since this is supported by PathOps).
310 */
311static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
312 SkPath* outPath) {
313 SkASSERT(invPath.isInverseFillType());
314
315 SkPath clipPath;
316 clipPath.addRect(bounds);
317
reedcdb42bb2015-06-26 10:23:07 -0700318 return Op(clipPath, invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000319}
320
321// Sanity check the numerical values of the SkRegion ops and PathOps ops
322// enums so region_op_to_pathops_op can do a straight passthrough cast.
323// If these are failing, it may be necessary to make region_op_to_pathops_op
324// do more.
bungeman99fe8222015-08-20 07:57:51 -0700325static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
326static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
327static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
328static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
329static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
330 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000331
332static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
333 SkASSERT(op >= 0);
334 SkASSERT(op <= SkRegion::kReverseDifference_Op);
335 return (SkPathOp)op;
336}
337
338/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
339 * Returns true if successful, or false if not successful.
340 * If successful, the resulting clip is stored in outClipPath.
341 * If not successful, outClipPath is undefined, and a fallback method
342 * should be used.
343 */
344static bool get_clip_stack_path(const SkMatrix& transform,
345 const SkClipStack& clipStack,
346 const SkRegion& clipRegion,
347 SkPath* outClipPath) {
348 outClipPath->reset();
349 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
350
351 const SkClipStack::Element* clipEntry;
352 SkClipStack::Iter iter;
353 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
354 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
355 SkPath entryPath;
356 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
357 outClipPath->reset();
358 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
359 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000360 } else {
361 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000362 }
363 entryPath.transform(transform);
364
365 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
366 *outClipPath = entryPath;
367 } else {
368 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
369 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
370 return false;
371 }
372 }
373 }
374
375 if (outClipPath->isInverseFillType()) {
376 // The bounds are slightly outset to ensure this is correct in the
377 // face of floating-point accuracy and possible SkRegion bitmap
378 // approximations.
379 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
380 clipBounds.outset(SK_Scalar1, SK_Scalar1);
381 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
382 return false;
383 }
384 }
385 return true;
386}
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000387
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000388// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000389// graphic state stack, and the fact that we can know all the clips used
390// on the page to optimize this.
391void GraphicStackState::updateClip(const SkClipStack& clipStack,
392 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000393 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000394 if (clipStack == currentEntry()->fClipStack) {
395 return;
396 }
397
398 while (fStackDepth > 0) {
399 pop();
400 if (clipStack == currentEntry()->fClipStack) {
401 return;
402 }
403 }
404 push();
405
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000406 currentEntry()->fClipStack = clipStack;
407 currentEntry()->fClipRegion = clipRegion;
408
409 SkMatrix transform;
410 transform.setTranslate(translation.fX, translation.fY);
411
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000412 SkPath clipPath;
413 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700414 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000415 return;
416 }
halcanarydda239e2016-03-31 07:33:57 -0700417
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000418 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
419 // already been applied. (If this is a top level device, then it specifies
420 // a clip to the content area. If this is a layer, then it specifies
421 // the clip in effect when the layer was created.) There's no need to
422 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
423 // initial clip on the parent layer. (This means there's a bug if the user
424 // expands the clip and then uses any xfer mode that uses dst:
425 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000426 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000427 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
428
429 // If the clip stack does anything other than intersect or if it uses
430 // an inverse fill type, we have to fall back to the clip region.
431 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000432 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000433 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000434 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
435 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000436 needRegion = true;
437 break;
438 }
439 }
440
441 if (needRegion) {
442 SkPath clipPath;
443 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700444 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000445 } else {
446 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000447 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000448 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000449 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
450 switch (clipEntry->getType()) {
451 case SkClipStack::Element::kRect_Type: {
452 SkRect translatedClip;
453 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700454 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000455 break;
456 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000457 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000458 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000459 clipEntry->asPath(&translatedPath);
460 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700461 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000462 break;
463 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000464 }
465 }
466 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000467}
468
469void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
470 if (matrix == currentEntry()->fMatrix) {
471 return;
472 }
473
474 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
475 SkASSERT(fStackDepth > 0);
476 SkASSERT(fEntries[fStackDepth].fClipStack ==
477 fEntries[fStackDepth -1].fClipStack);
478 pop();
479
480 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
481 }
482 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
483 return;
484 }
485
486 push();
487 SkPDFUtils::AppendTransform(matrix, fContentStream);
488 currentEntry()->fMatrix = matrix;
489}
490
halcanary2be7e012016-03-28 11:58:08 -0700491void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000492 // PDF treats a shader as a color, so we only set one or the other.
493 if (state.fShaderIndex >= 0) {
494 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000495 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000496 currentEntry()->fShaderIndex = state.fShaderIndex;
497 }
498 } else {
499 if (state.fColor != currentEntry()->fColor ||
500 currentEntry()->fShaderIndex >= 0) {
501 emit_pdf_color(state.fColor, fContentStream);
502 fContentStream->writeText("RG ");
503 emit_pdf_color(state.fColor, fContentStream);
504 fContentStream->writeText("rg\n");
505 currentEntry()->fColor = state.fColor;
506 currentEntry()->fShaderIndex = -1;
507 }
508 }
509
510 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000511 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000512 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
513 }
514
515 if (state.fTextScaleX) {
516 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
517 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
518 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700519 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000520 fContentStream->writeText(" Tz\n");
521 currentEntry()->fTextScaleX = state.fTextScaleX;
522 }
523 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700524 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
525 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
526 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000527 fContentStream->writeDecAsText(state.fTextFill);
528 fContentStream->writeText(" Tr\n");
529 currentEntry()->fTextFill = state.fTextFill;
530 }
531 }
532}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000533
reed76033be2015-03-14 10:54:31 -0700534static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700535 // PDF does not support image filters, so render them on CPU.
536 // Note that this rendering is done at "screen" resolution (100dpi), not
537 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700538 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800539 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700540
541 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700542 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700543}
544
545SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
reedcd4051e2016-07-15 09:41:26 -0700546 if (layerPaint && not_supported_for_layers(*layerPaint)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700547 return nullptr;
senorblancob0e89dc2014-10-20 14:03:12 -0700548 }
fmalita6987dca2014-11-13 08:33:37 -0800549 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700550 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000551}
552
halcanary989da4a2016-03-21 14:33:17 -0700553SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
554
bsalomon@google.come97f0852011-06-17 13:10:25 +0000555
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000556
557// A helper class to automatically finish a ContentEntry at the end of a
558// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000559class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000560public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000561 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
562 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000563 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700564 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000565 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700566 fDstFormXObject(nullptr) {
reed1e7f5e72016-04-27 07:49:17 -0700567 init(draw.fClipStack, draw.fRC->bwRgn(), *draw.fMatrix, paint, hasText);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000568 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000569 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
570 const SkRegion& clipRegion, const SkMatrix& matrix,
571 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000572 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700573 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000574 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700575 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000576 init(clipStack, clipRegion, matrix, paint, hasText);
577 }
578
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000579 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000580 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000581 SkPath* shape = &fShape;
582 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700583 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000584 }
585 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000586 }
reed@google.comfc641d02012-09-20 17:52:20 +0000587 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000588 }
589
halcanary2be7e012016-03-28 11:58:08 -0700590 SkPDFDevice::ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000591
592 /* Returns true when we explicitly need the shape of the drawing. */
593 bool needShape() {
594 switch (fXfermode) {
595 case SkXfermode::kClear_Mode:
596 case SkXfermode::kSrc_Mode:
597 case SkXfermode::kSrcIn_Mode:
598 case SkXfermode::kSrcOut_Mode:
599 case SkXfermode::kDstIn_Mode:
600 case SkXfermode::kDstOut_Mode:
601 case SkXfermode::kSrcATop_Mode:
602 case SkXfermode::kDstATop_Mode:
603 case SkXfermode::kModulate_Mode:
604 return true;
605 default:
606 return false;
607 }
608 }
609
610 /* Returns true unless we only need the shape of the drawing. */
611 bool needSource() {
612 if (fXfermode == SkXfermode::kClear_Mode) {
613 return false;
614 }
615 return true;
616 }
617
618 /* If the shape is different than the alpha component of the content, then
619 * setShape should be called with the shape. In particular, images and
620 * devices have rectangular shape.
621 */
622 void setShape(const SkPath& shape) {
623 fShape = shape;
624 }
625
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000626private:
627 SkPDFDevice* fDevice;
halcanary2be7e012016-03-28 11:58:08 -0700628 SkPDFDevice::ContentEntry* fContentEntry;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000629 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000630 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000631 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000632
633 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
634 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000635 // Shape has to be flatten before we get here.
636 if (matrix.hasPerspective()) {
637 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000638 return;
639 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000640 if (paint.getXfermode()) {
641 paint.getXfermode()->asMode(&fXfermode);
642 }
643 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
644 matrix, paint, hasText,
645 &fDstFormXObject);
646 }
647};
648
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000649////////////////////////////////////////////////////////////////////////////////
650
halcanary989da4a2016-03-21 14:33:17 -0700651SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
robertphillips9a53fd72015-06-22 09:46:59 -0700652 : INHERITED(SkSurfaceProps(0, kUnknown_SkPixelGeometry))
653 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800654 , fContentSize(pageSize)
655 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanary96fcdcc2015-08-27 07:41:13 -0700656 , fClipStack(nullptr)
halcanarya1f1ee92015-02-20 06:17:26 -0800657 , fRasterDpi(rasterDpi)
halcanary989da4a2016-03-21 14:33:17 -0700658 , fDocument(doc) {
halcanarya1f1ee92015-02-20 06:17:26 -0800659 SkASSERT(pageSize.width() > 0);
660 SkASSERT(pageSize.height() > 0);
661 fLegacyBitmap.setInfo(
662 SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()));
663 if (flip) {
664 // Skia generally uses the top left as the origin but PDF
665 // natively has the origin at the bottom left. This matrix
666 // corrects for that. But that only needs to be done once, we
667 // don't do it when layering.
668 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
669 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
670 } else {
671 fInitialTransform.setIdentity();
672 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000673}
674
675SkPDFDevice::~SkPDFDevice() {
halcanary3c35fb32016-06-30 11:55:07 -0700676 this->cleanUp();
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000677}
678
679void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700680 fContentEntries.reset();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000681}
682
halcanary3c35fb32016-06-30 11:55:07 -0700683void SkPDFDevice::cleanUp() {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000684 fGraphicStateResources.unrefAll();
685 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000686 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000687 fShaderResources.unrefAll();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000688}
689
reedf70b5312016-03-04 16:36:20 -0800690void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
691 SkData* value) {
692 if (0 == rect.width() && 0 == rect.height()) {
693 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
694 } else {
695 SkPath path;
696 path.addRect(rect);
697 handlePathAnnotation(path, d, key, value);
698 }
699}
700
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000701void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000702 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700703 replace_srcmode_on_opaque_paint(&newPaint);
704
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000705 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000706 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000707 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000708}
709
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000710void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
halcanary2be7e012016-03-28 11:58:08 -0700711 SkPDFDevice::ContentEntry* contentEntry) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000712 if (!contentEntry) {
713 return;
714 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000715 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
716 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000717 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000718 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000719 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000720 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000721 inverse.mapRect(&bbox);
722
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000723 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000724 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000725 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000726}
727
halcanary682ee012016-01-28 10:59:34 -0800728void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700729 SkCanvas::PointMode mode,
730 size_t count,
731 const SkPoint* points,
732 const SkPaint& srcPaint) {
733 SkPaint passedPaint = srcPaint;
734 replace_srcmode_on_opaque_paint(&passedPaint);
735
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000736 if (count == 0) {
737 return;
738 }
739
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000740 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
741 // We only use this when there's a path effect because of the overhead
742 // of multiple calls to setUpContentEntry it causes.
743 if (passedPaint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700744 if (d.fRC->isEmpty()) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000745 return;
746 }
747 SkDraw pointDraw(d);
748 pointDraw.fDevice = this;
749 pointDraw.drawPoints(mode, count, points, passedPaint, true);
750 return;
751 }
752
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000753 const SkPaint* paint = &passedPaint;
754 SkPaint modifiedPaint;
755
756 if (mode == SkCanvas::kPoints_PointMode &&
757 paint->getStrokeCap() != SkPaint::kRound_Cap) {
758 modifiedPaint = *paint;
759 paint = &modifiedPaint;
760 if (paint->getStrokeWidth()) {
761 // PDF won't draw a single point with square/butt caps because the
762 // orientation is ambiguous. Draw a rectangle instead.
763 modifiedPaint.setStyle(SkPaint::kFill_Style);
764 SkScalar strokeWidth = paint->getStrokeWidth();
765 SkScalar halfStroke = SkScalarHalf(strokeWidth);
766 for (size_t i = 0; i < count; i++) {
767 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
768 r.inset(-halfStroke, -halfStroke);
769 drawRect(d, r, modifiedPaint);
770 }
771 return;
772 } else {
773 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
774 }
775 }
776
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000777 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000778 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000779 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000780 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000781
782 switch (mode) {
783 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000784 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000785 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000786 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000787 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000788 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000789 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000790 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000791 break;
792 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000793 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000794 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000795 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000796 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000797 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000798 &content.entry()->fContent);
799 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000800 }
801 break;
802 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000803 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
804 for (size_t i = 0; i < count; i++) {
805 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000806 &content.entry()->fContent);
807 SkPDFUtils::ClosePath(&content.entry()->fContent);
808 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000809 }
810 break;
811 default:
812 SkASSERT(false);
813 }
814}
815
halcanary8103a342016-03-08 15:10:16 -0800816static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800817 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700818 annotation->insertName("Subtype", "Link");
halcanary488165e2016-04-22 06:10:21 -0700819 annotation->insertInt("F", 4); // required by ISO 19005
wangxianzhud76665d2015-07-17 17:23:15 -0700820
halcanaryece83922016-03-08 08:32:12 -0800821 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700822 border->reserve(3);
823 border->appendInt(0); // Horizontal corner radius.
824 border->appendInt(0); // Vertical corner radius.
825 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800826 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700827
halcanaryece83922016-03-08 08:32:12 -0800828 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700829 rect->reserve(4);
830 rect->appendScalar(translatedRect.fLeft);
831 rect->appendScalar(translatedRect.fTop);
832 rect->appendScalar(translatedRect.fRight);
833 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800834 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700835
halcanary8103a342016-03-08 15:10:16 -0800836 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700837}
838
halcanary8103a342016-03-08 15:10:16 -0800839static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
840 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700841 SkString url(static_cast<const char *>(urlData->data()),
842 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800843 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700844 action->insertName("S", "URI");
845 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800846 annotation->insertObject("A", std::move(action));
847 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700848}
849
halcanary8103a342016-03-08 15:10:16 -0800850static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
851 const SkRect& r) {
852 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700853 SkString name(static_cast<const char *>(nameData->data()),
854 nameData->size() - 1);
855 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800856 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700857}
858
halcanary682ee012016-01-28 10:59:34 -0800859void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700860 const SkRect& rect,
861 const SkPaint& srcPaint) {
862 SkPaint paint = srcPaint;
863 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000864 SkRect r = rect;
865 r.sort();
866
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000867 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700868 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000869 return;
870 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000871 SkPath path;
872 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700873 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000874 return;
875 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000876
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000877 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000878 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000879 return;
880 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000881 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000882 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000883 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000884}
885
halcanarya6814332015-05-27 08:53:36 -0700886void SkPDFDevice::drawRRect(const SkDraw& draw,
887 const SkRRect& rrect,
888 const SkPaint& srcPaint) {
889 SkPaint paint = srcPaint;
890 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000891 SkPath path;
892 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700893 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000894}
895
halcanarya6814332015-05-27 08:53:36 -0700896void SkPDFDevice::drawOval(const SkDraw& draw,
897 const SkRect& oval,
898 const SkPaint& srcPaint) {
899 SkPaint paint = srcPaint;
900 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700901 SkPath path;
902 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700903 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700904}
905
halcanary682ee012016-01-28 10:59:34 -0800906void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700907 const SkPath& origPath,
908 const SkPaint& srcPaint,
909 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000910 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700911 SkPaint paint = srcPaint;
912 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800913 SkPath modifiedPath;
914 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000915
916 SkMatrix matrix = *d.fMatrix;
917 if (prePathMatrix) {
918 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800919 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000920 pathPtr = &modifiedPath;
921 pathIsMutable = true;
922 }
halcanary682ee012016-01-28 10:59:34 -0800923 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000924 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000925 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000926 }
927 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000928
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000929 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700930 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000931 return;
932 }
halcanary682ee012016-01-28 10:59:34 -0800933 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000934 pathPtr = &modifiedPath;
935 pathIsMutable = true;
936 }
halcanary682ee012016-01-28 10:59:34 -0800937 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000938
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000939 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700940 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000941 if (fill) {
942 noEffectPaint.setStyle(SkPaint::kFill_Style);
943 } else {
944 noEffectPaint.setStyle(SkPaint::kStroke_Style);
945 noEffectPaint.setStrokeWidth(0);
946 }
halcanary96fcdcc2015-08-27 07:41:13 -0700947 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000948 return;
949 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000950
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000951 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000952 return;
953 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000954
reed1e7f5e72016-04-27 07:49:17 -0700955 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000956 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000957 return;
958 }
halcanary8b2bc252015-10-06 09:41:47 -0700959 bool consumeDegeratePathSegments =
960 paint.getStyle() == SkPaint::kFill_Style ||
961 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
962 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000963 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -0700964 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000965 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000966 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000967 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000968}
969
halcanary7a14b312015-10-01 07:28:13 -0700970void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
971 const SkBitmap& bitmap,
972 const SkRect* src,
973 const SkRect& dst,
974 const SkPaint& srcPaint,
975 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700976 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -0700977}
978
979void SkPDFDevice::drawBitmap(const SkDraw& d,
980 const SkBitmap& bitmap,
981 const SkMatrix& matrix,
982 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -0700983 SkPaint paint = srcPaint;
984 if (bitmap.isOpaque()) {
985 replace_srcmode_on_opaque_paint(&paint);
986 }
987
reed1e7f5e72016-04-27 07:49:17 -0700988 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700989 return;
990 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +0000991
halcanary7a14b312015-10-01 07:28:13 -0700992 SkMatrix transform = matrix;
993 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -0700994 SkImageBitmap imageBitmap(bitmap);
995 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700996 transform, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700997}
998
999void SkPDFDevice::drawSprite(const SkDraw& d,
1000 const SkBitmap& bitmap,
1001 int x,
1002 int y,
1003 const SkPaint& srcPaint) {
1004 SkPaint paint = srcPaint;
1005 if (bitmap.isOpaque()) {
1006 replace_srcmode_on_opaque_paint(&paint);
1007 }
1008
reed1e7f5e72016-04-27 07:49:17 -07001009 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -07001010 return;
1011 }
1012
1013 SkMatrix matrix;
1014 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -07001015 SkImageBitmap imageBitmap(bitmap);
1016 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -07001017 matrix, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001018}
1019
1020void SkPDFDevice::drawImage(const SkDraw& draw,
1021 const SkImage* image,
1022 SkScalar x,
1023 SkScalar y,
1024 const SkPaint& srcPaint) {
1025 SkPaint paint = srcPaint;
1026 if (!image) {
1027 return;
1028 }
1029 if (image->isOpaque()) {
1030 replace_srcmode_on_opaque_paint(&paint);
1031 }
reed1e7f5e72016-04-27 07:49:17 -07001032 if (draw.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -07001033 return;
1034 }
1035 SkMatrix transform = SkMatrix::MakeTrans(x, y);
1036 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001037 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
1038 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -07001039 transform, draw.fClipStack, draw.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001040}
1041
1042void SkPDFDevice::drawImageRect(const SkDraw& draw,
1043 const SkImage* image,
1044 const SkRect* src,
1045 const SkRect& dst,
1046 const SkPaint& srcPaint,
1047 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001048 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001049}
1050
halcanarybb264b72015-04-07 10:40:03 -07001051// Create a PDF string. Maximum length (in bytes) is 65,535.
1052// @param input A string value.
1053// @param len The length of the input array.
1054// @param wideChars True iff the upper byte in each uint16_t is
1055// significant and should be encoded and not
1056// discarded. If true, the upper byte is encoded
1057// first. Otherwise, we assert the upper byte is
1058// zero.
halcanaryee41b752016-06-23 14:08:11 -07001059static void write_wide_string(SkDynamicMemoryWStream* wStream,
1060 const uint16_t* input,
1061 size_t len,
1062 bool wideChars) {
halcanarybb264b72015-04-07 10:40:03 -07001063 if (wideChars) {
1064 SkASSERT(2 * len < 65535);
halcanaryee41b752016-06-23 14:08:11 -07001065 wStream->writeText("<");
halcanarybb264b72015-04-07 10:40:03 -07001066 for (size_t i = 0; i < len; i++) {
halcanarya76a10b72016-07-07 12:31:55 -07001067 SkPDFUtils::WriteUInt16BE(wStream, input[i]);
halcanarybb264b72015-04-07 10:40:03 -07001068 }
halcanaryee41b752016-06-23 14:08:11 -07001069 wStream->writeText(">");
halcanarybb264b72015-04-07 10:40:03 -07001070 } else {
1071 SkASSERT(len <= 65535);
halcanaryee41b752016-06-23 14:08:11 -07001072 SkAutoMalloc buffer(len); // Remove every other byte.
1073 uint8_t* ptr = (uint8_t*)buffer.get();
halcanarybb264b72015-04-07 10:40:03 -07001074 for (size_t i = 0; i < len; i++) {
1075 SkASSERT(0 == input[i] >> 8);
halcanaryee41b752016-06-23 14:08:11 -07001076 ptr[i] = static_cast<uint8_t>(input[i]);
halcanarybb264b72015-04-07 10:40:03 -07001077 }
halcanaryee41b752016-06-23 14:08:11 -07001078 SkPDFUtils::WriteString(wStream, (char*)buffer.get(), len);
halcanarybb264b72015-04-07 10:40:03 -07001079 }
1080}
1081
halcanary66a82f32015-10-12 13:05:04 -07001082static void draw_transparent_text(SkPDFDevice* device,
1083 const SkDraw& d,
1084 const void* text, size_t len,
1085 SkScalar x, SkScalar y,
1086 const SkPaint& srcPaint) {
1087
1088 SkPaint transparent;
1089 if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(),
1090 device->getCanon())) {
1091 SkDEBUGFAIL("default typeface should be embeddable");
1092 return; // Avoid infinite loop in release.
1093 }
1094 transparent.setTextSize(srcPaint.getTextSize());
1095 transparent.setColor(SK_ColorTRANSPARENT);
1096 switch (srcPaint.getTextEncoding()) {
1097 case SkPaint::kGlyphID_TextEncoding: {
1098 // Since a glyphId<->Unicode mapping is typeface-specific,
1099 // map back to Unicode first.
1100 size_t glyphCount = len / 2;
1101 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1102 srcPaint.glyphsToUnichars(
1103 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1104 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
1105 device->drawText(d, &unichars[0],
1106 glyphCount * sizeof(SkUnichar),
1107 x, y, transparent);
1108 break;
1109 }
1110 case SkPaint::kUTF8_TextEncoding:
1111 case SkPaint::kUTF16_TextEncoding:
1112 case SkPaint::kUTF32_TextEncoding:
1113 transparent.setTextEncoding(srcPaint.getTextEncoding());
1114 device->drawText(d, text, len, x, y, transparent);
1115 break;
1116 default:
1117 SkFAIL("unknown text encoding");
1118 }
1119}
1120
1121
halcanary682ee012016-01-28 10:59:34 -08001122void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
halcanarya6814332015-05-27 08:53:36 -07001123 SkScalar x, SkScalar y, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001124 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary6950de62015-11-07 05:29:00 -08001125 // https://bug.skia.org/3866
halcanary66a82f32015-10-12 13:05:04 -07001126 SkPath path;
1127 srcPaint.getTextPath(text, len, x, y, &path);
1128 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1129 // Draw text transparently to make it copyable/searchable/accessable.
1130 draw_transparent_text(this, d, text, len, x, y, srcPaint);
1131 return;
1132 }
halcanarya6814332015-05-27 08:53:36 -07001133 SkPaint paint = srcPaint;
1134 replace_srcmode_on_opaque_paint(&paint);
1135
halcanary96fcdcc2015-08-27 07:41:13 -07001136 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1137 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001138 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1139 // making text unreadable (e.g. same text twice when using CSS shadows).
1140 return;
1141 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001142 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001143 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001144 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001145 return;
1146 }
1147
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001148 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001149 const uint16_t* glyphIDs = nullptr;
reed@google.comaec40662014-04-18 19:29:07 +00001150 int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001151 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001152
benjaminwagnerd936f632016-02-23 10:44:31 -08001153 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001154 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001155 content.entry()->fContent.writeText("BT\n");
1156 set_text_transform(x, y, textPaint.getTextSkewX(),
1157 &content.entry()->fContent);
reed@google.comaec40662014-04-18 19:29:07 +00001158 int consumedGlyphCount = 0;
halcanary2f912f32014-10-16 09:53:20 -07001159
1160 SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs);
1161
halcanary3c35fb32016-06-30 11:55:07 -07001162 SkPDFGlyphSetMap* fontGlyphUsage = fDocument->getGlyphUsage();
1163
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001164 while (numGlyphs > consumedGlyphCount) {
robertphillips8e0c1502015-07-07 10:28:43 -07001165 this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001166 SkPDFFont* font = content.entry()->fState.fFont;
halcanary2f912f32014-10-16 09:53:20 -07001167
1168 int availableGlyphs = font->glyphsToPDFFontEncoding(
1169 glyphIDsCopy.begin() + consumedGlyphCount,
1170 numGlyphs - consumedGlyphCount);
halcanary3c35fb32016-06-30 11:55:07 -07001171 fontGlyphUsage->noteGlyphUsage(
halcanary2f912f32014-10-16 09:53:20 -07001172 font, glyphIDsCopy.begin() + consumedGlyphCount,
1173 availableGlyphs);
halcanaryee41b752016-06-23 14:08:11 -07001174 write_wide_string(&content.entry()->fContent,
1175 glyphIDsCopy.begin() + consumedGlyphCount,
1176 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001177 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001178 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001179 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001180 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001181}
1182
halcanary682ee012016-01-28 10:59:34 -08001183void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -07001184 const SkScalar pos[], int scalarsPerPos,
halcanary682ee012016-01-28 10:59:34 -08001185 const SkPoint& offset, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001186 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary66a82f32015-10-12 13:05:04 -07001187 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos);
1188 SkAutoTMalloc<SkPoint> positionsBuffer;
1189 if (2 != scalarsPerPos) {
1190 int glyphCount = srcPaint.textToGlyphs(text, len, NULL);
1191 positionsBuffer.reset(glyphCount);
1192 for (int i = 0; i < glyphCount; ++i) {
1193 positionsBuffer[i].set(pos[i], 0.0f);
1194 }
1195 positions = &positionsBuffer[0];
1196 }
1197 SkPath path;
1198 srcPaint.getPosTextPath(text, len, positions, &path);
1199 SkMatrix matrix;
1200 matrix.setTranslate(offset);
1201 this->drawPath(d, path, srcPaint, &matrix, true);
1202 // Draw text transparently to make it copyable/searchable/accessable.
1203 draw_transparent_text(
1204 this, d, text, len, offset.x() + positions[0].x(),
1205 offset.y() + positions[0].y(), srcPaint);
1206 return;
1207 }
1208
halcanarya6814332015-05-27 08:53:36 -07001209 SkPaint paint = srcPaint;
1210 replace_srcmode_on_opaque_paint(&paint);
1211
halcanary96fcdcc2015-08-27 07:41:13 -07001212 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1213 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001214 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1215 // making text unreadable (e.g. same text twice when using CSS shadows).
1216 return;
1217 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001218 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001219 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001220 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001221 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001222 return;
1223 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001224
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001225 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001226 const uint16_t* glyphIDs = nullptr;
bungeman22edc832014-10-03 07:55:58 -07001227 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001228 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001229
benjaminwagnerd936f632016-02-23 10:44:31 -08001230 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001231 content.entry()->fContent.writeText("BT\n");
robertphillips8e0c1502015-07-07 10:28:43 -07001232 this->updateFont(textPaint, glyphIDs[0], content.entry());
halcanary3c35fb32016-06-30 11:55:07 -07001233 SkPDFGlyphSetMap* fontGlyphUsage = fDocument->getGlyphUsage();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001234 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001235 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001236 uint16_t encodedValue = glyphIDs[i];
1237 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
bungeman22edc832014-10-03 07:55:58 -07001238 // The current pdf font cannot encode the current glyph.
1239 // Try to get a pdf font which can encode the current glyph.
robertphillips8e0c1502015-07-07 10:28:43 -07001240 this->updateFont(textPaint, glyphIDs[i], content.entry());
bungeman22edc832014-10-03 07:55:58 -07001241 font = content.entry()->fState.fFont;
1242 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
1243 SkDEBUGFAIL("PDF could not encode glyph.");
1244 continue;
1245 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001246 }
bungeman22edc832014-10-03 07:55:58 -07001247
halcanary3c35fb32016-06-30 11:55:07 -07001248 fontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
fmalita05c4a432014-09-29 06:29:53 -07001249 SkScalar x = offset.x() + pos[i * scalarsPerPos];
1250 SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0);
1251
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001252 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
bungeman22edc832014-10-03 07:55:58 -07001253 set_text_transform(x, y, textPaint.getTextSkewX(), &content.entry()->fContent);
halcanaryee41b752016-06-23 14:08:11 -07001254 write_wide_string(&content.entry()->fContent, &encodedValue, 1,
1255 font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001256 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001257 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001258 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001259}
1260
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001261void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001262 int vertexCount, const SkPoint verts[],
1263 const SkPoint texs[], const SkColor colors[],
1264 SkXfermode* xmode, const uint16_t indices[],
1265 int indexCount, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001266 if (d.fRC->isEmpty()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001267 return;
1268 }
reed@google.com85e143c2013-12-30 15:51:25 +00001269 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001270}
1271
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001272void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1273 int x, int y, const SkPaint& paint) {
fmalita6987dca2014-11-13 08:33:37 -08001274 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001275 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001276
1277 SkScalar scalarX = SkIntToScalar(x);
1278 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001279 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1280 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001281 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001282 }
halcanary91fcb3e2016-03-04 13:53:22 -08001283 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1284 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001285 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001286 }
halcanary91fcb3e2016-03-04 13:53:22 -08001287 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1288 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001289 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001290 }
1291
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001292 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001293 return;
1294 }
1295
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001296 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001297 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
reed1e7f5e72016-04-27 07:49:17 -07001298 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001299 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001300 return;
1301 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001302 if (content.needShape()) {
1303 SkPath shape;
1304 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001305 SkIntToScalar(device->width()),
1306 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001307 content.setShape(shape);
1308 }
1309 if (!content.needSource()) {
1310 return;
1311 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001312
halcanaryece83922016-03-08 08:32:12 -08001313 auto xObject = sk_make_sp<SkPDFFormXObject>(pdfDevice);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001314 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001315 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001316}
1317
reed89443ab2014-06-27 11:34:19 -07001318SkImageInfo SkPDFDevice::imageInfo() const {
1319 return fLegacyBitmap.info();
1320}
1321
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001322void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1323 INHERITED::onAttachToCanvas(canvas);
1324
1325 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1326 fClipStack = canvas->getClipStack();
1327}
1328
1329void SkPDFDevice::onDetachFromCanvas() {
1330 INHERITED::onDetachFromCanvas();
1331
halcanary96fcdcc2015-08-27 07:41:13 -07001332 fClipStack = nullptr;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001333}
1334
reede8f30622016-03-23 18:59:25 -07001335sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1336 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001337}
1338
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001339
halcanary8103a342016-03-08 15:10:16 -08001340sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001341 SkTDArray<SkPDFObject*> fonts;
1342 fonts.setReserve(fFontResources.count());
1343 for (SkPDFFont* font : fFontResources) {
1344 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001345 }
halcanary8103a342016-03-08 15:10:16 -08001346 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001347 &fGraphicStateResources,
1348 &fShaderResources,
1349 &fXObjectResources,
1350 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001351}
1352
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001353const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1354 return fFontResources;
1355}
1356
halcanary8103a342016-03-08 15:10:16 -08001357sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001358 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001359 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001360 mediaBox->appendInt(0);
1361 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001362 mediaBox->appendInt(fPageSize.width());
1363 mediaBox->appendInt(fPageSize.height());
1364 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001365}
1366
mtklein5f939ab2016-03-16 10:28:35 -07001367std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001368 SkDynamicMemoryWStream buffer;
1369 this->writeContent(&buffer);
mtklein5f939ab2016-03-16 10:28:35 -07001370 return std::unique_ptr<SkStreamAsset>(
halcanary8103a342016-03-08 15:10:16 -08001371 buffer.bytesWritten() > 0
1372 ? buffer.detachAsStream()
1373 : new SkMemoryStream);
reed@google.com5667afc2011-06-27 14:42:15 +00001374}
1375
halcanary334fcbc2015-02-24 12:56:16 -08001376void SkPDFDevice::writeContent(SkWStream* out) const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001377 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanary334fcbc2015-02-24 12:56:16 -08001378 SkPDFUtils::AppendTransform(fInitialTransform, out);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001379 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001380
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001381 // If the content area is the entire page, then we don't need to clip
1382 // the content area (PDF area clips to the page size). Otherwise,
1383 // we have to clip to the content area; we've already applied the
1384 // initial transform, so just clip to the device size.
1385 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001386 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1387 SkIntToScalar(this->height()));
halcanary96fcdcc2015-08-27 07:41:13 -07001388 emit_clip(nullptr, &r, out);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001389 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001390
halcanary2be7e012016-03-28 11:58:08 -07001391 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, out);
1392 for (const auto& entry : fContentEntries) {
1393 SkPoint translation;
1394 translation.iset(this->getOrigin());
1395 translation.negate();
1396 gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion,
1397 translation);
1398 gsState.updateMatrix(entry.fState.fMatrix);
1399 gsState.updateDrawingState(entry.fState);
1400
1401 entry.fContent.writeToStream(out);
1402 }
1403 gsState.drainStack();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001404}
1405
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001406/* Draws an inverse filled path by using Path Ops to compute the positive
1407 * inverse using the current clip as the inverse bounds.
1408 * Return true if this was an inverse path and was properly handled,
1409 * otherwise returns false and the normal drawing routine should continue,
1410 * either as a (incorrect) fallback or because the path was not inverse
1411 * in the first place.
1412 */
1413bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001414 const SkPaint& paint, bool pathIsMutable,
1415 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001416 if (!origPath.isInverseFillType()) {
1417 return false;
1418 }
1419
reed1e7f5e72016-04-27 07:49:17 -07001420 if (d.fRC->isEmpty()) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001421 return false;
1422 }
1423
1424 SkPath modifiedPath;
1425 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1426 SkPaint noInversePaint(paint);
1427
1428 // Merge stroking operations into final path.
1429 if (SkPaint::kStroke_Style == paint.getStyle() ||
1430 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1431 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1432 if (doFillPath) {
1433 noInversePaint.setStyle(SkPaint::kFill_Style);
1434 noInversePaint.setStrokeWidth(0);
1435 pathPtr = &modifiedPath;
1436 } else {
1437 // To be consistent with the raster output, hairline strokes
1438 // are rendered as non-inverted.
1439 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001440 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001441 return true;
1442 }
1443 }
1444
1445 // Get bounds of clip in current transform space
1446 // (clip bounds are given in device space).
1447 SkRect bounds;
1448 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001449 SkMatrix totalMatrix = *d.fMatrix;
1450 if (prePathMatrix) {
1451 totalMatrix.preConcat(*prePathMatrix);
1452 }
1453 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001454 return false;
1455 }
reed1e7f5e72016-04-27 07:49:17 -07001456 bounds.set(d.fRC->getBounds());
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001457 transformInverse.mapRect(&bounds);
1458
1459 // Extend the bounds by the line width (plus some padding)
1460 // so the edge doesn't cause a visible stroke.
1461 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1462 paint.getStrokeWidth() + SK_Scalar1);
1463
1464 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1465 return false;
1466 }
1467
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001468 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001469 return true;
1470}
1471
reedf70b5312016-03-04 16:36:20 -08001472void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001473 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001474 const char key[], SkData* value) {
1475 if (!value) {
1476 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001477 }
reedf70b5312016-03-04 16:36:20 -08001478
1479 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1480 SkPoint transformedPoint;
1481 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1482 fNamedDestinations.emplace_back(value, transformedPoint);
1483 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001484}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001485
reedf70b5312016-03-04 16:36:20 -08001486void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001487 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001488 const char key[], SkData* value) {
1489 if (!value) {
1490 return;
1491 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001492
1493 SkPath transformedPath = path;
1494 transformedPath.transform(*d.fMatrix);
1495 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001496 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1497 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001498 SkRect transformedRect = SkRect::Make(clip.getBounds());
1499
reedf70b5312016-03-04 16:36:20 -08001500 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001501 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001502 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001503 }
reedf70b5312016-03-04 16:36:20 -08001504 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001505 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001506 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001507 }
reed16108352016-03-03 09:14:36 -08001508 }
halcanary438de492015-04-28 06:21:01 -07001509}
1510
wangxianzhuef6c50a2015-09-17 20:38:02 -07001511void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1512 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001513 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001514 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001515 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001516 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001517 }
halcanary91fcb3e2016-03-04 13:53:22 -08001518 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001519 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001520 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001521 array->appendObject(
1522 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001523 }
1524}
epoger@google.comb58772f2013-03-08 09:09:10 +00001525
halcanary6d622702015-03-25 08:45:42 -07001526void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001527 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001528 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001529 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001530 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001531 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001532 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001533 pdfDest->appendScalar(p.x());
1534 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001535 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001536 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001537 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001538 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001539}
1540
reed@google.comfc641d02012-09-20 17:52:20 +00001541SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
halcanary385fe4d2015-08-26 13:07:48 -07001542 SkPDFFormXObject* xobject = new SkPDFFormXObject(this);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001543 // We always draw the form xobjects that we create back into the device, so
1544 // we simply preserve the font usage instead of pulling it out and merging
1545 // it back in later.
halcanary3c35fb32016-06-30 11:55:07 -07001546 cleanUp(); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001547 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001548 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001549}
1550
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001551void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
1552 SkPDFFormXObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001553 const SkClipStack* clipStack,
1554 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001555 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001556 bool invertClip) {
1557 if (clipRegion.isEmpty() && !invertClip) {
1558 return;
1559 }
1560
halcanary1437c1e2016-03-13 18:30:24 -07001561 auto sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanary989da4a2016-03-21 14:33:17 -07001562 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001563
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001564 SkMatrix identity;
1565 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001566 SkPaint paint;
1567 paint.setXfermodeMode(mode);
1568 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001569 if (!content.entry()) {
1570 return;
1571 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001572 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001573 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001574 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001575
halcanary1437c1e2016-03-13 18:30:24 -07001576 // Call makeNoSmaskGraphicState() instead of
1577 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1578 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001579 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001580 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001581 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001582}
1583
halcanary2be7e012016-03-28 11:58:08 -07001584SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001585 const SkRegion& clipRegion,
1586 const SkMatrix& matrix,
1587 const SkPaint& paint,
1588 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001589 SkPDFFormXObject** dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001590 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001591 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001592 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001593 }
1594
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001595 // The clip stack can come from an SkDraw where it is technically optional.
1596 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001597 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001598 if (clipRegion == fExistingClipRegion) {
1599 clipStack = &fExistingClipStack;
1600 } else {
1601 // GraphicStackState::updateClip expects the clip stack to have
1602 // fExistingClip as a prefix, so start there, then set the clip
1603 // to the passed region.
1604 synthesizedClipStack = fExistingClipStack;
1605 SkPath clipPath;
1606 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001607 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1608 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001609 clipStack = &synthesizedClipStack;
1610 }
1611 }
1612
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001613 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1614 if (paint.getXfermode()) {
1615 paint.getXfermode()->asMode(&xfermode);
1616 }
1617
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001618 // For the following modes, we want to handle source and destination
1619 // separately, so make an object of what's already there.
1620 if (xfermode == SkXfermode::kClear_Mode ||
1621 xfermode == SkXfermode::kSrc_Mode ||
1622 xfermode == SkXfermode::kSrcIn_Mode ||
1623 xfermode == SkXfermode::kDstIn_Mode ||
1624 xfermode == SkXfermode::kSrcOut_Mode ||
1625 xfermode == SkXfermode::kDstOut_Mode ||
1626 xfermode == SkXfermode::kSrcATop_Mode ||
1627 xfermode == SkXfermode::kDstATop_Mode ||
1628 xfermode == SkXfermode::kModulate_Mode) {
1629 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001630 *dst = createFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001631 SkASSERT(isContentEmpty());
1632 } else if (xfermode != SkXfermode::kSrc_Mode &&
1633 xfermode != SkXfermode::kSrcOut_Mode) {
1634 // Except for Src and SrcOut, if there isn't anything already there,
1635 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001636 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001637 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001638 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001639 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001640 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001641
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001642 // Dst xfer mode doesn't draw source at all.
1643 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001644 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001645 }
1646
halcanary2be7e012016-03-28 11:58:08 -07001647 SkPDFDevice::ContentEntry* entry;
1648 if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) {
1649 entry = fContentEntries.back();
1650 } else if (xfermode != SkXfermode::kDstOver_Mode) {
1651 entry = fContentEntries.emplace_back();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001652 } else {
halcanary2be7e012016-03-28 11:58:08 -07001653 entry = fContentEntries.emplace_front();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001654 }
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001655 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001656 hasText, &entry->fState);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001657 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001658}
1659
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001660void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001661 SkPDFFormXObject* dst,
1662 SkPath* shape) {
1663 if (xfermode != SkXfermode::kClear_Mode &&
1664 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001665 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001666 xfermode != SkXfermode::kSrcIn_Mode &&
1667 xfermode != SkXfermode::kDstIn_Mode &&
1668 xfermode != SkXfermode::kSrcOut_Mode &&
1669 xfermode != SkXfermode::kDstOut_Mode &&
1670 xfermode != SkXfermode::kSrcATop_Mode &&
1671 xfermode != SkXfermode::kDstATop_Mode &&
1672 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001673 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001674 return;
1675 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001676 if (xfermode == SkXfermode::kDstOver_Mode) {
1677 SkASSERT(!dst);
halcanary2be7e012016-03-28 11:58:08 -07001678 if (fContentEntries.front()->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001679 // For DstOver, an empty content entry was inserted before the rest
1680 // of the content entries. If nothing was drawn, it needs to be
1681 // removed.
halcanary2be7e012016-03-28 11:58:08 -07001682 fContentEntries.pop_front();
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001683 }
1684 return;
1685 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001686 if (!dst) {
1687 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1688 xfermode == SkXfermode::kSrcOut_Mode);
1689 return;
1690 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001691
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001692 SkASSERT(dst);
halcanary2be7e012016-03-28 11:58:08 -07001693 SkASSERT(fContentEntries.count() == 1);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001694 // Changing the current content into a form-xobject will destroy the clip
1695 // objects which is fine since the xobject will already be clipped. However
1696 // if source has shape, we need to clip it too, so a copy of the clip is
1697 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001698
1699 SkClipStack clipStack = fContentEntries.front()->fState.fClipStack;
1700 SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001701
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001702 SkMatrix identity;
1703 identity.reset();
1704 SkPaint stockPaint;
1705
halcanary48810a02016-03-07 14:57:50 -08001706 sk_sp<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001707 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001708 // If nothing was drawn and there's no shape, then the draw was a
1709 // no-op, but dst needs to be restored for that to be true.
1710 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1711 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1712 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001713 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001714 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001715 ScopedContentEntry content(this, &fExistingClipStack,
1716 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001717 stockPaint);
1718 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1719 &content.entry()->fContent);
1720 return;
1721 } else {
1722 xfermode = SkXfermode::kClear_Mode;
1723 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001724 } else {
halcanary2be7e012016-03-28 11:58:08 -07001725 SkASSERT(fContentEntries.count() == 1);
reed@google.comfc641d02012-09-20 17:52:20 +00001726 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001727 }
1728
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001729 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1730 // without alpha.
1731 if (xfermode == SkXfermode::kSrcATop_Mode) {
1732 // TODO(vandebo): In order to properly support SrcATop we have to track
1733 // the shape of what's been drawn at all times. It's the intersection of
1734 // the non-transparent parts of the device and the outlines (shape) of
1735 // all images and devices drawn.
1736 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001737 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001738 SkXfermode::kSrcOver_Mode, true);
1739 } else {
halcanary48810a02016-03-07 14:57:50 -08001740 sk_sp<SkPDFFormXObject> dstMaskStorage;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001741 SkPDFFormXObject* dstMask = srcFormXObject.get();
halcanary96fcdcc2015-08-27 07:41:13 -07001742 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001743 // Draw shape into a form-xobject.
reed1e7f5e72016-04-27 07:49:17 -07001744 SkRasterClip rc(clipRegion);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001745 SkDraw d;
1746 d.fMatrix = &identity;
reed1e7f5e72016-04-27 07:49:17 -07001747 d.fRC = &rc;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001748 d.fClipStack = &clipStack;
1749 SkPaint filledPaint;
1750 filledPaint.setColor(SK_ColorBLACK);
1751 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001752 this->drawPath(d, *shape, filledPaint, nullptr, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001753
1754 dstMaskStorage.reset(createFormXObjectFromDevice());
1755 dstMask = dstMaskStorage.get();
1756 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001757 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1758 &fExistingClipStack, fExistingClipRegion,
1759 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001760 }
1761
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001762 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001763 return;
1764 } else if (xfermode == SkXfermode::kSrc_Mode ||
1765 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001766 ScopedContentEntry content(this, &fExistingClipStack,
1767 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001768 if (content.entry()) {
1769 SkPDFUtils::DrawFormXObject(
1770 this->addXObjectResource(srcFormXObject.get()),
1771 &content.entry()->fContent);
1772 }
1773 if (xfermode == SkXfermode::kSrc_Mode) {
1774 return;
1775 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001776 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001777 ScopedContentEntry content(this, &fExistingClipStack,
1778 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001779 if (content.entry()) {
1780 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1781 &content.entry()->fContent);
1782 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001783 }
1784
1785 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1786 xfermode == SkXfermode::kDstIn_Mode ||
1787 xfermode == SkXfermode::kSrcOut_Mode ||
1788 xfermode == SkXfermode::kDstOut_Mode ||
1789 xfermode == SkXfermode::kSrcATop_Mode ||
1790 xfermode == SkXfermode::kDstATop_Mode ||
1791 xfermode == SkXfermode::kModulate_Mode);
1792
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001793 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001794 xfermode == SkXfermode::kSrcOut_Mode ||
1795 xfermode == SkXfermode::kSrcATop_Mode) {
1796 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001797 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001798 SkXfermode::kSrcOver_Mode,
1799 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001800 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001801 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
1802 if (xfermode == SkXfermode::kModulate_Mode) {
1803 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001804 dst, &fExistingClipStack,
1805 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001806 SkXfermode::kSrcOver_Mode, false);
1807 mode = SkXfermode::kMultiply_Mode;
1808 }
1809 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001810 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001811 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001812 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001813}
1814
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001815bool SkPDFDevice::isContentEmpty() {
halcanary2be7e012016-03-28 11:58:08 -07001816 if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) {
1817 SkASSERT(fContentEntries.count() <= 1);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001818 return true;
1819 }
1820 return false;
1821}
1822
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001823void SkPDFDevice::populateGraphicStateEntryFromPaint(
1824 const SkMatrix& matrix,
1825 const SkClipStack& clipStack,
1826 const SkRegion& clipRegion,
1827 const SkPaint& paint,
1828 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001829 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001830 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1831 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1832 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001833
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001834 entry->fMatrix = matrix;
1835 entry->fClipStack = clipStack;
1836 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001837 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1838 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001839
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001840 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001841 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001842 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001843 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001844 if (shader) {
1845 // PDF positions patterns relative to the initial transform, so
1846 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001847 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001848 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001849
1850 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001851 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001852 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001853
1854 // We need to apply the initial transform to bounds in order to get
1855 // bounds in a consistent coordinate system.
1856 SkRect boundsTemp;
1857 boundsTemp.set(bounds);
1858 fInitialTransform.mapRect(&boundsTemp);
1859 boundsTemp.roundOut(&bounds);
1860
halcanary792c80f2015-02-20 07:21:05 -08001861 SkScalar rasterScale =
1862 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
1863 pdfShader.reset(SkPDFShader::GetPDFShader(
reedfe630452016-03-25 09:08:00 -07001864 fDocument, fRasterDpi, shader, transform, bounds, rasterScale));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001865
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001866 if (pdfShader.get()) {
1867 // pdfShader has been canonicalized so we can directly compare
1868 // pointers.
1869 int resourceIndex = fShaderResources.find(pdfShader.get());
1870 if (resourceIndex < 0) {
1871 resourceIndex = fShaderResources.count();
1872 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001873 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001874 }
1875 entry->fShaderIndex = resourceIndex;
1876 } else {
1877 // A color shader is treated as an invalid shader so we don't have
1878 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001879 SkShader::GradientInfo gradientInfo;
1880 SkColor gradientColor;
1881 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001882 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001883 gradientInfo.fColorCount = 1;
1884 if (shader->asAGradient(&gradientInfo) ==
1885 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001886 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1887 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001888 }
1889 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001890 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001891
halcanary48810a02016-03-07 14:57:50 -08001892 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001893 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001894 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001895 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001896 } else {
1897 SkPaint newPaint = paint;
1898 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001899 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001900 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001901 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001902 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001903 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001904
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001905 if (hasText) {
1906 entry->fTextScaleX = paint.getTextScaleX();
1907 entry->fTextFill = paint.getStyle();
1908 } else {
1909 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001910 }
1911}
1912
halcanarybe27a112015-04-01 13:31:19 -07001913int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001914 // Assumes that gs has been canonicalized (so we can directly compare
1915 // pointers).
1916 int result = fGraphicStateResources.find(gs);
1917 if (result < 0) {
1918 result = fGraphicStateResources.count();
1919 fGraphicStateResources.push(gs);
1920 gs->ref();
1921 }
1922 return result;
1923}
1924
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001925int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
1926 // Assumes that xobject has been canonicalized (so we can directly compare
1927 // pointers).
1928 int result = fXObjectResources.find(xObject);
1929 if (result < 0) {
1930 result = fXObjectResources.count();
1931 fXObjectResources.push(xObject);
1932 xObject->ref();
1933 }
1934 return result;
1935}
1936
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001937void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
halcanary2be7e012016-03-28 11:58:08 -07001938 SkPDFDevice::ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001939 SkTypeface* typeface = paint.getTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -07001940 if (contentEntry->fState.fFont == nullptr ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001941 contentEntry->fState.fTextSize != paint.getTextSize() ||
1942 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001943 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001944 contentEntry->fContent.writeText("/");
1945 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
1946 SkPDFResourceDict::kFont_ResourceType,
1947 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001948 contentEntry->fContent.writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -07001949 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001950 contentEntry->fContent.writeText(" Tf\n");
1951 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001952 }
1953}
1954
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001955int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08001956 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07001957 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001958 int resourceIndex = fFontResources.find(newFont.get());
1959 if (resourceIndex < 0) {
1960 resourceIndex = fFontResources.count();
1961 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001962 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001963 }
1964 return resourceIndex;
1965}
1966
halcanary7a14b312015-10-01 07:28:13 -07001967static SkSize rect_to_size(const SkRect& r) {
1968 return SkSize::Make(r.width(), r.height());
1969}
1970
halcanarya50151d2016-03-25 11:57:49 -07001971static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
1972 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07001973 auto surface =
halcanarya50151d2016-03-25 11:57:49 -07001974 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
1975 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07001976 SkCanvas* canvas = surface->getCanvas();
1977 canvas->clear(SK_ColorTRANSPARENT);
1978 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07001979 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07001980 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07001981 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07001982 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07001983}
1984
1985////////////////////////////////////////////////////////////////////////////////
1986void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
1987 const SkClipStack* clipStack,
1988 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07001989 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07001990 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07001991 if (imageBitmap.dimensions().isZero()) {
1992 return;
1993 }
halcanary7a14b312015-10-01 07:28:13 -07001994 #ifdef SK_PDF_IMAGE_STATS
1995 gDrawImageCalls.fetch_add(1);
1996 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00001997 SkMatrix matrix = origMatrix;
1998 SkRegion perspectiveBounds;
1999 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07002000 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002001
2002 // Rasterize the bitmap using perspective in a new bitmap.
2003 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002004 if (fRasterDpi == 0) {
2005 return;
2006 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002007 // Transform the bitmap in the new space, without taking into
2008 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002009 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07002010 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07002011 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002012 perspectiveOutline.transform(origMatrix);
2013
2014 // TODO(edisonn): perf - use current clip too.
2015 // Retrieve the bounds of the new shape.
2016 SkRect bounds = perspectiveOutline.getBounds();
2017
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002018 // Transform the bitmap in the new space, taking into
2019 // account the initial transform.
2020 SkMatrix total = origMatrix;
2021 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07002022 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
2023 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2024 total.postScale(dpiScale, dpiScale);
2025
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002026 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002027 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002028 physicalPerspectiveOutline.transform(total);
2029
halcanary7a14b312015-10-01 07:28:13 -07002030 SkRect physicalPerspectiveBounds =
2031 physicalPerspectiveOutline.getBounds();
2032 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2033 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002034
2035 // TODO(edisonn): A better approach would be to use a bitmap shader
2036 // (in clamp mode) and draw a rect over the entire bounding box. Then
2037 // intersect perspectiveOutline to the clip. That will avoid introducing
2038 // alpha to the image while still giving good behavior at the edge of
2039 // the image. Avoiding alpha will reduce the pdf size and generation
2040 // CPU time some.
2041
halcanary7a14b312015-10-01 07:28:13 -07002042 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2043
reede8f30622016-03-23 18:59:25 -07002044 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002045 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002046 return;
2047 }
halcanary7a14b312015-10-01 07:28:13 -07002048 SkCanvas* canvas = surface->getCanvas();
2049 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002050
2051 SkScalar deltaX = bounds.left();
2052 SkScalar deltaY = bounds.top();
2053
2054 SkMatrix offsetMatrix = origMatrix;
2055 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002056 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002057
2058 // Translate the draw in the new canvas, so we perfectly fit the
2059 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002060 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002061 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002062 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002063 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002064
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002065 // In the new space, we use the identity matrix translated
2066 // and scaled to reflect DPI.
2067 matrix.setScale(1 / scaleX, 1 / scaleY);
2068 matrix.postTranslate(deltaX, deltaY);
2069
halcanary7a14b312015-10-01 07:28:13 -07002070 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002071 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002072
halcanarya50151d2016-03-25 11:57:49 -07002073 autoImageUnref = surface->makeImageSnapshot();
2074 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002075 }
2076
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002077 SkMatrix scaled;
2078 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002079 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2080 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002081 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002082 SkIRect subset = imageBitmap.bounds();
2083 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2084 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002085 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002086 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002087 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002088 return;
2089 }
2090 if (content.needShape()) {
2091 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002092 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002093 shape.transform(matrix);
2094 content.setShape(shape);
2095 }
2096 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002097 return;
2098 }
2099
halcanary287d22d2015-09-24 10:20:05 -07002100 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002101 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002102 // draw calls. This code here works for all
2103 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2104 // rasterize a layer on this backend). Fortuanely, this seems
2105 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002106 autoImageUnref = color_filter(imageBitmap, colorFilter);
2107 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002108 // TODO(halcanary): de-dupe this by caching filtered images.
2109 // (maybe in the resource cache?)
2110 }
halcanarya50151d2016-03-25 11:57:49 -07002111
2112 SkBitmapKey key = imageBitmap.getKey();
2113 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002114 if (!pdfimage) {
halcanarya50151d2016-03-25 11:57:49 -07002115 auto img = imageBitmap.makeImage();
2116 if (!img) {
2117 return;
2118 }
2119 pdfimage = SkPDFCreateBitmapObject(
2120 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002121 if (!pdfimage) {
2122 return;
halcanary287d22d2015-09-24 10:20:05 -07002123 }
halcanarya50151d2016-03-25 11:57:49 -07002124 fDocument->serialize(pdfimage); // serialize images early.
2125 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002126 }
halcanarya50151d2016-03-25 11:57:49 -07002127 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002128 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002129 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002130}