blob: 2a9aba59840968228569828720cbc84f6d9ce762 [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
130 /** Returns the media box for this device.
131 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000132 SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000133
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000134 /** Returns a SkStream with the page contents. The caller is responsible
135 for a reference to the returned value.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000136 */
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000137 SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000138
vandebo@chromium.org61d26782011-05-24 23:02:07 +0000139 const SkMatrix& initialTransform() const { return fInitialTransform; }
140
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000141protected:
142 // override
143 virtual SkDeviceFactory* onNewDeviceFactory();
144
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000145private:
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000146 friend class SkPDFDeviceFactory;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000147 // TODO(vandebo) push most of SkPDFDevice's state into a core object in
148 // order to get the right access levels without using friend.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000149 friend class ScopedContentEntry;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000150
ctguil@chromium.org15261292011-04-29 17:54:16 +0000151 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000152 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000153 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000154 SkClipStack fExistingClipStack;
155 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000156 SkRefPtr<SkPDFDict> fResourceDict;
157
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000158 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
159 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000160 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000161 SkTDArray<SkPDFShader*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000162
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000163 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000164 ContentEntry* fLastContentEntry;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000165
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000166 // For use by the DeviceFactory.
167 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
168 const SkRegion& existingClipRegion);
169
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000170 void init();
171 void cleanUp();
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000172 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000173
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000174 // Clear the passed clip from all existing content entries.
175 void clearClipFromContent(const SkClipStack* clipStack,
176 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000177 void drawFormXObjectWithClip(SkPDFFormXObject* form,
178 const SkClipStack* clipStack,
179 const SkRegion& clipRegion,
180 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000181
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000182 // If the paint or clip is such that we shouldn't draw anything, this
183 // returns NULL and does not create a content entry.
184 // setUpContentEntry and finishContentEntry can be used directly, but
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000185 // the preferred method is to use the ScopedContentEntry helper class.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000186 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
187 const SkRegion& clipRegion,
188 const SkMatrix& matrix,
189 const SkPaint& paint,
190 bool hasText,
191 SkRefPtr<SkPDFFormXObject>* dst);
192 void finishContentEntry(SkXfermode::Mode xfermode,
193 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000194 bool isContentEmpty();
195
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000196 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
197 const SkClipStack& clipStack,
198 const SkRegion& clipRegion,
199 const SkPaint& paint,
200 bool hasText,
201 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000202 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000203
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000204 void updateFont(const SkPaint& paint, uint16_t glyphID,
205 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000206 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000207
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000208 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000209 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000210 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000211 const SkRegion& clipRegion,
212 const SkBitmap& bitmap,
213 const SkIRect* srcRect,
214 const SkPaint& paint);
215
216 // Disable the default copy and assign implementation.
217 SkPDFDevice(const SkPDFDevice&);
218 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000219};
220
221#endif