blob: 37874337e02e5cd892795dbdf41bf19fc2df0a7c [file] [log] [blame]
bungeman@google.comb29c8832011-10-10 13:19:10 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
halcanary47ef4d52015-03-03 09:13:09 -08008#include "SkTypes.h"
mtklein1ee76512015-11-02 10:20:27 -08009#if defined(SK_BUILD_FOR_WIN32)
halcanary47ef4d52015-03-03 09:13:09 -080010
halcanary4dbbd042016-06-07 17:21:10 -070011#include "SkLeanWindows.h"
12
bungeman@google.comb29c8832011-10-10 13:19:10 +000013#ifndef UNICODE
14#define UNICODE
15#endif
16#ifndef _UNICODE
17#define _UNICODE
18#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +000019#include <ObjBase.h>
20#include <XpsObjectModel.h>
21#include <T2EmbApi.h>
22#include <FontSub.h>
halcanarybfa92752016-05-31 11:23:42 -070023#include <limits>
bungeman@google.comb29c8832011-10-10 13:19:10 +000024
25#include "SkColor.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000026#include "SkData.h"
27#include "SkDraw.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000028#include "SkEndian.h"
herbbda26432015-11-24 08:37:01 -080029#include "SkFindAndPlaceGlyph.h"
halcanary47ef4d52015-03-03 09:13:09 -080030#include "SkGeometry.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000031#include "SkGlyphCache.h"
32#include "SkHRESULT.h"
Mike Reed627778d2016-09-28 17:13:38 -040033#include "SkImage.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000034#include "SkImageEncoder.h"
35#include "SkIStream.h"
36#include "SkMaskFilter.h"
37#include "SkPaint.h"
reeda4393342016-03-18 11:22:57 -070038#include "SkPathEffect.h"
Ben Wagnerda5a1b82014-08-22 15:07:06 -040039#include "SkPathOps.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000040#include "SkPoint.h"
reed1e7f5e72016-04-27 07:49:17 -070041#include "SkRasterClip.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000042#include "SkRasterizer.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000043#include "SkSFNTHeader.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000044#include "SkShader.h"
45#include "SkSize.h"
46#include "SkStream.h"
47#include "SkTDArray.h"
48#include "SkTLazy.h"
49#include "SkTScopedComPtr.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000050#include "SkTTCFHeader.h"
reed@google.com398de9a2013-03-21 21:43:51 +000051#include "SkTypefacePriv.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000052#include "SkUtils.h"
53#include "SkXPSDevice.h"
54
55//Windows defines a FLOAT type,
56//make it clear when converting a scalar that this is what is wanted.
57#define SkScalarToFLOAT(n) SkScalarToFloat(n)
58
Ben Wagnerda5a1b82014-08-22 15:07:06 -040059//Dummy representation of a GUID from createId.
bungeman@google.comb29c8832011-10-10 13:19:10 +000060#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
halcanary96fcdcc2015-08-27 07:41:13 -070061//Length of GUID representation from createId, including nullptr terminator.
bungeman@google.comb29c8832011-10-10 13:19:10 +000062#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
63
64/**
65 Formats a GUID and places it into buffer.
66 buffer should have space for at least GUID_ID_LEN wide characters.
67 The string will always be wchar null terminated.
68 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
69 @return -1 if there was an error, > 0 if success.
70 */
71static int format_guid(const GUID& guid,
72 wchar_t* buffer, size_t bufferSize,
73 wchar_t sep = '-') {
74 SkASSERT(bufferSize >= GUID_ID_LEN);
75 return swprintf_s(buffer,
76 bufferSize,
77 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
78 guid.Data1,
79 sep,
80 guid.Data2,
81 sep,
82 guid.Data3,
83 sep,
84 guid.Data4[0],
85 guid.Data4[1],
86 sep,
87 guid.Data4[2],
88 guid.Data4[3],
89 guid.Data4[4],
90 guid.Data4[5],
91 guid.Data4[6],
92 guid.Data4[7]);
93}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000094
Ben Wagnerda5a1b82014-08-22 15:07:06 -040095HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000096 GUID guid = {};
Ben Wagnerda5a1b82014-08-22 15:07:06 -040097#ifdef SK_XPS_USE_DETERMINISTIC_IDS
98 guid.Data1 = fNextId++;
99 // The following make this a valid Type4 UUID.
100 guid.Data3 = 0x4000;
101 guid.Data4[0] = 0x80;
102#else
bungeman@google.comb29c8832011-10-10 13:19:10 +0000103 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400104#endif
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000105
bungeman@google.comb29c8832011-10-10 13:19:10 +0000106 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
107 HRM(E_UNEXPECTED, "Could not format GUID into id.");
108 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000109
bungeman@google.comb29c8832011-10-10 13:19:10 +0000110 return S_OK;
111}
112
113static SkBitmap make_fake_bitmap(int width, int height) {
114 SkBitmap bitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000115 bitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000116 return bitmap;
117}
118
reed@google.com900ecf22014-02-20 20:55:37 +0000119// TODO: should inherit from SkBaseDevice instead of SkBitmapDevice...
bungeman@google.comb29c8832011-10-10 13:19:10 +0000120SkXPSDevice::SkXPSDevice()
robertphillips9a53fd72015-06-22 09:46:59 -0700121 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
bungeman@google.comb29c8832011-10-10 13:19:10 +0000122 , fCurrentPage(0) {
123}
124
robertphillips9a53fd72015-06-22 09:46:59 -0700125SkXPSDevice::SkXPSDevice(IXpsOMObjectFactory* xpsFactory)
126 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
127 , fCurrentPage(0) {
128
129 HRVM(CoCreateInstance(
130 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700131 nullptr,
robertphillips9a53fd72015-06-22 09:46:59 -0700132 CLSCTX_INPROC_SERVER,
133 IID_PPV_ARGS(&this->fXpsFactory)),
134 "Could not create factory for layer.");
135
136 HRVM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
137 "Could not create canvas for layer.");
138}
139
bungeman@google.comb29c8832011-10-10 13:19:10 +0000140SkXPSDevice::~SkXPSDevice() {
141}
142
143SkXPSDevice::TypefaceUse::TypefaceUse()
144 : typefaceId(0xffffffff)
halcanary96fcdcc2015-08-27 07:41:13 -0700145 , fontData(nullptr)
146 , xpsFont(nullptr)
147 , glyphsUsed(nullptr) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000148}
149
150SkXPSDevice::TypefaceUse::~TypefaceUse() {
151 //xpsFont owns fontData ref
152 this->xpsFont->Release();
153 delete this->glyphsUsed;
154}
155
156bool SkXPSDevice::beginPortfolio(SkWStream* outputStream) {
157 if (!this->fAutoCo.succeeded()) return false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000158
bungeman@google.comb29c8832011-10-10 13:19:10 +0000159 //Create XPS Factory.
160 HRBM(CoCreateInstance(
161 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700162 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000163 CLSCTX_INPROC_SERVER,
164 IID_PPV_ARGS(&this->fXpsFactory)),
165 "Could not create XPS factory.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000166
bungeman@google.comb29c8832011-10-10 13:19:10 +0000167 HRBM(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream),
168 "Could not convert SkStream to IStream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000169
bungeman@google.comb29c8832011-10-10 13:19:10 +0000170 return true;
171}
172
173bool SkXPSDevice::beginSheet(
174 const SkVector& unitsPerMeter,
175 const SkVector& pixelsPerMeter,
176 const SkSize& trimSize,
177 const SkRect* mediaBox,
178 const SkRect* bleedBox,
179 const SkRect* artBox,
180 const SkRect* cropBox) {
181 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182
bungeman@google.comb29c8832011-10-10 13:19:10 +0000183 //For simplicity, just write everything out in geometry units,
184 //then have a base canvas do the scale to physical units.
185 this->fCurrentCanvasSize = trimSize;
186 this->fCurrentUnitsPerMeter = unitsPerMeter;
187 this->fCurrentPixelsPerMeter = pixelsPerMeter;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188
bungeman@google.comb29c8832011-10-10 13:19:10 +0000189 this->fCurrentXpsCanvas.reset();
190 HRBM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
191 "Could not create base canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000192
bungeman@google.comb29c8832011-10-10 13:19:10 +0000193 return true;
194}
195
halcanarybfa92752016-05-31 11:23:42 -0700196template <typename T> static constexpr size_t sk_digits_in() {
197 return static_cast<size_t>(std::numeric_limits<T>::digits10 + 1);
198}
199
bungeman@google.comb29c8832011-10-10 13:19:10 +0000200HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
201 const unsigned int pageNum,
202 IXpsOMImageResource** image) {
203 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
204 HRM(CoCreateInstance(
205 CLSID_XpsOMThumbnailGenerator,
halcanary96fcdcc2015-08-27 07:41:13 -0700206 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000207 CLSCTX_INPROC_SERVER,
208 IID_PPV_ARGS(&thumbnailGenerator)),
209 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000210
bungeman@google.comb29c8832011-10-10 13:19:10 +0000211 SkTScopedComPtr<IOpcPartUri> partUri;
halcanarybfa92752016-05-31 11:23:42 -0700212 constexpr size_t size = SkTMax(
213 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + sk_digits_in<decltype(pageNum)>(),
214 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png"));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000215 wchar_t buffer[size];
216 if (pageNum > 0) {
217 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
218 } else {
219 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400220 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000221 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
222 }
223 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
224 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000225
bungeman@google.comb29c8832011-10-10 13:19:10 +0000226 HRM(thumbnailGenerator->GenerateThumbnail(page,
227 XPS_IMAGE_TYPE_PNG,
228 XPS_THUMBNAIL_SIZE_LARGE,
229 partUri.get(),
230 image),
231 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000232
bungeman@google.comb29c8832011-10-10 13:19:10 +0000233 return S_OK;
234}
235
236HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
237 IXpsOMPage** page) {
halcanarybfa92752016-05-31 11:23:42 -0700238 constexpr size_t size =
239 SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
240 + sk_digits_in<decltype(fCurrentPage)>();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000241 wchar_t buffer[size];
242 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
243 this->fCurrentPage);
244 SkTScopedComPtr<IOpcPartUri> partUri;
245 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
246 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000247
bungeman@google.comb29c8832011-10-10 13:19:10 +0000248 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
249 HRM(this->fXpsFactory->CreatePage(&pageSize,
250 L"und",
251 partUri.get(),
252 page),
253 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000254
bungeman@google.comb29c8832011-10-10 13:19:10 +0000255 return S_OK;
256}
257
258HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
259 //Create package writer.
260 {
261 SkTScopedComPtr<IOpcPartUri> partUri;
262 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
263 &partUri),
264 "Could not create document sequence part uri.");
265 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
266 this->fOutputStream.get(),
267 TRUE,
268 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
269 partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700270 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000271 image,
halcanary96fcdcc2015-08-27 07:41:13 -0700272 nullptr,
273 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000274 &this->fPackageWriter),
275 "Could not create package writer.");
276 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000277
bungeman@google.comb29c8832011-10-10 13:19:10 +0000278 //Begin the lone document.
279 {
280 SkTScopedComPtr<IOpcPartUri> partUri;
281 HRM(this->fXpsFactory->CreatePartUri(
282 L"/Documents/1/FixedDocument.fdoc",
283 &partUri),
284 "Could not create fixed document part uri.");
285 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700286 nullptr,
287 nullptr,
288 nullptr,
289 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000290 "Could not start document.");
291 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000292
bungeman@google.comb29c8832011-10-10 13:19:10 +0000293 return S_OK;
294}
295
296bool SkXPSDevice::endSheet() {
297 //XPS is fixed at 96dpi (XPS Spec 11.1).
298 static const float xpsDPI = 96.0f;
299 static const float inchesPerMeter = 10000.0f / 254.0f;
300 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
301 const float scaleX = targetUnitsPerMeter
302 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
303 const float scaleY = targetUnitsPerMeter
304 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000305
bungeman@google.comb29c8832011-10-10 13:19:10 +0000306 //Create the scale canvas.
307 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
308 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
309 "Could not create scale canvas.");
310 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
311 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
312 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000313
bungeman@google.comb29c8832011-10-10 13:19:10 +0000314 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
315 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
316 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
317 "Could not create geometry to physical transform.");
318 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
319 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000320
bungeman@google.comb29c8832011-10-10 13:19:10 +0000321 //Add the content canvas to the scale canvas.
322 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
323 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000324
bungeman@google.comb29c8832011-10-10 13:19:10 +0000325 //Create the page.
326 XPS_SIZE pageSize = {
327 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
328 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
329 };
330 SkTScopedComPtr<IXpsOMPage> page;
331 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000332
bungeman@google.comb29c8832011-10-10 13:19:10 +0000333 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
334 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000335
bungeman@google.comb29c8832011-10-10 13:19:10 +0000336 //Add the scale canvas to the page.
337 HRBM(pageVisuals->Append(scaleCanvas.get()),
338 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000339
bungeman@google.comb29c8832011-10-10 13:19:10 +0000340 //Create the package writer if it hasn't been created yet.
halcanary96fcdcc2015-08-27 07:41:13 -0700341 if (nullptr == this->fPackageWriter.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000342 SkTScopedComPtr<IXpsOMImageResource> image;
343 //Ignore return, thumbnail is completely optional.
344 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000345
bungeman@google.comb29c8832011-10-10 13:19:10 +0000346 HRB(this->initXpsDocumentWriter(image.get()));
347 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000348
bungeman@google.comb29c8832011-10-10 13:19:10 +0000349 HRBM(this->fPackageWriter->AddPage(page.get(),
350 &pageSize,
halcanary96fcdcc2015-08-27 07:41:13 -0700351 nullptr,
352 nullptr,
353 nullptr,
354 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000355 "Could not write the page.");
356 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000357
bungeman@google.comb29c8832011-10-10 13:19:10 +0000358 return true;
359}
360
361static HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
362 //CreateFontPackage wants unsigned short.
363 //Microsoft, Y U NO stdint.h?
364 SkTDArray<unsigned short> keepList;
365 current->glyphsUsed->exportTo(&keepList);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000366
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000367 int ttcCount = (current->ttcIndex + 1);
368
bungeman@google.comb29c8832011-10-10 13:19:10 +0000369 //The following are declared with the types required by CreateFontPackage.
halcanary96fcdcc2015-08-27 07:41:13 -0700370 unsigned char *fontPackageBufferRaw = nullptr;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000371 unsigned long fontPackageBufferSize;
372 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000373 unsigned long result = CreateFontPackage(
374 (unsigned char *) current->fontData->getMemoryBase(),
375 (unsigned long) current->fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000376 &fontPackageBufferRaw,
377 &fontPackageBufferSize,
378 &bytesWritten,
379 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
380 current->ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000381 TTFCFP_SUBSET,
382 0,
383 0,
384 0,
385 keepList.begin(),
386 keepList.count(),
387 sk_malloc_throw,
388 sk_realloc_throw,
389 sk_free,
halcanary96fcdcc2015-08-27 07:41:13 -0700390 nullptr);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000391 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000392 if (result != NO_ERROR) {
393 SkDEBUGF(("CreateFontPackage Error %lu", result));
394 return E_UNEXPECTED;
395 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000396
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000397 // If it was originally a ttc, keep it a ttc.
398 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
399 size_t extra;
400 if (ttcCount > 0) {
401 // Create space for a ttc header.
402 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
403 fontPackageBuffer.realloc(bytesWritten + extra);
404 //overlap is certain, use memmove
405 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
406
407 // Write the ttc header.
408 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
409 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
410 ttcfHeader->version = SkTTCFHeader::version_1;
411 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
412 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
413 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
bsalomon0aa5cea2014-12-15 09:13:35 -0800414 *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000415 }
416
417 // Fix up offsets in sfnt table entries.
418 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
419 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
420 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
421 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
422 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
423 tableDirectory->offset = SkEndian_SwapBE32(
bsalomon0aa5cea2014-12-15 09:13:35 -0800424 SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000425 }
426 } else {
427 extra = 0;
428 fontPackageBuffer.realloc(bytesWritten);
429 }
430
Ben Wagner145dbcd2016-11-03 14:40:50 -0400431 std::unique_ptr<SkMemoryStream> newStream(new SkMemoryStream());
mtklein18300a32016-03-16 13:53:35 -0700432 newStream->setMemoryOwned(fontPackageBuffer.release(), bytesWritten + extra);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000433
bungeman@google.comb29c8832011-10-10 13:19:10 +0000434 SkTScopedComPtr<IStream> newIStream;
mtklein18300a32016-03-16 13:53:35 -0700435 SkIStream::CreateFromSkStream(newStream.release(), true, &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000436
bungeman@google.comb29c8832011-10-10 13:19:10 +0000437 XPS_FONT_EMBEDDING embedding;
438 HRM(current->xpsFont->GetEmbeddingOption(&embedding),
439 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000440
bungeman@google.comb29c8832011-10-10 13:19:10 +0000441 SkTScopedComPtr<IOpcPartUri> partUri;
442 HRM(current->xpsFont->GetPartName(&partUri),
443 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000444
bungeman@google.comb29c8832011-10-10 13:19:10 +0000445 HRM(current->xpsFont->SetContent(
446 newIStream.get(),
447 embedding,
448 partUri.get()),
449 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000450
bungeman@google.comb29c8832011-10-10 13:19:10 +0000451 return S_OK;
452}
453
454bool SkXPSDevice::endPortfolio() {
455 //Subset fonts
456 if (!this->fTypefaces.empty()) {
457 SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
458 const TypefaceUse* last = &this->fTypefaces.back();
459 for (; current <= last; ++current) {
460 //Ignore return for now, if it didn't subset, let it be.
461 subset_typeface(current);
462 }
463 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000464
bungeman@google.comb29c8832011-10-10 13:19:10 +0000465 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000466
bungeman@google.comb29c8832011-10-10 13:19:10 +0000467 return true;
468}
469
470static XPS_COLOR xps_color(const SkColor skColor) {
471 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
472 XPS_COLOR xpsColor;
473 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
474 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
475 xpsColor.value.sRGB.red = SkColorGetR(skColor);
476 xpsColor.value.sRGB.green = SkColorGetG(skColor);
477 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000478
bungeman@google.comb29c8832011-10-10 13:19:10 +0000479 return xpsColor;
480}
481
482static XPS_POINT xps_point(const SkPoint& point) {
483 XPS_POINT xpsPoint = {
484 SkScalarToFLOAT(point.fX),
485 SkScalarToFLOAT(point.fY),
486 };
487 return xpsPoint;
488}
489
490static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
491 SkPoint skTransformedPoint;
492 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
493 return xps_point(skTransformedPoint);
494}
495
496static XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
497 switch (tileMode) {
498 case SkShader::kClamp_TileMode:
499 return XPS_SPREAD_METHOD_PAD;
500 case SkShader::kRepeat_TileMode:
501 return XPS_SPREAD_METHOD_REPEAT;
502 case SkShader::kMirror_TileMode:
503 return XPS_SPREAD_METHOD_REFLECT;
504 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000505 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000506 }
507 return XPS_SPREAD_METHOD_PAD;
508}
509
510static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
511 const SkPoint& start, const SkPoint& end,
512 const SkMatrix& transform) {
513 SkPoint startTransformed;
514 transform.mapXY(start.fX, start.fY, &startTransformed);
515 SkPoint endTransformed;
516 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000517
bungeman@google.comb29c8832011-10-10 13:19:10 +0000518 //Manhattan distance between transformed start and end.
519 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
520 + (endTransformed.fY - startTransformed.fY);
521 if (SkScalarNearlyZero(startToEnd)) {
522 for (int i = 0; i < numOffsets; ++i) {
523 stopOffsets[i] = 0;
524 }
525 return;
526 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000527
bungeman@google.comb29c8832011-10-10 13:19:10 +0000528 for (int i = 0; i < numOffsets; ++i) {
529 SkPoint stop;
530 stop.fX = SkScalarMul(end.fX - start.fX, stopOffsets[i]);
531 stop.fY = SkScalarMul(end.fY - start.fY, stopOffsets[i]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000532
bungeman@google.comb29c8832011-10-10 13:19:10 +0000533 SkPoint stopTransformed;
534 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000535
bungeman@google.comb29c8832011-10-10 13:19:10 +0000536 //Manhattan distance between transformed start and stop.
537 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
538 + (stopTransformed.fY - startTransformed.fY);
539 //Percentage along transformed line.
reed80ea19c2015-05-12 10:37:34 -0700540 stopOffsets[i] = startToStop / startToEnd;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000541 }
542}
543
544HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
545 IXpsOMMatrixTransform** xpsTransform) {
546 SkScalar affine[6];
547 if (!matrix.asAffine(affine)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700548 *xpsTransform = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000549 return S_FALSE;
550 }
551 XPS_MATRIX rawXpsMatrix = {
552 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
553 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
554 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
555 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
556 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
557 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
558 };
559 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
560 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000561
bungeman@google.comb29c8832011-10-10 13:19:10 +0000562 return S_OK;
563}
564
565HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
566 IXpsOMVisualCollection* visuals,
567 IXpsOMPath** path) {
568 SkTScopedComPtr<IXpsOMGeometry> geometry;
569 HRM(this->fXpsFactory->CreateGeometry(&geometry),
570 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000571
bungeman@google.comb29c8832011-10-10 13:19:10 +0000572 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
573 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
574 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000575
bungeman@google.comb29c8832011-10-10 13:19:10 +0000576 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
577 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000578
bungeman@google.comb29c8832011-10-10 13:19:10 +0000579 HRM(visuals->Append(*path), "Could not add path to visuals.");
580 return S_OK;
581}
582
583HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
584 const SkAlpha alpha,
585 IXpsOMBrush** xpsBrush) {
586 XPS_COLOR xpsColor = xps_color(skColor);
587 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
halcanary96fcdcc2015-08-27 07:41:13 -0700588 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000589 "Could not create solid color brush.");
590 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
591 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
592 return S_OK;
593}
594
595HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
596 const XPS_RECT& imageViewBox,
597 IXpsOMImageResource* image,
598 IXpsOMVisualCollection* visuals) {
599 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
600 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000601
bungeman@google.comb29c8832011-10-10 13:19:10 +0000602 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
603 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000604
bungeman@google.comb29c8832011-10-10 13:19:10 +0000605 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
606 HRM(this->fXpsFactory->CreateImageBrush(image,
607 &imageViewBox,
608 &imageViewBox,
609 &areaToFillBrush),
610 "Could not create brush for side of clamp.");
611 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
612 "Could not set tile mode for side of clamp.");
613 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
614 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000615
bungeman@google.comb29c8832011-10-10 13:19:10 +0000616 return S_OK;
617}
618
619HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
620 const SkColor color,
621 IXpsOMVisualCollection* visuals) {
622 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
623 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000624
bungeman@google.comb29c8832011-10-10 13:19:10 +0000625 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
626 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000627
bungeman@google.comb29c8832011-10-10 13:19:10 +0000628 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
629 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
630 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
631 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000632
bungeman@google.comb29c8832011-10-10 13:19:10 +0000633 return S_OK;
634}
635
636static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
637static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
638static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
639static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
640static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
641
642//TODO(bungeman): In the future, should skia add None,
643//handle None+Mirror and None+Repeat correctly.
644//None is currently an internal hack so masks don't repeat (None+None only).
645static XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
646 [SkShader::kTileModeCount+1] = {
halcanarya634b742016-10-13 08:44:11 -0700647 //Clamp //Repeat //Mirror //None
648 /*Clamp */ {XTM_N, XTM_T, XTM_Y, XTM_N},
649 /*Repeat*/ {XTM_T, XTM_T, XTM_Y, XTM_N},
650 /*Mirror*/ {XTM_X, XTM_X, XTM_XY, XTM_X},
651 /*None */ {XTM_N, XTM_N, XTM_Y, XTM_N},
bungeman@google.comb29c8832011-10-10 13:19:10 +0000652};
653
654HRESULT SkXPSDevice::createXpsImageBrush(
655 const SkBitmap& bitmap,
656 const SkMatrix& localMatrix,
657 const SkShader::TileMode (&xy)[2],
658 const SkAlpha alpha,
659 IXpsOMTileBrush** xpsBrush) {
660 SkDynamicMemoryWStream write;
Hal Canarydb683012016-11-23 08:55:18 -0700661 if (!SkEncodeImage(&write, bitmap, SkEncodedImageFormat::kPNG, 100)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000662 HRM(E_FAIL, "Unable to encode bitmap as png.");
663 }
664 SkMemoryStream* read = new SkMemoryStream;
reed42943c82016-09-12 12:01:44 -0700665 read->setData(write.detachAsData());
bungeman@google.comb29c8832011-10-10 13:19:10 +0000666 SkTScopedComPtr<IStream> readWrapper;
667 HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
668 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000669
bungeman@google.comb29c8832011-10-10 13:19:10 +0000670 const size_t size =
671 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
672 wchar_t buffer[size];
673 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400674 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000675 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000676
bungeman@google.comb29c8832011-10-10 13:19:10 +0000677 SkTScopedComPtr<IOpcPartUri> imagePartUri;
678 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
679 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000680
bungeman@google.comb29c8832011-10-10 13:19:10 +0000681 SkTScopedComPtr<IXpsOMImageResource> imageResource;
682 HRM(this->fXpsFactory->CreateImageResource(
683 readWrapper.get(),
684 XPS_IMAGE_TYPE_PNG,
685 imagePartUri.get(),
686 &imageResource),
687 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000688
bungeman@google.comb29c8832011-10-10 13:19:10 +0000689 XPS_RECT bitmapRect = {
690 0.0, 0.0,
691 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
692 };
693 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
694 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
695 &bitmapRect, &bitmapRect,
696 &xpsImageBrush),
697 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000698
bungeman@google.comb29c8832011-10-10 13:19:10 +0000699 if (SkShader::kClamp_TileMode != xy[0] &&
700 SkShader::kClamp_TileMode != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000701
bungeman@google.comb29c8832011-10-10 13:19:10 +0000702 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
703 "Could not set image tile mode");
704 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
705 "Could not set image opacity.");
706 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
707 } else {
708 //TODO(bungeman): compute how big this really needs to be.
709 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
710 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
711 const SkScalar bWidth = SkIntToScalar(bitmap.width());
712 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000713
bungeman@google.comb29c8832011-10-10 13:19:10 +0000714 //create brush canvas
715 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
716 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
717 "Could not create image brush canvas.");
718 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
719 HRM(brushCanvas->GetVisuals(&brushVisuals),
720 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000721
bungeman@google.comb29c8832011-10-10 13:19:10 +0000722 //create central figure
723 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
724 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
725 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000726
bungeman@google.comb29c8832011-10-10 13:19:10 +0000727 SkTScopedComPtr<IXpsOMPath> centralPath;
728 HR(this->createPath(centralFigure.get(),
729 brushVisuals.get(),
730 &centralPath));
731 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
732 "Could not set tile mode for image brush central path.");
733 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
734 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000735
bungeman@google.comb29c8832011-10-10 13:19:10 +0000736 //add left/right
737 if (SkShader::kClamp_TileMode == xy[0]) {
738 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
739 XPS_RECT leftImageViewBox = {
740 0.0, 0.0,
741 1.0, static_cast<FLOAT>(bitmap.height()),
742 };
743 HR(this->sideOfClamp(leftArea, leftImageViewBox,
744 imageResource.get(),
745 brushVisuals.get()));
746
747 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
748 XPS_RECT rightImageViewBox = {
749 bitmap.width() - 1.0f, 0.0f,
750 1.0f, static_cast<FLOAT>(bitmap.height()),
751 };
752 HR(this->sideOfClamp(rightArea, rightImageViewBox,
753 imageResource.get(),
754 brushVisuals.get()));
755 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000756
bungeman@google.comb29c8832011-10-10 13:19:10 +0000757 //add top/bottom
758 if (SkShader::kClamp_TileMode == xy[1]) {
759 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
760 XPS_RECT topImageViewBox = {
761 0.0, 0.0,
762 static_cast<FLOAT>(bitmap.width()), 1.0,
763 };
764 HR(this->sideOfClamp(topArea, topImageViewBox,
765 imageResource.get(),
766 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000767
bungeman@google.comb29c8832011-10-10 13:19:10 +0000768 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
769 XPS_RECT bottomImageViewBox = {
770 0.0f, bitmap.height() - 1.0f,
771 static_cast<FLOAT>(bitmap.width()), 1.0f,
772 };
773 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
774 imageResource.get(),
775 brushVisuals.get()));
776 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000777
bungeman@google.comb29c8832011-10-10 13:19:10 +0000778 //add tl, tr, bl, br
779 if (SkShader::kClamp_TileMode == xy[0] &&
780 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000781
bungeman@google.comb29c8832011-10-10 13:19:10 +0000782 SkAutoLockPixels alp(bitmap);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000783
bungeman@google.comb29c8832011-10-10 13:19:10 +0000784 const SkColor tlColor = bitmap.getColor(0,0);
785 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
786 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000787
bungeman@google.comb29c8832011-10-10 13:19:10 +0000788 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
789 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
790 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000791
bungeman@google.comb29c8832011-10-10 13:19:10 +0000792 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
793 bitmap.height()-1);
794 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
795 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000796
bungeman@google.comb29c8832011-10-10 13:19:10 +0000797 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
798 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
799 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
800 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000801
bungeman@google.comb29c8832011-10-10 13:19:10 +0000802 //create visual brush from canvas
803 XPS_RECT bound = {};
804 if (SkShader::kClamp_TileMode == xy[0] &&
805 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000806
bungeman@google.comb29c8832011-10-10 13:19:10 +0000807 bound.x = BIG_F / -2;
808 bound.y = BIG_F / -2;
809 bound.width = BIG_F;
810 bound.height = BIG_F;
811 } else if (SkShader::kClamp_TileMode == xy[0]) {
812 bound.x = BIG_F / -2;
813 bound.y = 0.0f;
814 bound.width = BIG_F;
815 bound.height = static_cast<FLOAT>(bitmap.height());
816 } else if (SkShader::kClamp_TileMode == xy[1]) {
817 bound.x = 0;
818 bound.y = BIG_F / -2;
819 bound.width = static_cast<FLOAT>(bitmap.width());
820 bound.height = BIG_F;
821 }
822 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
823 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
824 "Could not create visual brush for image brush.");
825 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
826 "Could not set canvas on visual brush for image brush.");
827 HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
828 "Could not set tile mode on visual brush for image brush.");
829 HRM(clampBrush->SetOpacity(alpha / 255.0f),
830 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000831
bungeman@google.comb29c8832011-10-10 13:19:10 +0000832 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
833 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000834
bungeman@google.comb29c8832011-10-10 13:19:10 +0000835 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
836 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
bsalomon49f085d2014-09-05 13:34:00 -0700837 if (xpsMatrixToUse.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000838 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
839 "Could not set transform for image brush.");
840 } else {
841 //TODO(bungeman): perspective bitmaps in general.
842 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000843
bungeman@google.comb29c8832011-10-10 13:19:10 +0000844 return S_OK;
845}
846
847HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
848 const SkScalar offset,
849 IXpsOMGradientStop** xpsGradStop) {
850 XPS_COLOR gradStopXpsColor = xps_color(skColor);
851 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
halcanary96fcdcc2015-08-27 07:41:13 -0700852 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000853 SkScalarToFLOAT(offset),
854 xpsGradStop),
855 "Could not create gradient stop.");
856 return S_OK;
857}
858
859HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
860 const SkAlpha alpha,
861 const SkMatrix& localMatrix,
862 IXpsOMMatrixTransform* xpsMatrix,
863 IXpsOMBrush** xpsBrush) {
864 XPS_POINT startPoint;
865 XPS_POINT endPoint;
bsalomon49f085d2014-09-05 13:34:00 -0700866 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000867 startPoint = xps_point(info.fPoint[0]);
868 endPoint = xps_point(info.fPoint[1]);
869 } else {
870 transform_offsets(info.fColorOffsets, info.fColorCount,
871 info.fPoint[0], info.fPoint[1],
872 localMatrix);
873 startPoint = xps_point(info.fPoint[0], localMatrix);
874 endPoint = xps_point(info.fPoint[1], localMatrix);
875 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000876
bungeman@google.comb29c8832011-10-10 13:19:10 +0000877 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
878 HR(createXpsGradientStop(info.fColors[0],
879 info.fColorOffsets[0],
880 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000881
bungeman@google.comb29c8832011-10-10 13:19:10 +0000882 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
883 HR(createXpsGradientStop(info.fColors[1],
884 info.fColorOffsets[1],
885 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000886
bungeman@google.comb29c8832011-10-10 13:19:10 +0000887 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
888 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
889 gradStop1.get(),
890 &startPoint,
891 &endPoint,
892 &gradientBrush),
893 "Could not create linear gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700894 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000895 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
896 "Could not set transform on linear gradient brush.");
897 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000898
bungeman@google.comb29c8832011-10-10 13:19:10 +0000899 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
900 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
901 "Could not get linear gradient stop collection.");
902 for (int i = 2; i < info.fColorCount; ++i) {
903 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
904 HR(createXpsGradientStop(info.fColors[i],
905 info.fColorOffsets[i],
906 &gradStop));
907 HRM(gradStopCollection->Append(gradStop.get()),
908 "Could not add linear gradient stop.");
909 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000910
bungeman@google.comb29c8832011-10-10 13:19:10 +0000911 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
912 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000913
bungeman@google.comb29c8832011-10-10 13:19:10 +0000914 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
915 "Could not set opacity of linear gradient brush.");
916 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000917
bungeman@google.comb29c8832011-10-10 13:19:10 +0000918 return S_OK;
919}
920
921HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
922 const SkAlpha alpha,
923 const SkMatrix& localMatrix,
924 IXpsOMMatrixTransform* xpsMatrix,
925 IXpsOMBrush** xpsBrush) {
926 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
927 HR(createXpsGradientStop(info.fColors[0],
928 info.fColorOffsets[0],
929 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000930
bungeman@google.comb29c8832011-10-10 13:19:10 +0000931 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
932 HR(createXpsGradientStop(info.fColors[1],
933 info.fColorOffsets[1],
934 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000935
bungeman@google.comb29c8832011-10-10 13:19:10 +0000936 //TODO: figure out how to fake better if not affine
937 XPS_POINT centerPoint;
938 XPS_POINT gradientOrigin;
939 XPS_SIZE radiiSizes;
bsalomon49f085d2014-09-05 13:34:00 -0700940 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000941 centerPoint = xps_point(info.fPoint[0]);
942 gradientOrigin = xps_point(info.fPoint[0]);
943 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
944 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
945 } else {
946 centerPoint = xps_point(info.fPoint[0], localMatrix);
947 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000948
bungeman@google.comb29c8832011-10-10 13:19:10 +0000949 SkScalar radius = info.fRadius[0];
950 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000951
bungeman@google.comb29c8832011-10-10 13:19:10 +0000952 vec[0].set(radius, 0);
953 vec[1].set(0, radius);
954 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000955
bungeman@google.comb29c8832011-10-10 13:19:10 +0000956 SkScalar d0 = vec[0].length();
957 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000958
bungeman@google.comb29c8832011-10-10 13:19:10 +0000959 radiiSizes.width = SkScalarToFLOAT(d0);
960 radiiSizes.height = SkScalarToFLOAT(d1);
961 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000962
bungeman@google.comb29c8832011-10-10 13:19:10 +0000963 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
964 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
965 gradStop1.get(),
966 &centerPoint,
967 &gradientOrigin,
968 &radiiSizes,
969 &gradientBrush),
970 "Could not create radial gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700971 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000972 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
973 "Could not set transform on radial gradient brush.");
974 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000975
bungeman@google.comb29c8832011-10-10 13:19:10 +0000976 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
977 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
978 "Could not get radial gradient stop collection.");
979 for (int i = 2; i < info.fColorCount; ++i) {
980 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
981 HR(createXpsGradientStop(info.fColors[i],
982 info.fColorOffsets[i],
983 &gradStop));
984 HRM(gradStopCollection->Append(gradStop.get()),
985 "Could not add radial gradient stop.");
986 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000987
bungeman@google.comb29c8832011-10-10 13:19:10 +0000988 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
989 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000990
bungeman@google.comb29c8832011-10-10 13:19:10 +0000991 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
992 "Could not set opacity of radial gradient brush.");
993 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000994
bungeman@google.comb29c8832011-10-10 13:19:10 +0000995 return S_OK;
996}
997
998HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
999 IXpsOMBrush** brush,
1000 const SkMatrix* parentTransform) {
1001 const SkShader *shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -07001002 if (nullptr == shader) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001003 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1004 return S_OK;
1005 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001006
bungeman@google.comb29c8832011-10-10 13:19:10 +00001007 //Gradient shaders.
1008 SkShader::GradientInfo info;
1009 info.fColorCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001010 info.fColors = nullptr;
1011 info.fColorOffsets = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001012 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001013
bungeman@google.comb29c8832011-10-10 13:19:10 +00001014 if (SkShader::kNone_GradientType == gradientType) {
1015 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001016
bungeman@google.comb29c8832011-10-10 13:19:10 +00001017 } else if (SkShader::kColor_GradientType == gradientType) {
1018 SkASSERT(1 == info.fColorCount);
1019 SkColor color;
1020 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +00001021 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001022 SkAlpha alpha = skPaint.getAlpha();
1023 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1024 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001025
bungeman@google.comb29c8832011-10-10 13:19:10 +00001026 } else {
1027 if (info.fColorCount == 0) {
1028 const SkColor color = skPaint.getColor();
1029 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
1030 return S_OK;
1031 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001032
bungeman@google.comb29c8832011-10-10 13:19:10 +00001033 SkAutoTArray<SkColor> colors(info.fColorCount);
1034 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1035 info.fColors = colors.get();
1036 info.fColorOffsets = colorOffsets.get();
1037 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001038
bungeman@google.comb29c8832011-10-10 13:19:10 +00001039 if (1 == info.fColorCount) {
1040 SkColor color = info.fColors[0];
1041 SkAlpha alpha = skPaint.getAlpha();
1042 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1043 return S_OK;
1044 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001045
bsalomon@google.comf94b3a42012-10-31 18:09:01 +00001046 SkMatrix localMatrix = shader->getLocalMatrix();
bsalomon49f085d2014-09-05 13:34:00 -07001047 if (parentTransform) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001048 localMatrix.preConcat(*parentTransform);
1049 }
1050 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1051 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001052
bungeman@google.comb29c8832011-10-10 13:19:10 +00001053 if (SkShader::kLinear_GradientType == gradientType) {
1054 HR(this->createXpsLinearGradient(info,
1055 skPaint.getAlpha(),
1056 localMatrix,
1057 xpsMatrixToUse.get(),
1058 brush));
1059 return S_OK;
1060 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001061
bungeman@google.comb29c8832011-10-10 13:19:10 +00001062 if (SkShader::kRadial_GradientType == gradientType) {
1063 HR(this->createXpsRadialGradient(info,
1064 skPaint.getAlpha(),
1065 localMatrix,
1066 xpsMatrixToUse.get(),
1067 brush));
1068 return S_OK;
1069 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001070
reed71a6cbf2015-05-04 08:32:51 -07001071 if (SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001072 //simple if affine and one is 0, otherwise will have to fake
1073 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001074
bungeman@google.comb29c8832011-10-10 13:19:10 +00001075 if (SkShader::kSweep_GradientType == gradientType) {
1076 //have to fake
1077 }
1078 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001079
bungeman@google.comb29c8832011-10-10 13:19:10 +00001080 SkBitmap outTexture;
1081 SkMatrix outMatrix;
1082 SkShader::TileMode xy[2];
Mike Reed627778d2016-09-28 17:13:38 -04001083 SkImage* image = shader->isAImage(&outMatrix, xy);
1084 if (image && image->asLegacyBitmap(&outTexture, SkImage::kRO_LegacyBitmapMode)) {
reedf5822822015-08-19 11:46:38 -07001085 //TODO: outMatrix??
1086 SkMatrix localMatrix = shader->getLocalMatrix();
1087 if (parentTransform) {
1088 localMatrix.preConcat(*parentTransform);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001089 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001090
reedf5822822015-08-19 11:46:38 -07001091 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1092 HR(this->createXpsImageBrush(outTexture,
1093 localMatrix,
1094 xy,
1095 skPaint.getAlpha(),
1096 &tileBrush));
1097
1098 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
1099 } else {
1100 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1101 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001102 return S_OK;
1103}
1104
1105static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1106 const bool zeroWidth = (0 == paint.getStrokeWidth());
1107 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001108
bungeman@google.comb29c8832011-10-10 13:19:10 +00001109 return paint.getPathEffect() ||
1110 paint.getMaskFilter() ||
1111 paint.getRasterizer() ||
1112 (stroke && (
1113 (matrix.hasPerspective() && !zeroWidth) ||
1114 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1115 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1116 paint.getStrokeMiter() < SK_ScalarSqrt2)
1117 ))
1118 ;
1119}
1120
1121HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1122 IXpsOMGeometryFigure** xpsRect) {
1123 const SkPoint points[4] = {
1124 { rect.fLeft, rect.fTop },
1125 { rect.fRight, rect.fTop },
1126 { rect.fRight, rect.fBottom },
1127 { rect.fLeft, rect.fBottom },
1128 };
1129 return this->createXpsQuad(points, stroke, fill, xpsRect);
1130}
1131HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1132 BOOL stroke, BOOL fill,
1133 IXpsOMGeometryFigure** xpsQuad) {
1134 // Define the start point.
1135 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001136
bungeman@google.comb29c8832011-10-10 13:19:10 +00001137 // Create the figure.
1138 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1139 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001140
bungeman@google.comb29c8832011-10-10 13:19:10 +00001141 // Define the type of each segment.
1142 XPS_SEGMENT_TYPE segmentTypes[3] = {
1143 XPS_SEGMENT_TYPE_LINE,
1144 XPS_SEGMENT_TYPE_LINE,
1145 XPS_SEGMENT_TYPE_LINE,
1146 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001147
bungeman@google.comb29c8832011-10-10 13:19:10 +00001148 // Define the x and y coordinates of each corner of the figure.
1149 FLOAT segmentData[6] = {
1150 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1151 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1152 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1153 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001154
bungeman@google.comb29c8832011-10-10 13:19:10 +00001155 // Describe if the segments are stroked.
1156 BOOL segmentStrokes[3] = {
1157 stroke, stroke, stroke,
1158 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001159
bungeman@google.comb29c8832011-10-10 13:19:10 +00001160 // Add the segment data to the figure.
1161 HRM((*xpsQuad)->SetSegments(
1162 3, 6,
1163 segmentTypes , segmentData, segmentStrokes),
1164 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001165
bungeman@google.comb29c8832011-10-10 13:19:10 +00001166 // Set the closed and filled properties of the figure.
1167 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1168 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001169
bungeman@google.comb29c8832011-10-10 13:19:10 +00001170 return S_OK;
1171}
1172
bungeman@google.comb29c8832011-10-10 13:19:10 +00001173void SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1174 size_t count, const SkPoint points[],
1175 const SkPaint& paint) {
1176 //This will call back into the device to do the drawing.
1177 d.drawPoints(mode, count, points, paint, true);
1178}
1179
1180void SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1181 int vertexCount, const SkPoint verts[],
1182 const SkPoint texs[], const SkColor colors[],
Mike Reedfaba3712016-11-03 14:45:31 -04001183 SkBlendMode, const uint16_t indices[],
bungeman@google.comb29c8832011-10-10 13:19:10 +00001184 int indexCount, const SkPaint& paint) {
1185 //TODO: override this for XPS
1186 SkDEBUGF(("XPS drawVertices not yet implemented."));
1187}
1188
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001189void SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001190 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001191
bungeman@google.comb29c8832011-10-10 13:19:10 +00001192 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001193 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1194 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1195 if (paint->getStyle() != SkPaint::kFill_Style) {
1196 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001197 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001198
bungeman@google.comb29c8832011-10-10 13:19:10 +00001199 this->internalDrawRect(d, r, false, *fillPaint);
1200}
1201
1202void SkXPSDevice::drawRect(const SkDraw& d,
1203 const SkRect& r,
1204 const SkPaint& paint) {
1205 this->internalDrawRect(d, r, true, paint);
1206}
1207
scroggo@google.comcac8d012013-11-12 17:10:02 +00001208void SkXPSDevice::drawRRect(const SkDraw& d,
1209 const SkRRect& rr,
1210 const SkPaint& paint) {
1211 SkPath path;
1212 path.addRRect(rr);
halcanary96fcdcc2015-08-27 07:41:13 -07001213 this->drawPath(d, path, paint, nullptr, true);
scroggo@google.comcac8d012013-11-12 17:10:02 +00001214}
1215
bungeman@google.comb29c8832011-10-10 13:19:10 +00001216void SkXPSDevice::internalDrawRect(const SkDraw& d,
1217 const SkRect& r,
1218 bool transformRect,
1219 const SkPaint& paint) {
1220 //Exit early if there is nothing to draw.
reed1e7f5e72016-04-27 07:49:17 -07001221 if (d.fRC->isEmpty() ||
reed374772b2016-10-05 17:33:02 -07001222 (paint.getAlpha() == 0 && paint.isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001223 return;
1224 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001225
bungeman@google.comb29c8832011-10-10 13:19:10 +00001226 //Path the rect if we can't optimize it.
1227 if (rect_must_be_pathed(paint, *d.fMatrix)) {
1228 SkPath tmp;
1229 tmp.addRect(r);
1230 tmp.setFillType(SkPath::kWinding_FillType);
halcanary96fcdcc2015-08-27 07:41:13 -07001231 this->drawPath(d, tmp, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001232 return;
1233 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001234
bungeman@google.comb29c8832011-10-10 13:19:10 +00001235 //Create the shaded path.
1236 SkTScopedComPtr<IXpsOMPath> shadedPath;
1237 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1238 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001239
bungeman@google.comb29c8832011-10-10 13:19:10 +00001240 //Create the shaded geometry.
1241 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1242 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1243 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001244
bungeman@google.comb29c8832011-10-10 13:19:10 +00001245 //Add the geometry to the shaded path.
1246 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1247 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001248
bungeman@google.comb29c8832011-10-10 13:19:10 +00001249 //Set the brushes.
1250 BOOL fill = FALSE;
1251 BOOL stroke = FALSE;
1252 HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001253
bungeman@google.comb29c8832011-10-10 13:19:10 +00001254 bool xpsTransformsPath = true;
1255 //Transform the geometry.
1256 if (transformRect && xpsTransformsPath) {
1257 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1258 HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1259 if (xpsTransform.get()) {
1260 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1261 "Could not set transform for rect.");
1262 } else {
1263 xpsTransformsPath = false;
1264 }
1265 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001266
bungeman@google.comb29c8832011-10-10 13:19:10 +00001267 //Create the figure.
1268 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1269 {
1270 SkPoint points[4] = {
1271 { r.fLeft, r.fTop },
1272 { r.fLeft, r.fBottom },
1273 { r.fRight, r.fBottom },
1274 { r.fRight, r.fTop },
1275 };
1276 if (!xpsTransformsPath && transformRect) {
1277 d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1278 }
1279 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1280 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001281
bungeman@google.comb29c8832011-10-10 13:19:10 +00001282 //Get the figures of the shaded geometry.
1283 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1284 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1285 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001286
bungeman@google.comb29c8832011-10-10 13:19:10 +00001287 //Add the figure to the shaded geometry figures.
1288 HRVM(shadedFigures->Append(rectFigure.get()),
1289 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001290
bungeman@google.comb29c8832011-10-10 13:19:10 +00001291 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001292
bungeman@google.comb29c8832011-10-10 13:19:10 +00001293 //Add the shaded path to the current visuals.
1294 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1295 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1296 "Could not get current visuals for rect.");
1297 HRVM(currentVisuals->Append(shadedPath.get()),
1298 "Could not add rect to current visuals.");
1299}
1300
1301static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1302 const SkTDArray<BOOL>& segmentStrokes,
1303 const SkTDArray<FLOAT>& segmentData,
1304 BOOL stroke, BOOL fill,
1305 IXpsOMGeometryFigure* figure,
1306 IXpsOMGeometryFigureCollection* figures) {
1307 // Add the segment data to the figure.
1308 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1309 segmentTypes.begin() , segmentData.begin(),
1310 segmentStrokes.begin()),
1311 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001312
bungeman@google.comb29c8832011-10-10 13:19:10 +00001313 // Set the closed and filled properties of the figure.
1314 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1315 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001316
bungeman@google.comb29c8832011-10-10 13:19:10 +00001317 // Add the figure created above to this geometry.
1318 HRM(figures->Append(figure), "Could not add path to geometry.");
1319 return S_OK;
1320}
1321
1322HRESULT SkXPSDevice::addXpsPathGeometry(
1323 IXpsOMGeometryFigureCollection* xpsFigures,
1324 BOOL stroke, BOOL fill, const SkPath& path) {
1325 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1326 SkTDArray<BOOL> segmentStrokes;
1327 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001328
bungeman@google.comb29c8832011-10-10 13:19:10 +00001329 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1330 SkPath::Iter iter(path, true);
1331 SkPoint points[4];
1332 SkPath::Verb verb;
1333 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1334 switch (verb) {
1335 case SkPath::kMove_Verb: {
bsalomon49f085d2014-09-05 13:34:00 -07001336 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001337 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1338 stroke, fill,
1339 xpsFigure.get() , xpsFigures));
1340 xpsFigure.reset();
1341 segmentTypes.rewind();
1342 segmentStrokes.rewind();
1343 segmentData.rewind();
1344 }
1345 // Define the start point.
1346 XPS_POINT startPoint = xps_point(points[0]);
1347 // Create the figure.
1348 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1349 &xpsFigure),
1350 "Could not create path geometry figure.");
1351 break;
1352 }
1353 case SkPath::kLine_Verb:
1354 if (iter.isCloseLine()) break; //ignore the line, auto-closed
1355 segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1356 segmentStrokes.push(stroke);
1357 segmentData.push(SkScalarToFLOAT(points[1].fX));
1358 segmentData.push(SkScalarToFLOAT(points[1].fY));
1359 break;
1360 case SkPath::kQuad_Verb:
1361 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1362 segmentStrokes.push(stroke);
1363 segmentData.push(SkScalarToFLOAT(points[1].fX));
1364 segmentData.push(SkScalarToFLOAT(points[1].fY));
1365 segmentData.push(SkScalarToFLOAT(points[2].fX));
1366 segmentData.push(SkScalarToFLOAT(points[2].fY));
1367 break;
1368 case SkPath::kCubic_Verb:
1369 segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1370 segmentStrokes.push(stroke);
1371 segmentData.push(SkScalarToFLOAT(points[1].fX));
1372 segmentData.push(SkScalarToFLOAT(points[1].fY));
1373 segmentData.push(SkScalarToFLOAT(points[2].fX));
1374 segmentData.push(SkScalarToFLOAT(points[2].fY));
1375 segmentData.push(SkScalarToFLOAT(points[3].fX));
1376 segmentData.push(SkScalarToFLOAT(points[3].fY));
1377 break;
halcanary47ef4d52015-03-03 09:13:09 -08001378 case SkPath::kConic_Verb: {
1379 const SkScalar tol = SK_Scalar1 / 4;
1380 SkAutoConicToQuads converter;
1381 const SkPoint* quads =
1382 converter.computeQuads(points, iter.conicWeight(), tol);
1383 for (int i = 0; i < converter.countQuads(); ++i) {
1384 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1385 segmentStrokes.push(stroke);
1386 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fX));
1387 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fY));
1388 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fX));
1389 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fY));
1390 }
1391 break;
1392 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001393 case SkPath::kClose_Verb:
1394 // we ignore these, and just get the whole segment from
1395 // the corresponding line/quad/cubic verbs
1396 break;
1397 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001398 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001399 break;
1400 }
1401 }
bsalomon49f085d2014-09-05 13:34:00 -07001402 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001403 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1404 stroke, fill,
1405 xpsFigure.get(), xpsFigures));
1406 }
1407 return S_OK;
1408}
1409
bungeman@google.comb29c8832011-10-10 13:19:10 +00001410void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1411 SkMatrix* matrix,
1412 SkVector* ppuScale,
1413 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001414 //This action is in unit space, but the ppm is specified in physical space.
reed80ea19c2015-05-12 10:37:34 -07001415 ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
1416 fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001417
bungeman@google.comb29c8832011-10-10 13:19:10 +00001418 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001419
bungeman@google.comb29c8832011-10-10 13:19:10 +00001420 const SkIRect& irect = clip;
1421 SkRect clipRect = SkRect::MakeLTRB(
1422 SkScalarMul(SkIntToScalar(irect.fLeft), ppuScale->fX),
1423 SkScalarMul(SkIntToScalar(irect.fTop), ppuScale->fY),
1424 SkScalarMul(SkIntToScalar(irect.fRight), ppuScale->fX),
1425 SkScalarMul(SkIntToScalar(irect.fBottom), ppuScale->fY));
1426 clipRect.roundOut(clipIRect);
1427}
1428
1429HRESULT SkXPSDevice::applyMask(const SkDraw& d,
1430 const SkMask& mask,
1431 const SkVector& ppuScale,
1432 IXpsOMPath* shadedPath) {
1433 //Get the geometry object.
1434 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1435 HRM(shadedPath->GetGeometry(&shadedGeometry),
1436 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001437
bungeman@google.comb29c8832011-10-10 13:19:10 +00001438 //Get the figures from the geometry.
1439 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1440 HRM(shadedGeometry->GetFigures(&shadedFigures),
1441 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001442
bungeman@google.comb29c8832011-10-10 13:19:10 +00001443 SkMatrix m;
1444 m.reset();
1445 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1446 SkIntToScalar(mask.fBounds.fTop));
1447 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001448
bungeman@google.comb29c8832011-10-10 13:19:10 +00001449 SkShader::TileMode xy[2];
1450 xy[0] = (SkShader::TileMode)3;
1451 xy[1] = (SkShader::TileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001452
bungeman@google.comb29c8832011-10-10 13:19:10 +00001453 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001454 bm.installMaskPixels(mask);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001455
bungeman@google.comb29c8832011-10-10 13:19:10 +00001456 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1457 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1458 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1459 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001460
bungeman@google.comb29c8832011-10-10 13:19:10 +00001461 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1462 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1463 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1464 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1465 "Could not create mask shaded figure.");
1466 HRM(shadedFigures->Append(shadedFigure.get()),
1467 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001468
bungeman@google.comb29c8832011-10-10 13:19:10 +00001469 HR(this->clip(shadedPath, d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001470
bungeman@google.comb29c8832011-10-10 13:19:10 +00001471 //Add the path to the active visual collection.
1472 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1473 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1474 "Could not get mask current visuals.");
1475 HRM(currentVisuals->Append(shadedPath),
1476 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001477
bungeman@google.comb29c8832011-10-10 13:19:10 +00001478 return S_OK;
1479}
1480
1481HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1482 const SkPaint& shaderPaint,
1483 const SkMatrix& matrix,
1484 BOOL* fill, BOOL* stroke) {
1485 *fill = FALSE;
1486 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001487
bungeman@google.comb29c8832011-10-10 13:19:10 +00001488 const SkPaint::Style style = shaderPaint.getStyle();
1489 const bool hasFill = SkPaint::kFill_Style == style
1490 || SkPaint::kStrokeAndFill_Style == style;
1491 const bool hasStroke = SkPaint::kStroke_Style == style
1492 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001493
bungeman@google.comb29c8832011-10-10 13:19:10 +00001494 //TODO(bungeman): use dictionaries and lookups.
1495 if (hasFill) {
1496 *fill = TRUE;
1497 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1498 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1499 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1500 "Could not set fill for shaded path.");
1501 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001502
bungeman@google.comb29c8832011-10-10 13:19:10 +00001503 if (hasStroke) {
1504 *stroke = TRUE;
1505 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1506 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1507 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1508 "Could not set stroke brush for shaded path.");
1509 HRM(shadedPath->SetStrokeThickness(
1510 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1511 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001512
bungeman@google.comb29c8832011-10-10 13:19:10 +00001513 if (0 == shaderPaint.getStrokeWidth()) {
1514 //XPS hair width is a hack. (XPS Spec 11.6.12).
1515 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1516 HRM(shadedPath->GetStrokeDashes(&dashes),
1517 "Could not set dashes for shaded path.");
1518 XPS_DASH dash;
1519 dash.length = 1.0;
1520 dash.gap = 0.0;
1521 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1522 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1523 "Could not set dash offset for shaded path.");
1524 }
1525 }
1526 return S_OK;
1527}
1528
1529void SkXPSDevice::drawPath(const SkDraw& d,
1530 const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001531 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001532 const SkMatrix* prePathMatrix,
1533 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001534 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1535
bungeman@google.comb29c8832011-10-10 13:19:10 +00001536 // nothing to draw
reed1e7f5e72016-04-27 07:49:17 -07001537 if (d.fRC->isEmpty() ||
reed374772b2016-10-05 17:33:02 -07001538 (paint->getAlpha() == 0 && paint->isSrcOver())) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001539 return;
1540 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001541
bungeman@google.comb29c8832011-10-10 13:19:10 +00001542 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001543 const bool paintHasPathEffect = paint->getPathEffect()
1544 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001545
bungeman@google.comb29c8832011-10-10 13:19:10 +00001546 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1547 SkMatrix matrix = *d.fMatrix;
1548 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1549 if (prePathMatrix) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001550 if (paintHasPathEffect || paint->getRasterizer()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001551 if (!pathIsMutable) {
1552 skeletalPath = &modifiedPath;
1553 pathIsMutable = true;
1554 }
1555 platonicPath.transform(*prePathMatrix, skeletalPath);
1556 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001557 matrix.preConcat(*prePathMatrix);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001558 }
1559 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001560
bungeman@google.comb29c8832011-10-10 13:19:10 +00001561 //Apply path effect [Skeletal-path -> Fillable-path].
1562 SkPath* fillablePath = skeletalPath;
1563 if (paintHasPathEffect) {
1564 if (!pathIsMutable) {
1565 fillablePath = &modifiedPath;
1566 pathIsMutable = true;
1567 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001568 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001569
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001570 SkPaint* writablePaint = paint.writable();
halcanary96fcdcc2015-08-27 07:41:13 -07001571 writablePaint->setPathEffect(nullptr);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001572 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001573 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001574 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001575 writablePaint->setStyle(SkPaint::kStroke_Style);
1576 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001577 }
1578 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001579
bungeman@google.comb29c8832011-10-10 13:19:10 +00001580 //Create the shaded path. This will be the path which is painted.
1581 SkTScopedComPtr<IXpsOMPath> shadedPath;
1582 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1583 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001584
bungeman@google.comb29c8832011-10-10 13:19:10 +00001585 //Create the geometry for the shaded path.
1586 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1587 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1588 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001589
bungeman@google.comb29c8832011-10-10 13:19:10 +00001590 //Add the geometry to the shaded path.
1591 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1592 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001593
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001594 SkRasterizer* rasterizer = paint->getRasterizer();
1595 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001596
1597 //Determine if we will draw or shade and mask.
1598 if (rasterizer || filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001599 if (paint->getStyle() != SkPaint::kFill_Style) {
1600 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001601 }
1602 }
1603
bungeman@google.comb29c8832011-10-10 13:19:10 +00001604 //Set the brushes.
1605 BOOL fill;
1606 BOOL stroke;
1607 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001608 *paint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001609 *d.fMatrix,
1610 &fill,
1611 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001612
bungeman@google.comb29c8832011-10-10 13:19:10 +00001613 //Rasterizer
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001614 if (rasterizer) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001615 SkIRect clipIRect;
1616 SkVector ppuScale;
1617 this->convertToPpm(filter,
1618 &matrix,
1619 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001620 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001621 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001622
halcanary96fcdcc2015-08-27 07:41:13 -07001623 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001624
bungeman@google.comb29c8832011-10-10 13:19:10 +00001625 //[Fillable-path -> Mask]
1626 SkMask rasteredMask;
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001627 if (rasterizer->rasterize(
bungeman@google.comb29c8832011-10-10 13:19:10 +00001628 *fillablePath,
1629 matrix,
1630 &clipIRect,
1631 filter, //just to compute how much to draw.
1632 &rasteredMask,
1633 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001634
bungeman@google.comb29c8832011-10-10 13:19:10 +00001635 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1636 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001637
bungeman@google.comb29c8832011-10-10 13:19:10 +00001638 //[Mask -> Mask]
1639 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001640 if (filter && filter->filterMask(&filteredMask, *mask, *d.fMatrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001641 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001642 }
1643 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001644
bungeman@google.comb29c8832011-10-10 13:19:10 +00001645 //Draw mask.
1646 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1647 }
1648 return;
1649 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001650
bungeman@google.comb29c8832011-10-10 13:19:10 +00001651 //Mask filter
1652 if (filter) {
1653 SkIRect clipIRect;
1654 SkVector ppuScale;
1655 this->convertToPpm(filter,
1656 &matrix,
1657 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001658 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001659 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001660
bungeman@google.comb29c8832011-10-10 13:19:10 +00001661 //[Fillable-path -> Pixel-path]
1662 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1663 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001664
halcanary96fcdcc2015-08-27 07:41:13 -07001665 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001666
bsalomon055e1922016-05-06 07:22:58 -07001667 SkASSERT(SkPaint::kFill_Style == paint->getStyle() ||
1668 (SkPaint::kStroke_Style == paint->getStyle() && 0 == paint->getStrokeWidth()));
1669 SkStrokeRec::InitStyle style = (SkPaint::kFill_Style == paint->getStyle())
1670 ? SkStrokeRec::kFill_InitStyle
1671 : SkStrokeRec::kHairline_InitStyle;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001672 //[Pixel-path -> Mask]
1673 SkMask rasteredMask;
1674 if (SkDraw::DrawToMask(
1675 *pixelPath,
1676 &clipIRect,
1677 filter, //just to compute how much to draw.
1678 &matrix,
1679 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001680 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon055e1922016-05-06 07:22:58 -07001681 style)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001682
bungeman@google.comb29c8832011-10-10 13:19:10 +00001683 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1684 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001685
bungeman@google.comb29c8832011-10-10 13:19:10 +00001686 //[Mask -> Mask]
1687 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001688 if (filter->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001689 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001690 }
1691 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001692
bungeman@google.comb29c8832011-10-10 13:19:10 +00001693 //Draw mask.
1694 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1695 }
1696 return;
1697 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001698
bungeman@google.comb29c8832011-10-10 13:19:10 +00001699 //Get the figures from the shaded geometry.
1700 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1701 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1702 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001703
bungeman@google.comb29c8832011-10-10 13:19:10 +00001704 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001705
bungeman@google.comb29c8832011-10-10 13:19:10 +00001706 //Set the fill rule.
bungeman76db31a2014-08-25 07:31:53 -07001707 SkPath* xpsCompatiblePath = fillablePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001708 XPS_FILL_RULE xpsFillRule;
bungeman76db31a2014-08-25 07:31:53 -07001709 switch (fillablePath->getFillType()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001710 case SkPath::kWinding_FillType:
1711 xpsFillRule = XPS_FILL_RULE_NONZERO;
1712 break;
1713 case SkPath::kEvenOdd_FillType:
1714 xpsFillRule = XPS_FILL_RULE_EVENODD;
1715 break;
1716 case SkPath::kInverseWinding_FillType: {
bungeman76db31a2014-08-25 07:31:53 -07001717 //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
1718 if (!pathIsMutable) {
1719 xpsCompatiblePath = &modifiedPath;
1720 pathIsMutable = true;
1721 }
1722 if (!Simplify(*fillablePath, xpsCompatiblePath)) {
1723 SkDEBUGF(("Could not simplify inverse winding path."));
1724 return;
1725 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001726 }
bungeman76db31a2014-08-25 07:31:53 -07001727 // The xpsCompatiblePath is noW inverse even odd, so fall through.
bungeman@google.comb29c8832011-10-10 13:19:10 +00001728 case SkPath::kInverseEvenOdd_FillType: {
1729 const SkRect universe = SkRect::MakeLTRB(
1730 0, 0,
1731 this->fCurrentCanvasSize.fWidth,
1732 this->fCurrentCanvasSize.fHeight);
1733 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1734 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1735 HRVM(shadedFigures->Append(addOneFigure.get()),
1736 "Could not add even-odd flip figure to shaded path.");
1737 xpsTransformsPath = false;
1738 xpsFillRule = XPS_FILL_RULE_EVENODD;
1739 break;
1740 }
1741 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001742 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001743 }
1744 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1745 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001746
bungeman@google.comb29c8832011-10-10 13:19:10 +00001747 //Create the XPS transform, if possible.
1748 if (xpsTransformsPath) {
1749 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1750 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001751
bungeman@google.comb29c8832011-10-10 13:19:10 +00001752 if (xpsTransform.get()) {
1753 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1754 "Could not set transform on shaded path.");
1755 } else {
1756 xpsTransformsPath = false;
1757 }
1758 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001759
bungeman76db31a2014-08-25 07:31:53 -07001760 SkPath* devicePath = xpsCompatiblePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001761 if (!xpsTransformsPath) {
1762 //[Fillable-path -> Device-path]
bungeman76db31a2014-08-25 07:31:53 -07001763 devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
1764 xpsCompatiblePath->transform(matrix, devicePath);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001765 }
1766 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1767 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001768
bungeman@google.comb29c8832011-10-10 13:19:10 +00001769 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001770
bungeman@google.comb29c8832011-10-10 13:19:10 +00001771 //Add the path to the active visual collection.
1772 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1773 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1774 "Could not get current visuals for shaded path.");
1775 HRVM(currentVisuals->Append(shadedPath.get()),
1776 "Could not add shaded path to current visuals.");
1777}
1778
1779HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1780 SkPath clipPath;
reed1e7f5e72016-04-27 07:49:17 -07001781 if (d.fRC->isBW()) {
1782 SkAssertResult(d.fRC->bwRgn().getBoundaryPath(&clipPath));
1783 } else {
1784 // Don't have a way to turn a AAClip into a path, so we just use the bounds.
1785 // TODO: consider using fClipStack instead?
1786 clipPath.addRect(SkRect::Make(d.fRC->getBounds()));
1787 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001788
bungeman@google.comb29c8832011-10-10 13:19:10 +00001789 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1790}
1791HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1792 const SkPath& clipPath,
1793 XPS_FILL_RULE fillRule) {
1794 //Create the geometry.
1795 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1796 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1797 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001798
bungeman@google.comb29c8832011-10-10 13:19:10 +00001799 //Get the figure collection of the geometry.
1800 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1801 HRM(clipGeometry->GetFigures(&clipFigures),
1802 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001803
bungeman@google.comb29c8832011-10-10 13:19:10 +00001804 //Create the figures into the geometry.
1805 HR(this->addXpsPathGeometry(
1806 clipFigures.get(),
1807 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001808
bungeman@google.comb29c8832011-10-10 13:19:10 +00001809 HRM(clipGeometry->SetFillRule(fillRule),
1810 "Could not set fill rule.");
1811 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1812 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001813
bungeman@google.comb29c8832011-10-10 13:19:10 +00001814 return S_OK;
1815}
1816
1817void SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001818 const SkMatrix& matrix, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001819 if (d.fRC->isEmpty()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001820 return;
1821 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001822
bungeman@google.comb29c8832011-10-10 13:19:10 +00001823 SkIRect srcRect;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001824 srcRect.set(0, 0, bitmap.width(), bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001825
bungeman@google.comb29c8832011-10-10 13:19:10 +00001826 //Create the new shaded path.
1827 SkTScopedComPtr<IXpsOMPath> shadedPath;
1828 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1829 "Could not create path for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001830
bungeman@google.comb29c8832011-10-10 13:19:10 +00001831 //Create the shaded geometry.
1832 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1833 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1834 "Could not create geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001835
bungeman@google.comb29c8832011-10-10 13:19:10 +00001836 //Add the shaded geometry to the shaded path.
1837 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1838 "Could not set the geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001839
bungeman@google.comb29c8832011-10-10 13:19:10 +00001840 //Get the shaded figures from the shaded geometry.
1841 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1842 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1843 "Could not get the figures for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001844
bungeman@google.comb29c8832011-10-10 13:19:10 +00001845 SkMatrix transform = matrix;
1846 transform.postConcat(*d.fMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001847
bungeman@google.comb29c8832011-10-10 13:19:10 +00001848 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1849 HRV(this->createXpsTransform(transform, &xpsTransform));
1850 if (xpsTransform.get()) {
1851 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1852 "Could not set transform for bitmap.");
1853 } else {
1854 //TODO: perspective that bitmap!
1855 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001856
bungeman@google.comb29c8832011-10-10 13:19:10 +00001857 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
bsalomon49f085d2014-09-05 13:34:00 -07001858 if (xpsTransform.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001859 const SkShader::TileMode xy[2] = {
1860 SkShader::kClamp_TileMode,
1861 SkShader::kClamp_TileMode,
1862 };
1863 SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001864 HRV(this->createXpsImageBrush(bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001865 transform,
1866 xy,
1867 paint.getAlpha(),
1868 &xpsImageBrush));
1869 HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
1870 "Could not set bitmap brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001871
bungeman@google.comb29c8832011-10-10 13:19:10 +00001872 const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
1873 SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1874 HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
1875 }
1876 HRVM(shadedFigures->Append(rectFigure.get()),
1877 "Could not add bitmap figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001878
bungeman@google.comb29c8832011-10-10 13:19:10 +00001879 //Get the current visual collection and add the shaded path to it.
1880 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1881 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1882 "Could not get current visuals for bitmap");
1883 HRVM(currentVisuals->Append(shadedPath.get()),
1884 "Could not add bitmap to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001885
bungeman@google.comb29c8832011-10-10 13:19:10 +00001886 HRV(this->clip(shadedPath.get(), d));
1887}
1888
1889void SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
1890 int x, int y,
1891 const SkPaint& paint) {
1892 //TODO: override this for XPS
1893 SkDEBUGF(("XPS drawSprite not yet implemented."));
1894}
1895
1896HRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
1897 TypefaceUse** typefaceUse) {
reed@google.com398de9a2013-03-21 21:43:51 +00001898 SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001899
bungeman@google.comb29c8832011-10-10 13:19:10 +00001900 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00001901 const SkFontID typefaceID = typeface->uniqueID();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001902 if (!this->fTypefaces.empty()) {
1903 TypefaceUse* current = &this->fTypefaces.front();
1904 const TypefaceUse* last = &this->fTypefaces.back();
1905 for (; current <= last; ++current) {
1906 if (current->typefaceId == typefaceID) {
1907 *typefaceUse = current;
1908 return S_OK;
1909 }
1910 }
1911 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001912
bungeman@google.comb29c8832011-10-10 13:19:10 +00001913 //TODO: create glyph only fonts
1914 //and let the host deal with what kind of font we're looking at.
1915 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001916
bungeman@google.comb29c8832011-10-10 13:19:10 +00001917 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001918 int ttcIndex;
1919 SkStream* fontData = typeface->openStream(&ttcIndex);
bungeman@google.com635091f2013-10-01 15:03:18 +00001920 //TODO: cannot handle FON fonts.
bungeman@google.com8cddc8d2012-01-03 22:36:33 +00001921 HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001922 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001923
bungeman@google.comb29c8832011-10-10 13:19:10 +00001924 const size_t size =
1925 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1926 wchar_t buffer[size];
1927 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -04001928 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001929 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001930
bungeman@google.comb29c8832011-10-10 13:19:10 +00001931 SkTScopedComPtr<IOpcPartUri> partUri;
1932 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1933 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001934
bungeman@google.comb29c8832011-10-10 13:19:10 +00001935 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1936 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1937 embedding,
1938 partUri.get(),
1939 FALSE,
1940 &xpsFontResource),
1941 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001942
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001943 //TODO: change openStream to return -1 for non-ttc, get rid of this.
1944 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1945 bool isTTC = (data &&
1946 fontData->getLength() >= sizeof(SkTTCFHeader) &&
1947 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1948
bungeman@google.comb29c8832011-10-10 13:19:10 +00001949 TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
1950 newTypefaceUse.typefaceId = typefaceID;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001951 newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001952 newTypefaceUse.fontData = fontData;
1953 newTypefaceUse.xpsFont = xpsFontResource.release();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001954
robertphillips8e0c1502015-07-07 10:28:43 -07001955 SkAutoGlyphCache agc(paint, &this->surfaceProps(), &SkMatrix::I());
bungeman@google.comb29c8832011-10-10 13:19:10 +00001956 SkGlyphCache* glyphCache = agc.getCache();
1957 unsigned int glyphCount = glyphCache->getGlyphCount();
1958 newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001959
bungeman@google.comb29c8832011-10-10 13:19:10 +00001960 *typefaceUse = &newTypefaceUse;
1961 return S_OK;
1962}
1963
1964HRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
1965 IXpsOMObjectFactory* xpsFactory,
1966 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001967 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001968 LPCWSTR text,
1969 XPS_GLYPH_INDEX* xpsGlyphs,
1970 UINT32 xpsGlyphsLen,
1971 XPS_POINT *origin,
1972 FLOAT fontSize,
1973 XPS_STYLE_SIMULATION sims,
1974 const SkMatrix& transform,
1975 const SkPaint& paint) {
1976 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001977 HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
1978 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001979
bungeman@google.comb29c8832011-10-10 13:19:10 +00001980 //XPS uses affine transformations for everything...
1981 //...except positioning text.
1982 bool useCanvasForClip;
1983 if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
1984 origin->x += SkScalarToFLOAT(transform.getTranslateX());
1985 origin->y += SkScalarToFLOAT(transform.getTranslateY());
1986 useCanvasForClip = false;
1987 } else {
1988 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1989 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1990 if (xpsMatrixToUse.get()) {
1991 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1992 "Could not set transform matrix.");
1993 useCanvasForClip = true;
1994 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00001995 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001996 useCanvasForClip = false;
1997 }
1998 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001999
bungeman@google.comb29c8832011-10-10 13:19:10 +00002000 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
2001 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002002
bsalomon49f085d2014-09-05 13:34:00 -07002003 if (text) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002004 HRM(glyphsEditor->SetUnicodeString(text),
2005 "Could not set unicode string.");
2006 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002007
bsalomon49f085d2014-09-05 13:34:00 -07002008 if (xpsGlyphs) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002009 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
2010 "Could not set glyphs.");
2011 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002012
bungeman@google.comb29c8832011-10-10 13:19:10 +00002013 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002014
bungeman@google.comb29c8832011-10-10 13:19:10 +00002015 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
2016 HR(this->createXpsBrush(
2017 paint,
2018 &xpsFillBrush,
halcanary96fcdcc2015-08-27 07:41:13 -07002019 useCanvasForClip ? nullptr : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002020
bungeman@google.comb29c8832011-10-10 13:19:10 +00002021 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
2022 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002023
bungeman@google.comb29c8832011-10-10 13:19:10 +00002024 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002025
bungeman@google.comb29c8832011-10-10 13:19:10 +00002026 HRM(glyphs->SetFontRenderingEmSize(fontSize),
2027 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002028
bungeman@google.comb29c8832011-10-10 13:19:10 +00002029 HRM(glyphs->SetStyleSimulations(sims),
2030 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002031
bungeman@google.comb29c8832011-10-10 13:19:10 +00002032 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2033 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002034
bungeman@google.comb29c8832011-10-10 13:19:10 +00002035 if (!useCanvasForClip) {
2036 HR(this->clip(glyphs.get(), d));
2037 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2038 } else {
2039 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2040 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2041 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002042
bungeman@google.comb29c8832011-10-10 13:19:10 +00002043 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2044 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2045 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002046
bungeman@google.comb29c8832011-10-10 13:19:10 +00002047 HRM(glyphCanvasVisuals->Append(glyphs.get()),
2048 "Could not add glyphs to page.");
2049 HR(this->clip(glyphCanvas.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002050
bungeman@google.comb29c8832011-10-10 13:19:10 +00002051 HRM(visuals->Append(glyphCanvas.get()),
2052 "Could not add glyph canvas to page.");
2053 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002054
bungeman@google.comb29c8832011-10-10 13:19:10 +00002055 return S_OK;
2056}
2057
herbbda26432015-11-24 08:37:01 -08002058static int num_glyph_guess(SkPaint::TextEncoding encoding, const void* text, size_t byteLength) {
2059 switch (encoding) {
2060 case SkPaint::kUTF8_TextEncoding:
2061 return SkUTF8_CountUnichars(static_cast<const char *>(text), byteLength);
2062 case SkPaint::kUTF16_TextEncoding:
2063 return SkUTF16_CountUnichars(static_cast<const uint16_t *>(text), SkToInt(byteLength));
2064 case SkPaint::kGlyphID_TextEncoding:
2065 return SkToInt(byteLength / 2);
2066 default:
djsollenf2b340f2016-01-29 08:51:04 -08002067 SK_ABORT("Invalid Text Encoding");
bungeman@google.comb29c8832011-10-10 13:19:10 +00002068 }
herbbda26432015-11-24 08:37:01 -08002069 return 0;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002070}
2071
2072static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2073 const SkPaint::Style style = paint.getStyle();
2074 return matrix.hasPerspective()
2075 || SkPaint::kStroke_Style == style
2076 || SkPaint::kStrokeAndFill_Style == style
2077 || paint.getMaskFilter()
2078 || paint.getRasterizer()
2079 ;
2080}
2081
herbbda26432015-11-24 08:37:01 -08002082typedef SkTDArray<XPS_GLYPH_INDEX> GlyphRun;
2083
2084class ProcessOneGlyph {
2085public:
2086 ProcessOneGlyph(FLOAT centemPerUnit, SkBitSet* glyphUse, GlyphRun* xpsGlyphs)
2087 : fCentemPerUnit(centemPerUnit)
2088 , fGlyphUse(glyphUse)
2089 , fXpsGlyphs(xpsGlyphs) { }
2090
2091 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint) {
2092 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
2093
2094 SkScalar x = position.fX;
2095 SkScalar y = position.fY;
2096
2097 XPS_GLYPH_INDEX* xpsGlyph = fXpsGlyphs->append();
2098 uint16_t glyphID = glyph.getGlyphID();
halcanarye2348cc2016-08-19 16:23:23 -07002099 fGlyphUse->set(glyphID);
herbbda26432015-11-24 08:37:01 -08002100 xpsGlyph->index = glyphID;
2101 if (1 == fXpsGlyphs->count()) {
2102 xpsGlyph->advanceWidth = 0.0f;
2103 xpsGlyph->horizontalOffset = SkScalarToFloat(x) * fCentemPerUnit;
2104 xpsGlyph->verticalOffset = SkScalarToFloat(y) * -fCentemPerUnit;
2105 }
2106 else {
2107 const XPS_GLYPH_INDEX& first = (*fXpsGlyphs)[0];
2108 xpsGlyph->advanceWidth = 0.0f;
2109 xpsGlyph->horizontalOffset = (SkScalarToFloat(x) * fCentemPerUnit)
2110 - first.horizontalOffset;
2111 xpsGlyph->verticalOffset = (SkScalarToFloat(y) * -fCentemPerUnit)
2112 - first.verticalOffset;
2113 }
2114 }
2115
2116private:
2117 /** [in] Advance width and offsets for glyphs measured in
2118 hundredths of the font em size (XPS Spec 5.1.3). */
2119 const FLOAT fCentemPerUnit;
2120 /** [in,out] The accumulated glyphs used in the current typeface. */
2121 SkBitSet* const fGlyphUse;
2122 /** [out] The glyphs to draw. */
2123 GlyphRun* const fXpsGlyphs;
2124};
2125
bungeman@google.comb29c8832011-10-10 13:19:10 +00002126void SkXPSDevice::drawText(const SkDraw& d,
2127 const void* text, size_t byteLen,
2128 SkScalar x, SkScalar y,
2129 const SkPaint& paint) {
2130 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002131
bungeman@google.comb29c8832011-10-10 13:19:10 +00002132 if (text_must_be_pathed(paint, *d.fMatrix)) {
2133 SkPath path;
2134 paint.getTextPath(text, byteLen, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002135 this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002136 //TODO: add automation "text"
2137 return;
2138 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002139
bungeman@google.comb29c8832011-10-10 13:19:10 +00002140 TypefaceUse* typeface;
2141 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002142
herbbda26432015-11-24 08:37:01 -08002143 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002144
herbbda26432015-11-24 08:37:01 -08002145 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2146 SkGlyphCache* cache = autoCache.getCache();
reed@google.com089130c2011-12-12 21:52:18 +00002147
herbbda26432015-11-24 08:37:01 -08002148 // Advance width and offsets for glyphs measured in hundredths of the font em size
2149 // (XPS Spec 5.1.3).
2150 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2151 GlyphRun xpsGlyphs;
2152 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2153 static_cast<const char*>(text), byteLen));
2154
2155 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2156
2157 SkFindAndPlaceGlyph::ProcessText(
2158 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2159 SkPoint{ x, y }, matrix, paint.getTextAlign(), cache, processOneGlyph);
2160
2161 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002162 return;
2163 }
2164
bungeman@google.comb29c8832011-10-10 13:19:10 +00002165 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002166 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2167 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002168 };
herbbda26432015-11-24 08:37:01 -08002169 xpsGlyphs[0].horizontalOffset = 0.0f;
2170 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002171
bungeman@google.comb29c8832011-10-10 13:19:10 +00002172 HRV(AddGlyphs(d,
2173 this->fXpsFactory.get(),
2174 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002175 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002176 nullptr,
herbbda26432015-11-24 08:37:01 -08002177 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002178 &origin,
2179 SkScalarToFLOAT(paint.getTextSize()),
2180 XPS_STYLE_SIMULATION_NONE,
2181 *d.fMatrix,
2182 paint));
2183}
2184
2185void SkXPSDevice::drawPosText(const SkDraw& d,
2186 const void* text, size_t byteLen,
fmalita05c4a432014-09-29 06:29:53 -07002187 const SkScalar pos[], int scalarsPerPos,
2188 const SkPoint& offset, const SkPaint& paint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002189 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002190
bungeman@google.comb29c8832011-10-10 13:19:10 +00002191 if (text_must_be_pathed(paint, *d.fMatrix)) {
2192 SkPath path;
2193 //TODO: make this work, Draw currently does not handle as well.
2194 //paint.getTextPath(text, byteLength, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002195 //this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002196 //TODO: add automation "text"
2197 return;
2198 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002199
bungeman@google.comb29c8832011-10-10 13:19:10 +00002200 TypefaceUse* typeface;
2201 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002202
herbbda26432015-11-24 08:37:01 -08002203 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002204
herbbda26432015-11-24 08:37:01 -08002205 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2206 SkGlyphCache* cache = autoCache.getCache();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002207
herbbda26432015-11-24 08:37:01 -08002208 // Advance width and offsets for glyphs measured in hundredths of the font em size
2209 // (XPS Spec 5.1.3).
2210 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2211 GlyphRun xpsGlyphs;
2212 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2213 static_cast<const char*>(text), byteLen));
2214
2215 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2216
2217 SkFindAndPlaceGlyph::ProcessPosText(
2218 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2219 offset, matrix, pos, scalarsPerPos, paint.getTextAlign(), cache, processOneGlyph);
2220
2221 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002222 return;
2223 }
2224
bungeman@google.comb29c8832011-10-10 13:19:10 +00002225 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002226 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2227 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002228 };
herbbda26432015-11-24 08:37:01 -08002229 xpsGlyphs[0].horizontalOffset = 0.0f;
2230 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002231
bungeman@google.comb29c8832011-10-10 13:19:10 +00002232 HRV(AddGlyphs(d,
2233 this->fXpsFactory.get(),
2234 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002235 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002236 nullptr,
herbbda26432015-11-24 08:37:01 -08002237 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002238 &origin,
2239 SkScalarToFLOAT(paint.getTextSize()),
2240 XPS_STYLE_SIMULATION_NONE,
2241 *d.fMatrix,
2242 paint));
2243}
2244
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002245void SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002246 int x, int y,
2247 const SkPaint&) {
2248 SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002249
bungeman@google.comb29c8832011-10-10 13:19:10 +00002250 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2251 XPS_MATRIX rawTransform = {
2252 1.0f,
2253 0.0f,
2254 0.0f,
2255 1.0f,
2256 static_cast<FLOAT>(x),
2257 static_cast<FLOAT>(y),
2258 };
2259 HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2260 "Could not create layer transform.");
2261 HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2262 "Could not set layer transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002263
bungeman@google.comb29c8832011-10-10 13:19:10 +00002264 //Get the current visual collection and add the layer to it.
2265 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2266 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2267 "Could not get current visuals for layer.");
2268 HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2269 "Could not add layer to current visuals.");
2270}
2271
reed76033be2015-03-14 10:54:31 -07002272SkBaseDevice* SkXPSDevice::onCreateDevice(const CreateInfo& info, const SkPaint*) {
bungeman@google.com635091f2013-10-01 15:03:18 +00002273//Conditional for bug compatibility with PDF device.
2274#if 0
fmalita6987dca2014-11-13 08:33:37 -08002275 if (SkBaseDevice::kGeneral_Usage == info.fUsage) {
halcanary96fcdcc2015-08-27 07:41:13 -07002276 return nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002277 //To what stream do we write?
2278 //SkXPSDevice* dev = new SkXPSDevice(this);
2279 //SkSize s = SkSize::Make(width, height);
2280 //dev->BeginCanvas(s, s, SkMatrix::I());
2281 //return dev;
2282 }
bungeman@google.com635091f2013-10-01 15:03:18 +00002283#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +00002284 return new SkXPSDevice(this->fXpsFactory.get());
2285}
2286
mtklein1ee76512015-11-02 10:20:27 -08002287#endif//defined(SK_BUILD_FOR_WIN32)