blob: 5e050211be751e6e17dbc54b1ae11987342b3a3b [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 */
123 const SkRefPtr<SkPDFDict>& getResourceDict();
124
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.orga5180862010-10-26 19:48:49 +0000128 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 */
132 const SkTDArray<SkPDFFont*>& getFontResources() const;
133
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000134 /** Returns the media box for this device.
135 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000136 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.orgc2a9b7f2011-02-24 23:22:30 +0000141 SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000142
vandebo@chromium.org61d26782011-05-24 23:02:07 +0000143 const SkMatrix& initialTransform() const { return fInitialTransform; }
144
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000145protected:
146 // override
147 virtual SkDeviceFactory* onNewDeviceFactory();
148
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000149private:
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000150 friend class SkPDFDeviceFactory;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000151 // TODO(vandebo) push most of SkPDFDevice's state into a core object in
152 // order to get the right access levels without using friend.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000153 friend class ScopedContentEntry;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000154
ctguil@chromium.org15261292011-04-29 17:54:16 +0000155 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000156 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000157 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000158 SkClipStack fExistingClipStack;
159 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000160 SkRefPtr<SkPDFDict> fResourceDict;
161
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000162 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
163 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000164 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000165 SkTDArray<SkPDFShader*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000166
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000167 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000168 ContentEntry* fLastContentEntry;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000169
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000170 // For use by the DeviceFactory.
171 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
172 const SkRegion& existingClipRegion);
173
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000174 void init();
175 void cleanUp();
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000176 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000177
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000178 // Clear the passed clip from all existing content entries.
179 void clearClipFromContent(const SkClipStack* clipStack,
180 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000181 void drawFormXObjectWithClip(SkPDFFormXObject* form,
182 const SkClipStack* clipStack,
183 const SkRegion& clipRegion,
184 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000185
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000186 // If the paint or clip is such that we shouldn't draw anything, this
187 // returns NULL and does not create a content entry.
188 // setUpContentEntry and finishContentEntry can be used directly, but
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000189 // the preferred method is to use the ScopedContentEntry helper class.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000190 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
191 const SkRegion& clipRegion,
192 const SkMatrix& matrix,
193 const SkPaint& paint,
194 bool hasText,
195 SkRefPtr<SkPDFFormXObject>* dst);
196 void finishContentEntry(SkXfermode::Mode xfermode,
197 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000198 bool isContentEmpty();
199
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000200 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
201 const SkClipStack& clipStack,
202 const SkRegion& clipRegion,
203 const SkPaint& paint,
204 bool hasText,
205 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000206 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000207
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000208 void updateFont(const SkPaint& paint, uint16_t glyphID,
209 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000210 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000211
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000212 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000213 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000214 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000215 const SkRegion& clipRegion,
216 const SkBitmap& bitmap,
217 const SkIRect* srcRect,
218 const SkPaint& paint);
219
220 // Disable the default copy and assign implementation.
221 SkPDFDevice(const SkPDFDevice&);
222 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000223};
224
225#endif