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