blob: a5abcfcc50e2fe5a5353cff41cee5fb535cab4df [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 +000042/** \class SkPDFDevice
43
44 The drawing context for the PDF backend.
45*/
46class SkPDFDevice : public SkDevice {
47public:
48 /** Create a PDF drawing context with the given width and height.
49 * 72 points/in means letter paper is 612x792.
ctguil@chromium.org15261292011-04-29 17:54:16 +000050 * @param pageSize Page size in points.
51 * @param contentSize The content size of the page in points. This will be
52 * combined with the initial transform to determine the drawing area
53 * (as reported by the width and height methods). Anything outside
54 * of the drawing area will be clipped.
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000055 * @param initialTransform The initial transform to apply to the page.
56 * This may be useful to, for example, move the origin in and
57 * over a bit to account for a margin, scale the canvas,
58 * or apply a rotation. Note1: the SkPDFDevice also applies
59 * a scale+translate transform to move the origin from the
60 * bottom left (PDF default) to the top left. Note2: drawDevice
61 * (used by layer restore) draws the device after this initial
reed@google.comcde92112011-07-06 20:00:52 +000062 * transform is applied, so the PDF device does an
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000063 * inverse scale+translate to accommodate the one that SkPDFDevice
64 * always does.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000065 */
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +000066 // TODO(vandebo) The sizes should be SkSize and not SkISize.
ctguil@chromium.org15261292011-04-29 17:54:16 +000067 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
68 const SkMatrix& initialTransform);
69 SK_API virtual ~SkPDFDevice();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000070
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000071 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
72
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000073 virtual void clear(SkColor color);
74
reed@android.comf2b98d62010-12-20 18:26:13 +000075 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
76 return false;
77 }
78
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000079 /** These are called inside the per-device-layer loop for each draw call.
80 When these are called, we have already applied any saveLayer operations,
81 and are handling any looping from the paint, and any effects from the
82 DrawFilter.
83 */
84 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
85 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
86 size_t count, const SkPoint[],
87 const SkPaint& paint);
88 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +000089 virtual void drawPath(const SkDraw&, const SkPath& origpath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +000090 const SkPaint& paint, const SkMatrix* prePathMatrix,
91 bool pathIsMutable);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000092 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +000093 const SkIRect* srcRectOrNull,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000094 const SkMatrix& matrix, const SkPaint& paint);
95 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
96 const SkPaint& paint);
97 virtual void drawText(const SkDraw&, const void* text, size_t len,
98 SkScalar x, SkScalar y, const SkPaint& paint);
99 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
100 const SkScalar pos[], SkScalar constY,
101 int scalarsPerPos, const SkPaint& paint);
102 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
103 const SkPath& path, const SkMatrix* matrix,
104 const SkPaint& paint);
105 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
106 int vertexCount, const SkPoint verts[],
107 const SkPoint texs[], const SkColor colors[],
108 SkXfermode* xmode, const uint16_t indices[],
109 int indexCount, const SkPaint& paint);
110 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
111 const SkPaint&);
112
113 // PDF specific methods.
114
115 /** Returns a reference to the resource dictionary for this device.
116 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000117 SK_API const SkRefPtr<SkPDFDict>& getResourceDict();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000118
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000119 /** Get the list of resources (PDF objects) used on this page.
120 * @param resourceList A list to append the resources to.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000121 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000122 SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000123
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000124 /** Get the fonts used on this device.
125 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000126 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
vandebo@chromium.orgf0ec2662011-05-29 05:55:42 +0000127
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000128 /** Returns the media box for this device.
129 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000130 SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000131
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000132 /** Returns a SkStream with the page contents. The caller is responsible
133 for a reference to the returned value.
reed@google.com5667afc2011-06-27 14:42:15 +0000134 DEPRECATED: use copyContentToData()
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000135 */
vandebo@chromium.org3509f052011-05-30 20:52:33 +0000136 SK_API SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000137
reed@google.com5667afc2011-06-27 14:42:15 +0000138 /** Returns a SkStream with the page contents. The caller is responsible
139 * for calling data->unref() when it is finished.
140 */
141 SK_API SkData* copyContentToData() const;
142
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
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000147private:
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000148 // TODO(vandebo) push most of SkPDFDevice's state into a core object in
149 // order to get the right access levels without using friend.
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000150 friend class ScopedContentEntry;
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000151
ctguil@chromium.org15261292011-04-29 17:54:16 +0000152 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000153 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000154 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000155 SkClipStack fExistingClipStack;
156 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000157 SkRefPtr<SkPDFDict> fResourceDict;
158
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000159 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
160 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000161 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000162 SkTDArray<SkPDFShader*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000163
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000164 SkTScopedPtr<ContentEntry> fContentEntries;
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000165 ContentEntry* fLastContentEntry;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000166
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000167 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
168 const SkRegion& existingClipRegion);
169
bsalomon@google.come97f0852011-06-17 13:10:25 +0000170 // override from SkDevice
171 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
172 int width, int height,
173 bool isOpaque,
174 Usage usage);
175
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000176 void init();
177 void cleanUp();
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000178 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000179
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000180 // Clear the passed clip from all existing content entries.
181 void clearClipFromContent(const SkClipStack* clipStack,
182 const SkRegion& clipRegion);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000183 void drawFormXObjectWithClip(SkPDFFormXObject* form,
184 const SkClipStack* clipStack,
185 const SkRegion& clipRegion,
186 bool invertClip);
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000187
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000188 // If the paint or clip is such that we shouldn't draw anything, this
189 // returns NULL and does not create a content entry.
190 // setUpContentEntry and finishContentEntry can be used directly, but
vandebo@chromium.org13d14a92011-05-24 23:12:41 +0000191 // the preferred method is to use the ScopedContentEntry helper class.
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000192 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
193 const SkRegion& clipRegion,
194 const SkMatrix& matrix,
195 const SkPaint& paint,
196 bool hasText,
197 SkRefPtr<SkPDFFormXObject>* dst);
198 void finishContentEntry(SkXfermode::Mode xfermode,
199 SkPDFFormXObject* dst);
vandebo@chromium.org481aef62011-05-24 16:39:05 +0000200 bool isContentEmpty();
201
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000202 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
203 const SkClipStack& clipStack,
204 const SkRegion& clipRegion,
205 const SkPaint& paint,
206 bool hasText,
207 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000208 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000209
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000210 void updateFont(const SkPaint& paint, uint16_t glyphID,
211 ContentEntry* contentEntry);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000212 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000213
vandebo@chromium.orgb069c8c2011-05-24 17:19:38 +0000214 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000215 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000216 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000217 const SkRegion& clipRegion,
218 const SkBitmap& bitmap,
219 const SkIRect* srcRect,
220 const SkPaint& paint);
221
222 // Disable the default copy and assign implementation.
223 SkPDFDevice(const SkPDFDevice&);
224 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000225};
226
227#endif