blob: 57f314851e5c08a582ff6a8df1b6b3c840161b48 [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"
halcanary47ef4d52015-03-03 09:13:09 -080012
halcanary0cbe7ee2015-12-01 09:02:49 -080013#ifdef SK_BUILD_FOR_WIN
14
bungeman@google.comb29c8832011-10-10 13:19:10 +000015#include <ObjBase.h>
16#include <XpsObjectModel.h>
17
18#include "SkAutoCoInitialize.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000019#include "SkBitmapDevice.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000020#include "SkBitSet.h"
21#include "SkCanvas.h"
22#include "SkColor.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000023#include "SkPaint.h"
24#include "SkPath.h"
25#include "SkPoint.h"
26#include "SkShader.h"
27#include "SkSize.h"
28#include "SkTArray.h"
29#include "SkTScopedComPtr.h"
30#include "SkTypeface.h"
31
Ben Wagnerda5a1b82014-08-22 15:07:06 -040032//#define SK_XPS_USE_DETERMINISTIC_IDS
33
bungeman@google.comb29c8832011-10-10 13:19:10 +000034/** \class SkXPSDevice
35
36 The drawing context for the XPS backend.
37*/
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000038class SkXPSDevice : public SkBitmapDevice {
bungeman@google.comb29c8832011-10-10 13:19:10 +000039public:
40 SK_API SkXPSDevice();
41 SK_API virtual ~SkXPSDevice();
42
43 virtual bool beginPortfolio(SkWStream* outputStream);
44 /**
45 @param unitsPerMeter converts geometry units into physical units.
46 @param pixelsPerMeter resolution to use when geometry must be rasterized.
47 @param trimSize final page size in physical units.
48 The top left of the trim is the origin of physical space.
49 @param mediaBox The size of the physical media in physical units.
50 The top and left must be less than zero.
51 The bottom and right must be greater than the trimSize.
52 The default is to coincide with the trimSize.
53 @param bleedBox The size of the bleed box in physical units.
54 Must be contained within the mediaBox.
55 The default is to coincide with the mediaBox.
56 @param artBox The size of the content box in physical units.
57 Must be contained within the trimSize.
58 The default is to coincide with the trimSize.
59 @param cropBox The size of the recommended view port in physical units.
60 Must be contained within the mediaBox.
61 The default is to coincide with the mediaBox.
62 */
63 virtual bool beginSheet(
64 const SkVector& unitsPerMeter,
65 const SkVector& pixelsPerMeter,
66 const SkSize& trimSize,
67 const SkRect* mediaBox = NULL,
68 const SkRect* bleedBox = NULL,
69 const SkRect* artBox = NULL,
70 const SkRect* cropBox = NULL);
71
72 virtual bool endSheet();
73 virtual bool endPortfolio();
74
bungeman@google.comb29c8832011-10-10 13:19:10 +000075protected:
mtklein36352bf2015-03-25 18:17:31 -070076 void drawPaint(const SkDraw&, const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +000077
78 virtual void drawPoints(
79 const SkDraw&,
80 SkCanvas::PointMode mode,
81 size_t count, const SkPoint[],
mtklein36352bf2015-03-25 18:17:31 -070082 const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +000083
84 virtual void drawRect(
85 const SkDraw&,
86 const SkRect& r,
mtklein36352bf2015-03-25 18:17:31 -070087 const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +000088
scroggo@google.comcac8d012013-11-12 17:10:02 +000089 virtual void drawRRect(
90 const SkDraw&,
91 const SkRRect&,
mtklein36352bf2015-03-25 18:17:31 -070092 const SkPaint& paint) override;
scroggo@google.comcac8d012013-11-12 17:10:02 +000093
bungeman@google.comb29c8832011-10-10 13:19:10 +000094 virtual void drawPath(
95 const SkDraw&,
96 const SkPath& platonicPath,
97 const SkPaint& paint,
98 const SkMatrix* prePathMatrix,
mtklein36352bf2015-03-25 18:17:31 -070099 bool pathIsMutable) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000100
101 virtual void drawBitmap(
102 const SkDraw&,
103 const SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000104 const SkMatrix& matrix,
mtklein36352bf2015-03-25 18:17:31 -0700105 const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000106
107 virtual void drawSprite(
108 const SkDraw&,
109 const SkBitmap& bitmap,
110 int x, int y,
mtklein36352bf2015-03-25 18:17:31 -0700111 const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000112
113 virtual void drawText(
114 const SkDraw&,
115 const void* text, size_t len,
116 SkScalar x, SkScalar y,
mtklein36352bf2015-03-25 18:17:31 -0700117 const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000118
119 virtual void drawPosText(
120 const SkDraw&,
121 const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -0700122 const SkScalar pos[], int scalarsPerPos,
mtklein36352bf2015-03-25 18:17:31 -0700123 const SkPoint& offset, const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000124
bungeman@google.comb29c8832011-10-10 13:19:10 +0000125 virtual void drawVertices(
126 const SkDraw&,
127 SkCanvas::VertexMode,
128 int vertexCount, const SkPoint verts[],
129 const SkPoint texs[], const SkColor colors[],
130 SkXfermode* xmode,
131 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700132 const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000133
134 virtual void drawDevice(
135 const SkDraw&,
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000136 SkBaseDevice* device,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000137 int x, int y,
mtklein36352bf2015-03-25 18:17:31 -0700138 const SkPaint& paint) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000139
bungeman@google.comb29c8832011-10-10 13:19:10 +0000140private:
141 class TypefaceUse : ::SkNoncopyable {
142 public:
143 SkFontID typefaceId;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000144 int ttcIndex;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000145 SkStream* fontData;
146 IXpsOMFontResource* xpsFont;
147 SkBitSet* glyphsUsed;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000148
bungeman@google.comb29c8832011-10-10 13:19:10 +0000149 explicit TypefaceUse();
150 ~TypefaceUse();
151 };
152 friend static HRESULT subset_typeface(TypefaceUse* current);
153
154 SkXPSDevice(IXpsOMObjectFactory* xpsFactory);
155
156 SkAutoCoInitialize fAutoCo;
157 SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
158 SkTScopedComPtr<IStream> fOutputStream;
159 SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter;
160
161 unsigned int fCurrentPage;
162 SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas;
163 SkSize fCurrentCanvasSize;
164 SkVector fCurrentUnitsPerMeter;
165 SkVector fCurrentPixelsPerMeter;
166
167 SkTArray<TypefaceUse, true> fTypefaces;
168
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400169 /** Creates a GUID based id and places it into buffer.
170 buffer should have space for at least GUID_ID_LEN wide characters.
171 The string will always be wchar null terminated.
172 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
173 The string may begin with a digit,
174 and so may not be suitable as a bare resource key.
175 */
176 HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-');
177#ifdef SK_XPS_USE_DETERMINISTIC_IDS
178 decltype(GUID::Data1) fNextId = 0;
179#endif
180
bungeman@google.comb29c8832011-10-10 13:19:10 +0000181 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
182
183 HRESULT createXpsPage(
184 const XPS_SIZE& pageSize,
185 IXpsOMPage** page);
186
187 HRESULT createXpsThumbnail(
188 IXpsOMPage* page, const unsigned int pageNumber,
189 IXpsOMImageResource** image);
190
191 void internalDrawRect(
192 const SkDraw&,
193 const SkRect& r,
194 bool transformRect,
195 const SkPaint& paint);
196
197 HRESULT createXpsBrush(
198 const SkPaint& skPaint,
199 IXpsOMBrush** xpsBrush,
200 const SkMatrix* parentTransform = NULL);
201
202 HRESULT createXpsSolidColorBrush(
203 const SkColor skColor, const SkAlpha alpha,
204 IXpsOMBrush** xpsBrush);
205
206 HRESULT createXpsImageBrush(
207 const SkBitmap& bitmap,
208 const SkMatrix& localMatrix,
209 const SkShader::TileMode (&xy)[2],
210 const SkAlpha alpha,
211 IXpsOMTileBrush** xpsBrush);
212
213 HRESULT createXpsLinearGradient(
214 SkShader::GradientInfo info,
215 const SkAlpha alpha,
216 const SkMatrix& localMatrix,
217 IXpsOMMatrixTransform* xpsMatrixToUse,
218 IXpsOMBrush** xpsBrush);
219
220 HRESULT createXpsRadialGradient(
221 SkShader::GradientInfo info,
222 const SkAlpha alpha,
223 const SkMatrix& localMatrix,
224 IXpsOMMatrixTransform* xpsMatrixToUse,
225 IXpsOMBrush** xpsBrush);
226
227 HRESULT createXpsGradientStop(
228 const SkColor skColor,
229 const SkScalar offset,
230 IXpsOMGradientStop** xpsGradStop);
231
232 HRESULT createXpsTransform(
233 const SkMatrix& matrix,
234 IXpsOMMatrixTransform ** xpsTransform);
235
236 HRESULT createXpsRect(
237 const SkRect& rect,
238 BOOL stroke, BOOL fill,
239 IXpsOMGeometryFigure** xpsRect);
240
241 HRESULT createXpsQuad(
242 const SkPoint (&points)[4],
243 BOOL stroke, BOOL fill,
244 IXpsOMGeometryFigure** xpsQuad);
245
246 HRESULT CreateTypefaceUse(
247 const SkPaint& paint,
248 TypefaceUse** fontResource);
249
250 HRESULT AddGlyphs(
251 const SkDraw& d,
252 IXpsOMObjectFactory* xpsFactory,
253 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000254 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000255 LPCWSTR text,
256 XPS_GLYPH_INDEX* xpsGlyphs,
257 UINT32 xpsGlyphsLen,
258 XPS_POINT *origin,
259 FLOAT fontSize,
260 XPS_STYLE_SIMULATION sims,
261 const SkMatrix& transform,
262 const SkPaint& paint);
263
264 HRESULT addXpsPathGeometry(
265 IXpsOMGeometryFigureCollection* figures,
266 BOOL stroke, BOOL fill, const SkPath& path);
267
268 HRESULT createPath(
269 IXpsOMGeometryFigure* figure,
270 IXpsOMVisualCollection* visuals,
271 IXpsOMPath** path);
272
273 HRESULT sideOfClamp(
274 const SkRect& leftPoints, const XPS_RECT& left,
275 IXpsOMImageResource* imageResource,
276 IXpsOMVisualCollection* visuals);
277
278 HRESULT cornerOfClamp(
279 const SkRect& tlPoints,
280 const SkColor color,
281 IXpsOMVisualCollection* visuals);
282
283 HRESULT clip(
284 IXpsOMVisual* xpsVisual,
285 const SkDraw& d);
286 HRESULT clipToPath(
287 IXpsOMVisual* xpsVisual,
288 const SkPath& clipPath,
289 XPS_FILL_RULE fillRule);
290
291 HRESULT drawInverseWindingPath(
292 const SkDraw& d,
293 const SkPath& devicePath,
294 IXpsOMPath* xpsPath);
295
296 HRESULT shadePath(
297 IXpsOMPath* shadedPath,
298 const SkPaint& shaderPaint,
299 const SkMatrix& matrix,
300 BOOL* fill, BOOL* stroke);
301
302 void convertToPpm(
303 const SkMaskFilter* filter,
304 SkMatrix* matrix,
305 SkVector* ppuScale,
306 const SkIRect& clip, SkIRect* clipIRect);
307
308 HRESULT applyMask(
309 const SkDraw& d,
310 const SkMask& mask,
311 const SkVector& ppuScale,
312 IXpsOMPath* shadedPath);
313
mtklein36352bf2015-03-25 18:17:31 -0700314 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000315
316 // Disable the default copy and assign implementation.
317 SkXPSDevice(const SkXPSDevice&);
318 void operator=(const SkXPSDevice&);
319
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000320 typedef SkBitmapDevice INHERITED;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000321};
322
halcanary0cbe7ee2015-12-01 09:02:49 -0800323#endif // SK_BUILD_FOR_WIN
324#endif // SkXPSDevice_DEFINED