blob: 5b52ab8b04994d27d5b707272d9c993db967dff0 [file] [log] [blame]
bungeman@google.comb29c8832011-10-10 13:19:10 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
halcanary47ef4d52015-03-03 09:13:09 -08008#include "SkTypes.h"
mtklein1ee76512015-11-02 10:20:27 -08009#if defined(SK_BUILD_FOR_WIN32)
halcanary47ef4d52015-03-03 09:13:09 -080010
halcanary4dbbd042016-06-07 17:21:10 -070011#include "SkLeanWindows.h"
12
bungeman@google.comb29c8832011-10-10 13:19:10 +000013#ifndef UNICODE
14#define UNICODE
15#endif
16#ifndef _UNICODE
17#define _UNICODE
18#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +000019#include <ObjBase.h>
20#include <XpsObjectModel.h>
21#include <T2EmbApi.h>
22#include <FontSub.h>
halcanarybfa92752016-05-31 11:23:42 -070023#include <limits>
bungeman@google.comb29c8832011-10-10 13:19:10 +000024
25#include "SkColor.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000026#include "SkData.h"
27#include "SkDraw.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000028#include "SkEndian.h"
herbbda26432015-11-24 08:37:01 -080029#include "SkFindAndPlaceGlyph.h"
halcanary47ef4d52015-03-03 09:13:09 -080030#include "SkGeometry.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000031#include "SkGlyphCache.h"
32#include "SkHRESULT.h"
Hal Canaryb39b09e2017-02-01 17:04:44 -050033#include "SkIStream.h"
Mike Reed627778d2016-09-28 17:13:38 -040034#include "SkImage.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000035#include "SkImageEncoder.h"
Hal Canaryb39b09e2017-02-01 17:04:44 -050036#include "SkImagePriv.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000037#include "SkMaskFilter.h"
38#include "SkPaint.h"
reeda4393342016-03-18 11:22:57 -070039#include "SkPathEffect.h"
Ben Wagnerda5a1b82014-08-22 15:07:06 -040040#include "SkPathOps.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000041#include "SkPoint.h"
reed1e7f5e72016-04-27 07:49:17 -070042#include "SkRasterClip.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000043#include "SkRasterizer.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000044#include "SkSFNTHeader.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000045#include "SkShader.h"
46#include "SkSize.h"
47#include "SkStream.h"
48#include "SkTDArray.h"
49#include "SkTLazy.h"
50#include "SkTScopedComPtr.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000051#include "SkTTCFHeader.h"
reed@google.com398de9a2013-03-21 21:43:51 +000052#include "SkTypefacePriv.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000053#include "SkUtils.h"
54#include "SkXPSDevice.h"
55
56//Windows defines a FLOAT type,
57//make it clear when converting a scalar that this is what is wanted.
58#define SkScalarToFLOAT(n) SkScalarToFloat(n)
59
Ben Wagnerda5a1b82014-08-22 15:07:06 -040060//Dummy representation of a GUID from createId.
bungeman@google.comb29c8832011-10-10 13:19:10 +000061#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
halcanary96fcdcc2015-08-27 07:41:13 -070062//Length of GUID representation from createId, including nullptr terminator.
bungeman@google.comb29c8832011-10-10 13:19:10 +000063#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
64
65/**
66 Formats a GUID and places it into buffer.
67 buffer should have space for at least GUID_ID_LEN wide characters.
68 The string will always be wchar null terminated.
69 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
70 @return -1 if there was an error, > 0 if success.
71 */
72static int format_guid(const GUID& guid,
73 wchar_t* buffer, size_t bufferSize,
74 wchar_t sep = '-') {
75 SkASSERT(bufferSize >= GUID_ID_LEN);
76 return swprintf_s(buffer,
77 bufferSize,
78 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
79 guid.Data1,
80 sep,
81 guid.Data2,
82 sep,
83 guid.Data3,
84 sep,
85 guid.Data4[0],
86 guid.Data4[1],
87 sep,
88 guid.Data4[2],
89 guid.Data4[3],
90 guid.Data4[4],
91 guid.Data4[5],
92 guid.Data4[6],
93 guid.Data4[7]);
94}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000095
Ben Wagnerda5a1b82014-08-22 15:07:06 -040096HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000097 GUID guid = {};
Ben Wagnerda5a1b82014-08-22 15:07:06 -040098#ifdef SK_XPS_USE_DETERMINISTIC_IDS
99 guid.Data1 = fNextId++;
100 // The following make this a valid Type4 UUID.
101 guid.Data3 = 0x4000;
102 guid.Data4[0] = 0x80;
103#else
bungeman@google.comb29c8832011-10-10 13:19:10 +0000104 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400105#endif
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000106
bungeman@google.comb29c8832011-10-10 13:19:10 +0000107 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
108 HRM(E_UNEXPECTED, "Could not format GUID into id.");
109 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000110
bungeman@google.comb29c8832011-10-10 13:19:10 +0000111 return S_OK;
112}
113
Hal Canaryb39b09e2017-02-01 17:04:44 -0500114SkXPSDevice::SkXPSDevice(SkISize s)
115 : INHERITED(SkImageInfo::MakeUnknown(s.width(), s.height()),
116 SkSurfaceProps(0, kUnknown_SkPixelGeometry))
Hal Canaryabc88d22017-02-06 09:26:49 -0500117 , fCurrentPage(0) {}
bungeman@google.comb29c8832011-10-10 13:19:10 +0000118
Hal Canaryabc88d22017-02-06 09:26:49 -0500119SkXPSDevice::~SkXPSDevice() {}
bungeman@google.comb29c8832011-10-10 13:19:10 +0000120
121SkXPSDevice::TypefaceUse::TypefaceUse()
122 : typefaceId(0xffffffff)
halcanary96fcdcc2015-08-27 07:41:13 -0700123 , fontData(nullptr)
124 , xpsFont(nullptr)
Hal Canaryabc88d22017-02-06 09:26:49 -0500125 , glyphsUsed(nullptr) {}
bungeman@google.comb29c8832011-10-10 13:19:10 +0000126
127SkXPSDevice::TypefaceUse::~TypefaceUse() {
128 //xpsFont owns fontData ref
129 this->xpsFont->Release();
130 delete this->glyphsUsed;
131}
132
Hal Canaryabc88d22017-02-06 09:26:49 -0500133bool SkXPSDevice::beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory* factory) {
134 SkASSERT(factory);
135 fXpsFactory.reset(SkRefComPtr(factory));
136 HRB(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000137 return true;
138}
139
140bool SkXPSDevice::beginSheet(
141 const SkVector& unitsPerMeter,
142 const SkVector& pixelsPerMeter,
143 const SkSize& trimSize,
144 const SkRect* mediaBox,
145 const SkRect* bleedBox,
146 const SkRect* artBox,
147 const SkRect* cropBox) {
148 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000149
bungeman@google.comb29c8832011-10-10 13:19:10 +0000150 //For simplicity, just write everything out in geometry units,
151 //then have a base canvas do the scale to physical units.
152 this->fCurrentCanvasSize = trimSize;
153 this->fCurrentUnitsPerMeter = unitsPerMeter;
154 this->fCurrentPixelsPerMeter = pixelsPerMeter;
Hal Canaryabc88d22017-02-06 09:26:49 -0500155 return this->createCanvasForLayer();
156}
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000157
Hal Canaryabc88d22017-02-06 09:26:49 -0500158bool SkXPSDevice::createCanvasForLayer() {
159 SkASSERT(fXpsFactory);
160 fCurrentXpsCanvas.reset();
161 HRB(fXpsFactory->CreateCanvas(&fCurrentXpsCanvas));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000162 return true;
163}
164
halcanarybfa92752016-05-31 11:23:42 -0700165template <typename T> static constexpr size_t sk_digits_in() {
166 return static_cast<size_t>(std::numeric_limits<T>::digits10 + 1);
167}
168
bungeman@google.comb29c8832011-10-10 13:19:10 +0000169HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
170 const unsigned int pageNum,
171 IXpsOMImageResource** image) {
172 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
173 HRM(CoCreateInstance(
174 CLSID_XpsOMThumbnailGenerator,
halcanary96fcdcc2015-08-27 07:41:13 -0700175 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000176 CLSCTX_INPROC_SERVER,
177 IID_PPV_ARGS(&thumbnailGenerator)),
178 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179
bungeman@google.comb29c8832011-10-10 13:19:10 +0000180 SkTScopedComPtr<IOpcPartUri> partUri;
halcanarybfa92752016-05-31 11:23:42 -0700181 constexpr size_t size = SkTMax(
182 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + sk_digits_in<decltype(pageNum)>(),
183 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png"));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000184 wchar_t buffer[size];
185 if (pageNum > 0) {
186 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
187 } else {
188 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400189 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000190 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
191 }
192 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
193 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000194
bungeman@google.comb29c8832011-10-10 13:19:10 +0000195 HRM(thumbnailGenerator->GenerateThumbnail(page,
196 XPS_IMAGE_TYPE_PNG,
197 XPS_THUMBNAIL_SIZE_LARGE,
198 partUri.get(),
199 image),
200 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000201
bungeman@google.comb29c8832011-10-10 13:19:10 +0000202 return S_OK;
203}
204
205HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
206 IXpsOMPage** page) {
halcanarybfa92752016-05-31 11:23:42 -0700207 constexpr size_t size =
208 SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
209 + sk_digits_in<decltype(fCurrentPage)>();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000210 wchar_t buffer[size];
211 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
212 this->fCurrentPage);
213 SkTScopedComPtr<IOpcPartUri> partUri;
214 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
215 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000216
bungeman@google.comb29c8832011-10-10 13:19:10 +0000217 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
218 HRM(this->fXpsFactory->CreatePage(&pageSize,
219 L"und",
220 partUri.get(),
221 page),
222 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000223
bungeman@google.comb29c8832011-10-10 13:19:10 +0000224 return S_OK;
225}
226
227HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
228 //Create package writer.
229 {
230 SkTScopedComPtr<IOpcPartUri> partUri;
231 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
232 &partUri),
233 "Could not create document sequence part uri.");
234 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
235 this->fOutputStream.get(),
236 TRUE,
237 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
238 partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700239 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000240 image,
halcanary96fcdcc2015-08-27 07:41:13 -0700241 nullptr,
242 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000243 &this->fPackageWriter),
244 "Could not create package writer.");
245 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000246
bungeman@google.comb29c8832011-10-10 13:19:10 +0000247 //Begin the lone document.
248 {
249 SkTScopedComPtr<IOpcPartUri> partUri;
250 HRM(this->fXpsFactory->CreatePartUri(
251 L"/Documents/1/FixedDocument.fdoc",
252 &partUri),
253 "Could not create fixed document part uri.");
254 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700255 nullptr,
256 nullptr,
257 nullptr,
258 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000259 "Could not start document.");
260 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000261
bungeman@google.comb29c8832011-10-10 13:19:10 +0000262 return S_OK;
263}
264
265bool SkXPSDevice::endSheet() {
266 //XPS is fixed at 96dpi (XPS Spec 11.1).
267 static const float xpsDPI = 96.0f;
268 static const float inchesPerMeter = 10000.0f / 254.0f;
269 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
270 const float scaleX = targetUnitsPerMeter
271 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
272 const float scaleY = targetUnitsPerMeter
273 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000274
bungeman@google.comb29c8832011-10-10 13:19:10 +0000275 //Create the scale canvas.
276 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
277 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
278 "Could not create scale canvas.");
279 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
280 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
281 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000282
bungeman@google.comb29c8832011-10-10 13:19:10 +0000283 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
284 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
285 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
286 "Could not create geometry to physical transform.");
287 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
288 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000289
bungeman@google.comb29c8832011-10-10 13:19:10 +0000290 //Add the content canvas to the scale canvas.
291 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
292 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000293
bungeman@google.comb29c8832011-10-10 13:19:10 +0000294 //Create the page.
295 XPS_SIZE pageSize = {
296 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
297 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
298 };
299 SkTScopedComPtr<IXpsOMPage> page;
300 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000301
bungeman@google.comb29c8832011-10-10 13:19:10 +0000302 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
303 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000304
bungeman@google.comb29c8832011-10-10 13:19:10 +0000305 //Add the scale canvas to the page.
306 HRBM(pageVisuals->Append(scaleCanvas.get()),
307 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000308
bungeman@google.comb29c8832011-10-10 13:19:10 +0000309 //Create the package writer if it hasn't been created yet.
halcanary96fcdcc2015-08-27 07:41:13 -0700310 if (nullptr == this->fPackageWriter.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000311 SkTScopedComPtr<IXpsOMImageResource> image;
312 //Ignore return, thumbnail is completely optional.
313 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000314
bungeman@google.comb29c8832011-10-10 13:19:10 +0000315 HRB(this->initXpsDocumentWriter(image.get()));
316 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000317
bungeman@google.comb29c8832011-10-10 13:19:10 +0000318 HRBM(this->fPackageWriter->AddPage(page.get(),
319 &pageSize,
halcanary96fcdcc2015-08-27 07:41:13 -0700320 nullptr,
321 nullptr,
322 nullptr,
323 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000324 "Could not write the page.");
325 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000326
bungeman@google.comb29c8832011-10-10 13:19:10 +0000327 return true;
328}
329
330static HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
331 //CreateFontPackage wants unsigned short.
332 //Microsoft, Y U NO stdint.h?
333 SkTDArray<unsigned short> keepList;
334 current->glyphsUsed->exportTo(&keepList);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000335
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000336 int ttcCount = (current->ttcIndex + 1);
337
bungeman@google.comb29c8832011-10-10 13:19:10 +0000338 //The following are declared with the types required by CreateFontPackage.
halcanary96fcdcc2015-08-27 07:41:13 -0700339 unsigned char *fontPackageBufferRaw = nullptr;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000340 unsigned long fontPackageBufferSize;
341 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000342 unsigned long result = CreateFontPackage(
343 (unsigned char *) current->fontData->getMemoryBase(),
344 (unsigned long) current->fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000345 &fontPackageBufferRaw,
346 &fontPackageBufferSize,
347 &bytesWritten,
348 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
349 current->ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000350 TTFCFP_SUBSET,
351 0,
352 0,
353 0,
354 keepList.begin(),
355 keepList.count(),
356 sk_malloc_throw,
357 sk_realloc_throw,
358 sk_free,
halcanary96fcdcc2015-08-27 07:41:13 -0700359 nullptr);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000360 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000361 if (result != NO_ERROR) {
362 SkDEBUGF(("CreateFontPackage Error %lu", result));
363 return E_UNEXPECTED;
364 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000365
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000366 // If it was originally a ttc, keep it a ttc.
367 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
368 size_t extra;
369 if (ttcCount > 0) {
370 // Create space for a ttc header.
371 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
372 fontPackageBuffer.realloc(bytesWritten + extra);
373 //overlap is certain, use memmove
374 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
375
376 // Write the ttc header.
377 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
378 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
379 ttcfHeader->version = SkTTCFHeader::version_1;
380 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
381 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
382 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
bsalomon0aa5cea2014-12-15 09:13:35 -0800383 *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000384 }
385
386 // Fix up offsets in sfnt table entries.
387 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
388 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
389 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
390 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
391 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
392 tableDirectory->offset = SkEndian_SwapBE32(
bsalomon0aa5cea2014-12-15 09:13:35 -0800393 SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000394 }
395 } else {
396 extra = 0;
397 fontPackageBuffer.realloc(bytesWritten);
398 }
399
Ben Wagner145dbcd2016-11-03 14:40:50 -0400400 std::unique_ptr<SkMemoryStream> newStream(new SkMemoryStream());
mtklein18300a32016-03-16 13:53:35 -0700401 newStream->setMemoryOwned(fontPackageBuffer.release(), bytesWritten + extra);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000402
bungeman@google.comb29c8832011-10-10 13:19:10 +0000403 SkTScopedComPtr<IStream> newIStream;
mtklein18300a32016-03-16 13:53:35 -0700404 SkIStream::CreateFromSkStream(newStream.release(), true, &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000405
bungeman@google.comb29c8832011-10-10 13:19:10 +0000406 XPS_FONT_EMBEDDING embedding;
407 HRM(current->xpsFont->GetEmbeddingOption(&embedding),
408 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000409
bungeman@google.comb29c8832011-10-10 13:19:10 +0000410 SkTScopedComPtr<IOpcPartUri> partUri;
411 HRM(current->xpsFont->GetPartName(&partUri),
412 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000413
bungeman@google.comb29c8832011-10-10 13:19:10 +0000414 HRM(current->xpsFont->SetContent(
415 newIStream.get(),
416 embedding,
417 partUri.get()),
418 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000419
bungeman@google.comb29c8832011-10-10 13:19:10 +0000420 return S_OK;
421}
422
423bool SkXPSDevice::endPortfolio() {
424 //Subset fonts
425 if (!this->fTypefaces.empty()) {
426 SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
427 const TypefaceUse* last = &this->fTypefaces.back();
428 for (; current <= last; ++current) {
429 //Ignore return for now, if it didn't subset, let it be.
430 subset_typeface(current);
431 }
432 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000433
bungeman@google.comb29c8832011-10-10 13:19:10 +0000434 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000435
bungeman@google.comb29c8832011-10-10 13:19:10 +0000436 return true;
437}
438
439static XPS_COLOR xps_color(const SkColor skColor) {
440 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
441 XPS_COLOR xpsColor;
442 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
443 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
444 xpsColor.value.sRGB.red = SkColorGetR(skColor);
445 xpsColor.value.sRGB.green = SkColorGetG(skColor);
446 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000447
bungeman@google.comb29c8832011-10-10 13:19:10 +0000448 return xpsColor;
449}
450
451static XPS_POINT xps_point(const SkPoint& point) {
452 XPS_POINT xpsPoint = {
453 SkScalarToFLOAT(point.fX),
454 SkScalarToFLOAT(point.fY),
455 };
456 return xpsPoint;
457}
458
459static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
460 SkPoint skTransformedPoint;
461 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
462 return xps_point(skTransformedPoint);
463}
464
465static XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
466 switch (tileMode) {
467 case SkShader::kClamp_TileMode:
468 return XPS_SPREAD_METHOD_PAD;
469 case SkShader::kRepeat_TileMode:
470 return XPS_SPREAD_METHOD_REPEAT;
471 case SkShader::kMirror_TileMode:
472 return XPS_SPREAD_METHOD_REFLECT;
473 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000474 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000475 }
476 return XPS_SPREAD_METHOD_PAD;
477}
478
479static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
480 const SkPoint& start, const SkPoint& end,
481 const SkMatrix& transform) {
482 SkPoint startTransformed;
483 transform.mapXY(start.fX, start.fY, &startTransformed);
484 SkPoint endTransformed;
485 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000486
bungeman@google.comb29c8832011-10-10 13:19:10 +0000487 //Manhattan distance between transformed start and end.
488 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
489 + (endTransformed.fY - startTransformed.fY);
490 if (SkScalarNearlyZero(startToEnd)) {
491 for (int i = 0; i < numOffsets; ++i) {
492 stopOffsets[i] = 0;
493 }
494 return;
495 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000496
bungeman@google.comb29c8832011-10-10 13:19:10 +0000497 for (int i = 0; i < numOffsets; ++i) {
498 SkPoint stop;
Mike Reed8be952a2017-02-13 20:44:33 -0500499 stop.fX = (end.fX - start.fX) * stopOffsets[i];
500 stop.fY = (end.fY - start.fY) * stopOffsets[i];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000501
bungeman@google.comb29c8832011-10-10 13:19:10 +0000502 SkPoint stopTransformed;
503 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000504
bungeman@google.comb29c8832011-10-10 13:19:10 +0000505 //Manhattan distance between transformed start and stop.
506 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
507 + (stopTransformed.fY - startTransformed.fY);
508 //Percentage along transformed line.
reed80ea19c2015-05-12 10:37:34 -0700509 stopOffsets[i] = startToStop / startToEnd;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000510 }
511}
512
513HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
514 IXpsOMMatrixTransform** xpsTransform) {
515 SkScalar affine[6];
516 if (!matrix.asAffine(affine)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700517 *xpsTransform = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000518 return S_FALSE;
519 }
520 XPS_MATRIX rawXpsMatrix = {
521 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
522 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
523 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
524 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
525 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
526 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
527 };
528 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
529 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000530
bungeman@google.comb29c8832011-10-10 13:19:10 +0000531 return S_OK;
532}
533
534HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
535 IXpsOMVisualCollection* visuals,
536 IXpsOMPath** path) {
537 SkTScopedComPtr<IXpsOMGeometry> geometry;
538 HRM(this->fXpsFactory->CreateGeometry(&geometry),
539 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000540
bungeman@google.comb29c8832011-10-10 13:19:10 +0000541 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
542 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
543 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000544
bungeman@google.comb29c8832011-10-10 13:19:10 +0000545 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
546 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000547
bungeman@google.comb29c8832011-10-10 13:19:10 +0000548 HRM(visuals->Append(*path), "Could not add path to visuals.");
549 return S_OK;
550}
551
552HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
553 const SkAlpha alpha,
554 IXpsOMBrush** xpsBrush) {
555 XPS_COLOR xpsColor = xps_color(skColor);
556 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
halcanary96fcdcc2015-08-27 07:41:13 -0700557 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000558 "Could not create solid color brush.");
559 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
560 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
561 return S_OK;
562}
563
564HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
565 const XPS_RECT& imageViewBox,
566 IXpsOMImageResource* image,
567 IXpsOMVisualCollection* visuals) {
568 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
569 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000570
bungeman@google.comb29c8832011-10-10 13:19:10 +0000571 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
572 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000573
bungeman@google.comb29c8832011-10-10 13:19:10 +0000574 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
575 HRM(this->fXpsFactory->CreateImageBrush(image,
576 &imageViewBox,
577 &imageViewBox,
578 &areaToFillBrush),
579 "Could not create brush for side of clamp.");
580 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
581 "Could not set tile mode for side of clamp.");
582 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
583 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000584
bungeman@google.comb29c8832011-10-10 13:19:10 +0000585 return S_OK;
586}
587
588HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
589 const SkColor color,
590 IXpsOMVisualCollection* visuals) {
591 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
592 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000593
bungeman@google.comb29c8832011-10-10 13:19:10 +0000594 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
595 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000596
bungeman@google.comb29c8832011-10-10 13:19:10 +0000597 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
598 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
599 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
600 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000601
bungeman@google.comb29c8832011-10-10 13:19:10 +0000602 return S_OK;
603}
604
605static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
606static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
607static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
608static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
609static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
610
611//TODO(bungeman): In the future, should skia add None,
612//handle None+Mirror and None+Repeat correctly.
613//None is currently an internal hack so masks don't repeat (None+None only).
614static XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
615 [SkShader::kTileModeCount+1] = {
halcanarya634b742016-10-13 08:44:11 -0700616 //Clamp //Repeat //Mirror //None
617 /*Clamp */ {XTM_N, XTM_T, XTM_Y, XTM_N},
618 /*Repeat*/ {XTM_T, XTM_T, XTM_Y, XTM_N},
619 /*Mirror*/ {XTM_X, XTM_X, XTM_XY, XTM_X},
620 /*None */ {XTM_N, XTM_N, XTM_Y, XTM_N},
bungeman@google.comb29c8832011-10-10 13:19:10 +0000621};
622
623HRESULT SkXPSDevice::createXpsImageBrush(
624 const SkBitmap& bitmap,
625 const SkMatrix& localMatrix,
626 const SkShader::TileMode (&xy)[2],
627 const SkAlpha alpha,
628 IXpsOMTileBrush** xpsBrush) {
629 SkDynamicMemoryWStream write;
Hal Canarydb683012016-11-23 08:55:18 -0700630 if (!SkEncodeImage(&write, bitmap, SkEncodedImageFormat::kPNG, 100)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000631 HRM(E_FAIL, "Unable to encode bitmap as png.");
632 }
633 SkMemoryStream* read = new SkMemoryStream;
reed42943c82016-09-12 12:01:44 -0700634 read->setData(write.detachAsData());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000635 SkTScopedComPtr<IStream> readWrapper;
636 HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
637 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000638
bungeman@google.comb29c8832011-10-10 13:19:10 +0000639 const size_t size =
640 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
641 wchar_t buffer[size];
642 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400643 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000644 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000645
bungeman@google.comb29c8832011-10-10 13:19:10 +0000646 SkTScopedComPtr<IOpcPartUri> imagePartUri;
647 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
648 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000649
bungeman@google.comb29c8832011-10-10 13:19:10 +0000650 SkTScopedComPtr<IXpsOMImageResource> imageResource;
651 HRM(this->fXpsFactory->CreateImageResource(
652 readWrapper.get(),
653 XPS_IMAGE_TYPE_PNG,
654 imagePartUri.get(),
655 &imageResource),
656 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000657
bungeman@google.comb29c8832011-10-10 13:19:10 +0000658 XPS_RECT bitmapRect = {
659 0.0, 0.0,
660 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
661 };
662 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
663 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
664 &bitmapRect, &bitmapRect,
665 &xpsImageBrush),
666 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000667
bungeman@google.comb29c8832011-10-10 13:19:10 +0000668 if (SkShader::kClamp_TileMode != xy[0] &&
669 SkShader::kClamp_TileMode != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000670
bungeman@google.comb29c8832011-10-10 13:19:10 +0000671 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
672 "Could not set image tile mode");
673 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
674 "Could not set image opacity.");
675 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
676 } else {
677 //TODO(bungeman): compute how big this really needs to be.
678 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
679 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
680 const SkScalar bWidth = SkIntToScalar(bitmap.width());
681 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000682
bungeman@google.comb29c8832011-10-10 13:19:10 +0000683 //create brush canvas
684 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
685 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
686 "Could not create image brush canvas.");
687 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
688 HRM(brushCanvas->GetVisuals(&brushVisuals),
689 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000690
bungeman@google.comb29c8832011-10-10 13:19:10 +0000691 //create central figure
692 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
693 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
694 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000695
bungeman@google.comb29c8832011-10-10 13:19:10 +0000696 SkTScopedComPtr<IXpsOMPath> centralPath;
697 HR(this->createPath(centralFigure.get(),
698 brushVisuals.get(),
699 &centralPath));
700 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
701 "Could not set tile mode for image brush central path.");
702 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
703 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000704
bungeman@google.comb29c8832011-10-10 13:19:10 +0000705 //add left/right
706 if (SkShader::kClamp_TileMode == xy[0]) {
707 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
708 XPS_RECT leftImageViewBox = {
709 0.0, 0.0,
710 1.0, static_cast<FLOAT>(bitmap.height()),
711 };
712 HR(this->sideOfClamp(leftArea, leftImageViewBox,
713 imageResource.get(),
714 brushVisuals.get()));
715
716 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
717 XPS_RECT rightImageViewBox = {
718 bitmap.width() - 1.0f, 0.0f,
719 1.0f, static_cast<FLOAT>(bitmap.height()),
720 };
721 HR(this->sideOfClamp(rightArea, rightImageViewBox,
722 imageResource.get(),
723 brushVisuals.get()));
724 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000725
bungeman@google.comb29c8832011-10-10 13:19:10 +0000726 //add top/bottom
727 if (SkShader::kClamp_TileMode == xy[1]) {
728 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
729 XPS_RECT topImageViewBox = {
730 0.0, 0.0,
731 static_cast<FLOAT>(bitmap.width()), 1.0,
732 };
733 HR(this->sideOfClamp(topArea, topImageViewBox,
734 imageResource.get(),
735 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000736
bungeman@google.comb29c8832011-10-10 13:19:10 +0000737 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
738 XPS_RECT bottomImageViewBox = {
739 0.0f, bitmap.height() - 1.0f,
740 static_cast<FLOAT>(bitmap.width()), 1.0f,
741 };
742 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
743 imageResource.get(),
744 brushVisuals.get()));
745 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000746
bungeman@google.comb29c8832011-10-10 13:19:10 +0000747 //add tl, tr, bl, br
748 if (SkShader::kClamp_TileMode == xy[0] &&
749 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000750
bungeman@google.comb29c8832011-10-10 13:19:10 +0000751 SkAutoLockPixels alp(bitmap);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000752
bungeman@google.comb29c8832011-10-10 13:19:10 +0000753 const SkColor tlColor = bitmap.getColor(0,0);
754 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
755 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000756
bungeman@google.comb29c8832011-10-10 13:19:10 +0000757 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
758 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
759 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000760
bungeman@google.comb29c8832011-10-10 13:19:10 +0000761 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
762 bitmap.height()-1);
763 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
764 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000765
bungeman@google.comb29c8832011-10-10 13:19:10 +0000766 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
767 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
768 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
769 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000770
bungeman@google.comb29c8832011-10-10 13:19:10 +0000771 //create visual brush from canvas
772 XPS_RECT bound = {};
773 if (SkShader::kClamp_TileMode == xy[0] &&
774 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000775
bungeman@google.comb29c8832011-10-10 13:19:10 +0000776 bound.x = BIG_F / -2;
777 bound.y = BIG_F / -2;
778 bound.width = BIG_F;
779 bound.height = BIG_F;
780 } else if (SkShader::kClamp_TileMode == xy[0]) {
781 bound.x = BIG_F / -2;
782 bound.y = 0.0f;
783 bound.width = BIG_F;
784 bound.height = static_cast<FLOAT>(bitmap.height());
785 } else if (SkShader::kClamp_TileMode == xy[1]) {
786 bound.x = 0;
787 bound.y = BIG_F / -2;
788 bound.width = static_cast<FLOAT>(bitmap.width());
789 bound.height = BIG_F;
790 }
791 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
792 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
793 "Could not create visual brush for image brush.");
794 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
795 "Could not set canvas on visual brush for image brush.");
796 HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
797 "Could not set tile mode on visual brush for image brush.");
798 HRM(clampBrush->SetOpacity(alpha / 255.0f),
799 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000800
bungeman@google.comb29c8832011-10-10 13:19:10 +0000801 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
802 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000803
bungeman@google.comb29c8832011-10-10 13:19:10 +0000804 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
805 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
bsalomon49f085d2014-09-05 13:34:00 -0700806 if (xpsMatrixToUse.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000807 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
808 "Could not set transform for image brush.");
809 } else {
810 //TODO(bungeman): perspective bitmaps in general.
811 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000812
bungeman@google.comb29c8832011-10-10 13:19:10 +0000813 return S_OK;
814}
815
816HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
817 const SkScalar offset,
818 IXpsOMGradientStop** xpsGradStop) {
819 XPS_COLOR gradStopXpsColor = xps_color(skColor);
820 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
halcanary96fcdcc2015-08-27 07:41:13 -0700821 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000822 SkScalarToFLOAT(offset),
823 xpsGradStop),
824 "Could not create gradient stop.");
825 return S_OK;
826}
827
828HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
829 const SkAlpha alpha,
830 const SkMatrix& localMatrix,
831 IXpsOMMatrixTransform* xpsMatrix,
832 IXpsOMBrush** xpsBrush) {
833 XPS_POINT startPoint;
834 XPS_POINT endPoint;
bsalomon49f085d2014-09-05 13:34:00 -0700835 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000836 startPoint = xps_point(info.fPoint[0]);
837 endPoint = xps_point(info.fPoint[1]);
838 } else {
839 transform_offsets(info.fColorOffsets, info.fColorCount,
840 info.fPoint[0], info.fPoint[1],
841 localMatrix);
842 startPoint = xps_point(info.fPoint[0], localMatrix);
843 endPoint = xps_point(info.fPoint[1], localMatrix);
844 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000845
bungeman@google.comb29c8832011-10-10 13:19:10 +0000846 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
847 HR(createXpsGradientStop(info.fColors[0],
848 info.fColorOffsets[0],
849 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000850
bungeman@google.comb29c8832011-10-10 13:19:10 +0000851 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
852 HR(createXpsGradientStop(info.fColors[1],
853 info.fColorOffsets[1],
854 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000855
bungeman@google.comb29c8832011-10-10 13:19:10 +0000856 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
857 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
858 gradStop1.get(),
859 &startPoint,
860 &endPoint,
861 &gradientBrush),
862 "Could not create linear gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700863 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000864 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
865 "Could not set transform on linear gradient brush.");
866 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000867
bungeman@google.comb29c8832011-10-10 13:19:10 +0000868 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
869 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
870 "Could not get linear gradient stop collection.");
871 for (int i = 2; i < info.fColorCount; ++i) {
872 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
873 HR(createXpsGradientStop(info.fColors[i],
874 info.fColorOffsets[i],
875 &gradStop));
876 HRM(gradStopCollection->Append(gradStop.get()),
877 "Could not add linear gradient stop.");
878 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000879
bungeman@google.comb29c8832011-10-10 13:19:10 +0000880 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
881 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000882
bungeman@google.comb29c8832011-10-10 13:19:10 +0000883 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
884 "Could not set opacity of linear gradient brush.");
885 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000886
bungeman@google.comb29c8832011-10-10 13:19:10 +0000887 return S_OK;
888}
889
890HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
891 const SkAlpha alpha,
892 const SkMatrix& localMatrix,
893 IXpsOMMatrixTransform* xpsMatrix,
894 IXpsOMBrush** xpsBrush) {
895 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
896 HR(createXpsGradientStop(info.fColors[0],
897 info.fColorOffsets[0],
898 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000899
bungeman@google.comb29c8832011-10-10 13:19:10 +0000900 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
901 HR(createXpsGradientStop(info.fColors[1],
902 info.fColorOffsets[1],
903 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000904
bungeman@google.comb29c8832011-10-10 13:19:10 +0000905 //TODO: figure out how to fake better if not affine
906 XPS_POINT centerPoint;
907 XPS_POINT gradientOrigin;
908 XPS_SIZE radiiSizes;
bsalomon49f085d2014-09-05 13:34:00 -0700909 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000910 centerPoint = xps_point(info.fPoint[0]);
911 gradientOrigin = xps_point(info.fPoint[0]);
912 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
913 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
914 } else {
915 centerPoint = xps_point(info.fPoint[0], localMatrix);
916 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000917
bungeman@google.comb29c8832011-10-10 13:19:10 +0000918 SkScalar radius = info.fRadius[0];
919 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000920
bungeman@google.comb29c8832011-10-10 13:19:10 +0000921 vec[0].set(radius, 0);
922 vec[1].set(0, radius);
923 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000924
bungeman@google.comb29c8832011-10-10 13:19:10 +0000925 SkScalar d0 = vec[0].length();
926 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000927
bungeman@google.comb29c8832011-10-10 13:19:10 +0000928 radiiSizes.width = SkScalarToFLOAT(d0);
929 radiiSizes.height = SkScalarToFLOAT(d1);
930 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000931
bungeman@google.comb29c8832011-10-10 13:19:10 +0000932 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
933 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
934 gradStop1.get(),
935 &centerPoint,
936 &gradientOrigin,
937 &radiiSizes,
938 &gradientBrush),
939 "Could not create radial gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700940 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000941 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
942 "Could not set transform on radial gradient brush.");
943 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000944
bungeman@google.comb29c8832011-10-10 13:19:10 +0000945 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
946 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
947 "Could not get radial gradient stop collection.");
948 for (int i = 2; i < info.fColorCount; ++i) {
949 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
950 HR(createXpsGradientStop(info.fColors[i],
951 info.fColorOffsets[i],
952 &gradStop));
953 HRM(gradStopCollection->Append(gradStop.get()),
954 "Could not add radial gradient stop.");
955 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000956
bungeman@google.comb29c8832011-10-10 13:19:10 +0000957 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
958 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000959
bungeman@google.comb29c8832011-10-10 13:19:10 +0000960 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
961 "Could not set opacity of radial gradient brush.");
962 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000963
bungeman@google.comb29c8832011-10-10 13:19:10 +0000964 return S_OK;
965}
966
967HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
968 IXpsOMBrush** brush,
969 const SkMatrix* parentTransform) {
970 const SkShader *shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -0700971 if (nullptr == shader) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000972 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
973 return S_OK;
974 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000975
bungeman@google.comb29c8832011-10-10 13:19:10 +0000976 //Gradient shaders.
977 SkShader::GradientInfo info;
978 info.fColorCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700979 info.fColors = nullptr;
980 info.fColorOffsets = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000981 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000982
bungeman@google.comb29c8832011-10-10 13:19:10 +0000983 if (SkShader::kNone_GradientType == gradientType) {
984 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000985
bungeman@google.comb29c8832011-10-10 13:19:10 +0000986 } else if (SkShader::kColor_GradientType == gradientType) {
987 SkASSERT(1 == info.fColorCount);
988 SkColor color;
989 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +0000990 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000991 SkAlpha alpha = skPaint.getAlpha();
992 HR(this->createXpsSolidColorBrush(color, alpha, brush));
993 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000994
bungeman@google.comb29c8832011-10-10 13:19:10 +0000995 } else {
996 if (info.fColorCount == 0) {
997 const SkColor color = skPaint.getColor();
998 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
999 return S_OK;
1000 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001001
bungeman@google.comb29c8832011-10-10 13:19:10 +00001002 SkAutoTArray<SkColor> colors(info.fColorCount);
1003 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1004 info.fColors = colors.get();
1005 info.fColorOffsets = colorOffsets.get();
1006 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001007
bungeman@google.comb29c8832011-10-10 13:19:10 +00001008 if (1 == info.fColorCount) {
1009 SkColor color = info.fColors[0];
1010 SkAlpha alpha = skPaint.getAlpha();
1011 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1012 return S_OK;
1013 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001014
bsalomon@google.comf94b3a42012-10-31 18:09:01 +00001015 SkMatrix localMatrix = shader->getLocalMatrix();
bsalomon49f085d2014-09-05 13:34:00 -07001016 if (parentTransform) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001017 localMatrix.preConcat(*parentTransform);
1018 }
1019 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1020 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001021
bungeman@google.comb29c8832011-10-10 13:19:10 +00001022 if (SkShader::kLinear_GradientType == gradientType) {
1023 HR(this->createXpsLinearGradient(info,
1024 skPaint.getAlpha(),
1025 localMatrix,
1026 xpsMatrixToUse.get(),
1027 brush));
1028 return S_OK;
1029 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001030
bungeman@google.comb29c8832011-10-10 13:19:10 +00001031 if (SkShader::kRadial_GradientType == gradientType) {
1032 HR(this->createXpsRadialGradient(info,
1033 skPaint.getAlpha(),
1034 localMatrix,
1035 xpsMatrixToUse.get(),
1036 brush));
1037 return S_OK;
1038 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001039
reed71a6cbf2015-05-04 08:32:51 -07001040 if (SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001041 //simple if affine and one is 0, otherwise will have to fake
1042 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001043
bungeman@google.comb29c8832011-10-10 13:19:10 +00001044 if (SkShader::kSweep_GradientType == gradientType) {
1045 //have to fake
1046 }
1047 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001048
bungeman@google.comb29c8832011-10-10 13:19:10 +00001049 SkBitmap outTexture;
1050 SkMatrix outMatrix;
1051 SkShader::TileMode xy[2];
Mike Reed627778d2016-09-28 17:13:38 -04001052 SkImage* image = shader->isAImage(&outMatrix, xy);
1053 if (image && image->asLegacyBitmap(&outTexture, SkImage::kRO_LegacyBitmapMode)) {
reedf5822822015-08-19 11:46:38 -07001054 //TODO: outMatrix??
1055 SkMatrix localMatrix = shader->getLocalMatrix();
1056 if (parentTransform) {
1057 localMatrix.preConcat(*parentTransform);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001058 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001059
reedf5822822015-08-19 11:46:38 -07001060 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1061 HR(this->createXpsImageBrush(outTexture,
1062 localMatrix,
1063 xy,
1064 skPaint.getAlpha(),
1065 &tileBrush));
1066
1067 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
1068 } else {
1069 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1070 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001071 return S_OK;
1072}
1073
1074static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1075 const bool zeroWidth = (0 == paint.getStrokeWidth());
1076 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001077
bungeman@google.comb29c8832011-10-10 13:19:10 +00001078 return paint.getPathEffect() ||
1079 paint.getMaskFilter() ||
1080 paint.getRasterizer() ||
1081 (stroke && (
1082 (matrix.hasPerspective() && !zeroWidth) ||
1083 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1084 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1085 paint.getStrokeMiter() < SK_ScalarSqrt2)
1086 ))
1087 ;
1088}
1089
1090HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1091 IXpsOMGeometryFigure** xpsRect) {
1092 const SkPoint points[4] = {
1093 { rect.fLeft, rect.fTop },
1094 { rect.fRight, rect.fTop },
1095 { rect.fRight, rect.fBottom },
1096 { rect.fLeft, rect.fBottom },
1097 };
1098 return this->createXpsQuad(points, stroke, fill, xpsRect);
1099}
1100HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1101 BOOL stroke, BOOL fill,
1102 IXpsOMGeometryFigure** xpsQuad) {
1103 // Define the start point.
1104 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001105
bungeman@google.comb29c8832011-10-10 13:19:10 +00001106 // Create the figure.
1107 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1108 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001109
bungeman@google.comb29c8832011-10-10 13:19:10 +00001110 // Define the type of each segment.
1111 XPS_SEGMENT_TYPE segmentTypes[3] = {
1112 XPS_SEGMENT_TYPE_LINE,
1113 XPS_SEGMENT_TYPE_LINE,
1114 XPS_SEGMENT_TYPE_LINE,
1115 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001116
bungeman@google.comb29c8832011-10-10 13:19:10 +00001117 // Define the x and y coordinates of each corner of the figure.
1118 FLOAT segmentData[6] = {
1119 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1120 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1121 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1122 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001123
bungeman@google.comb29c8832011-10-10 13:19:10 +00001124 // Describe if the segments are stroked.
1125 BOOL segmentStrokes[3] = {
1126 stroke, stroke, stroke,
1127 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001128
bungeman@google.comb29c8832011-10-10 13:19:10 +00001129 // Add the segment data to the figure.
1130 HRM((*xpsQuad)->SetSegments(
1131 3, 6,
1132 segmentTypes , segmentData, segmentStrokes),
1133 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001134
bungeman@google.comb29c8832011-10-10 13:19:10 +00001135 // Set the closed and filled properties of the figure.
1136 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1137 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001138
bungeman@google.comb29c8832011-10-10 13:19:10 +00001139 return S_OK;
1140}
1141
bungeman@google.comb29c8832011-10-10 13:19:10 +00001142void SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1143 size_t count, const SkPoint points[],
1144 const SkPaint& paint) {
1145 //This will call back into the device to do the drawing.
1146 d.drawPoints(mode, count, points, paint, true);
1147}
1148
1149void SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1150 int vertexCount, const SkPoint verts[],
1151 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001152 SkBlendMode, const uint16_t indices[],
bungeman@google.comb29c8832011-10-10 13:19:10 +00001153 int indexCount, const SkPaint& paint) {
1154 //TODO: override this for XPS
1155 SkDEBUGF(("XPS drawVertices not yet implemented."));
1156}
1157
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001158void SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001159 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001160
bungeman@google.comb29c8832011-10-10 13:19:10 +00001161 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001162 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1163 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1164 if (paint->getStyle() != SkPaint::kFill_Style) {
1165 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001166 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001167
bungeman@google.comb29c8832011-10-10 13:19:10 +00001168 this->internalDrawRect(d, r, false, *fillPaint);
1169}
1170
1171void SkXPSDevice::drawRect(const SkDraw& d,
1172 const SkRect& r,
1173 const SkPaint& paint) {
1174 this->internalDrawRect(d, r, true, paint);
1175}
1176
scroggo@google.comcac8d012013-11-12 17:10:02 +00001177void SkXPSDevice::drawRRect(const SkDraw& d,
1178 const SkRRect& rr,
1179 const SkPaint& paint) {
1180 SkPath path;
1181 path.addRRect(rr);
halcanary96fcdcc2015-08-27 07:41:13 -07001182 this->drawPath(d, path, paint, nullptr, true);
scroggo@google.comcac8d012013-11-12 17:10:02 +00001183}
1184
bungeman@google.comb29c8832011-10-10 13:19:10 +00001185void SkXPSDevice::internalDrawRect(const SkDraw& d,
1186 const SkRect& r,
1187 bool transformRect,
1188 const SkPaint& paint) {
1189 //Exit early if there is nothing to draw.
reed1e7f5e72016-04-27 07:49:17 -07001190 if (d.fRC->isEmpty() ||
reed374772b2016-10-05 17:33:02 -07001191 (paint.getAlpha() == 0 && paint.isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001192 return;
1193 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001194
bungeman@google.comb29c8832011-10-10 13:19:10 +00001195 //Path the rect if we can't optimize it.
1196 if (rect_must_be_pathed(paint, *d.fMatrix)) {
1197 SkPath tmp;
1198 tmp.addRect(r);
1199 tmp.setFillType(SkPath::kWinding_FillType);
halcanary96fcdcc2015-08-27 07:41:13 -07001200 this->drawPath(d, tmp, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001201 return;
1202 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001203
bungeman@google.comb29c8832011-10-10 13:19:10 +00001204 //Create the shaded path.
1205 SkTScopedComPtr<IXpsOMPath> shadedPath;
1206 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1207 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001208
bungeman@google.comb29c8832011-10-10 13:19:10 +00001209 //Create the shaded geometry.
1210 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1211 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1212 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001213
bungeman@google.comb29c8832011-10-10 13:19:10 +00001214 //Add the geometry to the shaded path.
1215 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1216 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001217
bungeman@google.comb29c8832011-10-10 13:19:10 +00001218 //Set the brushes.
1219 BOOL fill = FALSE;
1220 BOOL stroke = FALSE;
1221 HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001222
bungeman@google.comb29c8832011-10-10 13:19:10 +00001223 bool xpsTransformsPath = true;
1224 //Transform the geometry.
1225 if (transformRect && xpsTransformsPath) {
1226 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1227 HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1228 if (xpsTransform.get()) {
1229 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1230 "Could not set transform for rect.");
1231 } else {
1232 xpsTransformsPath = false;
1233 }
1234 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001235
bungeman@google.comb29c8832011-10-10 13:19:10 +00001236 //Create the figure.
1237 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1238 {
1239 SkPoint points[4] = {
1240 { r.fLeft, r.fTop },
1241 { r.fLeft, r.fBottom },
1242 { r.fRight, r.fBottom },
1243 { r.fRight, r.fTop },
1244 };
1245 if (!xpsTransformsPath && transformRect) {
1246 d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1247 }
1248 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1249 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001250
bungeman@google.comb29c8832011-10-10 13:19:10 +00001251 //Get the figures of the shaded geometry.
1252 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1253 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1254 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001255
bungeman@google.comb29c8832011-10-10 13:19:10 +00001256 //Add the figure to the shaded geometry figures.
1257 HRVM(shadedFigures->Append(rectFigure.get()),
1258 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001259
bungeman@google.comb29c8832011-10-10 13:19:10 +00001260 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001261
bungeman@google.comb29c8832011-10-10 13:19:10 +00001262 //Add the shaded path to the current visuals.
1263 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1264 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1265 "Could not get current visuals for rect.");
1266 HRVM(currentVisuals->Append(shadedPath.get()),
1267 "Could not add rect to current visuals.");
1268}
1269
1270static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1271 const SkTDArray<BOOL>& segmentStrokes,
1272 const SkTDArray<FLOAT>& segmentData,
1273 BOOL stroke, BOOL fill,
1274 IXpsOMGeometryFigure* figure,
1275 IXpsOMGeometryFigureCollection* figures) {
1276 // Add the segment data to the figure.
1277 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1278 segmentTypes.begin() , segmentData.begin(),
1279 segmentStrokes.begin()),
1280 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001281
bungeman@google.comb29c8832011-10-10 13:19:10 +00001282 // Set the closed and filled properties of the figure.
1283 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1284 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001285
bungeman@google.comb29c8832011-10-10 13:19:10 +00001286 // Add the figure created above to this geometry.
1287 HRM(figures->Append(figure), "Could not add path to geometry.");
1288 return S_OK;
1289}
1290
1291HRESULT SkXPSDevice::addXpsPathGeometry(
1292 IXpsOMGeometryFigureCollection* xpsFigures,
1293 BOOL stroke, BOOL fill, const SkPath& path) {
1294 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1295 SkTDArray<BOOL> segmentStrokes;
1296 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001297
bungeman@google.comb29c8832011-10-10 13:19:10 +00001298 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1299 SkPath::Iter iter(path, true);
1300 SkPoint points[4];
1301 SkPath::Verb verb;
1302 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1303 switch (verb) {
1304 case SkPath::kMove_Verb: {
bsalomon49f085d2014-09-05 13:34:00 -07001305 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001306 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1307 stroke, fill,
1308 xpsFigure.get() , xpsFigures));
1309 xpsFigure.reset();
1310 segmentTypes.rewind();
1311 segmentStrokes.rewind();
1312 segmentData.rewind();
1313 }
1314 // Define the start point.
1315 XPS_POINT startPoint = xps_point(points[0]);
1316 // Create the figure.
1317 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1318 &xpsFigure),
1319 "Could not create path geometry figure.");
1320 break;
1321 }
1322 case SkPath::kLine_Verb:
1323 if (iter.isCloseLine()) break; //ignore the line, auto-closed
1324 segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1325 segmentStrokes.push(stroke);
1326 segmentData.push(SkScalarToFLOAT(points[1].fX));
1327 segmentData.push(SkScalarToFLOAT(points[1].fY));
1328 break;
1329 case SkPath::kQuad_Verb:
1330 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1331 segmentStrokes.push(stroke);
1332 segmentData.push(SkScalarToFLOAT(points[1].fX));
1333 segmentData.push(SkScalarToFLOAT(points[1].fY));
1334 segmentData.push(SkScalarToFLOAT(points[2].fX));
1335 segmentData.push(SkScalarToFLOAT(points[2].fY));
1336 break;
1337 case SkPath::kCubic_Verb:
1338 segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1339 segmentStrokes.push(stroke);
1340 segmentData.push(SkScalarToFLOAT(points[1].fX));
1341 segmentData.push(SkScalarToFLOAT(points[1].fY));
1342 segmentData.push(SkScalarToFLOAT(points[2].fX));
1343 segmentData.push(SkScalarToFLOAT(points[2].fY));
1344 segmentData.push(SkScalarToFLOAT(points[3].fX));
1345 segmentData.push(SkScalarToFLOAT(points[3].fY));
1346 break;
halcanary47ef4d52015-03-03 09:13:09 -08001347 case SkPath::kConic_Verb: {
1348 const SkScalar tol = SK_Scalar1 / 4;
1349 SkAutoConicToQuads converter;
1350 const SkPoint* quads =
1351 converter.computeQuads(points, iter.conicWeight(), tol);
1352 for (int i = 0; i < converter.countQuads(); ++i) {
1353 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1354 segmentStrokes.push(stroke);
1355 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fX));
1356 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fY));
1357 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fX));
1358 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fY));
1359 }
1360 break;
1361 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001362 case SkPath::kClose_Verb:
1363 // we ignore these, and just get the whole segment from
1364 // the corresponding line/quad/cubic verbs
1365 break;
1366 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001367 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001368 break;
1369 }
1370 }
bsalomon49f085d2014-09-05 13:34:00 -07001371 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001372 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1373 stroke, fill,
1374 xpsFigure.get(), xpsFigures));
1375 }
1376 return S_OK;
1377}
1378
bungeman@google.comb29c8832011-10-10 13:19:10 +00001379void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1380 SkMatrix* matrix,
1381 SkVector* ppuScale,
1382 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001383 //This action is in unit space, but the ppm is specified in physical space.
reed80ea19c2015-05-12 10:37:34 -07001384 ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
1385 fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001386
bungeman@google.comb29c8832011-10-10 13:19:10 +00001387 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001388
bungeman@google.comb29c8832011-10-10 13:19:10 +00001389 const SkIRect& irect = clip;
Mike Reed8be952a2017-02-13 20:44:33 -05001390 SkRect clipRect = SkRect::MakeLTRB(SkIntToScalar(irect.fLeft) * ppuScale->fX,
1391 SkIntToScalar(irect.fTop) * ppuScale->fY,
1392 SkIntToScalar(irect.fRight) * ppuScale->fX,
1393 SkIntToScalar(irect.fBottom) * ppuScale->fY);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001394 clipRect.roundOut(clipIRect);
1395}
1396
1397HRESULT SkXPSDevice::applyMask(const SkDraw& d,
1398 const SkMask& mask,
1399 const SkVector& ppuScale,
1400 IXpsOMPath* shadedPath) {
1401 //Get the geometry object.
1402 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1403 HRM(shadedPath->GetGeometry(&shadedGeometry),
1404 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001405
bungeman@google.comb29c8832011-10-10 13:19:10 +00001406 //Get the figures from the geometry.
1407 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1408 HRM(shadedGeometry->GetFigures(&shadedFigures),
1409 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001410
bungeman@google.comb29c8832011-10-10 13:19:10 +00001411 SkMatrix m;
1412 m.reset();
1413 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1414 SkIntToScalar(mask.fBounds.fTop));
1415 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001416
bungeman@google.comb29c8832011-10-10 13:19:10 +00001417 SkShader::TileMode xy[2];
1418 xy[0] = (SkShader::TileMode)3;
1419 xy[1] = (SkShader::TileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001420
bungeman@google.comb29c8832011-10-10 13:19:10 +00001421 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001422 bm.installMaskPixels(mask);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001423
bungeman@google.comb29c8832011-10-10 13:19:10 +00001424 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1425 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1426 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1427 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001428
bungeman@google.comb29c8832011-10-10 13:19:10 +00001429 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1430 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1431 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1432 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1433 "Could not create mask shaded figure.");
1434 HRM(shadedFigures->Append(shadedFigure.get()),
1435 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001436
bungeman@google.comb29c8832011-10-10 13:19:10 +00001437 HR(this->clip(shadedPath, d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001438
bungeman@google.comb29c8832011-10-10 13:19:10 +00001439 //Add the path to the active visual collection.
1440 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1441 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1442 "Could not get mask current visuals.");
1443 HRM(currentVisuals->Append(shadedPath),
1444 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001445
bungeman@google.comb29c8832011-10-10 13:19:10 +00001446 return S_OK;
1447}
1448
1449HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1450 const SkPaint& shaderPaint,
1451 const SkMatrix& matrix,
1452 BOOL* fill, BOOL* stroke) {
1453 *fill = FALSE;
1454 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001455
bungeman@google.comb29c8832011-10-10 13:19:10 +00001456 const SkPaint::Style style = shaderPaint.getStyle();
1457 const bool hasFill = SkPaint::kFill_Style == style
1458 || SkPaint::kStrokeAndFill_Style == style;
1459 const bool hasStroke = SkPaint::kStroke_Style == style
1460 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001461
bungeman@google.comb29c8832011-10-10 13:19:10 +00001462 //TODO(bungeman): use dictionaries and lookups.
1463 if (hasFill) {
1464 *fill = TRUE;
1465 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1466 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1467 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1468 "Could not set fill for shaded path.");
1469 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001470
bungeman@google.comb29c8832011-10-10 13:19:10 +00001471 if (hasStroke) {
1472 *stroke = TRUE;
1473 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1474 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1475 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1476 "Could not set stroke brush for shaded path.");
1477 HRM(shadedPath->SetStrokeThickness(
1478 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1479 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001480
bungeman@google.comb29c8832011-10-10 13:19:10 +00001481 if (0 == shaderPaint.getStrokeWidth()) {
1482 //XPS hair width is a hack. (XPS Spec 11.6.12).
1483 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1484 HRM(shadedPath->GetStrokeDashes(&dashes),
1485 "Could not set dashes for shaded path.");
1486 XPS_DASH dash;
1487 dash.length = 1.0;
1488 dash.gap = 0.0;
1489 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1490 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1491 "Could not set dash offset for shaded path.");
1492 }
1493 }
1494 return S_OK;
1495}
1496
1497void SkXPSDevice::drawPath(const SkDraw& d,
1498 const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001499 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001500 const SkMatrix* prePathMatrix,
1501 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001502 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1503
bungeman@google.comb29c8832011-10-10 13:19:10 +00001504 // nothing to draw
reed1e7f5e72016-04-27 07:49:17 -07001505 if (d.fRC->isEmpty() ||
reed374772b2016-10-05 17:33:02 -07001506 (paint->getAlpha() == 0 && paint->isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001507 return;
1508 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001509
bungeman@google.comb29c8832011-10-10 13:19:10 +00001510 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001511 const bool paintHasPathEffect = paint->getPathEffect()
1512 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001513
bungeman@google.comb29c8832011-10-10 13:19:10 +00001514 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1515 SkMatrix matrix = *d.fMatrix;
1516 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1517 if (prePathMatrix) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001518 if (paintHasPathEffect || paint->getRasterizer()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001519 if (!pathIsMutable) {
1520 skeletalPath = &modifiedPath;
1521 pathIsMutable = true;
1522 }
1523 platonicPath.transform(*prePathMatrix, skeletalPath);
1524 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001525 matrix.preConcat(*prePathMatrix);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001526 }
1527 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001528
bungeman@google.comb29c8832011-10-10 13:19:10 +00001529 //Apply path effect [Skeletal-path -> Fillable-path].
1530 SkPath* fillablePath = skeletalPath;
1531 if (paintHasPathEffect) {
1532 if (!pathIsMutable) {
1533 fillablePath = &modifiedPath;
1534 pathIsMutable = true;
1535 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001536 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001537
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001538 SkPaint* writablePaint = paint.writable();
halcanary96fcdcc2015-08-27 07:41:13 -07001539 writablePaint->setPathEffect(nullptr);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001540 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001541 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001542 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001543 writablePaint->setStyle(SkPaint::kStroke_Style);
1544 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001545 }
1546 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001547
bungeman@google.comb29c8832011-10-10 13:19:10 +00001548 //Create the shaded path. This will be the path which is painted.
1549 SkTScopedComPtr<IXpsOMPath> shadedPath;
1550 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1551 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001552
bungeman@google.comb29c8832011-10-10 13:19:10 +00001553 //Create the geometry for the shaded path.
1554 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1555 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1556 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001557
bungeman@google.comb29c8832011-10-10 13:19:10 +00001558 //Add the geometry to the shaded path.
1559 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1560 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001561
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001562 SkRasterizer* rasterizer = paint->getRasterizer();
1563 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001564
1565 //Determine if we will draw or shade and mask.
1566 if (rasterizer || filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001567 if (paint->getStyle() != SkPaint::kFill_Style) {
1568 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001569 }
1570 }
1571
bungeman@google.comb29c8832011-10-10 13:19:10 +00001572 //Set the brushes.
1573 BOOL fill;
1574 BOOL stroke;
1575 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001576 *paint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001577 *d.fMatrix,
1578 &fill,
1579 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001580
bungeman@google.comb29c8832011-10-10 13:19:10 +00001581 //Rasterizer
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001582 if (rasterizer) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001583 SkIRect clipIRect;
1584 SkVector ppuScale;
1585 this->convertToPpm(filter,
1586 &matrix,
1587 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001588 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001589 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001590
halcanary96fcdcc2015-08-27 07:41:13 -07001591 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001592
bungeman@google.comb29c8832011-10-10 13:19:10 +00001593 //[Fillable-path -> Mask]
1594 SkMask rasteredMask;
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001595 if (rasterizer->rasterize(
bungeman@google.comb29c8832011-10-10 13:19:10 +00001596 *fillablePath,
1597 matrix,
1598 &clipIRect,
1599 filter, //just to compute how much to draw.
1600 &rasteredMask,
1601 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001602
bungeman@google.comb29c8832011-10-10 13:19:10 +00001603 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1604 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001605
bungeman@google.comb29c8832011-10-10 13:19:10 +00001606 //[Mask -> Mask]
1607 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001608 if (filter && filter->filterMask(&filteredMask, *mask, *d.fMatrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001609 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001610 }
1611 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001612
bungeman@google.comb29c8832011-10-10 13:19:10 +00001613 //Draw mask.
1614 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1615 }
1616 return;
1617 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001618
bungeman@google.comb29c8832011-10-10 13:19:10 +00001619 //Mask filter
1620 if (filter) {
1621 SkIRect clipIRect;
1622 SkVector ppuScale;
1623 this->convertToPpm(filter,
1624 &matrix,
1625 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001626 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001627 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001628
bungeman@google.comb29c8832011-10-10 13:19:10 +00001629 //[Fillable-path -> Pixel-path]
1630 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1631 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001632
halcanary96fcdcc2015-08-27 07:41:13 -07001633 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001634
bsalomon055e1922016-05-06 07:22:58 -07001635 SkASSERT(SkPaint::kFill_Style == paint->getStyle() ||
1636 (SkPaint::kStroke_Style == paint->getStyle() && 0 == paint->getStrokeWidth()));
1637 SkStrokeRec::InitStyle style = (SkPaint::kFill_Style == paint->getStyle())
1638 ? SkStrokeRec::kFill_InitStyle
1639 : SkStrokeRec::kHairline_InitStyle;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001640 //[Pixel-path -> Mask]
1641 SkMask rasteredMask;
1642 if (SkDraw::DrawToMask(
1643 *pixelPath,
1644 &clipIRect,
1645 filter, //just to compute how much to draw.
1646 &matrix,
1647 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001648 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon055e1922016-05-06 07:22:58 -07001649 style)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001650
bungeman@google.comb29c8832011-10-10 13:19:10 +00001651 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1652 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001653
bungeman@google.comb29c8832011-10-10 13:19:10 +00001654 //[Mask -> Mask]
1655 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001656 if (filter->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001657 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001658 }
1659 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001660
bungeman@google.comb29c8832011-10-10 13:19:10 +00001661 //Draw mask.
1662 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1663 }
1664 return;
1665 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001666
bungeman@google.comb29c8832011-10-10 13:19:10 +00001667 //Get the figures from the shaded geometry.
1668 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1669 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1670 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001671
bungeman@google.comb29c8832011-10-10 13:19:10 +00001672 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001673
bungeman@google.comb29c8832011-10-10 13:19:10 +00001674 //Set the fill rule.
bungeman76db31a2014-08-25 07:31:53 -07001675 SkPath* xpsCompatiblePath = fillablePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001676 XPS_FILL_RULE xpsFillRule;
bungeman76db31a2014-08-25 07:31:53 -07001677 switch (fillablePath->getFillType()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001678 case SkPath::kWinding_FillType:
1679 xpsFillRule = XPS_FILL_RULE_NONZERO;
1680 break;
1681 case SkPath::kEvenOdd_FillType:
1682 xpsFillRule = XPS_FILL_RULE_EVENODD;
1683 break;
1684 case SkPath::kInverseWinding_FillType: {
bungeman76db31a2014-08-25 07:31:53 -07001685 //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
1686 if (!pathIsMutable) {
1687 xpsCompatiblePath = &modifiedPath;
1688 pathIsMutable = true;
1689 }
1690 if (!Simplify(*fillablePath, xpsCompatiblePath)) {
1691 SkDEBUGF(("Could not simplify inverse winding path."));
1692 return;
1693 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001694 }
bungeman76db31a2014-08-25 07:31:53 -07001695 // The xpsCompatiblePath is noW inverse even odd, so fall through.
bungeman@google.comb29c8832011-10-10 13:19:10 +00001696 case SkPath::kInverseEvenOdd_FillType: {
1697 const SkRect universe = SkRect::MakeLTRB(
1698 0, 0,
1699 this->fCurrentCanvasSize.fWidth,
1700 this->fCurrentCanvasSize.fHeight);
1701 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1702 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1703 HRVM(shadedFigures->Append(addOneFigure.get()),
1704 "Could not add even-odd flip figure to shaded path.");
1705 xpsTransformsPath = false;
1706 xpsFillRule = XPS_FILL_RULE_EVENODD;
1707 break;
1708 }
1709 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001710 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001711 }
1712 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1713 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001714
bungeman@google.comb29c8832011-10-10 13:19:10 +00001715 //Create the XPS transform, if possible.
1716 if (xpsTransformsPath) {
1717 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1718 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001719
bungeman@google.comb29c8832011-10-10 13:19:10 +00001720 if (xpsTransform.get()) {
1721 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1722 "Could not set transform on shaded path.");
1723 } else {
1724 xpsTransformsPath = false;
1725 }
1726 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001727
bungeman76db31a2014-08-25 07:31:53 -07001728 SkPath* devicePath = xpsCompatiblePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001729 if (!xpsTransformsPath) {
1730 //[Fillable-path -> Device-path]
bungeman76db31a2014-08-25 07:31:53 -07001731 devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
1732 xpsCompatiblePath->transform(matrix, devicePath);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001733 }
1734 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1735 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001736
bungeman@google.comb29c8832011-10-10 13:19:10 +00001737 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001738
bungeman@google.comb29c8832011-10-10 13:19:10 +00001739 //Add the path to the active visual collection.
1740 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1741 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1742 "Could not get current visuals for shaded path.");
1743 HRVM(currentVisuals->Append(shadedPath.get()),
1744 "Could not add shaded path to current visuals.");
1745}
1746
1747HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1748 SkPath clipPath;
reed1e7f5e72016-04-27 07:49:17 -07001749 if (d.fRC->isBW()) {
1750 SkAssertResult(d.fRC->bwRgn().getBoundaryPath(&clipPath));
1751 } else {
1752 // Don't have a way to turn a AAClip into a path, so we just use the bounds.
1753 // TODO: consider using fClipStack instead?
1754 clipPath.addRect(SkRect::Make(d.fRC->getBounds()));
1755 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001756
bungeman@google.comb29c8832011-10-10 13:19:10 +00001757 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1758}
1759HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1760 const SkPath& clipPath,
1761 XPS_FILL_RULE fillRule) {
1762 //Create the geometry.
1763 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1764 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1765 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001766
bungeman@google.comb29c8832011-10-10 13:19:10 +00001767 //Get the figure collection of the geometry.
1768 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1769 HRM(clipGeometry->GetFigures(&clipFigures),
1770 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001771
bungeman@google.comb29c8832011-10-10 13:19:10 +00001772 //Create the figures into the geometry.
1773 HR(this->addXpsPathGeometry(
1774 clipFigures.get(),
1775 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001776
bungeman@google.comb29c8832011-10-10 13:19:10 +00001777 HRM(clipGeometry->SetFillRule(fillRule),
1778 "Could not set fill rule.");
1779 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1780 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001781
bungeman@google.comb29c8832011-10-10 13:19:10 +00001782 return S_OK;
1783}
1784
1785void SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001786 const SkMatrix& matrix, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001787 if (d.fRC->isEmpty()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001788 return;
1789 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001790
bungeman@google.comb29c8832011-10-10 13:19:10 +00001791 SkIRect srcRect;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001792 srcRect.set(0, 0, bitmap.width(), bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001793
bungeman@google.comb29c8832011-10-10 13:19:10 +00001794 //Create the new shaded path.
1795 SkTScopedComPtr<IXpsOMPath> shadedPath;
1796 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1797 "Could not create path for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001798
bungeman@google.comb29c8832011-10-10 13:19:10 +00001799 //Create the shaded geometry.
1800 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1801 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1802 "Could not create geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001803
bungeman@google.comb29c8832011-10-10 13:19:10 +00001804 //Add the shaded geometry to the shaded path.
1805 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1806 "Could not set the geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001807
bungeman@google.comb29c8832011-10-10 13:19:10 +00001808 //Get the shaded figures from the shaded geometry.
1809 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1810 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1811 "Could not get the figures for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001812
bungeman@google.comb29c8832011-10-10 13:19:10 +00001813 SkMatrix transform = matrix;
1814 transform.postConcat(*d.fMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001815
bungeman@google.comb29c8832011-10-10 13:19:10 +00001816 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1817 HRV(this->createXpsTransform(transform, &xpsTransform));
1818 if (xpsTransform.get()) {
1819 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1820 "Could not set transform for bitmap.");
1821 } else {
1822 //TODO: perspective that bitmap!
1823 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001824
bungeman@google.comb29c8832011-10-10 13:19:10 +00001825 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
bsalomon49f085d2014-09-05 13:34:00 -07001826 if (xpsTransform.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001827 const SkShader::TileMode xy[2] = {
1828 SkShader::kClamp_TileMode,
1829 SkShader::kClamp_TileMode,
1830 };
1831 SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001832 HRV(this->createXpsImageBrush(bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001833 transform,
1834 xy,
1835 paint.getAlpha(),
1836 &xpsImageBrush));
1837 HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
1838 "Could not set bitmap brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001839
bungeman@google.comb29c8832011-10-10 13:19:10 +00001840 const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
1841 SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1842 HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
1843 }
1844 HRVM(shadedFigures->Append(rectFigure.get()),
1845 "Could not add bitmap figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001846
bungeman@google.comb29c8832011-10-10 13:19:10 +00001847 //Get the current visual collection and add the shaded path to it.
1848 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1849 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1850 "Could not get current visuals for bitmap");
1851 HRVM(currentVisuals->Append(shadedPath.get()),
1852 "Could not add bitmap to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001853
bungeman@google.comb29c8832011-10-10 13:19:10 +00001854 HRV(this->clip(shadedPath.get(), d));
1855}
1856
1857void SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
1858 int x, int y,
1859 const SkPaint& paint) {
1860 //TODO: override this for XPS
1861 SkDEBUGF(("XPS drawSprite not yet implemented."));
1862}
1863
1864HRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
1865 TypefaceUse** typefaceUse) {
reed@google.com398de9a2013-03-21 21:43:51 +00001866 SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001867
bungeman@google.comb29c8832011-10-10 13:19:10 +00001868 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00001869 const SkFontID typefaceID = typeface->uniqueID();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001870 if (!this->fTypefaces.empty()) {
1871 TypefaceUse* current = &this->fTypefaces.front();
1872 const TypefaceUse* last = &this->fTypefaces.back();
1873 for (; current <= last; ++current) {
1874 if (current->typefaceId == typefaceID) {
1875 *typefaceUse = current;
1876 return S_OK;
1877 }
1878 }
1879 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001880
bungeman@google.comb29c8832011-10-10 13:19:10 +00001881 //TODO: create glyph only fonts
1882 //and let the host deal with what kind of font we're looking at.
1883 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001884
bungeman@google.comb29c8832011-10-10 13:19:10 +00001885 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001886 int ttcIndex;
1887 SkStream* fontData = typeface->openStream(&ttcIndex);
bungeman@google.com635091f2013-10-01 15:03:18 +00001888 //TODO: cannot handle FON fonts.
bungeman@google.com8cddc8d2012-01-03 22:36:33 +00001889 HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001890 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001891
bungeman@google.comb29c8832011-10-10 13:19:10 +00001892 const size_t size =
1893 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1894 wchar_t buffer[size];
1895 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -04001896 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001897 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001898
bungeman@google.comb29c8832011-10-10 13:19:10 +00001899 SkTScopedComPtr<IOpcPartUri> partUri;
1900 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1901 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001902
bungeman@google.comb29c8832011-10-10 13:19:10 +00001903 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1904 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1905 embedding,
1906 partUri.get(),
1907 FALSE,
1908 &xpsFontResource),
1909 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001910
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001911 //TODO: change openStream to return -1 for non-ttc, get rid of this.
1912 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1913 bool isTTC = (data &&
1914 fontData->getLength() >= sizeof(SkTTCFHeader) &&
1915 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1916
bungeman@google.comb29c8832011-10-10 13:19:10 +00001917 TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
1918 newTypefaceUse.typefaceId = typefaceID;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001919 newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001920 newTypefaceUse.fontData = fontData;
1921 newTypefaceUse.xpsFont = xpsFontResource.release();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001922
robertphillips8e0c1502015-07-07 10:28:43 -07001923 SkAutoGlyphCache agc(paint, &this->surfaceProps(), &SkMatrix::I());
bungeman@google.comb29c8832011-10-10 13:19:10 +00001924 SkGlyphCache* glyphCache = agc.getCache();
1925 unsigned int glyphCount = glyphCache->getGlyphCount();
1926 newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001927
bungeman@google.comb29c8832011-10-10 13:19:10 +00001928 *typefaceUse = &newTypefaceUse;
1929 return S_OK;
1930}
1931
1932HRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
1933 IXpsOMObjectFactory* xpsFactory,
1934 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001935 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001936 LPCWSTR text,
1937 XPS_GLYPH_INDEX* xpsGlyphs,
1938 UINT32 xpsGlyphsLen,
1939 XPS_POINT *origin,
1940 FLOAT fontSize,
1941 XPS_STYLE_SIMULATION sims,
1942 const SkMatrix& transform,
1943 const SkPaint& paint) {
1944 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001945 HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
1946 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001947
bungeman@google.comb29c8832011-10-10 13:19:10 +00001948 //XPS uses affine transformations for everything...
1949 //...except positioning text.
1950 bool useCanvasForClip;
1951 if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
1952 origin->x += SkScalarToFLOAT(transform.getTranslateX());
1953 origin->y += SkScalarToFLOAT(transform.getTranslateY());
1954 useCanvasForClip = false;
1955 } else {
1956 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1957 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1958 if (xpsMatrixToUse.get()) {
1959 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1960 "Could not set transform matrix.");
1961 useCanvasForClip = true;
1962 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00001963 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001964 useCanvasForClip = false;
1965 }
1966 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001967
bungeman@google.comb29c8832011-10-10 13:19:10 +00001968 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1969 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001970
bsalomon49f085d2014-09-05 13:34:00 -07001971 if (text) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001972 HRM(glyphsEditor->SetUnicodeString(text),
1973 "Could not set unicode string.");
1974 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001975
bsalomon49f085d2014-09-05 13:34:00 -07001976 if (xpsGlyphs) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001977 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
1978 "Could not set glyphs.");
1979 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001980
bungeman@google.comb29c8832011-10-10 13:19:10 +00001981 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001982
bungeman@google.comb29c8832011-10-10 13:19:10 +00001983 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
1984 HR(this->createXpsBrush(
1985 paint,
1986 &xpsFillBrush,
halcanary96fcdcc2015-08-27 07:41:13 -07001987 useCanvasForClip ? nullptr : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001988
bungeman@google.comb29c8832011-10-10 13:19:10 +00001989 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
1990 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001991
bungeman@google.comb29c8832011-10-10 13:19:10 +00001992 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001993
bungeman@google.comb29c8832011-10-10 13:19:10 +00001994 HRM(glyphs->SetFontRenderingEmSize(fontSize),
1995 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001996
bungeman@google.comb29c8832011-10-10 13:19:10 +00001997 HRM(glyphs->SetStyleSimulations(sims),
1998 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001999
bungeman@google.comb29c8832011-10-10 13:19:10 +00002000 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2001 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002002
bungeman@google.comb29c8832011-10-10 13:19:10 +00002003 if (!useCanvasForClip) {
2004 HR(this->clip(glyphs.get(), d));
2005 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2006 } else {
2007 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2008 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2009 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002010
bungeman@google.comb29c8832011-10-10 13:19:10 +00002011 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2012 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2013 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002014
bungeman@google.comb29c8832011-10-10 13:19:10 +00002015 HRM(glyphCanvasVisuals->Append(glyphs.get()),
2016 "Could not add glyphs to page.");
2017 HR(this->clip(glyphCanvas.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002018
bungeman@google.comb29c8832011-10-10 13:19:10 +00002019 HRM(visuals->Append(glyphCanvas.get()),
2020 "Could not add glyph canvas to page.");
2021 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002022
bungeman@google.comb29c8832011-10-10 13:19:10 +00002023 return S_OK;
2024}
2025
herbbda26432015-11-24 08:37:01 -08002026static int num_glyph_guess(SkPaint::TextEncoding encoding, const void* text, size_t byteLength) {
2027 switch (encoding) {
2028 case SkPaint::kUTF8_TextEncoding:
2029 return SkUTF8_CountUnichars(static_cast<const char *>(text), byteLength);
2030 case SkPaint::kUTF16_TextEncoding:
2031 return SkUTF16_CountUnichars(static_cast<const uint16_t *>(text), SkToInt(byteLength));
2032 case SkPaint::kGlyphID_TextEncoding:
2033 return SkToInt(byteLength / 2);
2034 default:
djsollenf2b340f2016-01-29 08:51:04 -08002035 SK_ABORT("Invalid Text Encoding");
bungeman@google.comb29c8832011-10-10 13:19:10 +00002036 }
herbbda26432015-11-24 08:37:01 -08002037 return 0;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002038}
2039
2040static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2041 const SkPaint::Style style = paint.getStyle();
2042 return matrix.hasPerspective()
2043 || SkPaint::kStroke_Style == style
2044 || SkPaint::kStrokeAndFill_Style == style
2045 || paint.getMaskFilter()
2046 || paint.getRasterizer()
2047 ;
2048}
2049
herbbda26432015-11-24 08:37:01 -08002050typedef SkTDArray<XPS_GLYPH_INDEX> GlyphRun;
2051
2052class ProcessOneGlyph {
2053public:
2054 ProcessOneGlyph(FLOAT centemPerUnit, SkBitSet* glyphUse, GlyphRun* xpsGlyphs)
2055 : fCentemPerUnit(centemPerUnit)
2056 , fGlyphUse(glyphUse)
2057 , fXpsGlyphs(xpsGlyphs) { }
2058
2059 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint) {
2060 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
2061
2062 SkScalar x = position.fX;
2063 SkScalar y = position.fY;
2064
2065 XPS_GLYPH_INDEX* xpsGlyph = fXpsGlyphs->append();
2066 uint16_t glyphID = glyph.getGlyphID();
halcanarye2348cc2016-08-19 16:23:23 -07002067 fGlyphUse->set(glyphID);
herbbda26432015-11-24 08:37:01 -08002068 xpsGlyph->index = glyphID;
2069 if (1 == fXpsGlyphs->count()) {
2070 xpsGlyph->advanceWidth = 0.0f;
2071 xpsGlyph->horizontalOffset = SkScalarToFloat(x) * fCentemPerUnit;
2072 xpsGlyph->verticalOffset = SkScalarToFloat(y) * -fCentemPerUnit;
2073 }
2074 else {
2075 const XPS_GLYPH_INDEX& first = (*fXpsGlyphs)[0];
2076 xpsGlyph->advanceWidth = 0.0f;
2077 xpsGlyph->horizontalOffset = (SkScalarToFloat(x) * fCentemPerUnit)
2078 - first.horizontalOffset;
2079 xpsGlyph->verticalOffset = (SkScalarToFloat(y) * -fCentemPerUnit)
2080 - first.verticalOffset;
2081 }
2082 }
2083
2084private:
2085 /** [in] Advance width and offsets for glyphs measured in
2086 hundredths of the font em size (XPS Spec 5.1.3). */
2087 const FLOAT fCentemPerUnit;
2088 /** [in,out] The accumulated glyphs used in the current typeface. */
2089 SkBitSet* const fGlyphUse;
2090 /** [out] The glyphs to draw. */
2091 GlyphRun* const fXpsGlyphs;
2092};
2093
bungeman@google.comb29c8832011-10-10 13:19:10 +00002094void SkXPSDevice::drawText(const SkDraw& d,
2095 const void* text, size_t byteLen,
2096 SkScalar x, SkScalar y,
2097 const SkPaint& paint) {
2098 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002099
bungeman@google.comb29c8832011-10-10 13:19:10 +00002100 if (text_must_be_pathed(paint, *d.fMatrix)) {
2101 SkPath path;
2102 paint.getTextPath(text, byteLen, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002103 this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002104 //TODO: add automation "text"
2105 return;
2106 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002107
bungeman@google.comb29c8832011-10-10 13:19:10 +00002108 TypefaceUse* typeface;
2109 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002110
herbbda26432015-11-24 08:37:01 -08002111 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002112
herbbda26432015-11-24 08:37:01 -08002113 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2114 SkGlyphCache* cache = autoCache.getCache();
reed@google.com089130c2011-12-12 21:52:18 +00002115
herbbda26432015-11-24 08:37:01 -08002116 // Advance width and offsets for glyphs measured in hundredths of the font em size
2117 // (XPS Spec 5.1.3).
2118 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2119 GlyphRun xpsGlyphs;
2120 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2121 static_cast<const char*>(text), byteLen));
2122
2123 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2124
2125 SkFindAndPlaceGlyph::ProcessText(
2126 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2127 SkPoint{ x, y }, matrix, paint.getTextAlign(), cache, processOneGlyph);
2128
2129 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002130 return;
2131 }
2132
bungeman@google.comb29c8832011-10-10 13:19:10 +00002133 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002134 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2135 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002136 };
herbbda26432015-11-24 08:37:01 -08002137 xpsGlyphs[0].horizontalOffset = 0.0f;
2138 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002139
bungeman@google.comb29c8832011-10-10 13:19:10 +00002140 HRV(AddGlyphs(d,
2141 this->fXpsFactory.get(),
2142 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002143 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002144 nullptr,
herbbda26432015-11-24 08:37:01 -08002145 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002146 &origin,
2147 SkScalarToFLOAT(paint.getTextSize()),
2148 XPS_STYLE_SIMULATION_NONE,
2149 *d.fMatrix,
2150 paint));
2151}
2152
2153void SkXPSDevice::drawPosText(const SkDraw& d,
2154 const void* text, size_t byteLen,
fmalita05c4a432014-09-29 06:29:53 -07002155 const SkScalar pos[], int scalarsPerPos,
2156 const SkPoint& offset, const SkPaint& paint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002157 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002158
bungeman@google.comb29c8832011-10-10 13:19:10 +00002159 if (text_must_be_pathed(paint, *d.fMatrix)) {
2160 SkPath path;
2161 //TODO: make this work, Draw currently does not handle as well.
2162 //paint.getTextPath(text, byteLength, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002163 //this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002164 //TODO: add automation "text"
2165 return;
2166 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002167
bungeman@google.comb29c8832011-10-10 13:19:10 +00002168 TypefaceUse* typeface;
2169 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002170
herbbda26432015-11-24 08:37:01 -08002171 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002172
herbbda26432015-11-24 08:37:01 -08002173 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2174 SkGlyphCache* cache = autoCache.getCache();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002175
herbbda26432015-11-24 08:37:01 -08002176 // Advance width and offsets for glyphs measured in hundredths of the font em size
2177 // (XPS Spec 5.1.3).
2178 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2179 GlyphRun xpsGlyphs;
2180 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2181 static_cast<const char*>(text), byteLen));
2182
2183 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2184
2185 SkFindAndPlaceGlyph::ProcessPosText(
2186 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2187 offset, matrix, pos, scalarsPerPos, paint.getTextAlign(), cache, processOneGlyph);
2188
2189 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002190 return;
2191 }
2192
bungeman@google.comb29c8832011-10-10 13:19:10 +00002193 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002194 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2195 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002196 };
herbbda26432015-11-24 08:37:01 -08002197 xpsGlyphs[0].horizontalOffset = 0.0f;
2198 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002199
bungeman@google.comb29c8832011-10-10 13:19:10 +00002200 HRV(AddGlyphs(d,
2201 this->fXpsFactory.get(),
2202 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002203 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002204 nullptr,
herbbda26432015-11-24 08:37:01 -08002205 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002206 &origin,
2207 SkScalarToFLOAT(paint.getTextSize()),
2208 XPS_STYLE_SIMULATION_NONE,
2209 *d.fMatrix,
2210 paint));
2211}
2212
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002213void SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002214 int x, int y,
2215 const SkPaint&) {
2216 SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002217
bungeman@google.comb29c8832011-10-10 13:19:10 +00002218 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2219 XPS_MATRIX rawTransform = {
2220 1.0f,
2221 0.0f,
2222 0.0f,
2223 1.0f,
2224 static_cast<FLOAT>(x),
2225 static_cast<FLOAT>(y),
2226 };
2227 HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2228 "Could not create layer transform.");
2229 HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2230 "Could not set layer transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002231
bungeman@google.comb29c8832011-10-10 13:19:10 +00002232 //Get the current visual collection and add the layer to it.
2233 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2234 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2235 "Could not get current visuals for layer.");
2236 HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2237 "Could not add layer to current visuals.");
2238}
2239
reed76033be2015-03-14 10:54:31 -07002240SkBaseDevice* SkXPSDevice::onCreateDevice(const CreateInfo& info, const SkPaint*) {
bungeman@google.com635091f2013-10-01 15:03:18 +00002241//Conditional for bug compatibility with PDF device.
2242#if 0
fmalita6987dca2014-11-13 08:33:37 -08002243 if (SkBaseDevice::kGeneral_Usage == info.fUsage) {
halcanary96fcdcc2015-08-27 07:41:13 -07002244 return nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002245 //To what stream do we write?
2246 //SkXPSDevice* dev = new SkXPSDevice(this);
2247 //SkSize s = SkSize::Make(width, height);
2248 //dev->BeginCanvas(s, s, SkMatrix::I());
2249 //return dev;
2250 }
bungeman@google.com635091f2013-10-01 15:03:18 +00002251#endif
Hal Canaryabc88d22017-02-06 09:26:49 -05002252 SkXPSDevice* dev = new SkXPSDevice(info.fInfo.dimensions());
2253 // TODO(halcanary) implement copy constructor on SkTScopedCOmPtr
2254 dev->fXpsFactory.reset(SkRefComPtr(fXpsFactory.get()));
2255 SkAssertResult(dev->createCanvasForLayer());
2256 return dev;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002257}
2258
Hal Canaryb39b09e2017-02-01 17:04:44 -05002259void SkXPSDevice::drawOval(const SkDraw& d, const SkRect& o, const SkPaint& p) {
2260 SkPath path;
2261 path.addOval(o);
2262 this->drawPath(d, path, p, nullptr, true);
2263}
2264
2265void SkXPSDevice::drawBitmapRect(const SkDraw& draw,
2266 const SkBitmap& bitmap,
2267 const SkRect* src,
2268 const SkRect& dst,
2269 const SkPaint& paint,
2270 SkCanvas::SrcRectConstraint constraint) {
2271 SkRect srcBounds = src ? *src : SkRect::Make(bitmap.bounds());
2272 SkMatrix matrix = SkMatrix::MakeRectToRect(srcBounds, dst, SkMatrix::kFill_ScaleToFit);
2273
2274 auto bitmapShader = SkMakeBitmapShader(bitmap, SkShader::kClamp_TileMode,
2275 SkShader::kClamp_TileMode, &matrix,
Herb Derby07f665e2017-02-13 20:04:00 +00002276 kNever_SkCopyPixelsMode, nullptr);
Hal Canaryb39b09e2017-02-01 17:04:44 -05002277 SkASSERT(bitmapShader);
2278 if (!bitmapShader) { return; }
2279 SkPaint paintWithShader(paint);
2280 paintWithShader.setStyle(SkPaint::kFill_Style);
2281 paintWithShader.setShader(std::move(bitmapShader));
2282 this->drawRect(draw, dst, paintWithShader);
2283}
mtklein1ee76512015-11-02 10:20:27 -08002284#endif//defined(SK_BUILD_FOR_WIN32)