aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/pdf
diff options
context:
space:
mode:
authorGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2010-10-20 22:23:29 +0000
committerGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2010-10-20 22:23:29 +0000
commit9b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8 (patch)
tree35491ac7fd0c41cb8b5fb0a06a24840615e75b3a /include/pdf
parent17f694b0386ae42e66e76ffa19a0dabbc2191c62 (diff)
First pieces of SkPDFDevice. Supports:
Matrix transforms. Rendering bitmaps. Basic paint parameters. Rendering rectangles, points, lines, polygons. Render a paint to the page. Review URL: http://codereview.appspot.com/2584041 git-svn-id: http://skia.googlecode.com/svn/trunk@614 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/pdf')
-rw-r--r--include/pdf/SkPDFDevice.h155
-rw-r--r--include/pdf/SkPDFGraphicState.h80
-rw-r--r--include/pdf/SkPDFImage.h67
-rw-r--r--include/pdf/SkPDFTypes.h21
4 files changed, 323 insertions, 0 deletions
diff --git a/include/pdf/SkPDFDevice.h b/include/pdf/SkPDFDevice.h
new file mode 100644
index 0000000000..5421299226
--- /dev/null
+++ b/include/pdf/SkPDFDevice.h
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SkPDFDevice_DEFINED
+#define SkPDFDevice_DEFINED
+
+#include "SkRefCnt.h"
+#include "SkDevice.h"
+#include "SkString.h"
+
+class SkPDFArray;
+class SkPDFDevice;
+class SkPDFDict;
+class SkPDFGraphicState;
+class SkPDFObject;
+class SkPDFStream;
+
+class SkPDFDeviceFactory : public SkDeviceFactory {
+ virtual SkDevice* newDevice(SkBitmap::Config config, int width, int height,
+ bool isOpaque, bool isForLayer);
+ virtual uint32_t getDeviceCapabilities() { return 0; }
+};
+
+/** \class SkPDFDevice
+
+ The drawing context for the PDF backend.
+*/
+class SkPDFDevice : public SkDevice {
+public:
+ /** Create a PDF drawing context with the given width and height.
+ * 72 points/in means letter paper is 612x792.
+ * @param width Page width in points.
+ * @param height Page height in points.
+ */
+ SkPDFDevice(int width, int height);
+ virtual ~SkPDFDevice();
+
+ virtual SkDeviceFactory* getDeviceFactory() {
+ return SkNEW(SkPDFDeviceFactory);
+ }
+
+ virtual int width() const { return fWidth; };
+
+ virtual int height() const { return fHeight; };
+
+ /** Called with the correct matrix and clip before this device is drawn
+ to using those settings. If your subclass overrides this, be sure to
+ call through to the base class as well.
+ */
+ virtual void setMatrixClip(const SkMatrix&, const SkRegion&);
+
+ /** These are called inside the per-device-layer loop for each draw call.
+ When these are called, we have already applied any saveLayer operations,
+ and are handling any looping from the paint, and any effects from the
+ DrawFilter.
+ */
+ virtual void drawPaint(const SkDraw&, const SkPaint& paint);
+ virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
+ size_t count, const SkPoint[],
+ const SkPaint& paint);
+ virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
+ virtual void drawPath(const SkDraw&, const SkPath& path,
+ const SkPaint& paint);
+ virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
+ const SkMatrix& matrix, const SkPaint& paint);
+ virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
+ const SkPaint& paint);
+ virtual void drawText(const SkDraw&, const void* text, size_t len,
+ SkScalar x, SkScalar y, const SkPaint& paint);
+ virtual void drawPosText(const SkDraw&, const void* text, size_t len,
+ const SkScalar pos[], SkScalar constY,
+ int scalarsPerPos, const SkPaint& paint);
+ virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
+ const SkPath& path, const SkMatrix* matrix,
+ const SkPaint& paint);
+ virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
+ int vertexCount, const SkPoint verts[],
+ const SkPoint texs[], const SkColor colors[],
+ SkXfermode* xmode, const uint16_t indices[],
+ int indexCount, const SkPaint& paint);
+ virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
+ const SkPaint&);
+
+ // PDF specific methods.
+
+ /** Returns a reference to the resource dictionary for this device.
+ */
+ const SkRefPtr<SkPDFDict>& getResourceDict();
+
+ /** Get the list of resouces (PDF objects) used on this page
+ * @param resouceList A list to append the resouces to.
+ */
+ void getResouces(SkTDArray<SkPDFObject*>* resouceList);
+
+ /** Returns the media box for this device.
+ */
+ SkRefPtr<SkPDFArray> getMediaBox();
+
+ /** Returns a string with the page contents.
+ */
+ SkString content();
+
+private:
+ int fWidth;
+ int fHeight;
+ SkRefPtr<SkPDFDict> fResourceDict;
+
+ SkRefPtr<SkPDFGraphicState> fCurrentGraphicState;
+ SkColor fCurrentColor;
+ SkScalar fCurrentTextScaleX;
+ SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
+ SkTDArray<SkPDFObject*> fXObjectResources;
+
+ SkString fContent;
+
+ // The last requested transforms from SkCanvas (setMatrixClip)
+ SkMatrix fCurTransform;
+
+ // The transform currently in effect in the PDF content stream.
+ SkMatrix fActiveTransform;
+
+ void updateGSFromPaint(const SkPaint& newPaint, SkString* textStaetUpdate);
+
+ void moveTo(SkScalar x, SkScalar y);
+ void appendLine(SkScalar x, SkScalar y);
+ void appendCubic(SkScalar ctl1X, SkScalar ctl1Y,
+ SkScalar ctl2X, SkScalar ctl2Y,
+ SkScalar dstX, SkScalar dstY);
+ void appendRectangle(SkScalar x, SkScalar y, SkScalar w, SkScalar h);
+ void closePath();
+ void strokePath();
+ void internalDrawBitmap(const SkMatrix& matrix, const SkBitmap& bitmap,
+ const SkPaint& paint);
+
+ void setTransform(const SkMatrix& matrix);
+ void setNoTransform();
+ void applyTempTransform(const SkMatrix& matrix);
+ void removeTempTransform();
+ void applyTransform(const SkMatrix& matrix);
+};
+
+#endif
diff --git a/include/pdf/SkPDFGraphicState.h b/include/pdf/SkPDFGraphicState.h
new file mode 100644
index 0000000000..7fb09d46e0
--- /dev/null
+++ b/include/pdf/SkPDFGraphicState.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SkPDFGraphicState_DEFINED
+#define SkPDFGraphicState_DEFINED
+
+#include "SkPaint.h"
+#include "SkPDFTypes.h"
+#include "SkTemplates.h"
+#include "SkThread.h"
+
+/** \class SkPDFGraphicState
+ SkPaint objects roughly correspond to graphic state dictionaries that can
+ be installed. So that a given dictionary is only output to the pdf file
+ once, we want to canonicalize them. Static methods in this class manage
+ a weakly referenced set of SkPDFGraphicState objects: when the last
+ reference to a SkPDFGraphicState is removed, it removes itself from the
+ static set of objects.
+
+*/
+class SkPDFGraphicState : public SkPDFDict {
+public:
+ virtual ~SkPDFGraphicState();
+
+ // Override emitObject and getOutputSize so that we can populate
+ // the dictionary on demand.
+ virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
+ bool indirect);
+ virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
+
+ /** Get the graphic state for the passed SkPaint. The reference count of
+ * the object is incremented and it is the caller's responsibility to
+ * unreference it when done. This is needed to accommodate the weak
+ * reference pattern used when the returned object is new and has no
+ * other references.
+ * @param paint The SkPaint to emulate.
+ */
+ static SkPDFGraphicState* getGraphicStateForPaint(const SkPaint& paint);
+
+private:
+ const SkPaint fPaint;
+ bool fPopulated;
+
+ class GSCanonicalEntry {
+ public:
+ SkPDFGraphicState* fGraphicState;
+ const SkPaint* fPaint;
+
+ bool operator==(const GSCanonicalEntry& b) const;
+ explicit GSCanonicalEntry(SkPDFGraphicState* gs)
+ : fGraphicState(gs),
+ fPaint(&gs->fPaint) {}
+ explicit GSCanonicalEntry(const SkPaint* paint) : fPaint(paint) {}
+ };
+
+ // This should be made a hash table if performance is a problem.
+ static SkTDArray<GSCanonicalEntry>& canonicalPaints();
+ static SkMutex& canonicalPaintsMutex();
+
+ explicit SkPDFGraphicState(const SkPaint& paint);
+
+ void populateDict();
+
+ static int find(const SkPaint& paint);
+};
+
+#endif
diff --git a/include/pdf/SkPDFImage.h b/include/pdf/SkPDFImage.h
new file mode 100644
index 0000000000..6e5ee37926
--- /dev/null
+++ b/include/pdf/SkPDFImage.h
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SkPDFImage_DEFINED
+#define SkPDFImage_DEFINED
+
+#include "SkPDFStream.h"
+#include "SkPDFTypes.h"
+#include "SkRefCnt.h"
+
+class SkBitmap;
+class SkPaint;
+class SkPDFCatalog;
+
+/** \class SkPDFImage
+
+ An image XObject.
+*/
+
+// We could play the same trick here as is done in SkPDFGraphicState, storing
+// a copy of the Bitmap object (not the pixels), the pixel generation number,
+// and settings used from the paint to canonicalize image objects.
+class SkPDFImage : public SkPDFObject {
+public:
+ /** Create a PDF image XObject. Entries for the image properties are
+ * automatically added to the stream dictionary.
+ * @param bitmap The image to use.
+ * @param paint Used to calculate alpha, masks, etc.
+ */
+ SkPDFImage(const SkBitmap& bitmap, const SkPaint& paint);
+ virtual ~SkPDFImage();
+
+ // The SkPDFObject interface.
+ virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
+ bool indirect);
+ virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
+
+ /** Add the value to the stream dictionary with the given key.
+ * @param key The key for this dictionary entry.
+ * @param value The value for this dictionary entry.
+ */
+ void insert(SkPDFName* key, SkPDFObject* value);
+
+ /** Add the value to the stream dictionary with the given key.
+ * @param key The text of the key for this dictionary entry.
+ * @param value The value for this dictionary entry.
+ */
+ void insert(const char key[], SkPDFObject* value);
+
+private:
+ SkRefPtr<SkPDFStream> fStream;
+};
+
+#endif
diff --git a/include/pdf/SkPDFTypes.h b/include/pdf/SkPDFTypes.h
index e0b1041b92..2066352167 100644
--- a/include/pdf/SkPDFTypes.h
+++ b/include/pdf/SkPDFTypes.h
@@ -108,6 +108,27 @@ private:
int32_t fValue;
};
+/** \class SkPDFBool
+
+ An boolean value in a PDF.
+*/
+class SkPDFBool : public SkPDFObject {
+public:
+ /** Create a PDF boolean.
+ * @param value true or false.
+ */
+ explicit SkPDFBool(bool value);
+ virtual ~SkPDFBool();
+
+ // The SkPDFObject interface.
+ virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog,
+ bool indirect);
+ virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
+
+private:
+ bool fValue;
+};
+
/** \class SkPDFScalar
A real number object in a PDF.