blob: 554c58c5340eb2d79f009636a160ca75a69a1a37 [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"
43#include "src/core/SkMaskFilterBase.h"
44#include "src/core/SkRasterClip.h"
45#include "src/core/SkStrikeCache.h"
46#include "src/core/SkTLazy.h"
47#include "src/core/SkTypefacePriv.h"
48#include "src/core/SkUtils.h"
49#include "src/sfnt/SkSFNTHeader.h"
50#include "src/sfnt/SkTTCFHeader.h"
51#include "src/shaders/SkShaderBase.h"
52#include "src/utils/win/SkHRESULT.h"
53#include "src/utils/win/SkIStream.h"
54#include "src/utils/win/SkTScopedComPtr.h"
55#include "src/xps/SkXPSDevice.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000056
57//Windows defines a FLOAT type,
58//make it clear when converting a scalar that this is what is wanted.
59#define SkScalarToFLOAT(n) SkScalarToFloat(n)
60
Ben Wagnerda5a1b82014-08-22 15:07:06 -040061//Dummy representation of a GUID from createId.
bungeman@google.comb29c8832011-10-10 13:19:10 +000062#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
halcanary96fcdcc2015-08-27 07:41:13 -070063//Length of GUID representation from createId, including nullptr terminator.
bungeman@google.comb29c8832011-10-10 13:19:10 +000064#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
65
66/**
67 Formats a GUID and places it into buffer.
68 buffer should have space for at least GUID_ID_LEN wide characters.
69 The string will always be wchar null terminated.
70 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
71 @return -1 if there was an error, > 0 if success.
72 */
73static int format_guid(const GUID& guid,
74 wchar_t* buffer, size_t bufferSize,
75 wchar_t sep = '-') {
76 SkASSERT(bufferSize >= GUID_ID_LEN);
77 return swprintf_s(buffer,
78 bufferSize,
79 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
80 guid.Data1,
81 sep,
82 guid.Data2,
83 sep,
84 guid.Data3,
85 sep,
86 guid.Data4[0],
87 guid.Data4[1],
88 sep,
89 guid.Data4[2],
90 guid.Data4[3],
91 guid.Data4[4],
92 guid.Data4[5],
93 guid.Data4[6],
94 guid.Data4[7]);
95}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000096
Ben Wagnerda5a1b82014-08-22 15:07:06 -040097HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000098 GUID guid = {};
Ben Wagnerda5a1b82014-08-22 15:07:06 -040099#ifdef SK_XPS_USE_DETERMINISTIC_IDS
100 guid.Data1 = fNextId++;
101 // The following make this a valid Type4 UUID.
102 guid.Data3 = 0x4000;
103 guid.Data4[0] = 0x80;
104#else
bungeman@google.comb29c8832011-10-10 13:19:10 +0000105 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400106#endif
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000107
bungeman@google.comb29c8832011-10-10 13:19:10 +0000108 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
109 HRM(E_UNEXPECTED, "Could not format GUID into id.");
110 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000111
bungeman@google.comb29c8832011-10-10 13:19:10 +0000112 return S_OK;
113}
114
Hal Canaryb39b09e2017-02-01 17:04:44 -0500115SkXPSDevice::SkXPSDevice(SkISize s)
116 : INHERITED(SkImageInfo::MakeUnknown(s.width(), s.height()),
117 SkSurfaceProps(0, kUnknown_SkPixelGeometry))
Hal Canaryabc88d22017-02-06 09:26:49 -0500118 , fCurrentPage(0) {}
bungeman@google.comb29c8832011-10-10 13:19:10 +0000119
Hal Canaryabc88d22017-02-06 09:26:49 -0500120SkXPSDevice::~SkXPSDevice() {}
bungeman@google.comb29c8832011-10-10 13:19:10 +0000121
Hal Canaryabc88d22017-02-06 09:26:49 -0500122bool SkXPSDevice::beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory* factory) {
123 SkASSERT(factory);
124 fXpsFactory.reset(SkRefComPtr(factory));
125 HRB(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000126 return true;
127}
128
129bool SkXPSDevice::beginSheet(
130 const SkVector& unitsPerMeter,
131 const SkVector& pixelsPerMeter,
132 const SkSize& trimSize,
133 const SkRect* mediaBox,
134 const SkRect* bleedBox,
135 const SkRect* artBox,
136 const SkRect* cropBox) {
137 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000138
bungeman@google.comb29c8832011-10-10 13:19:10 +0000139 //For simplicity, just write everything out in geometry units,
140 //then have a base canvas do the scale to physical units.
141 this->fCurrentCanvasSize = trimSize;
142 this->fCurrentUnitsPerMeter = unitsPerMeter;
143 this->fCurrentPixelsPerMeter = pixelsPerMeter;
Hal Canaryabc88d22017-02-06 09:26:49 -0500144 return this->createCanvasForLayer();
145}
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000146
Hal Canaryabc88d22017-02-06 09:26:49 -0500147bool SkXPSDevice::createCanvasForLayer() {
148 SkASSERT(fXpsFactory);
149 fCurrentXpsCanvas.reset();
150 HRB(fXpsFactory->CreateCanvas(&fCurrentXpsCanvas));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000151 return true;
152}
153
halcanarybfa92752016-05-31 11:23:42 -0700154template <typename T> static constexpr size_t sk_digits_in() {
155 return static_cast<size_t>(std::numeric_limits<T>::digits10 + 1);
156}
157
bungeman@google.comb29c8832011-10-10 13:19:10 +0000158HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
159 const unsigned int pageNum,
160 IXpsOMImageResource** image) {
161 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
162 HRM(CoCreateInstance(
163 CLSID_XpsOMThumbnailGenerator,
halcanary96fcdcc2015-08-27 07:41:13 -0700164 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000165 CLSCTX_INPROC_SERVER,
166 IID_PPV_ARGS(&thumbnailGenerator)),
167 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000168
bungeman@google.comb29c8832011-10-10 13:19:10 +0000169 SkTScopedComPtr<IOpcPartUri> partUri;
halcanarybfa92752016-05-31 11:23:42 -0700170 constexpr size_t size = SkTMax(
171 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + sk_digits_in<decltype(pageNum)>(),
172 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png"));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000173 wchar_t buffer[size];
174 if (pageNum > 0) {
175 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
176 } else {
177 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400178 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000179 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
180 }
181 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
182 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
bungeman@google.comb29c8832011-10-10 13:19:10 +0000184 HRM(thumbnailGenerator->GenerateThumbnail(page,
185 XPS_IMAGE_TYPE_PNG,
186 XPS_THUMBNAIL_SIZE_LARGE,
187 partUri.get(),
188 image),
189 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000190
bungeman@google.comb29c8832011-10-10 13:19:10 +0000191 return S_OK;
192}
193
194HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
195 IXpsOMPage** page) {
halcanarybfa92752016-05-31 11:23:42 -0700196 constexpr size_t size =
197 SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
198 + sk_digits_in<decltype(fCurrentPage)>();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000199 wchar_t buffer[size];
200 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
201 this->fCurrentPage);
202 SkTScopedComPtr<IOpcPartUri> partUri;
203 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
204 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000205
bungeman@google.comb29c8832011-10-10 13:19:10 +0000206 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
207 HRM(this->fXpsFactory->CreatePage(&pageSize,
208 L"und",
209 partUri.get(),
210 page),
211 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000212
bungeman@google.comb29c8832011-10-10 13:19:10 +0000213 return S_OK;
214}
215
216HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
217 //Create package writer.
218 {
219 SkTScopedComPtr<IOpcPartUri> partUri;
220 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
221 &partUri),
222 "Could not create document sequence part uri.");
223 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
224 this->fOutputStream.get(),
225 TRUE,
226 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
227 partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700228 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000229 image,
halcanary96fcdcc2015-08-27 07:41:13 -0700230 nullptr,
231 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000232 &this->fPackageWriter),
233 "Could not create package writer.");
234 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000235
bungeman@google.comb29c8832011-10-10 13:19:10 +0000236 //Begin the lone document.
237 {
238 SkTScopedComPtr<IOpcPartUri> partUri;
239 HRM(this->fXpsFactory->CreatePartUri(
240 L"/Documents/1/FixedDocument.fdoc",
241 &partUri),
242 "Could not create fixed document part uri.");
243 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700244 nullptr,
245 nullptr,
246 nullptr,
247 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000248 "Could not start document.");
249 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000250
bungeman@google.comb29c8832011-10-10 13:19:10 +0000251 return S_OK;
252}
253
254bool SkXPSDevice::endSheet() {
255 //XPS is fixed at 96dpi (XPS Spec 11.1).
256 static const float xpsDPI = 96.0f;
257 static const float inchesPerMeter = 10000.0f / 254.0f;
258 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
259 const float scaleX = targetUnitsPerMeter
260 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
261 const float scaleY = targetUnitsPerMeter
262 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000263
bungeman@google.comb29c8832011-10-10 13:19:10 +0000264 //Create the scale canvas.
265 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
266 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
267 "Could not create scale canvas.");
268 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
269 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
270 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000271
bungeman@google.comb29c8832011-10-10 13:19:10 +0000272 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
273 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
274 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
275 "Could not create geometry to physical transform.");
276 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
277 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000278
bungeman@google.comb29c8832011-10-10 13:19:10 +0000279 //Add the content canvas to the scale canvas.
280 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
281 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000282
bungeman@google.comb29c8832011-10-10 13:19:10 +0000283 //Create the page.
284 XPS_SIZE pageSize = {
285 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
286 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
287 };
288 SkTScopedComPtr<IXpsOMPage> page;
289 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000290
bungeman@google.comb29c8832011-10-10 13:19:10 +0000291 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
292 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000293
bungeman@google.comb29c8832011-10-10 13:19:10 +0000294 //Add the scale canvas to the page.
295 HRBM(pageVisuals->Append(scaleCanvas.get()),
296 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000297
bungeman@google.comb29c8832011-10-10 13:19:10 +0000298 //Create the package writer if it hasn't been created yet.
halcanary96fcdcc2015-08-27 07:41:13 -0700299 if (nullptr == this->fPackageWriter.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000300 SkTScopedComPtr<IXpsOMImageResource> image;
301 //Ignore return, thumbnail is completely optional.
302 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000303
bungeman@google.comb29c8832011-10-10 13:19:10 +0000304 HRB(this->initXpsDocumentWriter(image.get()));
305 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000306
bungeman@google.comb29c8832011-10-10 13:19:10 +0000307 HRBM(this->fPackageWriter->AddPage(page.get(),
308 &pageSize,
halcanary96fcdcc2015-08-27 07:41:13 -0700309 nullptr,
310 nullptr,
311 nullptr,
312 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000313 "Could not write the page.");
314 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000315
bungeman@google.comb29c8832011-10-10 13:19:10 +0000316 return true;
317}
318
Ben Wagner11eae3d2019-08-22 17:40:34 -0400319static HRESULT subset_typeface(const SkXPSDevice::TypefaceUse& current) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000320 //CreateFontPackage wants unsigned short.
321 //Microsoft, Y U NO stdint.h?
Hal Canary9e41c212018-09-03 12:00:23 -0400322 std::vector<unsigned short> keepList;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400323 current.glyphsUsed.getSetValues([&keepList](unsigned v) {
Hal Canary52514d52018-10-19 10:08:42 -0400324 keepList.push_back((unsigned short)v);
325 });
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000326
Ben Wagner11eae3d2019-08-22 17:40:34 -0400327 int ttcCount = (current.ttcIndex + 1);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000328
bungeman@google.comb29c8832011-10-10 13:19:10 +0000329 //The following are declared with the types required by CreateFontPackage.
halcanary96fcdcc2015-08-27 07:41:13 -0700330 unsigned char *fontPackageBufferRaw = nullptr;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000331 unsigned long fontPackageBufferSize;
332 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000333 unsigned long result = CreateFontPackage(
Ben Wagner11eae3d2019-08-22 17:40:34 -0400334 (unsigned char *) current.fontData->getMemoryBase(),
335 (unsigned long) current.fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000336 &fontPackageBufferRaw,
337 &fontPackageBufferSize,
338 &bytesWritten,
339 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
Ben Wagner11eae3d2019-08-22 17:40:34 -0400340 current.ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000341 TTFCFP_SUBSET,
342 0,
343 0,
344 0,
Hal Canary9e41c212018-09-03 12:00:23 -0400345 keepList.data(),
346 SkTo<unsigned short>(keepList.size()),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000347 sk_malloc_throw,
348 sk_realloc_throw,
349 sk_free,
halcanary96fcdcc2015-08-27 07:41:13 -0700350 nullptr);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000351 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000352 if (result != NO_ERROR) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -0400353 SkDEBUGF("CreateFontPackage Error %lu", result);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000354 return E_UNEXPECTED;
355 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000356
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000357 // If it was originally a ttc, keep it a ttc.
358 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
359 size_t extra;
360 if (ttcCount > 0) {
361 // Create space for a ttc header.
362 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
363 fontPackageBuffer.realloc(bytesWritten + extra);
364 //overlap is certain, use memmove
365 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
366
367 // Write the ttc header.
368 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
369 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
370 ttcfHeader->version = SkTTCFHeader::version_1;
371 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
372 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
373 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
bsalomon0aa5cea2014-12-15 09:13:35 -0800374 *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000375 }
376
377 // Fix up offsets in sfnt table entries.
378 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
379 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
380 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
381 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
382 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
383 tableDirectory->offset = SkEndian_SwapBE32(
bsalomon0aa5cea2014-12-15 09:13:35 -0800384 SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000385 }
386 } else {
387 extra = 0;
388 fontPackageBuffer.realloc(bytesWritten);
389 }
390
Ben Wagner145dbcd2016-11-03 14:40:50 -0400391 std::unique_ptr<SkMemoryStream> newStream(new SkMemoryStream());
mtklein18300a32016-03-16 13:53:35 -0700392 newStream->setMemoryOwned(fontPackageBuffer.release(), bytesWritten + extra);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000393
bungeman@google.comb29c8832011-10-10 13:19:10 +0000394 SkTScopedComPtr<IStream> newIStream;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400395 SkIStream::CreateFromSkStream(std::move(newStream), &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000396
bungeman@google.comb29c8832011-10-10 13:19:10 +0000397 XPS_FONT_EMBEDDING embedding;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400398 HRM(current.xpsFont->GetEmbeddingOption(&embedding),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000399 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000400
bungeman@google.comb29c8832011-10-10 13:19:10 +0000401 SkTScopedComPtr<IOpcPartUri> partUri;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400402 HRM(current.xpsFont->GetPartName(&partUri),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000403 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000404
Ben Wagner11eae3d2019-08-22 17:40:34 -0400405 HRM(current.xpsFont->SetContent(
bungeman@google.comb29c8832011-10-10 13:19:10 +0000406 newIStream.get(),
407 embedding,
408 partUri.get()),
409 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000410
bungeman@google.comb29c8832011-10-10 13:19:10 +0000411 return S_OK;
412}
413
414bool SkXPSDevice::endPortfolio() {
415 //Subset fonts
Ben Wagner11eae3d2019-08-22 17:40:34 -0400416 for (const TypefaceUse& current : this->fTypefaces) {
417 //Ignore return for now, if it didn't subset, let it be.
418 subset_typeface(current);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000419 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000420
Ben Wagner11eae3d2019-08-22 17:40:34 -0400421 if (this->fPackageWriter) {
422 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
423 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000424
bungeman@google.comb29c8832011-10-10 13:19:10 +0000425 return true;
426}
427
428static XPS_COLOR xps_color(const SkColor skColor) {
429 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
430 XPS_COLOR xpsColor;
431 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
432 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
433 xpsColor.value.sRGB.red = SkColorGetR(skColor);
434 xpsColor.value.sRGB.green = SkColorGetG(skColor);
435 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000436
bungeman@google.comb29c8832011-10-10 13:19:10 +0000437 return xpsColor;
438}
439
440static XPS_POINT xps_point(const SkPoint& point) {
441 XPS_POINT xpsPoint = {
442 SkScalarToFLOAT(point.fX),
443 SkScalarToFLOAT(point.fY),
444 };
445 return xpsPoint;
446}
447
448static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
449 SkPoint skTransformedPoint;
450 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
451 return xps_point(skTransformedPoint);
452}
453
Mike Reed5c5de212019-04-03 16:51:47 -0400454static XPS_SPREAD_METHOD xps_spread_method(SkTileMode tileMode) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000455 switch (tileMode) {
Mike Reed5c5de212019-04-03 16:51:47 -0400456 case SkTileMode::kClamp:
bungeman@google.comb29c8832011-10-10 13:19:10 +0000457 return XPS_SPREAD_METHOD_PAD;
Mike Reed5c5de212019-04-03 16:51:47 -0400458 case SkTileMode::kRepeat:
bungeman@google.comb29c8832011-10-10 13:19:10 +0000459 return XPS_SPREAD_METHOD_REPEAT;
Mike Reed5c5de212019-04-03 16:51:47 -0400460 case SkTileMode::kMirror:
bungeman@google.comb29c8832011-10-10 13:19:10 +0000461 return XPS_SPREAD_METHOD_REFLECT;
Ben Wagner11eae3d2019-08-22 17:40:34 -0400462 case SkTileMode::kDecal:
463 // TODO: fake
464 return XPS_SPREAD_METHOD_PAD;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000465 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000466 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000467 }
468 return XPS_SPREAD_METHOD_PAD;
469}
470
471static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
472 const SkPoint& start, const SkPoint& end,
473 const SkMatrix& transform) {
474 SkPoint startTransformed;
475 transform.mapXY(start.fX, start.fY, &startTransformed);
476 SkPoint endTransformed;
477 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000478
bungeman@google.comb29c8832011-10-10 13:19:10 +0000479 //Manhattan distance between transformed start and end.
480 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
481 + (endTransformed.fY - startTransformed.fY);
482 if (SkScalarNearlyZero(startToEnd)) {
483 for (int i = 0; i < numOffsets; ++i) {
484 stopOffsets[i] = 0;
485 }
486 return;
487 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000488
bungeman@google.comb29c8832011-10-10 13:19:10 +0000489 for (int i = 0; i < numOffsets; ++i) {
490 SkPoint stop;
Mike Reed8be952a2017-02-13 20:44:33 -0500491 stop.fX = (end.fX - start.fX) * stopOffsets[i];
492 stop.fY = (end.fY - start.fY) * stopOffsets[i];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000493
bungeman@google.comb29c8832011-10-10 13:19:10 +0000494 SkPoint stopTransformed;
495 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000496
bungeman@google.comb29c8832011-10-10 13:19:10 +0000497 //Manhattan distance between transformed start and stop.
498 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
499 + (stopTransformed.fY - startTransformed.fY);
500 //Percentage along transformed line.
reed80ea19c2015-05-12 10:37:34 -0700501 stopOffsets[i] = startToStop / startToEnd;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000502 }
503}
504
505HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
506 IXpsOMMatrixTransform** xpsTransform) {
507 SkScalar affine[6];
508 if (!matrix.asAffine(affine)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700509 *xpsTransform = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000510 return S_FALSE;
511 }
512 XPS_MATRIX rawXpsMatrix = {
Mike Reedf0cb7a02017-10-13 13:26:00 +0000513 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
514 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
515 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
516 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
517 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
518 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000519 };
520 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
521 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000522
bungeman@google.comb29c8832011-10-10 13:19:10 +0000523 return S_OK;
524}
525
526HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
527 IXpsOMVisualCollection* visuals,
528 IXpsOMPath** path) {
529 SkTScopedComPtr<IXpsOMGeometry> geometry;
530 HRM(this->fXpsFactory->CreateGeometry(&geometry),
531 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000532
bungeman@google.comb29c8832011-10-10 13:19:10 +0000533 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
534 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
535 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000536
bungeman@google.comb29c8832011-10-10 13:19:10 +0000537 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
538 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000539
bungeman@google.comb29c8832011-10-10 13:19:10 +0000540 HRM(visuals->Append(*path), "Could not add path to visuals.");
541 return S_OK;
542}
543
544HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
545 const SkAlpha alpha,
546 IXpsOMBrush** xpsBrush) {
547 XPS_COLOR xpsColor = xps_color(skColor);
548 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
halcanary96fcdcc2015-08-27 07:41:13 -0700549 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000550 "Could not create solid color brush.");
551 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
552 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
553 return S_OK;
554}
555
556HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
557 const XPS_RECT& imageViewBox,
558 IXpsOMImageResource* image,
559 IXpsOMVisualCollection* visuals) {
560 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
561 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000562
bungeman@google.comb29c8832011-10-10 13:19:10 +0000563 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
564 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000565
bungeman@google.comb29c8832011-10-10 13:19:10 +0000566 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
567 HRM(this->fXpsFactory->CreateImageBrush(image,
568 &imageViewBox,
569 &imageViewBox,
570 &areaToFillBrush),
571 "Could not create brush for side of clamp.");
572 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
573 "Could not set tile mode for side of clamp.");
574 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
575 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000576
bungeman@google.comb29c8832011-10-10 13:19:10 +0000577 return S_OK;
578}
579
580HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
581 const SkColor color,
582 IXpsOMVisualCollection* visuals) {
583 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
584 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000585
bungeman@google.comb29c8832011-10-10 13:19:10 +0000586 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
587 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000588
bungeman@google.comb29c8832011-10-10 13:19:10 +0000589 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
590 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
591 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
592 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000593
bungeman@google.comb29c8832011-10-10 13:19:10 +0000594 return S_OK;
595}
596
597static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
598static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
599static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
600static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
601static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
602
603//TODO(bungeman): In the future, should skia add None,
604//handle None+Mirror and None+Repeat correctly.
605//None is currently an internal hack so masks don't repeat (None+None only).
Mike Reed5c5de212019-04-03 16:51:47 -0400606static XPS_TILE_MODE gSkToXpsTileMode[kSkTileModeCount+1]
607 [kSkTileModeCount+1] = {
halcanarya634b742016-10-13 08:44:11 -0700608 //Clamp //Repeat //Mirror //None
609 /*Clamp */ {XTM_N, XTM_T, XTM_Y, XTM_N},
610 /*Repeat*/ {XTM_T, XTM_T, XTM_Y, XTM_N},
611 /*Mirror*/ {XTM_X, XTM_X, XTM_XY, XTM_X},
612 /*None */ {XTM_N, XTM_N, XTM_Y, XTM_N},
bungeman@google.comb29c8832011-10-10 13:19:10 +0000613};
614
Mike Reed5c5de212019-04-03 16:51:47 -0400615static XPS_TILE_MODE SkToXpsTileMode(SkTileMode tmx, SkTileMode tmy) {
616 return gSkToXpsTileMode[(unsigned)tmx][(unsigned)tmy];
617}
618
bungeman@google.comb29c8832011-10-10 13:19:10 +0000619HRESULT SkXPSDevice::createXpsImageBrush(
620 const SkBitmap& bitmap,
621 const SkMatrix& localMatrix,
Mike Reed5c5de212019-04-03 16:51:47 -0400622 const SkTileMode (&xy)[2],
bungeman@google.comb29c8832011-10-10 13:19:10 +0000623 const SkAlpha alpha,
624 IXpsOMTileBrush** xpsBrush) {
625 SkDynamicMemoryWStream write;
Hal Canarydb683012016-11-23 08:55:18 -0700626 if (!SkEncodeImage(&write, bitmap, SkEncodedImageFormat::kPNG, 100)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000627 HRM(E_FAIL, "Unable to encode bitmap as png.");
628 }
Ben Wagner11eae3d2019-08-22 17:40:34 -0400629 SkTScopedComPtr<IStream> read;
630 HRM(SkIStream::CreateFromSkStream(write.detachAsStream(), &read),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000631 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000632
bungeman@google.comb29c8832011-10-10 13:19:10 +0000633 const size_t size =
634 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
635 wchar_t buffer[size];
636 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400637 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000638 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000639
bungeman@google.comb29c8832011-10-10 13:19:10 +0000640 SkTScopedComPtr<IOpcPartUri> imagePartUri;
641 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
642 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000643
bungeman@google.comb29c8832011-10-10 13:19:10 +0000644 SkTScopedComPtr<IXpsOMImageResource> imageResource;
645 HRM(this->fXpsFactory->CreateImageResource(
Ben Wagner11eae3d2019-08-22 17:40:34 -0400646 read.get(),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000647 XPS_IMAGE_TYPE_PNG,
648 imagePartUri.get(),
649 &imageResource),
650 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000651
bungeman@google.comb29c8832011-10-10 13:19:10 +0000652 XPS_RECT bitmapRect = {
653 0.0, 0.0,
654 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
655 };
656 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
657 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
658 &bitmapRect, &bitmapRect,
659 &xpsImageBrush),
660 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000661
Mike Reed5c5de212019-04-03 16:51:47 -0400662 if (SkTileMode::kClamp != xy[0] &&
663 SkTileMode::kClamp != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000664
Mike Reed5c5de212019-04-03 16:51:47 -0400665 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode(xy[0], xy[1])),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000666 "Could not set image tile mode");
667 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
668 "Could not set image opacity.");
669 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
670 } else {
671 //TODO(bungeman): compute how big this really needs to be.
672 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
673 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
674 const SkScalar bWidth = SkIntToScalar(bitmap.width());
675 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000676
bungeman@google.comb29c8832011-10-10 13:19:10 +0000677 //create brush canvas
678 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
679 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
680 "Could not create image brush canvas.");
681 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
682 HRM(brushCanvas->GetVisuals(&brushVisuals),
683 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000684
bungeman@google.comb29c8832011-10-10 13:19:10 +0000685 //create central figure
686 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
687 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
688 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000689
bungeman@google.comb29c8832011-10-10 13:19:10 +0000690 SkTScopedComPtr<IXpsOMPath> centralPath;
691 HR(this->createPath(centralFigure.get(),
692 brushVisuals.get(),
693 &centralPath));
694 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
695 "Could not set tile mode for image brush central path.");
696 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
697 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000698
bungeman@google.comb29c8832011-10-10 13:19:10 +0000699 //add left/right
Mike Reed5c5de212019-04-03 16:51:47 -0400700 if (SkTileMode::kClamp == xy[0]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000701 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
702 XPS_RECT leftImageViewBox = {
703 0.0, 0.0,
704 1.0, static_cast<FLOAT>(bitmap.height()),
705 };
706 HR(this->sideOfClamp(leftArea, leftImageViewBox,
707 imageResource.get(),
708 brushVisuals.get()));
709
710 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
711 XPS_RECT rightImageViewBox = {
712 bitmap.width() - 1.0f, 0.0f,
713 1.0f, static_cast<FLOAT>(bitmap.height()),
714 };
715 HR(this->sideOfClamp(rightArea, rightImageViewBox,
716 imageResource.get(),
717 brushVisuals.get()));
718 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000719
bungeman@google.comb29c8832011-10-10 13:19:10 +0000720 //add top/bottom
Mike Reed5c5de212019-04-03 16:51:47 -0400721 if (SkTileMode::kClamp == xy[1]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000722 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
723 XPS_RECT topImageViewBox = {
724 0.0, 0.0,
725 static_cast<FLOAT>(bitmap.width()), 1.0,
726 };
727 HR(this->sideOfClamp(topArea, topImageViewBox,
728 imageResource.get(),
729 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000730
bungeman@google.comb29c8832011-10-10 13:19:10 +0000731 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
732 XPS_RECT bottomImageViewBox = {
733 0.0f, bitmap.height() - 1.0f,
734 static_cast<FLOAT>(bitmap.width()), 1.0f,
735 };
736 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
737 imageResource.get(),
738 brushVisuals.get()));
739 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000740
bungeman@google.comb29c8832011-10-10 13:19:10 +0000741 //add tl, tr, bl, br
Mike Reed5c5de212019-04-03 16:51:47 -0400742 if (SkTileMode::kClamp == xy[0] &&
743 SkTileMode::kClamp == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000744
bungeman@google.comb29c8832011-10-10 13:19:10 +0000745 const SkColor tlColor = bitmap.getColor(0,0);
746 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
747 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000748
bungeman@google.comb29c8832011-10-10 13:19:10 +0000749 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
750 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
751 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000752
bungeman@google.comb29c8832011-10-10 13:19:10 +0000753 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
754 bitmap.height()-1);
755 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
756 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000757
bungeman@google.comb29c8832011-10-10 13:19:10 +0000758 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
759 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
760 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
761 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000762
bungeman@google.comb29c8832011-10-10 13:19:10 +0000763 //create visual brush from canvas
764 XPS_RECT bound = {};
Mike Reed5c5de212019-04-03 16:51:47 -0400765 if (SkTileMode::kClamp == xy[0] &&
766 SkTileMode::kClamp == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000767
bungeman@google.comb29c8832011-10-10 13:19:10 +0000768 bound.x = BIG_F / -2;
769 bound.y = BIG_F / -2;
770 bound.width = BIG_F;
771 bound.height = BIG_F;
Mike Reed5c5de212019-04-03 16:51:47 -0400772 } else if (SkTileMode::kClamp == xy[0]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000773 bound.x = BIG_F / -2;
774 bound.y = 0.0f;
775 bound.width = BIG_F;
776 bound.height = static_cast<FLOAT>(bitmap.height());
Mike Reed5c5de212019-04-03 16:51:47 -0400777 } else if (SkTileMode::kClamp == xy[1]) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000778 bound.x = 0;
779 bound.y = BIG_F / -2;
780 bound.width = static_cast<FLOAT>(bitmap.width());
781 bound.height = BIG_F;
782 }
783 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
784 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
785 "Could not create visual brush for image brush.");
786 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
787 "Could not set canvas on visual brush for image brush.");
Mike Reed5c5de212019-04-03 16:51:47 -0400788 HRM(clampBrush->SetTileMode(SkToXpsTileMode(xy[0], xy[1])),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000789 "Could not set tile mode on visual brush for image brush.");
790 HRM(clampBrush->SetOpacity(alpha / 255.0f),
791 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000792
bungeman@google.comb29c8832011-10-10 13:19:10 +0000793 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
794 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000795
bungeman@google.comb29c8832011-10-10 13:19:10 +0000796 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
797 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
bsalomon49f085d2014-09-05 13:34:00 -0700798 if (xpsMatrixToUse.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000799 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
800 "Could not set transform for image brush.");
801 } else {
802 //TODO(bungeman): perspective bitmaps in general.
803 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000804
bungeman@google.comb29c8832011-10-10 13:19:10 +0000805 return S_OK;
806}
807
808HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
809 const SkScalar offset,
810 IXpsOMGradientStop** xpsGradStop) {
811 XPS_COLOR gradStopXpsColor = xps_color(skColor);
812 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
halcanary96fcdcc2015-08-27 07:41:13 -0700813 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000814 SkScalarToFLOAT(offset),
815 xpsGradStop),
816 "Could not create gradient stop.");
817 return S_OK;
818}
819
820HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
821 const SkAlpha alpha,
822 const SkMatrix& localMatrix,
823 IXpsOMMatrixTransform* xpsMatrix,
824 IXpsOMBrush** xpsBrush) {
825 XPS_POINT startPoint;
826 XPS_POINT endPoint;
bsalomon49f085d2014-09-05 13:34:00 -0700827 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000828 startPoint = xps_point(info.fPoint[0]);
829 endPoint = xps_point(info.fPoint[1]);
830 } else {
831 transform_offsets(info.fColorOffsets, info.fColorCount,
832 info.fPoint[0], info.fPoint[1],
833 localMatrix);
834 startPoint = xps_point(info.fPoint[0], localMatrix);
835 endPoint = xps_point(info.fPoint[1], localMatrix);
836 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000837
bungeman@google.comb29c8832011-10-10 13:19:10 +0000838 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
839 HR(createXpsGradientStop(info.fColors[0],
840 info.fColorOffsets[0],
841 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000842
bungeman@google.comb29c8832011-10-10 13:19:10 +0000843 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
844 HR(createXpsGradientStop(info.fColors[1],
845 info.fColorOffsets[1],
846 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000847
bungeman@google.comb29c8832011-10-10 13:19:10 +0000848 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
849 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
850 gradStop1.get(),
851 &startPoint,
852 &endPoint,
853 &gradientBrush),
854 "Could not create linear gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700855 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000856 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
857 "Could not set transform on linear gradient brush.");
858 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000859
bungeman@google.comb29c8832011-10-10 13:19:10 +0000860 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
861 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
862 "Could not get linear gradient stop collection.");
863 for (int i = 2; i < info.fColorCount; ++i) {
864 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
865 HR(createXpsGradientStop(info.fColors[i],
866 info.fColorOffsets[i],
867 &gradStop));
868 HRM(gradStopCollection->Append(gradStop.get()),
869 "Could not add linear gradient stop.");
870 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000871
Mike Reed5c5de212019-04-03 16:51:47 -0400872 HRM(gradientBrush->SetSpreadMethod(xps_spread_method((SkTileMode)info.fTileMode)),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000873 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000874
bungeman@google.comb29c8832011-10-10 13:19:10 +0000875 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
876 "Could not set opacity of linear gradient brush.");
877 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000878
bungeman@google.comb29c8832011-10-10 13:19:10 +0000879 return S_OK;
880}
881
882HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
883 const SkAlpha alpha,
884 const SkMatrix& localMatrix,
885 IXpsOMMatrixTransform* xpsMatrix,
886 IXpsOMBrush** xpsBrush) {
887 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
888 HR(createXpsGradientStop(info.fColors[0],
889 info.fColorOffsets[0],
890 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000891
bungeman@google.comb29c8832011-10-10 13:19:10 +0000892 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
893 HR(createXpsGradientStop(info.fColors[1],
894 info.fColorOffsets[1],
895 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000896
bungeman@google.comb29c8832011-10-10 13:19:10 +0000897 //TODO: figure out how to fake better if not affine
898 XPS_POINT centerPoint;
899 XPS_POINT gradientOrigin;
900 XPS_SIZE radiiSizes;
bsalomon49f085d2014-09-05 13:34:00 -0700901 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000902 centerPoint = xps_point(info.fPoint[0]);
903 gradientOrigin = xps_point(info.fPoint[0]);
904 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
905 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
906 } else {
907 centerPoint = xps_point(info.fPoint[0], localMatrix);
908 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000909
bungeman@google.comb29c8832011-10-10 13:19:10 +0000910 SkScalar radius = info.fRadius[0];
911 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000912
bungeman@google.comb29c8832011-10-10 13:19:10 +0000913 vec[0].set(radius, 0);
914 vec[1].set(0, radius);
915 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000916
bungeman@google.comb29c8832011-10-10 13:19:10 +0000917 SkScalar d0 = vec[0].length();
918 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000919
bungeman@google.comb29c8832011-10-10 13:19:10 +0000920 radiiSizes.width = SkScalarToFLOAT(d0);
921 radiiSizes.height = SkScalarToFLOAT(d1);
922 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000923
bungeman@google.comb29c8832011-10-10 13:19:10 +0000924 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
925 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
926 gradStop1.get(),
927 &centerPoint,
928 &gradientOrigin,
929 &radiiSizes,
930 &gradientBrush),
931 "Could not create radial gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700932 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000933 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
934 "Could not set transform on radial gradient brush.");
935 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000936
bungeman@google.comb29c8832011-10-10 13:19:10 +0000937 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
938 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
939 "Could not get radial gradient stop collection.");
940 for (int i = 2; i < info.fColorCount; ++i) {
941 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
942 HR(createXpsGradientStop(info.fColors[i],
943 info.fColorOffsets[i],
944 &gradStop));
945 HRM(gradStopCollection->Append(gradStop.get()),
946 "Could not add radial gradient stop.");
947 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000948
Mike Reed5c5de212019-04-03 16:51:47 -0400949 HRM(gradientBrush->SetSpreadMethod(xps_spread_method((SkTileMode)info.fTileMode)),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000950 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000951
bungeman@google.comb29c8832011-10-10 13:19:10 +0000952 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
953 "Could not set opacity of radial gradient brush.");
954 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000955
bungeman@google.comb29c8832011-10-10 13:19:10 +0000956 return S_OK;
957}
958
959HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
960 IXpsOMBrush** brush,
961 const SkMatrix* parentTransform) {
962 const SkShader *shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -0700963 if (nullptr == shader) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000964 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
965 return S_OK;
966 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000967
bungeman@google.comb29c8832011-10-10 13:19:10 +0000968 //Gradient shaders.
969 SkShader::GradientInfo info;
970 info.fColorCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700971 info.fColors = nullptr;
972 info.fColorOffsets = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000973 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000974
bungeman@google.comb29c8832011-10-10 13:19:10 +0000975 if (SkShader::kNone_GradientType == gradientType) {
976 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000977
bungeman@google.comb29c8832011-10-10 13:19:10 +0000978 } else if (SkShader::kColor_GradientType == gradientType) {
979 SkASSERT(1 == info.fColorCount);
980 SkColor color;
981 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +0000982 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000983 SkAlpha alpha = skPaint.getAlpha();
984 HR(this->createXpsSolidColorBrush(color, alpha, brush));
985 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000986
bungeman@google.comb29c8832011-10-10 13:19:10 +0000987 } else {
988 if (info.fColorCount == 0) {
989 const SkColor color = skPaint.getColor();
990 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
991 return S_OK;
992 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000993
bungeman@google.comb29c8832011-10-10 13:19:10 +0000994 SkAutoTArray<SkColor> colors(info.fColorCount);
995 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
996 info.fColors = colors.get();
997 info.fColorOffsets = colorOffsets.get();
998 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000999
bungeman@google.comb29c8832011-10-10 13:19:10 +00001000 if (1 == info.fColorCount) {
1001 SkColor color = info.fColors[0];
1002 SkAlpha alpha = skPaint.getAlpha();
1003 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1004 return S_OK;
1005 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001006
Mike Reed7656b2c2019-04-08 11:48:20 -04001007 SkMatrix localMatrix = as_SB(shader)->getLocalMatrix();
bsalomon49f085d2014-09-05 13:34:00 -07001008 if (parentTransform) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001009 localMatrix.preConcat(*parentTransform);
1010 }
1011 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1012 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001013
bungeman@google.comb29c8832011-10-10 13:19:10 +00001014 if (SkShader::kLinear_GradientType == gradientType) {
1015 HR(this->createXpsLinearGradient(info,
1016 skPaint.getAlpha(),
1017 localMatrix,
1018 xpsMatrixToUse.get(),
1019 brush));
1020 return S_OK;
1021 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001022
bungeman@google.comb29c8832011-10-10 13:19:10 +00001023 if (SkShader::kRadial_GradientType == gradientType) {
1024 HR(this->createXpsRadialGradient(info,
1025 skPaint.getAlpha(),
1026 localMatrix,
1027 xpsMatrixToUse.get(),
1028 brush));
1029 return S_OK;
1030 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001031
reed71a6cbf2015-05-04 08:32:51 -07001032 if (SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001033 //simple if affine and one is 0, otherwise will have to fake
1034 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001035
bungeman@google.comb29c8832011-10-10 13:19:10 +00001036 if (SkShader::kSweep_GradientType == gradientType) {
1037 //have to fake
1038 }
1039 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001040
bungeman@google.comb29c8832011-10-10 13:19:10 +00001041 SkBitmap outTexture;
1042 SkMatrix outMatrix;
Mike Reed5c5de212019-04-03 16:51:47 -04001043 SkTileMode xy[2];
Mike Reed627778d2016-09-28 17:13:38 -04001044 SkImage* image = shader->isAImage(&outMatrix, xy);
Cary Clark4f5a79c2018-02-07 15:51:00 -05001045 if (image && image->asLegacyBitmap(&outTexture)) {
reedf5822822015-08-19 11:46:38 -07001046 //TODO: outMatrix??
Mike Reed7656b2c2019-04-08 11:48:20 -04001047 SkMatrix localMatrix = as_SB(shader)->getLocalMatrix();
reedf5822822015-08-19 11:46:38 -07001048 if (parentTransform) {
Hal Canarybc212432017-03-17 11:48:59 -04001049 localMatrix.postConcat(*parentTransform);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001050 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001051
reedf5822822015-08-19 11:46:38 -07001052 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1053 HR(this->createXpsImageBrush(outTexture,
1054 localMatrix,
1055 xy,
1056 skPaint.getAlpha(),
1057 &tileBrush));
1058
1059 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
1060 } else {
1061 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1062 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001063 return S_OK;
1064}
1065
1066static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1067 const bool zeroWidth = (0 == paint.getStrokeWidth());
1068 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001069
bungeman@google.comb29c8832011-10-10 13:19:10 +00001070 return paint.getPathEffect() ||
1071 paint.getMaskFilter() ||
bungeman@google.comb29c8832011-10-10 13:19:10 +00001072 (stroke && (
1073 (matrix.hasPerspective() && !zeroWidth) ||
1074 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1075 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1076 paint.getStrokeMiter() < SK_ScalarSqrt2)
1077 ))
1078 ;
1079}
1080
1081HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1082 IXpsOMGeometryFigure** xpsRect) {
1083 const SkPoint points[4] = {
1084 { rect.fLeft, rect.fTop },
1085 { rect.fRight, rect.fTop },
1086 { rect.fRight, rect.fBottom },
1087 { rect.fLeft, rect.fBottom },
1088 };
1089 return this->createXpsQuad(points, stroke, fill, xpsRect);
1090}
1091HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1092 BOOL stroke, BOOL fill,
1093 IXpsOMGeometryFigure** xpsQuad) {
1094 // Define the start point.
1095 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001096
bungeman@google.comb29c8832011-10-10 13:19:10 +00001097 // Create the figure.
1098 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1099 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001100
bungeman@google.comb29c8832011-10-10 13:19:10 +00001101 // Define the type of each segment.
1102 XPS_SEGMENT_TYPE segmentTypes[3] = {
1103 XPS_SEGMENT_TYPE_LINE,
1104 XPS_SEGMENT_TYPE_LINE,
1105 XPS_SEGMENT_TYPE_LINE,
1106 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001107
bungeman@google.comb29c8832011-10-10 13:19:10 +00001108 // Define the x and y coordinates of each corner of the figure.
1109 FLOAT segmentData[6] = {
1110 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1111 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1112 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1113 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001114
bungeman@google.comb29c8832011-10-10 13:19:10 +00001115 // Describe if the segments are stroked.
1116 BOOL segmentStrokes[3] = {
1117 stroke, stroke, stroke,
1118 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001119
bungeman@google.comb29c8832011-10-10 13:19:10 +00001120 // Add the segment data to the figure.
1121 HRM((*xpsQuad)->SetSegments(
1122 3, 6,
1123 segmentTypes , segmentData, segmentStrokes),
1124 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001125
bungeman@google.comb29c8832011-10-10 13:19:10 +00001126 // Set the closed and filled properties of the figure.
1127 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1128 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001129
bungeman@google.comb29c8832011-10-10 13:19:10 +00001130 return S_OK;
1131}
1132
Mike Reeda1361362017-03-07 09:37:29 -05001133void SkXPSDevice::drawPoints(SkCanvas::PointMode mode,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001134 size_t count, const SkPoint points[],
1135 const SkPaint& paint) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001136 //TODO
bungeman@google.comb29c8832011-10-10 13:19:10 +00001137}
1138
Ruiqi Maoc97a3392018-08-15 10:44:19 -04001139void SkXPSDevice::drawVertices(const SkVertices* v, const SkVertices::Bone bones[], int boneCount,
Ruiqi Maof5101492018-06-29 14:32:21 -04001140 SkBlendMode blendMode, const SkPaint& paint) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001141 //TODO
bungeman@google.comb29c8832011-10-10 13:19:10 +00001142}
1143
Mike Reeda1361362017-03-07 09:37:29 -05001144void SkXPSDevice::drawPaint(const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001145 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001146
bungeman@google.comb29c8832011-10-10 13:19:10 +00001147 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001148 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1149 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1150 if (paint->getStyle() != SkPaint::kFill_Style) {
1151 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001152 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001153
Mike Reeda1361362017-03-07 09:37:29 -05001154 this->internalDrawRect(r, false, *fillPaint);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001155}
1156
Mike Reeda1361362017-03-07 09:37:29 -05001157void SkXPSDevice::drawRect(const SkRect& r,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001158 const SkPaint& paint) {
Mike Reeda1361362017-03-07 09:37:29 -05001159 this->internalDrawRect(r, true, paint);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001160}
1161
Mike Reeda1361362017-03-07 09:37:29 -05001162void SkXPSDevice::drawRRect(const SkRRect& rr,
scroggo@google.comcac8d012013-11-12 17:10:02 +00001163 const SkPaint& paint) {
1164 SkPath path;
1165 path.addRRect(rr);
Robert Phillips137ca522018-08-15 10:14:33 -04001166 this->drawPath(path, paint, true);
scroggo@google.comcac8d012013-11-12 17:10:02 +00001167}
1168
Mike Reeda1361362017-03-07 09:37:29 -05001169static SkIRect size(const SkBaseDevice& dev) { return {0, 0, dev.width(), dev.height()}; }
1170
1171void SkXPSDevice::internalDrawRect(const SkRect& r,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001172 bool transformRect,
1173 const SkPaint& paint) {
1174 //Exit early if there is nothing to draw.
Mike Reeda1361362017-03-07 09:37:29 -05001175 if (this->cs().isEmpty(size(*this)) ||
reed374772b2016-10-05 17:33:02 -07001176 (paint.getAlpha() == 0 && paint.isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001177 return;
1178 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001179
bungeman@google.comb29c8832011-10-10 13:19:10 +00001180 //Path the rect if we can't optimize it.
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001181 if (rect_must_be_pathed(paint, this->localToDevice())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001182 SkPath tmp;
1183 tmp.addRect(r);
Mike Reed7d34dc72019-11-26 12:17:17 -05001184 tmp.setFillType(SkPathFillType::kWinding);
Robert Phillips137ca522018-08-15 10:14:33 -04001185 this->drawPath(tmp, paint, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001186 return;
1187 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001188
bungeman@google.comb29c8832011-10-10 13:19:10 +00001189 //Create the shaded path.
1190 SkTScopedComPtr<IXpsOMPath> shadedPath;
1191 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1192 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001193
bungeman@google.comb29c8832011-10-10 13:19:10 +00001194 //Create the shaded geometry.
1195 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1196 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1197 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001198
bungeman@google.comb29c8832011-10-10 13:19:10 +00001199 //Add the geometry to the shaded path.
1200 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1201 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001202
bungeman@google.comb29c8832011-10-10 13:19:10 +00001203 //Set the brushes.
1204 BOOL fill = FALSE;
1205 BOOL stroke = FALSE;
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001206 HRV(this->shadePath(shadedPath.get(), paint, this->localToDevice(), &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001207
bungeman@google.comb29c8832011-10-10 13:19:10 +00001208 bool xpsTransformsPath = true;
1209 //Transform the geometry.
1210 if (transformRect && xpsTransformsPath) {
1211 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001212 HRV(this->createXpsTransform(this->localToDevice(), &xpsTransform));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001213 if (xpsTransform.get()) {
1214 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1215 "Could not set transform for rect.");
1216 } else {
1217 xpsTransformsPath = false;
1218 }
1219 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001220
bungeman@google.comb29c8832011-10-10 13:19:10 +00001221 //Create the figure.
1222 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1223 {
1224 SkPoint points[4] = {
1225 { r.fLeft, r.fTop },
1226 { r.fLeft, r.fBottom },
1227 { r.fRight, r.fBottom },
1228 { r.fRight, r.fTop },
1229 };
1230 if (!xpsTransformsPath && transformRect) {
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001231 this->localToDevice().mapPoints(points, SK_ARRAY_COUNT(points));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001232 }
1233 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1234 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001235
bungeman@google.comb29c8832011-10-10 13:19:10 +00001236 //Get the figures of the shaded geometry.
1237 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1238 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1239 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001240
bungeman@google.comb29c8832011-10-10 13:19:10 +00001241 //Add the figure to the shaded geometry figures.
1242 HRVM(shadedFigures->Append(rectFigure.get()),
1243 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001244
Mike Reeda1361362017-03-07 09:37:29 -05001245 HRV(this->clip(shadedPath.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001246
bungeman@google.comb29c8832011-10-10 13:19:10 +00001247 //Add the shaded path to the current visuals.
1248 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1249 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1250 "Could not get current visuals for rect.");
1251 HRVM(currentVisuals->Append(shadedPath.get()),
1252 "Could not add rect to current visuals.");
1253}
1254
1255static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1256 const SkTDArray<BOOL>& segmentStrokes,
1257 const SkTDArray<FLOAT>& segmentData,
1258 BOOL stroke, BOOL fill,
1259 IXpsOMGeometryFigure* figure,
1260 IXpsOMGeometryFigureCollection* figures) {
1261 // Add the segment data to the figure.
1262 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1263 segmentTypes.begin() , segmentData.begin(),
1264 segmentStrokes.begin()),
1265 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001266
bungeman@google.comb29c8832011-10-10 13:19:10 +00001267 // Set the closed and filled properties of the figure.
1268 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1269 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001270
bungeman@google.comb29c8832011-10-10 13:19:10 +00001271 // Add the figure created above to this geometry.
1272 HRM(figures->Append(figure), "Could not add path to geometry.");
1273 return S_OK;
1274}
1275
1276HRESULT SkXPSDevice::addXpsPathGeometry(
1277 IXpsOMGeometryFigureCollection* xpsFigures,
1278 BOOL stroke, BOOL fill, const SkPath& path) {
1279 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1280 SkTDArray<BOOL> segmentStrokes;
1281 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001282
bungeman@google.comb29c8832011-10-10 13:19:10 +00001283 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1284 SkPath::Iter iter(path, true);
1285 SkPoint points[4];
1286 SkPath::Verb verb;
1287 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1288 switch (verb) {
1289 case SkPath::kMove_Verb: {
bsalomon49f085d2014-09-05 13:34:00 -07001290 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001291 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1292 stroke, fill,
1293 xpsFigure.get() , xpsFigures));
1294 xpsFigure.reset();
1295 segmentTypes.rewind();
1296 segmentStrokes.rewind();
1297 segmentData.rewind();
1298 }
1299 // Define the start point.
1300 XPS_POINT startPoint = xps_point(points[0]);
1301 // Create the figure.
1302 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1303 &xpsFigure),
1304 "Could not create path geometry figure.");
1305 break;
1306 }
1307 case SkPath::kLine_Verb:
1308 if (iter.isCloseLine()) break; //ignore the line, auto-closed
Mike Reedb5475792018-08-08 16:17:42 -04001309 segmentTypes.push_back(XPS_SEGMENT_TYPE_LINE);
1310 segmentStrokes.push_back(stroke);
1311 segmentData.push_back(SkScalarToFLOAT(points[1].fX));
1312 segmentData.push_back(SkScalarToFLOAT(points[1].fY));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001313 break;
1314 case SkPath::kQuad_Verb:
Mike Reedb5475792018-08-08 16:17:42 -04001315 segmentTypes.push_back(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1316 segmentStrokes.push_back(stroke);
1317 segmentData.push_back(SkScalarToFLOAT(points[1].fX));
1318 segmentData.push_back(SkScalarToFLOAT(points[1].fY));
1319 segmentData.push_back(SkScalarToFLOAT(points[2].fX));
1320 segmentData.push_back(SkScalarToFLOAT(points[2].fY));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001321 break;
1322 case SkPath::kCubic_Verb:
Mike Reedb5475792018-08-08 16:17:42 -04001323 segmentTypes.push_back(XPS_SEGMENT_TYPE_BEZIER);
1324 segmentStrokes.push_back(stroke);
1325 segmentData.push_back(SkScalarToFLOAT(points[1].fX));
1326 segmentData.push_back(SkScalarToFLOAT(points[1].fY));
1327 segmentData.push_back(SkScalarToFLOAT(points[2].fX));
1328 segmentData.push_back(SkScalarToFLOAT(points[2].fY));
1329 segmentData.push_back(SkScalarToFLOAT(points[3].fX));
1330 segmentData.push_back(SkScalarToFLOAT(points[3].fY));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001331 break;
halcanary47ef4d52015-03-03 09:13:09 -08001332 case SkPath::kConic_Verb: {
1333 const SkScalar tol = SK_Scalar1 / 4;
1334 SkAutoConicToQuads converter;
1335 const SkPoint* quads =
1336 converter.computeQuads(points, iter.conicWeight(), tol);
1337 for (int i = 0; i < converter.countQuads(); ++i) {
Mike Reedb5475792018-08-08 16:17:42 -04001338 segmentTypes.push_back(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1339 segmentStrokes.push_back(stroke);
1340 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 1].fX));
1341 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 1].fY));
1342 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 2].fX));
1343 segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 2].fY));
halcanary47ef4d52015-03-03 09:13:09 -08001344 }
1345 break;
1346 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001347 case SkPath::kClose_Verb:
1348 // we ignore these, and just get the whole segment from
1349 // the corresponding line/quad/cubic verbs
1350 break;
1351 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001352 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001353 break;
1354 }
1355 }
bsalomon49f085d2014-09-05 13:34:00 -07001356 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001357 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1358 stroke, fill,
1359 xpsFigure.get(), xpsFigures));
1360 }
1361 return S_OK;
1362}
1363
bungeman@google.comb29c8832011-10-10 13:19:10 +00001364void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1365 SkMatrix* matrix,
1366 SkVector* ppuScale,
1367 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001368 //This action is in unit space, but the ppm is specified in physical space.
reed80ea19c2015-05-12 10:37:34 -07001369 ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
1370 fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001371
bungeman@google.comb29c8832011-10-10 13:19:10 +00001372 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001373
bungeman@google.comb29c8832011-10-10 13:19:10 +00001374 const SkIRect& irect = clip;
Mike Reed8be952a2017-02-13 20:44:33 -05001375 SkRect clipRect = SkRect::MakeLTRB(SkIntToScalar(irect.fLeft) * ppuScale->fX,
1376 SkIntToScalar(irect.fTop) * ppuScale->fY,
1377 SkIntToScalar(irect.fRight) * ppuScale->fX,
1378 SkIntToScalar(irect.fBottom) * ppuScale->fY);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001379 clipRect.roundOut(clipIRect);
1380}
1381
Mike Reeda1361362017-03-07 09:37:29 -05001382HRESULT SkXPSDevice::applyMask(const SkMask& mask,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001383 const SkVector& ppuScale,
1384 IXpsOMPath* shadedPath) {
1385 //Get the geometry object.
1386 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1387 HRM(shadedPath->GetGeometry(&shadedGeometry),
1388 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001389
bungeman@google.comb29c8832011-10-10 13:19:10 +00001390 //Get the figures from the geometry.
1391 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1392 HRM(shadedGeometry->GetFigures(&shadedFigures),
1393 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001394
bungeman@google.comb29c8832011-10-10 13:19:10 +00001395 SkMatrix m;
1396 m.reset();
1397 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1398 SkIntToScalar(mask.fBounds.fTop));
1399 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001400
Mike Reed5c5de212019-04-03 16:51:47 -04001401 SkTileMode xy[2];
1402 xy[0] = (SkTileMode)3;
1403 xy[1] = (SkTileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001404
bungeman@google.comb29c8832011-10-10 13:19:10 +00001405 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001406 bm.installMaskPixels(mask);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001407
bungeman@google.comb29c8832011-10-10 13:19:10 +00001408 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1409 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1410 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1411 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001412
bungeman@google.comb29c8832011-10-10 13:19:10 +00001413 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1414 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1415 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1416 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1417 "Could not create mask shaded figure.");
1418 HRM(shadedFigures->Append(shadedFigure.get()),
1419 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001420
Mike Reeda1361362017-03-07 09:37:29 -05001421 HR(this->clip(shadedPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001422
bungeman@google.comb29c8832011-10-10 13:19:10 +00001423 //Add the path to the active visual collection.
1424 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1425 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1426 "Could not get mask current visuals.");
1427 HRM(currentVisuals->Append(shadedPath),
1428 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001429
bungeman@google.comb29c8832011-10-10 13:19:10 +00001430 return S_OK;
1431}
1432
1433HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1434 const SkPaint& shaderPaint,
1435 const SkMatrix& matrix,
1436 BOOL* fill, BOOL* stroke) {
1437 *fill = FALSE;
1438 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001439
bungeman@google.comb29c8832011-10-10 13:19:10 +00001440 const SkPaint::Style style = shaderPaint.getStyle();
1441 const bool hasFill = SkPaint::kFill_Style == style
1442 || SkPaint::kStrokeAndFill_Style == style;
1443 const bool hasStroke = SkPaint::kStroke_Style == style
1444 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001445
bungeman@google.comb29c8832011-10-10 13:19:10 +00001446 //TODO(bungeman): use dictionaries and lookups.
1447 if (hasFill) {
1448 *fill = TRUE;
1449 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1450 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1451 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1452 "Could not set fill for shaded path.");
1453 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001454
bungeman@google.comb29c8832011-10-10 13:19:10 +00001455 if (hasStroke) {
1456 *stroke = TRUE;
1457 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1458 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1459 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1460 "Could not set stroke brush for shaded path.");
1461 HRM(shadedPath->SetStrokeThickness(
1462 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1463 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001464
bungeman@google.comb29c8832011-10-10 13:19:10 +00001465 if (0 == shaderPaint.getStrokeWidth()) {
1466 //XPS hair width is a hack. (XPS Spec 11.6.12).
1467 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1468 HRM(shadedPath->GetStrokeDashes(&dashes),
1469 "Could not set dashes for shaded path.");
1470 XPS_DASH dash;
1471 dash.length = 1.0;
1472 dash.gap = 0.0;
1473 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1474 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1475 "Could not set dash offset for shaded path.");
1476 }
1477 }
1478 return S_OK;
1479}
1480
Mike Reeda1361362017-03-07 09:37:29 -05001481void SkXPSDevice::drawPath(const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001482 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001483 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001484 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1485
bungeman@google.comb29c8832011-10-10 13:19:10 +00001486 // nothing to draw
Mike Reeda1361362017-03-07 09:37:29 -05001487 if (this->cs().isEmpty(size(*this)) ||
reed374772b2016-10-05 17:33:02 -07001488 (paint->getAlpha() == 0 && paint->isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001489 return;
1490 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001491
bungeman@google.comb29c8832011-10-10 13:19:10 +00001492 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001493 const bool paintHasPathEffect = paint->getPathEffect()
1494 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001495
bungeman@google.comb29c8832011-10-10 13:19:10 +00001496 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001497 SkMatrix matrix = this->localToDevice();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001498 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001499
bungeman@google.comb29c8832011-10-10 13:19:10 +00001500 //Apply path effect [Skeletal-path -> Fillable-path].
1501 SkPath* fillablePath = skeletalPath;
1502 if (paintHasPathEffect) {
1503 if (!pathIsMutable) {
1504 fillablePath = &modifiedPath;
1505 pathIsMutable = true;
1506 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001507 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001508
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001509 SkPaint* writablePaint = paint.writable();
halcanary96fcdcc2015-08-27 07:41:13 -07001510 writablePaint->setPathEffect(nullptr);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001511 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001512 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001513 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001514 writablePaint->setStyle(SkPaint::kStroke_Style);
1515 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001516 }
1517 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001518
bungeman@google.comb29c8832011-10-10 13:19:10 +00001519 //Create the shaded path. This will be the path which is painted.
1520 SkTScopedComPtr<IXpsOMPath> shadedPath;
1521 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1522 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001523
bungeman@google.comb29c8832011-10-10 13:19:10 +00001524 //Create the geometry for the shaded path.
1525 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1526 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1527 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001528
bungeman@google.comb29c8832011-10-10 13:19:10 +00001529 //Add the geometry to the shaded path.
1530 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1531 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001532
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001533 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001534
1535 //Determine if we will draw or shade and mask.
Mike Reed8ad91a92018-01-19 19:09:32 -05001536 if (filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001537 if (paint->getStyle() != SkPaint::kFill_Style) {
1538 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001539 }
1540 }
1541
bungeman@google.comb29c8832011-10-10 13:19:10 +00001542 //Set the brushes.
1543 BOOL fill;
1544 BOOL stroke;
1545 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001546 *paint,
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001547 this->localToDevice(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001548 &fill,
1549 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001550
bungeman@google.comb29c8832011-10-10 13:19:10 +00001551 //Mask filter
1552 if (filter) {
1553 SkIRect clipIRect;
1554 SkVector ppuScale;
1555 this->convertToPpm(filter,
1556 &matrix,
1557 &ppuScale,
Mike Reeda1361362017-03-07 09:37:29 -05001558 this->cs().bounds(size(*this)).roundOut(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001559 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001560
bungeman@google.comb29c8832011-10-10 13:19:10 +00001561 //[Fillable-path -> Pixel-path]
1562 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1563 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001564
halcanary96fcdcc2015-08-27 07:41:13 -07001565 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001566
bsalomon055e1922016-05-06 07:22:58 -07001567 SkASSERT(SkPaint::kFill_Style == paint->getStyle() ||
1568 (SkPaint::kStroke_Style == paint->getStyle() && 0 == paint->getStrokeWidth()));
1569 SkStrokeRec::InitStyle style = (SkPaint::kFill_Style == paint->getStyle())
1570 ? SkStrokeRec::kFill_InitStyle
1571 : SkStrokeRec::kHairline_InitStyle;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001572 //[Pixel-path -> Mask]
1573 SkMask rasteredMask;
1574 if (SkDraw::DrawToMask(
1575 *pixelPath,
1576 &clipIRect,
1577 filter, //just to compute how much to draw.
1578 &matrix,
1579 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001580 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon055e1922016-05-06 07:22:58 -07001581 style)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001582
bungeman@google.comb29c8832011-10-10 13:19:10 +00001583 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1584 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001585
bungeman@google.comb29c8832011-10-10 13:19:10 +00001586 //[Mask -> Mask]
1587 SkMask filteredMask;
Mike Reed80747ef2018-01-23 15:29:32 -05001588 if (as_MFB(filter)->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001589 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001590 }
1591 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001592
bungeman@google.comb29c8832011-10-10 13:19:10 +00001593 //Draw mask.
Mike Reeda1361362017-03-07 09:37:29 -05001594 HRV(this->applyMask(*mask, ppuScale, shadedPath.get()));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001595 }
1596 return;
1597 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001598
bungeman@google.comb29c8832011-10-10 13:19:10 +00001599 //Get the figures from the shaded geometry.
1600 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1601 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1602 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001603
bungeman@google.comb29c8832011-10-10 13:19:10 +00001604 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001605
bungeman@google.comb29c8832011-10-10 13:19:10 +00001606 //Set the fill rule.
bungeman76db31a2014-08-25 07:31:53 -07001607 SkPath* xpsCompatiblePath = fillablePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001608 XPS_FILL_RULE xpsFillRule;
Mike Reed7d34dc72019-11-26 12:17:17 -05001609 switch (fillablePath->getNewFillType()) {
1610 case SkPathFillType::kWinding:
bungeman@google.comb29c8832011-10-10 13:19:10 +00001611 xpsFillRule = XPS_FILL_RULE_NONZERO;
1612 break;
Mike Reed7d34dc72019-11-26 12:17:17 -05001613 case SkPathFillType::kEvenOdd:
bungeman@google.comb29c8832011-10-10 13:19:10 +00001614 xpsFillRule = XPS_FILL_RULE_EVENODD;
1615 break;
Mike Reed7d34dc72019-11-26 12:17:17 -05001616 case SkPathFillType::kInverseWinding: {
bungeman76db31a2014-08-25 07:31:53 -07001617 //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
1618 if (!pathIsMutable) {
1619 xpsCompatiblePath = &modifiedPath;
1620 pathIsMutable = true;
1621 }
1622 if (!Simplify(*fillablePath, xpsCompatiblePath)) {
Hal Canary2b0e6cd2018-07-09 12:43:39 -04001623 SkDEBUGF("Could not simplify inverse winding path.");
bungeman76db31a2014-08-25 07:31:53 -07001624 return;
1625 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001626 }
Ben Wagner11eae3d2019-08-22 17:40:34 -04001627 // The xpsCompatiblePath is now inverse even odd, so fall through.
Mike Reed7d34dc72019-11-26 12:17:17 -05001628 case SkPathFillType::kInverseEvenOdd: {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001629 const SkRect universe = SkRect::MakeLTRB(
1630 0, 0,
1631 this->fCurrentCanvasSize.fWidth,
1632 this->fCurrentCanvasSize.fHeight);
1633 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1634 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1635 HRVM(shadedFigures->Append(addOneFigure.get()),
1636 "Could not add even-odd flip figure to shaded path.");
1637 xpsTransformsPath = false;
1638 xpsFillRule = XPS_FILL_RULE_EVENODD;
1639 break;
1640 }
1641 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001642 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001643 }
1644 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1645 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001646
bungeman@google.comb29c8832011-10-10 13:19:10 +00001647 //Create the XPS transform, if possible.
1648 if (xpsTransformsPath) {
1649 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1650 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001651
bungeman@google.comb29c8832011-10-10 13:19:10 +00001652 if (xpsTransform.get()) {
1653 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1654 "Could not set transform on shaded path.");
1655 } else {
1656 xpsTransformsPath = false;
1657 }
1658 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001659
bungeman76db31a2014-08-25 07:31:53 -07001660 SkPath* devicePath = xpsCompatiblePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001661 if (!xpsTransformsPath) {
1662 //[Fillable-path -> Device-path]
bungeman76db31a2014-08-25 07:31:53 -07001663 devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
1664 xpsCompatiblePath->transform(matrix, devicePath);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001665 }
1666 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1667 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001668
Mike Reeda1361362017-03-07 09:37:29 -05001669 HRV(this->clip(shadedPath.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001670
bungeman@google.comb29c8832011-10-10 13:19:10 +00001671 //Add the path to the active visual collection.
1672 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1673 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1674 "Could not get current visuals for shaded path.");
1675 HRVM(currentVisuals->Append(shadedPath.get()),
1676 "Could not add shaded path to current visuals.");
1677}
1678
Mike Reeda1361362017-03-07 09:37:29 -05001679HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001680 if (this->cs().isWideOpen()) {
1681 return S_OK;
1682 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001683 SkPath clipPath;
Mike Reeda1361362017-03-07 09:37:29 -05001684 // clipPath.addRect(this->cs().bounds(size(*this)));
1685 (void)this->cs().asPath(&clipPath);
Ben Wagner11eae3d2019-08-22 17:40:34 -04001686 // TODO: handle all the kinds of paths, like drawPath does
bungeman@google.comb29c8832011-10-10 13:19:10 +00001687 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1688}
1689HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1690 const SkPath& clipPath,
1691 XPS_FILL_RULE fillRule) {
1692 //Create the geometry.
1693 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1694 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1695 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001696
bungeman@google.comb29c8832011-10-10 13:19:10 +00001697 //Get the figure collection of the geometry.
1698 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1699 HRM(clipGeometry->GetFigures(&clipFigures),
1700 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001701
bungeman@google.comb29c8832011-10-10 13:19:10 +00001702 //Create the figures into the geometry.
1703 HR(this->addXpsPathGeometry(
1704 clipFigures.get(),
1705 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001706
bungeman@google.comb29c8832011-10-10 13:19:10 +00001707 HRM(clipGeometry->SetFillRule(fillRule),
1708 "Could not set fill rule.");
1709 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1710 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001711
bungeman@google.comb29c8832011-10-10 13:19:10 +00001712 return S_OK;
1713}
1714
Mike Reeda1361362017-03-07 09:37:29 -05001715void SkXPSDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001716 //TODO: override this for XPS
Hal Canary2b0e6cd2018-07-09 12:43:39 -04001717 SkDEBUGF("XPS drawSprite not yet implemented.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001718}
1719
Ben Wagner11eae3d2019-08-22 17:40:34 -04001720HRESULT SkXPSDevice::CreateTypefaceUse(const SkFont& font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001721 TypefaceUse** typefaceUse) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001722 SkAutoResolveDefaultTypeface typeface(font.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001723
bungeman@google.comb29c8832011-10-10 13:19:10 +00001724 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00001725 const SkFontID typefaceID = typeface->uniqueID();
Ben Wagner11eae3d2019-08-22 17:40:34 -04001726 for (TypefaceUse& current : this->fTypefaces) {
1727 if (current.typefaceId == typefaceID) {
1728 *typefaceUse = &current;
1729 return S_OK;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001730 }
1731 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001732
bungeman@google.comb29c8832011-10-10 13:19:10 +00001733 //TODO: create glyph only fonts
1734 //and let the host deal with what kind of font we're looking at.
1735 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001736
bungeman@google.comb29c8832011-10-10 13:19:10 +00001737 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001738 int ttcIndex;
Ben Wagnerff84d8a2019-02-26 15:39:41 -05001739 std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex);
Ben Wagner11eae3d2019-08-22 17:40:34 -04001740 if (!fontData) {
1741 return E_NOTIMPL;
1742 }
bungeman@google.com635091f2013-10-01 15:03:18 +00001743 //TODO: cannot handle FON fonts.
Ben Wagner11eae3d2019-08-22 17:40:34 -04001744 HRM(SkIStream::CreateFromSkStream(fontData->duplicate(), &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001745 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001746
bungeman@google.comb29c8832011-10-10 13:19:10 +00001747 const size_t size =
1748 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1749 wchar_t buffer[size];
1750 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -04001751 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001752 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001753
bungeman@google.comb29c8832011-10-10 13:19:10 +00001754 SkTScopedComPtr<IOpcPartUri> partUri;
1755 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1756 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001757
bungeman@google.comb29c8832011-10-10 13:19:10 +00001758 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1759 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1760 embedding,
1761 partUri.get(),
1762 FALSE,
1763 &xpsFontResource),
1764 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001765
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001766 //TODO: change openStream to return -1 for non-ttc, get rid of this.
1767 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1768 bool isTTC = (data &&
1769 fontData->getLength() >= sizeof(SkTTCFHeader) &&
1770 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1771
Ben Wagner11eae3d2019-08-22 17:40:34 -04001772 int glyphCount = typeface->countGlyphs();
1773
1774 TypefaceUse& newTypefaceUse = this->fTypefaces.emplace_back(
1775 typefaceID,
1776 isTTC ? ttcIndex : -1,
1777 std::move(fontData),
1778 std::move(xpsFontResource),
1779 glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001780
bungeman@google.comb29c8832011-10-10 13:19:10 +00001781 *typefaceUse = &newTypefaceUse;
1782 return S_OK;
1783}
1784
Mike Reeda1361362017-03-07 09:37:29 -05001785HRESULT SkXPSDevice::AddGlyphs(IXpsOMObjectFactory* xpsFactory,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001786 IXpsOMCanvas* canvas,
Ben Wagner11eae3d2019-08-22 17:40:34 -04001787 const TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001788 LPCWSTR text,
1789 XPS_GLYPH_INDEX* xpsGlyphs,
1790 UINT32 xpsGlyphsLen,
1791 XPS_POINT *origin,
1792 FLOAT fontSize,
1793 XPS_STYLE_SIMULATION sims,
1794 const SkMatrix& transform,
1795 const SkPaint& paint) {
1796 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
Ben Wagner11eae3d2019-08-22 17:40:34 -04001797 HRM(xpsFactory->CreateGlyphs(font->xpsFont.get(), &glyphs), "Could not create glyphs.");
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001798 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001799
bungeman@google.comb29c8832011-10-10 13:19:10 +00001800 //XPS uses affine transformations for everything...
1801 //...except positioning text.
1802 bool useCanvasForClip;
Ben Wagner11eae3d2019-08-22 17:40:34 -04001803 if (transform.isTranslate()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001804 origin->x += SkScalarToFLOAT(transform.getTranslateX());
1805 origin->y += SkScalarToFLOAT(transform.getTranslateY());
1806 useCanvasForClip = false;
1807 } else {
1808 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1809 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1810 if (xpsMatrixToUse.get()) {
1811 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1812 "Could not set transform matrix.");
1813 useCanvasForClip = true;
1814 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00001815 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001816 useCanvasForClip = false;
1817 }
1818 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001819
bungeman@google.comb29c8832011-10-10 13:19:10 +00001820 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1821 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001822
bsalomon49f085d2014-09-05 13:34:00 -07001823 if (text) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001824 HRM(glyphsEditor->SetUnicodeString(text),
1825 "Could not set unicode string.");
1826 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001827
bsalomon49f085d2014-09-05 13:34:00 -07001828 if (xpsGlyphs) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001829 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
1830 "Could not set glyphs.");
1831 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001832
bungeman@google.comb29c8832011-10-10 13:19:10 +00001833 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001834
bungeman@google.comb29c8832011-10-10 13:19:10 +00001835 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
1836 HR(this->createXpsBrush(
1837 paint,
1838 &xpsFillBrush,
halcanary96fcdcc2015-08-27 07:41:13 -07001839 useCanvasForClip ? nullptr : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001840
bungeman@google.comb29c8832011-10-10 13:19:10 +00001841 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
1842 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001843
bungeman@google.comb29c8832011-10-10 13:19:10 +00001844 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001845
bungeman@google.comb29c8832011-10-10 13:19:10 +00001846 HRM(glyphs->SetFontRenderingEmSize(fontSize),
1847 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001848
bungeman@google.comb29c8832011-10-10 13:19:10 +00001849 HRM(glyphs->SetStyleSimulations(sims),
1850 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001851
bungeman@google.comb29c8832011-10-10 13:19:10 +00001852 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
1853 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001854
bungeman@google.comb29c8832011-10-10 13:19:10 +00001855 if (!useCanvasForClip) {
Mike Reeda1361362017-03-07 09:37:29 -05001856 HR(this->clip(glyphs.get()));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001857 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
1858 } else {
1859 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
1860 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
1861 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001862
bungeman@google.comb29c8832011-10-10 13:19:10 +00001863 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
1864 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
1865 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001866
bungeman@google.comb29c8832011-10-10 13:19:10 +00001867 HRM(glyphCanvasVisuals->Append(glyphs.get()),
1868 "Could not add glyphs to page.");
Mike Reeda1361362017-03-07 09:37:29 -05001869 HR(this->clip(glyphCanvas.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001870
bungeman@google.comb29c8832011-10-10 13:19:10 +00001871 HRM(visuals->Append(glyphCanvas.get()),
1872 "Could not add glyph canvas to page.");
1873 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001874
bungeman@google.comb29c8832011-10-10 13:19:10 +00001875 return S_OK;
1876}
1877
bungeman@google.comb29c8832011-10-10 13:19:10 +00001878static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1879 const SkPaint::Style style = paint.getStyle();
1880 return matrix.hasPerspective()
1881 || SkPaint::kStroke_Style == style
1882 || SkPaint::kStrokeAndFill_Style == style
1883 || paint.getMaskFilter()
bungeman@google.comb29c8832011-10-10 13:19:10 +00001884 ;
1885}
1886
Ben Wagner11eae3d2019-08-22 17:40:34 -04001887void SkXPSDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
herbbda26432015-11-24 08:37:01 -08001888
Ben Wagner11eae3d2019-08-22 17:40:34 -04001889 const SkPaint& paint = glyphRunList.paint();
1890 for (const auto& run : glyphRunList) {
1891 const SkGlyphID* glyphIDs = run.glyphsIDs().data();
1892 size_t glyphCount = run.glyphsIDs().size();
1893 const SkFont& font = run.font();
herbbda26432015-11-24 08:37:01 -08001894
Ben Wagner11eae3d2019-08-22 17:40:34 -04001895 if (!glyphCount || !glyphIDs || font.getSize() <= 0) {
1896 continue;
herbbda26432015-11-24 08:37:01 -08001897 }
Ben Wagner11eae3d2019-08-22 17:40:34 -04001898
1899 TypefaceUse* typeface;
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001900 if (FAILED(CreateTypefaceUse(font, &typeface)) ||
1901 text_must_be_pathed(paint, this->localToDevice())) {
Ben Wagner11eae3d2019-08-22 17:40:34 -04001902 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,
Michael Ludwigc89d1b52019-10-18 11:32:56 -04001940 this->localToDevice(),
Ben Wagner11eae3d2019-08-22 17:40:34 -04001941 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)