blob: 2ba20f074414a79c5c20a2b849651faf749710f4 [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.com910267d2011-11-02 20:06:25 +0000144 virtual bool onReadPixels(const SkBitmap& bitmap,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000145 int x,
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000146 int y,
147 SkCanvas::Config8888) SK_OVERRIDE {
bsalomon@google.comc6980972011-11-02 19:57:21 +0000148 return false;
149 }
150
bungeman@google.comb29c8832011-10-10 13:19:10 +0000151private:
152 class TypefaceUse : ::SkNoncopyable {
153 public:
154 SkFontID typefaceId;
155 SkStream* fontData;
156 IXpsOMFontResource* xpsFont;
157 SkBitSet* glyphsUsed;
158
159 explicit TypefaceUse();
160 ~TypefaceUse();
161 };
162 friend static HRESULT subset_typeface(TypefaceUse* current);
163
164 SkXPSDevice(IXpsOMObjectFactory* xpsFactory);
165
166 SkAutoCoInitialize fAutoCo;
167 SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
168 SkTScopedComPtr<IStream> fOutputStream;
169 SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter;
170
171 unsigned int fCurrentPage;
172 SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas;
173 SkSize fCurrentCanvasSize;
174 SkVector fCurrentUnitsPerMeter;
175 SkVector fCurrentPixelsPerMeter;
176
177 SkTArray<TypefaceUse, true> fTypefaces;
178
179 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
180
181 HRESULT createXpsPage(
182 const XPS_SIZE& pageSize,
183 IXpsOMPage** page);
184
185 HRESULT createXpsThumbnail(
186 IXpsOMPage* page, const unsigned int pageNumber,
187 IXpsOMImageResource** image);
188
189 void internalDrawRect(
190 const SkDraw&,
191 const SkRect& r,
192 bool transformRect,
193 const SkPaint& paint);
194
195 HRESULT createXpsBrush(
196 const SkPaint& skPaint,
197 IXpsOMBrush** xpsBrush,
198 const SkMatrix* parentTransform = NULL);
199
200 HRESULT createXpsSolidColorBrush(
201 const SkColor skColor, const SkAlpha alpha,
202 IXpsOMBrush** xpsBrush);
203
204 HRESULT createXpsImageBrush(
205 const SkBitmap& bitmap,
206 const SkMatrix& localMatrix,
207 const SkShader::TileMode (&xy)[2],
208 const SkAlpha alpha,
209 IXpsOMTileBrush** xpsBrush);
210
211 HRESULT createXpsLinearGradient(
212 SkShader::GradientInfo info,
213 const SkAlpha alpha,
214 const SkMatrix& localMatrix,
215 IXpsOMMatrixTransform* xpsMatrixToUse,
216 IXpsOMBrush** xpsBrush);
217
218 HRESULT createXpsRadialGradient(
219 SkShader::GradientInfo info,
220 const SkAlpha alpha,
221 const SkMatrix& localMatrix,
222 IXpsOMMatrixTransform* xpsMatrixToUse,
223 IXpsOMBrush** xpsBrush);
224
225 HRESULT createXpsGradientStop(
226 const SkColor skColor,
227 const SkScalar offset,
228 IXpsOMGradientStop** xpsGradStop);
229
230 HRESULT createXpsTransform(
231 const SkMatrix& matrix,
232 IXpsOMMatrixTransform ** xpsTransform);
233
234 HRESULT createXpsRect(
235 const SkRect& rect,
236 BOOL stroke, BOOL fill,
237 IXpsOMGeometryFigure** xpsRect);
238
239 HRESULT createXpsQuad(
240 const SkPoint (&points)[4],
241 BOOL stroke, BOOL fill,
242 IXpsOMGeometryFigure** xpsQuad);
243
244 HRESULT CreateTypefaceUse(
245 const SkPaint& paint,
246 TypefaceUse** fontResource);
247
248 HRESULT AddGlyphs(
249 const SkDraw& d,
250 IXpsOMObjectFactory* xpsFactory,
251 IXpsOMCanvas* canvas,
252 IXpsOMFontResource* font,
253 LPCWSTR text,
254 XPS_GLYPH_INDEX* xpsGlyphs,
255 UINT32 xpsGlyphsLen,
256 XPS_POINT *origin,
257 FLOAT fontSize,
258 XPS_STYLE_SIMULATION sims,
259 const SkMatrix& transform,
260 const SkPaint& paint);
261
262 HRESULT addXpsPathGeometry(
263 IXpsOMGeometryFigureCollection* figures,
264 BOOL stroke, BOOL fill, const SkPath& path);
265
266 HRESULT createPath(
267 IXpsOMGeometryFigure* figure,
268 IXpsOMVisualCollection* visuals,
269 IXpsOMPath** path);
270
271 HRESULT sideOfClamp(
272 const SkRect& leftPoints, const XPS_RECT& left,
273 IXpsOMImageResource* imageResource,
274 IXpsOMVisualCollection* visuals);
275
276 HRESULT cornerOfClamp(
277 const SkRect& tlPoints,
278 const SkColor color,
279 IXpsOMVisualCollection* visuals);
280
281 HRESULT clip(
282 IXpsOMVisual* xpsVisual,
283 const SkDraw& d);
284 HRESULT clipToPath(
285 IXpsOMVisual* xpsVisual,
286 const SkPath& clipPath,
287 XPS_FILL_RULE fillRule);
288
289 HRESULT drawInverseWindingPath(
290 const SkDraw& d,
291 const SkPath& devicePath,
292 IXpsOMPath* xpsPath);
293
294 HRESULT shadePath(
295 IXpsOMPath* shadedPath,
296 const SkPaint& shaderPaint,
297 const SkMatrix& matrix,
298 BOOL* fill, BOOL* stroke);
299
300 void convertToPpm(
301 const SkMaskFilter* filter,
302 SkMatrix* matrix,
303 SkVector* ppuScale,
304 const SkIRect& clip, SkIRect* clipIRect);
305
306 HRESULT applyMask(
307 const SkDraw& d,
308 const SkMask& mask,
309 const SkVector& ppuScale,
310 IXpsOMPath* shadedPath);
311
312 // override from SkDevice
313 virtual SkDevice* onCreateCompatibleDevice(
314 SkBitmap::Config config,
315 int width, int height,
316 bool isOpaque,
317 Usage usage) SK_OVERRIDE;
318
319 // Disable the default copy and assign implementation.
320 SkXPSDevice(const SkXPSDevice&);
321 void operator=(const SkXPSDevice&);
322
323 typedef SkDevice INHERITED;
324};
325
326#endif