blob: c948a902fb03e02881226b1eb25ae7b06e2a128f [file] [log] [blame]
bungeman@google.comb29c8832011-10-10 13:19:10 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkXPSDevice_DEFINED
9#define SkXPSDevice_DEFINED
10
11#include "SkTypes.h"
12#include <ObjBase.h>
13#include <XpsObjectModel.h>
14
15#include "SkAutoCoInitialize.h"
16#include "SkBitSet.h"
17#include "SkCanvas.h"
18#include "SkColor.h"
19#include "SkDevice.h"
20#include "SkPaint.h"
21#include "SkPath.h"
22#include "SkPoint.h"
23#include "SkShader.h"
24#include "SkSize.h"
25#include "SkTArray.h"
26#include "SkTScopedComPtr.h"
27#include "SkTypeface.h"
28
29/** \class SkXPSDevice
30
31 The drawing context for the XPS backend.
32*/
33class SkXPSDevice : public SkDevice {
34public:
35 SK_API SkXPSDevice();
36 SK_API virtual ~SkXPSDevice();
37
38 virtual bool beginPortfolio(SkWStream* outputStream);
39 /**
40 @param unitsPerMeter converts geometry units into physical units.
41 @param pixelsPerMeter resolution to use when geometry must be rasterized.
42 @param trimSize final page size in physical units.
43 The top left of the trim is the origin of physical space.
44 @param mediaBox The size of the physical media in physical units.
45 The top and left must be less than zero.
46 The bottom and right must be greater than the trimSize.
47 The default is to coincide with the trimSize.
48 @param bleedBox The size of the bleed box in physical units.
49 Must be contained within the mediaBox.
50 The default is to coincide with the mediaBox.
51 @param artBox The size of the content box in physical units.
52 Must be contained within the trimSize.
53 The default is to coincide with the trimSize.
54 @param cropBox The size of the recommended view port in physical units.
55 Must be contained within the mediaBox.
56 The default is to coincide with the mediaBox.
57 */
58 virtual bool beginSheet(
59 const SkVector& unitsPerMeter,
60 const SkVector& pixelsPerMeter,
61 const SkSize& trimSize,
62 const SkRect* mediaBox = NULL,
63 const SkRect* bleedBox = NULL,
64 const SkRect* artBox = NULL,
65 const SkRect* cropBox = NULL);
66
67 virtual bool endSheet();
68 virtual bool endPortfolio();
69
reed@google.com982cb872011-12-07 18:34:08 +000070 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
bungeman@google.comb29c8832011-10-10 13:19:10 +000071
bungeman@google.comb29c8832011-10-10 13:19:10 +000072protected:
73 virtual void clear(SkColor color) SK_OVERRIDE;
74
75 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
76
77 virtual void drawPoints(
78 const SkDraw&,
79 SkCanvas::PointMode mode,
80 size_t count, const SkPoint[],
81 const SkPaint& paint) SK_OVERRIDE;
82
83 virtual void drawRect(
84 const SkDraw&,
85 const SkRect& r,
86 const SkPaint& paint) SK_OVERRIDE;
87
88 virtual void drawPath(
89 const SkDraw&,
90 const SkPath& platonicPath,
91 const SkPaint& paint,
92 const SkMatrix* prePathMatrix,
93 bool pathIsMutable) SK_OVERRIDE;
94
95 virtual void drawBitmap(
96 const SkDraw&,
97 const SkBitmap& bitmap,
98 const SkIRect* srcRectOrNull,
99 const SkMatrix& matrix,
100 const SkPaint& paint) SK_OVERRIDE;
101
102 virtual void drawSprite(
103 const SkDraw&,
104 const SkBitmap& bitmap,
105 int x, int y,
106 const SkPaint& paint) SK_OVERRIDE;
107
108 virtual void drawText(
109 const SkDraw&,
110 const void* text, size_t len,
111 SkScalar x, SkScalar y,
112 const SkPaint& paint) SK_OVERRIDE;
113
114 virtual void drawPosText(
115 const SkDraw&,
116 const void* text, size_t len,
117 const SkScalar pos[], SkScalar constY, int scalarsPerPos,
118 const SkPaint& paint) SK_OVERRIDE;
119
120 virtual void drawTextOnPath(
121 const SkDraw&,
122 const void* text, size_t len,
123 const SkPath& path,
124 const SkMatrix* matrix,
125 const SkPaint& paint) SK_OVERRIDE;
126
127 virtual void drawVertices(
128 const SkDraw&,
129 SkCanvas::VertexMode,
130 int vertexCount, const SkPoint verts[],
131 const SkPoint texs[], const SkColor colors[],
132 SkXfermode* xmode,
133 const uint16_t indices[], int indexCount,
134 const SkPaint& paint) SK_OVERRIDE;
135
136 virtual void drawDevice(
137 const SkDraw&,
138 SkDevice* device,
139 int x, int y,
140 const SkPaint& paint) SK_OVERRIDE;
141
bsalomon@google.com910267d2011-11-02 20:06:25 +0000142 virtual bool onReadPixels(const SkBitmap& bitmap,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000143 int x,
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000144 int y,
reed@google.com982cb872011-12-07 18:34:08 +0000145 SkCanvas::Config8888) SK_OVERRIDE;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000146
bungeman@google.comb29c8832011-10-10 13:19:10 +0000147private:
148 class TypefaceUse : ::SkNoncopyable {
149 public:
150 SkFontID typefaceId;
151 SkStream* fontData;
152 IXpsOMFontResource* xpsFont;
153 SkBitSet* glyphsUsed;
154
155 explicit TypefaceUse();
156 ~TypefaceUse();
157 };
158 friend static HRESULT subset_typeface(TypefaceUse* current);
159
160 SkXPSDevice(IXpsOMObjectFactory* xpsFactory);
161
162 SkAutoCoInitialize fAutoCo;
163 SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
164 SkTScopedComPtr<IStream> fOutputStream;
165 SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter;
166
167 unsigned int fCurrentPage;
168 SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas;
169 SkSize fCurrentCanvasSize;
170 SkVector fCurrentUnitsPerMeter;
171 SkVector fCurrentPixelsPerMeter;
172
173 SkTArray<TypefaceUse, true> fTypefaces;
174
175 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
176
177 HRESULT createXpsPage(
178 const XPS_SIZE& pageSize,
179 IXpsOMPage** page);
180
181 HRESULT createXpsThumbnail(
182 IXpsOMPage* page, const unsigned int pageNumber,
183 IXpsOMImageResource** image);
184
185 void internalDrawRect(
186 const SkDraw&,
187 const SkRect& r,
188 bool transformRect,
189 const SkPaint& paint);
190
191 HRESULT createXpsBrush(
192 const SkPaint& skPaint,
193 IXpsOMBrush** xpsBrush,
194 const SkMatrix* parentTransform = NULL);
195
196 HRESULT createXpsSolidColorBrush(
197 const SkColor skColor, const SkAlpha alpha,
198 IXpsOMBrush** xpsBrush);
199
200 HRESULT createXpsImageBrush(
201 const SkBitmap& bitmap,
202 const SkMatrix& localMatrix,
203 const SkShader::TileMode (&xy)[2],
204 const SkAlpha alpha,
205 IXpsOMTileBrush** xpsBrush);
206
207 HRESULT createXpsLinearGradient(
208 SkShader::GradientInfo info,
209 const SkAlpha alpha,
210 const SkMatrix& localMatrix,
211 IXpsOMMatrixTransform* xpsMatrixToUse,
212 IXpsOMBrush** xpsBrush);
213
214 HRESULT createXpsRadialGradient(
215 SkShader::GradientInfo info,
216 const SkAlpha alpha,
217 const SkMatrix& localMatrix,
218 IXpsOMMatrixTransform* xpsMatrixToUse,
219 IXpsOMBrush** xpsBrush);
220
221 HRESULT createXpsGradientStop(
222 const SkColor skColor,
223 const SkScalar offset,
224 IXpsOMGradientStop** xpsGradStop);
225
226 HRESULT createXpsTransform(
227 const SkMatrix& matrix,
228 IXpsOMMatrixTransform ** xpsTransform);
229
230 HRESULT createXpsRect(
231 const SkRect& rect,
232 BOOL stroke, BOOL fill,
233 IXpsOMGeometryFigure** xpsRect);
234
235 HRESULT createXpsQuad(
236 const SkPoint (&points)[4],
237 BOOL stroke, BOOL fill,
238 IXpsOMGeometryFigure** xpsQuad);
239
240 HRESULT CreateTypefaceUse(
241 const SkPaint& paint,
242 TypefaceUse** fontResource);
243
244 HRESULT AddGlyphs(
245 const SkDraw& d,
246 IXpsOMObjectFactory* xpsFactory,
247 IXpsOMCanvas* canvas,
248 IXpsOMFontResource* font,
249 LPCWSTR text,
250 XPS_GLYPH_INDEX* xpsGlyphs,
251 UINT32 xpsGlyphsLen,
252 XPS_POINT *origin,
253 FLOAT fontSize,
254 XPS_STYLE_SIMULATION sims,
255 const SkMatrix& transform,
256 const SkPaint& paint);
257
258 HRESULT addXpsPathGeometry(
259 IXpsOMGeometryFigureCollection* figures,
260 BOOL stroke, BOOL fill, const SkPath& path);
261
262 HRESULT createPath(
263 IXpsOMGeometryFigure* figure,
264 IXpsOMVisualCollection* visuals,
265 IXpsOMPath** path);
266
267 HRESULT sideOfClamp(
268 const SkRect& leftPoints, const XPS_RECT& left,
269 IXpsOMImageResource* imageResource,
270 IXpsOMVisualCollection* visuals);
271
272 HRESULT cornerOfClamp(
273 const SkRect& tlPoints,
274 const SkColor color,
275 IXpsOMVisualCollection* visuals);
276
277 HRESULT clip(
278 IXpsOMVisual* xpsVisual,
279 const SkDraw& d);
280 HRESULT clipToPath(
281 IXpsOMVisual* xpsVisual,
282 const SkPath& clipPath,
283 XPS_FILL_RULE fillRule);
284
285 HRESULT drawInverseWindingPath(
286 const SkDraw& d,
287 const SkPath& devicePath,
288 IXpsOMPath* xpsPath);
289
290 HRESULT shadePath(
291 IXpsOMPath* shadedPath,
292 const SkPaint& shaderPaint,
293 const SkMatrix& matrix,
294 BOOL* fill, BOOL* stroke);
295
296 void convertToPpm(
297 const SkMaskFilter* filter,
298 SkMatrix* matrix,
299 SkVector* ppuScale,
300 const SkIRect& clip, SkIRect* clipIRect);
301
302 HRESULT applyMask(
303 const SkDraw& d,
304 const SkMask& mask,
305 const SkVector& ppuScale,
306 IXpsOMPath* shadedPath);
307
308 // override from SkDevice
309 virtual SkDevice* onCreateCompatibleDevice(
310 SkBitmap::Config config,
311 int width, int height,
312 bool isOpaque,
313 Usage usage) SK_OVERRIDE;
314
315 // Disable the default copy and assign implementation.
316 SkXPSDevice(const SkXPSDevice&);
317 void operator=(const SkXPSDevice&);
318
319 typedef SkDevice INHERITED;
320};
321
322#endif