blob: 0ad993950902288136430ccd66937ad30eb2ea22 [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 Reed025e2442017-03-04 19:43:23 +000019#include "SkBitmapDevice.h"
Mike Reed27d07f02017-03-04 21:47:47 +000020#include "SkBitSet.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000021#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*/
Mike Reed27d07f02017-03-04 21:47:47 +000038class 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 Canaryabc88d22017-02-06 09:26:49 -050043 bool beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory*);
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:
Mike Reed27d07f02017-03-04 21:47:47 +000076 void drawPaint(const SkDraw&, const SkPaint& paint) override;
77
78 void drawPoints(
79 const SkDraw&,
80 SkCanvas::PointMode mode,
81 size_t count, const SkPoint[],
82 const SkPaint& paint) override;
83
84 void drawRect(
85 const SkDraw&,
86 const SkRect& r,
87 const SkPaint& paint) override;
88
89 void drawRRect(
90 const SkDraw&,
91 const SkRRect&,
92 const SkPaint& paint) override;
93
94 void drawPath(
95 const SkDraw&,
96 const SkPath& platonicPath,
97 const SkPaint& paint,
98 const SkMatrix* prePathMatrix,
99 bool pathIsMutable) override;
100
101 void drawBitmap(
102 const SkDraw&,
103 const SkBitmap& bitmap,
104 const SkMatrix& matrix,
105 const SkPaint& paint) override;
106
107 void drawSprite(
108 const SkDraw&,
109 const SkBitmap& bitmap,
110 int x, int y,
111 const SkPaint& paint) override;
112
113 void drawText(
114 const SkDraw&,
115 const void* text, size_t len,
116 SkScalar x, SkScalar y,
117 const SkPaint& paint) override;
118
119 void drawPosText(
120 const SkDraw&,
121 const void* text, size_t len,
122 const SkScalar pos[], int scalarsPerPos,
123 const SkPoint& offset, const SkPaint& paint) override;
124
125 void drawVertices(
126 const SkDraw&,
127 SkCanvas::VertexMode,
128 int vertexCount, const SkPoint verts[],
129 const SkPoint texs[], const SkColor colors[],
130 SkBlendMode,
131 const uint16_t indices[], int indexCount,
132 const SkPaint& paint) override;
133
134 void drawDevice(
135 const SkDraw&,
136 SkBaseDevice* device,
137 int x, int y,
138 const SkPaint& paint) override;
139
140 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&,
Hal Canaryb39b09e2017-02-01 17:04:44 -0500147 SkCanvas::SrcRectConstraint) override;
Mike Reed27d07f02017-03-04 21:47:47 +0000148
Hal Canaryb39b09e2017-02-01 17:04:44 -0500149
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 Canaryabc88d22017-02-06 09:26:49 -0500164 bool createCanvasForLayer();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000165
bungeman@google.comb29c8832011-10-10 13:19:10 +0000166 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
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400178 /** Creates a GUID based id and places it into buffer.
179 buffer should have space for at least GUID_ID_LEN wide characters.
180 The string will always be wchar null terminated.
181 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
182 The string may begin with a digit,
183 and so may not be suitable as a bare resource key.
184 */
185 HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-');
186#ifdef SK_XPS_USE_DETERMINISTIC_IDS
187 decltype(GUID::Data1) fNextId = 0;
188#endif
189
bungeman@google.comb29c8832011-10-10 13:19:10 +0000190 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
191
192 HRESULT createXpsPage(
193 const XPS_SIZE& pageSize,
194 IXpsOMPage** page);
195
196 HRESULT createXpsThumbnail(
197 IXpsOMPage* page, const unsigned int pageNumber,
198 IXpsOMImageResource** image);
199
200 void internalDrawRect(
Mike Reed27d07f02017-03-04 21:47:47 +0000201 const SkDraw&,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000202 const SkRect& r,
203 bool transformRect,
204 const SkPaint& paint);
205
206 HRESULT createXpsBrush(
207 const SkPaint& skPaint,
208 IXpsOMBrush** xpsBrush,
209 const SkMatrix* parentTransform = NULL);
210
211 HRESULT createXpsSolidColorBrush(
212 const SkColor skColor, const SkAlpha alpha,
213 IXpsOMBrush** xpsBrush);
214
215 HRESULT createXpsImageBrush(
216 const SkBitmap& bitmap,
217 const SkMatrix& localMatrix,
218 const SkShader::TileMode (&xy)[2],
219 const SkAlpha alpha,
220 IXpsOMTileBrush** xpsBrush);
221
222 HRESULT createXpsLinearGradient(
223 SkShader::GradientInfo info,
224 const SkAlpha alpha,
225 const SkMatrix& localMatrix,
226 IXpsOMMatrixTransform* xpsMatrixToUse,
227 IXpsOMBrush** xpsBrush);
228
229 HRESULT createXpsRadialGradient(
230 SkShader::GradientInfo info,
231 const SkAlpha alpha,
232 const SkMatrix& localMatrix,
233 IXpsOMMatrixTransform* xpsMatrixToUse,
234 IXpsOMBrush** xpsBrush);
235
236 HRESULT createXpsGradientStop(
237 const SkColor skColor,
238 const SkScalar offset,
239 IXpsOMGradientStop** xpsGradStop);
240
241 HRESULT createXpsTransform(
242 const SkMatrix& matrix,
243 IXpsOMMatrixTransform ** xpsTransform);
244
245 HRESULT createXpsRect(
246 const SkRect& rect,
247 BOOL stroke, BOOL fill,
248 IXpsOMGeometryFigure** xpsRect);
249
250 HRESULT createXpsQuad(
251 const SkPoint (&points)[4],
252 BOOL stroke, BOOL fill,
253 IXpsOMGeometryFigure** xpsQuad);
254
255 HRESULT CreateTypefaceUse(
256 const SkPaint& paint,
257 TypefaceUse** fontResource);
258
259 HRESULT AddGlyphs(
Mike Reed27d07f02017-03-04 21:47:47 +0000260 const SkDraw& d,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000261 IXpsOMObjectFactory* xpsFactory,
262 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000263 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000264 LPCWSTR text,
265 XPS_GLYPH_INDEX* xpsGlyphs,
266 UINT32 xpsGlyphsLen,
267 XPS_POINT *origin,
268 FLOAT fontSize,
269 XPS_STYLE_SIMULATION sims,
270 const SkMatrix& transform,
271 const SkPaint& paint);
272
273 HRESULT addXpsPathGeometry(
274 IXpsOMGeometryFigureCollection* figures,
275 BOOL stroke, BOOL fill, const SkPath& path);
276
277 HRESULT createPath(
278 IXpsOMGeometryFigure* figure,
279 IXpsOMVisualCollection* visuals,
280 IXpsOMPath** path);
281
282 HRESULT sideOfClamp(
283 const SkRect& leftPoints, const XPS_RECT& left,
284 IXpsOMImageResource* imageResource,
285 IXpsOMVisualCollection* visuals);
286
287 HRESULT cornerOfClamp(
288 const SkRect& tlPoints,
289 const SkColor color,
290 IXpsOMVisualCollection* visuals);
291
Mike Reed27d07f02017-03-04 21:47:47 +0000292 HRESULT clip(
293 IXpsOMVisual* xpsVisual,
294 const SkDraw& d);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000295 HRESULT clipToPath(
296 IXpsOMVisual* xpsVisual,
297 const SkPath& clipPath,
298 XPS_FILL_RULE fillRule);
299
300 HRESULT drawInverseWindingPath(
Mike Reed27d07f02017-03-04 21:47:47 +0000301 const SkDraw& d,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000302 const SkPath& devicePath,
303 IXpsOMPath* xpsPath);
304
305 HRESULT shadePath(
306 IXpsOMPath* shadedPath,
307 const SkPaint& shaderPaint,
308 const SkMatrix& matrix,
309 BOOL* fill, BOOL* stroke);
310
311 void convertToPpm(
312 const SkMaskFilter* filter,
313 SkMatrix* matrix,
314 SkVector* ppuScale,
315 const SkIRect& clip, SkIRect* clipIRect);
316
317 HRESULT applyMask(
Mike Reed27d07f02017-03-04 21:47:47 +0000318 const SkDraw& d,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000319 const SkMask& mask,
320 const SkVector& ppuScale,
321 IXpsOMPath* shadedPath);
322
mtklein36352bf2015-03-25 18:17:31 -0700323 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000324
325 // Disable the default copy and assign implementation.
326 SkXPSDevice(const SkXPSDevice&);
327 void operator=(const SkXPSDevice&);
328
Mike Reed27d07f02017-03-04 21:47:47 +0000329 typedef SkBaseDevice INHERITED;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000330};
331
halcanary0cbe7ee2015-12-01 09:02:49 -0800332#endif // SK_BUILD_FOR_WIN
333#endif // SkXPSDevice_DEFINED