Debian Codegraph @

src/xps/SkXPSDevice.h

raw
1 /*
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 "include/core/SkTypes.h"
12
13 #ifdef SK_BUILD_FOR_WIN
14
15 #include <ObjBase.h>
16 #include <XpsObjectModel.h>
17
18 #include "include/core/SkCPURecorder.h"
19 #include "include/core/SkCanvas.h"
20 #include "include/core/SkColor.h"
21 #include "include/core/SkPaint.h"
22 #include "include/core/SkPath.h"
23 #include "include/core/SkPoint.h"
24 #include "include/core/SkShader.h"
25 #include "include/core/SkSize.h"
26 #include "include/core/SkTypeface.h"
27 #include "include/docs/SkXPSDocument.h"
28 #include "include/private/base/SkTArray.h"
29 #include "src/core/SkBitmapDevice.h"
30 #include "src/core/SkClipStackDevice.h"
31 #include "src/utils/SkBitSet.h"
32 #include "src/utils/win/SkAutoCoInitialize.h"
33 #include "src/utils/win/SkTScopedComPtr.h"
34
35 namespace sktext {
36 class GlyphRunList;
37 }
38 //#define SK_XPS_USE_DETERMINISTIC_IDS
39
40 /** \class SkXPSDevice
41
42 The drawing context for the XPS backend.
43 */
44 class SkXPSDevice final : public SkClipStackDevice {
45 public:
46 SK_SPI SkXPSDevice(SkISize, SkXPS::Options);
47 SK_SPI ~SkXPSDevice() override;
48
49 // No support for copying nor moving.
50 SkXPSDevice(const SkXPSDevice&) = delete;
51 SkXPSDevice& operator=(const SkXPSDevice&) = delete;
52
53 bool beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory*);
54 /**
55 @param unitsPerMeter converts geometry units into physical units.
56 @param pixelsPerMeter resolution to use when geometry must be rasterized.
57 @param trimSize final page size in physical units.
58 The top left of the trim is the origin of physical space.
59 @param mediaBox The size of the physical media in physical units.
60 The top and left must be less than zero.
61 The bottom and right must be greater than the trimSize.
62 The default is to coincide with the trimSize.
63 @param bleedBox The size of the bleed box in physical units.
64 Must be contained within the mediaBox.
65 The default is to coincide with the mediaBox.
66 @param artBox The size of the content box in physical units.
67 Must be contained within the trimSize.
68 The default is to coincide with the trimSize.
69 @param cropBox The size of the recommended view port in physical units.
70 Must be contained within the mediaBox.
71 The default is to coincide with the mediaBox.
72 */
73 bool beginSheet(
74 const SkVector& unitsPerMeter,
75 const SkVector& pixelsPerMeter,
76 const SkSize& trimSize,
77 const SkRect* mediaBox = nullptr,
78 const SkRect* bleedBox = nullptr,
79 const SkRect* artBox = nullptr,
80 const SkRect* cropBox = nullptr);
81
82 bool endSheet();
83 bool endPortfolio();
84
85 void drawPaint(const SkPaint& paint) override;
86 void drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&) override;
87 void drawRect(const SkRect& r,
88 const SkPaint& paint) override;
89 void drawOval(const SkRect& oval,
90 const SkPaint& paint) override;
91 void drawRRect(const SkRRect& rr,
92 const SkPaint& paint) override;
93 void drawPath(const SkPath& path,
94 const SkPaint& paint) override;
95 void drawImageRect(const SkImage*,
96 const SkRect* srcOrNull, const SkRect& dst,
97 const SkSamplingOptions&, const SkPaint& paint,
98 SkCanvas::SrcRectConstraint) override;
99
100 void drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) override;
101 void drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) override;
102
103 void drawDevice(SkDevice*, const SkSamplingOptions&, const SkPaint&) override;
104
105 sk_sp<SkDevice> createDevice(const CreateInfo&, const SkPaint*) override;
106
107 SkRecorder* baseRecorder() const override {
108 // TODO(kjlubick) the creation of this should likely involve a CPU context.
109 return skcpu::Recorder::TODO();
110 }
111
112 private:
113 class TypefaceUse {
114 public:
115 TypefaceUse(SkTypefaceID id, int index, std::unique_ptr<SkStream> data,
116 SkTScopedComPtr<IXpsOMFontResource> xps, size_t numGlyphs)
117 : typefaceId(id), ttcIndex(index), fontData(std::move(data))
118 , xpsFont(std::move(xps)), glyphsUsed(numGlyphs) {}
119 const SkTypefaceID typefaceId;
120 const int ttcIndex;
121 const std::unique_ptr<SkStream> fontData;
122 const SkTScopedComPtr<IXpsOMFontResource> xpsFont;
123 SkBitSet glyphsUsed;
124 };
125 friend HRESULT subset_typeface(const TypefaceUse& current);
126
127 void onDrawGlyphRunList(SkCanvas*, const sktext::GlyphRunList&, const SkPaint&) override;
128
129 bool createCanvasForLayer();
130
131 SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory;
132 SkTScopedComPtr<IStream> fOutputStream;
133 SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter;
134
135 unsigned int fCurrentPage;
136 SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas;
137 SkSize fCurrentCanvasSize;
138 SkVector fCurrentUnitsPerMeter;
139 SkVector fCurrentPixelsPerMeter;
140
141 skia_private::TArray<TypefaceUse, true> fTypefaces;
142 skia_private::TArray<TypefaceUse, true>* fTopTypefaces;
143
144 SkXPS::Options fOpts;
145
146 /** Creates a GUID based id and places it into buffer.
147 buffer should have space for at least GUID_ID_LEN wide characters.
148 The string will always be wchar null terminated.
149 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
150 The string may begin with a digit,
151 and so may not be suitable as a bare resource key.
152 */
153 HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-');
154 #ifdef SK_XPS_USE_DETERMINISTIC_IDS
155 decltype(GUID::Data1) fNextId = 0;
156 #endif
157
158 HRESULT initXpsDocumentWriter(IXpsOMImageResource* image);
159
160 HRESULT createXpsPage(
161 const XPS_SIZE& pageSize,
162 IXpsOMPage** page);
163
164 HRESULT createXpsThumbnail(
165 IXpsOMPage* page, const unsigned int pageNumber,
166 IXpsOMImageResource** image);
167
168 void internalDrawRect(
169 const SkRect& r,
170 bool transformRect,
171 const SkPaint& paint);
172
173 HRESULT createXpsBrush(
174 const SkPaint& skPaint,
175 IXpsOMBrush** xpsBrush,
176 const SkMatrix* parentTransform = nullptr);
177
178 HRESULT createXpsSolidColorBrush(
179 const SkColor4f skColor, const SkAlpha alpha,
180 IXpsOMBrush** xpsBrush);
181
182 HRESULT createXpsImageBrush(
183 const SkPixmap& bitmap,
184 const SkMatrix& localMatrix,
185 const SkTileMode (&xy)[2],
186 const SkAlpha alpha,
187 IXpsOMTileBrush** xpsBrush);
188
189 HRESULT createXpsLinearGradient(
190 SkShaderBase::GradientInfo info,
191 const SkAlpha alpha,
192 const SkMatrix& localMatrix,
193 IXpsOMMatrixTransform* xpsMatrixToUse,
194 IXpsOMBrush** xpsBrush);
195
196 HRESULT createXpsRadialGradient(
197 SkShaderBase::GradientInfo info,
198 const SkAlpha alpha,
199 const SkMatrix& localMatrix,
200 IXpsOMMatrixTransform* xpsMatrixToUse,
201 IXpsOMBrush** xpsBrush);
202
203 HRESULT createXpsGradientStop(
204 const SkColor4f skColor,
205 const SkScalar offset,
206 IXpsOMGradientStop** xpsGradStop);
207
208 HRESULT createXpsTransform(
209 const SkMatrix& matrix,
210 IXpsOMMatrixTransform ** xpsTransform);
211
212 HRESULT createXpsRect(
213 const SkRect& rect,
214 BOOL stroke, BOOL fill,
215 IXpsOMGeometryFigure** xpsRect);
216
217 HRESULT createXpsQuad(
218 const SkPoint (&points)[4],
219 BOOL stroke, BOOL fill,
220 IXpsOMGeometryFigure** xpsQuad);
221
222 HRESULT CreateTypefaceUse(
223 const SkFont& font,
224 TypefaceUse** fontResource);
225
226 HRESULT AddGlyphs(
227 IXpsOMObjectFactory* xpsFactory,
228 IXpsOMCanvas* canvas,
229 const TypefaceUse* font,
230 LPCWSTR text,
231 XPS_GLYPH_INDEX* xpsGlyphs,
232 UINT32 xpsGlyphsLen,
233 XPS_POINT *origin,
234 FLOAT fontSize,
235 XPS_STYLE_SIMULATION sims,
236 const SkMatrix& transform,
237 const SkPaint& paint);
238
239 HRESULT addXpsPathGeometry(
240 IXpsOMGeometryFigureCollection* figures,
241 BOOL stroke, BOOL fill, const SkPath& path);
242
243 HRESULT createPath(
244 IXpsOMGeometryFigure* figure,
245 IXpsOMVisualCollection* visuals,
246 IXpsOMPath** path);
247
248 HRESULT sideOfClamp(
249 const SkRect& leftPoints, const XPS_RECT& left,
250 IXpsOMImageResource* imageResource,
251 IXpsOMVisualCollection* visuals);
252
253 HRESULT cornerOfClamp(
254 const SkRect& tlPoints,
255 const SkColor4f color,
256 IXpsOMVisualCollection* visuals);
257
258 HRESULT clip(IXpsOMVisual* xpsVisual);
259
260 HRESULT clipToPath(
261 IXpsOMVisual* xpsVisual,
262 const SkPath& clipPath,
263 XPS_FILL_RULE fillRule);
264
265 HRESULT drawInverseWindingPath(
266 const SkPath& devicePath,
267 IXpsOMPath* xpsPath);
268
269 HRESULT shadePath(
270 IXpsOMPath* shadedPath,
271 const SkPaint& shaderPaint,
272 const SkMatrix& matrix,
273 BOOL* fill, BOOL* stroke);
274
275 void convertToPpm(
276 const SkMaskFilter* filter,
277 SkMatrix* matrix,
278 SkVector* ppuScale,
279 const SkIRect& clip, SkIRect* clipIRect);
280
281 HRESULT applyMask(
282 const SkMask& mask,
283 const SkVector& ppuScale,
284 IXpsOMPath* shadedPath);
285 };
286
287 #endif // SK_BUILD_FOR_WIN
288 #endif // SkXPSDevice_DEFINED