| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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" |
| 23 | |
| 24 | class SkPDFArray; |
| 25 | class SkPDFDevice; |
| 26 | class SkPDFDict; |
| 27 | class SkPDFGraphicState; |
| 28 | class SkPDFObject; |
| 29 | class SkPDFStream; |
| 30 | |
| 31 | class SkPDFDeviceFactory : public SkDeviceFactory { |
| 32 | virtual SkDevice* newDevice(SkBitmap::Config config, int width, int height, |
| 33 | bool isOpaque, bool isForLayer); |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 34 | }; |
| 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 width Page width in points. |
| 45 | * @param height Page height in points. |
| 46 | */ |
| 47 | SkPDFDevice(int width, int height); |
| 48 | virtual ~SkPDFDevice(); |
| 49 | |
| 50 | virtual SkDeviceFactory* getDeviceFactory() { |
| 51 | return SkNEW(SkPDFDeviceFactory); |
| 52 | } |
| 53 | |
| vandebo@chromium.org | 35fc62b | 2010-10-26 19:47:30 +0000 | [diff] [blame^] | 54 | virtual uint32_t getDeviceCapabilities() { return kVector_Capability; } |
| 55 | |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 56 | virtual int width() const { return fWidth; }; |
| 57 | |
| 58 | virtual int height() const { return fHeight; }; |
| 59 | |
| 60 | /** Called with the correct matrix and clip before this device is drawn |
| 61 | to using those settings. If your subclass overrides this, be sure to |
| 62 | call through to the base class as well. |
| 63 | */ |
| 64 | virtual void setMatrixClip(const SkMatrix&, const SkRegion&); |
| 65 | |
| 66 | /** These are called inside the per-device-layer loop for each draw call. |
| 67 | When these are called, we have already applied any saveLayer operations, |
| 68 | and are handling any looping from the paint, and any effects from the |
| 69 | DrawFilter. |
| 70 | */ |
| 71 | virtual void drawPaint(const SkDraw&, const SkPaint& paint); |
| 72 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, |
| 73 | size_t count, const SkPoint[], |
| 74 | const SkPaint& paint); |
| 75 | virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint); |
| 76 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 77 | const SkPaint& paint); |
| 78 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 79 | const SkMatrix& matrix, const SkPaint& paint); |
| 80 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y, |
| 81 | const SkPaint& paint); |
| 82 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 83 | SkScalar x, SkScalar y, const SkPaint& paint); |
| 84 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 85 | const SkScalar pos[], SkScalar constY, |
| 86 | int scalarsPerPos, const SkPaint& paint); |
| 87 | virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 88 | const SkPath& path, const SkMatrix* matrix, |
| 89 | const SkPaint& paint); |
| 90 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, |
| 91 | int vertexCount, const SkPoint verts[], |
| 92 | const SkPoint texs[], const SkColor colors[], |
| 93 | SkXfermode* xmode, const uint16_t indices[], |
| 94 | int indexCount, const SkPaint& paint); |
| 95 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 96 | const SkPaint&); |
| 97 | |
| 98 | // PDF specific methods. |
| 99 | |
| 100 | /** Returns a reference to the resource dictionary for this device. |
| 101 | */ |
| 102 | const SkRefPtr<SkPDFDict>& getResourceDict(); |
| 103 | |
| 104 | /** Get the list of resouces (PDF objects) used on this page |
| 105 | * @param resouceList A list to append the resouces to. |
| 106 | */ |
| 107 | void getResouces(SkTDArray<SkPDFObject*>* resouceList); |
| 108 | |
| 109 | /** Returns the media box for this device. |
| 110 | */ |
| 111 | SkRefPtr<SkPDFArray> getMediaBox(); |
| 112 | |
| 113 | /** Returns a string with the page contents. |
| vandebo@chromium.org | ddbbd80 | 2010-10-26 19:45:06 +0000 | [diff] [blame] | 114 | * @param flipOrigin Flip the origin between top and bottom. |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 115 | */ |
| vandebo@chromium.org | ddbbd80 | 2010-10-26 19:45:06 +0000 | [diff] [blame] | 116 | SkString content(bool flipOrigin) const; |
| vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | int fWidth; |
| 120 | int fHeight; |
| 121 | SkRefPtr<SkPDFDict> fResourceDict; |
| 122 | |
| 123 | SkRefPtr<SkPDFGraphicState> fCurrentGraphicState; |
| 124 | SkColor fCurrentColor; |
| 125 | SkScalar fCurrentTextScaleX; |
| 126 | SkTDArray<SkPDFGraphicState*> fGraphicStateResources; |
| 127 | SkTDArray<SkPDFObject*> fXObjectResources; |
| 128 | |
| 129 | SkString fContent; |
| 130 | |
| 131 | // The last requested transforms from SkCanvas (setMatrixClip) |
| 132 | SkMatrix fCurTransform; |
| 133 | |
| 134 | // The transform currently in effect in the PDF content stream. |
| 135 | SkMatrix fActiveTransform; |
| 136 | |
| 137 | void updateGSFromPaint(const SkPaint& newPaint, SkString* textStaetUpdate); |
| 138 | |
| 139 | void moveTo(SkScalar x, SkScalar y); |
| 140 | void appendLine(SkScalar x, SkScalar y); |
| 141 | void appendCubic(SkScalar ctl1X, SkScalar ctl1Y, |
| 142 | SkScalar ctl2X, SkScalar ctl2Y, |
| 143 | SkScalar dstX, SkScalar dstY); |
| 144 | void appendRectangle(SkScalar x, SkScalar y, SkScalar w, SkScalar h); |
| 145 | void closePath(); |
| 146 | void strokePath(); |
| 147 | void internalDrawBitmap(const SkMatrix& matrix, const SkBitmap& bitmap, |
| 148 | const SkPaint& paint); |
| 149 | |
| 150 | void setTransform(const SkMatrix& matrix); |
| 151 | void setNoTransform(); |
| 152 | void applyTempTransform(const SkMatrix& matrix); |
| 153 | void removeTempTransform(); |
| 154 | void applyTransform(const SkMatrix& matrix); |
| 155 | }; |
| 156 | |
| 157 | #endif |