blob: 973ebea314ab518e3f19ab57d743d915cb3da883 [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00006 */
7
8#include "SkPDFDevice.h"
9
reedf70b5312016-03-04 16:36:20 -080010#include "SkAnnotationKeys.h"
martina.kollarovab8d6af12016-06-29 05:12:31 -070011#include "SkBitmapKey.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000012#include "SkColor.h"
halcanary287d22d2015-09-24 10:20:05 -070013#include "SkColorFilter.h"
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000014#include "SkDraw.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000015#include "SkGlyphCache.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000016#include "SkPath.h"
reeda4393342016-03-18 11:22:57 -070017#include "SkPathEffect.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000018#include "SkPathOps.h"
halcanarydb0dcc72015-03-20 12:31:52 -070019#include "SkPDFBitmap.h"
halcanary7a14b312015-10-01 07:28:13 -070020#include "SkPDFCanon.h"
halcanary989da4a2016-03-21 14:33:17 -070021#include "SkPDFDocument.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000022#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000023#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000024#include "SkPDFGraphicState.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000025#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000026#include "SkPDFShader.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000027#include "SkPDFStream.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000028#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000029#include "SkPDFUtils.h"
wangxianzhuef6c50a2015-09-17 20:38:02 -070030#include "SkRasterClip.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000031#include "SkRRect.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000032#include "SkString.h"
reed89443ab2014-06-27 11:34:19 -070033#include "SkSurface.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000034#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000035#include "SkTemplates.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000036#include "SkTypefacePriv.h"
halcanarya6814332015-05-27 08:53:36 -070037#include "SkXfermodeInterpretation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000038
edisonn@google.com73a7ea32013-11-11 20:55:15 +000039#define DPI_FOR_RASTER_SCALE_ONE 72
40
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000041// Utility functions
42
halcanarya6814332015-05-27 08:53:36 -070043// If the paint will definitely draw opaquely, replace kSrc_Mode with
44// kSrcOver_Mode. http://crbug.com/473572
45static void replace_srcmode_on_opaque_paint(SkPaint* paint) {
46 if (kSrcOver_SkXfermodeInterpretation
47 == SkInterpretXfermode(*paint, false)) {
halcanary96fcdcc2015-08-27 07:41:13 -070048 paint->setXfermode(nullptr);
halcanarya6814332015-05-27 08:53:36 -070049 }
50}
51
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000052static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000053 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
halcanaryeb92cb32016-07-15 13:41:27 -070054 SkPDFUtils::AppendColorComponent(SkColorGetR(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000055 result->writeText(" ");
halcanaryeb92cb32016-07-15 13:41:27 -070056 SkPDFUtils::AppendColorComponent(SkColorGetG(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000057 result->writeText(" ");
halcanaryeb92cb32016-07-15 13:41:27 -070058 SkPDFUtils::AppendColorComponent(SkColorGetB(color), result);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000059 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000060}
61
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000062static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000063 SkPaint result = paint;
64 if (result.isFakeBoldText()) {
65 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
66 kStdFakeBoldInterpKeys,
67 kStdFakeBoldInterpValues,
68 kStdFakeBoldInterpLength);
69 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000070 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000071 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000072 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000073 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000074 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000075 result.setStrokeWidth(width);
76 }
77 return result;
78}
79
80// Stolen from measure_text in SkDraw.cpp and then tweaked.
benjaminwagnerd936f632016-02-23 10:44:31 -080081static void align_text(SkPaint::GlyphCacheProc glyphCacheProc, const SkPaint& paint,
bungeman@google.com9a87cee2011-08-23 17:02:18 +000082 const uint16_t* glyphs, size_t len,
83 SkScalar* x, SkScalar* y) {
84 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000085 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000086 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000087
88 SkMatrix ident;
89 ident.reset();
halcanary96fcdcc2015-08-27 07:41:13 -070090 SkAutoGlyphCache autoCache(paint, nullptr, &ident);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000091 SkGlyphCache* cache = autoCache.getCache();
92
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +000093 const char* start = reinterpret_cast<const char*>(glyphs);
94 const char* stop = reinterpret_cast<const char*>(glyphs + len);
benjaminwagner6b3eacb2016-03-24 19:07:58 -070095 SkScalar xAdv = 0, yAdv = 0;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000096
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000097 // TODO(vandebo): This probably needs to take kerning into account.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000098 while (start < stop) {
benjaminwagnerd936f632016-02-23 10:44:31 -080099 const SkGlyph& glyph = glyphCacheProc(cache, &start);
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700100 xAdv += SkFloatToScalar(glyph.fAdvanceX);
101 yAdv += SkFloatToScalar(glyph.fAdvanceY);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000102 };
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000103 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000104 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000105 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000106
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000107 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700108 xAdv = SkScalarHalf(xAdv);
109 yAdv = SkScalarHalf(yAdv);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000110 }
benjaminwagner6b3eacb2016-03-24 19:07:58 -0700111 *x = *x - xAdv;
112 *y = *y - yAdv;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000113}
114
robertphillips@google.coma4662862013-11-21 14:24:16 +0000115static int max_glyphid_for_typeface(SkTypeface* typeface) {
reed@google.comfed86bd2013-03-14 15:04:57 +0000116 SkAutoResolveDefaultTypeface autoResolve(typeface);
117 typeface = autoResolve.get();
commit-bot@chromium.org6a4ba5b2013-07-17 21:55:08 +0000118 return typeface->countGlyphs() - 1;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000119}
120
121typedef SkAutoSTMalloc<128, uint16_t> SkGlyphStorage;
122
reed@google.comaec40662014-04-18 19:29:07 +0000123static int force_glyph_encoding(const SkPaint& paint, const void* text,
124 size_t len, SkGlyphStorage* storage,
bungeman22edc832014-10-03 07:55:58 -0700125 const uint16_t** glyphIDs) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000126 // Make sure we have a glyph id encoding.
127 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
halcanary96fcdcc2015-08-27 07:41:13 -0700128 int numGlyphs = paint.textToGlyphs(text, len, nullptr);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000129 storage->reset(numGlyphs);
130 paint.textToGlyphs(text, len, storage->get());
131 *glyphIDs = storage->get();
132 return numGlyphs;
133 }
134
135 // For user supplied glyph ids we need to validate them.
136 SkASSERT((len & 1) == 0);
reed@google.comaec40662014-04-18 19:29:07 +0000137 int numGlyphs = SkToInt(len / 2);
bungeman22edc832014-10-03 07:55:58 -0700138 const uint16_t* input = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000139
140 int maxGlyphID = max_glyphid_for_typeface(paint.getTypeface());
reed@google.comaec40662014-04-18 19:29:07 +0000141 int validated;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000142 for (validated = 0; validated < numGlyphs; ++validated) {
143 if (input[validated] > maxGlyphID) {
144 break;
145 }
146 }
147 if (validated >= numGlyphs) {
bungeman22edc832014-10-03 07:55:58 -0700148 *glyphIDs = static_cast<const uint16_t*>(text);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000149 return numGlyphs;
150 }
151
152 // Silently drop anything out of range.
153 storage->reset(numGlyphs);
154 if (validated > 0) {
155 memcpy(storage->get(), input, validated * sizeof(uint16_t));
156 }
157
reed@google.comaec40662014-04-18 19:29:07 +0000158 for (int i = validated; i < numGlyphs; ++i) {
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000159 storage->get()[i] = input[i];
160 if (input[i] > maxGlyphID) {
161 storage->get()[i] = 0;
162 }
163 }
164 *glyphIDs = storage->get();
165 return numGlyphs;
166}
167
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000168static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX,
169 SkWStream* content) {
170 // Flip the text about the x-axis to account for origin swap and include
171 // the passed parameters.
172 content->writeText("1 0 ");
halcanarybc4696b2015-05-06 10:56:04 -0700173 SkPDFUtils::AppendScalar(0 - textSkewX, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000174 content->writeText(" -1 ");
halcanarybc4696b2015-05-06 10:56:04 -0700175 SkPDFUtils::AppendScalar(x, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000176 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700177 SkPDFUtils::AppendScalar(y, content);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000178 content->writeText(" Tm\n");
179}
180
halcanary2be7e012016-03-28 11:58:08 -0700181SkPDFDevice::GraphicStateEntry::GraphicStateEntry()
182 : fColor(SK_ColorBLACK)
183 , fTextScaleX(SK_Scalar1)
184 , fTextFill(SkPaint::kFill_Style)
185 , fShaderIndex(-1)
186 , fGraphicStateIndex(-1)
187 , fFont(nullptr)
188 , fTextSize(SK_ScalarNaN) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000189 fMatrix.reset();
190}
191
halcanary2be7e012016-03-28 11:58:08 -0700192bool SkPDFDevice::GraphicStateEntry::compareInitialState(
193 const GraphicStateEntry& cur) {
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +0000194 return fColor == cur.fColor &&
195 fShaderIndex == cur.fShaderIndex &&
196 fGraphicStateIndex == cur.fGraphicStateIndex &&
197 fMatrix == cur.fMatrix &&
198 fClipStack == cur.fClipStack &&
199 (fTextScaleX == 0 ||
200 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000201}
202
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000203class GraphicStackState {
204public:
205 GraphicStackState(const SkClipStack& existingClipStack,
206 const SkRegion& existingClipRegion,
207 SkWStream* contentStream)
208 : fStackDepth(0),
209 fContentStream(contentStream) {
210 fEntries[0].fClipStack = existingClipStack;
211 fEntries[0].fClipRegion = existingClipRegion;
212 }
213
214 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000215 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000216 void updateMatrix(const SkMatrix& matrix);
halcanary2be7e012016-03-28 11:58:08 -0700217 void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000218
219 void drainStack();
220
221private:
222 void push();
223 void pop();
halcanary2be7e012016-03-28 11:58:08 -0700224 SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000225
226 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
227 static const int kMaxStackDepth = 12;
halcanary2be7e012016-03-28 11:58:08 -0700228 SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1];
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000229 int fStackDepth;
230 SkWStream* fContentStream;
231};
232
233void GraphicStackState::drainStack() {
234 while (fStackDepth) {
235 pop();
236 }
237}
238
239void GraphicStackState::push() {
240 SkASSERT(fStackDepth < kMaxStackDepth);
241 fContentStream->writeText("q\n");
242 fStackDepth++;
243 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
244}
245
246void GraphicStackState::pop() {
247 SkASSERT(fStackDepth > 0);
248 fContentStream->writeText("Q\n");
249 fStackDepth--;
250}
251
robertphillips@google.com80214e22012-07-20 15:33:18 +0000252// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000253// and then skips over the leading entries as specified in prefix. It requires
254// and asserts that "prefix" will be a prefix to "stack."
255static void skip_clip_stack_prefix(const SkClipStack& prefix,
256 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000257 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000258 SkClipStack::B2TIter prefixIter(prefix);
259 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000260
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000261 const SkClipStack::Element* prefixEntry;
262 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000263
264 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000265 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000266 iterEntry = iter->next();
267 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000268 // Because of SkClipStack does internal intersection, the last clip
269 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000270 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000271 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
272 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
273 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000274 // back up the iterator by one
275 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000276 prefixEntry = prefixIter.next();
277 break;
278 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000279 }
280
halcanary96fcdcc2015-08-27 07:41:13 -0700281 SkASSERT(prefixEntry == nullptr);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000282}
283
284static void emit_clip(SkPath* clipPath, SkRect* clipRect,
285 SkWStream* contentStream) {
286 SkASSERT(clipPath || clipRect);
287
288 SkPath::FillType clipFill;
289 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000290 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000291 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000292 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000293 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
294 clipFill = SkPath::kWinding_FillType;
295 }
296
297 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
298 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
299 if (clipFill == SkPath::kEvenOdd_FillType) {
300 contentStream->writeText("W* n\n");
301 } else {
302 contentStream->writeText("W n\n");
303 }
304}
305
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000306/* Calculate an inverted path's equivalent non-inverted path, given the
307 * canvas bounds.
308 * outPath may alias with invPath (since this is supported by PathOps).
309 */
310static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
311 SkPath* outPath) {
312 SkASSERT(invPath.isInverseFillType());
313
314 SkPath clipPath;
315 clipPath.addRect(bounds);
316
reedcdb42bb2015-06-26 10:23:07 -0700317 return Op(clipPath, invPath, kIntersect_SkPathOp, outPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000318}
319
320// Sanity check the numerical values of the SkRegion ops and PathOps ops
321// enums so region_op_to_pathops_op can do a straight passthrough cast.
322// If these are failing, it may be necessary to make region_op_to_pathops_op
323// do more.
bungeman99fe8222015-08-20 07:57:51 -0700324static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch");
325static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch");
326static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch");
327static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch");
328static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp,
329 "region_pathop_mismatch");
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000330
331static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
332 SkASSERT(op >= 0);
333 SkASSERT(op <= SkRegion::kReverseDifference_Op);
334 return (SkPathOp)op;
335}
336
337/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
338 * Returns true if successful, or false if not successful.
339 * If successful, the resulting clip is stored in outClipPath.
340 * If not successful, outClipPath is undefined, and a fallback method
341 * should be used.
342 */
343static bool get_clip_stack_path(const SkMatrix& transform,
344 const SkClipStack& clipStack,
345 const SkRegion& clipRegion,
346 SkPath* outClipPath) {
347 outClipPath->reset();
348 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
349
350 const SkClipStack::Element* clipEntry;
351 SkClipStack::Iter iter;
352 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
353 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
354 SkPath entryPath;
355 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
356 outClipPath->reset();
357 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
358 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000359 } else {
360 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000361 }
362 entryPath.transform(transform);
363
364 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
365 *outClipPath = entryPath;
366 } else {
367 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
368 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
369 return false;
370 }
371 }
372 }
373
374 if (outClipPath->isInverseFillType()) {
375 // The bounds are slightly outset to ensure this is correct in the
376 // face of floating-point accuracy and possible SkRegion bitmap
377 // approximations.
378 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
379 clipBounds.outset(SK_Scalar1, SK_Scalar1);
380 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
381 return false;
382 }
383 }
384 return true;
385}
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000386
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000387// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000388// graphic state stack, and the fact that we can know all the clips used
389// on the page to optimize this.
390void GraphicStackState::updateClip(const SkClipStack& clipStack,
391 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000392 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000393 if (clipStack == currentEntry()->fClipStack) {
394 return;
395 }
396
397 while (fStackDepth > 0) {
398 pop();
399 if (clipStack == currentEntry()->fClipStack) {
400 return;
401 }
402 }
403 push();
404
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000405 currentEntry()->fClipStack = clipStack;
406 currentEntry()->fClipRegion = clipRegion;
407
408 SkMatrix transform;
409 transform.setTranslate(translation.fX, translation.fY);
410
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000411 SkPath clipPath;
412 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700413 emit_clip(&clipPath, nullptr, fContentStream);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000414 return;
415 }
halcanarydda239e2016-03-31 07:33:57 -0700416
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000417 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
418 // already been applied. (If this is a top level device, then it specifies
419 // a clip to the content area. If this is a layer, then it specifies
420 // the clip in effect when the layer was created.) There's no need to
421 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
422 // initial clip on the parent layer. (This means there's a bug if the user
423 // expands the clip and then uses any xfer mode that uses dst:
424 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000425 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000426 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
427
428 // If the clip stack does anything other than intersect or if it uses
429 // an inverse fill type, we have to fall back to the clip region.
430 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000431 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000432 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000433 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
434 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000435 needRegion = true;
436 break;
437 }
438 }
439
440 if (needRegion) {
441 SkPath clipPath;
442 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
halcanary96fcdcc2015-08-27 07:41:13 -0700443 emit_clip(&clipPath, nullptr, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000444 } else {
445 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000446 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000447 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000448 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
449 switch (clipEntry->getType()) {
450 case SkClipStack::Element::kRect_Type: {
451 SkRect translatedClip;
452 transform.mapRect(&translatedClip, clipEntry->getRect());
halcanary96fcdcc2015-08-27 07:41:13 -0700453 emit_clip(nullptr, &translatedClip, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000454 break;
455 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000456 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000457 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000458 clipEntry->asPath(&translatedPath);
459 translatedPath.transform(transform, &translatedPath);
halcanary96fcdcc2015-08-27 07:41:13 -0700460 emit_clip(&translatedPath, nullptr, fContentStream);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000461 break;
462 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000463 }
464 }
465 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000466}
467
468void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
469 if (matrix == currentEntry()->fMatrix) {
470 return;
471 }
472
473 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
474 SkASSERT(fStackDepth > 0);
475 SkASSERT(fEntries[fStackDepth].fClipStack ==
476 fEntries[fStackDepth -1].fClipStack);
477 pop();
478
479 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
480 }
481 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
482 return;
483 }
484
485 push();
486 SkPDFUtils::AppendTransform(matrix, fContentStream);
487 currentEntry()->fMatrix = matrix;
488}
489
halcanary2be7e012016-03-28 11:58:08 -0700490void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000491 // PDF treats a shader as a color, so we only set one or the other.
492 if (state.fShaderIndex >= 0) {
493 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000494 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000495 currentEntry()->fShaderIndex = state.fShaderIndex;
496 }
497 } else {
498 if (state.fColor != currentEntry()->fColor ||
499 currentEntry()->fShaderIndex >= 0) {
500 emit_pdf_color(state.fColor, fContentStream);
501 fContentStream->writeText("RG ");
502 emit_pdf_color(state.fColor, fContentStream);
503 fContentStream->writeText("rg\n");
504 currentEntry()->fColor = state.fColor;
505 currentEntry()->fShaderIndex = -1;
506 }
507 }
508
509 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000510 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000511 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
512 }
513
514 if (state.fTextScaleX) {
515 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
516 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
517 SkIntToScalar(100));
halcanarybc4696b2015-05-06 10:56:04 -0700518 SkPDFUtils::AppendScalar(pdfScale, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000519 fContentStream->writeText(" Tz\n");
520 currentEntry()->fTextScaleX = state.fTextScaleX;
521 }
522 if (state.fTextFill != currentEntry()->fTextFill) {
bungeman99fe8222015-08-20 07:57:51 -0700523 static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value");
524 static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value");
525 static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value");
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000526 fContentStream->writeDecAsText(state.fTextFill);
527 fContentStream->writeText(" Tr\n");
528 currentEntry()->fTextFill = state.fTextFill;
529 }
530 }
531}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000532
reed76033be2015-03-14 10:54:31 -0700533static bool not_supported_for_layers(const SkPaint& layerPaint) {
senorblancob0e89dc2014-10-20 14:03:12 -0700534 // PDF does not support image filters, so render them on CPU.
535 // Note that this rendering is done at "screen" resolution (100dpi), not
536 // printer resolution.
halcanary7a14b312015-10-01 07:28:13 -0700537 // TODO: It may be possible to express some filters natively using PDF
halcanary6950de62015-11-07 05:29:00 -0800538 // to improve quality and file size (https://bug.skia.org/3043)
reed76033be2015-03-14 10:54:31 -0700539
540 // TODO: should we return true if there is a colorfilter?
halcanary96fcdcc2015-08-27 07:41:13 -0700541 return layerPaint.getImageFilter() != nullptr;
reed76033be2015-03-14 10:54:31 -0700542}
543
544SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
reedcd4051e2016-07-15 09:41:26 -0700545 if (layerPaint && not_supported_for_layers(*layerPaint)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700546 return nullptr;
senorblancob0e89dc2014-10-20 14:03:12 -0700547 }
fmalita6987dca2014-11-13 08:33:37 -0800548 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700549 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000550}
551
halcanary989da4a2016-03-21 14:33:17 -0700552SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
553
bsalomon@google.come97f0852011-06-17 13:10:25 +0000554
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000555
556// A helper class to automatically finish a ContentEntry at the end of a
557// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000558class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000559public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000560 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
561 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000562 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700563 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000564 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700565 fDstFormXObject(nullptr) {
reed1e7f5e72016-04-27 07:49:17 -0700566 init(draw.fClipStack, draw.fRC->bwRgn(), *draw.fMatrix, paint, hasText);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000567 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000568 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
569 const SkRegion& clipRegion, const SkMatrix& matrix,
570 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000571 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700572 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000573 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700574 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000575 init(clipStack, clipRegion, matrix, paint, hasText);
576 }
577
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000578 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000579 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000580 SkPath* shape = &fShape;
581 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700582 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000583 }
584 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000585 }
reed@google.comfc641d02012-09-20 17:52:20 +0000586 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000587 }
588
halcanary2be7e012016-03-28 11:58:08 -0700589 SkPDFDevice::ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000590
591 /* Returns true when we explicitly need the shape of the drawing. */
592 bool needShape() {
593 switch (fXfermode) {
594 case SkXfermode::kClear_Mode:
595 case SkXfermode::kSrc_Mode:
596 case SkXfermode::kSrcIn_Mode:
597 case SkXfermode::kSrcOut_Mode:
598 case SkXfermode::kDstIn_Mode:
599 case SkXfermode::kDstOut_Mode:
600 case SkXfermode::kSrcATop_Mode:
601 case SkXfermode::kDstATop_Mode:
602 case SkXfermode::kModulate_Mode:
603 return true;
604 default:
605 return false;
606 }
607 }
608
609 /* Returns true unless we only need the shape of the drawing. */
610 bool needSource() {
611 if (fXfermode == SkXfermode::kClear_Mode) {
612 return false;
613 }
614 return true;
615 }
616
617 /* If the shape is different than the alpha component of the content, then
618 * setShape should be called with the shape. In particular, images and
619 * devices have rectangular shape.
620 */
621 void setShape(const SkPath& shape) {
622 fShape = shape;
623 }
624
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000625private:
626 SkPDFDevice* fDevice;
halcanary2be7e012016-03-28 11:58:08 -0700627 SkPDFDevice::ContentEntry* fContentEntry;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000628 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000629 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000630 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000631
632 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
633 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000634 // Shape has to be flatten before we get here.
635 if (matrix.hasPerspective()) {
636 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000637 return;
638 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000639 if (paint.getXfermode()) {
640 paint.getXfermode()->asMode(&fXfermode);
641 }
642 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
643 matrix, paint, hasText,
644 &fDstFormXObject);
645 }
646};
647
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000648////////////////////////////////////////////////////////////////////////////////
649
halcanary989da4a2016-03-21 14:33:17 -0700650SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
robertphillips9a53fd72015-06-22 09:46:59 -0700651 : INHERITED(SkSurfaceProps(0, kUnknown_SkPixelGeometry))
652 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800653 , fContentSize(pageSize)
654 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
halcanary96fcdcc2015-08-27 07:41:13 -0700655 , fClipStack(nullptr)
halcanarya1f1ee92015-02-20 06:17:26 -0800656 , fRasterDpi(rasterDpi)
halcanary989da4a2016-03-21 14:33:17 -0700657 , fDocument(doc) {
halcanarya1f1ee92015-02-20 06:17:26 -0800658 SkASSERT(pageSize.width() > 0);
659 SkASSERT(pageSize.height() > 0);
660 fLegacyBitmap.setInfo(
661 SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()));
662 if (flip) {
663 // Skia generally uses the top left as the origin but PDF
664 // natively has the origin at the bottom left. This matrix
665 // corrects for that. But that only needs to be done once, we
666 // don't do it when layering.
667 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
668 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
669 } else {
670 fInitialTransform.setIdentity();
671 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000672}
673
674SkPDFDevice::~SkPDFDevice() {
halcanary3c35fb32016-06-30 11:55:07 -0700675 this->cleanUp();
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000676}
677
678void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700679 fContentEntries.reset();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000680}
681
halcanary3c35fb32016-06-30 11:55:07 -0700682void SkPDFDevice::cleanUp() {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000683 fGraphicStateResources.unrefAll();
684 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000685 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000686 fShaderResources.unrefAll();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000687}
688
reedf70b5312016-03-04 16:36:20 -0800689void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
690 SkData* value) {
691 if (0 == rect.width() && 0 == rect.height()) {
692 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
693 } else {
694 SkPath path;
695 path.addRect(rect);
696 handlePathAnnotation(path, d, key, value);
697 }
698}
699
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000700void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000701 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700702 replace_srcmode_on_opaque_paint(&newPaint);
703
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000704 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000705 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000706 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000707}
708
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000709void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
halcanary2be7e012016-03-28 11:58:08 -0700710 SkPDFDevice::ContentEntry* contentEntry) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000711 if (!contentEntry) {
712 return;
713 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000714 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
715 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000716 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000717 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000718 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000719 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000720 inverse.mapRect(&bbox);
721
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000722 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000723 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000724 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000725}
726
halcanary682ee012016-01-28 10:59:34 -0800727void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700728 SkCanvas::PointMode mode,
729 size_t count,
730 const SkPoint* points,
731 const SkPaint& srcPaint) {
732 SkPaint passedPaint = srcPaint;
733 replace_srcmode_on_opaque_paint(&passedPaint);
734
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000735 if (count == 0) {
736 return;
737 }
738
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000739 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
740 // We only use this when there's a path effect because of the overhead
741 // of multiple calls to setUpContentEntry it causes.
742 if (passedPaint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700743 if (d.fRC->isEmpty()) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000744 return;
745 }
746 SkDraw pointDraw(d);
747 pointDraw.fDevice = this;
748 pointDraw.drawPoints(mode, count, points, passedPaint, true);
749 return;
750 }
751
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000752 const SkPaint* paint = &passedPaint;
753 SkPaint modifiedPaint;
754
755 if (mode == SkCanvas::kPoints_PointMode &&
756 paint->getStrokeCap() != SkPaint::kRound_Cap) {
757 modifiedPaint = *paint;
758 paint = &modifiedPaint;
759 if (paint->getStrokeWidth()) {
760 // PDF won't draw a single point with square/butt caps because the
761 // orientation is ambiguous. Draw a rectangle instead.
762 modifiedPaint.setStyle(SkPaint::kFill_Style);
763 SkScalar strokeWidth = paint->getStrokeWidth();
764 SkScalar halfStroke = SkScalarHalf(strokeWidth);
765 for (size_t i = 0; i < count; i++) {
766 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
767 r.inset(-halfStroke, -halfStroke);
768 drawRect(d, r, modifiedPaint);
769 }
770 return;
771 } else {
772 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
773 }
774 }
775
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000776 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000777 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000778 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000779 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000780
781 switch (mode) {
782 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000783 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000784 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000785 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000786 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000787 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000788 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000789 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000790 break;
791 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000792 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000793 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000794 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000795 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000796 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000797 &content.entry()->fContent);
798 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000799 }
800 break;
801 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000802 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
803 for (size_t i = 0; i < count; i++) {
804 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000805 &content.entry()->fContent);
806 SkPDFUtils::ClosePath(&content.entry()->fContent);
807 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000808 }
809 break;
810 default:
811 SkASSERT(false);
812 }
813}
814
halcanary8103a342016-03-08 15:10:16 -0800815static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800816 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700817 annotation->insertName("Subtype", "Link");
halcanary488165e2016-04-22 06:10:21 -0700818 annotation->insertInt("F", 4); // required by ISO 19005
wangxianzhud76665d2015-07-17 17:23:15 -0700819
halcanaryece83922016-03-08 08:32:12 -0800820 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700821 border->reserve(3);
822 border->appendInt(0); // Horizontal corner radius.
823 border->appendInt(0); // Vertical corner radius.
824 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800825 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700826
halcanaryece83922016-03-08 08:32:12 -0800827 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700828 rect->reserve(4);
829 rect->appendScalar(translatedRect.fLeft);
830 rect->appendScalar(translatedRect.fTop);
831 rect->appendScalar(translatedRect.fRight);
832 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800833 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700834
halcanary8103a342016-03-08 15:10:16 -0800835 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700836}
837
halcanary8103a342016-03-08 15:10:16 -0800838static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
839 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700840 SkString url(static_cast<const char *>(urlData->data()),
841 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800842 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700843 action->insertName("S", "URI");
844 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800845 annotation->insertObject("A", std::move(action));
846 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700847}
848
halcanary8103a342016-03-08 15:10:16 -0800849static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
850 const SkRect& r) {
851 auto annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700852 SkString name(static_cast<const char *>(nameData->data()),
853 nameData->size() - 1);
854 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800855 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700856}
857
halcanary682ee012016-01-28 10:59:34 -0800858void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700859 const SkRect& rect,
860 const SkPaint& srcPaint) {
861 SkPaint paint = srcPaint;
862 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000863 SkRect r = rect;
864 r.sort();
865
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000866 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700867 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000868 return;
869 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000870 SkPath path;
871 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700872 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000873 return;
874 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000875
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000876 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000877 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000878 return;
879 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000880 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000881 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000882 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000883}
884
halcanarya6814332015-05-27 08:53:36 -0700885void SkPDFDevice::drawRRect(const SkDraw& draw,
886 const SkRRect& rrect,
887 const SkPaint& srcPaint) {
888 SkPaint paint = srcPaint;
889 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000890 SkPath path;
891 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700892 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000893}
894
halcanarya6814332015-05-27 08:53:36 -0700895void SkPDFDevice::drawOval(const SkDraw& draw,
896 const SkRect& oval,
897 const SkPaint& srcPaint) {
898 SkPaint paint = srcPaint;
899 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700900 SkPath path;
901 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700902 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700903}
904
halcanary682ee012016-01-28 10:59:34 -0800905void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700906 const SkPath& origPath,
907 const SkPaint& srcPaint,
908 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000909 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700910 SkPaint paint = srcPaint;
911 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800912 SkPath modifiedPath;
913 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000914
915 SkMatrix matrix = *d.fMatrix;
916 if (prePathMatrix) {
917 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800918 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000919 pathPtr = &modifiedPath;
920 pathIsMutable = true;
921 }
halcanary682ee012016-01-28 10:59:34 -0800922 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000923 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000924 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000925 }
926 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000927
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000928 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700929 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000930 return;
931 }
halcanary682ee012016-01-28 10:59:34 -0800932 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000933 pathPtr = &modifiedPath;
934 pathIsMutable = true;
935 }
halcanary682ee012016-01-28 10:59:34 -0800936 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000937
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000938 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700939 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000940 if (fill) {
941 noEffectPaint.setStyle(SkPaint::kFill_Style);
942 } else {
943 noEffectPaint.setStyle(SkPaint::kStroke_Style);
944 noEffectPaint.setStrokeWidth(0);
945 }
halcanary96fcdcc2015-08-27 07:41:13 -0700946 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000947 return;
948 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000949
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000950 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000951 return;
952 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000953
reed1e7f5e72016-04-27 07:49:17 -0700954 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000955 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000956 return;
957 }
halcanary8b2bc252015-10-06 09:41:47 -0700958 bool consumeDegeratePathSegments =
959 paint.getStyle() == SkPaint::kFill_Style ||
960 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
961 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000962 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -0700963 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000964 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000965 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000966 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000967}
968
halcanary7a14b312015-10-01 07:28:13 -0700969void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
970 const SkBitmap& bitmap,
971 const SkRect* src,
972 const SkRect& dst,
973 const SkPaint& srcPaint,
974 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700975 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -0700976}
977
978void SkPDFDevice::drawBitmap(const SkDraw& d,
979 const SkBitmap& bitmap,
980 const SkMatrix& matrix,
981 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -0700982 SkPaint paint = srcPaint;
983 if (bitmap.isOpaque()) {
984 replace_srcmode_on_opaque_paint(&paint);
985 }
986
reed1e7f5e72016-04-27 07:49:17 -0700987 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700988 return;
989 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +0000990
halcanary7a14b312015-10-01 07:28:13 -0700991 SkMatrix transform = matrix;
992 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -0700993 SkImageBitmap imageBitmap(bitmap);
994 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700995 transform, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700996}
997
998void SkPDFDevice::drawSprite(const SkDraw& d,
999 const SkBitmap& bitmap,
1000 int x,
1001 int y,
1002 const SkPaint& srcPaint) {
1003 SkPaint paint = srcPaint;
1004 if (bitmap.isOpaque()) {
1005 replace_srcmode_on_opaque_paint(&paint);
1006 }
1007
reed1e7f5e72016-04-27 07:49:17 -07001008 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -07001009 return;
1010 }
1011
1012 SkMatrix matrix;
1013 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -07001014 SkImageBitmap imageBitmap(bitmap);
1015 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -07001016 matrix, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001017}
1018
1019void SkPDFDevice::drawImage(const SkDraw& draw,
1020 const SkImage* image,
1021 SkScalar x,
1022 SkScalar y,
1023 const SkPaint& srcPaint) {
1024 SkPaint paint = srcPaint;
1025 if (!image) {
1026 return;
1027 }
1028 if (image->isOpaque()) {
1029 replace_srcmode_on_opaque_paint(&paint);
1030 }
reed1e7f5e72016-04-27 07:49:17 -07001031 if (draw.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -07001032 return;
1033 }
1034 SkMatrix transform = SkMatrix::MakeTrans(x, y);
1035 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001036 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
1037 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -07001038 transform, draw.fClipStack, draw.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001039}
1040
1041void SkPDFDevice::drawImageRect(const SkDraw& draw,
1042 const SkImage* image,
1043 const SkRect* src,
1044 const SkRect& dst,
1045 const SkPaint& srcPaint,
1046 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001047 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001048}
1049
halcanarybb264b72015-04-07 10:40:03 -07001050// Create a PDF string. Maximum length (in bytes) is 65,535.
1051// @param input A string value.
1052// @param len The length of the input array.
1053// @param wideChars True iff the upper byte in each uint16_t is
1054// significant and should be encoded and not
1055// discarded. If true, the upper byte is encoded
1056// first. Otherwise, we assert the upper byte is
1057// zero.
halcanaryee41b752016-06-23 14:08:11 -07001058static void write_wide_string(SkDynamicMemoryWStream* wStream,
1059 const uint16_t* input,
1060 size_t len,
1061 bool wideChars) {
halcanarybb264b72015-04-07 10:40:03 -07001062 if (wideChars) {
1063 SkASSERT(2 * len < 65535);
halcanaryee41b752016-06-23 14:08:11 -07001064 wStream->writeText("<");
halcanarybb264b72015-04-07 10:40:03 -07001065 for (size_t i = 0; i < len; i++) {
halcanarya76a10b72016-07-07 12:31:55 -07001066 SkPDFUtils::WriteUInt16BE(wStream, input[i]);
halcanarybb264b72015-04-07 10:40:03 -07001067 }
halcanaryee41b752016-06-23 14:08:11 -07001068 wStream->writeText(">");
halcanarybb264b72015-04-07 10:40:03 -07001069 } else {
1070 SkASSERT(len <= 65535);
halcanaryee41b752016-06-23 14:08:11 -07001071 SkAutoMalloc buffer(len); // Remove every other byte.
1072 uint8_t* ptr = (uint8_t*)buffer.get();
halcanarybb264b72015-04-07 10:40:03 -07001073 for (size_t i = 0; i < len; i++) {
1074 SkASSERT(0 == input[i] >> 8);
halcanaryee41b752016-06-23 14:08:11 -07001075 ptr[i] = static_cast<uint8_t>(input[i]);
halcanarybb264b72015-04-07 10:40:03 -07001076 }
halcanaryee41b752016-06-23 14:08:11 -07001077 SkPDFUtils::WriteString(wStream, (char*)buffer.get(), len);
halcanarybb264b72015-04-07 10:40:03 -07001078 }
1079}
1080
halcanaryf0c30f52016-07-15 13:35:45 -07001081namespace {
1082class GlyphPositioner {
1083public:
1084 GlyphPositioner(SkDynamicMemoryWStream* content,
1085 SkScalar textSkewX,
1086 bool wideChars)
1087 : fContent(content)
1088 , fCurrentMatrixX(0.0f)
1089 , fCurrentMatrixY(0.0f)
1090 , fXAdvance(0.0f)
1091 , fWideChars(wideChars)
1092 , fInText(false) {
1093 set_text_transform(0.0f, 0.0f, textSkewX, fContent);
1094 }
1095 ~GlyphPositioner() { SkASSERT(!fInText); /* flush first */ }
1096 void flush() {
1097 if (fInText) {
1098 fContent->writeText("> Tj\n");
1099 fInText = false;
1100 }
1101 }
1102 void setWideChars(bool wideChars) {
1103 if (fWideChars != wideChars) {
1104 SkASSERT(!fInText);
1105 fWideChars = wideChars;
1106 }
1107 }
1108 void writeGlyph(SkScalar x,
1109 SkScalar y,
1110 SkScalar advanceWidth,
1111 uint16_t glyph) {
1112 SkScalar xPosition = x - fCurrentMatrixX;
1113 SkScalar yPosition = y - fCurrentMatrixY;
1114 if (xPosition != fXAdvance || yPosition != 0) {
1115 this->flush();
1116 SkPDFUtils::AppendScalar(xPosition, fContent);
1117 fContent->writeText(" ");
1118 SkPDFUtils::AppendScalar(-yPosition, fContent);
1119 fContent->writeText(" Td ");
1120 fCurrentMatrixX = x;
1121 fCurrentMatrixY = y;
1122 fXAdvance = 0;
1123 }
1124 if (!fInText) {
1125 fContent->writeText("<");
1126 fInText = true;
1127 }
1128 if (fWideChars) {
1129 SkPDFUtils::WriteUInt16BE(fContent, glyph);
1130 } else {
1131 SkASSERT(0 == glyph >> 8);
1132 SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph));
1133 }
1134 fXAdvance += advanceWidth;
1135 }
1136
1137private:
1138 SkDynamicMemoryWStream* fContent;
1139 SkScalar fCurrentMatrixX;
1140 SkScalar fCurrentMatrixY;
1141 SkScalar fXAdvance;
1142 bool fWideChars;
1143 bool fInText;
1144};
1145} // namespace
1146
halcanary66a82f32015-10-12 13:05:04 -07001147static void draw_transparent_text(SkPDFDevice* device,
1148 const SkDraw& d,
1149 const void* text, size_t len,
1150 SkScalar x, SkScalar y,
1151 const SkPaint& srcPaint) {
1152
1153 SkPaint transparent;
1154 if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(),
1155 device->getCanon())) {
1156 SkDEBUGFAIL("default typeface should be embeddable");
1157 return; // Avoid infinite loop in release.
1158 }
1159 transparent.setTextSize(srcPaint.getTextSize());
1160 transparent.setColor(SK_ColorTRANSPARENT);
1161 switch (srcPaint.getTextEncoding()) {
1162 case SkPaint::kGlyphID_TextEncoding: {
1163 // Since a glyphId<->Unicode mapping is typeface-specific,
1164 // map back to Unicode first.
1165 size_t glyphCount = len / 2;
1166 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1167 srcPaint.glyphsToUnichars(
1168 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1169 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
1170 device->drawText(d, &unichars[0],
1171 glyphCount * sizeof(SkUnichar),
1172 x, y, transparent);
1173 break;
1174 }
1175 case SkPaint::kUTF8_TextEncoding:
1176 case SkPaint::kUTF16_TextEncoding:
1177 case SkPaint::kUTF32_TextEncoding:
1178 transparent.setTextEncoding(srcPaint.getTextEncoding());
1179 device->drawText(d, text, len, x, y, transparent);
1180 break;
1181 default:
1182 SkFAIL("unknown text encoding");
1183 }
1184}
1185
1186
halcanary682ee012016-01-28 10:59:34 -08001187void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
halcanarya6814332015-05-27 08:53:36 -07001188 SkScalar x, SkScalar y, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001189 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary6950de62015-11-07 05:29:00 -08001190 // https://bug.skia.org/3866
halcanary66a82f32015-10-12 13:05:04 -07001191 SkPath path;
1192 srcPaint.getTextPath(text, len, x, y, &path);
1193 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1194 // Draw text transparently to make it copyable/searchable/accessable.
1195 draw_transparent_text(this, d, text, len, x, y, srcPaint);
1196 return;
1197 }
halcanarya6814332015-05-27 08:53:36 -07001198 SkPaint paint = srcPaint;
1199 replace_srcmode_on_opaque_paint(&paint);
1200
halcanary96fcdcc2015-08-27 07:41:13 -07001201 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1202 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001203 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1204 // making text unreadable (e.g. same text twice when using CSS shadows).
1205 return;
1206 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001207 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001208 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001209 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001210 return;
1211 }
1212
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001213 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001214 const uint16_t* glyphIDs = nullptr;
reed@google.comaec40662014-04-18 19:29:07 +00001215 int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001216 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001217
benjaminwagnerd936f632016-02-23 10:44:31 -08001218 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001219 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001220 content.entry()->fContent.writeText("BT\n");
1221 set_text_transform(x, y, textPaint.getTextSkewX(),
1222 &content.entry()->fContent);
reed@google.comaec40662014-04-18 19:29:07 +00001223 int consumedGlyphCount = 0;
halcanary2f912f32014-10-16 09:53:20 -07001224
1225 SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs);
1226
halcanary3c35fb32016-06-30 11:55:07 -07001227 SkPDFGlyphSetMap* fontGlyphUsage = fDocument->getGlyphUsage();
1228
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001229 while (numGlyphs > consumedGlyphCount) {
robertphillips8e0c1502015-07-07 10:28:43 -07001230 this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001231 SkPDFFont* font = content.entry()->fState.fFont;
halcanary2f912f32014-10-16 09:53:20 -07001232
1233 int availableGlyphs = font->glyphsToPDFFontEncoding(
1234 glyphIDsCopy.begin() + consumedGlyphCount,
1235 numGlyphs - consumedGlyphCount);
halcanary3c35fb32016-06-30 11:55:07 -07001236 fontGlyphUsage->noteGlyphUsage(
halcanary2f912f32014-10-16 09:53:20 -07001237 font, glyphIDsCopy.begin() + consumedGlyphCount,
1238 availableGlyphs);
halcanaryee41b752016-06-23 14:08:11 -07001239 write_wide_string(&content.entry()->fContent,
1240 glyphIDsCopy.begin() + consumedGlyphCount,
1241 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001242 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001243 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001244 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001245 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001246}
1247
halcanary682ee012016-01-28 10:59:34 -08001248void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -07001249 const SkScalar pos[], int scalarsPerPos,
halcanary682ee012016-01-28 10:59:34 -08001250 const SkPoint& offset, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001251 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary66a82f32015-10-12 13:05:04 -07001252 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos);
1253 SkAutoTMalloc<SkPoint> positionsBuffer;
1254 if (2 != scalarsPerPos) {
1255 int glyphCount = srcPaint.textToGlyphs(text, len, NULL);
1256 positionsBuffer.reset(glyphCount);
1257 for (int i = 0; i < glyphCount; ++i) {
1258 positionsBuffer[i].set(pos[i], 0.0f);
1259 }
1260 positions = &positionsBuffer[0];
1261 }
1262 SkPath path;
1263 srcPaint.getPosTextPath(text, len, positions, &path);
1264 SkMatrix matrix;
1265 matrix.setTranslate(offset);
1266 this->drawPath(d, path, srcPaint, &matrix, true);
1267 // Draw text transparently to make it copyable/searchable/accessable.
1268 draw_transparent_text(
1269 this, d, text, len, offset.x() + positions[0].x(),
1270 offset.y() + positions[0].y(), srcPaint);
1271 return;
1272 }
1273
halcanarya6814332015-05-27 08:53:36 -07001274 SkPaint paint = srcPaint;
1275 replace_srcmode_on_opaque_paint(&paint);
1276
halcanary96fcdcc2015-08-27 07:41:13 -07001277 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1278 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001279 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1280 // making text unreadable (e.g. same text twice when using CSS shadows).
1281 return;
1282 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001283 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001284 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001285 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001286 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001287 return;
1288 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001289
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001290 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001291 const uint16_t* glyphIDs = nullptr;
bungeman22edc832014-10-03 07:55:58 -07001292 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001293 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001294
benjaminwagnerd936f632016-02-23 10:44:31 -08001295 SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001296 content.entry()->fContent.writeText("BT\n");
robertphillips8e0c1502015-07-07 10:28:43 -07001297 this->updateFont(textPaint, glyphIDs[0], content.entry());
halcanaryf0c30f52016-07-15 13:35:45 -07001298 GlyphPositioner glyphPositioner(&content.entry()->fContent,
1299 textPaint.getTextSkewX(),
1300 content.entry()->fState.fFont->multiByteGlyphs());
halcanary3c35fb32016-06-30 11:55:07 -07001301 SkPDFGlyphSetMap* fontGlyphUsage = fDocument->getGlyphUsage();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001302 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001303 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001304 uint16_t encodedValue = glyphIDs[i];
1305 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
bungeman22edc832014-10-03 07:55:58 -07001306 // The current pdf font cannot encode the current glyph.
1307 // Try to get a pdf font which can encode the current glyph.
halcanaryf0c30f52016-07-15 13:35:45 -07001308 glyphPositioner.flush();
robertphillips8e0c1502015-07-07 10:28:43 -07001309 this->updateFont(textPaint, glyphIDs[i], content.entry());
bungeman22edc832014-10-03 07:55:58 -07001310 font = content.entry()->fState.fFont;
halcanaryf0c30f52016-07-15 13:35:45 -07001311 glyphPositioner.setWideChars(font->multiByteGlyphs());
bungeman22edc832014-10-03 07:55:58 -07001312 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
1313 SkDEBUGFAIL("PDF could not encode glyph.");
1314 continue;
1315 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001316 }
bungeman22edc832014-10-03 07:55:58 -07001317
halcanary3c35fb32016-06-30 11:55:07 -07001318 fontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
fmalita05c4a432014-09-29 06:29:53 -07001319 SkScalar x = offset.x() + pos[i * scalarsPerPos];
1320 SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001321 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
halcanaryf0c30f52016-07-15 13:35:45 -07001322
1323 SkScalar advanceWidth = textPaint.measureText(&encodedValue, sizeof(uint16_t));
1324 glyphPositioner.writeGlyph(x, y, advanceWidth, encodedValue);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001325 }
halcanaryf0c30f52016-07-15 13:35:45 -07001326 glyphPositioner.flush(); // Must flush before ending text object.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001327 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001328}
1329
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001330void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001331 int vertexCount, const SkPoint verts[],
1332 const SkPoint texs[], const SkColor colors[],
1333 SkXfermode* xmode, const uint16_t indices[],
1334 int indexCount, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001335 if (d.fRC->isEmpty()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001336 return;
1337 }
reed@google.com85e143c2013-12-30 15:51:25 +00001338 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001339}
1340
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001341void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1342 int x, int y, const SkPaint& paint) {
fmalita6987dca2014-11-13 08:33:37 -08001343 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001344 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001345
1346 SkScalar scalarX = SkIntToScalar(x);
1347 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001348 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1349 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001350 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001351 }
halcanary91fcb3e2016-03-04 13:53:22 -08001352 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1353 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001354 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001355 }
halcanary91fcb3e2016-03-04 13:53:22 -08001356 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1357 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001358 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001359 }
1360
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001361 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001362 return;
1363 }
1364
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001365 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001366 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
reed1e7f5e72016-04-27 07:49:17 -07001367 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001368 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001369 return;
1370 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001371 if (content.needShape()) {
1372 SkPath shape;
1373 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001374 SkIntToScalar(device->width()),
1375 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001376 content.setShape(shape);
1377 }
1378 if (!content.needSource()) {
1379 return;
1380 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001381
halcanaryece83922016-03-08 08:32:12 -08001382 auto xObject = sk_make_sp<SkPDFFormXObject>(pdfDevice);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001383 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001384 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001385}
1386
reed89443ab2014-06-27 11:34:19 -07001387SkImageInfo SkPDFDevice::imageInfo() const {
1388 return fLegacyBitmap.info();
1389}
1390
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001391void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1392 INHERITED::onAttachToCanvas(canvas);
1393
1394 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1395 fClipStack = canvas->getClipStack();
1396}
1397
1398void SkPDFDevice::onDetachFromCanvas() {
1399 INHERITED::onDetachFromCanvas();
1400
halcanary96fcdcc2015-08-27 07:41:13 -07001401 fClipStack = nullptr;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001402}
1403
reede8f30622016-03-23 18:59:25 -07001404sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1405 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001406}
1407
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001408
halcanary8103a342016-03-08 15:10:16 -08001409sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001410 SkTDArray<SkPDFObject*> fonts;
1411 fonts.setReserve(fFontResources.count());
1412 for (SkPDFFont* font : fFontResources) {
1413 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001414 }
halcanary8103a342016-03-08 15:10:16 -08001415 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001416 &fGraphicStateResources,
1417 &fShaderResources,
1418 &fXObjectResources,
1419 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001420}
1421
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001422const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1423 return fFontResources;
1424}
1425
halcanary8103a342016-03-08 15:10:16 -08001426sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001427 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001428 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001429 mediaBox->appendInt(0);
1430 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001431 mediaBox->appendInt(fPageSize.width());
1432 mediaBox->appendInt(fPageSize.height());
1433 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001434}
1435
mtklein5f939ab2016-03-16 10:28:35 -07001436std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001437 SkDynamicMemoryWStream buffer;
1438 this->writeContent(&buffer);
mtklein5f939ab2016-03-16 10:28:35 -07001439 return std::unique_ptr<SkStreamAsset>(
halcanary8103a342016-03-08 15:10:16 -08001440 buffer.bytesWritten() > 0
1441 ? buffer.detachAsStream()
1442 : new SkMemoryStream);
reed@google.com5667afc2011-06-27 14:42:15 +00001443}
1444
halcanary334fcbc2015-02-24 12:56:16 -08001445void SkPDFDevice::writeContent(SkWStream* out) const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001446 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanary334fcbc2015-02-24 12:56:16 -08001447 SkPDFUtils::AppendTransform(fInitialTransform, out);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001448 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001449
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001450 // If the content area is the entire page, then we don't need to clip
1451 // the content area (PDF area clips to the page size). Otherwise,
1452 // we have to clip to the content area; we've already applied the
1453 // initial transform, so just clip to the device size.
1454 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001455 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1456 SkIntToScalar(this->height()));
halcanary96fcdcc2015-08-27 07:41:13 -07001457 emit_clip(nullptr, &r, out);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001458 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001459
halcanary2be7e012016-03-28 11:58:08 -07001460 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, out);
1461 for (const auto& entry : fContentEntries) {
1462 SkPoint translation;
1463 translation.iset(this->getOrigin());
1464 translation.negate();
1465 gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion,
1466 translation);
1467 gsState.updateMatrix(entry.fState.fMatrix);
1468 gsState.updateDrawingState(entry.fState);
1469
1470 entry.fContent.writeToStream(out);
1471 }
1472 gsState.drainStack();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001473}
1474
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001475/* Draws an inverse filled path by using Path Ops to compute the positive
1476 * inverse using the current clip as the inverse bounds.
1477 * Return true if this was an inverse path and was properly handled,
1478 * otherwise returns false and the normal drawing routine should continue,
1479 * either as a (incorrect) fallback or because the path was not inverse
1480 * in the first place.
1481 */
1482bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001483 const SkPaint& paint, bool pathIsMutable,
1484 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001485 if (!origPath.isInverseFillType()) {
1486 return false;
1487 }
1488
reed1e7f5e72016-04-27 07:49:17 -07001489 if (d.fRC->isEmpty()) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001490 return false;
1491 }
1492
1493 SkPath modifiedPath;
1494 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1495 SkPaint noInversePaint(paint);
1496
1497 // Merge stroking operations into final path.
1498 if (SkPaint::kStroke_Style == paint.getStyle() ||
1499 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1500 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1501 if (doFillPath) {
1502 noInversePaint.setStyle(SkPaint::kFill_Style);
1503 noInversePaint.setStrokeWidth(0);
1504 pathPtr = &modifiedPath;
1505 } else {
1506 // To be consistent with the raster output, hairline strokes
1507 // are rendered as non-inverted.
1508 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001509 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001510 return true;
1511 }
1512 }
1513
1514 // Get bounds of clip in current transform space
1515 // (clip bounds are given in device space).
1516 SkRect bounds;
1517 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001518 SkMatrix totalMatrix = *d.fMatrix;
1519 if (prePathMatrix) {
1520 totalMatrix.preConcat(*prePathMatrix);
1521 }
1522 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001523 return false;
1524 }
reed1e7f5e72016-04-27 07:49:17 -07001525 bounds.set(d.fRC->getBounds());
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001526 transformInverse.mapRect(&bounds);
1527
1528 // Extend the bounds by the line width (plus some padding)
1529 // so the edge doesn't cause a visible stroke.
1530 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1531 paint.getStrokeWidth() + SK_Scalar1);
1532
1533 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1534 return false;
1535 }
1536
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001537 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001538 return true;
1539}
1540
reedf70b5312016-03-04 16:36:20 -08001541void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001542 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001543 const char key[], SkData* value) {
1544 if (!value) {
1545 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001546 }
reedf70b5312016-03-04 16:36:20 -08001547
1548 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1549 SkPoint transformedPoint;
1550 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1551 fNamedDestinations.emplace_back(value, transformedPoint);
1552 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001553}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001554
reedf70b5312016-03-04 16:36:20 -08001555void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001556 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001557 const char key[], SkData* value) {
1558 if (!value) {
1559 return;
1560 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001561
1562 SkPath transformedPath = path;
1563 transformedPath.transform(*d.fMatrix);
1564 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001565 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1566 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001567 SkRect transformedRect = SkRect::Make(clip.getBounds());
1568
reedf70b5312016-03-04 16:36:20 -08001569 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001570 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001571 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001572 }
reedf70b5312016-03-04 16:36:20 -08001573 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001574 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001575 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001576 }
reed16108352016-03-03 09:14:36 -08001577 }
halcanary438de492015-04-28 06:21:01 -07001578}
1579
wangxianzhuef6c50a2015-09-17 20:38:02 -07001580void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1581 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001582 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001583 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001584 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001585 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001586 }
halcanary91fcb3e2016-03-04 13:53:22 -08001587 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001588 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001589 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001590 array->appendObject(
1591 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001592 }
1593}
epoger@google.comb58772f2013-03-08 09:09:10 +00001594
halcanary6d622702015-03-25 08:45:42 -07001595void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001596 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001597 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001598 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001599 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001600 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001601 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001602 pdfDest->appendScalar(p.x());
1603 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001604 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001605 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001606 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001607 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001608}
1609
reed@google.comfc641d02012-09-20 17:52:20 +00001610SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
halcanary385fe4d2015-08-26 13:07:48 -07001611 SkPDFFormXObject* xobject = new SkPDFFormXObject(this);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001612 // We always draw the form xobjects that we create back into the device, so
1613 // we simply preserve the font usage instead of pulling it out and merging
1614 // it back in later.
halcanary3c35fb32016-06-30 11:55:07 -07001615 cleanUp(); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001616 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001617 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001618}
1619
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001620void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
1621 SkPDFFormXObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001622 const SkClipStack* clipStack,
1623 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001624 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001625 bool invertClip) {
1626 if (clipRegion.isEmpty() && !invertClip) {
1627 return;
1628 }
1629
halcanary1437c1e2016-03-13 18:30:24 -07001630 auto sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanary989da4a2016-03-21 14:33:17 -07001631 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001632
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001633 SkMatrix identity;
1634 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001635 SkPaint paint;
1636 paint.setXfermodeMode(mode);
1637 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001638 if (!content.entry()) {
1639 return;
1640 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001641 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001642 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001643 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001644
halcanary1437c1e2016-03-13 18:30:24 -07001645 // Call makeNoSmaskGraphicState() instead of
1646 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1647 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001648 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001649 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001650 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001651}
1652
halcanary2be7e012016-03-28 11:58:08 -07001653SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001654 const SkRegion& clipRegion,
1655 const SkMatrix& matrix,
1656 const SkPaint& paint,
1657 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001658 SkPDFFormXObject** dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001659 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001660 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001661 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001662 }
1663
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001664 // The clip stack can come from an SkDraw where it is technically optional.
1665 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001666 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001667 if (clipRegion == fExistingClipRegion) {
1668 clipStack = &fExistingClipStack;
1669 } else {
1670 // GraphicStackState::updateClip expects the clip stack to have
1671 // fExistingClip as a prefix, so start there, then set the clip
1672 // to the passed region.
1673 synthesizedClipStack = fExistingClipStack;
1674 SkPath clipPath;
1675 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001676 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1677 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001678 clipStack = &synthesizedClipStack;
1679 }
1680 }
1681
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001682 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1683 if (paint.getXfermode()) {
1684 paint.getXfermode()->asMode(&xfermode);
1685 }
1686
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001687 // For the following modes, we want to handle source and destination
1688 // separately, so make an object of what's already there.
1689 if (xfermode == SkXfermode::kClear_Mode ||
1690 xfermode == SkXfermode::kSrc_Mode ||
1691 xfermode == SkXfermode::kSrcIn_Mode ||
1692 xfermode == SkXfermode::kDstIn_Mode ||
1693 xfermode == SkXfermode::kSrcOut_Mode ||
1694 xfermode == SkXfermode::kDstOut_Mode ||
1695 xfermode == SkXfermode::kSrcATop_Mode ||
1696 xfermode == SkXfermode::kDstATop_Mode ||
1697 xfermode == SkXfermode::kModulate_Mode) {
1698 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001699 *dst = createFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001700 SkASSERT(isContentEmpty());
1701 } else if (xfermode != SkXfermode::kSrc_Mode &&
1702 xfermode != SkXfermode::kSrcOut_Mode) {
1703 // Except for Src and SrcOut, if there isn't anything already there,
1704 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001705 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001706 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001707 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001708 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001709 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001710
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001711 // Dst xfer mode doesn't draw source at all.
1712 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001713 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001714 }
1715
halcanary2be7e012016-03-28 11:58:08 -07001716 SkPDFDevice::ContentEntry* entry;
1717 if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) {
1718 entry = fContentEntries.back();
1719 } else if (xfermode != SkXfermode::kDstOver_Mode) {
1720 entry = fContentEntries.emplace_back();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001721 } else {
halcanary2be7e012016-03-28 11:58:08 -07001722 entry = fContentEntries.emplace_front();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001723 }
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001724 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001725 hasText, &entry->fState);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001726 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001727}
1728
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001729void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001730 SkPDFFormXObject* dst,
1731 SkPath* shape) {
1732 if (xfermode != SkXfermode::kClear_Mode &&
1733 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001734 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001735 xfermode != SkXfermode::kSrcIn_Mode &&
1736 xfermode != SkXfermode::kDstIn_Mode &&
1737 xfermode != SkXfermode::kSrcOut_Mode &&
1738 xfermode != SkXfermode::kDstOut_Mode &&
1739 xfermode != SkXfermode::kSrcATop_Mode &&
1740 xfermode != SkXfermode::kDstATop_Mode &&
1741 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001742 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001743 return;
1744 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001745 if (xfermode == SkXfermode::kDstOver_Mode) {
1746 SkASSERT(!dst);
halcanary2be7e012016-03-28 11:58:08 -07001747 if (fContentEntries.front()->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001748 // For DstOver, an empty content entry was inserted before the rest
1749 // of the content entries. If nothing was drawn, it needs to be
1750 // removed.
halcanary2be7e012016-03-28 11:58:08 -07001751 fContentEntries.pop_front();
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001752 }
1753 return;
1754 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001755 if (!dst) {
1756 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1757 xfermode == SkXfermode::kSrcOut_Mode);
1758 return;
1759 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001760
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001761 SkASSERT(dst);
halcanary2be7e012016-03-28 11:58:08 -07001762 SkASSERT(fContentEntries.count() == 1);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001763 // Changing the current content into a form-xobject will destroy the clip
1764 // objects which is fine since the xobject will already be clipped. However
1765 // if source has shape, we need to clip it too, so a copy of the clip is
1766 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001767
1768 SkClipStack clipStack = fContentEntries.front()->fState.fClipStack;
1769 SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001770
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001771 SkMatrix identity;
1772 identity.reset();
1773 SkPaint stockPaint;
1774
halcanary48810a02016-03-07 14:57:50 -08001775 sk_sp<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001776 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001777 // If nothing was drawn and there's no shape, then the draw was a
1778 // no-op, but dst needs to be restored for that to be true.
1779 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1780 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1781 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001782 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001783 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001784 ScopedContentEntry content(this, &fExistingClipStack,
1785 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001786 stockPaint);
1787 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1788 &content.entry()->fContent);
1789 return;
1790 } else {
1791 xfermode = SkXfermode::kClear_Mode;
1792 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001793 } else {
halcanary2be7e012016-03-28 11:58:08 -07001794 SkASSERT(fContentEntries.count() == 1);
reed@google.comfc641d02012-09-20 17:52:20 +00001795 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001796 }
1797
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001798 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1799 // without alpha.
1800 if (xfermode == SkXfermode::kSrcATop_Mode) {
1801 // TODO(vandebo): In order to properly support SrcATop we have to track
1802 // the shape of what's been drawn at all times. It's the intersection of
1803 // the non-transparent parts of the device and the outlines (shape) of
1804 // all images and devices drawn.
1805 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001806 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001807 SkXfermode::kSrcOver_Mode, true);
1808 } else {
halcanary48810a02016-03-07 14:57:50 -08001809 sk_sp<SkPDFFormXObject> dstMaskStorage;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001810 SkPDFFormXObject* dstMask = srcFormXObject.get();
halcanary96fcdcc2015-08-27 07:41:13 -07001811 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001812 // Draw shape into a form-xobject.
reed1e7f5e72016-04-27 07:49:17 -07001813 SkRasterClip rc(clipRegion);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001814 SkDraw d;
1815 d.fMatrix = &identity;
reed1e7f5e72016-04-27 07:49:17 -07001816 d.fRC = &rc;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001817 d.fClipStack = &clipStack;
1818 SkPaint filledPaint;
1819 filledPaint.setColor(SK_ColorBLACK);
1820 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001821 this->drawPath(d, *shape, filledPaint, nullptr, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001822
1823 dstMaskStorage.reset(createFormXObjectFromDevice());
1824 dstMask = dstMaskStorage.get();
1825 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001826 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1827 &fExistingClipStack, fExistingClipRegion,
1828 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001829 }
1830
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001831 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001832 return;
1833 } else if (xfermode == SkXfermode::kSrc_Mode ||
1834 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001835 ScopedContentEntry content(this, &fExistingClipStack,
1836 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001837 if (content.entry()) {
1838 SkPDFUtils::DrawFormXObject(
1839 this->addXObjectResource(srcFormXObject.get()),
1840 &content.entry()->fContent);
1841 }
1842 if (xfermode == SkXfermode::kSrc_Mode) {
1843 return;
1844 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001845 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001846 ScopedContentEntry content(this, &fExistingClipStack,
1847 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001848 if (content.entry()) {
1849 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1850 &content.entry()->fContent);
1851 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001852 }
1853
1854 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1855 xfermode == SkXfermode::kDstIn_Mode ||
1856 xfermode == SkXfermode::kSrcOut_Mode ||
1857 xfermode == SkXfermode::kDstOut_Mode ||
1858 xfermode == SkXfermode::kSrcATop_Mode ||
1859 xfermode == SkXfermode::kDstATop_Mode ||
1860 xfermode == SkXfermode::kModulate_Mode);
1861
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001862 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001863 xfermode == SkXfermode::kSrcOut_Mode ||
1864 xfermode == SkXfermode::kSrcATop_Mode) {
1865 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001866 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001867 SkXfermode::kSrcOver_Mode,
1868 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001869 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001870 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
1871 if (xfermode == SkXfermode::kModulate_Mode) {
1872 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001873 dst, &fExistingClipStack,
1874 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001875 SkXfermode::kSrcOver_Mode, false);
1876 mode = SkXfermode::kMultiply_Mode;
1877 }
1878 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001879 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001880 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001881 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001882}
1883
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001884bool SkPDFDevice::isContentEmpty() {
halcanary2be7e012016-03-28 11:58:08 -07001885 if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) {
1886 SkASSERT(fContentEntries.count() <= 1);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001887 return true;
1888 }
1889 return false;
1890}
1891
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001892void SkPDFDevice::populateGraphicStateEntryFromPaint(
1893 const SkMatrix& matrix,
1894 const SkClipStack& clipStack,
1895 const SkRegion& clipRegion,
1896 const SkPaint& paint,
1897 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001898 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001899 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1900 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1901 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001902
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001903 entry->fMatrix = matrix;
1904 entry->fClipStack = clipStack;
1905 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001906 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1907 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001908
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001909 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001910 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001911 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001912 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001913 if (shader) {
1914 // PDF positions patterns relative to the initial transform, so
1915 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001916 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001917 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001918
1919 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001920 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001921 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001922
1923 // We need to apply the initial transform to bounds in order to get
1924 // bounds in a consistent coordinate system.
1925 SkRect boundsTemp;
1926 boundsTemp.set(bounds);
1927 fInitialTransform.mapRect(&boundsTemp);
1928 boundsTemp.roundOut(&bounds);
1929
halcanary792c80f2015-02-20 07:21:05 -08001930 SkScalar rasterScale =
1931 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
1932 pdfShader.reset(SkPDFShader::GetPDFShader(
reedfe630452016-03-25 09:08:00 -07001933 fDocument, fRasterDpi, shader, transform, bounds, rasterScale));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001934
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001935 if (pdfShader.get()) {
1936 // pdfShader has been canonicalized so we can directly compare
1937 // pointers.
1938 int resourceIndex = fShaderResources.find(pdfShader.get());
1939 if (resourceIndex < 0) {
1940 resourceIndex = fShaderResources.count();
1941 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001942 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001943 }
1944 entry->fShaderIndex = resourceIndex;
1945 } else {
1946 // A color shader is treated as an invalid shader so we don't have
1947 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001948 SkShader::GradientInfo gradientInfo;
1949 SkColor gradientColor;
1950 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001951 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001952 gradientInfo.fColorCount = 1;
1953 if (shader->asAGradient(&gradientInfo) ==
1954 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001955 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1956 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001957 }
1958 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001959 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001960
halcanary48810a02016-03-07 14:57:50 -08001961 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001962 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001963 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001964 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001965 } else {
1966 SkPaint newPaint = paint;
1967 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001968 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001969 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001970 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001971 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001972 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001973
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001974 if (hasText) {
1975 entry->fTextScaleX = paint.getTextScaleX();
1976 entry->fTextFill = paint.getStyle();
1977 } else {
1978 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001979 }
1980}
1981
halcanarybe27a112015-04-01 13:31:19 -07001982int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001983 // Assumes that gs has been canonicalized (so we can directly compare
1984 // pointers).
1985 int result = fGraphicStateResources.find(gs);
1986 if (result < 0) {
1987 result = fGraphicStateResources.count();
1988 fGraphicStateResources.push(gs);
1989 gs->ref();
1990 }
1991 return result;
1992}
1993
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001994int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
1995 // Assumes that xobject has been canonicalized (so we can directly compare
1996 // pointers).
1997 int result = fXObjectResources.find(xObject);
1998 if (result < 0) {
1999 result = fXObjectResources.count();
2000 fXObjectResources.push(xObject);
2001 xObject->ref();
2002 }
2003 return result;
2004}
2005
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002006void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
halcanary2be7e012016-03-28 11:58:08 -07002007 SkPDFDevice::ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002008 SkTypeface* typeface = paint.getTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -07002009 if (contentEntry->fState.fFont == nullptr ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002010 contentEntry->fState.fTextSize != paint.getTextSize() ||
2011 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002012 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00002013 contentEntry->fContent.writeText("/");
2014 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
2015 SkPDFResourceDict::kFont_ResourceType,
2016 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002017 contentEntry->fContent.writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -07002018 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002019 contentEntry->fContent.writeText(" Tf\n");
2020 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002021 }
2022}
2023
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002024int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08002025 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07002026 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002027 int resourceIndex = fFontResources.find(newFont.get());
2028 if (resourceIndex < 0) {
2029 resourceIndex = fFontResources.count();
2030 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002031 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002032 }
2033 return resourceIndex;
2034}
2035
halcanary7a14b312015-10-01 07:28:13 -07002036static SkSize rect_to_size(const SkRect& r) {
2037 return SkSize::Make(r.width(), r.height());
2038}
2039
halcanarya50151d2016-03-25 11:57:49 -07002040static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
2041 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07002042 auto surface =
halcanarya50151d2016-03-25 11:57:49 -07002043 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
2044 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07002045 SkCanvas* canvas = surface->getCanvas();
2046 canvas->clear(SK_ColorTRANSPARENT);
2047 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07002048 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07002049 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07002050 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07002051 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07002052}
2053
2054////////////////////////////////////////////////////////////////////////////////
2055void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
2056 const SkClipStack* clipStack,
2057 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07002058 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07002059 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07002060 if (imageBitmap.dimensions().isZero()) {
2061 return;
2062 }
halcanary7a14b312015-10-01 07:28:13 -07002063 #ifdef SK_PDF_IMAGE_STATS
2064 gDrawImageCalls.fetch_add(1);
2065 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002066 SkMatrix matrix = origMatrix;
2067 SkRegion perspectiveBounds;
2068 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07002069 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002070
2071 // Rasterize the bitmap using perspective in a new bitmap.
2072 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002073 if (fRasterDpi == 0) {
2074 return;
2075 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002076 // Transform the bitmap in the new space, without taking into
2077 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002078 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07002079 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07002080 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002081 perspectiveOutline.transform(origMatrix);
2082
2083 // TODO(edisonn): perf - use current clip too.
2084 // Retrieve the bounds of the new shape.
2085 SkRect bounds = perspectiveOutline.getBounds();
2086
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002087 // Transform the bitmap in the new space, taking into
2088 // account the initial transform.
2089 SkMatrix total = origMatrix;
2090 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07002091 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
2092 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2093 total.postScale(dpiScale, dpiScale);
2094
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002095 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002096 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002097 physicalPerspectiveOutline.transform(total);
2098
halcanary7a14b312015-10-01 07:28:13 -07002099 SkRect physicalPerspectiveBounds =
2100 physicalPerspectiveOutline.getBounds();
2101 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2102 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002103
2104 // TODO(edisonn): A better approach would be to use a bitmap shader
2105 // (in clamp mode) and draw a rect over the entire bounding box. Then
2106 // intersect perspectiveOutline to the clip. That will avoid introducing
2107 // alpha to the image while still giving good behavior at the edge of
2108 // the image. Avoiding alpha will reduce the pdf size and generation
2109 // CPU time some.
2110
halcanary7a14b312015-10-01 07:28:13 -07002111 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2112
reede8f30622016-03-23 18:59:25 -07002113 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002114 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002115 return;
2116 }
halcanary7a14b312015-10-01 07:28:13 -07002117 SkCanvas* canvas = surface->getCanvas();
2118 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002119
2120 SkScalar deltaX = bounds.left();
2121 SkScalar deltaY = bounds.top();
2122
2123 SkMatrix offsetMatrix = origMatrix;
2124 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002125 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002126
2127 // Translate the draw in the new canvas, so we perfectly fit the
2128 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002129 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002130 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002131 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002132 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002133
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002134 // In the new space, we use the identity matrix translated
2135 // and scaled to reflect DPI.
2136 matrix.setScale(1 / scaleX, 1 / scaleY);
2137 matrix.postTranslate(deltaX, deltaY);
2138
halcanary7a14b312015-10-01 07:28:13 -07002139 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002140 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002141
halcanarya50151d2016-03-25 11:57:49 -07002142 autoImageUnref = surface->makeImageSnapshot();
2143 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002144 }
2145
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002146 SkMatrix scaled;
2147 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002148 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2149 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002150 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002151 SkIRect subset = imageBitmap.bounds();
2152 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2153 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002154 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002155 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002156 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002157 return;
2158 }
2159 if (content.needShape()) {
2160 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002161 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002162 shape.transform(matrix);
2163 content.setShape(shape);
2164 }
2165 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002166 return;
2167 }
2168
halcanary287d22d2015-09-24 10:20:05 -07002169 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002170 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002171 // draw calls. This code here works for all
2172 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2173 // rasterize a layer on this backend). Fortuanely, this seems
2174 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002175 autoImageUnref = color_filter(imageBitmap, colorFilter);
2176 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002177 // TODO(halcanary): de-dupe this by caching filtered images.
2178 // (maybe in the resource cache?)
2179 }
halcanarya50151d2016-03-25 11:57:49 -07002180
2181 SkBitmapKey key = imageBitmap.getKey();
2182 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002183 if (!pdfimage) {
halcanarya50151d2016-03-25 11:57:49 -07002184 auto img = imageBitmap.makeImage();
2185 if (!img) {
2186 return;
2187 }
2188 pdfimage = SkPDFCreateBitmapObject(
2189 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002190 if (!pdfimage) {
2191 return;
halcanary287d22d2015-09-24 10:20:05 -07002192 }
halcanarya50151d2016-03-25 11:57:49 -07002193 fDocument->serialize(pdfimage); // serialize images early.
2194 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002195 }
halcanarya50151d2016-03-25 11:57:49 -07002196 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002197 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002198 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002199}