blob: a03b2932cacede43bd53aa3fc4a01ddbc3d1d3b8 [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);
bungeman@google.comb29c8832011-10-10 13:19:10 +000042 SK_API virtual ~SkXPSDevice();
43
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,
88 const SkMatrix* prePathMatrix = NULL,
89 bool pathIsMutable = false) override;
90 void drawBitmap(const SkBitmap& bitmap,
91 const SkMatrix& matrix, const SkPaint& paint) override;
92 void drawSprite(const SkBitmap& bitmap,
93 int x, int y, const SkPaint& paint) override;
94 void drawBitmapRect(const SkBitmap&,
95 const SkRect* srcOrNull, const SkRect& dst,
96 const SkPaint& paint,
Hal Canaryb39b09e2017-02-01 17:04:44 -050097 SkCanvas::SrcRectConstraint) override;
Mike Reeda1361362017-03-07 09:37:29 -050098 void drawText(const void* text, size_t len,
99 SkScalar x, SkScalar y, const SkPaint& paint) override;
100 void drawPosText(const void* text, size_t len,
101 const SkScalar pos[], int scalarsPerPos,
102 const SkPoint& offset, const SkPaint& paint) override;
103 void drawVertices(SkCanvas::VertexMode, int vertexCount,
104 const SkPoint verts[], const SkPoint texs[],
105 const SkColor colors[], SkBlendMode,
106 const uint16_t indices[], int indexCount,
107 const SkPaint& paint) override;
108 void drawDevice(SkBaseDevice*, int x, int y,
109 const SkPaint&) override;
Hal Canaryb39b09e2017-02-01 17:04:44 -0500110
bungeman@google.comb29c8832011-10-10 13:19:10 +0000111private:
112 class TypefaceUse : ::SkNoncopyable {
113 public:
114 SkFontID typefaceId;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000115 int ttcIndex;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000116 SkStream* fontData;
117 IXpsOMFontResource* xpsFont;
118 SkBitSet* glyphsUsed;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000119
bungeman@google.comb29c8832011-10-10 13:19:10 +0000120 explicit TypefaceUse();
121 ~TypefaceUse();
122 };
halcanarya634b742016-10-13 08:44:11 -0700123 friend HRESULT subset_typeface(TypefaceUse* current);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000124
Hal Canaryabc88d22017-02-06 09:26:49 -0500125 bool createCanvasForLayer();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000126
bungeman@google.comb29c8832011-10-10 13:19:10 +0000127 SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
128 SkTScopedComPtr<IStream> fOutputStream;
129 SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter;
130
131 unsigned int fCurrentPage;
132 SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas;
133 SkSize fCurrentCanvasSize;
134 SkVector fCurrentUnitsPerMeter;
135 SkVector fCurrentPixelsPerMeter;
136
137 SkTArray<TypefaceUse, true> fTypefaces;
138
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400139 /** Creates a GUID based id and places it into buffer.
140 buffer should have space for at least GUID_ID_LEN wide characters.
141 The string will always be wchar null terminated.
142 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
143 The string may begin with a digit,
144 and so may not be suitable as a bare resource key.
145 */
146 HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-');
147#ifdef SK_XPS_USE_DETERMINISTIC_IDS
148 decltype(GUID::Data1) fNextId = 0;
149#endif
150
bungeman@google.comb29c8832011-10-10 13:19:10 +0000151 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
152
153 HRESULT createXpsPage(
154 const XPS_SIZE& pageSize,
155 IXpsOMPage** page);
156
157 HRESULT createXpsThumbnail(
158 IXpsOMPage* page, const unsigned int pageNumber,
159 IXpsOMImageResource** image);
160
161 void internalDrawRect(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000162 const SkRect& r,
163 bool transformRect,
164 const SkPaint& paint);
165
166 HRESULT createXpsBrush(
167 const SkPaint& skPaint,
168 IXpsOMBrush** xpsBrush,
169 const SkMatrix* parentTransform = NULL);
170
171 HRESULT createXpsSolidColorBrush(
172 const SkColor skColor, const SkAlpha alpha,
173 IXpsOMBrush** xpsBrush);
174
175 HRESULT createXpsImageBrush(
176 const SkBitmap& bitmap,
177 const SkMatrix& localMatrix,
178 const SkShader::TileMode (&xy)[2],
179 const SkAlpha alpha,
180 IXpsOMTileBrush** xpsBrush);
181
182 HRESULT createXpsLinearGradient(
183 SkShader::GradientInfo info,
184 const SkAlpha alpha,
185 const SkMatrix& localMatrix,
186 IXpsOMMatrixTransform* xpsMatrixToUse,
187 IXpsOMBrush** xpsBrush);
188
189 HRESULT createXpsRadialGradient(
190 SkShader::GradientInfo info,
191 const SkAlpha alpha,
192 const SkMatrix& localMatrix,
193 IXpsOMMatrixTransform* xpsMatrixToUse,
194 IXpsOMBrush** xpsBrush);
195
196 HRESULT createXpsGradientStop(
197 const SkColor skColor,
198 const SkScalar offset,
199 IXpsOMGradientStop** xpsGradStop);
200
201 HRESULT createXpsTransform(
202 const SkMatrix& matrix,
203 IXpsOMMatrixTransform ** xpsTransform);
204
205 HRESULT createXpsRect(
206 const SkRect& rect,
207 BOOL stroke, BOOL fill,
208 IXpsOMGeometryFigure** xpsRect);
209
210 HRESULT createXpsQuad(
211 const SkPoint (&points)[4],
212 BOOL stroke, BOOL fill,
213 IXpsOMGeometryFigure** xpsQuad);
214
215 HRESULT CreateTypefaceUse(
216 const SkPaint& paint,
217 TypefaceUse** fontResource);
218
219 HRESULT AddGlyphs(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000220 IXpsOMObjectFactory* xpsFactory,
221 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000222 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000223 LPCWSTR text,
224 XPS_GLYPH_INDEX* xpsGlyphs,
225 UINT32 xpsGlyphsLen,
226 XPS_POINT *origin,
227 FLOAT fontSize,
228 XPS_STYLE_SIMULATION sims,
229 const SkMatrix& transform,
230 const SkPaint& paint);
231
232 HRESULT addXpsPathGeometry(
233 IXpsOMGeometryFigureCollection* figures,
234 BOOL stroke, BOOL fill, const SkPath& path);
235
236 HRESULT createPath(
237 IXpsOMGeometryFigure* figure,
238 IXpsOMVisualCollection* visuals,
239 IXpsOMPath** path);
240
241 HRESULT sideOfClamp(
242 const SkRect& leftPoints, const XPS_RECT& left,
243 IXpsOMImageResource* imageResource,
244 IXpsOMVisualCollection* visuals);
245
246 HRESULT cornerOfClamp(
247 const SkRect& tlPoints,
248 const SkColor color,
249 IXpsOMVisualCollection* visuals);
250
Mike Reeda1361362017-03-07 09:37:29 -0500251 HRESULT clip(IXpsOMVisual* xpsVisual);
252
bungeman@google.comb29c8832011-10-10 13:19:10 +0000253 HRESULT clipToPath(
254 IXpsOMVisual* xpsVisual,
255 const SkPath& clipPath,
256 XPS_FILL_RULE fillRule);
257
258 HRESULT drawInverseWindingPath(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000259 const SkPath& devicePath,
260 IXpsOMPath* xpsPath);
261
262 HRESULT shadePath(
263 IXpsOMPath* shadedPath,
264 const SkPaint& shaderPaint,
265 const SkMatrix& matrix,
266 BOOL* fill, BOOL* stroke);
267
268 void convertToPpm(
269 const SkMaskFilter* filter,
270 SkMatrix* matrix,
271 SkVector* ppuScale,
272 const SkIRect& clip, SkIRect* clipIRect);
273
274 HRESULT applyMask(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000275 const SkMask& mask,
276 const SkVector& ppuScale,
277 IXpsOMPath* shadedPath);
278
mtklein36352bf2015-03-25 18:17:31 -0700279 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000280
281 // Disable the default copy and assign implementation.
282 SkXPSDevice(const SkXPSDevice&);
283 void operator=(const SkXPSDevice&);
284
Mike Reeda1361362017-03-07 09:37:29 -0500285 typedef SkClipStackDevice INHERITED;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000286};
287
halcanary0cbe7ee2015-12-01 09:02:49 -0800288#endif // SK_BUILD_FOR_WIN
289#endif // SkXPSDevice_DEFINED