blob: 84959b6c1bee9fcc648850df21074ae0d2218f5e [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00006 */
7
8#include "SkPDFDevice.h"
9
reedf70b5312016-03-04 16:36:20 -080010#include "SkAnnotationKeys.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000011#include "SkColor.h"
halcanary287d22d2015-09-24 10:20:05 -070012#include "SkColorFilter.h"
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000013#include "SkClipStack.h"
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000014#include "SkDraw.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkGlyphCache.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000016#include "SkPaint.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000017#include "SkPath.h"
reeda4393342016-03-18 11:22:57 -070018#include "SkPathEffect.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000019#include "SkPathOps.h"
halcanarydb0dcc72015-03-20 12:31:52 -070020#include "SkPDFBitmap.h"
halcanary7a14b312015-10-01 07:28:13 -070021#include "SkPDFCanon.h"
halcanary989da4a2016-03-21 14:33:17 -070022#include "SkPDFDocument.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000023#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000024#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000025#include "SkPDFGraphicState.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000026#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000027#include "SkPDFShader.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000028#include "SkPDFStream.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000029#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000030#include "SkPDFUtils.h"
wangxianzhuef6c50a2015-09-17 20:38:02 -070031#include "SkRasterClip.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000032#include "SkRect.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000033#include "SkRRect.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000034#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070035#include "SkSurface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000036#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000037#include "SkTemplates.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000038#include "SkTypefacePriv.h"
halcanarya6814332015-05-27 08:53:36 -070039#include "SkXfermodeInterpretation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000040
edisonn@google.com73a7ea32013-11-11 20:55:15 +000041#define DPI_FOR_RASTER_SCALE_ONE 72
42
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000043// Utility functions
44
halcanarya6814332015-05-27 08:53:36 -070045// If the paint will definitely draw opaquely, replace kSrc_Mode with
46// kSrcOver_Mode. http://crbug.com/473572
47static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
48 if (kSrcOver_SkXfermodeInterpretation
49 == SkInterpretXfermode(*paint, false)) {
halcanary96fcdcc2015-08-27 07:41:13 -070050 paint->setXfermode(nullptr);
halcanarya6814332015-05-27 08:53:36 -070051 }
52}
53
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000054static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000055 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
reed80ea19c2015-05-12 10:37:34 -070056 SkScalar colorScale = SkScalarInvert(0xFF);
57 SkPDFUtils::AppendScalar(SkColorGetR(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000058 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070059 SkPDFUtils::AppendScalar(SkColorGetG(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000060 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070061 SkPDFUtils::AppendScalar(SkColorGetB(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000062 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000063}
64
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000065static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000066 SkPaint result = paint;
67 if (result.isFakeBoldText()) {
68 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
69 kStdFakeBoldInterpKeys,
70 kStdFakeBoldInterpValues,
71 kStdFakeBoldInterpLength);
72 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000073 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000074 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000075 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000076 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000077 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000078 result.setStrokeWidth(width);
79 }
80 return result;
81}
82
83// Stolen from measure_text in SkDraw.cpp and then tweaked.
benjaminwagnerd936f632016-02-23 10:44:31 -080084static void align_text(SkPaint::GlyphCacheProc glyphCacheProc, const SkPaint& paint,
bungeman@google.com9a87cee2011-08-23 17:02:18 +000085 const uint16_t* glyphs, size_t len,
86 SkScalar* x, SkScalar* y) {
87 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000088 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000089 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000090
91 SkMatrix ident;
92 ident.reset();
halcanary96fcdcc2015-08-27 07:41:13 -070093 SkAutoGlyphCache autoCache(paint, nullptr, &ident);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000094 SkGlyphCache* cache = autoCache.getCache();
95
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +000096 const char* start = reinterpret_cast<const char*>(glyphs);
97 const char* stop = reinterpret_cast<const char*>(glyphs + len);
benjaminwagner6b3eacb2016-03-24 19:07:58 -070098 SkScalar xAdv = 0, yAdv = 0;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000099
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000100 // TODO(vandebo): This probably needs to take kerning into account.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000101 while (start < stop) {
benjaminwagnerd936f632016-02-23 10:44:31 -0800102 const SkGlyph& glyph = glyphCacheProc(cache, &start);
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700103 xAdv += SkFloatToScalar(glyph.fAdvanceX);
104 yAdv += SkFloatToScalar(glyph.fAdvanceY);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000105 };
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000106 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000107 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000108 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000109
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000110 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700111 xAdv = SkScalarHalf(xAdv);
112 yAdv = SkScalarHalf(yAdv);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000113 }
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700114 *x = *x - xAdv;
115 *y = *y - yAdv;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000116}
117
robertphillips@google.coma4662862013-11-21 14:24:16 +0000118static int max_glyphid_for_typeface(SkTypeface* typeface) {
reed@google.comfed86bd2013-03-14 15:04:57 +0000119 SkAutoResolveDefaultTypeface autoResolve(typeface);
120 typeface = autoResolve.get();
commit-bot@chromium.org6a4ba5b2013-07-17 21:55:08 +0000121 return typeface->countGlyphs() - 1;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000122}
123
124typedef SkAutoSTMalloc<128, uint16_t> SkGlyphStorage;
125
reed@google.comaec40662014-04-18 19:29:07 +0000126static int force_glyph_encoding(const SkPaint& paint, const void* text,
127 size_t len, SkGlyphStorage* storage,
bungeman22edc832014-10-03 07:55:58 -0700128 const uint16_t** glyphIDs) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000129 // Make sure we have a glyph id encoding.
130 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
halcanary96fcdcc2015-08-27 07:41:13 -0700131 int numGlyphs = paint.textToGlyphs(text, len, nullptr);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000132 storage->reset(numGlyphs);
133 paint.textToGlyphs(text, len, storage->get());
134 *glyphIDs = storage->get();
135 return numGlyphs;
136 }
137
138 // For user supplied glyph ids we need to validate them.
139 SkASSERT((len & 1) == 0);
reed@google.comaec40662014-04-18 19:29:07 +0000140 int numGlyphs = SkToInt(len / 2);
bungeman22edc832014-10-03 07:55:58 -0700141 const uint16_t* input = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000142
143 int maxGlyphID = max_glyphid_for_typeface(paint.getTypeface());
reed@google.comaec40662014-04-18 19:29:07 +0000144 int validated;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000145 for (validated = 0; validated < numGlyphs; ++validated) {
146 if (input[validated] > maxGlyphID) {
147 break;
148 }
149 }
150 if (validated >= numGlyphs) {
bungeman22edc832014-10-03 07:55:58 -0700151 *glyphIDs = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000152 return numGlyphs;
153 }
154
155 // Silently drop anything out of range.
156 storage->reset(numGlyphs);
157 if (validated > 0) {
158 memcpy(storage->get(), input, validated * sizeof(uint16_t));
159 }
160
reed@google.comaec40662014-04-18 19:29:07 +0000161 for (int i = validated; i < numGlyphs; ++i) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000162 storage->get()[i] = input[i];
163 if (input[i] > maxGlyphID) {
164 storage->get()[i] = 0;
165 }
166 }
167 *glyphIDs = storage->get();
168 return numGlyphs;
169}
170
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000171static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX,
172 SkWStream* content) {
173 // Flip the text about the x-axis to account for origin swap and include
174 // the passed parameters.
175 content->writeText("1 0 ");
halcanarybc4696b2015-05-06 10:56:04 -0700176 SkPDFUtils::AppendScalar(0 - textSkewX, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000177 content->writeText(" -1 ");
halcanarybc4696b2015-05-06 10:56:04 -0700178 SkPDFUtils::AppendScalar(x, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000179 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700180 SkPDFUtils::AppendScalar(y, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000181 content->writeText(" Tm\n");
182}
183
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000184// It is important to not confuse GraphicStateEntry with SkPDFGraphicState, the
185// later being our representation of an object in the PDF file.
186struct GraphicStateEntry {
187 GraphicStateEntry();
188
189 // Compare the fields we care about when setting up a new content entry.
190 bool compareInitialState(const GraphicStateEntry& b);
191
192 SkMatrix fMatrix;
193 // We can't do set operations on Paths, though PDF natively supports
194 // intersect. If the clip stack does anything other than intersect,
195 // we have to fall back to the region. Treat fClipStack as authoritative.
196 // See http://code.google.com/p/skia/issues/detail?id=221
197 SkClipStack fClipStack;
198 SkRegion fClipRegion;
199
200 // When emitting the content entry, we will ensure the graphic state
201 // is set to these values first.
202 SkColor fColor;
203 SkScalar fTextScaleX; // Zero means we don't care what the value is.
204 SkPaint::Style fTextFill; // Only if TextScaleX is non-zero.
205 int fShaderIndex;
206 int fGraphicStateIndex;
207
208 // We may change the font (i.e. for Type1 support) within a
halcanary96fcdcc2015-08-27 07:41:13 -0700209 // ContentEntry. This is the one currently in effect, or nullptr if none.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000210 SkPDFFont* fFont;
211 // In PDF, text size has no default value. It is only valid if fFont is
halcanary96fcdcc2015-08-27 07:41:13 -0700212 // not nullptr.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000213 SkScalar fTextSize;
214};
215
216GraphicStateEntry::GraphicStateEntry() : fColor(SK_ColorBLACK),
217 fTextScaleX(SK_Scalar1),
218 fTextFill(SkPaint::kFill_Style),
219 fShaderIndex(-1),
220 fGraphicStateIndex(-1),
halcanary96fcdcc2015-08-27 07:41:13 -0700221 fFont(nullptr),
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000222 fTextSize(SK_ScalarNaN) {
223 fMatrix.reset();
224}
225
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +0000226bool GraphicStateEntry::compareInitialState(const GraphicStateEntry& cur) {
227 return fColor == cur.fColor &&
228 fShaderIndex == cur.fShaderIndex &&
229 fGraphicStateIndex == cur.fGraphicStateIndex &&
230 fMatrix == cur.fMatrix &&
231 fClipStack == cur.fClipStack &&
232 (fTextScaleX == 0 ||
233 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000234}
235
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000236class GraphicStackState {
237public:
238 GraphicStackState(const SkClipStack& existingClipStack,
239 const SkRegion& existingClipRegion,
240 SkWStream* contentStream)
241 : fStackDepth(0),
242 fContentStream(contentStream) {
243 fEntries[0].fClipStack = existingClipStack;
244 fEntries[0].fClipRegion = existingClipRegion;
245 }
246
247 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000248 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000249 void updateMatrix(const SkMatrix& matrix);
250 void updateDrawingState(const GraphicStateEntry& state);
251
252 void drainStack();
253
254private:
255 void push();
256 void pop();
257 GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
258
259 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
260 static const int kMaxStackDepth = 12;
261 GraphicStateEntry fEntries[kMaxStackDepth + 1];
262 int fStackDepth;
263 SkWStream* fContentStream;
264};
265
266void GraphicStackState::drainStack() {
267 while (fStackDepth) {
268 pop();
269 }
270}
271
272void GraphicStackState::push() {
273 SkASSERT(fStackDepth < kMaxStackDepth);
274 fContentStream->writeText("q\n");
275 fStackDepth++;
276 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
277}
278
279void GraphicStackState::pop() {
280 SkASSERT(fStackDepth > 0);
281 fContentStream->writeText("Q\n");
282 fStackDepth--;
283}
284
robertphillips@google.com80214e22012-07-20 15:33:18 +0000285// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000286// and then skips over the leading entries as specified in prefix. It requires
287// and asserts that "prefix" will be a prefix to "stack."
288static void skip_clip_stack_prefix(const SkClipStack& prefix,
289 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000290 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000291 SkClipStack::B2TIter prefixIter(prefix);
292 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000293
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000294 const SkClipStack::Element* prefixEntry;
295 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000296
297 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000298 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000299 iterEntry = iter->next();
300 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000301 // Because of SkClipStack does internal intersection, the last clip
302 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000303 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000304 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
305 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
306 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000307 // back up the iterator by one
308 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000309 prefixEntry = prefixIter.next();
310 break;
311 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000312 }
313
halcanary96fcdcc2015-08-27 07:41:13 -0700314 SkASSERT(prefixEntry == nullptr);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000315}
316
317static void emit_clip(SkPath* clipPath, SkRect* clipRect,
318 SkWStream* contentStream) {
319 SkASSERT(clipPath || clipRect);
320
321 SkPath::FillType clipFill;
322 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000323 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000324 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000325 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000326 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
327 clipFill = SkPath::kWinding_FillType;
328 }
329
330 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
331 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
332 if (clipFill == SkPath::kEvenOdd_FillType) {
333 contentStream->writeText("W* n\n");
334 } else {
335 contentStream->writeText("W n\n");
336 }
337}
338
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000339/* Calculate an inverted path's equivalent non-inverted path, given the
340 * canvas bounds.
341 * outPath may alias with invPath (since this is supported by PathOps).
342 */
343static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
344 SkPath* outPath) {
345 SkASSERT(invPath.isInverseFillType());
346
347 SkPath clipPath;
348 clipPath.addRect(bounds);
349
reedcdb42bb2015-06-26 10:23:07 -0700350 return Op(clipPath, invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000351}
352
fmalita632e92f2015-04-22 15:02:03 -0700353#ifdef SK_PDF_USE_PATHOPS_CLIPPING
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000354// Sanity check the numerical values of the SkRegion ops and PathOps ops
355// enums so region_op_to_pathops_op can do a straight passthrough cast.
356// If these are failing, it may be necessary to make region_op_to_pathops_op
357// do more.
bungeman99fe8222015-08-20 07:57:51 -0700358static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
359static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
360static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
361static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
362static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
363 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000364
365static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
366 SkASSERT(op >= 0);
367 SkASSERT(op <= SkRegion::kReverseDifference_Op);
368 return (SkPathOp)op;
369}
370
371/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
372 * Returns true if successful, or false if not successful.
373 * If successful, the resulting clip is stored in outClipPath.
374 * If not successful, outClipPath is undefined, and a fallback method
375 * should be used.
376 */
377static bool get_clip_stack_path(const SkMatrix& transform,
378 const SkClipStack& clipStack,
379 const SkRegion& clipRegion,
380 SkPath* outClipPath) {
381 outClipPath->reset();
382 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
383
384 const SkClipStack::Element* clipEntry;
385 SkClipStack::Iter iter;
386 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
387 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
388 SkPath entryPath;
389 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
390 outClipPath->reset();
391 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
392 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000393 } else {
394 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000395 }
396 entryPath.transform(transform);
397
398 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
399 *outClipPath = entryPath;
400 } else {
401 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
402 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
403 return false;
404 }
405 }
406 }
407
408 if (outClipPath->isInverseFillType()) {
409 // The bounds are slightly outset to ensure this is correct in the
410 // face of floating-point accuracy and possible SkRegion bitmap
411 // approximations.
412 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
413 clipBounds.outset(SK_Scalar1, SK_Scalar1);
414 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
415 return false;
416 }
417 }
418 return true;
419}
420#endif
421
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000422// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000423// graphic state stack, and the fact that we can know all the clips used
424// on the page to optimize this.
425void GraphicStackState::updateClip(const SkClipStack& clipStack,
426 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000427 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000428 if (clipStack == currentEntry()->fClipStack) {
429 return;
430 }
431
432 while (fStackDepth > 0) {
433 pop();
434 if (clipStack == currentEntry()->fClipStack) {
435 return;
436 }
437 }
438 push();
439
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000440 currentEntry()->fClipStack = clipStack;
441 currentEntry()->fClipRegion = clipRegion;
442
443 SkMatrix transform;
444 transform.setTranslate(translation.fX, translation.fY);
445
fmalita632e92f2015-04-22 15:02:03 -0700446#ifdef SK_PDF_USE_PATHOPS_CLIPPING
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000447 SkPath clipPath;
448 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700449 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000450 return;
451 }
452#endif
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000453 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
454 // already been applied. (If this is a top level device, then it specifies
455 // a clip to the content area. If this is a layer, then it specifies
456 // the clip in effect when the layer was created.) There's no need to
457 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
458 // initial clip on the parent layer. (This means there's a bug if the user
459 // expands the clip and then uses any xfer mode that uses dst:
460 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000461 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000462 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
463
464 // If the clip stack does anything other than intersect or if it uses
465 // an inverse fill type, we have to fall back to the clip region.
466 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000467 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000468 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000469 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
470 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000471 needRegion = true;
472 break;
473 }
474 }
475
476 if (needRegion) {
477 SkPath clipPath;
478 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700479 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000480 } else {
481 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000482 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000483 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000484 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
485 switch (clipEntry->getType()) {
486 case SkClipStack::Element::kRect_Type: {
487 SkRect translatedClip;
488 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700489 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000490 break;
491 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000492 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000493 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000494 clipEntry->asPath(&translatedPath);
495 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700496 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000497 break;
498 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000499 }
500 }
501 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000502}
503
504void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
505 if (matrix == currentEntry()->fMatrix) {
506 return;
507 }
508
509 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
510 SkASSERT(fStackDepth > 0);
511 SkASSERT(fEntries[fStackDepth].fClipStack ==
512 fEntries[fStackDepth -1].fClipStack);
513 pop();
514
515 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
516 }
517 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
518 return;
519 }
520
521 push();
522 SkPDFUtils::AppendTransform(matrix, fContentStream);
523 currentEntry()->fMatrix = matrix;
524}
525
526void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) {
527 // PDF treats a shader as a color, so we only set one or the other.
528 if (state.fShaderIndex >= 0) {
529 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000530 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000531 currentEntry()->fShaderIndex = state.fShaderIndex;
532 }
533 } else {
534 if (state.fColor != currentEntry()->fColor ||
535 currentEntry()->fShaderIndex >= 0) {
536 emit_pdf_color(state.fColor, fContentStream);
537 fContentStream->writeText("RG ");
538 emit_pdf_color(state.fColor, fContentStream);
539 fContentStream->writeText("rg\n");
540 currentEntry()->fColor = state.fColor;
541 currentEntry()->fShaderIndex = -1;
542 }
543 }
544
545 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000546 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000547 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
548 }
549
550 if (state.fTextScaleX) {
551 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
552 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
553 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700554 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000555 fContentStream->writeText(" Tz\n");
556 currentEntry()->fTextScaleX = state.fTextScaleX;
557 }
558 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700559 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
560 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
561 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000562 fContentStream->writeDecAsText(state.fTextFill);
563 fContentStream->writeText(" Tr\n");
564 currentEntry()->fTextFill = state.fTextFill;
565 }
566 }
567}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000568
reed76033be2015-03-14 10:54:31 -0700569static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700570 // PDF does not support image filters, so render them on CPU.
571 // Note that this rendering is done at "screen" resolution (100dpi), not
572 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700573 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800574 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700575
576 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700577 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700578}
579
580SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
halcanary00b7e5e2015-04-15 13:05:18 -0700581 if (cinfo.fForImageFilter ||
582 (layerPaint && not_supported_for_layers(*layerPaint))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700583 return nullptr;
senorblancob0e89dc2014-10-20 14:03:12 -0700584 }
fmalita6987dca2014-11-13 08:33:37 -0800585 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700586 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000587}
588
halcanary989da4a2016-03-21 14:33:17 -0700589SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
590
bsalomon@google.come97f0852011-06-17 13:10:25 +0000591
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000592struct ContentEntry {
593 GraphicStateEntry fState;
594 SkDynamicMemoryWStream fContent;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000595 SkAutoTDelete<ContentEntry> fNext;
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000596
597 // If the stack is too deep we could get Stack Overflow.
598 // So we manually destruct the object.
599 ~ContentEntry() {
mtklein18300a32016-03-16 13:53:35 -0700600 ContentEntry* val = fNext.release();
halcanary96fcdcc2015-08-27 07:41:13 -0700601 while (val != nullptr) {
mtklein18300a32016-03-16 13:53:35 -0700602 ContentEntry* valNext = val->fNext.release();
halcanary96fcdcc2015-08-27 07:41:13 -0700603 // When the destructor is called, fNext is nullptr and exits.
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000604 delete val;
605 val = valNext;
606 }
607 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000608};
609
610// A helper class to automatically finish a ContentEntry at the end of a
611// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000612class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000613public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000614 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
615 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000616 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700617 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000618 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700619 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000620 init(draw.fClipStack, *draw.fClip, *draw.fMatrix, paint, hasText);
621 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000622 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
623 const SkRegion& clipRegion, const SkMatrix& matrix,
624 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000625 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700626 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000627 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700628 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000629 init(clipStack, clipRegion, matrix, paint, hasText);
630 }
631
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000632 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000633 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000634 SkPath* shape = &fShape;
635 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700636 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000637 }
638 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000639 }
reed@google.comfc641d02012-09-20 17:52:20 +0000640 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000641 }
642
643 ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000644
645 /* Returns true when we explicitly need the shape of the drawing. */
646 bool needShape() {
647 switch (fXfermode) {
648 case SkXfermode::kClear_Mode:
649 case SkXfermode::kSrc_Mode:
650 case SkXfermode::kSrcIn_Mode:
651 case SkXfermode::kSrcOut_Mode:
652 case SkXfermode::kDstIn_Mode:
653 case SkXfermode::kDstOut_Mode:
654 case SkXfermode::kSrcATop_Mode:
655 case SkXfermode::kDstATop_Mode:
656 case SkXfermode::kModulate_Mode:
657 return true;
658 default:
659 return false;
660 }
661 }
662
663 /* Returns true unless we only need the shape of the drawing. */
664 bool needSource() {
665 if (fXfermode == SkXfermode::kClear_Mode) {
666 return false;
667 }
668 return true;
669 }
670
671 /* If the shape is different than the alpha component of the content, then
672 * setShape should be called with the shape. In particular, images and
673 * devices have rectangular shape.
674 */
675 void setShape(const SkPath& shape) {
676 fShape = shape;
677 }
678
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000679private:
680 SkPDFDevice* fDevice;
681 ContentEntry* fContentEntry;
682 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000683 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000684 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000685
686 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
687 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000688 // Shape has to be flatten before we get here.
689 if (matrix.hasPerspective()) {
690 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000691 return;
692 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000693 if (paint.getXfermode()) {
694 paint.getXfermode()->asMode(&fXfermode);
695 }
696 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
697 matrix, paint, hasText,
698 &fDstFormXObject);
699 }
700};
701
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000702////////////////////////////////////////////////////////////////////////////////
703
halcanary989da4a2016-03-21 14:33:17 -0700704SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
robertphillips9a53fd72015-06-22 09:46:59 -0700705 : INHERITED(SkSurfaceProps(0, kUnknown_SkPixelGeometry))
706 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800707 , fContentSize(pageSize)
708 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanary96fcdcc2015-08-27 07:41:13 -0700709 , fLastContentEntry(nullptr)
halcanary96fcdcc2015-08-27 07:41:13 -0700710 , fClipStack(nullptr)
halcanary385fe4d2015-08-26 13:07:48 -0700711 , fFontGlyphUsage(new SkPDFGlyphSetMap)
halcanarya1f1ee92015-02-20 06:17:26 -0800712 , fRasterDpi(rasterDpi)
halcanary989da4a2016-03-21 14:33:17 -0700713 , fDocument(doc) {
halcanarya1f1ee92015-02-20 06:17:26 -0800714 SkASSERT(pageSize.width() > 0);
715 SkASSERT(pageSize.height() > 0);
716 fLegacyBitmap.setInfo(
717 SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()));
718 if (flip) {
719 // Skia generally uses the top left as the origin but PDF
720 // natively has the origin at the bottom left. This matrix
721 // corrects for that. But that only needs to be done once, we
722 // don't do it when layering.
723 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
724 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
725 } else {
726 fInitialTransform.setIdentity();
727 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000728}
729
730SkPDFDevice::~SkPDFDevice() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000731 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000732}
733
734void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700735 fContentEntries.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700736 fLastContentEntry = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -0700737 if (fFontGlyphUsage.get() == nullptr) {
halcanary385fe4d2015-08-26 13:07:48 -0700738 fFontGlyphUsage.reset(new SkPDFGlyphSetMap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000739 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000740}
741
vandebo@chromium.org98594282011-07-25 22:34:12 +0000742void SkPDFDevice::cleanUp(bool clearFontUsage) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000743 fGraphicStateResources.unrefAll();
744 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000745 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000746 fShaderResources.unrefAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000747
vandebo@chromium.org98594282011-07-25 22:34:12 +0000748 if (clearFontUsage) {
749 fFontGlyphUsage->reset();
750 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000751}
752
reedf70b5312016-03-04 16:36:20 -0800753void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
754 SkData* value) {
755 if (0 == rect.width() && 0 == rect.height()) {
756 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
757 } else {
758 SkPath path;
759 path.addRect(rect);
760 handlePathAnnotation(path, d, key, value);
761 }
762}
763
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000764void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000765 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700766 replace_srcmode_on_opaque_paint(&newPaint);
767
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000768 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000769 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000770 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000771}
772
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000773void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
774 ContentEntry* contentEntry) {
775 if (!contentEntry) {
776 return;
777 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000778 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
779 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000780 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000781 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000782 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000783 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000784 inverse.mapRect(&bbox);
785
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000786 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000787 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000788 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000789}
790
halcanary682ee012016-01-28 10:59:34 -0800791void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700792 SkCanvas::PointMode mode,
793 size_t count,
794 const SkPoint* points,
795 const SkPaint& srcPaint) {
796 SkPaint passedPaint = srcPaint;
797 replace_srcmode_on_opaque_paint(&passedPaint);
798
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000799 if (count == 0) {
800 return;
801 }
802
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000803 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
804 // We only use this when there's a path effect because of the overhead
805 // of multiple calls to setUpContentEntry it causes.
806 if (passedPaint.getPathEffect()) {
807 if (d.fClip->isEmpty()) {
808 return;
809 }
810 SkDraw pointDraw(d);
811 pointDraw.fDevice = this;
812 pointDraw.drawPoints(mode, count, points, passedPaint, true);
813 return;
814 }
815
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000816 const SkPaint* paint = &passedPaint;
817 SkPaint modifiedPaint;
818
819 if (mode == SkCanvas::kPoints_PointMode &&
820 paint->getStrokeCap() != SkPaint::kRound_Cap) {
821 modifiedPaint = *paint;
822 paint = &modifiedPaint;
823 if (paint->getStrokeWidth()) {
824 // PDF won't draw a single point with square/butt caps because the
825 // orientation is ambiguous. Draw a rectangle instead.
826 modifiedPaint.setStyle(SkPaint::kFill_Style);
827 SkScalar strokeWidth = paint->getStrokeWidth();
828 SkScalar halfStroke = SkScalarHalf(strokeWidth);
829 for (size_t i = 0; i < count; i++) {
830 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
831 r.inset(-halfStroke, -halfStroke);
832 drawRect(d, r, modifiedPaint);
833 }
834 return;
835 } else {
836 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
837 }
838 }
839
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000840 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000841 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000842 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000843 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000844
845 switch (mode) {
846 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000847 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000848 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000849 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000850 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000851 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000852 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000853 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000854 break;
855 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000856 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000857 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000858 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000859 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000860 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000861 &content.entry()->fContent);
862 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000863 }
864 break;
865 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000866 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
867 for (size_t i = 0; i < count; i++) {
868 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000869 &content.entry()->fContent);
870 SkPDFUtils::ClosePath(&content.entry()->fContent);
871 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000872 }
873 break;
874 default:
875 SkASSERT(false);
876 }
877}
878
halcanary8103a342016-03-08 15:10:16 -0800879static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800880 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700881 annotation->insertName("Subtype", "Link");
882
halcanaryece83922016-03-08 08:32:12 -0800883 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700884 border->reserve(3);
885 border->appendInt(0); // Horizontal corner radius.
886 border->appendInt(0); // Vertical corner radius.
887 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800888 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700889
halcanaryece83922016-03-08 08:32:12 -0800890 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700891 rect->reserve(4);
892 rect->appendScalar(translatedRect.fLeft);
893 rect->appendScalar(translatedRect.fTop);
894 rect->appendScalar(translatedRect.fRight);
895 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800896 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700897
halcanary8103a342016-03-08 15:10:16 -0800898 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700899}
900
halcanary8103a342016-03-08 15:10:16 -0800901static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
902 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700903 SkString url(static_cast<const char *>(urlData->data()),
904 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800905 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700906 action->insertName("S", "URI");
907 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800908 annotation->insertObject("A", std::move(action));
909 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700910}
911
halcanary8103a342016-03-08 15:10:16 -0800912static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
913 const SkRect& r) {
914 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700915 SkString name(static_cast<const char *>(nameData->data()),
916 nameData->size() - 1);
917 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800918 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700919}
920
halcanary682ee012016-01-28 10:59:34 -0800921void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700922 const SkRect& rect,
923 const SkPaint& srcPaint) {
924 SkPaint paint = srcPaint;
925 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000926 SkRect r = rect;
927 r.sort();
928
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000929 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000930 if (d.fClip->isEmpty()) {
931 return;
932 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000933 SkPath path;
934 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700935 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000936 return;
937 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000938
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000939 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000940 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000941 return;
942 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000943 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000944 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000945 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000946}
947
halcanarya6814332015-05-27 08:53:36 -0700948void SkPDFDevice::drawRRect(const SkDraw& draw,
949 const SkRRect& rrect,
950 const SkPaint& srcPaint) {
951 SkPaint paint = srcPaint;
952 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000953 SkPath path;
954 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700955 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000956}
957
halcanarya6814332015-05-27 08:53:36 -0700958void SkPDFDevice::drawOval(const SkDraw& draw,
959 const SkRect& oval,
960 const SkPaint& srcPaint) {
961 SkPaint paint = srcPaint;
962 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700963 SkPath path;
964 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700965 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700966}
967
halcanary682ee012016-01-28 10:59:34 -0800968void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700969 const SkPath& origPath,
970 const SkPaint& srcPaint,
971 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000972 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700973 SkPaint paint = srcPaint;
974 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800975 SkPath modifiedPath;
976 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000977
978 SkMatrix matrix = *d.fMatrix;
979 if (prePathMatrix) {
980 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800981 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000982 pathPtr = &modifiedPath;
983 pathIsMutable = true;
984 }
halcanary682ee012016-01-28 10:59:34 -0800985 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000986 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000987 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000988 }
989 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000990
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000991 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000992 if (d.fClip->isEmpty()) {
993 return;
994 }
halcanary682ee012016-01-28 10:59:34 -0800995 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000996 pathPtr = &modifiedPath;
997 pathIsMutable = true;
998 }
halcanary682ee012016-01-28 10:59:34 -0800999 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +00001000
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001001 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -07001002 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001003 if (fill) {
1004 noEffectPaint.setStyle(SkPaint::kFill_Style);
1005 } else {
1006 noEffectPaint.setStyle(SkPaint::kStroke_Style);
1007 noEffectPaint.setStrokeWidth(0);
1008 }
halcanary96fcdcc2015-08-27 07:41:13 -07001009 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +00001010 return;
1011 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001012
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001013 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001014 return;
1015 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001016
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001017 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001018 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001019 return;
1020 }
halcanary8b2bc252015-10-06 09:41:47 -07001021 bool consumeDegeratePathSegments =
1022 paint.getStyle() == SkPaint::kFill_Style ||
1023 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
1024 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +00001025 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -07001026 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +00001027 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001028 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001029 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001030}
1031
halcanary7a14b312015-10-01 07:28:13 -07001032void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
1033 const SkBitmap& bitmap,
1034 const SkRect* src,
1035 const SkRect& dst,
1036 const SkPaint& srcPaint,
1037 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001038 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -07001039}
1040
1041void SkPDFDevice::drawBitmap(const SkDraw& d,
1042 const SkBitmap& bitmap,
1043 const SkMatrix& matrix,
1044 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -07001045 SkPaint paint = srcPaint;
1046 if (bitmap.isOpaque()) {
1047 replace_srcmode_on_opaque_paint(&paint);
1048 }
1049
halcanary7a14b312015-10-01 07:28:13 -07001050 if (d.fClip->isEmpty()) {
1051 return;
1052 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001053
halcanary7a14b312015-10-01 07:28:13 -07001054 SkMatrix transform = matrix;
1055 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001056 SkImageBitmap imageBitmap(bitmap);
1057 this->internalDrawImage(
1058 transform, d.fClipStack, *d.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001059}
1060
1061void SkPDFDevice::drawSprite(const SkDraw& d,
1062 const SkBitmap& bitmap,
1063 int x,
1064 int y,
1065 const SkPaint& srcPaint) {
1066 SkPaint paint = srcPaint;
1067 if (bitmap.isOpaque()) {
1068 replace_srcmode_on_opaque_paint(&paint);
1069 }
1070
1071 if (d.fClip->isEmpty()) {
1072 return;
1073 }
1074
1075 SkMatrix matrix;
1076 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -07001077 SkImageBitmap imageBitmap(bitmap);
1078 this->internalDrawImage(
1079 matrix, d.fClipStack, *d.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001080}
1081
1082void SkPDFDevice::drawImage(const SkDraw& draw,
1083 const SkImage* image,
1084 SkScalar x,
1085 SkScalar y,
1086 const SkPaint& srcPaint) {
1087 SkPaint paint = srcPaint;
1088 if (!image) {
1089 return;
1090 }
1091 if (image->isOpaque()) {
1092 replace_srcmode_on_opaque_paint(&paint);
1093 }
1094 if (draw.fClip->isEmpty()) {
1095 return;
1096 }
1097 SkMatrix transform = SkMatrix::MakeTrans(x, y);
1098 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001099 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
1100 this->internalDrawImage(
1101 transform, draw.fClipStack, *draw.fClip, imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001102}
1103
1104void SkPDFDevice::drawImageRect(const SkDraw& draw,
1105 const SkImage* image,
1106 const SkRect* src,
1107 const SkRect& dst,
1108 const SkPaint& srcPaint,
1109 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001110 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001111}
1112
halcanarybb264b72015-04-07 10:40:03 -07001113// Create a PDF string. Maximum length (in bytes) is 65,535.
1114// @param input A string value.
1115// @param len The length of the input array.
1116// @param wideChars True iff the upper byte in each uint16_t is
1117// significant and should be encoded and not
1118// discarded. If true, the upper byte is encoded
1119// first. Otherwise, we assert the upper byte is
1120// zero.
1121static SkString format_wide_string(const uint16_t* input,
1122 size_t len,
1123 bool wideChars) {
1124 if (wideChars) {
1125 SkASSERT(2 * len < 65535);
1126 static const char gHex[] = "0123456789ABCDEF";
1127 SkString result(4 * len + 2);
1128 result[0] = '<';
1129 for (size_t i = 0; i < len; i++) {
1130 result[4 * i + 1] = gHex[(input[i] >> 12) & 0xF];
1131 result[4 * i + 2] = gHex[(input[i] >> 8) & 0xF];
1132 result[4 * i + 3] = gHex[(input[i] >> 4) & 0xF];
1133 result[4 * i + 4] = gHex[(input[i] ) & 0xF];
1134 }
1135 result[4 * len + 1] = '>';
1136 return result;
1137 } else {
1138 SkASSERT(len <= 65535);
1139 SkString tmp(len);
1140 for (size_t i = 0; i < len; i++) {
1141 SkASSERT(0 == input[i] >> 8);
1142 tmp[i] = static_cast<uint8_t>(input[i]);
1143 }
halcanarybc4696b2015-05-06 10:56:04 -07001144 return SkPDFUtils::FormatString(tmp.c_str(), tmp.size());
halcanarybb264b72015-04-07 10:40:03 -07001145 }
1146}
1147
halcanary66a82f32015-10-12 13:05:04 -07001148static void draw_transparent_text(SkPDFDevice* device,
1149 const SkDraw& d,
1150 const void* text, size_t len,
1151 SkScalar x, SkScalar y,
1152 const SkPaint& srcPaint) {
1153
1154 SkPaint transparent;
1155 if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(),
1156 device->getCanon())) {
1157 SkDEBUGFAIL("default typeface should be embeddable");
1158 return; // Avoid infinite loop in release.
1159 }
1160 transparent.setTextSize(srcPaint.getTextSize());
1161 transparent.setColor(SK_ColorTRANSPARENT);
1162 switch (srcPaint.getTextEncoding()) {
1163 case SkPaint::kGlyphID_TextEncoding: {
1164 // Since a glyphId<->Unicode mapping is typeface-specific,
1165 // map back to Unicode first.
1166 size_t glyphCount = len / 2;
1167 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1168 srcPaint.glyphsToUnichars(
1169 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1170 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
1171 device->drawText(d, &unichars[0],
1172 glyphCount * sizeof(SkUnichar),
1173 x, y, transparent);
1174 break;
1175 }
1176 case SkPaint::kUTF8_TextEncoding:
1177 case SkPaint::kUTF16_TextEncoding:
1178 case SkPaint::kUTF32_TextEncoding:
1179 transparent.setTextEncoding(srcPaint.getTextEncoding());
1180 device->drawText(d, text, len, x, y, transparent);
1181 break;
1182 default:
1183 SkFAIL("unknown text encoding");
1184 }
1185}
1186
1187
halcanary682ee012016-01-28 10:59:34 -08001188void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
halcanarya6814332015-05-27 08:53:36 -07001189 SkScalar x, SkScalar y, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001190 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary6950de62015-11-07 05:29:00 -08001191 // https://bug.skia.org/3866
halcanary66a82f32015-10-12 13:05:04 -07001192 SkPath path;
1193 srcPaint.getTextPath(text, len, x, y, &path);
1194 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1195 // Draw text transparently to make it copyable/searchable/accessable.
1196 draw_transparent_text(this, d, text, len, x, y, srcPaint);
1197 return;
1198 }
halcanarya6814332015-05-27 08:53:36 -07001199 SkPaint paint = srcPaint;
1200 replace_srcmode_on_opaque_paint(&paint);
1201
halcanary96fcdcc2015-08-27 07:41:13 -07001202 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1203 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001204 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1205 // making text unreadable (e.g. same text twice when using CSS shadows).
1206 return;
1207 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001208 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001209 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001210 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001211 return;
1212 }
1213
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001214 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001215 const uint16_t* glyphIDs = nullptr;
reed@google.comaec40662014-04-18 19:29:07 +00001216 int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001217 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001218
benjaminwagnerd936f632016-02-23 10:44:31 -08001219 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001220 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001221 content.entry()->fContent.writeText("BT\n");
1222 set_text_transform(x, y, textPaint.getTextSkewX(),
1223 &content.entry()->fContent);
reed@google.comaec40662014-04-18 19:29:07 +00001224 int consumedGlyphCount = 0;
halcanary2f912f32014-10-16 09:53:20 -07001225
1226 SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs);
1227
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001228 while (numGlyphs > consumedGlyphCount) {
robertphillips8e0c1502015-07-07 10:28:43 -07001229 this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001230 SkPDFFont* font = content.entry()->fState.fFont;
halcanary2f912f32014-10-16 09:53:20 -07001231
1232 int availableGlyphs = font->glyphsToPDFFontEncoding(
1233 glyphIDsCopy.begin() + consumedGlyphCount,
1234 numGlyphs - consumedGlyphCount);
1235 fFontGlyphUsage->noteGlyphUsage(
1236 font, glyphIDsCopy.begin() + consumedGlyphCount,
1237 availableGlyphs);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001238 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001239 format_wide_string(glyphIDsCopy.begin() + consumedGlyphCount,
1240 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001241 content.entry()->fContent.writeText(encodedString.c_str());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001242 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001243 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001244 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001245 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001246}
1247
halcanary682ee012016-01-28 10:59:34 -08001248void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -07001249 const SkScalar pos[], int scalarsPerPos,
halcanary682ee012016-01-28 10:59:34 -08001250 const SkPoint& offset, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001251 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary66a82f32015-10-12 13:05:04 -07001252 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos);
1253 SkAutoTMalloc<SkPoint> positionsBuffer;
1254 if (2 != scalarsPerPos) {
1255 int glyphCount = srcPaint.textToGlyphs(text, len, NULL);
1256 positionsBuffer.reset(glyphCount);
1257 for (int i = 0; i < glyphCount; ++i) {
1258 positionsBuffer[i].set(pos[i], 0.0f);
1259 }
1260 positions = &positionsBuffer[0];
1261 }
1262 SkPath path;
1263 srcPaint.getPosTextPath(text, len, positions, &path);
1264 SkMatrix matrix;
1265 matrix.setTranslate(offset);
1266 this->drawPath(d, path, srcPaint, &matrix, true);
1267 // Draw text transparently to make it copyable/searchable/accessable.
1268 draw_transparent_text(
1269 this, d, text, len, offset.x() + positions[0].x(),
1270 offset.y() + positions[0].y(), srcPaint);
1271 return;
1272 }
1273
halcanarya6814332015-05-27 08:53:36 -07001274 SkPaint paint = srcPaint;
1275 replace_srcmode_on_opaque_paint(&paint);
1276
halcanary96fcdcc2015-08-27 07:41:13 -07001277 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1278 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001279 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1280 // making text unreadable (e.g. same text twice when using CSS shadows).
1281 return;
1282 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001283 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001284 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001285 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001286 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001287 return;
1288 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001289
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001290 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001291 const uint16_t* glyphIDs = nullptr;
bungeman22edc832014-10-03 07:55:58 -07001292 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001293 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001294
benjaminwagnerd936f632016-02-23 10:44:31 -08001295 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001296 content.entry()->fContent.writeText("BT\n");
robertphillips8e0c1502015-07-07 10:28:43 -07001297 this->updateFont(textPaint, glyphIDs[0], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001298 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001299 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001300 uint16_t encodedValue = glyphIDs[i];
1301 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
bungeman22edc832014-10-03 07:55:58 -07001302 // The current pdf font cannot encode the current glyph.
1303 // Try to get a pdf font which can encode the current glyph.
robertphillips8e0c1502015-07-07 10:28:43 -07001304 this->updateFont(textPaint, glyphIDs[i], content.entry());
bungeman22edc832014-10-03 07:55:58 -07001305 font = content.entry()->fState.fFont;
1306 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
1307 SkDEBUGFAIL("PDF could not encode glyph.");
1308 continue;
1309 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001310 }
bungeman22edc832014-10-03 07:55:58 -07001311
vandebo@chromium.org98594282011-07-25 22:34:12 +00001312 fFontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
fmalita05c4a432014-09-29 06:29:53 -07001313 SkScalar x = offset.x() + pos[i * scalarsPerPos];
1314 SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0);
1315
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001316 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
bungeman22edc832014-10-03 07:55:58 -07001317 set_text_transform(x, y, textPaint.getTextSkewX(), &content.entry()->fContent);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001318 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001319 format_wide_string(&encodedValue, 1, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001320 content.entry()->fContent.writeText(encodedString.c_str());
1321 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001322 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001323 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001324}
1325
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001326void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001327 int vertexCount, const SkPoint verts[],
1328 const SkPoint texs[], const SkColor colors[],
1329 SkXfermode* xmode, const uint16_t indices[],
1330 int indexCount, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001331 if (d.fClip->isEmpty()) {
1332 return;
1333 }
reed@google.com85e143c2013-12-30 15:51:25 +00001334 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001335}
1336
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001337void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1338 int x, int y, const SkPaint& paint) {
fmalita6987dca2014-11-13 08:33:37 -08001339 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001340 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001341
1342 SkScalar scalarX = SkIntToScalar(x);
1343 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001344 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1345 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001346 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001347 }
halcanary91fcb3e2016-03-04 13:53:22 -08001348 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1349 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001350 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001351 }
halcanary91fcb3e2016-03-04 13:53:22 -08001352 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1353 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001354 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001355 }
1356
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001357 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001358 return;
1359 }
1360
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001361 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001362 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001363 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001364 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001365 return;
1366 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001367 if (content.needShape()) {
1368 SkPath shape;
1369 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001370 SkIntToScalar(device->width()),
1371 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001372 content.setShape(shape);
1373 }
1374 if (!content.needSource()) {
1375 return;
1376 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001377
halcanaryece83922016-03-08 08:32:12 -08001378 auto xObject = sk_make_sp<SkPDFFormXObject>(pdfDevice);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001379 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001380 &content.entry()->fContent);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001381
1382 // Merge glyph sets from the drawn device.
1383 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001384}
1385
reed89443ab2014-06-27 11:34:19 -07001386SkImageInfo SkPDFDevice::imageInfo() const {
1387 return fLegacyBitmap.info();
1388}
1389
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001390void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1391 INHERITED::onAttachToCanvas(canvas);
1392
1393 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1394 fClipStack = canvas->getClipStack();
1395}
1396
1397void SkPDFDevice::onDetachFromCanvas() {
1398 INHERITED::onDetachFromCanvas();
1399
halcanary96fcdcc2015-08-27 07:41:13 -07001400 fClipStack = nullptr;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001401}
1402
reede8f30622016-03-23 18:59:25 -07001403sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1404 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001405}
1406
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001407
halcanary8103a342016-03-08 15:10:16 -08001408sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001409 SkTDArray<SkPDFObject*> fonts;
1410 fonts.setReserve(fFontResources.count());
1411 for (SkPDFFont* font : fFontResources) {
1412 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001413 }
halcanary8103a342016-03-08 15:10:16 -08001414 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001415 &fGraphicStateResources,
1416 &fShaderResources,
1417 &fXObjectResources,
1418 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001419}
1420
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001421const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1422 return fFontResources;
1423}
1424
halcanary8103a342016-03-08 15:10:16 -08001425sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001426 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001427 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001428 mediaBox->appendInt(0);
1429 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001430 mediaBox->appendInt(fPageSize.width());
1431 mediaBox->appendInt(fPageSize.height());
1432 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001433}
1434
mtklein5f939ab2016-03-16 10:28:35 -07001435std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001436 SkDynamicMemoryWStream buffer;
1437 this->writeContent(&buffer);
mtklein5f939ab2016-03-16 10:28:35 -07001438 return std::unique_ptr<SkStreamAsset>(
halcanary8103a342016-03-08 15:10:16 -08001439 buffer.bytesWritten() > 0
1440 ? buffer.detachAsStream()
1441 : new SkMemoryStream);
reed@google.com5667afc2011-06-27 14:42:15 +00001442}
1443
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001444void SkPDFDevice::copyContentEntriesToData(ContentEntry* entry,
1445 SkWStream* data) const {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001446 // TODO(ctguil): For margins, I'm not sure fExistingClipStack/Region is the
1447 // right thing to pass here.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001448 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, data);
halcanary96fcdcc2015-08-27 07:41:13 -07001449 while (entry != nullptr) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +00001450 SkPoint translation;
1451 translation.iset(this->getOrigin());
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001452 translation.negate();
1453 gsState.updateClip(entry->fState.fClipStack, entry->fState.fClipRegion,
1454 translation);
1455 gsState.updateMatrix(entry->fState.fMatrix);
1456 gsState.updateDrawingState(entry->fState);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001457
halcanary7af21502015-02-23 12:17:59 -08001458 entry->fContent.writeToStream(data);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001459 entry = entry->fNext.get();
1460 }
1461 gsState.drainStack();
1462}
1463
halcanary334fcbc2015-02-24 12:56:16 -08001464void SkPDFDevice::writeContent(SkWStream* out) const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001465 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanary334fcbc2015-02-24 12:56:16 -08001466 SkPDFUtils::AppendTransform(fInitialTransform, out);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001467 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001468
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001469 // If the content area is the entire page, then we don't need to clip
1470 // the content area (PDF area clips to the page size). Otherwise,
1471 // we have to clip to the content area; we've already applied the
1472 // initial transform, so just clip to the device size.
1473 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001474 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1475 SkIntToScalar(this->height()));
halcanary96fcdcc2015-08-27 07:41:13 -07001476 emit_clip(nullptr, &r, out);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001477 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001478
halcanary334fcbc2015-02-24 12:56:16 -08001479 SkPDFDevice::copyContentEntriesToData(fContentEntries.get(), out);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001480}
1481
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001482/* Draws an inverse filled path by using Path Ops to compute the positive
1483 * inverse using the current clip as the inverse bounds.
1484 * Return true if this was an inverse path and was properly handled,
1485 * otherwise returns false and the normal drawing routine should continue,
1486 * either as a (incorrect) fallback or because the path was not inverse
1487 * in the first place.
1488 */
1489bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001490 const SkPaint& paint, bool pathIsMutable,
1491 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001492 if (!origPath.isInverseFillType()) {
1493 return false;
1494 }
1495
1496 if (d.fClip->isEmpty()) {
1497 return false;
1498 }
1499
1500 SkPath modifiedPath;
1501 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1502 SkPaint noInversePaint(paint);
1503
1504 // Merge stroking operations into final path.
1505 if (SkPaint::kStroke_Style == paint.getStyle() ||
1506 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1507 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1508 if (doFillPath) {
1509 noInversePaint.setStyle(SkPaint::kFill_Style);
1510 noInversePaint.setStrokeWidth(0);
1511 pathPtr = &modifiedPath;
1512 } else {
1513 // To be consistent with the raster output, hairline strokes
1514 // are rendered as non-inverted.
1515 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001516 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001517 return true;
1518 }
1519 }
1520
1521 // Get bounds of clip in current transform space
1522 // (clip bounds are given in device space).
1523 SkRect bounds;
1524 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001525 SkMatrix totalMatrix = *d.fMatrix;
1526 if (prePathMatrix) {
1527 totalMatrix.preConcat(*prePathMatrix);
1528 }
1529 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001530 return false;
1531 }
1532 bounds.set(d.fClip->getBounds());
1533 transformInverse.mapRect(&bounds);
1534
1535 // Extend the bounds by the line width (plus some padding)
1536 // so the edge doesn't cause a visible stroke.
1537 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1538 paint.getStrokeWidth() + SK_Scalar1);
1539
1540 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1541 return false;
1542 }
1543
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001544 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001545 return true;
1546}
1547
reedf70b5312016-03-04 16:36:20 -08001548void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001549 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001550 const char key[], SkData* value) {
1551 if (!value) {
1552 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001553 }
reedf70b5312016-03-04 16:36:20 -08001554
1555 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1556 SkPoint transformedPoint;
1557 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1558 fNamedDestinations.emplace_back(value, transformedPoint);
1559 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001560}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001561
reedf70b5312016-03-04 16:36:20 -08001562void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001563 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001564 const char key[], SkData* value) {
1565 if (!value) {
1566 return;
1567 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001568
1569 SkPath transformedPath = path;
1570 transformedPath.transform(*d.fMatrix);
1571 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001572 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1573 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001574 SkRect transformedRect = SkRect::Make(clip.getBounds());
1575
reedf70b5312016-03-04 16:36:20 -08001576 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001577 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001578 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001579 }
reedf70b5312016-03-04 16:36:20 -08001580 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001581 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001582 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001583 }
reed16108352016-03-03 09:14:36 -08001584 }
halcanary438de492015-04-28 06:21:01 -07001585}
1586
wangxianzhuef6c50a2015-09-17 20:38:02 -07001587void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1588 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001589 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001590 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001591 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001592 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001593 }
halcanary91fcb3e2016-03-04 13:53:22 -08001594 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001595 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001596 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001597 array->appendObject(
1598 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001599 }
1600}
epoger@google.comb58772f2013-03-08 09:09:10 +00001601
halcanary6d622702015-03-25 08:45:42 -07001602void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001603 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001604 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001605 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001606 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001607 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001608 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001609 pdfDest->appendScalar(p.x());
1610 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001611 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001612 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001613 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001614 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001615}
1616
reed@google.comfc641d02012-09-20 17:52:20 +00001617SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
halcanary385fe4d2015-08-26 13:07:48 -07001618 SkPDFFormXObject* xobject = new SkPDFFormXObject(this);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001619 // We always draw the form xobjects that we create back into the device, so
1620 // we simply preserve the font usage instead of pulling it out and merging
1621 // it back in later.
1622 cleanUp(false); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001623 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001624 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001625}
1626
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001627void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
1628 SkPDFFormXObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001629 const SkClipStack* clipStack,
1630 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001631 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001632 bool invertClip) {
1633 if (clipRegion.isEmpty() && !invertClip) {
1634 return;
1635 }
1636
halcanary1437c1e2016-03-13 18:30:24 -07001637 auto sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanary989da4a2016-03-21 14:33:17 -07001638 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001639
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001640 SkMatrix identity;
1641 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001642 SkPaint paint;
1643 paint.setXfermodeMode(mode);
1644 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001645 if (!content.entry()) {
1646 return;
1647 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001648 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001649 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001650 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001651
halcanary1437c1e2016-03-13 18:30:24 -07001652 // Call makeNoSmaskGraphicState() instead of
1653 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1654 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001655 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001656 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001657 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001658}
1659
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001660ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
1661 const SkRegion& clipRegion,
1662 const SkMatrix& matrix,
1663 const SkPaint& paint,
1664 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001665 SkPDFFormXObject** dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001666 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001667 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001668 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001669 }
1670
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001671 // The clip stack can come from an SkDraw where it is technically optional.
1672 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001673 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001674 if (clipRegion == fExistingClipRegion) {
1675 clipStack = &fExistingClipStack;
1676 } else {
1677 // GraphicStackState::updateClip expects the clip stack to have
1678 // fExistingClip as a prefix, so start there, then set the clip
1679 // to the passed region.
1680 synthesizedClipStack = fExistingClipStack;
1681 SkPath clipPath;
1682 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001683 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1684 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001685 clipStack = &synthesizedClipStack;
1686 }
1687 }
1688
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001689 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1690 if (paint.getXfermode()) {
1691 paint.getXfermode()->asMode(&xfermode);
1692 }
1693
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001694 // For the following modes, we want to handle source and destination
1695 // separately, so make an object of what's already there.
1696 if (xfermode == SkXfermode::kClear_Mode ||
1697 xfermode == SkXfermode::kSrc_Mode ||
1698 xfermode == SkXfermode::kSrcIn_Mode ||
1699 xfermode == SkXfermode::kDstIn_Mode ||
1700 xfermode == SkXfermode::kSrcOut_Mode ||
1701 xfermode == SkXfermode::kDstOut_Mode ||
1702 xfermode == SkXfermode::kSrcATop_Mode ||
1703 xfermode == SkXfermode::kDstATop_Mode ||
1704 xfermode == SkXfermode::kModulate_Mode) {
1705 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001706 *dst = createFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001707 SkASSERT(isContentEmpty());
1708 } else if (xfermode != SkXfermode::kSrc_Mode &&
1709 xfermode != SkXfermode::kSrcOut_Mode) {
1710 // Except for Src and SrcOut, if there isn't anything already there,
1711 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001712 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001713 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001714 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001715 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001716 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001717
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001718 // Dst xfer mode doesn't draw source at all.
1719 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001720 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001721 }
1722
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001723 ContentEntry* entry;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001724 SkAutoTDelete<ContentEntry> newEntry;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001725
halcanary73557f62016-03-25 14:44:33 -07001726 if (fLastContentEntry && fLastContentEntry->fContent.getOffset() == 0) {
1727 entry = fLastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001728 } else {
1729 newEntry.reset(new ContentEntry);
1730 entry = newEntry.get();
1731 }
1732
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001733 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001734 hasText, &entry->fState);
halcanary73557f62016-03-25 14:44:33 -07001735 if (fLastContentEntry && xfermode != SkXfermode::kDstOver_Mode &&
1736 entry->fState.compareInitialState(fLastContentEntry->fState)) {
1737 return fLastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001738 }
1739
halcanary73557f62016-03-25 14:44:33 -07001740 if (!fLastContentEntry) {
1741 fContentEntries.reset(entry);
1742 fLastContentEntry = entry;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001743 } else if (xfermode == SkXfermode::kDstOver_Mode) {
halcanary73557f62016-03-25 14:44:33 -07001744 entry->fNext.reset(fContentEntries.release());
1745 fContentEntries.reset(entry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001746 } else {
halcanary73557f62016-03-25 14:44:33 -07001747 fLastContentEntry->fNext.reset(entry);
1748 fLastContentEntry = entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001749 }
halcanaryfcad44b2016-03-06 14:47:10 -08001750 newEntry.release();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001751 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001752}
1753
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001754void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001755 SkPDFFormXObject* dst,
1756 SkPath* shape) {
1757 if (xfermode != SkXfermode::kClear_Mode &&
1758 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001759 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001760 xfermode != SkXfermode::kSrcIn_Mode &&
1761 xfermode != SkXfermode::kDstIn_Mode &&
1762 xfermode != SkXfermode::kSrcOut_Mode &&
1763 xfermode != SkXfermode::kDstOut_Mode &&
1764 xfermode != SkXfermode::kSrcATop_Mode &&
1765 xfermode != SkXfermode::kDstATop_Mode &&
1766 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001767 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001768 return;
1769 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001770 if (xfermode == SkXfermode::kDstOver_Mode) {
1771 SkASSERT(!dst);
halcanary73557f62016-03-25 14:44:33 -07001772 if (fContentEntries->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001773 // For DstOver, an empty content entry was inserted before the rest
1774 // of the content entries. If nothing was drawn, it needs to be
1775 // removed.
halcanary73557f62016-03-25 14:44:33 -07001776 fContentEntries.reset(fContentEntries->fNext.release());
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001777 }
1778 return;
1779 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001780 if (!dst) {
1781 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1782 xfermode == SkXfermode::kSrcOut_Mode);
1783 return;
1784 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001785
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001786 SkASSERT(dst);
halcanary73557f62016-03-25 14:44:33 -07001787 SkASSERT(!fContentEntries->fNext.get());
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001788 // Changing the current content into a form-xobject will destroy the clip
1789 // objects which is fine since the xobject will already be clipped. However
1790 // if source has shape, we need to clip it too, so a copy of the clip is
1791 // saved.
halcanary73557f62016-03-25 14:44:33 -07001792 SkClipStack clipStack = fContentEntries->fState.fClipStack;
1793 SkRegion clipRegion = fContentEntries->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001794
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001795 SkMatrix identity;
1796 identity.reset();
1797 SkPaint stockPaint;
1798
halcanary48810a02016-03-07 14:57:50 -08001799 sk_sp<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001800 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001801 // If nothing was drawn and there's no shape, then the draw was a
1802 // no-op, but dst needs to be restored for that to be true.
1803 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1804 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1805 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001806 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001807 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001808 ScopedContentEntry content(this, &fExistingClipStack,
1809 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001810 stockPaint);
1811 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1812 &content.entry()->fContent);
1813 return;
1814 } else {
1815 xfermode = SkXfermode::kClear_Mode;
1816 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001817 } else {
1818 SkASSERT(!fContentEntries->fNext.get());
reed@google.comfc641d02012-09-20 17:52:20 +00001819 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001820 }
1821
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001822 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1823 // without alpha.
1824 if (xfermode == SkXfermode::kSrcATop_Mode) {
1825 // TODO(vandebo): In order to properly support SrcATop we have to track
1826 // the shape of what's been drawn at all times. It's the intersection of
1827 // the non-transparent parts of the device and the outlines (shape) of
1828 // all images and devices drawn.
1829 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001830 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001831 SkXfermode::kSrcOver_Mode, true);
1832 } else {
halcanary48810a02016-03-07 14:57:50 -08001833 sk_sp<SkPDFFormXObject> dstMaskStorage;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001834 SkPDFFormXObject* dstMask = srcFormXObject.get();
halcanary96fcdcc2015-08-27 07:41:13 -07001835 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001836 // Draw shape into a form-xobject.
1837 SkDraw d;
1838 d.fMatrix = &identity;
1839 d.fClip = &clipRegion;
1840 d.fClipStack = &clipStack;
1841 SkPaint filledPaint;
1842 filledPaint.setColor(SK_ColorBLACK);
1843 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001844 this->drawPath(d, *shape, filledPaint, nullptr, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001845
1846 dstMaskStorage.reset(createFormXObjectFromDevice());
1847 dstMask = dstMaskStorage.get();
1848 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001849 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1850 &fExistingClipStack, fExistingClipRegion,
1851 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001852 }
1853
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001854 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001855 return;
1856 } else if (xfermode == SkXfermode::kSrc_Mode ||
1857 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001858 ScopedContentEntry content(this, &fExistingClipStack,
1859 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001860 if (content.entry()) {
1861 SkPDFUtils::DrawFormXObject(
1862 this->addXObjectResource(srcFormXObject.get()),
1863 &content.entry()->fContent);
1864 }
1865 if (xfermode == SkXfermode::kSrc_Mode) {
1866 return;
1867 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001868 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001869 ScopedContentEntry content(this, &fExistingClipStack,
1870 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001871 if (content.entry()) {
1872 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1873 &content.entry()->fContent);
1874 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001875 }
1876
1877 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1878 xfermode == SkXfermode::kDstIn_Mode ||
1879 xfermode == SkXfermode::kSrcOut_Mode ||
1880 xfermode == SkXfermode::kDstOut_Mode ||
1881 xfermode == SkXfermode::kSrcATop_Mode ||
1882 xfermode == SkXfermode::kDstATop_Mode ||
1883 xfermode == SkXfermode::kModulate_Mode);
1884
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001885 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001886 xfermode == SkXfermode::kSrcOut_Mode ||
1887 xfermode == SkXfermode::kSrcATop_Mode) {
1888 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001889 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001890 SkXfermode::kSrcOver_Mode,
1891 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001892 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001893 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
1894 if (xfermode == SkXfermode::kModulate_Mode) {
1895 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001896 dst, &fExistingClipStack,
1897 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001898 SkXfermode::kSrcOver_Mode, false);
1899 mode = SkXfermode::kMultiply_Mode;
1900 }
1901 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001902 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001903 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001904 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001905}
1906
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001907bool SkPDFDevice::isContentEmpty() {
halcanary73557f62016-03-25 14:44:33 -07001908 if (!fContentEntries || fContentEntries->fContent.getOffset() == 0) {
1909 SkASSERT(!fContentEntries || !fContentEntries->fNext.get());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001910 return true;
1911 }
1912 return false;
1913}
1914
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001915void SkPDFDevice::populateGraphicStateEntryFromPaint(
1916 const SkMatrix& matrix,
1917 const SkClipStack& clipStack,
1918 const SkRegion& clipRegion,
1919 const SkPaint& paint,
1920 bool hasText,
1921 GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001922 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1923 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1924 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001925
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001926 entry->fMatrix = matrix;
1927 entry->fClipStack = clipStack;
1928 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001929 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1930 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001931
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001932 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001933 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001934 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001935 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001936 if (shader) {
1937 // PDF positions patterns relative to the initial transform, so
1938 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001939 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001940 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001941
1942 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001943 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001944 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001945
1946 // We need to apply the initial transform to bounds in order to get
1947 // bounds in a consistent coordinate system.
1948 SkRect boundsTemp;
1949 boundsTemp.set(bounds);
1950 fInitialTransform.mapRect(&boundsTemp);
1951 boundsTemp.roundOut(&bounds);
1952
halcanary792c80f2015-02-20 07:21:05 -08001953 SkScalar rasterScale =
1954 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
1955 pdfShader.reset(SkPDFShader::GetPDFShader(
reedfe630452016-03-25 09:08:00 -07001956 fDocument, fRasterDpi, shader, transform, bounds, rasterScale));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001957
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001958 if (pdfShader.get()) {
1959 // pdfShader has been canonicalized so we can directly compare
1960 // pointers.
1961 int resourceIndex = fShaderResources.find(pdfShader.get());
1962 if (resourceIndex < 0) {
1963 resourceIndex = fShaderResources.count();
1964 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001965 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001966 }
1967 entry->fShaderIndex = resourceIndex;
1968 } else {
1969 // A color shader is treated as an invalid shader so we don't have
1970 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001971 SkShader::GradientInfo gradientInfo;
1972 SkColor gradientColor;
1973 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001974 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001975 gradientInfo.fColorCount = 1;
1976 if (shader->asAGradient(&gradientInfo) ==
1977 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001978 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1979 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001980 }
1981 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001982 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001983
halcanary48810a02016-03-07 14:57:50 -08001984 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001985 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001986 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001987 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001988 } else {
1989 SkPaint newPaint = paint;
1990 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001991 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001992 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001993 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001994 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001995 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001996
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001997 if (hasText) {
1998 entry->fTextScaleX = paint.getTextScaleX();
1999 entry->fTextFill = paint.getStyle();
2000 } else {
2001 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002002 }
2003}
2004
halcanarybe27a112015-04-01 13:31:19 -07002005int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002006 // Assumes that gs has been canonicalized (so we can directly compare
2007 // pointers).
2008 int result = fGraphicStateResources.find(gs);
2009 if (result < 0) {
2010 result = fGraphicStateResources.count();
2011 fGraphicStateResources.push(gs);
2012 gs->ref();
2013 }
2014 return result;
2015}
2016
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002017int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
2018 // Assumes that xobject has been canonicalized (so we can directly compare
2019 // pointers).
2020 int result = fXObjectResources.find(xObject);
2021 if (result < 0) {
2022 result = fXObjectResources.count();
2023 fXObjectResources.push(xObject);
2024 xObject->ref();
2025 }
2026 return result;
2027}
2028
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002029void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
2030 ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002031 SkTypeface* typeface = paint.getTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -07002032 if (contentEntry->fState.fFont == nullptr ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002033 contentEntry->fState.fTextSize != paint.getTextSize() ||
2034 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002035 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00002036 contentEntry->fContent.writeText("/");
2037 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
2038 SkPDFResourceDict::kFont_ResourceType,
2039 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002040 contentEntry->fContent.writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -07002041 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002042 contentEntry->fContent.writeText(" Tf\n");
2043 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002044 }
2045}
2046
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002047int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08002048 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07002049 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002050 int resourceIndex = fFontResources.find(newFont.get());
2051 if (resourceIndex < 0) {
2052 resourceIndex = fFontResources.count();
2053 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002054 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002055 }
2056 return resourceIndex;
2057}
2058
halcanary7a14b312015-10-01 07:28:13 -07002059static SkSize rect_to_size(const SkRect& r) {
2060 return SkSize::Make(r.width(), r.height());
2061}
2062
halcanarya50151d2016-03-25 11:57:49 -07002063static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
2064 SkColorFilter* colorFilter) {
2065 auto surface =
2066 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
2067 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07002068 SkCanvas* canvas = surface->getCanvas();
2069 canvas->clear(SK_ColorTRANSPARENT);
2070 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07002071 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07002072 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07002073 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07002074 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07002075}
2076
2077////////////////////////////////////////////////////////////////////////////////
2078void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
2079 const SkClipStack* clipStack,
2080 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07002081 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07002082 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07002083 if (imageBitmap.dimensions().isZero()) {
2084 return;
2085 }
halcanary7a14b312015-10-01 07:28:13 -07002086 #ifdef SK_PDF_IMAGE_STATS
2087 gDrawImageCalls.fetch_add(1);
2088 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002089 SkMatrix matrix = origMatrix;
2090 SkRegion perspectiveBounds;
2091 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07002092 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002093
2094 // Rasterize the bitmap using perspective in a new bitmap.
2095 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002096 if (fRasterDpi == 0) {
2097 return;
2098 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002099 // Transform the bitmap in the new space, without taking into
2100 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002101 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07002102 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07002103 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002104 perspectiveOutline.transform(origMatrix);
2105
2106 // TODO(edisonn): perf - use current clip too.
2107 // Retrieve the bounds of the new shape.
2108 SkRect bounds = perspectiveOutline.getBounds();
2109
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002110 // Transform the bitmap in the new space, taking into
2111 // account the initial transform.
2112 SkMatrix total = origMatrix;
2113 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07002114 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
2115 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2116 total.postScale(dpiScale, dpiScale);
2117
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002118 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002119 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002120 physicalPerspectiveOutline.transform(total);
2121
halcanary7a14b312015-10-01 07:28:13 -07002122 SkRect physicalPerspectiveBounds =
2123 physicalPerspectiveOutline.getBounds();
2124 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2125 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002126
2127 // TODO(edisonn): A better approach would be to use a bitmap shader
2128 // (in clamp mode) and draw a rect over the entire bounding box. Then
2129 // intersect perspectiveOutline to the clip. That will avoid introducing
2130 // alpha to the image while still giving good behavior at the edge of
2131 // the image. Avoiding alpha will reduce the pdf size and generation
2132 // CPU time some.
2133
halcanary7a14b312015-10-01 07:28:13 -07002134 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2135
reede8f30622016-03-23 18:59:25 -07002136 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002137 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002138 return;
2139 }
halcanary7a14b312015-10-01 07:28:13 -07002140 SkCanvas* canvas = surface->getCanvas();
2141 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002142
2143 SkScalar deltaX = bounds.left();
2144 SkScalar deltaY = bounds.top();
2145
2146 SkMatrix offsetMatrix = origMatrix;
2147 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002148 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002149
2150 // Translate the draw in the new canvas, so we perfectly fit the
2151 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002152 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002153 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002154 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002155 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002156
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002157 // In the new space, we use the identity matrix translated
2158 // and scaled to reflect DPI.
2159 matrix.setScale(1 / scaleX, 1 / scaleY);
2160 matrix.postTranslate(deltaX, deltaY);
2161
halcanary7a14b312015-10-01 07:28:13 -07002162 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002163 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002164
halcanarya50151d2016-03-25 11:57:49 -07002165 autoImageUnref = surface->makeImageSnapshot();
2166 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002167 }
2168
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002169 SkMatrix scaled;
2170 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002171 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2172 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002173 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002174 SkIRect subset = imageBitmap.bounds();
2175 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2176 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002177 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002178 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002179 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002180 return;
2181 }
2182 if (content.needShape()) {
2183 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002184 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002185 shape.transform(matrix);
2186 content.setShape(shape);
2187 }
2188 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002189 return;
2190 }
2191
halcanary287d22d2015-09-24 10:20:05 -07002192 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002193 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002194 // draw calls. This code here works for all
2195 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2196 // rasterize a layer on this backend). Fortuanely, this seems
2197 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002198 autoImageUnref = color_filter(imageBitmap, colorFilter);
2199 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002200 // TODO(halcanary): de-dupe this by caching filtered images.
2201 // (maybe in the resource cache?)
2202 }
halcanarya50151d2016-03-25 11:57:49 -07002203
2204 SkBitmapKey key = imageBitmap.getKey();
2205 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002206 if (!pdfimage) {
halcanarya50151d2016-03-25 11:57:49 -07002207 auto img = imageBitmap.makeImage();
2208 if (!img) {
2209 return;
2210 }
2211 pdfimage = SkPDFCreateBitmapObject(
2212 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002213 if (!pdfimage) {
2214 return;
halcanary287d22d2015-09-24 10:20:05 -07002215 }
halcanarya50151d2016-03-25 11:57:49 -07002216 fDocument->serialize(pdfimage); // serialize images early.
2217 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002218 }
halcanarya50151d2016-03-25 11:57:49 -07002219 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002220 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002221 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002222}