blob: 877eb3b7183baa2b23d9cae02cb6532bd20dcbfa [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 Reed27d07f02017-03-04 21:47:47 +000019#include "SkBitSet.h"
Mike Reeda1361362017-03-07 09:37:29 -050020#include "SkBitmapDevice.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000021#include "SkCanvas.h"
Mike Reeda1361362017-03-07 09:37:29 -050022#include "SkClipStackDevice.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000023#include "SkColor.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000024#include "SkPaint.h"
25#include "SkPath.h"
26#include "SkPoint.h"
27#include "SkShader.h"
28#include "SkSize.h"
29#include "SkTArray.h"
30#include "SkTScopedComPtr.h"
31#include "SkTypeface.h"
32
Ben Wagnerda5a1b82014-08-22 15:07:06 -040033//#define SK_XPS_USE_DETERMINISTIC_IDS
34
bungeman@google.comb29c8832011-10-10 13:19:10 +000035/** \class SkXPSDevice
36
37 The drawing context for the XPS backend.
38*/
Mike Reeda1361362017-03-07 09:37:29 -050039class SkXPSDevice : public SkClipStackDevice {
bungeman@google.comb29c8832011-10-10 13:19:10 +000040public:
Hal Canaryb39b09e2017-02-01 17:04:44 -050041 SK_API SkXPSDevice(SkISize);
Chris Dalton1ef80942017-12-04 12:01:30 -070042 SK_API ~SkXPSDevice() override;
bungeman@google.comb29c8832011-10-10 13:19:10 +000043
Hal Canaryabc88d22017-02-06 09:26:49 -050044 bool beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory*);
bungeman@google.comb29c8832011-10-10 13:19:10 +000045 /**
46 @param unitsPerMeter converts geometry units into physical units.
47 @param pixelsPerMeter resolution to use when geometry must be rasterized.
48 @param trimSize final page size in physical units.
49 The top left of the trim is the origin of physical space.
50 @param mediaBox The size of the physical media in physical units.
51 The top and left must be less than zero.
52 The bottom and right must be greater than the trimSize.
53 The default is to coincide with the trimSize.
54 @param bleedBox The size of the bleed box in physical units.
55 Must be contained within the mediaBox.
56 The default is to coincide with the mediaBox.
57 @param artBox The size of the content box in physical units.
58 Must be contained within the trimSize.
59 The default is to coincide with the trimSize.
60 @param cropBox The size of the recommended view port in physical units.
61 Must be contained within the mediaBox.
62 The default is to coincide with the mediaBox.
63 */
Hal Canaryb39b09e2017-02-01 17:04:44 -050064 bool beginSheet(
bungeman@google.comb29c8832011-10-10 13:19:10 +000065 const SkVector& unitsPerMeter,
66 const SkVector& pixelsPerMeter,
67 const SkSize& trimSize,
68 const SkRect* mediaBox = NULL,
69 const SkRect* bleedBox = NULL,
70 const SkRect* artBox = NULL,
71 const SkRect* cropBox = NULL);
72
Hal Canaryb39b09e2017-02-01 17:04:44 -050073 bool endSheet();
74 bool endPortfolio();
bungeman@google.comb29c8832011-10-10 13:19:10 +000075
bungeman@google.comb29c8832011-10-10 13:19:10 +000076protected:
Mike Reeda1361362017-03-07 09:37:29 -050077 void drawPaint(const SkPaint& paint) override;
78 void drawPoints(SkCanvas::PointMode mode, size_t count,
79 const SkPoint[], const SkPaint& paint) override;
80 void drawRect(const SkRect& r,
81 const SkPaint& paint) override;
82 void drawOval(const SkRect& oval,
83 const SkPaint& paint) override;
84 void drawRRect(const SkRRect& rr,
85 const SkPaint& paint) override;
86 void drawPath(const SkPath& path,
87 const SkPaint& paint,
Mike Reeda1361362017-03-07 09:37:29 -050088 bool pathIsMutable = false) override;
89 void drawBitmap(const SkBitmap& bitmap,
Hal Canaryb9642382017-06-27 09:58:56 -040090 SkScalar x,
91 SkScalar y,
92 const SkPaint& paint) override;
Mike Reeda1361362017-03-07 09:37:29 -050093 void drawSprite(const SkBitmap& bitmap,
94 int x, int y, const SkPaint& paint) override;
95 void drawBitmapRect(const SkBitmap&,
96 const SkRect* srcOrNull, const SkRect& dst,
97 const SkPaint& paint,
Hal Canaryb39b09e2017-02-01 17:04:44 -050098 SkCanvas::SrcRectConstraint) override;
Mike Reeda1361362017-03-07 09:37:29 -050099 void drawPosText(const void* text, size_t len,
100 const SkScalar pos[], int scalarsPerPos,
101 const SkPoint& offset, const SkPaint& paint) override;
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400102 void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
Ruiqi Maof5101492018-06-29 14:32:21 -0400103 const SkPaint&) override;
Mike Reeda1361362017-03-07 09:37:29 -0500104 void drawDevice(SkBaseDevice*, int x, int y,
105 const SkPaint&) override;
Hal Canaryb39b09e2017-02-01 17:04:44 -0500106
bungeman@google.comb29c8832011-10-10 13:19:10 +0000107private:
108 class TypefaceUse : ::SkNoncopyable {
109 public:
110 SkFontID typefaceId;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000111 int ttcIndex;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000112 SkStream* fontData;
113 IXpsOMFontResource* xpsFont;
114 SkBitSet* glyphsUsed;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000115
bungeman@google.comb29c8832011-10-10 13:19:10 +0000116 explicit TypefaceUse();
117 ~TypefaceUse();
118 };
halcanarya634b742016-10-13 08:44:11 -0700119 friend HRESULT subset_typeface(TypefaceUse* current);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000120
Hal Canaryabc88d22017-02-06 09:26:49 -0500121 bool createCanvasForLayer();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000122
bungeman@google.comb29c8832011-10-10 13:19:10 +0000123 SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
124 SkTScopedComPtr<IStream> fOutputStream;
125 SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter;
126
127 unsigned int fCurrentPage;
128 SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas;
129 SkSize fCurrentCanvasSize;
130 SkVector fCurrentUnitsPerMeter;
131 SkVector fCurrentPixelsPerMeter;
132
133 SkTArray<TypefaceUse, true> fTypefaces;
134
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400135 /** Creates a GUID based id and places it into buffer.
136 buffer should have space for at least GUID_ID_LEN wide characters.
137 The string will always be wchar null terminated.
138 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
139 The string may begin with a digit,
140 and so may not be suitable as a bare resource key.
141 */
142 HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-');
143#ifdef SK_XPS_USE_DETERMINISTIC_IDS
144 decltype(GUID::Data1) fNextId = 0;
145#endif
146
bungeman@google.comb29c8832011-10-10 13:19:10 +0000147 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
148
149 HRESULT createXpsPage(
150 const XPS_SIZE& pageSize,
151 IXpsOMPage** page);
152
153 HRESULT createXpsThumbnail(
154 IXpsOMPage* page, const unsigned int pageNumber,
155 IXpsOMImageResource** image);
156
157 void internalDrawRect(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000158 const SkRect& r,
159 bool transformRect,
160 const SkPaint& paint);
161
162 HRESULT createXpsBrush(
163 const SkPaint& skPaint,
164 IXpsOMBrush** xpsBrush,
165 const SkMatrix* parentTransform = NULL);
166
167 HRESULT createXpsSolidColorBrush(
168 const SkColor skColor, const SkAlpha alpha,
169 IXpsOMBrush** xpsBrush);
170
171 HRESULT createXpsImageBrush(
172 const SkBitmap& bitmap,
173 const SkMatrix& localMatrix,
174 const SkShader::TileMode (&xy)[2],
175 const SkAlpha alpha,
176 IXpsOMTileBrush** xpsBrush);
177
178 HRESULT createXpsLinearGradient(
179 SkShader::GradientInfo info,
180 const SkAlpha alpha,
181 const SkMatrix& localMatrix,
182 IXpsOMMatrixTransform* xpsMatrixToUse,
183 IXpsOMBrush** xpsBrush);
184
185 HRESULT createXpsRadialGradient(
186 SkShader::GradientInfo info,
187 const SkAlpha alpha,
188 const SkMatrix& localMatrix,
189 IXpsOMMatrixTransform* xpsMatrixToUse,
190 IXpsOMBrush** xpsBrush);
191
192 HRESULT createXpsGradientStop(
193 const SkColor skColor,
194 const SkScalar offset,
195 IXpsOMGradientStop** xpsGradStop);
196
197 HRESULT createXpsTransform(
198 const SkMatrix& matrix,
199 IXpsOMMatrixTransform ** xpsTransform);
200
201 HRESULT createXpsRect(
202 const SkRect& rect,
203 BOOL stroke, BOOL fill,
204 IXpsOMGeometryFigure** xpsRect);
205
206 HRESULT createXpsQuad(
207 const SkPoint (&points)[4],
208 BOOL stroke, BOOL fill,
209 IXpsOMGeometryFigure** xpsQuad);
210
211 HRESULT CreateTypefaceUse(
212 const SkPaint& paint,
213 TypefaceUse** fontResource);
214
215 HRESULT AddGlyphs(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000216 IXpsOMObjectFactory* xpsFactory,
217 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000218 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000219 LPCWSTR text,
220 XPS_GLYPH_INDEX* xpsGlyphs,
221 UINT32 xpsGlyphsLen,
222 XPS_POINT *origin,
223 FLOAT fontSize,
224 XPS_STYLE_SIMULATION sims,
225 const SkMatrix& transform,
226 const SkPaint& paint);
227
228 HRESULT addXpsPathGeometry(
229 IXpsOMGeometryFigureCollection* figures,
230 BOOL stroke, BOOL fill, const SkPath& path);
231
232 HRESULT createPath(
233 IXpsOMGeometryFigure* figure,
234 IXpsOMVisualCollection* visuals,
235 IXpsOMPath** path);
236
237 HRESULT sideOfClamp(
238 const SkRect& leftPoints, const XPS_RECT& left,
239 IXpsOMImageResource* imageResource,
240 IXpsOMVisualCollection* visuals);
241
242 HRESULT cornerOfClamp(
243 const SkRect& tlPoints,
244 const SkColor color,
245 IXpsOMVisualCollection* visuals);
246
Mike Reeda1361362017-03-07 09:37:29 -0500247 HRESULT clip(IXpsOMVisual* xpsVisual);
248
bungeman@google.comb29c8832011-10-10 13:19:10 +0000249 HRESULT clipToPath(
250 IXpsOMVisual* xpsVisual,
251 const SkPath& clipPath,
252 XPS_FILL_RULE fillRule);
253
254 HRESULT drawInverseWindingPath(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000255 const SkPath& devicePath,
256 IXpsOMPath* xpsPath);
257
258 HRESULT shadePath(
259 IXpsOMPath* shadedPath,
260 const SkPaint& shaderPaint,
261 const SkMatrix& matrix,
262 BOOL* fill, BOOL* stroke);
263
264 void convertToPpm(
265 const SkMaskFilter* filter,
266 SkMatrix* matrix,
267 SkVector* ppuScale,
268 const SkIRect& clip, SkIRect* clipIRect);
269
270 HRESULT applyMask(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000271 const SkMask& mask,
272 const SkVector& ppuScale,
273 IXpsOMPath* shadedPath);
274
mtklein36352bf2015-03-25 18:17:31 -0700275 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000276
277 // Disable the default copy and assign implementation.
278 SkXPSDevice(const SkXPSDevice&);
279 void operator=(const SkXPSDevice&);
280
Mike Reeda1361362017-03-07 09:37:29 -0500281 typedef SkClipStackDevice INHERITED;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000282};
283
halcanary0cbe7ee2015-12-01 09:02:49 -0800284#endif // SK_BUILD_FOR_WIN
285#endif // SkXPSDevice_DEFINED