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" |
| 22 | #include "SkString.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; |
| 32 | class SkPDFStream; |
| 33 | |
| 34 | class SkPDFDeviceFactory : public SkDeviceFactory { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 35 | virtual SkDevice* newDevice(SkCanvas*, SkBitmap::Config, int width, int height, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 36 | bool isOpaque, bool isForLayer); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | /** \class SkPDFDevice |
| 40 | |
| 41 | The drawing context for the PDF backend. |
| 42 | */ |
| 43 | class SkPDFDevice : public SkDevice { |
| 44 | public: |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 45 | /** Skia generally uses the top left as the origin and PDFs natively use |
| 46 | the bottom left. We can move the origin to the top left in the PDF |
| 47 | with a transform, but we have to be careful to apply the transform |
| 48 | only once. |
| 49 | */ |
| 50 | enum OriginTransform { |
| 51 | kFlip_OriginTransform, |
| 52 | kNoFlip_OriginTransform, |
| 53 | }; |
| 54 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 55 | /** Create a PDF drawing context with the given width and height. |
| 56 | * 72 points/in means letter paper is 612x792. |
| 57 | * @param width Page width in points. |
| 58 | * @param height Page height in points. |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 59 | * @param flipOrigin Flip the origin from lower left to upper left. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 60 | */ |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 61 | SkPDFDevice(int width, int height, |
| 62 | OriginTransform flipOrigin = kFlip_OriginTransform); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 63 | virtual ~SkPDFDevice(); |
| 64 | |
| 65 | virtual SkDeviceFactory* getDeviceFactory() { |
| 66 | return SkNEW(SkPDFDeviceFactory); |
| 67 | } |
| 68 | |
vandebo@chromium.org | 35fc62b | 2010-10-26 19:47:30 +0000 | [diff] [blame] | 69 | virtual uint32_t getDeviceCapabilities() { return kVector_Capability; } |
| 70 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 71 | virtual int width() const { return fWidth; }; |
| 72 | |
| 73 | virtual int height() const { return fHeight; }; |
| 74 | |
| 75 | /** Called with the correct matrix and clip before this device is drawn |
| 76 | to using those settings. If your subclass overrides this, be sure to |
| 77 | call through to the base class as well. |
| 78 | */ |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 79 | virtual void setMatrixClip(const SkMatrix&, const SkRegion&, |
| 80 | const SkClipStack&); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 81 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 82 | virtual bool readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
| 83 | return false; |
| 84 | } |
| 85 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 86 | /** These are called inside the per-device-layer loop for each draw call. |
| 87 | When these are called, we have already applied any saveLayer operations, |
| 88 | and are handling any looping from the paint, and any effects from the |
| 89 | DrawFilter. |
| 90 | */ |
| 91 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 92 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, |
| 93 | size_t count, const SkPoint[], |
| 94 | const SkPaint& paint); |
| 95 | virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint); |
| 96 | virtual void drawPath(const SkDraw&, const SkPath& path, |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 97 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 98 | bool pathIsMutable); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 99 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 100 | const SkIRect* srcRectOrNull, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 101 | const SkMatrix& matrix, const SkPaint& paint); |
| 102 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y, |
| 103 | const SkPaint& paint); |
| 104 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 105 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 106 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 107 | const SkScalar pos[], SkScalar constY, |
| 108 | int scalarsPerPos, const SkPaint& paint); |
| 109 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 110 | const SkPath& path, const SkMatrix* matrix, |
| 111 | const SkPaint& paint); |
| 112 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, |
| 113 | int vertexCount, const SkPoint verts[], |
| 114 | const SkPoint texs[], const SkColor colors[], |
| 115 | SkXfermode* xmode, const uint16_t indices[], |
| 116 | int indexCount, const SkPaint& paint); |
| 117 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 118 | const SkPaint&); |
| 119 | |
| 120 | // PDF specific methods. |
| 121 | |
| 122 | /** Returns a reference to the resource dictionary for this device. |
| 123 | */ |
| 124 | const SkRefPtr<SkPDFDict>& getResourceDict(); |
| 125 | |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 126 | /** Get the list of resources (PDF objects) used on this page. |
| 127 | * @param resourceList A list to append the resources to. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 128 | */ |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 129 | void getResources(SkTDArray<SkPDFObject*>* resourceList) const; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 130 | |
| 131 | /** Returns the media box for this device. |
| 132 | */ |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 133 | SkRefPtr<SkPDFArray> getMediaBox() const; |
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 | /** Returns a SkStream with the page contents. The caller is responsible |
| 136 | for a reference to the returned value. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 137 | */ |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame^] | 138 | SkStream* content() const; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | int fWidth; |
| 142 | int fHeight; |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 143 | OriginTransform fFlipOrigin; |
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 | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 149 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 150 | // In PDF, transforms and clips can only be undone by popping the graphic |
| 151 | // state to before the transform or clip was applied. Because it can be |
| 152 | // a lot of work to reapply a clip and because this class has to apply |
| 153 | // different transforms to accomplish various operations, the clip is |
| 154 | // always applied before a transform and always at a different graphic |
| 155 | // state-stack level than a transform. This strategy results in the |
| 156 | // following possible states for the graphic state stack: |
| 157 | // empty: (identity transform and clip to page) |
| 158 | // one entry: a transform |
| 159 | // one entry: a clip |
| 160 | // two entries: a clip and then a transform |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 161 | // Pointers are owned by the respective Resources list. |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 162 | struct GraphicStackEntry { |
| 163 | SkColor fColor; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 164 | SkScalar fTextSize; |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 165 | SkScalar fTextScaleX; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 166 | SkPaint::Style fTextFill; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 167 | SkPDFFont* fFont; |
| 168 | SkPDFGraphicState* fGraphicState; |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 169 | SkRegion fClip; |
| 170 | SkMatrix fTransform; |
| 171 | }; |
| 172 | struct GraphicStackEntry fGraphicStack[3]; |
| 173 | int fGraphicStackIndex; |
| 174 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 175 | SkString fContent; |
| 176 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 177 | void updateGSFromPaint(const SkPaint& newPaint, bool forText); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 178 | void updateFont(const SkPaint& paint, uint16_t glyphID); |
| 179 | int getFontResourceIndex(uint32_t fontID, uint16_t glyphID); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 180 | |
| 181 | void moveTo(SkScalar x, SkScalar y); |
| 182 | void appendLine(SkScalar x, SkScalar y); |
| 183 | void appendCubic(SkScalar ctl1X, SkScalar ctl1Y, |
| 184 | SkScalar ctl2X, SkScalar ctl2Y, |
| 185 | SkScalar dstX, SkScalar dstY); |
| 186 | void appendRectangle(SkScalar x, SkScalar y, SkScalar w, SkScalar h); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 187 | void emitPath(const SkPath& path); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 188 | void closePath(); |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 189 | void paintPath(SkPaint::Style style, SkPath::FillType fill); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 190 | void strokePath(); |
| 191 | void pushGS(); |
| 192 | void popGS(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 193 | void setTextTransform(SkScalar x, SkScalar y, SkScalar textSkewX); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 194 | void internalDrawBitmap(const SkMatrix& matrix, const SkBitmap& bitmap, |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 195 | const SkIRect* srcRect, const SkPaint& paint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 196 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 197 | SkMatrix setTransform(const SkMatrix& matrix); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | #endif |