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
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 SkBaseDevice {
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 void clear(SkColor color) SK_OVERRIDE;
75 /** These are called inside the per-device-layer loop for each draw call.
76 When these are called, we have already applied any saveLayer operations,
77 and are handling any looping from the paint, and any effects from the
80 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
81 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
82 size_t count, const SkPoint[],
83 const SkPaint& paint) SK_OVERRIDE;
84 virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
85 virtual void drawOval(const SkDraw&, const SkRect& oval, const SkPaint& paint) SK_OVERRIDE;
86 virtual void drawRRect(const SkDraw&, const SkRRect& rr, const SkPaint& paint) SK_OVERRIDE;
87 virtual void drawPath(const SkDraw&, const SkPath& origpath,
88 const SkPaint& paint, const SkMatrix* prePathMatrix,
89 bool pathIsMutable) SK_OVERRIDE;
90 virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
91 const SkRect* src, const SkRect& dst,
93 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE;
94 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
95 const SkMatrix& matrix, const SkPaint&) SK_OVERRIDE;
96 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
97 const SkPaint& paint) SK_OVERRIDE;
98 virtual void drawText(const SkDraw&, const void* text, size_t len,
99 SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
100 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
101 const SkScalar pos[], SkScalar constY,
102 int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
103 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
104 const SkPath& path, const SkMatrix* matrix,
105 const SkPaint& paint) SK_OVERRIDE;
106 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
107 int vertexCount, const SkPoint verts[],
108 const SkPoint texs[], const SkColor colors[],
109 SkXfermode* xmode, const uint16_t indices[],
110 int indexCount, const SkPaint& paint) SK_OVERRIDE;
111 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
112 const SkPaint&) SK_OVERRIDE;
114 virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
115 virtual void onDetachFromCanvas() SK_OVERRIDE;
116 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
119 kContent_DrawingArea, // Drawing area for the page content.
120 kMargin_DrawingArea, // Drawing area for the margin content.
123 /** Sets the drawing area for the device. Subsequent draw calls are directed
124 * to the specific drawing area (margin or content). The default drawing
125 * area is the content drawing area.
127 * Currently if margin content is drawn and then a complex (for PDF) xfer
128 * mode is used, like SrcIn, Clear, etc, the margin content will get
129 * clipped. A simple way to avoid the bug is to always draw the margin
132 SK_API void setDrawingArea(DrawingArea drawingArea);
134 /** Sets the DCTEncoder for images.
135 * @param encoder The encoder to encode a bitmap as JPEG (DCT).
136 * Result of encodings are cached, if the encoder changes the
137 * behaivor dynamically and an image is added to a second catalog,
138 * we will likely use the result of the first encoding call.
139 * By returning false from the encoder function, the encoder result
141 * Callers might not want to encode small images, as the time spent
142 * encoding and decoding might not be worth the space savings,
145 void setDCTEncoder(SkPicture::EncodeBitmap encoder) {
149 // PDF specific methods.
151 /** Returns the resource dictionary for this device.
153 SK_API SkPDFResourceDict* getResourceDict();
155 /** Get the fonts used on this device.
157 SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
159 /** Add our named destinations to the supplied dictionary.
160 * @param dict Dictionary to add destinations to.
161 * @param page The PDF object representing the page for this device.
163 void appendDestinations(SkPDFDict* dict, SkPDFObject* page);
165 /** Returns a copy of the media box for this device. The caller is required
166 * to unref() this when it is finished.
168 SK_API SkPDFArray* copyMediaBox() const;
170 /** Get the annotations from this page, or NULL if there are none.
172 SK_API SkPDFArray* getAnnotations() const { return fAnnotations; }
174 /** Returns a SkStream with the page contents. The caller is responsible
175 for a reference to the returned value.
176 DEPRECATED: use copyContentToData()
178 SK_API SkStream* content() const;
180 /** Returns a SkStream with the page contents. The caller is responsible
181 * for calling data->unref() when it is finished.
183 SK_API SkData* copyContentToData() const;
185 SK_API const SkMatrix& initialTransform() const {
186 return fInitialTransform;
189 /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
190 * that shows on this device.
192 const SkPDFGlyphSetMap& getFontGlyphUsage() const {
193 return *(fFontGlyphUsage.get());
198 * rasterDpi - the DPI at which features without native PDF support
199 * will be rasterized (e.g. draw image with perspective,
200 * draw text with perspective, ...)
201 * A larger DPI would create a PDF that reflects the original
202 * intent with better fidelity, but it can make for larger
203 * PDF files too, which would use more memory while rendering,
204 * and it would be slower to be processed or sent online or
207 void setRasterDpi(SkScalar rasterDpi) {
208 fRasterDpi = rasterDpi;
212 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
213 return fLegacyBitmap;
216 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE {
220 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
223 // TODO(vandebo): push most of SkPDFDevice's state into a core object in
224 // order to get the right access levels without using friend.
225 friend class ScopedContentEntry;
228 SkISize fContentSize;
229 SkMatrix fInitialTransform;
230 SkClipStack fExistingClipStack;
231 SkRegion fExistingClipRegion;
232 SkPDFArray* fAnnotations;
233 SkPDFResourceDict* fResourceDict;
234 SkTDArray<NamedDestination*> fNamedDestinations;
236 SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
237 SkTDArray<SkPDFObject*> fXObjectResources;
238 SkTDArray<SkPDFFont*> fFontResources;
239 SkTDArray<SkPDFObject*> fShaderResources;
241 SkAutoTDelete<ContentEntry> fContentEntries;
242 ContentEntry* fLastContentEntry;
243 SkAutoTDelete<ContentEntry> fMarginContentEntries;
244 ContentEntry* fLastMarginContentEntry;
245 DrawingArea fDrawingArea;
247 const SkClipStack* fClipStack;
249 // Accessor and setter functions based on the current DrawingArea.
250 SkAutoTDelete<ContentEntry>* getContentEntries();
251 ContentEntry* getLastContentEntry();
252 void setLastContentEntry(ContentEntry* contentEntry);
254 // Glyph ids used for each font on this device.
255 SkAutoTDelete<SkPDFGlyphSetMap> fFontGlyphUsage;
257 SkPicture::EncodeBitmap fEncoder;
260 SkBitmap fLegacyBitmap;
262 SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
263 const SkRegion& existingClipRegion);
265 // override from SkBaseDevice
266 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
269 void cleanUp(bool clearFontUsage);
270 SkPDFFormXObject* createFormXObjectFromDevice();
272 void drawFormXObjectWithMask(int xObjectIndex,
273 SkPDFFormXObject* mask,
274 const SkClipStack* clipStack,
275 const SkRegion& clipRegion,
276 SkXfermode::Mode mode,
279 // If the paint or clip is such that we shouldn't draw anything, this
280 // returns NULL and does not create a content entry.
281 // setUpContentEntry and finishContentEntry can be used directly, but
282 // the preferred method is to use the ScopedContentEntry helper class.
283 ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
284 const SkRegion& clipRegion,
285 const SkMatrix& matrix,
286 const SkPaint& paint,
288 SkPDFFormXObject** dst);
289 void finishContentEntry(SkXfermode::Mode xfermode,
290 SkPDFFormXObject* dst,
292 bool isContentEmpty();
294 void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
295 const SkClipStack& clipStack,
296 const SkRegion& clipRegion,
297 const SkPaint& paint,
299 GraphicStateEntry* entry);
300 int addGraphicStateResource(SkPDFGraphicState* gs);
301 int addXObjectResource(SkPDFObject* xObject);
303 void updateFont(const SkPaint& paint, uint16_t glyphID,
304 ContentEntry* contentEntry);
305 int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
307 void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
308 void internalDrawBitmap(const SkMatrix& matrix,
309 const SkClipStack* clipStack,
310 const SkRegion& clipRegion,
311 const SkBitmap& bitmap,
312 const SkIRect* srcRect,
313 const SkPaint& paint);
315 /** Helper method for copyContentToData. It is responsible for copying the
316 * list of content entries |entry| to |data|.
318 void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
320 #ifdef SK_PDF_USE_PATHOPS
321 bool handleInversePath(const SkDraw& d, const SkPath& origPath,
322 const SkPaint& paint, bool pathIsMutable,
323 const SkMatrix* prePathMatrix = NULL);
325 bool handleRectAnnotation(const SkRect& r, const SkMatrix& matrix,
326 const SkPaint& paint);
327 bool handlePointAnnotation(const SkPoint* points, size_t count,
328 const SkMatrix& matrix, const SkPaint& paint);
329 SkPDFDict* createLinkAnnotation(const SkRect& r, const SkMatrix& matrix);
330 void handleLinkToURL(SkData* urlData, const SkRect& r,
331 const SkMatrix& matrix);
332 void handleLinkToNamedDest(SkData* nameData, const SkRect& r,
333 const SkMatrix& matrix);
334 void defineNamedDestination(SkData* nameData, const SkPoint& point,
335 const SkMatrix& matrix);
337 typedef SkBaseDevice INHERITED;
339 // TODO(edisonn): Only SkDocument_PDF and SkPDFImageShader should be able to create
341 //friend class SkDocument_PDF;
342 //friend class SkPDFImageShader;