SkPDFDevice.h revision b58772f86659cfe0e8d9247fcee878dddd8fdad9
1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
29b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
49b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
79b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org */
89b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
109b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org#ifndef SkPDFDevice_DEFINED
119b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org#define SkPDFDevice_DEFINED
129b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
13b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org#include "SkCanvas.h"
149b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org#include "SkDevice.h"
15a518086928494319b8968abc09808eff492c194fvandebo@chromium.org#include "SkPaint.h"
16a518086928494319b8968abc09808eff492c194fvandebo@chromium.org#include "SkPath.h"
17238be8c7e5de5a83517440a3db7f7965b47fb010vandebo@chromium.org#include "SkRect.h"
189fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org#include "SkRefCnt.h"
199fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org#include "SkStream.h"
20b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com#include "SkTDArray.h"
219fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org#include "SkTScopedPtr.h"
229b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
239b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgclass SkPDFArray;
249b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgclass SkPDFDevice;
259b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgclass SkPDFDict;
2628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.orgclass SkPDFFont;
276112c215fbdd53388e64ece36e6c7bba0fe3a451vandebo@chromium.orgclass SkPDFFormXObject;
289859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.orgclass SkPDFGlyphSetMap;
299b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgclass SkPDFGraphicState;
309b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgclass SkPDFObject;
31da912d61ede86dd3dfa8f645c6f3977f2183812bvandebo@chromium.orgclass SkPDFShader;
329b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgclass SkPDFStream;
339b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
349fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org// Private classes.
359fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.orgstruct ContentEntry;
369fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.orgstruct GraphicStateEntry;
37b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.comstruct NamedDestination;
389fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org
399b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org/** \class SkPDFDevice
409b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
419b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    The drawing context for the PDF backend.
429b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org*/
439b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgclass SkPDFDevice : public SkDevice {
449b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgpublic:
459b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    /** Create a PDF drawing context with the given width and height.
469b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     *  72 points/in means letter paper is 612x792.
47152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org     *  @param pageSize Page size in points.
48152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org     *  @param contentSize The content size of the page in points. This will be
49152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org     *         combined with the initial transform to determine the drawing area
50152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org     *         (as reported by the width and height methods). Anything outside
51152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org     *         of the drawing area will be clipped.
5275f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *  @param initialTransform The initial transform to apply to the page.
5375f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         This may be useful to, for example, move the origin in and
5475f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         over a bit to account for a margin, scale the canvas,
5575f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         or apply a rotation.  Note1: the SkPDFDevice also applies
5675f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         a scale+translate transform to move the origin from the
5775f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         bottom left (PDF default) to the top left.  Note2: drawDevice
5875f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         (used by layer restore) draws the device after this initial
59cde92111d50a96b6d0f3e166fbac7c9bc6eca349reed@google.com     *         transform is applied, so the PDF device does an
6075f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         inverse scale+translate to accommodate the one that SkPDFDevice
6175f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org     *         always does.
629b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     */
63769fa6a013baca6d7404e2bf096a34a7e3635fa5ctguil@chromium.org    // TODO(vandebo): The sizes should be SkSize and not SkISize.
64152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org    SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
65152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org                       const SkMatrix& initialTransform);
66152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org    SK_API virtual ~SkPDFDevice();
679b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
68982cb875f01e247843b9a79082f680cdcf234c2creed@google.com    virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
6935fc62b960db6739b19c59576085663796951e47vandebo@chromium.org
70982cb875f01e247843b9a79082f680cdcf234c2creed@google.com    virtual void clear(SkColor color) SK_OVERRIDE;
7177bcaa324a574584331322d98768582d9232f7fcvandebo@chromium.org
729b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    /** These are called inside the per-device-layer loop for each draw call.
739b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     When these are called, we have already applied any saveLayer operations,
749b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     and are handling any looping from the paint, and any effects from the
759b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     DrawFilter.
769b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     */
77982cb875f01e247843b9a79082f680cdcf234c2creed@google.com    virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
789b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
799b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org                            size_t count, const SkPoint[],
80982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                            const SkPaint& paint) SK_OVERRIDE;
819b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
82ff3903202e5bce39016bac896ba6a417dc707d52vandebo@chromium.org    virtual void drawPath(const SkDraw&, const SkPath& origpath,
8302cc5aa736086320649d8a932515691ae18a0dd5vandebo@chromium.org                          const SkPaint& paint, const SkMatrix* prePathMatrix,
84982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                          bool pathIsMutable) SK_OVERRIDE;
852ae67e70cd06be3f64c6290be7724af8ce6bc853edisonn@google.com    virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
862ae67e70cd06be3f64c6290be7724af8ce6bc853edisonn@google.com                                const SkRect* src, const SkRect& dst,
872ae67e70cd06be3f64c6290be7724af8ce6bc853edisonn@google.com                                const SkPaint& paint) SK_OVERRIDE;
889b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
89f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com                            const SkIRect* srcRectOrNull,
90982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                            const SkMatrix& matrix, const SkPaint&) SK_OVERRIDE;
919b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
92982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                            const SkPaint& paint) SK_OVERRIDE;
939b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawText(const SkDraw&, const void* text, size_t len,
94982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                          SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
959b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawPosText(const SkDraw&, const void* text, size_t len,
969b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org                             const SkScalar pos[], SkScalar constY,
97982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                             int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
989b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
999b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org                                const SkPath& path, const SkMatrix* matrix,
100982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                                const SkPaint& paint) SK_OVERRIDE;
1019b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
1029b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org                              int vertexCount, const SkPoint verts[],
1039b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org                              const SkPoint texs[], const SkColor colors[],
1049b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org                              SkXfermode* xmode, const uint16_t indices[],
105982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                              int indexCount, const SkPaint& paint) SK_OVERRIDE;
1069b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
107982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                            const SkPaint&) SK_OVERRIDE;
1089b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
10940a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    virtual void onAttachToCanvas(SkCanvas* canvas) SK_OVERRIDE;
11040a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    virtual void onDetachFromCanvas() SK_OVERRIDE;
11140a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com
1128dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    enum DrawingArea {
1139510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org        kContent_DrawingArea,  // Drawing area for the page content.
1149510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org        kMargin_DrawingArea,   // Drawing area for the margin content.
1158dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    };
1168dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org
1178dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    /** Sets the drawing area for the device. Subsequent draw calls are directed
118769fa6a013baca6d7404e2bf096a34a7e3635fa5ctguil@chromium.org     *  to the specific drawing area (margin or content). The default drawing
1198dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org     *  area is the content drawing area.
1209510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org     *
1219510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org     *  Currently if margin content is drawn and then a complex (for PDF) xfer
1229510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org     *  mode is used, like SrcIn, Clear, etc, the margin content will get
1239510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org     *  clipped. A simple way to avoid the bug is to always draw the margin
1249510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org     *  content last.
1258dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org     */
12617b78946096265d80215a6c946286ecaa35ea7edepoger@google.com    SK_API void setDrawingArea(DrawingArea drawingArea);
1278dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org
1289b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    // PDF specific methods.
1299b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
1301feb33068b1313d2647c50b90ae8e0a3d510db2ereed@google.com    /** Returns the resource dictionary for this device.
1319b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     */
1321feb33068b1313d2647c50b90ae8e0a3d510db2ereed@google.com    SK_API SkPDFDict* getResourceDict();
1339b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
134a518086928494319b8968abc09808eff492c194fvandebo@chromium.org    /** Get the list of resources (PDF objects) used on this page.
1355bd26d32ab85d09dccabbdc6dd944ef36ac32423edisonn@google.com     *  @param resourceList A list to append the resources to.
136188838c20818307fda770ffc395a76ea63c1c8ccvandebo@chromium.org     *  @param recursive    If recursive is true, get the resources of the
137188838c20818307fda770ffc395a76ea63c1c8ccvandebo@chromium.org     *                      device's resources recursively. (Useful for adding
138188838c20818307fda770ffc395a76ea63c1c8ccvandebo@chromium.org     *                      objects to the catalog.)
1399b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     */
1405bd26d32ab85d09dccabbdc6dd944ef36ac32423edisonn@google.com    SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList,
141188838c20818307fda770ffc395a76ea63c1c8ccvandebo@chromium.org                             bool recursive) const;
1429b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
143f0ec2666d9a3f0f1662f0d63b5147628c49648aavandebo@chromium.org    /** Get the fonts used on this device.
144f0ec2666d9a3f0f1662f0d63b5147628c49648aavandebo@chromium.org     */
1453509f050810ab95cc36265f3b486b9569799041dvandebo@chromium.org    SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
146f0ec2666d9a3f0f1662f0d63b5147628c49648aavandebo@chromium.org
147b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    /** Add our named destinations to the supplied dictionary.
148b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com     *  @param dict  Dictionary to add destinations to.
149b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com     *  @param page  The PDF object representing the page for this device.
150b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com     */
151b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    void appendDestinations(SkPDFDict* dict, SkPDFObject* page);
152b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com
1532a006c112743e07ce258ca223631fc19233f5ddcreed@google.com    /** Returns a copy of the media box for this device. The caller is required
1542a006c112743e07ce258ca223631fc19233f5ddcreed@google.com     *  to unref() this when it is finished.
1559b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     */
1562a006c112743e07ce258ca223631fc19233f5ddcreed@google.com    SK_API SkPDFArray* copyMediaBox() const;
1579b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
1582a006c112743e07ce258ca223631fc19233f5ddcreed@google.com    /** Get the annotations from this page, or NULL if there are none.
159238be8c7e5de5a83517440a3db7f7965b47fb010vandebo@chromium.org     */
1602a006c112743e07ce258ca223631fc19233f5ddcreed@google.com    SK_API SkPDFArray* getAnnotations() const { return fAnnotations; }
161238be8c7e5de5a83517440a3db7f7965b47fb010vandebo@chromium.org
162c2a9b7fe5640af8f0c371561f1ac71b045d6d8ecvandebo@chromium.org    /** Returns a SkStream with the page contents.  The caller is responsible
163c2a9b7fe5640af8f0c371561f1ac71b045d6d8ecvandebo@chromium.org        for a reference to the returned value.
1645667afc5cb4a8cd15a27667f222b6d9c94d61c38reed@google.com        DEPRECATED: use copyContentToData()
1659b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org     */
1663509f050810ab95cc36265f3b486b9569799041dvandebo@chromium.org    SK_API SkStream* content() const;
1679b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
1685667afc5cb4a8cd15a27667f222b6d9c94d61c38reed@google.com    /** Returns a SkStream with the page contents.  The caller is responsible
1695667afc5cb4a8cd15a27667f222b6d9c94d61c38reed@google.com     *  for calling data->unref() when it is finished.
1705667afc5cb4a8cd15a27667f222b6d9c94d61c38reed@google.com     */
1715667afc5cb4a8cd15a27667f222b6d9c94d61c38reed@google.com    SK_API SkData* copyContentToData() const;
1729859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org
1733509f050810ab95cc36265f3b486b9569799041dvandebo@chromium.org    SK_API const SkMatrix& initialTransform() const {
1743509f050810ab95cc36265f3b486b9569799041dvandebo@chromium.org        return fInitialTransform;
1753509f050810ab95cc36265f3b486b9569799041dvandebo@chromium.org    }
17661d267879bdf85c84b05f0519eb53b9322abbf0evandebo@chromium.org
1779859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org    /** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
1789859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org     *  that shows on this device.
1799859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org     */
1809859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org    const SkPDFGlyphSetMap& getFontGlyphUsage() const {
1819859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org        return *(fFontGlyphUsage.get());
1829859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org    }
18374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
184c69809745e6496564639e42ef998ad39adf7dfb8bsalomon@google.comprotected:
185982cb875f01e247843b9a79082f680cdcf234c2creed@google.com    virtual bool onReadPixels(const SkBitmap& bitmap, int x, int y,
186982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                              SkCanvas::Config8888) SK_OVERRIDE;
1879859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org
188b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com    virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE;
189b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com
1909b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.orgprivate:
191769fa6a013baca6d7404e2bf096a34a7e3635fa5ctguil@chromium.org    // TODO(vandebo): push most of SkPDFDevice's state into a core object in
192b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    // order to get the right access levels without using friend.
19313d14a9dbd2cf0a9654045cc967e92626690631avandebo@chromium.org    friend class ScopedContentEntry;
194a0c7edbb0804144ab320951db5c741eea247fc0fvandebo@chromium.org
195152612938020fa46999f33668027d5bc0f7afd18ctguil@chromium.org    SkISize fPageSize;
1969fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org    SkISize fContentSize;
19775f97e452e8f2ee55cd2b283df7d7734f48bc2bfvandebo@chromium.org    SkMatrix fInitialTransform;
1989fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org    SkClipStack fExistingClipStack;
1999fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org    SkRegion fExistingClipRegion;
2002a006c112743e07ce258ca223631fc19233f5ddcreed@google.com    SkPDFArray* fAnnotations;
201fc641d09e5a1a9f7fae369ae06a8b96089faf57breed@google.com    SkPDFDict* fResourceDict;
202b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    SkTDArray<NamedDestination*> fNamedDestinations;
2039b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
2049b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
2059b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org    SkTDArray<SkPDFObject*> fXObjectResources;
20628be72b63e457c680c192a34fb9f58e1c693363fvandebo@chromium.org    SkTDArray<SkPDFFont*> fFontResources;
207421d6443fbd3a913dfa32b6492c4a2969bc6314bvandebo@chromium.org    SkTDArray<SkPDFObject*> fShaderResources;
2089b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
2099fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org    SkTScopedPtr<ContentEntry> fContentEntries;
210b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    ContentEntry* fLastContentEntry;
2118dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    SkTScopedPtr<ContentEntry> fMarginContentEntries;
2128dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    ContentEntry* fLastMarginContentEntry;
2138dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    DrawingArea fDrawingArea;
2148dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org
21540a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    const SkClipStack* fClipStack;
21640a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com
2178dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    // Accessor and setter functions based on the current DrawingArea.
2189510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org    SkTScopedPtr<ContentEntry>* getContentEntries();
2198dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    ContentEntry* getLastContentEntry();
2208dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    void setLastContentEntry(ContentEntry* contentEntry);
2219b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
2229859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org    // Glyph ids used for each font on this device.
2239859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org    SkTScopedPtr<SkPDFGlyphSetMap> fFontGlyphUsage;
2249859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org
225a0c7edbb0804144ab320951db5c741eea247fc0fvandebo@chromium.org    SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
226a0c7edbb0804144ab320951db5c741eea247fc0fvandebo@chromium.org                const SkRegion& existingClipRegion);
227a0c7edbb0804144ab320951db5c741eea247fc0fvandebo@chromium.org
228e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com    // override from SkDevice
2299859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org    virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
2309859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org                                               int width, int height,
231e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com                                               bool isOpaque,
232982cb875f01e247843b9a79082f680cdcf234c2creed@google.com                                               Usage usage) SK_OVERRIDE;
233e97f0856a8044866b12527819d14cdfbcdfd96f2bsalomon@google.com
23477bcaa324a574584331322d98768582d9232f7fcvandebo@chromium.org    void init();
2359859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.org    void cleanUp(bool clearFontUsage);
236fc641d09e5a1a9f7fae369ae06a8b96089faf57breed@google.com    SkPDFFormXObject* createFormXObjectFromDevice();
2379fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org
238466f3d66f09285d2d988315bacde573a5359ce54vandebo@chromium.org    // Clear the passed clip from all existing content entries.
239466f3d66f09285d2d988315bacde573a5359ce54vandebo@chromium.org    void clearClipFromContent(const SkClipStack* clipStack,
240466f3d66f09285d2d988315bacde573a5359ce54vandebo@chromium.org                              const SkRegion& clipRegion);
241481aef68333e01c19badda456d8e60bd1f1bee2avandebo@chromium.org    void drawFormXObjectWithClip(SkPDFFormXObject* form,
242481aef68333e01c19badda456d8e60bd1f1bee2avandebo@chromium.org                                 const SkClipStack* clipStack,
243481aef68333e01c19badda456d8e60bd1f1bee2avandebo@chromium.org                                 const SkRegion& clipRegion,
244481aef68333e01c19badda456d8e60bd1f1bee2avandebo@chromium.org                                 bool invertClip);
245466f3d66f09285d2d988315bacde573a5359ce54vandebo@chromium.org
246b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    // If the paint or clip is such that we shouldn't draw anything, this
247b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    // returns NULL and does not create a content entry.
248b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    // setUpContentEntry and finishContentEntry can be used directly, but
24913d14a9dbd2cf0a9654045cc967e92626690631avandebo@chromium.org    // the preferred method is to use the ScopedContentEntry helper class.
250b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
251b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org                                    const SkRegion& clipRegion,
252b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org                                    const SkMatrix& matrix,
253b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org                                    const SkPaint& paint,
254b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org                                    bool hasText,
255fc641d09e5a1a9f7fae369ae06a8b96089faf57breed@google.com                                    SkPDFFormXObject** dst);
256b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    void finishContentEntry(SkXfermode::Mode xfermode,
257b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org                            SkPDFFormXObject* dst);
258481aef68333e01c19badda456d8e60bd1f1bee2avandebo@chromium.org    bool isContentEmpty();
259481aef68333e01c19badda456d8e60bd1f1bee2avandebo@chromium.org
2609fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org    void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
2619fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                                            const SkClipStack& clipStack,
2629fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                                            const SkRegion& clipRegion,
2639fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                                            const SkPaint& paint,
2649fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                                            bool hasText,
2659fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                                            GraphicStateEntry* entry);
2666112c215fbdd53388e64ece36e6c7bba0fe3a451vandebo@chromium.org    int addGraphicStateResource(SkPDFGraphicState* gs);
2679fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org
268b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    void updateFont(const SkPaint& paint, uint16_t glyphID,
269b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org                    ContentEntry* contentEntry);
2709db86bb9cd1b77be0afc504ccc07026e4282d7e7ctguil@chromium.org    int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
2719fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org
272b069c8cfcd5df285193eb334b3bc33438782e8davandebo@chromium.org    void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
2739fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org    void internalDrawBitmap(const SkMatrix& matrix,
27478dad54080ad806be89adb5cc0e3c530b031cdafvandebo@chromium.org                            const SkClipStack* clipStack,
2759fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                            const SkRegion& clipRegion,
2769fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                            const SkBitmap& bitmap,
2779fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                            const SkIRect* srcRect,
2789fbdf875183f5142b8e0ba46ab430cc46ad701bfvandebo@chromium.org                            const SkPaint& paint);
2799b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
2808dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    /** Helper method for copyContentToData. It is responsible for copying the
2818dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org     *  list of content entries |entry| to |data|.
2828dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org     */
2838dcf74f27690476193f5d4ca34fba2e87ca7c98dctguil@chromium.org    void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
2849510ccc06bbfa5e888f66578042674be98d8ac60ctguil@chromium.org
285b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    bool handleRectAnnotation(const SkRect& r, const SkMatrix& matrix,
286b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com                              const SkPaint& paint);
287b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    bool handlePointAnnotation(const SkPoint* points, size_t count,
288b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com                               const SkMatrix& matrix, const SkPaint& paint);
289b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    SkPDFDict* createLinkAnnotation(const SkRect& r, const SkMatrix& matrix);
290b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    void handleLinkToURL(SkData* urlData, const SkRect& r,
291b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com                         const SkMatrix& matrix);
292b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    void handleLinkToNamedDest(SkData* nameData, const SkRect& r,
293b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com                               const SkMatrix& matrix);
294b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com    void defineNamedDestination(SkData* nameData, const SkPoint& point,
295b58772f86659cfe0e8d9247fcee878dddd8fdad9epoger@google.com                                const SkMatrix& matrix);
296238be8c7e5de5a83517440a3db7f7965b47fb010vandebo@chromium.org
29740a1ae4df28810aa5aa5cf2627d8387b2dfb867arobertphillips@google.com    typedef SkDevice INHERITED;
2989b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org};
2999b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org
3009b49dc0db8254e3dcdc2de4a1e0add4f8a7ac5a8vandebo@chromium.org#endif
301