blob: f2901316c28d9dcdc42d573a0023933e1fabeb81 [file] [log] [blame]
bungeman@google.comb29c8832011-10-10 13:19:10 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef UNICODE
9#define UNICODE
10#endif
11#ifndef _UNICODE
12#define _UNICODE
13#endif
14#include "SkTypes.h"
15#include <ObjBase.h>
16#include <XpsObjectModel.h>
17#include <T2EmbApi.h>
18#include <FontSub.h>
19
20#include "SkColor.h"
21#include "SkConstexprMath.h"
22#include "SkData.h"
23#include "SkDraw.h"
24#include "SkDrawProcs.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000025#include "SkEndian.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000026#include "SkFontHost.h"
27#include "SkGlyphCache.h"
28#include "SkHRESULT.h"
29#include "SkImageEncoder.h"
30#include "SkIStream.h"
31#include "SkMaskFilter.h"
32#include "SkPaint.h"
33#include "SkPoint.h"
34#include "SkRasterizer.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000035#include "SkSFNTHeader.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000036#include "SkShader.h"
37#include "SkSize.h"
38#include "SkStream.h"
39#include "SkTDArray.h"
40#include "SkTLazy.h"
41#include "SkTScopedComPtr.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000042#include "SkTTCFHeader.h"
reed@google.com398de9a2013-03-21 21:43:51 +000043#include "SkTypefacePriv.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000044#include "SkUtils.h"
45#include "SkXPSDevice.h"
46
47//Windows defines a FLOAT type,
48//make it clear when converting a scalar that this is what is wanted.
49#define SkScalarToFLOAT(n) SkScalarToFloat(n)
50
51//Dummy representation of a GUID from create_id.
52#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
53//Length of GUID representation from create_id, including NULL terminator.
54#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
55
56/**
57 Formats a GUID and places it into buffer.
58 buffer should have space for at least GUID_ID_LEN wide characters.
59 The string will always be wchar null terminated.
60 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
61 @return -1 if there was an error, > 0 if success.
62 */
63static int format_guid(const GUID& guid,
64 wchar_t* buffer, size_t bufferSize,
65 wchar_t sep = '-') {
66 SkASSERT(bufferSize >= GUID_ID_LEN);
67 return swprintf_s(buffer,
68 bufferSize,
69 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
70 guid.Data1,
71 sep,
72 guid.Data2,
73 sep,
74 guid.Data3,
75 sep,
76 guid.Data4[0],
77 guid.Data4[1],
78 sep,
79 guid.Data4[2],
80 guid.Data4[3],
81 guid.Data4[4],
82 guid.Data4[5],
83 guid.Data4[6],
84 guid.Data4[7]);
85}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000086
bungeman@google.comb29c8832011-10-10 13:19:10 +000087/**
88 Creates a GUID based id and places it into buffer.
89 buffer should have space for at least GUID_ID_LEN wide characters.
90 The string will always be wchar null terminated.
91 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
92 The string may begin with a digit,
93 and so may not be suitable as a bare resource key.
94 */
95static HRESULT create_id(wchar_t* buffer, size_t bufferSize,
96 wchar_t sep = '-') {
97 GUID guid = {};
98 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +000099
bungeman@google.comb29c8832011-10-10 13:19:10 +0000100 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
101 HRM(E_UNEXPECTED, "Could not format GUID into id.");
102 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000103
bungeman@google.comb29c8832011-10-10 13:19:10 +0000104 return S_OK;
105}
106
107static SkBitmap make_fake_bitmap(int width, int height) {
108 SkBitmap bitmap;
reed@google.com900ecf22014-02-20 20:55:37 +0000109 bitmap.setConfig(SkImageInfo::MakeUnknown(width, height));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000110 return bitmap;
111}
112
reed@google.com900ecf22014-02-20 20:55:37 +0000113// TODO: should inherit from SkBaseDevice instead of SkBitmapDevice...
bungeman@google.comb29c8832011-10-10 13:19:10 +0000114SkXPSDevice::SkXPSDevice()
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000115 : SkBitmapDevice(make_fake_bitmap(10000, 10000))
bungeman@google.comb29c8832011-10-10 13:19:10 +0000116 , fCurrentPage(0) {
117}
118
119SkXPSDevice::~SkXPSDevice() {
120}
121
122SkXPSDevice::TypefaceUse::TypefaceUse()
123 : typefaceId(0xffffffff)
124 , fontData(NULL)
125 , xpsFont(NULL)
126 , glyphsUsed(NULL) {
127}
128
129SkXPSDevice::TypefaceUse::~TypefaceUse() {
130 //xpsFont owns fontData ref
131 this->xpsFont->Release();
132 delete this->glyphsUsed;
133}
134
135bool SkXPSDevice::beginPortfolio(SkWStream* outputStream) {
136 if (!this->fAutoCo.succeeded()) return false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000137
bungeman@google.comb29c8832011-10-10 13:19:10 +0000138 //Create XPS Factory.
139 HRBM(CoCreateInstance(
140 CLSID_XpsOMObjectFactory,
141 NULL,
142 CLSCTX_INPROC_SERVER,
143 IID_PPV_ARGS(&this->fXpsFactory)),
144 "Could not create XPS factory.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000145
bungeman@google.comb29c8832011-10-10 13:19:10 +0000146 HRBM(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream),
147 "Could not convert SkStream to IStream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000148
bungeman@google.comb29c8832011-10-10 13:19:10 +0000149 return true;
150}
151
152bool SkXPSDevice::beginSheet(
153 const SkVector& unitsPerMeter,
154 const SkVector& pixelsPerMeter,
155 const SkSize& trimSize,
156 const SkRect* mediaBox,
157 const SkRect* bleedBox,
158 const SkRect* artBox,
159 const SkRect* cropBox) {
160 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000161
bungeman@google.comb29c8832011-10-10 13:19:10 +0000162 //For simplicity, just write everything out in geometry units,
163 //then have a base canvas do the scale to physical units.
164 this->fCurrentCanvasSize = trimSize;
165 this->fCurrentUnitsPerMeter = unitsPerMeter;
166 this->fCurrentPixelsPerMeter = pixelsPerMeter;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000167
bungeman@google.comb29c8832011-10-10 13:19:10 +0000168 this->fCurrentXpsCanvas.reset();
169 HRBM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
170 "Could not create base canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000171
bungeman@google.comb29c8832011-10-10 13:19:10 +0000172 return true;
173}
174
175HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
176 const unsigned int pageNum,
177 IXpsOMImageResource** image) {
178 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
179 HRM(CoCreateInstance(
180 CLSID_XpsOMThumbnailGenerator,
181 NULL,
182 CLSCTX_INPROC_SERVER,
183 IID_PPV_ARGS(&thumbnailGenerator)),
184 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000185
bungeman@google.comb29c8832011-10-10 13:19:10 +0000186 SkTScopedComPtr<IOpcPartUri> partUri;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000187 static const size_t size = SkTUMax<
bungeman@google.comb29c8832011-10-10 13:19:10 +0000188 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + SK_DIGITS_IN(pageNum),
189 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png")
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000190 >::value;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000191 wchar_t buffer[size];
192 if (pageNum > 0) {
193 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
194 } else {
195 wchar_t id[GUID_ID_LEN];
196 HR(create_id(id, GUID_ID_LEN));
197 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
198 }
199 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
200 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000201
bungeman@google.comb29c8832011-10-10 13:19:10 +0000202 HRM(thumbnailGenerator->GenerateThumbnail(page,
203 XPS_IMAGE_TYPE_PNG,
204 XPS_THUMBNAIL_SIZE_LARGE,
205 partUri.get(),
206 image),
207 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000208
bungeman@google.comb29c8832011-10-10 13:19:10 +0000209 return S_OK;
210}
211
212HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
213 IXpsOMPage** page) {
214 static const size_t size = SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
215 + SK_DIGITS_IN(fCurrentPage);
216 wchar_t buffer[size];
217 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
218 this->fCurrentPage);
219 SkTScopedComPtr<IOpcPartUri> partUri;
220 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
221 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000222
bungeman@google.comb29c8832011-10-10 13:19:10 +0000223 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
224 HRM(this->fXpsFactory->CreatePage(&pageSize,
225 L"und",
226 partUri.get(),
227 page),
228 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000229
bungeman@google.comb29c8832011-10-10 13:19:10 +0000230 return S_OK;
231}
232
233HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
234 //Create package writer.
235 {
236 SkTScopedComPtr<IOpcPartUri> partUri;
237 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
238 &partUri),
239 "Could not create document sequence part uri.");
240 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
241 this->fOutputStream.get(),
242 TRUE,
243 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
244 partUri.get(),
245 NULL,
246 image,
247 NULL,
248 NULL,
249 &this->fPackageWriter),
250 "Could not create package writer.");
251 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000252
bungeman@google.comb29c8832011-10-10 13:19:10 +0000253 //Begin the lone document.
254 {
255 SkTScopedComPtr<IOpcPartUri> partUri;
256 HRM(this->fXpsFactory->CreatePartUri(
257 L"/Documents/1/FixedDocument.fdoc",
258 &partUri),
259 "Could not create fixed document part uri.");
260 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
261 NULL,
262 NULL,
263 NULL,
264 NULL),
265 "Could not start document.");
266 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000267
bungeman@google.comb29c8832011-10-10 13:19:10 +0000268 return S_OK;
269}
270
271bool SkXPSDevice::endSheet() {
272 //XPS is fixed at 96dpi (XPS Spec 11.1).
273 static const float xpsDPI = 96.0f;
274 static const float inchesPerMeter = 10000.0f / 254.0f;
275 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
276 const float scaleX = targetUnitsPerMeter
277 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
278 const float scaleY = targetUnitsPerMeter
279 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000280
bungeman@google.comb29c8832011-10-10 13:19:10 +0000281 //Create the scale canvas.
282 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
283 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
284 "Could not create scale canvas.");
285 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
286 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
287 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000288
bungeman@google.comb29c8832011-10-10 13:19:10 +0000289 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
290 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
291 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
292 "Could not create geometry to physical transform.");
293 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
294 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000295
bungeman@google.comb29c8832011-10-10 13:19:10 +0000296 //Add the content canvas to the scale canvas.
297 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
298 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000299
bungeman@google.comb29c8832011-10-10 13:19:10 +0000300 //Create the page.
301 XPS_SIZE pageSize = {
302 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
303 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
304 };
305 SkTScopedComPtr<IXpsOMPage> page;
306 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000307
bungeman@google.comb29c8832011-10-10 13:19:10 +0000308 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
309 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000310
bungeman@google.comb29c8832011-10-10 13:19:10 +0000311 //Add the scale canvas to the page.
312 HRBM(pageVisuals->Append(scaleCanvas.get()),
313 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000314
bungeman@google.comb29c8832011-10-10 13:19:10 +0000315 //Create the package writer if it hasn't been created yet.
316 if (NULL == this->fPackageWriter.get()) {
317 SkTScopedComPtr<IXpsOMImageResource> image;
318 //Ignore return, thumbnail is completely optional.
319 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000320
bungeman@google.comb29c8832011-10-10 13:19:10 +0000321 HRB(this->initXpsDocumentWriter(image.get()));
322 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000323
bungeman@google.comb29c8832011-10-10 13:19:10 +0000324 HRBM(this->fPackageWriter->AddPage(page.get(),
325 &pageSize,
326 NULL,
327 NULL,
328 NULL,
329 NULL),
330 "Could not write the page.");
331 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000332
bungeman@google.comb29c8832011-10-10 13:19:10 +0000333 return true;
334}
335
336static HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
337 //CreateFontPackage wants unsigned short.
338 //Microsoft, Y U NO stdint.h?
339 SkTDArray<unsigned short> keepList;
340 current->glyphsUsed->exportTo(&keepList);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000341
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000342 int ttcCount = (current->ttcIndex + 1);
343
bungeman@google.comb29c8832011-10-10 13:19:10 +0000344 //The following are declared with the types required by CreateFontPackage.
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000345 unsigned char *fontPackageBufferRaw = NULL;
346 unsigned long fontPackageBufferSize;
347 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000348 unsigned long result = CreateFontPackage(
349 (unsigned char *) current->fontData->getMemoryBase(),
350 (unsigned long) current->fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000351 &fontPackageBufferRaw,
352 &fontPackageBufferSize,
353 &bytesWritten,
354 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
355 current->ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000356 TTFCFP_SUBSET,
357 0,
358 0,
359 0,
360 keepList.begin(),
361 keepList.count(),
362 sk_malloc_throw,
363 sk_realloc_throw,
364 sk_free,
365 NULL);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000366 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000367 if (result != NO_ERROR) {
368 SkDEBUGF(("CreateFontPackage Error %lu", result));
369 return E_UNEXPECTED;
370 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000371
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000372 // If it was originally a ttc, keep it a ttc.
373 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
374 size_t extra;
375 if (ttcCount > 0) {
376 // Create space for a ttc header.
377 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
378 fontPackageBuffer.realloc(bytesWritten + extra);
379 //overlap is certain, use memmove
380 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
381
382 // Write the ttc header.
383 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
384 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
385 ttcfHeader->version = SkTTCFHeader::version_1;
386 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
387 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
388 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
389 *offsetPtr = SkEndian_SwapBE32(extra);
390 }
391
392 // Fix up offsets in sfnt table entries.
393 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
394 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
395 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
396 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
397 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
398 tableDirectory->offset = SkEndian_SwapBE32(
399 SkEndian_SwapBE32(tableDirectory->offset) + extra);
400 }
401 } else {
402 extra = 0;
403 fontPackageBuffer.realloc(bytesWritten);
404 }
405
406 SkAutoTUnref<SkMemoryStream> newStream(new SkMemoryStream());
407 newStream->setMemoryOwned(fontPackageBuffer.detach(), bytesWritten + extra);
408
bungeman@google.comb29c8832011-10-10 13:19:10 +0000409 SkTScopedComPtr<IStream> newIStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000410 SkIStream::CreateFromSkStream(newStream.detach(), true, &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000411
bungeman@google.comb29c8832011-10-10 13:19:10 +0000412 XPS_FONT_EMBEDDING embedding;
413 HRM(current->xpsFont->GetEmbeddingOption(&embedding),
414 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000415
bungeman@google.comb29c8832011-10-10 13:19:10 +0000416 SkTScopedComPtr<IOpcPartUri> partUri;
417 HRM(current->xpsFont->GetPartName(&partUri),
418 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000419
bungeman@google.comb29c8832011-10-10 13:19:10 +0000420 HRM(current->xpsFont->SetContent(
421 newIStream.get(),
422 embedding,
423 partUri.get()),
424 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000425
bungeman@google.comb29c8832011-10-10 13:19:10 +0000426 return S_OK;
427}
428
429bool SkXPSDevice::endPortfolio() {
430 //Subset fonts
431 if (!this->fTypefaces.empty()) {
432 SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
433 const TypefaceUse* last = &this->fTypefaces.back();
434 for (; current <= last; ++current) {
435 //Ignore return for now, if it didn't subset, let it be.
436 subset_typeface(current);
437 }
438 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000439
bungeman@google.comb29c8832011-10-10 13:19:10 +0000440 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000441
bungeman@google.comb29c8832011-10-10 13:19:10 +0000442 return true;
443}
444
445static XPS_COLOR xps_color(const SkColor skColor) {
446 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
447 XPS_COLOR xpsColor;
448 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
449 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
450 xpsColor.value.sRGB.red = SkColorGetR(skColor);
451 xpsColor.value.sRGB.green = SkColorGetG(skColor);
452 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000453
bungeman@google.comb29c8832011-10-10 13:19:10 +0000454 return xpsColor;
455}
456
457static XPS_POINT xps_point(const SkPoint& point) {
458 XPS_POINT xpsPoint = {
459 SkScalarToFLOAT(point.fX),
460 SkScalarToFLOAT(point.fY),
461 };
462 return xpsPoint;
463}
464
465static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
466 SkPoint skTransformedPoint;
467 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
468 return xps_point(skTransformedPoint);
469}
470
471static XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
472 switch (tileMode) {
473 case SkShader::kClamp_TileMode:
474 return XPS_SPREAD_METHOD_PAD;
475 case SkShader::kRepeat_TileMode:
476 return XPS_SPREAD_METHOD_REPEAT;
477 case SkShader::kMirror_TileMode:
478 return XPS_SPREAD_METHOD_REFLECT;
479 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000480 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000481 }
482 return XPS_SPREAD_METHOD_PAD;
483}
484
485static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
486 const SkPoint& start, const SkPoint& end,
487 const SkMatrix& transform) {
488 SkPoint startTransformed;
489 transform.mapXY(start.fX, start.fY, &startTransformed);
490 SkPoint endTransformed;
491 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000492
bungeman@google.comb29c8832011-10-10 13:19:10 +0000493 //Manhattan distance between transformed start and end.
494 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
495 + (endTransformed.fY - startTransformed.fY);
496 if (SkScalarNearlyZero(startToEnd)) {
497 for (int i = 0; i < numOffsets; ++i) {
498 stopOffsets[i] = 0;
499 }
500 return;
501 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000502
bungeman@google.comb29c8832011-10-10 13:19:10 +0000503 for (int i = 0; i < numOffsets; ++i) {
504 SkPoint stop;
505 stop.fX = SkScalarMul(end.fX - start.fX, stopOffsets[i]);
506 stop.fY = SkScalarMul(end.fY - start.fY, stopOffsets[i]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000507
bungeman@google.comb29c8832011-10-10 13:19:10 +0000508 SkPoint stopTransformed;
509 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000510
bungeman@google.comb29c8832011-10-10 13:19:10 +0000511 //Manhattan distance between transformed start and stop.
512 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
513 + (stopTransformed.fY - startTransformed.fY);
514 //Percentage along transformed line.
515 stopOffsets[i] = SkScalarDiv(startToStop, startToEnd);
516 }
517}
518
519HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
520 IXpsOMMatrixTransform** xpsTransform) {
521 SkScalar affine[6];
522 if (!matrix.asAffine(affine)) {
523 *xpsTransform = NULL;
524 return S_FALSE;
525 }
526 XPS_MATRIX rawXpsMatrix = {
527 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
528 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
529 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
530 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
531 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
532 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
533 };
534 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
535 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000536
bungeman@google.comb29c8832011-10-10 13:19:10 +0000537 return S_OK;
538}
539
540HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
541 IXpsOMVisualCollection* visuals,
542 IXpsOMPath** path) {
543 SkTScopedComPtr<IXpsOMGeometry> geometry;
544 HRM(this->fXpsFactory->CreateGeometry(&geometry),
545 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000546
bungeman@google.comb29c8832011-10-10 13:19:10 +0000547 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
548 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
549 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000550
bungeman@google.comb29c8832011-10-10 13:19:10 +0000551 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
552 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000553
bungeman@google.comb29c8832011-10-10 13:19:10 +0000554 HRM(visuals->Append(*path), "Could not add path to visuals.");
555 return S_OK;
556}
557
558HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
559 const SkAlpha alpha,
560 IXpsOMBrush** xpsBrush) {
561 XPS_COLOR xpsColor = xps_color(skColor);
562 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
563 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, NULL, &solidBrush),
564 "Could not create solid color brush.");
565 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
566 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
567 return S_OK;
568}
569
570HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
571 const XPS_RECT& imageViewBox,
572 IXpsOMImageResource* image,
573 IXpsOMVisualCollection* visuals) {
574 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
575 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000576
bungeman@google.comb29c8832011-10-10 13:19:10 +0000577 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
578 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000579
bungeman@google.comb29c8832011-10-10 13:19:10 +0000580 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
581 HRM(this->fXpsFactory->CreateImageBrush(image,
582 &imageViewBox,
583 &imageViewBox,
584 &areaToFillBrush),
585 "Could not create brush for side of clamp.");
586 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
587 "Could not set tile mode for side of clamp.");
588 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
589 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000590
bungeman@google.comb29c8832011-10-10 13:19:10 +0000591 return S_OK;
592}
593
594HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
595 const SkColor color,
596 IXpsOMVisualCollection* visuals) {
597 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
598 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000599
bungeman@google.comb29c8832011-10-10 13:19:10 +0000600 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
601 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000602
bungeman@google.comb29c8832011-10-10 13:19:10 +0000603 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
604 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
605 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
606 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000607
bungeman@google.comb29c8832011-10-10 13:19:10 +0000608 return S_OK;
609}
610
611static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
612static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
613static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
614static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
615static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
616
617//TODO(bungeman): In the future, should skia add None,
618//handle None+Mirror and None+Repeat correctly.
619//None is currently an internal hack so masks don't repeat (None+None only).
620static XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
621 [SkShader::kTileModeCount+1] = {
622 //Clamp //Repeat //Mirror //None
623/*Clamp */ XTM_N, XTM_T, XTM_Y, XTM_N,
624/*Repeat*/ XTM_T, XTM_T, XTM_Y, XTM_N,
625/*Mirror*/ XTM_X, XTM_X, XTM_XY, XTM_X,
626/*None */ XTM_N, XTM_N, XTM_Y, XTM_N,
627};
628
629HRESULT SkXPSDevice::createXpsImageBrush(
630 const SkBitmap& bitmap,
631 const SkMatrix& localMatrix,
632 const SkShader::TileMode (&xy)[2],
633 const SkAlpha alpha,
634 IXpsOMTileBrush** xpsBrush) {
635 SkDynamicMemoryWStream write;
636 if (!SkImageEncoder::EncodeStream(&write, bitmap,
637 SkImageEncoder::kPNG_Type, 100)) {
638 HRM(E_FAIL, "Unable to encode bitmap as png.");
639 }
640 SkMemoryStream* read = new SkMemoryStream;
641 read->setData(write.copyToData())->unref();
642 SkTScopedComPtr<IStream> readWrapper;
643 HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
644 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000645
bungeman@google.comb29c8832011-10-10 13:19:10 +0000646 const size_t size =
647 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
648 wchar_t buffer[size];
649 wchar_t id[GUID_ID_LEN];
650 HR(create_id(id, GUID_ID_LEN));
651 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000652
bungeman@google.comb29c8832011-10-10 13:19:10 +0000653 SkTScopedComPtr<IOpcPartUri> imagePartUri;
654 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
655 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000656
bungeman@google.comb29c8832011-10-10 13:19:10 +0000657 SkTScopedComPtr<IXpsOMImageResource> imageResource;
658 HRM(this->fXpsFactory->CreateImageResource(
659 readWrapper.get(),
660 XPS_IMAGE_TYPE_PNG,
661 imagePartUri.get(),
662 &imageResource),
663 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000664
bungeman@google.comb29c8832011-10-10 13:19:10 +0000665 XPS_RECT bitmapRect = {
666 0.0, 0.0,
667 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
668 };
669 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
670 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
671 &bitmapRect, &bitmapRect,
672 &xpsImageBrush),
673 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000674
bungeman@google.comb29c8832011-10-10 13:19:10 +0000675 if (SkShader::kClamp_TileMode != xy[0] &&
676 SkShader::kClamp_TileMode != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000677
bungeman@google.comb29c8832011-10-10 13:19:10 +0000678 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
679 "Could not set image tile mode");
680 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
681 "Could not set image opacity.");
682 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
683 } else {
684 //TODO(bungeman): compute how big this really needs to be.
685 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
686 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
687 const SkScalar bWidth = SkIntToScalar(bitmap.width());
688 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000689
bungeman@google.comb29c8832011-10-10 13:19:10 +0000690 //create brush canvas
691 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
692 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
693 "Could not create image brush canvas.");
694 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
695 HRM(brushCanvas->GetVisuals(&brushVisuals),
696 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000697
bungeman@google.comb29c8832011-10-10 13:19:10 +0000698 //create central figure
699 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
700 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
701 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000702
bungeman@google.comb29c8832011-10-10 13:19:10 +0000703 SkTScopedComPtr<IXpsOMPath> centralPath;
704 HR(this->createPath(centralFigure.get(),
705 brushVisuals.get(),
706 &centralPath));
707 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
708 "Could not set tile mode for image brush central path.");
709 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
710 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000711
bungeman@google.comb29c8832011-10-10 13:19:10 +0000712 //add left/right
713 if (SkShader::kClamp_TileMode == xy[0]) {
714 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
715 XPS_RECT leftImageViewBox = {
716 0.0, 0.0,
717 1.0, static_cast<FLOAT>(bitmap.height()),
718 };
719 HR(this->sideOfClamp(leftArea, leftImageViewBox,
720 imageResource.get(),
721 brushVisuals.get()));
722
723 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
724 XPS_RECT rightImageViewBox = {
725 bitmap.width() - 1.0f, 0.0f,
726 1.0f, static_cast<FLOAT>(bitmap.height()),
727 };
728 HR(this->sideOfClamp(rightArea, rightImageViewBox,
729 imageResource.get(),
730 brushVisuals.get()));
731 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000732
bungeman@google.comb29c8832011-10-10 13:19:10 +0000733 //add top/bottom
734 if (SkShader::kClamp_TileMode == xy[1]) {
735 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
736 XPS_RECT topImageViewBox = {
737 0.0, 0.0,
738 static_cast<FLOAT>(bitmap.width()), 1.0,
739 };
740 HR(this->sideOfClamp(topArea, topImageViewBox,
741 imageResource.get(),
742 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000743
bungeman@google.comb29c8832011-10-10 13:19:10 +0000744 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
745 XPS_RECT bottomImageViewBox = {
746 0.0f, bitmap.height() - 1.0f,
747 static_cast<FLOAT>(bitmap.width()), 1.0f,
748 };
749 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
750 imageResource.get(),
751 brushVisuals.get()));
752 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000753
bungeman@google.comb29c8832011-10-10 13:19:10 +0000754 //add tl, tr, bl, br
755 if (SkShader::kClamp_TileMode == xy[0] &&
756 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000757
bungeman@google.comb29c8832011-10-10 13:19:10 +0000758 SkAutoLockPixels alp(bitmap);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000759
bungeman@google.comb29c8832011-10-10 13:19:10 +0000760 const SkColor tlColor = bitmap.getColor(0,0);
761 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
762 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000763
bungeman@google.comb29c8832011-10-10 13:19:10 +0000764 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
765 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
766 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000767
bungeman@google.comb29c8832011-10-10 13:19:10 +0000768 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
769 bitmap.height()-1);
770 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
771 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000772
bungeman@google.comb29c8832011-10-10 13:19:10 +0000773 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
774 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
775 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
776 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000777
bungeman@google.comb29c8832011-10-10 13:19:10 +0000778 //create visual brush from canvas
779 XPS_RECT bound = {};
780 if (SkShader::kClamp_TileMode == xy[0] &&
781 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000782
bungeman@google.comb29c8832011-10-10 13:19:10 +0000783 bound.x = BIG_F / -2;
784 bound.y = BIG_F / -2;
785 bound.width = BIG_F;
786 bound.height = BIG_F;
787 } else if (SkShader::kClamp_TileMode == xy[0]) {
788 bound.x = BIG_F / -2;
789 bound.y = 0.0f;
790 bound.width = BIG_F;
791 bound.height = static_cast<FLOAT>(bitmap.height());
792 } else if (SkShader::kClamp_TileMode == xy[1]) {
793 bound.x = 0;
794 bound.y = BIG_F / -2;
795 bound.width = static_cast<FLOAT>(bitmap.width());
796 bound.height = BIG_F;
797 }
798 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
799 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
800 "Could not create visual brush for image brush.");
801 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
802 "Could not set canvas on visual brush for image brush.");
803 HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
804 "Could not set tile mode on visual brush for image brush.");
805 HRM(clampBrush->SetOpacity(alpha / 255.0f),
806 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000807
bungeman@google.comb29c8832011-10-10 13:19:10 +0000808 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
809 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000810
bungeman@google.comb29c8832011-10-10 13:19:10 +0000811 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
812 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
813 if (NULL != xpsMatrixToUse.get()) {
814 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
815 "Could not set transform for image brush.");
816 } else {
817 //TODO(bungeman): perspective bitmaps in general.
818 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000819
bungeman@google.comb29c8832011-10-10 13:19:10 +0000820 return S_OK;
821}
822
823HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
824 const SkScalar offset,
825 IXpsOMGradientStop** xpsGradStop) {
826 XPS_COLOR gradStopXpsColor = xps_color(skColor);
827 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
828 NULL,
829 SkScalarToFLOAT(offset),
830 xpsGradStop),
831 "Could not create gradient stop.");
832 return S_OK;
833}
834
835HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
836 const SkAlpha alpha,
837 const SkMatrix& localMatrix,
838 IXpsOMMatrixTransform* xpsMatrix,
839 IXpsOMBrush** xpsBrush) {
840 XPS_POINT startPoint;
841 XPS_POINT endPoint;
842 if (NULL != xpsMatrix) {
843 startPoint = xps_point(info.fPoint[0]);
844 endPoint = xps_point(info.fPoint[1]);
845 } else {
846 transform_offsets(info.fColorOffsets, info.fColorCount,
847 info.fPoint[0], info.fPoint[1],
848 localMatrix);
849 startPoint = xps_point(info.fPoint[0], localMatrix);
850 endPoint = xps_point(info.fPoint[1], localMatrix);
851 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000852
bungeman@google.comb29c8832011-10-10 13:19:10 +0000853 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
854 HR(createXpsGradientStop(info.fColors[0],
855 info.fColorOffsets[0],
856 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000857
bungeman@google.comb29c8832011-10-10 13:19:10 +0000858 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
859 HR(createXpsGradientStop(info.fColors[1],
860 info.fColorOffsets[1],
861 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000862
bungeman@google.comb29c8832011-10-10 13:19:10 +0000863 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
864 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
865 gradStop1.get(),
866 &startPoint,
867 &endPoint,
868 &gradientBrush),
869 "Could not create linear gradient brush.");
870 if (NULL != xpsMatrix) {
871 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
872 "Could not set transform on linear gradient brush.");
873 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000874
bungeman@google.comb29c8832011-10-10 13:19:10 +0000875 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
876 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
877 "Could not get linear gradient stop collection.");
878 for (int i = 2; i < info.fColorCount; ++i) {
879 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
880 HR(createXpsGradientStop(info.fColors[i],
881 info.fColorOffsets[i],
882 &gradStop));
883 HRM(gradStopCollection->Append(gradStop.get()),
884 "Could not add linear gradient stop.");
885 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000886
bungeman@google.comb29c8832011-10-10 13:19:10 +0000887 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
888 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000889
bungeman@google.comb29c8832011-10-10 13:19:10 +0000890 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
891 "Could not set opacity of linear gradient brush.");
892 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000893
bungeman@google.comb29c8832011-10-10 13:19:10 +0000894 return S_OK;
895}
896
897HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
898 const SkAlpha alpha,
899 const SkMatrix& localMatrix,
900 IXpsOMMatrixTransform* xpsMatrix,
901 IXpsOMBrush** xpsBrush) {
902 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
903 HR(createXpsGradientStop(info.fColors[0],
904 info.fColorOffsets[0],
905 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000906
bungeman@google.comb29c8832011-10-10 13:19:10 +0000907 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
908 HR(createXpsGradientStop(info.fColors[1],
909 info.fColorOffsets[1],
910 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000911
bungeman@google.comb29c8832011-10-10 13:19:10 +0000912 //TODO: figure out how to fake better if not affine
913 XPS_POINT centerPoint;
914 XPS_POINT gradientOrigin;
915 XPS_SIZE radiiSizes;
916 if (NULL != xpsMatrix) {
917 centerPoint = xps_point(info.fPoint[0]);
918 gradientOrigin = xps_point(info.fPoint[0]);
919 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
920 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
921 } else {
922 centerPoint = xps_point(info.fPoint[0], localMatrix);
923 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000924
bungeman@google.comb29c8832011-10-10 13:19:10 +0000925 SkScalar radius = info.fRadius[0];
926 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000927
bungeman@google.comb29c8832011-10-10 13:19:10 +0000928 vec[0].set(radius, 0);
929 vec[1].set(0, radius);
930 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000931
bungeman@google.comb29c8832011-10-10 13:19:10 +0000932 SkScalar d0 = vec[0].length();
933 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000934
bungeman@google.comb29c8832011-10-10 13:19:10 +0000935 radiiSizes.width = SkScalarToFLOAT(d0);
936 radiiSizes.height = SkScalarToFLOAT(d1);
937 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000938
bungeman@google.comb29c8832011-10-10 13:19:10 +0000939 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
940 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
941 gradStop1.get(),
942 &centerPoint,
943 &gradientOrigin,
944 &radiiSizes,
945 &gradientBrush),
946 "Could not create radial gradient brush.");
947 if (NULL != xpsMatrix) {
948 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
949 "Could not set transform on radial gradient brush.");
950 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000951
bungeman@google.comb29c8832011-10-10 13:19:10 +0000952 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
953 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
954 "Could not get radial gradient stop collection.");
955 for (int i = 2; i < info.fColorCount; ++i) {
956 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
957 HR(createXpsGradientStop(info.fColors[i],
958 info.fColorOffsets[i],
959 &gradStop));
960 HRM(gradStopCollection->Append(gradStop.get()),
961 "Could not add radial gradient stop.");
962 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000963
bungeman@google.comb29c8832011-10-10 13:19:10 +0000964 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
965 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000966
bungeman@google.comb29c8832011-10-10 13:19:10 +0000967 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
968 "Could not set opacity of radial gradient brush.");
969 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000970
bungeman@google.comb29c8832011-10-10 13:19:10 +0000971 return S_OK;
972}
973
974HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
975 IXpsOMBrush** brush,
976 const SkMatrix* parentTransform) {
977 const SkShader *shader = skPaint.getShader();
978 if (NULL == shader) {
979 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
980 return S_OK;
981 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000982
bungeman@google.comb29c8832011-10-10 13:19:10 +0000983 //Gradient shaders.
984 SkShader::GradientInfo info;
985 info.fColorCount = 0;
986 info.fColors = NULL;
987 info.fColorOffsets = NULL;
988 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000989
bungeman@google.comb29c8832011-10-10 13:19:10 +0000990 if (SkShader::kNone_GradientType == gradientType) {
991 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000992
bungeman@google.comb29c8832011-10-10 13:19:10 +0000993 } else if (SkShader::kColor_GradientType == gradientType) {
994 SkASSERT(1 == info.fColorCount);
995 SkColor color;
996 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +0000997 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000998 SkAlpha alpha = skPaint.getAlpha();
999 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1000 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001001
bungeman@google.comb29c8832011-10-10 13:19:10 +00001002 } else {
1003 if (info.fColorCount == 0) {
1004 const SkColor color = skPaint.getColor();
1005 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
1006 return S_OK;
1007 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001008
bungeman@google.comb29c8832011-10-10 13:19:10 +00001009 SkAutoTArray<SkColor> colors(info.fColorCount);
1010 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1011 info.fColors = colors.get();
1012 info.fColorOffsets = colorOffsets.get();
1013 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001014
bungeman@google.comb29c8832011-10-10 13:19:10 +00001015 if (1 == info.fColorCount) {
1016 SkColor color = info.fColors[0];
1017 SkAlpha alpha = skPaint.getAlpha();
1018 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1019 return S_OK;
1020 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001021
bsalomon@google.comf94b3a42012-10-31 18:09:01 +00001022 SkMatrix localMatrix = shader->getLocalMatrix();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001023 if (NULL != parentTransform) {
1024 localMatrix.preConcat(*parentTransform);
1025 }
1026 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1027 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001028
bungeman@google.comb29c8832011-10-10 13:19:10 +00001029 if (SkShader::kLinear_GradientType == gradientType) {
1030 HR(this->createXpsLinearGradient(info,
1031 skPaint.getAlpha(),
1032 localMatrix,
1033 xpsMatrixToUse.get(),
1034 brush));
1035 return S_OK;
1036 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001037
bungeman@google.comb29c8832011-10-10 13:19:10 +00001038 if (SkShader::kRadial_GradientType == gradientType) {
1039 HR(this->createXpsRadialGradient(info,
1040 skPaint.getAlpha(),
1041 localMatrix,
1042 xpsMatrixToUse.get(),
1043 brush));
1044 return S_OK;
1045 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001046
reed@google.com83226972012-06-07 20:26:47 +00001047 if (SkShader::kRadial2_GradientType == gradientType ||
1048 SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001049 //simple if affine and one is 0, otherwise will have to fake
1050 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001051
bungeman@google.comb29c8832011-10-10 13:19:10 +00001052 if (SkShader::kSweep_GradientType == gradientType) {
1053 //have to fake
1054 }
1055 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001056
bungeman@google.comb29c8832011-10-10 13:19:10 +00001057 SkBitmap outTexture;
1058 SkMatrix outMatrix;
1059 SkShader::TileMode xy[2];
bungeman@google.comb29c8832011-10-10 13:19:10 +00001060 SkShader::BitmapType bitmapType = shader->asABitmap(&outTexture,
1061 &outMatrix,
rileya@google.com91f319c2012-07-25 17:18:31 +00001062 xy);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001063 switch (bitmapType) {
1064 case SkShader::kNone_BitmapType:
1065 break;
1066 case SkShader::kDefault_BitmapType: {
1067 //TODO: outMatrix??
bsalomon@google.comf94b3a42012-10-31 18:09:01 +00001068 SkMatrix localMatrix = shader->getLocalMatrix();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001069 if (NULL != parentTransform) {
1070 localMatrix.preConcat(*parentTransform);
1071 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001072
bungeman@google.comb29c8832011-10-10 13:19:10 +00001073 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1074 HR(this->createXpsImageBrush(outTexture,
1075 localMatrix,
1076 xy,
1077 skPaint.getAlpha(),
1078 &tileBrush));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001079
bungeman@google.comb29c8832011-10-10 13:19:10 +00001080 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001081
bungeman@google.comb29c8832011-10-10 13:19:10 +00001082 return S_OK;
1083 }
1084 case SkShader::kRadial_BitmapType:
1085 case SkShader::kSweep_BitmapType:
1086 case SkShader::kTwoPointRadial_BitmapType:
1087 default:
1088 break;
1089 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001090
bungeman@google.comb29c8832011-10-10 13:19:10 +00001091 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1092 return S_OK;
1093}
1094
1095static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1096 const bool zeroWidth = (0 == paint.getStrokeWidth());
1097 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001098
bungeman@google.comb29c8832011-10-10 13:19:10 +00001099 return paint.getPathEffect() ||
1100 paint.getMaskFilter() ||
1101 paint.getRasterizer() ||
1102 (stroke && (
1103 (matrix.hasPerspective() && !zeroWidth) ||
1104 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1105 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1106 paint.getStrokeMiter() < SK_ScalarSqrt2)
1107 ))
1108 ;
1109}
1110
1111HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1112 IXpsOMGeometryFigure** xpsRect) {
1113 const SkPoint points[4] = {
1114 { rect.fLeft, rect.fTop },
1115 { rect.fRight, rect.fTop },
1116 { rect.fRight, rect.fBottom },
1117 { rect.fLeft, rect.fBottom },
1118 };
1119 return this->createXpsQuad(points, stroke, fill, xpsRect);
1120}
1121HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1122 BOOL stroke, BOOL fill,
1123 IXpsOMGeometryFigure** xpsQuad) {
1124 // Define the start point.
1125 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001126
bungeman@google.comb29c8832011-10-10 13:19:10 +00001127 // Create the figure.
1128 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1129 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001130
bungeman@google.comb29c8832011-10-10 13:19:10 +00001131 // Define the type of each segment.
1132 XPS_SEGMENT_TYPE segmentTypes[3] = {
1133 XPS_SEGMENT_TYPE_LINE,
1134 XPS_SEGMENT_TYPE_LINE,
1135 XPS_SEGMENT_TYPE_LINE,
1136 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001137
bungeman@google.comb29c8832011-10-10 13:19:10 +00001138 // Define the x and y coordinates of each corner of the figure.
1139 FLOAT segmentData[6] = {
1140 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1141 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1142 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1143 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001144
bungeman@google.comb29c8832011-10-10 13:19:10 +00001145 // Describe if the segments are stroked.
1146 BOOL segmentStrokes[3] = {
1147 stroke, stroke, stroke,
1148 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001149
bungeman@google.comb29c8832011-10-10 13:19:10 +00001150 // Add the segment data to the figure.
1151 HRM((*xpsQuad)->SetSegments(
1152 3, 6,
1153 segmentTypes , segmentData, segmentStrokes),
1154 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001155
bungeman@google.comb29c8832011-10-10 13:19:10 +00001156 // Set the closed and filled properties of the figure.
1157 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1158 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001159
bungeman@google.comb29c8832011-10-10 13:19:10 +00001160 return S_OK;
1161}
1162
reed@google.com982cb872011-12-07 18:34:08 +00001163uint32_t SkXPSDevice::getDeviceCapabilities() {
1164 return kVector_Capability;
1165}
1166
bungeman@google.comb29c8832011-10-10 13:19:10 +00001167void SkXPSDevice::clear(SkColor color) {
1168 //TODO: override this for XPS
1169 SkDEBUGF(("XPS clear not yet implemented."));
1170}
1171
1172void SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1173 size_t count, const SkPoint points[],
1174 const SkPaint& paint) {
1175 //This will call back into the device to do the drawing.
1176 d.drawPoints(mode, count, points, paint, true);
1177}
1178
1179void SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1180 int vertexCount, const SkPoint verts[],
1181 const SkPoint texs[], const SkColor colors[],
1182 SkXfermode* xmode, const uint16_t indices[],
1183 int indexCount, const SkPaint& paint) {
1184 //TODO: override this for XPS
1185 SkDEBUGF(("XPS drawVertices not yet implemented."));
1186}
1187
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001188void SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001189 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001190
bungeman@google.comb29c8832011-10-10 13:19:10 +00001191 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001192 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1193 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1194 if (paint->getStyle() != SkPaint::kFill_Style) {
1195 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001196 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001197
bungeman@google.comb29c8832011-10-10 13:19:10 +00001198 this->internalDrawRect(d, r, false, *fillPaint);
1199}
1200
1201void SkXPSDevice::drawRect(const SkDraw& d,
1202 const SkRect& r,
1203 const SkPaint& paint) {
1204 this->internalDrawRect(d, r, true, paint);
1205}
1206
scroggo@google.comcac8d012013-11-12 17:10:02 +00001207void SkXPSDevice::drawRRect(const SkDraw& d,
1208 const SkRRect& rr,
1209 const SkPaint& paint) {
1210 SkPath path;
1211 path.addRRect(rr);
1212 this->drawPath(d, path, paint, NULL, true);
1213}
1214
bungeman@google.comb29c8832011-10-10 13:19:10 +00001215void SkXPSDevice::internalDrawRect(const SkDraw& d,
1216 const SkRect& r,
1217 bool transformRect,
1218 const SkPaint& paint) {
1219 //Exit early if there is nothing to draw.
1220 if (d.fClip->isEmpty() ||
1221 (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
1222 return;
1223 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001224
bungeman@google.comb29c8832011-10-10 13:19:10 +00001225 //Path the rect if we can't optimize it.
1226 if (rect_must_be_pathed(paint, *d.fMatrix)) {
1227 SkPath tmp;
1228 tmp.addRect(r);
1229 tmp.setFillType(SkPath::kWinding_FillType);
1230 this->drawPath(d, tmp, paint, NULL, true);
1231 return;
1232 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001233
bungeman@google.comb29c8832011-10-10 13:19:10 +00001234 //Create the shaded path.
1235 SkTScopedComPtr<IXpsOMPath> shadedPath;
1236 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1237 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001238
bungeman@google.comb29c8832011-10-10 13:19:10 +00001239 //Create the shaded geometry.
1240 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1241 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1242 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001243
bungeman@google.comb29c8832011-10-10 13:19:10 +00001244 //Add the geometry to the shaded path.
1245 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1246 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001247
bungeman@google.comb29c8832011-10-10 13:19:10 +00001248 //Set the brushes.
1249 BOOL fill = FALSE;
1250 BOOL stroke = FALSE;
1251 HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001252
bungeman@google.comb29c8832011-10-10 13:19:10 +00001253 bool xpsTransformsPath = true;
1254 //Transform the geometry.
1255 if (transformRect && xpsTransformsPath) {
1256 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1257 HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1258 if (xpsTransform.get()) {
1259 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1260 "Could not set transform for rect.");
1261 } else {
1262 xpsTransformsPath = false;
1263 }
1264 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001265
bungeman@google.comb29c8832011-10-10 13:19:10 +00001266 //Create the figure.
1267 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1268 {
1269 SkPoint points[4] = {
1270 { r.fLeft, r.fTop },
1271 { r.fLeft, r.fBottom },
1272 { r.fRight, r.fBottom },
1273 { r.fRight, r.fTop },
1274 };
1275 if (!xpsTransformsPath && transformRect) {
1276 d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1277 }
1278 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1279 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001280
bungeman@google.comb29c8832011-10-10 13:19:10 +00001281 //Get the figures of the shaded geometry.
1282 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1283 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1284 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001285
bungeman@google.comb29c8832011-10-10 13:19:10 +00001286 //Add the figure to the shaded geometry figures.
1287 HRVM(shadedFigures->Append(rectFigure.get()),
1288 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001289
bungeman@google.comb29c8832011-10-10 13:19:10 +00001290 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001291
bungeman@google.comb29c8832011-10-10 13:19:10 +00001292 //Add the shaded path to the current visuals.
1293 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1294 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1295 "Could not get current visuals for rect.");
1296 HRVM(currentVisuals->Append(shadedPath.get()),
1297 "Could not add rect to current visuals.");
1298}
1299
1300static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1301 const SkTDArray<BOOL>& segmentStrokes,
1302 const SkTDArray<FLOAT>& segmentData,
1303 BOOL stroke, BOOL fill,
1304 IXpsOMGeometryFigure* figure,
1305 IXpsOMGeometryFigureCollection* figures) {
1306 // Add the segment data to the figure.
1307 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1308 segmentTypes.begin() , segmentData.begin(),
1309 segmentStrokes.begin()),
1310 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001311
bungeman@google.comb29c8832011-10-10 13:19:10 +00001312 // Set the closed and filled properties of the figure.
1313 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1314 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001315
bungeman@google.comb29c8832011-10-10 13:19:10 +00001316 // Add the figure created above to this geometry.
1317 HRM(figures->Append(figure), "Could not add path to geometry.");
1318 return S_OK;
1319}
1320
1321HRESULT SkXPSDevice::addXpsPathGeometry(
1322 IXpsOMGeometryFigureCollection* xpsFigures,
1323 BOOL stroke, BOOL fill, const SkPath& path) {
1324 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1325 SkTDArray<BOOL> segmentStrokes;
1326 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001327
bungeman@google.comb29c8832011-10-10 13:19:10 +00001328 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1329 SkPath::Iter iter(path, true);
1330 SkPoint points[4];
1331 SkPath::Verb verb;
1332 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1333 switch (verb) {
1334 case SkPath::kMove_Verb: {
1335 if (NULL != xpsFigure.get()) {
1336 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1337 stroke, fill,
1338 xpsFigure.get() , xpsFigures));
1339 xpsFigure.reset();
1340 segmentTypes.rewind();
1341 segmentStrokes.rewind();
1342 segmentData.rewind();
1343 }
1344 // Define the start point.
1345 XPS_POINT startPoint = xps_point(points[0]);
1346 // Create the figure.
1347 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1348 &xpsFigure),
1349 "Could not create path geometry figure.");
1350 break;
1351 }
1352 case SkPath::kLine_Verb:
1353 if (iter.isCloseLine()) break; //ignore the line, auto-closed
1354 segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1355 segmentStrokes.push(stroke);
1356 segmentData.push(SkScalarToFLOAT(points[1].fX));
1357 segmentData.push(SkScalarToFLOAT(points[1].fY));
1358 break;
1359 case SkPath::kQuad_Verb:
1360 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1361 segmentStrokes.push(stroke);
1362 segmentData.push(SkScalarToFLOAT(points[1].fX));
1363 segmentData.push(SkScalarToFLOAT(points[1].fY));
1364 segmentData.push(SkScalarToFLOAT(points[2].fX));
1365 segmentData.push(SkScalarToFLOAT(points[2].fY));
1366 break;
1367 case SkPath::kCubic_Verb:
1368 segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1369 segmentStrokes.push(stroke);
1370 segmentData.push(SkScalarToFLOAT(points[1].fX));
1371 segmentData.push(SkScalarToFLOAT(points[1].fY));
1372 segmentData.push(SkScalarToFLOAT(points[2].fX));
1373 segmentData.push(SkScalarToFLOAT(points[2].fY));
1374 segmentData.push(SkScalarToFLOAT(points[3].fX));
1375 segmentData.push(SkScalarToFLOAT(points[3].fY));
1376 break;
1377 case SkPath::kClose_Verb:
1378 // we ignore these, and just get the whole segment from
1379 // the corresponding line/quad/cubic verbs
1380 break;
1381 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001382 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001383 break;
1384 }
1385 }
1386 if (NULL != xpsFigure.get()) {
1387 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1388 stroke, fill,
1389 xpsFigure.get(), xpsFigures));
1390 }
1391 return S_OK;
1392}
1393
1394HRESULT SkXPSDevice::drawInverseWindingPath(const SkDraw& d,
1395 const SkPath& devicePath,
1396 IXpsOMPath* shadedPath) {
1397 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1398 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001399
bungeman@google.comb29c8832011-10-10 13:19:10 +00001400 const XPS_RECT universeRectXps = {
1401 0.0f, 0.0f,
1402 SkScalarToFLOAT(this->fCurrentCanvasSize.fWidth),
1403 SkScalarToFLOAT(this->fCurrentCanvasSize.fHeight),
1404 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001405
bungeman@google.comb29c8832011-10-10 13:19:10 +00001406 //Get the geometry.
1407 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1408 HRM(shadedPath->GetGeometry(&shadedGeometry),
1409 "Could not get shaded geometry for inverse path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001410
bungeman@google.comb29c8832011-10-10 13:19:10 +00001411 //Get the figures from the geometry.
1412 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1413 HRM(shadedGeometry->GetFigures(&shadedFigures),
1414 "Could not get shaded figures for inverse path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001415
bungeman@google.comb29c8832011-10-10 13:19:10 +00001416 HRM(shadedGeometry->SetFillRule(XPS_FILL_RULE_NONZERO),
1417 "Could not set shaded fill rule for inverse path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001418
bungeman@google.comb29c8832011-10-10 13:19:10 +00001419 //Take everything drawn so far, and make a shared resource out of it.
1420 //Replace everything drawn so far with
1421 //inverse canvas
1422 // old canvas of everything so far
1423 // world shaded figure, clipped to current clip
1424 // top canvas of everything so far, clipped to path
1425 //Note: this is not quite right when there is nothing solid in the
1426 //canvas of everything so far, as the bit on top will allow
1427 //the world paint to show through.
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001428
bungeman@google.comb29c8832011-10-10 13:19:10 +00001429 //Create new canvas.
1430 SkTScopedComPtr<IXpsOMCanvas> newCanvas;
1431 HRM(this->fXpsFactory->CreateCanvas(&newCanvas),
1432 "Could not create inverse canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001433
bungeman@google.comb29c8832011-10-10 13:19:10 +00001434 //Save the old canvas to a dictionary on the new canvas.
1435 SkTScopedComPtr<IXpsOMDictionary> newDictionary;
1436 HRM(this->fXpsFactory->CreateDictionary(&newDictionary),
1437 "Could not create inverse dictionary.");
1438 HRM(newCanvas->SetDictionaryLocal(newDictionary.get()),
1439 "Could not set inverse dictionary.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001440
bungeman@google.comb29c8832011-10-10 13:19:10 +00001441 const size_t size = SK_ARRAY_COUNT(L"ID" L_GUID_ID);
1442 wchar_t buffer[size];
1443 wchar_t id[GUID_ID_LEN];
1444 HR(create_id(id, GUID_ID_LEN, '_'));
1445 swprintf_s(buffer, size, L"ID%s", id);
1446 HRM(newDictionary->Append(buffer, this->fCurrentXpsCanvas.get()),
1447 "Could not add canvas to inverse dictionary.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001448
bungeman@google.comb29c8832011-10-10 13:19:10 +00001449 //Start drawing
1450 SkTScopedComPtr<IXpsOMVisualCollection> newVisuals;
1451 HRM(newCanvas->GetVisuals(&newVisuals),
1452 "Could not get inverse canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001453
bungeman@google.comb29c8832011-10-10 13:19:10 +00001454 //Draw old canvas from dictionary onto new canvas.
1455 SkTScopedComPtr<IXpsOMGeometry> oldGeometry;
1456 HRM(this->fXpsFactory->CreateGeometry(&oldGeometry),
1457 "Could not create old inverse geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001458
bungeman@google.comb29c8832011-10-10 13:19:10 +00001459 SkTScopedComPtr<IXpsOMGeometryFigureCollection> oldFigures;
1460 HRM(oldGeometry->GetFigures(&oldFigures),
1461 "Could not get old inverse figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001462
bungeman@google.comb29c8832011-10-10 13:19:10 +00001463 SkTScopedComPtr<IXpsOMGeometryFigure> oldFigure;
1464 HR(this->createXpsRect(universeRect, FALSE, TRUE, &oldFigure));
1465 HRM(oldFigures->Append(oldFigure.get()),
1466 "Could not add old inverse figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001467
bungeman@google.comb29c8832011-10-10 13:19:10 +00001468 SkTScopedComPtr<IXpsOMVisualBrush> oldBrush;
1469 HRM(this->fXpsFactory->CreateVisualBrush(&universeRectXps,
1470 &universeRectXps,
1471 &oldBrush),
1472 "Could not create old inverse brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001473
bungeman@google.comb29c8832011-10-10 13:19:10 +00001474 SkTScopedComPtr<IXpsOMPath> oldPath;
1475 HRM(this->fXpsFactory->CreatePath(&oldPath),
1476 "Could not create old inverse path.");
1477 HRM(oldPath->SetGeometryLocal(oldGeometry.get()),
1478 "Could not set old inverse geometry.");
1479 HRM(oldPath->SetFillBrushLocal(oldBrush.get()),
1480 "Could not set old inverse fill brush.");
1481 //the brush must be parented before setting the lookup.
1482 HRM(newVisuals->Append(oldPath.get()),
1483 "Could not add old inverse path to new canvas visuals.");
1484 HRM(oldBrush->SetVisualLookup(buffer),
1485 "Could not set old inverse brush visual lookup.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001486
bungeman@google.comb29c8832011-10-10 13:19:10 +00001487 //Draw the clip filling shader.
1488 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1489 HR(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure));
1490 HRM(shadedFigures->Append(shadedFigure.get()),
1491 "Could not add inverse shaded figure.");
1492 //the geometry is already set
1493 HR(this->clip(shadedPath, d));
1494 HRM(newVisuals->Append(shadedPath),
1495 "Could not add inverse shaded path to canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001496
bungeman@google.comb29c8832011-10-10 13:19:10 +00001497 //Draw the old canvas on top, clipped to the original path.
1498 SkTScopedComPtr<IXpsOMCanvas> topCanvas;
1499 HRM(this->fXpsFactory->CreateCanvas(&topCanvas),
1500 "Could not create top inverse canvas.");
1501 //Clip the canvas to prevent alpha spill.
1502 //This is the entire reason this canvas exists.
1503 HR(this->clip(topCanvas.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001504
bungeman@google.comb29c8832011-10-10 13:19:10 +00001505 SkTScopedComPtr<IXpsOMGeometry> topGeometry;
1506 HRM(this->fXpsFactory->CreateGeometry(&topGeometry),
1507 "Could not create top inverse geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001508
bungeman@google.comb29c8832011-10-10 13:19:10 +00001509 SkTScopedComPtr<IXpsOMGeometryFigureCollection> topFigures;
1510 HRM(topGeometry->GetFigures(&topFigures),
1511 "Could not get top inverse figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001512
bungeman@google.comb29c8832011-10-10 13:19:10 +00001513 SkTScopedComPtr<IXpsOMGeometryFigure> topFigure;
1514 HR(this->createXpsRect(universeRect, FALSE, TRUE, &topFigure));
1515 HRM(topFigures->Append(topFigure.get()),
1516 "Could not add old inverse figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001517
bungeman@google.comb29c8832011-10-10 13:19:10 +00001518 SkTScopedComPtr<IXpsOMVisualBrush> topBrush;
1519 HRM(this->fXpsFactory->CreateVisualBrush(&universeRectXps,
1520 &universeRectXps,
1521 &topBrush),
1522 "Could not create top inverse brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001523
bungeman@google.comb29c8832011-10-10 13:19:10 +00001524 SkTScopedComPtr<IXpsOMPath> topPath;
1525 HRM(this->fXpsFactory->CreatePath(&topPath),
1526 "Could not create top inverse path.");
1527 HRM(topPath->SetGeometryLocal(topGeometry.get()),
1528 "Could not set top inverse geometry.");
1529 HRM(topPath->SetFillBrushLocal(topBrush.get()),
1530 "Could not set top inverse fill brush.");
1531 //the brush must be parented before setting the lookup.
1532 HRM(newVisuals->Append(topCanvas.get()),
1533 "Could not add top canvas to inverse canvas visuals.");
1534 SkTScopedComPtr<IXpsOMVisualCollection> topVisuals;
1535 HRM(topCanvas->GetVisuals(&topVisuals),
1536 "Could not get top inverse canvas visuals.");
1537 HRM(topVisuals->Append(topPath.get()),
1538 "Could not add top inverse path to top canvas visuals.");
1539 HRM(topBrush->SetVisualLookup(buffer),
1540 "Could not set top inverse brush visual lookup.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001541
bungeman@google.comb29c8832011-10-10 13:19:10 +00001542 HR(this->clipToPath(topPath.get(), devicePath, XPS_FILL_RULE_NONZERO));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001543
bungeman@google.comb29c8832011-10-10 13:19:10 +00001544 //swap current canvas to new canvas
1545 this->fCurrentXpsCanvas.swap(newCanvas);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001546
bungeman@google.comb29c8832011-10-10 13:19:10 +00001547 return S_OK;
1548}
1549
1550void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1551 SkMatrix* matrix,
1552 SkVector* ppuScale,
1553 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001554 //This action is in unit space, but the ppm is specified in physical space.
1555 ppuScale->fX = SkScalarDiv(this->fCurrentPixelsPerMeter.fX,
1556 this->fCurrentUnitsPerMeter.fX);
1557 ppuScale->fY = SkScalarDiv(this->fCurrentPixelsPerMeter.fY,
1558 this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001559
bungeman@google.comb29c8832011-10-10 13:19:10 +00001560 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001561
bungeman@google.comb29c8832011-10-10 13:19:10 +00001562 const SkIRect& irect = clip;
1563 SkRect clipRect = SkRect::MakeLTRB(
1564 SkScalarMul(SkIntToScalar(irect.fLeft), ppuScale->fX),
1565 SkScalarMul(SkIntToScalar(irect.fTop), ppuScale->fY),
1566 SkScalarMul(SkIntToScalar(irect.fRight), ppuScale->fX),
1567 SkScalarMul(SkIntToScalar(irect.fBottom), ppuScale->fY));
1568 clipRect.roundOut(clipIRect);
1569}
1570
1571HRESULT SkXPSDevice::applyMask(const SkDraw& d,
1572 const SkMask& mask,
1573 const SkVector& ppuScale,
1574 IXpsOMPath* shadedPath) {
1575 //Get the geometry object.
1576 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1577 HRM(shadedPath->GetGeometry(&shadedGeometry),
1578 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001579
bungeman@google.comb29c8832011-10-10 13:19:10 +00001580 //Get the figures from the geometry.
1581 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1582 HRM(shadedGeometry->GetFigures(&shadedFigures),
1583 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001584
bungeman@google.comb29c8832011-10-10 13:19:10 +00001585 SkMatrix m;
1586 m.reset();
1587 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1588 SkIntToScalar(mask.fBounds.fTop));
1589 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001590
bungeman@google.comb29c8832011-10-10 13:19:10 +00001591 SkShader::TileMode xy[2];
1592 xy[0] = (SkShader::TileMode)3;
1593 xy[1] = (SkShader::TileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001594
bungeman@google.comb29c8832011-10-10 13:19:10 +00001595 SkBitmap bm;
1596 bm.setConfig(SkBitmap::kA8_Config,
1597 mask.fBounds.width(),
1598 mask.fBounds.height(),
1599 mask.fRowBytes);
1600 bm.setPixels(mask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001601
bungeman@google.comb29c8832011-10-10 13:19:10 +00001602 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1603 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1604 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1605 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001606
bungeman@google.comb29c8832011-10-10 13:19:10 +00001607 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1608 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1609 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1610 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1611 "Could not create mask shaded figure.");
1612 HRM(shadedFigures->Append(shadedFigure.get()),
1613 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001614
bungeman@google.comb29c8832011-10-10 13:19:10 +00001615 HR(this->clip(shadedPath, d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001616
bungeman@google.comb29c8832011-10-10 13:19:10 +00001617 //Add the path to the active visual collection.
1618 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1619 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1620 "Could not get mask current visuals.");
1621 HRM(currentVisuals->Append(shadedPath),
1622 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001623
bungeman@google.comb29c8832011-10-10 13:19:10 +00001624 return S_OK;
1625}
1626
1627HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1628 const SkPaint& shaderPaint,
1629 const SkMatrix& matrix,
1630 BOOL* fill, BOOL* stroke) {
1631 *fill = FALSE;
1632 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001633
bungeman@google.comb29c8832011-10-10 13:19:10 +00001634 const SkPaint::Style style = shaderPaint.getStyle();
1635 const bool hasFill = SkPaint::kFill_Style == style
1636 || SkPaint::kStrokeAndFill_Style == style;
1637 const bool hasStroke = SkPaint::kStroke_Style == style
1638 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001639
bungeman@google.comb29c8832011-10-10 13:19:10 +00001640 //TODO(bungeman): use dictionaries and lookups.
1641 if (hasFill) {
1642 *fill = TRUE;
1643 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1644 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1645 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1646 "Could not set fill for shaded path.");
1647 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001648
bungeman@google.comb29c8832011-10-10 13:19:10 +00001649 if (hasStroke) {
1650 *stroke = TRUE;
1651 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1652 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1653 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1654 "Could not set stroke brush for shaded path.");
1655 HRM(shadedPath->SetStrokeThickness(
1656 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1657 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001658
bungeman@google.comb29c8832011-10-10 13:19:10 +00001659 if (0 == shaderPaint.getStrokeWidth()) {
1660 //XPS hair width is a hack. (XPS Spec 11.6.12).
1661 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1662 HRM(shadedPath->GetStrokeDashes(&dashes),
1663 "Could not set dashes for shaded path.");
1664 XPS_DASH dash;
1665 dash.length = 1.0;
1666 dash.gap = 0.0;
1667 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1668 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1669 "Could not set dash offset for shaded path.");
1670 }
1671 }
1672 return S_OK;
1673}
1674
1675void SkXPSDevice::drawPath(const SkDraw& d,
1676 const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001677 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001678 const SkMatrix* prePathMatrix,
1679 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001680 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1681
bungeman@google.comb29c8832011-10-10 13:19:10 +00001682 // nothing to draw
1683 if (d.fClip->isEmpty() ||
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001684 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001685 return;
1686 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001687
bungeman@google.comb29c8832011-10-10 13:19:10 +00001688 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001689 const bool paintHasPathEffect = paint->getPathEffect()
1690 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001691
bungeman@google.comb29c8832011-10-10 13:19:10 +00001692 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1693 SkMatrix matrix = *d.fMatrix;
1694 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1695 if (prePathMatrix) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001696 if (paintHasPathEffect || paint->getRasterizer()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001697 if (!pathIsMutable) {
1698 skeletalPath = &modifiedPath;
1699 pathIsMutable = true;
1700 }
1701 platonicPath.transform(*prePathMatrix, skeletalPath);
1702 } else {
1703 if (!matrix.preConcat(*prePathMatrix)) {
1704 return;
1705 }
1706 }
1707 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001708
bungeman@google.comb29c8832011-10-10 13:19:10 +00001709 //Apply path effect [Skeletal-path -> Fillable-path].
1710 SkPath* fillablePath = skeletalPath;
1711 if (paintHasPathEffect) {
1712 if (!pathIsMutable) {
1713 fillablePath = &modifiedPath;
1714 pathIsMutable = true;
1715 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001716 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001717
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001718 SkPaint* writablePaint = paint.writable();
1719 writablePaint->setPathEffect(NULL);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001720 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001721 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001722 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001723 writablePaint->setStyle(SkPaint::kStroke_Style);
1724 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001725 }
1726 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001727
bungeman@google.comb29c8832011-10-10 13:19:10 +00001728 //Create the shaded path. This will be the path which is painted.
1729 SkTScopedComPtr<IXpsOMPath> shadedPath;
1730 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1731 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001732
bungeman@google.comb29c8832011-10-10 13:19:10 +00001733 //Create the geometry for the shaded path.
1734 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1735 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1736 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001737
bungeman@google.comb29c8832011-10-10 13:19:10 +00001738 //Add the geometry to the shaded path.
1739 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1740 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001741
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001742 SkRasterizer* rasterizer = paint->getRasterizer();
1743 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001744
1745 //Determine if we will draw or shade and mask.
1746 if (rasterizer || filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001747 if (paint->getStyle() != SkPaint::kFill_Style) {
1748 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001749 }
1750 }
1751
bungeman@google.comb29c8832011-10-10 13:19:10 +00001752 //Set the brushes.
1753 BOOL fill;
1754 BOOL stroke;
1755 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001756 *paint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001757 *d.fMatrix,
1758 &fill,
1759 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001760
bungeman@google.comb29c8832011-10-10 13:19:10 +00001761 //Rasterizer
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001762 if (rasterizer) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001763 SkIRect clipIRect;
1764 SkVector ppuScale;
1765 this->convertToPpm(filter,
1766 &matrix,
1767 &ppuScale,
1768 d.fClip->getBounds(),
1769 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001770
bungeman@google.comb29c8832011-10-10 13:19:10 +00001771 SkMask* mask = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001772
bungeman@google.comb29c8832011-10-10 13:19:10 +00001773 //[Fillable-path -> Mask]
1774 SkMask rasteredMask;
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001775 if (rasterizer->rasterize(
bungeman@google.comb29c8832011-10-10 13:19:10 +00001776 *fillablePath,
1777 matrix,
1778 &clipIRect,
1779 filter, //just to compute how much to draw.
1780 &rasteredMask,
1781 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001782
bungeman@google.comb29c8832011-10-10 13:19:10 +00001783 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1784 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001785
bungeman@google.comb29c8832011-10-10 13:19:10 +00001786 //[Mask -> Mask]
1787 SkMask filteredMask;
1788 if (filter &&
1789 filter->filterMask(&filteredMask, *mask, *d.fMatrix, NULL)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001790
bungeman@google.comb29c8832011-10-10 13:19:10 +00001791 mask = &filteredMask;
1792 } else {
1793 filteredMask.fImage = NULL;
1794 }
1795 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001796
bungeman@google.comb29c8832011-10-10 13:19:10 +00001797 //Draw mask.
1798 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1799 }
1800 return;
1801 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001802
bungeman@google.comb29c8832011-10-10 13:19:10 +00001803 //Mask filter
1804 if (filter) {
1805 SkIRect clipIRect;
1806 SkVector ppuScale;
1807 this->convertToPpm(filter,
1808 &matrix,
1809 &ppuScale,
1810 d.fClip->getBounds(),
1811 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001812
bungeman@google.comb29c8832011-10-10 13:19:10 +00001813 //[Fillable-path -> Pixel-path]
1814 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1815 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001816
bungeman@google.comb29c8832011-10-10 13:19:10 +00001817 SkMask* mask = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001818
bungeman@google.comb29c8832011-10-10 13:19:10 +00001819 //[Pixel-path -> Mask]
1820 SkMask rasteredMask;
1821 if (SkDraw::DrawToMask(
1822 *pixelPath,
1823 &clipIRect,
1824 filter, //just to compute how much to draw.
1825 &matrix,
1826 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001827 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001828 paint->getStyle())) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001829
bungeman@google.comb29c8832011-10-10 13:19:10 +00001830 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1831 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001832
bungeman@google.comb29c8832011-10-10 13:19:10 +00001833 //[Mask -> Mask]
1834 SkMask filteredMask;
1835 if (filter->filterMask(&filteredMask,
1836 rasteredMask,
1837 matrix,
1838 NULL)) {
1839 mask = &filteredMask;
1840 } else {
1841 filteredMask.fImage = NULL;
1842 }
1843 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001844
bungeman@google.comb29c8832011-10-10 13:19:10 +00001845 //Draw mask.
1846 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1847 }
1848 return;
1849 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001850
bungeman@google.comb29c8832011-10-10 13:19:10 +00001851 //Get the figures from the shaded geometry.
1852 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1853 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1854 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001855
bungeman@google.comb29c8832011-10-10 13:19:10 +00001856 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001857
bungeman@google.comb29c8832011-10-10 13:19:10 +00001858 //Set the fill rule.
1859 XPS_FILL_RULE xpsFillRule;
1860 switch (platonicPath.getFillType()) {
1861 case SkPath::kWinding_FillType:
1862 xpsFillRule = XPS_FILL_RULE_NONZERO;
1863 break;
1864 case SkPath::kEvenOdd_FillType:
1865 xpsFillRule = XPS_FILL_RULE_EVENODD;
1866 break;
1867 case SkPath::kInverseWinding_FillType: {
1868 //[Fillable-path -> Device-path]
1869 SkPath* devicePath = pathIsMutable ? fillablePath : &modifiedPath;
1870 fillablePath->transform(matrix, devicePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001871
bungeman@google.comb29c8832011-10-10 13:19:10 +00001872 HRV(this->drawInverseWindingPath(d,
1873 *devicePath,
1874 shadedPath.get()));
1875 return;
1876 }
1877 case SkPath::kInverseEvenOdd_FillType: {
1878 const SkRect universe = SkRect::MakeLTRB(
1879 0, 0,
1880 this->fCurrentCanvasSize.fWidth,
1881 this->fCurrentCanvasSize.fHeight);
1882 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1883 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1884 HRVM(shadedFigures->Append(addOneFigure.get()),
1885 "Could not add even-odd flip figure to shaded path.");
1886 xpsTransformsPath = false;
1887 xpsFillRule = XPS_FILL_RULE_EVENODD;
1888 break;
1889 }
1890 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001891 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001892 }
1893 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1894 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001895
bungeman@google.comb29c8832011-10-10 13:19:10 +00001896 //Create the XPS transform, if possible.
1897 if (xpsTransformsPath) {
1898 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1899 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001900
bungeman@google.comb29c8832011-10-10 13:19:10 +00001901 if (xpsTransform.get()) {
1902 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1903 "Could not set transform on shaded path.");
1904 } else {
1905 xpsTransformsPath = false;
1906 }
1907 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001908
bungeman@google.comb29c8832011-10-10 13:19:10 +00001909 SkPath* devicePath = fillablePath;
1910 if (!xpsTransformsPath) {
1911 //[Fillable-path -> Device-path]
1912 devicePath = pathIsMutable ? fillablePath : &modifiedPath;
1913 fillablePath->transform(matrix, devicePath);
1914 }
1915 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1916 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001917
bungeman@google.comb29c8832011-10-10 13:19:10 +00001918 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001919
bungeman@google.comb29c8832011-10-10 13:19:10 +00001920 //Add the path to the active visual collection.
1921 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1922 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1923 "Could not get current visuals for shaded path.");
1924 HRVM(currentVisuals->Append(shadedPath.get()),
1925 "Could not add shaded path to current visuals.");
1926}
1927
1928HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1929 SkPath clipPath;
1930 SkAssertResult(d.fClip->getBoundaryPath(&clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001931
bungeman@google.comb29c8832011-10-10 13:19:10 +00001932 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1933}
1934HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1935 const SkPath& clipPath,
1936 XPS_FILL_RULE fillRule) {
1937 //Create the geometry.
1938 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1939 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1940 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001941
bungeman@google.comb29c8832011-10-10 13:19:10 +00001942 //Get the figure collection of the geometry.
1943 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1944 HRM(clipGeometry->GetFigures(&clipFigures),
1945 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001946
bungeman@google.comb29c8832011-10-10 13:19:10 +00001947 //Create the figures into the geometry.
1948 HR(this->addXpsPathGeometry(
1949 clipFigures.get(),
1950 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001951
bungeman@google.comb29c8832011-10-10 13:19:10 +00001952 HRM(clipGeometry->SetFillRule(fillRule),
1953 "Could not set fill rule.");
1954 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1955 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001956
bungeman@google.comb29c8832011-10-10 13:19:10 +00001957 return S_OK;
1958}
1959
1960void SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001961 const SkMatrix& matrix, const SkPaint& paint) {
1962 if (d.fClip->isEmpty()) {
1963 return;
1964 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001965
bungeman@google.comb29c8832011-10-10 13:19:10 +00001966 SkIRect srcRect;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001967 srcRect.set(0, 0, bitmap.width(), bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001968
bungeman@google.comb29c8832011-10-10 13:19:10 +00001969 //Create the new shaded path.
1970 SkTScopedComPtr<IXpsOMPath> shadedPath;
1971 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1972 "Could not create path for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001973
bungeman@google.comb29c8832011-10-10 13:19:10 +00001974 //Create the shaded geometry.
1975 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1976 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1977 "Could not create geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001978
bungeman@google.comb29c8832011-10-10 13:19:10 +00001979 //Add the shaded geometry to the shaded path.
1980 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1981 "Could not set the geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001982
bungeman@google.comb29c8832011-10-10 13:19:10 +00001983 //Get the shaded figures from the shaded geometry.
1984 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1985 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1986 "Could not get the figures for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001987
bungeman@google.comb29c8832011-10-10 13:19:10 +00001988 SkMatrix transform = matrix;
1989 transform.postConcat(*d.fMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001990
bungeman@google.comb29c8832011-10-10 13:19:10 +00001991 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1992 HRV(this->createXpsTransform(transform, &xpsTransform));
1993 if (xpsTransform.get()) {
1994 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1995 "Could not set transform for bitmap.");
1996 } else {
1997 //TODO: perspective that bitmap!
1998 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001999
bungeman@google.comb29c8832011-10-10 13:19:10 +00002000 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
2001 if (NULL != xpsTransform.get()) {
2002 const SkShader::TileMode xy[2] = {
2003 SkShader::kClamp_TileMode,
2004 SkShader::kClamp_TileMode,
2005 };
2006 SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00002007 HRV(this->createXpsImageBrush(bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002008 transform,
2009 xy,
2010 paint.getAlpha(),
2011 &xpsImageBrush));
2012 HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
2013 "Could not set bitmap brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002014
bungeman@google.comb29c8832011-10-10 13:19:10 +00002015 const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
2016 SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
2017 HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
2018 }
2019 HRVM(shadedFigures->Append(rectFigure.get()),
2020 "Could not add bitmap figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002021
bungeman@google.comb29c8832011-10-10 13:19:10 +00002022 //Get the current visual collection and add the shaded path to it.
2023 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2024 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2025 "Could not get current visuals for bitmap");
2026 HRVM(currentVisuals->Append(shadedPath.get()),
2027 "Could not add bitmap to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002028
bungeman@google.comb29c8832011-10-10 13:19:10 +00002029 HRV(this->clip(shadedPath.get(), d));
2030}
2031
2032void SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
2033 int x, int y,
2034 const SkPaint& paint) {
2035 //TODO: override this for XPS
2036 SkDEBUGF(("XPS drawSprite not yet implemented."));
2037}
2038
2039HRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
2040 TypefaceUse** typefaceUse) {
reed@google.com398de9a2013-03-21 21:43:51 +00002041 SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002042
bungeman@google.comb29c8832011-10-10 13:19:10 +00002043 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00002044 const SkFontID typefaceID = typeface->uniqueID();
bungeman@google.comb29c8832011-10-10 13:19:10 +00002045 if (!this->fTypefaces.empty()) {
2046 TypefaceUse* current = &this->fTypefaces.front();
2047 const TypefaceUse* last = &this->fTypefaces.back();
2048 for (; current <= last; ++current) {
2049 if (current->typefaceId == typefaceID) {
2050 *typefaceUse = current;
2051 return S_OK;
2052 }
2053 }
2054 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002055
bungeman@google.comb29c8832011-10-10 13:19:10 +00002056 //TODO: create glyph only fonts
2057 //and let the host deal with what kind of font we're looking at.
2058 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002059
bungeman@google.comb29c8832011-10-10 13:19:10 +00002060 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002061 int ttcIndex;
2062 SkStream* fontData = typeface->openStream(&ttcIndex);
bungeman@google.com635091f2013-10-01 15:03:18 +00002063 //TODO: cannot handle FON fonts.
bungeman@google.com8cddc8d2012-01-03 22:36:33 +00002064 HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002065 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002066
bungeman@google.comb29c8832011-10-10 13:19:10 +00002067 const size_t size =
2068 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
2069 wchar_t buffer[size];
2070 wchar_t id[GUID_ID_LEN];
2071 HR(create_id(id, GUID_ID_LEN));
2072 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002073
bungeman@google.comb29c8832011-10-10 13:19:10 +00002074 SkTScopedComPtr<IOpcPartUri> partUri;
2075 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
2076 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002077
bungeman@google.comb29c8832011-10-10 13:19:10 +00002078 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
2079 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
2080 embedding,
2081 partUri.get(),
2082 FALSE,
2083 &xpsFontResource),
2084 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002085
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002086 //TODO: change openStream to return -1 for non-ttc, get rid of this.
2087 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
2088 bool isTTC = (data &&
2089 fontData->getLength() >= sizeof(SkTTCFHeader) &&
2090 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
2091
bungeman@google.comb29c8832011-10-10 13:19:10 +00002092 TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
2093 newTypefaceUse.typefaceId = typefaceID;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002094 newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002095 newTypefaceUse.fontData = fontData;
2096 newTypefaceUse.xpsFont = xpsFontResource.release();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002097
mtklein@google.comcf4d0fb2013-11-18 16:33:08 +00002098 SkAutoGlyphCache agc(paint, NULL, &SkMatrix::I());
bungeman@google.comb29c8832011-10-10 13:19:10 +00002099 SkGlyphCache* glyphCache = agc.getCache();
2100 unsigned int glyphCount = glyphCache->getGlyphCount();
2101 newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002102
bungeman@google.comb29c8832011-10-10 13:19:10 +00002103 *typefaceUse = &newTypefaceUse;
2104 return S_OK;
2105}
2106
2107HRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
2108 IXpsOMObjectFactory* xpsFactory,
2109 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002110 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002111 LPCWSTR text,
2112 XPS_GLYPH_INDEX* xpsGlyphs,
2113 UINT32 xpsGlyphsLen,
2114 XPS_POINT *origin,
2115 FLOAT fontSize,
2116 XPS_STYLE_SIMULATION sims,
2117 const SkMatrix& transform,
2118 const SkPaint& paint) {
2119 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002120 HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
2121 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002122
bungeman@google.comb29c8832011-10-10 13:19:10 +00002123 //XPS uses affine transformations for everything...
2124 //...except positioning text.
2125 bool useCanvasForClip;
2126 if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
2127 origin->x += SkScalarToFLOAT(transform.getTranslateX());
2128 origin->y += SkScalarToFLOAT(transform.getTranslateY());
2129 useCanvasForClip = false;
2130 } else {
2131 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
2132 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
2133 if (xpsMatrixToUse.get()) {
2134 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
2135 "Could not set transform matrix.");
2136 useCanvasForClip = true;
2137 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00002138 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00002139 useCanvasForClip = false;
2140 }
2141 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002142
bungeman@google.comb29c8832011-10-10 13:19:10 +00002143 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
2144 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002145
bungeman@google.comb29c8832011-10-10 13:19:10 +00002146 if (NULL != text) {
2147 HRM(glyphsEditor->SetUnicodeString(text),
2148 "Could not set unicode string.");
2149 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002150
bungeman@google.comb29c8832011-10-10 13:19:10 +00002151 if (NULL != xpsGlyphs) {
2152 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
2153 "Could not set glyphs.");
2154 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002155
bungeman@google.comb29c8832011-10-10 13:19:10 +00002156 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002157
bungeman@google.comb29c8832011-10-10 13:19:10 +00002158 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
2159 HR(this->createXpsBrush(
2160 paint,
2161 &xpsFillBrush,
2162 useCanvasForClip ? NULL : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002163
bungeman@google.comb29c8832011-10-10 13:19:10 +00002164 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
2165 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002166
bungeman@google.comb29c8832011-10-10 13:19:10 +00002167 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002168
bungeman@google.comb29c8832011-10-10 13:19:10 +00002169 HRM(glyphs->SetFontRenderingEmSize(fontSize),
2170 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002171
bungeman@google.comb29c8832011-10-10 13:19:10 +00002172 HRM(glyphs->SetStyleSimulations(sims),
2173 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002174
bungeman@google.comb29c8832011-10-10 13:19:10 +00002175 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2176 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002177
bungeman@google.comb29c8832011-10-10 13:19:10 +00002178 if (!useCanvasForClip) {
2179 HR(this->clip(glyphs.get(), d));
2180 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2181 } else {
2182 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2183 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2184 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002185
bungeman@google.comb29c8832011-10-10 13:19:10 +00002186 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2187 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2188 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002189
bungeman@google.comb29c8832011-10-10 13:19:10 +00002190 HRM(glyphCanvasVisuals->Append(glyphs.get()),
2191 "Could not add glyphs to page.");
2192 HR(this->clip(glyphCanvas.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002193
bungeman@google.comb29c8832011-10-10 13:19:10 +00002194 HRM(visuals->Append(glyphCanvas.get()),
2195 "Could not add glyph canvas to page.");
2196 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002197
bungeman@google.comb29c8832011-10-10 13:19:10 +00002198 return S_OK;
2199}
2200
2201struct SkXPSDrawProcs : public SkDrawProcs {
2202public:
2203 /** [in] Advance width and offsets for glyphs measured in
2204 hundredths of the font em size (XPS Spec 5.1.3). */
2205 FLOAT centemPerUnit;
2206 /** [in,out] The accumulated glyphs used in the current typeface. */
2207 SkBitSet* glyphUse;
2208 /** [out] The glyphs to draw. */
2209 SkTDArray<XPS_GLYPH_INDEX> xpsGlyphs;
2210};
2211
2212static void xps_draw_1_glyph(const SkDraw1Glyph& state,
2213 SkFixed x, SkFixed y,
2214 const SkGlyph& skGlyph) {
2215 SkASSERT(skGlyph.fWidth > 0 && skGlyph.fHeight > 0);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002216
bungeman@google.comb29c8832011-10-10 13:19:10 +00002217 SkXPSDrawProcs* procs = static_cast<SkXPSDrawProcs*>(state.fDraw->fProcs);
bungeman@google.com2211b622012-01-13 15:02:58 +00002218
2219 //Draw pre-adds half the sampling frequency for floor rounding.
bungeman@google.com94471032013-02-25 15:55:13 +00002220 x -= state.fHalfSampleX;
2221 y -= state.fHalfSampleY;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002222
bungeman@google.comb29c8832011-10-10 13:19:10 +00002223 XPS_GLYPH_INDEX* xpsGlyph = procs->xpsGlyphs.append();
2224 uint16_t glyphID = skGlyph.getGlyphID();
2225 procs->glyphUse->setBit(glyphID, true);
2226 xpsGlyph->index = glyphID;
2227 if (1 == procs->xpsGlyphs.count()) {
2228 xpsGlyph->advanceWidth = 0.0f;
2229 xpsGlyph->horizontalOffset = SkFixedToFloat(x) * procs->centemPerUnit;
2230 xpsGlyph->verticalOffset = SkFixedToFloat(y) * -procs->centemPerUnit;
2231 } else {
2232 const XPS_GLYPH_INDEX& first = procs->xpsGlyphs[0];
2233 xpsGlyph->advanceWidth = 0.0f;
2234 xpsGlyph->horizontalOffset = (SkFixedToFloat(x) * procs->centemPerUnit)
2235 - first.horizontalOffset;
2236 xpsGlyph->verticalOffset = (SkFixedToFloat(y) * -procs->centemPerUnit)
2237 - first.verticalOffset;
2238 }
2239}
2240
2241static void text_draw_init(const SkPaint& paint,
2242 const void* text, size_t byteLength,
2243 SkBitSet& glyphsUsed,
2244 SkDraw& myDraw, SkXPSDrawProcs& procs) {
2245 procs.fD1GProc = xps_draw_1_glyph;
bungeman@google.com4b18f572013-07-22 15:21:23 +00002246 size_t numGlyphGuess;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002247 switch (paint.getTextEncoding()) {
2248 case SkPaint::kUTF8_TextEncoding:
2249 numGlyphGuess = SkUTF8_CountUnichars(
2250 static_cast<const char *>(text),
2251 byteLength);
2252 break;
2253 case SkPaint::kUTF16_TextEncoding:
2254 numGlyphGuess = SkUTF16_CountUnichars(
2255 static_cast<const uint16_t *>(text),
2256 byteLength);
2257 break;
2258 case SkPaint::kGlyphID_TextEncoding:
2259 numGlyphGuess = byteLength / 2;
2260 break;
2261 default:
2262 SK_DEBUGBREAK(true);
2263 }
2264 procs.xpsGlyphs.setReserve(numGlyphGuess);
2265 procs.glyphUse = &glyphsUsed;
2266 procs.centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002267
bungeman@google.comb29c8832011-10-10 13:19:10 +00002268 myDraw.fProcs = &procs;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002269}
2270
2271static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2272 const SkPaint::Style style = paint.getStyle();
2273 return matrix.hasPerspective()
2274 || SkPaint::kStroke_Style == style
2275 || SkPaint::kStrokeAndFill_Style == style
2276 || paint.getMaskFilter()
2277 || paint.getRasterizer()
2278 ;
2279}
2280
2281void SkXPSDevice::drawText(const SkDraw& d,
2282 const void* text, size_t byteLen,
2283 SkScalar x, SkScalar y,
2284 const SkPaint& paint) {
2285 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002286
bungeman@google.comb29c8832011-10-10 13:19:10 +00002287 if (text_must_be_pathed(paint, *d.fMatrix)) {
2288 SkPath path;
2289 paint.getTextPath(text, byteLen, x, y, &path);
2290 this->drawPath(d, path, paint, NULL, true);
2291 //TODO: add automation "text"
2292 return;
2293 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002294
bungeman@google.comb29c8832011-10-10 13:19:10 +00002295 TypefaceUse* typeface;
2296 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002297
bungeman@google.comb29c8832011-10-10 13:19:10 +00002298 SkDraw myDraw(d);
bungeman@google.com635091f2013-10-01 15:03:18 +00002299 myDraw.fMatrix = &SkMatrix::I();
bungeman@google.comb29c8832011-10-10 13:19:10 +00002300 SkXPSDrawProcs procs;
2301 text_draw_init(paint, text, byteLen, *typeface->glyphsUsed, myDraw, procs);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002302
bungeman@google.comb29c8832011-10-10 13:19:10 +00002303 myDraw.drawText(static_cast<const char*>(text), byteLen, x, y, paint);
reed@google.com089130c2011-12-12 21:52:18 +00002304
2305 // SkDraw may have clipped out the glyphs, so we need to check
2306 if (procs.xpsGlyphs.count() == 0) {
2307 return;
2308 }
2309
bungeman@google.comb29c8832011-10-10 13:19:10 +00002310 XPS_POINT origin = {
2311 procs.xpsGlyphs[0].horizontalOffset / procs.centemPerUnit,
2312 procs.xpsGlyphs[0].verticalOffset / -procs.centemPerUnit,
2313 };
2314 procs.xpsGlyphs[0].horizontalOffset = 0.0f;
2315 procs.xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002316
bungeman@google.comb29c8832011-10-10 13:19:10 +00002317 HRV(AddGlyphs(d,
2318 this->fXpsFactory.get(),
2319 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002320 typeface,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002321 NULL,
2322 procs.xpsGlyphs.begin(), procs.xpsGlyphs.count(),
2323 &origin,
2324 SkScalarToFLOAT(paint.getTextSize()),
2325 XPS_STYLE_SIMULATION_NONE,
2326 *d.fMatrix,
2327 paint));
2328}
2329
2330void SkXPSDevice::drawPosText(const SkDraw& d,
2331 const void* text, size_t byteLen,
2332 const SkScalar pos[],
2333 SkScalar constY, int scalarsPerPos,
2334 const SkPaint& paint) {
2335 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002336
bungeman@google.comb29c8832011-10-10 13:19:10 +00002337 if (text_must_be_pathed(paint, *d.fMatrix)) {
2338 SkPath path;
2339 //TODO: make this work, Draw currently does not handle as well.
2340 //paint.getTextPath(text, byteLength, x, y, &path);
2341 //this->drawPath(d, path, paint, NULL, true);
2342 //TODO: add automation "text"
2343 return;
2344 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002345
bungeman@google.comb29c8832011-10-10 13:19:10 +00002346 TypefaceUse* typeface;
2347 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002348
bungeman@google.comb29c8832011-10-10 13:19:10 +00002349 SkDraw myDraw(d);
bungeman@google.com635091f2013-10-01 15:03:18 +00002350 myDraw.fMatrix = &SkMatrix::I();
bungeman@google.comb29c8832011-10-10 13:19:10 +00002351 SkXPSDrawProcs procs;
2352 text_draw_init(paint, text, byteLen, *typeface->glyphsUsed, myDraw, procs);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002353
bungeman@google.comb29c8832011-10-10 13:19:10 +00002354 myDraw.drawPosText(static_cast<const char*>(text), byteLen,
2355 pos, constY, scalarsPerPos,
2356 paint);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002357
reed@google.com089130c2011-12-12 21:52:18 +00002358 // SkDraw may have clipped out the glyphs, so we need to check
2359 if (procs.xpsGlyphs.count() == 0) {
2360 return;
2361 }
2362
bungeman@google.comb29c8832011-10-10 13:19:10 +00002363 XPS_POINT origin = {
2364 procs.xpsGlyphs[0].horizontalOffset / procs.centemPerUnit,
2365 procs.xpsGlyphs[0].verticalOffset / -procs.centemPerUnit,
2366 };
2367 procs.xpsGlyphs[0].horizontalOffset = 0.0f;
2368 procs.xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002369
bungeman@google.comb29c8832011-10-10 13:19:10 +00002370 HRV(AddGlyphs(d,
2371 this->fXpsFactory.get(),
2372 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002373 typeface,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002374 NULL,
2375 procs.xpsGlyphs.begin(), procs.xpsGlyphs.count(),
2376 &origin,
2377 SkScalarToFLOAT(paint.getTextSize()),
2378 XPS_STYLE_SIMULATION_NONE,
2379 *d.fMatrix,
2380 paint));
2381}
2382
2383void SkXPSDevice::drawTextOnPath(const SkDraw& d, const void* text, size_t len,
2384 const SkPath& path, const SkMatrix* matrix,
2385 const SkPaint& paint) {
2386 //This will call back into the device to do the drawing.
2387 d.drawTextOnPath((const char*)text, len, path, matrix, paint);
2388}
2389
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002390void SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002391 int x, int y,
2392 const SkPaint&) {
2393 SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002394
bungeman@google.comb29c8832011-10-10 13:19:10 +00002395 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2396 XPS_MATRIX rawTransform = {
2397 1.0f,
2398 0.0f,
2399 0.0f,
2400 1.0f,
2401 static_cast<FLOAT>(x),
2402 static_cast<FLOAT>(y),
2403 };
2404 HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2405 "Could not create layer transform.");
2406 HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2407 "Could not set layer transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002408
bungeman@google.comb29c8832011-10-10 13:19:10 +00002409 //Get the current visual collection and add the layer to it.
2410 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2411 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2412 "Could not get current visuals for layer.");
2413 HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2414 "Could not add layer to current visuals.");
2415}
2416
reed@google.com982cb872011-12-07 18:34:08 +00002417bool SkXPSDevice::onReadPixels(const SkBitmap& bitmap, int x, int y,
2418 SkCanvas::Config8888) {
2419 return false;
2420}
2421
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00002422SkBaseDevice* SkXPSDevice::onCreateDevice(const SkImageInfo&, Usage) {
bungeman@google.com635091f2013-10-01 15:03:18 +00002423//Conditional for bug compatibility with PDF device.
2424#if 0
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002425 if (SkBaseDevice::kGeneral_Usage == usage) {
reed@google.comb55deeb2012-01-06 14:43:09 +00002426 return NULL;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002427 SK_CRASH();
2428 //To what stream do we write?
2429 //SkXPSDevice* dev = new SkXPSDevice(this);
2430 //SkSize s = SkSize::Make(width, height);
2431 //dev->BeginCanvas(s, s, SkMatrix::I());
2432 //return dev;
2433 }
bungeman@google.com635091f2013-10-01 15:03:18 +00002434#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +00002435 return new SkXPSDevice(this->fXpsFactory.get());
2436}
2437
2438SkXPSDevice::SkXPSDevice(IXpsOMObjectFactory* xpsFactory)
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002439 : SkBitmapDevice(make_fake_bitmap(10000, 10000))
bungeman@google.comb29c8832011-10-10 13:19:10 +00002440 , fCurrentPage(0) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002441
bungeman@google.comb29c8832011-10-10 13:19:10 +00002442 HRVM(CoCreateInstance(
2443 CLSID_XpsOMObjectFactory,
2444 NULL,
2445 CLSCTX_INPROC_SERVER,
2446 IID_PPV_ARGS(&this->fXpsFactory)),
2447 "Could not create factory for layer.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002448
bungeman@google.comb29c8832011-10-10 13:19:10 +00002449 HRVM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
2450 "Could not create canvas for layer.");
2451}
reed@google.comb55deeb2012-01-06 14:43:09 +00002452
senorblanco@chromium.orge629c7b2014-02-05 23:23:39 +00002453bool SkXPSDevice::allowImageFilter(const SkImageFilter*) {
reed@google.comb55deeb2012-01-06 14:43:09 +00002454 return false;
2455}