| 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 | |
| 20 | #include "SkRefCnt.h" |
| 21 | #include "SkDevice.h" |
| vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 22 | #include "SkStream.h" |
| vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 23 | #include "SkPaint.h" |
| 24 | #include "SkPath.h" |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 25 | |
| 26 | class SkPDFArray; |
| 27 | class SkPDFDevice; |
| 28 | class SkPDFDict; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 29 | class SkPDFFont; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 30 | class SkPDFGraphicState; |
| 31 | class SkPDFObject; |
| vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 32 | class SkPDFShader; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 33 | class SkPDFStream; |
| 34 | |
| 35 | class SkPDFDeviceFactory : public SkDeviceFactory { |
| vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 36 | virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width, |
| 37 | int height, bool isOpaque, bool isForLayer); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /** \class SkPDFDevice |
| 41 | |
| 42 | The drawing context for the PDF backend. |
| 43 | */ |
| 44 | class SkPDFDevice : public SkDevice { |
| 45 | public: |
| 46 | /** Create a PDF drawing context with the given width and height. |
| 47 | * 72 points/in means letter paper is 612x792. |
| ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame^] | 48 | * @param pageSize Page size in points. |
| 49 | * @param contentSize The content size of the page in points. This will be |
| 50 | * combined with the initial transform to determine the drawing area |
| 51 | * (as reported by the width and height methods). Anything outside |
| 52 | * of the drawing area will be clipped. |
| vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 53 | * @param initialTransform The initial transform to apply to the page. |
| 54 | * This may be useful to, for example, move the origin in and |
| 55 | * over a bit to account for a margin, scale the canvas, |
| 56 | * or apply a rotation. Note1: the SkPDFDevice also applies |
| 57 | * a scale+translate transform to move the origin from the |
| 58 | * bottom left (PDF default) to the top left. Note2: drawDevice |
| 59 | * (used by layer restore) draws the device after this initial |
| 60 | * transform is applied, so the PDF device factory does an |
| 61 | * inverse scale+translate to accommodate the one that SkPDFDevice |
| 62 | * always does. |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 63 | */ |
| ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame^] | 64 | SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize, |
| 65 | const SkMatrix& initialTransform); |
| 66 | SK_API virtual ~SkPDFDevice(); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 67 | |
| vandebo@chromium.org | 35fc62b | 2010-10-26 19:47:30 +0000 | [diff] [blame] | 68 | virtual uint32_t getDeviceCapabilities() { return kVector_Capability; } |
| 69 | |
| vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 70 | virtual void clear(SkColor color); |
| 71 | |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 72 | /** Called with the correct matrix and clip before this device is drawn |
| 73 | to using those settings. If your subclass overrides this, be sure to |
| 74 | call through to the base class as well. |
| 75 | */ |
| reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 76 | virtual void setMatrixClip(const SkMatrix&, const SkRegion&, |
| 77 | const SkClipStack&); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 78 | |
| reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 79 | virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
| 80 | return false; |
| 81 | } |
| 82 | |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 83 | /** These are called inside the per-device-layer loop for each draw call. |
| 84 | When these are called, we have already applied any saveLayer operations, |
| 85 | and are handling any looping from the paint, and any effects from the |
| 86 | DrawFilter. |
| 87 | */ |
| 88 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 89 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, |
| 90 | size_t count, const SkPoint[], |
| 91 | const SkPaint& paint); |
| 92 | virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint); |
| 93 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 94 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 95 | bool pathIsMutable); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 96 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 97 | const SkIRect* srcRectOrNull, |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 98 | const SkMatrix& matrix, const SkPaint& paint); |
| 99 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y, |
| 100 | const SkPaint& paint); |
| 101 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 102 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 103 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 104 | const SkScalar pos[], SkScalar constY, |
| 105 | int scalarsPerPos, const SkPaint& paint); |
| 106 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 107 | const SkPath& path, const SkMatrix* matrix, |
| 108 | const SkPaint& paint); |
| 109 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, |
| 110 | int vertexCount, const SkPoint verts[], |
| 111 | const SkPoint texs[], const SkColor colors[], |
| 112 | SkXfermode* xmode, const uint16_t indices[], |
| 113 | int indexCount, const SkPaint& paint); |
| 114 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 115 | const SkPaint&); |
| 116 | |
| 117 | // PDF specific methods. |
| 118 | |
| 119 | /** Returns a reference to the resource dictionary for this device. |
| 120 | */ |
| 121 | const SkRefPtr<SkPDFDict>& getResourceDict(); |
| 122 | |
| vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 123 | /** Get the list of resources (PDF objects) used on this page. |
| 124 | * @param resourceList A list to append the resources to. |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 125 | */ |
| vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 126 | void getResources(SkTDArray<SkPDFObject*>* resourceList) const; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 127 | |
| 128 | /** Returns the media box for this device. |
| 129 | */ |
| vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 130 | SkRefPtr<SkPDFArray> getMediaBox() const; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 131 | |
| vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 132 | /** Returns a SkStream with the page contents. The caller is responsible |
| 133 | for a reference to the returned value. |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 134 | */ |
| vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 135 | SkStream* content() const; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 136 | |
| mike@reedtribe.org | ea4ac97 | 2011-04-26 11:48:33 +0000 | [diff] [blame] | 137 | protected: |
| 138 | // override |
| 139 | virtual SkDeviceFactory* onNewDeviceFactory(); |
| 140 | |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 141 | private: |
| ctguil@chromium.org | 1526129 | 2011-04-29 17:54:16 +0000 | [diff] [blame^] | 142 | SkISize fPageSize; |
| vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 143 | SkMatrix fInitialTransform; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 144 | SkRefPtr<SkPDFDict> fResourceDict; |
| 145 | |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 146 | SkTDArray<SkPDFGraphicState*> fGraphicStateResources; |
| 147 | SkTDArray<SkPDFObject*> fXObjectResources; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 148 | SkTDArray<SkPDFFont*> fFontResources; |
| vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 149 | SkTDArray<SkPDFShader*> fShaderResources; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 150 | |
| vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 151 | // In PDF, transforms and clips can only be undone by popping the graphic |
| 152 | // state to before the transform or clip was applied. Because it can be |
| 153 | // a lot of work to reapply a clip and because this class has to apply |
| 154 | // different transforms to accomplish various operations, the clip is |
| 155 | // always applied before a transform and always at a different graphic |
| 156 | // state-stack level than a transform. This strategy results in the |
| 157 | // following possible states for the graphic state stack: |
| 158 | // empty: (identity transform and clip to page) |
| 159 | // one entry: a transform |
| 160 | // one entry: a clip |
| 161 | // two entries: a clip and then a transform |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 162 | // Pointers are owned by the respective Resources list. |
| vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 163 | struct GraphicStackEntry { |
| 164 | SkColor fColor; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 165 | SkScalar fTextSize; |
| vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 166 | SkScalar fTextScaleX; |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 167 | SkPaint::Style fTextFill; |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 168 | SkPDFFont* fFont; |
| vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 169 | SkPDFShader* fShader; |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 170 | SkPDFGraphicState* fGraphicState; |
| vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 171 | SkRegion fClip; |
| 172 | SkMatrix fTransform; |
| 173 | }; |
| 174 | struct GraphicStackEntry fGraphicStack[3]; |
| 175 | int fGraphicStackIndex; |
| 176 | |
| vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 177 | SkDynamicMemoryWStream fContent; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 178 | |
| vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 179 | void init(); |
| 180 | void cleanUp(); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 181 | void updateGSFromPaint(const SkPaint& newPaint, bool forText); |
| vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 182 | void updateFont(const SkPaint& paint, uint16_t glyphID); |
| ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 183 | int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 184 | |
| vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 185 | void pushGS(); |
| 186 | void popGS(); |
| vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 187 | void setTextTransform(SkScalar x, SkScalar y, SkScalar textSkewX); |
| vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 188 | void internalDrawPaint(const SkPaint& paint); |
| 189 | void internalDrawRect(const SkRect& r, const SkPaint& paint); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 190 | void internalDrawBitmap(const SkMatrix& matrix, const SkBitmap& bitmap, |
| vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 191 | const SkIRect* srcRect, const SkPaint& paint); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 192 | |
| vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 193 | SkMatrix setTransform(const SkMatrix& matrix); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | #endif |