blob: d13d30cbfb46efbbbe29f6bc3f22522190af75de [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"
30#include "SkString.h"
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000031#include "SkTextFormatParams.h"
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +000032#include "SkTemplates.h"
reed@google.comfed86bd2013-03-14 15:04:57 +000033#include "SkTypefacePriv.h"
edisonn@google.com6addb192013-04-02 15:33:08 +000034#include "SkTSet.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000035
36// Utility functions
37
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000038static void emit_pdf_color(SkColor color, SkWStream* result) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000039 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
40 SkScalar colorMax = SkIntToScalar(0xFF);
vandebo@chromium.org094316b2011-03-04 03:15:13 +000041 SkPDFScalar::Append(
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000042 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result);
43 result->writeText(" ");
vandebo@chromium.org094316b2011-03-04 03:15:13 +000044 SkPDFScalar::Append(
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000045 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result);
46 result->writeText(" ");
vandebo@chromium.org094316b2011-03-04 03:15:13 +000047 SkPDFScalar::Append(
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000048 SkScalarDiv(SkIntToScalar(SkColorGetB(color)), colorMax), result);
49 result->writeText(" ");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000050}
51
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000052static SkPaint calculate_text_paint(const SkPaint& paint) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000053 SkPaint result = paint;
54 if (result.isFakeBoldText()) {
55 SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(),
56 kStdFakeBoldInterpKeys,
57 kStdFakeBoldInterpValues,
58 kStdFakeBoldInterpLength);
59 SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000060 if (result.getStyle() == SkPaint::kFill_Style) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000061 result.setStyle(SkPaint::kStrokeAndFill_Style);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000062 } else {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000063 width += result.getStrokeWidth();
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000064 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000065 result.setStrokeWidth(width);
66 }
67 return result;
68}
69
70// Stolen from measure_text in SkDraw.cpp and then tweaked.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000071static void align_text(SkDrawCacheProc glyphCacheProc, const SkPaint& paint,
bungeman@google.com9a87cee2011-08-23 17:02:18 +000072 const uint16_t* glyphs, size_t len,
73 SkScalar* x, SkScalar* y) {
74 if (paint.getTextAlign() == SkPaint::kLeft_Align) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000075 return;
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000076 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000077
78 SkMatrix ident;
79 ident.reset();
bungeman@google.com532470f2013-01-22 19:25:14 +000080 SkAutoGlyphCache autoCache(paint, NULL, &ident);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000081 SkGlyphCache* cache = autoCache.getCache();
82
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +000083 const char* start = reinterpret_cast<const char*>(glyphs);
84 const char* stop = reinterpret_cast<const char*>(glyphs + len);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000085 SkFixed xAdv = 0, yAdv = 0;
86
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000087 // TODO(vandebo): This probably needs to take kerning into account.
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000088 while (start < stop) {
89 const SkGlyph& glyph = glyphCacheProc(cache, &start, 0, 0);
90 xAdv += glyph.fAdvanceX;
91 yAdv += glyph.fAdvanceY;
92 };
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +000093 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 SkScalar xAdj = SkFixedToScalar(xAdv);
98 SkScalar yAdj = SkFixedToScalar(yAdv);
99 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
100 xAdj = SkScalarHalf(xAdj);
101 yAdj = SkScalarHalf(yAdj);
102 }
103 *x = *x - xAdj;
104 *y = *y - yAdj;
105}
106
reed@google.comfed86bd2013-03-14 15:04:57 +0000107static size_t max_glyphid_for_typeface(SkTypeface* typeface) {
108 SkAutoResolveDefaultTypeface autoResolve(typeface);
109 typeface = autoResolve.get();
commit-bot@chromium.org6a4ba5b2013-07-17 21:55:08 +0000110 return typeface->countGlyphs() - 1;
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +0000111}
112
113typedef SkAutoSTMalloc<128, uint16_t> SkGlyphStorage;
114
115static size_t force_glyph_encoding(const SkPaint& paint, const void* text,
116 size_t len, SkGlyphStorage* storage,
117 uint16_t** glyphIDs) {
118 // Make sure we have a glyph id encoding.
119 if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
120 size_t numGlyphs = paint.textToGlyphs(text, len, NULL);
121 storage->reset(numGlyphs);
122 paint.textToGlyphs(text, len, storage->get());
123 *glyphIDs = storage->get();
124 return numGlyphs;
125 }
126
127 // For user supplied glyph ids we need to validate them.
128 SkASSERT((len & 1) == 0);
129 size_t numGlyphs = len / 2;
130 const uint16_t* input =
131 reinterpret_cast<uint16_t*>(const_cast<void*>((text)));
132
133 int maxGlyphID = max_glyphid_for_typeface(paint.getTypeface());
134 size_t validated;
135 for (validated = 0; validated < numGlyphs; ++validated) {
136 if (input[validated] > maxGlyphID) {
137 break;
138 }
139 }
140 if (validated >= numGlyphs) {
141 *glyphIDs = reinterpret_cast<uint16_t*>(const_cast<void*>((text)));
142 return numGlyphs;
143 }
144
145 // Silently drop anything out of range.
146 storage->reset(numGlyphs);
147 if (validated > 0) {
148 memcpy(storage->get(), input, validated * sizeof(uint16_t));
149 }
150
151 for (size_t i = validated; i < numGlyphs; ++i) {
152 storage->get()[i] = input[i];
153 if (input[i] > maxGlyphID) {
154 storage->get()[i] = 0;
155 }
156 }
157 *glyphIDs = storage->get();
158 return numGlyphs;
159}
160
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000161static void set_text_transform(SkScalar x, SkScalar y, SkScalar textSkewX,
162 SkWStream* content) {
163 // Flip the text about the x-axis to account for origin swap and include
164 // the passed parameters.
165 content->writeText("1 0 ");
166 SkPDFScalar::Append(0 - textSkewX, content);
167 content->writeText(" -1 ");
168 SkPDFScalar::Append(x, content);
169 content->writeText(" ");
170 SkPDFScalar::Append(y, content);
171 content->writeText(" Tm\n");
172}
173
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000174// It is important to not confuse GraphicStateEntry with SkPDFGraphicState, the
175// later being our representation of an object in the PDF file.
176struct GraphicStateEntry {
177 GraphicStateEntry();
178
179 // Compare the fields we care about when setting up a new content entry.
180 bool compareInitialState(const GraphicStateEntry& b);
181
182 SkMatrix fMatrix;
183 // We can't do set operations on Paths, though PDF natively supports
184 // intersect. If the clip stack does anything other than intersect,
185 // we have to fall back to the region. Treat fClipStack as authoritative.
186 // See http://code.google.com/p/skia/issues/detail?id=221
187 SkClipStack fClipStack;
188 SkRegion fClipRegion;
189
190 // When emitting the content entry, we will ensure the graphic state
191 // is set to these values first.
192 SkColor fColor;
193 SkScalar fTextScaleX; // Zero means we don't care what the value is.
194 SkPaint::Style fTextFill; // Only if TextScaleX is non-zero.
195 int fShaderIndex;
196 int fGraphicStateIndex;
197
198 // We may change the font (i.e. for Type1 support) within a
199 // ContentEntry. This is the one currently in effect, or NULL if none.
200 SkPDFFont* fFont;
201 // In PDF, text size has no default value. It is only valid if fFont is
202 // not NULL.
203 SkScalar fTextSize;
204};
205
206GraphicStateEntry::GraphicStateEntry() : fColor(SK_ColorBLACK),
207 fTextScaleX(SK_Scalar1),
208 fTextFill(SkPaint::kFill_Style),
209 fShaderIndex(-1),
210 fGraphicStateIndex(-1),
211 fFont(NULL),
212 fTextSize(SK_ScalarNaN) {
213 fMatrix.reset();
214}
215
216bool GraphicStateEntry::compareInitialState(const GraphicStateEntry& b) {
217 return fColor == b.fColor &&
218 fShaderIndex == b.fShaderIndex &&
219 fGraphicStateIndex == b.fGraphicStateIndex &&
220 fMatrix == b.fMatrix &&
221 fClipStack == b.fClipStack &&
222 (fTextScaleX == 0 ||
223 b.fTextScaleX == 0 ||
224 (fTextScaleX == b.fTextScaleX && fTextFill == b.fTextFill));
225}
226
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000227class GraphicStackState {
228public:
229 GraphicStackState(const SkClipStack& existingClipStack,
230 const SkRegion& existingClipRegion,
231 SkWStream* contentStream)
232 : fStackDepth(0),
233 fContentStream(contentStream) {
234 fEntries[0].fClipStack = existingClipStack;
235 fEntries[0].fClipRegion = existingClipRegion;
236 }
237
238 void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000239 const SkPoint& translation);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000240 void updateMatrix(const SkMatrix& matrix);
241 void updateDrawingState(const GraphicStateEntry& state);
242
243 void drainStack();
244
245private:
246 void push();
247 void pop();
248 GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; }
249
250 // Conservative limit on save depth, see impl. notes in PDF 1.4 spec.
251 static const int kMaxStackDepth = 12;
252 GraphicStateEntry fEntries[kMaxStackDepth + 1];
253 int fStackDepth;
254 SkWStream* fContentStream;
255};
256
257void GraphicStackState::drainStack() {
258 while (fStackDepth) {
259 pop();
260 }
261}
262
263void GraphicStackState::push() {
264 SkASSERT(fStackDepth < kMaxStackDepth);
265 fContentStream->writeText("q\n");
266 fStackDepth++;
267 fEntries[fStackDepth] = fEntries[fStackDepth - 1];
268}
269
270void GraphicStackState::pop() {
271 SkASSERT(fStackDepth > 0);
272 fContentStream->writeText("Q\n");
273 fStackDepth--;
274}
275
robertphillips@google.com80214e22012-07-20 15:33:18 +0000276// This function initializes iter to be an iterator on the "stack" argument
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000277// and then skips over the leading entries as specified in prefix. It requires
278// and asserts that "prefix" will be a prefix to "stack."
279static void skip_clip_stack_prefix(const SkClipStack& prefix,
280 const SkClipStack& stack,
robertphillips@google.comc0290622012-07-16 21:20:03 +0000281 SkClipStack::Iter* iter) {
robertphillips@google.com80214e22012-07-20 15:33:18 +0000282 SkClipStack::B2TIter prefixIter(prefix);
283 iter->reset(stack, SkClipStack::Iter::kBottom_IterStart);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000284
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000285 const SkClipStack::Element* prefixEntry;
286 const SkClipStack::Element* iterEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000287
288 for (prefixEntry = prefixIter.next(); prefixEntry;
robertphillips@google.comc0290622012-07-16 21:20:03 +0000289 prefixEntry = prefixIter.next()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000290 iterEntry = iter->next();
291 SkASSERT(iterEntry);
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000292 // Because of SkClipStack does internal intersection, the last clip
293 // entry may differ.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000294 if (*prefixEntry != *iterEntry) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000295 SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op);
296 SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op);
297 SkASSERT(iterEntry->getType() == prefixEntry->getType());
robertphillips@google.comc0290622012-07-16 21:20:03 +0000298 // back up the iterator by one
299 iter->prev();
vandebo@chromium.org8887ede2011-05-25 01:27:52 +0000300 prefixEntry = prefixIter.next();
301 break;
302 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000303 }
304
305 SkASSERT(prefixEntry == NULL);
306}
307
308static void emit_clip(SkPath* clipPath, SkRect* clipRect,
309 SkWStream* contentStream) {
310 SkASSERT(clipPath || clipRect);
311
312 SkPath::FillType clipFill;
313 if (clipPath) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000314 SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000315 clipFill = clipPath->getFillType();
vandebo@chromium.org3e7b2802011-06-27 18:12:31 +0000316 } else {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000317 SkPDFUtils::AppendRectangle(*clipRect, contentStream);
318 clipFill = SkPath::kWinding_FillType;
319 }
320
321 NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false);
322 NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false);
323 if (clipFill == SkPath::kEvenOdd_FillType) {
324 contentStream->writeText("W* n\n");
325 } else {
326 contentStream->writeText("W n\n");
327 }
328}
329
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000330#ifdef SK_PDF_USE_PATHOPS
331/* Calculate an inverted path's equivalent non-inverted path, given the
332 * canvas bounds.
333 * outPath may alias with invPath (since this is supported by PathOps).
334 */
335static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath,
336 SkPath* outPath) {
337 SkASSERT(invPath.isInverseFillType());
338
339 SkPath clipPath;
340 clipPath.addRect(bounds);
341
342 return Op(clipPath, invPath, kIntersect_PathOp, outPath);
343}
344
345// Sanity check the numerical values of the SkRegion ops and PathOps ops
346// enums so region_op_to_pathops_op can do a straight passthrough cast.
347// If these are failing, it may be necessary to make region_op_to_pathops_op
348// do more.
349SK_COMPILE_ASSERT(SkRegion::kDifference_Op == (int)kDifference_PathOp,
350 region_pathop_mismatch);
351SK_COMPILE_ASSERT(SkRegion::kIntersect_Op == (int)kIntersect_PathOp,
352 region_pathop_mismatch);
353SK_COMPILE_ASSERT(SkRegion::kUnion_Op == (int)kUnion_PathOp,
354 region_pathop_mismatch);
355SK_COMPILE_ASSERT(SkRegion::kXOR_Op == (int)kXOR_PathOp,
356 region_pathop_mismatch);
357SK_COMPILE_ASSERT(SkRegion::kReverseDifference_Op ==
358 (int)kReverseDifference_PathOp,
359 region_pathop_mismatch);
360
361static SkPathOp region_op_to_pathops_op(SkRegion::Op op) {
362 SkASSERT(op >= 0);
363 SkASSERT(op <= SkRegion::kReverseDifference_Op);
364 return (SkPathOp)op;
365}
366
367/* Uses Path Ops to calculate a vector SkPath clip from a clip stack.
368 * Returns true if successful, or false if not successful.
369 * If successful, the resulting clip is stored in outClipPath.
370 * If not successful, outClipPath is undefined, and a fallback method
371 * should be used.
372 */
373static bool get_clip_stack_path(const SkMatrix& transform,
374 const SkClipStack& clipStack,
375 const SkRegion& clipRegion,
376 SkPath* outClipPath) {
377 outClipPath->reset();
378 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
379
380 const SkClipStack::Element* clipEntry;
381 SkClipStack::Iter iter;
382 iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart);
383 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
384 SkPath entryPath;
385 if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) {
386 outClipPath->reset();
387 outClipPath->setFillType(SkPath::kInverseWinding_FillType);
388 continue;
389 } else if (SkClipStack::Element::kRect_Type == clipEntry->getType()) {
390 entryPath.addRect(clipEntry->getRect());
391 } else if (SkClipStack::Element::kPath_Type == clipEntry->getType()) {
392 entryPath = clipEntry->getPath();
393 }
394 entryPath.transform(transform);
395
396 if (SkRegion::kReplace_Op == clipEntry->getOp()) {
397 *outClipPath = entryPath;
398 } else {
399 SkPathOp op = region_op_to_pathops_op(clipEntry->getOp());
400 if (!Op(*outClipPath, entryPath, op, outClipPath)) {
401 return false;
402 }
403 }
404 }
405
406 if (outClipPath->isInverseFillType()) {
407 // The bounds are slightly outset to ensure this is correct in the
408 // face of floating-point accuracy and possible SkRegion bitmap
409 // approximations.
410 SkRect clipBounds = SkRect::Make(clipRegion.getBounds());
411 clipBounds.outset(SK_Scalar1, SK_Scalar1);
412 if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) {
413 return false;
414 }
415 }
416 return true;
417}
418#endif
419
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000420// TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000421// graphic state stack, and the fact that we can know all the clips used
422// on the page to optimize this.
423void GraphicStackState::updateClip(const SkClipStack& clipStack,
424 const SkRegion& clipRegion,
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000425 const SkPoint& translation) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000426 if (clipStack == currentEntry()->fClipStack) {
427 return;
428 }
429
430 while (fStackDepth > 0) {
431 pop();
432 if (clipStack == currentEntry()->fClipStack) {
433 return;
434 }
435 }
436 push();
437
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +0000438 currentEntry()->fClipStack = clipStack;
439 currentEntry()->fClipRegion = clipRegion;
440
441 SkMatrix transform;
442 transform.setTranslate(translation.fX, translation.fY);
443
444#ifdef SK_PDF_USE_PATHOPS
445 SkPath clipPath;
446 if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) {
447 emit_clip(&clipPath, NULL, fContentStream);
448 return;
449 }
450#endif
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000451 // gsState->initialEntry()->fClipStack/Region specifies the clip that has
452 // already been applied. (If this is a top level device, then it specifies
453 // a clip to the content area. If this is a layer, then it specifies
454 // the clip in effect when the layer was created.) There's no need to
455 // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the
456 // initial clip on the parent layer. (This means there's a bug if the user
457 // expands the clip and then uses any xfer mode that uses dst:
458 // http://code.google.com/p/skia/issues/detail?id=228 )
robertphillips@google.comc0290622012-07-16 21:20:03 +0000459 SkClipStack::Iter iter;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000460 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
461
462 // If the clip stack does anything other than intersect or if it uses
463 // an inverse fill type, we have to fall back to the clip region.
464 bool needRegion = false;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000465 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000466 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000467 if (clipEntry->getOp() != SkRegion::kIntersect_Op || clipEntry->isInverseFilled()) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000468 needRegion = true;
469 break;
470 }
471 }
472
473 if (needRegion) {
474 SkPath clipPath;
475 SkAssertResult(clipRegion.getBoundaryPath(&clipPath));
476 emit_clip(&clipPath, NULL, fContentStream);
477 } else {
478 skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000479 const SkClipStack::Element* clipEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000480 for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000481 SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op);
482 switch (clipEntry->getType()) {
483 case SkClipStack::Element::kRect_Type: {
484 SkRect translatedClip;
485 transform.mapRect(&translatedClip, clipEntry->getRect());
486 emit_clip(NULL, &translatedClip, fContentStream);
487 break;
488 }
489 case SkClipStack::Element::kPath_Type: {
490 SkPath translatedPath;
491 clipEntry->getPath().transform(transform, &translatedPath);
492 emit_clip(&translatedPath, NULL, fContentStream);
493 break;
494 }
495 default:
496 SkASSERT(false);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000497 }
498 }
499 }
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000500}
501
502void GraphicStackState::updateMatrix(const SkMatrix& matrix) {
503 if (matrix == currentEntry()->fMatrix) {
504 return;
505 }
506
507 if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) {
508 SkASSERT(fStackDepth > 0);
509 SkASSERT(fEntries[fStackDepth].fClipStack ==
510 fEntries[fStackDepth -1].fClipStack);
511 pop();
512
513 SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask);
514 }
515 if (matrix.getType() == SkMatrix::kIdentity_Mask) {
516 return;
517 }
518
519 push();
520 SkPDFUtils::AppendTransform(matrix, fContentStream);
521 currentEntry()->fMatrix = matrix;
522}
523
524void GraphicStackState::updateDrawingState(const GraphicStateEntry& state) {
525 // PDF treats a shader as a color, so we only set one or the other.
526 if (state.fShaderIndex >= 0) {
527 if (state.fShaderIndex != currentEntry()->fShaderIndex) {
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000528 SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000529 currentEntry()->fShaderIndex = state.fShaderIndex;
530 }
531 } else {
532 if (state.fColor != currentEntry()->fColor ||
533 currentEntry()->fShaderIndex >= 0) {
534 emit_pdf_color(state.fColor, fContentStream);
535 fContentStream->writeText("RG ");
536 emit_pdf_color(state.fColor, fContentStream);
537 fContentStream->writeText("rg\n");
538 currentEntry()->fColor = state.fColor;
539 currentEntry()->fShaderIndex = -1;
540 }
541 }
542
543 if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000544 SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000545 currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex;
546 }
547
548 if (state.fTextScaleX) {
549 if (state.fTextScaleX != currentEntry()->fTextScaleX) {
550 SkScalar pdfScale = SkScalarMul(state.fTextScaleX,
551 SkIntToScalar(100));
552 SkPDFScalar::Append(pdfScale, fContentStream);
553 fContentStream->writeText(" Tz\n");
554 currentEntry()->fTextScaleX = state.fTextScaleX;
555 }
556 if (state.fTextFill != currentEntry()->fTextFill) {
557 SK_COMPILE_ASSERT(SkPaint::kFill_Style == 0, enum_must_match_value);
558 SK_COMPILE_ASSERT(SkPaint::kStroke_Style == 1,
559 enum_must_match_value);
560 SK_COMPILE_ASSERT(SkPaint::kStrokeAndFill_Style == 2,
561 enum_must_match_value);
562 fContentStream->writeDecAsText(state.fTextFill);
563 fContentStream->writeText(" Tr\n");
564 currentEntry()->fTextFill = state.fTextFill;
565 }
566 }
567}
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000568
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000569SkBaseDevice* SkPDFDevice::onCreateCompatibleDevice(SkBitmap::Config config,
570 int width, int height,
571 bool isOpaque,
572 Usage usage) {
bsalomon@google.come97f0852011-06-17 13:10:25 +0000573 SkMatrix initialTransform;
574 initialTransform.reset();
575 SkISize size = SkISize::Make(width, height);
576 return SkNEW_ARGS(SkPDFDevice, (size, size, initialTransform));
577}
578
579
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000580struct ContentEntry {
581 GraphicStateEntry fState;
582 SkDynamicMemoryWStream fContent;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000583 SkAutoTDelete<ContentEntry> fNext;
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000584
585 // If the stack is too deep we could get Stack Overflow.
586 // So we manually destruct the object.
587 ~ContentEntry() {
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000588 ContentEntry* val = fNext.detach();
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000589 while (val != NULL) {
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000590 ContentEntry* valNext = val->fNext.detach();
edisonn@google.com2e6a69b2013-02-05 23:13:39 +0000591 // When the destructor is called, fNext is NULL and exits.
592 delete val;
593 val = valNext;
594 }
595 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000596};
597
598// A helper class to automatically finish a ContentEntry at the end of a
599// drawing method and maintain the state needed between set up and finish.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000600class ScopedContentEntry {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000601public:
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000602 ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw,
603 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000604 : fDevice(device),
605 fContentEntry(NULL),
606 fXfermode(SkXfermode::kSrcOver_Mode) {
607 init(draw.fClipStack, *draw.fClip, *draw.fMatrix, paint, hasText);
608 }
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000609 ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack,
610 const SkRegion& clipRegion, const SkMatrix& matrix,
611 const SkPaint& paint, bool hasText = false)
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000612 : fDevice(device),
613 fContentEntry(NULL),
614 fXfermode(SkXfermode::kSrcOver_Mode) {
615 init(clipStack, clipRegion, matrix, paint, hasText);
616 }
617
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000618 ~ScopedContentEntry() {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000619 if (fContentEntry) {
reed@google.comfc641d02012-09-20 17:52:20 +0000620 fDevice->finishContentEntry(fXfermode, fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000621 }
reed@google.comfc641d02012-09-20 17:52:20 +0000622 SkSafeUnref(fDstFormXObject);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000623 }
624
625 ContentEntry* entry() { return fContentEntry; }
626private:
627 SkPDFDevice* fDevice;
628 ContentEntry* fContentEntry;
629 SkXfermode::Mode fXfermode;
reed@google.comfc641d02012-09-20 17:52:20 +0000630 SkPDFFormXObject* fDstFormXObject;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000631
632 void init(const SkClipStack* clipStack, const SkRegion& clipRegion,
633 const SkMatrix& matrix, const SkPaint& paint, bool hasText) {
reed@google.comfc641d02012-09-20 17:52:20 +0000634 fDstFormXObject = NULL;
edisonn@google.comaa6c4d22013-09-19 17:36:47 +0000635 if (matrix.hasPerspective() ||
636 (paint.getShader() &&
637 paint.getShader()->getLocalMatrix().hasPerspective())) {
638 // Just report that PDF does not supports perspective
639 // TODO(edisonn): update the shape when possible
640 // or dump in an image otherwise
641 NOT_IMPLEMENTED(true, false);
642 return;
643 }
644
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000645 if (paint.getXfermode()) {
646 paint.getXfermode()->asMode(&fXfermode);
647 }
648 fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion,
649 matrix, paint, hasText,
650 &fDstFormXObject);
651 }
652};
653
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000654////////////////////////////////////////////////////////////////////////////////
655
ctguil@chromium.org15261292011-04-29 17:54:16 +0000656static inline SkBitmap makeContentBitmap(const SkISize& contentSize,
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000657 const SkMatrix* initialTransform) {
reed@android.comf2b98d62010-12-20 18:26:13 +0000658 SkBitmap bitmap;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000659 if (initialTransform) {
660 // Compute the size of the drawing area.
661 SkVector drawingSize;
662 SkMatrix inverse;
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000663 drawingSize.set(SkIntToScalar(contentSize.fWidth),
664 SkIntToScalar(contentSize.fHeight));
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000665 if (!initialTransform->invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000666 // This shouldn't happen, initial transform should be invertible.
667 SkASSERT(false);
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000668 inverse.reset();
669 }
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000670 inverse.mapVectors(&drawingSize, 1);
671 SkISize size = SkSize::Make(drawingSize.fX, drawingSize.fY).toRound();
672 bitmap.setConfig(SkBitmap::kNo_Config, abs(size.fWidth),
673 abs(size.fHeight));
674 } else {
675 bitmap.setConfig(SkBitmap::kNo_Config, abs(contentSize.fWidth),
676 abs(contentSize.fHeight));
677 }
678
reed@android.comf2b98d62010-12-20 18:26:13 +0000679 return bitmap;
680}
681
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000682// TODO(vandebo) change pageSize to SkSize.
ctguil@chromium.org15261292011-04-29 17:54:16 +0000683SkPDFDevice::SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000684 const SkMatrix& initialTransform)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000685 : SkBitmapDevice(makeContentBitmap(contentSize, &initialTransform)),
ctguil@chromium.org15261292011-04-29 17:54:16 +0000686 fPageSize(pageSize),
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000687 fContentSize(contentSize),
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000688 fLastContentEntry(NULL),
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000689 fLastMarginContentEntry(NULL),
edisonn@google.comd9dfa182013-04-24 13:01:01 +0000690 fClipStack(NULL),
691 fEncoder(NULL) {
edisonn@google.comaa6c4d22013-09-19 17:36:47 +0000692 // just report that PDF does not supports perspective
693 // TODO(edisonn): update the shape when possible
694 // or dump in an image otherwise
695 NOT_IMPLEMENTED(initialTransform.hasPerspective(), true);
696
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000697 // Skia generally uses the top left as the origin but PDF natively has the
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000698 // origin at the bottom left. This matrix corrects for that. But that only
699 // needs to be done once, we don't do it when layering.
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000700 fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight));
701 fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000702 fInitialTransform.preConcat(initialTransform);
703
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000704 SkIRect existingClip = SkIRect::MakeWH(this->width(), this->height());
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000705 fExistingClipRegion.setRect(existingClip);
706
707 this->init();
708}
709
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000710// TODO(vandebo) change layerSize to SkSize.
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000711SkPDFDevice::SkPDFDevice(const SkISize& layerSize,
712 const SkClipStack& existingClipStack,
713 const SkRegion& existingClipRegion)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000714 : SkBitmapDevice(makeContentBitmap(layerSize, NULL)),
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000715 fPageSize(layerSize),
716 fContentSize(layerSize),
717 fExistingClipStack(existingClipStack),
718 fExistingClipRegion(existingClipRegion),
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000719 fLastContentEntry(NULL),
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000720 fLastMarginContentEntry(NULL),
721 fClipStack(NULL) {
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000722 fInitialTransform.reset();
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000723 this->init();
724}
725
726SkPDFDevice::~SkPDFDevice() {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000727 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000728}
729
730void SkPDFDevice::init() {
reed@google.com2a006c12012-09-19 17:05:55 +0000731 fAnnotations = NULL;
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000732 fResourceDict = NULL;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000733 fContentEntries.free();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000734 fLastContentEntry = NULL;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000735 fMarginContentEntries.free();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000736 fLastMarginContentEntry = NULL;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000737 fDrawingArea = kContent_DrawingArea;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +0000738 if (fFontGlyphUsage.get() == NULL) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000739 fFontGlyphUsage.reset(new SkPDFGlyphSetMap());
740 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000741}
742
vandebo@chromium.org98594282011-07-25 22:34:12 +0000743void SkPDFDevice::cleanUp(bool clearFontUsage) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000744 fGraphicStateResources.unrefAll();
745 fXObjectResources.unrefAll();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000746 fFontResources.unrefAll();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000747 fShaderResources.unrefAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000748 SkSafeUnref(fAnnotations);
reed@google.comfc641d02012-09-20 17:52:20 +0000749 SkSafeUnref(fResourceDict);
epoger@google.comb58772f2013-03-08 09:09:10 +0000750 fNamedDestinations.deleteAll();
reed@google.com2a006c12012-09-19 17:05:55 +0000751
vandebo@chromium.org98594282011-07-25 22:34:12 +0000752 if (clearFontUsage) {
753 fFontGlyphUsage->reset();
754 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000755}
756
reed@google.com982cb872011-12-07 18:34:08 +0000757uint32_t SkPDFDevice::getDeviceCapabilities() {
758 return kVector_Capability;
759}
760
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000761void SkPDFDevice::clear(SkColor color) {
vandebo@chromium.org98594282011-07-25 22:34:12 +0000762 this->cleanUp(true);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000763 this->init();
764
765 SkPaint paint;
766 paint.setColor(color);
767 paint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000768 SkMatrix identity;
769 identity.reset();
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000770 ScopedContentEntry content(this, &fExistingClipStack, fExistingClipRegion,
771 identity, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000772 internalDrawPaint(paint, content.entry());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000773}
774
775void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000776 SkPaint newPaint = paint;
777 newPaint.setStyle(SkPaint::kFill_Style);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000778 ScopedContentEntry content(this, d, newPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000779 internalDrawPaint(newPaint, content.entry());
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000780}
781
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000782void SkPDFDevice::internalDrawPaint(const SkPaint& paint,
783 ContentEntry* contentEntry) {
784 if (!contentEntry) {
785 return;
786 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000787 SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()),
788 SkIntToScalar(this->height()));
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000789 SkMatrix inverse;
commit-bot@chromium.orgd2cfa742013-09-20 18:58:30 +0000790 if (!contentEntry->fState.fMatrix.invert(&inverse)) {
vandebo@chromium.org386dfc02012-04-17 22:31:52 +0000791 return;
vandebo@chromium.orgb0549902012-04-13 20:45:46 +0000792 }
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000793 inverse.mapRect(&bbox);
794
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000795 SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000796 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000797 &contentEntry->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000798}
799
800void SkPDFDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
801 size_t count, const SkPoint* points,
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000802 const SkPaint& passedPaint) {
803 if (count == 0) {
804 return;
805 }
806
epoger@google.comb58772f2013-03-08 09:09:10 +0000807 if (handlePointAnnotation(points, count, *d.fMatrix, passedPaint)) {
808 return;
809 }
810
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000811 // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath.
812 // We only use this when there's a path effect because of the overhead
813 // of multiple calls to setUpContentEntry it causes.
814 if (passedPaint.getPathEffect()) {
815 if (d.fClip->isEmpty()) {
816 return;
817 }
818 SkDraw pointDraw(d);
819 pointDraw.fDevice = this;
820 pointDraw.drawPoints(mode, count, points, passedPaint, true);
821 return;
822 }
823
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000824 const SkPaint* paint = &passedPaint;
825 SkPaint modifiedPaint;
826
827 if (mode == SkCanvas::kPoints_PointMode &&
828 paint->getStrokeCap() != SkPaint::kRound_Cap) {
829 modifiedPaint = *paint;
830 paint = &modifiedPaint;
831 if (paint->getStrokeWidth()) {
832 // PDF won't draw a single point with square/butt caps because the
833 // orientation is ambiguous. Draw a rectangle instead.
834 modifiedPaint.setStyle(SkPaint::kFill_Style);
835 SkScalar strokeWidth = paint->getStrokeWidth();
836 SkScalar halfStroke = SkScalarHalf(strokeWidth);
837 for (size_t i = 0; i < count; i++) {
838 SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0);
839 r.inset(-halfStroke, -halfStroke);
840 drawRect(d, r, modifiedPaint);
841 }
842 return;
843 } else {
844 modifiedPaint.setStrokeCap(SkPaint::kRound_Cap);
845 }
846 }
847
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000848 ScopedContentEntry content(this, d, *paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000849 if (!content.entry()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000850 return;
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +0000851 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000852
853 switch (mode) {
854 case SkCanvas::kPolygon_PointMode:
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000855 SkPDFUtils::MoveTo(points[0].fX, points[0].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000856 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000857 for (size_t i = 1; i < count; i++) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000858 SkPDFUtils::AppendLine(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000859 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000860 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000861 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000862 break;
863 case SkCanvas::kLines_PointMode:
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000864 for (size_t i = 0; i < count/2; i++) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000865 SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000866 &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000867 SkPDFUtils::AppendLine(points[i * 2 + 1].fX,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000868 points[i * 2 + 1].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000869 &content.entry()->fContent);
870 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000871 }
872 break;
873 case SkCanvas::kPoints_PointMode:
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000874 SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap);
875 for (size_t i = 0; i < count; i++) {
876 SkPDFUtils::MoveTo(points[i].fX, points[i].fY,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000877 &content.entry()->fContent);
878 SkPDFUtils::ClosePath(&content.entry()->fContent);
879 SkPDFUtils::StrokePath(&content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000880 }
881 break;
882 default:
883 SkASSERT(false);
884 }
885}
886
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000887void SkPDFDevice::drawRect(const SkDraw& d, const SkRect& rect,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000888 const SkPaint& paint) {
commit-bot@chromium.org969fd6a2013-05-14 18:16:40 +0000889 SkRect r = rect;
890 r.sort();
891
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000892 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000893 if (d.fClip->isEmpty()) {
894 return;
895 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000896 SkPath path;
897 path.addRect(r);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000898 drawPath(d, path, paint, NULL, true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000899 return;
900 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000901
epoger@google.comb58772f2013-03-08 09:09:10 +0000902 if (handleRectAnnotation(r, *d.fMatrix, paint)) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +0000903 return;
904 }
905
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000906 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000907 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000908 return;
909 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000910 SkPDFUtils::AppendRectangle(r, &content.entry()->fContent);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000911 SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000912 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000913}
914
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000915void SkPDFDevice::drawPath(const SkDraw& d, const SkPath& origPath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000916 const SkPaint& paint, const SkMatrix* prePathMatrix,
917 bool pathIsMutable) {
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000918 SkPath modifiedPath;
919 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
920
921 SkMatrix matrix = *d.fMatrix;
922 if (prePathMatrix) {
923 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
924 if (!pathIsMutable) {
925 pathPtr = &modifiedPath;
926 pathIsMutable = true;
927 }
928 origPath.transform(*prePathMatrix, pathPtr);
929 } else {
930 if (!matrix.preConcat(*prePathMatrix)) {
edisonn@google.comaa6c4d22013-09-19 17:36:47 +0000931 // TODO(edisonn): report somehow why we failed?
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000932 return;
933 }
934 }
935 }
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +0000936
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000937 if (paint.getPathEffect()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000938 if (d.fClip->isEmpty()) {
939 return;
940 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000941 if (!pathIsMutable) {
942 pathPtr = &modifiedPath;
943 pathIsMutable = true;
944 }
945 bool fill = paint.getFillPath(origPath, pathPtr);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000946
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000947 SkPaint noEffectPaint(paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000948 noEffectPaint.setPathEffect(NULL);
949 if (fill) {
950 noEffectPaint.setStyle(SkPaint::kFill_Style);
951 } else {
952 noEffectPaint.setStyle(SkPaint::kStroke_Style);
953 noEffectPaint.setStrokeWidth(0);
954 }
955 drawPath(d, *pathPtr, noEffectPaint, NULL, true);
vandebo@chromium.org7d71f7f2010-10-26 19:51:44 +0000956 return;
957 }
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000958
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +0000959#ifdef SK_PDF_USE_PATHOPS
960 if (handleInversePath(d, origPath, paint, pathIsMutable)) {
961 return;
962 }
963#endif
964
epoger@google.comb58772f2013-03-08 09:09:10 +0000965 if (handleRectAnnotation(pathPtr->getBounds(), *d.fMatrix, paint)) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +0000966 return;
967 }
968
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000969 ScopedContentEntry content(this, d, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000970 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000971 return;
972 }
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000973 SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(),
974 &content.entry()->fContent);
vandebo@chromium.orgff390322011-05-17 18:58:44 +0000975 SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000976 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000977}
978
edisonn@google.com2ae67e72013-02-12 01:06:38 +0000979void SkPDFDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
980 const SkRect* src, const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000981 const SkPaint& paint,
982 SkCanvas::DrawBitmapRectFlags flags) {
983 // TODO: this code path must be updated to respect the flags parameter
edisonn@google.com2ae67e72013-02-12 01:06:38 +0000984 SkMatrix matrix;
985 SkRect bitmapBounds, tmpSrc, tmpDst;
986 SkBitmap tmpBitmap;
987
988 bitmapBounds.isetWH(bitmap.width(), bitmap.height());
989
990 // Compute matrix from the two rectangles
991 if (src) {
992 tmpSrc = *src;
993 } else {
994 tmpSrc = bitmapBounds;
995 }
996 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
997
998 const SkBitmap* bitmapPtr = &bitmap;
999
1000 // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
1001 // needed (if the src was clipped). No check needed if src==null.
1002 if (src) {
1003 if (!bitmapBounds.contains(*src)) {
1004 if (!tmpSrc.intersect(bitmapBounds)) {
1005 return; // nothing to draw
1006 }
1007 // recompute dst, based on the smaller tmpSrc
1008 matrix.mapRect(&tmpDst, tmpSrc);
1009 }
1010
1011 // since we may need to clamp to the borders of the src rect within
1012 // the bitmap, we extract a subset.
1013 // TODO: make sure this is handled in drawBitmap and remove from here.
1014 SkIRect srcIR;
1015 tmpSrc.roundOut(&srcIR);
1016 if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
1017 return;
1018 }
1019 bitmapPtr = &tmpBitmap;
1020
1021 // Since we did an extract, we need to adjust the matrix accordingly
1022 SkScalar dx = 0, dy = 0;
1023 if (srcIR.fLeft > 0) {
1024 dx = SkIntToScalar(srcIR.fLeft);
1025 }
1026 if (srcIR.fTop > 0) {
1027 dy = SkIntToScalar(srcIR.fTop);
1028 }
1029 if (dx || dy) {
1030 matrix.preTranslate(dx, dy);
1031 }
1032 }
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001033 this->drawBitmap(draw, *bitmapPtr, matrix, paint);
edisonn@google.com2ae67e72013-02-12 01:06:38 +00001034}
1035
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001036void SkPDFDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001037 const SkMatrix& matrix, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001038 if (d.fClip->isEmpty()) {
1039 return;
1040 }
1041
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001042 SkMatrix transform = matrix;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001043 transform.postConcat(*d.fMatrix);
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001044 this->internalDrawBitmap(transform, d.fClipStack, *d.fClip, bitmap, NULL, paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001045}
1046
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001047void SkPDFDevice::drawSprite(const SkDraw& d, const SkBitmap& bitmap,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001048 int x, int y, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001049 if (d.fClip->isEmpty()) {
1050 return;
1051 }
1052
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001053 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001054 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001055 this->internalDrawBitmap(matrix, d.fClipStack, *d.fClip, bitmap, NULL, paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001056}
1057
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001058void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001059 SkScalar x, SkScalar y, const SkPaint& paint) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001060 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false);
1061 if (paint.getMaskFilter() != NULL) {
1062 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1063 // making text unreadable (e.g. same text twice when using CSS shadows).
1064 return;
1065 }
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001066 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001067 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001068 if (!content.entry()) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001069 return;
1070 }
1071
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001072 SkGlyphStorage storage(0);
1073 uint16_t* glyphIDs = NULL;
1074 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage,
1075 &glyphIDs);
1076 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001077
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001078 SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc();
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001079 align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001080 content.entry()->fContent.writeText("BT\n");
1081 set_text_transform(x, y, textPaint.getTextSkewX(),
1082 &content.entry()->fContent);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001083 size_t consumedGlyphCount = 0;
1084 while (numGlyphs > consumedGlyphCount) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001085 updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry());
1086 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001087 size_t availableGlyphs =
1088 font->glyphsToPDFFontEncoding(glyphIDs + consumedGlyphCount,
1089 numGlyphs - consumedGlyphCount);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001090 fFontGlyphUsage->noteGlyphUsage(font, glyphIDs + consumedGlyphCount,
1091 availableGlyphs);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001092 SkString encodedString =
reed@google.comf6c3ebd2011-07-20 17:20:28 +00001093 SkPDFString::FormatString(glyphIDs + consumedGlyphCount,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001094 availableGlyphs, font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001095 content.entry()->fContent.writeText(encodedString.c_str());
vandebo@chromium.org01294102011-02-28 19:52:18 +00001096 consumedGlyphCount += availableGlyphs;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001097 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001098 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001099 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001100}
1101
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001102void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001103 const SkScalar pos[], SkScalar constY,
1104 int scalarsPerPos, const SkPaint& paint) {
edisonn@google.comb62f93f2013-03-24 18:05:10 +00001105 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false);
1106 if (paint.getMaskFilter() != NULL) {
1107 // Don't pretend we support drawing MaskFilters, it makes for artifacts
1108 // making text unreadable (e.g. same text twice when using CSS shadows).
1109 return;
1110 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001111 SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001112 SkPaint textPaint = calculate_text_paint(paint);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001113 ScopedContentEntry content(this, d, textPaint, true);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001114 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001115 return;
1116 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001117
vandebo@chromium.org4e1cc6a2013-01-25 19:27:23 +00001118 SkGlyphStorage storage(0);
1119 uint16_t* glyphIDs = NULL;
1120 size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage,
1121 &glyphIDs);
1122 textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001123
1124 SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001125 content.entry()->fContent.writeText("BT\n");
1126 updateFont(textPaint, glyphIDs[0], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001127 for (size_t i = 0; i < numGlyphs; i++) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001128 SkPDFFont* font = content.entry()->fState.fFont;
vandebo@chromium.org01294102011-02-28 19:52:18 +00001129 uint16_t encodedValue = glyphIDs[i];
1130 if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001131 updateFont(textPaint, glyphIDs[i], content.entry());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001132 i--;
1133 continue;
1134 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001135 fFontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001136 SkScalar x = pos[i * scalarsPerPos];
1137 SkScalar y = scalarsPerPos == 1 ? constY : pos[i * scalarsPerPos + 1];
bungeman@google.com9a87cee2011-08-23 17:02:18 +00001138 align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001139 set_text_transform(x, y, textPaint.getTextSkewX(),
1140 &content.entry()->fContent);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001141 SkString encodedString =
reed@google.comf6c3ebd2011-07-20 17:20:28 +00001142 SkPDFString::FormatString(&encodedValue, 1,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +00001143 font->multiByteGlyphs());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001144 content.entry()->fContent.writeText(encodedString.c_str());
1145 content.entry()->fContent.writeText(" Tj\n");
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001146 }
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001147 content.entry()->fContent.writeText("ET\n");
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001148}
1149
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001150void SkPDFDevice::drawTextOnPath(const SkDraw& d, const void* text, size_t len,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001151 const SkPath& path, const SkMatrix* matrix,
1152 const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001153 if (d.fClip->isEmpty()) {
1154 return;
1155 }
vandebo@chromium.org290e3bb2011-11-08 23:42:53 +00001156 d.drawTextOnPath((const char*)text, len, path, matrix, paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001157}
1158
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001159void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001160 int vertexCount, const SkPoint verts[],
1161 const SkPoint texs[], const SkColor colors[],
1162 SkXfermode* xmode, const uint16_t indices[],
1163 int indexCount, const SkPaint& paint) {
vandebo@chromium.orgfb0b0ed2011-04-15 20:01:17 +00001164 if (d.fClip->isEmpty()) {
1165 return;
1166 }
vandebo@chromium.orga5180862010-10-26 19:48:49 +00001167 NOT_IMPLEMENTED("drawVerticies", true);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001168}
1169
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001170void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device, int x, int y,
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001171 const SkPaint& paint) {
1172 if ((device->getDeviceCapabilities() & kVector_Capability) == 0) {
1173 // If we somehow get a raster device, do what our parent would do.
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001174 INHERITED::drawDevice(d, device, x, y, paint);
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001175 return;
1176 }
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001177
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001178 // Assume that a vector capable device means that it's a PDF Device.
1179 SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);
ctguil@chromium.orgf4ff39c2011-05-24 19:55:05 +00001180 if (pdfDevice->isContentEmpty()) {
vandebo@chromium.orgee7a9562011-05-24 17:38:01 +00001181 return;
1182 }
1183
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001184 SkMatrix matrix;
reed@google.coma6d59f62011-03-07 21:29:21 +00001185 matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y));
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001186 ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001187 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001188 return;
1189 }
vandebo@chromium.org1aef2ed2011-02-03 21:46:10 +00001190
1191 SkPDFFormXObject* xobject = new SkPDFFormXObject(pdfDevice);
vandebo@chromium.orgeb6c7592010-10-26 19:54:45 +00001192 fXObjectResources.push(xobject); // Transfer reference.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001193 SkPDFUtils::DrawFormXObject(fXObjectResources.count() - 1,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001194 &content.entry()->fContent);
vandebo@chromium.org98594282011-07-25 22:34:12 +00001195
1196 // Merge glyph sets from the drawn device.
1197 fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage());
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001198}
1199
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001200void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) {
1201 INHERITED::onAttachToCanvas(canvas);
1202
1203 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
1204 fClipStack = canvas->getClipStack();
1205}
1206
1207void SkPDFDevice::onDetachFromCanvas() {
1208 INHERITED::onDetachFromCanvas();
1209
1210 fClipStack = NULL;
1211}
1212
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001213ContentEntry* SkPDFDevice::getLastContentEntry() {
1214 if (fDrawingArea == kContent_DrawingArea) {
1215 return fLastContentEntry;
1216 } else {
1217 return fLastMarginContentEntry;
1218 }
1219}
1220
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001221SkAutoTDelete<ContentEntry>* SkPDFDevice::getContentEntries() {
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001222 if (fDrawingArea == kContent_DrawingArea) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001223 return &fContentEntries;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001224 } else {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001225 return &fMarginContentEntries;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001226 }
1227}
1228
1229void SkPDFDevice::setLastContentEntry(ContentEntry* contentEntry) {
1230 if (fDrawingArea == kContent_DrawingArea) {
1231 fLastContentEntry = contentEntry;
1232 } else {
1233 fLastMarginContentEntry = contentEntry;
1234 }
1235}
1236
1237void SkPDFDevice::setDrawingArea(DrawingArea drawingArea) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001238 // A ScopedContentEntry only exists during the course of a draw call, so
1239 // this can't be called while a ScopedContentEntry exists.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001240 fDrawingArea = drawingArea;
1241}
1242
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001243SkPDFResourceDict* SkPDFDevice::getResourceDict() {
reed@google.comfc641d02012-09-20 17:52:20 +00001244 if (NULL == fResourceDict) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001245 fResourceDict = SkNEW(SkPDFResourceDict);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001246
1247 if (fGraphicStateResources.count()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001248 for (int i = 0; i < fGraphicStateResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001249 fResourceDict->insertResourceAsReference(
1250 SkPDFResourceDict::kExtGState_ResourceType,
1251 i, fGraphicStateResources[i]);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001252 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001253 }
1254
1255 if (fXObjectResources.count()) {
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001256 for (int i = 0; i < fXObjectResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001257 fResourceDict->insertResourceAsReference(
1258 SkPDFResourceDict::kXObject_ResourceType,
1259 i, fXObjectResources[i]);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001260 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001261 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001262
1263 if (fFontResources.count()) {
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001264 for (int i = 0; i < fFontResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001265 fResourceDict->insertResourceAsReference(
1266 SkPDFResourceDict::kFont_ResourceType,
1267 i, fFontResources[i]);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001268 }
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001269 }
1270
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001271 if (fShaderResources.count()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001272 SkAutoTUnref<SkPDFDict> patterns(new SkPDFDict());
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001273 for (int i = 0; i < fShaderResources.count(); i++) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001274 fResourceDict->insertResourceAsReference(
1275 SkPDFResourceDict::kPattern_ResourceType,
1276 i, fShaderResources[i]);
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001277 }
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001278 }
vandebo@chromium.orgfc166672013-07-22 18:31:24 +00001279 }
1280 return fResourceDict;
1281}
1282
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +00001283const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const {
1284 return fFontResources;
1285}
1286
reed@google.com2a006c12012-09-19 17:05:55 +00001287SkPDFArray* SkPDFDevice::copyMediaBox() const {
1288 // should this be a singleton?
1289 SkAutoTUnref<SkPDFInt> zero(SkNEW_ARGS(SkPDFInt, (0)));
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +00001290
reed@google.com2a006c12012-09-19 17:05:55 +00001291 SkPDFArray* mediaBox = SkNEW(SkPDFArray);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001292 mediaBox->reserve(4);
1293 mediaBox->append(zero.get());
1294 mediaBox->append(zero.get());
reed@google.comc789cf12011-07-20 12:14:33 +00001295 mediaBox->appendInt(fPageSize.fWidth);
1296 mediaBox->appendInt(fPageSize.fHeight);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001297 return mediaBox;
1298}
1299
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001300SkStream* SkPDFDevice::content() const {
reed@google.com5667afc2011-06-27 14:42:15 +00001301 SkMemoryStream* result = new SkMemoryStream;
1302 result->setData(this->copyContentToData())->unref();
1303 return result;
1304}
1305
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001306void SkPDFDevice::copyContentEntriesToData(ContentEntry* entry,
1307 SkWStream* data) const {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001308 // TODO(ctguil): For margins, I'm not sure fExistingClipStack/Region is the
1309 // right thing to pass here.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001310 GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, data);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001311 while (entry != NULL) {
vandebo@chromium.org663515b2012-01-05 18:45:27 +00001312 SkPoint translation;
1313 translation.iset(this->getOrigin());
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001314 translation.negate();
1315 gsState.updateClip(entry->fState.fClipStack, entry->fState.fClipRegion,
1316 translation);
1317 gsState.updateMatrix(entry->fState.fMatrix);
1318 gsState.updateDrawingState(entry->fState);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001319
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001320 SkAutoDataUnref copy(entry->fContent.copyToData());
robertphillips@google.com59f46b82012-07-10 17:30:58 +00001321 data->write(copy->data(), copy->size());
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001322 entry = entry->fNext.get();
1323 }
1324 gsState.drainStack();
1325}
1326
reed@google.com5667afc2011-06-27 14:42:15 +00001327SkData* SkPDFDevice::copyContentToData() const {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001328 SkDynamicMemoryWStream data;
1329 if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) {
1330 SkPDFUtils::AppendTransform(fInitialTransform, &data);
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +00001331 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001332
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001333 // TODO(aayushkumar): Apply clip along the margins. Currently, webkit
1334 // colors the contentArea white before it starts drawing into it and
1335 // that currently acts as our clip.
1336 // Also, think about adding a transform here (or assume that the values
1337 // sent across account for that)
1338 SkPDFDevice::copyContentEntriesToData(fMarginContentEntries.get(), &data);
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001339
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001340 // If the content area is the entire page, then we don't need to clip
1341 // the content area (PDF area clips to the page size). Otherwise,
1342 // we have to clip to the content area; we've already applied the
1343 // initial transform, so just clip to the device size.
1344 if (fPageSize != fContentSize) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001345 SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()),
1346 SkIntToScalar(this->height()));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001347 emit_clip(NULL, &r, &data);
1348 }
vandebo@chromium.org98594282011-07-25 22:34:12 +00001349
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001350 SkPDFDevice::copyContentEntriesToData(fContentEntries.get(), &data);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001351
reed@google.com5667afc2011-06-27 14:42:15 +00001352 // potentially we could cache this SkData, and only rebuild it if we
1353 // see that our state has changed.
1354 return data.copyToData();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001355}
1356
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +00001357#ifdef SK_PDF_USE_PATHOPS
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001358/* Draws an inverse filled path by using Path Ops to compute the positive
1359 * inverse using the current clip as the inverse bounds.
1360 * Return true if this was an inverse path and was properly handled,
1361 * otherwise returns false and the normal drawing routine should continue,
1362 * either as a (incorrect) fallback or because the path was not inverse
1363 * in the first place.
1364 */
1365bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath,
1366 const SkPaint& paint, bool pathIsMutable) {
1367 if (!origPath.isInverseFillType()) {
1368 return false;
1369 }
1370
1371 if (d.fClip->isEmpty()) {
1372 return false;
1373 }
1374
1375 SkPath modifiedPath;
1376 SkPath* pathPtr = const_cast<SkPath*>(&origPath);
1377 SkPaint noInversePaint(paint);
1378
1379 // Merge stroking operations into final path.
1380 if (SkPaint::kStroke_Style == paint.getStyle() ||
1381 SkPaint::kStrokeAndFill_Style == paint.getStyle()) {
1382 bool doFillPath = paint.getFillPath(origPath, &modifiedPath);
1383 if (doFillPath) {
1384 noInversePaint.setStyle(SkPaint::kFill_Style);
1385 noInversePaint.setStrokeWidth(0);
1386 pathPtr = &modifiedPath;
1387 } else {
1388 // To be consistent with the raster output, hairline strokes
1389 // are rendered as non-inverted.
1390 modifiedPath.toggleInverseFillType();
1391 drawPath(d, modifiedPath, paint, NULL, true);
1392 return true;
1393 }
1394 }
1395
1396 // Get bounds of clip in current transform space
1397 // (clip bounds are given in device space).
1398 SkRect bounds;
1399 SkMatrix transformInverse;
1400 if (!d.fMatrix->invert(&transformInverse)) {
1401 return false;
1402 }
1403 bounds.set(d.fClip->getBounds());
1404 transformInverse.mapRect(&bounds);
1405
1406 // Extend the bounds by the line width (plus some padding)
1407 // so the edge doesn't cause a visible stroke.
1408 bounds.outset(paint.getStrokeWidth() + SK_Scalar1,
1409 paint.getStrokeWidth() + SK_Scalar1);
1410
1411 if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) {
1412 return false;
1413 }
1414
1415 drawPath(d, modifiedPath, noInversePaint, NULL, true);
1416 return true;
1417}
commit-bot@chromium.orgd2623a12013-08-08 02:52:05 +00001418#endif
commit-bot@chromium.org92ffe7d2013-07-31 22:54:31 +00001419
epoger@google.comb58772f2013-03-08 09:09:10 +00001420bool SkPDFDevice::handleRectAnnotation(const SkRect& r, const SkMatrix& matrix,
1421 const SkPaint& p) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001422 SkAnnotation* annotationInfo = p.getAnnotation();
1423 if (!annotationInfo) {
1424 return false;
1425 }
1426 SkData* urlData = annotationInfo->find(SkAnnotationKeys::URL_Key());
epoger@google.comb58772f2013-03-08 09:09:10 +00001427 if (urlData) {
1428 handleLinkToURL(urlData, r, matrix);
1429 return p.isNoDrawAnnotation();
1430 }
1431 SkData* linkToName = annotationInfo->find(SkAnnotationKeys::Link_Named_Dest_Key());
1432 if (linkToName) {
1433 handleLinkToNamedDest(linkToName, r, matrix);
1434 return p.isNoDrawAnnotation();
1435 }
1436 return false;
1437}
1438
1439bool SkPDFDevice::handlePointAnnotation(const SkPoint* points, size_t count,
1440 const SkMatrix& matrix,
1441 const SkPaint& paint) {
1442 SkAnnotation* annotationInfo = paint.getAnnotation();
1443 if (!annotationInfo) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001444 return false;
1445 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001446 SkData* nameData = annotationInfo->find(SkAnnotationKeys::Define_Named_Dest_Key());
1447 if (nameData) {
1448 for (size_t i = 0; i < count; i++) {
1449 defineNamedDestination(nameData, points[i], matrix);
1450 }
1451 return paint.isNoDrawAnnotation();
1452 }
1453 return false;
1454}
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001455
epoger@google.comb58772f2013-03-08 09:09:10 +00001456SkPDFDict* SkPDFDevice::createLinkAnnotation(const SkRect& r, const SkMatrix& matrix) {
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001457 SkMatrix transform = matrix;
1458 transform.postConcat(fInitialTransform);
1459 SkRect translatedRect;
1460 transform.mapRect(&translatedRect, r);
1461
reed@google.com2a006c12012-09-19 17:05:55 +00001462 if (NULL == fAnnotations) {
1463 fAnnotations = SkNEW(SkPDFArray);
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001464 }
epoger@google.comb58772f2013-03-08 09:09:10 +00001465 SkPDFDict* annotation(SkNEW_ARGS(SkPDFDict, ("Annot")));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001466 annotation->insertName("Subtype", "Link");
epoger@google.comb58772f2013-03-08 09:09:10 +00001467 fAnnotations->append(annotation);
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001468
epoger@google.comb58772f2013-03-08 09:09:10 +00001469 SkAutoTUnref<SkPDFArray> border(SkNEW(SkPDFArray));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001470 border->reserve(3);
1471 border->appendInt(0); // Horizontal corner radius.
1472 border->appendInt(0); // Vertical corner radius.
1473 border->appendInt(0); // Width, 0 = no border.
1474 annotation->insert("Border", border.get());
1475
epoger@google.comb58772f2013-03-08 09:09:10 +00001476 SkAutoTUnref<SkPDFArray> rect(SkNEW(SkPDFArray));
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001477 rect->reserve(4);
1478 rect->appendScalar(translatedRect.fLeft);
1479 rect->appendScalar(translatedRect.fTop);
1480 rect->appendScalar(translatedRect.fRight);
1481 rect->appendScalar(translatedRect.fBottom);
1482 annotation->insert("Rect", rect.get());
1483
epoger@google.comb58772f2013-03-08 09:09:10 +00001484 return annotation;
1485}
epoger@google.com1cad8982013-03-06 00:05:13 +00001486
epoger@google.comb58772f2013-03-08 09:09:10 +00001487void SkPDFDevice::handleLinkToURL(SkData* urlData, const SkRect& r,
1488 const SkMatrix& matrix) {
1489 SkAutoTUnref<SkPDFDict> annotation(createLinkAnnotation(r, matrix));
1490
1491 SkString url(static_cast<const char *>(urlData->data()),
1492 urlData->size() - 1);
1493 SkAutoTUnref<SkPDFDict> action(SkNEW_ARGS(SkPDFDict, ("Action")));
1494 action->insertName("S", "URI");
1495 action->insert("URI", SkNEW_ARGS(SkPDFString, (url)))->unref();
1496 annotation->insert("A", action.get());
1497}
1498
1499void SkPDFDevice::handleLinkToNamedDest(SkData* nameData, const SkRect& r,
1500 const SkMatrix& matrix) {
1501 SkAutoTUnref<SkPDFDict> annotation(createLinkAnnotation(r, matrix));
1502 SkString name(static_cast<const char *>(nameData->data()),
1503 nameData->size() - 1);
1504 annotation->insert("Dest", SkNEW_ARGS(SkPDFName, (name)))->unref();
1505}
1506
1507struct NamedDestination {
1508 const SkData* nameData;
1509 SkPoint point;
1510
1511 NamedDestination(const SkData* nameData, const SkPoint& point)
1512 : nameData(nameData), point(point) {
1513 nameData->ref();
1514 }
1515
1516 ~NamedDestination() {
1517 nameData->unref();
1518 }
1519};
1520
1521void SkPDFDevice::defineNamedDestination(SkData* nameData, const SkPoint& point,
1522 const SkMatrix& matrix) {
1523 SkMatrix transform = matrix;
1524 transform.postConcat(fInitialTransform);
1525 SkPoint translatedPoint;
1526 transform.mapXY(point.x(), point.y(), &translatedPoint);
1527 fNamedDestinations.push(
1528 SkNEW_ARGS(NamedDestination, (nameData, translatedPoint)));
1529}
1530
1531void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) {
1532 int nDest = fNamedDestinations.count();
1533 for (int i = 0; i < nDest; i++) {
1534 NamedDestination* dest = fNamedDestinations[i];
1535 SkAutoTUnref<SkPDFArray> pdfDest(SkNEW(SkPDFArray));
1536 pdfDest->reserve(5);
1537 pdfDest->append(SkNEW_ARGS(SkPDFObjRef, (page)))->unref();
1538 pdfDest->appendName("XYZ");
1539 pdfDest->appendScalar(dest->point.x());
1540 pdfDest->appendScalar(dest->point.y());
1541 pdfDest->appendInt(0); // Leave zoom unchanged
1542 dict->insert(static_cast<const char *>(dest->nameData->data()), pdfDest);
1543 }
vandebo@chromium.org238be8c2012-07-13 20:06:02 +00001544}
1545
reed@google.comfc641d02012-09-20 17:52:20 +00001546SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() {
1547 SkPDFFormXObject* xobject = SkNEW_ARGS(SkPDFFormXObject, (this));
vandebo@chromium.org98594282011-07-25 22:34:12 +00001548 // We always draw the form xobjects that we create back into the device, so
1549 // we simply preserve the font usage instead of pulling it out and merging
1550 // it back in later.
1551 cleanUp(false); // Reset this device to have no content.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001552 init();
reed@google.comfc641d02012-09-20 17:52:20 +00001553 return xobject;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001554}
1555
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001556void SkPDFDevice::clearClipFromContent(const SkClipStack* clipStack,
1557 const SkRegion& clipRegion) {
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001558 if (clipRegion.isEmpty() || isContentEmpty()) {
1559 return;
1560 }
reed@google.comfc641d02012-09-20 17:52:20 +00001561 SkAutoTUnref<SkPDFFormXObject> curContent(createFormXObjectFromDevice());
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001562
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001563 // Redraw what we already had, but with the clip as a mask.
reed@google.comfc641d02012-09-20 17:52:20 +00001564 drawFormXObjectWithClip(curContent, clipStack, clipRegion, true);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001565}
1566
1567void SkPDFDevice::drawFormXObjectWithClip(SkPDFFormXObject* xobject,
1568 const SkClipStack* clipStack,
1569 const SkRegion& clipRegion,
1570 bool invertClip) {
1571 if (clipRegion.isEmpty() && !invertClip) {
1572 return;
1573 }
1574
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001575 // Create the mask.
1576 SkMatrix identity;
1577 identity.reset();
1578 SkDraw draw;
1579 draw.fMatrix = &identity;
1580 draw.fClip = &clipRegion;
1581 draw.fClipStack = clipStack;
1582 SkPaint stockPaint;
1583 this->drawPaint(draw, stockPaint);
reed@google.comfc641d02012-09-20 17:52:20 +00001584 SkAutoTUnref<SkPDFFormXObject> maskFormXObject(createFormXObjectFromDevice());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001585 SkAutoTUnref<SkPDFGraphicState> sMaskGS(
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +00001586 SkPDFGraphicState::GetSMaskGraphicState(maskFormXObject, invertClip,
1587 SkPDFGraphicState::kAlpha_SMaskMode));
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001588
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001589 // Draw the xobject with the clip as a mask.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001590 ScopedContentEntry content(this, &fExistingClipStack, fExistingClipRegion,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001591 identity, stockPaint);
1592 if (!content.entry()) {
1593 return;
1594 }
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001595 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001596 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001597 SkPDFUtils::DrawFormXObject(fXObjectResources.count(),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001598 &content.entry()->fContent);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001599 fXObjectResources.push(xobject);
1600 xobject->ref();
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001601
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001602 sMaskGS.reset(SkPDFGraphicState::GetNoSMaskGraphicState());
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001603 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001604 &content.entry()->fContent);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001605}
1606
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001607ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack,
1608 const SkRegion& clipRegion,
1609 const SkMatrix& matrix,
1610 const SkPaint& paint,
1611 bool hasText,
reed@google.comfc641d02012-09-20 17:52:20 +00001612 SkPDFFormXObject** dst) {
1613 *dst = NULL;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001614 if (clipRegion.isEmpty()) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001615 return NULL;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001616 }
1617
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001618 // The clip stack can come from an SkDraw where it is technically optional.
1619 SkClipStack synthesizedClipStack;
1620 if (clipStack == NULL) {
1621 if (clipRegion == fExistingClipRegion) {
1622 clipStack = &fExistingClipStack;
1623 } else {
1624 // GraphicStackState::updateClip expects the clip stack to have
1625 // fExistingClip as a prefix, so start there, then set the clip
1626 // to the passed region.
1627 synthesizedClipStack = fExistingClipStack;
1628 SkPath clipPath;
1629 clipRegion.getBoundaryPath(&clipPath);
reed@google.com00177082011-10-12 14:34:30 +00001630 synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op,
1631 false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001632 clipStack = &synthesizedClipStack;
1633 }
1634 }
1635
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001636 SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode;
1637 if (paint.getXfermode()) {
1638 paint.getXfermode()->asMode(&xfermode);
1639 }
1640
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001641 if (xfermode == SkXfermode::kClear_Mode ||
1642 xfermode == SkXfermode::kSrc_Mode) {
vandebo@chromium.org466f3d62011-05-18 23:06:29 +00001643 this->clearClipFromContent(clipStack, clipRegion);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001644 } else if (xfermode == SkXfermode::kSrcIn_Mode ||
1645 xfermode == SkXfermode::kDstIn_Mode ||
1646 xfermode == SkXfermode::kSrcOut_Mode ||
1647 xfermode == SkXfermode::kDstOut_Mode) {
1648 // For the following modes, we use both source and destination, but
1649 // we use one as a smask for the other, so we have to make form xobjects
1650 // out of both of them: SrcIn, DstIn, SrcOut, DstOut.
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001651 if (isContentEmpty()) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001652 return NULL;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001653 } else {
reed@google.comfc641d02012-09-20 17:52:20 +00001654 *dst = createFormXObjectFromDevice();
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001655 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001656 }
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +00001657 // TODO(vandebo): Figure out how/if we can handle the following modes:
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001658 // SrcAtop, DestAtop, Xor, Plus.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001659
1660 // These xfer modes don't draw source at all.
1661 if (xfermode == SkXfermode::kClear_Mode ||
1662 xfermode == SkXfermode::kDst_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001663 return NULL;
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001664 }
1665
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001666 ContentEntry* entry;
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001667 SkAutoTDelete<ContentEntry> newEntry;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001668
1669 ContentEntry* lastContentEntry = getLastContentEntry();
1670 if (lastContentEntry && lastContentEntry->fContent.getOffset() == 0) {
1671 entry = lastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001672 } else {
1673 newEntry.reset(new ContentEntry);
1674 entry = newEntry.get();
1675 }
1676
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001677 populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001678 hasText, &entry->fState);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001679 if (lastContentEntry && xfermode != SkXfermode::kDstOver_Mode &&
1680 entry->fState.compareInitialState(lastContentEntry->fState)) {
1681 return lastContentEntry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001682 }
1683
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001684 SkAutoTDelete<ContentEntry>* contentEntries = getContentEntries();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001685 if (!lastContentEntry) {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001686 contentEntries->reset(entry);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001687 setLastContentEntry(entry);
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001688 } else if (xfermode == SkXfermode::kDstOver_Mode) {
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001689 entry->fNext.reset(contentEntries->detach());
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001690 contentEntries->reset(entry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001691 } else {
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001692 lastContentEntry->fNext.reset(entry);
1693 setLastContentEntry(entry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001694 }
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00001695 newEntry.detach();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001696 return entry;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001697}
1698
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001699void SkPDFDevice::finishContentEntry(const SkXfermode::Mode xfermode,
1700 SkPDFFormXObject* dst) {
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001701 if (xfermode != SkXfermode::kSrcIn_Mode &&
1702 xfermode != SkXfermode::kDstIn_Mode &&
1703 xfermode != SkXfermode::kSrcOut_Mode &&
1704 xfermode != SkXfermode::kDstOut_Mode) {
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001705 SkASSERT(!dst);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001706 return;
1707 }
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001708
1709 ContentEntry* contentEntries = getContentEntries()->get();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001710 SkASSERT(dst);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001711 SkASSERT(!contentEntries->fNext.get());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001712 // We have to make a copy of these here because changing the current
1713 // content into a form xobject will destroy them.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +00001714 SkClipStack clipStack = contentEntries->fState.fClipStack;
1715 SkRegion clipRegion = contentEntries->fState.fClipRegion;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001716
reed@google.comfc641d02012-09-20 17:52:20 +00001717 SkAutoTUnref<SkPDFFormXObject> srcFormXObject;
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001718 if (!isContentEmpty()) {
reed@google.comfc641d02012-09-20 17:52:20 +00001719 srcFormXObject.reset(createFormXObjectFromDevice());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001720 }
1721
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001722 drawFormXObjectWithClip(dst, &clipStack, clipRegion, true);
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001723
1724 // We've redrawn dst minus the clip area, if there's no src, we're done.
1725 if (!srcFormXObject.get()) {
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001726 return;
1727 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001728
1729 SkMatrix identity;
1730 identity.reset();
1731 SkPaint stockPaint;
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001732 ScopedContentEntry inClipContentEntry(this, &fExistingClipStack,
1733 fExistingClipRegion, identity,
1734 stockPaint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001735 if (!inClipContentEntry.entry()) {
1736 return;
1737 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001738
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001739 SkAutoTUnref<SkPDFGraphicState> sMaskGS;
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001740 if (xfermode == SkXfermode::kSrcIn_Mode ||
1741 xfermode == SkXfermode::kSrcOut_Mode) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001742 sMaskGS.reset(SkPDFGraphicState::GetSMaskGraphicState(
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +00001743 dst,
1744 xfermode == SkXfermode::kSrcOut_Mode,
1745 SkPDFGraphicState::kAlpha_SMaskMode));
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001746 fXObjectResources.push(srcFormXObject.get());
reed@google.comfc641d02012-09-20 17:52:20 +00001747 srcFormXObject.get()->ref();
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001748 } else {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001749 sMaskGS.reset(SkPDFGraphicState::GetSMaskGraphicState(
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +00001750 srcFormXObject.get(),
1751 xfermode == SkXfermode::kDstOut_Mode,
1752 SkPDFGraphicState::kAlpha_SMaskMode));
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001753 // dst already added to fXObjectResources in drawFormXObjectWithClip.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001754 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001755 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001756 &inClipContentEntry.entry()->fContent);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001757
1758 SkPDFUtils::DrawFormXObject(fXObjectResources.count() - 1,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001759 &inClipContentEntry.entry()->fContent);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001760
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001761 sMaskGS.reset(SkPDFGraphicState::GetNoSMaskGraphicState());
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001762 SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()),
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001763 &inClipContentEntry.entry()->fContent);
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001764}
1765
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001766bool SkPDFDevice::isContentEmpty() {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +00001767 ContentEntry* contentEntries = getContentEntries()->get();
1768 if (!contentEntries || contentEntries->fContent.getOffset() == 0) {
1769 SkASSERT(!contentEntries || !contentEntries->fNext.get());
vandebo@chromium.org481aef62011-05-24 16:39:05 +00001770 return true;
1771 }
1772 return false;
1773}
1774
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001775void SkPDFDevice::populateGraphicStateEntryFromPaint(
1776 const SkMatrix& matrix,
1777 const SkClipStack& clipStack,
1778 const SkRegion& clipRegion,
1779 const SkPaint& paint,
1780 bool hasText,
1781 GraphicStateEntry* entry) {
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001782 SkASSERT(paint.getPathEffect() == NULL);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001783
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001784 NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false);
1785 NOT_IMPLEMENTED(paint.getColorFilter() != NULL, false);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001786
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001787 entry->fMatrix = matrix;
1788 entry->fClipStack = clipStack;
1789 entry->fClipRegion = clipRegion;
vandebo@chromium.orgda6c5692012-06-28 21:37:20 +00001790 entry->fColor = SkColorSetA(paint.getColor(), 0xFF);
1791 entry->fShaderIndex = -1;
vandebo@chromium.org48543272011-02-08 19:28:07 +00001792
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001793 // PDF treats a shader as a color, so we only set one or the other.
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001794 SkAutoTUnref<SkPDFObject> pdfShader;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001795 const SkShader* shader = paint.getShader();
1796 SkColor color = paint.getColor();
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001797 if (shader) {
1798 // PDF positions patterns relative to the initial transform, so
1799 // we need to apply the current transform to the shader parameters.
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001800 SkMatrix transform = matrix;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +00001801 transform.postConcat(fInitialTransform);
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001802
1803 // PDF doesn't support kClamp_TileMode, so we simulate it by making
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001804 // a pattern the size of the current clip.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001805 SkIRect bounds = clipRegion.getBounds();
vandebo@chromium.org293a7582012-03-16 19:50:37 +00001806
1807 // We need to apply the initial transform to bounds in order to get
1808 // bounds in a consistent coordinate system.
1809 SkRect boundsTemp;
1810 boundsTemp.set(bounds);
1811 fInitialTransform.mapRect(&boundsTemp);
1812 boundsTemp.roundOut(&bounds);
1813
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001814 pdfShader.reset(SkPDFShader::GetPDFShader(*shader, transform, bounds));
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001815
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001816 if (pdfShader.get()) {
1817 // pdfShader has been canonicalized so we can directly compare
1818 // pointers.
1819 int resourceIndex = fShaderResources.find(pdfShader.get());
1820 if (resourceIndex < 0) {
1821 resourceIndex = fShaderResources.count();
1822 fShaderResources.push(pdfShader.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001823 pdfShader.get()->ref();
vandebo@chromium.orgb88cfe52011-07-18 18:40:32 +00001824 }
1825 entry->fShaderIndex = resourceIndex;
1826 } else {
1827 // A color shader is treated as an invalid shader so we don't have
1828 // to set a shader just for a color.
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001829 SkShader::GradientInfo gradientInfo;
1830 SkColor gradientColor;
1831 gradientInfo.fColors = &gradientColor;
1832 gradientInfo.fColorOffsets = NULL;
1833 gradientInfo.fColorCount = 1;
1834 if (shader->asAGradient(&gradientInfo) ==
1835 SkShader::kColor_GradientType) {
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001836 entry->fColor = SkColorSetA(gradientColor, 0xFF);
1837 color = gradientColor;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001838 }
1839 }
vandebo@chromium.orgda912d62011-03-08 18:31:02 +00001840 }
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001841
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001842 SkAutoTUnref<SkPDFGraphicState> newGraphicState;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001843 if (color == paint.getColor()) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001844 newGraphicState.reset(
1845 SkPDFGraphicState::GetGraphicStateForPaint(paint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001846 } else {
1847 SkPaint newPaint = paint;
1848 newPaint.setColor(color);
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001849 newGraphicState.reset(
1850 SkPDFGraphicState::GetGraphicStateForPaint(newPaint));
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001851 }
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001852 int resourceIndex = addGraphicStateResource(newGraphicState.get());
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001853 entry->fGraphicStateIndex = resourceIndex;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001854
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001855 if (hasText) {
1856 entry->fTextScaleX = paint.getTextScaleX();
1857 entry->fTextFill = paint.getStyle();
1858 } else {
1859 entry->fTextScaleX = 0;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001860 }
1861}
1862
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001863int SkPDFDevice::addGraphicStateResource(SkPDFGraphicState* gs) {
1864 // Assumes that gs has been canonicalized (so we can directly compare
1865 // pointers).
1866 int result = fGraphicStateResources.find(gs);
1867 if (result < 0) {
1868 result = fGraphicStateResources.count();
1869 fGraphicStateResources.push(gs);
1870 gs->ref();
1871 }
1872 return result;
1873}
1874
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001875void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID,
1876 ContentEntry* contentEntry) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001877 SkTypeface* typeface = paint.getTypeface();
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001878 if (contentEntry->fState.fFont == NULL ||
1879 contentEntry->fState.fTextSize != paint.getTextSize() ||
1880 !contentEntry->fState.fFont->hasGlyph(glyphID)) {
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001881 int fontIndex = getFontResourceIndex(typeface, glyphID);
commit-bot@chromium.org47401352013-07-23 21:49:29 +00001882 contentEntry->fContent.writeText("/");
1883 contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName(
1884 SkPDFResourceDict::kFont_ResourceType,
1885 fontIndex).c_str());
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001886 contentEntry->fContent.writeText(" ");
1887 SkPDFScalar::Append(paint.getTextSize(), &contentEntry->fContent);
1888 contentEntry->fContent.writeText(" Tf\n");
1889 contentEntry->fState.fFont = fFontResources[fontIndex];
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001890 }
1891}
1892
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +00001893int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001894 SkAutoTUnref<SkPDFFont> newFont(SkPDFFont::GetFontResource(typeface, glyphID));
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001895 int resourceIndex = fFontResources.find(newFont.get());
1896 if (resourceIndex < 0) {
1897 resourceIndex = fFontResources.count();
1898 fFontResources.push(newFont.get());
vandebo@chromium.orgd96d17b2013-01-04 19:31:24 +00001899 newFont.get()->ref();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +00001900 }
1901 return resourceIndex;
1902}
1903
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001904void SkPDFDevice::internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +00001905 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +00001906 const SkRegion& clipRegion,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001907 const SkBitmap& bitmap,
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +00001908 const SkIRect* srcRect,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001909 const SkPaint& paint) {
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001910 SkMatrix scaled;
1911 // Adjust for origin flip.
vandebo@chromium.org663515b2012-01-05 18:45:27 +00001912 scaled.setScale(SK_Scalar1, -SK_Scalar1);
1913 scaled.postTranslate(0, SK_Scalar1);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001914 // Scale the image up from 1x1 to WxH.
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001915 SkIRect subset = SkIRect::MakeWH(bitmap.width(), bitmap.height());
reed@google.coma6d59f62011-03-07 21:29:21 +00001916 scaled.postScale(SkIntToScalar(subset.width()),
1917 SkIntToScalar(subset.height()));
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001918 scaled.postConcat(matrix);
vandebo@chromium.org13d14a92011-05-24 23:12:41 +00001919 ScopedContentEntry content(this, clipStack, clipRegion, scaled, paint);
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001920 if (!content.entry()) {
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001921 return;
1922 }
1923
1924 if (srcRect && !subset.intersect(*srcRect)) {
1925 return;
1926 }
1927
edisonn@google.comd9dfa182013-04-24 13:01:01 +00001928 SkPDFImage* image = SkPDFImage::CreateImage(bitmap, subset, fEncoder);
vandebo@chromium.org25adce82011-05-09 08:05:01 +00001929 if (!image) {
1930 return;
1931 }
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +00001932
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001933 fXObjectResources.push(image); // Transfer reference.
vandebo@chromium.org6112c212011-05-13 03:50:38 +00001934 SkPDFUtils::DrawFormXObject(fXObjectResources.count() - 1,
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +00001935 &content.entry()->fContent);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001936}
reed@google.com982cb872011-12-07 18:34:08 +00001937
1938bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y,
1939 SkCanvas::Config8888) {
1940 return false;
1941}
reed@google.comb55deeb2012-01-06 14:43:09 +00001942
1943bool SkPDFDevice::allowImageFilter(SkImageFilter*) {
1944 return false;
1945}