blob: a7ef08d21d56c22c8f20490896b1f6b9ac35b3de [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00006 */
7
8#include "SkPDFDevice.h"
9
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000010#include "SkAnnotation.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000011#include "SkColor.h"
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000012#include "SkClipStack.h"
reed@google.com8a85d0c2011-06-24 19:12:12 +000013#include "SkData.h"
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +000014#include "SkDraw.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000015#include "SkFontHost.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000016#include "SkGlyphCache.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000017#include "SkPaint.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000018#include "SkPath.h"
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +000019#include "SkPathOps.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000020#include "SkPDFFont.h"
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +000021#include "SkPDFFormXObject.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000022#include "SkPDFGraphicState.h"
23#include "SkPDFImage.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000024#include "SkPDFResourceDict.h"
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000025#include "SkPDFShader.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000026#include "SkPDFStream.h"
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000027#include "SkPDFTypes.h"
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +000028#include "SkPDFUtils.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000029#include "SkRect.h"
scroggo@google.coma8e33a92013-11-08 18:02:53 +000030#include "SkRRect.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000031#include "SkString.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000032#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000033#include "SkTemplates.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000034#include "SkTypefacePriv.h"
edisonn@google.com6addb192013-04-02 15:33:08 +000035#include "SkTSet.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000036
djsollen@google.com5df5e612013-10-03 14:42:24 +000037#ifdef SK_BUILD_FOR_ANDROID
38#include "SkTypeface_android.h"
39
40struct TypefaceFallbackData {
41 SkTypeface* typeface;
42 int lowerBounds;
43 int upperBounds;
44
45 bool operator==(const TypefaceFallbackData& b) const {
46 return typeface == b.typeface &&
47 lowerBounds == b.lowerBounds &&
48 upperBounds == b.upperBounds;
49 }
50};
51#endif
52
edisonn@google.com73a7ea32013-11-11 20:55:15 +000053#define DPI_FOR_RASTER_SCALE_ONE 72
54
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000055// Utility functions
56
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000057static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000058 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
59 SkScalar colorMax = SkIntToScalar(0xFF);
vandebo@chromium.org094316b2011-03-04 03:15:13 +000060 SkPDFScalar::Append(
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000061 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result);
62 result->writeText(" ");
vandebo@chromium.org094316b2011-03-04 03:15:13 +000063 SkPDFScalar::Append(
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000064 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result);
65 result->writeText(" ");
vandebo@chromium.org094316b2011-03-04 03:15:13 +000066 SkPDFScalar::Append(
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000067 SkScalarDiv(SkIntToScalar(SkColorGetB(color)), colorMax), result);
68 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000069}
70
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000071static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000072 SkPaint result = paint;
73 if (result.isFakeBoldText()) {
74 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
75 kStdFakeBoldInterpKeys,
76 kStdFakeBoldInterpValues,
77 kStdFakeBoldInterpLength);
78 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000079 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000080 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000081 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000082 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000083 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000084 result.setStrokeWidth(width);
85 }
86 return result;
87}
88
89// Stolen from measure_text in SkDraw.cpp and then tweaked.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000090static void align_text(SkDrawCacheProc glyphCacheProc, const SkPaint& paint,
bungeman@google.com9a87cee2011-08-23 17:02:18 +000091 const uint16_t* glyphs, size_t len,
92 SkScalar* x, SkScalar* y) {
93 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000094 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000095 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000096
97 SkMatrix ident;
98 ident.reset();
bungeman@google.com532470f2013-01-22 19:25:14 +000099 SkAutoGlyphCache autoCache(paint, NULL, &ident);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000100 SkGlyphCache* cache = autoCache.getCache();
101
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000102 const char* start = reinterpret_cast<const char*>(glyphs);
103 const char* stop = reinterpret_cast<const char*>(glyphs + len);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000104 SkFixed xAdv = 0, yAdv = 0;
105
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000106 // TODO(vandebo): This probably needs to take kerning into account.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000107 while (start < stop) {
108 const SkGlyph& glyph = glyphCacheProc(cache, &start, 0, 0);
109 xAdv += glyph.fAdvanceX;
110 yAdv += glyph.fAdvanceY;
111 };
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000112 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000113 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000114 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000115
116 SkScalar xAdj = SkFixedToScalar(xAdv);
117 SkScalar yAdj = SkFixedToScalar(yAdv);
118 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
119 xAdj = SkScalarHalf(xAdj);
120 yAdj = SkScalarHalf(yAdj);
121 }
122 *x = *x - xAdj;
123 *y = *y - yAdj;
124}
125
robertphillips@google.coma4662862013-11-21 14:24:16 +0000126static int max_glyphid_for_typeface(SkTypeface* typeface) {
reed@google.comfed86bd2013-03-14 15:04:57 +0000127 SkAutoResolveDefaultTypeface autoResolve(typeface);
128 typeface = autoResolve.get();
commit-bot@chromium.org6a4ba5b2013-07-17 21:55:08 +0000129 return typeface->countGlyphs() - 1;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000130}
131
132typedef SkAutoSTMalloc<128, uint16_t> SkGlyphStorage;
133
134static size_t force_glyph_encoding(const SkPaint& paint, const void* text,
135 size_t len, SkGlyphStorage* storage,
136 uint16_t** glyphIDs) {
137 // Make sure we have a glyph id encoding.
138 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
139 size_t numGlyphs = paint.textToGlyphs(text, len, NULL);
140 storage->reset(numGlyphs);
141 paint.textToGlyphs(text, len, storage->get());
142 *glyphIDs = storage->get();
143 return numGlyphs;
144 }
145
146 // For user supplied glyph ids we need to validate them.
147 SkASSERT((len & 1) == 0);
148 size_t numGlyphs = len / 2;
149 const uint16_t* input =
150 reinterpret_cast<uint16_t*>(const_cast<void*>((text)));
151
152 int maxGlyphID = max_glyphid_for_typeface(paint.getTypeface());
153 size_t validated;
154 for (validated = 0; validated < numGlyphs; ++validated) {
155 if (input[validated] > maxGlyphID) {
156 break;
157 }
158 }
159 if (validated >= numGlyphs) {
160 *glyphIDs = reinterpret_cast<uint16_t*>(const_cast<void*>((text)));
161 return numGlyphs;
162 }
163
164 // Silently drop anything out of range.
165 storage->reset(numGlyphs);
166 if (validated > 0) {
167 memcpy(storage->get(), input, validated * sizeof(uint16_t));
168 }
169
170 for (size_t i = validated; i < numGlyphs; ++i) {
171 storage->get()[i] = input[i];
172 if (input[i] > maxGlyphID) {
173 storage->get()[i] = 0;
174 }
175 }
176 *glyphIDs = storage->get();
177 return numGlyphs;
178}
179
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000180static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX,
181 SkWStream* content) {
182 // Flip the text about the x-axis to account for origin swap and include
183 // the passed parameters.
184 content->writeText("1 0 ");
185 SkPDFScalar::Append(0 - textSkewX, content);
186 content->writeText(" -1 ");
187 SkPDFScalar::Append(x, content);
188 content->writeText(" ");
189 SkPDFScalar::Append(y, content);
190 content->writeText(" Tm\n");
191}
192
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000193// It is important to not confuse GraphicStateEntry with SkPDFGraphicState, the
194// later being our representation of an object in the PDF file.
195struct GraphicStateEntry {
196 GraphicStateEntry();
197
198 // Compare the fields we care about when setting up a new content entry.
199 bool compareInitialState(const GraphicStateEntry& b);
200
201 SkMatrix fMatrix;
202 // We can't do set operations on Paths, though PDF natively supports
203 // intersect. If the clip stack does anything other than intersect,
204 // we have to fall back to the region. Treat fClipStack as authoritative.
205 // See http://code.google.com/p/skia/issues/detail?id=221
206 SkClipStack fClipStack;
207 SkRegion fClipRegion;
208
209 // When emitting the content entry, we will ensure the graphic state
210 // is set to these values first.
211 SkColor fColor;
212 SkScalar fTextScaleX; // Zero means we don't care what the value is.
213 SkPaint::Style fTextFill; // Only if TextScaleX is non-zero.
214 int fShaderIndex;
215 int fGraphicStateIndex;
216
217 // We may change the font (i.e. for Type1 support) within a
218 // ContentEntry. This is the one currently in effect, or NULL if none.
219 SkPDFFont* fFont;
220 // In PDF, text size has no default value. It is only valid if fFont is
221 // not NULL.
222 SkScalar fTextSize;
223};
224
225GraphicStateEntry::GraphicStateEntry() : fColor(SK_ColorBLACK),
226 fTextScaleX(SK_Scalar1),
227 fTextFill(SkPaint::kFill_Style),
228 fShaderIndex(-1),
229 fGraphicStateIndex(-1),
230 fFont(NULL),
231 fTextSize(SK_ScalarNaN) {
232 fMatrix.reset();
233}
234
commit-bot@chromium.orgb000d762014-02-07 19:39:57 +0000235bool GraphicStateEntry::compareInitialState(const GraphicStateEntry& cur) {
236 return fColor == cur.fColor &&
237 fShaderIndex == cur.fShaderIndex &&
238 fGraphicStateIndex == cur.fGraphicStateIndex &&
239 fMatrix == cur.fMatrix &&
240 fClipStack == cur.fClipStack &&
241 (fTextScaleX == 0 ||
242 (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000243}
244
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000245class GraphicStackState {
246public:
247 GraphicStackState(const SkClipStack& existingClipStack,
248 const SkRegion& existingClipRegion,
249 SkWStream* contentStream)
250 : fStackDepth(0),
251 fContentStream(contentStream) {
252 fEntries[0].fClipStack = existingClipStack;
253 fEntries[0].fClipRegion = existingClipRegion;
254 }
255
256 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000257 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000258 void updateMatrix(const SkMatrix& matrix);
259 void updateDrawingState(const GraphicStateEntry& state);
260
261 void drainStack();
262
263private:
264 void push();
265 void pop();
266 GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
267
268 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
269 static const int kMaxStackDepth = 12;
270 GraphicStateEntry fEntries[kMaxStackDepth + 1];
271 int fStackDepth;
272 SkWStream* fContentStream;
273};
274
275void GraphicStackState::drainStack() {
276 while (fStackDepth) {
277 pop();
278 }
279}
280
281void GraphicStackState::push() {
282 SkASSERT(fStackDepth < kMaxStackDepth);
283 fContentStream->writeText("q\n");
284 fStackDepth++;
285 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
286}
287
288void GraphicStackState::pop() {
289 SkASSERT(fStackDepth > 0);
290 fContentStream->writeText("Q\n");
291 fStackDepth--;
292}
293
robertphillips@google.com80214e22012-07-20 15:33:18 +0000294// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000295// and then skips over the leading entries as specified in prefix. It requires
296// and asserts that "prefix" will be a prefix to "stack."
297static void skip_clip_stack_prefix(const SkClipStack& prefix,
298 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000299 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000300 SkClipStack::B2TIter prefixIter(prefix);
301 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000302
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000303 const SkClipStack::Element* prefixEntry;
304 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000305
306 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000307 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000308 iterEntry = iter->next();
309 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000310 // Because of SkClipStack does internal intersection, the last clip
311 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000312 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000313 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
314 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
315 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000316 // back up the iterator by one
317 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000318 prefixEntry = prefixIter.next();
319 break;
320 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000321 }
322
323 SkASSERT(prefixEntry == NULL);
324}
325
326static void emit_clip(SkPath* clipPath, SkRect* clipRect,
327 SkWStream* contentStream) {
328 SkASSERT(clipPath || clipRect);
329
330 SkPath::FillType clipFill;
331 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000332 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000333 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000334 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000335 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
336 clipFill = SkPath::kWinding_FillType;
337 }
338
339 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
340 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
341 if (clipFill == SkPath::kEvenOdd_FillType) {
342 contentStream->writeText("W* n\n");
343 } else {
344 contentStream->writeText("W n\n");
345 }
346}
347
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000348#ifdef SK_PDF_USE_PATHOPS
349/* Calculate an inverted path's equivalent non-inverted path, given the
350 * canvas bounds.
351 * outPath may alias with invPath (since this is supported by PathOps).
352 */
353static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
354 SkPath* outPath) {
355 SkASSERT(invPath.isInverseFillType());
356
357 SkPath clipPath;
358 clipPath.addRect(bounds);
359
360 return Op(clipPath, invPath, kIntersect_PathOp, outPath);
361}
362
363// Sanity check the numerical values of the SkRegion ops and PathOps ops
364// enums so region_op_to_pathops_op can do a straight passthrough cast.
365// If these are failing, it may be necessary to make region_op_to_pathops_op
366// do more.
367SK_COMPILE_ASSERT(SkRegion::kDifference_Op == (int)kDifference_PathOp,
368 region_pathop_mismatch);
369SK_COMPILE_ASSERT(SkRegion::kIntersect_Op == (int)kIntersect_PathOp,
370 region_pathop_mismatch);
371SK_COMPILE_ASSERT(SkRegion::kUnion_Op == (int)kUnion_PathOp,
372 region_pathop_mismatch);
373SK_COMPILE_ASSERT(SkRegion::kXOR_Op == (int)kXOR_PathOp,
374 region_pathop_mismatch);
375SK_COMPILE_ASSERT(SkRegion::kReverseDifference_Op ==
376 (int)kReverseDifference_PathOp,
377 region_pathop_mismatch);
378
379static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
380 SkASSERT(op >= 0);
381 SkASSERT(op <= SkRegion::kReverseDifference_Op);
382 return (SkPathOp)op;
383}
384
385/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
386 * Returns true if successful, or false if not successful.
387 * If successful, the resulting clip is stored in outClipPath.
388 * If not successful, outClipPath is undefined, and a fallback method
389 * should be used.
390 */
391static bool get_clip_stack_path(const SkMatrix& transform,
392 const SkClipStack& clipStack,
393 const SkRegion& clipRegion,
394 SkPath* outClipPath) {
395 outClipPath->reset();
396 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
397
398 const SkClipStack::Element* clipEntry;
399 SkClipStack::Iter iter;
400 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
401 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
402 SkPath entryPath;
403 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
404 outClipPath->reset();
405 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
406 continue;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000407 } else {
408 clipEntry->asPath(&entryPath);
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000409 }
410 entryPath.transform(transform);
411
412 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
413 *outClipPath = entryPath;
414 } else {
415 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
416 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
417 return false;
418 }
419 }
420 }
421
422 if (outClipPath->isInverseFillType()) {
423 // The bounds are slightly outset to ensure this is correct in the
424 // face of floating-point accuracy and possible SkRegion bitmap
425 // approximations.
426 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
427 clipBounds.outset(SK_Scalar1, SK_Scalar1);
428 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
429 return false;
430 }
431 }
432 return true;
433}
434#endif
435
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000436// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000437// graphic state stack, and the fact that we can know all the clips used
438// on the page to optimize this.
439void GraphicStackState::updateClip(const SkClipStack& clipStack,
440 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000441 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000442 if (clipStack == currentEntry()->fClipStack) {
443 return;
444 }
445
446 while (fStackDepth > 0) {
447 pop();
448 if (clipStack == currentEntry()->fClipStack) {
449 return;
450 }
451 }
452 push();
453
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000454 currentEntry()->fClipStack = clipStack;
455 currentEntry()->fClipRegion = clipRegion;
456
457 SkMatrix transform;
458 transform.setTranslate(translation.fX, translation.fY);
459
460#ifdef SK_PDF_USE_PATHOPS
461 SkPath clipPath;
462 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
463 emit_clip(&clipPath, NULL, fContentStream);
464 return;
465 }
466#endif
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000467 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
468 // already been applied. (If this is a top level device, then it specifies
469 // a clip to the content area. If this is a layer, then it specifies
470 // the clip in effect when the layer was created.) There's no need to
471 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
472 // initial clip on the parent layer. (This means there's a bug if the user
473 // expands the clip and then uses any xfer mode that uses dst:
474 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000475 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000476 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
477
478 // If the clip stack does anything other than intersect or if it uses
479 // an inverse fill type, we have to fall back to the clip region.
480 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000481 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000482 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000483 if (clipEntry->getOp() != SkRegion::kIntersect_Op ||
484 clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000485 needRegion = true;
486 break;
487 }
488 }
489
490 if (needRegion) {
491 SkPath clipPath;
492 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
493 emit_clip(&clipPath, NULL, fContentStream);
494 } else {
495 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000496 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000497 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000498 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
499 switch (clipEntry->getType()) {
500 case SkClipStack::Element::kRect_Type: {
501 SkRect translatedClip;
502 transform.mapRect(&translatedClip, clipEntry->getRect());
503 emit_clip(NULL, &translatedClip, fContentStream);
504 break;
505 }
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000506 default: {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000507 SkPath translatedPath;
commit-bot@chromium.org5a346a82014-02-16 16:01:14 +0000508 clipEntry->asPath(&translatedPath);
509 translatedPath.transform(transform, &translatedPath);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000510 emit_clip(&translatedPath, NULL, fContentStream);
511 break;
512 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000513 }
514 }
515 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000516}
517
518void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
519 if (matrix == currentEntry()->fMatrix) {
520 return;
521 }
522
523 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
524 SkASSERT(fStackDepth > 0);
525 SkASSERT(fEntries[fStackDepth].fClipStack ==
526 fEntries[fStackDepth -1].fClipStack);
527 pop();
528
529 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
530 }
531 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
532 return;
533 }
534
535 push();
536 SkPDFUtils::AppendTransform(matrix, fContentStream);
537 currentEntry()->fMatrix = matrix;
538}
539
540void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) {
541 // PDF treats a shader as a color, so we only set one or the other.
542 if (state.fShaderIndex >= 0) {
543 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000544 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000545 currentEntry()->fShaderIndex = state.fShaderIndex;
546 }
547 } else {
548 if (state.fColor != currentEntry()->fColor ||
549 currentEntry()->fShaderIndex >= 0) {
550 emit_pdf_color(state.fColor, fContentStream);
551 fContentStream->writeText("RG ");
552 emit_pdf_color(state.fColor, fContentStream);
553 fContentStream->writeText("rg\n");
554 currentEntry()->fColor = state.fColor;
555 currentEntry()->fShaderIndex = -1;
556 }
557 }
558
559 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000560 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000561 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
562 }
563
564 if (state.fTextScaleX) {
565 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
566 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
567 SkIntToScalar(100));
568 SkPDFScalar::Append(pdfScale, fContentStream);
569 fContentStream->writeText(" Tz\n");
570 currentEntry()->fTextScaleX = state.fTextScaleX;
571 }
572 if (state.fTextFill != currentEntry()->fTextFill) {
573 SK_COMPILE_ASSERT(SkPaint::kFill_Style == 0, enum_must_match_value);
574 SK_COMPILE_ASSERT(SkPaint::kStroke_Style == 1,
575 enum_must_match_value);
576 SK_COMPILE_ASSERT(SkPaint::kStrokeAndFill_Style == 2,
577 enum_must_match_value);
578 fContentStream->writeDecAsText(state.fTextFill);
579 fContentStream->writeText(" Tr\n");
580 currentEntry()->fTextFill = state.fTextFill;
581 }
582 }
583}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000584
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000585SkBaseDevice* SkPDFDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
bsalomon@google.come97f0852011-06-17 13:10:25 +0000586 SkMatrix initialTransform;
587 initialTransform.reset();
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000588 SkISize size = SkISize::Make(info.width(), info.height());
bsalomon@google.come97f0852011-06-17 13:10:25 +0000589 return SkNEW_ARGS(SkPDFDevice, (size, size, initialTransform));
590}
591
592
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000593struct ContentEntry {
594 GraphicStateEntry fState;
595 SkDynamicMemoryWStream fContent;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000596 SkAutoTDelete<ContentEntry> fNext;
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000597
598 // If the stack is too deep we could get Stack Overflow.
599 // So we manually destruct the object.
600 ~ContentEntry() {
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000601 ContentEntry* val = fNext.detach();
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000602 while (val != NULL) {
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000603 ContentEntry* valNext = val->fNext.detach();
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000604 // When the destructor is called, fNext is NULL and exits.
605 delete val;
606 val = valNext;
607 }
608 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000609};
610
611// A helper class to automatically finish a ContentEntry at the end of a
612// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000613class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000614public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000615 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
616 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000617 : fDevice(device),
618 fContentEntry(NULL),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000619 fXfermode(SkXfermode::kSrcOver_Mode),
620 fDstFormXObject(NULL) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000621 init(draw.fClipStack, *draw.fClip, *draw.fMatrix, paint, hasText);
622 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000623 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
624 const SkRegion& clipRegion, const SkMatrix& matrix,
625 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000626 : fDevice(device),
627 fContentEntry(NULL),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000628 fXfermode(SkXfermode::kSrcOver_Mode),
629 fDstFormXObject(NULL) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000630 init(clipStack, clipRegion, matrix, paint, hasText);
631 }
632
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000633 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000634 if (fContentEntry) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000635 SkPath* shape = &fShape;
636 if (shape->isEmpty()) {
637 shape = NULL;
638 }
639 fDevice->finishContentEntry(fXfermode, fDstFormXObject, shape);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000640 }
reed@google.comfc641d02012-09-20 17:52:20 +0000641 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000642 }
643
644 ContentEntry* entry() { return fContentEntry; }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000645
646 /* Returns true when we explicitly need the shape of the drawing. */
647 bool needShape() {
648 switch (fXfermode) {
649 case SkXfermode::kClear_Mode:
650 case SkXfermode::kSrc_Mode:
651 case SkXfermode::kSrcIn_Mode:
652 case SkXfermode::kSrcOut_Mode:
653 case SkXfermode::kDstIn_Mode:
654 case SkXfermode::kDstOut_Mode:
655 case SkXfermode::kSrcATop_Mode:
656 case SkXfermode::kDstATop_Mode:
657 case SkXfermode::kModulate_Mode:
658 return true;
659 default:
660 return false;
661 }
662 }
663
664 /* Returns true unless we only need the shape of the drawing. */
665 bool needSource() {
666 if (fXfermode == SkXfermode::kClear_Mode) {
667 return false;
668 }
669 return true;
670 }
671
672 /* If the shape is different than the alpha component of the content, then
673 * setShape should be called with the shape. In particular, images and
674 * devices have rectangular shape.
675 */
676 void setShape(const SkPath& shape) {
677 fShape = shape;
678 }
679
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000680private:
681 SkPDFDevice* fDevice;
682 ContentEntry* fContentEntry;
683 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000684 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000685 SkPath fShape;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000686
687 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
688 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000689 // Shape has to be flatten before we get here.
690 if (matrix.hasPerspective()) {
691 NOT_IMPLEMENTED(!matrix.hasPerspective(), false);
vandebo@chromium.orgdc37e202013-10-18 20:16:34 +0000692 return;
693 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000694 if (paint.getXfermode()) {
695 paint.getXfermode()->asMode(&fXfermode);
696 }
697 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
698 matrix, paint, hasText,
699 &fDstFormXObject);
700 }
701};
702
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000703////////////////////////////////////////////////////////////////////////////////
704
ctguil@chromium.org15261292011-04-29 17:54:16 +0000705static inline SkBitmap makeContentBitmap(const SkISize& contentSize,
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000706 const SkMatrix* initialTransform) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000707 SkBitmap bitmap;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000708 if (initialTransform) {
709 // Compute the size of the drawing area.
710 SkVector drawingSize;
711 SkMatrix inverse;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000712 drawingSize.set(SkIntToScalar(contentSize.fWidth),
713 SkIntToScalar(contentSize.fHeight));
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000714 if (!initialTransform->invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000715 // This shouldn't happen, initial transform should be invertible.
716 SkASSERT(false);
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000717 inverse.reset();
718 }
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000719 inverse.mapVectors(&drawingSize, 1);
720 SkISize size = SkSize::Make(drawingSize.fX, drawingSize.fY).toRound();
721 bitmap.setConfig(SkBitmap::kNo_Config, abs(size.fWidth),
722 abs(size.fHeight));
723 } else {
724 bitmap.setConfig(SkBitmap::kNo_Config, abs(contentSize.fWidth),
725 abs(contentSize.fHeight));
726 }
727
reed@android.comf2b98d62010-12-20 18:26:13 +0000728 return bitmap;
729}
730
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000731// TODO(vandebo) change pageSize to SkSize.
ctguil@chromium.org15261292011-04-29 17:54:16 +0000732SkPDFDevice::SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000733 const SkMatrix& initialTransform)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000734 : SkBitmapDevice(makeContentBitmap(contentSize, &initialTransform)),
ctguil@chromium.org15261292011-04-29 17:54:16 +0000735 fPageSize(pageSize),
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000736 fContentSize(contentSize),
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000737 fLastContentEntry(NULL),
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000738 fLastMarginContentEntry(NULL),
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000739 fClipStack(NULL),
commit-bot@chromium.org8c294902013-10-21 17:14:37 +0000740 fEncoder(NULL),
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000741 fRasterDpi(72.0f) {
edisonn@google.com83d8eda2013-10-24 13:19:28 +0000742 // Just report that PDF does not supports perspective in the
743 // initial transform.
edisonn@google.comaa6c4d22013-09-19 17:36:47 +0000744 NOT_IMPLEMENTED(initialTransform.hasPerspective(), true);
745
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000746 // Skia generally uses the top left as the origin but PDF natively has the
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000747 // origin at the bottom left. This matrix corrects for that. But that only
748 // needs to be done once, we don't do it when layering.
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000749 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
750 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000751 fInitialTransform.preConcat(initialTransform);
752
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000753 SkIRect existingClip = SkIRect::MakeWH(this->width(), this->height());
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000754 fExistingClipRegion.setRect(existingClip);
755
756 this->init();
757}
758
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000759// TODO(vandebo) change layerSize to SkSize.
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000760SkPDFDevice::SkPDFDevice(const SkISize& layerSize,
761 const SkClipStack& existingClipStack,
762 const SkRegion& existingClipRegion)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000763 : SkBitmapDevice(makeContentBitmap(layerSize, NULL)),
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000764 fPageSize(layerSize),
765 fContentSize(layerSize),
766 fExistingClipStack(existingClipStack),
767 fExistingClipRegion(existingClipRegion),
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000768 fLastContentEntry(NULL),
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000769 fLastMarginContentEntry(NULL),
commit-bot@chromium.org8c294902013-10-21 17:14:37 +0000770 fClipStack(NULL),
771 fEncoder(NULL),
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000772 fRasterDpi(72.0f) {
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000773 fInitialTransform.reset();
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000774 this->init();
775}
776
777SkPDFDevice::~SkPDFDevice() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000778 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000779}
780
781void SkPDFDevice::init() {
reed@google.com2a006c12012-09-19 17:05:55 +0000782 fAnnotations = NULL;
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000783 fResourceDict = NULL;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000784 fContentEntries.free();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000785 fLastContentEntry = NULL;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000786 fMarginContentEntries.free();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000787 fLastMarginContentEntry = NULL;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000788 fDrawingArea = kContent_DrawingArea;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000789 if (fFontGlyphUsage.get() == NULL) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000790 fFontGlyphUsage.reset(new SkPDFGlyphSetMap());
791 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000792}
793
vandebo@chromium.org98594282011-07-25 22:34:12 +0000794void SkPDFDevice::cleanUp(bool clearFontUsage) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000795 fGraphicStateResources.unrefAll();
796 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000797 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000798 fShaderResources.unrefAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000799 SkSafeUnref(fAnnotations);
reed@google.comfc641d02012-09-20 17:52:20 +0000800 SkSafeUnref(fResourceDict);
epoger@google.comb58772f2013-03-08 09:09:10 +0000801 fNamedDestinations.deleteAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000802
vandebo@chromium.org98594282011-07-25 22:34:12 +0000803 if (clearFontUsage) {
804 fFontGlyphUsage->reset();
805 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000806}
807
reed@google.com982cb872011-12-07 18:34:08 +0000808uint32_t SkPDFDevice::getDeviceCapabilities() {
809 return kVector_Capability;
810}
811
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000812void SkPDFDevice::clear(SkColor color) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000813 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000814 this->init();
815
816 SkPaint paint;
817 paint.setColor(color);
818 paint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000819 SkMatrix identity;
820 identity.reset();
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000821 ScopedContentEntry content(this, &fExistingClipStack, fExistingClipRegion,
822 identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000823 internalDrawPaint(paint, content.entry());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000824}
825
826void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000827 SkPaint newPaint = paint;
828 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000829 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000830 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000831}
832
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000833void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
834 ContentEntry* contentEntry) {
835 if (!contentEntry) {
836 return;
837 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000838 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
839 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000840 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000841 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000842 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000843 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000844 inverse.mapRect(&bbox);
845
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000846 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000847 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000848 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000849}
850
851void SkPDFDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
852 size_t count, const SkPoint* points,
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000853 const SkPaint& passedPaint) {
854 if (count == 0) {
855 return;
856 }
857
epoger@google.comb58772f2013-03-08 09:09:10 +0000858 if (handlePointAnnotation(points, count, *d.fMatrix, passedPaint)) {
859 return;
860 }
861
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000862 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
863 // We only use this when there's a path effect because of the overhead
864 // of multiple calls to setUpContentEntry it causes.
865 if (passedPaint.getPathEffect()) {
866 if (d.fClip->isEmpty()) {
867 return;
868 }
869 SkDraw pointDraw(d);
870 pointDraw.fDevice = this;
871 pointDraw.drawPoints(mode, count, points, passedPaint, true);
872 return;
873 }
874
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000875 const SkPaint* paint = &passedPaint;
876 SkPaint modifiedPaint;
877
878 if (mode == SkCanvas::kPoints_PointMode &&
879 paint->getStrokeCap() != SkPaint::kRound_Cap) {
880 modifiedPaint = *paint;
881 paint = &modifiedPaint;
882 if (paint->getStrokeWidth()) {
883 // PDF won't draw a single point with square/butt caps because the
884 // orientation is ambiguous. Draw a rectangle instead.
885 modifiedPaint.setStyle(SkPaint::kFill_Style);
886 SkScalar strokeWidth = paint->getStrokeWidth();
887 SkScalar halfStroke = SkScalarHalf(strokeWidth);
888 for (size_t i = 0; i < count; i++) {
889 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
890 r.inset(-halfStroke, -halfStroke);
891 drawRect(d, r, modifiedPaint);
892 }
893 return;
894 } else {
895 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
896 }
897 }
898
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000899 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000900 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000901 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000902 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000903
904 switch (mode) {
905 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000906 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000907 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000908 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000909 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000910 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000911 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000912 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000913 break;
914 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000915 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000916 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000917 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000918 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000919 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000920 &content.entry()->fContent);
921 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000922 }
923 break;
924 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000925 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
926 for (size_t i = 0; i < count; i++) {
927 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000928 &content.entry()->fContent);
929 SkPDFUtils::ClosePath(&content.entry()->fContent);
930 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000931 }
932 break;
933 default:
934 SkASSERT(false);
935 }
936}
937
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000938void SkPDFDevice::drawRect(const SkDraw& d, const SkRect& rect,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000939 const SkPaint& paint) {
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000940 SkRect r = rect;
941 r.sort();
942
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000943 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000944 if (d.fClip->isEmpty()) {
945 return;
946 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000947 SkPath path;
948 path.addRect(r);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000949 drawPath(d, path, paint, NULL, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000950 return;
951 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000952
epoger@google.comb58772f2013-03-08 09:09:10 +0000953 if (handleRectAnnotation(r, *d.fMatrix, paint)) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +0000954 return;
955 }
956
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000957 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000958 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000959 return;
960 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000961 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000962 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000963 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000964}
965
scroggo@google.coma8e33a92013-11-08 18:02:53 +0000966void SkPDFDevice::drawRRect(const SkDraw& draw, const SkRRect& rrect,
967 const SkPaint& paint) {
968 SkPath path;
969 path.addRRect(rrect);
970 this->drawPath(draw, path, paint, NULL, true);
971}
972
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000973void SkPDFDevice::drawPath(const SkDraw& d, const SkPath& origPath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000974 const SkPaint& paint, const SkMatrix* prePathMatrix,
975 bool pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000976 SkPath modifiedPath;
977 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
978
979 SkMatrix matrix = *d.fMatrix;
980 if (prePathMatrix) {
981 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
982 if (!pathIsMutable) {
983 pathPtr = &modifiedPath;
984 pathIsMutable = true;
985 }
986 origPath.transform(*prePathMatrix, pathPtr);
987 } else {
988 if (!matrix.preConcat(*prePathMatrix)) {
edisonn@google.comaa6c4d22013-09-19 17:36:47 +0000989 // TODO(edisonn): report somehow why we failed?
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000990 return;
991 }
992 }
993 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000994
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000995 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000996 if (d.fClip->isEmpty()) {
997 return;
998 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000999 if (!pathIsMutable) {
1000 pathPtr = &modifiedPath;
1001 pathIsMutable = true;
1002 }
1003 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +00001004
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001005 SkPaint noEffectPaint(paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001006 noEffectPaint.setPathEffect(NULL);
1007 if (fill) {
1008 noEffectPaint.setStyle(SkPaint::kFill_Style);
1009 } else {
1010 noEffectPaint.setStyle(SkPaint::kStroke_Style);
1011 noEffectPaint.setStrokeWidth(0);
1012 }
1013 drawPath(d, *pathPtr, noEffectPaint, NULL, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +00001014 return;
1015 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001016
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001017#ifdef SK_PDF_USE_PATHOPS
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001018 if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001019 return;
1020 }
1021#endif
1022
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001023 if (handleRectAnnotation(pathPtr->getBounds(), matrix, paint)) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001024 return;
1025 }
1026
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001027 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001028 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001029 return;
1030 }
vandebo@chromium.org683001c2012-05-09 17:17:51 +00001031 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
1032 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +00001033 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001034 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001035}
1036
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001037void SkPDFDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1038 const SkRect* src, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001039 const SkPaint& paint,
1040 SkCanvas::DrawBitmapRectFlags flags) {
1041 // TODO: this code path must be updated to respect the flags parameter
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001042 SkMatrix matrix;
1043 SkRect bitmapBounds, tmpSrc, tmpDst;
1044 SkBitmap tmpBitmap;
1045
1046 bitmapBounds.isetWH(bitmap.width(), bitmap.height());
1047
1048 // Compute matrix from the two rectangles
1049 if (src) {
1050 tmpSrc = *src;
1051 } else {
1052 tmpSrc = bitmapBounds;
1053 }
1054 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1055
1056 const SkBitmap* bitmapPtr = &bitmap;
1057
1058 // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
1059 // needed (if the src was clipped). No check needed if src==null.
1060 if (src) {
1061 if (!bitmapBounds.contains(*src)) {
1062 if (!tmpSrc.intersect(bitmapBounds)) {
1063 return; // nothing to draw
1064 }
1065 // recompute dst, based on the smaller tmpSrc
1066 matrix.mapRect(&tmpDst, tmpSrc);
1067 }
1068
1069 // since we may need to clamp to the borders of the src rect within
1070 // the bitmap, we extract a subset.
1071 // TODO: make sure this is handled in drawBitmap and remove from here.
1072 SkIRect srcIR;
1073 tmpSrc.roundOut(&srcIR);
1074 if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
1075 return;
1076 }
1077 bitmapPtr = &tmpBitmap;
1078
1079 // Since we did an extract, we need to adjust the matrix accordingly
1080 SkScalar dx = 0, dy = 0;
1081 if (srcIR.fLeft > 0) {
1082 dx = SkIntToScalar(srcIR.fLeft);
1083 }
1084 if (srcIR.fTop > 0) {
1085 dy = SkIntToScalar(srcIR.fTop);
1086 }
1087 if (dx || dy) {
1088 matrix.preTranslate(dx, dy);
1089 }
1090 }
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001091 this->drawBitmap(draw, *bitmapPtr, matrix, paint);
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001092}
1093
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001094void SkPDFDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001095 const SkMatrix& matrix, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001096 if (d.fClip->isEmpty()) {
1097 return;
1098 }
1099
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001100 SkMatrix transform = matrix;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001101 transform.postConcat(*d.fMatrix);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001102 this->internalDrawBitmap(transform, d.fClipStack, *d.fClip, bitmap, NULL,
1103 paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001104}
1105
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001106void SkPDFDevice::drawSprite(const SkDraw& d, const SkBitmap& bitmap,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001107 int x, int y, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001108 if (d.fClip->isEmpty()) {
1109 return;
1110 }
1111
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001112 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001113 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001114 this->internalDrawBitmap(matrix, d.fClipStack, *d.fClip, bitmap, NULL,
1115 paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001116}
1117
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001118void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001119 SkScalar x, SkScalar y, const SkPaint& paint) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001120 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false);
1121 if (paint.getMaskFilter() != NULL) {
1122 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1123 // making text unreadable (e.g. same text twice when using CSS shadows).
1124 return;
1125 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001126 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001127 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001128 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001129 return;
1130 }
1131
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001132 SkGlyphStorage storage(0);
1133 uint16_t* glyphIDs = NULL;
1134 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage,
1135 &glyphIDs);
1136 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001137
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001138 SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc();
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001139 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001140 content.entry()->fContent.writeText("BT\n");
1141 set_text_transform(x, y, textPaint.getTextSkewX(),
1142 &content.entry()->fContent);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001143 size_t consumedGlyphCount = 0;
1144 while (numGlyphs > consumedGlyphCount) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001145 updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
1146 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001147 size_t availableGlyphs =
1148 font->glyphsToPDFFontEncoding(glyphIDs + consumedGlyphCount,
1149 numGlyphs - consumedGlyphCount);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001150 fFontGlyphUsage->noteGlyphUsage(font, glyphIDs + consumedGlyphCount,
1151 availableGlyphs);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001152 SkString encodedString =
reed@google.comf6c3ebd2011-07-20 17:20:28 +00001153 SkPDFString::FormatString(glyphIDs + consumedGlyphCount,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001154 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001155 content.entry()->fContent.writeText(encodedString.c_str());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001156 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001157 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001158 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001159 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001160}
1161
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001162void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001163 const SkScalar pos[], SkScalar constY,
1164 int scalarsPerPos, const SkPaint& paint) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001165 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false);
1166 if (paint.getMaskFilter() != NULL) {
1167 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1168 // making text unreadable (e.g. same text twice when using CSS shadows).
1169 return;
1170 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001171 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001172 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001173 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001174 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001175 return;
1176 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001177
djsollen@google.com5df5e612013-10-03 14:42:24 +00001178#ifdef SK_BUILD_FOR_ANDROID
1179 /*
1180 * In the case that we have enabled fallback fonts on Android we need to
1181 * take the following steps to ensure that the PDF draws all characters,
1182 * regardless of their underlying font file, correctly.
1183 *
1184 * 1. Convert input into GlyphID encoding if it currently is not
1185 * 2. Iterate over the glyphIDs and identify the actual typeface that each
1186 * glyph resolves to
1187 * 3. Iterate over those typefaces and recursively call this function with
1188 * only the glyphs (and their positions) that the typeface is capable of
1189 * resolving.
1190 */
1191 if (paint.getPaintOptionsAndroid().isUsingFontFallbacks()) {
1192 uint16_t* glyphIDs = NULL;
1193 SkGlyphStorage tmpStorage(0);
1194 size_t numGlyphs = 0;
1195
1196 // convert to glyphIDs
1197 if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) {
1198 numGlyphs = len / 2;
1199 glyphIDs = reinterpret_cast<uint16_t*>(const_cast<void*>(text));
1200 } else {
1201 numGlyphs = paint.textToGlyphs(text, len, NULL);
1202 tmpStorage.reset(numGlyphs);
1203 paint.textToGlyphs(text, len, tmpStorage.get());
1204 glyphIDs = tmpStorage.get();
1205 }
1206
1207 // if no typeface is provided in the paint get the default
1208 SkAutoTUnref<SkTypeface> origFace(SkSafeRef(paint.getTypeface()));
1209 if (NULL == origFace.get()) {
1210 origFace.reset(SkTypeface::RefDefault());
1211 }
1212 const uint16_t origGlyphCount = origFace->countGlyphs();
1213
1214 // keep a list of the already visited typefaces and some data about them
1215 SkTDArray<TypefaceFallbackData> visitedTypefaces;
1216
1217 // find all the typefaces needed to resolve this run of text
1218 bool usesOriginalTypeface = false;
1219 for (uint16_t x = 0; x < numGlyphs; ++x) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001220 // optimization that checks to see if original typeface can resolve
1221 // the glyph
djsollen@google.com5df5e612013-10-03 14:42:24 +00001222 if (glyphIDs[x] < origGlyphCount) {
1223 usesOriginalTypeface = true;
1224 continue;
1225 }
1226
1227 // find the fallback typeface that supports this glyph
1228 TypefaceFallbackData data;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001229 data.typeface =
1230 SkGetTypefaceForGlyphID(glyphIDs[x], origFace.get(),
1231 paint.getPaintOptionsAndroid(),
1232 &data.lowerBounds,
1233 &data.upperBounds);
djsollen@google.com5df5e612013-10-03 14:42:24 +00001234 // add the typeface and its data if we don't have it
1235 if (data.typeface && !visitedTypefaces.contains(data)) {
1236 visitedTypefaces.push(data);
1237 }
1238 }
1239
1240 // if the original font was used then add it to the list as well
1241 if (usesOriginalTypeface) {
1242 TypefaceFallbackData* data = visitedTypefaces.push();
1243 data->typeface = origFace.get();
1244 data->lowerBounds = 0;
1245 data->upperBounds = origGlyphCount;
1246 }
1247
1248 // keep a scratch glyph and pos storage
1249 SkAutoTMalloc<SkScalar> posStorage(len * scalarsPerPos);
1250 SkScalar* tmpPos = posStorage.get();
1251 SkGlyphStorage glyphStorage(numGlyphs);
1252 uint16_t* tmpGlyphIDs = glyphStorage.get();
1253
1254 // loop through all the valid typefaces, trim the glyphs to only those
1255 // resolved by the typeface, and then draw that run of glyphs
1256 for (int x = 0; x < visitedTypefaces.count(); ++x) {
1257 const TypefaceFallbackData& data = visitedTypefaces[x];
1258
1259 int tmpGlyphCount = 0;
1260 for (uint16_t y = 0; y < numGlyphs; ++y) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001261 if (glyphIDs[y] >= data.lowerBounds &&
1262 glyphIDs[y] < data.upperBounds) {
djsollen@google.com5df5e612013-10-03 14:42:24 +00001263 tmpGlyphIDs[tmpGlyphCount] = glyphIDs[y] - data.lowerBounds;
1264 memcpy(&(tmpPos[tmpGlyphCount * scalarsPerPos]),
1265 &(pos[y * scalarsPerPos]),
1266 scalarsPerPos * sizeof(SkScalar));
1267 tmpGlyphCount++;
1268 }
1269 }
1270
1271 // recursively call this function with the right typeface
1272 SkPaint tmpPaint = paint;
1273 tmpPaint.setTypeface(data.typeface);
1274 tmpPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
1275
1276 // turn off fallback chaining
1277 SkPaintOptionsAndroid paintOpts = tmpPaint.getPaintOptionsAndroid();
1278 paintOpts.setUseFontFallbacks(false);
1279 tmpPaint.setPaintOptionsAndroid(paintOpts);
1280
1281 this->drawPosText(d, tmpGlyphIDs, tmpGlyphCount * 2, tmpPos, constY,
1282 scalarsPerPos, tmpPaint);
1283 }
1284 return;
1285 }
1286#endif
1287
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001288 SkGlyphStorage storage(0);
1289 uint16_t* glyphIDs = NULL;
1290 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage,
1291 &glyphIDs);
1292 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001293
1294 SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001295 content.entry()->fContent.writeText("BT\n");
1296 updateFont(textPaint, glyphIDs[0], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001297 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001298 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001299 uint16_t encodedValue = glyphIDs[i];
1300 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001301 updateFont(textPaint, glyphIDs[i], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001302 i--;
1303 continue;
1304 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001305 fFontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001306 SkScalar x = pos[i * scalarsPerPos];
1307 SkScalar y = scalarsPerPos == 1 ? constY : pos[i * scalarsPerPos + 1];
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001308 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001309 set_text_transform(x, y, textPaint.getTextSkewX(),
1310 &content.entry()->fContent);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001311 SkString encodedString =
reed@google.comf6c3ebd2011-07-20 17:20:28 +00001312 SkPDFString::FormatString(&encodedValue, 1,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001313 font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001314 content.entry()->fContent.writeText(encodedString.c_str());
1315 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001316 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001317 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001318}
1319
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001320void SkPDFDevice::drawTextOnPath(const SkDraw& d, const void* text, size_t len,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001321 const SkPath& path, const SkMatrix* matrix,
1322 const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001323 if (d.fClip->isEmpty()) {
1324 return;
1325 }
vandebo@chromium.org290e3bb2011-11-08 23:42:53 +00001326 d.drawTextOnPath((const char*)text, len, path, matrix, paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001327}
1328
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001329void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001330 int vertexCount, const SkPoint verts[],
1331 const SkPoint texs[], const SkColor colors[],
1332 SkXfermode* xmode, const uint16_t indices[],
1333 int indexCount, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001334 if (d.fClip->isEmpty()) {
1335 return;
1336 }
reed@google.com85e143c2013-12-30 15:51:25 +00001337 // TODO: implement drawVertices
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001338}
1339
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001340void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
1341 int x, int y, const SkPaint& paint) {
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001342 if ((device->getDeviceCapabilities() & kVector_Capability) == 0) {
1343 // If we somehow get a raster device, do what our parent would do.
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001344 INHERITED::drawDevice(d, device, x, y, paint);
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001345 return;
1346 }
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001347
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001348 // Assume that a vector capable device means that it's a PDF Device.
1349 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001350 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001351 return;
1352 }
1353
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001354 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001355 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001356 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001357 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001358 return;
1359 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001360 if (content.needShape()) {
1361 SkPath shape;
1362 shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00001363 SkIntToScalar(device->width()),
1364 SkIntToScalar(device->height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001365 content.setShape(shape);
1366 }
1367 if (!content.needSource()) {
1368 return;
1369 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001370
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001371 SkAutoTUnref<SkPDFFormXObject> xObject(new SkPDFFormXObject(pdfDevice));
1372 SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001373 &content.entry()->fContent);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001374
1375 // Merge glyph sets from the drawn device.
1376 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001377}
1378
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001379void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1380 INHERITED::onAttachToCanvas(canvas);
1381
1382 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1383 fClipStack = canvas->getClipStack();
1384}
1385
1386void SkPDFDevice::onDetachFromCanvas() {
1387 INHERITED::onDetachFromCanvas();
1388
1389 fClipStack = NULL;
1390}
1391
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001392ContentEntry* SkPDFDevice::getLastContentEntry() {
1393 if (fDrawingArea == kContent_DrawingArea) {
1394 return fLastContentEntry;
1395 } else {
1396 return fLastMarginContentEntry;
1397 }
1398}
1399
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001400SkAutoTDelete<ContentEntry>* SkPDFDevice::getContentEntries() {
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001401 if (fDrawingArea == kContent_DrawingArea) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001402 return &fContentEntries;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001403 } else {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001404 return &fMarginContentEntries;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001405 }
1406}
1407
1408void SkPDFDevice::setLastContentEntry(ContentEntry* contentEntry) {
1409 if (fDrawingArea == kContent_DrawingArea) {
1410 fLastContentEntry = contentEntry;
1411 } else {
1412 fLastMarginContentEntry = contentEntry;
1413 }
1414}
1415
1416void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001417 // A ScopedContentEntry only exists during the course of a draw call, so
1418 // this can't be called while a ScopedContentEntry exists.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001419 fDrawingArea = drawingArea;
1420}
1421
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001422SkPDFResourceDict* SkPDFDevice::getResourceDict() {
reed@google.comfc641d02012-09-20 17:52:20 +00001423 if (NULL == fResourceDict) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001424 fResourceDict = SkNEW(SkPDFResourceDict);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001425
1426 if (fGraphicStateResources.count()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001427 for (int i = 0; i < fGraphicStateResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001428 fResourceDict->insertResourceAsReference(
1429 SkPDFResourceDict::kExtGState_ResourceType,
1430 i, fGraphicStateResources[i]);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001431 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001432 }
1433
1434 if (fXObjectResources.count()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001435 for (int i = 0; i < fXObjectResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001436 fResourceDict->insertResourceAsReference(
1437 SkPDFResourceDict::kXObject_ResourceType,
1438 i, fXObjectResources[i]);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001439 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001440 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001441
1442 if (fFontResources.count()) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001443 for (int i = 0; i < fFontResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001444 fResourceDict->insertResourceAsReference(
1445 SkPDFResourceDict::kFont_ResourceType,
1446 i, fFontResources[i]);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001447 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001448 }
1449
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001450 if (fShaderResources.count()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001451 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001452 for (int i = 0; i < fShaderResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001453 fResourceDict->insertResourceAsReference(
1454 SkPDFResourceDict::kPattern_ResourceType,
1455 i, fShaderResources[i]);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001456 }
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001457 }
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001458 }
1459 return fResourceDict;
1460}
1461
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001462const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1463 return fFontResources;
1464}
1465
reed@google.com2a006c12012-09-19 17:05:55 +00001466SkPDFArray* SkPDFDevice::copyMediaBox() const {
1467 // should this be a singleton?
1468 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0)));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001469
reed@google.com2a006c12012-09-19 17:05:55 +00001470 SkPDFArray* mediaBox = SkNEW(SkPDFArray);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001471 mediaBox->reserve(4);
1472 mediaBox->append(zero.get());
1473 mediaBox->append(zero.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001474 mediaBox->appendInt(fPageSize.fWidth);
1475 mediaBox->appendInt(fPageSize.fHeight);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001476 return mediaBox;
1477}
1478
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001479SkStream* SkPDFDevice::content() const {
reed@google.com5667afc2011-06-27 14:42:15 +00001480 SkMemoryStream* result = new SkMemoryStream;
1481 result->setData(this->copyContentToData())->unref();
1482 return result;
1483}
1484
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001485void SkPDFDevice::copyContentEntriesToData(ContentEntry* entry,
1486 SkWStream* data) const {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001487 // TODO(ctguil): For margins, I'm not sure fExistingClipStack/Region is the
1488 // right thing to pass here.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001489 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, data);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001490 while (entry != NULL) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +00001491 SkPoint translation;
1492 translation.iset(this->getOrigin());
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001493 translation.negate();
1494 gsState.updateClip(entry->fState.fClipStack, entry->fState.fClipRegion,
1495 translation);
1496 gsState.updateMatrix(entry->fState.fMatrix);
1497 gsState.updateDrawingState(entry->fState);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001498
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001499 SkAutoDataUnref copy(entry->fContent.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001500 data->write(copy->data(), copy->size());
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001501 entry = entry->fNext.get();
1502 }
1503 gsState.drainStack();
1504}
1505
reed@google.com5667afc2011-06-27 14:42:15 +00001506SkData* SkPDFDevice::copyContentToData() const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001507 SkDynamicMemoryWStream data;
1508 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
1509 SkPDFUtils::AppendTransform(fInitialTransform, &data);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001510 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001511
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001512 // TODO(aayushkumar): Apply clip along the margins. Currently, webkit
1513 // colors the contentArea white before it starts drawing into it and
1514 // that currently acts as our clip.
1515 // Also, think about adding a transform here (or assume that the values
1516 // sent across account for that)
1517 SkPDFDevice::copyContentEntriesToData(fMarginContentEntries.get(), &data);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001518
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001519 // If the content area is the entire page, then we don't need to clip
1520 // the content area (PDF area clips to the page size). Otherwise,
1521 // we have to clip to the content area; we've already applied the
1522 // initial transform, so just clip to the device size.
1523 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001524 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1525 SkIntToScalar(this->height()));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001526 emit_clip(NULL, &r, &data);
1527 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001528
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001529 SkPDFDevice::copyContentEntriesToData(fContentEntries.get(), &data);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001530
reed@google.com5667afc2011-06-27 14:42:15 +00001531 // potentially we could cache this SkData, and only rebuild it if we
1532 // see that our state has changed.
1533 return data.copyToData();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001534}
1535
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +00001536#ifdef SK_PDF_USE_PATHOPS
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001537/* Draws an inverse filled path by using Path Ops to compute the positive
1538 * inverse using the current clip as the inverse bounds.
1539 * Return true if this was an inverse path and was properly handled,
1540 * otherwise returns false and the normal drawing routine should continue,
1541 * either as a (incorrect) fallback or because the path was not inverse
1542 * in the first place.
1543 */
1544bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001545 const SkPaint& paint, bool pathIsMutable,
1546 const SkMatrix* prePathMatrix) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001547 if (!origPath.isInverseFillType()) {
1548 return false;
1549 }
1550
1551 if (d.fClip->isEmpty()) {
1552 return false;
1553 }
1554
1555 SkPath modifiedPath;
1556 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1557 SkPaint noInversePaint(paint);
1558
1559 // Merge stroking operations into final path.
1560 if (SkPaint::kStroke_Style == paint.getStyle() ||
1561 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1562 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1563 if (doFillPath) {
1564 noInversePaint.setStyle(SkPaint::kFill_Style);
1565 noInversePaint.setStrokeWidth(0);
1566 pathPtr = &modifiedPath;
1567 } else {
1568 // To be consistent with the raster output, hairline strokes
1569 // are rendered as non-inverted.
1570 modifiedPath.toggleInverseFillType();
1571 drawPath(d, modifiedPath, paint, NULL, true);
1572 return true;
1573 }
1574 }
1575
1576 // Get bounds of clip in current transform space
1577 // (clip bounds are given in device space).
1578 SkRect bounds;
1579 SkMatrix transformInverse;
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001580 SkMatrix totalMatrix = *d.fMatrix;
1581 if (prePathMatrix) {
1582 totalMatrix.preConcat(*prePathMatrix);
1583 }
1584 if (!totalMatrix.invert(&transformInverse)) {
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001585 return false;
1586 }
1587 bounds.set(d.fClip->getBounds());
1588 transformInverse.mapRect(&bounds);
1589
1590 // Extend the bounds by the line width (plus some padding)
1591 // so the edge doesn't cause a visible stroke.
1592 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1593 paint.getStrokeWidth() + SK_Scalar1);
1594
1595 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1596 return false;
1597 }
1598
edisonn@google.coma9ebd162013-10-07 13:22:21 +00001599 drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true);
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001600 return true;
1601}
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +00001602#endif
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001603
epoger@google.comb58772f2013-03-08 09:09:10 +00001604bool SkPDFDevice::handleRectAnnotation(const SkRect& r, const SkMatrix& matrix,
1605 const SkPaint& p) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001606 SkAnnotation* annotationInfo = p.getAnnotation();
1607 if (!annotationInfo) {
1608 return false;
1609 }
1610 SkData* urlData = annotationInfo->find(SkAnnotationKeys::URL_Key());
epoger@google.comb58772f2013-03-08 09:09:10 +00001611 if (urlData) {
1612 handleLinkToURL(urlData, r, matrix);
reed@google.com44699382013-10-31 17:28:30 +00001613 return p.getAnnotation() != NULL;
epoger@google.comb58772f2013-03-08 09:09:10 +00001614 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001615 SkData* linkToName = annotationInfo->find(
1616 SkAnnotationKeys::Link_Named_Dest_Key());
epoger@google.comb58772f2013-03-08 09:09:10 +00001617 if (linkToName) {
1618 handleLinkToNamedDest(linkToName, r, matrix);
reed@google.com44699382013-10-31 17:28:30 +00001619 return p.getAnnotation() != NULL;
epoger@google.comb58772f2013-03-08 09:09:10 +00001620 }
1621 return false;
1622}
1623
1624bool SkPDFDevice::handlePointAnnotation(const SkPoint* points, size_t count,
1625 const SkMatrix& matrix,
1626 const SkPaint& paint) {
1627 SkAnnotation* annotationInfo = paint.getAnnotation();
1628 if (!annotationInfo) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001629 return false;
1630 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001631 SkData* nameData = annotationInfo->find(
1632 SkAnnotationKeys::Define_Named_Dest_Key());
epoger@google.comb58772f2013-03-08 09:09:10 +00001633 if (nameData) {
1634 for (size_t i = 0; i < count; i++) {
1635 defineNamedDestination(nameData, points[i], matrix);
1636 }
reed@google.com44699382013-10-31 17:28:30 +00001637 return paint.getAnnotation() != NULL;
epoger@google.comb58772f2013-03-08 09:09:10 +00001638 }
1639 return false;
1640}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001641
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001642SkPDFDict* SkPDFDevice::createLinkAnnotation(const SkRect& r,
1643 const SkMatrix& matrix) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001644 SkMatrix transform = matrix;
1645 transform.postConcat(fInitialTransform);
1646 SkRect translatedRect;
1647 transform.mapRect(&translatedRect, r);
1648
reed@google.com2a006c12012-09-19 17:05:55 +00001649 if (NULL == fAnnotations) {
1650 fAnnotations = SkNEW(SkPDFArray);
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001651 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001652 SkPDFDict* annotation(SkNEW_ARGS(SkPDFDict, ("Annot")));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001653 annotation->insertName("Subtype", "Link");
epoger@google.comb58772f2013-03-08 09:09:10 +00001654 fAnnotations->append(annotation);
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001655
epoger@google.comb58772f2013-03-08 09:09:10 +00001656 SkAutoTUnref<SkPDFArray> border(SkNEW(SkPDFArray));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001657 border->reserve(3);
1658 border->appendInt(0); // Horizontal corner radius.
1659 border->appendInt(0); // Vertical corner radius.
1660 border->appendInt(0); // Width, 0 = no border.
1661 annotation->insert("Border", border.get());
1662
epoger@google.comb58772f2013-03-08 09:09:10 +00001663 SkAutoTUnref<SkPDFArray> rect(SkNEW(SkPDFArray));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001664 rect->reserve(4);
1665 rect->appendScalar(translatedRect.fLeft);
1666 rect->appendScalar(translatedRect.fTop);
1667 rect->appendScalar(translatedRect.fRight);
1668 rect->appendScalar(translatedRect.fBottom);
1669 annotation->insert("Rect", rect.get());
1670
epoger@google.comb58772f2013-03-08 09:09:10 +00001671 return annotation;
1672}
epoger@google.com1cad8982013-03-06 00:05:13 +00001673
epoger@google.comb58772f2013-03-08 09:09:10 +00001674void SkPDFDevice::handleLinkToURL(SkData* urlData, const SkRect& r,
1675 const SkMatrix& matrix) {
1676 SkAutoTUnref<SkPDFDict> annotation(createLinkAnnotation(r, matrix));
1677
1678 SkString url(static_cast<const char *>(urlData->data()),
1679 urlData->size() - 1);
1680 SkAutoTUnref<SkPDFDict> action(SkNEW_ARGS(SkPDFDict, ("Action")));
1681 action->insertName("S", "URI");
1682 action->insert("URI", SkNEW_ARGS(SkPDFString, (url)))->unref();
1683 annotation->insert("A", action.get());
1684}
1685
1686void SkPDFDevice::handleLinkToNamedDest(SkData* nameData, const SkRect& r,
1687 const SkMatrix& matrix) {
1688 SkAutoTUnref<SkPDFDict> annotation(createLinkAnnotation(r, matrix));
1689 SkString name(static_cast<const char *>(nameData->data()),
1690 nameData->size() - 1);
1691 annotation->insert("Dest", SkNEW_ARGS(SkPDFName, (name)))->unref();
1692}
1693
1694struct NamedDestination {
1695 const SkData* nameData;
1696 SkPoint point;
1697
1698 NamedDestination(const SkData* nameData, const SkPoint& point)
1699 : nameData(nameData), point(point) {
1700 nameData->ref();
1701 }
1702
1703 ~NamedDestination() {
1704 nameData->unref();
1705 }
1706};
1707
1708void SkPDFDevice::defineNamedDestination(SkData* nameData, const SkPoint& point,
1709 const SkMatrix& matrix) {
1710 SkMatrix transform = matrix;
1711 transform.postConcat(fInitialTransform);
1712 SkPoint translatedPoint;
1713 transform.mapXY(point.x(), point.y(), &translatedPoint);
1714 fNamedDestinations.push(
1715 SkNEW_ARGS(NamedDestination, (nameData, translatedPoint)));
1716}
1717
1718void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) {
1719 int nDest = fNamedDestinations.count();
1720 for (int i = 0; i < nDest; i++) {
1721 NamedDestination* dest = fNamedDestinations[i];
1722 SkAutoTUnref<SkPDFArray> pdfDest(SkNEW(SkPDFArray));
1723 pdfDest->reserve(5);
1724 pdfDest->append(SkNEW_ARGS(SkPDFObjRef, (page)))->unref();
1725 pdfDest->appendName("XYZ");
1726 pdfDest->appendScalar(dest->point.x());
1727 pdfDest->appendScalar(dest->point.y());
1728 pdfDest->appendInt(0); // Leave zoom unchanged
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001729 dict->insert(static_cast<const char *>(dest->nameData->data()),
1730 pdfDest);
epoger@google.comb58772f2013-03-08 09:09:10 +00001731 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001732}
1733
reed@google.comfc641d02012-09-20 17:52:20 +00001734SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
1735 SkPDFFormXObject* xobject = SkNEW_ARGS(SkPDFFormXObject, (this));
vandebo@chromium.org98594282011-07-25 22:34:12 +00001736 // We always draw the form xobjects that we create back into the device, so
1737 // we simply preserve the font usage instead of pulling it out and merging
1738 // it back in later.
1739 cleanUp(false); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001740 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001741 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001742}
1743
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001744void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex,
1745 SkPDFFormXObject* mask,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001746 const SkClipStack* clipStack,
1747 const SkRegion& clipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001748 SkXfermode::Mode mode,
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001749 bool invertClip) {
1750 if (clipRegion.isEmpty() && !invertClip) {
1751 return;
1752 }
1753
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001754 SkAutoTUnref<SkPDFGraphicState> sMaskGS(
1755 SkPDFGraphicState::GetSMaskGraphicState(
1756 mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode));
1757
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001758 SkMatrix identity;
1759 identity.reset();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001760 SkPaint paint;
1761 paint.setXfermodeMode(mode);
1762 ScopedContentEntry content(this, clipStack, clipRegion, identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001763 if (!content.entry()) {
1764 return;
1765 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001766 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001767 &content.entry()->fContent);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001768 SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001769
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001770 sMaskGS.reset(SkPDFGraphicState::GetNoSMaskGraphicState());
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001771 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001772 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001773}
1774
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001775ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
1776 const SkRegion& clipRegion,
1777 const SkMatrix& matrix,
1778 const SkPaint& paint,
1779 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001780 SkPDFFormXObject** dst) {
1781 *dst = NULL;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001782 if (clipRegion.isEmpty()) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001783 return NULL;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001784 }
1785
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001786 // The clip stack can come from an SkDraw where it is technically optional.
1787 SkClipStack synthesizedClipStack;
1788 if (clipStack == NULL) {
1789 if (clipRegion == fExistingClipRegion) {
1790 clipStack = &fExistingClipStack;
1791 } else {
1792 // GraphicStackState::updateClip expects the clip stack to have
1793 // fExistingClip as a prefix, so start there, then set the clip
1794 // to the passed region.
1795 synthesizedClipStack = fExistingClipStack;
1796 SkPath clipPath;
1797 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001798 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1799 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001800 clipStack = &synthesizedClipStack;
1801 }
1802 }
1803
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001804 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1805 if (paint.getXfermode()) {
1806 paint.getXfermode()->asMode(&xfermode);
1807 }
1808
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001809 // For the following modes, we want to handle source and destination
1810 // separately, so make an object of what's already there.
1811 if (xfermode == SkXfermode::kClear_Mode ||
1812 xfermode == SkXfermode::kSrc_Mode ||
1813 xfermode == SkXfermode::kSrcIn_Mode ||
1814 xfermode == SkXfermode::kDstIn_Mode ||
1815 xfermode == SkXfermode::kSrcOut_Mode ||
1816 xfermode == SkXfermode::kDstOut_Mode ||
1817 xfermode == SkXfermode::kSrcATop_Mode ||
1818 xfermode == SkXfermode::kDstATop_Mode ||
1819 xfermode == SkXfermode::kModulate_Mode) {
1820 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001821 *dst = createFormXObjectFromDevice();
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001822 SkASSERT(isContentEmpty());
1823 } else if (xfermode != SkXfermode::kSrc_Mode &&
1824 xfermode != SkXfermode::kSrcOut_Mode) {
1825 // Except for Src and SrcOut, if there isn't anything already there,
1826 // then we're done.
1827 return NULL;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001828 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001829 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001830 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001831 // Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001832
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001833 // Dst xfer mode doesn't draw source at all.
1834 if (xfermode == SkXfermode::kDst_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001835 return NULL;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001836 }
1837
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001838 ContentEntry* entry;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001839 SkAutoTDelete<ContentEntry> newEntry;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001840
1841 ContentEntry* lastContentEntry = getLastContentEntry();
1842 if (lastContentEntry && lastContentEntry->fContent.getOffset() == 0) {
1843 entry = lastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001844 } else {
1845 newEntry.reset(new ContentEntry);
1846 entry = newEntry.get();
1847 }
1848
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001849 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001850 hasText, &entry->fState);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001851 if (lastContentEntry && xfermode != SkXfermode::kDstOver_Mode &&
1852 entry->fState.compareInitialState(lastContentEntry->fState)) {
1853 return lastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001854 }
1855
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001856 SkAutoTDelete<ContentEntry>* contentEntries = getContentEntries();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001857 if (!lastContentEntry) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001858 contentEntries->reset(entry);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001859 setLastContentEntry(entry);
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001860 } else if (xfermode == SkXfermode::kDstOver_Mode) {
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001861 entry->fNext.reset(contentEntries->detach());
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001862 contentEntries->reset(entry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001863 } else {
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001864 lastContentEntry->fNext.reset(entry);
1865 setLastContentEntry(entry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001866 }
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001867 newEntry.detach();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001868 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001869}
1870
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001871void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001872 SkPDFFormXObject* dst,
1873 SkPath* shape) {
1874 if (xfermode != SkXfermode::kClear_Mode &&
1875 xfermode != SkXfermode::kSrc_Mode &&
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001876 xfermode != SkXfermode::kDstOver_Mode &&
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001877 xfermode != SkXfermode::kSrcIn_Mode &&
1878 xfermode != SkXfermode::kDstIn_Mode &&
1879 xfermode != SkXfermode::kSrcOut_Mode &&
1880 xfermode != SkXfermode::kDstOut_Mode &&
1881 xfermode != SkXfermode::kSrcATop_Mode &&
1882 xfermode != SkXfermode::kDstATop_Mode &&
1883 xfermode != SkXfermode::kModulate_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001884 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001885 return;
1886 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001887 if (xfermode == SkXfermode::kDstOver_Mode) {
1888 SkASSERT(!dst);
1889 ContentEntry* firstContentEntry = getContentEntries()->get();
1890 if (firstContentEntry->fContent.getOffset() == 0) {
1891 // For DstOver, an empty content entry was inserted before the rest
1892 // of the content entries. If nothing was drawn, it needs to be
1893 // removed.
1894 SkAutoTDelete<ContentEntry>* contentEntries = getContentEntries();
1895 contentEntries->reset(firstContentEntry->fNext.detach());
1896 }
1897 return;
1898 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001899 if (!dst) {
1900 SkASSERT(xfermode == SkXfermode::kSrc_Mode ||
1901 xfermode == SkXfermode::kSrcOut_Mode);
1902 return;
1903 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001904
1905 ContentEntry* contentEntries = getContentEntries()->get();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001906 SkASSERT(dst);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001907 SkASSERT(!contentEntries->fNext.get());
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001908 // Changing the current content into a form-xobject will destroy the clip
1909 // objects which is fine since the xobject will already be clipped. However
1910 // if source has shape, we need to clip it too, so a copy of the clip is
1911 // saved.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001912 SkClipStack clipStack = contentEntries->fState.fClipStack;
1913 SkRegion clipRegion = contentEntries->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001914
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001915 SkMatrix identity;
1916 identity.reset();
1917 SkPaint stockPaint;
1918
reed@google.comfc641d02012-09-20 17:52:20 +00001919 SkAutoTUnref<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001920 if (isContentEmpty()) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001921 // If nothing was drawn and there's no shape, then the draw was a
1922 // no-op, but dst needs to be restored for that to be true.
1923 // If there is shape, then an empty source with Src, SrcIn, SrcOut,
1924 // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop
1925 // reduces to Dst.
1926 if (shape == NULL || xfermode == SkXfermode::kDstOut_Mode ||
1927 xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001928 ScopedContentEntry content(this, &fExistingClipStack,
1929 fExistingClipRegion, identity,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001930 stockPaint);
1931 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1932 &content.entry()->fContent);
1933 return;
1934 } else {
1935 xfermode = SkXfermode::kClear_Mode;
1936 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001937 } else {
1938 SkASSERT(!fContentEntries->fNext.get());
reed@google.comfc641d02012-09-20 17:52:20 +00001939 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001940 }
1941
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001942 // TODO(vandebo) srcFormXObject may contain alpha, but here we want it
1943 // without alpha.
1944 if (xfermode == SkXfermode::kSrcATop_Mode) {
1945 // TODO(vandebo): In order to properly support SrcATop we have to track
1946 // the shape of what's been drawn at all times. It's the intersection of
1947 // the non-transparent parts of the device and the outlines (shape) of
1948 // all images and devices drawn.
1949 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001950 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001951 SkXfermode::kSrcOver_Mode, true);
1952 } else {
1953 SkAutoTUnref<SkPDFFormXObject> dstMaskStorage;
1954 SkPDFFormXObject* dstMask = srcFormXObject.get();
1955 if (shape != NULL) {
1956 // Draw shape into a form-xobject.
1957 SkDraw d;
1958 d.fMatrix = &identity;
1959 d.fClip = &clipRegion;
1960 d.fClipStack = &clipStack;
1961 SkPaint filledPaint;
1962 filledPaint.setColor(SK_ColorBLACK);
1963 filledPaint.setStyle(SkPaint::kFill_Style);
1964 this->drawPath(d, *shape, filledPaint, NULL, true);
1965
1966 dstMaskStorage.reset(createFormXObjectFromDevice());
1967 dstMask = dstMaskStorage.get();
1968 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001969 drawFormXObjectWithMask(addXObjectResource(dst), dstMask,
1970 &fExistingClipStack, fExistingClipRegion,
1971 SkXfermode::kSrcOver_Mode, true);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001972 }
1973
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001974 if (xfermode == SkXfermode::kClear_Mode) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001975 return;
1976 } else if (xfermode == SkXfermode::kSrc_Mode ||
1977 xfermode == SkXfermode::kDstATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001978 ScopedContentEntry content(this, &fExistingClipStack,
1979 fExistingClipRegion, identity, stockPaint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001980 if (content.entry()) {
1981 SkPDFUtils::DrawFormXObject(
1982 this->addXObjectResource(srcFormXObject.get()),
1983 &content.entry()->fContent);
1984 }
1985 if (xfermode == SkXfermode::kSrc_Mode) {
1986 return;
1987 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001988 } else if (xfermode == SkXfermode::kSrcATop_Mode) {
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00001989 ScopedContentEntry content(this, &fExistingClipStack,
1990 fExistingClipRegion, identity, stockPaint);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +00001991 if (content.entry()) {
1992 SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst),
1993 &content.entry()->fContent);
1994 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +00001995 }
1996
1997 SkASSERT(xfermode == SkXfermode::kSrcIn_Mode ||
1998 xfermode == SkXfermode::kDstIn_Mode ||
1999 xfermode == SkXfermode::kSrcOut_Mode ||
2000 xfermode == SkXfermode::kDstOut_Mode ||
2001 xfermode == SkXfermode::kSrcATop_Mode ||
2002 xfermode == SkXfermode::kDstATop_Mode ||
2003 xfermode == SkXfermode::kModulate_Mode);
2004
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002005 if (xfermode == SkXfermode::kSrcIn_Mode ||
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002006 xfermode == SkXfermode::kSrcOut_Mode ||
2007 xfermode == SkXfermode::kSrcATop_Mode) {
2008 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00002009 &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002010 SkXfermode::kSrcOver_Mode,
2011 xfermode == SkXfermode::kSrcOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002012 } else {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002013 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
2014 if (xfermode == SkXfermode::kModulate_Mode) {
2015 drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00002016 dst, &fExistingClipStack,
2017 fExistingClipRegion,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002018 SkXfermode::kSrcOver_Mode, false);
2019 mode = SkXfermode::kMultiply_Mode;
2020 }
2021 drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(),
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +00002022 &fExistingClipStack, fExistingClipRegion, mode,
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002023 xfermode == SkXfermode::kDstOut_Mode);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002024 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002025}
2026
vandebo@chromium.org481aef62011-05-24 16:39:05 +00002027bool SkPDFDevice::isContentEmpty() {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00002028 ContentEntry* contentEntries = getContentEntries()->get();
2029 if (!contentEntries || contentEntries->fContent.getOffset() == 0) {
2030 SkASSERT(!contentEntries || !contentEntries->fNext.get());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00002031 return true;
2032 }
2033 return false;
2034}
2035
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002036void SkPDFDevice::populateGraphicStateEntryFromPaint(
2037 const SkMatrix& matrix,
2038 const SkClipStack& clipStack,
2039 const SkRegion& clipRegion,
2040 const SkPaint& paint,
2041 bool hasText,
2042 GraphicStateEntry* entry) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002043 SkASSERT(paint.getPathEffect() == NULL);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002044
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002045 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false);
2046 NOT_IMPLEMENTED(paint.getColorFilter() != NULL, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002047
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002048 entry->fMatrix = matrix;
2049 entry->fClipStack = clipStack;
2050 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00002051 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
2052 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00002053
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002054 // PDF treats a shader as a color, so we only set one or the other.
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002055 SkAutoTUnref<SkPDFObject> pdfShader;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002056 const SkShader* shader = paint.getShader();
2057 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002058 if (shader) {
2059 // PDF positions patterns relative to the initial transform, so
2060 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002061 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00002062 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002063
2064 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002065 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002066 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00002067
2068 // We need to apply the initial transform to bounds in order to get
2069 // bounds in a consistent coordinate system.
2070 SkRect boundsTemp;
2071 boundsTemp.set(bounds);
2072 fInitialTransform.mapRect(&boundsTemp);
2073 boundsTemp.roundOut(&bounds);
2074
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002075 pdfShader.reset(SkPDFShader::GetPDFShader(*shader, transform, bounds));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002076
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00002077 if (pdfShader.get()) {
2078 // pdfShader has been canonicalized so we can directly compare
2079 // pointers.
2080 int resourceIndex = fShaderResources.find(pdfShader.get());
2081 if (resourceIndex < 0) {
2082 resourceIndex = fShaderResources.count();
2083 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002084 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00002085 }
2086 entry->fShaderIndex = resourceIndex;
2087 } else {
2088 // A color shader is treated as an invalid shader so we don't have
2089 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002090 SkShader::GradientInfo gradientInfo;
2091 SkColor gradientColor;
2092 gradientInfo.fColors = &gradientColor;
2093 gradientInfo.fColorOffsets = NULL;
2094 gradientInfo.fColorCount = 1;
2095 if (shader->asAGradient(&gradientInfo) ==
2096 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002097 entry->fColor = SkColorSetA(gradientColor, 0xFF);
2098 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002099 }
2100 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00002101 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002102
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002103 SkAutoTUnref<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002104 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002105 newGraphicState.reset(
2106 SkPDFGraphicState::GetGraphicStateForPaint(paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002107 } else {
2108 SkPaint newPaint = paint;
2109 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002110 newGraphicState.reset(
2111 SkPDFGraphicState::GetGraphicStateForPaint(newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002112 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002113 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002114 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002115
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00002116 if (hasText) {
2117 entry->fTextScaleX = paint.getTextScaleX();
2118 entry->fTextFill = paint.getStyle();
2119 } else {
2120 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002121 }
2122}
2123
vandebo@chromium.org6112c212011-05-13 03:50:38 +00002124int SkPDFDevice::addGraphicStateResource(SkPDFGraphicState* gs) {
2125 // Assumes that gs has been canonicalized (so we can directly compare
2126 // pointers).
2127 int result = fGraphicStateResources.find(gs);
2128 if (result < 0) {
2129 result = fGraphicStateResources.count();
2130 fGraphicStateResources.push(gs);
2131 gs->ref();
2132 }
2133 return result;
2134}
2135
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002136int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) {
2137 // Assumes that xobject has been canonicalized (so we can directly compare
2138 // pointers).
2139 int result = fXObjectResources.find(xObject);
2140 if (result < 0) {
2141 result = fXObjectResources.count();
2142 fXObjectResources.push(xObject);
2143 xObject->ref();
2144 }
2145 return result;
2146}
2147
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002148void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
2149 ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002150 SkTypeface* typeface = paint.getTypeface();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002151 if (contentEntry->fState.fFont == NULL ||
2152 contentEntry->fState.fTextSize != paint.getTextSize() ||
2153 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002154 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00002155 contentEntry->fContent.writeText("/");
2156 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
2157 SkPDFResourceDict::kFont_ResourceType,
2158 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002159 contentEntry->fContent.writeText(" ");
2160 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent);
2161 contentEntry->fContent.writeText(" Tf\n");
2162 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002163 }
2164}
2165
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00002166int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002167 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface,
2168 glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002169 int resourceIndex = fFontResources.find(newFont.get());
2170 if (resourceIndex < 0) {
2171 resourceIndex = fFontResources.count();
2172 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00002173 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00002174 }
2175 return resourceIndex;
2176}
2177
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002178void SkPDFDevice::internalDrawBitmap(const SkMatrix& origMatrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +00002179 const SkClipStack* clipStack,
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002180 const SkRegion& origClipRegion,
2181 const SkBitmap& origBitmap,
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +00002182 const SkIRect* srcRect,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002183 const SkPaint& paint) {
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002184 SkMatrix matrix = origMatrix;
2185 SkRegion perspectiveBounds;
2186 const SkRegion* clipRegion = &origClipRegion;
2187 SkBitmap perspectiveBitmap;
2188 const SkBitmap* bitmap = &origBitmap;
2189 SkBitmap tmpSubsetBitmap;
2190
2191 // Rasterize the bitmap using perspective in a new bitmap.
2192 if (origMatrix.hasPerspective()) {
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002193 if (fRasterDpi == 0) {
2194 return;
2195 }
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002196 SkBitmap* subsetBitmap;
2197 if (srcRect) {
2198 if (!origBitmap.extractSubset(&tmpSubsetBitmap, *srcRect)) {
2199 return;
2200 }
2201 subsetBitmap = &tmpSubsetBitmap;
2202 } else {
2203 subsetBitmap = &tmpSubsetBitmap;
2204 *subsetBitmap = origBitmap;
2205 }
2206 srcRect = NULL;
2207
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002208 // Transform the bitmap in the new space, without taking into
2209 // account the initial transform.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002210 SkPath perspectiveOutline;
2211 perspectiveOutline.addRect(
2212 SkRect::MakeWH(SkIntToScalar(subsetBitmap->width()),
2213 SkIntToScalar(subsetBitmap->height())));
2214 perspectiveOutline.transform(origMatrix);
2215
2216 // TODO(edisonn): perf - use current clip too.
2217 // Retrieve the bounds of the new shape.
2218 SkRect bounds = perspectiveOutline.getBounds();
2219
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002220 // Transform the bitmap in the new space, taking into
2221 // account the initial transform.
2222 SkMatrix total = origMatrix;
2223 total.postConcat(fInitialTransform);
2224 total.postScale(SkIntToScalar(fRasterDpi) /
2225 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE),
2226 SkIntToScalar(fRasterDpi) /
2227 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE));
2228 SkPath physicalPerspectiveOutline;
2229 physicalPerspectiveOutline.addRect(
2230 SkRect::MakeWH(SkIntToScalar(subsetBitmap->width()),
2231 SkIntToScalar(subsetBitmap->height())));
2232 physicalPerspectiveOutline.transform(total);
2233
2234 SkScalar scaleX = physicalPerspectiveOutline.getBounds().width() /
2235 bounds.width();
2236 SkScalar scaleY = physicalPerspectiveOutline.getBounds().height() /
2237 bounds.height();
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002238
2239 // TODO(edisonn): A better approach would be to use a bitmap shader
2240 // (in clamp mode) and draw a rect over the entire bounding box. Then
2241 // intersect perspectiveOutline to the clip. That will avoid introducing
2242 // alpha to the image while still giving good behavior at the edge of
2243 // the image. Avoiding alpha will reduce the pdf size and generation
2244 // CPU time some.
2245
reed@google.com9ebcac52014-01-24 18:53:42 +00002246 const int w = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().width());
2247 const int h = SkScalarCeilToInt(physicalPerspectiveOutline.getBounds().height());
2248 if (!perspectiveBitmap.allocPixels(SkImageInfo::MakeN32Premul(w, h))) {
2249 return;
2250 }
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002251 perspectiveBitmap.eraseColor(SK_ColorTRANSPARENT);
2252
2253 SkBitmapDevice device(perspectiveBitmap);
2254 SkCanvas canvas(&device);
2255
2256 SkScalar deltaX = bounds.left();
2257 SkScalar deltaY = bounds.top();
2258
2259 SkMatrix offsetMatrix = origMatrix;
2260 offsetMatrix.postTranslate(-deltaX, -deltaY);
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002261 offsetMatrix.postScale(scaleX, scaleY);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002262
2263 // Translate the draw in the new canvas, so we perfectly fit the
2264 // shape in the bitmap.
2265 canvas.setMatrix(offsetMatrix);
2266
2267 canvas.drawBitmap(*subsetBitmap, SkIntToScalar(0), SkIntToScalar(0));
2268
2269 // Make sure the final bits are in the bitmap.
2270 canvas.flush();
2271
edisonn@google.com73a7ea32013-11-11 20:55:15 +00002272 // In the new space, we use the identity matrix translated
2273 // and scaled to reflect DPI.
2274 matrix.setScale(1 / scaleX, 1 / scaleY);
2275 matrix.postTranslate(deltaX, deltaY);
2276
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002277 perspectiveBounds.setRect(
2278 SkIRect::MakeXYWH(SkScalarFloorToInt(bounds.x()),
2279 SkScalarFloorToInt(bounds.y()),
2280 SkScalarCeilToInt(bounds.width()),
2281 SkScalarCeilToInt(bounds.height())));
2282 clipRegion = &perspectiveBounds;
2283 srcRect = NULL;
2284 bitmap = &perspectiveBitmap;
2285 }
2286
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002287 SkMatrix scaled;
2288 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00002289 scaled.setScale(SK_Scalar1, -SK_Scalar1);
2290 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002291 // Scale the image up from 1x1 to WxH.
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002292 SkIRect subset = SkIRect::MakeWH(bitmap->width(), bitmap->height());
reed@google.coma6d59f62011-03-07 21:29:21 +00002293 scaled.postScale(SkIntToScalar(subset.width()),
2294 SkIntToScalar(subset.height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002295 scaled.postConcat(matrix);
edisonn@google.com9cf0cb12013-10-16 18:32:35 +00002296 ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002297 if (!content.entry() || (srcRect && !subset.intersect(*srcRect))) {
2298 return;
2299 }
2300 if (content.needShape()) {
2301 SkPath shape;
vandebo@chromium.orgfd3c8c22013-10-30 21:00:47 +00002302 shape.addRect(SkRect::MakeWH(SkIntToScalar(subset.width()),
2303 SkIntToScalar( subset.height())));
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002304 shape.transform(matrix);
2305 content.setShape(shape);
2306 }
2307 if (!content.needSource()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002308 return;
2309 }
2310
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002311 SkAutoTUnref<SkPDFImage> image(
2312 SkPDFImage::CreateImage(*bitmap, subset, fEncoder));
vandebo@chromium.org25adce82011-05-09 08:05:01 +00002313 if (!image) {
2314 return;
2315 }
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00002316
vandebo@chromium.org3b416212013-10-30 20:48:05 +00002317 SkPDFUtils::DrawFormXObject(this->addXObjectResource(image.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00002318 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002319}
reed@google.com982cb872011-12-07 18:34:08 +00002320
2321bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y,
2322 SkCanvas::Config8888) {
2323 return false;
2324}
reed@google.comb55deeb2012-01-06 14:43:09 +00002325
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00002326bool SkPDFDevice::allowImageFilter(const SkImageFilter*) {
reed@google.comb55deeb2012-01-06 14:43:09 +00002327 return false;
2328}