blob: 354729d7c5760ae9d98902428396364a018defdd [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 */
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +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
reed@google.com982cb872011-12-07 18:34:08 +000065 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000066
reed@google.com982cb872011-12-07 18:34:08 +000067 virtual void clear(SkColor color) SK_OVERRIDE;
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000068
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000069 /** These are called inside the per-device-layer loop for each draw call.
70 When these are called, we have already applied any saveLayer operations,
71 and are handling any looping from the paint, and any effects from the
72 DrawFilter.
73 */
reed@google.com982cb872011-12-07 18:34:08 +000074 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000075 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
76 size_t count, const SkPoint[],
reed@google.com982cb872011-12-07 18:34:08 +000077 const SkPaint& paint) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000078 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +000079 virtual void drawPath(const SkDraw&, const SkPath& origpath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +000080 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.com982cb872011-12-07 18:34:08 +000081 bool pathIsMutable) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000082 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +000083 const SkIRect* srcRectOrNull,
reed@google.com982cb872011-12-07 18:34:08 +000084 const SkMatrix& matrix, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000085 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
reed@google.com982cb872011-12-07 18:34:08 +000086 const SkPaint& paint) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000087 virtual void drawText(const SkDraw&, const void* text, size_t len,
reed@google.com982cb872011-12-07 18:34:08 +000088 SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000089 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
90 const SkScalar pos[], SkScalar constY,
reed@google.com982cb872011-12-07 18:34:08 +000091 int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000092 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
93 const SkPath& path, const SkMatrix* matrix,
reed@google.com982cb872011-12-07 18:34:08 +000094 const SkPaint& paint) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000095 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
96 int vertexCount, const SkPoint verts[],
97 const SkPoint texs[], const SkColor colors[],
98 SkXfermode* xmode, const uint16_t indices[],
reed@google.com982cb872011-12-07 18:34:08 +000099 int indexCount, const SkPaint& paint) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000100 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
reed@google.com982cb872011-12-07 18:34:08 +0000101 const SkPaint&) SK_OVERRIDE;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000102
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000103 enum DrawingArea {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000104 kContent_DrawingArea, // Drawing area for the page content.
105 kMargin_DrawingArea, // Drawing area for the margin content.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000106 };
107
108 /** Sets the drawing area for the device. Subsequent draw calls are directed
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000109 * to the specific drawing area (margin or content). The default drawing
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000110 * area is the content drawing area.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000111 *
112 * Currently if margin content is drawn and then a complex (for PDF) xfer
113 * mode is used, like SrcIn, Clear, etc, the margin content will get
114 * clipped. A simple way to avoid the bug is to always draw the margin
115 * content last.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000116 */
epoger@google.com17b78942011-08-26 14:40:38 +0000117 SK_API void setDrawingArea(DrawingArea drawingArea);
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000118
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000119 // PDF specific methods.
120
reed@google.com1feb3302011-07-20 18:43:19 +0000121 /** Returns the resource dictionary for this device.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000122 */
reed@google.com1feb3302011-07-20 18:43:19 +0000123 SK_API SkPDFDict* getResourceDict();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000124
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000125 /** Get the list of resources (PDF objects) used on this page.
126 * @param resourceList A list to append the resources to.
vandebo@chromium.org188838c2012-03-09 22:16:58 +0000127 * @param recursive If recursive is true, get the resources of the
128 * device's resources recursively. (Useful for adding
129 * objects to the catalog.)
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000130 */
vandebo@chromium.org188838c2012-03-09 22:16:58 +0000131 SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList,
132 bool recursive) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000133
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000134 /** Get the fonts used on this device.
135 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000136 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000137
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000138 /** Returns the media box for this device.
139 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000140 SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000141
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000142 /** Returns a SkStream with the page contents. The caller is responsible
143 for a reference to the returned value.
reed@google.com5667afc2011-06-27 14:42:15 +0000144 DEPRECATED: use copyContentToData()
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000145 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000146 SK_API SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000147
reed@google.com5667afc2011-06-27 14:42:15 +0000148 /** Returns a SkStream with the page contents. The caller is responsible
149 * for calling data->unref() when it is finished.
150 */
151 SK_API SkData* copyContentToData() const;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000152
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000153 SK_API const SkMatrix& initialTransform() const {
154 return fInitialTransform;
155 }
vandebo@chromium.org61d26782011-05-24 23:02:07 +0000156
vandebo@chromium.org98594282011-07-25 22:34:12 +0000157 /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
158 * that shows on this device.
159 */
160 const SkPDFGlyphSetMap& getFontGlyphUsage() const {
161 return *(fFontGlyphUsage.get());
162 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000163
bsalomon@google.comc6980972011-11-02 19:57:21 +0000164protected:
reed@google.com982cb872011-12-07 18:34:08 +0000165 virtual bool onReadPixels(const SkBitmap& bitmap, int x, int y,
166 SkCanvas::Config8888) SK_OVERRIDE;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000167
reed@google.comb55deeb2012-01-06 14:43:09 +0000168 virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE;
169
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000170private:
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000171 // TODO(vandebo): push most of SkPDFDevice's state into a core object in
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000172 // order to get the right access levels without using friend.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000173 friend class ScopedContentEntry;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000174
ctguil@chromium.org15261292011-04-29 17:54:16 +0000175 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000176 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000177 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000178 SkClipStack fExistingClipStack;
179 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000180 SkRefPtr<SkPDFDict> fResourceDict;
181
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000182 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
183 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000184 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000185 SkTDArray<SkPDFObject*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000186
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000187 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000188 ContentEntry* fLastContentEntry;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000189 SkTScopedPtr<ContentEntry> fMarginContentEntries;
190 ContentEntry* fLastMarginContentEntry;
191 DrawingArea fDrawingArea;
192
193 // Accessor and setter functions based on the current DrawingArea.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000194 SkTScopedPtr<ContentEntry>* getContentEntries();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000195 ContentEntry* getLastContentEntry();
196 void setLastContentEntry(ContentEntry* contentEntry);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000197
vandebo@chromium.org98594282011-07-25 22:34:12 +0000198 // Glyph ids used for each font on this device.
199 SkTScopedPtr<SkPDFGlyphSetMap> fFontGlyphUsage;
200
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000201 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
202 const SkRegion& existingClipRegion);
203
bsalomon@google.come97f0852011-06-17 13:10:25 +0000204 // override from SkDevice
vandebo@chromium.org98594282011-07-25 22:34:12 +0000205 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
206 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000207 bool isOpaque,
reed@google.com982cb872011-12-07 18:34:08 +0000208 Usage usage) SK_OVERRIDE;
bsalomon@google.come97f0852011-06-17 13:10:25 +0000209
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000210 void init();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000211 void cleanUp(bool clearFontUsage);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000212 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000213
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000214 // Clear the passed clip from all existing content entries.
215 void clearClipFromContent(const SkClipStack* clipStack,
216 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000217 void drawFormXObjectWithClip(SkPDFFormXObject* form,
218 const SkClipStack* clipStack,
219 const SkRegion& clipRegion,
220 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000221
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000222 // If the paint or clip is such that we shouldn't draw anything, this
223 // returns NULL and does not create a content entry.
224 // setUpContentEntry and finishContentEntry can be used directly, but
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000225 // the preferred method is to use the ScopedContentEntry helper class.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000226 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
227 const SkRegion& clipRegion,
228 const SkMatrix& matrix,
229 const SkPaint& paint,
230 bool hasText,
231 SkRefPtr<SkPDFFormXObject>* dst);
232 void finishContentEntry(SkXfermode::Mode xfermode,
233 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000234 bool isContentEmpty();
235
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000236 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
237 const SkClipStack& clipStack,
238 const SkRegion& clipRegion,
239 const SkPaint& paint,
240 bool hasText,
241 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000242 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000243
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000244 void updateFont(const SkPaint& paint, uint16_t glyphID,
245 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000246 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000247
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000248 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000249 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000250 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000251 const SkRegion& clipRegion,
252 const SkBitmap& bitmap,
253 const SkIRect* srcRect,
254 const SkPaint& paint);
255
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000256 /** Helper method for copyContentToData. It is responsible for copying the
257 * list of content entries |entry| to |data|.
258 */
259 void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000260
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000261 // Disable the default copy and assign implementation.
262 SkPDFDevice(const SkPDFDevice&);
263 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000264};
265
266#endif