OLD | NEW |
| (Empty) |
1 | |
2 /* | |
3 * Copyright 2011 Google Inc. | |
4 * | |
5 * Use of this source code is governed by a BSD-style license that can be | |
6 * found in the LICENSE file. | |
7 */ | |
8 | |
9 | |
10 #ifndef SkPDFDevice_DEFINED | |
11 #define SkPDFDevice_DEFINED | |
12 | |
13 #include "SkDevice.h" | |
14 #include "SkBitmap.h" | |
15 #include "SkCanvas.h" | |
16 #include "SkPaint.h" | |
17 #include "SkPath.h" | |
18 #include "SkPicture.h" | |
19 #include "SkRect.h" | |
20 #include "SkRefCnt.h" | |
21 #include "SkStream.h" | |
22 #include "SkTDArray.h" | |
23 #include "SkTemplates.h" | |
24 | |
25 class SkPDFArray; | |
26 class SkPDFDevice; | |
27 class SkPDFDict; | |
28 class SkPDFFont; | |
29 class SkPDFFormXObject; | |
30 class SkPDFGlyphSetMap; | |
31 class SkPDFGraphicState; | |
32 class SkPDFObject; | |
33 class SkPDFResourceDict; | |
34 class SkPDFShader; | |
35 class SkPDFStream; | |
36 class SkRRect; | |
37 template <typename T> class SkTSet; | |
38 | |
39 // Private classes. | |
40 struct ContentEntry; | |
41 struct GraphicStateEntry; | |
42 struct NamedDestination; | |
43 | |
44 /** \class SkPDFDevice | |
45 | |
46 The drawing context for the PDF backend. | |
47 */ | |
48 class SkPDFDevice : public SkBaseDevice { | |
49 public: | |
50 /** Create a PDF drawing context with the given width and height. | |
51 * 72 points/in means letter paper is 612x792. | |
52 * @param pageSize Page size in points. | |
53 * @param contentSize The content size of the page in points. This will be | |
54 * combined with the initial transform to determine the drawing area | |
55 * (as reported by the width and height methods). Anything outside | |
56 * of the drawing area will be clipped. | |
57 * @param initialTransform The initial transform to apply to the page. | |
58 * This may be useful to, for example, move the origin in and | |
59 * over a bit to account for a margin, scale the canvas, | |
60 * or apply a rotation. Note1: the SkPDFDevice also applies | |
61 * a scale+translate transform to move the origin from the | |
62 * bottom left (PDF default) to the top left. Note2: drawDevice | |
63 * (used by layer restore) draws the device after this initial | |
64 * transform is applied, so the PDF device does an | |
65 * inverse scale+translate to accommodate the one that SkPDFDevice | |
66 * always does. | |
67 */ | |
68 // Deprecated, please use SkDocument::CreatePdf() instead. | |
69 SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize, | |
70 const SkMatrix& initialTransform); | |
71 SK_API virtual ~SkPDFDevice(); | |
72 | |
73 /** 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 void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE; | |
79 void drawPoints(const SkDraw&, SkCanvas::PointMode mode, | |
80 size_t count, const SkPoint[], | |
81 const SkPaint& paint) SK_OVERRIDE; | |
82 void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint) SK_OVERR
IDE; | |
83 void drawOval(const SkDraw&, const SkRect& oval, const SkPaint& paint) SK_OV
ERRIDE; | |
84 void drawRRect(const SkDraw&, const SkRRect& rr, const SkPaint& paint) SK_OV
ERRIDE; | |
85 void drawPath(const SkDraw&, const SkPath& origpath, | |
86 const SkPaint& paint, const SkMatrix* prePathMatrix, | |
87 bool pathIsMutable) SK_OVERRIDE; | |
88 void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, | |
89 const SkRect* src, const SkRect& dst, | |
90 const SkPaint& paint, | |
91 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE; | |
92 void drawBitmap(const SkDraw&, const SkBitmap& bitmap, | |
93 const SkMatrix& matrix, const SkPaint&) SK_OVERRIDE; | |
94 void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y, | |
95 const SkPaint& paint) SK_OVERRIDE; | |
96 void drawText(const SkDraw&, const void* text, size_t len, | |
97 SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE; | |
98 void drawPosText(const SkDraw&, const void* text, size_t len, | |
99 const SkScalar pos[], int scalarsPerPos, | |
100 const SkPoint& offset, const SkPaint&) SK_OVERRIDE; | |
101 void drawTextOnPath(const SkDraw&, const void* text, size_t len, | |
102 const SkPath& path, const SkMatrix* matrix, | |
103 const SkPaint& paint) SK_OVERRIDE; | |
104 void drawVertices(const SkDraw&, SkCanvas::VertexMode, | |
105 int vertexCount, const SkPoint verts[], | |
106 const SkPoint texs[], const SkColor colors[], | |
107 SkXfermode* xmode, const uint16_t indices[], | |
108 int indexCount, const SkPaint& paint) SK_OVERRIDE; | |
109 void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, | |
110 const SkPaint&) SK_OVERRIDE; | |
111 | |
112 void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE; | |
113 void onDetachFromCanvas() SK_OVERRIDE; | |
114 SkImageInfo imageInfo() const SK_OVERRIDE; | |
115 | |
116 enum DrawingArea { | |
117 kContent_DrawingArea, // Drawing area for the page content. | |
118 kMargin_DrawingArea, // Drawing area for the margin content. | |
119 }; | |
120 | |
121 /** Sets the drawing area for the device. Subsequent draw calls are directed | |
122 * to the specific drawing area (margin or content). The default drawing | |
123 * area is the content drawing area. | |
124 * | |
125 * Currently if margin content is drawn and then a complex (for PDF) xfer | |
126 * mode is used, like SrcIn, Clear, etc, the margin content will get | |
127 * clipped. A simple way to avoid the bug is to always draw the margin | |
128 * content last. | |
129 */ | |
130 SK_API void setDrawingArea(DrawingArea drawingArea); | |
131 | |
132 /** Sets the DCTEncoder for images. | |
133 * @param encoder The encoder to encode a bitmap as JPEG (DCT). | |
134 * Result of encodings are cached, if the encoder changes the | |
135 * behaivor dynamically and an image is added to a second catalog, | |
136 * we will likely use the result of the first encoding call. | |
137 * By returning false from the encoder function, the encoder result | |
138 * is not used. | |
139 * Callers might not want to encode small images, as the time spent | |
140 * encoding and decoding might not be worth the space savings, | |
141 * if any at all. | |
142 */ | |
143 void setDCTEncoder(SkPicture::EncodeBitmap encoder) { | |
144 fEncoder = encoder; | |
145 } | |
146 | |
147 // PDF specific methods. | |
148 | |
149 /** Returns the resource dictionary for this device. | |
150 */ | |
151 SK_API SkPDFResourceDict* getResourceDict(); | |
152 | |
153 /** Get the fonts used on this device. | |
154 */ | |
155 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const; | |
156 | |
157 /** Add our named destinations to the supplied dictionary. | |
158 * @param dict Dictionary to add destinations to. | |
159 * @param page The PDF object representing the page for this device. | |
160 */ | |
161 void appendDestinations(SkPDFDict* dict, SkPDFObject* page); | |
162 | |
163 /** Returns a copy of the media box for this device. The caller is required | |
164 * to unref() this when it is finished. | |
165 */ | |
166 SK_API SkPDFArray* copyMediaBox() const; | |
167 | |
168 /** Get the annotations from this page, or NULL if there are none. | |
169 */ | |
170 SK_API SkPDFArray* getAnnotations() const { return fAnnotations; } | |
171 | |
172 /** Returns a SkStream with the page contents. The caller is responsible | |
173 for a reference to the returned value. | |
174 DEPRECATED: use copyContentToData() | |
175 */ | |
176 SK_API SkStream* content() const; | |
177 | |
178 /** Returns a SkStream with the page contents. The caller is responsible | |
179 * for calling data->unref() when it is finished. | |
180 */ | |
181 SK_API SkData* copyContentToData() const; | |
182 | |
183 SK_API const SkMatrix& initialTransform() const { | |
184 return fInitialTransform; | |
185 } | |
186 | |
187 /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font | |
188 * that shows on this device. | |
189 */ | |
190 const SkPDFGlyphSetMap& getFontGlyphUsage() const { | |
191 return *(fFontGlyphUsage.get()); | |
192 } | |
193 | |
194 | |
195 /** | |
196 * rasterDpi - the DPI at which features without native PDF support | |
197 * will be rasterized (e.g. draw image with perspective, | |
198 * draw text with perspective, ...) | |
199 * A larger DPI would create a PDF that reflects the original | |
200 * intent with better fidelity, but it can make for larger | |
201 * PDF files too, which would use more memory while rendering, | |
202 * and it would be slower to be processed or sent online or | |
203 * to printer. | |
204 */ | |
205 void setRasterDpi(SkScalar rasterDpi) { | |
206 fRasterDpi = rasterDpi; | |
207 } | |
208 | |
209 protected: | |
210 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE { | |
211 return fLegacyBitmap; | |
212 } | |
213 | |
214 virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) SK_
OVERRIDE; | |
215 | |
216 private: | |
217 // TODO(vandebo): push most of SkPDFDevice's state into a core object in | |
218 // order to get the right access levels without using friend. | |
219 friend class ScopedContentEntry; | |
220 | |
221 SkISize fPageSize; | |
222 SkISize fContentSize; | |
223 SkMatrix fInitialTransform; | |
224 SkClipStack fExistingClipStack; | |
225 SkRegion fExistingClipRegion; | |
226 SkPDFArray* fAnnotations; | |
227 SkPDFResourceDict* fResourceDict; | |
228 SkTDArray<NamedDestination*> fNamedDestinations; | |
229 | |
230 SkTDArray<SkPDFGraphicState*> fGraphicStateResources; | |
231 SkTDArray<SkPDFObject*> fXObjectResources; | |
232 SkTDArray<SkPDFFont*> fFontResources; | |
233 SkTDArray<SkPDFObject*> fShaderResources; | |
234 | |
235 SkAutoTDelete<ContentEntry> fContentEntries; | |
236 ContentEntry* fLastContentEntry; | |
237 SkAutoTDelete<ContentEntry> fMarginContentEntries; | |
238 ContentEntry* fLastMarginContentEntry; | |
239 DrawingArea fDrawingArea; | |
240 | |
241 const SkClipStack* fClipStack; | |
242 | |
243 // Accessor and setter functions based on the current DrawingArea. | |
244 SkAutoTDelete<ContentEntry>* getContentEntries(); | |
245 ContentEntry* getLastContentEntry(); | |
246 void setLastContentEntry(ContentEntry* contentEntry); | |
247 | |
248 // Glyph ids used for each font on this device. | |
249 SkAutoTDelete<SkPDFGlyphSetMap> fFontGlyphUsage; | |
250 | |
251 SkPicture::EncodeBitmap fEncoder; | |
252 SkScalar fRasterDpi; | |
253 | |
254 SkBitmap fLegacyBitmap; | |
255 | |
256 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack, | |
257 const SkRegion& existingClipRegion); | |
258 | |
259 // override from SkBaseDevice | |
260 virtual SkBaseDevice* onCreateCompatibleDevice(const CreateInfo&) SK_OVERRID
E; | |
261 | |
262 void init(); | |
263 void cleanUp(bool clearFontUsage); | |
264 SkPDFFormXObject* createFormXObjectFromDevice(); | |
265 | |
266 void drawFormXObjectWithMask(int xObjectIndex, | |
267 SkPDFFormXObject* mask, | |
268 const SkClipStack* clipStack, | |
269 const SkRegion& clipRegion, | |
270 SkXfermode::Mode mode, | |
271 bool invertClip); | |
272 | |
273 // If the paint or clip is such that we shouldn't draw anything, this | |
274 // returns NULL and does not create a content entry. | |
275 // setUpContentEntry and finishContentEntry can be used directly, but | |
276 // the preferred method is to use the ScopedContentEntry helper class. | |
277 ContentEntry* setUpContentEntry(const SkClipStack* clipStack, | |
278 const SkRegion& clipRegion, | |
279 const SkMatrix& matrix, | |
280 const SkPaint& paint, | |
281 bool hasText, | |
282 SkPDFFormXObject** dst); | |
283 void finishContentEntry(SkXfermode::Mode xfermode, | |
284 SkPDFFormXObject* dst, | |
285 SkPath* shape); | |
286 bool isContentEmpty(); | |
287 | |
288 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix, | |
289 const SkClipStack& clipStack, | |
290 const SkRegion& clipRegion, | |
291 const SkPaint& paint, | |
292 bool hasText, | |
293 GraphicStateEntry* entry); | |
294 int addGraphicStateResource(SkPDFGraphicState* gs); | |
295 int addXObjectResource(SkPDFObject* xObject); | |
296 | |
297 void updateFont(const SkPaint& paint, uint16_t glyphID, | |
298 ContentEntry* contentEntry); | |
299 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID); | |
300 | |
301 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry); | |
302 void internalDrawBitmap(const SkMatrix& matrix, | |
303 const SkClipStack* clipStack, | |
304 const SkRegion& clipRegion, | |
305 const SkBitmap& bitmap, | |
306 const SkIRect* srcRect, | |
307 const SkPaint& paint); | |
308 | |
309 /** Helper method for copyContentToData. It is responsible for copying the | |
310 * list of content entries |entry| to |data|. | |
311 */ | |
312 void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const; | |
313 | |
314 #ifdef SK_PDF_USE_PATHOPS | |
315 bool handleInversePath(const SkDraw& d, const SkPath& origPath, | |
316 const SkPaint& paint, bool pathIsMutable, | |
317 const SkMatrix* prePathMatrix = NULL); | |
318 #endif | |
319 bool handleRectAnnotation(const SkRect& r, const SkMatrix& matrix, | |
320 const SkPaint& paint); | |
321 bool handlePointAnnotation(const SkPoint* points, size_t count, | |
322 const SkMatrix& matrix, const SkPaint& paint); | |
323 SkPDFDict* createLinkAnnotation(const SkRect& r, const SkMatrix& matrix); | |
324 void handleLinkToURL(SkData* urlData, const SkRect& r, | |
325 const SkMatrix& matrix); | |
326 void handleLinkToNamedDest(SkData* nameData, const SkRect& r, | |
327 const SkMatrix& matrix); | |
328 void defineNamedDestination(SkData* nameData, const SkPoint& point, | |
329 const SkMatrix& matrix); | |
330 | |
331 typedef SkBaseDevice INHERITED; | |
332 | |
333 // TODO(edisonn): Only SkDocument_PDF and SkPDFImageShader should be able to
create | |
334 // an SkPDFDevice | |
335 //friend class SkDocument_PDF; | |
336 //friend class SkPDFImageShader; | |
337 }; | |
338 | |
339 #endif | |
OLD | NEW |