blob: 6972f8b01f46e255f2ed5bc995948462bbdd04cb [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"
reedf70b5312016-03-04 16:36:20 -08009#include "SkAnnotationKeys.h"
reed7503d602016-07-15 14:23:29 -070010#include "SkBitmapDevice.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)) {
reed7503d602016-07-15 14:23:29 -0700546 // need to return a raster device, which we will detect in drawDevice()
547 return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
senorblancob0e89dc2014-10-20 14:03:12 -0700548 }
fmalita6987dca2014-11-13 08:33:37 -0800549 SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
halcanary989da4a2016-03-21 14:33:17 -0700550 return SkPDFDevice::Create(size, fRasterDpi, fDocument);
bsalomon@google.come97f0852011-06-17 13:10:25 +0000551}
552
halcanary989da4a2016-03-21 14:33:17 -0700553SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); }
554
bsalomon@google.come97f0852011-06-17 13:10:25 +0000555
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000556
557// A helper class to automatically finish a ContentEntry at the end of a
558// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000559class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000560public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000561 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
562 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000563 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700564 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000565 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700566 fDstFormXObject(nullptr) {
reed1e7f5e72016-04-27 07:49:17 -0700567 init(draw.fClipStack, draw.fRC->bwRgn(), *draw.fMatrix, paint, hasText);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000568 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000569 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
570 const SkRegion& clipRegion, const SkMatrix& matrix,
571 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000572 : fDevice(device),
halcanary96fcdcc2015-08-27 07:41:13 -0700573 fContentEntry(nullptr),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000574 fXfermode(SkXfermode::kSrcOver_Mode),
halcanary96fcdcc2015-08-27 07:41:13 -0700575 fDstFormXObject(nullptr) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000576 init(clipStack, clipRegion, matrix, paint, hasText);
577 }
578
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000579 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000580 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000581 SkPath* shape = &fShape;
582 if (shape->isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700583 shape = nullptr;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000584 }
585 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000586 }
reed@google.comfc641d02012-09-20 17:52:20 +0000587 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000588 }
589
halcanary2be7e012016-03-28 11:58:08 -0700590 SkPDFDevice::ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000591
592 /* Returns true when we explicitly need the shape of the drawing. */
593 bool needShape() {
594 switch (fXfermode) {
595 case SkXfermode::kClear_Mode:
596 case SkXfermode::kSrc_Mode:
597 case SkXfermode::kSrcIn_Mode:
598 case SkXfermode::kSrcOut_Mode:
599 case SkXfermode::kDstIn_Mode:
600 case SkXfermode::kDstOut_Mode:
601 case SkXfermode::kSrcATop_Mode:
602 case SkXfermode::kDstATop_Mode:
603 case SkXfermode::kModulate_Mode:
604 return true;
605 default:
606 return false;
607 }
608 }
609
610 /* Returns true unless we only need the shape of the drawing. */
611 bool needSource() {
612 if (fXfermode == SkXfermode::kClear_Mode) {
613 return false;
614 }
615 return true;
616 }
617
618 /* If the shape is different than the alpha component of the content, then
619 * setShape should be called with the shape. In particular, images and
620 * devices have rectangular shape.
621 */
622 void setShape(const SkPath& shape) {
623 fShape = shape;
624 }
625
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000626private:
627 SkPDFDevice* fDevice;
halcanary2be7e012016-03-28 11:58:08 -0700628 SkPDFDevice::ContentEntry* fContentEntry;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000629 SkXfermode::Mode fXfermode;
halcanary4b1e17e2016-07-27 14:49:46 -0700630 SkPDFObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000631 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000632
633 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
634 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000635 // Shape has to be flatten before we get here.
636 if (matrix.hasPerspective()) {
637 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000638 return;
639 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000640 if (paint.getXfermode()) {
641 paint.getXfermode()->asMode(&fXfermode);
642 }
643 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
644 matrix, paint, hasText,
645 &fDstFormXObject);
646 }
647};
648
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000649////////////////////////////////////////////////////////////////////////////////
650
halcanary989da4a2016-03-21 14:33:17 -0700651SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip)
robertphillips9a53fd72015-06-22 09:46:59 -0700652 : INHERITED(SkSurfaceProps(0, kUnknown_SkPixelGeometry))
653 , fPageSize(pageSize)
halcanarya1f1ee92015-02-20 06:17:26 -0800654 , fContentSize(pageSize)
655 , fExistingClipRegion(SkIRect::MakeSize(pageSize))
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);
robertphillips1f3923e2016-07-21 07:17:54 -0700660
halcanarya1f1ee92015-02-20 06:17:26 -0800661 if (flip) {
662 // Skia generally uses the top left as the origin but PDF
663 // natively has the origin at the bottom left. This matrix
664 // corrects for that. But that only needs to be done once, we
665 // don't do it when layering.
666 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
667 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
668 } else {
669 fInitialTransform.setIdentity();
670 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000671}
672
673SkPDFDevice::~SkPDFDevice() {
halcanary3c35fb32016-06-30 11:55:07 -0700674 this->cleanUp();
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000675}
676
677void SkPDFDevice::init() {
mtklein852f15d2016-03-17 10:51:27 -0700678 fContentEntries.reset();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000679}
680
halcanary3c35fb32016-06-30 11:55:07 -0700681void SkPDFDevice::cleanUp() {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000682 fGraphicStateResources.unrefAll();
683 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000684 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000685 fShaderResources.unrefAll();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000686}
687
reedf70b5312016-03-04 16:36:20 -0800688void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[],
689 SkData* value) {
690 if (0 == rect.width() && 0 == rect.height()) {
691 handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value);
692 } else {
693 SkPath path;
694 path.addRect(rect);
695 handlePathAnnotation(path, d, key, value);
696 }
697}
698
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000699void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000700 SkPaint newPaint = paint;
halcanarya6814332015-05-27 08:53:36 -0700701 replace_srcmode_on_opaque_paint(&newPaint);
702
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000703 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000704 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000705 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000706}
707
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000708void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
halcanary2be7e012016-03-28 11:58:08 -0700709 SkPDFDevice::ContentEntry* contentEntry) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000710 if (!contentEntry) {
711 return;
712 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000713 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
714 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000715 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000716 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000717 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000718 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000719 inverse.mapRect(&bbox);
720
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000721 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000722 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000723 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000724}
725
halcanary682ee012016-01-28 10:59:34 -0800726void SkPDFDevice::drawPoints(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700727 SkCanvas::PointMode mode,
728 size_t count,
729 const SkPoint* points,
730 const SkPaint& srcPaint) {
731 SkPaint passedPaint = srcPaint;
732 replace_srcmode_on_opaque_paint(&passedPaint);
733
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000734 if (count == 0) {
735 return;
736 }
737
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000738 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
739 // We only use this when there's a path effect because of the overhead
740 // of multiple calls to setUpContentEntry it causes.
741 if (passedPaint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700742 if (d.fRC->isEmpty()) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000743 return;
744 }
745 SkDraw pointDraw(d);
746 pointDraw.fDevice = this;
747 pointDraw.drawPoints(mode, count, points, passedPaint, true);
748 return;
749 }
750
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000751 const SkPaint* paint = &passedPaint;
752 SkPaint modifiedPaint;
753
754 if (mode == SkCanvas::kPoints_PointMode &&
755 paint->getStrokeCap() != SkPaint::kRound_Cap) {
756 modifiedPaint = *paint;
757 paint = &modifiedPaint;
758 if (paint->getStrokeWidth()) {
759 // PDF won't draw a single point with square/butt caps because the
760 // orientation is ambiguous. Draw a rectangle instead.
761 modifiedPaint.setStyle(SkPaint::kFill_Style);
762 SkScalar strokeWidth = paint->getStrokeWidth();
763 SkScalar halfStroke = SkScalarHalf(strokeWidth);
764 for (size_t i = 0; i < count; i++) {
765 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
766 r.inset(-halfStroke, -halfStroke);
767 drawRect(d, r, modifiedPaint);
768 }
769 return;
770 } else {
771 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
772 }
773 }
774
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000775 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000776 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000777 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000778 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000779
780 switch (mode) {
781 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000782 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000783 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000784 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000785 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000786 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000787 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000788 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000789 break;
790 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000791 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000792 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000793 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000794 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000795 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000796 &content.entry()->fContent);
797 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000798 }
799 break;
800 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000801 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
802 for (size_t i = 0; i < count; i++) {
803 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000804 &content.entry()->fContent);
805 SkPDFUtils::ClosePath(&content.entry()->fContent);
806 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000807 }
808 break;
809 default:
810 SkASSERT(false);
811 }
812}
813
halcanary8103a342016-03-08 15:10:16 -0800814static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) {
halcanaryece83922016-03-08 08:32:12 -0800815 auto annotation = sk_make_sp<SkPDFDict>("Annot");
wangxianzhud76665d2015-07-17 17:23:15 -0700816 annotation->insertName("Subtype", "Link");
halcanary488165e2016-04-22 06:10:21 -0700817 annotation->insertInt("F", 4); // required by ISO 19005
wangxianzhud76665d2015-07-17 17:23:15 -0700818
halcanaryece83922016-03-08 08:32:12 -0800819 auto border = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700820 border->reserve(3);
821 border->appendInt(0); // Horizontal corner radius.
822 border->appendInt(0); // Vertical corner radius.
823 border->appendInt(0); // Width, 0 = no border.
halcanary8103a342016-03-08 15:10:16 -0800824 annotation->insertObject("Border", std::move(border));
wangxianzhud76665d2015-07-17 17:23:15 -0700825
halcanaryece83922016-03-08 08:32:12 -0800826 auto rect = sk_make_sp<SkPDFArray>();
wangxianzhud76665d2015-07-17 17:23:15 -0700827 rect->reserve(4);
828 rect->appendScalar(translatedRect.fLeft);
829 rect->appendScalar(translatedRect.fTop);
830 rect->appendScalar(translatedRect.fRight);
831 rect->appendScalar(translatedRect.fBottom);
halcanary8103a342016-03-08 15:10:16 -0800832 annotation->insertObject("Rect", std::move(rect));
wangxianzhud76665d2015-07-17 17:23:15 -0700833
halcanary8103a342016-03-08 15:10:16 -0800834 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700835}
836
halcanary8103a342016-03-08 15:10:16 -0800837static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700838 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700839 SkString url(static_cast<const char *>(urlData->data()),
840 urlData->size() - 1);
halcanaryece83922016-03-08 08:32:12 -0800841 auto action = sk_make_sp<SkPDFDict>("Action");
wangxianzhud76665d2015-07-17 17:23:15 -0700842 action->insertName("S", "URI");
843 action->insertString("URI", url);
halcanary8103a342016-03-08 15:10:16 -0800844 annotation->insertObject("A", std::move(action));
845 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700846}
847
halcanary8103a342016-03-08 15:10:16 -0800848static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData,
849 const SkRect& r) {
halcanary4b1e17e2016-07-27 14:49:46 -0700850 sk_sp<SkPDFDict> annotation = create_link_annotation(r);
wangxianzhud76665d2015-07-17 17:23:15 -0700851 SkString name(static_cast<const char *>(nameData->data()),
852 nameData->size() - 1);
853 annotation->insertName("Dest", name);
halcanary8103a342016-03-08 15:10:16 -0800854 return annotation;
wangxianzhud76665d2015-07-17 17:23:15 -0700855}
856
halcanary682ee012016-01-28 10:59:34 -0800857void SkPDFDevice::drawRect(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700858 const SkRect& rect,
859 const SkPaint& srcPaint) {
860 SkPaint paint = srcPaint;
861 replace_srcmode_on_opaque_paint(&paint);
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000862 SkRect r = rect;
863 r.sort();
864
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000865 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700866 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000867 return;
868 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000869 SkPath path;
870 path.addRect(r);
halcanary96fcdcc2015-08-27 07:41:13 -0700871 drawPath(d, path, paint, nullptr, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000872 return;
873 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000874
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000875 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000876 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000877 return;
878 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000879 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000880 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000881 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000882}
883
halcanarya6814332015-05-27 08:53:36 -0700884void SkPDFDevice::drawRRect(const SkDraw& draw,
885 const SkRRect& rrect,
886 const SkPaint& srcPaint) {
887 SkPaint paint = srcPaint;
888 replace_srcmode_on_opaque_paint(&paint);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000889 SkPath path;
890 path.addRRect(rrect);
halcanary96fcdcc2015-08-27 07:41:13 -0700891 this->drawPath(draw, path, paint, nullptr, true);
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000892}
893
halcanarya6814332015-05-27 08:53:36 -0700894void SkPDFDevice::drawOval(const SkDraw& draw,
895 const SkRect& oval,
896 const SkPaint& srcPaint) {
897 SkPaint paint = srcPaint;
898 replace_srcmode_on_opaque_paint(&paint);
reed89443ab2014-06-27 11:34:19 -0700899 SkPath path;
900 path.addOval(oval);
halcanary96fcdcc2015-08-27 07:41:13 -0700901 this->drawPath(draw, path, paint, nullptr, true);
reed89443ab2014-06-27 11:34:19 -0700902}
903
halcanary682ee012016-01-28 10:59:34 -0800904void SkPDFDevice::drawPath(const SkDraw& d,
halcanarya6814332015-05-27 08:53:36 -0700905 const SkPath& origPath,
906 const SkPaint& srcPaint,
907 const SkMatrix* prePathMatrix,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000908 bool pathIsMutable) {
halcanarya6814332015-05-27 08:53:36 -0700909 SkPaint paint = srcPaint;
910 replace_srcmode_on_opaque_paint(&paint);
halcanary682ee012016-01-28 10:59:34 -0800911 SkPath modifiedPath;
912 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000913
914 SkMatrix matrix = *d.fMatrix;
915 if (prePathMatrix) {
916 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
halcanary682ee012016-01-28 10:59:34 -0800917 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000918 pathPtr = &modifiedPath;
919 pathIsMutable = true;
920 }
halcanary682ee012016-01-28 10:59:34 -0800921 origPath.transform(*prePathMatrix, pathPtr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000922 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +0000923 matrix.preConcat(*prePathMatrix);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000924 }
925 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000926
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000927 if (paint.getPathEffect()) {
reed1e7f5e72016-04-27 07:49:17 -0700928 if (d.fRC->isEmpty()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000929 return;
930 }
halcanary682ee012016-01-28 10:59:34 -0800931 if (!pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000932 pathPtr = &modifiedPath;
933 pathIsMutable = true;
934 }
halcanary682ee012016-01-28 10:59:34 -0800935 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000936
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000937 SkPaint noEffectPaint(paint);
halcanary96fcdcc2015-08-27 07:41:13 -0700938 noEffectPaint.setPathEffect(nullptr);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000939 if (fill) {
940 noEffectPaint.setStyle(SkPaint::kFill_Style);
941 } else {
942 noEffectPaint.setStyle(SkPaint::kStroke_Style);
943 noEffectPaint.setStrokeWidth(0);
944 }
halcanary96fcdcc2015-08-27 07:41:13 -0700945 drawPath(d, *pathPtr, noEffectPaint, nullptr, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000946 return;
947 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000948
edisonn@google.coma9ebd162013-10-07 13:22:21 +0000949 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000950 return;
951 }
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000952
reed1e7f5e72016-04-27 07:49:17 -0700953 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000954 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000955 return;
956 }
halcanary8b2bc252015-10-06 09:41:47 -0700957 bool consumeDegeratePathSegments =
958 paint.getStyle() == SkPaint::kFill_Style ||
959 (paint.getStrokeCap() != SkPaint::kRound_Cap &&
960 paint.getStrokeCap() != SkPaint::kSquare_Cap);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000961 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
halcanary8b2bc252015-10-06 09:41:47 -0700962 consumeDegeratePathSegments,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000963 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000964 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000965 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000966}
967
halcanary7a14b312015-10-01 07:28:13 -0700968void SkPDFDevice::drawBitmapRect(const SkDraw& draw,
969 const SkBitmap& bitmap,
970 const SkRect* src,
971 const SkRect& dst,
972 const SkPaint& srcPaint,
973 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -0700974 SkASSERT(false);
halcanary7a14b312015-10-01 07:28:13 -0700975}
976
977void SkPDFDevice::drawBitmap(const SkDraw& d,
978 const SkBitmap& bitmap,
979 const SkMatrix& matrix,
980 const SkPaint& srcPaint) {
halcanarya6814332015-05-27 08:53:36 -0700981 SkPaint paint = srcPaint;
982 if (bitmap.isOpaque()) {
983 replace_srcmode_on_opaque_paint(&paint);
984 }
985
reed1e7f5e72016-04-27 07:49:17 -0700986 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -0700987 return;
988 }
edisonn@google.com2ae67e72013-02-12 01:06:38 +0000989
halcanary7a14b312015-10-01 07:28:13 -0700990 SkMatrix transform = matrix;
991 transform.postConcat(*d.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -0700992 SkImageBitmap imageBitmap(bitmap);
993 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -0700994 transform, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -0700995}
996
997void SkPDFDevice::drawSprite(const SkDraw& d,
998 const SkBitmap& bitmap,
999 int x,
1000 int y,
1001 const SkPaint& srcPaint) {
1002 SkPaint paint = srcPaint;
1003 if (bitmap.isOpaque()) {
1004 replace_srcmode_on_opaque_paint(&paint);
1005 }
1006
reed1e7f5e72016-04-27 07:49:17 -07001007 if (d.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -07001008 return;
1009 }
1010
1011 SkMatrix matrix;
1012 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
halcanarya50151d2016-03-25 11:57:49 -07001013 SkImageBitmap imageBitmap(bitmap);
1014 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -07001015 matrix, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001016}
1017
1018void SkPDFDevice::drawImage(const SkDraw& draw,
1019 const SkImage* image,
1020 SkScalar x,
1021 SkScalar y,
1022 const SkPaint& srcPaint) {
1023 SkPaint paint = srcPaint;
1024 if (!image) {
1025 return;
1026 }
1027 if (image->isOpaque()) {
1028 replace_srcmode_on_opaque_paint(&paint);
1029 }
reed1e7f5e72016-04-27 07:49:17 -07001030 if (draw.fRC->isEmpty()) {
halcanary7a14b312015-10-01 07:28:13 -07001031 return;
1032 }
1033 SkMatrix transform = SkMatrix::MakeTrans(x, y);
1034 transform.postConcat(*draw.fMatrix);
halcanarya50151d2016-03-25 11:57:49 -07001035 SkImageBitmap imageBitmap(const_cast<SkImage*>(image));
1036 this->internalDrawImage(
reed1e7f5e72016-04-27 07:49:17 -07001037 transform, draw.fClipStack, draw.fRC->bwRgn(), imageBitmap, paint);
halcanary7a14b312015-10-01 07:28:13 -07001038}
1039
1040void SkPDFDevice::drawImageRect(const SkDraw& draw,
1041 const SkImage* image,
1042 const SkRect* src,
1043 const SkRect& dst,
1044 const SkPaint& srcPaint,
1045 SkCanvas::SrcRectConstraint constraint) {
halcanary66be6262016-03-21 13:01:34 -07001046 SkASSERT(false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001047}
1048
halcanarybb264b72015-04-07 10:40:03 -07001049// Create a PDF string. Maximum length (in bytes) is 65,535.
1050// @param input A string value.
1051// @param len The length of the input array.
1052// @param wideChars True iff the upper byte in each uint16_t is
1053// significant and should be encoded and not
1054// discarded. If true, the upper byte is encoded
1055// first. Otherwise, we assert the upper byte is
1056// zero.
halcanaryee41b752016-06-23 14:08:11 -07001057static void write_wide_string(SkDynamicMemoryWStream* wStream,
1058 const uint16_t* input,
1059 size_t len,
1060 bool wideChars) {
halcanarybb264b72015-04-07 10:40:03 -07001061 if (wideChars) {
1062 SkASSERT(2 * len < 65535);
halcanaryee41b752016-06-23 14:08:11 -07001063 wStream->writeText("<");
halcanarybb264b72015-04-07 10:40:03 -07001064 for (size_t i = 0; i < len; i++) {
halcanarya76a10b72016-07-07 12:31:55 -07001065 SkPDFUtils::WriteUInt16BE(wStream, input[i]);
halcanarybb264b72015-04-07 10:40:03 -07001066 }
halcanaryee41b752016-06-23 14:08:11 -07001067 wStream->writeText(">");
halcanarybb264b72015-04-07 10:40:03 -07001068 } else {
1069 SkASSERT(len <= 65535);
halcanaryee41b752016-06-23 14:08:11 -07001070 SkAutoMalloc buffer(len); // Remove every other byte.
1071 uint8_t* ptr = (uint8_t*)buffer.get();
halcanarybb264b72015-04-07 10:40:03 -07001072 for (size_t i = 0; i < len; i++) {
1073 SkASSERT(0 == input[i] >> 8);
halcanaryee41b752016-06-23 14:08:11 -07001074 ptr[i] = static_cast<uint8_t>(input[i]);
halcanarybb264b72015-04-07 10:40:03 -07001075 }
halcanaryee41b752016-06-23 14:08:11 -07001076 SkPDFUtils::WriteString(wStream, (char*)buffer.get(), len);
halcanarybb264b72015-04-07 10:40:03 -07001077 }
1078}
1079
halcanaryf0c30f52016-07-15 13:35:45 -07001080namespace {
1081class GlyphPositioner {
1082public:
1083 GlyphPositioner(SkDynamicMemoryWStream* content,
1084 SkScalar textSkewX,
1085 bool wideChars)
1086 : fContent(content)
1087 , fCurrentMatrixX(0.0f)
1088 , fCurrentMatrixY(0.0f)
1089 , fXAdvance(0.0f)
1090 , fWideChars(wideChars)
1091 , fInText(false) {
1092 set_text_transform(0.0f, 0.0f, textSkewX, fContent);
1093 }
1094 ~GlyphPositioner() { SkASSERT(!fInText); /* flush first */ }
1095 void flush() {
1096 if (fInText) {
1097 fContent->writeText("> Tj\n");
1098 fInText = false;
1099 }
1100 }
1101 void setWideChars(bool wideChars) {
1102 if (fWideChars != wideChars) {
1103 SkASSERT(!fInText);
1104 fWideChars = wideChars;
1105 }
1106 }
1107 void writeGlyph(SkScalar x,
1108 SkScalar y,
1109 SkScalar advanceWidth,
1110 uint16_t glyph) {
1111 SkScalar xPosition = x - fCurrentMatrixX;
1112 SkScalar yPosition = y - fCurrentMatrixY;
1113 if (xPosition != fXAdvance || yPosition != 0) {
1114 this->flush();
1115 SkPDFUtils::AppendScalar(xPosition, fContent);
1116 fContent->writeText(" ");
1117 SkPDFUtils::AppendScalar(-yPosition, fContent);
1118 fContent->writeText(" Td ");
1119 fCurrentMatrixX = x;
1120 fCurrentMatrixY = y;
1121 fXAdvance = 0;
1122 }
1123 if (!fInText) {
1124 fContent->writeText("<");
1125 fInText = true;
1126 }
1127 if (fWideChars) {
1128 SkPDFUtils::WriteUInt16BE(fContent, glyph);
1129 } else {
1130 SkASSERT(0 == glyph >> 8);
1131 SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph));
1132 }
1133 fXAdvance += advanceWidth;
1134 }
1135
1136private:
1137 SkDynamicMemoryWStream* fContent;
1138 SkScalar fCurrentMatrixX;
1139 SkScalar fCurrentMatrixY;
1140 SkScalar fXAdvance;
1141 bool fWideChars;
1142 bool fInText;
1143};
1144} // namespace
1145
halcanary66a82f32015-10-12 13:05:04 -07001146static void draw_transparent_text(SkPDFDevice* device,
1147 const SkDraw& d,
1148 const void* text, size_t len,
1149 SkScalar x, SkScalar y,
1150 const SkPaint& srcPaint) {
1151
1152 SkPaint transparent;
1153 if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(),
1154 device->getCanon())) {
1155 SkDEBUGFAIL("default typeface should be embeddable");
1156 return; // Avoid infinite loop in release.
1157 }
1158 transparent.setTextSize(srcPaint.getTextSize());
1159 transparent.setColor(SK_ColorTRANSPARENT);
1160 switch (srcPaint.getTextEncoding()) {
1161 case SkPaint::kGlyphID_TextEncoding: {
1162 // Since a glyphId<->Unicode mapping is typeface-specific,
1163 // map back to Unicode first.
1164 size_t glyphCount = len / 2;
1165 SkAutoTMalloc<SkUnichar> unichars(glyphCount);
1166 srcPaint.glyphsToUnichars(
1167 (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]);
1168 transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding);
1169 device->drawText(d, &unichars[0],
1170 glyphCount * sizeof(SkUnichar),
1171 x, y, transparent);
1172 break;
1173 }
1174 case SkPaint::kUTF8_TextEncoding:
1175 case SkPaint::kUTF16_TextEncoding:
1176 case SkPaint::kUTF32_TextEncoding:
1177 transparent.setTextEncoding(srcPaint.getTextEncoding());
1178 device->drawText(d, text, len, x, y, transparent);
1179 break;
1180 default:
1181 SkFAIL("unknown text encoding");
1182 }
1183}
1184
1185
halcanary682ee012016-01-28 10:59:34 -08001186void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
halcanarya6814332015-05-27 08:53:36 -07001187 SkScalar x, SkScalar y, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001188 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary6950de62015-11-07 05:29:00 -08001189 // https://bug.skia.org/3866
halcanary66a82f32015-10-12 13:05:04 -07001190 SkPath path;
1191 srcPaint.getTextPath(text, len, x, y, &path);
1192 this->drawPath(d, path, srcPaint, &SkMatrix::I(), true);
1193 // Draw text transparently to make it copyable/searchable/accessable.
1194 draw_transparent_text(this, d, text, len, x, y, srcPaint);
1195 return;
1196 }
halcanarya6814332015-05-27 08:53:36 -07001197 SkPaint paint = srcPaint;
1198 replace_srcmode_on_opaque_paint(&paint);
1199
halcanary96fcdcc2015-08-27 07:41:13 -07001200 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1201 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001202 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1203 // making text unreadable (e.g. same text twice when using CSS shadows).
1204 return;
1205 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001206 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001207 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001208 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001209 return;
1210 }
1211
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001212 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001213 const uint16_t* glyphIDs = nullptr;
reed@google.comaec40662014-04-18 19:29:07 +00001214 int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001215 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001216
robertphillipse34f17d2016-07-19 07:59:22 -07001217 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(textPaint.getTextEncoding(),
1218 textPaint.isDevKernText(),
1219 true);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001220 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001221 content.entry()->fContent.writeText("BT\n");
1222 set_text_transform(x, y, textPaint.getTextSkewX(),
1223 &content.entry()->fContent);
reed@google.comaec40662014-04-18 19:29:07 +00001224 int consumedGlyphCount = 0;
halcanary2f912f32014-10-16 09:53:20 -07001225
1226 SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs);
1227
halcanary3c35fb32016-06-30 11:55:07 -07001228 SkPDFGlyphSetMap* fontGlyphUsage = fDocument->getGlyphUsage();
1229
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001230 while (numGlyphs > consumedGlyphCount) {
robertphillips8e0c1502015-07-07 10:28:43 -07001231 this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001232 SkPDFFont* font = content.entry()->fState.fFont;
halcanary2f912f32014-10-16 09:53:20 -07001233
1234 int availableGlyphs = font->glyphsToPDFFontEncoding(
1235 glyphIDsCopy.begin() + consumedGlyphCount,
1236 numGlyphs - consumedGlyphCount);
halcanary3c35fb32016-06-30 11:55:07 -07001237 fontGlyphUsage->noteGlyphUsage(
halcanary2f912f32014-10-16 09:53:20 -07001238 font, glyphIDsCopy.begin() + consumedGlyphCount,
1239 availableGlyphs);
halcanaryee41b752016-06-23 14:08:11 -07001240 write_wide_string(&content.entry()->fContent,
1241 glyphIDsCopy.begin() + consumedGlyphCount,
1242 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001243 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001244 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001245 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001246 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001247}
1248
halcanary682ee012016-01-28 10:59:34 -08001249void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -07001250 const SkScalar pos[], int scalarsPerPos,
halcanary682ee012016-01-28 10:59:34 -08001251 const SkPoint& offset, const SkPaint& srcPaint) {
halcanary989da4a2016-03-21 14:33:17 -07001252 if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) {
halcanary66a82f32015-10-12 13:05:04 -07001253 const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos);
1254 SkAutoTMalloc<SkPoint> positionsBuffer;
1255 if (2 != scalarsPerPos) {
1256 int glyphCount = srcPaint.textToGlyphs(text, len, NULL);
1257 positionsBuffer.reset(glyphCount);
1258 for (int i = 0; i < glyphCount; ++i) {
1259 positionsBuffer[i].set(pos[i], 0.0f);
1260 }
1261 positions = &positionsBuffer[0];
1262 }
1263 SkPath path;
1264 srcPaint.getPosTextPath(text, len, positions, &path);
1265 SkMatrix matrix;
1266 matrix.setTranslate(offset);
1267 this->drawPath(d, path, srcPaint, &matrix, true);
1268 // Draw text transparently to make it copyable/searchable/accessable.
1269 draw_transparent_text(
1270 this, d, text, len, offset.x() + positions[0].x(),
1271 offset.y() + positions[0].y(), srcPaint);
1272 return;
1273 }
1274
halcanarya6814332015-05-27 08:53:36 -07001275 SkPaint paint = srcPaint;
1276 replace_srcmode_on_opaque_paint(&paint);
1277
halcanary96fcdcc2015-08-27 07:41:13 -07001278 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1279 if (paint.getMaskFilter() != nullptr) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001280 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1281 // making text unreadable (e.g. same text twice when using CSS shadows).
1282 return;
1283 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001284 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001285 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001286 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001287 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001288 return;
1289 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001290
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001291 SkGlyphStorage storage(0);
halcanary96fcdcc2015-08-27 07:41:13 -07001292 const uint16_t* glyphIDs = nullptr;
bungeman22edc832014-10-03 07:55:58 -07001293 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs);
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001294 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001295
robertphillipse34f17d2016-07-19 07:59:22 -07001296 SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(textPaint.getTextEncoding(),
1297 textPaint.isDevKernText(),
1298 true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001299 content.entry()->fContent.writeText("BT\n");
robertphillips8e0c1502015-07-07 10:28:43 -07001300 this->updateFont(textPaint, glyphIDs[0], content.entry());
halcanaryf0c30f52016-07-15 13:35:45 -07001301 GlyphPositioner glyphPositioner(&content.entry()->fContent,
1302 textPaint.getTextSkewX(),
1303 content.entry()->fState.fFont->multiByteGlyphs());
halcanary3c35fb32016-06-30 11:55:07 -07001304 SkPDFGlyphSetMap* fontGlyphUsage = fDocument->getGlyphUsage();
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001305 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001306 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001307 uint16_t encodedValue = glyphIDs[i];
1308 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
bungeman22edc832014-10-03 07:55:58 -07001309 // The current pdf font cannot encode the current glyph.
1310 // Try to get a pdf font which can encode the current glyph.
halcanaryf0c30f52016-07-15 13:35:45 -07001311 glyphPositioner.flush();
robertphillips8e0c1502015-07-07 10:28:43 -07001312 this->updateFont(textPaint, glyphIDs[i], content.entry());
bungeman22edc832014-10-03 07:55:58 -07001313 font = content.entry()->fState.fFont;
halcanaryf0c30f52016-07-15 13:35:45 -07001314 glyphPositioner.setWideChars(font->multiByteGlyphs());
bungeman22edc832014-10-03 07:55:58 -07001315 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
1316 SkDEBUGFAIL("PDF could not encode glyph.");
1317 continue;
1318 }
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001319 }
bungeman22edc832014-10-03 07:55:58 -07001320
halcanary3c35fb32016-06-30 11:55:07 -07001321 fontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
fmalita05c4a432014-09-29 06:29:53 -07001322 SkScalar x = offset.x() + pos[i * scalarsPerPos];
1323 SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0);
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001324 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
halcanaryf0c30f52016-07-15 13:35:45 -07001325
1326 SkScalar advanceWidth = textPaint.measureText(&encodedValue, sizeof(uint16_t));
1327 glyphPositioner.writeGlyph(x, y, advanceWidth, encodedValue);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001328 }
halcanaryf0c30f52016-07-15 13:35:45 -07001329 glyphPositioner.flush(); // Must flush before ending text object.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001330 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001331}
1332
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001333void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001334 int vertexCount, const SkPoint verts[],
1335 const SkPoint texs[], const SkColor colors[],
1336 SkXfermode* xmode, const uint16_t indices[],
1337 int indexCount, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001338 if (d.fRC->isEmpty()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001339 return;
1340 }
reed@google.com85e143c2013-12-30 15:51:25 +00001341 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001342}
1343
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001344void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1345 int x, int y, const SkPaint& paint) {
reedcf5c8462016-07-20 12:28:40 -07001346 SkASSERT(!paint.getImageFilter());
1347
reed7503d602016-07-15 14:23:29 -07001348 // Check if the source device is really a bitmapdevice (because that's what we returned
1349 // from createDevice (likely due to an imagefilter)
1350 SkPixmap pmap;
1351 if (device->peekPixels(&pmap)) {
1352 SkBitmap bitmap;
1353 bitmap.installPixels(pmap);
reedcf5c8462016-07-20 12:28:40 -07001354 this->drawSprite(d, bitmap, x, y, paint);
reed7503d602016-07-15 14:23:29 -07001355 return;
1356 }
1357
fmalita6987dca2014-11-13 08:33:37 -08001358 // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001359 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001360
1361 SkScalar scalarX = SkIntToScalar(x);
1362 SkScalar scalarY = SkIntToScalar(y);
halcanary91fcb3e2016-03-04 13:53:22 -08001363 for (const RectWithData& l : pdfDevice->fLinkToURLs) {
1364 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001365 fLinkToURLs.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001366 }
halcanary91fcb3e2016-03-04 13:53:22 -08001367 for (const RectWithData& l : pdfDevice->fLinkToDestinations) {
1368 SkRect r = l.rect.makeOffset(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001369 fLinkToDestinations.emplace_back(r, l.data.get());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001370 }
halcanary91fcb3e2016-03-04 13:53:22 -08001371 for (const NamedDestination& d : pdfDevice->fNamedDestinations) {
1372 SkPoint p = d.point + SkPoint::Make(scalarX, scalarY);
halcanaryd7b28852016-03-07 12:39:14 -08001373 fNamedDestinations.emplace_back(d.nameData.get(), p);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001374 }
1375
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001376 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001377 return;
1378 }
1379
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001380 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001381 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
reed1e7f5e72016-04-27 07:49:17 -07001382 ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001383 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001384 return;
1385 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001386 if (content.needShape()) {
1387 SkPath shape;
1388 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001389 SkIntToScalar(device->width()),
1390 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001391 content.setShape(shape);
1392 }
1393 if (!content.needSource()) {
1394 return;
1395 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001396
halcanary4b1e17e2016-07-27 14:49:46 -07001397 sk_sp<SkPDFObject> xObject = pdfDevice->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001398 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001399 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001400}
1401
reed89443ab2014-06-27 11:34:19 -07001402SkImageInfo SkPDFDevice::imageInfo() const {
robertphillips1f3923e2016-07-21 07:17:54 -07001403 SkImageInfo info = SkImageInfo::MakeUnknown(fPageSize.width(), fPageSize.height());
1404 return info;
reed89443ab2014-06-27 11:34:19 -07001405}
1406
reede8f30622016-03-23 18:59:25 -07001407sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
1408 return SkSurface::MakeRaster(info, &props);
reed89443ab2014-06-27 11:34:19 -07001409}
1410
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001411
halcanary8103a342016-03-08 15:10:16 -08001412sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const {
halcanary2b861552015-04-09 13:27:40 -07001413 SkTDArray<SkPDFObject*> fonts;
1414 fonts.setReserve(fFontResources.count());
1415 for (SkPDFFont* font : fFontResources) {
1416 fonts.push(font);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001417 }
halcanary8103a342016-03-08 15:10:16 -08001418 return SkPDFResourceDict::Make(
halcanary2b861552015-04-09 13:27:40 -07001419 &fGraphicStateResources,
1420 &fShaderResources,
1421 &fXObjectResources,
1422 &fonts);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001423}
1424
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001425const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1426 return fFontResources;
1427}
1428
halcanary8103a342016-03-08 15:10:16 -08001429sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const {
halcanaryece83922016-03-08 08:32:12 -08001430 auto mediaBox = sk_make_sp<SkPDFArray>();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001431 mediaBox->reserve(4);
halcanary130444f2015-04-25 06:45:07 -07001432 mediaBox->appendInt(0);
1433 mediaBox->appendInt(0);
halcanary8103a342016-03-08 15:10:16 -08001434 mediaBox->appendInt(fPageSize.width());
1435 mediaBox->appendInt(fPageSize.height());
1436 return mediaBox;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001437}
1438
mtklein5f939ab2016-03-16 10:28:35 -07001439std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const {
halcanary334fcbc2015-02-24 12:56:16 -08001440 SkDynamicMemoryWStream buffer;
1441 this->writeContent(&buffer);
mtklein5f939ab2016-03-16 10:28:35 -07001442 return std::unique_ptr<SkStreamAsset>(
halcanary8103a342016-03-08 15:10:16 -08001443 buffer.bytesWritten() > 0
1444 ? buffer.detachAsStream()
1445 : new SkMemoryStream);
reed@google.com5667afc2011-06-27 14:42:15 +00001446}
1447
halcanary334fcbc2015-02-24 12:56:16 -08001448void SkPDFDevice::writeContent(SkWStream* out) const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001449 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
halcanary334fcbc2015-02-24 12:56:16 -08001450 SkPDFUtils::AppendTransform(fInitialTransform, out);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001451 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001452
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001453 // If the content area is the entire page, then we don't need to clip
1454 // the content area (PDF area clips to the page size). Otherwise,
1455 // we have to clip to the content area; we've already applied the
1456 // initial transform, so just clip to the device size.
1457 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001458 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1459 SkIntToScalar(this->height()));
halcanary96fcdcc2015-08-27 07:41:13 -07001460 emit_clip(nullptr, &r, out);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001461 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001462
halcanary2be7e012016-03-28 11:58:08 -07001463 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, out);
1464 for (const auto& entry : fContentEntries) {
1465 SkPoint translation;
1466 translation.iset(this->getOrigin());
1467 translation.negate();
1468 gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion,
1469 translation);
1470 gsState.updateMatrix(entry.fState.fMatrix);
1471 gsState.updateDrawingState(entry.fState);
1472
1473 entry.fContent.writeToStream(out);
1474 }
1475 gsState.drainStack();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001476}
1477
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001478/* Draws an inverse filled path by using Path Ops to compute the positive
1479 * inverse using the current clip as the inverse bounds.
1480 * Return true if this was an inverse path and was properly handled,
1481 * otherwise returns false and the normal drawing routine should continue,
1482 * either as a (incorrect) fallback or because the path was not inverse
1483 * in the first place.
1484 */
1485bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001486 const SkPaint& paint, bool pathIsMutable,
1487 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001488 if (!origPath.isInverseFillType()) {
1489 return false;
1490 }
1491
reed1e7f5e72016-04-27 07:49:17 -07001492 if (d.fRC->isEmpty()) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001493 return false;
1494 }
1495
1496 SkPath modifiedPath;
1497 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1498 SkPaint noInversePaint(paint);
1499
1500 // Merge stroking operations into final path.
1501 if (SkPaint::kStroke_Style == paint.getStyle() ||
1502 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1503 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1504 if (doFillPath) {
1505 noInversePaint.setStyle(SkPaint::kFill_Style);
1506 noInversePaint.setStrokeWidth(0);
1507 pathPtr = &modifiedPath;
1508 } else {
1509 // To be consistent with the raster output, hairline strokes
1510 // are rendered as non-inverted.
1511 modifiedPath.toggleInverseFillType();
halcanary96fcdcc2015-08-27 07:41:13 -07001512 drawPath(d, modifiedPath, paint, nullptr, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001513 return true;
1514 }
1515 }
1516
1517 // Get bounds of clip in current transform space
1518 // (clip bounds are given in device space).
1519 SkRect bounds;
1520 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001521 SkMatrix totalMatrix = *d.fMatrix;
1522 if (prePathMatrix) {
1523 totalMatrix.preConcat(*prePathMatrix);
1524 }
1525 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001526 return false;
1527 }
reed1e7f5e72016-04-27 07:49:17 -07001528 bounds.set(d.fRC->getBounds());
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001529 transformInverse.mapRect(&bounds);
1530
1531 // Extend the bounds by the line width (plus some padding)
1532 // so the edge doesn't cause a visible stroke.
1533 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1534 paint.getStrokeWidth() + SK_Scalar1);
1535
1536 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1537 return false;
1538 }
1539
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001540 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001541 return true;
1542}
1543
reedf70b5312016-03-04 16:36:20 -08001544void SkPDFDevice::handlePointAnnotation(const SkPoint& point,
epoger@google.comb58772f2013-03-08 09:09:10 +00001545 const SkMatrix& matrix,
reedf70b5312016-03-04 16:36:20 -08001546 const char key[], SkData* value) {
1547 if (!value) {
1548 return;
epoger@google.comb58772f2013-03-08 09:09:10 +00001549 }
reedf70b5312016-03-04 16:36:20 -08001550
1551 if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) {
1552 SkPoint transformedPoint;
1553 matrix.mapXY(point.x(), point.y(), &transformedPoint);
1554 fNamedDestinations.emplace_back(value, transformedPoint);
1555 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001556}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001557
reedf70b5312016-03-04 16:36:20 -08001558void SkPDFDevice::handlePathAnnotation(const SkPath& path,
wangxianzhuef6c50a2015-09-17 20:38:02 -07001559 const SkDraw& d,
reedf70b5312016-03-04 16:36:20 -08001560 const char key[], SkData* value) {
1561 if (!value) {
1562 return;
1563 }
wangxianzhuef6c50a2015-09-17 20:38:02 -07001564
1565 SkPath transformedPath = path;
1566 transformedPath.transform(*d.fMatrix);
1567 SkRasterClip clip = *d.fRC;
senorblancoafc7cce2016-02-02 18:44:15 -08001568 clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op,
1569 false);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001570 SkRect transformedRect = SkRect::Make(clip.getBounds());
1571
reedf70b5312016-03-04 16:36:20 -08001572 if (!strcmp(SkAnnotationKeys::URL_Key(), key)) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001573 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001574 fLinkToURLs.emplace_back(transformedRect, value);
wangxianzhuef6c50a2015-09-17 20:38:02 -07001575 }
reedf70b5312016-03-04 16:36:20 -08001576 } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) {
reed16108352016-03-03 09:14:36 -08001577 if (!transformedRect.isEmpty()) {
reedf70b5312016-03-04 16:36:20 -08001578 fLinkToDestinations.emplace_back(transformedRect, value);
reed16108352016-03-03 09:14:36 -08001579 }
reed16108352016-03-03 09:14:36 -08001580 }
halcanary438de492015-04-28 06:21:01 -07001581}
1582
wangxianzhuef6c50a2015-09-17 20:38:02 -07001583void SkPDFDevice::appendAnnotations(SkPDFArray* array) const {
1584 array->reserve(fLinkToURLs.count() + fLinkToDestinations.count());
halcanary91fcb3e2016-03-04 13:53:22 -08001585 for (const RectWithData& rectWithURL : fLinkToURLs) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001586 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001587 fInitialTransform.mapRect(&r, rectWithURL.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001588 array->appendObject(create_link_to_url(rectWithURL.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001589 }
halcanary91fcb3e2016-03-04 13:53:22 -08001590 for (const RectWithData& linkToDestination : fLinkToDestinations) {
wangxianzhuef6c50a2015-09-17 20:38:02 -07001591 SkRect r;
halcanary91fcb3e2016-03-04 13:53:22 -08001592 fInitialTransform.mapRect(&r, linkToDestination.rect);
halcanaryd7b28852016-03-07 12:39:14 -08001593 array->appendObject(
1594 create_link_named_dest(linkToDestination.data.get(), r));
wangxianzhuef6c50a2015-09-17 20:38:02 -07001595 }
1596}
epoger@google.comb58772f2013-03-08 09:09:10 +00001597
halcanary6d622702015-03-25 08:45:42 -07001598void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const {
halcanary91fcb3e2016-03-04 13:53:22 -08001599 for (const NamedDestination& dest : fNamedDestinations) {
halcanaryece83922016-03-08 08:32:12 -08001600 auto pdfDest = sk_make_sp<SkPDFArray>();
epoger@google.comb58772f2013-03-08 09:09:10 +00001601 pdfDest->reserve(5);
halcanarye94ea622016-03-09 07:52:09 -08001602 pdfDest->appendObjRef(sk_ref_sp(page));
epoger@google.comb58772f2013-03-08 09:09:10 +00001603 pdfDest->appendName("XYZ");
halcanary91fcb3e2016-03-04 13:53:22 -08001604 SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y());
wangxianzhuef6c50a2015-09-17 20:38:02 -07001605 pdfDest->appendScalar(p.x());
1606 pdfDest->appendScalar(p.y());
epoger@google.comb58772f2013-03-08 09:09:10 +00001607 pdfDest->appendInt(0); // Leave zoom unchanged
halcanary91fcb3e2016-03-04 13:53:22 -08001608 SkString name(static_cast<const char*>(dest.nameData->data()));
halcanary8103a342016-03-08 15:10:16 -08001609 dict->insertObject(name, std::move(pdfDest));
epoger@google.comb58772f2013-03-08 09:09:10 +00001610 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001611}
1612
halcanary4b1e17e2016-07-27 14:49:46 -07001613sk_sp<SkPDFObject> SkPDFDevice::makeFormXObjectFromDevice() {
1614 sk_sp<SkPDFObject> xobject =
1615 SkPDFMakeFormXObject(this->content(), this->copyMediaBox(),
1616 this->makeResourceDict(), nullptr);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001617 // We always draw the form xobjects that we create back into the device, so
1618 // we simply preserve the font usage instead of pulling it out and merging
1619 // it back in later.
halcanary4b1e17e2016-07-27 14:49:46 -07001620 this->cleanUp(); // Reset this device to have no content.
1621 this->init();
reed@google.comfc641d02012-09-20 17:52:20 +00001622 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001623}
1624
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001625void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
halcanary4b1e17e2016-07-27 14:49:46 -07001626 SkPDFObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001627 const SkClipStack* clipStack,
1628 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001629 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001630 bool invertClip) {
1631 if (clipRegion.isEmpty() && !invertClip) {
1632 return;
1633 }
1634
halcanary4b1e17e2016-07-27 14:49:46 -07001635 sk_sp<SkPDFDict> sMaskGS = SkPDFGraphicState::GetSMaskGraphicState(
halcanary989da4a2016-03-21 14:33:17 -07001636 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon());
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001637
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001638 SkMatrix identity;
1639 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001640 SkPaint paint;
1641 paint.setXfermodeMode(mode);
1642 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001643 if (!content.entry()) {
1644 return;
1645 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001646 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001647 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001648 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001649
halcanary1437c1e2016-03-13 18:30:24 -07001650 // Call makeNoSmaskGraphicState() instead of
1651 // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon
1652 // can deduplicate.
halcanary989da4a2016-03-21 14:33:17 -07001653 sMaskGS = fDocument->canon()->makeNoSmaskGraphicState();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001654 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001655 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001656}
1657
halcanary2be7e012016-03-28 11:58:08 -07001658SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001659 const SkRegion& clipRegion,
1660 const SkMatrix& matrix,
1661 const SkPaint& paint,
1662 bool hasText,
halcanary4b1e17e2016-07-27 14:49:46 -07001663 SkPDFObject** dst) {
halcanary96fcdcc2015-08-27 07:41:13 -07001664 *dst = nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001665 if (clipRegion.isEmpty()) {
halcanary96fcdcc2015-08-27 07:41:13 -07001666 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001667 }
1668
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001669 // The clip stack can come from an SkDraw where it is technically optional.
1670 SkClipStack synthesizedClipStack;
halcanary96fcdcc2015-08-27 07:41:13 -07001671 if (clipStack == nullptr) {
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001672 if (clipRegion == fExistingClipRegion) {
1673 clipStack = &fExistingClipStack;
1674 } else {
1675 // GraphicStackState::updateClip expects the clip stack to have
1676 // fExistingClip as a prefix, so start there, then set the clip
1677 // to the passed region.
1678 synthesizedClipStack = fExistingClipStack;
1679 SkPath clipPath;
1680 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001681 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1682 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001683 clipStack = &synthesizedClipStack;
1684 }
1685 }
1686
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001687 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1688 if (paint.getXfermode()) {
1689 paint.getXfermode()->asMode(&xfermode);
1690 }
1691
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001692 // For the following modes, we want to handle source and destination
1693 // separately, so make an object of what's already there.
1694 if (xfermode == SkXfermode::kClear_Mode ||
1695 xfermode == SkXfermode::kSrc_Mode ||
1696 xfermode == SkXfermode::kSrcIn_Mode ||
1697 xfermode == SkXfermode::kDstIn_Mode ||
1698 xfermode == SkXfermode::kSrcOut_Mode ||
1699 xfermode == SkXfermode::kDstOut_Mode ||
1700 xfermode == SkXfermode::kSrcATop_Mode ||
1701 xfermode == SkXfermode::kDstATop_Mode ||
1702 xfermode == SkXfermode::kModulate_Mode) {
1703 if (!isContentEmpty()) {
halcanary4b1e17e2016-07-27 14:49:46 -07001704 // TODO(halcanary): make this safer.
1705 *dst = this->makeFormXObjectFromDevice().release();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001706 SkASSERT(isContentEmpty());
1707 } else if (xfermode != SkXfermode::kSrc_Mode &&
1708 xfermode != SkXfermode::kSrcOut_Mode) {
1709 // Except for Src and SrcOut, if there isn't anything already there,
1710 // then we're done.
halcanary96fcdcc2015-08-27 07:41:13 -07001711 return nullptr;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001712 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001713 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001714 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001715 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001716
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001717 // Dst xfer mode doesn't draw source at all.
1718 if (xfermode == SkXfermode::kDst_Mode) {
halcanary96fcdcc2015-08-27 07:41:13 -07001719 return nullptr;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001720 }
1721
halcanary2be7e012016-03-28 11:58:08 -07001722 SkPDFDevice::ContentEntry* entry;
1723 if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) {
1724 entry = fContentEntries.back();
1725 } else if (xfermode != SkXfermode::kDstOver_Mode) {
1726 entry = fContentEntries.emplace_back();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001727 } else {
halcanary2be7e012016-03-28 11:58:08 -07001728 entry = fContentEntries.emplace_front();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001729 }
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001730 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001731 hasText, &entry->fState);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001732 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001733}
1734
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001735void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
halcanary4b1e17e2016-07-27 14:49:46 -07001736 SkPDFObject* dst,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001737 SkPath* shape) {
1738 if (xfermode != SkXfermode::kClear_Mode &&
1739 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001740 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001741 xfermode != SkXfermode::kSrcIn_Mode &&
1742 xfermode != SkXfermode::kDstIn_Mode &&
1743 xfermode != SkXfermode::kSrcOut_Mode &&
1744 xfermode != SkXfermode::kDstOut_Mode &&
1745 xfermode != SkXfermode::kSrcATop_Mode &&
1746 xfermode != SkXfermode::kDstATop_Mode &&
1747 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001748 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001749 return;
1750 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001751 if (xfermode == SkXfermode::kDstOver_Mode) {
1752 SkASSERT(!dst);
halcanary2be7e012016-03-28 11:58:08 -07001753 if (fContentEntries.front()->fContent.getOffset() == 0) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001754 // For DstOver, an empty content entry was inserted before the rest
1755 // of the content entries. If nothing was drawn, it needs to be
1756 // removed.
halcanary2be7e012016-03-28 11:58:08 -07001757 fContentEntries.pop_front();
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001758 }
1759 return;
1760 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001761 if (!dst) {
1762 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1763 xfermode == SkXfermode::kSrcOut_Mode);
1764 return;
1765 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001766
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001767 SkASSERT(dst);
halcanary2be7e012016-03-28 11:58:08 -07001768 SkASSERT(fContentEntries.count() == 1);
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001769 // Changing the current content into a form-xobject will destroy the clip
1770 // objects which is fine since the xobject will already be clipped. However
1771 // if source has shape, we need to clip it too, so a copy of the clip is
1772 // saved.
halcanary2be7e012016-03-28 11:58:08 -07001773
1774 SkClipStack clipStack = fContentEntries.front()->fState.fClipStack;
1775 SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001776
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001777 SkMatrix identity;
1778 identity.reset();
1779 SkPaint stockPaint;
1780
halcanary4b1e17e2016-07-27 14:49:46 -07001781 sk_sp<SkPDFObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001782 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001783 // If nothing was drawn and there's no shape, then the draw was a
1784 // no-op, but dst needs to be restored for that to be true.
1785 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1786 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1787 // reduces to Dst.
halcanary96fcdcc2015-08-27 07:41:13 -07001788 if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode ||
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001789 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001790 ScopedContentEntry content(this, &fExistingClipStack,
1791 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001792 stockPaint);
1793 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1794 &content.entry()->fContent);
1795 return;
1796 } else {
1797 xfermode = SkXfermode::kClear_Mode;
1798 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001799 } else {
halcanary2be7e012016-03-28 11:58:08 -07001800 SkASSERT(fContentEntries.count() == 1);
halcanary4b1e17e2016-07-27 14:49:46 -07001801 srcFormXObject = this->makeFormXObjectFromDevice();
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001802 }
1803
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001804 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1805 // without alpha.
1806 if (xfermode == SkXfermode::kSrcATop_Mode) {
1807 // TODO(vandebo): In order to properly support SrcATop we have to track
1808 // the shape of what's been drawn at all times. It's the intersection of
1809 // the non-transparent parts of the device and the outlines (shape) of
1810 // all images and devices drawn.
1811 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001812 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001813 SkXfermode::kSrcOver_Mode, true);
1814 } else {
halcanary4b1e17e2016-07-27 14:49:46 -07001815 sk_sp<SkPDFObject> dstMaskStorage;
1816 SkPDFObject* dstMask = srcFormXObject.get();
halcanary96fcdcc2015-08-27 07:41:13 -07001817 if (shape != nullptr) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001818 // Draw shape into a form-xobject.
reed1e7f5e72016-04-27 07:49:17 -07001819 SkRasterClip rc(clipRegion);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001820 SkDraw d;
1821 d.fMatrix = &identity;
reed1e7f5e72016-04-27 07:49:17 -07001822 d.fRC = &rc;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001823 d.fClipStack = &clipStack;
1824 SkPaint filledPaint;
1825 filledPaint.setColor(SK_ColorBLACK);
1826 filledPaint.setStyle(SkPaint::kFill_Style);
halcanary96fcdcc2015-08-27 07:41:13 -07001827 this->drawPath(d, *shape, filledPaint, nullptr, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001828
halcanary4b1e17e2016-07-27 14:49:46 -07001829 dstMaskStorage = this->makeFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001830 dstMask = dstMaskStorage.get();
1831 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001832 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1833 &fExistingClipStack, fExistingClipRegion,
1834 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001835 }
1836
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001837 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001838 return;
1839 } else if (xfermode == SkXfermode::kSrc_Mode ||
1840 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001841 ScopedContentEntry content(this, &fExistingClipStack,
1842 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001843 if (content.entry()) {
1844 SkPDFUtils::DrawFormXObject(
1845 this->addXObjectResource(srcFormXObject.get()),
1846 &content.entry()->fContent);
1847 }
1848 if (xfermode == SkXfermode::kSrc_Mode) {
1849 return;
1850 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001851 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001852 ScopedContentEntry content(this, &fExistingClipStack,
1853 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001854 if (content.entry()) {
1855 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1856 &content.entry()->fContent);
1857 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001858 }
1859
1860 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1861 xfermode == SkXfermode::kDstIn_Mode ||
1862 xfermode == SkXfermode::kSrcOut_Mode ||
1863 xfermode == SkXfermode::kDstOut_Mode ||
1864 xfermode == SkXfermode::kSrcATop_Mode ||
1865 xfermode == SkXfermode::kDstATop_Mode ||
1866 xfermode == SkXfermode::kModulate_Mode);
1867
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001868 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001869 xfermode == SkXfermode::kSrcOut_Mode ||
1870 xfermode == SkXfermode::kSrcATop_Mode) {
1871 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001872 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001873 SkXfermode::kSrcOver_Mode,
1874 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001875 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001876 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
1877 if (xfermode == SkXfermode::kModulate_Mode) {
1878 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001879 dst, &fExistingClipStack,
1880 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001881 SkXfermode::kSrcOver_Mode, false);
1882 mode = SkXfermode::kMultiply_Mode;
1883 }
1884 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001885 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001886 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001887 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001888}
1889
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001890bool SkPDFDevice::isContentEmpty() {
halcanary2be7e012016-03-28 11:58:08 -07001891 if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) {
1892 SkASSERT(fContentEntries.count() <= 1);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001893 return true;
1894 }
1895 return false;
1896}
1897
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001898void SkPDFDevice::populateGraphicStateEntryFromPaint(
1899 const SkMatrix& matrix,
1900 const SkClipStack& clipStack,
1901 const SkRegion& clipRegion,
1902 const SkPaint& paint,
1903 bool hasText,
halcanary2be7e012016-03-28 11:58:08 -07001904 SkPDFDevice::GraphicStateEntry* entry) {
halcanary96fcdcc2015-08-27 07:41:13 -07001905 NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false);
1906 NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false);
1907 NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001908
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001909 entry->fMatrix = matrix;
1910 entry->fClipStack = clipStack;
1911 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001912 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1913 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001914
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001915 // PDF treats a shader as a color, so we only set one or the other.
halcanary48810a02016-03-07 14:57:50 -08001916 sk_sp<SkPDFObject> pdfShader;
reedfe630452016-03-25 09:08:00 -07001917 SkShader* shader = paint.getShader();
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001918 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001919 if (shader) {
1920 // PDF positions patterns relative to the initial transform, so
1921 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001922 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001923 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001924
1925 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001926 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001927 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001928
1929 // We need to apply the initial transform to bounds in order to get
1930 // bounds in a consistent coordinate system.
1931 SkRect boundsTemp;
1932 boundsTemp.set(bounds);
1933 fInitialTransform.mapRect(&boundsTemp);
1934 boundsTemp.roundOut(&bounds);
1935
halcanary792c80f2015-02-20 07:21:05 -08001936 SkScalar rasterScale =
1937 SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE;
1938 pdfShader.reset(SkPDFShader::GetPDFShader(
reedfe630452016-03-25 09:08:00 -07001939 fDocument, fRasterDpi, shader, transform, bounds, rasterScale));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001940
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001941 if (pdfShader.get()) {
1942 // pdfShader has been canonicalized so we can directly compare
1943 // pointers.
1944 int resourceIndex = fShaderResources.find(pdfShader.get());
1945 if (resourceIndex < 0) {
1946 resourceIndex = fShaderResources.count();
1947 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001948 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001949 }
1950 entry->fShaderIndex = resourceIndex;
1951 } else {
1952 // A color shader is treated as an invalid shader so we don't have
1953 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001954 SkShader::GradientInfo gradientInfo;
1955 SkColor gradientColor;
1956 gradientInfo.fColors = &gradientColor;
halcanary96fcdcc2015-08-27 07:41:13 -07001957 gradientInfo.fColorOffsets = nullptr;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001958 gradientInfo.fColorCount = 1;
1959 if (shader->asAGradient(&gradientInfo) ==
1960 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001961 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1962 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001963 }
1964 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001965 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001966
halcanary48810a02016-03-07 14:57:50 -08001967 sk_sp<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001968 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001969 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001970 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001971 } else {
1972 SkPaint newPaint = paint;
1973 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001974 newGraphicState.reset(
halcanary989da4a2016-03-21 14:33:17 -07001975 SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001976 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001977 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001978 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001979
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001980 if (hasText) {
1981 entry->fTextScaleX = paint.getTextScaleX();
1982 entry->fTextFill = paint.getStyle();
1983 } else {
1984 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001985 }
1986}
1987
halcanarybe27a112015-04-01 13:31:19 -07001988int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001989 // Assumes that gs has been canonicalized (so we can directly compare
1990 // pointers).
1991 int result = fGraphicStateResources.find(gs);
1992 if (result < 0) {
1993 result = fGraphicStateResources.count();
1994 fGraphicStateResources.push(gs);
1995 gs->ref();
1996 }
1997 return result;
1998}
1999
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002000int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
2001 // Assumes that xobject has been canonicalized (so we can directly compare
2002 // pointers).
2003 int result = fXObjectResources.find(xObject);
2004 if (result < 0) {
2005 result = fXObjectResources.count();
2006 fXObjectResources.push(xObject);
2007 xObject->ref();
2008 }
2009 return result;
2010}
2011
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002012void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
halcanary2be7e012016-03-28 11:58:08 -07002013 SkPDFDevice::ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002014 SkTypeface* typeface = paint.getTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -07002015 if (contentEntry->fState.fFont == nullptr ||
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002016 contentEntry->fState.fTextSize != paint.getTextSize() ||
2017 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002018 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00002019 contentEntry->fContent.writeText("/");
2020 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
2021 SkPDFResourceDict::kFont_ResourceType,
2022 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002023 contentEntry->fContent.writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -07002024 SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002025 contentEntry->fContent.writeText(" Tf\n");
2026 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002027 }
2028}
2029
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002030int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
halcanary48810a02016-03-07 14:57:50 -08002031 sk_sp<SkPDFFont> newFont(
halcanary989da4a2016-03-21 14:33:17 -07002032 SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002033 int resourceIndex = fFontResources.find(newFont.get());
2034 if (resourceIndex < 0) {
2035 resourceIndex = fFontResources.count();
2036 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002037 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002038 }
2039 return resourceIndex;
2040}
2041
halcanary7a14b312015-10-01 07:28:13 -07002042static SkSize rect_to_size(const SkRect& r) {
2043 return SkSize::Make(r.width(), r.height());
2044}
2045
halcanarya50151d2016-03-25 11:57:49 -07002046static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap,
2047 SkColorFilter* colorFilter) {
halcanary9d524f22016-03-29 09:03:52 -07002048 auto surface =
halcanarya50151d2016-03-25 11:57:49 -07002049 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions()));
2050 SkASSERT(surface);
halcanary7a14b312015-10-01 07:28:13 -07002051 SkCanvas* canvas = surface->getCanvas();
2052 canvas->clear(SK_ColorTRANSPARENT);
2053 SkPaint paint;
reedd053ce92016-03-22 10:17:23 -07002054 paint.setColorFilter(sk_ref_sp(colorFilter));
halcanarya50151d2016-03-25 11:57:49 -07002055 imageBitmap.draw(canvas, &paint);
halcanary7a14b312015-10-01 07:28:13 -07002056 canvas->flush();
halcanarya50151d2016-03-25 11:57:49 -07002057 return surface->makeImageSnapshot();
halcanary7a14b312015-10-01 07:28:13 -07002058}
2059
2060////////////////////////////////////////////////////////////////////////////////
2061void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix,
2062 const SkClipStack* clipStack,
2063 const SkRegion& origClipRegion,
halcanarya50151d2016-03-25 11:57:49 -07002064 SkImageBitmap imageBitmap,
halcanary7a14b312015-10-01 07:28:13 -07002065 const SkPaint& paint) {
halcanarya50151d2016-03-25 11:57:49 -07002066 if (imageBitmap.dimensions().isZero()) {
2067 return;
2068 }
halcanary7a14b312015-10-01 07:28:13 -07002069 #ifdef SK_PDF_IMAGE_STATS
2070 gDrawImageCalls.fetch_add(1);
2071 #endif
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002072 SkMatrix matrix = origMatrix;
2073 SkRegion perspectiveBounds;
2074 const SkRegion* clipRegion = &origClipRegion;
halcanarya50151d2016-03-25 11:57:49 -07002075 sk_sp<SkImage> autoImageUnref;
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002076
2077 // Rasterize the bitmap using perspective in a new bitmap.
2078 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002079 if (fRasterDpi == 0) {
2080 return;
2081 }
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002082 // Transform the bitmap in the new space, without taking into
2083 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002084 SkPath perspectiveOutline;
halcanarya50151d2016-03-25 11:57:49 -07002085 SkRect imageBounds = SkRect::Make(imageBitmap.bounds());
halcanary7a14b312015-10-01 07:28:13 -07002086 perspectiveOutline.addRect(imageBounds);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002087 perspectiveOutline.transform(origMatrix);
2088
2089 // TODO(edisonn): perf - use current clip too.
2090 // Retrieve the bounds of the new shape.
2091 SkRect bounds = perspectiveOutline.getBounds();
2092
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002093 // Transform the bitmap in the new space, taking into
2094 // account the initial transform.
2095 SkMatrix total = origMatrix;
2096 total.postConcat(fInitialTransform);
halcanary7a14b312015-10-01 07:28:13 -07002097 SkScalar dpiScale = SkIntToScalar(fRasterDpi) /
2098 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2099 total.postScale(dpiScale, dpiScale);
2100
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002101 SkPath physicalPerspectiveOutline;
halcanary7a14b312015-10-01 07:28:13 -07002102 physicalPerspectiveOutline.addRect(imageBounds);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002103 physicalPerspectiveOutline.transform(total);
2104
halcanary7a14b312015-10-01 07:28:13 -07002105 SkRect physicalPerspectiveBounds =
2106 physicalPerspectiveOutline.getBounds();
2107 SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width();
2108 SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002109
2110 // TODO(edisonn): A better approach would be to use a bitmap shader
2111 // (in clamp mode) and draw a rect over the entire bounding box. Then
2112 // intersect perspectiveOutline to the clip. That will avoid introducing
2113 // alpha to the image while still giving good behavior at the edge of
2114 // the image. Avoiding alpha will reduce the pdf size and generation
2115 // CPU time some.
2116
halcanary7a14b312015-10-01 07:28:13 -07002117 SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil();
2118
reede8f30622016-03-23 18:59:25 -07002119 auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)));
halcanary7a14b312015-10-01 07:28:13 -07002120 if (!surface) {
reed@google.com9ebcac52014-01-24 18:53:42 +00002121 return;
2122 }
halcanary7a14b312015-10-01 07:28:13 -07002123 SkCanvas* canvas = surface->getCanvas();
2124 canvas->clear(SK_ColorTRANSPARENT);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002125
2126 SkScalar deltaX = bounds.left();
2127 SkScalar deltaY = bounds.top();
2128
2129 SkMatrix offsetMatrix = origMatrix;
2130 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002131 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002132
2133 // Translate the draw in the new canvas, so we perfectly fit the
2134 // shape in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002135 canvas->setMatrix(offsetMatrix);
halcanarya50151d2016-03-25 11:57:49 -07002136 imageBitmap.draw(canvas, nullptr);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002137 // Make sure the final bits are in the bitmap.
halcanary7a14b312015-10-01 07:28:13 -07002138 canvas->flush();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002139
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002140 // In the new space, we use the identity matrix translated
2141 // and scaled to reflect DPI.
2142 matrix.setScale(1 / scaleX, 1 / scaleY);
2143 matrix.postTranslate(deltaX, deltaY);
2144
halcanary7a14b312015-10-01 07:28:13 -07002145 perspectiveBounds.setRect(bounds.roundOut());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002146 clipRegion = &perspectiveBounds;
halcanary7a14b312015-10-01 07:28:13 -07002147
halcanarya50151d2016-03-25 11:57:49 -07002148 autoImageUnref = surface->makeImageSnapshot();
2149 imageBitmap = SkImageBitmap(autoImageUnref.get());
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002150 }
2151
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002152 SkMatrix scaled;
2153 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002154 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2155 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002156 // Scale the image up from 1x1 to WxH.
halcanarya50151d2016-03-25 11:57:49 -07002157 SkIRect subset = imageBitmap.bounds();
2158 scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()),
2159 SkIntToScalar(imageBitmap.dimensions().height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002160 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002161 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
halcanarya50151d2016-03-25 11:57:49 -07002162 if (!content.entry()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002163 return;
2164 }
2165 if (content.needShape()) {
2166 SkPath shape;
halcanary7a14b312015-10-01 07:28:13 -07002167 shape.addRect(SkRect::Make(subset));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002168 shape.transform(matrix);
2169 content.setShape(shape);
2170 }
2171 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002172 return;
2173 }
2174
halcanary287d22d2015-09-24 10:20:05 -07002175 if (SkColorFilter* colorFilter = paint.getColorFilter()) {
halcanary6950de62015-11-07 05:29:00 -08002176 // TODO(https://bug.skia.org/4378): implement colorfilter on other
halcanary7a14b312015-10-01 07:28:13 -07002177 // draw calls. This code here works for all
2178 // drawBitmap*()/drawImage*() calls amd ImageFilters (which
2179 // rasterize a layer on this backend). Fortuanely, this seems
2180 // to be how Chromium impements most color-filters.
halcanarya50151d2016-03-25 11:57:49 -07002181 autoImageUnref = color_filter(imageBitmap, colorFilter);
2182 imageBitmap = SkImageBitmap(autoImageUnref.get());
halcanary7a14b312015-10-01 07:28:13 -07002183 // TODO(halcanary): de-dupe this by caching filtered images.
2184 // (maybe in the resource cache?)
2185 }
halcanarya50151d2016-03-25 11:57:49 -07002186
2187 SkBitmapKey key = imageBitmap.getKey();
2188 sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key);
halcanary7a14b312015-10-01 07:28:13 -07002189 if (!pdfimage) {
halcanary4b1e17e2016-07-27 14:49:46 -07002190 sk_sp<SkImage> img = imageBitmap.makeImage();
halcanarya50151d2016-03-25 11:57:49 -07002191 if (!img) {
2192 return;
2193 }
2194 pdfimage = SkPDFCreateBitmapObject(
2195 std::move(img), fDocument->canon()->getPixelSerializer());
halcanary7a14b312015-10-01 07:28:13 -07002196 if (!pdfimage) {
2197 return;
halcanary287d22d2015-09-24 10:20:05 -07002198 }
halcanarya50151d2016-03-25 11:57:49 -07002199 fDocument->serialize(pdfimage); // serialize images early.
2200 fDocument->canon()->addPDFBitmap(key, pdfimage);
halcanary287d22d2015-09-24 10:20:05 -07002201 }
halcanarya50151d2016-03-25 11:57:49 -07002202 // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject>
halcanary3d8c33c2015-10-01 11:06:22 -07002203 SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002204 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002205}
reede51c3562016-07-19 14:33:20 -07002206
2207///////////////////////////////////////////////////////////////////////////////////////////////////
2208
2209#include "SkSpecialImage.h"
2210#include "SkImageFilter.h"
2211
2212void SkPDFDevice::drawSpecial(const SkDraw& draw, SkSpecialImage* srcImg, int x, int y,
2213 const SkPaint& paint) {
2214 SkASSERT(!srcImg->isTextureBacked());
2215
2216 SkBitmap resultBM;
2217
2218 SkImageFilter* filter = paint.getImageFilter();
2219 if (filter) {
2220 SkIPoint offset = SkIPoint::Make(0, 0);
2221 SkMatrix matrix = *draw.fMatrix;
2222 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
2223 const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-x, -y);
2224// SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache());
2225 SkImageFilter::Context ctx(matrix, clipBounds, nullptr /*cache.get()*/);
2226
2227 sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset));
2228 if (resultImg) {
2229 SkPaint tmpUnfiltered(paint);
2230 tmpUnfiltered.setImageFilter(nullptr);
2231 if (resultImg->getROPixels(&resultBM)) {
2232 this->drawSprite(draw, resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered);
2233 }
2234 }
2235 } else {
2236 if (srcImg->getROPixels(&resultBM)) {
2237 this->drawSprite(draw, resultBM, x, y, paint);
2238 }
2239 }
2240}
2241
2242sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) {
2243 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
2244}
2245
2246sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) {
2247 return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()),
2248 image->makeNonTextureImage());
2249}
2250
2251sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() {
reede51c3562016-07-19 14:33:20 -07002252 return nullptr;
2253}