blob: 48233bb55437a37a90fba0c6726a2baf9bc94e2e [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
bungeman@google.comb29c8832011-10-10 13:19:10 +000011#ifndef UNICODE
12#define UNICODE
13#endif
14#ifndef _UNICODE
15#define _UNICODE
16#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +000017#include <ObjBase.h>
18#include <XpsObjectModel.h>
19#include <T2EmbApi.h>
20#include <FontSub.h>
21
22#include "SkColor.h"
23#include "SkConstexprMath.h"
24#include "SkData.h"
25#include "SkDraw.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000026#include "SkEndian.h"
herbbda26432015-11-24 08:37:01 -080027#include "SkFindAndPlaceGlyph.h"
halcanary47ef4d52015-03-03 09:13:09 -080028#include "SkGeometry.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000029#include "SkGlyphCache.h"
30#include "SkHRESULT.h"
31#include "SkImageEncoder.h"
32#include "SkIStream.h"
33#include "SkMaskFilter.h"
34#include "SkPaint.h"
Ben Wagnerda5a1b82014-08-22 15:07:06 -040035#include "SkPathOps.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000036#include "SkPoint.h"
37#include "SkRasterizer.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000038#include "SkSFNTHeader.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000039#include "SkShader.h"
40#include "SkSize.h"
41#include "SkStream.h"
42#include "SkTDArray.h"
43#include "SkTLazy.h"
44#include "SkTScopedComPtr.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000045#include "SkTTCFHeader.h"
reed@google.com398de9a2013-03-21 21:43:51 +000046#include "SkTypefacePriv.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000047#include "SkUtils.h"
48#include "SkXPSDevice.h"
49
50//Windows defines a FLOAT type,
51//make it clear when converting a scalar that this is what is wanted.
52#define SkScalarToFLOAT(n) SkScalarToFloat(n)
53
Ben Wagnerda5a1b82014-08-22 15:07:06 -040054//Dummy representation of a GUID from createId.
bungeman@google.comb29c8832011-10-10 13:19:10 +000055#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
halcanary96fcdcc2015-08-27 07:41:13 -070056//Length of GUID representation from createId, including nullptr terminator.
bungeman@google.comb29c8832011-10-10 13:19:10 +000057#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
58
59/**
60 Formats a GUID and places it into buffer.
61 buffer should have space for at least GUID_ID_LEN wide characters.
62 The string will always be wchar null terminated.
63 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
64 @return -1 if there was an error, > 0 if success.
65 */
66static int format_guid(const GUID& guid,
67 wchar_t* buffer, size_t bufferSize,
68 wchar_t sep = '-') {
69 SkASSERT(bufferSize >= GUID_ID_LEN);
70 return swprintf_s(buffer,
71 bufferSize,
72 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
73 guid.Data1,
74 sep,
75 guid.Data2,
76 sep,
77 guid.Data3,
78 sep,
79 guid.Data4[0],
80 guid.Data4[1],
81 sep,
82 guid.Data4[2],
83 guid.Data4[3],
84 guid.Data4[4],
85 guid.Data4[5],
86 guid.Data4[6],
87 guid.Data4[7]);
88}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000089
Ben Wagnerda5a1b82014-08-22 15:07:06 -040090HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000091 GUID guid = {};
Ben Wagnerda5a1b82014-08-22 15:07:06 -040092#ifdef SK_XPS_USE_DETERMINISTIC_IDS
93 guid.Data1 = fNextId++;
94 // The following make this a valid Type4 UUID.
95 guid.Data3 = 0x4000;
96 guid.Data4[0] = 0x80;
97#else
bungeman@google.comb29c8832011-10-10 13:19:10 +000098 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
Ben Wagnerda5a1b82014-08-22 15:07:06 -040099#endif
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000100
bungeman@google.comb29c8832011-10-10 13:19:10 +0000101 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
102 HRM(E_UNEXPECTED, "Could not format GUID into id.");
103 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000104
bungeman@google.comb29c8832011-10-10 13:19:10 +0000105 return S_OK;
106}
107
108static SkBitmap make_fake_bitmap(int width, int height) {
109 SkBitmap bitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000110 bitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000111 return bitmap;
112}
113
reed@google.com900ecf22014-02-20 20:55:37 +0000114// TODO: should inherit from SkBaseDevice instead of SkBitmapDevice...
bungeman@google.comb29c8832011-10-10 13:19:10 +0000115SkXPSDevice::SkXPSDevice()
robertphillips9a53fd72015-06-22 09:46:59 -0700116 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
bungeman@google.comb29c8832011-10-10 13:19:10 +0000117 , fCurrentPage(0) {
118}
119
robertphillips9a53fd72015-06-22 09:46:59 -0700120SkXPSDevice::SkXPSDevice(IXpsOMObjectFactory* xpsFactory)
121 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
122 , fCurrentPage(0) {
123
124 HRVM(CoCreateInstance(
125 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700126 nullptr,
robertphillips9a53fd72015-06-22 09:46:59 -0700127 CLSCTX_INPROC_SERVER,
128 IID_PPV_ARGS(&this->fXpsFactory)),
129 "Could not create factory for layer.");
130
131 HRVM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
132 "Could not create canvas for layer.");
133}
134
bungeman@google.comb29c8832011-10-10 13:19:10 +0000135SkXPSDevice::~SkXPSDevice() {
136}
137
138SkXPSDevice::TypefaceUse::TypefaceUse()
139 : typefaceId(0xffffffff)
halcanary96fcdcc2015-08-27 07:41:13 -0700140 , fontData(nullptr)
141 , xpsFont(nullptr)
142 , glyphsUsed(nullptr) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000143}
144
145SkXPSDevice::TypefaceUse::~TypefaceUse() {
146 //xpsFont owns fontData ref
147 this->xpsFont->Release();
148 delete this->glyphsUsed;
149}
150
151bool SkXPSDevice::beginPortfolio(SkWStream* outputStream) {
152 if (!this->fAutoCo.succeeded()) return false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000153
bungeman@google.comb29c8832011-10-10 13:19:10 +0000154 //Create XPS Factory.
155 HRBM(CoCreateInstance(
156 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700157 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000158 CLSCTX_INPROC_SERVER,
159 IID_PPV_ARGS(&this->fXpsFactory)),
160 "Could not create XPS factory.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000161
bungeman@google.comb29c8832011-10-10 13:19:10 +0000162 HRBM(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream),
163 "Could not convert SkStream to IStream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000164
bungeman@google.comb29c8832011-10-10 13:19:10 +0000165 return true;
166}
167
168bool SkXPSDevice::beginSheet(
169 const SkVector& unitsPerMeter,
170 const SkVector& pixelsPerMeter,
171 const SkSize& trimSize,
172 const SkRect* mediaBox,
173 const SkRect* bleedBox,
174 const SkRect* artBox,
175 const SkRect* cropBox) {
176 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000177
bungeman@google.comb29c8832011-10-10 13:19:10 +0000178 //For simplicity, just write everything out in geometry units,
179 //then have a base canvas do the scale to physical units.
180 this->fCurrentCanvasSize = trimSize;
181 this->fCurrentUnitsPerMeter = unitsPerMeter;
182 this->fCurrentPixelsPerMeter = pixelsPerMeter;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
bungeman@google.comb29c8832011-10-10 13:19:10 +0000184 this->fCurrentXpsCanvas.reset();
185 HRBM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
186 "Could not create base canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000187
bungeman@google.comb29c8832011-10-10 13:19:10 +0000188 return true;
189}
190
191HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
192 const unsigned int pageNum,
193 IXpsOMImageResource** image) {
194 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
195 HRM(CoCreateInstance(
196 CLSID_XpsOMThumbnailGenerator,
halcanary96fcdcc2015-08-27 07:41:13 -0700197 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000198 CLSCTX_INPROC_SERVER,
199 IID_PPV_ARGS(&thumbnailGenerator)),
200 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000201
bungeman@google.comb29c8832011-10-10 13:19:10 +0000202 SkTScopedComPtr<IOpcPartUri> partUri;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000203 static const size_t size = SkTUMax<
bungeman@google.comb29c8832011-10-10 13:19:10 +0000204 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + SK_DIGITS_IN(pageNum),
205 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png")
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000206 >::value;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000207 wchar_t buffer[size];
208 if (pageNum > 0) {
209 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
210 } else {
211 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400212 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000213 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
214 }
215 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
216 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000217
bungeman@google.comb29c8832011-10-10 13:19:10 +0000218 HRM(thumbnailGenerator->GenerateThumbnail(page,
219 XPS_IMAGE_TYPE_PNG,
220 XPS_THUMBNAIL_SIZE_LARGE,
221 partUri.get(),
222 image),
223 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000224
bungeman@google.comb29c8832011-10-10 13:19:10 +0000225 return S_OK;
226}
227
228HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
229 IXpsOMPage** page) {
230 static const size_t size = SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
231 + SK_DIGITS_IN(fCurrentPage);
232 wchar_t buffer[size];
233 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
234 this->fCurrentPage);
235 SkTScopedComPtr<IOpcPartUri> partUri;
236 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
237 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000238
bungeman@google.comb29c8832011-10-10 13:19:10 +0000239 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
240 HRM(this->fXpsFactory->CreatePage(&pageSize,
241 L"und",
242 partUri.get(),
243 page),
244 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000245
bungeman@google.comb29c8832011-10-10 13:19:10 +0000246 return S_OK;
247}
248
249HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
250 //Create package writer.
251 {
252 SkTScopedComPtr<IOpcPartUri> partUri;
253 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
254 &partUri),
255 "Could not create document sequence part uri.");
256 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
257 this->fOutputStream.get(),
258 TRUE,
259 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
260 partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700261 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000262 image,
halcanary96fcdcc2015-08-27 07:41:13 -0700263 nullptr,
264 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000265 &this->fPackageWriter),
266 "Could not create package writer.");
267 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000268
bungeman@google.comb29c8832011-10-10 13:19:10 +0000269 //Begin the lone document.
270 {
271 SkTScopedComPtr<IOpcPartUri> partUri;
272 HRM(this->fXpsFactory->CreatePartUri(
273 L"/Documents/1/FixedDocument.fdoc",
274 &partUri),
275 "Could not create fixed document part uri.");
276 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700277 nullptr,
278 nullptr,
279 nullptr,
280 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000281 "Could not start document.");
282 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000283
bungeman@google.comb29c8832011-10-10 13:19:10 +0000284 return S_OK;
285}
286
287bool SkXPSDevice::endSheet() {
288 //XPS is fixed at 96dpi (XPS Spec 11.1).
289 static const float xpsDPI = 96.0f;
290 static const float inchesPerMeter = 10000.0f / 254.0f;
291 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
292 const float scaleX = targetUnitsPerMeter
293 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
294 const float scaleY = targetUnitsPerMeter
295 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000296
bungeman@google.comb29c8832011-10-10 13:19:10 +0000297 //Create the scale canvas.
298 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
299 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
300 "Could not create scale canvas.");
301 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
302 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
303 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000304
bungeman@google.comb29c8832011-10-10 13:19:10 +0000305 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
306 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
307 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
308 "Could not create geometry to physical transform.");
309 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
310 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000311
bungeman@google.comb29c8832011-10-10 13:19:10 +0000312 //Add the content canvas to the scale canvas.
313 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
314 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000315
bungeman@google.comb29c8832011-10-10 13:19:10 +0000316 //Create the page.
317 XPS_SIZE pageSize = {
318 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
319 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
320 };
321 SkTScopedComPtr<IXpsOMPage> page;
322 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000323
bungeman@google.comb29c8832011-10-10 13:19:10 +0000324 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
325 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000326
bungeman@google.comb29c8832011-10-10 13:19:10 +0000327 //Add the scale canvas to the page.
328 HRBM(pageVisuals->Append(scaleCanvas.get()),
329 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000330
bungeman@google.comb29c8832011-10-10 13:19:10 +0000331 //Create the package writer if it hasn't been created yet.
halcanary96fcdcc2015-08-27 07:41:13 -0700332 if (nullptr == this->fPackageWriter.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000333 SkTScopedComPtr<IXpsOMImageResource> image;
334 //Ignore return, thumbnail is completely optional.
335 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000336
bungeman@google.comb29c8832011-10-10 13:19:10 +0000337 HRB(this->initXpsDocumentWriter(image.get()));
338 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000339
bungeman@google.comb29c8832011-10-10 13:19:10 +0000340 HRBM(this->fPackageWriter->AddPage(page.get(),
341 &pageSize,
halcanary96fcdcc2015-08-27 07:41:13 -0700342 nullptr,
343 nullptr,
344 nullptr,
345 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000346 "Could not write the page.");
347 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000348
bungeman@google.comb29c8832011-10-10 13:19:10 +0000349 return true;
350}
351
352static HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
353 //CreateFontPackage wants unsigned short.
354 //Microsoft, Y U NO stdint.h?
355 SkTDArray<unsigned short> keepList;
356 current->glyphsUsed->exportTo(&keepList);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000357
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000358 int ttcCount = (current->ttcIndex + 1);
359
bungeman@google.comb29c8832011-10-10 13:19:10 +0000360 //The following are declared with the types required by CreateFontPackage.
halcanary96fcdcc2015-08-27 07:41:13 -0700361 unsigned char *fontPackageBufferRaw = nullptr;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000362 unsigned long fontPackageBufferSize;
363 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000364 unsigned long result = CreateFontPackage(
365 (unsigned char *) current->fontData->getMemoryBase(),
366 (unsigned long) current->fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000367 &fontPackageBufferRaw,
368 &fontPackageBufferSize,
369 &bytesWritten,
370 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
371 current->ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000372 TTFCFP_SUBSET,
373 0,
374 0,
375 0,
376 keepList.begin(),
377 keepList.count(),
378 sk_malloc_throw,
379 sk_realloc_throw,
380 sk_free,
halcanary96fcdcc2015-08-27 07:41:13 -0700381 nullptr);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000382 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000383 if (result != NO_ERROR) {
384 SkDEBUGF(("CreateFontPackage Error %lu", result));
385 return E_UNEXPECTED;
386 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000387
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000388 // If it was originally a ttc, keep it a ttc.
389 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
390 size_t extra;
391 if (ttcCount > 0) {
392 // Create space for a ttc header.
393 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
394 fontPackageBuffer.realloc(bytesWritten + extra);
395 //overlap is certain, use memmove
396 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
397
398 // Write the ttc header.
399 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
400 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
401 ttcfHeader->version = SkTTCFHeader::version_1;
402 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
403 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
404 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
bsalomon0aa5cea2014-12-15 09:13:35 -0800405 *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000406 }
407
408 // Fix up offsets in sfnt table entries.
409 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
410 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
411 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
412 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
413 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
414 tableDirectory->offset = SkEndian_SwapBE32(
bsalomon0aa5cea2014-12-15 09:13:35 -0800415 SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000416 }
417 } else {
418 extra = 0;
419 fontPackageBuffer.realloc(bytesWritten);
420 }
421
scroggoa1193e42015-01-21 12:09:53 -0800422 SkAutoTDelete<SkMemoryStream> newStream(new SkMemoryStream());
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000423 newStream->setMemoryOwned(fontPackageBuffer.detach(), bytesWritten + extra);
424
bungeman@google.comb29c8832011-10-10 13:19:10 +0000425 SkTScopedComPtr<IStream> newIStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000426 SkIStream::CreateFromSkStream(newStream.detach(), true, &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000427
bungeman@google.comb29c8832011-10-10 13:19:10 +0000428 XPS_FONT_EMBEDDING embedding;
429 HRM(current->xpsFont->GetEmbeddingOption(&embedding),
430 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000431
bungeman@google.comb29c8832011-10-10 13:19:10 +0000432 SkTScopedComPtr<IOpcPartUri> partUri;
433 HRM(current->xpsFont->GetPartName(&partUri),
434 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000435
bungeman@google.comb29c8832011-10-10 13:19:10 +0000436 HRM(current->xpsFont->SetContent(
437 newIStream.get(),
438 embedding,
439 partUri.get()),
440 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000441
bungeman@google.comb29c8832011-10-10 13:19:10 +0000442 return S_OK;
443}
444
445bool SkXPSDevice::endPortfolio() {
446 //Subset fonts
447 if (!this->fTypefaces.empty()) {
448 SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
449 const TypefaceUse* last = &this->fTypefaces.back();
450 for (; current <= last; ++current) {
451 //Ignore return for now, if it didn't subset, let it be.
452 subset_typeface(current);
453 }
454 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000455
bungeman@google.comb29c8832011-10-10 13:19:10 +0000456 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000457
bungeman@google.comb29c8832011-10-10 13:19:10 +0000458 return true;
459}
460
461static XPS_COLOR xps_color(const SkColor skColor) {
462 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
463 XPS_COLOR xpsColor;
464 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
465 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
466 xpsColor.value.sRGB.red = SkColorGetR(skColor);
467 xpsColor.value.sRGB.green = SkColorGetG(skColor);
468 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000469
bungeman@google.comb29c8832011-10-10 13:19:10 +0000470 return xpsColor;
471}
472
473static XPS_POINT xps_point(const SkPoint& point) {
474 XPS_POINT xpsPoint = {
475 SkScalarToFLOAT(point.fX),
476 SkScalarToFLOAT(point.fY),
477 };
478 return xpsPoint;
479}
480
481static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
482 SkPoint skTransformedPoint;
483 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
484 return xps_point(skTransformedPoint);
485}
486
487static XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
488 switch (tileMode) {
489 case SkShader::kClamp_TileMode:
490 return XPS_SPREAD_METHOD_PAD;
491 case SkShader::kRepeat_TileMode:
492 return XPS_SPREAD_METHOD_REPEAT;
493 case SkShader::kMirror_TileMode:
494 return XPS_SPREAD_METHOD_REFLECT;
495 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000496 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000497 }
498 return XPS_SPREAD_METHOD_PAD;
499}
500
501static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
502 const SkPoint& start, const SkPoint& end,
503 const SkMatrix& transform) {
504 SkPoint startTransformed;
505 transform.mapXY(start.fX, start.fY, &startTransformed);
506 SkPoint endTransformed;
507 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000508
bungeman@google.comb29c8832011-10-10 13:19:10 +0000509 //Manhattan distance between transformed start and end.
510 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
511 + (endTransformed.fY - startTransformed.fY);
512 if (SkScalarNearlyZero(startToEnd)) {
513 for (int i = 0; i < numOffsets; ++i) {
514 stopOffsets[i] = 0;
515 }
516 return;
517 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000518
bungeman@google.comb29c8832011-10-10 13:19:10 +0000519 for (int i = 0; i < numOffsets; ++i) {
520 SkPoint stop;
521 stop.fX = SkScalarMul(end.fX - start.fX, stopOffsets[i]);
522 stop.fY = SkScalarMul(end.fY - start.fY, stopOffsets[i]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000523
bungeman@google.comb29c8832011-10-10 13:19:10 +0000524 SkPoint stopTransformed;
525 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000526
bungeman@google.comb29c8832011-10-10 13:19:10 +0000527 //Manhattan distance between transformed start and stop.
528 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
529 + (stopTransformed.fY - startTransformed.fY);
530 //Percentage along transformed line.
reed80ea19c2015-05-12 10:37:34 -0700531 stopOffsets[i] = startToStop / startToEnd;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000532 }
533}
534
535HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
536 IXpsOMMatrixTransform** xpsTransform) {
537 SkScalar affine[6];
538 if (!matrix.asAffine(affine)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700539 *xpsTransform = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000540 return S_FALSE;
541 }
542 XPS_MATRIX rawXpsMatrix = {
543 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
544 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
545 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
546 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
547 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
548 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
549 };
550 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
551 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000552
bungeman@google.comb29c8832011-10-10 13:19:10 +0000553 return S_OK;
554}
555
556HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
557 IXpsOMVisualCollection* visuals,
558 IXpsOMPath** path) {
559 SkTScopedComPtr<IXpsOMGeometry> geometry;
560 HRM(this->fXpsFactory->CreateGeometry(&geometry),
561 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000562
bungeman@google.comb29c8832011-10-10 13:19:10 +0000563 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
564 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
565 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000566
bungeman@google.comb29c8832011-10-10 13:19:10 +0000567 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
568 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000569
bungeman@google.comb29c8832011-10-10 13:19:10 +0000570 HRM(visuals->Append(*path), "Could not add path to visuals.");
571 return S_OK;
572}
573
574HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
575 const SkAlpha alpha,
576 IXpsOMBrush** xpsBrush) {
577 XPS_COLOR xpsColor = xps_color(skColor);
578 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
halcanary96fcdcc2015-08-27 07:41:13 -0700579 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000580 "Could not create solid color brush.");
581 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
582 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
583 return S_OK;
584}
585
586HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
587 const XPS_RECT& imageViewBox,
588 IXpsOMImageResource* image,
589 IXpsOMVisualCollection* visuals) {
590 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
591 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000592
bungeman@google.comb29c8832011-10-10 13:19:10 +0000593 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
594 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000595
bungeman@google.comb29c8832011-10-10 13:19:10 +0000596 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
597 HRM(this->fXpsFactory->CreateImageBrush(image,
598 &imageViewBox,
599 &imageViewBox,
600 &areaToFillBrush),
601 "Could not create brush for side of clamp.");
602 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
603 "Could not set tile mode for side of clamp.");
604 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
605 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000606
bungeman@google.comb29c8832011-10-10 13:19:10 +0000607 return S_OK;
608}
609
610HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
611 const SkColor color,
612 IXpsOMVisualCollection* visuals) {
613 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
614 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000615
bungeman@google.comb29c8832011-10-10 13:19:10 +0000616 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
617 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000618
bungeman@google.comb29c8832011-10-10 13:19:10 +0000619 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
620 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
621 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
622 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000623
bungeman@google.comb29c8832011-10-10 13:19:10 +0000624 return S_OK;
625}
626
627static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
628static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
629static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
630static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
631static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
632
633//TODO(bungeman): In the future, should skia add None,
634//handle None+Mirror and None+Repeat correctly.
635//None is currently an internal hack so masks don't repeat (None+None only).
636static XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
637 [SkShader::kTileModeCount+1] = {
638 //Clamp //Repeat //Mirror //None
639/*Clamp */ XTM_N, XTM_T, XTM_Y, XTM_N,
640/*Repeat*/ XTM_T, XTM_T, XTM_Y, XTM_N,
641/*Mirror*/ XTM_X, XTM_X, XTM_XY, XTM_X,
642/*None */ XTM_N, XTM_N, XTM_Y, XTM_N,
643};
644
645HRESULT SkXPSDevice::createXpsImageBrush(
646 const SkBitmap& bitmap,
647 const SkMatrix& localMatrix,
648 const SkShader::TileMode (&xy)[2],
649 const SkAlpha alpha,
650 IXpsOMTileBrush** xpsBrush) {
651 SkDynamicMemoryWStream write;
652 if (!SkImageEncoder::EncodeStream(&write, bitmap,
653 SkImageEncoder::kPNG_Type, 100)) {
654 HRM(E_FAIL, "Unable to encode bitmap as png.");
655 }
656 SkMemoryStream* read = new SkMemoryStream;
657 read->setData(write.copyToData())->unref();
658 SkTScopedComPtr<IStream> readWrapper;
659 HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
660 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000661
bungeman@google.comb29c8832011-10-10 13:19:10 +0000662 const size_t size =
663 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
664 wchar_t buffer[size];
665 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400666 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000667 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000668
bungeman@google.comb29c8832011-10-10 13:19:10 +0000669 SkTScopedComPtr<IOpcPartUri> imagePartUri;
670 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
671 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000672
bungeman@google.comb29c8832011-10-10 13:19:10 +0000673 SkTScopedComPtr<IXpsOMImageResource> imageResource;
674 HRM(this->fXpsFactory->CreateImageResource(
675 readWrapper.get(),
676 XPS_IMAGE_TYPE_PNG,
677 imagePartUri.get(),
678 &imageResource),
679 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000680
bungeman@google.comb29c8832011-10-10 13:19:10 +0000681 XPS_RECT bitmapRect = {
682 0.0, 0.0,
683 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
684 };
685 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
686 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
687 &bitmapRect, &bitmapRect,
688 &xpsImageBrush),
689 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000690
bungeman@google.comb29c8832011-10-10 13:19:10 +0000691 if (SkShader::kClamp_TileMode != xy[0] &&
692 SkShader::kClamp_TileMode != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000693
bungeman@google.comb29c8832011-10-10 13:19:10 +0000694 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
695 "Could not set image tile mode");
696 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
697 "Could not set image opacity.");
698 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
699 } else {
700 //TODO(bungeman): compute how big this really needs to be.
701 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
702 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
703 const SkScalar bWidth = SkIntToScalar(bitmap.width());
704 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000705
bungeman@google.comb29c8832011-10-10 13:19:10 +0000706 //create brush canvas
707 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
708 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
709 "Could not create image brush canvas.");
710 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
711 HRM(brushCanvas->GetVisuals(&brushVisuals),
712 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000713
bungeman@google.comb29c8832011-10-10 13:19:10 +0000714 //create central figure
715 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
716 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
717 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000718
bungeman@google.comb29c8832011-10-10 13:19:10 +0000719 SkTScopedComPtr<IXpsOMPath> centralPath;
720 HR(this->createPath(centralFigure.get(),
721 brushVisuals.get(),
722 &centralPath));
723 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
724 "Could not set tile mode for image brush central path.");
725 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
726 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000727
bungeman@google.comb29c8832011-10-10 13:19:10 +0000728 //add left/right
729 if (SkShader::kClamp_TileMode == xy[0]) {
730 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
731 XPS_RECT leftImageViewBox = {
732 0.0, 0.0,
733 1.0, static_cast<FLOAT>(bitmap.height()),
734 };
735 HR(this->sideOfClamp(leftArea, leftImageViewBox,
736 imageResource.get(),
737 brushVisuals.get()));
738
739 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
740 XPS_RECT rightImageViewBox = {
741 bitmap.width() - 1.0f, 0.0f,
742 1.0f, static_cast<FLOAT>(bitmap.height()),
743 };
744 HR(this->sideOfClamp(rightArea, rightImageViewBox,
745 imageResource.get(),
746 brushVisuals.get()));
747 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000748
bungeman@google.comb29c8832011-10-10 13:19:10 +0000749 //add top/bottom
750 if (SkShader::kClamp_TileMode == xy[1]) {
751 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
752 XPS_RECT topImageViewBox = {
753 0.0, 0.0,
754 static_cast<FLOAT>(bitmap.width()), 1.0,
755 };
756 HR(this->sideOfClamp(topArea, topImageViewBox,
757 imageResource.get(),
758 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000759
bungeman@google.comb29c8832011-10-10 13:19:10 +0000760 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
761 XPS_RECT bottomImageViewBox = {
762 0.0f, bitmap.height() - 1.0f,
763 static_cast<FLOAT>(bitmap.width()), 1.0f,
764 };
765 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
766 imageResource.get(),
767 brushVisuals.get()));
768 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000769
bungeman@google.comb29c8832011-10-10 13:19:10 +0000770 //add tl, tr, bl, br
771 if (SkShader::kClamp_TileMode == xy[0] &&
772 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000773
bungeman@google.comb29c8832011-10-10 13:19:10 +0000774 SkAutoLockPixels alp(bitmap);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000775
bungeman@google.comb29c8832011-10-10 13:19:10 +0000776 const SkColor tlColor = bitmap.getColor(0,0);
777 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
778 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000779
bungeman@google.comb29c8832011-10-10 13:19:10 +0000780 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
781 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
782 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000783
bungeman@google.comb29c8832011-10-10 13:19:10 +0000784 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
785 bitmap.height()-1);
786 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
787 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000788
bungeman@google.comb29c8832011-10-10 13:19:10 +0000789 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
790 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
791 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
792 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000793
bungeman@google.comb29c8832011-10-10 13:19:10 +0000794 //create visual brush from canvas
795 XPS_RECT bound = {};
796 if (SkShader::kClamp_TileMode == xy[0] &&
797 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000798
bungeman@google.comb29c8832011-10-10 13:19:10 +0000799 bound.x = BIG_F / -2;
800 bound.y = BIG_F / -2;
801 bound.width = BIG_F;
802 bound.height = BIG_F;
803 } else if (SkShader::kClamp_TileMode == xy[0]) {
804 bound.x = BIG_F / -2;
805 bound.y = 0.0f;
806 bound.width = BIG_F;
807 bound.height = static_cast<FLOAT>(bitmap.height());
808 } else if (SkShader::kClamp_TileMode == xy[1]) {
809 bound.x = 0;
810 bound.y = BIG_F / -2;
811 bound.width = static_cast<FLOAT>(bitmap.width());
812 bound.height = BIG_F;
813 }
814 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
815 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
816 "Could not create visual brush for image brush.");
817 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
818 "Could not set canvas on visual brush for image brush.");
819 HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
820 "Could not set tile mode on visual brush for image brush.");
821 HRM(clampBrush->SetOpacity(alpha / 255.0f),
822 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000823
bungeman@google.comb29c8832011-10-10 13:19:10 +0000824 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
825 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000826
bungeman@google.comb29c8832011-10-10 13:19:10 +0000827 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
828 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
bsalomon49f085d2014-09-05 13:34:00 -0700829 if (xpsMatrixToUse.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000830 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
831 "Could not set transform for image brush.");
832 } else {
833 //TODO(bungeman): perspective bitmaps in general.
834 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000835
bungeman@google.comb29c8832011-10-10 13:19:10 +0000836 return S_OK;
837}
838
839HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
840 const SkScalar offset,
841 IXpsOMGradientStop** xpsGradStop) {
842 XPS_COLOR gradStopXpsColor = xps_color(skColor);
843 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
halcanary96fcdcc2015-08-27 07:41:13 -0700844 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000845 SkScalarToFLOAT(offset),
846 xpsGradStop),
847 "Could not create gradient stop.");
848 return S_OK;
849}
850
851HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
852 const SkAlpha alpha,
853 const SkMatrix& localMatrix,
854 IXpsOMMatrixTransform* xpsMatrix,
855 IXpsOMBrush** xpsBrush) {
856 XPS_POINT startPoint;
857 XPS_POINT endPoint;
bsalomon49f085d2014-09-05 13:34:00 -0700858 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000859 startPoint = xps_point(info.fPoint[0]);
860 endPoint = xps_point(info.fPoint[1]);
861 } else {
862 transform_offsets(info.fColorOffsets, info.fColorCount,
863 info.fPoint[0], info.fPoint[1],
864 localMatrix);
865 startPoint = xps_point(info.fPoint[0], localMatrix);
866 endPoint = xps_point(info.fPoint[1], localMatrix);
867 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000868
bungeman@google.comb29c8832011-10-10 13:19:10 +0000869 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
870 HR(createXpsGradientStop(info.fColors[0],
871 info.fColorOffsets[0],
872 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000873
bungeman@google.comb29c8832011-10-10 13:19:10 +0000874 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
875 HR(createXpsGradientStop(info.fColors[1],
876 info.fColorOffsets[1],
877 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000878
bungeman@google.comb29c8832011-10-10 13:19:10 +0000879 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
880 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
881 gradStop1.get(),
882 &startPoint,
883 &endPoint,
884 &gradientBrush),
885 "Could not create linear gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700886 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000887 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
888 "Could not set transform on linear gradient brush.");
889 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000890
bungeman@google.comb29c8832011-10-10 13:19:10 +0000891 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
892 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
893 "Could not get linear gradient stop collection.");
894 for (int i = 2; i < info.fColorCount; ++i) {
895 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
896 HR(createXpsGradientStop(info.fColors[i],
897 info.fColorOffsets[i],
898 &gradStop));
899 HRM(gradStopCollection->Append(gradStop.get()),
900 "Could not add linear gradient stop.");
901 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000902
bungeman@google.comb29c8832011-10-10 13:19:10 +0000903 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
904 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000905
bungeman@google.comb29c8832011-10-10 13:19:10 +0000906 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
907 "Could not set opacity of linear gradient brush.");
908 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000909
bungeman@google.comb29c8832011-10-10 13:19:10 +0000910 return S_OK;
911}
912
913HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
914 const SkAlpha alpha,
915 const SkMatrix& localMatrix,
916 IXpsOMMatrixTransform* xpsMatrix,
917 IXpsOMBrush** xpsBrush) {
918 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
919 HR(createXpsGradientStop(info.fColors[0],
920 info.fColorOffsets[0],
921 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000922
bungeman@google.comb29c8832011-10-10 13:19:10 +0000923 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
924 HR(createXpsGradientStop(info.fColors[1],
925 info.fColorOffsets[1],
926 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000927
bungeman@google.comb29c8832011-10-10 13:19:10 +0000928 //TODO: figure out how to fake better if not affine
929 XPS_POINT centerPoint;
930 XPS_POINT gradientOrigin;
931 XPS_SIZE radiiSizes;
bsalomon49f085d2014-09-05 13:34:00 -0700932 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000933 centerPoint = xps_point(info.fPoint[0]);
934 gradientOrigin = xps_point(info.fPoint[0]);
935 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
936 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
937 } else {
938 centerPoint = xps_point(info.fPoint[0], localMatrix);
939 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000940
bungeman@google.comb29c8832011-10-10 13:19:10 +0000941 SkScalar radius = info.fRadius[0];
942 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000943
bungeman@google.comb29c8832011-10-10 13:19:10 +0000944 vec[0].set(radius, 0);
945 vec[1].set(0, radius);
946 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000947
bungeman@google.comb29c8832011-10-10 13:19:10 +0000948 SkScalar d0 = vec[0].length();
949 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000950
bungeman@google.comb29c8832011-10-10 13:19:10 +0000951 radiiSizes.width = SkScalarToFLOAT(d0);
952 radiiSizes.height = SkScalarToFLOAT(d1);
953 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000954
bungeman@google.comb29c8832011-10-10 13:19:10 +0000955 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
956 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
957 gradStop1.get(),
958 &centerPoint,
959 &gradientOrigin,
960 &radiiSizes,
961 &gradientBrush),
962 "Could not create radial gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700963 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000964 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
965 "Could not set transform on radial gradient brush.");
966 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000967
bungeman@google.comb29c8832011-10-10 13:19:10 +0000968 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
969 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
970 "Could not get radial gradient stop collection.");
971 for (int i = 2; i < info.fColorCount; ++i) {
972 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
973 HR(createXpsGradientStop(info.fColors[i],
974 info.fColorOffsets[i],
975 &gradStop));
976 HRM(gradStopCollection->Append(gradStop.get()),
977 "Could not add radial gradient stop.");
978 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000979
bungeman@google.comb29c8832011-10-10 13:19:10 +0000980 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
981 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000982
bungeman@google.comb29c8832011-10-10 13:19:10 +0000983 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
984 "Could not set opacity of radial gradient brush.");
985 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000986
bungeman@google.comb29c8832011-10-10 13:19:10 +0000987 return S_OK;
988}
989
990HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
991 IXpsOMBrush** brush,
992 const SkMatrix* parentTransform) {
993 const SkShader *shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -0700994 if (nullptr == shader) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000995 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
996 return S_OK;
997 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000998
bungeman@google.comb29c8832011-10-10 13:19:10 +0000999 //Gradient shaders.
1000 SkShader::GradientInfo info;
1001 info.fColorCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001002 info.fColors = nullptr;
1003 info.fColorOffsets = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001004 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001005
bungeman@google.comb29c8832011-10-10 13:19:10 +00001006 if (SkShader::kNone_GradientType == gradientType) {
1007 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001008
bungeman@google.comb29c8832011-10-10 13:19:10 +00001009 } else if (SkShader::kColor_GradientType == gradientType) {
1010 SkASSERT(1 == info.fColorCount);
1011 SkColor color;
1012 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +00001013 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001014 SkAlpha alpha = skPaint.getAlpha();
1015 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1016 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001017
bungeman@google.comb29c8832011-10-10 13:19:10 +00001018 } else {
1019 if (info.fColorCount == 0) {
1020 const SkColor color = skPaint.getColor();
1021 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
1022 return S_OK;
1023 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001024
bungeman@google.comb29c8832011-10-10 13:19:10 +00001025 SkAutoTArray<SkColor> colors(info.fColorCount);
1026 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1027 info.fColors = colors.get();
1028 info.fColorOffsets = colorOffsets.get();
1029 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001030
bungeman@google.comb29c8832011-10-10 13:19:10 +00001031 if (1 == info.fColorCount) {
1032 SkColor color = info.fColors[0];
1033 SkAlpha alpha = skPaint.getAlpha();
1034 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1035 return S_OK;
1036 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001037
bsalomon@google.comf94b3a42012-10-31 18:09:01 +00001038 SkMatrix localMatrix = shader->getLocalMatrix();
bsalomon49f085d2014-09-05 13:34:00 -07001039 if (parentTransform) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001040 localMatrix.preConcat(*parentTransform);
1041 }
1042 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1043 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001044
bungeman@google.comb29c8832011-10-10 13:19:10 +00001045 if (SkShader::kLinear_GradientType == gradientType) {
1046 HR(this->createXpsLinearGradient(info,
1047 skPaint.getAlpha(),
1048 localMatrix,
1049 xpsMatrixToUse.get(),
1050 brush));
1051 return S_OK;
1052 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001053
bungeman@google.comb29c8832011-10-10 13:19:10 +00001054 if (SkShader::kRadial_GradientType == gradientType) {
1055 HR(this->createXpsRadialGradient(info,
1056 skPaint.getAlpha(),
1057 localMatrix,
1058 xpsMatrixToUse.get(),
1059 brush));
1060 return S_OK;
1061 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001062
reed71a6cbf2015-05-04 08:32:51 -07001063 if (SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001064 //simple if affine and one is 0, otherwise will have to fake
1065 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001066
bungeman@google.comb29c8832011-10-10 13:19:10 +00001067 if (SkShader::kSweep_GradientType == gradientType) {
1068 //have to fake
1069 }
1070 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001071
bungeman@google.comb29c8832011-10-10 13:19:10 +00001072 SkBitmap outTexture;
1073 SkMatrix outMatrix;
1074 SkShader::TileMode xy[2];
reedf5822822015-08-19 11:46:38 -07001075 if (shader->isABitmap(&outTexture, &outMatrix, xy)) {
1076 //TODO: outMatrix??
1077 SkMatrix localMatrix = shader->getLocalMatrix();
1078 if (parentTransform) {
1079 localMatrix.preConcat(*parentTransform);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001080 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001081
reedf5822822015-08-19 11:46:38 -07001082 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1083 HR(this->createXpsImageBrush(outTexture,
1084 localMatrix,
1085 xy,
1086 skPaint.getAlpha(),
1087 &tileBrush));
1088
1089 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
1090 } else {
1091 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1092 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001093 return S_OK;
1094}
1095
1096static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1097 const bool zeroWidth = (0 == paint.getStrokeWidth());
1098 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001099
bungeman@google.comb29c8832011-10-10 13:19:10 +00001100 return paint.getPathEffect() ||
1101 paint.getMaskFilter() ||
1102 paint.getRasterizer() ||
1103 (stroke && (
1104 (matrix.hasPerspective() && !zeroWidth) ||
1105 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1106 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1107 paint.getStrokeMiter() < SK_ScalarSqrt2)
1108 ))
1109 ;
1110}
1111
1112HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1113 IXpsOMGeometryFigure** xpsRect) {
1114 const SkPoint points[4] = {
1115 { rect.fLeft, rect.fTop },
1116 { rect.fRight, rect.fTop },
1117 { rect.fRight, rect.fBottom },
1118 { rect.fLeft, rect.fBottom },
1119 };
1120 return this->createXpsQuad(points, stroke, fill, xpsRect);
1121}
1122HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1123 BOOL stroke, BOOL fill,
1124 IXpsOMGeometryFigure** xpsQuad) {
1125 // Define the start point.
1126 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001127
bungeman@google.comb29c8832011-10-10 13:19:10 +00001128 // Create the figure.
1129 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1130 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001131
bungeman@google.comb29c8832011-10-10 13:19:10 +00001132 // Define the type of each segment.
1133 XPS_SEGMENT_TYPE segmentTypes[3] = {
1134 XPS_SEGMENT_TYPE_LINE,
1135 XPS_SEGMENT_TYPE_LINE,
1136 XPS_SEGMENT_TYPE_LINE,
1137 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001138
bungeman@google.comb29c8832011-10-10 13:19:10 +00001139 // Define the x and y coordinates of each corner of the figure.
1140 FLOAT segmentData[6] = {
1141 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1142 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1143 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1144 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001145
bungeman@google.comb29c8832011-10-10 13:19:10 +00001146 // Describe if the segments are stroked.
1147 BOOL segmentStrokes[3] = {
1148 stroke, stroke, stroke,
1149 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001150
bungeman@google.comb29c8832011-10-10 13:19:10 +00001151 // Add the segment data to the figure.
1152 HRM((*xpsQuad)->SetSegments(
1153 3, 6,
1154 segmentTypes , segmentData, segmentStrokes),
1155 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001156
bungeman@google.comb29c8832011-10-10 13:19:10 +00001157 // Set the closed and filled properties of the figure.
1158 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1159 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001160
bungeman@google.comb29c8832011-10-10 13:19:10 +00001161 return S_OK;
1162}
1163
bungeman@google.comb29c8832011-10-10 13:19:10 +00001164void SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1165 size_t count, const SkPoint points[],
1166 const SkPaint& paint) {
1167 //This will call back into the device to do the drawing.
1168 d.drawPoints(mode, count, points, paint, true);
1169}
1170
1171void SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1172 int vertexCount, const SkPoint verts[],
1173 const SkPoint texs[], const SkColor colors[],
1174 SkXfermode* xmode, const uint16_t indices[],
1175 int indexCount, const SkPaint& paint) {
1176 //TODO: override this for XPS
1177 SkDEBUGF(("XPS drawVertices not yet implemented."));
1178}
1179
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001180void SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001181 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001182
bungeman@google.comb29c8832011-10-10 13:19:10 +00001183 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001184 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1185 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1186 if (paint->getStyle() != SkPaint::kFill_Style) {
1187 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001188 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001189
bungeman@google.comb29c8832011-10-10 13:19:10 +00001190 this->internalDrawRect(d, r, false, *fillPaint);
1191}
1192
1193void SkXPSDevice::drawRect(const SkDraw& d,
1194 const SkRect& r,
1195 const SkPaint& paint) {
1196 this->internalDrawRect(d, r, true, paint);
1197}
1198
scroggo@google.comcac8d012013-11-12 17:10:02 +00001199void SkXPSDevice::drawRRect(const SkDraw& d,
1200 const SkRRect& rr,
1201 const SkPaint& paint) {
1202 SkPath path;
1203 path.addRRect(rr);
halcanary96fcdcc2015-08-27 07:41:13 -07001204 this->drawPath(d, path, paint, nullptr, true);
scroggo@google.comcac8d012013-11-12 17:10:02 +00001205}
1206
bungeman@google.comb29c8832011-10-10 13:19:10 +00001207void SkXPSDevice::internalDrawRect(const SkDraw& d,
1208 const SkRect& r,
1209 bool transformRect,
1210 const SkPaint& paint) {
1211 //Exit early if there is nothing to draw.
1212 if (d.fClip->isEmpty() ||
halcanary96fcdcc2015-08-27 07:41:13 -07001213 (paint.getAlpha() == 0 && paint.getXfermode() == nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001214 return;
1215 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001216
bungeman@google.comb29c8832011-10-10 13:19:10 +00001217 //Path the rect if we can't optimize it.
1218 if (rect_must_be_pathed(paint, *d.fMatrix)) {
1219 SkPath tmp;
1220 tmp.addRect(r);
1221 tmp.setFillType(SkPath::kWinding_FillType);
halcanary96fcdcc2015-08-27 07:41:13 -07001222 this->drawPath(d, tmp, paint, nullptr, true);
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 //Create the shaded path.
1227 SkTScopedComPtr<IXpsOMPath> shadedPath;
1228 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1229 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001230
bungeman@google.comb29c8832011-10-10 13:19:10 +00001231 //Create the shaded geometry.
1232 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1233 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1234 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001235
bungeman@google.comb29c8832011-10-10 13:19:10 +00001236 //Add the geometry to the shaded path.
1237 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1238 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001239
bungeman@google.comb29c8832011-10-10 13:19:10 +00001240 //Set the brushes.
1241 BOOL fill = FALSE;
1242 BOOL stroke = FALSE;
1243 HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001244
bungeman@google.comb29c8832011-10-10 13:19:10 +00001245 bool xpsTransformsPath = true;
1246 //Transform the geometry.
1247 if (transformRect && xpsTransformsPath) {
1248 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1249 HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1250 if (xpsTransform.get()) {
1251 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1252 "Could not set transform for rect.");
1253 } else {
1254 xpsTransformsPath = false;
1255 }
1256 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001257
bungeman@google.comb29c8832011-10-10 13:19:10 +00001258 //Create the figure.
1259 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1260 {
1261 SkPoint points[4] = {
1262 { r.fLeft, r.fTop },
1263 { r.fLeft, r.fBottom },
1264 { r.fRight, r.fBottom },
1265 { r.fRight, r.fTop },
1266 };
1267 if (!xpsTransformsPath && transformRect) {
1268 d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1269 }
1270 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1271 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001272
bungeman@google.comb29c8832011-10-10 13:19:10 +00001273 //Get the figures of the shaded geometry.
1274 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1275 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1276 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001277
bungeman@google.comb29c8832011-10-10 13:19:10 +00001278 //Add the figure to the shaded geometry figures.
1279 HRVM(shadedFigures->Append(rectFigure.get()),
1280 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001281
bungeman@google.comb29c8832011-10-10 13:19:10 +00001282 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001283
bungeman@google.comb29c8832011-10-10 13:19:10 +00001284 //Add the shaded path to the current visuals.
1285 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1286 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1287 "Could not get current visuals for rect.");
1288 HRVM(currentVisuals->Append(shadedPath.get()),
1289 "Could not add rect to current visuals.");
1290}
1291
1292static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1293 const SkTDArray<BOOL>& segmentStrokes,
1294 const SkTDArray<FLOAT>& segmentData,
1295 BOOL stroke, BOOL fill,
1296 IXpsOMGeometryFigure* figure,
1297 IXpsOMGeometryFigureCollection* figures) {
1298 // Add the segment data to the figure.
1299 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1300 segmentTypes.begin() , segmentData.begin(),
1301 segmentStrokes.begin()),
1302 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001303
bungeman@google.comb29c8832011-10-10 13:19:10 +00001304 // Set the closed and filled properties of the figure.
1305 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1306 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001307
bungeman@google.comb29c8832011-10-10 13:19:10 +00001308 // Add the figure created above to this geometry.
1309 HRM(figures->Append(figure), "Could not add path to geometry.");
1310 return S_OK;
1311}
1312
1313HRESULT SkXPSDevice::addXpsPathGeometry(
1314 IXpsOMGeometryFigureCollection* xpsFigures,
1315 BOOL stroke, BOOL fill, const SkPath& path) {
1316 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1317 SkTDArray<BOOL> segmentStrokes;
1318 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001319
bungeman@google.comb29c8832011-10-10 13:19:10 +00001320 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1321 SkPath::Iter iter(path, true);
1322 SkPoint points[4];
1323 SkPath::Verb verb;
1324 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1325 switch (verb) {
1326 case SkPath::kMove_Verb: {
bsalomon49f085d2014-09-05 13:34:00 -07001327 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001328 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1329 stroke, fill,
1330 xpsFigure.get() , xpsFigures));
1331 xpsFigure.reset();
1332 segmentTypes.rewind();
1333 segmentStrokes.rewind();
1334 segmentData.rewind();
1335 }
1336 // Define the start point.
1337 XPS_POINT startPoint = xps_point(points[0]);
1338 // Create the figure.
1339 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1340 &xpsFigure),
1341 "Could not create path geometry figure.");
1342 break;
1343 }
1344 case SkPath::kLine_Verb:
1345 if (iter.isCloseLine()) break; //ignore the line, auto-closed
1346 segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1347 segmentStrokes.push(stroke);
1348 segmentData.push(SkScalarToFLOAT(points[1].fX));
1349 segmentData.push(SkScalarToFLOAT(points[1].fY));
1350 break;
1351 case SkPath::kQuad_Verb:
1352 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1353 segmentStrokes.push(stroke);
1354 segmentData.push(SkScalarToFLOAT(points[1].fX));
1355 segmentData.push(SkScalarToFLOAT(points[1].fY));
1356 segmentData.push(SkScalarToFLOAT(points[2].fX));
1357 segmentData.push(SkScalarToFLOAT(points[2].fY));
1358 break;
1359 case SkPath::kCubic_Verb:
1360 segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1361 segmentStrokes.push(stroke);
1362 segmentData.push(SkScalarToFLOAT(points[1].fX));
1363 segmentData.push(SkScalarToFLOAT(points[1].fY));
1364 segmentData.push(SkScalarToFLOAT(points[2].fX));
1365 segmentData.push(SkScalarToFLOAT(points[2].fY));
1366 segmentData.push(SkScalarToFLOAT(points[3].fX));
1367 segmentData.push(SkScalarToFLOAT(points[3].fY));
1368 break;
halcanary47ef4d52015-03-03 09:13:09 -08001369 case SkPath::kConic_Verb: {
1370 const SkScalar tol = SK_Scalar1 / 4;
1371 SkAutoConicToQuads converter;
1372 const SkPoint* quads =
1373 converter.computeQuads(points, iter.conicWeight(), tol);
1374 for (int i = 0; i < converter.countQuads(); ++i) {
1375 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1376 segmentStrokes.push(stroke);
1377 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fX));
1378 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fY));
1379 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fX));
1380 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fY));
1381 }
1382 break;
1383 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001384 case SkPath::kClose_Verb:
1385 // we ignore these, and just get the whole segment from
1386 // the corresponding line/quad/cubic verbs
1387 break;
1388 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001389 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001390 break;
1391 }
1392 }
bsalomon49f085d2014-09-05 13:34:00 -07001393 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001394 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1395 stroke, fill,
1396 xpsFigure.get(), xpsFigures));
1397 }
1398 return S_OK;
1399}
1400
bungeman@google.comb29c8832011-10-10 13:19:10 +00001401void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1402 SkMatrix* matrix,
1403 SkVector* ppuScale,
1404 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001405 //This action is in unit space, but the ppm is specified in physical space.
reed80ea19c2015-05-12 10:37:34 -07001406 ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
1407 fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001408
bungeman@google.comb29c8832011-10-10 13:19:10 +00001409 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001410
bungeman@google.comb29c8832011-10-10 13:19:10 +00001411 const SkIRect& irect = clip;
1412 SkRect clipRect = SkRect::MakeLTRB(
1413 SkScalarMul(SkIntToScalar(irect.fLeft), ppuScale->fX),
1414 SkScalarMul(SkIntToScalar(irect.fTop), ppuScale->fY),
1415 SkScalarMul(SkIntToScalar(irect.fRight), ppuScale->fX),
1416 SkScalarMul(SkIntToScalar(irect.fBottom), ppuScale->fY));
1417 clipRect.roundOut(clipIRect);
1418}
1419
1420HRESULT SkXPSDevice::applyMask(const SkDraw& d,
1421 const SkMask& mask,
1422 const SkVector& ppuScale,
1423 IXpsOMPath* shadedPath) {
1424 //Get the geometry object.
1425 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1426 HRM(shadedPath->GetGeometry(&shadedGeometry),
1427 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001428
bungeman@google.comb29c8832011-10-10 13:19:10 +00001429 //Get the figures from the geometry.
1430 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1431 HRM(shadedGeometry->GetFigures(&shadedFigures),
1432 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001433
bungeman@google.comb29c8832011-10-10 13:19:10 +00001434 SkMatrix m;
1435 m.reset();
1436 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1437 SkIntToScalar(mask.fBounds.fTop));
1438 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001439
bungeman@google.comb29c8832011-10-10 13:19:10 +00001440 SkShader::TileMode xy[2];
1441 xy[0] = (SkShader::TileMode)3;
1442 xy[1] = (SkShader::TileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001443
bungeman@google.comb29c8832011-10-10 13:19:10 +00001444 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001445 bm.installMaskPixels(mask);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001446
bungeman@google.comb29c8832011-10-10 13:19:10 +00001447 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1448 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1449 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1450 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001451
bungeman@google.comb29c8832011-10-10 13:19:10 +00001452 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1453 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1454 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1455 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1456 "Could not create mask shaded figure.");
1457 HRM(shadedFigures->Append(shadedFigure.get()),
1458 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001459
bungeman@google.comb29c8832011-10-10 13:19:10 +00001460 HR(this->clip(shadedPath, d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001461
bungeman@google.comb29c8832011-10-10 13:19:10 +00001462 //Add the path to the active visual collection.
1463 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1464 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1465 "Could not get mask current visuals.");
1466 HRM(currentVisuals->Append(shadedPath),
1467 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001468
bungeman@google.comb29c8832011-10-10 13:19:10 +00001469 return S_OK;
1470}
1471
1472HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1473 const SkPaint& shaderPaint,
1474 const SkMatrix& matrix,
1475 BOOL* fill, BOOL* stroke) {
1476 *fill = FALSE;
1477 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001478
bungeman@google.comb29c8832011-10-10 13:19:10 +00001479 const SkPaint::Style style = shaderPaint.getStyle();
1480 const bool hasFill = SkPaint::kFill_Style == style
1481 || SkPaint::kStrokeAndFill_Style == style;
1482 const bool hasStroke = SkPaint::kStroke_Style == style
1483 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001484
bungeman@google.comb29c8832011-10-10 13:19:10 +00001485 //TODO(bungeman): use dictionaries and lookups.
1486 if (hasFill) {
1487 *fill = TRUE;
1488 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1489 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1490 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1491 "Could not set fill for shaded path.");
1492 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001493
bungeman@google.comb29c8832011-10-10 13:19:10 +00001494 if (hasStroke) {
1495 *stroke = TRUE;
1496 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1497 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1498 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1499 "Could not set stroke brush for shaded path.");
1500 HRM(shadedPath->SetStrokeThickness(
1501 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1502 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001503
bungeman@google.comb29c8832011-10-10 13:19:10 +00001504 if (0 == shaderPaint.getStrokeWidth()) {
1505 //XPS hair width is a hack. (XPS Spec 11.6.12).
1506 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1507 HRM(shadedPath->GetStrokeDashes(&dashes),
1508 "Could not set dashes for shaded path.");
1509 XPS_DASH dash;
1510 dash.length = 1.0;
1511 dash.gap = 0.0;
1512 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1513 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1514 "Could not set dash offset for shaded path.");
1515 }
1516 }
1517 return S_OK;
1518}
1519
1520void SkXPSDevice::drawPath(const SkDraw& d,
1521 const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001522 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001523 const SkMatrix* prePathMatrix,
1524 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001525 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1526
bungeman@google.comb29c8832011-10-10 13:19:10 +00001527 // nothing to draw
1528 if (d.fClip->isEmpty() ||
halcanary96fcdcc2015-08-27 07:41:13 -07001529 (paint->getAlpha() == 0 && paint->getXfermode() == nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001530 return;
1531 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001532
bungeman@google.comb29c8832011-10-10 13:19:10 +00001533 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001534 const bool paintHasPathEffect = paint->getPathEffect()
1535 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001536
bungeman@google.comb29c8832011-10-10 13:19:10 +00001537 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1538 SkMatrix matrix = *d.fMatrix;
1539 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1540 if (prePathMatrix) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001541 if (paintHasPathEffect || paint->getRasterizer()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001542 if (!pathIsMutable) {
1543 skeletalPath = &modifiedPath;
1544 pathIsMutable = true;
1545 }
1546 platonicPath.transform(*prePathMatrix, skeletalPath);
1547 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001548 matrix.preConcat(*prePathMatrix);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001549 }
1550 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001551
bungeman@google.comb29c8832011-10-10 13:19:10 +00001552 //Apply path effect [Skeletal-path -> Fillable-path].
1553 SkPath* fillablePath = skeletalPath;
1554 if (paintHasPathEffect) {
1555 if (!pathIsMutable) {
1556 fillablePath = &modifiedPath;
1557 pathIsMutable = true;
1558 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001559 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001560
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001561 SkPaint* writablePaint = paint.writable();
halcanary96fcdcc2015-08-27 07:41:13 -07001562 writablePaint->setPathEffect(nullptr);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001563 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001564 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001565 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001566 writablePaint->setStyle(SkPaint::kStroke_Style);
1567 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001568 }
1569 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001570
bungeman@google.comb29c8832011-10-10 13:19:10 +00001571 //Create the shaded path. This will be the path which is painted.
1572 SkTScopedComPtr<IXpsOMPath> shadedPath;
1573 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1574 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001575
bungeman@google.comb29c8832011-10-10 13:19:10 +00001576 //Create the geometry for the shaded path.
1577 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1578 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1579 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001580
bungeman@google.comb29c8832011-10-10 13:19:10 +00001581 //Add the geometry to the shaded path.
1582 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1583 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001584
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001585 SkRasterizer* rasterizer = paint->getRasterizer();
1586 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001587
1588 //Determine if we will draw or shade and mask.
1589 if (rasterizer || filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001590 if (paint->getStyle() != SkPaint::kFill_Style) {
1591 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001592 }
1593 }
1594
bungeman@google.comb29c8832011-10-10 13:19:10 +00001595 //Set the brushes.
1596 BOOL fill;
1597 BOOL stroke;
1598 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001599 *paint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001600 *d.fMatrix,
1601 &fill,
1602 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001603
bungeman@google.comb29c8832011-10-10 13:19:10 +00001604 //Rasterizer
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001605 if (rasterizer) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001606 SkIRect clipIRect;
1607 SkVector ppuScale;
1608 this->convertToPpm(filter,
1609 &matrix,
1610 &ppuScale,
1611 d.fClip->getBounds(),
1612 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001613
halcanary96fcdcc2015-08-27 07:41:13 -07001614 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001615
bungeman@google.comb29c8832011-10-10 13:19:10 +00001616 //[Fillable-path -> Mask]
1617 SkMask rasteredMask;
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001618 if (rasterizer->rasterize(
bungeman@google.comb29c8832011-10-10 13:19:10 +00001619 *fillablePath,
1620 matrix,
1621 &clipIRect,
1622 filter, //just to compute how much to draw.
1623 &rasteredMask,
1624 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001625
bungeman@google.comb29c8832011-10-10 13:19:10 +00001626 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1627 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001628
bungeman@google.comb29c8832011-10-10 13:19:10 +00001629 //[Mask -> Mask]
1630 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001631 if (filter && filter->filterMask(&filteredMask, *mask, *d.fMatrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001632 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001633 }
1634 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001635
bungeman@google.comb29c8832011-10-10 13:19:10 +00001636 //Draw mask.
1637 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1638 }
1639 return;
1640 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001641
bungeman@google.comb29c8832011-10-10 13:19:10 +00001642 //Mask filter
1643 if (filter) {
1644 SkIRect clipIRect;
1645 SkVector ppuScale;
1646 this->convertToPpm(filter,
1647 &matrix,
1648 &ppuScale,
1649 d.fClip->getBounds(),
1650 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001651
bungeman@google.comb29c8832011-10-10 13:19:10 +00001652 //[Fillable-path -> Pixel-path]
1653 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1654 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001655
halcanary96fcdcc2015-08-27 07:41:13 -07001656 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001657
bungeman@google.comb29c8832011-10-10 13:19:10 +00001658 //[Pixel-path -> Mask]
1659 SkMask rasteredMask;
1660 if (SkDraw::DrawToMask(
1661 *pixelPath,
1662 &clipIRect,
1663 filter, //just to compute how much to draw.
1664 &matrix,
1665 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001666 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001667 paint->getStyle())) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001668
bungeman@google.comb29c8832011-10-10 13:19:10 +00001669 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1670 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001671
bungeman@google.comb29c8832011-10-10 13:19:10 +00001672 //[Mask -> Mask]
1673 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001674 if (filter->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001675 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001676 }
1677 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001678
bungeman@google.comb29c8832011-10-10 13:19:10 +00001679 //Draw mask.
1680 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1681 }
1682 return;
1683 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001684
bungeman@google.comb29c8832011-10-10 13:19:10 +00001685 //Get the figures from the shaded geometry.
1686 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1687 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1688 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001689
bungeman@google.comb29c8832011-10-10 13:19:10 +00001690 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001691
bungeman@google.comb29c8832011-10-10 13:19:10 +00001692 //Set the fill rule.
bungeman76db31a2014-08-25 07:31:53 -07001693 SkPath* xpsCompatiblePath = fillablePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001694 XPS_FILL_RULE xpsFillRule;
bungeman76db31a2014-08-25 07:31:53 -07001695 switch (fillablePath->getFillType()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001696 case SkPath::kWinding_FillType:
1697 xpsFillRule = XPS_FILL_RULE_NONZERO;
1698 break;
1699 case SkPath::kEvenOdd_FillType:
1700 xpsFillRule = XPS_FILL_RULE_EVENODD;
1701 break;
1702 case SkPath::kInverseWinding_FillType: {
bungeman76db31a2014-08-25 07:31:53 -07001703 //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
1704 if (!pathIsMutable) {
1705 xpsCompatiblePath = &modifiedPath;
1706 pathIsMutable = true;
1707 }
1708 if (!Simplify(*fillablePath, xpsCompatiblePath)) {
1709 SkDEBUGF(("Could not simplify inverse winding path."));
1710 return;
1711 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001712 }
bungeman76db31a2014-08-25 07:31:53 -07001713 // The xpsCompatiblePath is noW inverse even odd, so fall through.
bungeman@google.comb29c8832011-10-10 13:19:10 +00001714 case SkPath::kInverseEvenOdd_FillType: {
1715 const SkRect universe = SkRect::MakeLTRB(
1716 0, 0,
1717 this->fCurrentCanvasSize.fWidth,
1718 this->fCurrentCanvasSize.fHeight);
1719 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1720 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1721 HRVM(shadedFigures->Append(addOneFigure.get()),
1722 "Could not add even-odd flip figure to shaded path.");
1723 xpsTransformsPath = false;
1724 xpsFillRule = XPS_FILL_RULE_EVENODD;
1725 break;
1726 }
1727 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001728 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001729 }
1730 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1731 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001732
bungeman@google.comb29c8832011-10-10 13:19:10 +00001733 //Create the XPS transform, if possible.
1734 if (xpsTransformsPath) {
1735 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1736 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001737
bungeman@google.comb29c8832011-10-10 13:19:10 +00001738 if (xpsTransform.get()) {
1739 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1740 "Could not set transform on shaded path.");
1741 } else {
1742 xpsTransformsPath = false;
1743 }
1744 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001745
bungeman76db31a2014-08-25 07:31:53 -07001746 SkPath* devicePath = xpsCompatiblePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001747 if (!xpsTransformsPath) {
1748 //[Fillable-path -> Device-path]
bungeman76db31a2014-08-25 07:31:53 -07001749 devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
1750 xpsCompatiblePath->transform(matrix, devicePath);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001751 }
1752 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1753 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001754
bungeman@google.comb29c8832011-10-10 13:19:10 +00001755 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001756
bungeman@google.comb29c8832011-10-10 13:19:10 +00001757 //Add the path to the active visual collection.
1758 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1759 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1760 "Could not get current visuals for shaded path.");
1761 HRVM(currentVisuals->Append(shadedPath.get()),
1762 "Could not add shaded path to current visuals.");
1763}
1764
1765HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1766 SkPath clipPath;
1767 SkAssertResult(d.fClip->getBoundaryPath(&clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001768
bungeman@google.comb29c8832011-10-10 13:19:10 +00001769 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1770}
1771HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1772 const SkPath& clipPath,
1773 XPS_FILL_RULE fillRule) {
1774 //Create the geometry.
1775 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1776 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1777 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001778
bungeman@google.comb29c8832011-10-10 13:19:10 +00001779 //Get the figure collection of the geometry.
1780 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1781 HRM(clipGeometry->GetFigures(&clipFigures),
1782 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001783
bungeman@google.comb29c8832011-10-10 13:19:10 +00001784 //Create the figures into the geometry.
1785 HR(this->addXpsPathGeometry(
1786 clipFigures.get(),
1787 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001788
bungeman@google.comb29c8832011-10-10 13:19:10 +00001789 HRM(clipGeometry->SetFillRule(fillRule),
1790 "Could not set fill rule.");
1791 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1792 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001793
bungeman@google.comb29c8832011-10-10 13:19:10 +00001794 return S_OK;
1795}
1796
1797void SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001798 const SkMatrix& matrix, const SkPaint& paint) {
1799 if (d.fClip->isEmpty()) {
1800 return;
1801 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001802
bungeman@google.comb29c8832011-10-10 13:19:10 +00001803 SkIRect srcRect;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001804 srcRect.set(0, 0, bitmap.width(), bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001805
bungeman@google.comb29c8832011-10-10 13:19:10 +00001806 //Create the new shaded path.
1807 SkTScopedComPtr<IXpsOMPath> shadedPath;
1808 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1809 "Could not create path for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001810
bungeman@google.comb29c8832011-10-10 13:19:10 +00001811 //Create the shaded geometry.
1812 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1813 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1814 "Could not create geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001815
bungeman@google.comb29c8832011-10-10 13:19:10 +00001816 //Add the shaded geometry to the shaded path.
1817 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1818 "Could not set the geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001819
bungeman@google.comb29c8832011-10-10 13:19:10 +00001820 //Get the shaded figures from the shaded geometry.
1821 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1822 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1823 "Could not get the figures for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001824
bungeman@google.comb29c8832011-10-10 13:19:10 +00001825 SkMatrix transform = matrix;
1826 transform.postConcat(*d.fMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001827
bungeman@google.comb29c8832011-10-10 13:19:10 +00001828 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1829 HRV(this->createXpsTransform(transform, &xpsTransform));
1830 if (xpsTransform.get()) {
1831 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1832 "Could not set transform for bitmap.");
1833 } else {
1834 //TODO: perspective that bitmap!
1835 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001836
bungeman@google.comb29c8832011-10-10 13:19:10 +00001837 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
bsalomon49f085d2014-09-05 13:34:00 -07001838 if (xpsTransform.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001839 const SkShader::TileMode xy[2] = {
1840 SkShader::kClamp_TileMode,
1841 SkShader::kClamp_TileMode,
1842 };
1843 SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001844 HRV(this->createXpsImageBrush(bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001845 transform,
1846 xy,
1847 paint.getAlpha(),
1848 &xpsImageBrush));
1849 HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
1850 "Could not set bitmap brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001851
bungeman@google.comb29c8832011-10-10 13:19:10 +00001852 const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
1853 SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1854 HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
1855 }
1856 HRVM(shadedFigures->Append(rectFigure.get()),
1857 "Could not add bitmap figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001858
bungeman@google.comb29c8832011-10-10 13:19:10 +00001859 //Get the current visual collection and add the shaded path to it.
1860 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1861 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1862 "Could not get current visuals for bitmap");
1863 HRVM(currentVisuals->Append(shadedPath.get()),
1864 "Could not add bitmap to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001865
bungeman@google.comb29c8832011-10-10 13:19:10 +00001866 HRV(this->clip(shadedPath.get(), d));
1867}
1868
1869void SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
1870 int x, int y,
1871 const SkPaint& paint) {
1872 //TODO: override this for XPS
1873 SkDEBUGF(("XPS drawSprite not yet implemented."));
1874}
1875
1876HRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
1877 TypefaceUse** typefaceUse) {
reed@google.com398de9a2013-03-21 21:43:51 +00001878 SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001879
bungeman@google.comb29c8832011-10-10 13:19:10 +00001880 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00001881 const SkFontID typefaceID = typeface->uniqueID();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001882 if (!this->fTypefaces.empty()) {
1883 TypefaceUse* current = &this->fTypefaces.front();
1884 const TypefaceUse* last = &this->fTypefaces.back();
1885 for (; current <= last; ++current) {
1886 if (current->typefaceId == typefaceID) {
1887 *typefaceUse = current;
1888 return S_OK;
1889 }
1890 }
1891 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001892
bungeman@google.comb29c8832011-10-10 13:19:10 +00001893 //TODO: create glyph only fonts
1894 //and let the host deal with what kind of font we're looking at.
1895 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001896
bungeman@google.comb29c8832011-10-10 13:19:10 +00001897 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001898 int ttcIndex;
1899 SkStream* fontData = typeface->openStream(&ttcIndex);
bungeman@google.com635091f2013-10-01 15:03:18 +00001900 //TODO: cannot handle FON fonts.
bungeman@google.com8cddc8d2012-01-03 22:36:33 +00001901 HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001902 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001903
bungeman@google.comb29c8832011-10-10 13:19:10 +00001904 const size_t size =
1905 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1906 wchar_t buffer[size];
1907 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -04001908 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001909 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001910
bungeman@google.comb29c8832011-10-10 13:19:10 +00001911 SkTScopedComPtr<IOpcPartUri> partUri;
1912 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1913 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001914
bungeman@google.comb29c8832011-10-10 13:19:10 +00001915 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1916 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1917 embedding,
1918 partUri.get(),
1919 FALSE,
1920 &xpsFontResource),
1921 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001922
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001923 //TODO: change openStream to return -1 for non-ttc, get rid of this.
1924 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1925 bool isTTC = (data &&
1926 fontData->getLength() >= sizeof(SkTTCFHeader) &&
1927 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1928
bungeman@google.comb29c8832011-10-10 13:19:10 +00001929 TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
1930 newTypefaceUse.typefaceId = typefaceID;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001931 newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001932 newTypefaceUse.fontData = fontData;
1933 newTypefaceUse.xpsFont = xpsFontResource.release();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001934
robertphillips8e0c1502015-07-07 10:28:43 -07001935 SkAutoGlyphCache agc(paint, &this->surfaceProps(), &SkMatrix::I());
bungeman@google.comb29c8832011-10-10 13:19:10 +00001936 SkGlyphCache* glyphCache = agc.getCache();
1937 unsigned int glyphCount = glyphCache->getGlyphCount();
1938 newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001939
bungeman@google.comb29c8832011-10-10 13:19:10 +00001940 *typefaceUse = &newTypefaceUse;
1941 return S_OK;
1942}
1943
1944HRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
1945 IXpsOMObjectFactory* xpsFactory,
1946 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001947 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001948 LPCWSTR text,
1949 XPS_GLYPH_INDEX* xpsGlyphs,
1950 UINT32 xpsGlyphsLen,
1951 XPS_POINT *origin,
1952 FLOAT fontSize,
1953 XPS_STYLE_SIMULATION sims,
1954 const SkMatrix& transform,
1955 const SkPaint& paint) {
1956 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001957 HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
1958 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001959
bungeman@google.comb29c8832011-10-10 13:19:10 +00001960 //XPS uses affine transformations for everything...
1961 //...except positioning text.
1962 bool useCanvasForClip;
1963 if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
1964 origin->x += SkScalarToFLOAT(transform.getTranslateX());
1965 origin->y += SkScalarToFLOAT(transform.getTranslateY());
1966 useCanvasForClip = false;
1967 } else {
1968 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1969 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1970 if (xpsMatrixToUse.get()) {
1971 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1972 "Could not set transform matrix.");
1973 useCanvasForClip = true;
1974 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00001975 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001976 useCanvasForClip = false;
1977 }
1978 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001979
bungeman@google.comb29c8832011-10-10 13:19:10 +00001980 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1981 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001982
bsalomon49f085d2014-09-05 13:34:00 -07001983 if (text) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001984 HRM(glyphsEditor->SetUnicodeString(text),
1985 "Could not set unicode string.");
1986 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001987
bsalomon49f085d2014-09-05 13:34:00 -07001988 if (xpsGlyphs) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001989 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
1990 "Could not set glyphs.");
1991 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001992
bungeman@google.comb29c8832011-10-10 13:19:10 +00001993 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001994
bungeman@google.comb29c8832011-10-10 13:19:10 +00001995 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
1996 HR(this->createXpsBrush(
1997 paint,
1998 &xpsFillBrush,
halcanary96fcdcc2015-08-27 07:41:13 -07001999 useCanvasForClip ? nullptr : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002000
bungeman@google.comb29c8832011-10-10 13:19:10 +00002001 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
2002 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002003
bungeman@google.comb29c8832011-10-10 13:19:10 +00002004 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002005
bungeman@google.comb29c8832011-10-10 13:19:10 +00002006 HRM(glyphs->SetFontRenderingEmSize(fontSize),
2007 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002008
bungeman@google.comb29c8832011-10-10 13:19:10 +00002009 HRM(glyphs->SetStyleSimulations(sims),
2010 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002011
bungeman@google.comb29c8832011-10-10 13:19:10 +00002012 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2013 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002014
bungeman@google.comb29c8832011-10-10 13:19:10 +00002015 if (!useCanvasForClip) {
2016 HR(this->clip(glyphs.get(), d));
2017 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2018 } else {
2019 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2020 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2021 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002022
bungeman@google.comb29c8832011-10-10 13:19:10 +00002023 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2024 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2025 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002026
bungeman@google.comb29c8832011-10-10 13:19:10 +00002027 HRM(glyphCanvasVisuals->Append(glyphs.get()),
2028 "Could not add glyphs to page.");
2029 HR(this->clip(glyphCanvas.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002030
bungeman@google.comb29c8832011-10-10 13:19:10 +00002031 HRM(visuals->Append(glyphCanvas.get()),
2032 "Could not add glyph canvas to page.");
2033 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002034
bungeman@google.comb29c8832011-10-10 13:19:10 +00002035 return S_OK;
2036}
2037
herbbda26432015-11-24 08:37:01 -08002038static int num_glyph_guess(SkPaint::TextEncoding encoding, const void* text, size_t byteLength) {
2039 switch (encoding) {
2040 case SkPaint::kUTF8_TextEncoding:
2041 return SkUTF8_CountUnichars(static_cast<const char *>(text), byteLength);
2042 case SkPaint::kUTF16_TextEncoding:
2043 return SkUTF16_CountUnichars(static_cast<const uint16_t *>(text), SkToInt(byteLength));
2044 case SkPaint::kGlyphID_TextEncoding:
2045 return SkToInt(byteLength / 2);
2046 default:
2047 SK_ALWAYSBREAK(true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002048 }
herbbda26432015-11-24 08:37:01 -08002049 return 0;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002050}
2051
2052static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2053 const SkPaint::Style style = paint.getStyle();
2054 return matrix.hasPerspective()
2055 || SkPaint::kStroke_Style == style
2056 || SkPaint::kStrokeAndFill_Style == style
2057 || paint.getMaskFilter()
2058 || paint.getRasterizer()
2059 ;
2060}
2061
herbbda26432015-11-24 08:37:01 -08002062typedef SkTDArray<XPS_GLYPH_INDEX> GlyphRun;
2063
2064class ProcessOneGlyph {
2065public:
2066 ProcessOneGlyph(FLOAT centemPerUnit, SkBitSet* glyphUse, GlyphRun* xpsGlyphs)
2067 : fCentemPerUnit(centemPerUnit)
2068 , fGlyphUse(glyphUse)
2069 , fXpsGlyphs(xpsGlyphs) { }
2070
2071 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint) {
2072 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
2073
2074 SkScalar x = position.fX;
2075 SkScalar y = position.fY;
2076
2077 XPS_GLYPH_INDEX* xpsGlyph = fXpsGlyphs->append();
2078 uint16_t glyphID = glyph.getGlyphID();
2079 fGlyphUse->setBit(glyphID, true);
2080 xpsGlyph->index = glyphID;
2081 if (1 == fXpsGlyphs->count()) {
2082 xpsGlyph->advanceWidth = 0.0f;
2083 xpsGlyph->horizontalOffset = SkScalarToFloat(x) * fCentemPerUnit;
2084 xpsGlyph->verticalOffset = SkScalarToFloat(y) * -fCentemPerUnit;
2085 }
2086 else {
2087 const XPS_GLYPH_INDEX& first = (*fXpsGlyphs)[0];
2088 xpsGlyph->advanceWidth = 0.0f;
2089 xpsGlyph->horizontalOffset = (SkScalarToFloat(x) * fCentemPerUnit)
2090 - first.horizontalOffset;
2091 xpsGlyph->verticalOffset = (SkScalarToFloat(y) * -fCentemPerUnit)
2092 - first.verticalOffset;
2093 }
2094 }
2095
2096private:
2097 /** [in] Advance width and offsets for glyphs measured in
2098 hundredths of the font em size (XPS Spec 5.1.3). */
2099 const FLOAT fCentemPerUnit;
2100 /** [in,out] The accumulated glyphs used in the current typeface. */
2101 SkBitSet* const fGlyphUse;
2102 /** [out] The glyphs to draw. */
2103 GlyphRun* const fXpsGlyphs;
2104};
2105
bungeman@google.comb29c8832011-10-10 13:19:10 +00002106void SkXPSDevice::drawText(const SkDraw& d,
2107 const void* text, size_t byteLen,
2108 SkScalar x, SkScalar y,
2109 const SkPaint& paint) {
2110 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002111
bungeman@google.comb29c8832011-10-10 13:19:10 +00002112 if (text_must_be_pathed(paint, *d.fMatrix)) {
2113 SkPath path;
2114 paint.getTextPath(text, byteLen, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002115 this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002116 //TODO: add automation "text"
2117 return;
2118 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002119
bungeman@google.comb29c8832011-10-10 13:19:10 +00002120 TypefaceUse* typeface;
2121 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002122
herbbda26432015-11-24 08:37:01 -08002123 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002124
herbbda26432015-11-24 08:37:01 -08002125 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2126 SkGlyphCache* cache = autoCache.getCache();
reed@google.com089130c2011-12-12 21:52:18 +00002127
herbbda26432015-11-24 08:37:01 -08002128 // Advance width and offsets for glyphs measured in hundredths of the font em size
2129 // (XPS Spec 5.1.3).
2130 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2131 GlyphRun xpsGlyphs;
2132 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2133 static_cast<const char*>(text), byteLen));
2134
2135 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2136
2137 SkFindAndPlaceGlyph::ProcessText(
2138 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2139 SkPoint{ x, y }, matrix, paint.getTextAlign(), cache, processOneGlyph);
2140
2141 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002142 return;
2143 }
2144
bungeman@google.comb29c8832011-10-10 13:19:10 +00002145 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002146 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2147 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002148 };
herbbda26432015-11-24 08:37:01 -08002149 xpsGlyphs[0].horizontalOffset = 0.0f;
2150 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002151
bungeman@google.comb29c8832011-10-10 13:19:10 +00002152 HRV(AddGlyphs(d,
2153 this->fXpsFactory.get(),
2154 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002155 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002156 nullptr,
herbbda26432015-11-24 08:37:01 -08002157 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002158 &origin,
2159 SkScalarToFLOAT(paint.getTextSize()),
2160 XPS_STYLE_SIMULATION_NONE,
2161 *d.fMatrix,
2162 paint));
2163}
2164
2165void SkXPSDevice::drawPosText(const SkDraw& d,
2166 const void* text, size_t byteLen,
fmalita05c4a432014-09-29 06:29:53 -07002167 const SkScalar pos[], int scalarsPerPos,
2168 const SkPoint& offset, const SkPaint& paint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002169 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002170
bungeman@google.comb29c8832011-10-10 13:19:10 +00002171 if (text_must_be_pathed(paint, *d.fMatrix)) {
2172 SkPath path;
2173 //TODO: make this work, Draw currently does not handle as well.
2174 //paint.getTextPath(text, byteLength, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002175 //this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002176 //TODO: add automation "text"
2177 return;
2178 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002179
bungeman@google.comb29c8832011-10-10 13:19:10 +00002180 TypefaceUse* typeface;
2181 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002182
herbbda26432015-11-24 08:37:01 -08002183 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002184
herbbda26432015-11-24 08:37:01 -08002185 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2186 SkGlyphCache* cache = autoCache.getCache();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002187
herbbda26432015-11-24 08:37:01 -08002188 // Advance width and offsets for glyphs measured in hundredths of the font em size
2189 // (XPS Spec 5.1.3).
2190 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2191 GlyphRun xpsGlyphs;
2192 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2193 static_cast<const char*>(text), byteLen));
2194
2195 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2196
2197 SkFindAndPlaceGlyph::ProcessPosText(
2198 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2199 offset, matrix, pos, scalarsPerPos, paint.getTextAlign(), cache, processOneGlyph);
2200
2201 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002202 return;
2203 }
2204
bungeman@google.comb29c8832011-10-10 13:19:10 +00002205 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002206 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2207 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002208 };
herbbda26432015-11-24 08:37:01 -08002209 xpsGlyphs[0].horizontalOffset = 0.0f;
2210 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002211
bungeman@google.comb29c8832011-10-10 13:19:10 +00002212 HRV(AddGlyphs(d,
2213 this->fXpsFactory.get(),
2214 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002215 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002216 nullptr,
herbbda26432015-11-24 08:37:01 -08002217 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002218 &origin,
2219 SkScalarToFLOAT(paint.getTextSize()),
2220 XPS_STYLE_SIMULATION_NONE,
2221 *d.fMatrix,
2222 paint));
2223}
2224
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002225void SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002226 int x, int y,
2227 const SkPaint&) {
2228 SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002229
bungeman@google.comb29c8832011-10-10 13:19:10 +00002230 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2231 XPS_MATRIX rawTransform = {
2232 1.0f,
2233 0.0f,
2234 0.0f,
2235 1.0f,
2236 static_cast<FLOAT>(x),
2237 static_cast<FLOAT>(y),
2238 };
2239 HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2240 "Could not create layer transform.");
2241 HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2242 "Could not set layer transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002243
bungeman@google.comb29c8832011-10-10 13:19:10 +00002244 //Get the current visual collection and add the layer to it.
2245 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2246 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2247 "Could not get current visuals for layer.");
2248 HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2249 "Could not add layer to current visuals.");
2250}
2251
reed76033be2015-03-14 10:54:31 -07002252SkBaseDevice* SkXPSDevice::onCreateDevice(const CreateInfo& info, const SkPaint*) {
bungeman@google.com635091f2013-10-01 15:03:18 +00002253//Conditional for bug compatibility with PDF device.
2254#if 0
fmalita6987dca2014-11-13 08:33:37 -08002255 if (SkBaseDevice::kGeneral_Usage == info.fUsage) {
halcanary96fcdcc2015-08-27 07:41:13 -07002256 return nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002257 SK_CRASH();
2258 //To what stream do we write?
2259 //SkXPSDevice* dev = new SkXPSDevice(this);
2260 //SkSize s = SkSize::Make(width, height);
2261 //dev->BeginCanvas(s, s, SkMatrix::I());
2262 //return dev;
2263 }
bungeman@google.com635091f2013-10-01 15:03:18 +00002264#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +00002265 return new SkXPSDevice(this->fXpsFactory.get());
2266}
2267
mtklein1ee76512015-11-02 10:20:27 -08002268#endif//defined(SK_BUILD_FOR_WIN32)