blob: 9cc2563caf44532123e3d6e3fe2f39ba5a65fdb8 [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"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000022#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000023#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000024#include "SkPDFGraphicState.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000025#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000026#include "SkPDFShader.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000027#include "SkPDFStream.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000028#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000029#include "SkPDFUtils.h"
wangxianzhuef6c50a2015-09-17 20:38:02 -070030#include "SkRasterClip.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000031#include "SkRect.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000032#include "SkRRect.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000033#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070034#include "SkSurface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000035#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000036#include "SkTemplates.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000037#include "SkTypefacePriv.h"
halcanarya6814332015-05-27 08:53:36 -070038#include "SkXfermodeInterpretation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000039
edisonn@google.com73a7ea32013-11-11 20:55:15 +000040#define DPI_FOR_RASTER_SCALE_ONE 72
41
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000042// Utility functions
43
halcanarya6814332015-05-27 08:53:36 -070044// If the paint will definitely draw opaquely, replace kSrc_Mode with
45// kSrcOver_Mode. http://crbug.com/473572
46static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
47 if (kSrcOver_SkXfermodeInterpretation
48 == SkInterpretXfermode(*paint, false)) {
halcanary96fcdcc2015-08-27 07:41:13 -070049 paint->setXfermode(nullptr);
halcanarya6814332015-05-27 08:53:36 -070050 }
51}
52
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000053static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000054 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
reed80ea19c2015-05-12 10:37:34 -070055 SkScalar colorScale = SkScalarInvert(0xFF);
56 SkPDFUtils::AppendScalar(SkColorGetR(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000057 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070058 SkPDFUtils::AppendScalar(SkColorGetG(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000059 result->writeText(" ");
reed80ea19c2015-05-12 10:37:34 -070060 SkPDFUtils::AppendScalar(SkColorGetB(color) * colorScale, result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000061 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000062}
63
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000064static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000065 SkPaint result = paint;
66 if (result.isFakeBoldText()) {
67 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
68 kStdFakeBoldInterpKeys,
69 kStdFakeBoldInterpValues,
70 kStdFakeBoldInterpLength);
71 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000072 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000073 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000074 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000075 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000076 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000077 result.setStrokeWidth(width);
78 }
79 return result;
80}
81
82// Stolen from measure_text in SkDraw.cpp and then tweaked.
benjaminwagnerd936f632016-02-23 10:44:31 -080083static void align_text(SkPaint::GlyphCacheProc glyphCacheProc, const SkPaint& paint,
bungeman@google.com9a87cee2011-08-23 17:02:18 +000084 const uint16_t* glyphs, size_t len,
85 SkScalar* x, SkScalar* y) {
86 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000087 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000088 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000089
90 SkMatrix ident;
91 ident.reset();
halcanary96fcdcc2015-08-27 07:41:13 -070092 SkAutoGlyphCache autoCache(paint, nullptr, &ident);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000093 SkGlyphCache* cache = autoCache.getCache();
94
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +000095 const char* start = reinterpret_cast<const char*>(glyphs);
96 const char* stop = reinterpret_cast<const char*>(glyphs + len);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000097 SkFixed xAdv = 0, yAdv = 0;
98
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000099 // TODO(vandebo): This probably needs to take kerning into account.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000100 while (start < stop) {
benjaminwagnerd936f632016-02-23 10:44:31 -0800101 const SkGlyph& glyph = glyphCacheProc(cache, &start);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000102 xAdv += glyph.fAdvanceX;
103 yAdv += glyph.fAdvanceY;
104 };
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000105 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000106 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000107 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000108
109 SkScalar xAdj = SkFixedToScalar(xAdv);
110 SkScalar yAdj = SkFixedToScalar(yAdv);
111 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
112 xAdj = SkScalarHalf(xAdj);
113 yAdj = SkScalarHalf(yAdj);
114 }
115 *x = *x - xAdj;
116 *y = *y - yAdj;
117}
118
robertphillips@google.coma4662862013-11-21 14:24:16 +0000119static int max_glyphid_for_typeface(SkTypeface* typeface) {
reed@google.comfed86bd2013-03-14 15:04:57 +0000120 SkAutoResolveDefaultTypeface autoResolve(typeface);
121 typeface = autoResolve.get();
commit-bot@chromium.org6a4ba5b2013-07-17 21:55:08 +0000122 return typeface->countGlyphs() - 1;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000123}
124
125typedef SkAutoSTMalloc<128, uint16_t> SkGlyphStorage;
126
reed@google.comaec40662014-04-18 19:29:07 +0000127static int force_glyph_encoding(const SkPaint& paint, const void* text,
128 size_t len, SkGlyphStorage* storage,
bungeman22edc832014-10-03 07:55:58 -0700129 const uint16_t** glyphIDs) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000130 // Make sure we have a glyph id encoding.
131 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
halcanary96fcdcc2015-08-27 07:41:13 -0700132 int numGlyphs = paint.textToGlyphs(text, len, nullptr);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000133 storage->reset(numGlyphs);
134 paint.textToGlyphs(text, len, storage->get());
135 *glyphIDs = storage->get();
136 return numGlyphs;
137 }
138
139 // For user supplied glyph ids we need to validate them.
140 SkASSERT((len & 1) == 0);
reed@google.comaec40662014-04-18 19:29:07 +0000141 int numGlyphs = SkToInt(len / 2);
bungeman22edc832014-10-03 07:55:58 -0700142 const uint16_t* input = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000143
144 int maxGlyphID = max_glyphid_for_typeface(paint.getTypeface());
reed@google.comaec40662014-04-18 19:29:07 +0000145 int validated;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000146 for (validated = 0; validated < numGlyphs; ++validated) {
147 if (input[validated] > maxGlyphID) {
148 break;
149 }
150 }
151 if (validated >= numGlyphs) {
bungeman22edc832014-10-03 07:55:58 -0700152 *glyphIDs = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000153 return numGlyphs;
154 }
155
156 // Silently drop anything out of range.
157 storage->reset(numGlyphs);
158 if (validated > 0) {
159 memcpy(storage->get(), input, validated * sizeof(uint16_t));
160 }
161
reed@google.comaec40662014-04-18 19:29:07 +0000162 for (int i = validated; i < numGlyphs; ++i) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000163 storage->get()[i] = input[i];
164 if (input[i] > maxGlyphID) {
165 storage->get()[i] = 0;
166 }
167 }
168 *glyphIDs = storage->get();
169 return numGlyphs;
170}
171
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000172static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX,
173 SkWStream* content) {
174 // Flip the text about the x-axis to account for origin swap and include
175 // the passed parameters.
176 content->writeText("1 0 ");
halcanarybc4696b2015-05-06 10:56:04 -0700177 SkPDFUtils::AppendScalar(0 - textSkewX, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000178 content->writeText(" -1 ");
halcanarybc4696b2015-05-06 10:56:04 -0700179 SkPDFUtils::AppendScalar(x, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000180 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700181 SkPDFUtils::AppendScalar(y, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000182 content->writeText(" Tm\n");
183}
184
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000185// It is important to not confuse GraphicStateEntry with SkPDFGraphicState, the
186// later being our representation of an object in the PDF file.
187struct GraphicStateEntry {
188 GraphicStateEntry();
189
190 // Compare the fields we care about when setting up a new content entry.
191 bool compareInitialState(const GraphicStateEntry& b);
192
193 SkMatrix fMatrix;
194 // We can't do set operations on Paths, though PDF natively supports
195 // intersect. If the clip stack does anything other than intersect,
196 // we have to fall back to the region. Treat fClipStack as authoritative.
197 // See http://code.google.com/p/skia/issues/detail?id=221
198 SkClipStack fClipStack;
199 SkRegion fClipRegion;
200
201 // When emitting the content entry, we will ensure the graphic state
202 // is set to these values first.
203 SkColor fColor;
204 SkScalar fTextScaleX; // Zero means we don't care what the value is.
205 SkPaint::Style fTextFill; // Only if TextScaleX is non-zero.
206 int fShaderIndex;
207 int fGraphicStateIndex;
208
209 // We may change the font (i.e. for Type1 support) within a
halcanary96fcdcc2015-08-27 07:41:13 -0700210 // ContentEntry. This is the one currently in effect, or nullptr if none.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000211 SkPDFFont* fFont;
212 // In PDF, text size has no default value. It is only valid if fFont is
halcanary96fcdcc2015-08-27 07:41:13 -0700213 // not nullptr.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000214 SkScalar fTextSize;
215};
216
217GraphicStateEntry::GraphicStateEntry() : fColor(SK_ColorBLACK),
218 fTextScaleX(SK_Scalar1),
219 fTextFill(SkPaint::kFill_Style),
220 fShaderIndex(-1),
221 fGraphicStateIndex(-1),
halcanary96fcdcc2015-08-27 07:41:13 -0700222 fFont(nullptr),
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000223 fTextSize(SK_ScalarNaN) {
224 fMatrix.reset();
225}
226
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +0000227bool GraphicStateEntry::compareInitialState(const GraphicStateEntry& cur) {
228 return fColor == cur.fColor &&
229 fShaderIndex == cur.fShaderIndex &&
230 fGraphicStateIndex == cur.fGraphicStateIndex &&
231 fMatrix == cur.fMatrix &&
232 fClipStack == cur.fClipStack &&
233 (fTextScaleX == 0 ||
234 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000235}
236
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000237class GraphicStackState {
238public:
239 GraphicStackState(const SkClipStack& existingClipStack,
240 const SkRegion& existingClipRegion,
241 SkWStream* contentStream)
242 : fStackDepth(0),
243 fContentStream(contentStream) {
244 fEntries[0].fClipStack = existingClipStack;
245 fEntries[0].fClipRegion = existingClipRegion;
246 }
247
248 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000249 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000250 void updateMatrix(const SkMatrix& matrix);
251 void updateDrawingState(const GraphicStateEntry& state);
252
253 void drainStack();
254
255private:
256 void push();
257 void pop();
258 GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
259
260 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
261 static const int kMaxStackDepth = 12;
262 GraphicStateEntry fEntries[kMaxStackDepth + 1];
263 int fStackDepth;
264 SkWStream* fContentStream;
265};
266
267void GraphicStackState::drainStack() {
268 while (fStackDepth) {
269 pop();
270 }
271}
272
273void GraphicStackState::push() {
274 SkASSERT(fStackDepth < kMaxStackDepth);
275 fContentStream->writeText("q\n");
276 fStackDepth++;
277 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
278}
279
280void GraphicStackState::pop() {
281 SkASSERT(fStackDepth > 0);
282 fContentStream->writeText("Q\n");
283 fStackDepth--;
284}
285
robertphillips@google.com80214e22012-07-20 15:33:18 +0000286// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000287// and then skips over the leading entries as specified in prefix. It requires
288// and asserts that "prefix" will be a prefix to "stack."
289static void skip_clip_stack_prefix(const SkClipStack& prefix,
290 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000291 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000292 SkClipStack::B2TIter prefixIter(prefix);
293 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000294
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000295 const SkClipStack::Element* prefixEntry;
296 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000297
298 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000299 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000300 iterEntry = iter->next();
301 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000302 // Because of SkClipStack does internal intersection, the last clip
303 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000304 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000305 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
306 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
307 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000308 // back up the iterator by one
309 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000310 prefixEntry = prefixIter.next();
311 break;
312 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000313 }
314
halcanary96fcdcc2015-08-27 07:41:13 -0700315 SkASSERT(prefixEntry == nullptr);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000316}
317
318static void emit_clip(SkPath* clipPath, SkRect* clipRect,
319 SkWStream* contentStream) {
320 SkASSERT(clipPath || clipRect);
321
322 SkPath::FillType clipFill;
323 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000324 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000325 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000326 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000327 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
328 clipFill = SkPath::kWinding_FillType;
329 }
330
331 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
332 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
333 if (clipFill == SkPath::kEvenOdd_FillType) {
334 contentStream->writeText("W* n\n");
335 } else {
336 contentStream->writeText("W n\n");
337 }
338}
339
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000340/* Calculate an inverted path's equivalent non-inverted path, given the
341 * canvas bounds.
342 * outPath may alias with invPath (since this is supported by PathOps).
343 */
344static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
345 SkPath* outPath) {
346 SkASSERT(invPath.isInverseFillType());
347
348 SkPath clipPath;
349 clipPath.addRect(bounds);
350
reedcdb42bb2015-06-26 10:23:07 -0700351 return Op(clipPath, invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000352}
353
fmalita632e92f2015-04-22 15:02:03 -0700354#ifdef SK_PDF_USE_PATHOPS_CLIPPING
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000355// Sanity check the numerical values of the SkRegion ops and PathOps ops
356// enums so region_op_to_pathops_op can do a straight passthrough cast.
357// If these are failing, it may be necessary to make region_op_to_pathops_op
358// do more.
bungeman99fe8222015-08-20 07:57:51 -0700359static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
360static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
361static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
362static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
363static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
364 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000365
366static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
367 SkASSERT(op >= 0);
368 SkASSERT(op <= SkRegion::kReverseDifference_Op);
369 return (SkPathOp)op;
370}
371
372/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
373 * Returns true if successful, or false if not successful.
374 * If successful, the resulting clip is stored in outClipPath.
375 * If not successful, outClipPath is undefined, and a fallback method
376 * should be used.
377 */
378static bool get_clip_stack_path(const SkMatrix& transform,
379 const SkClipStack& clipStack,
380 const SkRegion& clipRegion,
381 SkPath* outClipPath) {
382 outClipPath->reset();
383 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
384
385 const SkClipStack::Element* clipEntry;
386 SkClipStack::Iter iter;
387 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
388 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
389 SkPath entryPath;
390 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
391 outClipPath->reset();
392 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
393 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000394 } else {
395 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000396 }
397 entryPath.transform(transform);
398
399 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
400 *outClipPath = entryPath;
401 } else {
402 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
403 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
404 return false;
405 }
406 }
407 }
408
409 if (outClipPath->isInverseFillType()) {
410 // The bounds are slightly outset to ensure this is correct in the
411 // face of floating-point accuracy and possible SkRegion bitmap
412 // approximations.
413 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
414 clipBounds.outset(SK_Scalar1, SK_Scalar1);
415 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
416 return false;
417 }
418 }
419 return true;
420}
421#endif
422
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000423// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000424// graphic state stack, and the fact that we can know all the clips used
425// on the page to optimize this.
426void GraphicStackState::updateClip(const SkClipStack& clipStack,
427 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000428 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000429 if (clipStack == currentEntry()->fClipStack) {
430 return;
431 }
432
433 while (fStackDepth > 0) {
434 pop();
435 if (clipStack == currentEntry()->fClipStack) {
436 return;
437 }
438 }
439 push();
440
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000441 currentEntry()->fClipStack = clipStack;
442 currentEntry()->fClipRegion = clipRegion;
443
444 SkMatrix transform;
445 transform.setTranslate(translation.fX, translation.fY);
446
fmalita632e92f2015-04-22 15:02:03 -0700447#ifdef SK_PDF_USE_PATHOPS_CLIPPING
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000448 SkPath clipPath;
449 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700450 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000451 return;
452 }
453#endif
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000454 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
455 // already been applied. (If this is a top level device, then it specifies
456 // a clip to the content area. If this is a layer, then it specifies
457 // the clip in effect when the layer was created.) There's no need to
458 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
459 // initial clip on the parent layer. (This means there's a bug if the user
460 // expands the clip and then uses any xfer mode that uses dst:
461 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000462 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000463 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
464
465 // If the clip stack does anything other than intersect or if it uses
466 // an inverse fill type, we have to fall back to the clip region.
467 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000468 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000469 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000470 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
471 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000472 needRegion = true;
473 break;
474 }
475 }
476
477 if (needRegion) {
478 SkPath clipPath;
479 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700480 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000481 } else {
482 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000483 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000484 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000485 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
486 switch (clipEntry->getType()) {
487 case SkClipStack::Element::kRect_Type: {
488 SkRect translatedClip;
489 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700490 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000491 break;
492 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000493 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000494 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000495 clipEntry->asPath(&translatedPath);
496 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700497 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000498 break;
499 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000500 }
501 }
502 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000503}
504
505void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
506 if (matrix == currentEntry()->fMatrix) {
507 return;
508 }
509
510 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
511 SkASSERT(fStackDepth > 0);
512 SkASSERT(fEntries[fStackDepth].fClipStack ==
513 fEntries[fStackDepth -1].fClipStack);
514 pop();
515
516 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
517 }
518 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
519 return;
520 }
521
522 push();
523 SkPDFUtils::AppendTransform(matrix, fContentStream);
524 currentEntry()->fMatrix = matrix;
525}
526
527void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) {
528 // PDF treats a shader as a color, so we only set one or the other.
529 if (state.fShaderIndex >= 0) {
530 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000531 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000532 currentEntry()->fShaderIndex = state.fShaderIndex;
533 }
534 } else {
535 if (state.fColor != currentEntry()->fColor ||
536 currentEntry()->fShaderIndex >= 0) {
537 emit_pdf_color(state.fColor, fContentStream);
538 fContentStream->writeText("RG ");
539 emit_pdf_color(state.fColor, fContentStream);
540 fContentStream->writeText("rg\n");
541 currentEntry()->fColor = state.fColor;
542 currentEntry()->fShaderIndex = -1;
543 }
544 }
545
546 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000547 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000548 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
549 }
550
551 if (state.fTextScaleX) {
552 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
553 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
554 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700555 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000556 fContentStream->writeText(" Tz\n");
557 currentEntry()->fTextScaleX = state.fTextScaleX;
558 }
559 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700560 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
561 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
562 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000563 fContentStream->writeDecAsText(state.fTextFill);
564 fContentStream->writeText(" Tr\n");
565 currentEntry()->fTextFill = state.fTextFill;
566 }
567 }
568}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000569
reed76033be2015-03-14 10:54:31 -0700570static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700571 // PDF does not support image filters, so render them on CPU.
572 // Note that this rendering is done at "screen" resolution (100dpi), not
573 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700574 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800575 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700576
577 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700578 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700579}
580
581SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
halcanary00b7e5e2015-04-15 13:05:18 -0700582 if (cinfo.fForImageFilter ||
583 (layerPaint && not_supported_for_layers(*layerPaint))) {
halcanary96fcdcc2015-08-27 07:41:13 -0700584 return nullptr;
senorblancob0e89dc2014-10-20 14:03:12 -0700585 }
fmalita6987dca2014-11-13 08:33:37 -0800586 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanarya1f1ee92015-02-20 06:17:26 -0800587 return SkPDFDevice::Create(size, fRasterDpi, fCanon);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000588}
589
590
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000591struct ContentEntry {
592 GraphicStateEntry fState;
593 SkDynamicMemoryWStream fContent;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000594 SkAutoTDelete<ContentEntry> fNext;
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000595
596 // If the stack is too deep we could get Stack Overflow.
597 // So we manually destruct the object.
598 ~ContentEntry() {
mtklein18300a32016-03-16 13:53:35 -0700599 ContentEntry* val = fNext.release();
halcanary96fcdcc2015-08-27 07:41:13 -0700600 while (val != nullptr) {
mtklein18300a32016-03-16 13:53:35 -0700601 ContentEntry* valNext = val->fNext.release();
halcanary96fcdcc2015-08-27 07:41:13 -0700602 // When the destructor is called, fNext is nullptr and exits.
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000603 delete val;
604 val = valNext;
605 }
606 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000607};
608
609// A helper class to automatically finish a ContentEntry at the end of a
610// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000611class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000612public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000613 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
614 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000615 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700616 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000617 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700618 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000619 init(draw.fClipStack, *draw.fClip, *draw.fMatrix, paint, hasText);
620 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000621 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
622 const SkRegion& clipRegion, const SkMatrix& matrix,
623 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000624 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700625 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000626 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700627 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000628 init(clipStack, clipRegion, matrix, paint, hasText);
629 }
630
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000631 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000632 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000633 SkPath* shape = &fShape;
634 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700635 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000636 }
637 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000638 }
reed@google.comfc641d02012-09-20 17:52:20 +0000639 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000640 }
641
642 ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000643
644 /* Returns true when we explicitly need the shape of the drawing. */
645 bool needShape() {
646 switch (fXfermode) {
647 case SkXfermode::kClear_Mode:
648 case SkXfermode::kSrc_Mode:
649 case SkXfermode::kSrcIn_Mode:
650 case SkXfermode::kSrcOut_Mode:
651 case SkXfermode::kDstIn_Mode:
652 case SkXfermode::kDstOut_Mode:
653 case SkXfermode::kSrcATop_Mode:
654 case SkXfermode::kDstATop_Mode:
655 case SkXfermode::kModulate_Mode:
656 return true;
657 default:
658 return false;
659 }
660 }
661
662 /* Returns true unless we only need the shape of the drawing. */
663 bool needSource() {
664 if (fXfermode == SkXfermode::kClear_Mode) {
665 return false;
666 }
667 return true;
668 }
669
670 /* If the shape is different than the alpha component of the content, then
671 * setShape should be called with the shape. In particular, images and
672 * devices have rectangular shape.
673 */
674 void setShape(const SkPath& shape) {
675 fShape = shape;
676 }
677
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000678private:
679 SkPDFDevice* fDevice;
680 ContentEntry* fContentEntry;
681 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000682 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000683 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000684
685 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
686 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000687 // Shape has to be flatten before we get here.
688 if (matrix.hasPerspective()) {
689 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000690 return;
691 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000692 if (paint.getXfermode()) {
693 paint.getXfermode()->asMode(&fXfermode);
694 }
695 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
696 matrix, paint, hasText,
697 &fDstFormXObject);
698 }
699};
700
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000701////////////////////////////////////////////////////////////////////////////////
702
halcanary385fe4d2015-08-26 13:07:48 -0700703SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFCanon* canon, bool flip)
robertphillips9a53fd72015-06-22 09:46:59 -0700704 : INHERITED(SkSurfaceProps(0, kUnknown_SkPixelGeometry))
705 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800706 , fContentSize(pageSize)
707 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanary96fcdcc2015-08-27 07:41:13 -0700708 , fLastContentEntry(nullptr)
709 , fLastMarginContentEntry(nullptr)
halcanarya1f1ee92015-02-20 06:17:26 -0800710 , fDrawingArea(kContent_DrawingArea)
halcanary96fcdcc2015-08-27 07:41:13 -0700711 , fClipStack(nullptr)
halcanary385fe4d2015-08-26 13:07:48 -0700712 , fFontGlyphUsage(new SkPDFGlyphSetMap)
halcanarya1f1ee92015-02-20 06:17:26 -0800713 , fRasterDpi(rasterDpi)
714 , fCanon(canon) {
715 SkASSERT(pageSize.width() > 0);
716 SkASSERT(pageSize.height() > 0);
717 fLegacyBitmap.setInfo(
718 SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()));
719 if (flip) {
720 // Skia generally uses the top left as the origin but PDF
721 // natively has the origin at the bottom left. This matrix
722 // corrects for that. But that only needs to be done once, we
723 // don't do it when layering.
724 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
725 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
726 } else {
727 fInitialTransform.setIdentity();
728 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000729}
730
731SkPDFDevice::~SkPDFDevice() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000732 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000733}
734
735void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700736 fContentEntries.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700737 fLastContentEntry = nullptr;
mtklein852f15d2016-03-17 10:51:27 -0700738 fMarginContentEntries.reset();
halcanary96fcdcc2015-08-27 07:41:13 -0700739 fLastMarginContentEntry = nullptr;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000740 fDrawingArea = kContent_DrawingArea;
halcanary96fcdcc2015-08-27 07:41:13 -0700741 if (fFontGlyphUsage.get() == nullptr) {
halcanary385fe4d2015-08-26 13:07:48 -0700742 fFontGlyphUsage.reset(new SkPDFGlyphSetMap);
vandebo@chromium.org98594282011-07-25 22:34:12 +0000743 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000744}
745
vandebo@chromium.org98594282011-07-25 22:34:12 +0000746void SkPDFDevice::cleanUp(bool clearFontUsage) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000747 fGraphicStateResources.unrefAll();
748 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000749 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000750 fShaderResources.unrefAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000751
vandebo@chromium.org98594282011-07-25 22:34:12 +0000752 if (clearFontUsage) {
753 fFontGlyphUsage->reset();
754 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000755}
756
reedf70b5312016-03-04 16:36:20 -0800757void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
758 SkData* value) {
759 if (0 == rect.width() && 0 == rect.height()) {
760 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
761 } else {
762 SkPath path;
763 path.addRect(rect);
764 handlePathAnnotation(path, d, key, value);
765 }
766}
767
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000768void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000769 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700770 replace_srcmode_on_opaque_paint(&newPaint);
771
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000772 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000773 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000774 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000775}
776
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000777void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
778 ContentEntry* contentEntry) {
779 if (!contentEntry) {
780 return;
781 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000782 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
783 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000784 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000785 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000786 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000787 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000788 inverse.mapRect(&bbox);
789
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000790 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000791 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000792 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000793}
794
halcanary682ee012016-01-28 10:59:34 -0800795void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700796 SkCanvas::PointMode mode,
797 size_t count,
798 const SkPoint* points,
799 const SkPaint& srcPaint) {
800 SkPaint passedPaint = srcPaint;
801 replace_srcmode_on_opaque_paint(&passedPaint);
802
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000803 if (count == 0) {
804 return;
805 }
806
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000807 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
808 // We only use this when there's a path effect because of the overhead
809 // of multiple calls to setUpContentEntry it causes.
810 if (passedPaint.getPathEffect()) {
811 if (d.fClip->isEmpty()) {
812 return;
813 }
814 SkDraw pointDraw(d);
815 pointDraw.fDevice = this;
816 pointDraw.drawPoints(mode, count, points, passedPaint, true);
817 return;
818 }
819
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000820 const SkPaint* paint = &passedPaint;
821 SkPaint modifiedPaint;
822
823 if (mode == SkCanvas::kPoints_PointMode &&
824 paint->getStrokeCap() != SkPaint::kRound_Cap) {
825 modifiedPaint = *paint;
826 paint = &modifiedPaint;
827 if (paint->getStrokeWidth()) {
828 // PDF won't draw a single point with square/butt caps because the
829 // orientation is ambiguous. Draw a rectangle instead.
830 modifiedPaint.setStyle(SkPaint::kFill_Style);
831 SkScalar strokeWidth = paint->getStrokeWidth();
832 SkScalar halfStroke = SkScalarHalf(strokeWidth);
833 for (size_t i = 0; i < count; i++) {
834 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
835 r.inset(-halfStroke, -halfStroke);
836 drawRect(d, r, modifiedPaint);
837 }
838 return;
839 } else {
840 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
841 }
842 }
843
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000844 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000845 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000846 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000847 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000848
849 switch (mode) {
850 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000851 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000852 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000853 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000854 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000855 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000856 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000857 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000858 break;
859 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000860 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000861 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000862 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000863 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000864 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000865 &content.entry()->fContent);
866 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000867 }
868 break;
869 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000870 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
871 for (size_t i = 0; i < count; i++) {
872 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000873 &content.entry()->fContent);
874 SkPDFUtils::ClosePath(&content.entry()->fContent);
875 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000876 }
877 break;
878 default:
879 SkASSERT(false);
880 }
881}
882
halcanary8103a342016-03-08 15:10:16 -0800883static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800884 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700885 annotation->insertName("Subtype", "Link");
886
halcanaryece83922016-03-08 08:32:12 -0800887 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700888 border->reserve(3);
889 border->appendInt(0); // Horizontal corner radius.
890 border->appendInt(0); // Vertical corner radius.
891 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800892 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700893
halcanaryece83922016-03-08 08:32:12 -0800894 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700895 rect->reserve(4);
896 rect->appendScalar(translatedRect.fLeft);
897 rect->appendScalar(translatedRect.fTop);
898 rect->appendScalar(translatedRect.fRight);
899 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800900 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700901
halcanary8103a342016-03-08 15:10:16 -0800902 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700903}
904
halcanary8103a342016-03-08 15:10:16 -0800905static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
906 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700907 SkString url(static_cast<const char *>(urlData->data()),
908 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800909 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700910 action->insertName("S", "URI");
911 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800912 annotation->insertObject("A", std::move(action));
913 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700914}
915
halcanary8103a342016-03-08 15:10:16 -0800916static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
917 const SkRect& r) {
918 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700919 SkString name(static_cast<const char *>(nameData->data()),
920 nameData->size() - 1);
921 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800922 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700923}
924
halcanary682ee012016-01-28 10:59:34 -0800925void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700926 const SkRect& rect,
927 const SkPaint& srcPaint) {
928 SkPaint paint = srcPaint;
929 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000930 SkRect r = rect;
931 r.sort();
932
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000933 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000934 if (d.fClip->isEmpty()) {
935 return;
936 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000937 SkPath path;
938 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700939 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000940 return;
941 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000942
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000943 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000944 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000945 return;
946 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000947 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000948 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000949 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000950}
951
halcanarya6814332015-05-27 08:53:36 -0700952void SkPDFDevice::drawRRect(const SkDraw& draw,
953 const SkRRect& rrect,
954 const SkPaint& srcPaint) {
955 SkPaint paint = srcPaint;
956 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000957 SkPath path;
958 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700959 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000960}
961
halcanarya6814332015-05-27 08:53:36 -0700962void SkPDFDevice::drawOval(const SkDraw& draw,
963 const SkRect& oval,
964 const SkPaint& srcPaint) {
965 SkPaint paint = srcPaint;
966 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700967 SkPath path;
968 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700969 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700970}
971
halcanary682ee012016-01-28 10:59:34 -0800972void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700973 const SkPath& origPath,
974 const SkPaint& srcPaint,
975 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000976 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700977 SkPaint paint = srcPaint;
978 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800979 SkPath modifiedPath;
980 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000981
982 SkMatrix matrix = *d.fMatrix;
983 if (prePathMatrix) {
984 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800985 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000986 pathPtr = &modifiedPath;
987 pathIsMutable = true;
988 }
halcanary682ee012016-01-28 10:59:34 -0800989 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000990 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000991 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000992 }
993 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000994
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000995 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000996 if (d.fClip->isEmpty()) {
997 return;
998 }
halcanary682ee012016-01-28 10:59:34 -0800999 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001000 pathPtr = &modifiedPath;
1001 pathIsMutable = true;
1002 }
halcanary682ee012016-01-28 10:59:34 -08001003 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +00001004
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001005 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -07001006 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001007 if (fill) {
1008 noEffectPaint.setStyle(SkPaint::kFill_Style);
1009 } else {
1010 noEffectPaint.setStyle(SkPaint::kStroke_Style);
1011 noEffectPaint.setStrokeWidth(0);
1012 }
halcanary96fcdcc2015-08-27 07:41:13 -07001013 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +00001014 return;
1015 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001016
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001017 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001018 return;
1019 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001020
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001021 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001022 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001023 return;
1024 }
halcanary8b2bc252015-10-06 09:41:47 -07001025 bool consumeDegeratePathSegments =
1026 paint.getStyle() == SkPaint::kFill_Style ||
1027 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
1028 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +00001029 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -07001030 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +00001031 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001032 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001033 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001034}
1035
halcanary7a14b312015-10-01 07:28:13 -07001036void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
1037 const SkBitmap& bitmap,
1038 const SkRect* src,
1039 const SkRect& dst,
1040 const SkPaint& srcPaint,
1041 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001042 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -07001043}
1044
1045void SkPDFDevice::drawBitmap(const SkDraw& d,
1046 const SkBitmap& bitmap,
1047 const SkMatrix& matrix,
1048 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -07001049 SkPaint paint = srcPaint;
1050 if (bitmap.isOpaque()) {
1051 replace_srcmode_on_opaque_paint(&paint);
1052 }
1053
halcanary7a14b312015-10-01 07:28:13 -07001054 if (d.fClip->isEmpty()) {
1055 return;
1056 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001057
halcanary7a14b312015-10-01 07:28:13 -07001058 SkMatrix transform = matrix;
1059 transform.postConcat(*d.fMatrix);
1060 const SkImage* image = fCanon->bitmapToImage(bitmap);
1061 if (!image) {
1062 return;
1063 }
1064 this->internalDrawImage(transform, d.fClipStack, *d.fClip, image, nullptr,
1065 paint);
1066}
1067
1068void SkPDFDevice::drawSprite(const SkDraw& d,
1069 const SkBitmap& bitmap,
1070 int x,
1071 int y,
1072 const SkPaint& srcPaint) {
1073 SkPaint paint = srcPaint;
1074 if (bitmap.isOpaque()) {
1075 replace_srcmode_on_opaque_paint(&paint);
1076 }
1077
1078 if (d.fClip->isEmpty()) {
1079 return;
1080 }
1081
1082 SkMatrix matrix;
1083 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
1084 const SkImage* image = fCanon->bitmapToImage(bitmap);
1085 if (!image) {
1086 return;
1087 }
1088 this->internalDrawImage(matrix, d.fClipStack, *d.fClip, image, nullptr,
1089 paint);
1090}
1091
1092void SkPDFDevice::drawImage(const SkDraw& draw,
1093 const SkImage* image,
1094 SkScalar x,
1095 SkScalar y,
1096 const SkPaint& srcPaint) {
1097 SkPaint paint = srcPaint;
1098 if (!image) {
1099 return;
1100 }
1101 if (image->isOpaque()) {
1102 replace_srcmode_on_opaque_paint(&paint);
1103 }
1104 if (draw.fClip->isEmpty()) {
1105 return;
1106 }
1107 SkMatrix transform = SkMatrix::MakeTrans(x, y);
1108 transform.postConcat(*draw.fMatrix);
1109 this->internalDrawImage(transform, draw.fClipStack, *draw.fClip, image,
1110 nullptr, paint);
1111}
1112
1113void SkPDFDevice::drawImageRect(const SkDraw& draw,
1114 const SkImage* image,
1115 const SkRect* src,
1116 const SkRect& dst,
1117 const SkPaint& srcPaint,
1118 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001119 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001120}
1121
halcanarybb264b72015-04-07 10:40:03 -07001122// Create a PDF string. Maximum length (in bytes) is 65,535.
1123// @param input A string value.
1124// @param len The length of the input array.
1125// @param wideChars True iff the upper byte in each uint16_t is
1126// significant and should be encoded and not
1127// discarded. If true, the upper byte is encoded
1128// first. Otherwise, we assert the upper byte is
1129// zero.
1130static SkString format_wide_string(const uint16_t* input,
1131 size_t len,
1132 bool wideChars) {
1133 if (wideChars) {
1134 SkASSERT(2 * len < 65535);
1135 static const char gHex[] = "0123456789ABCDEF";
1136 SkString result(4 * len + 2);
1137 result[0] = '<';
1138 for (size_t i = 0; i < len; i++) {
1139 result[4 * i + 1] = gHex[(input[i] >> 12) & 0xF];
1140 result[4 * i + 2] = gHex[(input[i] >> 8) & 0xF];
1141 result[4 * i + 3] = gHex[(input[i] >> 4) & 0xF];
1142 result[4 * i + 4] = gHex[(input[i] ) & 0xF];
1143 }
1144 result[4 * len + 1] = '>';
1145 return result;
1146 } else {
1147 SkASSERT(len <= 65535);
1148 SkString tmp(len);
1149 for (size_t i = 0; i < len; i++) {
1150 SkASSERT(0 == input[i] >> 8);
1151 tmp[i] = static_cast<uint8_t>(input[i]);
1152 }
halcanarybc4696b2015-05-06 10:56:04 -07001153 return SkPDFUtils::FormatString(tmp.c_str(), tmp.size());
halcanarybb264b72015-04-07 10:40:03 -07001154 }
1155}
1156
halcanary66a82f32015-10-12 13:05:04 -07001157static void draw_transparent_text(SkPDFDevice* device,
1158 const SkDraw& d,
1159 const void* text, size_t len,
1160 SkScalar x, SkScalar y,
1161 const SkPaint& srcPaint) {
1162
1163 SkPaint transparent;
1164 if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(),
1165 device->getCanon())) {
1166 SkDEBUGFAIL("default typeface should be embeddable");
1167 return; // Avoid infinite loop in release.
1168 }
1169 transparent.setTextSize(srcPaint.getTextSize());
1170 transparent.setColor(SK_ColorTRANSPARENT);
1171 switch (srcPaint.getTextEncoding()) {
1172 case SkPaint::kGlyphID_TextEncoding: {
1173 // Since a glyphId<->Unicode mapping is typeface-specific,
1174 // map back to Unicode first.
1175 size_t glyphCount = len / 2;
1176 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1177 srcPaint.glyphsToUnichars(
1178 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1179 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
1180 device->drawText(d, &unichars[0],
1181 glyphCount * sizeof(SkUnichar),
1182 x, y, transparent);
1183 break;
1184 }
1185 case SkPaint::kUTF8_TextEncoding:
1186 case SkPaint::kUTF16_TextEncoding:
1187 case SkPaint::kUTF32_TextEncoding:
1188 transparent.setTextEncoding(srcPaint.getTextEncoding());
1189 device->drawText(d, text, len, x, y, transparent);
1190 break;
1191 default:
1192 SkFAIL("unknown text encoding");
1193 }
1194}
1195
1196
halcanary682ee012016-01-28 10:59:34 -08001197void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
halcanarya6814332015-05-27 08:53:36 -07001198 SkScalar x, SkScalar y, const SkPaint& srcPaint) {
halcanary66a82f32015-10-12 13:05:04 -07001199 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fCanon)) {
halcanary6950de62015-11-07 05:29:00 -08001200 // https://bug.skia.org/3866
halcanary66a82f32015-10-12 13:05:04 -07001201 SkPath path;
1202 srcPaint.getTextPath(text, len, x, y, &path);
1203 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1204 // Draw text transparently to make it copyable/searchable/accessable.
1205 draw_transparent_text(this, d, text, len, x, y, srcPaint);
1206 return;
1207 }
halcanarya6814332015-05-27 08:53:36 -07001208 SkPaint paint = srcPaint;
1209 replace_srcmode_on_opaque_paint(&paint);
1210
halcanary96fcdcc2015-08-27 07:41:13 -07001211 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1212 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001213 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1214 // making text unreadable (e.g. same text twice when using CSS shadows).
1215 return;
1216 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001217 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001218 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001219 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001220 return;
1221 }
1222
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001223 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001224 const uint16_t* glyphIDs = nullptr;
reed@google.comaec40662014-04-18 19:29:07 +00001225 int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001226 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001227
benjaminwagnerd936f632016-02-23 10:44:31 -08001228 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001229 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001230 content.entry()->fContent.writeText("BT\n");
1231 set_text_transform(x, y, textPaint.getTextSkewX(),
1232 &content.entry()->fContent);
reed@google.comaec40662014-04-18 19:29:07 +00001233 int consumedGlyphCount = 0;
halcanary2f912f32014-10-16 09:53:20 -07001234
1235 SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs);
1236
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001237 while (numGlyphs > consumedGlyphCount) {
robertphillips8e0c1502015-07-07 10:28:43 -07001238 this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001239 SkPDFFont* font = content.entry()->fState.fFont;
halcanary2f912f32014-10-16 09:53:20 -07001240
1241 int availableGlyphs = font->glyphsToPDFFontEncoding(
1242 glyphIDsCopy.begin() + consumedGlyphCount,
1243 numGlyphs - consumedGlyphCount);
1244 fFontGlyphUsage->noteGlyphUsage(
1245 font, glyphIDsCopy.begin() + consumedGlyphCount,
1246 availableGlyphs);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001247 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001248 format_wide_string(glyphIDsCopy.begin() + consumedGlyphCount,
1249 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001250 content.entry()->fContent.writeText(encodedString.c_str());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001251 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001252 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001253 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001254 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001255}
1256
halcanary682ee012016-01-28 10:59:34 -08001257void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -07001258 const SkScalar pos[], int scalarsPerPos,
halcanary682ee012016-01-28 10:59:34 -08001259 const SkPoint& offset, const SkPaint& srcPaint) {
halcanary66a82f32015-10-12 13:05:04 -07001260 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fCanon)) {
1261 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos);
1262 SkAutoTMalloc<SkPoint> positionsBuffer;
1263 if (2 != scalarsPerPos) {
1264 int glyphCount = srcPaint.textToGlyphs(text, len, NULL);
1265 positionsBuffer.reset(glyphCount);
1266 for (int i = 0; i < glyphCount; ++i) {
1267 positionsBuffer[i].set(pos[i], 0.0f);
1268 }
1269 positions = &positionsBuffer[0];
1270 }
1271 SkPath path;
1272 srcPaint.getPosTextPath(text, len, positions, &path);
1273 SkMatrix matrix;
1274 matrix.setTranslate(offset);
1275 this->drawPath(d, path, srcPaint, &matrix, true);
1276 // Draw text transparently to make it copyable/searchable/accessable.
1277 draw_transparent_text(
1278 this, d, text, len, offset.x() + positions[0].x(),
1279 offset.y() + positions[0].y(), srcPaint);
1280 return;
1281 }
1282
halcanarya6814332015-05-27 08:53:36 -07001283 SkPaint paint = srcPaint;
1284 replace_srcmode_on_opaque_paint(&paint);
1285
halcanary96fcdcc2015-08-27 07:41:13 -07001286 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1287 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001288 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1289 // making text unreadable (e.g. same text twice when using CSS shadows).
1290 return;
1291 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001292 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001293 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001294 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001295 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001296 return;
1297 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001298
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001299 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001300 const uint16_t* glyphIDs = nullptr;
bungeman22edc832014-10-03 07:55:58 -07001301 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001302 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001303
benjaminwagnerd936f632016-02-23 10:44:31 -08001304 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001305 content.entry()->fContent.writeText("BT\n");
robertphillips8e0c1502015-07-07 10:28:43 -07001306 this->updateFont(textPaint, glyphIDs[0], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001307 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001308 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001309 uint16_t encodedValue = glyphIDs[i];
1310 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
bungeman22edc832014-10-03 07:55:58 -07001311 // The current pdf font cannot encode the current glyph.
1312 // Try to get a pdf font which can encode the current glyph.
robertphillips8e0c1502015-07-07 10:28:43 -07001313 this->updateFont(textPaint, glyphIDs[i], content.entry());
bungeman22edc832014-10-03 07:55:58 -07001314 font = content.entry()->fState.fFont;
1315 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
1316 SkDEBUGFAIL("PDF could not encode glyph.");
1317 continue;
1318 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001319 }
bungeman22edc832014-10-03 07:55:58 -07001320
vandebo@chromium.org98594282011-07-25 22:34:12 +00001321 fFontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
fmalita05c4a432014-09-29 06:29:53 -07001322 SkScalar x = offset.x() + pos[i * scalarsPerPos];
1323 SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0);
1324
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001325 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
bungeman22edc832014-10-03 07:55:58 -07001326 set_text_transform(x, y, textPaint.getTextSkewX(), &content.entry()->fContent);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001327 SkString encodedString =
halcanarybb264b72015-04-07 10:40:03 -07001328 format_wide_string(&encodedValue, 1, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001329 content.entry()->fContent.writeText(encodedString.c_str());
1330 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001331 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001332 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001333}
1334
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001335void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001336 int vertexCount, const SkPoint verts[],
1337 const SkPoint texs[], const SkColor colors[],
1338 SkXfermode* xmode, const uint16_t indices[],
1339 int indexCount, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001340 if (d.fClip->isEmpty()) {
1341 return;
1342 }
reed@google.com85e143c2013-12-30 15:51:25 +00001343 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001344}
1345
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001346void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1347 int x, int y, const SkPaint& paint) {
fmalita6987dca2014-11-13 08:33:37 -08001348 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001349 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001350
1351 SkScalar scalarX = SkIntToScalar(x);
1352 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001353 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1354 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001355 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001356 }
halcanary91fcb3e2016-03-04 13:53:22 -08001357 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1358 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001359 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001360 }
halcanary91fcb3e2016-03-04 13:53:22 -08001361 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1362 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001363 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001364 }
1365
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001366 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001367 return;
1368 }
1369
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001370 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001371 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001372 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001373 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001374 return;
1375 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001376 if (content.needShape()) {
1377 SkPath shape;
1378 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001379 SkIntToScalar(device->width()),
1380 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001381 content.setShape(shape);
1382 }
1383 if (!content.needSource()) {
1384 return;
1385 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001386
halcanaryece83922016-03-08 08:32:12 -08001387 auto xObject = sk_make_sp<SkPDFFormXObject>(pdfDevice);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001388 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001389 &content.entry()->fContent);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001390
1391 // Merge glyph sets from the drawn device.
1392 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001393}
1394
reed89443ab2014-06-27 11:34:19 -07001395SkImageInfo SkPDFDevice::imageInfo() const {
1396 return fLegacyBitmap.info();
1397}
1398
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001399void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1400 INHERITED::onAttachToCanvas(canvas);
1401
1402 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1403 fClipStack = canvas->getClipStack();
1404}
1405
1406void SkPDFDevice::onDetachFromCanvas() {
1407 INHERITED::onDetachFromCanvas();
1408
halcanary96fcdcc2015-08-27 07:41:13 -07001409 fClipStack = nullptr;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001410}
1411
reed4a8126e2014-09-22 07:29:03 -07001412SkSurface* SkPDFDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1413 return SkSurface::NewRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001414}
1415
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001416ContentEntry* SkPDFDevice::getLastContentEntry() {
1417 if (fDrawingArea == kContent_DrawingArea) {
1418 return fLastContentEntry;
1419 } else {
1420 return fLastMarginContentEntry;
1421 }
1422}
1423
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001424SkAutoTDelete<ContentEntry>* SkPDFDevice::getContentEntries() {
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001425 if (fDrawingArea == kContent_DrawingArea) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001426 return &fContentEntries;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001427 } else {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001428 return &fMarginContentEntries;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001429 }
1430}
1431
1432void SkPDFDevice::setLastContentEntry(ContentEntry* contentEntry) {
1433 if (fDrawingArea == kContent_DrawingArea) {
1434 fLastContentEntry = contentEntry;
1435 } else {
1436 fLastMarginContentEntry = contentEntry;
1437 }
1438}
1439
1440void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001441 // A ScopedContentEntry only exists during the course of a draw call, so
1442 // this can't be called while a ScopedContentEntry exists.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001443 fDrawingArea = drawingArea;
1444}
1445
halcanary8103a342016-03-08 15:10:16 -08001446sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001447 SkTDArray<SkPDFObject*> fonts;
1448 fonts.setReserve(fFontResources.count());
1449 for (SkPDFFont* font : fFontResources) {
1450 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001451 }
halcanary8103a342016-03-08 15:10:16 -08001452 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001453 &fGraphicStateResources,
1454 &fShaderResources,
1455 &fXObjectResources,
1456 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001457}
1458
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001459const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1460 return fFontResources;
1461}
1462
halcanary8103a342016-03-08 15:10:16 -08001463sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001464 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001465 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001466 mediaBox->appendInt(0);
1467 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001468 mediaBox->appendInt(fPageSize.width());
1469 mediaBox->appendInt(fPageSize.height());
1470 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001471}
1472
mtklein5f939ab2016-03-16 10:28:35 -07001473std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001474 SkDynamicMemoryWStream buffer;
1475 this->writeContent(&buffer);
mtklein5f939ab2016-03-16 10:28:35 -07001476 return std::unique_ptr<SkStreamAsset>(
halcanary8103a342016-03-08 15:10:16 -08001477 buffer.bytesWritten() > 0
1478 ? buffer.detachAsStream()
1479 : new SkMemoryStream);
reed@google.com5667afc2011-06-27 14:42:15 +00001480}
1481
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001482void SkPDFDevice::copyContentEntriesToData(ContentEntry* entry,
1483 SkWStream* data) const {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001484 // TODO(ctguil): For margins, I'm not sure fExistingClipStack/Region is the
1485 // right thing to pass here.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001486 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, data);
halcanary96fcdcc2015-08-27 07:41:13 -07001487 while (entry != nullptr) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +00001488 SkPoint translation;
1489 translation.iset(this->getOrigin());
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001490 translation.negate();
1491 gsState.updateClip(entry->fState.fClipStack, entry->fState.fClipRegion,
1492 translation);
1493 gsState.updateMatrix(entry->fState.fMatrix);
1494 gsState.updateDrawingState(entry->fState);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001495
halcanary7af21502015-02-23 12:17:59 -08001496 entry->fContent.writeToStream(data);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001497 entry = entry->fNext.get();
1498 }
1499 gsState.drainStack();
1500}
1501
halcanary334fcbc2015-02-24 12:56:16 -08001502void SkPDFDevice::writeContent(SkWStream* out) const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001503 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanary334fcbc2015-02-24 12:56:16 -08001504 SkPDFUtils::AppendTransform(fInitialTransform, out);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001505 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001506
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001507 // TODO(aayushkumar): Apply clip along the margins. Currently, webkit
1508 // colors the contentArea white before it starts drawing into it and
1509 // that currently acts as our clip.
1510 // Also, think about adding a transform here (or assume that the values
1511 // sent across account for that)
halcanary334fcbc2015-02-24 12:56:16 -08001512 SkPDFDevice::copyContentEntriesToData(fMarginContentEntries.get(), out);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001513
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001514 // If the content area is the entire page, then we don't need to clip
1515 // the content area (PDF area clips to the page size). Otherwise,
1516 // we have to clip to the content area; we've already applied the
1517 // initial transform, so just clip to the device size.
1518 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001519 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1520 SkIntToScalar(this->height()));
halcanary96fcdcc2015-08-27 07:41:13 -07001521 emit_clip(nullptr, &r, out);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001522 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001523
halcanary334fcbc2015-02-24 12:56:16 -08001524 SkPDFDevice::copyContentEntriesToData(fContentEntries.get(), out);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001525}
1526
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001527/* Draws an inverse filled path by using Path Ops to compute the positive
1528 * inverse using the current clip as the inverse bounds.
1529 * Return true if this was an inverse path and was properly handled,
1530 * otherwise returns false and the normal drawing routine should continue,
1531 * either as a (incorrect) fallback or because the path was not inverse
1532 * in the first place.
1533 */
1534bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001535 const SkPaint& paint, bool pathIsMutable,
1536 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001537 if (!origPath.isInverseFillType()) {
1538 return false;
1539 }
1540
1541 if (d.fClip->isEmpty()) {
1542 return false;
1543 }
1544
1545 SkPath modifiedPath;
1546 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1547 SkPaint noInversePaint(paint);
1548
1549 // Merge stroking operations into final path.
1550 if (SkPaint::kStroke_Style == paint.getStyle() ||
1551 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1552 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1553 if (doFillPath) {
1554 noInversePaint.setStyle(SkPaint::kFill_Style);
1555 noInversePaint.setStrokeWidth(0);
1556 pathPtr = &modifiedPath;
1557 } else {
1558 // To be consistent with the raster output, hairline strokes
1559 // are rendered as non-inverted.
1560 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001561 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001562 return true;
1563 }
1564 }
1565
1566 // Get bounds of clip in current transform space
1567 // (clip bounds are given in device space).
1568 SkRect bounds;
1569 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001570 SkMatrix totalMatrix = *d.fMatrix;
1571 if (prePathMatrix) {
1572 totalMatrix.preConcat(*prePathMatrix);
1573 }
1574 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001575 return false;
1576 }
1577 bounds.set(d.fClip->getBounds());
1578 transformInverse.mapRect(&bounds);
1579
1580 // Extend the bounds by the line width (plus some padding)
1581 // so the edge doesn't cause a visible stroke.
1582 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1583 paint.getStrokeWidth() + SK_Scalar1);
1584
1585 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1586 return false;
1587 }
1588
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001589 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001590 return true;
1591}
1592
reedf70b5312016-03-04 16:36:20 -08001593void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001594 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001595 const char key[], SkData* value) {
1596 if (!value) {
1597 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001598 }
reedf70b5312016-03-04 16:36:20 -08001599
1600 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1601 SkPoint transformedPoint;
1602 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1603 fNamedDestinations.emplace_back(value, transformedPoint);
1604 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001605}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001606
reedf70b5312016-03-04 16:36:20 -08001607void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001608 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001609 const char key[], SkData* value) {
1610 if (!value) {
1611 return;
1612 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001613
1614 SkPath transformedPath = path;
1615 transformedPath.transform(*d.fMatrix);
1616 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001617 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1618 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001619 SkRect transformedRect = SkRect::Make(clip.getBounds());
1620
reedf70b5312016-03-04 16:36:20 -08001621 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001622 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001623 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001624 }
reedf70b5312016-03-04 16:36:20 -08001625 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001626 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001627 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001628 }
reed16108352016-03-03 09:14:36 -08001629 }
halcanary438de492015-04-28 06:21:01 -07001630}
1631
wangxianzhuef6c50a2015-09-17 20:38:02 -07001632void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1633 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001634 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001635 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001636 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001637 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001638 }
halcanary91fcb3e2016-03-04 13:53:22 -08001639 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001640 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001641 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001642 array->appendObject(
1643 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001644 }
1645}
epoger@google.comb58772f2013-03-08 09:09:10 +00001646
halcanary6d622702015-03-25 08:45:42 -07001647void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001648 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001649 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001650 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001651 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001652 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001653 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001654 pdfDest->appendScalar(p.x());
1655 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001656 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001657 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001658 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001659 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001660}
1661
reed@google.comfc641d02012-09-20 17:52:20 +00001662SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
halcanary385fe4d2015-08-26 13:07:48 -07001663 SkPDFFormXObject* xobject = new SkPDFFormXObject(this);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001664 // We always draw the form xobjects that we create back into the device, so
1665 // we simply preserve the font usage instead of pulling it out and merging
1666 // it back in later.
1667 cleanUp(false); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001668 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001669 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001670}
1671
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001672void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
1673 SkPDFFormXObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001674 const SkClipStack* clipStack,
1675 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001676 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001677 bool invertClip) {
1678 if (clipRegion.isEmpty() && !invertClip) {
1679 return;
1680 }
1681
halcanary1437c1e2016-03-13 18:30:24 -07001682 auto sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
1683 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fCanon);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001684
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001685 SkMatrix identity;
1686 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001687 SkPaint paint;
1688 paint.setXfermodeMode(mode);
1689 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001690 if (!content.entry()) {
1691 return;
1692 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001693 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001694 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001695 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001696
halcanary1437c1e2016-03-13 18:30:24 -07001697 // Call makeNoSmaskGraphicState() instead of
1698 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1699 // can deduplicate.
1700 sMaskGS = fCanon->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001701 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001702 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001703}
1704
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001705ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
1706 const SkRegion& clipRegion,
1707 const SkMatrix& matrix,
1708 const SkPaint& paint,
1709 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001710 SkPDFFormXObject** dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001711 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001712 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001713 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001714 }
1715
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001716 // The clip stack can come from an SkDraw where it is technically optional.
1717 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001718 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001719 if (clipRegion == fExistingClipRegion) {
1720 clipStack = &fExistingClipStack;
1721 } else {
1722 // GraphicStackState::updateClip expects the clip stack to have
1723 // fExistingClip as a prefix, so start there, then set the clip
1724 // to the passed region.
1725 synthesizedClipStack = fExistingClipStack;
1726 SkPath clipPath;
1727 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001728 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1729 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001730 clipStack = &synthesizedClipStack;
1731 }
1732 }
1733
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001734 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1735 if (paint.getXfermode()) {
1736 paint.getXfermode()->asMode(&xfermode);
1737 }
1738
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001739 // For the following modes, we want to handle source and destination
1740 // separately, so make an object of what's already there.
1741 if (xfermode == SkXfermode::kClear_Mode ||
1742 xfermode == SkXfermode::kSrc_Mode ||
1743 xfermode == SkXfermode::kSrcIn_Mode ||
1744 xfermode == SkXfermode::kDstIn_Mode ||
1745 xfermode == SkXfermode::kSrcOut_Mode ||
1746 xfermode == SkXfermode::kDstOut_Mode ||
1747 xfermode == SkXfermode::kSrcATop_Mode ||
1748 xfermode == SkXfermode::kDstATop_Mode ||
1749 xfermode == SkXfermode::kModulate_Mode) {
1750 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001751 *dst = createFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001752 SkASSERT(isContentEmpty());
1753 } else if (xfermode != SkXfermode::kSrc_Mode &&
1754 xfermode != SkXfermode::kSrcOut_Mode) {
1755 // Except for Src and SrcOut, if there isn't anything already there,
1756 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001757 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001758 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001759 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001760 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001761 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001762
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001763 // Dst xfer mode doesn't draw source at all.
1764 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001765 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001766 }
1767
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001768 ContentEntry* entry;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001769 SkAutoTDelete<ContentEntry> newEntry;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001770
1771 ContentEntry* lastContentEntry = getLastContentEntry();
1772 if (lastContentEntry && lastContentEntry->fContent.getOffset() == 0) {
1773 entry = lastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001774 } else {
1775 newEntry.reset(new ContentEntry);
1776 entry = newEntry.get();
1777 }
1778
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001779 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001780 hasText, &entry->fState);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001781 if (lastContentEntry && xfermode != SkXfermode::kDstOver_Mode &&
1782 entry->fState.compareInitialState(lastContentEntry->fState)) {
1783 return lastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001784 }
1785
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001786 SkAutoTDelete<ContentEntry>* contentEntries = getContentEntries();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001787 if (!lastContentEntry) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001788 contentEntries->reset(entry);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001789 setLastContentEntry(entry);
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001790 } else if (xfermode == SkXfermode::kDstOver_Mode) {
mtklein18300a32016-03-16 13:53:35 -07001791 entry->fNext.reset(contentEntries->release());
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001792 contentEntries->reset(entry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001793 } else {
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001794 lastContentEntry->fNext.reset(entry);
1795 setLastContentEntry(entry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001796 }
halcanaryfcad44b2016-03-06 14:47:10 -08001797 newEntry.release();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001798 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001799}
1800
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001801void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001802 SkPDFFormXObject* dst,
1803 SkPath* shape) {
1804 if (xfermode != SkXfermode::kClear_Mode &&
1805 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001806 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001807 xfermode != SkXfermode::kSrcIn_Mode &&
1808 xfermode != SkXfermode::kDstIn_Mode &&
1809 xfermode != SkXfermode::kSrcOut_Mode &&
1810 xfermode != SkXfermode::kDstOut_Mode &&
1811 xfermode != SkXfermode::kSrcATop_Mode &&
1812 xfermode != SkXfermode::kDstATop_Mode &&
1813 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001814 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001815 return;
1816 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001817 if (xfermode == SkXfermode::kDstOver_Mode) {
1818 SkASSERT(!dst);
1819 ContentEntry* firstContentEntry = getContentEntries()->get();
1820 if (firstContentEntry->fContent.getOffset() == 0) {
1821 // For DstOver, an empty content entry was inserted before the rest
1822 // of the content entries. If nothing was drawn, it needs to be
1823 // removed.
1824 SkAutoTDelete<ContentEntry>* contentEntries = getContentEntries();
mtklein18300a32016-03-16 13:53:35 -07001825 contentEntries->reset(firstContentEntry->fNext.release());
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001826 }
1827 return;
1828 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001829 if (!dst) {
1830 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1831 xfermode == SkXfermode::kSrcOut_Mode);
1832 return;
1833 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001834
1835 ContentEntry* contentEntries = getContentEntries()->get();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001836 SkASSERT(dst);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001837 SkASSERT(!contentEntries->fNext.get());
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001838 // Changing the current content into a form-xobject will destroy the clip
1839 // objects which is fine since the xobject will already be clipped. However
1840 // if source has shape, we need to clip it too, so a copy of the clip is
1841 // saved.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001842 SkClipStack clipStack = contentEntries->fState.fClipStack;
1843 SkRegion clipRegion = contentEntries->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001844
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001845 SkMatrix identity;
1846 identity.reset();
1847 SkPaint stockPaint;
1848
halcanary48810a02016-03-07 14:57:50 -08001849 sk_sp<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001850 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001851 // If nothing was drawn and there's no shape, then the draw was a
1852 // no-op, but dst needs to be restored for that to be true.
1853 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1854 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1855 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001856 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001857 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001858 ScopedContentEntry content(this, &fExistingClipStack,
1859 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001860 stockPaint);
1861 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1862 &content.entry()->fContent);
1863 return;
1864 } else {
1865 xfermode = SkXfermode::kClear_Mode;
1866 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001867 } else {
1868 SkASSERT(!fContentEntries->fNext.get());
reed@google.comfc641d02012-09-20 17:52:20 +00001869 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001870 }
1871
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001872 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1873 // without alpha.
1874 if (xfermode == SkXfermode::kSrcATop_Mode) {
1875 // TODO(vandebo): In order to properly support SrcATop we have to track
1876 // the shape of what's been drawn at all times. It's the intersection of
1877 // the non-transparent parts of the device and the outlines (shape) of
1878 // all images and devices drawn.
1879 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001880 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001881 SkXfermode::kSrcOver_Mode, true);
1882 } else {
halcanary48810a02016-03-07 14:57:50 -08001883 sk_sp<SkPDFFormXObject> dstMaskStorage;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001884 SkPDFFormXObject* dstMask = srcFormXObject.get();
halcanary96fcdcc2015-08-27 07:41:13 -07001885 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001886 // Draw shape into a form-xobject.
1887 SkDraw d;
1888 d.fMatrix = &identity;
1889 d.fClip = &clipRegion;
1890 d.fClipStack = &clipStack;
1891 SkPaint filledPaint;
1892 filledPaint.setColor(SK_ColorBLACK);
1893 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001894 this->drawPath(d, *shape, filledPaint, nullptr, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001895
1896 dstMaskStorage.reset(createFormXObjectFromDevice());
1897 dstMask = dstMaskStorage.get();
1898 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001899 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1900 &fExistingClipStack, fExistingClipRegion,
1901 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001902 }
1903
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001904 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001905 return;
1906 } else if (xfermode == SkXfermode::kSrc_Mode ||
1907 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001908 ScopedContentEntry content(this, &fExistingClipStack,
1909 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001910 if (content.entry()) {
1911 SkPDFUtils::DrawFormXObject(
1912 this->addXObjectResource(srcFormXObject.get()),
1913 &content.entry()->fContent);
1914 }
1915 if (xfermode == SkXfermode::kSrc_Mode) {
1916 return;
1917 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001918 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001919 ScopedContentEntry content(this, &fExistingClipStack,
1920 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001921 if (content.entry()) {
1922 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1923 &content.entry()->fContent);
1924 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001925 }
1926
1927 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1928 xfermode == SkXfermode::kDstIn_Mode ||
1929 xfermode == SkXfermode::kSrcOut_Mode ||
1930 xfermode == SkXfermode::kDstOut_Mode ||
1931 xfermode == SkXfermode::kSrcATop_Mode ||
1932 xfermode == SkXfermode::kDstATop_Mode ||
1933 xfermode == SkXfermode::kModulate_Mode);
1934
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001935 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001936 xfermode == SkXfermode::kSrcOut_Mode ||
1937 xfermode == SkXfermode::kSrcATop_Mode) {
1938 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001939 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001940 SkXfermode::kSrcOver_Mode,
1941 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001942 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001943 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
1944 if (xfermode == SkXfermode::kModulate_Mode) {
1945 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001946 dst, &fExistingClipStack,
1947 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001948 SkXfermode::kSrcOver_Mode, false);
1949 mode = SkXfermode::kMultiply_Mode;
1950 }
1951 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001952 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001953 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001954 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001955}
1956
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001957bool SkPDFDevice::isContentEmpty() {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001958 ContentEntry* contentEntries = getContentEntries()->get();
1959 if (!contentEntries || contentEntries->fContent.getOffset() == 0) {
1960 SkASSERT(!contentEntries || !contentEntries->fNext.get());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001961 return true;
1962 }
1963 return false;
1964}
1965
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001966void SkPDFDevice::populateGraphicStateEntryFromPaint(
1967 const SkMatrix& matrix,
1968 const SkClipStack& clipStack,
1969 const SkRegion& clipRegion,
1970 const SkPaint& paint,
1971 bool hasText,
1972 GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001973 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1974 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1975 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001976
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001977 entry->fMatrix = matrix;
1978 entry->fClipStack = clipStack;
1979 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001980 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1981 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001982
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001983 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001984 sk_sp<SkPDFObject> pdfShader;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001985 const SkShader* shader = paint.getShader();
1986 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001987 if (shader) {
1988 // PDF positions patterns relative to the initial transform, so
1989 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001990 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001991 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001992
1993 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001994 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001995 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001996
1997 // We need to apply the initial transform to bounds in order to get
1998 // bounds in a consistent coordinate system.
1999 SkRect boundsTemp;
2000 boundsTemp.set(bounds);
2001 fInitialTransform.mapRect(&boundsTemp);
2002 boundsTemp.roundOut(&bounds);
2003
halcanary792c80f2015-02-20 07:21:05 -08002004 SkScalar rasterScale =
2005 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
2006 pdfShader.reset(SkPDFShader::GetPDFShader(
2007 fCanon, fRasterDpi, *shader, transform, bounds, rasterScale));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002008
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00002009 if (pdfShader.get()) {
2010 // pdfShader has been canonicalized so we can directly compare
2011 // pointers.
2012 int resourceIndex = fShaderResources.find(pdfShader.get());
2013 if (resourceIndex < 0) {
2014 resourceIndex = fShaderResources.count();
2015 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002016 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00002017 }
2018 entry->fShaderIndex = resourceIndex;
2019 } else {
2020 // A color shader is treated as an invalid shader so we don't have
2021 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002022 SkShader::GradientInfo gradientInfo;
2023 SkColor gradientColor;
2024 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07002025 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002026 gradientInfo.fColorCount = 1;
2027 if (shader->asAGradient(&gradientInfo) ==
2028 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002029 entry->fColor = SkColorSetA(gradientColor, 0xFF);
2030 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002031 }
2032 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002033 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002034
halcanary48810a02016-03-07 14:57:50 -08002035 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002036 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002037 newGraphicState.reset(
halcanary792c80f2015-02-20 07:21:05 -08002038 SkPDFGraphicState::GetGraphicStateForPaint(fCanon, paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002039 } else {
2040 SkPaint newPaint = paint;
2041 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002042 newGraphicState.reset(
halcanary792c80f2015-02-20 07:21:05 -08002043 SkPDFGraphicState::GetGraphicStateForPaint(fCanon, newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002044 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002045 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002046 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002047
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002048 if (hasText) {
2049 entry->fTextScaleX = paint.getTextScaleX();
2050 entry->fTextFill = paint.getStyle();
2051 } else {
2052 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002053 }
2054}
2055
halcanarybe27a112015-04-01 13:31:19 -07002056int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002057 // Assumes that gs has been canonicalized (so we can directly compare
2058 // pointers).
2059 int result = fGraphicStateResources.find(gs);
2060 if (result < 0) {
2061 result = fGraphicStateResources.count();
2062 fGraphicStateResources.push(gs);
2063 gs->ref();
2064 }
2065 return result;
2066}
2067
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002068int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
2069 // Assumes that xobject has been canonicalized (so we can directly compare
2070 // pointers).
2071 int result = fXObjectResources.find(xObject);
2072 if (result < 0) {
2073 result = fXObjectResources.count();
2074 fXObjectResources.push(xObject);
2075 xObject->ref();
2076 }
2077 return result;
2078}
2079
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002080void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
2081 ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002082 SkTypeface* typeface = paint.getTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -07002083 if (contentEntry->fState.fFont == nullptr ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002084 contentEntry->fState.fTextSize != paint.getTextSize() ||
2085 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002086 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00002087 contentEntry->fContent.writeText("/");
2088 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
2089 SkPDFResourceDict::kFont_ResourceType,
2090 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002091 contentEntry->fContent.writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -07002092 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002093 contentEntry->fContent.writeText(" Tf\n");
2094 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002095 }
2096}
2097
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002098int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08002099 sk_sp<SkPDFFont> newFont(
halcanary792c80f2015-02-20 07:21:05 -08002100 SkPDFFont::GetFontResource(fCanon, typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002101 int resourceIndex = fFontResources.find(newFont.get());
2102 if (resourceIndex < 0) {
2103 resourceIndex = fFontResources.count();
2104 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002105 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002106 }
2107 return resourceIndex;
2108}
2109
halcanary7a14b312015-10-01 07:28:13 -07002110static SkSize rect_to_size(const SkRect& r) {
2111 return SkSize::Make(r.width(), r.height());
2112}
2113
2114static const SkImage* color_filter(const SkImage* image,
2115 SkColorFilter* colorFilter) {
halcanary48810a02016-03-07 14:57:50 -08002116 sk_sp<SkSurface> surface(SkSurface::NewRaster(
halcanary7a14b312015-10-01 07:28:13 -07002117 SkImageInfo::MakeN32Premul(image->dimensions())));
2118 if (!surface) {
2119 return image;
2120 }
2121 SkCanvas* canvas = surface->getCanvas();
2122 canvas->clear(SK_ColorTRANSPARENT);
2123 SkPaint paint;
2124 paint.setColorFilter(colorFilter);
2125 canvas->drawImage(image, 0, 0, &paint);
2126 canvas->flush();
reed9ce9d672016-03-17 10:51:11 -07002127 return surface->makeImageSnapshot().release();
halcanary7a14b312015-10-01 07:28:13 -07002128}
2129
2130////////////////////////////////////////////////////////////////////////////////
2131void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
2132 const SkClipStack* clipStack,
2133 const SkRegion& origClipRegion,
2134 const SkImage* image,
2135 const SkIRect* srcRect,
2136 const SkPaint& paint) {
2137 SkASSERT(image);
2138 #ifdef SK_PDF_IMAGE_STATS
2139 gDrawImageCalls.fetch_add(1);
2140 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002141 SkMatrix matrix = origMatrix;
2142 SkRegion perspectiveBounds;
2143 const SkRegion* clipRegion = &origClipRegion;
halcanary48810a02016-03-07 14:57:50 -08002144 sk_sp<const SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002145
halcanary7a14b312015-10-01 07:28:13 -07002146 if (srcRect) {
reed9ce9d672016-03-17 10:51:11 -07002147 autoImageUnref = image->makeSubset(*srcRect);
halcanary7a14b312015-10-01 07:28:13 -07002148 if (!autoImageUnref) {
2149 return;
2150 }
halcanaryfcad44b2016-03-06 14:47:10 -08002151 image = autoImageUnref.get();
halcanary7a14b312015-10-01 07:28:13 -07002152 }
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002153 // Rasterize the bitmap using perspective in a new bitmap.
2154 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002155 if (fRasterDpi == 0) {
2156 return;
2157 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002158 // Transform the bitmap in the new space, without taking into
2159 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002160 SkPath perspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002161 SkRect imageBounds = SkRect::Make(image->bounds());
2162 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002163 perspectiveOutline.transform(origMatrix);
2164
2165 // TODO(edisonn): perf - use current clip too.
2166 // Retrieve the bounds of the new shape.
2167 SkRect bounds = perspectiveOutline.getBounds();
2168
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002169 // Transform the bitmap in the new space, taking into
2170 // account the initial transform.
2171 SkMatrix total = origMatrix;
2172 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07002173 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
2174 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2175 total.postScale(dpiScale, dpiScale);
2176
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002177 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002178 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002179 physicalPerspectiveOutline.transform(total);
2180
halcanary7a14b312015-10-01 07:28:13 -07002181 SkRect physicalPerspectiveBounds =
2182 physicalPerspectiveOutline.getBounds();
2183 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2184 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002185
2186 // TODO(edisonn): A better approach would be to use a bitmap shader
2187 // (in clamp mode) and draw a rect over the entire bounding box. Then
2188 // intersect perspectiveOutline to the clip. That will avoid introducing
2189 // alpha to the image while still giving good behavior at the edge of
2190 // the image. Avoiding alpha will reduce the pdf size and generation
2191 // CPU time some.
2192
halcanary7a14b312015-10-01 07:28:13 -07002193 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2194
halcanary48810a02016-03-07 14:57:50 -08002195 sk_sp<SkSurface> surface(
halcanary7a14b312015-10-01 07:28:13 -07002196 SkSurface::NewRaster(SkImageInfo::MakeN32Premul(wh)));
2197 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002198 return;
2199 }
halcanary7a14b312015-10-01 07:28:13 -07002200 SkCanvas* canvas = surface->getCanvas();
2201 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002202
2203 SkScalar deltaX = bounds.left();
2204 SkScalar deltaY = bounds.top();
2205
2206 SkMatrix offsetMatrix = origMatrix;
2207 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002208 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002209
2210 // Translate the draw in the new canvas, so we perfectly fit the
2211 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002212 canvas->setMatrix(offsetMatrix);
2213 canvas->drawImage(image, 0, 0, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002214 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002215 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002216
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002217 // In the new space, we use the identity matrix translated
2218 // and scaled to reflect DPI.
2219 matrix.setScale(1 / scaleX, 1 / scaleY);
2220 matrix.postTranslate(deltaX, deltaY);
2221
halcanary7a14b312015-10-01 07:28:13 -07002222 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002223 clipRegion = &perspectiveBounds;
halcanary96fcdcc2015-08-27 07:41:13 -07002224 srcRect = nullptr;
halcanary7a14b312015-10-01 07:28:13 -07002225
reed9ce9d672016-03-17 10:51:11 -07002226 autoImageUnref.reset(surface->makeImageSnapshot().release());
halcanaryfcad44b2016-03-06 14:47:10 -08002227 image = autoImageUnref.get();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002228 }
2229
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002230 SkMatrix scaled;
2231 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002232 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2233 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002234 // Scale the image up from 1x1 to WxH.
halcanary7a14b312015-10-01 07:28:13 -07002235 SkIRect subset = image->bounds();
2236 scaled.postScale(SkIntToScalar(image->width()),
2237 SkIntToScalar(image->height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002238 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002239 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002240 if (!content.entry() || (srcRect && !subset.intersect(*srcRect))) {
2241 return;
2242 }
2243 if (content.needShape()) {
2244 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002245 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002246 shape.transform(matrix);
2247 content.setShape(shape);
2248 }
2249 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002250 return;
2251 }
2252
halcanary287d22d2015-09-24 10:20:05 -07002253 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002254 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002255 // draw calls. This code here works for all
2256 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2257 // rasterize a layer on this backend). Fortuanely, this seems
2258 // to be how Chromium impements most color-filters.
2259 autoImageUnref.reset(color_filter(image, colorFilter));
halcanaryfcad44b2016-03-06 14:47:10 -08002260 image = autoImageUnref.get();
halcanary7a14b312015-10-01 07:28:13 -07002261 // TODO(halcanary): de-dupe this by caching filtered images.
2262 // (maybe in the resource cache?)
2263 }
halcanary48810a02016-03-07 14:57:50 -08002264 sk_sp<SkPDFObject> pdfimage(SkSafeRef(fCanon->findPDFBitmap(image)));
halcanary7a14b312015-10-01 07:28:13 -07002265 if (!pdfimage) {
halcanary712fdf72015-12-10 08:59:43 -08002266 pdfimage.reset(SkPDFCreateBitmapObject(
halcanaryfcad44b2016-03-06 14:47:10 -08002267 image, fCanon->getPixelSerializer()));
halcanary7a14b312015-10-01 07:28:13 -07002268 if (!pdfimage) {
2269 return;
halcanary287d22d2015-09-24 10:20:05 -07002270 }
halcanaryfcad44b2016-03-06 14:47:10 -08002271 fCanon->addPDFBitmap(image->uniqueID(), pdfimage.get());
halcanary287d22d2015-09-24 10:20:05 -07002272 }
halcanary3d8c33c2015-10-01 11:06:22 -07002273 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002274 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002275}