blob: fb74c63a1ba55dc12cf1d74e0a9d8e502771801d [file] [log] [blame]
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +00001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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"
23
24class SkPDFArray;
25class SkPDFDevice;
26class SkPDFDict;
27class SkPDFGraphicState;
28class SkPDFObject;
29class SkPDFStream;
30
31class SkPDFDeviceFactory : public SkDeviceFactory {
32 virtual SkDevice* newDevice(SkBitmap::Config config, int width, int height,
33 bool isOpaque, bool isForLayer);
34 virtual uint32_t getDeviceCapabilities() { return 0; }
35};
36
37/** \class SkPDFDevice
38
39 The drawing context for the PDF backend.
40*/
41class SkPDFDevice : public SkDevice {
42public:
43 /** Create a PDF drawing context with the given width and height.
44 * 72 points/in means letter paper is 612x792.
45 * @param width Page width in points.
46 * @param height Page height in points.
47 */
48 SkPDFDevice(int width, int height);
49 virtual ~SkPDFDevice();
50
51 virtual SkDeviceFactory* getDeviceFactory() {
52 return SkNEW(SkPDFDeviceFactory);
53 }
54
55 virtual int width() const { return fWidth; };
56
57 virtual int height() const { return fHeight; };
58
59 /** Called with the correct matrix and clip before this device is drawn
60 to using those settings. If your subclass overrides this, be sure to
61 call through to the base class as well.
62 */
63 virtual void setMatrixClip(const SkMatrix&, const SkRegion&);
64
65 /** These are called inside the per-device-layer loop for each draw call.
66 When these are called, we have already applied any saveLayer operations,
67 and are handling any looping from the paint, and any effects from the
68 DrawFilter.
69 */
70 virtual void drawPaint(const SkDraw&, const SkPaint& paint);
71 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
72 size_t count, const SkPoint[],
73 const SkPaint& paint);
74 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
75 virtual void drawPath(const SkDraw&, const SkPath& path,
76 const SkPaint& paint);
77 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
78 const SkMatrix& matrix, const SkPaint& paint);
79 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
80 const SkPaint& paint);
81 virtual void drawText(const SkDraw&, const void* text, size_t len,
82 SkScalar x, SkScalar y, const SkPaint& paint);
83 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
84 const SkScalar pos[], SkScalar constY,
85 int scalarsPerPos, const SkPaint& paint);
86 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
87 const SkPath& path, const SkMatrix* matrix,
88 const SkPaint& paint);
89 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
90 int vertexCount, const SkPoint verts[],
91 const SkPoint texs[], const SkColor colors[],
92 SkXfermode* xmode, const uint16_t indices[],
93 int indexCount, const SkPaint& paint);
94 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
95 const SkPaint&);
96
97 // PDF specific methods.
98
99 /** Returns a reference to the resource dictionary for this device.
100 */
101 const SkRefPtr<SkPDFDict>& getResourceDict();
102
103 /** Get the list of resouces (PDF objects) used on this page
104 * @param resouceList A list to append the resouces to.
105 */
106 void getResouces(SkTDArray<SkPDFObject*>* resouceList);
107
108 /** Returns the media box for this device.
109 */
110 SkRefPtr<SkPDFArray> getMediaBox();
111
112 /** Returns a string with the page contents.
vandebo@chromium.orgddbbd802010-10-26 19:45:06 +0000113 * @param flipOrigin Flip the origin between top and bottom.
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000114 */
vandebo@chromium.orgddbbd802010-10-26 19:45:06 +0000115 SkString content(bool flipOrigin) const;
vandebo@chromium.org9b49dc02010-10-20 22:23:29 +0000116
117private:
118 int fWidth;
119 int fHeight;
120 SkRefPtr<SkPDFDict> fResourceDict;
121
122 SkRefPtr<SkPDFGraphicState> fCurrentGraphicState;
123 SkColor fCurrentColor;
124 SkScalar fCurrentTextScaleX;
125 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
126 SkTDArray<SkPDFObject*> fXObjectResources;
127
128 SkString fContent;
129
130 // The last requested transforms from SkCanvas (setMatrixClip)
131 SkMatrix fCurTransform;
132
133 // The transform currently in effect in the PDF content stream.
134 SkMatrix fActiveTransform;
135
136 void updateGSFromPaint(const SkPaint& newPaint, SkString* textStaetUpdate);
137
138 void moveTo(SkScalar x, SkScalar y);
139 void appendLine(SkScalar x, SkScalar y);
140 void appendCubic(SkScalar ctl1X, SkScalar ctl1Y,
141 SkScalar ctl2X, SkScalar ctl2Y,
142 SkScalar dstX, SkScalar dstY);
143 void appendRectangle(SkScalar x, SkScalar y, SkScalar w, SkScalar h);
144 void closePath();
145 void strokePath();
146 void internalDrawBitmap(const SkMatrix& matrix, const SkBitmap& bitmap,
147 const SkPaint& paint);
148
149 void setTransform(const SkMatrix& matrix);
150 void setNoTransform();
151 void applyTempTransform(const SkMatrix& matrix);
152 void removeTempTransform();
153 void applyTransform(const SkMatrix& matrix);
154};
155
156#endif