blob: 98a473fed70058410aed05d3ad4ebb69e4dc165c [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
20#include "SkRefCnt.h"
21#include "SkDevice.h"
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000022#include "SkStream.h"
vandebo@chromium.orga5180862010-10-26 19:48:49 +000023#include "SkPaint.h"
24#include "SkPath.h"
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000025
26class SkPDFArray;
27class SkPDFDevice;
28class SkPDFDict;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +000029class SkPDFFont;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000030class SkPDFGraphicState;
31class SkPDFObject;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +000032class SkPDFShader;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000033class SkPDFStream;
34
35class SkPDFDeviceFactory : public SkDeviceFactory {
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000036 virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width,
37 int height, bool isOpaque, bool isForLayer);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000038};
39
40/** \class SkPDFDevice
41
42 The drawing context for the PDF backend.
43*/
44class SkPDFDevice : public SkDevice {
45public:
46 /** Create a PDF drawing context with the given width and height.
47 * 72 points/in means letter paper is 612x792.
ctguil@chromium.org15261292011-04-29 17:54:16 +000048 * @param pageSize Page size in points.
49 * @param contentSize The content size of the page in points. This will be
50 * combined with the initial transform to determine the drawing area
51 * (as reported by the width and height methods). Anything outside
52 * of the drawing area will be clipped.
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000053 * @param initialTransform The initial transform to apply to the page.
54 * This may be useful to, for example, move the origin in and
55 * over a bit to account for a margin, scale the canvas,
56 * or apply a rotation. Note1: the SkPDFDevice also applies
57 * a scale+translate transform to move the origin from the
58 * bottom left (PDF default) to the top left. Note2: drawDevice
59 * (used by layer restore) draws the device after this initial
60 * transform is applied, so the PDF device factory does an
61 * inverse scale+translate to accommodate the one that SkPDFDevice
62 * always does.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000063 */
vandebo@chromium.orgbe2048a2011-05-02 15:24:01 +000064 // TODO(vandebo) The sizes should be SkSize and not SkISize.
ctguil@chromium.org15261292011-04-29 17:54:16 +000065 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
66 const SkMatrix& initialTransform);
67 SK_API virtual ~SkPDFDevice();
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000068
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000069 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
70
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +000071 virtual void clear(SkColor color);
72
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000073 /** Called with the correct matrix and clip before this device is drawn
74 to using those settings. If your subclass overrides this, be sure to
75 call through to the base class as well.
76 */
reed@google.com46799cd2011-02-22 20:56:26 +000077 virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
78 const SkClipStack&);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000079
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);
94 virtual void drawPath(const SkDraw&, const SkPath& path,
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:
ctguil@chromium.org15261292011-04-29 17:54:16 +0000143 SkISize fPageSize;
vandebo@chromium.org75f97e42011-04-11 23:24:18 +0000144 SkMatrix fInitialTransform;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000145 SkRefPtr<SkPDFDict> fResourceDict;
146
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000147 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
148 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000149 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000150 SkTDArray<SkPDFShader*> fShaderResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000151
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000152 // In PDF, transforms and clips can only be undone by popping the graphic
153 // state to before the transform or clip was applied. Because it can be
154 // a lot of work to reapply a clip and because this class has to apply
155 // different transforms to accomplish various operations, the clip is
156 // always applied before a transform and always at a different graphic
157 // state-stack level than a transform. This strategy results in the
158 // following possible states for the graphic state stack:
159 // empty: (identity transform and clip to page)
160 // one entry: a transform
161 // one entry: a clip
162 // two entries: a clip and then a transform
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000163 // Pointers are owned by the respective Resources list.
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000164 struct GraphicStackEntry {
165 SkColor fColor;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000166 SkScalar fTextSize;
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000167 SkScalar fTextScaleX;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000168 SkPaint::Style fTextFill;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000169 SkPDFFont* fFont;
vandebo@chromium.orgda912d62011-03-08 18:31:02 +0000170 SkPDFShader* fShader;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000171 SkPDFGraphicState* fGraphicState;
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000172 SkRegion fClip;
173 SkMatrix fTransform;
174 };
175 struct GraphicStackEntry fGraphicStack[3];
176 int fGraphicStackIndex;
177
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000178 SkDynamicMemoryWStream fContent;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000179
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000180 void init();
181 void cleanUp();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000182 void updateGSFromPaint(const SkPaint& newPaint, bool forText);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000183 void updateFont(const SkPaint& paint, uint16_t glyphID);
ctguil@chromium.org9db86bb2011-03-04 21:43:27 +0000184 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000185
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000186 void pushGS();
187 void popGS();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000188 void setTextTransform(SkScalar x, SkScalar y, SkScalar textSkewX);
vandebo@chromium.org77bcaa32011-04-15 20:57:37 +0000189 void internalDrawPaint(const SkPaint& paint);
190 void internalDrawRect(const SkRect& r, const SkPaint& paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000191 void internalDrawBitmap(const SkMatrix& matrix, const SkBitmap& bitmap,
vandebo@chromium.orgbefebb82011-01-29 01:38:50 +0000192 const SkIRect* srcRect, const SkPaint& paint);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000193
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000194 SkMatrix setTransform(const SkMatrix& matrix);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000195};
196
197#endif