blob: eb983fc0d73bea670487a35f8a877fa2ddcd20b7 [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>
halcanarybfa92752016-05-31 11:23:42 -070021#include <limits>
bungeman@google.comb29c8832011-10-10 13:19:10 +000022
23#include "SkColor.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000024#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"
reeda4393342016-03-18 11:22:57 -070035#include "SkPathEffect.h"
Ben Wagnerda5a1b82014-08-22 15:07:06 -040036#include "SkPathOps.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000037#include "SkPoint.h"
reed1e7f5e72016-04-27 07:49:17 -070038#include "SkRasterClip.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000039#include "SkRasterizer.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000040#include "SkSFNTHeader.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000041#include "SkShader.h"
42#include "SkSize.h"
43#include "SkStream.h"
44#include "SkTDArray.h"
45#include "SkTLazy.h"
46#include "SkTScopedComPtr.h"
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000047#include "SkTTCFHeader.h"
reed@google.com398de9a2013-03-21 21:43:51 +000048#include "SkTypefacePriv.h"
bungeman@google.comb29c8832011-10-10 13:19:10 +000049#include "SkUtils.h"
50#include "SkXPSDevice.h"
51
52//Windows defines a FLOAT type,
53//make it clear when converting a scalar that this is what is wanted.
54#define SkScalarToFLOAT(n) SkScalarToFloat(n)
55
Ben Wagnerda5a1b82014-08-22 15:07:06 -040056//Dummy representation of a GUID from createId.
bungeman@google.comb29c8832011-10-10 13:19:10 +000057#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
halcanary96fcdcc2015-08-27 07:41:13 -070058//Length of GUID representation from createId, including nullptr terminator.
bungeman@google.comb29c8832011-10-10 13:19:10 +000059#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
60
61/**
62 Formats a GUID and places it into buffer.
63 buffer should have space for at least GUID_ID_LEN wide characters.
64 The string will always be wchar null terminated.
65 XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
66 @return -1 if there was an error, > 0 if success.
67 */
68static int format_guid(const GUID& guid,
69 wchar_t* buffer, size_t bufferSize,
70 wchar_t sep = '-') {
71 SkASSERT(bufferSize >= GUID_ID_LEN);
72 return swprintf_s(buffer,
73 bufferSize,
74 L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
75 guid.Data1,
76 sep,
77 guid.Data2,
78 sep,
79 guid.Data3,
80 sep,
81 guid.Data4[0],
82 guid.Data4[1],
83 sep,
84 guid.Data4[2],
85 guid.Data4[3],
86 guid.Data4[4],
87 guid.Data4[5],
88 guid.Data4[6],
89 guid.Data4[7]);
90}
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +000091
Ben Wagnerda5a1b82014-08-22 15:07:06 -040092HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000093 GUID guid = {};
Ben Wagnerda5a1b82014-08-22 15:07:06 -040094#ifdef SK_XPS_USE_DETERMINISTIC_IDS
95 guid.Data1 = fNextId++;
96 // The following make this a valid Type4 UUID.
97 guid.Data3 = 0x4000;
98 guid.Data4[0] = 0x80;
99#else
bungeman@google.comb29c8832011-10-10 13:19:10 +0000100 HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400101#endif
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000102
bungeman@google.comb29c8832011-10-10 13:19:10 +0000103 if (format_guid(guid, buffer, bufferSize, sep) == -1) {
104 HRM(E_UNEXPECTED, "Could not format GUID into id.");
105 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000106
bungeman@google.comb29c8832011-10-10 13:19:10 +0000107 return S_OK;
108}
109
110static SkBitmap make_fake_bitmap(int width, int height) {
111 SkBitmap bitmap;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000112 bitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000113 return bitmap;
114}
115
reed@google.com900ecf22014-02-20 20:55:37 +0000116// TODO: should inherit from SkBaseDevice instead of SkBitmapDevice...
bungeman@google.comb29c8832011-10-10 13:19:10 +0000117SkXPSDevice::SkXPSDevice()
robertphillips9a53fd72015-06-22 09:46:59 -0700118 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
bungeman@google.comb29c8832011-10-10 13:19:10 +0000119 , fCurrentPage(0) {
120}
121
robertphillips9a53fd72015-06-22 09:46:59 -0700122SkXPSDevice::SkXPSDevice(IXpsOMObjectFactory* xpsFactory)
123 : INHERITED(make_fake_bitmap(10000, 10000), SkSurfaceProps(0, kUnknown_SkPixelGeometry))
124 , fCurrentPage(0) {
125
126 HRVM(CoCreateInstance(
127 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700128 nullptr,
robertphillips9a53fd72015-06-22 09:46:59 -0700129 CLSCTX_INPROC_SERVER,
130 IID_PPV_ARGS(&this->fXpsFactory)),
131 "Could not create factory for layer.");
132
133 HRVM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
134 "Could not create canvas for layer.");
135}
136
bungeman@google.comb29c8832011-10-10 13:19:10 +0000137SkXPSDevice::~SkXPSDevice() {
138}
139
140SkXPSDevice::TypefaceUse::TypefaceUse()
141 : typefaceId(0xffffffff)
halcanary96fcdcc2015-08-27 07:41:13 -0700142 , fontData(nullptr)
143 , xpsFont(nullptr)
144 , glyphsUsed(nullptr) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000145}
146
147SkXPSDevice::TypefaceUse::~TypefaceUse() {
148 //xpsFont owns fontData ref
149 this->xpsFont->Release();
150 delete this->glyphsUsed;
151}
152
153bool SkXPSDevice::beginPortfolio(SkWStream* outputStream) {
154 if (!this->fAutoCo.succeeded()) return false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000155
bungeman@google.comb29c8832011-10-10 13:19:10 +0000156 //Create XPS Factory.
157 HRBM(CoCreateInstance(
158 CLSID_XpsOMObjectFactory,
halcanary96fcdcc2015-08-27 07:41:13 -0700159 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000160 CLSCTX_INPROC_SERVER,
161 IID_PPV_ARGS(&this->fXpsFactory)),
162 "Could not create XPS factory.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000163
bungeman@google.comb29c8832011-10-10 13:19:10 +0000164 HRBM(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream),
165 "Could not convert SkStream to IStream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000166
bungeman@google.comb29c8832011-10-10 13:19:10 +0000167 return true;
168}
169
170bool SkXPSDevice::beginSheet(
171 const SkVector& unitsPerMeter,
172 const SkVector& pixelsPerMeter,
173 const SkSize& trimSize,
174 const SkRect* mediaBox,
175 const SkRect* bleedBox,
176 const SkRect* artBox,
177 const SkRect* cropBox) {
178 ++this->fCurrentPage;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179
bungeman@google.comb29c8832011-10-10 13:19:10 +0000180 //For simplicity, just write everything out in geometry units,
181 //then have a base canvas do the scale to physical units.
182 this->fCurrentCanvasSize = trimSize;
183 this->fCurrentUnitsPerMeter = unitsPerMeter;
184 this->fCurrentPixelsPerMeter = pixelsPerMeter;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000185
bungeman@google.comb29c8832011-10-10 13:19:10 +0000186 this->fCurrentXpsCanvas.reset();
187 HRBM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
188 "Could not create base canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000189
bungeman@google.comb29c8832011-10-10 13:19:10 +0000190 return true;
191}
192
halcanarybfa92752016-05-31 11:23:42 -0700193template <typename T> static constexpr size_t sk_digits_in() {
194 return static_cast<size_t>(std::numeric_limits<T>::digits10 + 1);
195}
196
bungeman@google.comb29c8832011-10-10 13:19:10 +0000197HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
198 const unsigned int pageNum,
199 IXpsOMImageResource** image) {
200 SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
201 HRM(CoCreateInstance(
202 CLSID_XpsOMThumbnailGenerator,
halcanary96fcdcc2015-08-27 07:41:13 -0700203 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000204 CLSCTX_INPROC_SERVER,
205 IID_PPV_ARGS(&thumbnailGenerator)),
206 "Could not create thumbnail generator.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000207
bungeman@google.comb29c8832011-10-10 13:19:10 +0000208 SkTScopedComPtr<IOpcPartUri> partUri;
halcanarybfa92752016-05-31 11:23:42 -0700209 constexpr size_t size = SkTMax(
210 SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + sk_digits_in<decltype(pageNum)>(),
211 SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png"));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000212 wchar_t buffer[size];
213 if (pageNum > 0) {
214 swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
215 } else {
216 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400217 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000218 swprintf_s(buffer, size, L"/Metadata/%s.png", id);
219 }
220 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
221 "Could not create thumbnail part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000222
bungeman@google.comb29c8832011-10-10 13:19:10 +0000223 HRM(thumbnailGenerator->GenerateThumbnail(page,
224 XPS_IMAGE_TYPE_PNG,
225 XPS_THUMBNAIL_SIZE_LARGE,
226 partUri.get(),
227 image),
228 "Could not generate thumbnail.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000229
bungeman@google.comb29c8832011-10-10 13:19:10 +0000230 return S_OK;
231}
232
233HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
234 IXpsOMPage** page) {
halcanarybfa92752016-05-31 11:23:42 -0700235 constexpr size_t size =
236 SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
237 + sk_digits_in<decltype(fCurrentPage)>();
bungeman@google.comb29c8832011-10-10 13:19:10 +0000238 wchar_t buffer[size];
239 swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
240 this->fCurrentPage);
241 SkTScopedComPtr<IOpcPartUri> partUri;
242 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
243 "Could not create page part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000244
bungeman@google.comb29c8832011-10-10 13:19:10 +0000245 //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
246 HRM(this->fXpsFactory->CreatePage(&pageSize,
247 L"und",
248 partUri.get(),
249 page),
250 "Could not create page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000251
bungeman@google.comb29c8832011-10-10 13:19:10 +0000252 return S_OK;
253}
254
255HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
256 //Create package writer.
257 {
258 SkTScopedComPtr<IOpcPartUri> partUri;
259 HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
260 &partUri),
261 "Could not create document sequence part uri.");
262 HRM(this->fXpsFactory->CreatePackageWriterOnStream(
263 this->fOutputStream.get(),
264 TRUE,
265 XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
266 partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700267 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000268 image,
halcanary96fcdcc2015-08-27 07:41:13 -0700269 nullptr,
270 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000271 &this->fPackageWriter),
272 "Could not create package writer.");
273 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000274
bungeman@google.comb29c8832011-10-10 13:19:10 +0000275 //Begin the lone document.
276 {
277 SkTScopedComPtr<IOpcPartUri> partUri;
278 HRM(this->fXpsFactory->CreatePartUri(
279 L"/Documents/1/FixedDocument.fdoc",
280 &partUri),
281 "Could not create fixed document part uri.");
282 HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
halcanary96fcdcc2015-08-27 07:41:13 -0700283 nullptr,
284 nullptr,
285 nullptr,
286 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000287 "Could not start document.");
288 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000289
bungeman@google.comb29c8832011-10-10 13:19:10 +0000290 return S_OK;
291}
292
293bool SkXPSDevice::endSheet() {
294 //XPS is fixed at 96dpi (XPS Spec 11.1).
295 static const float xpsDPI = 96.0f;
296 static const float inchesPerMeter = 10000.0f / 254.0f;
297 static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
298 const float scaleX = targetUnitsPerMeter
299 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
300 const float scaleY = targetUnitsPerMeter
301 / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000302
bungeman@google.comb29c8832011-10-10 13:19:10 +0000303 //Create the scale canvas.
304 SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
305 HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
306 "Could not create scale canvas.");
307 SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
308 HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
309 "Could not get scale canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000310
bungeman@google.comb29c8832011-10-10 13:19:10 +0000311 SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
312 XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
313 HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
314 "Could not create geometry to physical transform.");
315 HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
316 "Could not set transform on scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000317
bungeman@google.comb29c8832011-10-10 13:19:10 +0000318 //Add the content canvas to the scale canvas.
319 HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
320 "Could not add base canvas to scale canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000321
bungeman@google.comb29c8832011-10-10 13:19:10 +0000322 //Create the page.
323 XPS_SIZE pageSize = {
324 SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
325 SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
326 };
327 SkTScopedComPtr<IXpsOMPage> page;
328 HRB(this->createXpsPage(pageSize, &page));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000329
bungeman@google.comb29c8832011-10-10 13:19:10 +0000330 SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
331 HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000332
bungeman@google.comb29c8832011-10-10 13:19:10 +0000333 //Add the scale canvas to the page.
334 HRBM(pageVisuals->Append(scaleCanvas.get()),
335 "Could not add scale canvas to page.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000336
bungeman@google.comb29c8832011-10-10 13:19:10 +0000337 //Create the package writer if it hasn't been created yet.
halcanary96fcdcc2015-08-27 07:41:13 -0700338 if (nullptr == this->fPackageWriter.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000339 SkTScopedComPtr<IXpsOMImageResource> image;
340 //Ignore return, thumbnail is completely optional.
341 this->createXpsThumbnail(page.get(), 0, &image);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000342
bungeman@google.comb29c8832011-10-10 13:19:10 +0000343 HRB(this->initXpsDocumentWriter(image.get()));
344 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000345
bungeman@google.comb29c8832011-10-10 13:19:10 +0000346 HRBM(this->fPackageWriter->AddPage(page.get(),
347 &pageSize,
halcanary96fcdcc2015-08-27 07:41:13 -0700348 nullptr,
349 nullptr,
350 nullptr,
351 nullptr),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000352 "Could not write the page.");
353 this->fCurrentXpsCanvas.reset();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000354
bungeman@google.comb29c8832011-10-10 13:19:10 +0000355 return true;
356}
357
358static HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
359 //CreateFontPackage wants unsigned short.
360 //Microsoft, Y U NO stdint.h?
361 SkTDArray<unsigned short> keepList;
362 current->glyphsUsed->exportTo(&keepList);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000363
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000364 int ttcCount = (current->ttcIndex + 1);
365
bungeman@google.comb29c8832011-10-10 13:19:10 +0000366 //The following are declared with the types required by CreateFontPackage.
halcanary96fcdcc2015-08-27 07:41:13 -0700367 unsigned char *fontPackageBufferRaw = nullptr;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000368 unsigned long fontPackageBufferSize;
369 unsigned long bytesWritten;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000370 unsigned long result = CreateFontPackage(
371 (unsigned char *) current->fontData->getMemoryBase(),
372 (unsigned long) current->fontData->getLength(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000373 &fontPackageBufferRaw,
374 &fontPackageBufferSize,
375 &bytesWritten,
376 TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
377 current->ttcIndex,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000378 TTFCFP_SUBSET,
379 0,
380 0,
381 0,
382 keepList.begin(),
383 keepList.count(),
384 sk_malloc_throw,
385 sk_realloc_throw,
386 sk_free,
halcanary96fcdcc2015-08-27 07:41:13 -0700387 nullptr);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000388 SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
bungeman@google.comb29c8832011-10-10 13:19:10 +0000389 if (result != NO_ERROR) {
390 SkDEBUGF(("CreateFontPackage Error %lu", result));
391 return E_UNEXPECTED;
392 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000393
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000394 // If it was originally a ttc, keep it a ttc.
395 // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
396 size_t extra;
397 if (ttcCount > 0) {
398 // Create space for a ttc header.
399 extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
400 fontPackageBuffer.realloc(bytesWritten + extra);
401 //overlap is certain, use memmove
402 memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
403
404 // Write the ttc header.
405 SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
406 ttcfHeader->ttcTag = SkTTCFHeader::TAG;
407 ttcfHeader->version = SkTTCFHeader::version_1;
408 ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
409 SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
410 for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
bsalomon0aa5cea2014-12-15 09:13:35 -0800411 *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000412 }
413
414 // Fix up offsets in sfnt table entries.
415 SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
416 int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
417 SkSFNTHeader::TableDirectoryEntry* tableDirectory =
418 SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
419 for (int i = 0; i < numTables; ++i, ++tableDirectory) {
420 tableDirectory->offset = SkEndian_SwapBE32(
bsalomon0aa5cea2014-12-15 09:13:35 -0800421 SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000422 }
423 } else {
424 extra = 0;
425 fontPackageBuffer.realloc(bytesWritten);
426 }
427
scroggoa1193e42015-01-21 12:09:53 -0800428 SkAutoTDelete<SkMemoryStream> newStream(new SkMemoryStream());
mtklein18300a32016-03-16 13:53:35 -0700429 newStream->setMemoryOwned(fontPackageBuffer.release(), bytesWritten + extra);
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +0000430
bungeman@google.comb29c8832011-10-10 13:19:10 +0000431 SkTScopedComPtr<IStream> newIStream;
mtklein18300a32016-03-16 13:53:35 -0700432 SkIStream::CreateFromSkStream(newStream.release(), true, &newIStream);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000433
bungeman@google.comb29c8832011-10-10 13:19:10 +0000434 XPS_FONT_EMBEDDING embedding;
435 HRM(current->xpsFont->GetEmbeddingOption(&embedding),
436 "Could not get embedding option from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000437
bungeman@google.comb29c8832011-10-10 13:19:10 +0000438 SkTScopedComPtr<IOpcPartUri> partUri;
439 HRM(current->xpsFont->GetPartName(&partUri),
440 "Could not get part uri from font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000441
bungeman@google.comb29c8832011-10-10 13:19:10 +0000442 HRM(current->xpsFont->SetContent(
443 newIStream.get(),
444 embedding,
445 partUri.get()),
446 "Could not set new stream for subsetted font.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000447
bungeman@google.comb29c8832011-10-10 13:19:10 +0000448 return S_OK;
449}
450
451bool SkXPSDevice::endPortfolio() {
452 //Subset fonts
453 if (!this->fTypefaces.empty()) {
454 SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
455 const TypefaceUse* last = &this->fTypefaces.back();
456 for (; current <= last; ++current) {
457 //Ignore return for now, if it didn't subset, let it be.
458 subset_typeface(current);
459 }
460 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000461
bungeman@google.comb29c8832011-10-10 13:19:10 +0000462 HRBM(this->fPackageWriter->Close(), "Could not close writer.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000463
bungeman@google.comb29c8832011-10-10 13:19:10 +0000464 return true;
465}
466
467static XPS_COLOR xps_color(const SkColor skColor) {
468 //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
469 XPS_COLOR xpsColor;
470 xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
471 xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
472 xpsColor.value.sRGB.red = SkColorGetR(skColor);
473 xpsColor.value.sRGB.green = SkColorGetG(skColor);
474 xpsColor.value.sRGB.blue = SkColorGetB(skColor);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000475
bungeman@google.comb29c8832011-10-10 13:19:10 +0000476 return xpsColor;
477}
478
479static XPS_POINT xps_point(const SkPoint& point) {
480 XPS_POINT xpsPoint = {
481 SkScalarToFLOAT(point.fX),
482 SkScalarToFLOAT(point.fY),
483 };
484 return xpsPoint;
485}
486
487static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
488 SkPoint skTransformedPoint;
489 matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
490 return xps_point(skTransformedPoint);
491}
492
493static XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
494 switch (tileMode) {
495 case SkShader::kClamp_TileMode:
496 return XPS_SPREAD_METHOD_PAD;
497 case SkShader::kRepeat_TileMode:
498 return XPS_SPREAD_METHOD_REPEAT;
499 case SkShader::kMirror_TileMode:
500 return XPS_SPREAD_METHOD_REFLECT;
501 default:
mtklein@google.com330313a2013-08-22 15:37:26 +0000502 SkDEBUGFAIL("Unknown tile mode.");
bungeman@google.comb29c8832011-10-10 13:19:10 +0000503 }
504 return XPS_SPREAD_METHOD_PAD;
505}
506
507static void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
508 const SkPoint& start, const SkPoint& end,
509 const SkMatrix& transform) {
510 SkPoint startTransformed;
511 transform.mapXY(start.fX, start.fY, &startTransformed);
512 SkPoint endTransformed;
513 transform.mapXY(end.fX, end.fY, &endTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000514
bungeman@google.comb29c8832011-10-10 13:19:10 +0000515 //Manhattan distance between transformed start and end.
516 SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
517 + (endTransformed.fY - startTransformed.fY);
518 if (SkScalarNearlyZero(startToEnd)) {
519 for (int i = 0; i < numOffsets; ++i) {
520 stopOffsets[i] = 0;
521 }
522 return;
523 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000524
bungeman@google.comb29c8832011-10-10 13:19:10 +0000525 for (int i = 0; i < numOffsets; ++i) {
526 SkPoint stop;
527 stop.fX = SkScalarMul(end.fX - start.fX, stopOffsets[i]);
528 stop.fY = SkScalarMul(end.fY - start.fY, stopOffsets[i]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000529
bungeman@google.comb29c8832011-10-10 13:19:10 +0000530 SkPoint stopTransformed;
531 transform.mapXY(stop.fX, stop.fY, &stopTransformed);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000532
bungeman@google.comb29c8832011-10-10 13:19:10 +0000533 //Manhattan distance between transformed start and stop.
534 SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
535 + (stopTransformed.fY - startTransformed.fY);
536 //Percentage along transformed line.
reed80ea19c2015-05-12 10:37:34 -0700537 stopOffsets[i] = startToStop / startToEnd;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000538 }
539}
540
541HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
542 IXpsOMMatrixTransform** xpsTransform) {
543 SkScalar affine[6];
544 if (!matrix.asAffine(affine)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700545 *xpsTransform = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +0000546 return S_FALSE;
547 }
548 XPS_MATRIX rawXpsMatrix = {
549 SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
550 SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
551 SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
552 SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
553 SkScalarToFLOAT(affine[SkMatrix::kATransX]),
554 SkScalarToFLOAT(affine[SkMatrix::kATransY]),
555 };
556 HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
557 "Could not create transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000558
bungeman@google.comb29c8832011-10-10 13:19:10 +0000559 return S_OK;
560}
561
562HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
563 IXpsOMVisualCollection* visuals,
564 IXpsOMPath** path) {
565 SkTScopedComPtr<IXpsOMGeometry> geometry;
566 HRM(this->fXpsFactory->CreateGeometry(&geometry),
567 "Could not create geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000568
bungeman@google.comb29c8832011-10-10 13:19:10 +0000569 SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
570 HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
571 HRM(figureCollection->Append(figure), "Could not add figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000572
bungeman@google.comb29c8832011-10-10 13:19:10 +0000573 HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
574 HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000575
bungeman@google.comb29c8832011-10-10 13:19:10 +0000576 HRM(visuals->Append(*path), "Could not add path to visuals.");
577 return S_OK;
578}
579
580HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
581 const SkAlpha alpha,
582 IXpsOMBrush** xpsBrush) {
583 XPS_COLOR xpsColor = xps_color(skColor);
584 SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
halcanary96fcdcc2015-08-27 07:41:13 -0700585 HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush),
bungeman@google.comb29c8832011-10-10 13:19:10 +0000586 "Could not create solid color brush.");
587 HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
588 HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
589 return S_OK;
590}
591
592HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
593 const XPS_RECT& imageViewBox,
594 IXpsOMImageResource* image,
595 IXpsOMVisualCollection* visuals) {
596 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
597 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000598
bungeman@google.comb29c8832011-10-10 13:19:10 +0000599 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
600 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000601
bungeman@google.comb29c8832011-10-10 13:19:10 +0000602 SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
603 HRM(this->fXpsFactory->CreateImageBrush(image,
604 &imageViewBox,
605 &imageViewBox,
606 &areaToFillBrush),
607 "Could not create brush for side of clamp.");
608 HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
609 "Could not set tile mode for side of clamp.");
610 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
611 "Could not set brush for side of clamp");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000612
bungeman@google.comb29c8832011-10-10 13:19:10 +0000613 return S_OK;
614}
615
616HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
617 const SkColor color,
618 IXpsOMVisualCollection* visuals) {
619 SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
620 HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000621
bungeman@google.comb29c8832011-10-10 13:19:10 +0000622 SkTScopedComPtr<IXpsOMPath> areaToFillPath;
623 HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000624
bungeman@google.comb29c8832011-10-10 13:19:10 +0000625 SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
626 HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
627 HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
628 "Could not set brush for corner of clamp.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000629
bungeman@google.comb29c8832011-10-10 13:19:10 +0000630 return S_OK;
631}
632
633static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE;
634static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE;
635static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX;
636static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY;
637static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
638
639//TODO(bungeman): In the future, should skia add None,
640//handle None+Mirror and None+Repeat correctly.
641//None is currently an internal hack so masks don't repeat (None+None only).
642static XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
643 [SkShader::kTileModeCount+1] = {
644 //Clamp //Repeat //Mirror //None
645/*Clamp */ XTM_N, XTM_T, XTM_Y, XTM_N,
646/*Repeat*/ XTM_T, XTM_T, XTM_Y, XTM_N,
647/*Mirror*/ XTM_X, XTM_X, XTM_XY, XTM_X,
648/*None */ XTM_N, XTM_N, XTM_Y, XTM_N,
649};
650
651HRESULT SkXPSDevice::createXpsImageBrush(
652 const SkBitmap& bitmap,
653 const SkMatrix& localMatrix,
654 const SkShader::TileMode (&xy)[2],
655 const SkAlpha alpha,
656 IXpsOMTileBrush** xpsBrush) {
657 SkDynamicMemoryWStream write;
658 if (!SkImageEncoder::EncodeStream(&write, bitmap,
659 SkImageEncoder::kPNG_Type, 100)) {
660 HRM(E_FAIL, "Unable to encode bitmap as png.");
661 }
662 SkMemoryStream* read = new SkMemoryStream;
663 read->setData(write.copyToData())->unref();
664 SkTScopedComPtr<IStream> readWrapper;
665 HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
666 "Could not create stream from png data.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000667
bungeman@google.comb29c8832011-10-10 13:19:10 +0000668 const size_t size =
669 SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
670 wchar_t buffer[size];
671 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -0400672 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +0000673 swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000674
bungeman@google.comb29c8832011-10-10 13:19:10 +0000675 SkTScopedComPtr<IOpcPartUri> imagePartUri;
676 HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
677 "Could not create image part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000678
bungeman@google.comb29c8832011-10-10 13:19:10 +0000679 SkTScopedComPtr<IXpsOMImageResource> imageResource;
680 HRM(this->fXpsFactory->CreateImageResource(
681 readWrapper.get(),
682 XPS_IMAGE_TYPE_PNG,
683 imagePartUri.get(),
684 &imageResource),
685 "Could not create image resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000686
bungeman@google.comb29c8832011-10-10 13:19:10 +0000687 XPS_RECT bitmapRect = {
688 0.0, 0.0,
689 static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
690 };
691 SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
692 HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
693 &bitmapRect, &bitmapRect,
694 &xpsImageBrush),
695 "Could not create image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000696
bungeman@google.comb29c8832011-10-10 13:19:10 +0000697 if (SkShader::kClamp_TileMode != xy[0] &&
698 SkShader::kClamp_TileMode != xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000699
bungeman@google.comb29c8832011-10-10 13:19:10 +0000700 HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
701 "Could not set image tile mode");
702 HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
703 "Could not set image opacity.");
704 HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
705 } else {
706 //TODO(bungeman): compute how big this really needs to be.
707 const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
708 const FLOAT BIG_F = SkScalarToFLOAT(BIG);
709 const SkScalar bWidth = SkIntToScalar(bitmap.width());
710 const SkScalar bHeight = SkIntToScalar(bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000711
bungeman@google.comb29c8832011-10-10 13:19:10 +0000712 //create brush canvas
713 SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
714 HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
715 "Could not create image brush canvas.");
716 SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
717 HRM(brushCanvas->GetVisuals(&brushVisuals),
718 "Could not get image brush canvas visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000719
bungeman@google.comb29c8832011-10-10 13:19:10 +0000720 //create central figure
721 const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
722 SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
723 HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000724
bungeman@google.comb29c8832011-10-10 13:19:10 +0000725 SkTScopedComPtr<IXpsOMPath> centralPath;
726 HR(this->createPath(centralFigure.get(),
727 brushVisuals.get(),
728 &centralPath));
729 HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
730 "Could not set tile mode for image brush central path.");
731 HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
732 "Could not set fill brush for image brush central path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000733
bungeman@google.comb29c8832011-10-10 13:19:10 +0000734 //add left/right
735 if (SkShader::kClamp_TileMode == xy[0]) {
736 SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
737 XPS_RECT leftImageViewBox = {
738 0.0, 0.0,
739 1.0, static_cast<FLOAT>(bitmap.height()),
740 };
741 HR(this->sideOfClamp(leftArea, leftImageViewBox,
742 imageResource.get(),
743 brushVisuals.get()));
744
745 SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
746 XPS_RECT rightImageViewBox = {
747 bitmap.width() - 1.0f, 0.0f,
748 1.0f, static_cast<FLOAT>(bitmap.height()),
749 };
750 HR(this->sideOfClamp(rightArea, rightImageViewBox,
751 imageResource.get(),
752 brushVisuals.get()));
753 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000754
bungeman@google.comb29c8832011-10-10 13:19:10 +0000755 //add top/bottom
756 if (SkShader::kClamp_TileMode == xy[1]) {
757 SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
758 XPS_RECT topImageViewBox = {
759 0.0, 0.0,
760 static_cast<FLOAT>(bitmap.width()), 1.0,
761 };
762 HR(this->sideOfClamp(topArea, topImageViewBox,
763 imageResource.get(),
764 brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000765
bungeman@google.comb29c8832011-10-10 13:19:10 +0000766 SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
767 XPS_RECT bottomImageViewBox = {
768 0.0f, bitmap.height() - 1.0f,
769 static_cast<FLOAT>(bitmap.width()), 1.0f,
770 };
771 HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
772 imageResource.get(),
773 brushVisuals.get()));
774 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000775
bungeman@google.comb29c8832011-10-10 13:19:10 +0000776 //add tl, tr, bl, br
777 if (SkShader::kClamp_TileMode == xy[0] &&
778 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000779
bungeman@google.comb29c8832011-10-10 13:19:10 +0000780 SkAutoLockPixels alp(bitmap);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000781
bungeman@google.comb29c8832011-10-10 13:19:10 +0000782 const SkColor tlColor = bitmap.getColor(0,0);
783 const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
784 HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000785
bungeman@google.comb29c8832011-10-10 13:19:10 +0000786 const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
787 const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
788 HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000789
bungeman@google.comb29c8832011-10-10 13:19:10 +0000790 const SkColor brColor = bitmap.getColor(bitmap.width()-1,
791 bitmap.height()-1);
792 const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
793 HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000794
bungeman@google.comb29c8832011-10-10 13:19:10 +0000795 const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
796 const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
797 HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
798 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000799
bungeman@google.comb29c8832011-10-10 13:19:10 +0000800 //create visual brush from canvas
801 XPS_RECT bound = {};
802 if (SkShader::kClamp_TileMode == xy[0] &&
803 SkShader::kClamp_TileMode == xy[1]) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000804
bungeman@google.comb29c8832011-10-10 13:19:10 +0000805 bound.x = BIG_F / -2;
806 bound.y = BIG_F / -2;
807 bound.width = BIG_F;
808 bound.height = BIG_F;
809 } else if (SkShader::kClamp_TileMode == xy[0]) {
810 bound.x = BIG_F / -2;
811 bound.y = 0.0f;
812 bound.width = BIG_F;
813 bound.height = static_cast<FLOAT>(bitmap.height());
814 } else if (SkShader::kClamp_TileMode == xy[1]) {
815 bound.x = 0;
816 bound.y = BIG_F / -2;
817 bound.width = static_cast<FLOAT>(bitmap.width());
818 bound.height = BIG_F;
819 }
820 SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
821 HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
822 "Could not create visual brush for image brush.");
823 HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
824 "Could not set canvas on visual brush for image brush.");
825 HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
826 "Could not set tile mode on visual brush for image brush.");
827 HRM(clampBrush->SetOpacity(alpha / 255.0f),
828 "Could not set opacity on visual brush for image brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000829
bungeman@google.comb29c8832011-10-10 13:19:10 +0000830 HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
831 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000832
bungeman@google.comb29c8832011-10-10 13:19:10 +0000833 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
834 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
bsalomon49f085d2014-09-05 13:34:00 -0700835 if (xpsMatrixToUse.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000836 HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
837 "Could not set transform for image brush.");
838 } else {
839 //TODO(bungeman): perspective bitmaps in general.
840 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000841
bungeman@google.comb29c8832011-10-10 13:19:10 +0000842 return S_OK;
843}
844
845HRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
846 const SkScalar offset,
847 IXpsOMGradientStop** xpsGradStop) {
848 XPS_COLOR gradStopXpsColor = xps_color(skColor);
849 HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
halcanary96fcdcc2015-08-27 07:41:13 -0700850 nullptr,
bungeman@google.comb29c8832011-10-10 13:19:10 +0000851 SkScalarToFLOAT(offset),
852 xpsGradStop),
853 "Could not create gradient stop.");
854 return S_OK;
855}
856
857HRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
858 const SkAlpha alpha,
859 const SkMatrix& localMatrix,
860 IXpsOMMatrixTransform* xpsMatrix,
861 IXpsOMBrush** xpsBrush) {
862 XPS_POINT startPoint;
863 XPS_POINT endPoint;
bsalomon49f085d2014-09-05 13:34:00 -0700864 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000865 startPoint = xps_point(info.fPoint[0]);
866 endPoint = xps_point(info.fPoint[1]);
867 } else {
868 transform_offsets(info.fColorOffsets, info.fColorCount,
869 info.fPoint[0], info.fPoint[1],
870 localMatrix);
871 startPoint = xps_point(info.fPoint[0], localMatrix);
872 endPoint = xps_point(info.fPoint[1], localMatrix);
873 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000874
bungeman@google.comb29c8832011-10-10 13:19:10 +0000875 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
876 HR(createXpsGradientStop(info.fColors[0],
877 info.fColorOffsets[0],
878 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000879
bungeman@google.comb29c8832011-10-10 13:19:10 +0000880 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
881 HR(createXpsGradientStop(info.fColors[1],
882 info.fColorOffsets[1],
883 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000884
bungeman@google.comb29c8832011-10-10 13:19:10 +0000885 SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
886 HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
887 gradStop1.get(),
888 &startPoint,
889 &endPoint,
890 &gradientBrush),
891 "Could not create linear gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700892 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000893 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
894 "Could not set transform on linear gradient brush.");
895 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000896
bungeman@google.comb29c8832011-10-10 13:19:10 +0000897 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
898 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
899 "Could not get linear gradient stop collection.");
900 for (int i = 2; i < info.fColorCount; ++i) {
901 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
902 HR(createXpsGradientStop(info.fColors[i],
903 info.fColorOffsets[i],
904 &gradStop));
905 HRM(gradStopCollection->Append(gradStop.get()),
906 "Could not add linear gradient stop.");
907 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000908
bungeman@google.comb29c8832011-10-10 13:19:10 +0000909 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
910 "Could not set spread method of linear gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000911
bungeman@google.comb29c8832011-10-10 13:19:10 +0000912 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
913 "Could not set opacity of linear gradient brush.");
914 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000915
bungeman@google.comb29c8832011-10-10 13:19:10 +0000916 return S_OK;
917}
918
919HRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
920 const SkAlpha alpha,
921 const SkMatrix& localMatrix,
922 IXpsOMMatrixTransform* xpsMatrix,
923 IXpsOMBrush** xpsBrush) {
924 SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
925 HR(createXpsGradientStop(info.fColors[0],
926 info.fColorOffsets[0],
927 &gradStop0));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000928
bungeman@google.comb29c8832011-10-10 13:19:10 +0000929 SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
930 HR(createXpsGradientStop(info.fColors[1],
931 info.fColorOffsets[1],
932 &gradStop1));
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000933
bungeman@google.comb29c8832011-10-10 13:19:10 +0000934 //TODO: figure out how to fake better if not affine
935 XPS_POINT centerPoint;
936 XPS_POINT gradientOrigin;
937 XPS_SIZE radiiSizes;
bsalomon49f085d2014-09-05 13:34:00 -0700938 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000939 centerPoint = xps_point(info.fPoint[0]);
940 gradientOrigin = xps_point(info.fPoint[0]);
941 radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
942 radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
943 } else {
944 centerPoint = xps_point(info.fPoint[0], localMatrix);
945 gradientOrigin = xps_point(info.fPoint[0], localMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000946
bungeman@google.comb29c8832011-10-10 13:19:10 +0000947 SkScalar radius = info.fRadius[0];
948 SkVector vec[2];
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000949
bungeman@google.comb29c8832011-10-10 13:19:10 +0000950 vec[0].set(radius, 0);
951 vec[1].set(0, radius);
952 localMatrix.mapVectors(vec, 2);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000953
bungeman@google.comb29c8832011-10-10 13:19:10 +0000954 SkScalar d0 = vec[0].length();
955 SkScalar d1 = vec[1].length();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000956
bungeman@google.comb29c8832011-10-10 13:19:10 +0000957 radiiSizes.width = SkScalarToFLOAT(d0);
958 radiiSizes.height = SkScalarToFLOAT(d1);
959 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000960
bungeman@google.comb29c8832011-10-10 13:19:10 +0000961 SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
962 HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
963 gradStop1.get(),
964 &centerPoint,
965 &gradientOrigin,
966 &radiiSizes,
967 &gradientBrush),
968 "Could not create radial gradient brush.");
bsalomon49f085d2014-09-05 13:34:00 -0700969 if (xpsMatrix) {
bungeman@google.comb29c8832011-10-10 13:19:10 +0000970 HRM(gradientBrush->SetTransformLocal(xpsMatrix),
971 "Could not set transform on radial gradient brush.");
972 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000973
bungeman@google.comb29c8832011-10-10 13:19:10 +0000974 SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
975 HRM(gradientBrush->GetGradientStops(&gradStopCollection),
976 "Could not get radial gradient stop collection.");
977 for (int i = 2; i < info.fColorCount; ++i) {
978 SkTScopedComPtr<IXpsOMGradientStop> gradStop;
979 HR(createXpsGradientStop(info.fColors[i],
980 info.fColorOffsets[i],
981 &gradStop));
982 HRM(gradStopCollection->Append(gradStop.get()),
983 "Could not add radial gradient stop.");
984 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000985
bungeman@google.comb29c8832011-10-10 13:19:10 +0000986 HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
987 "Could not set spread method of radial gradient.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000988
bungeman@google.comb29c8832011-10-10 13:19:10 +0000989 HRM(gradientBrush->SetOpacity(alpha / 255.0f),
990 "Could not set opacity of radial gradient brush.");
991 HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000992
bungeman@google.comb29c8832011-10-10 13:19:10 +0000993 return S_OK;
994}
995
996HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
997 IXpsOMBrush** brush,
998 const SkMatrix* parentTransform) {
999 const SkShader *shader = skPaint.getShader();
halcanary96fcdcc2015-08-27 07:41:13 -07001000 if (nullptr == shader) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001001 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1002 return S_OK;
1003 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001004
bungeman@google.comb29c8832011-10-10 13:19:10 +00001005 //Gradient shaders.
1006 SkShader::GradientInfo info;
1007 info.fColorCount = 0;
halcanary96fcdcc2015-08-27 07:41:13 -07001008 info.fColors = nullptr;
1009 info.fColorOffsets = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001010 SkShader::GradientType gradientType = shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001011
bungeman@google.comb29c8832011-10-10 13:19:10 +00001012 if (SkShader::kNone_GradientType == gradientType) {
1013 //Nothing to see, move along.
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001014
bungeman@google.comb29c8832011-10-10 13:19:10 +00001015 } else if (SkShader::kColor_GradientType == gradientType) {
1016 SkASSERT(1 == info.fColorCount);
1017 SkColor color;
1018 info.fColors = &color;
bsalomon@google.comb58a6392013-03-21 20:29:05 +00001019 shader->asAGradient(&info);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001020 SkAlpha alpha = skPaint.getAlpha();
1021 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1022 return S_OK;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001023
bungeman@google.comb29c8832011-10-10 13:19:10 +00001024 } else {
1025 if (info.fColorCount == 0) {
1026 const SkColor color = skPaint.getColor();
1027 HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
1028 return S_OK;
1029 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001030
bungeman@google.comb29c8832011-10-10 13:19:10 +00001031 SkAutoTArray<SkColor> colors(info.fColorCount);
1032 SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1033 info.fColors = colors.get();
1034 info.fColorOffsets = colorOffsets.get();
1035 shader->asAGradient(&info);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001036
bungeman@google.comb29c8832011-10-10 13:19:10 +00001037 if (1 == info.fColorCount) {
1038 SkColor color = info.fColors[0];
1039 SkAlpha alpha = skPaint.getAlpha();
1040 HR(this->createXpsSolidColorBrush(color, alpha, brush));
1041 return S_OK;
1042 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001043
bsalomon@google.comf94b3a42012-10-31 18:09:01 +00001044 SkMatrix localMatrix = shader->getLocalMatrix();
bsalomon49f085d2014-09-05 13:34:00 -07001045 if (parentTransform) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001046 localMatrix.preConcat(*parentTransform);
1047 }
1048 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1049 HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001050
bungeman@google.comb29c8832011-10-10 13:19:10 +00001051 if (SkShader::kLinear_GradientType == gradientType) {
1052 HR(this->createXpsLinearGradient(info,
1053 skPaint.getAlpha(),
1054 localMatrix,
1055 xpsMatrixToUse.get(),
1056 brush));
1057 return S_OK;
1058 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001059
bungeman@google.comb29c8832011-10-10 13:19:10 +00001060 if (SkShader::kRadial_GradientType == gradientType) {
1061 HR(this->createXpsRadialGradient(info,
1062 skPaint.getAlpha(),
1063 localMatrix,
1064 xpsMatrixToUse.get(),
1065 brush));
1066 return S_OK;
1067 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001068
reed71a6cbf2015-05-04 08:32:51 -07001069 if (SkShader::kConical_GradientType == gradientType) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001070 //simple if affine and one is 0, otherwise will have to fake
1071 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001072
bungeman@google.comb29c8832011-10-10 13:19:10 +00001073 if (SkShader::kSweep_GradientType == gradientType) {
1074 //have to fake
1075 }
1076 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001077
bungeman@google.comb29c8832011-10-10 13:19:10 +00001078 SkBitmap outTexture;
1079 SkMatrix outMatrix;
1080 SkShader::TileMode xy[2];
reedf5822822015-08-19 11:46:38 -07001081 if (shader->isABitmap(&outTexture, &outMatrix, xy)) {
1082 //TODO: outMatrix??
1083 SkMatrix localMatrix = shader->getLocalMatrix();
1084 if (parentTransform) {
1085 localMatrix.preConcat(*parentTransform);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001086 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001087
reedf5822822015-08-19 11:46:38 -07001088 SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1089 HR(this->createXpsImageBrush(outTexture,
1090 localMatrix,
1091 xy,
1092 skPaint.getAlpha(),
1093 &tileBrush));
1094
1095 HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
1096 } else {
1097 HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1098 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001099 return S_OK;
1100}
1101
1102static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1103 const bool zeroWidth = (0 == paint.getStrokeWidth());
1104 const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001105
bungeman@google.comb29c8832011-10-10 13:19:10 +00001106 return paint.getPathEffect() ||
1107 paint.getMaskFilter() ||
1108 paint.getRasterizer() ||
1109 (stroke && (
1110 (matrix.hasPerspective() && !zeroWidth) ||
1111 SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1112 (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1113 paint.getStrokeMiter() < SK_ScalarSqrt2)
1114 ))
1115 ;
1116}
1117
1118HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1119 IXpsOMGeometryFigure** xpsRect) {
1120 const SkPoint points[4] = {
1121 { rect.fLeft, rect.fTop },
1122 { rect.fRight, rect.fTop },
1123 { rect.fRight, rect.fBottom },
1124 { rect.fLeft, rect.fBottom },
1125 };
1126 return this->createXpsQuad(points, stroke, fill, xpsRect);
1127}
1128HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1129 BOOL stroke, BOOL fill,
1130 IXpsOMGeometryFigure** xpsQuad) {
1131 // Define the start point.
1132 XPS_POINT startPoint = xps_point(points[0]);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001133
bungeman@google.comb29c8832011-10-10 13:19:10 +00001134 // Create the figure.
1135 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1136 "Could not create quad geometry figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001137
bungeman@google.comb29c8832011-10-10 13:19:10 +00001138 // Define the type of each segment.
1139 XPS_SEGMENT_TYPE segmentTypes[3] = {
1140 XPS_SEGMENT_TYPE_LINE,
1141 XPS_SEGMENT_TYPE_LINE,
1142 XPS_SEGMENT_TYPE_LINE,
1143 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001144
bungeman@google.comb29c8832011-10-10 13:19:10 +00001145 // Define the x and y coordinates of each corner of the figure.
1146 FLOAT segmentData[6] = {
1147 SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1148 SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1149 SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1150 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001151
bungeman@google.comb29c8832011-10-10 13:19:10 +00001152 // Describe if the segments are stroked.
1153 BOOL segmentStrokes[3] = {
1154 stroke, stroke, stroke,
1155 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001156
bungeman@google.comb29c8832011-10-10 13:19:10 +00001157 // Add the segment data to the figure.
1158 HRM((*xpsQuad)->SetSegments(
1159 3, 6,
1160 segmentTypes , segmentData, segmentStrokes),
1161 "Could not add segment data to quad.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001162
bungeman@google.comb29c8832011-10-10 13:19:10 +00001163 // Set the closed and filled properties of the figure.
1164 HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1165 HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001166
bungeman@google.comb29c8832011-10-10 13:19:10 +00001167 return S_OK;
1168}
1169
bungeman@google.comb29c8832011-10-10 13:19:10 +00001170void SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1171 size_t count, const SkPoint points[],
1172 const SkPaint& paint) {
1173 //This will call back into the device to do the drawing.
1174 d.drawPoints(mode, count, points, paint, true);
1175}
1176
1177void SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1178 int vertexCount, const SkPoint verts[],
1179 const SkPoint texs[], const SkColor colors[],
1180 SkXfermode* xmode, const uint16_t indices[],
1181 int indexCount, const SkPaint& paint) {
1182 //TODO: override this for XPS
1183 SkDEBUGF(("XPS drawVertices not yet implemented."));
1184}
1185
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001186void SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001187 const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001188
bungeman@google.comb29c8832011-10-10 13:19:10 +00001189 //If trying to paint with a stroke, ignore that and fill.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001190 SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
1191 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1192 if (paint->getStyle() != SkPaint::kFill_Style) {
1193 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001194 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001195
bungeman@google.comb29c8832011-10-10 13:19:10 +00001196 this->internalDrawRect(d, r, false, *fillPaint);
1197}
1198
1199void SkXPSDevice::drawRect(const SkDraw& d,
1200 const SkRect& r,
1201 const SkPaint& paint) {
1202 this->internalDrawRect(d, r, true, paint);
1203}
1204
scroggo@google.comcac8d012013-11-12 17:10:02 +00001205void SkXPSDevice::drawRRect(const SkDraw& d,
1206 const SkRRect& rr,
1207 const SkPaint& paint) {
1208 SkPath path;
1209 path.addRRect(rr);
halcanary96fcdcc2015-08-27 07:41:13 -07001210 this->drawPath(d, path, paint, nullptr, true);
scroggo@google.comcac8d012013-11-12 17:10:02 +00001211}
1212
bungeman@google.comb29c8832011-10-10 13:19:10 +00001213void SkXPSDevice::internalDrawRect(const SkDraw& d,
1214 const SkRect& r,
1215 bool transformRect,
1216 const SkPaint& paint) {
1217 //Exit early if there is nothing to draw.
reed1e7f5e72016-04-27 07:49:17 -07001218 if (d.fRC->isEmpty() ||
halcanary96fcdcc2015-08-27 07:41:13 -07001219 (paint.getAlpha() == 0 && paint.getXfermode() == nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001220 return;
1221 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001222
bungeman@google.comb29c8832011-10-10 13:19:10 +00001223 //Path the rect if we can't optimize it.
1224 if (rect_must_be_pathed(paint, *d.fMatrix)) {
1225 SkPath tmp;
1226 tmp.addRect(r);
1227 tmp.setFillType(SkPath::kWinding_FillType);
halcanary96fcdcc2015-08-27 07:41:13 -07001228 this->drawPath(d, tmp, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001229 return;
1230 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001231
bungeman@google.comb29c8832011-10-10 13:19:10 +00001232 //Create the shaded path.
1233 SkTScopedComPtr<IXpsOMPath> shadedPath;
1234 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1235 "Could not create shaded path for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001236
bungeman@google.comb29c8832011-10-10 13:19:10 +00001237 //Create the shaded geometry.
1238 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1239 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1240 "Could not create shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001241
bungeman@google.comb29c8832011-10-10 13:19:10 +00001242 //Add the geometry to the shaded path.
1243 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1244 "Could not set shaded geometry for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001245
bungeman@google.comb29c8832011-10-10 13:19:10 +00001246 //Set the brushes.
1247 BOOL fill = FALSE;
1248 BOOL stroke = FALSE;
1249 HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001250
bungeman@google.comb29c8832011-10-10 13:19:10 +00001251 bool xpsTransformsPath = true;
1252 //Transform the geometry.
1253 if (transformRect && xpsTransformsPath) {
1254 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1255 HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1256 if (xpsTransform.get()) {
1257 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1258 "Could not set transform for rect.");
1259 } else {
1260 xpsTransformsPath = false;
1261 }
1262 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001263
bungeman@google.comb29c8832011-10-10 13:19:10 +00001264 //Create the figure.
1265 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1266 {
1267 SkPoint points[4] = {
1268 { r.fLeft, r.fTop },
1269 { r.fLeft, r.fBottom },
1270 { r.fRight, r.fBottom },
1271 { r.fRight, r.fTop },
1272 };
1273 if (!xpsTransformsPath && transformRect) {
1274 d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1275 }
1276 HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1277 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001278
bungeman@google.comb29c8832011-10-10 13:19:10 +00001279 //Get the figures of the shaded geometry.
1280 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1281 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1282 "Could not get shaded figures for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001283
bungeman@google.comb29c8832011-10-10 13:19:10 +00001284 //Add the figure to the shaded geometry figures.
1285 HRVM(shadedFigures->Append(rectFigure.get()),
1286 "Could not add shaded figure for rect.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001287
bungeman@google.comb29c8832011-10-10 13:19:10 +00001288 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001289
bungeman@google.comb29c8832011-10-10 13:19:10 +00001290 //Add the shaded path to the current visuals.
1291 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1292 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1293 "Could not get current visuals for rect.");
1294 HRVM(currentVisuals->Append(shadedPath.get()),
1295 "Could not add rect to current visuals.");
1296}
1297
1298static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1299 const SkTDArray<BOOL>& segmentStrokes,
1300 const SkTDArray<FLOAT>& segmentData,
1301 BOOL stroke, BOOL fill,
1302 IXpsOMGeometryFigure* figure,
1303 IXpsOMGeometryFigureCollection* figures) {
1304 // Add the segment data to the figure.
1305 HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1306 segmentTypes.begin() , segmentData.begin(),
1307 segmentStrokes.begin()),
1308 "Could not set path segments.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001309
bungeman@google.comb29c8832011-10-10 13:19:10 +00001310 // Set the closed and filled properties of the figure.
1311 HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1312 HRM(figure->SetIsFilled(fill), "Could not set path fill.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001313
bungeman@google.comb29c8832011-10-10 13:19:10 +00001314 // Add the figure created above to this geometry.
1315 HRM(figures->Append(figure), "Could not add path to geometry.");
1316 return S_OK;
1317}
1318
1319HRESULT SkXPSDevice::addXpsPathGeometry(
1320 IXpsOMGeometryFigureCollection* xpsFigures,
1321 BOOL stroke, BOOL fill, const SkPath& path) {
1322 SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1323 SkTDArray<BOOL> segmentStrokes;
1324 SkTDArray<FLOAT> segmentData;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001325
bungeman@google.comb29c8832011-10-10 13:19:10 +00001326 SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1327 SkPath::Iter iter(path, true);
1328 SkPoint points[4];
1329 SkPath::Verb verb;
1330 while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1331 switch (verb) {
1332 case SkPath::kMove_Verb: {
bsalomon49f085d2014-09-05 13:34:00 -07001333 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001334 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1335 stroke, fill,
1336 xpsFigure.get() , xpsFigures));
1337 xpsFigure.reset();
1338 segmentTypes.rewind();
1339 segmentStrokes.rewind();
1340 segmentData.rewind();
1341 }
1342 // Define the start point.
1343 XPS_POINT startPoint = xps_point(points[0]);
1344 // Create the figure.
1345 HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1346 &xpsFigure),
1347 "Could not create path geometry figure.");
1348 break;
1349 }
1350 case SkPath::kLine_Verb:
1351 if (iter.isCloseLine()) break; //ignore the line, auto-closed
1352 segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1353 segmentStrokes.push(stroke);
1354 segmentData.push(SkScalarToFLOAT(points[1].fX));
1355 segmentData.push(SkScalarToFLOAT(points[1].fY));
1356 break;
1357 case SkPath::kQuad_Verb:
1358 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1359 segmentStrokes.push(stroke);
1360 segmentData.push(SkScalarToFLOAT(points[1].fX));
1361 segmentData.push(SkScalarToFLOAT(points[1].fY));
1362 segmentData.push(SkScalarToFLOAT(points[2].fX));
1363 segmentData.push(SkScalarToFLOAT(points[2].fY));
1364 break;
1365 case SkPath::kCubic_Verb:
1366 segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1367 segmentStrokes.push(stroke);
1368 segmentData.push(SkScalarToFLOAT(points[1].fX));
1369 segmentData.push(SkScalarToFLOAT(points[1].fY));
1370 segmentData.push(SkScalarToFLOAT(points[2].fX));
1371 segmentData.push(SkScalarToFLOAT(points[2].fY));
1372 segmentData.push(SkScalarToFLOAT(points[3].fX));
1373 segmentData.push(SkScalarToFLOAT(points[3].fY));
1374 break;
halcanary47ef4d52015-03-03 09:13:09 -08001375 case SkPath::kConic_Verb: {
1376 const SkScalar tol = SK_Scalar1 / 4;
1377 SkAutoConicToQuads converter;
1378 const SkPoint* quads =
1379 converter.computeQuads(points, iter.conicWeight(), tol);
1380 for (int i = 0; i < converter.countQuads(); ++i) {
1381 segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1382 segmentStrokes.push(stroke);
1383 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fX));
1384 segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fY));
1385 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fX));
1386 segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fY));
1387 }
1388 break;
1389 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001390 case SkPath::kClose_Verb:
1391 // we ignore these, and just get the whole segment from
1392 // the corresponding line/quad/cubic verbs
1393 break;
1394 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001395 SkDEBUGFAIL("unexpected verb");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001396 break;
1397 }
1398 }
bsalomon49f085d2014-09-05 13:34:00 -07001399 if (xpsFigure.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001400 HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1401 stroke, fill,
1402 xpsFigure.get(), xpsFigures));
1403 }
1404 return S_OK;
1405}
1406
bungeman@google.comb29c8832011-10-10 13:19:10 +00001407void SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1408 SkMatrix* matrix,
1409 SkVector* ppuScale,
1410 const SkIRect& clip, SkIRect* clipIRect) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001411 //This action is in unit space, but the ppm is specified in physical space.
reed80ea19c2015-05-12 10:37:34 -07001412 ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
1413 fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001414
bungeman@google.comb29c8832011-10-10 13:19:10 +00001415 matrix->postScale(ppuScale->fX, ppuScale->fY);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001416
bungeman@google.comb29c8832011-10-10 13:19:10 +00001417 const SkIRect& irect = clip;
1418 SkRect clipRect = SkRect::MakeLTRB(
1419 SkScalarMul(SkIntToScalar(irect.fLeft), ppuScale->fX),
1420 SkScalarMul(SkIntToScalar(irect.fTop), ppuScale->fY),
1421 SkScalarMul(SkIntToScalar(irect.fRight), ppuScale->fX),
1422 SkScalarMul(SkIntToScalar(irect.fBottom), ppuScale->fY));
1423 clipRect.roundOut(clipIRect);
1424}
1425
1426HRESULT SkXPSDevice::applyMask(const SkDraw& d,
1427 const SkMask& mask,
1428 const SkVector& ppuScale,
1429 IXpsOMPath* shadedPath) {
1430 //Get the geometry object.
1431 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1432 HRM(shadedPath->GetGeometry(&shadedGeometry),
1433 "Could not get mask shaded geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001434
bungeman@google.comb29c8832011-10-10 13:19:10 +00001435 //Get the figures from the geometry.
1436 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1437 HRM(shadedGeometry->GetFigures(&shadedFigures),
1438 "Could not get mask shaded figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001439
bungeman@google.comb29c8832011-10-10 13:19:10 +00001440 SkMatrix m;
1441 m.reset();
1442 m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1443 SkIntToScalar(mask.fBounds.fTop));
1444 m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001445
bungeman@google.comb29c8832011-10-10 13:19:10 +00001446 SkShader::TileMode xy[2];
1447 xy[0] = (SkShader::TileMode)3;
1448 xy[1] = (SkShader::TileMode)3;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001449
bungeman@google.comb29c8832011-10-10 13:19:10 +00001450 SkBitmap bm;
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001451 bm.installMaskPixels(mask);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001452
bungeman@google.comb29c8832011-10-10 13:19:10 +00001453 SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1454 HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1455 HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1456 "Could not set mask.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001457
bungeman@google.comb29c8832011-10-10 13:19:10 +00001458 const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1459 this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1460 SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1461 HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1462 "Could not create mask shaded figure.");
1463 HRM(shadedFigures->Append(shadedFigure.get()),
1464 "Could not add mask shaded figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001465
bungeman@google.comb29c8832011-10-10 13:19:10 +00001466 HR(this->clip(shadedPath, d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001467
bungeman@google.comb29c8832011-10-10 13:19:10 +00001468 //Add the path to the active visual collection.
1469 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1470 HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1471 "Could not get mask current visuals.");
1472 HRM(currentVisuals->Append(shadedPath),
1473 "Could not add masked shaded path to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001474
bungeman@google.comb29c8832011-10-10 13:19:10 +00001475 return S_OK;
1476}
1477
1478HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1479 const SkPaint& shaderPaint,
1480 const SkMatrix& matrix,
1481 BOOL* fill, BOOL* stroke) {
1482 *fill = FALSE;
1483 *stroke = FALSE;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001484
bungeman@google.comb29c8832011-10-10 13:19:10 +00001485 const SkPaint::Style style = shaderPaint.getStyle();
1486 const bool hasFill = SkPaint::kFill_Style == style
1487 || SkPaint::kStrokeAndFill_Style == style;
1488 const bool hasStroke = SkPaint::kStroke_Style == style
1489 || SkPaint::kStrokeAndFill_Style == style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001490
bungeman@google.comb29c8832011-10-10 13:19:10 +00001491 //TODO(bungeman): use dictionaries and lookups.
1492 if (hasFill) {
1493 *fill = TRUE;
1494 SkTScopedComPtr<IXpsOMBrush> fillBrush;
1495 HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1496 HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1497 "Could not set fill for shaded path.");
1498 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001499
bungeman@google.comb29c8832011-10-10 13:19:10 +00001500 if (hasStroke) {
1501 *stroke = TRUE;
1502 SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1503 HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1504 HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1505 "Could not set stroke brush for shaded path.");
1506 HRM(shadedPath->SetStrokeThickness(
1507 SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1508 "Could not set shaded path stroke thickness.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001509
bungeman@google.comb29c8832011-10-10 13:19:10 +00001510 if (0 == shaderPaint.getStrokeWidth()) {
1511 //XPS hair width is a hack. (XPS Spec 11.6.12).
1512 SkTScopedComPtr<IXpsOMDashCollection> dashes;
1513 HRM(shadedPath->GetStrokeDashes(&dashes),
1514 "Could not set dashes for shaded path.");
1515 XPS_DASH dash;
1516 dash.length = 1.0;
1517 dash.gap = 0.0;
1518 HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1519 HRM(shadedPath->SetStrokeDashOffset(-2.0),
1520 "Could not set dash offset for shaded path.");
1521 }
1522 }
1523 return S_OK;
1524}
1525
1526void SkXPSDevice::drawPath(const SkDraw& d,
1527 const SkPath& platonicPath,
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001528 const SkPaint& origPaint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001529 const SkMatrix* prePathMatrix,
1530 bool pathIsMutable) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001531 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
1532
bungeman@google.comb29c8832011-10-10 13:19:10 +00001533 // nothing to draw
reed1e7f5e72016-04-27 07:49:17 -07001534 if (d.fRC->isEmpty() ||
halcanary96fcdcc2015-08-27 07:41:13 -07001535 (paint->getAlpha() == 0 && paint->getXfermode() == nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001536 return;
1537 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001538
bungeman@google.comb29c8832011-10-10 13:19:10 +00001539 SkPath modifiedPath;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001540 const bool paintHasPathEffect = paint->getPathEffect()
1541 || paint->getStyle() != SkPaint::kFill_Style;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001542
bungeman@google.comb29c8832011-10-10 13:19:10 +00001543 //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1544 SkMatrix matrix = *d.fMatrix;
1545 SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1546 if (prePathMatrix) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001547 if (paintHasPathEffect || paint->getRasterizer()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001548 if (!pathIsMutable) {
1549 skeletalPath = &modifiedPath;
1550 pathIsMutable = true;
1551 }
1552 platonicPath.transform(*prePathMatrix, skeletalPath);
1553 } else {
commit-bot@chromium.org92362382014-03-18 12:51:48 +00001554 matrix.preConcat(*prePathMatrix);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001555 }
1556 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001557
bungeman@google.comb29c8832011-10-10 13:19:10 +00001558 //Apply path effect [Skeletal-path -> Fillable-path].
1559 SkPath* fillablePath = skeletalPath;
1560 if (paintHasPathEffect) {
1561 if (!pathIsMutable) {
1562 fillablePath = &modifiedPath;
1563 pathIsMutable = true;
1564 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001565 bool fill = paint->getFillPath(*skeletalPath, fillablePath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001566
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001567 SkPaint* writablePaint = paint.writable();
halcanary96fcdcc2015-08-27 07:41:13 -07001568 writablePaint->setPathEffect(nullptr);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001569 if (fill) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001570 writablePaint->setStyle(SkPaint::kFill_Style);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001571 } else {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001572 writablePaint->setStyle(SkPaint::kStroke_Style);
1573 writablePaint->setStrokeWidth(0);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001574 }
1575 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001576
bungeman@google.comb29c8832011-10-10 13:19:10 +00001577 //Create the shaded path. This will be the path which is painted.
1578 SkTScopedComPtr<IXpsOMPath> shadedPath;
1579 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1580 "Could not create shaded path for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001581
bungeman@google.comb29c8832011-10-10 13:19:10 +00001582 //Create the geometry for the shaded path.
1583 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1584 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1585 "Could not create shaded geometry for path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001586
bungeman@google.comb29c8832011-10-10 13:19:10 +00001587 //Add the geometry to the shaded path.
1588 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1589 "Could not add the shaded geometry to shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001590
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001591 SkRasterizer* rasterizer = paint->getRasterizer();
1592 SkMaskFilter* filter = paint->getMaskFilter();
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001593
1594 //Determine if we will draw or shade and mask.
1595 if (rasterizer || filter) {
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001596 if (paint->getStyle() != SkPaint::kFill_Style) {
1597 paint.writable()->setStyle(SkPaint::kFill_Style);
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001598 }
1599 }
1600
bungeman@google.comb29c8832011-10-10 13:19:10 +00001601 //Set the brushes.
1602 BOOL fill;
1603 BOOL stroke;
1604 HRV(this->shadePath(shadedPath.get(),
bsalomon@google.com5dc26b92012-10-11 19:32:32 +00001605 *paint,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001606 *d.fMatrix,
1607 &fill,
1608 &stroke));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001609
bungeman@google.comb29c8832011-10-10 13:19:10 +00001610 //Rasterizer
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001611 if (rasterizer) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001612 SkIRect clipIRect;
1613 SkVector ppuScale;
1614 this->convertToPpm(filter,
1615 &matrix,
1616 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001617 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001618 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001619
halcanary96fcdcc2015-08-27 07:41:13 -07001620 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001621
bungeman@google.comb29c8832011-10-10 13:19:10 +00001622 //[Fillable-path -> Mask]
1623 SkMask rasteredMask;
bungeman@google.comd998cbd2012-04-05 18:57:53 +00001624 if (rasterizer->rasterize(
bungeman@google.comb29c8832011-10-10 13:19:10 +00001625 *fillablePath,
1626 matrix,
1627 &clipIRect,
1628 filter, //just to compute how much to draw.
1629 &rasteredMask,
1630 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001631
bungeman@google.comb29c8832011-10-10 13:19:10 +00001632 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1633 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001634
bungeman@google.comb29c8832011-10-10 13:19:10 +00001635 //[Mask -> Mask]
1636 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001637 if (filter && filter->filterMask(&filteredMask, *mask, *d.fMatrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001638 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001639 }
1640 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001641
bungeman@google.comb29c8832011-10-10 13:19:10 +00001642 //Draw mask.
1643 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1644 }
1645 return;
1646 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001647
bungeman@google.comb29c8832011-10-10 13:19:10 +00001648 //Mask filter
1649 if (filter) {
1650 SkIRect clipIRect;
1651 SkVector ppuScale;
1652 this->convertToPpm(filter,
1653 &matrix,
1654 &ppuScale,
reed1e7f5e72016-04-27 07:49:17 -07001655 d.fRC->getBounds(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001656 &clipIRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001657
bungeman@google.comb29c8832011-10-10 13:19:10 +00001658 //[Fillable-path -> Pixel-path]
1659 SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1660 fillablePath->transform(matrix, pixelPath);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001661
halcanary96fcdcc2015-08-27 07:41:13 -07001662 SkMask* mask = nullptr;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001663
bsalomon055e1922016-05-06 07:22:58 -07001664 SkASSERT(SkPaint::kFill_Style == paint->getStyle() ||
1665 (SkPaint::kStroke_Style == paint->getStyle() && 0 == paint->getStrokeWidth()));
1666 SkStrokeRec::InitStyle style = (SkPaint::kFill_Style == paint->getStyle())
1667 ? SkStrokeRec::kFill_InitStyle
1668 : SkStrokeRec::kHairline_InitStyle;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001669 //[Pixel-path -> Mask]
1670 SkMask rasteredMask;
1671 if (SkDraw::DrawToMask(
1672 *pixelPath,
1673 &clipIRect,
1674 filter, //just to compute how much to draw.
1675 &matrix,
1676 &rasteredMask,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +00001677 SkMask::kComputeBoundsAndRenderImage_CreateMode,
bsalomon055e1922016-05-06 07:22:58 -07001678 style)) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001679
bungeman@google.comb29c8832011-10-10 13:19:10 +00001680 SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1681 mask = &rasteredMask;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001682
bungeman@google.comb29c8832011-10-10 13:19:10 +00001683 //[Mask -> Mask]
1684 SkMask filteredMask;
robertphillipse80eb922015-12-17 11:33:12 -08001685 if (filter->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001686 mask = &filteredMask;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001687 }
1688 SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001689
bungeman@google.comb29c8832011-10-10 13:19:10 +00001690 //Draw mask.
1691 HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1692 }
1693 return;
1694 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001695
bungeman@google.comb29c8832011-10-10 13:19:10 +00001696 //Get the figures from the shaded geometry.
1697 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1698 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1699 "Could not get shaded figures for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001700
bungeman@google.comb29c8832011-10-10 13:19:10 +00001701 bool xpsTransformsPath = true;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001702
bungeman@google.comb29c8832011-10-10 13:19:10 +00001703 //Set the fill rule.
bungeman76db31a2014-08-25 07:31:53 -07001704 SkPath* xpsCompatiblePath = fillablePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001705 XPS_FILL_RULE xpsFillRule;
bungeman76db31a2014-08-25 07:31:53 -07001706 switch (fillablePath->getFillType()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001707 case SkPath::kWinding_FillType:
1708 xpsFillRule = XPS_FILL_RULE_NONZERO;
1709 break;
1710 case SkPath::kEvenOdd_FillType:
1711 xpsFillRule = XPS_FILL_RULE_EVENODD;
1712 break;
1713 case SkPath::kInverseWinding_FillType: {
bungeman76db31a2014-08-25 07:31:53 -07001714 //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
1715 if (!pathIsMutable) {
1716 xpsCompatiblePath = &modifiedPath;
1717 pathIsMutable = true;
1718 }
1719 if (!Simplify(*fillablePath, xpsCompatiblePath)) {
1720 SkDEBUGF(("Could not simplify inverse winding path."));
1721 return;
1722 }
bungeman@google.comb29c8832011-10-10 13:19:10 +00001723 }
bungeman76db31a2014-08-25 07:31:53 -07001724 // The xpsCompatiblePath is noW inverse even odd, so fall through.
bungeman@google.comb29c8832011-10-10 13:19:10 +00001725 case SkPath::kInverseEvenOdd_FillType: {
1726 const SkRect universe = SkRect::MakeLTRB(
1727 0, 0,
1728 this->fCurrentCanvasSize.fWidth,
1729 this->fCurrentCanvasSize.fHeight);
1730 SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1731 HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1732 HRVM(shadedFigures->Append(addOneFigure.get()),
1733 "Could not add even-odd flip figure to shaded path.");
1734 xpsTransformsPath = false;
1735 xpsFillRule = XPS_FILL_RULE_EVENODD;
1736 break;
1737 }
1738 default:
mtklein@google.com330313a2013-08-22 15:37:26 +00001739 SkDEBUGFAIL("Unknown SkPath::FillType.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001740 }
1741 HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1742 "Could not set fill rule for shaded path.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001743
bungeman@google.comb29c8832011-10-10 13:19:10 +00001744 //Create the XPS transform, if possible.
1745 if (xpsTransformsPath) {
1746 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1747 HRV(this->createXpsTransform(matrix, &xpsTransform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001748
bungeman@google.comb29c8832011-10-10 13:19:10 +00001749 if (xpsTransform.get()) {
1750 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1751 "Could not set transform on shaded path.");
1752 } else {
1753 xpsTransformsPath = false;
1754 }
1755 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001756
bungeman76db31a2014-08-25 07:31:53 -07001757 SkPath* devicePath = xpsCompatiblePath;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001758 if (!xpsTransformsPath) {
1759 //[Fillable-path -> Device-path]
bungeman76db31a2014-08-25 07:31:53 -07001760 devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
1761 xpsCompatiblePath->transform(matrix, devicePath);
bungeman@google.comb29c8832011-10-10 13:19:10 +00001762 }
1763 HRV(this->addXpsPathGeometry(shadedFigures.get(),
1764 stroke, fill, *devicePath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001765
bungeman@google.comb29c8832011-10-10 13:19:10 +00001766 HRV(this->clip(shadedPath.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001767
bungeman@google.comb29c8832011-10-10 13:19:10 +00001768 //Add the path to the active visual collection.
1769 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1770 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1771 "Could not get current visuals for shaded path.");
1772 HRVM(currentVisuals->Append(shadedPath.get()),
1773 "Could not add shaded path to current visuals.");
1774}
1775
1776HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1777 SkPath clipPath;
reed1e7f5e72016-04-27 07:49:17 -07001778 if (d.fRC->isBW()) {
1779 SkAssertResult(d.fRC->bwRgn().getBoundaryPath(&clipPath));
1780 } else {
1781 // Don't have a way to turn a AAClip into a path, so we just use the bounds.
1782 // TODO: consider using fClipStack instead?
1783 clipPath.addRect(SkRect::Make(d.fRC->getBounds()));
1784 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001785
bungeman@google.comb29c8832011-10-10 13:19:10 +00001786 return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1787}
1788HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1789 const SkPath& clipPath,
1790 XPS_FILL_RULE fillRule) {
1791 //Create the geometry.
1792 SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1793 HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1794 "Could not create clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001795
bungeman@google.comb29c8832011-10-10 13:19:10 +00001796 //Get the figure collection of the geometry.
1797 SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1798 HRM(clipGeometry->GetFigures(&clipFigures),
1799 "Could not get the clip figures.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001800
bungeman@google.comb29c8832011-10-10 13:19:10 +00001801 //Create the figures into the geometry.
1802 HR(this->addXpsPathGeometry(
1803 clipFigures.get(),
1804 FALSE, TRUE, clipPath));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001805
bungeman@google.comb29c8832011-10-10 13:19:10 +00001806 HRM(clipGeometry->SetFillRule(fillRule),
1807 "Could not set fill rule.");
1808 HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1809 "Could not set clip geometry.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001810
bungeman@google.comb29c8832011-10-10 13:19:10 +00001811 return S_OK;
1812}
1813
1814void SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001815 const SkMatrix& matrix, const SkPaint& paint) {
reed1e7f5e72016-04-27 07:49:17 -07001816 if (d.fRC->isEmpty()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001817 return;
1818 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001819
bungeman@google.comb29c8832011-10-10 13:19:10 +00001820 SkIRect srcRect;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001821 srcRect.set(0, 0, bitmap.width(), bitmap.height());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001822
bungeman@google.comb29c8832011-10-10 13:19:10 +00001823 //Create the new shaded path.
1824 SkTScopedComPtr<IXpsOMPath> shadedPath;
1825 HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1826 "Could not create path for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001827
bungeman@google.comb29c8832011-10-10 13:19:10 +00001828 //Create the shaded geometry.
1829 SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1830 HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1831 "Could not create geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001832
bungeman@google.comb29c8832011-10-10 13:19:10 +00001833 //Add the shaded geometry to the shaded path.
1834 HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1835 "Could not set the geometry for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001836
bungeman@google.comb29c8832011-10-10 13:19:10 +00001837 //Get the shaded figures from the shaded geometry.
1838 SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1839 HRVM(shadedGeometry->GetFigures(&shadedFigures),
1840 "Could not get the figures for bitmap.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001841
bungeman@google.comb29c8832011-10-10 13:19:10 +00001842 SkMatrix transform = matrix;
1843 transform.postConcat(*d.fMatrix);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001844
bungeman@google.comb29c8832011-10-10 13:19:10 +00001845 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1846 HRV(this->createXpsTransform(transform, &xpsTransform));
1847 if (xpsTransform.get()) {
1848 HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1849 "Could not set transform for bitmap.");
1850 } else {
1851 //TODO: perspective that bitmap!
1852 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001853
bungeman@google.comb29c8832011-10-10 13:19:10 +00001854 SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
bsalomon49f085d2014-09-05 13:34:00 -07001855 if (xpsTransform.get()) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00001856 const SkShader::TileMode xy[2] = {
1857 SkShader::kClamp_TileMode,
1858 SkShader::kClamp_TileMode,
1859 };
1860 SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001861 HRV(this->createXpsImageBrush(bitmap,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001862 transform,
1863 xy,
1864 paint.getAlpha(),
1865 &xpsImageBrush));
1866 HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
1867 "Could not set bitmap brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001868
bungeman@google.comb29c8832011-10-10 13:19:10 +00001869 const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
1870 SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1871 HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
1872 }
1873 HRVM(shadedFigures->Append(rectFigure.get()),
1874 "Could not add bitmap figure.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001875
bungeman@google.comb29c8832011-10-10 13:19:10 +00001876 //Get the current visual collection and add the shaded path to it.
1877 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1878 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1879 "Could not get current visuals for bitmap");
1880 HRVM(currentVisuals->Append(shadedPath.get()),
1881 "Could not add bitmap to current visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001882
bungeman@google.comb29c8832011-10-10 13:19:10 +00001883 HRV(this->clip(shadedPath.get(), d));
1884}
1885
1886void SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
1887 int x, int y,
1888 const SkPaint& paint) {
1889 //TODO: override this for XPS
1890 SkDEBUGF(("XPS drawSprite not yet implemented."));
1891}
1892
1893HRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
1894 TypefaceUse** typefaceUse) {
reed@google.com398de9a2013-03-21 21:43:51 +00001895 SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001896
bungeman@google.comb29c8832011-10-10 13:19:10 +00001897 //Check cache.
reed@google.com398de9a2013-03-21 21:43:51 +00001898 const SkFontID typefaceID = typeface->uniqueID();
bungeman@google.comb29c8832011-10-10 13:19:10 +00001899 if (!this->fTypefaces.empty()) {
1900 TypefaceUse* current = &this->fTypefaces.front();
1901 const TypefaceUse* last = &this->fTypefaces.back();
1902 for (; current <= last; ++current) {
1903 if (current->typefaceId == typefaceID) {
1904 *typefaceUse = current;
1905 return S_OK;
1906 }
1907 }
1908 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001909
bungeman@google.comb29c8832011-10-10 13:19:10 +00001910 //TODO: create glyph only fonts
1911 //and let the host deal with what kind of font we're looking at.
1912 XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001913
bungeman@google.comb29c8832011-10-10 13:19:10 +00001914 SkTScopedComPtr<IStream> fontStream;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001915 int ttcIndex;
1916 SkStream* fontData = typeface->openStream(&ttcIndex);
bungeman@google.com635091f2013-10-01 15:03:18 +00001917 //TODO: cannot handle FON fonts.
bungeman@google.com8cddc8d2012-01-03 22:36:33 +00001918 HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
bungeman@google.comb29c8832011-10-10 13:19:10 +00001919 "Could not create font stream.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001920
bungeman@google.comb29c8832011-10-10 13:19:10 +00001921 const size_t size =
1922 SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1923 wchar_t buffer[size];
1924 wchar_t id[GUID_ID_LEN];
Ben Wagnerda5a1b82014-08-22 15:07:06 -04001925 HR(this->createId(id, GUID_ID_LEN));
bungeman@google.comb29c8832011-10-10 13:19:10 +00001926 swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001927
bungeman@google.comb29c8832011-10-10 13:19:10 +00001928 SkTScopedComPtr<IOpcPartUri> partUri;
1929 HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1930 "Could not create font resource part uri.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001931
bungeman@google.comb29c8832011-10-10 13:19:10 +00001932 SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1933 HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1934 embedding,
1935 partUri.get(),
1936 FALSE,
1937 &xpsFontResource),
1938 "Could not create font resource.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001939
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001940 //TODO: change openStream to return -1 for non-ttc, get rid of this.
1941 uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1942 bool isTTC = (data &&
1943 fontData->getLength() >= sizeof(SkTTCFHeader) &&
1944 ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1945
bungeman@google.comb29c8832011-10-10 13:19:10 +00001946 TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
1947 newTypefaceUse.typefaceId = typefaceID;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001948 newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
bungeman@google.comb29c8832011-10-10 13:19:10 +00001949 newTypefaceUse.fontData = fontData;
1950 newTypefaceUse.xpsFont = xpsFontResource.release();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001951
robertphillips8e0c1502015-07-07 10:28:43 -07001952 SkAutoGlyphCache agc(paint, &this->surfaceProps(), &SkMatrix::I());
bungeman@google.comb29c8832011-10-10 13:19:10 +00001953 SkGlyphCache* glyphCache = agc.getCache();
1954 unsigned int glyphCount = glyphCache->getGlyphCount();
1955 newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001956
bungeman@google.comb29c8832011-10-10 13:19:10 +00001957 *typefaceUse = &newTypefaceUse;
1958 return S_OK;
1959}
1960
1961HRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
1962 IXpsOMObjectFactory* xpsFactory,
1963 IXpsOMCanvas* canvas,
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001964 TypefaceUse* font,
bungeman@google.comb29c8832011-10-10 13:19:10 +00001965 LPCWSTR text,
1966 XPS_GLYPH_INDEX* xpsGlyphs,
1967 UINT32 xpsGlyphsLen,
1968 XPS_POINT *origin,
1969 FLOAT fontSize,
1970 XPS_STYLE_SIMULATION sims,
1971 const SkMatrix& transform,
1972 const SkPaint& paint) {
1973 SkTScopedComPtr<IXpsOMGlyphs> glyphs;
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00001974 HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
1975 HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001976
bungeman@google.comb29c8832011-10-10 13:19:10 +00001977 //XPS uses affine transformations for everything...
1978 //...except positioning text.
1979 bool useCanvasForClip;
1980 if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
1981 origin->x += SkScalarToFLOAT(transform.getTranslateX());
1982 origin->y += SkScalarToFLOAT(transform.getTranslateY());
1983 useCanvasForClip = false;
1984 } else {
1985 SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1986 HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1987 if (xpsMatrixToUse.get()) {
1988 HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1989 "Could not set transform matrix.");
1990 useCanvasForClip = true;
1991 } else {
mtklein@google.com330313a2013-08-22 15:37:26 +00001992 SkDEBUGFAIL("Attempt to add glyphs in perspective.");
bungeman@google.comb29c8832011-10-10 13:19:10 +00001993 useCanvasForClip = false;
1994 }
1995 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001996
bungeman@google.comb29c8832011-10-10 13:19:10 +00001997 SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1998 HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001999
bsalomon49f085d2014-09-05 13:34:00 -07002000 if (text) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002001 HRM(glyphsEditor->SetUnicodeString(text),
2002 "Could not set unicode string.");
2003 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002004
bsalomon49f085d2014-09-05 13:34:00 -07002005 if (xpsGlyphs) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002006 HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
2007 "Could not set glyphs.");
2008 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002009
bungeman@google.comb29c8832011-10-10 13:19:10 +00002010 HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002011
bungeman@google.comb29c8832011-10-10 13:19:10 +00002012 SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
2013 HR(this->createXpsBrush(
2014 paint,
2015 &xpsFillBrush,
halcanary96fcdcc2015-08-27 07:41:13 -07002016 useCanvasForClip ? nullptr : &transform));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002017
bungeman@google.comb29c8832011-10-10 13:19:10 +00002018 HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
2019 "Could not set fill brush.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002020
bungeman@google.comb29c8832011-10-10 13:19:10 +00002021 HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002022
bungeman@google.comb29c8832011-10-10 13:19:10 +00002023 HRM(glyphs->SetFontRenderingEmSize(fontSize),
2024 "Could not set font size.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002025
bungeman@google.comb29c8832011-10-10 13:19:10 +00002026 HRM(glyphs->SetStyleSimulations(sims),
2027 "Could not set style simulations.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002028
bungeman@google.comb29c8832011-10-10 13:19:10 +00002029 SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2030 HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002031
bungeman@google.comb29c8832011-10-10 13:19:10 +00002032 if (!useCanvasForClip) {
2033 HR(this->clip(glyphs.get(), d));
2034 HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2035 } else {
2036 SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2037 HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2038 "Could not create glyph canvas.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002039
bungeman@google.comb29c8832011-10-10 13:19:10 +00002040 SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2041 HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2042 "Could not get glyph visuals collection.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002043
bungeman@google.comb29c8832011-10-10 13:19:10 +00002044 HRM(glyphCanvasVisuals->Append(glyphs.get()),
2045 "Could not add glyphs to page.");
2046 HR(this->clip(glyphCanvas.get(), d));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002047
bungeman@google.comb29c8832011-10-10 13:19:10 +00002048 HRM(visuals->Append(glyphCanvas.get()),
2049 "Could not add glyph canvas to page.");
2050 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002051
bungeman@google.comb29c8832011-10-10 13:19:10 +00002052 return S_OK;
2053}
2054
herbbda26432015-11-24 08:37:01 -08002055static int num_glyph_guess(SkPaint::TextEncoding encoding, const void* text, size_t byteLength) {
2056 switch (encoding) {
2057 case SkPaint::kUTF8_TextEncoding:
2058 return SkUTF8_CountUnichars(static_cast<const char *>(text), byteLength);
2059 case SkPaint::kUTF16_TextEncoding:
2060 return SkUTF16_CountUnichars(static_cast<const uint16_t *>(text), SkToInt(byteLength));
2061 case SkPaint::kGlyphID_TextEncoding:
2062 return SkToInt(byteLength / 2);
2063 default:
djsollenf2b340f2016-01-29 08:51:04 -08002064 SK_ABORT("Invalid Text Encoding");
bungeman@google.comb29c8832011-10-10 13:19:10 +00002065 }
herbbda26432015-11-24 08:37:01 -08002066 return 0;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002067}
2068
2069static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2070 const SkPaint::Style style = paint.getStyle();
2071 return matrix.hasPerspective()
2072 || SkPaint::kStroke_Style == style
2073 || SkPaint::kStrokeAndFill_Style == style
2074 || paint.getMaskFilter()
2075 || paint.getRasterizer()
2076 ;
2077}
2078
herbbda26432015-11-24 08:37:01 -08002079typedef SkTDArray<XPS_GLYPH_INDEX> GlyphRun;
2080
2081class ProcessOneGlyph {
2082public:
2083 ProcessOneGlyph(FLOAT centemPerUnit, SkBitSet* glyphUse, GlyphRun* xpsGlyphs)
2084 : fCentemPerUnit(centemPerUnit)
2085 , fGlyphUse(glyphUse)
2086 , fXpsGlyphs(xpsGlyphs) { }
2087
2088 void operator()(const SkGlyph& glyph, SkPoint position, SkPoint) {
2089 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
2090
2091 SkScalar x = position.fX;
2092 SkScalar y = position.fY;
2093
2094 XPS_GLYPH_INDEX* xpsGlyph = fXpsGlyphs->append();
2095 uint16_t glyphID = glyph.getGlyphID();
2096 fGlyphUse->setBit(glyphID, true);
2097 xpsGlyph->index = glyphID;
2098 if (1 == fXpsGlyphs->count()) {
2099 xpsGlyph->advanceWidth = 0.0f;
2100 xpsGlyph->horizontalOffset = SkScalarToFloat(x) * fCentemPerUnit;
2101 xpsGlyph->verticalOffset = SkScalarToFloat(y) * -fCentemPerUnit;
2102 }
2103 else {
2104 const XPS_GLYPH_INDEX& first = (*fXpsGlyphs)[0];
2105 xpsGlyph->advanceWidth = 0.0f;
2106 xpsGlyph->horizontalOffset = (SkScalarToFloat(x) * fCentemPerUnit)
2107 - first.horizontalOffset;
2108 xpsGlyph->verticalOffset = (SkScalarToFloat(y) * -fCentemPerUnit)
2109 - first.verticalOffset;
2110 }
2111 }
2112
2113private:
2114 /** [in] Advance width and offsets for glyphs measured in
2115 hundredths of the font em size (XPS Spec 5.1.3). */
2116 const FLOAT fCentemPerUnit;
2117 /** [in,out] The accumulated glyphs used in the current typeface. */
2118 SkBitSet* const fGlyphUse;
2119 /** [out] The glyphs to draw. */
2120 GlyphRun* const fXpsGlyphs;
2121};
2122
bungeman@google.comb29c8832011-10-10 13:19:10 +00002123void SkXPSDevice::drawText(const SkDraw& d,
2124 const void* text, size_t byteLen,
2125 SkScalar x, SkScalar y,
2126 const SkPaint& paint) {
2127 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002128
bungeman@google.comb29c8832011-10-10 13:19:10 +00002129 if (text_must_be_pathed(paint, *d.fMatrix)) {
2130 SkPath path;
2131 paint.getTextPath(text, byteLen, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002132 this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002133 //TODO: add automation "text"
2134 return;
2135 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002136
bungeman@google.comb29c8832011-10-10 13:19:10 +00002137 TypefaceUse* typeface;
2138 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002139
herbbda26432015-11-24 08:37:01 -08002140 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002141
herbbda26432015-11-24 08:37:01 -08002142 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2143 SkGlyphCache* cache = autoCache.getCache();
reed@google.com089130c2011-12-12 21:52:18 +00002144
herbbda26432015-11-24 08:37:01 -08002145 // Advance width and offsets for glyphs measured in hundredths of the font em size
2146 // (XPS Spec 5.1.3).
2147 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2148 GlyphRun xpsGlyphs;
2149 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2150 static_cast<const char*>(text), byteLen));
2151
2152 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2153
2154 SkFindAndPlaceGlyph::ProcessText(
2155 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2156 SkPoint{ x, y }, matrix, paint.getTextAlign(), cache, processOneGlyph);
2157
2158 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002159 return;
2160 }
2161
bungeman@google.comb29c8832011-10-10 13:19:10 +00002162 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002163 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2164 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002165 };
herbbda26432015-11-24 08:37:01 -08002166 xpsGlyphs[0].horizontalOffset = 0.0f;
2167 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002168
bungeman@google.comb29c8832011-10-10 13:19:10 +00002169 HRV(AddGlyphs(d,
2170 this->fXpsFactory.get(),
2171 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002172 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002173 nullptr,
herbbda26432015-11-24 08:37:01 -08002174 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002175 &origin,
2176 SkScalarToFLOAT(paint.getTextSize()),
2177 XPS_STYLE_SIMULATION_NONE,
2178 *d.fMatrix,
2179 paint));
2180}
2181
2182void SkXPSDevice::drawPosText(const SkDraw& d,
2183 const void* text, size_t byteLen,
fmalita05c4a432014-09-29 06:29:53 -07002184 const SkScalar pos[], int scalarsPerPos,
2185 const SkPoint& offset, const SkPaint& paint) {
bungeman@google.comb29c8832011-10-10 13:19:10 +00002186 if (byteLen < 1) return;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002187
bungeman@google.comb29c8832011-10-10 13:19:10 +00002188 if (text_must_be_pathed(paint, *d.fMatrix)) {
2189 SkPath path;
2190 //TODO: make this work, Draw currently does not handle as well.
2191 //paint.getTextPath(text, byteLength, x, y, &path);
halcanary96fcdcc2015-08-27 07:41:13 -07002192 //this->drawPath(d, path, paint, nullptr, true);
bungeman@google.comb29c8832011-10-10 13:19:10 +00002193 //TODO: add automation "text"
2194 return;
2195 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002196
bungeman@google.comb29c8832011-10-10 13:19:10 +00002197 TypefaceUse* typeface;
2198 HRV(CreateTypefaceUse(paint, &typeface));
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002199
herbbda26432015-11-24 08:37:01 -08002200 const SkMatrix& matrix = SkMatrix::I();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002201
herbbda26432015-11-24 08:37:01 -08002202 SkAutoGlyphCache autoCache(paint, &this->surfaceProps(), &matrix);
2203 SkGlyphCache* cache = autoCache.getCache();
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002204
herbbda26432015-11-24 08:37:01 -08002205 // Advance width and offsets for glyphs measured in hundredths of the font em size
2206 // (XPS Spec 5.1.3).
2207 FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
2208 GlyphRun xpsGlyphs;
2209 xpsGlyphs.setReserve(num_glyph_guess(paint.getTextEncoding(),
2210 static_cast<const char*>(text), byteLen));
2211
2212 ProcessOneGlyph processOneGlyph(centemPerUnit, typeface->glyphsUsed, &xpsGlyphs);
2213
2214 SkFindAndPlaceGlyph::ProcessPosText(
2215 paint.getTextEncoding(), static_cast<const char*>(text), byteLen,
2216 offset, matrix, pos, scalarsPerPos, paint.getTextAlign(), cache, processOneGlyph);
2217
2218 if (xpsGlyphs.count() == 0) {
reed@google.com089130c2011-12-12 21:52:18 +00002219 return;
2220 }
2221
bungeman@google.comb29c8832011-10-10 13:19:10 +00002222 XPS_POINT origin = {
herbbda26432015-11-24 08:37:01 -08002223 xpsGlyphs[0].horizontalOffset / centemPerUnit,
2224 xpsGlyphs[0].verticalOffset / -centemPerUnit,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002225 };
herbbda26432015-11-24 08:37:01 -08002226 xpsGlyphs[0].horizontalOffset = 0.0f;
2227 xpsGlyphs[0].verticalOffset = 0.0f;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002228
bungeman@google.comb29c8832011-10-10 13:19:10 +00002229 HRV(AddGlyphs(d,
2230 this->fXpsFactory.get(),
2231 this->fCurrentXpsCanvas.get(),
commit-bot@chromium.orgb5e34e22013-05-07 15:28:15 +00002232 typeface,
halcanary96fcdcc2015-08-27 07:41:13 -07002233 nullptr,
herbbda26432015-11-24 08:37:01 -08002234 xpsGlyphs.begin(), xpsGlyphs.count(),
bungeman@google.comb29c8832011-10-10 13:19:10 +00002235 &origin,
2236 SkScalarToFLOAT(paint.getTextSize()),
2237 XPS_STYLE_SIMULATION_NONE,
2238 *d.fMatrix,
2239 paint));
2240}
2241
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002242void SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
bungeman@google.comb29c8832011-10-10 13:19:10 +00002243 int x, int y,
2244 const SkPaint&) {
2245 SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002246
bungeman@google.comb29c8832011-10-10 13:19:10 +00002247 SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2248 XPS_MATRIX rawTransform = {
2249 1.0f,
2250 0.0f,
2251 0.0f,
2252 1.0f,
2253 static_cast<FLOAT>(x),
2254 static_cast<FLOAT>(y),
2255 };
2256 HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2257 "Could not create layer transform.");
2258 HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2259 "Could not set layer transform.");
vandebo@chromium.org74b46192012-01-28 01:45:11 +00002260
bungeman@google.comb29c8832011-10-10 13:19:10 +00002261 //Get the current visual collection and add the layer to it.
2262 SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2263 HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2264 "Could not get current visuals for layer.");
2265 HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2266 "Could not add layer to current visuals.");
2267}
2268
reed76033be2015-03-14 10:54:31 -07002269SkBaseDevice* SkXPSDevice::onCreateDevice(const CreateInfo& info, const SkPaint*) {
bungeman@google.com635091f2013-10-01 15:03:18 +00002270//Conditional for bug compatibility with PDF device.
2271#if 0
fmalita6987dca2014-11-13 08:33:37 -08002272 if (SkBaseDevice::kGeneral_Usage == info.fUsage) {
halcanary96fcdcc2015-08-27 07:41:13 -07002273 return nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +00002274 //To what stream do we write?
2275 //SkXPSDevice* dev = new SkXPSDevice(this);
2276 //SkSize s = SkSize::Make(width, height);
2277 //dev->BeginCanvas(s, s, SkMatrix::I());
2278 //return dev;
2279 }
bungeman@google.com635091f2013-10-01 15:03:18 +00002280#endif
bungeman@google.comb29c8832011-10-10 13:19:10 +00002281 return new SkXPSDevice(this->fXpsFactory.get());
2282}
2283
mtklein1ee76512015-11-02 10:20:27 -08002284#endif//defined(SK_BUILD_FOR_WIN32)