2011-07-28 14:26:00 +00:00
|
|
|
|
2010-10-20 22:23:29 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2011 Google Inc.
|
2010-10-20 22:23:29 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-10-20 22:23:29 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2010-10-20 22:23:29 +00:00
|
|
|
#ifndef SkPDFDevice_DEFINED
|
|
|
|
#define SkPDFDevice_DEFINED
|
|
|
|
|
2011-05-24 17:19:38 +00:00
|
|
|
#include "SkCanvas.h"
|
2010-10-20 22:23:29 +00:00
|
|
|
#include "SkDevice.h"
|
2010-10-26 19:48:49 +00:00
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkPath.h"
|
2011-05-09 07:55:58 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTScopedPtr.h"
|
2010-10-20 22:23:29 +00:00
|
|
|
|
|
|
|
class SkPDFArray;
|
|
|
|
class SkPDFDevice;
|
|
|
|
class SkPDFDict;
|
PDF: Add text support with a font framework (font embedding to come).
Supports fakeBold, underline, strikethrough, mode (fill, stroke, both), size, skew, alignment (left, center, right).
Missing is drawFontOnPath and font lookup and embedding.
Changed SkPDFString to support how it is used from drawText methods.
Moved compile assert into SkTypes.
Moved constants and utility function used to support fakeBold, underline, and strikethrough into higher level locations.
Review URL: http://codereview.appspot.com/2946041
git-svn-id: http://skia.googlecode.com/svn/trunk@624 2bbb7eff-a529-9590-31e7-b0007b416f81
2010-11-11 21:37:00 +00:00
|
|
|
class SkPDFFont;
|
2011-05-13 03:50:38 +00:00
|
|
|
class SkPDFFormXObject;
|
2011-07-25 22:34:12 +00:00
|
|
|
class SkPDFGlyphSetMap;
|
2010-10-20 22:23:29 +00:00
|
|
|
class SkPDFGraphicState;
|
|
|
|
class SkPDFObject;
|
2011-03-08 18:31:02 +00:00
|
|
|
class SkPDFShader;
|
2010-10-20 22:23:29 +00:00
|
|
|
class SkPDFStream;
|
|
|
|
|
2011-05-09 07:55:58 +00:00
|
|
|
// Private classes.
|
|
|
|
struct ContentEntry;
|
|
|
|
struct GraphicStateEntry;
|
|
|
|
|
2010-10-20 22:23:29 +00:00
|
|
|
/** \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.
|
2011-04-29 17:54:16 +00:00
|
|
|
* @param pageSize Page size in points.
|
|
|
|
* @param contentSize The content size of the page in points. This will be
|
|
|
|
* combined with the initial transform to determine the drawing area
|
|
|
|
* (as reported by the width and height methods). Anything outside
|
|
|
|
* of the drawing area will be clipped.
|
2011-04-11 23:24:18 +00:00
|
|
|
* @param initialTransform The initial transform to apply to the page.
|
|
|
|
* This may be useful to, for example, move the origin in and
|
|
|
|
* over a bit to account for a margin, scale the canvas,
|
|
|
|
* or apply a rotation. Note1: the SkPDFDevice also applies
|
|
|
|
* a scale+translate transform to move the origin from the
|
|
|
|
* bottom left (PDF default) to the top left. Note2: drawDevice
|
|
|
|
* (used by layer restore) draws the device after this initial
|
2011-07-06 20:00:52 +00:00
|
|
|
* transform is applied, so the PDF device does an
|
2011-04-11 23:24:18 +00:00
|
|
|
* inverse scale+translate to accommodate the one that SkPDFDevice
|
|
|
|
* always does.
|
2010-10-20 22:23:29 +00:00
|
|
|
*/
|
2011-08-20 00:36:18 +00:00
|
|
|
// TODO(vandebo): The sizes should be SkSize and not SkISize.
|
2011-04-29 17:54:16 +00:00
|
|
|
SK_API SkPDFDevice(const SkISize& pageSize, const SkISize& contentSize,
|
|
|
|
const SkMatrix& initialTransform);
|
|
|
|
SK_API virtual ~SkPDFDevice();
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-12-07 18:34:08 +00:00
|
|
|
virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
|
2010-10-26 19:47:30 +00:00
|
|
|
|
2011-12-07 18:34:08 +00:00
|
|
|
virtual void clear(SkColor color) SK_OVERRIDE;
|
2011-04-15 20:57:37 +00:00
|
|
|
|
2010-10-20 22:23:29 +00:00
|
|
|
/** 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.
|
|
|
|
*/
|
2011-12-07 18:34:08 +00:00
|
|
|
virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
|
|
|
|
size_t count, const SkPoint[],
|
2011-12-07 18:34:08 +00:00
|
|
|
const SkPaint& paint) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawRect(const SkDraw&, const SkRect& r, const SkPaint& paint);
|
2011-05-17 18:58:44 +00:00
|
|
|
virtual void drawPath(const SkDraw&, const SkPath& origpath,
|
2011-01-25 22:06:29 +00:00
|
|
|
const SkPaint& paint, const SkMatrix* prePathMatrix,
|
2011-12-07 18:34:08 +00:00
|
|
|
bool pathIsMutable) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
|
2010-12-20 18:26:13 +00:00
|
|
|
const SkIRect* srcRectOrNull,
|
2011-12-07 18:34:08 +00:00
|
|
|
const SkMatrix& matrix, const SkPaint&) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, int x, int y,
|
2011-12-07 18:34:08 +00:00
|
|
|
const SkPaint& paint) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawText(const SkDraw&, const void* text, size_t len,
|
2011-12-07 18:34:08 +00:00
|
|
|
SkScalar x, SkScalar y, const SkPaint&) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawPosText(const SkDraw&, const void* text, size_t len,
|
|
|
|
const SkScalar pos[], SkScalar constY,
|
2011-12-07 18:34:08 +00:00
|
|
|
int scalarsPerPos, const SkPaint&) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
|
|
|
|
const SkPath& path, const SkMatrix* matrix,
|
2011-12-07 18:34:08 +00:00
|
|
|
const SkPaint& paint) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
|
|
|
|
int vertexCount, const SkPoint verts[],
|
|
|
|
const SkPoint texs[], const SkColor colors[],
|
|
|
|
SkXfermode* xmode, const uint16_t indices[],
|
2011-12-07 18:34:08 +00:00
|
|
|
int indexCount, const SkPaint& paint) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
|
2011-12-07 18:34:08 +00:00
|
|
|
const SkPaint&) SK_OVERRIDE;
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-07-12 21:56:27 +00:00
|
|
|
enum DrawingArea {
|
2011-07-27 00:10:51 +00:00
|
|
|
kContent_DrawingArea, // Drawing area for the page content.
|
|
|
|
kMargin_DrawingArea, // Drawing area for the margin content.
|
2011-07-12 21:56:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Sets the drawing area for the device. Subsequent draw calls are directed
|
2011-08-20 00:36:18 +00:00
|
|
|
* to the specific drawing area (margin or content). The default drawing
|
2011-07-12 21:56:27 +00:00
|
|
|
* area is the content drawing area.
|
2011-07-27 00:10:51 +00:00
|
|
|
*
|
|
|
|
* Currently if margin content is drawn and then a complex (for PDF) xfer
|
|
|
|
* mode is used, like SrcIn, Clear, etc, the margin content will get
|
|
|
|
* clipped. A simple way to avoid the bug is to always draw the margin
|
|
|
|
* content last.
|
2011-07-12 21:56:27 +00:00
|
|
|
*/
|
2011-08-26 14:40:38 +00:00
|
|
|
SK_API void setDrawingArea(DrawingArea drawingArea);
|
2011-07-12 21:56:27 +00:00
|
|
|
|
2010-10-20 22:23:29 +00:00
|
|
|
// PDF specific methods.
|
|
|
|
|
2011-07-20 18:43:19 +00:00
|
|
|
/** Returns the resource dictionary for this device.
|
2010-10-20 22:23:29 +00:00
|
|
|
*/
|
2011-07-20 18:43:19 +00:00
|
|
|
SK_API SkPDFDict* getResourceDict();
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2010-10-26 19:48:49 +00:00
|
|
|
/** Get the list of resources (PDF objects) used on this page.
|
|
|
|
* @param resourceList A list to append the resources to.
|
2012-03-09 22:16:58 +00:00
|
|
|
* @param recursive If recursive is true, get the resources of the
|
|
|
|
* device's resources recursively. (Useful for adding
|
|
|
|
* objects to the catalog.)
|
2010-10-20 22:23:29 +00:00
|
|
|
*/
|
2012-03-09 22:16:58 +00:00
|
|
|
SK_API void getResources(SkTDArray<SkPDFObject*>* resourceList,
|
|
|
|
bool recursive) const;
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-05-29 05:55:42 +00:00
|
|
|
/** Get the fonts used on this device.
|
|
|
|
*/
|
2011-05-30 20:52:33 +00:00
|
|
|
SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
|
2011-05-29 05:55:42 +00:00
|
|
|
|
2010-10-20 22:23:29 +00:00
|
|
|
/** Returns the media box for this device.
|
|
|
|
*/
|
2011-05-30 20:52:33 +00:00
|
|
|
SK_API SkRefPtr<SkPDFArray> getMediaBox() const;
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-02-24 23:22:30 +00:00
|
|
|
/** Returns a SkStream with the page contents. The caller is responsible
|
|
|
|
for a reference to the returned value.
|
2011-06-27 14:42:15 +00:00
|
|
|
DEPRECATED: use copyContentToData()
|
2010-10-20 22:23:29 +00:00
|
|
|
*/
|
2011-05-30 20:52:33 +00:00
|
|
|
SK_API SkStream* content() const;
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-06-27 14:42:15 +00:00
|
|
|
/** Returns a SkStream with the page contents. The caller is responsible
|
|
|
|
* for calling data->unref() when it is finished.
|
|
|
|
*/
|
|
|
|
SK_API SkData* copyContentToData() const;
|
2011-07-25 22:34:12 +00:00
|
|
|
|
2011-05-30 20:52:33 +00:00
|
|
|
SK_API const SkMatrix& initialTransform() const {
|
|
|
|
return fInitialTransform;
|
|
|
|
}
|
2011-05-24 23:02:07 +00:00
|
|
|
|
2011-07-25 22:34:12 +00:00
|
|
|
/** Returns a SkPDFGlyphSetMap which represents glyph usage of every font
|
|
|
|
* that shows on this device.
|
|
|
|
*/
|
|
|
|
const SkPDFGlyphSetMap& getFontGlyphUsage() const {
|
|
|
|
return *(fFontGlyphUsage.get());
|
|
|
|
}
|
2012-01-28 01:45:11 +00:00
|
|
|
|
2011-11-02 19:57:21 +00:00
|
|
|
protected:
|
2011-12-07 18:34:08 +00:00
|
|
|
virtual bool onReadPixels(const SkBitmap& bitmap, int x, int y,
|
|
|
|
SkCanvas::Config8888) SK_OVERRIDE;
|
2011-07-25 22:34:12 +00:00
|
|
|
|
2012-01-06 14:43:09 +00:00
|
|
|
virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE;
|
|
|
|
|
2010-10-20 22:23:29 +00:00
|
|
|
private:
|
2011-08-20 00:36:18 +00:00
|
|
|
// TODO(vandebo): push most of SkPDFDevice's state into a core object in
|
2011-05-24 17:19:38 +00:00
|
|
|
// order to get the right access levels without using friend.
|
2011-05-24 23:12:41 +00:00
|
|
|
friend class ScopedContentEntry;
|
2011-05-09 07:58:08 +00:00
|
|
|
|
2011-04-29 17:54:16 +00:00
|
|
|
SkISize fPageSize;
|
2011-05-09 07:55:58 +00:00
|
|
|
SkISize fContentSize;
|
2011-04-11 23:24:18 +00:00
|
|
|
SkMatrix fInitialTransform;
|
2011-05-09 07:55:58 +00:00
|
|
|
SkClipStack fExistingClipStack;
|
|
|
|
SkRegion fExistingClipRegion;
|
2010-10-20 22:23:29 +00:00
|
|
|
SkRefPtr<SkPDFDict> fResourceDict;
|
|
|
|
|
|
|
|
SkTDArray<SkPDFGraphicState*> fGraphicStateResources;
|
|
|
|
SkTDArray<SkPDFObject*> fXObjectResources;
|
PDF: Add text support with a font framework (font embedding to come).
Supports fakeBold, underline, strikethrough, mode (fill, stroke, both), size, skew, alignment (left, center, right).
Missing is drawFontOnPath and font lookup and embedding.
Changed SkPDFString to support how it is used from drawText methods.
Moved compile assert into SkTypes.
Moved constants and utility function used to support fakeBold, underline, and strikethrough into higher level locations.
Review URL: http://codereview.appspot.com/2946041
git-svn-id: http://skia.googlecode.com/svn/trunk@624 2bbb7eff-a529-9590-31e7-b0007b416f81
2010-11-11 21:37:00 +00:00
|
|
|
SkTDArray<SkPDFFont*> fFontResources;
|
2011-07-20 17:39:01 +00:00
|
|
|
SkTDArray<SkPDFObject*> fShaderResources;
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-05-09 07:55:58 +00:00
|
|
|
SkTScopedPtr<ContentEntry> fContentEntries;
|
2011-05-24 17:19:38 +00:00
|
|
|
ContentEntry* fLastContentEntry;
|
2011-07-12 21:56:27 +00:00
|
|
|
SkTScopedPtr<ContentEntry> fMarginContentEntries;
|
|
|
|
ContentEntry* fLastMarginContentEntry;
|
|
|
|
DrawingArea fDrawingArea;
|
|
|
|
|
|
|
|
// Accessor and setter functions based on the current DrawingArea.
|
2011-07-27 00:10:51 +00:00
|
|
|
SkTScopedPtr<ContentEntry>* getContentEntries();
|
2011-07-12 21:56:27 +00:00
|
|
|
ContentEntry* getLastContentEntry();
|
|
|
|
void setLastContentEntry(ContentEntry* contentEntry);
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-07-25 22:34:12 +00:00
|
|
|
// Glyph ids used for each font on this device.
|
|
|
|
SkTScopedPtr<SkPDFGlyphSetMap> fFontGlyphUsage;
|
|
|
|
|
2011-05-09 07:58:08 +00:00
|
|
|
SkPDFDevice(const SkISize& layerSize, const SkClipStack& existingClipStack,
|
|
|
|
const SkRegion& existingClipRegion);
|
|
|
|
|
2011-06-17 13:10:25 +00:00
|
|
|
// override from SkDevice
|
2011-07-25 22:34:12 +00:00
|
|
|
virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
|
|
|
|
int width, int height,
|
2011-06-17 13:10:25 +00:00
|
|
|
bool isOpaque,
|
2011-12-07 18:34:08 +00:00
|
|
|
Usage usage) SK_OVERRIDE;
|
2011-06-17 13:10:25 +00:00
|
|
|
|
2011-04-15 20:57:37 +00:00
|
|
|
void init();
|
2011-07-25 22:34:12 +00:00
|
|
|
void cleanUp(bool clearFontUsage);
|
2011-05-13 03:50:38 +00:00
|
|
|
void createFormXObjectFromDevice(SkRefPtr<SkPDFFormXObject>* xobject);
|
2011-05-09 07:55:58 +00:00
|
|
|
|
2011-05-18 23:06:29 +00:00
|
|
|
// Clear the passed clip from all existing content entries.
|
|
|
|
void clearClipFromContent(const SkClipStack* clipStack,
|
|
|
|
const SkRegion& clipRegion);
|
[PDF] Add clip support and some optimizations for "complex" xfer modes.
For Clear, Src, Dst/Src-In/Out, we have to consider the current clip, and
potentially draw Dst, clipped to the inverse of the current clip before
doing the operation of interest.
For clear or src, if we haven't drawn anything, or the clip is empty, there's
nothing to be done.
For Src/Dst-In/Out, if either is empty, the result is empty.
Review URL: http://codereview.appspot.com/4538082
git-svn-id: http://skia.googlecode.com/svn/trunk@1407 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-05-24 16:39:05 +00:00
|
|
|
void drawFormXObjectWithClip(SkPDFFormXObject* form,
|
|
|
|
const SkClipStack* clipStack,
|
|
|
|
const SkRegion& clipRegion,
|
|
|
|
bool invertClip);
|
2011-05-18 23:06:29 +00:00
|
|
|
|
2011-05-24 17:19:38 +00:00
|
|
|
// If the paint or clip is such that we shouldn't draw anything, this
|
|
|
|
// returns NULL and does not create a content entry.
|
|
|
|
// setUpContentEntry and finishContentEntry can be used directly, but
|
2011-05-24 23:12:41 +00:00
|
|
|
// the preferred method is to use the ScopedContentEntry helper class.
|
2011-05-24 17:19:38 +00:00
|
|
|
ContentEntry* setUpContentEntry(const SkClipStack* clipStack,
|
|
|
|
const SkRegion& clipRegion,
|
|
|
|
const SkMatrix& matrix,
|
|
|
|
const SkPaint& paint,
|
|
|
|
bool hasText,
|
|
|
|
SkRefPtr<SkPDFFormXObject>* dst);
|
|
|
|
void finishContentEntry(SkXfermode::Mode xfermode,
|
|
|
|
SkPDFFormXObject* dst);
|
[PDF] Add clip support and some optimizations for "complex" xfer modes.
For Clear, Src, Dst/Src-In/Out, we have to consider the current clip, and
potentially draw Dst, clipped to the inverse of the current clip before
doing the operation of interest.
For clear or src, if we haven't drawn anything, or the clip is empty, there's
nothing to be done.
For Src/Dst-In/Out, if either is empty, the result is empty.
Review URL: http://codereview.appspot.com/4538082
git-svn-id: http://skia.googlecode.com/svn/trunk@1407 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-05-24 16:39:05 +00:00
|
|
|
bool isContentEmpty();
|
|
|
|
|
2011-05-09 07:55:58 +00:00
|
|
|
void populateGraphicStateEntryFromPaint(const SkMatrix& matrix,
|
|
|
|
const SkClipStack& clipStack,
|
|
|
|
const SkRegion& clipRegion,
|
|
|
|
const SkPaint& paint,
|
|
|
|
bool hasText,
|
|
|
|
GraphicStateEntry* entry);
|
2011-05-13 03:50:38 +00:00
|
|
|
int addGraphicStateResource(SkPDFGraphicState* gs);
|
2011-05-09 07:55:58 +00:00
|
|
|
|
2011-05-24 17:19:38 +00:00
|
|
|
void updateFont(const SkPaint& paint, uint16_t glyphID,
|
|
|
|
ContentEntry* contentEntry);
|
2011-03-04 21:43:27 +00:00
|
|
|
int getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID);
|
2011-05-09 07:55:58 +00:00
|
|
|
|
2011-05-24 17:19:38 +00:00
|
|
|
void internalDrawPaint(const SkPaint& paint, ContentEntry* contentEntry);
|
2011-05-09 07:55:58 +00:00
|
|
|
void internalDrawBitmap(const SkMatrix& matrix,
|
2011-05-11 18:46:03 +00:00
|
|
|
const SkClipStack* clipStack,
|
2011-05-09 07:55:58 +00:00
|
|
|
const SkRegion& clipRegion,
|
|
|
|
const SkBitmap& bitmap,
|
|
|
|
const SkIRect* srcRect,
|
|
|
|
const SkPaint& paint);
|
2010-10-20 22:23:29 +00:00
|
|
|
|
2011-07-12 21:56:27 +00:00
|
|
|
/** Helper method for copyContentToData. It is responsible for copying the
|
|
|
|
* list of content entries |entry| to |data|.
|
|
|
|
*/
|
|
|
|
void copyContentEntriesToData(ContentEntry* entry, SkWStream* data) const;
|
2011-07-27 00:10:51 +00:00
|
|
|
|
2011-05-09 07:55:58 +00:00
|
|
|
// Disable the default copy and assign implementation.
|
|
|
|
SkPDFDevice(const SkPDFDevice&);
|
|
|
|
void operator=(const SkPDFDevice&);
|
2010-10-20 22:23:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|