blob: 7d093dc5d92cc63cb90c8f3f3a197407ee48974e [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.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +000039class ContentEntryAccessor;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000040struct ContentEntry;
41struct GraphicStateEntry;
42
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000043class SkPDFDeviceFactory : public SkDeviceFactory {
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +000044public:
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000045 virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width,
46 int height, bool isOpaque, bool isForLayer);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000047};
48
49/** \class SkPDFDevice
50
51 The drawing context for the PDF backend.
52*/
53class SkPDFDevice : public SkDevice {
54public:
55 /** Create a PDF drawing context with the given width and height.
56 * 72 points/in means letter paper is 612x792.
ctguil@chromium.org15261292011-04-29 17:54:16 +000057 * @param pageSize Page size in points.
58 * @param contentSize The content size of the page in points. This will be
59 * combined with the initial transform to determine the drawing area
60 * (as reported by the width and height methods). Anything outside
61 * of the drawing area will be clipped.
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000062 * @param initialTransform The initial transform to apply to the page.
63 * This may be useful to, for example, move the origin in and
64 * over a bit to account for a margin, scale the canvas,
65 * or apply a rotation. Note1: the SkPDFDevice also applies
66 * a scale+translate transform to move the origin from the
67 * bottom left (PDF default) to the top left. Note2: drawDevice
68 * (used by layer restore) draws the device after this initial
69 * transform is applied, so the PDF device factory does an
70 * inverse scale+translate to accommodate the one that SkPDFDevice
71 * always does.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000072 */
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +000073 // TODO(vandebo) The sizes should be SkSize and not SkISize.
ctguil@chromium.org15261292011-04-29 17:54:16 +000074 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
75 const SkMatrix& initialTransform);
76 SK_API virtual ~SkPDFDevice();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000077
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000078 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
79
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000080 virtual void clear(SkColor color);
81
reed@android.comf2b98d62010-12-20 18:26:13 +000082 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
83 return false;
84 }
85
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000086 /** These are called inside the per-device-layer loop for each draw call.
87 When these are called, we have already applied any saveLayer operations,
88 and are handling any looping from the paint, and any effects from the
89 DrawFilter.
90 */
91 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
92 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
93 size_t count, const SkPoint[],
94 const SkPaint& paint);
95 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +000096 virtual void drawPath(const SkDraw&, const SkPath& origpath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +000097 const SkPaint& paint, const SkMatrix* prePathMatrix,
98 bool pathIsMutable);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000099 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +0000100 const SkIRect* srcRectOrNull,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000101 const SkMatrix& matrix, const SkPaint& paint);
102 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
103 const SkPaint& paint);
104 virtual void drawText(const SkDraw&, const void* text, size_t len,
105 SkScalar x, SkScalar y, const SkPaint& paint);
106 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
107 const SkScalar pos[], SkScalar constY,
108 int scalarsPerPos, const SkPaint& paint);
109 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
110 const SkPath& path, const SkMatrix* matrix,
111 const SkPaint& paint);
112 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
113 int vertexCount, const SkPoint verts[],
114 const SkPoint texs[], const SkColor colors[],
115 SkXfermode* xmode, const uint16_t indices[],
116 int indexCount, const SkPaint& paint);
117 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
118 const SkPaint&);
119
120 // PDF specific methods.
121
122 /** Returns a reference to the resource dictionary for this device.
123 */
124 const SkRefPtr<SkPDFDict>& getResourceDict();
125
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000126 /** Get the list of resources (PDF objects) used on this page.
127 * @param resourceList A list to append the resources to.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000128 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000129 void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000130
131 /** Returns the media box for this device.
132 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000133 SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000134
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000135 /** Returns a SkStream with the page contents. The caller is responsible
136 for a reference to the returned value.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000137 */
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000138 SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000139
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000140protected:
141 // override
142 virtual SkDeviceFactory* onNewDeviceFactory();
143
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000144private:
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000145 friend class SkPDFDeviceFactory;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000146 // TODO(vandebo) push most of SkPDFDevice's state into a core object in
147 // order to get the right access levels without using friend.
148 friend class ContentEntryAccessor;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000149
ctguil@chromium.org15261292011-04-29 17:54:16 +0000150 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000151 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000152 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000153 SkClipStack fExistingClipStack;
154 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000155 SkRefPtr<SkPDFDict> fResourceDict;
156
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000157 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
158 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000159 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000160 SkTDArray<SkPDFShader*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000161
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000162 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000163 ContentEntry* fLastContentEntry;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000164
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000165 // For use by the DeviceFactory.
166 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
167 const SkRegion& existingClipRegion);
168
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000169 void init();
170 void cleanUp();
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000171 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000172
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000173 // Clear the passed clip from all existing content entries.
174 void clearClipFromContent(const SkClipStack* clipStack,
175 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000176 void drawFormXObjectWithClip(SkPDFFormXObject* form,
177 const SkClipStack* clipStack,
178 const SkRegion& clipRegion,
179 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000180
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000181 // If the paint or clip is such that we shouldn't draw anything, this
182 // returns NULL and does not create a content entry.
183 // setUpContentEntry and finishContentEntry can be used directly, but
184 // the preferred method is to use the ContentEntryAccessor helper class.
185 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
186 const SkRegion& clipRegion,
187 const SkMatrix& matrix,
188 const SkPaint& paint,
189 bool hasText,
190 SkRefPtr<SkPDFFormXObject>* dst);
191 void finishContentEntry(SkXfermode::Mode xfermode,
192 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000193 bool isContentEmpty();
194
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000195 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
196 const SkClipStack& clipStack,
197 const SkRegion& clipRegion,
198 const SkPaint& paint,
199 bool hasText,
200 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000201 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000202
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000203 void updateFont(const SkPaint& paint, uint16_t glyphID,
204 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000205 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000206
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000207 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000208 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000209 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000210 const SkRegion& clipRegion,
211 const SkBitmap& bitmap,
212 const SkIRect* srcRect,
213 const SkPaint& paint);
214
215 // Disable the default copy and assign implementation.
216 SkPDFDevice(const SkPDFDevice&);
217 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000218};
219
220#endif