blob: aef9dc8baf8659b751c966becb167a9ccd3adcda [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
halcanary47ef4d52015-03-03 09:13:09 -08008#include "SkTypes.h"
mtklein1ee76512015-11-02 10:20:27 -08009#if defined(SK_BUILD_FOR_WIN32)
halcanary47ef4d52015-03-03 09:13:09 -080010
bungeman@google.comb29c8832011-10-10 13:19:10 +000011#ifndef UNICODE
12#define UNICODE
13#endif
14#ifndef _UNICODE
15#define _UNICODE
16#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +000017#include <ObjBase.h>
18#include <XpsObjectModel.h>
19#include <T2EmbApi.h>
20#include <FontSub.h>
21
22#include "SkColor.h"
23#include "SkConstexprMath.h"
24#include "SkData.h"
25#include "SkDraw.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000026#include "SkEndian.h"
herbbda26432015-11-24 08:37:01 -080027#include "SkFindAndPlaceGlyph.h"
halcanary47ef4d52015-03-03 09:13:09 -080028#include "SkGeometry.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000029#include "SkGlyphCache.h"
30#include "SkHRESULT.h"
31#include "SkImageEncoder.h"
32#include "SkIStream.h"
33#include "SkMaskFilter.h"
34#include "SkPaint.h"
reeda4393342016-03-18 11:22:57 -070035#include "SkPathEffect.h"
Ben Wagnerda5a1b82014-08-22 15:07:06 -040036#include "SkPathOps.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000037#include "SkPoint.h"
reed1e7f5e72016-04-27 07:49:17 -070038#include "SkRasterClip.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000039#include "SkRasterizer.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000040#include "SkSFNTHeader.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000041#include "SkShader.h"
42#include "SkSize.h"
43#include "SkStream.h"
44#include "SkTDArray.h"
45#include "SkTLazy.h"
46#include "SkTScopedComPtr.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000047#include "SkTTCFHeader.h"
reed@google.com398de9a2013-03-21 21:43:51 +000048#include "SkTypefacePriv.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000049#include "SkUtils.h"
50#include "SkXPSDevice.h"
51
52//Windows defines a FLOAT type,
53//make it clear when converting a scalar that this is what is wanted.
54#define SkScalarToFLOAT(n) SkScalarToFloat(n)
55
Ben Wagnerda5a1b82014-08-22 15:07:06 -040056//Dummy representation of a GUID from createId.
bungeman@google.comb29c8832011-10-10 13:19:10 +000057#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
halcanary96fcdcc2015-08-27 07:41:13 -070058//Length of GUID representation from createId, including nullptr terminator.
bungeman@google.comb29c8832011-10-10 13:19:10 +000059#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
60
61/**
62 Formats a GUID and places it into buffer.
63 buffer should have space for at least GUID_ID_LEN wide characters.
64 The string will always be wchar null terminated.
65 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
66 @return -1 if there was an error, > 0 if success.
67 */
68static int format_guid(const GUID& guid,
69 wchar_t* buffer, size_t bufferSize,
70 wchar_t sep = '-') {
71 SkASSERT(bufferSize >= GUID_ID_LEN);
72 return swprintf_s(buffer,
73 bufferSize,
74 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
75 guid.Data1,
76 sep,
77 guid.Data2,
78 sep,
79 guid.Data3,
80 sep,
81 guid.Data4[0],
82 guid.Data4[1],
83 sep,
84 guid.Data4[2],
85 guid.Data4[3],
86 guid.Data4[4],
87 guid.Data4[5],
88 guid.Data4[6],
89 guid.Data4[7]);
90}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000091
Ben Wagnerda5a1b82014-08-22 15:07:06 -040092HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000093 GUID guid = {};
Ben Wagnerda5a1b82014-08-22 15:07:06 -040094#ifdef SK_XPS_USE_DETERMINISTIC_IDS
95 guid.Data1 = fNextId++;
96 // The following make this a valid Type4 UUID.
97 guid.Data3 = 0x4000;
98 guid.Data4[0] = 0x80;
99#else
bungeman@google.comb29c8832011-10-10 13:19:10 +0000100 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400101#endif
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000102
bungeman@google.comb29c8832011-10-10 13:19:10 +0000103 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
104 HRM(E_UNEXPECTED, "Could not format GUID into id.");
105 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000106
bungeman@google.comb29c8832011-10-10 13:19:10 +0000107 return S_OK;
108}
109
110static SkBitmap make_fake_bitmap(int width, int height) {
111 SkBitmap bitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000112 bitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000113 return bitmap;
114}
115
reed@google.com900ecf22014-02-20 20:55:37 +0000116// TODO: should inherit from SkBaseDevice instead of SkBitmapDevice...
bungeman@google.comb29c8832011-10-10 13:19:10 +0000117SkXPSDevice::SkXPSDevice()
robertphillips9a53fd72015-06-22 09:46:59 -0700118 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
bungeman@google.comb29c8832011-10-10 13:19:10 +0000119 , fCurrentPage(0) {
120}
121
robertphillips9a53fd72015-06-22 09:46:59 -0700122SkXPSDevice::SkXPSDevice(IXpsOMObjectFactory* xpsFactory)
123 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
124 , fCurrentPage(0) {
125
126 HRVM(CoCreateInstance(
127 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700128 nullptr,
robertphillips9a53fd72015-06-22 09:46:59 -0700129 CLSCTX_INPROC_SERVER,
130 IID_PPV_ARGS(&this->fXpsFactory)),
131 "Could not create factory for layer.");
132
133 HRVM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
134 "Could not create canvas for layer.");
135}
136
bungeman@google.comb29c8832011-10-10 13:19:10 +0000137SkXPSDevice::~SkXPSDevice() {
138}
139
140SkXPSDevice::TypefaceUse::TypefaceUse()
141 : typefaceId(0xffffffff)
halcanary96fcdcc2015-08-27 07:41:13 -0700142 , fontData(nullptr)
143 , xpsFont(nullptr)
144 , glyphsUsed(nullptr) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000145}
146
147SkXPSDevice::TypefaceUse::~TypefaceUse() {
148 //xpsFont owns fontData ref
149 this->xpsFont->Release();
150 delete this->glyphsUsed;
151}
152
153bool SkXPSDevice::beginPortfolio(SkWStream* outputStream) {
154 if (!this->fAutoCo.succeeded()) return false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000155
bungeman@google.comb29c8832011-10-10 13:19:10 +0000156 //Create XPS Factory.
157 HRBM(CoCreateInstance(
158 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700159 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000160 CLSCTX_INPROC_SERVER,
161 IID_PPV_ARGS(&this->fXpsFactory)),
162 "Could not create XPS factory.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000163
bungeman@google.comb29c8832011-10-10 13:19:10 +0000164 HRBM(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream),
165 "Could not convert SkStream to IStream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000166
bungeman@google.comb29c8832011-10-10 13:19:10 +0000167 return true;
168}
169
170bool SkXPSDevice::beginSheet(
171 const SkVector& unitsPerMeter,
172 const SkVector& pixelsPerMeter,
173 const SkSize& trimSize,
174 const SkRect* mediaBox,
175 const SkRect* bleedBox,
176 const SkRect* artBox,
177 const SkRect* cropBox) {
178 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179
bungeman@google.comb29c8832011-10-10 13:19:10 +0000180 //For simplicity, just write everything out in geometry units,
181 //then have a base canvas do the scale to physical units.
182 this->fCurrentCanvasSize = trimSize;
183 this->fCurrentUnitsPerMeter = unitsPerMeter;
184 this->fCurrentPixelsPerMeter = pixelsPerMeter;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000185
bungeman@google.comb29c8832011-10-10 13:19:10 +0000186 this->fCurrentXpsCanvas.reset();
187 HRBM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
188 "Could not create base canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000189
bungeman@google.comb29c8832011-10-10 13:19:10 +0000190 return true;
191}
192
193HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
194 const unsigned int pageNum,
195 IXpsOMImageResource** image) {
196 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
197 HRM(CoCreateInstance(
198 CLSID_XpsOMThumbnailGenerator,
halcanary96fcdcc2015-08-27 07:41:13 -0700199 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000200 CLSCTX_INPROC_SERVER,
201 IID_PPV_ARGS(&thumbnailGenerator)),
202 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000203
bungeman@google.comb29c8832011-10-10 13:19:10 +0000204 SkTScopedComPtr<IOpcPartUri> partUri;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000205 static const size_t size = SkTUMax<
bungeman@google.comb29c8832011-10-10 13:19:10 +0000206 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + SK_DIGITS_IN(pageNum),
207 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png")
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000208 >::value;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000209 wchar_t buffer[size];
210 if (pageNum > 0) {
211 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
212 } else {
213 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400214 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000215 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
216 }
217 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
218 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000219
bungeman@google.comb29c8832011-10-10 13:19:10 +0000220 HRM(thumbnailGenerator->GenerateThumbnail(page,
221 XPS_IMAGE_TYPE_PNG,
222 XPS_THUMBNAIL_SIZE_LARGE,
223 partUri.get(),
224 image),
225 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000226
bungeman@google.comb29c8832011-10-10 13:19:10 +0000227 return S_OK;
228}
229
230HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
231 IXpsOMPage** page) {
232 static const size_t size = SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
233 + SK_DIGITS_IN(fCurrentPage);
234 wchar_t buffer[size];
235 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
236 this->fCurrentPage);
237 SkTScopedComPtr<IOpcPartUri> partUri;
238 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
239 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000240
bungeman@google.comb29c8832011-10-10 13:19:10 +0000241 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
242 HRM(this->fXpsFactory->CreatePage(&pageSize,
243 L"und",
244 partUri.get(),
245 page),
246 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000247
bungeman@google.comb29c8832011-10-10 13:19:10 +0000248 return S_OK;
249}
250
251HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
252 //Create package writer.
253 {
254 SkTScopedComPtr<IOpcPartUri> partUri;
255 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
256 &partUri),
257 "Could not create document sequence part uri.");
258 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
259 this->fOutputStream.get(),
260 TRUE,
261 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
262 partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700263 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000264 image,
halcanary96fcdcc2015-08-27 07:41:13 -0700265 nullptr,
266 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000267 &this->fPackageWriter),
268 "Could not create package writer.");
269 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000270
bungeman@google.comb29c8832011-10-10 13:19:10 +0000271 //Begin the lone document.
272 {
273 SkTScopedComPtr<IOpcPartUri> partUri;
274 HRM(this->fXpsFactory->CreatePartUri(
275 L"/Documents/1/FixedDocument.fdoc",
276 &partUri),
277 "Could not create fixed document part uri.");
278 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700279 nullptr,
280 nullptr,
281 nullptr,
282 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000283 "Could not start document.");
284 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000285
bungeman@google.comb29c8832011-10-10 13:19:10 +0000286 return S_OK;
287}
288
289bool SkXPSDevice::endSheet() {
290 //XPS is fixed at 96dpi (XPS Spec 11.1).
291 static const float xpsDPI = 96.0f;
292 static const float inchesPerMeter = 10000.0f / 254.0f;
293 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
294 const float scaleX = targetUnitsPerMeter
295 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
296 const float scaleY = targetUnitsPerMeter
297 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000298
bungeman@google.comb29c8832011-10-10 13:19:10 +0000299 //Create the scale canvas.
300 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
301 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
302 "Could not create scale canvas.");
303 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
304 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
305 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000306
bungeman@google.comb29c8832011-10-10 13:19:10 +0000307 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
308 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
309 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
310 "Could not create geometry to physical transform.");
311 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
312 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000313
bungeman@google.comb29c8832011-10-10 13:19:10 +0000314 //Add the content canvas to the scale canvas.
315 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
316 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000317
bungeman@google.comb29c8832011-10-10 13:19:10 +0000318 //Create the page.
319 XPS_SIZE pageSize = {
320 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
321 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
322 };
323 SkTScopedComPtr<IXpsOMPage> page;
324 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000325
bungeman@google.comb29c8832011-10-10 13:19:10 +0000326 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
327 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000328
bungeman@google.comb29c8832011-10-10 13:19:10 +0000329 //Add the scale canvas to the page.
330 HRBM(pageVisuals->Append(scaleCanvas.get()),
331 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000332
bungeman@google.comb29c8832011-10-10 13:19:10 +0000333 //Create the package writer if it hasn't been created yet.
halcanary96fcdcc2015-08-27 07:41:13 -0700334 if (nullptr == this->fPackageWriter.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000335 SkTScopedComPtr<IXpsOMImageResource> image;
336 //Ignore return, thumbnail is completely optional.
337 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000338
bungeman@google.comb29c8832011-10-10 13:19:10 +0000339 HRB(this->initXpsDocumentWriter(image.get()));
340 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000341
bungeman@google.comb29c8832011-10-10 13:19:10 +0000342 HRBM(this->fPackageWriter->AddPage(page.get(),
343 &pageSize,
halcanary96fcdcc2015-08-27 07:41:13 -0700344 nullptr,
345 nullptr,
346 nullptr,
347 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000348 "Could not write the page.");
349 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000350
bungeman@google.comb29c8832011-10-10 13:19:10 +0000351 return true;
352}
353
354static HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
355 //CreateFontPackage wants unsigned short.
356 //Microsoft, Y U NO stdint.h?
357 SkTDArray<unsigned short> keepList;
358 current->glyphsUsed->exportTo(&keepList);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000359
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000360 int ttcCount = (current->ttcIndex + 1);
361
bungeman@google.comb29c8832011-10-10 13:19:10 +0000362 //The following are declared with the types required by CreateFontPackage.
halcanary96fcdcc2015-08-27 07:41:13 -0700363 unsigned char *fontPackageBufferRaw = nullptr;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000364 unsigned long fontPackageBufferSize;
365 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000366 unsigned long result = CreateFontPackage(
367 (unsigned char *) current->fontData->getMemoryBase(),
368 (unsigned long) current->fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000369 &fontPackageBufferRaw,
370 &fontPackageBufferSize,
371 &bytesWritten,
372 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
373 current->ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000374 TTFCFP_SUBSET,
375 0,
376 0,
377 0,
378 keepList.begin(),
379 keepList.count(),
380 sk_malloc_throw,
381 sk_realloc_throw,
382 sk_free,
halcanary96fcdcc2015-08-27 07:41:13 -0700383 nullptr);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000384 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000385 if (result != NO_ERROR) {
386 SkDEBUGF(("CreateFontPackage Error %lu", result));
387 return E_UNEXPECTED;
388 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000389
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000390 // If it was originally a ttc, keep it a ttc.
391 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
392 size_t extra;
393 if (ttcCount > 0) {
394 // Create space for a ttc header.
395 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
396 fontPackageBuffer.realloc(bytesWritten + extra);
397 //overlap is certain, use memmove
398 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
399
400 // Write the ttc header.
401 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
402 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
403 ttcfHeader->version = SkTTCFHeader::version_1;
404 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
405 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
406 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
bsalomon0aa5cea2014-12-15 09:13:35 -0800407 *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000408 }
409
410 // Fix up offsets in sfnt table entries.
411 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
412 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
413 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
414 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
415 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
416 tableDirectory->offset = SkEndian_SwapBE32(
bsalomon0aa5cea2014-12-15 09:13:35 -0800417 SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000418 }
419 } else {
420 extra = 0;
421 fontPackageBuffer.realloc(bytesWritten);
422 }
423
scroggoa1193e42015-01-21 12:09:53 -0800424 SkAutoTDelete<SkMemoryStream> newStream(new SkMemoryStream());
mtklein18300a32016-03-16 13:53:35 -0700425 newStream->setMemoryOwned(fontPackageBuffer.release(), bytesWritten + extra);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000426
bungeman@google.comb29c8832011-10-10 13:19:10 +0000427 SkTScopedComPtr<IStream> newIStream;
mtklein18300a32016-03-16 13:53:35 -0700428 SkIStream::CreateFromSkStream(newStream.release(), true, &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000429
bungeman@google.comb29c8832011-10-10 13:19:10 +0000430 XPS_FONT_EMBEDDING embedding;
431 HRM(current->xpsFont->GetEmbeddingOption(&embedding),
432 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000433
bungeman@google.comb29c8832011-10-10 13:19:10 +0000434 SkTScopedComPtr<IOpcPartUri> partUri;
435 HRM(current->xpsFont->GetPartName(&partUri),
436 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000437
bungeman@google.comb29c8832011-10-10 13:19:10 +0000438 HRM(current->xpsFont->SetContent(
439 newIStream.get(),
440 embedding,
441 partUri.get()),
442 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000443
bungeman@google.comb29c8832011-10-10 13:19:10 +0000444 return S_OK;
445}
446
447bool SkXPSDevice::endPortfolio() {
448 //Subset fonts
449 if (!this->fTypefaces.empty()) {
450 SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
451 const TypefaceUse* last = &this->fTypefaces.back();
452 for (; current <= last; ++current) {
453 //Ignore return for now, if it didn't subset, let it be.
454 subset_typeface(current);
455 }
456 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000457
bungeman@google.comb29c8832011-10-10 13:19:10 +0000458 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000459
bungeman@google.comb29c8832011-10-10 13:19:10 +0000460 return true;
461}
462
463static XPS_COLOR xps_color(const SkColor skColor) {
464 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
465 XPS_COLOR xpsColor;
466 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
467 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
468 xpsColor.value.sRGB.red = SkColorGetR(skColor);
469 xpsColor.value.sRGB.green = SkColorGetG(skColor);
470 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000471
bungeman@google.comb29c8832011-10-10 13:19:10 +0000472 return xpsColor;
473}
474
475static XPS_POINT xps_point(const SkPoint& point) {
476 XPS_POINT xpsPoint = {
477 SkScalarToFLOAT(point.fX),
478 SkScalarToFLOAT(point.fY),
479 };
480 return xpsPoint;
481}
482
483static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
484 SkPoint skTransformedPoint;
485 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
486 return xps_point(skTransformedPoint);
487}
488
489static XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
490 switch (tileMode) {
491 case SkShader::kClamp_TileMode:
492 return XPS_SPREAD_METHOD_PAD;
493 case SkShader::kRepeat_TileMode:
494 return XPS_SPREAD_METHOD_REPEAT;
495 case SkShader::kMirror_TileMode:
496 return XPS_SPREAD_METHOD_REFLECT;
497 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000498 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000499 }
500 return XPS_SPREAD_METHOD_PAD;
501}
502
503static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
504 const SkPoint& start, const SkPoint& end,
505 const SkMatrix& transform) {
506 SkPoint startTransformed;
507 transform.mapXY(start.fX, start.fY, &startTransformed);
508 SkPoint endTransformed;
509 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000510
bungeman@google.comb29c8832011-10-10 13:19:10 +0000511 //Manhattan distance between transformed start and end.
512 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
513 + (endTransformed.fY - startTransformed.fY);
514 if (SkScalarNearlyZero(startToEnd)) {
515 for (int i = 0; i < numOffsets; ++i) {
516 stopOffsets[i] = 0;
517 }
518 return;
519 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000520
bungeman@google.comb29c8832011-10-10 13:19:10 +0000521 for (int i = 0; i < numOffsets; ++i) {
522 SkPoint stop;
523 stop.fX = SkScalarMul(end.fX - start.fX, stopOffsets[i]);
524 stop.fY = SkScalarMul(end.fY - start.fY, stopOffsets[i]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000525
bungeman@google.comb29c8832011-10-10 13:19:10 +0000526 SkPoint stopTransformed;
527 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000528
bungeman@google.comb29c8832011-10-10 13:19:10 +0000529 //Manhattan distance between transformed start and stop.
530 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
531 + (stopTransformed.fY - startTransformed.fY);
532 //Percentage along transformed line.
reed80ea19c2015-05-12 10:37:34 -0700533 stopOffsets[i] = startToStop / startToEnd;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000534 }
535}
536
537HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
538 IXpsOMMatrixTransform** xpsTransform) {
539 SkScalar affine[6];
540 if (!matrix.asAffine(affine)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700541 *xpsTransform = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000542 return S_FALSE;
543 }
544 XPS_MATRIX rawXpsMatrix = {
545 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
546 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
547 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
548 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
549 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
550 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
551 };
552 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
553 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000554
bungeman@google.comb29c8832011-10-10 13:19:10 +0000555 return S_OK;
556}
557
558HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
559 IXpsOMVisualCollection* visuals,
560 IXpsOMPath** path) {
561 SkTScopedComPtr<IXpsOMGeometry> geometry;
562 HRM(this->fXpsFactory->CreateGeometry(&geometry),
563 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000564
bungeman@google.comb29c8832011-10-10 13:19:10 +0000565 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
566 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
567 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000568
bungeman@google.comb29c8832011-10-10 13:19:10 +0000569 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
570 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000571
bungeman@google.comb29c8832011-10-10 13:19:10 +0000572 HRM(visuals->Append(*path), "Could not add path to visuals.");
573 return S_OK;
574}
575
576HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
577 const SkAlpha alpha,
578 IXpsOMBrush** xpsBrush) {
579 XPS_COLOR xpsColor = xps_color(skColor);
580 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
halcanary96fcdcc2015-08-27 07:41:13 -0700581 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000582 "Could not create solid color brush.");
583 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
584 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
585 return S_OK;
586}
587
588HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
589 const XPS_RECT& imageViewBox,
590 IXpsOMImageResource* image,
591 IXpsOMVisualCollection* visuals) {
592 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
593 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000594
bungeman@google.comb29c8832011-10-10 13:19:10 +0000595 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
596 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000597
bungeman@google.comb29c8832011-10-10 13:19:10 +0000598 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
599 HRM(this->fXpsFactory->CreateImageBrush(image,
600 &imageViewBox,
601 &imageViewBox,
602 &areaToFillBrush),
603 "Could not create brush for side of clamp.");
604 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
605 "Could not set tile mode for side of clamp.");
606 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
607 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000608
bungeman@google.comb29c8832011-10-10 13:19:10 +0000609 return S_OK;
610}
611
612HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
613 const SkColor color,
614 IXpsOMVisualCollection* visuals) {
615 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
616 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000617
bungeman@google.comb29c8832011-10-10 13:19:10 +0000618 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
619 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000620
bungeman@google.comb29c8832011-10-10 13:19:10 +0000621 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
622 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
623 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
624 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000625
bungeman@google.comb29c8832011-10-10 13:19:10 +0000626 return S_OK;
627}
628
629static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
630static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
631static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
632static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
633static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
634
635//TODO(bungeman): In the future, should skia add None,
636//handle None+Mirror and None+Repeat correctly.
637//None is currently an internal hack so masks don't repeat (None+None only).
638static XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
639 [SkShader::kTileModeCount+1] = {
640 //Clamp //Repeat //Mirror //None
641/*Clamp */ XTM_N, XTM_T, XTM_Y, XTM_N,
642/*Repeat*/ XTM_T, XTM_T, XTM_Y, XTM_N,
643/*Mirror*/ XTM_X, XTM_X, XTM_XY, XTM_X,
644/*None */ XTM_N, XTM_N, XTM_Y, XTM_N,
645};
646
647HRESULT SkXPSDevice::createXpsImageBrush(
648 const SkBitmap& bitmap,
649 const SkMatrix& localMatrix,
650 const SkShader::TileMode (&xy)[2],
651 const SkAlpha alpha,
652 IXpsOMTileBrush** xpsBrush) {
653 SkDynamicMemoryWStream write;
654 if (!SkImageEncoder::EncodeStream(&write, bitmap,
655 SkImageEncoder::kPNG_Type, 100)) {
656 HRM(E_FAIL, "Unable to encode bitmap as png.");
657 }
658 SkMemoryStream* read = new SkMemoryStream;
659 read->setData(write.copyToData())->unref();
660 SkTScopedComPtr<IStream> readWrapper;
661 HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
662 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000663
bungeman@google.comb29c8832011-10-10 13:19:10 +0000664 const size_t size =
665 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
666 wchar_t buffer[size];
667 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400668 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000669 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000670
bungeman@google.comb29c8832011-10-10 13:19:10 +0000671 SkTScopedComPtr<IOpcPartUri> imagePartUri;
672 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
673 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000674
bungeman@google.comb29c8832011-10-10 13:19:10 +0000675 SkTScopedComPtr<IXpsOMImageResource> imageResource;
676 HRM(this->fXpsFactory->CreateImageResource(
677 readWrapper.get(),
678 XPS_IMAGE_TYPE_PNG,
679 imagePartUri.get(),
680 &imageResource),
681 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000682
bungeman@google.comb29c8832011-10-10 13:19:10 +0000683 XPS_RECT bitmapRect = {
684 0.0, 0.0,
685 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
686 };
687 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
688 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
689 &bitmapRect, &bitmapRect,
690 &xpsImageBrush),
691 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000692
bungeman@google.comb29c8832011-10-10 13:19:10 +0000693 if (SkShader::kClamp_TileMode != xy[0] &&
694 SkShader::kClamp_TileMode != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000695
bungeman@google.comb29c8832011-10-10 13:19:10 +0000696 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
697 "Could not set image tile mode");
698 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
699 "Could not set image opacity.");
700 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
701 } else {
702 //TODO(bungeman): compute how big this really needs to be.
703 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
704 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
705 const SkScalar bWidth = SkIntToScalar(bitmap.width());
706 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000707
bungeman@google.comb29c8832011-10-10 13:19:10 +0000708 //create brush canvas
709 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
710 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
711 "Could not create image brush canvas.");
712 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
713 HRM(brushCanvas->GetVisuals(&brushVisuals),
714 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000715
bungeman@google.comb29c8832011-10-10 13:19:10 +0000716 //create central figure
717 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
718 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
719 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000720
bungeman@google.comb29c8832011-10-10 13:19:10 +0000721 SkTScopedComPtr<IXpsOMPath> centralPath;
722 HR(this->createPath(centralFigure.get(),
723 brushVisuals.get(),
724 &centralPath));
725 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
726 "Could not set tile mode for image brush central path.");
727 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
728 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000729
bungeman@google.comb29c8832011-10-10 13:19:10 +0000730 //add left/right
731 if (SkShader::kClamp_TileMode == xy[0]) {
732 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
733 XPS_RECT leftImageViewBox = {
734 0.0, 0.0,
735 1.0, static_cast<FLOAT>(bitmap.height()),
736 };
737 HR(this->sideOfClamp(leftArea, leftImageViewBox,
738 imageResource.get(),
739 brushVisuals.get()));
740
741 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
742 XPS_RECT rightImageViewBox = {
743 bitmap.width() - 1.0f, 0.0f,
744 1.0f, static_cast<FLOAT>(bitmap.height()),
745 };
746 HR(this->sideOfClamp(rightArea, rightImageViewBox,
747 imageResource.get(),
748 brushVisuals.get()));
749 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000750
bungeman@google.comb29c8832011-10-10 13:19:10 +0000751 //add top/bottom
752 if (SkShader::kClamp_TileMode == xy[1]) {
753 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
754 XPS_RECT topImageViewBox = {
755 0.0, 0.0,
756 static_cast<FLOAT>(bitmap.width()), 1.0,
757 };
758 HR(this->sideOfClamp(topArea, topImageViewBox,
759 imageResource.get(),
760 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000761
bungeman@google.comb29c8832011-10-10 13:19:10 +0000762 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
763 XPS_RECT bottomImageViewBox = {
764 0.0f, bitmap.height() - 1.0f,
765 static_cast<FLOAT>(bitmap.width()), 1.0f,
766 };
767 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
768 imageResource.get(),
769 brushVisuals.get()));
770 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000771
bungeman@google.comb29c8832011-10-10 13:19:10 +0000772 //add tl, tr, bl, br
773 if (SkShader::kClamp_TileMode == xy[0] &&
774 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000775
bungeman@google.comb29c8832011-10-10 13:19:10 +0000776 SkAutoLockPixels alp(bitmap);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000777
bungeman@google.comb29c8832011-10-10 13:19:10 +0000778 const SkColor tlColor = bitmap.getColor(0,0);
779 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
780 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000781
bungeman@google.comb29c8832011-10-10 13:19:10 +0000782 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
783 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
784 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000785
bungeman@google.comb29c8832011-10-10 13:19:10 +0000786 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
787 bitmap.height()-1);
788 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
789 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000790
bungeman@google.comb29c8832011-10-10 13:19:10 +0000791 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
792 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
793 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
794 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000795
bungeman@google.comb29c8832011-10-10 13:19:10 +0000796 //create visual brush from canvas
797 XPS_RECT bound = {};
798 if (SkShader::kClamp_TileMode == xy[0] &&
799 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000800
bungeman@google.comb29c8832011-10-10 13:19:10 +0000801 bound.x = BIG_F / -2;
802 bound.y = BIG_F / -2;
803 bound.width = BIG_F;
804 bound.height = BIG_F;
805 } else if (SkShader::kClamp_TileMode == xy[0]) {
806 bound.x = BIG_F / -2;
807 bound.y = 0.0f;
808 bound.width = BIG_F;
809 bound.height = static_cast<FLOAT>(bitmap.height());
810 } else if (SkShader::kClamp_TileMode == xy[1]) {
811 bound.x = 0;
812 bound.y = BIG_F / -2;
813 bound.width = static_cast<FLOAT>(bitmap.width());
814 bound.height = BIG_F;
815 }
816 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
817 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
818 "Could not create visual brush for image brush.");
819 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
820 "Could not set canvas on visual brush for image brush.");
821 HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
822 "Could not set tile mode on visual brush for image brush.");
823 HRM(clampBrush->SetOpacity(alpha / 255.0f),
824 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000825
bungeman@google.comb29c8832011-10-10 13:19:10 +0000826 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
827 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000828
bungeman@google.comb29c8832011-10-10 13:19:10 +0000829 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
830 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
bsalomon49f085d2014-09-05 13:34:00 -0700831 if (xpsMatrixToUse.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000832 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
833 "Could not set transform for image brush.");
834 } else {
835 //TODO(bungeman): perspective bitmaps in general.
836 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000837
bungeman@google.comb29c8832011-10-10 13:19:10 +0000838 return S_OK;
839}
840
841HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
842 const SkScalar offset,
843 IXpsOMGradientStop** xpsGradStop) {
844 XPS_COLOR gradStopXpsColor = xps_color(skColor);
845 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
halcanary96fcdcc2015-08-27 07:41:13 -0700846 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000847 SkScalarToFLOAT(offset),
848 xpsGradStop),
849 "Could not create gradient stop.");
850 return S_OK;
851}
852
853HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
854 const SkAlpha alpha,
855 const SkMatrix& localMatrix,
856 IXpsOMMatrixTransform* xpsMatrix,
857 IXpsOMBrush** xpsBrush) {
858 XPS_POINT startPoint;
859 XPS_POINT endPoint;
bsalomon49f085d2014-09-05 13:34:00 -0700860 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000861 startPoint = xps_point(info.fPoint[0]);
862 endPoint = xps_point(info.fPoint[1]);
863 } else {
864 transform_offsets(info.fColorOffsets, info.fColorCount,
865 info.fPoint[0], info.fPoint[1],
866 localMatrix);
867 startPoint = xps_point(info.fPoint[0], localMatrix);
868 endPoint = xps_point(info.fPoint[1], localMatrix);
869 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000870
bungeman@google.comb29c8832011-10-10 13:19:10 +0000871 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
872 HR(createXpsGradientStop(info.fColors[0],
873 info.fColorOffsets[0],
874 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000875
bungeman@google.comb29c8832011-10-10 13:19:10 +0000876 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
877 HR(createXpsGradientStop(info.fColors[1],
878 info.fColorOffsets[1],
879 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000880
bungeman@google.comb29c8832011-10-10 13:19:10 +0000881 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
882 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
883 gradStop1.get(),
884 &startPoint,
885 &endPoint,
886 &gradientBrush),
887 "Could not create linear gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700888 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000889 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
890 "Could not set transform on linear gradient brush.");
891 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000892
bungeman@google.comb29c8832011-10-10 13:19:10 +0000893 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
894 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
895 "Could not get linear gradient stop collection.");
896 for (int i = 2; i < info.fColorCount; ++i) {
897 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
898 HR(createXpsGradientStop(info.fColors[i],
899 info.fColorOffsets[i],
900 &gradStop));
901 HRM(gradStopCollection->Append(gradStop.get()),
902 "Could not add linear gradient stop.");
903 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000904
bungeman@google.comb29c8832011-10-10 13:19:10 +0000905 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
906 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000907
bungeman@google.comb29c8832011-10-10 13:19:10 +0000908 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
909 "Could not set opacity of linear gradient brush.");
910 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000911
bungeman@google.comb29c8832011-10-10 13:19:10 +0000912 return S_OK;
913}
914
915HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
916 const SkAlpha alpha,
917 const SkMatrix& localMatrix,
918 IXpsOMMatrixTransform* xpsMatrix,
919 IXpsOMBrush** xpsBrush) {
920 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
921 HR(createXpsGradientStop(info.fColors[0],
922 info.fColorOffsets[0],
923 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000924
bungeman@google.comb29c8832011-10-10 13:19:10 +0000925 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
926 HR(createXpsGradientStop(info.fColors[1],
927 info.fColorOffsets[1],
928 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000929
bungeman@google.comb29c8832011-10-10 13:19:10 +0000930 //TODO: figure out how to fake better if not affine
931 XPS_POINT centerPoint;
932 XPS_POINT gradientOrigin;
933 XPS_SIZE radiiSizes;
bsalomon49f085d2014-09-05 13:34:00 -0700934 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000935 centerPoint = xps_point(info.fPoint[0]);
936 gradientOrigin = xps_point(info.fPoint[0]);
937 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
938 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
939 } else {
940 centerPoint = xps_point(info.fPoint[0], localMatrix);
941 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000942
bungeman@google.comb29c8832011-10-10 13:19:10 +0000943 SkScalar radius = info.fRadius[0];
944 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000945
bungeman@google.comb29c8832011-10-10 13:19:10 +0000946 vec[0].set(radius, 0);
947 vec[1].set(0, radius);
948 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000949
bungeman@google.comb29c8832011-10-10 13:19:10 +0000950 SkScalar d0 = vec[0].length();
951 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000952
bungeman@google.comb29c8832011-10-10 13:19:10 +0000953 radiiSizes.width = SkScalarToFLOAT(d0);
954 radiiSizes.height = SkScalarToFLOAT(d1);
955 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000956
bungeman@google.comb29c8832011-10-10 13:19:10 +0000957 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
958 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
959 gradStop1.get(),
960 &centerPoint,
961 &gradientOrigin,
962 &radiiSizes,
963 &gradientBrush),
964 "Could not create radial gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700965 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000966 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
967 "Could not set transform on radial gradient brush.");
968 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000969
bungeman@google.comb29c8832011-10-10 13:19:10 +0000970 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
971 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
972 "Could not get radial gradient stop collection.");
973 for (int i = 2; i < info.fColorCount; ++i) {
974 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
975 HR(createXpsGradientStop(info.fColors[i],
976 info.fColorOffsets[i],
977 &gradStop));
978 HRM(gradStopCollection->Append(gradStop.get()),
979 "Could not add radial gradient stop.");
980 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000981
bungeman@google.comb29c8832011-10-10 13:19:10 +0000982 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
983 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000984
bungeman@google.comb29c8832011-10-10 13:19:10 +0000985 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
986 "Could not set opacity of radial gradient brush.");
987 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000988
bungeman@google.comb29c8832011-10-10 13:19:10 +0000989 return S_OK;
990}
991
992HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
993 IXpsOMBrush** brush,
994 const SkMatrix* parentTransform) {
995 const SkShader *shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -0700996 if (nullptr == shader) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000997 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
998 return S_OK;
999 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001000
bungeman@google.comb29c8832011-10-10 13:19:10 +00001001 //Gradient shaders.
1002 SkShader::GradientInfo info;
1003 info.fColorCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001004 info.fColors = nullptr;
1005 info.fColorOffsets = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001006 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001007
bungeman@google.comb29c8832011-10-10 13:19:10 +00001008 if (SkShader::kNone_GradientType == gradientType) {
1009 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001010
bungeman@google.comb29c8832011-10-10 13:19:10 +00001011 } else if (SkShader::kColor_GradientType == gradientType) {
1012 SkASSERT(1 == info.fColorCount);
1013 SkColor color;
1014 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +00001015 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001016 SkAlpha alpha = skPaint.getAlpha();
1017 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1018 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001019
bungeman@google.comb29c8832011-10-10 13:19:10 +00001020 } else {
1021 if (info.fColorCount == 0) {
1022 const SkColor color = skPaint.getColor();
1023 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
1024 return S_OK;
1025 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001026
bungeman@google.comb29c8832011-10-10 13:19:10 +00001027 SkAutoTArray<SkColor> colors(info.fColorCount);
1028 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1029 info.fColors = colors.get();
1030 info.fColorOffsets = colorOffsets.get();
1031 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001032
bungeman@google.comb29c8832011-10-10 13:19:10 +00001033 if (1 == info.fColorCount) {
1034 SkColor color = info.fColors[0];
1035 SkAlpha alpha = skPaint.getAlpha();
1036 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1037 return S_OK;
1038 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001039
bsalomon@google.comf94b3a42012-10-31 18:09:01 +00001040 SkMatrix localMatrix = shader->getLocalMatrix();
bsalomon49f085d2014-09-05 13:34:00 -07001041 if (parentTransform) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001042 localMatrix.preConcat(*parentTransform);
1043 }
1044 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1045 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001046
bungeman@google.comb29c8832011-10-10 13:19:10 +00001047 if (SkShader::kLinear_GradientType == gradientType) {
1048 HR(this->createXpsLinearGradient(info,
1049 skPaint.getAlpha(),
1050 localMatrix,
1051 xpsMatrixToUse.get(),
1052 brush));
1053 return S_OK;
1054 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001055
bungeman@google.comb29c8832011-10-10 13:19:10 +00001056 if (SkShader::kRadial_GradientType == gradientType) {
1057 HR(this->createXpsRadialGradient(info,
1058 skPaint.getAlpha(),
1059 localMatrix,
1060 xpsMatrixToUse.get(),
1061 brush));
1062 return S_OK;
1063 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001064
reed71a6cbf2015-05-04 08:32:51 -07001065 if (SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001066 //simple if affine and one is 0, otherwise will have to fake
1067 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001068
bungeman@google.comb29c8832011-10-10 13:19:10 +00001069 if (SkShader::kSweep_GradientType == gradientType) {
1070 //have to fake
1071 }
1072 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001073
bungeman@google.comb29c8832011-10-10 13:19:10 +00001074 SkBitmap outTexture;
1075 SkMatrix outMatrix;
1076 SkShader::TileMode xy[2];
reedf5822822015-08-19 11:46:38 -07001077 if (shader->isABitmap(&outTexture, &outMatrix, xy)) {
1078 //TODO: outMatrix??
1079 SkMatrix localMatrix = shader->getLocalMatrix();
1080 if (parentTransform) {
1081 localMatrix.preConcat(*parentTransform);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001082 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001083
reedf5822822015-08-19 11:46:38 -07001084 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1085 HR(this->createXpsImageBrush(outTexture,
1086 localMatrix,
1087 xy,
1088 skPaint.getAlpha(),
1089 &tileBrush));
1090
1091 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
1092 } else {
1093 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1094 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001095 return S_OK;
1096}
1097
1098static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1099 const bool zeroWidth = (0 == paint.getStrokeWidth());
1100 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001101
bungeman@google.comb29c8832011-10-10 13:19:10 +00001102 return paint.getPathEffect() ||
1103 paint.getMaskFilter() ||
1104 paint.getRasterizer() ||
1105 (stroke && (
1106 (matrix.hasPerspective() && !zeroWidth) ||
1107 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1108 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1109 paint.getStrokeMiter() < SK_ScalarSqrt2)
1110 ))
1111 ;
1112}
1113
1114HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1115 IXpsOMGeometryFigure** xpsRect) {
1116 const SkPoint points[4] = {
1117 { rect.fLeft, rect.fTop },
1118 { rect.fRight, rect.fTop },
1119 { rect.fRight, rect.fBottom },
1120 { rect.fLeft, rect.fBottom },
1121 };
1122 return this->createXpsQuad(points, stroke, fill, xpsRect);
1123}
1124HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1125 BOOL stroke, BOOL fill,
1126 IXpsOMGeometryFigure** xpsQuad) {
1127 // Define the start point.
1128 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001129
bungeman@google.comb29c8832011-10-10 13:19:10 +00001130 // Create the figure.
1131 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1132 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001133
bungeman@google.comb29c8832011-10-10 13:19:10 +00001134 // Define the type of each segment.
1135 XPS_SEGMENT_TYPE segmentTypes[3] = {
1136 XPS_SEGMENT_TYPE_LINE,
1137 XPS_SEGMENT_TYPE_LINE,
1138 XPS_SEGMENT_TYPE_LINE,
1139 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001140
bungeman@google.comb29c8832011-10-10 13:19:10 +00001141 // Define the x and y coordinates of each corner of the figure.
1142 FLOAT segmentData[6] = {
1143 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1144 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1145 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1146 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001147
bungeman@google.comb29c8832011-10-10 13:19:10 +00001148 // Describe if the segments are stroked.
1149 BOOL segmentStrokes[3] = {
1150 stroke, stroke, stroke,
1151 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001152
bungeman@google.comb29c8832011-10-10 13:19:10 +00001153 // Add the segment data to the figure.
1154 HRM((*xpsQuad)->SetSegments(
1155 3, 6,
1156 segmentTypes , segmentData, segmentStrokes),
1157 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001158
bungeman@google.comb29c8832011-10-10 13:19:10 +00001159 // Set the closed and filled properties of the figure.
1160 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1161 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001162
bungeman@google.comb29c8832011-10-10 13:19:10 +00001163 return S_OK;
1164}
1165
bungeman@google.comb29c8832011-10-10 13:19:10 +00001166void SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1167 size_t count, const SkPoint points[],
1168 const SkPaint& paint) {
1169 //This will call back into the device to do the drawing.
1170 d.drawPoints(mode, count, points, paint, true);
1171}
1172
1173void SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1174 int vertexCount, const SkPoint verts[],
1175 const SkPoint texs[], const SkColor colors[],
1176 SkXfermode* xmode, const uint16_t indices[],
1177 int indexCount, const SkPaint& paint) {
1178 //TODO: override this for XPS
1179 SkDEBUGF(("XPS drawVertices not yet implemented."));
1180}
1181
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001182void SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001183 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001184
bungeman@google.comb29c8832011-10-10 13:19:10 +00001185 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001186 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1187 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1188 if (paint->getStyle() != SkPaint::kFill_Style) {
1189 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001190 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001191
bungeman@google.comb29c8832011-10-10 13:19:10 +00001192 this->internalDrawRect(d, r, false, *fillPaint);
1193}
1194
1195void SkXPSDevice::drawRect(const SkDraw& d,
1196 const SkRect& r,
1197 const SkPaint& paint) {
1198 this->internalDrawRect(d, r, true, paint);
1199}
1200
scroggo@google.comcac8d012013-11-12 17:10:02 +00001201void SkXPSDevice::drawRRect(const SkDraw& d,
1202 const SkRRect& rr,
1203 const SkPaint& paint) {
1204 SkPath path;
1205 path.addRRect(rr);
halcanary96fcdcc2015-08-27 07:41:13 -07001206 this->drawPath(d, path, paint, nullptr, true);
scroggo@google.comcac8d012013-11-12 17:10:02 +00001207}
1208
bungeman@google.comb29c8832011-10-10 13:19:10 +00001209void SkXPSDevice::internalDrawRect(const SkDraw& d,
1210 const SkRect& r,
1211 bool transformRect,
1212 const SkPaint& paint) {
1213 //Exit early if there is nothing to draw.
reed1e7f5e72016-04-27 07:49:17 -07001214 if (d.fRC->isEmpty() ||
halcanary96fcdcc2015-08-27 07:41:13 -07001215 (paint.getAlpha() == 0 && paint.getXfermode() == nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001216 return;
1217 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001218
bungeman@google.comb29c8832011-10-10 13:19:10 +00001219 //Path the rect if we can't optimize it.
1220 if (rect_must_be_pathed(paint, *d.fMatrix)) {
1221 SkPath tmp;
1222 tmp.addRect(r);
1223 tmp.setFillType(SkPath::kWinding_FillType);
halcanary96fcdcc2015-08-27 07:41:13 -07001224 this->drawPath(d, tmp, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001225 return;
1226 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001227
bungeman@google.comb29c8832011-10-10 13:19:10 +00001228 //Create the shaded path.
1229 SkTScopedComPtr<IXpsOMPath> shadedPath;
1230 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1231 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001232
bungeman@google.comb29c8832011-10-10 13:19:10 +00001233 //Create the shaded geometry.
1234 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1235 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1236 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001237
bungeman@google.comb29c8832011-10-10 13:19:10 +00001238 //Add the geometry to the shaded path.
1239 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1240 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001241
bungeman@google.comb29c8832011-10-10 13:19:10 +00001242 //Set the brushes.
1243 BOOL fill = FALSE;
1244 BOOL stroke = FALSE;
1245 HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001246
bungeman@google.comb29c8832011-10-10 13:19:10 +00001247 bool xpsTransformsPath = true;
1248 //Transform the geometry.
1249 if (transformRect && xpsTransformsPath) {
1250 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1251 HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1252 if (xpsTransform.get()) {
1253 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1254 "Could not set transform for rect.");
1255 } else {
1256 xpsTransformsPath = false;
1257 }
1258 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001259
bungeman@google.comb29c8832011-10-10 13:19:10 +00001260 //Create the figure.
1261 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1262 {
1263 SkPoint points[4] = {
1264 { r.fLeft, r.fTop },
1265 { r.fLeft, r.fBottom },
1266 { r.fRight, r.fBottom },
1267 { r.fRight, r.fTop },
1268 };
1269 if (!xpsTransformsPath && transformRect) {
1270 d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1271 }
1272 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1273 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001274
bungeman@google.comb29c8832011-10-10 13:19:10 +00001275 //Get the figures of the shaded geometry.
1276 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1277 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1278 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001279
bungeman@google.comb29c8832011-10-10 13:19:10 +00001280 //Add the figure to the shaded geometry figures.
1281 HRVM(shadedFigures->Append(rectFigure.get()),
1282 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001283
bungeman@google.comb29c8832011-10-10 13:19:10 +00001284 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001285
bungeman@google.comb29c8832011-10-10 13:19:10 +00001286 //Add the shaded path to the current visuals.
1287 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1288 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1289 "Could not get current visuals for rect.");
1290 HRVM(currentVisuals->Append(shadedPath.get()),
1291 "Could not add rect to current visuals.");
1292}
1293
1294static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1295 const SkTDArray<BOOL>& segmentStrokes,
1296 const SkTDArray<FLOAT>& segmentData,
1297 BOOL stroke, BOOL fill,
1298 IXpsOMGeometryFigure* figure,
1299 IXpsOMGeometryFigureCollection* figures) {
1300 // Add the segment data to the figure.
1301 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1302 segmentTypes.begin() , segmentData.begin(),
1303 segmentStrokes.begin()),
1304 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001305
bungeman@google.comb29c8832011-10-10 13:19:10 +00001306 // Set the closed and filled properties of the figure.
1307 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1308 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001309
bungeman@google.comb29c8832011-10-10 13:19:10 +00001310 // Add the figure created above to this geometry.
1311 HRM(figures->Append(figure), "Could not add path to geometry.");
1312 return S_OK;
1313}
1314
1315HRESULT SkXPSDevice::addXpsPathGeometry(
1316 IXpsOMGeometryFigureCollection* xpsFigures,
1317 BOOL stroke, BOOL fill, const SkPath& path) {
1318 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1319 SkTDArray<BOOL> segmentStrokes;
1320 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001321
bungeman@google.comb29c8832011-10-10 13:19:10 +00001322 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1323 SkPath::Iter iter(path, true);
1324 SkPoint points[4];
1325 SkPath::Verb verb;
1326 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1327 switch (verb) {
1328 case SkPath::kMove_Verb: {
bsalomon49f085d2014-09-05 13:34:00 -07001329 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001330 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1331 stroke, fill,
1332 xpsFigure.get() , xpsFigures));
1333 xpsFigure.reset();
1334 segmentTypes.rewind();
1335 segmentStrokes.rewind();
1336 segmentData.rewind();
1337 }
1338 // Define the start point.
1339 XPS_POINT startPoint = xps_point(points[0]);
1340 // Create the figure.
1341 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1342 &xpsFigure),
1343 "Could not create path geometry figure.");
1344 break;
1345 }
1346 case SkPath::kLine_Verb:
1347 if (iter.isCloseLine()) break; //ignore the line, auto-closed
1348 segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1349 segmentStrokes.push(stroke);
1350 segmentData.push(SkScalarToFLOAT(points[1].fX));
1351 segmentData.push(SkScalarToFLOAT(points[1].fY));
1352 break;
1353 case SkPath::kQuad_Verb:
1354 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1355 segmentStrokes.push(stroke);
1356 segmentData.push(SkScalarToFLOAT(points[1].fX));
1357 segmentData.push(SkScalarToFLOAT(points[1].fY));
1358 segmentData.push(SkScalarToFLOAT(points[2].fX));
1359 segmentData.push(SkScalarToFLOAT(points[2].fY));
1360 break;
1361 case SkPath::kCubic_Verb:
1362 segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1363 segmentStrokes.push(stroke);
1364 segmentData.push(SkScalarToFLOAT(points[1].fX));
1365 segmentData.push(SkScalarToFLOAT(points[1].fY));
1366 segmentData.push(SkScalarToFLOAT(points[2].fX));
1367 segmentData.push(SkScalarToFLOAT(points[2].fY));
1368 segmentData.push(SkScalarToFLOAT(points[3].fX));
1369 segmentData.push(SkScalarToFLOAT(points[3].fY));
1370 break;
halcanary47ef4d52015-03-03 09:13:09 -08001371 case SkPath::kConic_Verb: {
1372 const SkScalar tol = SK_Scalar1 / 4;
1373 SkAutoConicToQuads converter;
1374 const SkPoint* quads =
1375 converter.computeQuads(points, iter.conicWeight(), tol);
1376 for (int i = 0; i < converter.countQuads(); ++i) {
1377 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1378 segmentStrokes.push(stroke);
1379 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fX));
1380 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fY));
1381 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fX));
1382 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fY));
1383 }
1384 break;
1385 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001386 case SkPath::kClose_Verb:
1387 // we ignore these, and just get the whole segment from
1388 // the corresponding line/quad/cubic verbs
1389 break;
1390 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001391 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001392 break;
1393 }
1394 }
bsalomon49f085d2014-09-05 13:34:00 -07001395 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001396 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1397 stroke, fill,
1398 xpsFigure.get(), xpsFigures));
1399 }
1400 return S_OK;
1401}
1402
bungeman@google.comb29c8832011-10-10 13:19:10 +00001403void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1404 SkMatrix* matrix,
1405 SkVector* ppuScale,
1406 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001407 //This action is in unit space, but the ppm is specified in physical space.
reed80ea19c2015-05-12 10:37:34 -07001408 ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
1409 fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001410
bungeman@google.comb29c8832011-10-10 13:19:10 +00001411 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001412
bungeman@google.comb29c8832011-10-10 13:19:10 +00001413 const SkIRect& irect = clip;
1414 SkRect clipRect = SkRect::MakeLTRB(
1415 SkScalarMul(SkIntToScalar(irect.fLeft), ppuScale->fX),
1416 SkScalarMul(SkIntToScalar(irect.fTop), ppuScale->fY),
1417 SkScalarMul(SkIntToScalar(irect.fRight), ppuScale->fX),
1418 SkScalarMul(SkIntToScalar(irect.fBottom), ppuScale->fY));
1419 clipRect.roundOut(clipIRect);
1420}
1421
1422HRESULT SkXPSDevice::applyMask(const SkDraw& d,
1423 const SkMask& mask,
1424 const SkVector& ppuScale,
1425 IXpsOMPath* shadedPath) {
1426 //Get the geometry object.
1427 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1428 HRM(shadedPath->GetGeometry(&shadedGeometry),
1429 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001430
bungeman@google.comb29c8832011-10-10 13:19:10 +00001431 //Get the figures from the geometry.
1432 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1433 HRM(shadedGeometry->GetFigures(&shadedFigures),
1434 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001435
bungeman@google.comb29c8832011-10-10 13:19:10 +00001436 SkMatrix m;
1437 m.reset();
1438 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1439 SkIntToScalar(mask.fBounds.fTop));
1440 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001441
bungeman@google.comb29c8832011-10-10 13:19:10 +00001442 SkShader::TileMode xy[2];
1443 xy[0] = (SkShader::TileMode)3;
1444 xy[1] = (SkShader::TileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001445
bungeman@google.comb29c8832011-10-10 13:19:10 +00001446 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001447 bm.installMaskPixels(mask);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001448
bungeman@google.comb29c8832011-10-10 13:19:10 +00001449 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1450 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1451 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1452 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001453
bungeman@google.comb29c8832011-10-10 13:19:10 +00001454 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1455 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1456 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1457 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1458 "Could not create mask shaded figure.");
1459 HRM(shadedFigures->Append(shadedFigure.get()),
1460 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001461
bungeman@google.comb29c8832011-10-10 13:19:10 +00001462 HR(this->clip(shadedPath, d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001463
bungeman@google.comb29c8832011-10-10 13:19:10 +00001464 //Add the path to the active visual collection.
1465 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1466 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1467 "Could not get mask current visuals.");
1468 HRM(currentVisuals->Append(shadedPath),
1469 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001470
bungeman@google.comb29c8832011-10-10 13:19:10 +00001471 return S_OK;
1472}
1473
1474HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1475 const SkPaint& shaderPaint,
1476 const SkMatrix& matrix,
1477 BOOL* fill, BOOL* stroke) {
1478 *fill = FALSE;
1479 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001480
bungeman@google.comb29c8832011-10-10 13:19:10 +00001481 const SkPaint::Style style = shaderPaint.getStyle();
1482 const bool hasFill = SkPaint::kFill_Style == style
1483 || SkPaint::kStrokeAndFill_Style == style;
1484 const bool hasStroke = SkPaint::kStroke_Style == style
1485 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001486
bungeman@google.comb29c8832011-10-10 13:19:10 +00001487 //TODO(bungeman): use dictionaries and lookups.
1488 if (hasFill) {
1489 *fill = TRUE;
1490 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1491 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1492 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1493 "Could not set fill for shaded path.");
1494 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001495
bungeman@google.comb29c8832011-10-10 13:19:10 +00001496 if (hasStroke) {
1497 *stroke = TRUE;
1498 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1499 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1500 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1501 "Could not set stroke brush for shaded path.");
1502 HRM(shadedPath->SetStrokeThickness(
1503 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1504 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001505
bungeman@google.comb29c8832011-10-10 13:19:10 +00001506 if (0 == shaderPaint.getStrokeWidth()) {
1507 //XPS hair width is a hack. (XPS Spec 11.6.12).
1508 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1509 HRM(shadedPath->GetStrokeDashes(&dashes),
1510 "Could not set dashes for shaded path.");
1511 XPS_DASH dash;
1512 dash.length = 1.0;
1513 dash.gap = 0.0;
1514 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1515 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1516 "Could not set dash offset for shaded path.");
1517 }
1518 }
1519 return S_OK;
1520}
1521
1522void SkXPSDevice::drawPath(const SkDraw& d,
1523 const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001524 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001525 const SkMatrix* prePathMatrix,
1526 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001527 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1528
bungeman@google.comb29c8832011-10-10 13:19:10 +00001529 // nothing to draw
reed1e7f5e72016-04-27 07:49:17 -07001530 if (d.fRC->isEmpty() ||
halcanary96fcdcc2015-08-27 07:41:13 -07001531 (paint->getAlpha() == 0 && paint->getXfermode() == nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001532 return;
1533 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001534
bungeman@google.comb29c8832011-10-10 13:19:10 +00001535 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001536 const bool paintHasPathEffect = paint->getPathEffect()
1537 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001538
bungeman@google.comb29c8832011-10-10 13:19:10 +00001539 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1540 SkMatrix matrix = *d.fMatrix;
1541 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1542 if (prePathMatrix) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001543 if (paintHasPathEffect || paint->getRasterizer()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001544 if (!pathIsMutable) {
1545 skeletalPath = &modifiedPath;
1546 pathIsMutable = true;
1547 }
1548 platonicPath.transform(*prePathMatrix, skeletalPath);
1549 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001550 matrix.preConcat(*prePathMatrix);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001551 }
1552 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001553
bungeman@google.comb29c8832011-10-10 13:19:10 +00001554 //Apply path effect [Skeletal-path -> Fillable-path].
1555 SkPath* fillablePath = skeletalPath;
1556 if (paintHasPathEffect) {
1557 if (!pathIsMutable) {
1558 fillablePath = &modifiedPath;
1559 pathIsMutable = true;
1560 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001561 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001562
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001563 SkPaint* writablePaint = paint.writable();
halcanary96fcdcc2015-08-27 07:41:13 -07001564 writablePaint->setPathEffect(nullptr);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001565 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001566 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001567 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001568 writablePaint->setStyle(SkPaint::kStroke_Style);
1569 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001570 }
1571 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001572
bungeman@google.comb29c8832011-10-10 13:19:10 +00001573 //Create the shaded path. This will be the path which is painted.
1574 SkTScopedComPtr<IXpsOMPath> shadedPath;
1575 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1576 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001577
bungeman@google.comb29c8832011-10-10 13:19:10 +00001578 //Create the geometry for the shaded path.
1579 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1580 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1581 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001582
bungeman@google.comb29c8832011-10-10 13:19:10 +00001583 //Add the geometry to the shaded path.
1584 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1585 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001586
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001587 SkRasterizer* rasterizer = paint->getRasterizer();
1588 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001589
1590 //Determine if we will draw or shade and mask.
1591 if (rasterizer || filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001592 if (paint->getStyle() != SkPaint::kFill_Style) {
1593 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001594 }
1595 }
1596
bungeman@google.comb29c8832011-10-10 13:19:10 +00001597 //Set the brushes.
1598 BOOL fill;
1599 BOOL stroke;
1600 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001601 *paint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001602 *d.fMatrix,
1603 &fill,
1604 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001605
bungeman@google.comb29c8832011-10-10 13:19:10 +00001606 //Rasterizer
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001607 if (rasterizer) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001608 SkIRect clipIRect;
1609 SkVector ppuScale;
1610 this->convertToPpm(filter,
1611 &matrix,
1612 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001613 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001614 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001615
halcanary96fcdcc2015-08-27 07:41:13 -07001616 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001617
bungeman@google.comb29c8832011-10-10 13:19:10 +00001618 //[Fillable-path -> Mask]
1619 SkMask rasteredMask;
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001620 if (rasterizer->rasterize(
bungeman@google.comb29c8832011-10-10 13:19:10 +00001621 *fillablePath,
1622 matrix,
1623 &clipIRect,
1624 filter, //just to compute how much to draw.
1625 &rasteredMask,
1626 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001627
bungeman@google.comb29c8832011-10-10 13:19:10 +00001628 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1629 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001630
bungeman@google.comb29c8832011-10-10 13:19:10 +00001631 //[Mask -> Mask]
1632 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001633 if (filter && filter->filterMask(&filteredMask, *mask, *d.fMatrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001634 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001635 }
1636 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001637
bungeman@google.comb29c8832011-10-10 13:19:10 +00001638 //Draw mask.
1639 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1640 }
1641 return;
1642 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001643
bungeman@google.comb29c8832011-10-10 13:19:10 +00001644 //Mask filter
1645 if (filter) {
1646 SkIRect clipIRect;
1647 SkVector ppuScale;
1648 this->convertToPpm(filter,
1649 &matrix,
1650 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001651 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001652 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001653
bungeman@google.comb29c8832011-10-10 13:19:10 +00001654 //[Fillable-path -> Pixel-path]
1655 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1656 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001657
halcanary96fcdcc2015-08-27 07:41:13 -07001658 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001659
bsalomon055e1922016-05-06 07:22:58 -07001660 SkASSERT(SkPaint::kFill_Style == paint->getStyle() ||
1661 (SkPaint::kStroke_Style == paint->getStyle() && 0 == paint->getStrokeWidth()));
1662 SkStrokeRec::InitStyle style = (SkPaint::kFill_Style == paint->getStyle())
1663 ? SkStrokeRec::kFill_InitStyle
1664 : SkStrokeRec::kHairline_InitStyle;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001665 //[Pixel-path -> Mask]
1666 SkMask rasteredMask;
1667 if (SkDraw::DrawToMask(
1668 *pixelPath,
1669 &clipIRect,
1670 filter, //just to compute how much to draw.
1671 &matrix,
1672 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001673 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon055e1922016-05-06 07:22:58 -07001674 style)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001675
bungeman@google.comb29c8832011-10-10 13:19:10 +00001676 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1677 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001678
bungeman@google.comb29c8832011-10-10 13:19:10 +00001679 //[Mask -> Mask]
1680 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001681 if (filter->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001682 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001683 }
1684 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001685
bungeman@google.comb29c8832011-10-10 13:19:10 +00001686 //Draw mask.
1687 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1688 }
1689 return;
1690 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001691
bungeman@google.comb29c8832011-10-10 13:19:10 +00001692 //Get the figures from the shaded geometry.
1693 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1694 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1695 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001696
bungeman@google.comb29c8832011-10-10 13:19:10 +00001697 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001698
bungeman@google.comb29c8832011-10-10 13:19:10 +00001699 //Set the fill rule.
bungeman76db31a2014-08-25 07:31:53 -07001700 SkPath* xpsCompatiblePath = fillablePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001701 XPS_FILL_RULE xpsFillRule;
bungeman76db31a2014-08-25 07:31:53 -07001702 switch (fillablePath->getFillType()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001703 case SkPath::kWinding_FillType:
1704 xpsFillRule = XPS_FILL_RULE_NONZERO;
1705 break;
1706 case SkPath::kEvenOdd_FillType:
1707 xpsFillRule = XPS_FILL_RULE_EVENODD;
1708 break;
1709 case SkPath::kInverseWinding_FillType: {
bungeman76db31a2014-08-25 07:31:53 -07001710 //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
1711 if (!pathIsMutable) {
1712 xpsCompatiblePath = &modifiedPath;
1713 pathIsMutable = true;
1714 }
1715 if (!Simplify(*fillablePath, xpsCompatiblePath)) {
1716 SkDEBUGF(("Could not simplify inverse winding path."));
1717 return;
1718 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001719 }
bungeman76db31a2014-08-25 07:31:53 -07001720 // The xpsCompatiblePath is noW inverse even odd, so fall through.
bungeman@google.comb29c8832011-10-10 13:19:10 +00001721 case SkPath::kInverseEvenOdd_FillType: {
1722 const SkRect universe = SkRect::MakeLTRB(
1723 0, 0,
1724 this->fCurrentCanvasSize.fWidth,
1725 this->fCurrentCanvasSize.fHeight);
1726 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1727 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1728 HRVM(shadedFigures->Append(addOneFigure.get()),
1729 "Could not add even-odd flip figure to shaded path.");
1730 xpsTransformsPath = false;
1731 xpsFillRule = XPS_FILL_RULE_EVENODD;
1732 break;
1733 }
1734 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001735 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001736 }
1737 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1738 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001739
bungeman@google.comb29c8832011-10-10 13:19:10 +00001740 //Create the XPS transform, if possible.
1741 if (xpsTransformsPath) {
1742 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1743 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001744
bungeman@google.comb29c8832011-10-10 13:19:10 +00001745 if (xpsTransform.get()) {
1746 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1747 "Could not set transform on shaded path.");
1748 } else {
1749 xpsTransformsPath = false;
1750 }
1751 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001752
bungeman76db31a2014-08-25 07:31:53 -07001753 SkPath* devicePath = xpsCompatiblePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001754 if (!xpsTransformsPath) {
1755 //[Fillable-path -> Device-path]
bungeman76db31a2014-08-25 07:31:53 -07001756 devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
1757 xpsCompatiblePath->transform(matrix, devicePath);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001758 }
1759 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1760 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001761
bungeman@google.comb29c8832011-10-10 13:19:10 +00001762 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001763
bungeman@google.comb29c8832011-10-10 13:19:10 +00001764 //Add the path to the active visual collection.
1765 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1766 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1767 "Could not get current visuals for shaded path.");
1768 HRVM(currentVisuals->Append(shadedPath.get()),
1769 "Could not add shaded path to current visuals.");
1770}
1771
1772HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1773 SkPath clipPath;
reed1e7f5e72016-04-27 07:49:17 -07001774 if (d.fRC->isBW()) {
1775 SkAssertResult(d.fRC->bwRgn().getBoundaryPath(&clipPath));
1776 } else {
1777 // Don't have a way to turn a AAClip into a path, so we just use the bounds.
1778 // TODO: consider using fClipStack instead?
1779 clipPath.addRect(SkRect::Make(d.fRC->getBounds()));
1780 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001781
bungeman@google.comb29c8832011-10-10 13:19:10 +00001782 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1783}
1784HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1785 const SkPath& clipPath,
1786 XPS_FILL_RULE fillRule) {
1787 //Create the geometry.
1788 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1789 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1790 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001791
bungeman@google.comb29c8832011-10-10 13:19:10 +00001792 //Get the figure collection of the geometry.
1793 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1794 HRM(clipGeometry->GetFigures(&clipFigures),
1795 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001796
bungeman@google.comb29c8832011-10-10 13:19:10 +00001797 //Create the figures into the geometry.
1798 HR(this->addXpsPathGeometry(
1799 clipFigures.get(),
1800 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001801
bungeman@google.comb29c8832011-10-10 13:19:10 +00001802 HRM(clipGeometry->SetFillRule(fillRule),
1803 "Could not set fill rule.");
1804 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1805 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001806
bungeman@google.comb29c8832011-10-10 13:19:10 +00001807 return S_OK;
1808}
1809
1810void SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001811 const SkMatrix& matrix, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001812 if (d.fRC->isEmpty()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001813 return;
1814 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001815
bungeman@google.comb29c8832011-10-10 13:19:10 +00001816 SkIRect srcRect;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001817 srcRect.set(0, 0, bitmap.width(), bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001818
bungeman@google.comb29c8832011-10-10 13:19:10 +00001819 //Create the new shaded path.
1820 SkTScopedComPtr<IXpsOMPath> shadedPath;
1821 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1822 "Could not create path for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001823
bungeman@google.comb29c8832011-10-10 13:19:10 +00001824 //Create the shaded geometry.
1825 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1826 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1827 "Could not create geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001828
bungeman@google.comb29c8832011-10-10 13:19:10 +00001829 //Add the shaded geometry to the shaded path.
1830 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1831 "Could not set the geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001832
bungeman@google.comb29c8832011-10-10 13:19:10 +00001833 //Get the shaded figures from the shaded geometry.
1834 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1835 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1836 "Could not get the figures for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001837
bungeman@google.comb29c8832011-10-10 13:19:10 +00001838 SkMatrix transform = matrix;
1839 transform.postConcat(*d.fMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001840
bungeman@google.comb29c8832011-10-10 13:19:10 +00001841 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1842 HRV(this->createXpsTransform(transform, &xpsTransform));
1843 if (xpsTransform.get()) {
1844 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1845 "Could not set transform for bitmap.");
1846 } else {
1847 //TODO: perspective that bitmap!
1848 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001849
bungeman@google.comb29c8832011-10-10 13:19:10 +00001850 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
bsalomon49f085d2014-09-05 13:34:00 -07001851 if (xpsTransform.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001852 const SkShader::TileMode xy[2] = {
1853 SkShader::kClamp_TileMode,
1854 SkShader::kClamp_TileMode,
1855 };
1856 SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001857 HRV(this->createXpsImageBrush(bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001858 transform,
1859 xy,
1860 paint.getAlpha(),
1861 &xpsImageBrush));
1862 HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
1863 "Could not set bitmap brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001864
bungeman@google.comb29c8832011-10-10 13:19:10 +00001865 const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
1866 SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1867 HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
1868 }
1869 HRVM(shadedFigures->Append(rectFigure.get()),
1870 "Could not add bitmap figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001871
bungeman@google.comb29c8832011-10-10 13:19:10 +00001872 //Get the current visual collection and add the shaded path to it.
1873 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1874 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1875 "Could not get current visuals for bitmap");
1876 HRVM(currentVisuals->Append(shadedPath.get()),
1877 "Could not add bitmap to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001878
bungeman@google.comb29c8832011-10-10 13:19:10 +00001879 HRV(this->clip(shadedPath.get(), d));
1880}
1881
1882void SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
1883 int x, int y,
1884 const SkPaint& paint) {
1885 //TODO: override this for XPS
1886 SkDEBUGF(("XPS drawSprite not yet implemented."));
1887}
1888
1889HRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
1890 TypefaceUse** typefaceUse) {
reed@google.com398de9a2013-03-21 21:43:51 +00001891 SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001892
bungeman@google.comb29c8832011-10-10 13:19:10 +00001893 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00001894 const SkFontID typefaceID = typeface->uniqueID();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001895 if (!this->fTypefaces.empty()) {
1896 TypefaceUse* current = &this->fTypefaces.front();
1897 const TypefaceUse* last = &this->fTypefaces.back();
1898 for (; current <= last; ++current) {
1899 if (current->typefaceId == typefaceID) {
1900 *typefaceUse = current;
1901 return S_OK;
1902 }
1903 }
1904 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001905
bungeman@google.comb29c8832011-10-10 13:19:10 +00001906 //TODO: create glyph only fonts
1907 //and let the host deal with what kind of font we're looking at.
1908 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001909
bungeman@google.comb29c8832011-10-10 13:19:10 +00001910 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001911 int ttcIndex;
1912 SkStream* fontData = typeface->openStream(&ttcIndex);
bungeman@google.com635091f2013-10-01 15:03:18 +00001913 //TODO: cannot handle FON fonts.
bungeman@google.com8cddc8d2012-01-03 22:36:33 +00001914 HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001915 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001916
bungeman@google.comb29c8832011-10-10 13:19:10 +00001917 const size_t size =
1918 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1919 wchar_t buffer[size];
1920 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -04001921 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001922 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001923
bungeman@google.comb29c8832011-10-10 13:19:10 +00001924 SkTScopedComPtr<IOpcPartUri> partUri;
1925 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1926 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001927
bungeman@google.comb29c8832011-10-10 13:19:10 +00001928 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1929 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1930 embedding,
1931 partUri.get(),
1932 FALSE,
1933 &xpsFontResource),
1934 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001935
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001936 //TODO: change openStream to return -1 for non-ttc, get rid of this.
1937 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1938 bool isTTC = (data &&
1939 fontData->getLength() >= sizeof(SkTTCFHeader) &&
1940 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1941
bungeman@google.comb29c8832011-10-10 13:19:10 +00001942 TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
1943 newTypefaceUse.typefaceId = typefaceID;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001944 newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001945 newTypefaceUse.fontData = fontData;
1946 newTypefaceUse.xpsFont = xpsFontResource.release();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001947
robertphillips8e0c1502015-07-07 10:28:43 -07001948 SkAutoGlyphCache agc(paint, &this->surfaceProps(), &SkMatrix::I());
bungeman@google.comb29c8832011-10-10 13:19:10 +00001949 SkGlyphCache* glyphCache = agc.getCache();
1950 unsigned int glyphCount = glyphCache->getGlyphCount();
1951 newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001952
bungeman@google.comb29c8832011-10-10 13:19:10 +00001953 *typefaceUse = &newTypefaceUse;
1954 return S_OK;
1955}
1956
1957HRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
1958 IXpsOMObjectFactory* xpsFactory,
1959 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001960 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001961 LPCWSTR text,
1962 XPS_GLYPH_INDEX* xpsGlyphs,
1963 UINT32 xpsGlyphsLen,
1964 XPS_POINT *origin,
1965 FLOAT fontSize,
1966 XPS_STYLE_SIMULATION sims,
1967 const SkMatrix& transform,
1968 const SkPaint& paint) {
1969 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001970 HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
1971 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001972
bungeman@google.comb29c8832011-10-10 13:19:10 +00001973 //XPS uses affine transformations for everything...
1974 //...except positioning text.
1975 bool useCanvasForClip;
1976 if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
1977 origin->x += SkScalarToFLOAT(transform.getTranslateX());
1978 origin->y += SkScalarToFLOAT(transform.getTranslateY());
1979 useCanvasForClip = false;
1980 } else {
1981 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1982 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1983 if (xpsMatrixToUse.get()) {
1984 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1985 "Could not set transform matrix.");
1986 useCanvasForClip = true;
1987 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00001988 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001989 useCanvasForClip = false;
1990 }
1991 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001992
bungeman@google.comb29c8832011-10-10 13:19:10 +00001993 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1994 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001995
bsalomon49f085d2014-09-05 13:34:00 -07001996 if (text) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001997 HRM(glyphsEditor->SetUnicodeString(text),
1998 "Could not set unicode string.");
1999 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002000
bsalomon49f085d2014-09-05 13:34:00 -07002001 if (xpsGlyphs) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002002 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
2003 "Could not set glyphs.");
2004 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002005
bungeman@google.comb29c8832011-10-10 13:19:10 +00002006 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002007
bungeman@google.comb29c8832011-10-10 13:19:10 +00002008 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
2009 HR(this->createXpsBrush(
2010 paint,
2011 &xpsFillBrush,
halcanary96fcdcc2015-08-27 07:41:13 -07002012 useCanvasForClip ? nullptr : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002013
bungeman@google.comb29c8832011-10-10 13:19:10 +00002014 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
2015 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002016
bungeman@google.comb29c8832011-10-10 13:19:10 +00002017 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002018
bungeman@google.comb29c8832011-10-10 13:19:10 +00002019 HRM(glyphs->SetFontRenderingEmSize(fontSize),
2020 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002021
bungeman@google.comb29c8832011-10-10 13:19:10 +00002022 HRM(glyphs->SetStyleSimulations(sims),
2023 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002024
bungeman@google.comb29c8832011-10-10 13:19:10 +00002025 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2026 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002027
bungeman@google.comb29c8832011-10-10 13:19:10 +00002028 if (!useCanvasForClip) {
2029 HR(this->clip(glyphs.get(), d));
2030 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2031 } else {
2032 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2033 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2034 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002035
bungeman@google.comb29c8832011-10-10 13:19:10 +00002036 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2037 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2038 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002039
bungeman@google.comb29c8832011-10-10 13:19:10 +00002040 HRM(glyphCanvasVisuals->Append(glyphs.get()),
2041 "Could not add glyphs to page.");
2042 HR(this->clip(glyphCanvas.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002043
bungeman@google.comb29c8832011-10-10 13:19:10 +00002044 HRM(visuals->Append(glyphCanvas.get()),
2045 "Could not add glyph canvas to page.");
2046 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002047
bungeman@google.comb29c8832011-10-10 13:19:10 +00002048 return S_OK;
2049}
2050
herbbda26432015-11-24 08:37:01 -08002051static int num_glyph_guess(SkPaint::TextEncoding encoding, const void* text, size_t byteLength) {
2052 switch (encoding) {
2053 case SkPaint::kUTF8_TextEncoding:
2054 return SkUTF8_CountUnichars(static_cast<const char *>(text), byteLength);
2055 case SkPaint::kUTF16_TextEncoding:
2056 return SkUTF16_CountUnichars(static_cast<const uint16_t *>(text), SkToInt(byteLength));
2057 case SkPaint::kGlyphID_TextEncoding:
2058 return SkToInt(byteLength / 2);
2059 default:
djsollenf2b340f2016-01-29 08:51:04 -08002060 SK_ABORT("Invalid Text Encoding");
bungeman@google.comb29c8832011-10-10 13:19:10 +00002061 }
herbbda26432015-11-24 08:37:01 -08002062 return 0;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002063}
2064
2065static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2066 const SkPaint::Style style = paint.getStyle();
2067 return matrix.hasPerspective()
2068 || SkPaint::kStroke_Style == style
2069 || SkPaint::kStrokeAndFill_Style == style
2070 || paint.getMaskFilter()
2071 || paint.getRasterizer()
2072 ;
2073}
2074
herbbda26432015-11-24 08:37:01 -08002075typedef SkTDArray<XPS_GLYPH_INDEX> GlyphRun;
2076
2077class ProcessOneGlyph {
2078public:
2079 ProcessOneGlyph(FLOAT centemPerUnit, SkBitSet* glyphUse, GlyphRun* xpsGlyphs)
2080 : fCentemPerUnit(centemPerUnit)
2081 , fGlyphUse(glyphUse)
2082 , fXpsGlyphs(xpsGlyphs) { }
2083
2084 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint) {
2085 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
2086
2087 SkScalar x = position.fX;
2088 SkScalar y = position.fY;
2089
2090 XPS_GLYPH_INDEX* xpsGlyph = fXpsGlyphs->append();
2091 uint16_t glyphID = glyph.getGlyphID();
2092 fGlyphUse->setBit(glyphID, true);
2093 xpsGlyph->index = glyphID;
2094 if (1 == fXpsGlyphs->count()) {
2095 xpsGlyph->advanceWidth = 0.0f;
2096 xpsGlyph->horizontalOffset = SkScalarToFloat(x) * fCentemPerUnit;
2097 xpsGlyph->verticalOffset = SkScalarToFloat(y) * -fCentemPerUnit;
2098 }
2099 else {
2100 const XPS_GLYPH_INDEX& first = (*fXpsGlyphs)[0];
2101 xpsGlyph->advanceWidth = 0.0f;
2102 xpsGlyph->horizontalOffset = (SkScalarToFloat(x) * fCentemPerUnit)
2103 - first.horizontalOffset;
2104 xpsGlyph->verticalOffset = (SkScalarToFloat(y) * -fCentemPerUnit)
2105 - first.verticalOffset;
2106 }
2107 }
2108
2109private:
2110 /** [in] Advance width and offsets for glyphs measured in
2111 hundredths of the font em size (XPS Spec 5.1.3). */
2112 const FLOAT fCentemPerUnit;
2113 /** [in,out] The accumulated glyphs used in the current typeface. */
2114 SkBitSet* const fGlyphUse;
2115 /** [out] The glyphs to draw. */
2116 GlyphRun* const fXpsGlyphs;
2117};
2118
bungeman@google.comb29c8832011-10-10 13:19:10 +00002119void SkXPSDevice::drawText(const SkDraw& d,
2120 const void* text, size_t byteLen,
2121 SkScalar x, SkScalar y,
2122 const SkPaint& paint) {
2123 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002124
bungeman@google.comb29c8832011-10-10 13:19:10 +00002125 if (text_must_be_pathed(paint, *d.fMatrix)) {
2126 SkPath path;
2127 paint.getTextPath(text, byteLen, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002128 this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002129 //TODO: add automation "text"
2130 return;
2131 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002132
bungeman@google.comb29c8832011-10-10 13:19:10 +00002133 TypefaceUse* typeface;
2134 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002135
herbbda26432015-11-24 08:37:01 -08002136 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002137
herbbda26432015-11-24 08:37:01 -08002138 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2139 SkGlyphCache* cache = autoCache.getCache();
reed@google.com089130c2011-12-12 21:52:18 +00002140
herbbda26432015-11-24 08:37:01 -08002141 // Advance width and offsets for glyphs measured in hundredths of the font em size
2142 // (XPS Spec 5.1.3).
2143 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2144 GlyphRun xpsGlyphs;
2145 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2146 static_cast<const char*>(text), byteLen));
2147
2148 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2149
2150 SkFindAndPlaceGlyph::ProcessText(
2151 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2152 SkPoint{ x, y }, matrix, paint.getTextAlign(), cache, processOneGlyph);
2153
2154 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002155 return;
2156 }
2157
bungeman@google.comb29c8832011-10-10 13:19:10 +00002158 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002159 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2160 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002161 };
herbbda26432015-11-24 08:37:01 -08002162 xpsGlyphs[0].horizontalOffset = 0.0f;
2163 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002164
bungeman@google.comb29c8832011-10-10 13:19:10 +00002165 HRV(AddGlyphs(d,
2166 this->fXpsFactory.get(),
2167 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002168 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002169 nullptr,
herbbda26432015-11-24 08:37:01 -08002170 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002171 &origin,
2172 SkScalarToFLOAT(paint.getTextSize()),
2173 XPS_STYLE_SIMULATION_NONE,
2174 *d.fMatrix,
2175 paint));
2176}
2177
2178void SkXPSDevice::drawPosText(const SkDraw& d,
2179 const void* text, size_t byteLen,
fmalita05c4a432014-09-29 06:29:53 -07002180 const SkScalar pos[], int scalarsPerPos,
2181 const SkPoint& offset, const SkPaint& paint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002182 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002183
bungeman@google.comb29c8832011-10-10 13:19:10 +00002184 if (text_must_be_pathed(paint, *d.fMatrix)) {
2185 SkPath path;
2186 //TODO: make this work, Draw currently does not handle as well.
2187 //paint.getTextPath(text, byteLength, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002188 //this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002189 //TODO: add automation "text"
2190 return;
2191 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002192
bungeman@google.comb29c8832011-10-10 13:19:10 +00002193 TypefaceUse* typeface;
2194 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002195
herbbda26432015-11-24 08:37:01 -08002196 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002197
herbbda26432015-11-24 08:37:01 -08002198 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2199 SkGlyphCache* cache = autoCache.getCache();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002200
herbbda26432015-11-24 08:37:01 -08002201 // Advance width and offsets for glyphs measured in hundredths of the font em size
2202 // (XPS Spec 5.1.3).
2203 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2204 GlyphRun xpsGlyphs;
2205 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2206 static_cast<const char*>(text), byteLen));
2207
2208 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2209
2210 SkFindAndPlaceGlyph::ProcessPosText(
2211 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2212 offset, matrix, pos, scalarsPerPos, paint.getTextAlign(), cache, processOneGlyph);
2213
2214 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002215 return;
2216 }
2217
bungeman@google.comb29c8832011-10-10 13:19:10 +00002218 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002219 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2220 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002221 };
herbbda26432015-11-24 08:37:01 -08002222 xpsGlyphs[0].horizontalOffset = 0.0f;
2223 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002224
bungeman@google.comb29c8832011-10-10 13:19:10 +00002225 HRV(AddGlyphs(d,
2226 this->fXpsFactory.get(),
2227 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002228 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002229 nullptr,
herbbda26432015-11-24 08:37:01 -08002230 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002231 &origin,
2232 SkScalarToFLOAT(paint.getTextSize()),
2233 XPS_STYLE_SIMULATION_NONE,
2234 *d.fMatrix,
2235 paint));
2236}
2237
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002238void SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002239 int x, int y,
2240 const SkPaint&) {
2241 SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002242
bungeman@google.comb29c8832011-10-10 13:19:10 +00002243 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2244 XPS_MATRIX rawTransform = {
2245 1.0f,
2246 0.0f,
2247 0.0f,
2248 1.0f,
2249 static_cast<FLOAT>(x),
2250 static_cast<FLOAT>(y),
2251 };
2252 HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2253 "Could not create layer transform.");
2254 HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2255 "Could not set layer transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002256
bungeman@google.comb29c8832011-10-10 13:19:10 +00002257 //Get the current visual collection and add the layer to it.
2258 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2259 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2260 "Could not get current visuals for layer.");
2261 HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2262 "Could not add layer to current visuals.");
2263}
2264
reed76033be2015-03-14 10:54:31 -07002265SkBaseDevice* SkXPSDevice::onCreateDevice(const CreateInfo& info, const SkPaint*) {
bungeman@google.com635091f2013-10-01 15:03:18 +00002266//Conditional for bug compatibility with PDF device.
2267#if 0
fmalita6987dca2014-11-13 08:33:37 -08002268 if (SkBaseDevice::kGeneral_Usage == info.fUsage) {
halcanary96fcdcc2015-08-27 07:41:13 -07002269 return nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002270 //To what stream do we write?
2271 //SkXPSDevice* dev = new SkXPSDevice(this);
2272 //SkSize s = SkSize::Make(width, height);
2273 //dev->BeginCanvas(s, s, SkMatrix::I());
2274 //return dev;
2275 }
bungeman@google.com635091f2013-10-01 15:03:18 +00002276#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +00002277 return new SkXPSDevice(this->fXpsFactory.get());
2278}
2279
mtklein1ee76512015-11-02 10:20:27 -08002280#endif//defined(SK_BUILD_FOR_WIN32)