3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkPDFDevice_DEFINED
11 #define SkPDFDevice_DEFINED
13 #include "SkBitmapDevice.h"
18 #include "SkPicture.h"
22 #include "SkTDArray.h"
23 #include "SkTemplates.h"
29 class SkPDFFormXObject;
30 class SkPDFGlyphSetMap;
31 class SkPDFGraphicState;
33 class SkPDFResourceDict;
37 template <typename T> class SkTSet;
41 struct GraphicStateEntry;
42 struct NamedDestination;
44 /** \class SkPDFDevice
46 The drawing context for the PDF backend.
48 class SkPDFDevice : public SkBitmapDevice {
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
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();
73 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
75 virtual void clear(SkColor color) SK_OVERRIDE;
77 /** These are called inside the per-device-layer loop for each draw call.
78 When these are called, we have already applied any saveLayer operations,
79 and are handling any looping from the paint, and any effects from the
82 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
83 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
84 size_t count, const SkPoint[],
85 const SkPaint& paint) SK_OVERRIDE;
86 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
87 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
88 const SkPaint& paint) SK_OVERRIDE;
89 virtual void drawPath(const SkDraw&, const SkPath& origpath,
90 const SkPaint& paint, const SkMatrix* prePathMatrix,
91 bool pathIsMutable) SK_OVERRIDE;
92 virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
93 const SkRect* src, const SkRect& dst,
95 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
96 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
97 const SkMatrix& matrix, const SkPaint&) SK_OVERRIDE;
98 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
99 const SkPaint& paint) SK_OVERRIDE;
100 virtual void drawText(const SkDraw&, const void* text, size_t len,
101 SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
102 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
103 const SkScalar pos[], SkScalar constY,
104 int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
105 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
106 const SkPath& path, const SkMatrix* matrix,
107 const SkPaint& paint) SK_OVERRIDE;
108 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
109 int vertexCount, const SkPoint verts[],
110 const SkPoint texs[], const SkColor colors[],
111 SkXfermode* xmode, const uint16_t indices[],
112 int indexCount, const SkPaint& paint) SK_OVERRIDE;
113 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
114 const SkPaint&) SK_OVERRIDE;
116 virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
117 virtual void onDetachFromCanvas() SK_OVERRIDE;
120 kContent_DrawingArea, // Drawing area for the page content.
121 kMargin_DrawingArea, // Drawing area for the margin content.
124 /** Sets the drawing area for the device. Subsequent draw calls are directed
125 * to the specific drawing area (margin or content). The default drawing
126 * area is the content drawing area.
128 * Currently if margin content is drawn and then a complex (for PDF) xfer
129 * mode is used, like SrcIn, Clear, etc, the margin content will get
130 * clipped. A simple way to avoid the bug is to always draw the margin
133 SK_API void setDrawingArea(DrawingArea drawingArea);
135 /** Sets the DCTEncoder for images.
136 * @param encoder The encoder to encode a bitmap as JPEG (DCT).
137 * Result of encodings are cached, if the encoder changes the
138 * behaivor dynamically and an image is added to a second catalog,
139 * we will likely use the result of the first encoding call.
140 * By returning false from the encoder function, the encoder result
142 * Callers might not want to encode small images, as the time spent
143 * encoding and decoding might not be worth the space savings,
146 void setDCTEncoder(SkPicture::EncodeBitmap encoder) {
150 // PDF specific methods.
152 /** Returns the resource dictionary for this device.
154 SK_API SkPDFResourceDict* getResourceDict();
156 /** Get the fonts used on this device.
158 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
160 /** Add our named destinations to the supplied dictionary.
161 * @param dict Dictionary to add destinations to.
162 * @param page The PDF object representing the page for this device.
164 void appendDestinations(SkPDFDict* dict, SkPDFObject* page);
166 /** Returns a copy of the media box for this device. The caller is required
167 * to unref() this when it is finished.
169 SK_API SkPDFArray* copyMediaBox() const;
171 /** Get the annotations from this page, or NULL if there are none.
173 SK_API SkPDFArray* getAnnotations() const { return fAnnotations; }
175 /** Returns a SkStream with the page contents. The caller is responsible
176 for a reference to the returned value.
177 DEPRECATED: use copyContentToData()
179 SK_API SkStream* content() const;
181 /** Returns a SkStream with the page contents. The caller is responsible
182 * for calling data->unref() when it is finished.
184 SK_API SkData* copyContentToData() const;
186 SK_API const SkMatrix& initialTransform() const {
187 return fInitialTransform;
190 /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
191 * that shows on this device.
193 const SkPDFGlyphSetMap& getFontGlyphUsage() const {
194 return *(fFontGlyphUsage.get());
199 * rasterDpi - the DPI at which features without native PDF support
200 * will be rasterized (e.g. draw image with perspective,
201 * draw text with perspective, ...)
202 * A larger DPI would create a PDF that reflects the original
203 * intent with better fidelity, but it can make for larger
204 * PDF files too, which would use more memory while rendering,
205 * and it would be slower to be processed or sent online or
208 void setRasterDpi(SkScalar rasterDpi) {
209 fRasterDpi = rasterDpi;
213 virtual bool onReadPixels(const SkBitmap& bitmap, int x, int y,
214 SkCanvas::Config8888) SK_OVERRIDE;
216 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE;
219 // TODO(vandebo): push most of SkPDFDevice's state into a core object in
220 // order to get the right access levels without using friend.
221 friend class ScopedContentEntry;
224 SkISize fContentSize;
225 SkMatrix fInitialTransform;
226 SkClipStack fExistingClipStack;
227 SkRegion fExistingClipRegion;
228 SkPDFArray* fAnnotations;
229 SkPDFResourceDict* fResourceDict;
230 SkTDArray<NamedDestination*> fNamedDestinations;
232 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
233 SkTDArray<SkPDFObject*> fXObjectResources;
234 SkTDArray<SkPDFFont*> fFontResources;
235 SkTDArray<SkPDFObject*> fShaderResources;
237 SkAutoTDelete<ContentEntry> fContentEntries;
238 ContentEntry* fLastContentEntry;
239 SkAutoTDelete<ContentEntry> fMarginContentEntries;
240 ContentEntry* fLastMarginContentEntry;
241 DrawingArea fDrawingArea;
243 const SkClipStack* fClipStack;
245 // Accessor and setter functions based on the current DrawingArea.
246 SkAutoTDelete<ContentEntry>* getContentEntries();
247 ContentEntry* getLastContentEntry();
248 void setLastContentEntry(ContentEntry* contentEntry);
250 // Glyph ids used for each font on this device.
251 SkAutoTDelete<SkPDFGlyphSetMap> fFontGlyphUsage;
253 SkPicture::EncodeBitmap fEncoder;
256 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
257 const SkRegion& existingClipRegion);
259 // override from SkBaseDevice
260 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
261 int width, int height,
263 Usage usage) SK_OVERRIDE;
266 void cleanUp(bool clearFontUsage);
267 SkPDFFormXObject* createFormXObjectFromDevice();
269 void drawFormXObjectWithMask(int xObjectIndex,
270 SkPDFFormXObject* mask,
271 const SkClipStack* clipStack,
272 const SkRegion& clipRegion,
273 SkXfermode::Mode mode,
276 // If the paint or clip is such that we shouldn't draw anything, this
277 // returns NULL and does not create a content entry.
278 // setUpContentEntry and finishContentEntry can be used directly, but
279 // the preferred method is to use the ScopedContentEntry helper class.
280 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
281 const SkRegion& clipRegion,
282 const SkMatrix& matrix,
283 const SkPaint& paint,
285 SkPDFFormXObject** dst);
286 void finishContentEntry(SkXfermode::Mode xfermode,
287 SkPDFFormXObject* dst,
289 bool isContentEmpty();
291 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
292 const SkClipStack& clipStack,
293 const SkRegion& clipRegion,
294 const SkPaint& paint,
296 GraphicStateEntry* entry);
297 int addGraphicStateResource(SkPDFGraphicState* gs);
298 int addXObjectResource(SkPDFObject* xObject);
300 void updateFont(const SkPaint& paint, uint16_t glyphID,
301 ContentEntry* contentEntry);
302 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
304 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
305 void internalDrawBitmap(const SkMatrix& matrix,
306 const SkClipStack* clipStack,
307 const SkRegion& clipRegion,
308 const SkBitmap& bitmap,
309 const SkIRect* srcRect,
310 const SkPaint& paint);
312 /** Helper method for copyContentToData. It is responsible for copying the
313 * list of content entries |entry| to |data|.
315 void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
317 #ifdef SK_PDF_USE_PATHOPS
318 bool handleInversePath(const SkDraw& d, const SkPath& origPath,
319 const SkPaint& paint, bool pathIsMutable,
320 const SkMatrix* prePathMatrix = NULL);
322 bool handleRectAnnotation(const SkRect& r, const SkMatrix& matrix,
323 const SkPaint& paint);
324 bool handlePointAnnotation(const SkPoint* points, size_t count,
325 const SkMatrix& matrix, const SkPaint& paint);
326 SkPDFDict* createLinkAnnotation(const SkRect& r, const SkMatrix& matrix);
327 void handleLinkToURL(SkData* urlData, const SkRect& r,
328 const SkMatrix& matrix);
329 void handleLinkToNamedDest(SkData* nameData, const SkRect& r,
330 const SkMatrix& matrix);
331 void defineNamedDestination(SkData* nameData, const SkPoint& point,
332 const SkMatrix& matrix);
334 typedef SkBitmapDevice INHERITED;
336 // TODO(edisonn): Only SkDocument_PDF and SkPDFImageShader should be able to create
338 //friend class SkDocument_PDF;
339 //friend class SkPDFImageShader;