blob: df4f3341cf2ea165ffe8ce4d16d6488adb838eb9 [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00002 * Copyright (C) 2011 Google Inc.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkPDFDevice_DEFINED
18#define SkPDFDevice_DEFINED
19
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +000020#include "SkCanvas.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000021#include "SkDevice.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000022#include "SkPaint.h"
23#include "SkPath.h"
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000024#include "SkRefCnt.h"
25#include "SkStream.h"
26#include "SkTScopedPtr.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000027
28class SkPDFArray;
29class SkPDFDevice;
30class SkPDFDict;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000031class SkPDFFont;
vandebo@chromium.org6112c212011-05-13 03:50:38 +000032class SkPDFFormXObject;
vandebo@chromium.org98594282011-07-25 22:34:12 +000033class SkPDFGlyphSetMap;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000034class SkPDFGraphicState;
35class SkPDFObject;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000036class SkPDFShader;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000037class SkPDFStream;
38
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000039// Private classes.
40struct ContentEntry;
41struct GraphicStateEntry;
42
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000043/** \class SkPDFDevice
44
45 The drawing context for the PDF backend.
46*/
47class SkPDFDevice : public SkDevice {
48public:
49 /** Create a PDF drawing context with the given width and height.
50 * 72 points/in means letter paper is 612x792.
ctguil@chromium.org15261292011-04-29 17:54:16 +000051 * @param pageSize Page size in points.
52 * @param contentSize The content size of the page in points. This will be
53 * combined with the initial transform to determine the drawing area
54 * (as reported by the width and height methods). Anything outside
55 * of the drawing area will be clipped.
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000056 * @param initialTransform The initial transform to apply to the page.
57 * This may be useful to, for example, move the origin in and
58 * over a bit to account for a margin, scale the canvas,
59 * or apply a rotation. Note1: the SkPDFDevice also applies
60 * a scale+translate transform to move the origin from the
61 * bottom left (PDF default) to the top left. Note2: drawDevice
62 * (used by layer restore) draws the device after this initial
reed@google.comcde92112011-07-06 20:00:52 +000063 * transform is applied, so the PDF device does an
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000064 * inverse scale+translate to accommodate the one that SkPDFDevice
65 * always does.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000066 */
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +000067 // TODO(vandebo) The sizes should be SkSize and not SkISize.
ctguil@chromium.org15261292011-04-29 17:54:16 +000068 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
69 const SkMatrix& initialTransform);
70 SK_API virtual ~SkPDFDevice();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000071
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000072 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
73
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000074 virtual void clear(SkColor color);
75
reed@android.comf2b98d62010-12-20 18:26:13 +000076 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
77 return false;
78 }
79
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000080 /** These are called inside the per-device-layer loop for each draw call.
81 When these are called, we have already applied any saveLayer operations,
82 and are handling any looping from the paint, and any effects from the
83 DrawFilter.
84 */
85 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
86 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
87 size_t count, const SkPoint[],
88 const SkPaint& paint);
89 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +000090 virtual void drawPath(const SkDraw&, const SkPath& origpath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +000091 const SkPaint& paint, const SkMatrix* prePathMatrix,
92 bool pathIsMutable);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000093 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +000094 const SkIRect* srcRectOrNull,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000095 const SkMatrix& matrix, const SkPaint& paint);
96 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
97 const SkPaint& paint);
98 virtual void drawText(const SkDraw&, const void* text, size_t len,
99 SkScalar x, SkScalar y, const SkPaint& paint);
100 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
101 const SkScalar pos[], SkScalar constY,
102 int scalarsPerPos, const SkPaint& paint);
103 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
104 const SkPath& path, const SkMatrix* matrix,
105 const SkPaint& paint);
106 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
107 int vertexCount, const SkPoint verts[],
108 const SkPoint texs[], const SkColor colors[],
109 SkXfermode* xmode, const uint16_t indices[],
110 int indexCount, const SkPaint& paint);
111 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
112 const SkPaint&);
113
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000114 enum DrawingArea {
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000115 kContent_DrawingArea, // Drawing area for the page content.
116 kMargin_DrawingArea, // Drawing area for the margin content.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000117 };
118
119 /** Sets the drawing area for the device. Subsequent draw calls are directed
120 * to the specific drawing area (margin or content). The default drawing
121 * area is the content drawing area.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000122 *
123 * Currently if margin content is drawn and then a complex (for PDF) xfer
124 * mode is used, like SrcIn, Clear, etc, the margin content will get
125 * clipped. A simple way to avoid the bug is to always draw the margin
126 * content last.
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000127 */
128 void setDrawingArea(DrawingArea drawingArea);
129
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000130 // PDF specific methods.
131
reed@google.com1feb3302011-07-20 18:43:19 +0000132 /** Returns the resource dictionary for this device.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000133 */
reed@google.com1feb3302011-07-20 18:43:19 +0000134 SK_API SkPDFDict* getResourceDict();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000135
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000136 /** Get the list of resources (PDF objects) used on this page.
137 * @param resourceList A list to append the resources to.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000138 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000139 SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000140
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000141 /** Get the fonts used on this device.
142 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000143 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000144
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000145 /** Returns the media box for this device.
146 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000147 SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000148
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000149 /** Returns a SkStream with the page contents. The caller is responsible
150 for a reference to the returned value.
reed@google.com5667afc2011-06-27 14:42:15 +0000151 DEPRECATED: use copyContentToData()
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000152 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000153 SK_API SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000154
reed@google.com5667afc2011-06-27 14:42:15 +0000155 /** Returns a SkStream with the page contents. The caller is responsible
156 * for calling data->unref() when it is finished.
157 */
158 SK_API SkData* copyContentToData() const;
vandebo@chromium.org98594282011-07-25 22:34:12 +0000159
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000160 SK_API const SkMatrix& initialTransform() const {
161 return fInitialTransform;
162 }
vandebo@chromium.org61d26782011-05-24 23:02:07 +0000163
vandebo@chromium.org98594282011-07-25 22:34:12 +0000164 /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
165 * that shows on this device.
166 */
167 const SkPDFGlyphSetMap& getFontGlyphUsage() const {
168 return *(fFontGlyphUsage.get());
169 }
170
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000171private:
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000172 // TODO(vandebo) push most of SkPDFDevice's state into a core object in
173 // order to get the right access levels without using friend.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000174 friend class ScopedContentEntry;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000175
ctguil@chromium.org15261292011-04-29 17:54:16 +0000176 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000177 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000178 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000179 SkClipStack fExistingClipStack;
180 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000181 SkRefPtr<SkPDFDict> fResourceDict;
182
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000183 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
184 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000185 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.org421d6442011-07-20 17:39:01 +0000186 SkTDArray<SkPDFObject*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000187
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000188 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000189 ContentEntry* fLastContentEntry;
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000190 SkTScopedPtr<ContentEntry> fMarginContentEntries;
191 ContentEntry* fLastMarginContentEntry;
192 DrawingArea fDrawingArea;
193
194 // Accessor and setter functions based on the current DrawingArea.
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000195 SkTScopedPtr<ContentEntry>* getContentEntries();
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000196 ContentEntry* getLastContentEntry();
197 void setLastContentEntry(ContentEntry* contentEntry);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000198
vandebo@chromium.org98594282011-07-25 22:34:12 +0000199 // Glyph ids used for each font on this device.
200 SkTScopedPtr<SkPDFGlyphSetMap> fFontGlyphUsage;
201
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000202 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
203 const SkRegion& existingClipRegion);
204
bsalomon@google.come97f0852011-06-17 13:10:25 +0000205 // override from SkDevice
vandebo@chromium.org98594282011-07-25 22:34:12 +0000206 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
207 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000208 bool isOpaque,
209 Usage usage);
210
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000211 void init();
vandebo@chromium.org98594282011-07-25 22:34:12 +0000212 void cleanUp(bool clearFontUsage);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000213 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000214
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000215 // Clear the passed clip from all existing content entries.
216 void clearClipFromContent(const SkClipStack* clipStack,
217 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000218 void drawFormXObjectWithClip(SkPDFFormXObject* form,
219 const SkClipStack* clipStack,
220 const SkRegion& clipRegion,
221 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000222
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000223 // If the paint or clip is such that we shouldn't draw anything, this
224 // returns NULL and does not create a content entry.
225 // setUpContentEntry and finishContentEntry can be used directly, but
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000226 // the preferred method is to use the ScopedContentEntry helper class.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000227 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
228 const SkRegion& clipRegion,
229 const SkMatrix& matrix,
230 const SkPaint& paint,
231 bool hasText,
232 SkRefPtr<SkPDFFormXObject>* dst);
233 void finishContentEntry(SkXfermode::Mode xfermode,
234 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000235 bool isContentEmpty();
236
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000237 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
238 const SkClipStack& clipStack,
239 const SkRegion& clipRegion,
240 const SkPaint& paint,
241 bool hasText,
242 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000243 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000244
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000245 void updateFont(const SkPaint& paint, uint16_t glyphID,
246 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000247 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000248
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000249 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000250 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000251 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000252 const SkRegion& clipRegion,
253 const SkBitmap& bitmap,
254 const SkIRect* srcRect,
255 const SkPaint& paint);
256
ctguil@chromium.org8dcf74f2011-07-12 21:56:27 +0000257 /** Helper method for copyContentToData. It is responsible for copying the
258 * list of content entries |entry| to |data|.
259 */
260 void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
ctguil@chromium.org9510ccc2011-07-27 00:10:51 +0000261
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000262 // Disable the default copy and assign implementation.
263 SkPDFDevice(const SkPDFDevice&);
264 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000265};
266
267#endif