blob: ed61ced8d02a3ebcfe872a4a2b4001c58311e785 [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
70 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE {
71 return kVector_Capability;
72 }
73
bungeman@google.comb29c8832011-10-10 13:19:10 +000074protected:
75 virtual void clear(SkColor color) SK_OVERRIDE;
76
77 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE;
78
79 virtual void drawPoints(
80 const SkDraw&,
81 SkCanvas::PointMode mode,
82 size_t count, const SkPoint[],
83 const SkPaint& paint) SK_OVERRIDE;
84
85 virtual void drawRect(
86 const SkDraw&,
87 const SkRect& r,
88 const SkPaint& paint) SK_OVERRIDE;
89
90 virtual void drawPath(
91 const SkDraw&,
92 const SkPath& platonicPath,
93 const SkPaint& paint,
94 const SkMatrix* prePathMatrix,
95 bool pathIsMutable) SK_OVERRIDE;
96
97 virtual void drawBitmap(
98 const SkDraw&,
99 const SkBitmap& bitmap,
100 const SkIRect* srcRectOrNull,
101 const SkMatrix& matrix,
102 const SkPaint& paint) SK_OVERRIDE;
103
104 virtual void drawSprite(
105 const SkDraw&,
106 const SkBitmap& bitmap,
107 int x, int y,
108 const SkPaint& paint) SK_OVERRIDE;
109
110 virtual void drawText(
111 const SkDraw&,
112 const void* text, size_t len,
113 SkScalar x, SkScalar y,
114 const SkPaint& paint) SK_OVERRIDE;
115
116 virtual void drawPosText(
117 const SkDraw&,
118 const void* text, size_t len,
119 const SkScalar pos[], SkScalar constY, int scalarsPerPos,
120 const SkPaint& paint) SK_OVERRIDE;
121
122 virtual void drawTextOnPath(
123 const SkDraw&,
124 const void* text, size_t len,
125 const SkPath& path,
126 const SkMatrix* matrix,
127 const SkPaint& paint) SK_OVERRIDE;
128
129 virtual void drawVertices(
130 const SkDraw&,
131 SkCanvas::VertexMode,
132 int vertexCount, const SkPoint verts[],
133 const SkPoint texs[], const SkColor colors[],
134 SkXfermode* xmode,
135 const uint16_t indices[], int indexCount,
136 const SkPaint& paint) SK_OVERRIDE;
137
138 virtual void drawDevice(
139 const SkDraw&,
140 SkDevice* device,
141 int x, int y,
142 const SkPaint& paint) SK_OVERRIDE;
143
bsalomon@google.comc6980972011-11-02 19:57:21 +0000144 virtual bool onReadPixels(const SkBitmap* bitmap,
145 int x,
146 int y) SK_OVERRIDE {
147 return false;
148 }
149
bungeman@google.comb29c8832011-10-10 13:19:10 +0000150private:
151 class TypefaceUse : ::SkNoncopyable {
152 public:
153 SkFontID typefaceId;
154 SkStream* fontData;
155 IXpsOMFontResource* xpsFont;
156 SkBitSet* glyphsUsed;
157
158 explicit TypefaceUse();
159 ~TypefaceUse();
160 };
161 friend static HRESULT subset_typeface(TypefaceUse* current);
162
163 SkXPSDevice(IXpsOMObjectFactory* xpsFactory);
164
165 SkAutoCoInitialize fAutoCo;
166 SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
167 SkTScopedComPtr<IStream> fOutputStream;
168 SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter;
169
170 unsigned int fCurrentPage;
171 SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas;
172 SkSize fCurrentCanvasSize;
173 SkVector fCurrentUnitsPerMeter;
174 SkVector fCurrentPixelsPerMeter;
175
176 SkTArray<TypefaceUse, true> fTypefaces;
177
178 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
179
180 HRESULT createXpsPage(
181 const XPS_SIZE& pageSize,
182 IXpsOMPage** page);
183
184 HRESULT createXpsThumbnail(
185 IXpsOMPage* page, const unsigned int pageNumber,
186 IXpsOMImageResource** image);
187
188 void internalDrawRect(
189 const SkDraw&,
190 const SkRect& r,
191 bool transformRect,
192 const SkPaint& paint);
193
194 HRESULT createXpsBrush(
195 const SkPaint& skPaint,
196 IXpsOMBrush** xpsBrush,
197 const SkMatrix* parentTransform = NULL);
198
199 HRESULT createXpsSolidColorBrush(
200 const SkColor skColor, const SkAlpha alpha,
201 IXpsOMBrush** xpsBrush);
202
203 HRESULT createXpsImageBrush(
204 const SkBitmap& bitmap,
205 const SkMatrix& localMatrix,
206 const SkShader::TileMode (&xy)[2],
207 const SkAlpha alpha,
208 IXpsOMTileBrush** xpsBrush);
209
210 HRESULT createXpsLinearGradient(
211 SkShader::GradientInfo info,
212 const SkAlpha alpha,
213 const SkMatrix& localMatrix,
214 IXpsOMMatrixTransform* xpsMatrixToUse,
215 IXpsOMBrush** xpsBrush);
216
217 HRESULT createXpsRadialGradient(
218 SkShader::GradientInfo info,
219 const SkAlpha alpha,
220 const SkMatrix& localMatrix,
221 IXpsOMMatrixTransform* xpsMatrixToUse,
222 IXpsOMBrush** xpsBrush);
223
224 HRESULT createXpsGradientStop(
225 const SkColor skColor,
226 const SkScalar offset,
227 IXpsOMGradientStop** xpsGradStop);
228
229 HRESULT createXpsTransform(
230 const SkMatrix& matrix,
231 IXpsOMMatrixTransform ** xpsTransform);
232
233 HRESULT createXpsRect(
234 const SkRect& rect,
235 BOOL stroke, BOOL fill,
236 IXpsOMGeometryFigure** xpsRect);
237
238 HRESULT createXpsQuad(
239 const SkPoint (&points)[4],
240 BOOL stroke, BOOL fill,
241 IXpsOMGeometryFigure** xpsQuad);
242
243 HRESULT CreateTypefaceUse(
244 const SkPaint& paint,
245 TypefaceUse** fontResource);
246
247 HRESULT AddGlyphs(
248 const SkDraw& d,
249 IXpsOMObjectFactory* xpsFactory,
250 IXpsOMCanvas* canvas,
251 IXpsOMFontResource* font,
252 LPCWSTR text,
253 XPS_GLYPH_INDEX* xpsGlyphs,
254 UINT32 xpsGlyphsLen,
255 XPS_POINT *origin,
256 FLOAT fontSize,
257 XPS_STYLE_SIMULATION sims,
258 const SkMatrix& transform,
259 const SkPaint& paint);
260
261 HRESULT addXpsPathGeometry(
262 IXpsOMGeometryFigureCollection* figures,
263 BOOL stroke, BOOL fill, const SkPath& path);
264
265 HRESULT createPath(
266 IXpsOMGeometryFigure* figure,
267 IXpsOMVisualCollection* visuals,
268 IXpsOMPath** path);
269
270 HRESULT sideOfClamp(
271 const SkRect& leftPoints, const XPS_RECT& left,
272 IXpsOMImageResource* imageResource,
273 IXpsOMVisualCollection* visuals);
274
275 HRESULT cornerOfClamp(
276 const SkRect& tlPoints,
277 const SkColor color,
278 IXpsOMVisualCollection* visuals);
279
280 HRESULT clip(
281 IXpsOMVisual* xpsVisual,
282 const SkDraw& d);
283 HRESULT clipToPath(
284 IXpsOMVisual* xpsVisual,
285 const SkPath& clipPath,
286 XPS_FILL_RULE fillRule);
287
288 HRESULT drawInverseWindingPath(
289 const SkDraw& d,
290 const SkPath& devicePath,
291 IXpsOMPath* xpsPath);
292
293 HRESULT shadePath(
294 IXpsOMPath* shadedPath,
295 const SkPaint& shaderPaint,
296 const SkMatrix& matrix,
297 BOOL* fill, BOOL* stroke);
298
299 void convertToPpm(
300 const SkMaskFilter* filter,
301 SkMatrix* matrix,
302 SkVector* ppuScale,
303 const SkIRect& clip, SkIRect* clipIRect);
304
305 HRESULT applyMask(
306 const SkDraw& d,
307 const SkMask& mask,
308 const SkVector& ppuScale,
309 IXpsOMPath* shadedPath);
310
311 // override from SkDevice
312 virtual SkDevice* onCreateCompatibleDevice(
313 SkBitmap::Config config,
314 int width, int height,
315 bool isOpaque,
316 Usage usage) SK_OVERRIDE;
317
318 // Disable the default copy and assign implementation.
319 SkXPSDevice(const SkXPSDevice&);
320 void operator=(const SkXPSDevice&);
321
322 typedef SkDevice INHERITED;
323};
324
325#endif