blob: 9892d5cc1fd185bdbb8dfc154c1baa4abc420d41 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000010#ifndef SkPDFDevice_DEFINED
11#define SkPDFDevice_DEFINED
12
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +000013#include "SkCanvas.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000014#include "SkDevice.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000015#include "SkPaint.h"
16#include "SkPath.h"
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000017#include "SkRefCnt.h"
18#include "SkStream.h"
19#include "SkTScopedPtr.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000020
21class SkPDFArray;
22class SkPDFDevice;
23class SkPDFDict;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000024class SkPDFFont;
vandebo@chromium.org6112c212011-05-13 03:50:38 +000025class SkPDFFormXObject;
vandebo@chromium.org98594282011-07-25 22:34:12 +000026class SkPDFGlyphSetMap;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000027class SkPDFGraphicState;
28class SkPDFObject;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000029class SkPDFShader;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000030class SkPDFStream;
31
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000032// Private classes.
33struct ContentEntry;
34struct GraphicStateEntry;
35
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000036/** \class SkPDFDevice
37
38 The drawing context for the PDF backend.
39*/
40class SkPDFDevice : public SkDevice {
41public:
42 /** Create a PDF drawing context with the given width and height.
43 * 72 points/in means letter paper is 612x792.
ctguil@chromium.org15261292011-04-29 17:54:16 +000044 * @param pageSize Page size in points.
45 * @param contentSize The content size of the page in points. This will be
46 * combined with the initial transform to determine the drawing area
47 * (as reported by the width and height methods). Anything outside
48 * of the drawing area will be clipped.
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000049 * @param initialTransform The initial transform to apply to the page.
50 * This may be useful to, for example, move the origin in and
51 * over a bit to account for a margin, scale the canvas,
52 * or apply a rotation. Note1: the SkPDFDevice also applies
53 * a scale+translate transform to move the origin from the
54 * bottom left (PDF default) to the top left. Note2: drawDevice
55 * (used by layer restore) draws the device after this initial
reed@google.comcde92112011-07-06 20:00:52 +000056 * transform is applied, so the PDF device does an
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000057 * inverse scale+translate to accommodate the one that SkPDFDevice
58 * always does.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000059 */
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +000060 // TODO(vandebo) The sizes should be SkSize and not SkISize.
ctguil@chromium.org15261292011-04-29 17:54:16 +000061 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
62 const SkMatrix& initialTransform);
63 SK_API virtual ~SkPDFDevice();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000064
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000065 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
66
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000067 virtual void clear(SkColor color);
68
reed@android.comf2b98d62010-12-20 18:26:13 +000069 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
70 return false;
71 }
72
bungeman@google.com88edf1e2011-08-08 19:41:56 +000073 virtual void setMatrixClip(const SkMatrix& m, const SkRegion& r,
74 const SkClipStack& c)
75 {
76 SkDevice::setMatrixClip(m, r, c);
77 }
78
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000079 /** These are called inside the per-device-layer loop for each draw call.
80 When these are called, we have already applied any saveLayer operations,
81 and are handling any looping from the paint, and any effects from the
82 DrawFilter.
83 */
84 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
85 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
86 size_t count, const SkPoint[],
87 const SkPaint& paint);
88 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +000089 virtual void drawPath(const SkDraw&, const SkPath& origpath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +000090 const SkPaint& paint, const SkMatrix* prePathMatrix,
91 bool pathIsMutable);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000092 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +000093 const SkIRect* srcRectOrNull,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000094 const SkMatrix& matrix, const SkPaint& paint);
95 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
96 const SkPaint& paint);
97 virtual void drawText(const SkDraw&, const void* text, size_t len,
98 SkScalar x, SkScalar y, const SkPaint& paint);
99 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
100 const SkScalar pos[], SkScalar constY,
101 int scalarsPerPos, const SkPaint& paint);
102 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
103 const SkPath& path, const SkMatrix* matrix,
104 const SkPaint& paint);
105 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
106 int vertexCount, const SkPoint verts[],
107 const SkPoint texs[], const SkColor colors[],
108 SkXfermode* xmode, const uint16_t indices[],
109 int indexCount, const SkPaint& paint);
110 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
111 const SkPaint&);
112
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000113 enum DrawingArea {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000114 kContent_DrawingArea, // Drawing area for the page content.
115 kMargin_DrawingArea, // Drawing area for the margin content.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000116 };
117
118 /** Sets the drawing area for the device. Subsequent draw calls are directed
119 * to the specific drawing area (margin or content). The default drawing
120 * area is the content drawing area.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000121 *
122 * Currently if margin content is drawn and then a complex (for PDF) xfer
123 * mode is used, like SrcIn, Clear, etc, the margin content will get
124 * clipped. A simple way to avoid the bug is to always draw the margin
125 * content last.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000126 */
127 void setDrawingArea(DrawingArea drawingArea);
128
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000129 // PDF specific methods.
130
reed@google.com1feb3302011-07-20 18:43:19 +0000131 /** Returns the resource dictionary for this device.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000132 */
reed@google.com1feb3302011-07-20 18:43:19 +0000133 SK_API SkPDFDict* getResourceDict();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000134
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000135 /** Get the list of resources (PDF objects) used on this page.
136 * @param resourceList A list to append the resources to.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000137 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000138 SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000139
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000140 /** Get the fonts used on this device.
141 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000142 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000143
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000144 /** Returns the media box for this device.
145 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000146 SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000147
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000148 /** Returns a SkStream with the page contents. The caller is responsible
149 for a reference to the returned value.
reed@google.com5667afc2011-06-27 14:42:15 +0000150 DEPRECATED: use copyContentToData()
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000151 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000152 SK_API SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000153
reed@google.com5667afc2011-06-27 14:42:15 +0000154 /** Returns a SkStream with the page contents. The caller is responsible
155 * for calling data->unref() when it is finished.
156 */
157 SK_API SkData* copyContentToData() const;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000158
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000159 SK_API const SkMatrix& initialTransform() const {
160 return fInitialTransform;
161 }
vandebo@chromium.org61d26782011-05-24 23:02:07 +0000162
vandebo@chromium.org98594282011-07-25 22:34:12 +0000163 /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
164 * that shows on this device.
165 */
166 const SkPDFGlyphSetMap& getFontGlyphUsage() const {
167 return *(fFontGlyphUsage.get());
168 }
169
vandebo@chromium.org7b5e0172011-08-15 21:20:59 +0000170 // TODO(vandebo) Remove this as soon as Chrome's Platform device goes away.
171 void setOrigin(int x, int y);
172
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000173private:
vandebo@chromium.org7b5e0172011-08-15 21:20:59 +0000174 typedef SkDevice INHERITED;
175
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000176 // TODO(vandebo) push most of SkPDFDevice's state into a core object in
177 // order to get the right access levels without using friend.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000178 friend class ScopedContentEntry;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000179
ctguil@chromium.org15261292011-04-29 17:54:16 +0000180 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000181 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000182 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000183 SkClipStack fExistingClipStack;
184 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000185 SkRefPtr<SkPDFDict> fResourceDict;
186
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000187 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
188 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000189 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000190 SkTDArray<SkPDFObject*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000191
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000192 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000193 ContentEntry* fLastContentEntry;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000194 SkTScopedPtr<ContentEntry> fMarginContentEntries;
195 ContentEntry* fLastMarginContentEntry;
196 DrawingArea fDrawingArea;
197
198 // Accessor and setter functions based on the current DrawingArea.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000199 SkTScopedPtr<ContentEntry>* getContentEntries();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000200 ContentEntry* getLastContentEntry();
201 void setLastContentEntry(ContentEntry* contentEntry);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000202
vandebo@chromium.org98594282011-07-25 22:34:12 +0000203 // Glyph ids used for each font on this device.
204 SkTScopedPtr<SkPDFGlyphSetMap> fFontGlyphUsage;
205
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000206 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
207 const SkRegion& existingClipRegion);
208
bsalomon@google.come97f0852011-06-17 13:10:25 +0000209 // override from SkDevice
vandebo@chromium.org98594282011-07-25 22:34:12 +0000210 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
211 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000212 bool isOpaque,
213 Usage usage);
214
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000215 void init();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000216 void cleanUp(bool clearFontUsage);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000217 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000218
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000219 // Clear the passed clip from all existing content entries.
220 void clearClipFromContent(const SkClipStack* clipStack,
221 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000222 void drawFormXObjectWithClip(SkPDFFormXObject* form,
223 const SkClipStack* clipStack,
224 const SkRegion& clipRegion,
225 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000226
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000227 // If the paint or clip is such that we shouldn't draw anything, this
228 // returns NULL and does not create a content entry.
229 // setUpContentEntry and finishContentEntry can be used directly, but
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000230 // the preferred method is to use the ScopedContentEntry helper class.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000231 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
232 const SkRegion& clipRegion,
233 const SkMatrix& matrix,
234 const SkPaint& paint,
235 bool hasText,
236 SkRefPtr<SkPDFFormXObject>* dst);
237 void finishContentEntry(SkXfermode::Mode xfermode,
238 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000239 bool isContentEmpty();
240
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000241 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
242 const SkClipStack& clipStack,
243 const SkRegion& clipRegion,
244 const SkPaint& paint,
245 bool hasText,
246 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000247 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000248
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000249 void updateFont(const SkPaint& paint, uint16_t glyphID,
250 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000251 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000252
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000253 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000254 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000255 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000256 const SkRegion& clipRegion,
257 const SkBitmap& bitmap,
258 const SkIRect* srcRect,
259 const SkPaint& paint);
260
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000261 /** Helper method for copyContentToData. It is responsible for copying the
262 * list of content entries |entry| to |data|.
263 */
264 void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000265
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000266 // Disable the default copy and assign implementation.
267 SkPDFDevice(const SkPDFDevice&);
268 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000269};
270
271#endif