blob: cebf60d5647773f163ce5552858a396d05b32f02 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Mike Klein8f11d4d2018-01-24 12:42:55 -05009#if defined(SK_BUILD_FOR_WIN)
halcanary47ef4d52015-03-03 09:13:09 -080010
Ben Wagnerab6eefe2019-05-20 11:02:49 -040011#include "src/core/SkLeanWindows.h"
halcanary4dbbd042016-06-07 17:21:10 -070012
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/core/SkColor.h"
26#include "include/core/SkData.h"
27#include "include/core/SkImage.h"
28#include "include/core/SkImageEncoder.h"
29#include "include/core/SkPaint.h"
30#include "include/core/SkPathEffect.h"
31#include "include/core/SkPoint.h"
32#include "include/core/SkShader.h"
33#include "include/core/SkSize.h"
34#include "include/core/SkStream.h"
35#include "include/core/SkVertices.h"
36#include "include/pathops/SkPathOps.h"
37#include "include/private/SkTDArray.h"
38#include "include/private/SkTo.h"
39#include "src/core/SkDraw.h"
40#include "src/core/SkEndian.h"
41#include "src/core/SkGeometry.h"
42#include "src/core/SkImagePriv.h"
Ben Wagner11eae3d2019-08-22 17:40:34 -040043#include "src/core/SkMakeUnique.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/core/SkMaskFilterBase.h"
45#include "src/core/SkRasterClip.h"
46#include "src/core/SkStrikeCache.h"
47#include "src/core/SkTLazy.h"
48#include "src/core/SkTypefacePriv.h"
49#include "src/core/SkUtils.h"
50#include "src/sfnt/SkSFNTHeader.h"
51#include "src/sfnt/SkTTCFHeader.h"
52#include "src/shaders/SkShaderBase.h"
53#include "src/utils/win/SkHRESULT.h"
54#include "src/utils/win/SkIStream.h"
55#include "src/utils/win/SkTScopedComPtr.h"
56#include "src/xps/SkXPSDevice.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000057
58//Windows defines a FLOAT type,
59//make it clear when converting a scalar that this is what is wanted.
60#define SkScalarToFLOAT(n) SkScalarToFloat(n)
61
Ben Wagnerda5a1b82014-08-22 15:07:06 -040062//Dummy representation of a GUID from createId.
bungeman@google.comb29c8832011-10-10 13:19:10 +000063#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
halcanary96fcdcc2015-08-27 07:41:13 -070064//Length of GUID representation from createId, including nullptr terminator.
bungeman@google.comb29c8832011-10-10 13:19:10 +000065#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
66
67/**
68 Formats a GUID and places it into buffer.
69 buffer should have space for at least GUID_ID_LEN wide characters.
70 The string will always be wchar null terminated.
71 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
72 @return -1 if there was an error, > 0 if success.
73 */
74static int format_guid(const GUID& guid,
75 wchar_t* buffer, size_t bufferSize,
76 wchar_t sep = '-') {
77 SkASSERT(bufferSize >= GUID_ID_LEN);
78 return swprintf_s(buffer,
79 bufferSize,
80 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
81 guid.Data1,
82 sep,
83 guid.Data2,
84 sep,
85 guid.Data3,
86 sep,
87 guid.Data4[0],
88 guid.Data4[1],
89 sep,
90 guid.Data4[2],
91 guid.Data4[3],
92 guid.Data4[4],
93 guid.Data4[5],
94 guid.Data4[6],
95 guid.Data4[7]);
96}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000097
Ben Wagnerda5a1b82014-08-22 15:07:06 -040098HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000099 GUID guid = {};
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400100#ifdef SK_XPS_USE_DETERMINISTIC_IDS
101 guid.Data1 = fNextId++;
102 // The following make this a valid Type4 UUID.
103 guid.Data3 = 0x4000;
104 guid.Data4[0] = 0x80;
105#else
bungeman@google.comb29c8832011-10-10 13:19:10 +0000106 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400107#endif
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000108
bungeman@google.comb29c8832011-10-10 13:19:10 +0000109 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
110 HRM(E_UNEXPECTED, "Could not format GUID into id.");
111 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000112
bungeman@google.comb29c8832011-10-10 13:19:10 +0000113 return S_OK;
114}
115
Hal Canaryb39b09e2017-02-01 17:04:44 -0500116SkXPSDevice::SkXPSDevice(SkISize s)
117 : INHERITED(SkImageInfo::MakeUnknown(s.width(), s.height()),
118 SkSurfaceProps(0, kUnknown_SkPixelGeometry))
Hal Canaryabc88d22017-02-06 09:26:49 -0500119 , fCurrentPage(0) {}
bungeman@google.comb29c8832011-10-10 13:19:10 +0000120
Hal Canaryabc88d22017-02-06 09:26:49 -0500121SkXPSDevice::~SkXPSDevice() {}
bungeman@google.comb29c8832011-10-10 13:19:10 +0000122
Hal Canaryabc88d22017-02-06 09:26:49 -0500123bool SkXPSDevice::beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory* factory) {
124 SkASSERT(factory);
125 fXpsFactory.reset(SkRefComPtr(factory));
126 HRB(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000127 return true;
128}
129
130bool SkXPSDevice::beginSheet(
131 const SkVector& unitsPerMeter,
132 const SkVector& pixelsPerMeter,
133 const SkSize& trimSize,
134 const SkRect* mediaBox,
135 const SkRect* bleedBox,
136 const SkRect* artBox,
137 const SkRect* cropBox) {
138 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000139
bungeman@google.comb29c8832011-10-10 13:19:10 +0000140 //For simplicity, just write everything out in geometry units,
141 //then have a base canvas do the scale to physical units.
142 this->fCurrentCanvasSize = trimSize;
143 this->fCurrentUnitsPerMeter = unitsPerMeter;
144 this->fCurrentPixelsPerMeter = pixelsPerMeter;
Hal Canaryabc88d22017-02-06 09:26:49 -0500145 return this->createCanvasForLayer();
146}
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000147
Hal Canaryabc88d22017-02-06 09:26:49 -0500148bool SkXPSDevice::createCanvasForLayer() {
149 SkASSERT(fXpsFactory);
150 fCurrentXpsCanvas.reset();
151 HRB(fXpsFactory->CreateCanvas(&fCurrentXpsCanvas));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000152 return true;
153}
154
halcanarybfa92752016-05-31 11:23:42 -0700155template <typename T> static constexpr size_t sk_digits_in() {
156 return static_cast<size_t>(std::numeric_limits<T>::digits10 + 1);
157}
158
bungeman@google.comb29c8832011-10-10 13:19:10 +0000159HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
160 const unsigned int pageNum,
161 IXpsOMImageResource** image) {
162 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
163 HRM(CoCreateInstance(
164 CLSID_XpsOMThumbnailGenerator,
halcanary96fcdcc2015-08-27 07:41:13 -0700165 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000166 CLSCTX_INPROC_SERVER,
167 IID_PPV_ARGS(&thumbnailGenerator)),
168 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000169
bungeman@google.comb29c8832011-10-10 13:19:10 +0000170 SkTScopedComPtr<IOpcPartUri> partUri;
halcanarybfa92752016-05-31 11:23:42 -0700171 constexpr size_t size = SkTMax(
172 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + sk_digits_in<decltype(pageNum)>(),
173 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png"));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000174 wchar_t buffer[size];
175 if (pageNum > 0) {
176 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
177 } else {
178 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400179 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000180 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
181 }
182 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
183 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
bungeman@google.comb29c8832011-10-10 13:19:10 +0000185 HRM(thumbnailGenerator->GenerateThumbnail(page,
186 XPS_IMAGE_TYPE_PNG,
187 XPS_THUMBNAIL_SIZE_LARGE,
188 partUri.get(),
189 image),
190 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000191
bungeman@google.comb29c8832011-10-10 13:19:10 +0000192 return S_OK;
193}
194
195HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
196 IXpsOMPage** page) {
halcanarybfa92752016-05-31 11:23:42 -0700197 constexpr size_t size =
198 SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
199 + sk_digits_in<decltype(fCurrentPage)>();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000200 wchar_t buffer[size];
201 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
202 this->fCurrentPage);
203 SkTScopedComPtr<IOpcPartUri> partUri;
204 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
205 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000206
bungeman@google.comb29c8832011-10-10 13:19:10 +0000207 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
208 HRM(this->fXpsFactory->CreatePage(&pageSize,
209 L"und",
210 partUri.get(),
211 page),
212 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000213
bungeman@google.comb29c8832011-10-10 13:19:10 +0000214 return S_OK;
215}
216
217HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
218 //Create package writer.
219 {
220 SkTScopedComPtr<IOpcPartUri> partUri;
221 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
222 &partUri),
223 "Could not create document sequence part uri.");
224 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
225 this->fOutputStream.get(),
226 TRUE,
227 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
228 partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700229 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000230 image,
halcanary96fcdcc2015-08-27 07:41:13 -0700231 nullptr,
232 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000233 &this->fPackageWriter),
234 "Could not create package writer.");
235 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000236
bungeman@google.comb29c8832011-10-10 13:19:10 +0000237 //Begin the lone document.
238 {
239 SkTScopedComPtr<IOpcPartUri> partUri;
240 HRM(this->fXpsFactory->CreatePartUri(
241 L"/Documents/1/FixedDocument.fdoc",
242 &partUri),
243 "Could not create fixed document part uri.");
244 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700245 nullptr,
246 nullptr,
247 nullptr,
248 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000249 "Could not start document.");
250 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000251
bungeman@google.comb29c8832011-10-10 13:19:10 +0000252 return S_OK;
253}
254
255bool SkXPSDevice::endSheet() {
256 //XPS is fixed at 96dpi (XPS Spec 11.1).
257 static const float xpsDPI = 96.0f;
258 static const float inchesPerMeter = 10000.0f / 254.0f;
259 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
260 const float scaleX = targetUnitsPerMeter
261 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
262 const float scaleY = targetUnitsPerMeter
263 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000264
bungeman@google.comb29c8832011-10-10 13:19:10 +0000265 //Create the scale canvas.
266 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
267 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
268 "Could not create scale canvas.");
269 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
270 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
271 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000272
bungeman@google.comb29c8832011-10-10 13:19:10 +0000273 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
274 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
275 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
276 "Could not create geometry to physical transform.");
277 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
278 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000279
bungeman@google.comb29c8832011-10-10 13:19:10 +0000280 //Add the content canvas to the scale canvas.
281 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
282 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000283
bungeman@google.comb29c8832011-10-10 13:19:10 +0000284 //Create the page.
285 XPS_SIZE pageSize = {
286 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
287 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
288 };
289 SkTScopedComPtr<IXpsOMPage> page;
290 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000291
bungeman@google.comb29c8832011-10-10 13:19:10 +0000292 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
293 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000294
bungeman@google.comb29c8832011-10-10 13:19:10 +0000295 //Add the scale canvas to the page.
296 HRBM(pageVisuals->Append(scaleCanvas.get()),
297 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000298
bungeman@google.comb29c8832011-10-10 13:19:10 +0000299 //Create the package writer if it hasn't been created yet.
halcanary96fcdcc2015-08-27 07:41:13 -0700300 if (nullptr == this->fPackageWriter.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000301 SkTScopedComPtr<IXpsOMImageResource> image;
302 //Ignore return, thumbnail is completely optional.
303 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000304
bungeman@google.comb29c8832011-10-10 13:19:10 +0000305 HRB(this->initXpsDocumentWriter(image.get()));
306 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000307
bungeman@google.comb29c8832011-10-10 13:19:10 +0000308 HRBM(this->fPackageWriter->AddPage(page.get(),
309 &pageSize,
halcanary96fcdcc2015-08-27 07:41:13 -0700310 nullptr,
311 nullptr,
312 nullptr,
313 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000314 "Could not write the page.");
315 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000316
bungeman@google.comb29c8832011-10-10 13:19:10 +0000317 return true;
318}
319
Ben Wagner11eae3d2019-08-22 17:40:34 -0400320static HRESULT subset_typeface(const SkXPSDevice::TypefaceUse& current) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000321 //CreateFontPackage wants unsigned short.
322 //Microsoft, Y U NO stdint.h?
Hal Canary9e41c212018-09-03 12:00:23 -0400323 std::vector<unsigned short> keepList;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400324 current.glyphsUsed.getSetValues([&keepList](unsigned v) {
Hal Canary52514d52018-10-19 10:08:42 -0400325 keepList.push_back((unsigned short)v);
326 });
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000327
Ben Wagner11eae3d2019-08-22 17:40:34 -0400328 int ttcCount = (current.ttcIndex + 1);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000329
bungeman@google.comb29c8832011-10-10 13:19:10 +0000330 //The following are declared with the types required by CreateFontPackage.
halcanary96fcdcc2015-08-27 07:41:13 -0700331 unsigned char *fontPackageBufferRaw = nullptr;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000332 unsigned long fontPackageBufferSize;
333 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000334 unsigned long result = CreateFontPackage(
Ben Wagner11eae3d2019-08-22 17:40:34 -0400335 (unsigned char *) current.fontData->getMemoryBase(),
336 (unsigned long) current.fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000337 &fontPackageBufferRaw,
338 &fontPackageBufferSize,
339 &bytesWritten,
340 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
Ben Wagner11eae3d2019-08-22 17:40:34 -0400341 current.ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000342 TTFCFP_SUBSET,
343 0,
344 0,
345 0,
Hal Canary9e41c212018-09-03 12:00:23 -0400346 keepList.data(),
347 SkTo<unsigned short>(keepList.size()),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000348 sk_malloc_throw,
349 sk_realloc_throw,
350 sk_free,
halcanary96fcdcc2015-08-27 07:41:13 -0700351 nullptr);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000352 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000353 if (result != NO_ERROR) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400354 SkDEBUGF("CreateFontPackage Error %lu", result);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000355 return E_UNEXPECTED;
356 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000357
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000358 // If it was originally a ttc, keep it a ttc.
359 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
360 size_t extra;
361 if (ttcCount > 0) {
362 // Create space for a ttc header.
363 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
364 fontPackageBuffer.realloc(bytesWritten + extra);
365 //overlap is certain, use memmove
366 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
367
368 // Write the ttc header.
369 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
370 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
371 ttcfHeader->version = SkTTCFHeader::version_1;
372 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
373 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
374 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
bsalomon0aa5cea2014-12-15 09:13:35 -0800375 *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000376 }
377
378 // Fix up offsets in sfnt table entries.
379 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
380 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
381 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
382 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
383 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
384 tableDirectory->offset = SkEndian_SwapBE32(
bsalomon0aa5cea2014-12-15 09:13:35 -0800385 SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000386 }
387 } else {
388 extra = 0;
389 fontPackageBuffer.realloc(bytesWritten);
390 }
391
Ben Wagner145dbcd2016-11-03 14:40:50 -0400392 std::unique_ptr<SkMemoryStream> newStream(new SkMemoryStream());
mtklein18300a32016-03-16 13:53:35 -0700393 newStream->setMemoryOwned(fontPackageBuffer.release(), bytesWritten + extra);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000394
bungeman@google.comb29c8832011-10-10 13:19:10 +0000395 SkTScopedComPtr<IStream> newIStream;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400396 SkIStream::CreateFromSkStream(std::move(newStream), &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000397
bungeman@google.comb29c8832011-10-10 13:19:10 +0000398 XPS_FONT_EMBEDDING embedding;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400399 HRM(current.xpsFont->GetEmbeddingOption(&embedding),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000400 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000401
bungeman@google.comb29c8832011-10-10 13:19:10 +0000402 SkTScopedComPtr<IOpcPartUri> partUri;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400403 HRM(current.xpsFont->GetPartName(&partUri),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000404 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000405
Ben Wagner11eae3d2019-08-22 17:40:34 -0400406 HRM(current.xpsFont->SetContent(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000407 newIStream.get(),
408 embedding,
409 partUri.get()),
410 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000411
bungeman@google.comb29c8832011-10-10 13:19:10 +0000412 return S_OK;
413}
414
415bool SkXPSDevice::endPortfolio() {
416 //Subset fonts
Ben Wagner11eae3d2019-08-22 17:40:34 -0400417 for (const TypefaceUse& current : this->fTypefaces) {
418 //Ignore return for now, if it didn't subset, let it be.
419 subset_typeface(current);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000420 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000421
Ben Wagner11eae3d2019-08-22 17:40:34 -0400422 if (this->fPackageWriter) {
423 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
424 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000425
bungeman@google.comb29c8832011-10-10 13:19:10 +0000426 return true;
427}
428
429static XPS_COLOR xps_color(const SkColor skColor) {
430 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
431 XPS_COLOR xpsColor;
432 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
433 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
434 xpsColor.value.sRGB.red = SkColorGetR(skColor);
435 xpsColor.value.sRGB.green = SkColorGetG(skColor);
436 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000437
bungeman@google.comb29c8832011-10-10 13:19:10 +0000438 return xpsColor;
439}
440
441static XPS_POINT xps_point(const SkPoint& point) {
442 XPS_POINT xpsPoint = {
443 SkScalarToFLOAT(point.fX),
444 SkScalarToFLOAT(point.fY),
445 };
446 return xpsPoint;
447}
448
449static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
450 SkPoint skTransformedPoint;
451 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
452 return xps_point(skTransformedPoint);
453}
454
Mike Reed5c5de212019-04-03 16:51:47 -0400455static XPS_SPREAD_METHOD xps_spread_method(SkTileMode tileMode) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000456 switch (tileMode) {
Mike Reed5c5de212019-04-03 16:51:47 -0400457 case SkTileMode::kClamp:
bungeman@google.comb29c8832011-10-10 13:19:10 +0000458 return XPS_SPREAD_METHOD_PAD;
Mike Reed5c5de212019-04-03 16:51:47 -0400459 case SkTileMode::kRepeat:
bungeman@google.comb29c8832011-10-10 13:19:10 +0000460 return XPS_SPREAD_METHOD_REPEAT;
Mike Reed5c5de212019-04-03 16:51:47 -0400461 case SkTileMode::kMirror:
bungeman@google.comb29c8832011-10-10 13:19:10 +0000462 return XPS_SPREAD_METHOD_REFLECT;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400463 case SkTileMode::kDecal:
464 // TODO: fake
465 return XPS_SPREAD_METHOD_PAD;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000466 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000467 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000468 }
469 return XPS_SPREAD_METHOD_PAD;
470}
471
472static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
473 const SkPoint& start, const SkPoint& end,
474 const SkMatrix& transform) {
475 SkPoint startTransformed;
476 transform.mapXY(start.fX, start.fY, &startTransformed);
477 SkPoint endTransformed;
478 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000479
bungeman@google.comb29c8832011-10-10 13:19:10 +0000480 //Manhattan distance between transformed start and end.
481 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
482 + (endTransformed.fY - startTransformed.fY);
483 if (SkScalarNearlyZero(startToEnd)) {
484 for (int i = 0; i < numOffsets; ++i) {
485 stopOffsets[i] = 0;
486 }
487 return;
488 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000489
bungeman@google.comb29c8832011-10-10 13:19:10 +0000490 for (int i = 0; i < numOffsets; ++i) {
491 SkPoint stop;
Mike Reed8be952a2017-02-13 20:44:33 -0500492 stop.fX = (end.fX - start.fX) * stopOffsets[i];
493 stop.fY = (end.fY - start.fY) * stopOffsets[i];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000494
bungeman@google.comb29c8832011-10-10 13:19:10 +0000495 SkPoint stopTransformed;
496 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000497
bungeman@google.comb29c8832011-10-10 13:19:10 +0000498 //Manhattan distance between transformed start and stop.
499 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
500 + (stopTransformed.fY - startTransformed.fY);
501 //Percentage along transformed line.
reed80ea19c2015-05-12 10:37:34 -0700502 stopOffsets[i] = startToStop / startToEnd;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000503 }
504}
505
506HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
507 IXpsOMMatrixTransform** xpsTransform) {
508 SkScalar affine[6];
509 if (!matrix.asAffine(affine)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700510 *xpsTransform = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000511 return S_FALSE;
512 }
513 XPS_MATRIX rawXpsMatrix = {
Mike Reedf0cb7a02017-10-13 13:26:00 +0000514 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
515 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
516 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
517 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
518 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
519 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000520 };
521 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
522 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000523
bungeman@google.comb29c8832011-10-10 13:19:10 +0000524 return S_OK;
525}
526
527HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
528 IXpsOMVisualCollection* visuals,
529 IXpsOMPath** path) {
530 SkTScopedComPtr<IXpsOMGeometry> geometry;
531 HRM(this->fXpsFactory->CreateGeometry(&geometry),
532 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000533
bungeman@google.comb29c8832011-10-10 13:19:10 +0000534 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
535 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
536 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000537
bungeman@google.comb29c8832011-10-10 13:19:10 +0000538 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
539 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000540
bungeman@google.comb29c8832011-10-10 13:19:10 +0000541 HRM(visuals->Append(*path), "Could not add path to visuals.");
542 return S_OK;
543}
544
545HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
546 const SkAlpha alpha,
547 IXpsOMBrush** xpsBrush) {
548 XPS_COLOR xpsColor = xps_color(skColor);
549 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
halcanary96fcdcc2015-08-27 07:41:13 -0700550 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000551 "Could not create solid color brush.");
552 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
553 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
554 return S_OK;
555}
556
557HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
558 const XPS_RECT& imageViewBox,
559 IXpsOMImageResource* image,
560 IXpsOMVisualCollection* visuals) {
561 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
562 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000563
bungeman@google.comb29c8832011-10-10 13:19:10 +0000564 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
565 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000566
bungeman@google.comb29c8832011-10-10 13:19:10 +0000567 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
568 HRM(this->fXpsFactory->CreateImageBrush(image,
569 &imageViewBox,
570 &imageViewBox,
571 &areaToFillBrush),
572 "Could not create brush for side of clamp.");
573 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
574 "Could not set tile mode for side of clamp.");
575 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
576 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000577
bungeman@google.comb29c8832011-10-10 13:19:10 +0000578 return S_OK;
579}
580
581HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
582 const SkColor color,
583 IXpsOMVisualCollection* visuals) {
584 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
585 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000586
bungeman@google.comb29c8832011-10-10 13:19:10 +0000587 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
588 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000589
bungeman@google.comb29c8832011-10-10 13:19:10 +0000590 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
591 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
592 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
593 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000594
bungeman@google.comb29c8832011-10-10 13:19:10 +0000595 return S_OK;
596}
597
598static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
599static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
600static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
601static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
602static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
603
604//TODO(bungeman): In the future, should skia add None,
605//handle None+Mirror and None+Repeat correctly.
606//None is currently an internal hack so masks don't repeat (None+None only).
Mike Reed5c5de212019-04-03 16:51:47 -0400607static XPS_TILE_MODE gSkToXpsTileMode[kSkTileModeCount+1]
608 [kSkTileModeCount+1] = {
halcanarya634b742016-10-13 08:44:11 -0700609 //Clamp //Repeat //Mirror //None
610 /*Clamp */ {XTM_N, XTM_T, XTM_Y, XTM_N},
611 /*Repeat*/ {XTM_T, XTM_T, XTM_Y, XTM_N},
612 /*Mirror*/ {XTM_X, XTM_X, XTM_XY, XTM_X},
613 /*None */ {XTM_N, XTM_N, XTM_Y, XTM_N},
bungeman@google.comb29c8832011-10-10 13:19:10 +0000614};
615
Mike Reed5c5de212019-04-03 16:51:47 -0400616static XPS_TILE_MODE SkToXpsTileMode(SkTileMode tmx, SkTileMode tmy) {
617 return gSkToXpsTileMode[(unsigned)tmx][(unsigned)tmy];
618}
619
bungeman@google.comb29c8832011-10-10 13:19:10 +0000620HRESULT SkXPSDevice::createXpsImageBrush(
621 const SkBitmap& bitmap,
622 const SkMatrix& localMatrix,
Mike Reed5c5de212019-04-03 16:51:47 -0400623 const SkTileMode (&xy)[2],
bungeman@google.comb29c8832011-10-10 13:19:10 +0000624 const SkAlpha alpha,
625 IXpsOMTileBrush** xpsBrush) {
626 SkDynamicMemoryWStream write;
Hal Canarydb683012016-11-23 08:55:18 -0700627 if (!SkEncodeImage(&write, bitmap, SkEncodedImageFormat::kPNG, 100)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000628 HRM(E_FAIL, "Unable to encode bitmap as png.");
629 }
Ben Wagner11eae3d2019-08-22 17:40:34 -0400630 SkTScopedComPtr<IStream> read;
631 HRM(SkIStream::CreateFromSkStream(write.detachAsStream(), &read),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000632 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000633
bungeman@google.comb29c8832011-10-10 13:19:10 +0000634 const size_t size =
635 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
636 wchar_t buffer[size];
637 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400638 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000639 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000640
bungeman@google.comb29c8832011-10-10 13:19:10 +0000641 SkTScopedComPtr<IOpcPartUri> imagePartUri;
642 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
643 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000644
bungeman@google.comb29c8832011-10-10 13:19:10 +0000645 SkTScopedComPtr<IXpsOMImageResource> imageResource;
646 HRM(this->fXpsFactory->CreateImageResource(
Ben Wagner11eae3d2019-08-22 17:40:34 -0400647 read.get(),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000648 XPS_IMAGE_TYPE_PNG,
649 imagePartUri.get(),
650 &imageResource),
651 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000652
bungeman@google.comb29c8832011-10-10 13:19:10 +0000653 XPS_RECT bitmapRect = {
654 0.0, 0.0,
655 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
656 };
657 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
658 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
659 &bitmapRect, &bitmapRect,
660 &xpsImageBrush),
661 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000662
Mike Reed5c5de212019-04-03 16:51:47 -0400663 if (SkTileMode::kClamp != xy[0] &&
664 SkTileMode::kClamp != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000665
Mike Reed5c5de212019-04-03 16:51:47 -0400666 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode(xy[0], xy[1])),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000667 "Could not set image tile mode");
668 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
669 "Could not set image opacity.");
670 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
671 } else {
672 //TODO(bungeman): compute how big this really needs to be.
673 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
674 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
675 const SkScalar bWidth = SkIntToScalar(bitmap.width());
676 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000677
bungeman@google.comb29c8832011-10-10 13:19:10 +0000678 //create brush canvas
679 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
680 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
681 "Could not create image brush canvas.");
682 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
683 HRM(brushCanvas->GetVisuals(&brushVisuals),
684 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000685
bungeman@google.comb29c8832011-10-10 13:19:10 +0000686 //create central figure
687 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
688 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
689 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000690
bungeman@google.comb29c8832011-10-10 13:19:10 +0000691 SkTScopedComPtr<IXpsOMPath> centralPath;
692 HR(this->createPath(centralFigure.get(),
693 brushVisuals.get(),
694 &centralPath));
695 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
696 "Could not set tile mode for image brush central path.");
697 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
698 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000699
bungeman@google.comb29c8832011-10-10 13:19:10 +0000700 //add left/right
Mike Reed5c5de212019-04-03 16:51:47 -0400701 if (SkTileMode::kClamp == xy[0]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000702 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
703 XPS_RECT leftImageViewBox = {
704 0.0, 0.0,
705 1.0, static_cast<FLOAT>(bitmap.height()),
706 };
707 HR(this->sideOfClamp(leftArea, leftImageViewBox,
708 imageResource.get(),
709 brushVisuals.get()));
710
711 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
712 XPS_RECT rightImageViewBox = {
713 bitmap.width() - 1.0f, 0.0f,
714 1.0f, static_cast<FLOAT>(bitmap.height()),
715 };
716 HR(this->sideOfClamp(rightArea, rightImageViewBox,
717 imageResource.get(),
718 brushVisuals.get()));
719 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000720
bungeman@google.comb29c8832011-10-10 13:19:10 +0000721 //add top/bottom
Mike Reed5c5de212019-04-03 16:51:47 -0400722 if (SkTileMode::kClamp == xy[1]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000723 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
724 XPS_RECT topImageViewBox = {
725 0.0, 0.0,
726 static_cast<FLOAT>(bitmap.width()), 1.0,
727 };
728 HR(this->sideOfClamp(topArea, topImageViewBox,
729 imageResource.get(),
730 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000731
bungeman@google.comb29c8832011-10-10 13:19:10 +0000732 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
733 XPS_RECT bottomImageViewBox = {
734 0.0f, bitmap.height() - 1.0f,
735 static_cast<FLOAT>(bitmap.width()), 1.0f,
736 };
737 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
738 imageResource.get(),
739 brushVisuals.get()));
740 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000741
bungeman@google.comb29c8832011-10-10 13:19:10 +0000742 //add tl, tr, bl, br
Mike Reed5c5de212019-04-03 16:51:47 -0400743 if (SkTileMode::kClamp == xy[0] &&
744 SkTileMode::kClamp == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000745
bungeman@google.comb29c8832011-10-10 13:19:10 +0000746 const SkColor tlColor = bitmap.getColor(0,0);
747 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
748 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000749
bungeman@google.comb29c8832011-10-10 13:19:10 +0000750 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
751 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
752 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000753
bungeman@google.comb29c8832011-10-10 13:19:10 +0000754 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
755 bitmap.height()-1);
756 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
757 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000758
bungeman@google.comb29c8832011-10-10 13:19:10 +0000759 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
760 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
761 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
762 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000763
bungeman@google.comb29c8832011-10-10 13:19:10 +0000764 //create visual brush from canvas
765 XPS_RECT bound = {};
Mike Reed5c5de212019-04-03 16:51:47 -0400766 if (SkTileMode::kClamp == xy[0] &&
767 SkTileMode::kClamp == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000768
bungeman@google.comb29c8832011-10-10 13:19:10 +0000769 bound.x = BIG_F / -2;
770 bound.y = BIG_F / -2;
771 bound.width = BIG_F;
772 bound.height = BIG_F;
Mike Reed5c5de212019-04-03 16:51:47 -0400773 } else if (SkTileMode::kClamp == xy[0]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000774 bound.x = BIG_F / -2;
775 bound.y = 0.0f;
776 bound.width = BIG_F;
777 bound.height = static_cast<FLOAT>(bitmap.height());
Mike Reed5c5de212019-04-03 16:51:47 -0400778 } else if (SkTileMode::kClamp == xy[1]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000779 bound.x = 0;
780 bound.y = BIG_F / -2;
781 bound.width = static_cast<FLOAT>(bitmap.width());
782 bound.height = BIG_F;
783 }
784 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
785 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
786 "Could not create visual brush for image brush.");
787 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
788 "Could not set canvas on visual brush for image brush.");
Mike Reed5c5de212019-04-03 16:51:47 -0400789 HRM(clampBrush->SetTileMode(SkToXpsTileMode(xy[0], xy[1])),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000790 "Could not set tile mode on visual brush for image brush.");
791 HRM(clampBrush->SetOpacity(alpha / 255.0f),
792 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000793
bungeman@google.comb29c8832011-10-10 13:19:10 +0000794 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
795 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000796
bungeman@google.comb29c8832011-10-10 13:19:10 +0000797 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
798 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
bsalomon49f085d2014-09-05 13:34:00 -0700799 if (xpsMatrixToUse.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000800 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
801 "Could not set transform for image brush.");
802 } else {
803 //TODO(bungeman): perspective bitmaps in general.
804 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000805
bungeman@google.comb29c8832011-10-10 13:19:10 +0000806 return S_OK;
807}
808
809HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
810 const SkScalar offset,
811 IXpsOMGradientStop** xpsGradStop) {
812 XPS_COLOR gradStopXpsColor = xps_color(skColor);
813 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
halcanary96fcdcc2015-08-27 07:41:13 -0700814 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000815 SkScalarToFLOAT(offset),
816 xpsGradStop),
817 "Could not create gradient stop.");
818 return S_OK;
819}
820
821HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
822 const SkAlpha alpha,
823 const SkMatrix& localMatrix,
824 IXpsOMMatrixTransform* xpsMatrix,
825 IXpsOMBrush** xpsBrush) {
826 XPS_POINT startPoint;
827 XPS_POINT endPoint;
bsalomon49f085d2014-09-05 13:34:00 -0700828 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000829 startPoint = xps_point(info.fPoint[0]);
830 endPoint = xps_point(info.fPoint[1]);
831 } else {
832 transform_offsets(info.fColorOffsets, info.fColorCount,
833 info.fPoint[0], info.fPoint[1],
834 localMatrix);
835 startPoint = xps_point(info.fPoint[0], localMatrix);
836 endPoint = xps_point(info.fPoint[1], localMatrix);
837 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000838
bungeman@google.comb29c8832011-10-10 13:19:10 +0000839 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
840 HR(createXpsGradientStop(info.fColors[0],
841 info.fColorOffsets[0],
842 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000843
bungeman@google.comb29c8832011-10-10 13:19:10 +0000844 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
845 HR(createXpsGradientStop(info.fColors[1],
846 info.fColorOffsets[1],
847 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000848
bungeman@google.comb29c8832011-10-10 13:19:10 +0000849 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
850 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
851 gradStop1.get(),
852 &startPoint,
853 &endPoint,
854 &gradientBrush),
855 "Could not create linear gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700856 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000857 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
858 "Could not set transform on linear gradient brush.");
859 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000860
bungeman@google.comb29c8832011-10-10 13:19:10 +0000861 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
862 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
863 "Could not get linear gradient stop collection.");
864 for (int i = 2; i < info.fColorCount; ++i) {
865 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
866 HR(createXpsGradientStop(info.fColors[i],
867 info.fColorOffsets[i],
868 &gradStop));
869 HRM(gradStopCollection->Append(gradStop.get()),
870 "Could not add linear gradient stop.");
871 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000872
Mike Reed5c5de212019-04-03 16:51:47 -0400873 HRM(gradientBrush->SetSpreadMethod(xps_spread_method((SkTileMode)info.fTileMode)),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000874 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000875
bungeman@google.comb29c8832011-10-10 13:19:10 +0000876 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
877 "Could not set opacity of linear gradient brush.");
878 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000879
bungeman@google.comb29c8832011-10-10 13:19:10 +0000880 return S_OK;
881}
882
883HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
884 const SkAlpha alpha,
885 const SkMatrix& localMatrix,
886 IXpsOMMatrixTransform* xpsMatrix,
887 IXpsOMBrush** xpsBrush) {
888 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
889 HR(createXpsGradientStop(info.fColors[0],
890 info.fColorOffsets[0],
891 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000892
bungeman@google.comb29c8832011-10-10 13:19:10 +0000893 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
894 HR(createXpsGradientStop(info.fColors[1],
895 info.fColorOffsets[1],
896 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000897
bungeman@google.comb29c8832011-10-10 13:19:10 +0000898 //TODO: figure out how to fake better if not affine
899 XPS_POINT centerPoint;
900 XPS_POINT gradientOrigin;
901 XPS_SIZE radiiSizes;
bsalomon49f085d2014-09-05 13:34:00 -0700902 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000903 centerPoint = xps_point(info.fPoint[0]);
904 gradientOrigin = xps_point(info.fPoint[0]);
905 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
906 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
907 } else {
908 centerPoint = xps_point(info.fPoint[0], localMatrix);
909 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000910
bungeman@google.comb29c8832011-10-10 13:19:10 +0000911 SkScalar radius = info.fRadius[0];
912 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000913
bungeman@google.comb29c8832011-10-10 13:19:10 +0000914 vec[0].set(radius, 0);
915 vec[1].set(0, radius);
916 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000917
bungeman@google.comb29c8832011-10-10 13:19:10 +0000918 SkScalar d0 = vec[0].length();
919 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000920
bungeman@google.comb29c8832011-10-10 13:19:10 +0000921 radiiSizes.width = SkScalarToFLOAT(d0);
922 radiiSizes.height = SkScalarToFLOAT(d1);
923 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000924
bungeman@google.comb29c8832011-10-10 13:19:10 +0000925 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
926 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
927 gradStop1.get(),
928 &centerPoint,
929 &gradientOrigin,
930 &radiiSizes,
931 &gradientBrush),
932 "Could not create radial gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700933 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000934 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
935 "Could not set transform on radial gradient brush.");
936 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000937
bungeman@google.comb29c8832011-10-10 13:19:10 +0000938 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
939 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
940 "Could not get radial gradient stop collection.");
941 for (int i = 2; i < info.fColorCount; ++i) {
942 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
943 HR(createXpsGradientStop(info.fColors[i],
944 info.fColorOffsets[i],
945 &gradStop));
946 HRM(gradStopCollection->Append(gradStop.get()),
947 "Could not add radial gradient stop.");
948 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000949
Mike Reed5c5de212019-04-03 16:51:47 -0400950 HRM(gradientBrush->SetSpreadMethod(xps_spread_method((SkTileMode)info.fTileMode)),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000951 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000952
bungeman@google.comb29c8832011-10-10 13:19:10 +0000953 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
954 "Could not set opacity of radial gradient brush.");
955 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000956
bungeman@google.comb29c8832011-10-10 13:19:10 +0000957 return S_OK;
958}
959
960HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
961 IXpsOMBrush** brush,
962 const SkMatrix* parentTransform) {
963 const SkShader *shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -0700964 if (nullptr == shader) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000965 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
966 return S_OK;
967 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000968
bungeman@google.comb29c8832011-10-10 13:19:10 +0000969 //Gradient shaders.
970 SkShader::GradientInfo info;
971 info.fColorCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700972 info.fColors = nullptr;
973 info.fColorOffsets = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000974 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000975
bungeman@google.comb29c8832011-10-10 13:19:10 +0000976 if (SkShader::kNone_GradientType == gradientType) {
977 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000978
bungeman@google.comb29c8832011-10-10 13:19:10 +0000979 } else if (SkShader::kColor_GradientType == gradientType) {
980 SkASSERT(1 == info.fColorCount);
981 SkColor color;
982 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +0000983 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000984 SkAlpha alpha = skPaint.getAlpha();
985 HR(this->createXpsSolidColorBrush(color, alpha, brush));
986 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000987
bungeman@google.comb29c8832011-10-10 13:19:10 +0000988 } else {
989 if (info.fColorCount == 0) {
990 const SkColor color = skPaint.getColor();
991 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
992 return S_OK;
993 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000994
bungeman@google.comb29c8832011-10-10 13:19:10 +0000995 SkAutoTArray<SkColor> colors(info.fColorCount);
996 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
997 info.fColors = colors.get();
998 info.fColorOffsets = colorOffsets.get();
999 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001000
bungeman@google.comb29c8832011-10-10 13:19:10 +00001001 if (1 == info.fColorCount) {
1002 SkColor color = info.fColors[0];
1003 SkAlpha alpha = skPaint.getAlpha();
1004 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1005 return S_OK;
1006 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001007
Mike Reed7656b2c2019-04-08 11:48:20 -04001008 SkMatrix localMatrix = as_SB(shader)->getLocalMatrix();
bsalomon49f085d2014-09-05 13:34:00 -07001009 if (parentTransform) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001010 localMatrix.preConcat(*parentTransform);
1011 }
1012 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1013 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001014
bungeman@google.comb29c8832011-10-10 13:19:10 +00001015 if (SkShader::kLinear_GradientType == gradientType) {
1016 HR(this->createXpsLinearGradient(info,
1017 skPaint.getAlpha(),
1018 localMatrix,
1019 xpsMatrixToUse.get(),
1020 brush));
1021 return S_OK;
1022 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001023
bungeman@google.comb29c8832011-10-10 13:19:10 +00001024 if (SkShader::kRadial_GradientType == gradientType) {
1025 HR(this->createXpsRadialGradient(info,
1026 skPaint.getAlpha(),
1027 localMatrix,
1028 xpsMatrixToUse.get(),
1029 brush));
1030 return S_OK;
1031 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001032
reed71a6cbf2015-05-04 08:32:51 -07001033 if (SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001034 //simple if affine and one is 0, otherwise will have to fake
1035 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001036
bungeman@google.comb29c8832011-10-10 13:19:10 +00001037 if (SkShader::kSweep_GradientType == gradientType) {
1038 //have to fake
1039 }
1040 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001041
bungeman@google.comb29c8832011-10-10 13:19:10 +00001042 SkBitmap outTexture;
1043 SkMatrix outMatrix;
Mike Reed5c5de212019-04-03 16:51:47 -04001044 SkTileMode xy[2];
Mike Reed627778d2016-09-28 17:13:38 -04001045 SkImage* image = shader->isAImage(&outMatrix, xy);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001046 if (image && image->asLegacyBitmap(&outTexture)) {
reedf5822822015-08-19 11:46:38 -07001047 //TODO: outMatrix??
Mike Reed7656b2c2019-04-08 11:48:20 -04001048 SkMatrix localMatrix = as_SB(shader)->getLocalMatrix();
reedf5822822015-08-19 11:46:38 -07001049 if (parentTransform) {
Hal Canarybc212432017-03-17 11:48:59 -04001050 localMatrix.postConcat(*parentTransform);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001051 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001052
reedf5822822015-08-19 11:46:38 -07001053 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1054 HR(this->createXpsImageBrush(outTexture,
1055 localMatrix,
1056 xy,
1057 skPaint.getAlpha(),
1058 &tileBrush));
1059
1060 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
1061 } else {
1062 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1063 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001064 return S_OK;
1065}
1066
1067static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1068 const bool zeroWidth = (0 == paint.getStrokeWidth());
1069 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001070
bungeman@google.comb29c8832011-10-10 13:19:10 +00001071 return paint.getPathEffect() ||
1072 paint.getMaskFilter() ||
bungeman@google.comb29c8832011-10-10 13:19:10 +00001073 (stroke && (
1074 (matrix.hasPerspective() && !zeroWidth) ||
1075 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1076 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1077 paint.getStrokeMiter() < SK_ScalarSqrt2)
1078 ))
1079 ;
1080}
1081
1082HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1083 IXpsOMGeometryFigure** xpsRect) {
1084 const SkPoint points[4] = {
1085 { rect.fLeft, rect.fTop },
1086 { rect.fRight, rect.fTop },
1087 { rect.fRight, rect.fBottom },
1088 { rect.fLeft, rect.fBottom },
1089 };
1090 return this->createXpsQuad(points, stroke, fill, xpsRect);
1091}
1092HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1093 BOOL stroke, BOOL fill,
1094 IXpsOMGeometryFigure** xpsQuad) {
1095 // Define the start point.
1096 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001097
bungeman@google.comb29c8832011-10-10 13:19:10 +00001098 // Create the figure.
1099 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1100 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001101
bungeman@google.comb29c8832011-10-10 13:19:10 +00001102 // Define the type of each segment.
1103 XPS_SEGMENT_TYPE segmentTypes[3] = {
1104 XPS_SEGMENT_TYPE_LINE,
1105 XPS_SEGMENT_TYPE_LINE,
1106 XPS_SEGMENT_TYPE_LINE,
1107 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001108
bungeman@google.comb29c8832011-10-10 13:19:10 +00001109 // Define the x and y coordinates of each corner of the figure.
1110 FLOAT segmentData[6] = {
1111 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1112 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1113 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1114 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001115
bungeman@google.comb29c8832011-10-10 13:19:10 +00001116 // Describe if the segments are stroked.
1117 BOOL segmentStrokes[3] = {
1118 stroke, stroke, stroke,
1119 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001120
bungeman@google.comb29c8832011-10-10 13:19:10 +00001121 // Add the segment data to the figure.
1122 HRM((*xpsQuad)->SetSegments(
1123 3, 6,
1124 segmentTypes , segmentData, segmentStrokes),
1125 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001126
bungeman@google.comb29c8832011-10-10 13:19:10 +00001127 // Set the closed and filled properties of the figure.
1128 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1129 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001130
bungeman@google.comb29c8832011-10-10 13:19:10 +00001131 return S_OK;
1132}
1133
Mike Reeda1361362017-03-07 09:37:29 -05001134void SkXPSDevice::drawPoints(SkCanvas::PointMode mode,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001135 size_t count, const SkPoint points[],
1136 const SkPaint& paint) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001137 //TODO
bungeman@google.comb29c8832011-10-10 13:19:10 +00001138}
1139
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001140void SkXPSDevice::drawVertices(const SkVertices* v, const SkVertices::Bone bones[], int boneCount,
Ruiqi Maof5101492018-06-29 14:32:21 -04001141 SkBlendMode blendMode, const SkPaint& paint) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001142 //TODO
bungeman@google.comb29c8832011-10-10 13:19:10 +00001143}
1144
Mike Reeda1361362017-03-07 09:37:29 -05001145void SkXPSDevice::drawPaint(const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001146 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001147
bungeman@google.comb29c8832011-10-10 13:19:10 +00001148 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001149 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1150 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1151 if (paint->getStyle() != SkPaint::kFill_Style) {
1152 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001153 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001154
Mike Reeda1361362017-03-07 09:37:29 -05001155 this->internalDrawRect(r, false, *fillPaint);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001156}
1157
Mike Reeda1361362017-03-07 09:37:29 -05001158void SkXPSDevice::drawRect(const SkRect& r,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001159 const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -05001160 this->internalDrawRect(r, true, paint);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001161}
1162
Mike Reeda1361362017-03-07 09:37:29 -05001163void SkXPSDevice::drawRRect(const SkRRect& rr,
scroggo@google.comcac8d012013-11-12 17:10:02 +00001164 const SkPaint& paint) {
1165 SkPath path;
1166 path.addRRect(rr);
Robert Phillips137ca522018-08-15 10:14:33 -04001167 this->drawPath(path, paint, true);
scroggo@google.comcac8d012013-11-12 17:10:02 +00001168}
1169
Mike Reeda1361362017-03-07 09:37:29 -05001170static SkIRect size(const SkBaseDevice& dev) { return {0, 0, dev.width(), dev.height()}; }
1171
1172void SkXPSDevice::internalDrawRect(const SkRect& r,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001173 bool transformRect,
1174 const SkPaint& paint) {
1175 //Exit early if there is nothing to draw.
Mike Reeda1361362017-03-07 09:37:29 -05001176 if (this->cs().isEmpty(size(*this)) ||
reed374772b2016-10-05 17:33:02 -07001177 (paint.getAlpha() == 0 && paint.isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001178 return;
1179 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001180
bungeman@google.comb29c8832011-10-10 13:19:10 +00001181 //Path the rect if we can't optimize it.
Mike Reeda1361362017-03-07 09:37:29 -05001182 if (rect_must_be_pathed(paint, this->ctm())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001183 SkPath tmp;
1184 tmp.addRect(r);
1185 tmp.setFillType(SkPath::kWinding_FillType);
Robert Phillips137ca522018-08-15 10:14:33 -04001186 this->drawPath(tmp, paint, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001187 return;
1188 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001189
bungeman@google.comb29c8832011-10-10 13:19:10 +00001190 //Create the shaded path.
1191 SkTScopedComPtr<IXpsOMPath> shadedPath;
1192 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1193 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001194
bungeman@google.comb29c8832011-10-10 13:19:10 +00001195 //Create the shaded geometry.
1196 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1197 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1198 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001199
bungeman@google.comb29c8832011-10-10 13:19:10 +00001200 //Add the geometry to the shaded path.
1201 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1202 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001203
bungeman@google.comb29c8832011-10-10 13:19:10 +00001204 //Set the brushes.
1205 BOOL fill = FALSE;
1206 BOOL stroke = FALSE;
Mike Reeda1361362017-03-07 09:37:29 -05001207 HRV(this->shadePath(shadedPath.get(), paint, this->ctm(), &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001208
bungeman@google.comb29c8832011-10-10 13:19:10 +00001209 bool xpsTransformsPath = true;
1210 //Transform the geometry.
1211 if (transformRect && xpsTransformsPath) {
1212 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
Mike Reeda1361362017-03-07 09:37:29 -05001213 HRV(this->createXpsTransform(this->ctm(), &xpsTransform));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001214 if (xpsTransform.get()) {
1215 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1216 "Could not set transform for rect.");
1217 } else {
1218 xpsTransformsPath = false;
1219 }
1220 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001221
bungeman@google.comb29c8832011-10-10 13:19:10 +00001222 //Create the figure.
1223 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1224 {
1225 SkPoint points[4] = {
1226 { r.fLeft, r.fTop },
1227 { r.fLeft, r.fBottom },
1228 { r.fRight, r.fBottom },
1229 { r.fRight, r.fTop },
1230 };
1231 if (!xpsTransformsPath && transformRect) {
Mike Reeda1361362017-03-07 09:37:29 -05001232 this->ctm().mapPoints(points, SK_ARRAY_COUNT(points));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001233 }
1234 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1235 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001236
bungeman@google.comb29c8832011-10-10 13:19:10 +00001237 //Get the figures of the shaded geometry.
1238 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1239 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1240 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001241
bungeman@google.comb29c8832011-10-10 13:19:10 +00001242 //Add the figure to the shaded geometry figures.
1243 HRVM(shadedFigures->Append(rectFigure.get()),
1244 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001245
Mike Reeda1361362017-03-07 09:37:29 -05001246 HRV(this->clip(shadedPath.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001247
bungeman@google.comb29c8832011-10-10 13:19:10 +00001248 //Add the shaded path to the current visuals.
1249 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1250 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1251 "Could not get current visuals for rect.");
1252 HRVM(currentVisuals->Append(shadedPath.get()),
1253 "Could not add rect to current visuals.");
1254}
1255
1256static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1257 const SkTDArray<BOOL>& segmentStrokes,
1258 const SkTDArray<FLOAT>& segmentData,
1259 BOOL stroke, BOOL fill,
1260 IXpsOMGeometryFigure* figure,
1261 IXpsOMGeometryFigureCollection* figures) {
1262 // Add the segment data to the figure.
1263 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1264 segmentTypes.begin() , segmentData.begin(),
1265 segmentStrokes.begin()),
1266 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001267
bungeman@google.comb29c8832011-10-10 13:19:10 +00001268 // Set the closed and filled properties of the figure.
1269 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1270 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001271
bungeman@google.comb29c8832011-10-10 13:19:10 +00001272 // Add the figure created above to this geometry.
1273 HRM(figures->Append(figure), "Could not add path to geometry.");
1274 return S_OK;
1275}
1276
1277HRESULT SkXPSDevice::addXpsPathGeometry(
1278 IXpsOMGeometryFigureCollection* xpsFigures,
1279 BOOL stroke, BOOL fill, const SkPath& path) {
1280 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1281 SkTDArray<BOOL> segmentStrokes;
1282 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001283
bungeman@google.comb29c8832011-10-10 13:19:10 +00001284 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1285 SkPath::Iter iter(path, true);
1286 SkPoint points[4];
1287 SkPath::Verb verb;
1288 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1289 switch (verb) {
1290 case SkPath::kMove_Verb: {
bsalomon49f085d2014-09-05 13:34:00 -07001291 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001292 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1293 stroke, fill,
1294 xpsFigure.get() , xpsFigures));
1295 xpsFigure.reset();
1296 segmentTypes.rewind();
1297 segmentStrokes.rewind();
1298 segmentData.rewind();
1299 }
1300 // Define the start point.
1301 XPS_POINT startPoint = xps_point(points[0]);
1302 // Create the figure.
1303 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1304 &xpsFigure),
1305 "Could not create path geometry figure.");
1306 break;
1307 }
1308 case SkPath::kLine_Verb:
1309 if (iter.isCloseLine()) break; //ignore the line, auto-closed
Mike Reedb5475792018-08-08 16:17:42 -04001310 segmentTypes.push_back(XPS_SEGMENT_TYPE_LINE);
1311 segmentStrokes.push_back(stroke);
1312 segmentData.push_back(SkScalarToFLOAT(points[1].fX));
1313 segmentData.push_back(SkScalarToFLOAT(points[1].fY));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001314 break;
1315 case SkPath::kQuad_Verb:
Mike Reedb5475792018-08-08 16:17:42 -04001316 segmentTypes.push_back(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1317 segmentStrokes.push_back(stroke);
1318 segmentData.push_back(SkScalarToFLOAT(points[1].fX));
1319 segmentData.push_back(SkScalarToFLOAT(points[1].fY));
1320 segmentData.push_back(SkScalarToFLOAT(points[2].fX));
1321 segmentData.push_back(SkScalarToFLOAT(points[2].fY));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001322 break;
1323 case SkPath::kCubic_Verb:
Mike Reedb5475792018-08-08 16:17:42 -04001324 segmentTypes.push_back(XPS_SEGMENT_TYPE_BEZIER);
1325 segmentStrokes.push_back(stroke);
1326 segmentData.push_back(SkScalarToFLOAT(points[1].fX));
1327 segmentData.push_back(SkScalarToFLOAT(points[1].fY));
1328 segmentData.push_back(SkScalarToFLOAT(points[2].fX));
1329 segmentData.push_back(SkScalarToFLOAT(points[2].fY));
1330 segmentData.push_back(SkScalarToFLOAT(points[3].fX));
1331 segmentData.push_back(SkScalarToFLOAT(points[3].fY));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001332 break;
halcanary47ef4d52015-03-03 09:13:09 -08001333 case SkPath::kConic_Verb: {
1334 const SkScalar tol = SK_Scalar1 / 4;
1335 SkAutoConicToQuads converter;
1336 const SkPoint* quads =
1337 converter.computeQuads(points, iter.conicWeight(), tol);
1338 for (int i = 0; i < converter.countQuads(); ++i) {
Mike Reedb5475792018-08-08 16:17:42 -04001339 segmentTypes.push_back(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1340 segmentStrokes.push_back(stroke);
1341 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 1].fX));
1342 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 1].fY));
1343 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 2].fX));
1344 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 2].fY));
halcanary47ef4d52015-03-03 09:13:09 -08001345 }
1346 break;
1347 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001348 case SkPath::kClose_Verb:
1349 // we ignore these, and just get the whole segment from
1350 // the corresponding line/quad/cubic verbs
1351 break;
1352 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001353 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001354 break;
1355 }
1356 }
bsalomon49f085d2014-09-05 13:34:00 -07001357 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001358 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1359 stroke, fill,
1360 xpsFigure.get(), xpsFigures));
1361 }
1362 return S_OK;
1363}
1364
bungeman@google.comb29c8832011-10-10 13:19:10 +00001365void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1366 SkMatrix* matrix,
1367 SkVector* ppuScale,
1368 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001369 //This action is in unit space, but the ppm is specified in physical space.
reed80ea19c2015-05-12 10:37:34 -07001370 ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
1371 fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001372
bungeman@google.comb29c8832011-10-10 13:19:10 +00001373 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001374
bungeman@google.comb29c8832011-10-10 13:19:10 +00001375 const SkIRect& irect = clip;
Mike Reed8be952a2017-02-13 20:44:33 -05001376 SkRect clipRect = SkRect::MakeLTRB(SkIntToScalar(irect.fLeft) * ppuScale->fX,
1377 SkIntToScalar(irect.fTop) * ppuScale->fY,
1378 SkIntToScalar(irect.fRight) * ppuScale->fX,
1379 SkIntToScalar(irect.fBottom) * ppuScale->fY);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001380 clipRect.roundOut(clipIRect);
1381}
1382
Mike Reeda1361362017-03-07 09:37:29 -05001383HRESULT SkXPSDevice::applyMask(const SkMask& mask,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001384 const SkVector& ppuScale,
1385 IXpsOMPath* shadedPath) {
1386 //Get the geometry object.
1387 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1388 HRM(shadedPath->GetGeometry(&shadedGeometry),
1389 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001390
bungeman@google.comb29c8832011-10-10 13:19:10 +00001391 //Get the figures from the geometry.
1392 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1393 HRM(shadedGeometry->GetFigures(&shadedFigures),
1394 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001395
bungeman@google.comb29c8832011-10-10 13:19:10 +00001396 SkMatrix m;
1397 m.reset();
1398 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1399 SkIntToScalar(mask.fBounds.fTop));
1400 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001401
Mike Reed5c5de212019-04-03 16:51:47 -04001402 SkTileMode xy[2];
1403 xy[0] = (SkTileMode)3;
1404 xy[1] = (SkTileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001405
bungeman@google.comb29c8832011-10-10 13:19:10 +00001406 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001407 bm.installMaskPixels(mask);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001408
bungeman@google.comb29c8832011-10-10 13:19:10 +00001409 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1410 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1411 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1412 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001413
bungeman@google.comb29c8832011-10-10 13:19:10 +00001414 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1415 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1416 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1417 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1418 "Could not create mask shaded figure.");
1419 HRM(shadedFigures->Append(shadedFigure.get()),
1420 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001421
Mike Reeda1361362017-03-07 09:37:29 -05001422 HR(this->clip(shadedPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001423
bungeman@google.comb29c8832011-10-10 13:19:10 +00001424 //Add the path to the active visual collection.
1425 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1426 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1427 "Could not get mask current visuals.");
1428 HRM(currentVisuals->Append(shadedPath),
1429 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001430
bungeman@google.comb29c8832011-10-10 13:19:10 +00001431 return S_OK;
1432}
1433
1434HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1435 const SkPaint& shaderPaint,
1436 const SkMatrix& matrix,
1437 BOOL* fill, BOOL* stroke) {
1438 *fill = FALSE;
1439 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001440
bungeman@google.comb29c8832011-10-10 13:19:10 +00001441 const SkPaint::Style style = shaderPaint.getStyle();
1442 const bool hasFill = SkPaint::kFill_Style == style
1443 || SkPaint::kStrokeAndFill_Style == style;
1444 const bool hasStroke = SkPaint::kStroke_Style == style
1445 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001446
bungeman@google.comb29c8832011-10-10 13:19:10 +00001447 //TODO(bungeman): use dictionaries and lookups.
1448 if (hasFill) {
1449 *fill = TRUE;
1450 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1451 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1452 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1453 "Could not set fill for shaded path.");
1454 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001455
bungeman@google.comb29c8832011-10-10 13:19:10 +00001456 if (hasStroke) {
1457 *stroke = TRUE;
1458 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1459 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1460 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1461 "Could not set stroke brush for shaded path.");
1462 HRM(shadedPath->SetStrokeThickness(
1463 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1464 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001465
bungeman@google.comb29c8832011-10-10 13:19:10 +00001466 if (0 == shaderPaint.getStrokeWidth()) {
1467 //XPS hair width is a hack. (XPS Spec 11.6.12).
1468 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1469 HRM(shadedPath->GetStrokeDashes(&dashes),
1470 "Could not set dashes for shaded path.");
1471 XPS_DASH dash;
1472 dash.length = 1.0;
1473 dash.gap = 0.0;
1474 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1475 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1476 "Could not set dash offset for shaded path.");
1477 }
1478 }
1479 return S_OK;
1480}
1481
Mike Reeda1361362017-03-07 09:37:29 -05001482void SkXPSDevice::drawPath(const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001483 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001484 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001485 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1486
bungeman@google.comb29c8832011-10-10 13:19:10 +00001487 // nothing to draw
Mike Reeda1361362017-03-07 09:37:29 -05001488 if (this->cs().isEmpty(size(*this)) ||
reed374772b2016-10-05 17:33:02 -07001489 (paint->getAlpha() == 0 && paint->isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001490 return;
1491 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001492
bungeman@google.comb29c8832011-10-10 13:19:10 +00001493 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001494 const bool paintHasPathEffect = paint->getPathEffect()
1495 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001496
bungeman@google.comb29c8832011-10-10 13:19:10 +00001497 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
Mike Reeda1361362017-03-07 09:37:29 -05001498 SkMatrix matrix = this->ctm();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001499 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001500
bungeman@google.comb29c8832011-10-10 13:19:10 +00001501 //Apply path effect [Skeletal-path -> Fillable-path].
1502 SkPath* fillablePath = skeletalPath;
1503 if (paintHasPathEffect) {
1504 if (!pathIsMutable) {
1505 fillablePath = &modifiedPath;
1506 pathIsMutable = true;
1507 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001508 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001509
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001510 SkPaint* writablePaint = paint.writable();
halcanary96fcdcc2015-08-27 07:41:13 -07001511 writablePaint->setPathEffect(nullptr);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001512 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001513 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001514 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001515 writablePaint->setStyle(SkPaint::kStroke_Style);
1516 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001517 }
1518 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001519
bungeman@google.comb29c8832011-10-10 13:19:10 +00001520 //Create the shaded path. This will be the path which is painted.
1521 SkTScopedComPtr<IXpsOMPath> shadedPath;
1522 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1523 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001524
bungeman@google.comb29c8832011-10-10 13:19:10 +00001525 //Create the geometry for the shaded path.
1526 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1527 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1528 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001529
bungeman@google.comb29c8832011-10-10 13:19:10 +00001530 //Add the geometry to the shaded path.
1531 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1532 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001533
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001534 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001535
1536 //Determine if we will draw or shade and mask.
Mike Reed8ad91a92018-01-19 19:09:32 -05001537 if (filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001538 if (paint->getStyle() != SkPaint::kFill_Style) {
1539 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001540 }
1541 }
1542
bungeman@google.comb29c8832011-10-10 13:19:10 +00001543 //Set the brushes.
1544 BOOL fill;
1545 BOOL stroke;
1546 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001547 *paint,
Mike Reeda1361362017-03-07 09:37:29 -05001548 this->ctm(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001549 &fill,
1550 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001551
bungeman@google.comb29c8832011-10-10 13:19:10 +00001552 //Mask filter
1553 if (filter) {
1554 SkIRect clipIRect;
1555 SkVector ppuScale;
1556 this->convertToPpm(filter,
1557 &matrix,
1558 &ppuScale,
Mike Reeda1361362017-03-07 09:37:29 -05001559 this->cs().bounds(size(*this)).roundOut(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001560 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001561
bungeman@google.comb29c8832011-10-10 13:19:10 +00001562 //[Fillable-path -> Pixel-path]
1563 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1564 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001565
halcanary96fcdcc2015-08-27 07:41:13 -07001566 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001567
bsalomon055e1922016-05-06 07:22:58 -07001568 SkASSERT(SkPaint::kFill_Style == paint->getStyle() ||
1569 (SkPaint::kStroke_Style == paint->getStyle() && 0 == paint->getStrokeWidth()));
1570 SkStrokeRec::InitStyle style = (SkPaint::kFill_Style == paint->getStyle())
1571 ? SkStrokeRec::kFill_InitStyle
1572 : SkStrokeRec::kHairline_InitStyle;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001573 //[Pixel-path -> Mask]
1574 SkMask rasteredMask;
1575 if (SkDraw::DrawToMask(
1576 *pixelPath,
1577 &clipIRect,
1578 filter, //just to compute how much to draw.
1579 &matrix,
1580 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001581 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon055e1922016-05-06 07:22:58 -07001582 style)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001583
bungeman@google.comb29c8832011-10-10 13:19:10 +00001584 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1585 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001586
bungeman@google.comb29c8832011-10-10 13:19:10 +00001587 //[Mask -> Mask]
1588 SkMask filteredMask;
Mike Reed80747ef2018-01-23 15:29:32 -05001589 if (as_MFB(filter)->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001590 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001591 }
1592 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001593
bungeman@google.comb29c8832011-10-10 13:19:10 +00001594 //Draw mask.
Mike Reeda1361362017-03-07 09:37:29 -05001595 HRV(this->applyMask(*mask, ppuScale, shadedPath.get()));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001596 }
1597 return;
1598 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001599
bungeman@google.comb29c8832011-10-10 13:19:10 +00001600 //Get the figures from the shaded geometry.
1601 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1602 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1603 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001604
bungeman@google.comb29c8832011-10-10 13:19:10 +00001605 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001606
bungeman@google.comb29c8832011-10-10 13:19:10 +00001607 //Set the fill rule.
bungeman76db31a2014-08-25 07:31:53 -07001608 SkPath* xpsCompatiblePath = fillablePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001609 XPS_FILL_RULE xpsFillRule;
bungeman76db31a2014-08-25 07:31:53 -07001610 switch (fillablePath->getFillType()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001611 case SkPath::kWinding_FillType:
1612 xpsFillRule = XPS_FILL_RULE_NONZERO;
1613 break;
1614 case SkPath::kEvenOdd_FillType:
1615 xpsFillRule = XPS_FILL_RULE_EVENODD;
1616 break;
1617 case SkPath::kInverseWinding_FillType: {
bungeman76db31a2014-08-25 07:31:53 -07001618 //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
1619 if (!pathIsMutable) {
1620 xpsCompatiblePath = &modifiedPath;
1621 pathIsMutable = true;
1622 }
1623 if (!Simplify(*fillablePath, xpsCompatiblePath)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -04001624 SkDEBUGF("Could not simplify inverse winding path.");
bungeman76db31a2014-08-25 07:31:53 -07001625 return;
1626 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001627 }
Ben Wagner11eae3d2019-08-22 17:40:34 -04001628 // The xpsCompatiblePath is now inverse even odd, so fall through.
bungeman@google.comb29c8832011-10-10 13:19:10 +00001629 case SkPath::kInverseEvenOdd_FillType: {
1630 const SkRect universe = SkRect::MakeLTRB(
1631 0, 0,
1632 this->fCurrentCanvasSize.fWidth,
1633 this->fCurrentCanvasSize.fHeight);
1634 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1635 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1636 HRVM(shadedFigures->Append(addOneFigure.get()),
1637 "Could not add even-odd flip figure to shaded path.");
1638 xpsTransformsPath = false;
1639 xpsFillRule = XPS_FILL_RULE_EVENODD;
1640 break;
1641 }
1642 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001643 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001644 }
1645 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1646 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001647
bungeman@google.comb29c8832011-10-10 13:19:10 +00001648 //Create the XPS transform, if possible.
1649 if (xpsTransformsPath) {
1650 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1651 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001652
bungeman@google.comb29c8832011-10-10 13:19:10 +00001653 if (xpsTransform.get()) {
1654 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1655 "Could not set transform on shaded path.");
1656 } else {
1657 xpsTransformsPath = false;
1658 }
1659 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001660
bungeman76db31a2014-08-25 07:31:53 -07001661 SkPath* devicePath = xpsCompatiblePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001662 if (!xpsTransformsPath) {
1663 //[Fillable-path -> Device-path]
bungeman76db31a2014-08-25 07:31:53 -07001664 devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
1665 xpsCompatiblePath->transform(matrix, devicePath);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001666 }
1667 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1668 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001669
Mike Reeda1361362017-03-07 09:37:29 -05001670 HRV(this->clip(shadedPath.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001671
bungeman@google.comb29c8832011-10-10 13:19:10 +00001672 //Add the path to the active visual collection.
1673 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1674 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1675 "Could not get current visuals for shaded path.");
1676 HRVM(currentVisuals->Append(shadedPath.get()),
1677 "Could not add shaded path to current visuals.");
1678}
1679
Mike Reeda1361362017-03-07 09:37:29 -05001680HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001681 if (this->cs().isWideOpen()) {
1682 return S_OK;
1683 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001684 SkPath clipPath;
Mike Reeda1361362017-03-07 09:37:29 -05001685 // clipPath.addRect(this->cs().bounds(size(*this)));
1686 (void)this->cs().asPath(&clipPath);
Ben Wagner11eae3d2019-08-22 17:40:34 -04001687 // TODO: handle all the kinds of paths, like drawPath does
bungeman@google.comb29c8832011-10-10 13:19:10 +00001688 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1689}
1690HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1691 const SkPath& clipPath,
1692 XPS_FILL_RULE fillRule) {
1693 //Create the geometry.
1694 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1695 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1696 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001697
bungeman@google.comb29c8832011-10-10 13:19:10 +00001698 //Get the figure collection of the geometry.
1699 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1700 HRM(clipGeometry->GetFigures(&clipFigures),
1701 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001702
bungeman@google.comb29c8832011-10-10 13:19:10 +00001703 //Create the figures into the geometry.
1704 HR(this->addXpsPathGeometry(
1705 clipFigures.get(),
1706 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001707
bungeman@google.comb29c8832011-10-10 13:19:10 +00001708 HRM(clipGeometry->SetFillRule(fillRule),
1709 "Could not set fill rule.");
1710 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1711 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001712
bungeman@google.comb29c8832011-10-10 13:19:10 +00001713 return S_OK;
1714}
1715
Mike Reeda1361362017-03-07 09:37:29 -05001716void SkXPSDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001717 //TODO: override this for XPS
Hal Canary2b0e6cd2018-07-09 12:43:39 -04001718 SkDEBUGF("XPS drawSprite not yet implemented.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001719}
1720
Ben Wagner11eae3d2019-08-22 17:40:34 -04001721HRESULT SkXPSDevice::CreateTypefaceUse(const SkFont& font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001722 TypefaceUse** typefaceUse) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001723 SkAutoResolveDefaultTypeface typeface(font.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001724
bungeman@google.comb29c8832011-10-10 13:19:10 +00001725 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00001726 const SkFontID typefaceID = typeface->uniqueID();
Ben Wagner11eae3d2019-08-22 17:40:34 -04001727 for (TypefaceUse& current : this->fTypefaces) {
1728 if (current.typefaceId == typefaceID) {
1729 *typefaceUse = &current;
1730 return S_OK;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001731 }
1732 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001733
bungeman@google.comb29c8832011-10-10 13:19:10 +00001734 //TODO: create glyph only fonts
1735 //and let the host deal with what kind of font we're looking at.
1736 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001737
bungeman@google.comb29c8832011-10-10 13:19:10 +00001738 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001739 int ttcIndex;
Ben Wagnerff84d8a2019-02-26 15:39:41 -05001740 std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex);
Ben Wagner11eae3d2019-08-22 17:40:34 -04001741 if (!fontData) {
1742 return E_NOTIMPL;
1743 }
bungeman@google.com635091f2013-10-01 15:03:18 +00001744 //TODO: cannot handle FON fonts.
Ben Wagner11eae3d2019-08-22 17:40:34 -04001745 HRM(SkIStream::CreateFromSkStream(fontData->duplicate(), &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001746 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001747
bungeman@google.comb29c8832011-10-10 13:19:10 +00001748 const size_t size =
1749 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1750 wchar_t buffer[size];
1751 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -04001752 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001753 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001754
bungeman@google.comb29c8832011-10-10 13:19:10 +00001755 SkTScopedComPtr<IOpcPartUri> partUri;
1756 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1757 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001758
bungeman@google.comb29c8832011-10-10 13:19:10 +00001759 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1760 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1761 embedding,
1762 partUri.get(),
1763 FALSE,
1764 &xpsFontResource),
1765 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001766
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001767 //TODO: change openStream to return -1 for non-ttc, get rid of this.
1768 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1769 bool isTTC = (data &&
1770 fontData->getLength() >= sizeof(SkTTCFHeader) &&
1771 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1772
Ben Wagner11eae3d2019-08-22 17:40:34 -04001773 int glyphCount = typeface->countGlyphs();
1774
1775 TypefaceUse& newTypefaceUse = this->fTypefaces.emplace_back(
1776 typefaceID,
1777 isTTC ? ttcIndex : -1,
1778 std::move(fontData),
1779 std::move(xpsFontResource),
1780 glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001781
bungeman@google.comb29c8832011-10-10 13:19:10 +00001782 *typefaceUse = &newTypefaceUse;
1783 return S_OK;
1784}
1785
Mike Reeda1361362017-03-07 09:37:29 -05001786HRESULT SkXPSDevice::AddGlyphs(IXpsOMObjectFactory* xpsFactory,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001787 IXpsOMCanvas* canvas,
Ben Wagner11eae3d2019-08-22 17:40:34 -04001788 const TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001789 LPCWSTR text,
1790 XPS_GLYPH_INDEX* xpsGlyphs,
1791 UINT32 xpsGlyphsLen,
1792 XPS_POINT *origin,
1793 FLOAT fontSize,
1794 XPS_STYLE_SIMULATION sims,
1795 const SkMatrix& transform,
1796 const SkPaint& paint) {
1797 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
Ben Wagner11eae3d2019-08-22 17:40:34 -04001798 HRM(xpsFactory->CreateGlyphs(font->xpsFont.get(), &glyphs), "Could not create glyphs.");
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001799 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001800
bungeman@google.comb29c8832011-10-10 13:19:10 +00001801 //XPS uses affine transformations for everything...
1802 //...except positioning text.
1803 bool useCanvasForClip;
Ben Wagner11eae3d2019-08-22 17:40:34 -04001804 if (transform.isTranslate()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001805 origin->x += SkScalarToFLOAT(transform.getTranslateX());
1806 origin->y += SkScalarToFLOAT(transform.getTranslateY());
1807 useCanvasForClip = false;
1808 } else {
1809 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1810 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1811 if (xpsMatrixToUse.get()) {
1812 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1813 "Could not set transform matrix.");
1814 useCanvasForClip = true;
1815 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00001816 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001817 useCanvasForClip = false;
1818 }
1819 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001820
bungeman@google.comb29c8832011-10-10 13:19:10 +00001821 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1822 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001823
bsalomon49f085d2014-09-05 13:34:00 -07001824 if (text) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001825 HRM(glyphsEditor->SetUnicodeString(text),
1826 "Could not set unicode string.");
1827 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001828
bsalomon49f085d2014-09-05 13:34:00 -07001829 if (xpsGlyphs) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001830 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
1831 "Could not set glyphs.");
1832 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001833
bungeman@google.comb29c8832011-10-10 13:19:10 +00001834 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001835
bungeman@google.comb29c8832011-10-10 13:19:10 +00001836 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
1837 HR(this->createXpsBrush(
1838 paint,
1839 &xpsFillBrush,
halcanary96fcdcc2015-08-27 07:41:13 -07001840 useCanvasForClip ? nullptr : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001841
bungeman@google.comb29c8832011-10-10 13:19:10 +00001842 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
1843 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001844
bungeman@google.comb29c8832011-10-10 13:19:10 +00001845 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001846
bungeman@google.comb29c8832011-10-10 13:19:10 +00001847 HRM(glyphs->SetFontRenderingEmSize(fontSize),
1848 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001849
bungeman@google.comb29c8832011-10-10 13:19:10 +00001850 HRM(glyphs->SetStyleSimulations(sims),
1851 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001852
bungeman@google.comb29c8832011-10-10 13:19:10 +00001853 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
1854 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001855
bungeman@google.comb29c8832011-10-10 13:19:10 +00001856 if (!useCanvasForClip) {
Mike Reeda1361362017-03-07 09:37:29 -05001857 HR(this->clip(glyphs.get()));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001858 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
1859 } else {
1860 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
1861 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
1862 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001863
bungeman@google.comb29c8832011-10-10 13:19:10 +00001864 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
1865 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
1866 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001867
bungeman@google.comb29c8832011-10-10 13:19:10 +00001868 HRM(glyphCanvasVisuals->Append(glyphs.get()),
1869 "Could not add glyphs to page.");
Mike Reeda1361362017-03-07 09:37:29 -05001870 HR(this->clip(glyphCanvas.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001871
bungeman@google.comb29c8832011-10-10 13:19:10 +00001872 HRM(visuals->Append(glyphCanvas.get()),
1873 "Could not add glyph canvas to page.");
1874 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001875
bungeman@google.comb29c8832011-10-10 13:19:10 +00001876 return S_OK;
1877}
1878
bungeman@google.comb29c8832011-10-10 13:19:10 +00001879static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1880 const SkPaint::Style style = paint.getStyle();
1881 return matrix.hasPerspective()
1882 || SkPaint::kStroke_Style == style
1883 || SkPaint::kStrokeAndFill_Style == style
1884 || paint.getMaskFilter()
bungeman@google.comb29c8832011-10-10 13:19:10 +00001885 ;
1886}
1887
Ben Wagner11eae3d2019-08-22 17:40:34 -04001888void SkXPSDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
herbbda26432015-11-24 08:37:01 -08001889
Ben Wagner11eae3d2019-08-22 17:40:34 -04001890 const SkPaint& paint = glyphRunList.paint();
1891 for (const auto& run : glyphRunList) {
1892 const SkGlyphID* glyphIDs = run.glyphsIDs().data();
1893 size_t glyphCount = run.glyphsIDs().size();
1894 const SkFont& font = run.font();
herbbda26432015-11-24 08:37:01 -08001895
Ben Wagner11eae3d2019-08-22 17:40:34 -04001896 if (!glyphCount || !glyphIDs || font.getSize() <= 0) {
1897 continue;
herbbda26432015-11-24 08:37:01 -08001898 }
Ben Wagner11eae3d2019-08-22 17:40:34 -04001899
1900 TypefaceUse* typeface;
1901 if (FAILED(CreateTypefaceUse(font, &typeface)) || text_must_be_pathed(paint, this->ctm())) {
1902 SkPath path;
1903 //TODO: make this work, Draw currently does not handle as well.
1904 //paint.getTextPath(text, byteLength, x, y, &path);
1905 //this->drawPath(path, paint, nullptr, true);
1906 //TODO: add automation "text"
1907 continue;
herbbda26432015-11-24 08:37:01 -08001908 }
Ben Wagner11eae3d2019-08-22 17:40:34 -04001909
1910 //TODO: handle font scale and skew in x (text_scale_skew)
1911
1912 // Advance width and offsets for glyphs measured in hundredths of the font em size
1913 // (XPS Spec 5.1.3).
1914 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(font.getSize());
1915 SkAutoSTMalloc<32, XPS_GLYPH_INDEX> xpsGlyphs(glyphCount);
1916
1917 for (size_t i = 0; i < glyphCount; ++i) {
1918 const SkPoint& position = run.positions()[i];
1919 XPS_GLYPH_INDEX& xpsGlyph = xpsGlyphs[i];
1920 xpsGlyph.index = glyphIDs[i];
1921 xpsGlyph.advanceWidth = 0.0f;
1922 xpsGlyph.horizontalOffset = (SkScalarToFloat(position.fX) * centemPerUnit);
1923 xpsGlyph.verticalOffset = (SkScalarToFloat(position.fY) * -centemPerUnit);
1924 typeface->glyphsUsed.set(xpsGlyph.index);
1925 }
1926
1927 XPS_POINT origin = {
1928 glyphRunList.origin().x(),
1929 glyphRunList.origin().y(),
1930 };
1931
1932 HRV(AddGlyphs(this->fXpsFactory.get(),
1933 this->fCurrentXpsCanvas.get(),
1934 typeface,
1935 nullptr,
1936 xpsGlyphs.get(), glyphCount,
1937 &origin,
1938 SkScalarToFLOAT(font.getSize()),
1939 XPS_STYLE_SIMULATION_NONE,
1940 this->ctm(),
1941 paint));
herbbda26432015-11-24 08:37:01 -08001942 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001943}
Ben Wagner11eae3d2019-08-22 17:40:34 -04001944
Mike Reeda1361362017-03-07 09:37:29 -05001945void SkXPSDevice::drawDevice( SkBaseDevice* dev,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001946 int x, int y,
1947 const SkPaint&) {
1948 SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001949
bungeman@google.comb29c8832011-10-10 13:19:10 +00001950 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
Mike Reeda1361362017-03-07 09:37:29 -05001951 // TODO(halcanary): assert that current transform is identity rather than calling setter.
1952 XPS_MATRIX rawTransform = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f};
bungeman@google.comb29c8832011-10-10 13:19:10 +00001953 HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
1954 "Could not create layer transform.");
1955 HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
1956 "Could not set layer transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001957
bungeman@google.comb29c8832011-10-10 13:19:10 +00001958 //Get the current visual collection and add the layer to it.
1959 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1960 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1961 "Could not get current visuals for layer.");
1962 HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
1963 "Could not add layer to current visuals.");
1964}
1965
reed76033be2015-03-14 10:54:31 -07001966SkBaseDevice* SkXPSDevice::onCreateDevice(const CreateInfo& info, const SkPaint*) {
bungeman@google.com635091f2013-10-01 15:03:18 +00001967//Conditional for bug compatibility with PDF device.
1968#if 0
fmalita6987dca2014-11-13 08:33:37 -08001969 if (SkBaseDevice::kGeneral_Usage == info.fUsage) {
halcanary96fcdcc2015-08-27 07:41:13 -07001970 return nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001971 //To what stream do we write?
1972 //SkXPSDevice* dev = new SkXPSDevice(this);
1973 //SkSize s = SkSize::Make(width, height);
1974 //dev->BeginCanvas(s, s, SkMatrix::I());
1975 //return dev;
1976 }
bungeman@google.com635091f2013-10-01 15:03:18 +00001977#endif
Hal Canaryabc88d22017-02-06 09:26:49 -05001978 SkXPSDevice* dev = new SkXPSDevice(info.fInfo.dimensions());
1979 // TODO(halcanary) implement copy constructor on SkTScopedCOmPtr
1980 dev->fXpsFactory.reset(SkRefComPtr(fXpsFactory.get()));
1981 SkAssertResult(dev->createCanvasForLayer());
1982 return dev;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001983}
1984
Mike Reeda1361362017-03-07 09:37:29 -05001985void SkXPSDevice::drawOval( const SkRect& o, const SkPaint& p) {
Hal Canaryb39b09e2017-02-01 17:04:44 -05001986 SkPath path;
1987 path.addOval(o);
Robert Phillips137ca522018-08-15 10:14:33 -04001988 this->drawPath(path, p, true);
Hal Canaryb39b09e2017-02-01 17:04:44 -05001989}
1990
Mike Reeda1361362017-03-07 09:37:29 -05001991void SkXPSDevice::drawBitmapRect(const SkBitmap& bitmap,
1992 const SkRect* src,
1993 const SkRect& dst,
1994 const SkPaint& paint,
1995 SkCanvas::SrcRectConstraint constraint) {
Hal Canary7da8d642017-03-17 15:16:21 -04001996 SkRect bitmapBounds = SkRect::Make(bitmap.bounds());
1997 SkRect srcBounds = src ? *src : bitmapBounds;
Hal Canaryb39b09e2017-02-01 17:04:44 -05001998 SkMatrix matrix = SkMatrix::MakeRectToRect(srcBounds, dst, SkMatrix::kFill_ScaleToFit);
Hal Canary7da8d642017-03-17 15:16:21 -04001999 SkRect actualDst;
2000 if (!src || bitmapBounds.contains(*src)) {
2001 actualDst = dst;
2002 } else {
2003 if (!srcBounds.intersect(bitmapBounds)) {
2004 return;
2005 }
2006 matrix.mapRect(&actualDst, srcBounds);
2007 }
Mike Reede25b4472019-04-02 17:49:12 -04002008 auto bitmapShader = SkMakeBitmapShaderForPaint(paint, bitmap, SkTileMode::kClamp,
2009 SkTileMode::kClamp, &matrix,
Michael Ludwigc47e81b2019-04-02 15:18:02 -04002010 kNever_SkCopyPixelsMode);
Hal Canaryb39b09e2017-02-01 17:04:44 -05002011 SkASSERT(bitmapShader);
2012 if (!bitmapShader) { return; }
2013 SkPaint paintWithShader(paint);
2014 paintWithShader.setStyle(SkPaint::kFill_Style);
2015 paintWithShader.setShader(std::move(bitmapShader));
Hal Canary7da8d642017-03-17 15:16:21 -04002016 this->drawRect(actualDst, paintWithShader);
Hal Canaryb39b09e2017-02-01 17:04:44 -05002017}
Mike Klein8f11d4d2018-01-24 12:42:55 -05002018#endif//defined(SK_BUILD_FOR_WIN)