blob: 64e0407855bcbcd156e7dfa74f87d9543975fa19 [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"
22#include "SkString.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;
32class SkPDFStream;
33
34class SkPDFDeviceFactory : public SkDeviceFactory {
reed@android.comf2b98d62010-12-20 18:26:13 +000035 virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width, int height,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000036 bool isOpaque, bool isForLayer);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000037};
38
39/** \class SkPDFDevice
40
41 The drawing context for the PDF backend.
42*/
43class SkPDFDevice : public SkDevice {
44public:
45 /** Create a PDF drawing context with the given width and height.
46 * 72 points/in means letter paper is 612x792.
47 * @param width Page width in points.
48 * @param height Page height in points.
49 */
50 SkPDFDevice(int width, int height);
51 virtual ~SkPDFDevice();
52
53 virtual SkDeviceFactory* getDeviceFactory() {
54 return SkNEW(SkPDFDeviceFactory);
55 }
56
vandebo@chromium.org35fc62b2010-10-26 19:47:30 +000057 virtual uint32_t getDeviceCapabilities() { return kVector_Capability; }
58
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000059 virtual int width() const { return fWidth; };
60
61 virtual int height() const { return fHeight; };
62
63 /** Called with the correct matrix and clip before this device is drawn
64 to using those settings. If your subclass overrides this, be sure to
65 call through to the base class as well.
66 */
67 virtual void setMatrixClip(const SkMatrix&, const SkRegion&);
68
reed@android.comf2b98d62010-12-20 18:26:13 +000069 virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
70 return false;
71 }
72
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000073 /** These are called inside the per-device-layer loop for each draw call.
74 When these are called, we have already applied any saveLayer operations,
75 and are handling any looping from the paint, and any effects from the
76 DrawFilter.
77 */
78 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
79 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
80 size_t count, const SkPoint[],
81 const SkPaint& paint);
82 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
83 virtual void drawPath(const SkDraw&, const SkPath& path,
84 const SkPaint& paint);
85 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
reed@android.comf2b98d62010-12-20 18:26:13 +000086 const SkIRect* srcRectOrNull,
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +000087 const SkMatrix& matrix, const SkPaint& paint);
88 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
89 const SkPaint& paint);
90 virtual void drawText(const SkDraw&, const void* text, size_t len,
91 SkScalar x, SkScalar y, const SkPaint& paint);
92 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
93 const SkScalar pos[], SkScalar constY,
94 int scalarsPerPos, const SkPaint& paint);
95 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
96 const SkPath& path, const SkMatrix* matrix,
97 const SkPaint& paint);
98 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
99 int vertexCount, const SkPoint verts[],
100 const SkPoint texs[], const SkColor colors[],
101 SkXfermode* xmode, const uint16_t indices[],
102 int indexCount, const SkPaint& paint);
103 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
104 const SkPaint&);
105
106 // PDF specific methods.
107
108 /** Returns a reference to the resource dictionary for this device.
109 */
110 const SkRefPtr<SkPDFDict>& getResourceDict();
111
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000112 /** Get the list of resources (PDF objects) used on this page.
113 * @param resourceList A list to append the resources to.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000114 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000115 void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000116
117 /** Returns the media box for this device.
118 */
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000119 SkRefPtr<SkPDFArray> getMediaBox() const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000120
121 /** Returns a string with the page contents.
vandebo@chromium.orgddbbd802010-10-26 19:45:06 +0000122 * @param flipOrigin Flip the origin between top and bottom.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000123 */
vandebo@chromium.orgddbbd802010-10-26 19:45:06 +0000124 SkString content(bool flipOrigin) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000125
126private:
127 int fWidth;
128 int fHeight;
129 SkRefPtr<SkPDFDict> fResourceDict;
130
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000131 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
132 SkTDArray<SkPDFObject*> fXObjectResources;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000133 SkTDArray<SkPDFFont*> fFontResources;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000134
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000135 // In PDF, transforms and clips can only be undone by popping the graphic
136 // state to before the transform or clip was applied. Because it can be
137 // a lot of work to reapply a clip and because this class has to apply
138 // different transforms to accomplish various operations, the clip is
139 // always applied before a transform and always at a different graphic
140 // state-stack level than a transform. This strategy results in the
141 // following possible states for the graphic state stack:
142 // empty: (identity transform and clip to page)
143 // one entry: a transform
144 // one entry: a clip
145 // two entries: a clip and then a transform
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000146 // Pointers are owned by the respective Resources list.
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000147 struct GraphicStackEntry {
148 SkColor fColor;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000149 SkScalar fTextSize;
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000150 SkScalar fTextScaleX;
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000151 SkPaint::Style fTextFill;
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000152 SkPDFFont* fFont;
153 SkPDFGraphicState* fGraphicState;
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000154 SkRegion fClip;
155 SkMatrix fTransform;
156 };
157 struct GraphicStackEntry fGraphicStack[3];
158 int fGraphicStackIndex;
159
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000160 SkString fContent;
161
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000162 void updateGSFromPaint(const SkPaint& newPaint, bool forText);
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000163 void updateFont(const SkPaint& paint, uint16_t glyphID);
164 int getFontResourceIndex(uint32_t fontID, uint16_t glyphID);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000165
166 void moveTo(SkScalar x, SkScalar y);
167 void appendLine(SkScalar x, SkScalar y);
168 void appendCubic(SkScalar ctl1X, SkScalar ctl1Y,
169 SkScalar ctl2X, SkScalar ctl2Y,
170 SkScalar dstX, SkScalar dstY);
171 void appendRectangle(SkScalar x, SkScalar y, SkScalar w, SkScalar h);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000172 void emitPath(const SkPath& path);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000173 void closePath();
vandebo@chromium.orga5180862010-10-26 19:48:49 +0000174 void paintPath(SkPaint::Style style, SkPath::FillType fill);
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000175 void strokePath();
176 void pushGS();
177 void popGS();
vandebo@chromium.org28be72b2010-11-11 21:37:00 +0000178 void setTextTransform(SkScalar x, SkScalar y, SkScalar textSkewX);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000179 void internalDrawBitmap(const SkMatrix& matrix, const SkBitmap& bitmap,
180 const SkPaint& paint);
181
vandebo@chromium.org7e2ff7c2010-11-03 23:55:28 +0000182 SkMatrix setTransform(const SkMatrix& matrix);
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000183};
184
185#endif