blob: 2205f6cf584bb2c0fd774ddf91531c0ff3cd39e2 [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.org9b49dc02010-10-20 22:23:29 +000033class SkPDFGraphicState;
34class SkPDFObject;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000035class SkPDFShader;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000036class SkPDFStream;
37
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000038// Private classes.
39struct ContentEntry;
40struct GraphicStateEntry;
41
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000042class SkPDFDeviceFactory : public SkDeviceFactory {
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +000043public:
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000044 virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width,
45 int height, bool isOpaque, bool isForLayer);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000046};
47
48/** \class SkPDFDevice
49
50 The drawing context for the PDF backend.
51*/
52class SkPDFDevice : public SkDevice {
53public:
54 /** Create a PDF drawing context with the given width and height.
55 * 72 points/in means letter paper is 612x792.
ctguil@chromium.org15261292011-04-29 17:54:16 +000056 * @param pageSize Page size in points.
57 * @param contentSize The content size of the page in points. This will be
58 * combined with the initial transform to determine the drawing area
59 * (as reported by the width and height methods). Anything outside
60 * of the drawing area will be clipped.
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000061 * @param initialTransform The initial transform to apply to the page.
62 * This may be useful to, for example, move the origin in and
63 * over a bit to account for a margin, scale the canvas,
64 * or apply a rotation. Note1: the SkPDFDevice also applies
65 * a scale+translate transform to move the origin from the
66 * bottom left (PDF default) to the top left. Note2: drawDevice
67 * (used by layer restore) draws the device after this initial
68 * transform is applied, so the PDF device factory does an
69 * inverse scale+translate to accommodate the one that SkPDFDevice
70 * always does.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000071 */
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +000072 // TODO(vandebo) The sizes should be SkSize and not SkISize.
ctguil@chromium.org15261292011-04-29 17:54:16 +000073 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
74 const SkMatrix& initialTransform);
75 SK_API virtual ~SkPDFDevice();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000076
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000077 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
78
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000079 virtual void clear(SkColor color);
80
reed@android.comf2b98d62010-12-20 18:26:13 +000081 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
82 return false;
83 }
84
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000085 /** These are called inside the per-device-layer loop for each draw call.
86 When these are called, we have already applied any saveLayer operations,
87 and are handling any looping from the paint, and any effects from the
88 DrawFilter.
89 */
90 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
91 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
92 size_t count, const SkPoint[],
93 const SkPaint& paint);
94 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +000095 virtual void drawPath(const SkDraw&, const SkPath& origpath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +000096 const SkPaint& paint, const SkMatrix* prePathMatrix,
97 bool pathIsMutable);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000098 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +000099 const SkIRect* srcRectOrNull,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000100 const SkMatrix& matrix, const SkPaint& paint);
101 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
102 const SkPaint& paint);
103 virtual void drawText(const SkDraw&, const void* text, size_t len,
104 SkScalar x, SkScalar y, const SkPaint& paint);
105 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
106 const SkScalar pos[], SkScalar constY,
107 int scalarsPerPos, const SkPaint& paint);
108 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
109 const SkPath& path, const SkMatrix* matrix,
110 const SkPaint& paint);
111 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
112 int vertexCount, const SkPoint verts[],
113 const SkPoint texs[], const SkColor colors[],
114 SkXfermode* xmode, const uint16_t indices[],
115 int indexCount, const SkPaint& paint);
116 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
117 const SkPaint&);
118
119 // PDF specific methods.
120
121 /** Returns a reference to the resource dictionary for this device.
122 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000123 SK_API const SkRefPtr<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.org9b49dc02010-10-20 22:23:29 +0000127 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000128 SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000129
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000130 /** Get the fonts used on this device.
131 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000132 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000133
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000134 /** Returns the media box for this device.
135 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000136 SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000137
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000138 /** Returns a SkStream with the page contents. The caller is responsible
139 for a reference to the returned value.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000140 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000141 SK_API SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000142
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000143 SK_API const SkMatrix& initialTransform() const {
144 return fInitialTransform;
145 }
vandebo@chromium.org61d26782011-05-24 23:02:07 +0000146
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000147protected:
148 // override
149 virtual SkDeviceFactory* onNewDeviceFactory();
150
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000151private:
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000152 friend class SkPDFDeviceFactory;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000153 // TODO(vandebo) push most of SkPDFDevice's state into a core object in
154 // order to get the right access levels without using friend.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000155 friend class ScopedContentEntry;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000156
ctguil@chromium.org15261292011-04-29 17:54:16 +0000157 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000158 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000159 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000160 SkClipStack fExistingClipStack;
161 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000162 SkRefPtr<SkPDFDict> fResourceDict;
163
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000164 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
165 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000166 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000167 SkTDArray<SkPDFShader*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000168
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000169 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000170 ContentEntry* fLastContentEntry;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000171
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000172 // For use by the DeviceFactory.
173 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
174 const SkRegion& existingClipRegion);
175
bsalomon@google.come97f0852011-06-17 13:10:25 +0000176 // override from SkDevice
177 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
178 int width, int height,
179 bool isOpaque,
180 Usage usage);
181
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000182 void init();
183 void cleanUp();
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000184 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000185
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000186 // Clear the passed clip from all existing content entries.
187 void clearClipFromContent(const SkClipStack* clipStack,
188 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000189 void drawFormXObjectWithClip(SkPDFFormXObject* form,
190 const SkClipStack* clipStack,
191 const SkRegion& clipRegion,
192 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000193
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000194 // If the paint or clip is such that we shouldn't draw anything, this
195 // returns NULL and does not create a content entry.
196 // setUpContentEntry and finishContentEntry can be used directly, but
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000197 // the preferred method is to use the ScopedContentEntry helper class.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000198 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
199 const SkRegion& clipRegion,
200 const SkMatrix& matrix,
201 const SkPaint& paint,
202 bool hasText,
203 SkRefPtr<SkPDFFormXObject>* dst);
204 void finishContentEntry(SkXfermode::Mode xfermode,
205 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000206 bool isContentEmpty();
207
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000208 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
209 const SkClipStack& clipStack,
210 const SkRegion& clipRegion,
211 const SkPaint& paint,
212 bool hasText,
213 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000214 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000215
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000216 void updateFont(const SkPaint& paint, uint16_t glyphID,
217 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000218 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000219
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000220 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000221 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000222 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000223 const SkRegion& clipRegion,
224 const SkBitmap& bitmap,
225 const SkIRect* srcRect,
226 const SkPaint& paint);
227
228 // Disable the default copy and assign implementation.
229 SkPDFDevice(const SkPDFDevice&);
230 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000231};
232
233#endif