• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkCanvas.h"
14 #include "SkDevice.h"
15 #include "SkPaint.h"
16 #include "SkPath.h"
17 #include "SkRefCnt.h"
18 #include "SkStream.h"
19 #include "SkTScopedPtr.h"
20 
21 class SkPDFArray;
22 class SkPDFDevice;
23 class SkPDFDict;
24 class SkPDFFont;
25 class SkPDFFormXObject;
26 class SkPDFGlyphSetMap;
27 class SkPDFGraphicState;
28 class SkPDFObject;
29 class SkPDFShader;
30 class SkPDFStream;
31 
32 // Private classes.
33 struct ContentEntry;
34 struct GraphicStateEntry;
35 
36 /** \class SkPDFDevice
37 
38     The drawing context for the PDF backend.
39 */
40 class SkPDFDevice : public SkDevice {
41 public:
42     /** Create a PDF drawing context with the given width and height.
43      *  72 points/in means letter paper is 612x792.
44      *  @param pageSize Page size in points.
45      *  @param contentSize The content size of the page in points. This will be
46      *         combined with the initial transform to determine the drawing area
47      *         (as reported by the width and height methods). Anything outside
48      *         of the drawing area will be clipped.
49      *  @param initialTransform The initial transform to apply to the page.
50      *         This may be useful to, for example, move the origin in and
51      *         over a bit to account for a margin, scale the canvas,
52      *         or apply a rotation.  Note1: the SkPDFDevice also applies
53      *         a scale+translate transform to move the origin from the
54      *         bottom left (PDF default) to the top left.  Note2: drawDevice
55      *         (used by layer restore) draws the device after this initial
56      *         transform is applied, so the PDF device does an
57      *         inverse scale+translate to accommodate the one that SkPDFDevice
58      *         always does.
59      */
60     // TODO(vandebo): The sizes should be SkSize and not SkISize.
61     SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
62                        const SkMatrix& initialTransform);
63     SK_API virtual ~SkPDFDevice();
64 
65     virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
66 
67     virtual void clear(SkColor color) SK_OVERRIDE;
68 
69     /** These are called inside the per-device-layer loop for each draw call.
70      When these are called, we have already applied any saveLayer operations,
71      and are handling any looping from the paint, and any effects from the
72      DrawFilter.
73      */
74     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
75     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
76                             size_t count, const SkPoint[],
77                             const SkPaint& paint) SK_OVERRIDE;
78     virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
79     virtual void drawPath(const SkDraw&, const SkPath& origpath,
80                           const SkPaint& paint, const SkMatrix* prePathMatrix,
81                           bool pathIsMutable) SK_OVERRIDE;
82     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
83                             const SkIRect* srcRectOrNull,
84                             const SkMatrix& matrix, const SkPaint&) SK_OVERRIDE;
85     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
86                             const SkPaint& paint) SK_OVERRIDE;
87     virtual void drawText(const SkDraw&, const void* text, size_t len,
88                           SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
89     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
90                              const SkScalar pos[], SkScalar constY,
91                              int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
92     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
93                                 const SkPath& path, const SkMatrix* matrix,
94                                 const SkPaint& paint) SK_OVERRIDE;
95     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
96                               int vertexCount, const SkPoint verts[],
97                               const SkPoint texs[], const SkColor colors[],
98                               SkXfermode* xmode, const uint16_t indices[],
99                               int indexCount, const SkPaint& paint) SK_OVERRIDE;
100     virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
101                             const SkPaint&) SK_OVERRIDE;
102 
103     enum DrawingArea {
104         kContent_DrawingArea,  // Drawing area for the page content.
105         kMargin_DrawingArea,   // Drawing area for the margin content.
106     };
107 
108     /** Sets the drawing area for the device. Subsequent draw calls are directed
109      *  to the specific drawing area (margin or content). The default drawing
110      *  area is the content drawing area.
111      *
112      *  Currently if margin content is drawn and then a complex (for PDF) xfer
113      *  mode is used, like SrcIn, Clear, etc, the margin content will get
114      *  clipped. A simple way to avoid the bug is to always draw the margin
115      *  content last.
116      */
117     SK_API void setDrawingArea(DrawingArea drawingArea);
118 
119     // PDF specific methods.
120 
121     /** Returns the resource dictionary for this device.
122      */
123     SK_API SkPDFDict* getResourceDict();
124 
125     /** Get the list of resources (PDF objects) used on this page.
126      *  @param resourceList A list to append the resources to.
127      */
128     SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList) const;
129 
130     /** Get the fonts used on this device.
131      */
132     SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
133 
134     /** Returns the media box for this device.
135      */
136     SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
137 
138     /** Returns a SkStream with the page contents.  The caller is responsible
139         for a reference to the returned value.
140         DEPRECATED: use copyContentToData()
141      */
142     SK_API SkStream* content() const;
143 
144     /** Returns a SkStream with the page contents.  The caller is responsible
145      *  for calling data->unref() when it is finished.
146      */
147     SK_API SkData* copyContentToData() const;
148 
initialTransform()149     SK_API const SkMatrix& initialTransform() const {
150         return fInitialTransform;
151     }
152 
153     /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
154      *  that shows on this device.
155      */
getFontGlyphUsage()156     const SkPDFGlyphSetMap& getFontGlyphUsage() const {
157         return *(fFontGlyphUsage.get());
158     }
159 
160 protected:
161     virtual bool onReadPixels(const SkBitmap& bitmap, int x, int y,
162                               SkCanvas::Config8888) SK_OVERRIDE;
163 
164     virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE;
165 
166 private:
167     // TODO(vandebo): push most of SkPDFDevice's state into a core object in
168     // order to get the right access levels without using friend.
169     friend class ScopedContentEntry;
170 
171     SkISize fPageSize;
172     SkISize fContentSize;
173     SkMatrix fInitialTransform;
174     SkClipStack fExistingClipStack;
175     SkRegion fExistingClipRegion;
176     SkRefPtr<SkPDFDict> fResourceDict;
177 
178     SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
179     SkTDArray<SkPDFObject*> fXObjectResources;
180     SkTDArray<SkPDFFont*> fFontResources;
181     SkTDArray<SkPDFObject*> fShaderResources;
182 
183     SkTScopedPtr<ContentEntry> fContentEntries;
184     ContentEntry* fLastContentEntry;
185     SkTScopedPtr<ContentEntry> fMarginContentEntries;
186     ContentEntry* fLastMarginContentEntry;
187     DrawingArea fDrawingArea;
188 
189     // Accessor and setter functions based on the current DrawingArea.
190     SkTScopedPtr<ContentEntry>* getContentEntries();
191     ContentEntry* getLastContentEntry();
192     void setLastContentEntry(ContentEntry* contentEntry);
193 
194     // Glyph ids used for each font on this device.
195     SkTScopedPtr<SkPDFGlyphSetMap> fFontGlyphUsage;
196 
197     SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
198                 const SkRegion& existingClipRegion);
199 
200     // override from SkDevice
201     virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
202                                                int width, int height,
203                                                bool isOpaque,
204                                                Usage usage) SK_OVERRIDE;
205 
206     void init();
207     void cleanUp(bool clearFontUsage);
208     void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
209 
210     // Clear the passed clip from all existing content entries.
211     void clearClipFromContent(const SkClipStack* clipStack,
212                               const SkRegion& clipRegion);
213     void drawFormXObjectWithClip(SkPDFFormXObject* form,
214                                  const SkClipStack* clipStack,
215                                  const SkRegion& clipRegion,
216                                  bool invertClip);
217 
218     // If the paint or clip is such that we shouldn't draw anything, this
219     // returns NULL and does not create a content entry.
220     // setUpContentEntry and finishContentEntry can be used directly, but
221     // the preferred method is to use the ScopedContentEntry helper class.
222     ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
223                                     const SkRegion& clipRegion,
224                                     const SkMatrix& matrix,
225                                     const SkPaint& paint,
226                                     bool hasText,
227                                     SkRefPtr<SkPDFFormXObject>* dst);
228     void finishContentEntry(SkXfermode::Mode xfermode,
229                             SkPDFFormXObject* dst);
230     bool isContentEmpty();
231 
232     void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
233                                             const SkClipStack& clipStack,
234                                             const SkRegion& clipRegion,
235                                             const SkPaint& paint,
236                                             bool hasText,
237                                             GraphicStateEntry* entry);
238     int addGraphicStateResource(SkPDFGraphicState* gs);
239 
240     void updateFont(const SkPaint& paint, uint16_t glyphID,
241                     ContentEntry* contentEntry);
242     int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
243 
244     void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
245     void internalDrawBitmap(const SkMatrix& matrix,
246                             const SkClipStack* clipStack,
247                             const SkRegion& clipRegion,
248                             const SkBitmap& bitmap,
249                             const SkIRect* srcRect,
250                             const SkPaint& paint);
251 
252     /** Helper method for copyContentToData. It is responsible for copying the
253      *  list of content entries |entry| to |data|.
254      */
255     void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
256 
257     // Disable the default copy and assign implementation.
258     SkPDFDevice(const SkPDFDevice&);
259     void operator=(const SkPDFDevice&);
260 };
261 
262 #endif
263