blob: db8436d5e8706fb37ad6423fab9efdc539d1136d [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"
Mike Reed986480a2017-01-13 22:43:16 +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*/
Hal Canaryb39b09e2017-02-01 17:04:44 -050038class SkXPSDevice : public SkBaseDevice {
bungeman@google.comb29c8832011-10-10 13:19:10 +000039public:
Hal Canaryb39b09e2017-02-01 17:04:44 -050040 SK_API SkXPSDevice(SkISize);
bungeman@google.comb29c8832011-10-10 13:19:10 +000041 SK_API virtual ~SkXPSDevice();
42
Hal Canaryb39b09e2017-02-01 17:04:44 -050043 bool beginPortfolio(SkWStream* outputStream);
bungeman@google.comb29c8832011-10-10 13:19:10 +000044 /**
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 */
Hal Canaryb39b09e2017-02-01 17:04:44 -050063 bool beginSheet(
bungeman@google.comb29c8832011-10-10 13:19:10 +000064 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
Hal Canaryb39b09e2017-02-01 17:04:44 -050072 bool endSheet();
73 bool endPortfolio();
bungeman@google.comb29c8832011-10-10 13:19:10 +000074
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
Hal Canaryb39b09e2017-02-01 17:04:44 -050078 void drawPoints(
bungeman@google.comb29c8832011-10-10 13:19:10 +000079 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
Hal Canaryb39b09e2017-02-01 17:04:44 -050084 void drawRect(
bungeman@google.comb29c8832011-10-10 13:19:10 +000085 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
Hal Canaryb39b09e2017-02-01 17:04:44 -050089 void drawRRect(
scroggo@google.comcac8d012013-11-12 17:10:02 +000090 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
Hal Canaryb39b09e2017-02-01 17:04:44 -050094 void drawPath(
bungeman@google.comb29c8832011-10-10 13:19:10 +000095 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
Hal Canaryb39b09e2017-02-01 17:04:44 -0500101 void drawBitmap(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000102 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
Hal Canaryb39b09e2017-02-01 17:04:44 -0500107 void drawSprite(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000108 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
Hal Canaryb39b09e2017-02-01 17:04:44 -0500113 void drawText(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000114 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
Hal Canaryb39b09e2017-02-01 17:04:44 -0500119 void drawPosText(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000120 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
Hal Canaryb39b09e2017-02-01 17:04:44 -0500125 void drawVertices(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000126 const SkDraw&,
127 SkCanvas::VertexMode,
128 int vertexCount, const SkPoint verts[],
129 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -0400130 SkBlendMode,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000131 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
Hal Canaryb39b09e2017-02-01 17:04:44 -0500134 void drawDevice(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000135 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
Hal Canaryb39b09e2017-02-01 17:04:44 -0500140 void drawOval(const SkDraw&, const SkRect&, const SkPaint&) override;
141
142 void drawBitmapRect(const SkDraw&,
143 const SkBitmap&,
144 const SkRect*,
145 const SkRect&,
146 const SkPaint&,
147 SkCanvas::SrcRectConstraint) override;
148
149
bungeman@google.comb29c8832011-10-10 13:19:10 +0000150private:
151 class TypefaceUse : ::SkNoncopyable {
152 public:
153 SkFontID typefaceId;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000154 int ttcIndex;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000155 SkStream* fontData;
156 IXpsOMFontResource* xpsFont;
157 SkBitSet* glyphsUsed;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000158
bungeman@google.comb29c8832011-10-10 13:19:10 +0000159 explicit TypefaceUse();
160 ~TypefaceUse();
161 };
halcanarya634b742016-10-13 08:44:11 -0700162 friend HRESULT subset_typeface(TypefaceUse* current);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000163
Hal Canaryb39b09e2017-02-01 17:04:44 -0500164 SkXPSDevice(SkISize, IXpsOMObjectFactory*);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000165
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
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400179 /** Creates a GUID based id and places it into buffer.
180 buffer should have space for at least GUID_ID_LEN wide characters.
181 The string will always be wchar null terminated.
182 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
183 The string may begin with a digit,
184 and so may not be suitable as a bare resource key.
185 */
186 HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-');
187#ifdef SK_XPS_USE_DETERMINISTIC_IDS
188 decltype(GUID::Data1) fNextId = 0;
189#endif
190
bungeman@google.comb29c8832011-10-10 13:19:10 +0000191 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
192
193 HRESULT createXpsPage(
194 const XPS_SIZE& pageSize,
195 IXpsOMPage** page);
196
197 HRESULT createXpsThumbnail(
198 IXpsOMPage* page, const unsigned int pageNumber,
199 IXpsOMImageResource** image);
200
201 void internalDrawRect(
202 const SkDraw&,
203 const SkRect& r,
204 bool transformRect,
205 const SkPaint& paint);
206
207 HRESULT createXpsBrush(
208 const SkPaint& skPaint,
209 IXpsOMBrush** xpsBrush,
210 const SkMatrix* parentTransform = NULL);
211
212 HRESULT createXpsSolidColorBrush(
213 const SkColor skColor, const SkAlpha alpha,
214 IXpsOMBrush** xpsBrush);
215
216 HRESULT createXpsImageBrush(
217 const SkBitmap& bitmap,
218 const SkMatrix& localMatrix,
219 const SkShader::TileMode (&xy)[2],
220 const SkAlpha alpha,
221 IXpsOMTileBrush** xpsBrush);
222
223 HRESULT createXpsLinearGradient(
224 SkShader::GradientInfo info,
225 const SkAlpha alpha,
226 const SkMatrix& localMatrix,
227 IXpsOMMatrixTransform* xpsMatrixToUse,
228 IXpsOMBrush** xpsBrush);
229
230 HRESULT createXpsRadialGradient(
231 SkShader::GradientInfo info,
232 const SkAlpha alpha,
233 const SkMatrix& localMatrix,
234 IXpsOMMatrixTransform* xpsMatrixToUse,
235 IXpsOMBrush** xpsBrush);
236
237 HRESULT createXpsGradientStop(
238 const SkColor skColor,
239 const SkScalar offset,
240 IXpsOMGradientStop** xpsGradStop);
241
242 HRESULT createXpsTransform(
243 const SkMatrix& matrix,
244 IXpsOMMatrixTransform ** xpsTransform);
245
246 HRESULT createXpsRect(
247 const SkRect& rect,
248 BOOL stroke, BOOL fill,
249 IXpsOMGeometryFigure** xpsRect);
250
251 HRESULT createXpsQuad(
252 const SkPoint (&points)[4],
253 BOOL stroke, BOOL fill,
254 IXpsOMGeometryFigure** xpsQuad);
255
256 HRESULT CreateTypefaceUse(
257 const SkPaint& paint,
258 TypefaceUse** fontResource);
259
260 HRESULT AddGlyphs(
261 const SkDraw& d,
262 IXpsOMObjectFactory* xpsFactory,
263 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000264 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000265 LPCWSTR text,
266 XPS_GLYPH_INDEX* xpsGlyphs,
267 UINT32 xpsGlyphsLen,
268 XPS_POINT *origin,
269 FLOAT fontSize,
270 XPS_STYLE_SIMULATION sims,
271 const SkMatrix& transform,
272 const SkPaint& paint);
273
274 HRESULT addXpsPathGeometry(
275 IXpsOMGeometryFigureCollection* figures,
276 BOOL stroke, BOOL fill, const SkPath& path);
277
278 HRESULT createPath(
279 IXpsOMGeometryFigure* figure,
280 IXpsOMVisualCollection* visuals,
281 IXpsOMPath** path);
282
283 HRESULT sideOfClamp(
284 const SkRect& leftPoints, const XPS_RECT& left,
285 IXpsOMImageResource* imageResource,
286 IXpsOMVisualCollection* visuals);
287
288 HRESULT cornerOfClamp(
289 const SkRect& tlPoints,
290 const SkColor color,
291 IXpsOMVisualCollection* visuals);
292
293 HRESULT clip(
294 IXpsOMVisual* xpsVisual,
295 const SkDraw& d);
296 HRESULT clipToPath(
297 IXpsOMVisual* xpsVisual,
298 const SkPath& clipPath,
299 XPS_FILL_RULE fillRule);
300
301 HRESULT drawInverseWindingPath(
302 const SkDraw& d,
303 const SkPath& devicePath,
304 IXpsOMPath* xpsPath);
305
306 HRESULT shadePath(
307 IXpsOMPath* shadedPath,
308 const SkPaint& shaderPaint,
309 const SkMatrix& matrix,
310 BOOL* fill, BOOL* stroke);
311
312 void convertToPpm(
313 const SkMaskFilter* filter,
314 SkMatrix* matrix,
315 SkVector* ppuScale,
316 const SkIRect& clip, SkIRect* clipIRect);
317
318 HRESULT applyMask(
319 const SkDraw& d,
320 const SkMask& mask,
321 const SkVector& ppuScale,
322 IXpsOMPath* shadedPath);
323
mtklein36352bf2015-03-25 18:17:31 -0700324 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000325
326 // Disable the default copy and assign implementation.
327 SkXPSDevice(const SkXPSDevice&);
328 void operator=(const SkXPSDevice&);
329
Hal Canaryb39b09e2017-02-01 17:04:44 -0500330 typedef SkBaseDevice INHERITED;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000331};
332
halcanary0cbe7ee2015-12-01 09:02:49 -0800333#endif // SK_BUILD_FOR_WIN
334#endif // SkXPSDevice_DEFINED