vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1 | /* |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 Google Inc. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SkPDFDevice_DEFINED |
| 18 | #define SkPDFDevice_DEFINED |
| 19 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 20 | #include "SkCanvas.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 21 | #include "SkDevice.h" |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 22 | #include "SkPaint.h" |
| 23 | #include "SkPath.h" |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 24 | #include "SkRefCnt.h" |
| 25 | #include "SkStream.h" |
| 26 | #include "SkTScopedPtr.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 27 | |
| 28 | class SkPDFArray; |
| 29 | class SkPDFDevice; |
| 30 | class SkPDFDict; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 31 | class SkPDFFont; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 32 | class SkPDFFormXObject; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 33 | class SkPDFGraphicState; |
| 34 | class SkPDFObject; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 35 | class SkPDFShader; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 36 | class SkPDFStream; |
| 37 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 38 | // Private classes. |
| 39 | struct ContentEntry; |
| 40 | struct GraphicStateEntry; |
| 41 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 42 | /** \class SkPDFDevice |
| 43 | |
| 44 | The drawing context for the PDF backend. |
| 45 | */ |
| 46 | class SkPDFDevice : public SkDevice { |
| 47 | public: |
| 48 | /** Create a PDF drawing context with the given width and height. |
| 49 | * 72 points/in means letter paper is 612x792. |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 50 | * @param pageSize Page size in points. |
| 51 | * @param contentSize The content size of the page in points. This will be |
| 52 | * combined with the initial transform to determine the drawing area |
| 53 | * (as reported by the width and height methods). Anything outside |
| 54 | * of the drawing area will be clipped. |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 55 | * @param initialTransform The initial transform to apply to the page. |
| 56 | * This may be useful to, for example, move the origin in and |
| 57 | * over a bit to account for a margin, scale the canvas, |
| 58 | * or apply a rotation. Note1: the SkPDFDevice also applies |
| 59 | * a scale+translate transform to move the origin from the |
| 60 | * bottom left (PDF default) to the top left. Note2: drawDevice |
| 61 | * (used by layer restore) draws the device after this initial |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 62 | * transform is applied, so the PDF device does an |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 63 | * inverse scale+translate to accommodate the one that SkPDFDevice |
| 64 | * always does. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 65 | */ |
vandebo@chromium.org | be2048a | 2011-05-02 15:24:01 +0000 | [diff] [blame] | 66 | // TODO(vandebo) The sizes should be SkSize and not SkISize. |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 67 | SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize, |
| 68 | const SkMatrix& initialTransform); |
| 69 | SK_API virtual ~SkPDFDevice(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 70 | |
vandebo@chromium.org | 35fc62b | 2010-10-26 19:47:30 +0000 | [diff] [blame] | 71 | virtual uint32_t getDeviceCapabilities() { return kVector_Capability; } |
| 72 | |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 73 | virtual void clear(SkColor color); |
| 74 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 75 | virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
| 76 | return false; |
| 77 | } |
| 78 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 79 | /** These are called inside the per-device-layer loop for each draw call. |
| 80 | When these are called, we have already applied any saveLayer operations, |
| 81 | and are handling any looping from the paint, and any effects from the |
| 82 | DrawFilter. |
| 83 | */ |
| 84 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 85 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, |
| 86 | size_t count, const SkPoint[], |
| 87 | const SkPaint& paint); |
| 88 | virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 89 | virtual void drawPath(const SkDraw&, const SkPath& origpath, |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 90 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 91 | bool pathIsMutable); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 92 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 93 | const SkIRect* srcRectOrNull, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 94 | const SkMatrix& matrix, const SkPaint& paint); |
| 95 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y, |
| 96 | const SkPaint& paint); |
| 97 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 98 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 99 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 100 | const SkScalar pos[], SkScalar constY, |
| 101 | int scalarsPerPos, const SkPaint& paint); |
| 102 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 103 | const SkPath& path, const SkMatrix* matrix, |
| 104 | const SkPaint& paint); |
| 105 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, |
| 106 | int vertexCount, const SkPoint verts[], |
| 107 | const SkPoint texs[], const SkColor colors[], |
| 108 | SkXfermode* xmode, const uint16_t indices[], |
| 109 | int indexCount, const SkPaint& paint); |
| 110 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 111 | const SkPaint&); |
| 112 | |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 113 | enum DrawingArea { |
| 114 | kContent_DrawingArea, // Drawing area for the page content. |
| 115 | kMargin_DrawingArea, // Drawing area for the margin content. |
| 116 | }; |
| 117 | |
| 118 | /** Sets the drawing area for the device. Subsequent draw calls are directed |
| 119 | * to the specific drawing area (margin or content). The default drawing |
| 120 | * area is the content drawing area. |
| 121 | */ |
| 122 | void setDrawingArea(DrawingArea drawingArea); |
| 123 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 124 | // PDF specific methods. |
| 125 | |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame^] | 126 | /** Returns the resource dictionary for this device. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 127 | */ |
reed@google.com | 1feb330 | 2011-07-20 18:43:19 +0000 | [diff] [blame^] | 128 | SK_API SkPDFDict* getResourceDict(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 129 | |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 130 | /** Get the list of resources (PDF objects) used on this page. |
| 131 | * @param resourceList A list to append the resources to. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 132 | */ |
vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 133 | SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList) const; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 134 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 135 | /** Get the fonts used on this device. |
| 136 | */ |
vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 137 | SK_API const SkTDArray<SkPDFFont*>& getFontResources() const; |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 138 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 139 | /** Returns the media box for this device. |
| 140 | */ |
vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 141 | SK_API SkRefPtr<SkPDFArray> getMediaBox() const; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 142 | |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 143 | /** Returns a SkStream with the page contents. The caller is responsible |
| 144 | for a reference to the returned value. |
reed@google.com | 5667afc | 2011-06-27 14:42:15 +0000 | [diff] [blame] | 145 | DEPRECATED: use copyContentToData() |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 146 | */ |
vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 147 | SK_API SkStream* content() const; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 148 | |
reed@google.com | 5667afc | 2011-06-27 14:42:15 +0000 | [diff] [blame] | 149 | /** Returns a SkStream with the page contents. The caller is responsible |
| 150 | * for calling data->unref() when it is finished. |
| 151 | */ |
| 152 | SK_API SkData* copyContentToData() const; |
| 153 | |
vandebo@chromium.org | 3509f05 | 2011-05-30 20:52:33 +0000 | [diff] [blame] | 154 | SK_API const SkMatrix& initialTransform() const { |
| 155 | return fInitialTransform; |
| 156 | } |
vandebo@chromium.org | 61d2678 | 2011-05-24 23:02:07 +0000 | [diff] [blame] | 157 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 158 | private: |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 159 | // TODO(vandebo) push most of SkPDFDevice's state into a core object in |
| 160 | // order to get the right access levels without using friend. |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 161 | friend class ScopedContentEntry; |
vandebo@chromium.org | a0c7edb | 2011-05-09 07:58:08 +0000 | [diff] [blame] | 162 | |
ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame] | 163 | SkISize fPageSize; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 164 | SkISize fContentSize; |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 165 | SkMatrix fInitialTransform; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 166 | SkClipStack fExistingClipStack; |
| 167 | SkRegion fExistingClipRegion; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 168 | SkRefPtr<SkPDFDict> fResourceDict; |
| 169 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 170 | SkTDArray<SkPDFGraphicState*> fGraphicStateResources; |
| 171 | SkTDArray<SkPDFObject*> fXObjectResources; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 172 | SkTDArray<SkPDFFont*> fFontResources; |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 173 | SkTDArray<SkPDFObject*> fShaderResources; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 174 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 175 | SkTScopedPtr<ContentEntry> fContentEntries; |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 176 | ContentEntry* fLastContentEntry; |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 177 | SkTScopedPtr<ContentEntry> fMarginContentEntries; |
| 178 | ContentEntry* fLastMarginContentEntry; |
| 179 | DrawingArea fDrawingArea; |
| 180 | |
| 181 | // Accessor and setter functions based on the current DrawingArea. |
| 182 | SkTScopedPtr<ContentEntry>& getContentEntries(); |
| 183 | ContentEntry* getLastContentEntry(); |
| 184 | void setLastContentEntry(ContentEntry* contentEntry); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 185 | |
vandebo@chromium.org | a0c7edb | 2011-05-09 07:58:08 +0000 | [diff] [blame] | 186 | SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack, |
| 187 | const SkRegion& existingClipRegion); |
| 188 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 189 | // override from SkDevice |
| 190 | virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config, |
| 191 | int width, int height, |
| 192 | bool isOpaque, |
| 193 | Usage usage); |
| 194 | |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 195 | void init(); |
| 196 | void cleanUp(); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 197 | void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 198 | |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 199 | // Clear the passed clip from all existing content entries. |
| 200 | void clearClipFromContent(const SkClipStack* clipStack, |
| 201 | const SkRegion& clipRegion); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 202 | void drawFormXObjectWithClip(SkPDFFormXObject* form, |
| 203 | const SkClipStack* clipStack, |
| 204 | const SkRegion& clipRegion, |
| 205 | bool invertClip); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 206 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 207 | // If the paint or clip is such that we shouldn't draw anything, this |
| 208 | // returns NULL and does not create a content entry. |
| 209 | // setUpContentEntry and finishContentEntry can be used directly, but |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 210 | // the preferred method is to use the ScopedContentEntry helper class. |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 211 | ContentEntry* setUpContentEntry(const SkClipStack* clipStack, |
| 212 | const SkRegion& clipRegion, |
| 213 | const SkMatrix& matrix, |
| 214 | const SkPaint& paint, |
| 215 | bool hasText, |
| 216 | SkRefPtr<SkPDFFormXObject>* dst); |
| 217 | void finishContentEntry(SkXfermode::Mode xfermode, |
| 218 | SkPDFFormXObject* dst); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 219 | bool isContentEmpty(); |
| 220 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 221 | void populateGraphicStateEntryFromPaint(const SkMatrix& matrix, |
| 222 | const SkClipStack& clipStack, |
| 223 | const SkRegion& clipRegion, |
| 224 | const SkPaint& paint, |
| 225 | bool hasText, |
| 226 | GraphicStateEntry* entry); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 227 | int addGraphicStateResource(SkPDFGraphicState* gs); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 228 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 229 | void updateFont(const SkPaint& paint, uint16_t glyphID, |
| 230 | ContentEntry* contentEntry); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 231 | int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 232 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 233 | void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 234 | void internalDrawBitmap(const SkMatrix& matrix, |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 235 | const SkClipStack* clipStack, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 236 | const SkRegion& clipRegion, |
| 237 | const SkBitmap& bitmap, |
| 238 | const SkIRect* srcRect, |
| 239 | const SkPaint& paint); |
| 240 | |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 241 | /** Helper method for copyContentToData. It is responsible for copying the |
| 242 | * list of content entries |entry| to |data|. |
| 243 | */ |
| 244 | void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const; |
| 245 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 246 | // Disable the default copy and assign implementation. |
| 247 | SkPDFDevice(const SkPDFDevice&); |
| 248 | void operator=(const SkPDFDevice&); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | #endif |