blob: 0f634aac44cad7650444d6dede808a374d866aca [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.org9b49dc02010-10-20 22:23:29 +000020#include "SkDevice.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000021#include "SkPaint.h"
22#include "SkPath.h"
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000023#include "SkRefCnt.h"
24#include "SkStream.h"
25#include "SkTScopedPtr.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000026
27class SkPDFArray;
28class SkPDFDevice;
29class SkPDFDict;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000030class SkPDFFont;
vandebo@chromium.org6112c212011-05-13 03:50:38 +000031class SkPDFFormXObject;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000032class SkPDFGraphicState;
33class SkPDFObject;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000034class SkPDFShader;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000035class SkPDFStream;
36
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +000037// Private classes.
38struct ContentEntry;
39struct GraphicStateEntry;
40
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000041class SkPDFDeviceFactory : public SkDeviceFactory {
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +000042public:
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000043 virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width,
44 int height, bool isOpaque, bool isForLayer);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000045};
46
47/** \class SkPDFDevice
48
49 The drawing context for the PDF backend.
50*/
51class SkPDFDevice : public SkDevice {
52public:
53 /** Create a PDF drawing context with the given width and height.
54 * 72 points/in means letter paper is 612x792.
ctguil@chromium.org15261292011-04-29 17:54:16 +000055 * @param pageSize Page size in points.
56 * @param contentSize The content size of the page in points. This will be
57 * combined with the initial transform to determine the drawing area
58 * (as reported by the width and height methods). Anything outside
59 * of the drawing area will be clipped.
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000060 * @param initialTransform The initial transform to apply to the page.
61 * This may be useful to, for example, move the origin in and
62 * over a bit to account for a margin, scale the canvas,
63 * or apply a rotation. Note1: the SkPDFDevice also applies
64 * a scale+translate transform to move the origin from the
65 * bottom left (PDF default) to the top left. Note2: drawDevice
66 * (used by layer restore) draws the device after this initial
67 * transform is applied, so the PDF device factory does an
68 * inverse scale+translate to accommodate the one that SkPDFDevice
69 * always does.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000070 */
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +000071 // TODO(vandebo) The sizes should be SkSize and not SkISize.
ctguil@chromium.org15261292011-04-29 17:54:16 +000072 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
73 const SkMatrix& initialTransform);
74 SK_API virtual ~SkPDFDevice();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000075
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000076 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
77
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000078 virtual void clear(SkColor color);
79
reed@android.comf2b98d62010-12-20 18:26:13 +000080 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
81 return false;
82 }
83
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000084 /** These are called inside the per-device-layer loop for each draw call.
85 When these are called, we have already applied any saveLayer operations,
86 and are handling any looping from the paint, and any effects from the
87 DrawFilter.
88 */
89 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
90 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
91 size_t count, const SkPoint[],
92 const SkPaint& paint);
93 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
vandebo@chromium.orgff390322011-05-17 18:58:44 +000094 virtual void drawPath(const SkDraw&, const SkPath& origpath,
vandebo@chromium.org02cc5aa2011-01-25 22:06:29 +000095 const SkPaint& paint, const SkMatrix* prePathMatrix,
96 bool pathIsMutable);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000097 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +000098 const SkIRect* srcRectOrNull,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000099 const SkMatrix& matrix, const SkPaint& paint);
100 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
101 const SkPaint& paint);
102 virtual void drawText(const SkDraw&, const void* text, size_t len,
103 SkScalar x, SkScalar y, const SkPaint& paint);
104 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
105 const SkScalar pos[], SkScalar constY,
106 int scalarsPerPos, const SkPaint& paint);
107 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
108 const SkPath& path, const SkMatrix* matrix,
109 const SkPaint& paint);
110 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
111 int vertexCount, const SkPoint verts[],
112 const SkPoint texs[], const SkColor colors[],
113 SkXfermode* xmode, const uint16_t indices[],
114 int indexCount, const SkPaint& paint);
115 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
116 const SkPaint&);
117
118 // PDF specific methods.
119
120 /** Returns a reference to the resource dictionary for this device.
121 */
122 const SkRefPtr<SkPDFDict>& getResourceDict();
123
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000124 /** Get the list of resources (PDF objects) used on this page.
125 * @param resourceList A list to append the resources to.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000126 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000127 void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000128
129 /** Returns the media box for this device.
130 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000131 SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000132
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000133 /** Returns a SkStream with the page contents. The caller is responsible
134 for a reference to the returned value.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000135 */
vandebo@chromium.orgc2a9b7f2011-02-24 23:22:30 +0000136 SkStream* content() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000137
mike@reedtribe.orgea4ac972011-04-26 11:48:33 +0000138protected:
139 // override
140 virtual SkDeviceFactory* onNewDeviceFactory();
141
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000142private:
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000143 friend class SkPDFDeviceFactory;
144
ctguil@chromium.org15261292011-04-29 17:54:16 +0000145 SkISize fPageSize;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000146 SkISize fContentSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000147 SkMatrix fInitialTransform;
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000148 SkClipStack fExistingClipStack;
149 SkRegion fExistingClipRegion;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000150 SkRefPtr<SkPDFDict> fResourceDict;
151
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000152 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
153 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000154 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000155 SkTDArray<SkPDFShader*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000156
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000157 SkTScopedPtr<ContentEntry> fContentEntries;
158 ContentEntry* fCurrentContentEntry;
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000159 SkRefPtr<SkPDFFormXObject> fDstFormXObject;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000160
vandebo@chromium.orga0c7edb2011-05-09 07:58:08 +0000161 // For use by the DeviceFactory.
162 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
163 const SkRegion& existingClipRegion);
164
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000165 void init();
166 void cleanUp();
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000167 void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000168
vandebo@chromium.org466f3d62011-05-18 23:06:29 +0000169 // Clear the passed clip from all existing content entries.
170 void clearClipFromContent(const SkClipStack* clipStack,
171 const SkRegion& clipRegion);
172
vandebo@chromium.org25adce82011-05-09 08:05:01 +0000173 // If the paint or clip is such that we shouldn't draw anything, these
174 // return false and do not create a content entry.
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000175 bool setUpContentEntry(const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000176 const SkRegion& clipRegion,
177 const SkMatrix& matrix,
178 const SkPaint& paint,
179 bool hasText = false);
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000180 bool setUpContentEntryForText(const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000181 const SkRegion& clipRegion,
182 const SkMatrix& matrix,
183 const SkPaint& paint);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000184 void finishContentEntry(const SkPaint& paint);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000185 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
186 const SkClipStack& clipStack,
187 const SkRegion& clipRegion,
188 const SkPaint& paint,
189 bool hasText,
190 GraphicStateEntry* entry);
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000191 int addGraphicStateResource(SkPDFGraphicState* gs);
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000192
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000193 void updateFont(const SkPaint& paint, uint16_t glyphID);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000194 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000195 void setTextTransform(SkScalar x, SkScalar y, SkScalar textSkewX);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000196
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000197 void internalDrawPaint(const SkPaint& paint);
198 void internalDrawBitmap(const SkMatrix& matrix,
vandebo@chromium.org78dad542011-05-11 18:46:03 +0000199 const SkClipStack* clipStack,
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000200 const SkRegion& clipRegion,
201 const SkBitmap& bitmap,
202 const SkIRect* srcRect,
203 const SkPaint& paint);
204
205 // Disable the default copy and assign implementation.
206 SkPDFDevice(const SkPDFDevice&);
207 void operator=(const SkPDFDevice&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000208};
209
210#endif