| 1 |
/* |
| 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 |
|
| 8 |
#include "include/core/SkTypes.h" |
| 9 |
#if defined(SK_BUILD_FOR_WIN) |
| 10 |
|
| 11 |
#include "src/base/SkLeanWindows.h" |
| 12 |
|
| 13 |
#ifndef UNICODE |
| 14 |
#define UNICODE |
| 15 |
#endif |
| 16 |
#ifndef _UNICODE |
| 17 |
#define _UNICODE |
| 18 |
#endif |
| 19 |
#include <ObjBase.h> |
| 20 |
#include <XpsObjectModel.h> |
| 21 |
#include <T2EmbApi.h> |
| 22 |
#include <FontSub.h> |
| 23 |
#include <limits> |
| 24 |
|
| 25 |
#include "include/core/SkColor.h" |
| 26 |
#include "include/core/SkData.h" |
| 27 |
#include "include/core/SkImage.h" |
| 28 |
#include "include/core/SkPaint.h" |
| 29 |
#include "include/core/SkPathEffect.h" |
| 30 |
#include "include/core/SkPathUtils.h" |
| 31 |
#include "include/core/SkPoint.h" |
| 32 |
#include "include/core/SkShader.h" |
| 33 |
#include "include/core/SkSize.h" |
| 34 |
#include "include/core/SkStream.h" |
| 35 |
#include "include/core/SkVertices.h" |
| 36 |
#include "include/pathops/SkPathOps.h" |
| 37 |
#include "include/private/base/SkTDArray.h" |
| 38 |
#include "include/private/base/SkTo.h" |
| 39 |
#include "src/base/SkEndian.h" |
| 40 |
#include "src/base/SkTLazy.h" |
| 41 |
#include "src/base/SkUtils.h" |
| 42 |
#include "src/core/SkDraw.h" |
| 43 |
#include "src/core/SkFontPriv.h" |
| 44 |
#include "src/core/SkGeometry.h" |
| 45 |
#include "src/core/SkMaskFilterBase.h" |
| 46 |
#include "src/core/SkPathPriv.h" |
| 47 |
#include "src/core/SkRasterClip.h" |
| 48 |
#include "src/core/SkStrikeCache.h" |
| 49 |
#include "src/image/SkImage_Base.h" |
| 50 |
#include "src/image/SkImage_Raster.h" |
| 51 |
#include "src/sfnt/SkSFNTHeader.h" |
| 52 |
#include "src/sfnt/SkTTCFHeader.h" |
| 53 |
#include "src/shaders/SkColorShader.h" |
| 54 |
#include "src/shaders/SkImageShader.h" |
| 55 |
#include "src/shaders/SkShaderBase.h" |
| 56 |
#include "src/text/GlyphRun.h" |
| 57 |
#include "src/utils/SkClipStackUtils.h" |
| 58 |
#include "src/utils/win/SkHRESULT.h" |
| 59 |
#include "src/utils/win/SkIStream.h" |
| 60 |
#include "src/utils/win/SkTScopedComPtr.h" |
| 61 |
#include "src/xps/SkXPSDevice.h" |
| 62 |
|
| 63 |
using namespace skia_private; |
| 64 |
|
| 65 |
//Windows defines a FLOAT type, |
| 66 |
//make it clear when converting a scalar that this is what is wanted. |
| 67 |
#define SkScalarToFLOAT(n) SkScalarToFloat(n) |
| 68 |
|
| 69 |
//Placeholder representation of a GUID from createId. |
| 70 |
#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX" |
| 71 |
//Length of GUID representation from createId, including nullptr terminator. |
| 72 |
#define GUID_ID_LEN std::size(L_GUID_ID) |
| 73 |
|
| 74 |
/** |
| 75 |
Formats a GUID and places it into buffer. |
| 76 |
buffer should have space for at least GUID_ID_LEN wide characters. |
| 77 |
The string will always be wchar null terminated. |
| 78 |
XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0 |
| 79 |
@return -1 if there was an error, > 0 if success. |
| 80 |
*/ |
| 81 |
static int format_guid(const GUID& guid, |
| 82 |
wchar_t* buffer, size_t bufferSize, |
| 83 |
wchar_t sep = '-') { |
| 84 |
SkASSERT(bufferSize >= GUID_ID_LEN); |
| 85 |
return swprintf_s(buffer, |
| 86 |
bufferSize, |
| 87 |
L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X", |
| 88 |
guid.Data1, |
| 89 |
sep, |
| 90 |
guid.Data2, |
| 91 |
sep, |
| 92 |
guid.Data3, |
| 93 |
sep, |
| 94 |
guid.Data4[0], |
| 95 |
guid.Data4[1], |
| 96 |
sep, |
| 97 |
guid.Data4[2], |
| 98 |
guid.Data4[3], |
| 99 |
guid.Data4[4], |
| 100 |
guid.Data4[5], |
| 101 |
guid.Data4[6], |
| 102 |
guid.Data4[7]); |
| 103 |
} |
| 104 |
|
| 105 |
HRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) { |
| 106 |
GUID guid = {}; |
| 107 |
#ifdef SK_XPS_USE_DETERMINISTIC_IDS |
| 108 |
guid.Data1 = fNextId++; |
| 109 |
// The following make this a valid Type4 UUID. |
| 110 |
guid.Data3 = 0x4000; |
| 111 |
guid.Data4[0] = 0x80; |
| 112 |
#else |
| 113 |
HRM(CoCreateGuid(&guid), "Could not create GUID for id."); |
| 114 |
#endif |
| 115 |
|
| 116 |
if (format_guid(guid, buffer, bufferSize, sep) == -1) { |
| 117 |
HRM(E_UNEXPECTED, "Could not format GUID into id."); |
| 118 |
} |
| 119 |
|
| 120 |
return S_OK; |
| 121 |
} |
| 122 |
|
| 123 |
SkXPSDevice::SkXPSDevice(SkISize s, SkXPS::Options opts) |
| 124 |
: SkClipStackDevice(SkImageInfo::MakeUnknown(s.width(), s.height()), SkSurfaceProps()) |
| 125 |
, fCurrentPage(0) |
| 126 |
, fTopTypefaces(&fTypefaces) |
| 127 |
, fOpts(opts) {} |
| 128 |
|
| 129 |
SkXPSDevice::~SkXPSDevice() {} |
| 130 |
|
| 131 |
bool SkXPSDevice::beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory* factory) { |
| 132 |
SkASSERT(factory); |
| 133 |
fXpsFactory.reset(SkRefComPtr(factory)); |
| 134 |
HRB(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream)); |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
bool SkXPSDevice::beginSheet( |
| 139 |
const SkVector& unitsPerMeter, |
| 140 |
const SkVector& pixelsPerMeter, |
| 141 |
const SkSize& trimSize, |
| 142 |
const SkRect* mediaBox, |
| 143 |
const SkRect* bleedBox, |
| 144 |
const SkRect* artBox, |
| 145 |
const SkRect* cropBox) { |
| 146 |
++this->fCurrentPage; |
| 147 |
|
| 148 |
//For simplicity, just write everything out in geometry units, |
| 149 |
//then have a base canvas do the scale to physical units. |
| 150 |
this->fCurrentCanvasSize = trimSize; |
| 151 |
this->fCurrentUnitsPerMeter = unitsPerMeter; |
| 152 |
this->fCurrentPixelsPerMeter = pixelsPerMeter; |
| 153 |
return this->createCanvasForLayer(); |
| 154 |
} |
| 155 |
|
| 156 |
bool SkXPSDevice::createCanvasForLayer() { |
| 157 |
SkASSERT(fXpsFactory); |
| 158 |
fCurrentXpsCanvas.reset(); |
| 159 |
HRB(fXpsFactory->CreateCanvas(&fCurrentXpsCanvas)); |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
template <typename T> static constexpr size_t sk_digits_in() { |
| 164 |
return static_cast<size_t>(std::numeric_limits<T>::digits10 + 1); |
| 165 |
} |
| 166 |
|
| 167 |
HRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page, |
| 168 |
const unsigned int pageNum, |
| 169 |
IXpsOMImageResource** image) { |
| 170 |
SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator; |
| 171 |
HRM(CoCreateInstance( |
| 172 |
CLSID_XpsOMThumbnailGenerator, |
| 173 |
nullptr, |
| 174 |
CLSCTX_INPROC_SERVER, |
| 175 |
IID_PPV_ARGS(&thumbnailGenerator)), |
| 176 |
"Could not create thumbnail generator."); |
| 177 |
|
| 178 |
SkTScopedComPtr<IOpcPartUri> partUri; |
| 179 |
constexpr size_t size = std::max( |
| 180 |
std::size(L"/Documents/1/Metadata/.png") + sk_digits_in<decltype(pageNum)>(), |
| 181 |
std::size(L"/Metadata/" L_GUID_ID L".png")); |
| 182 |
wchar_t buffer[size]; |
| 183 |
if (pageNum > 0) { |
| 184 |
swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum); |
| 185 |
} else { |
| 186 |
wchar_t id[GUID_ID_LEN]; |
| 187 |
HR(this->createId(id, GUID_ID_LEN)); |
| 188 |
swprintf_s(buffer, size, L"/Metadata/%s.png", id); |
| 189 |
} |
| 190 |
HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri), |
| 191 |
"Could not create thumbnail part uri."); |
| 192 |
|
| 193 |
HRM(thumbnailGenerator->GenerateThumbnail(page, |
| 194 |
XPS_IMAGE_TYPE_PNG, |
| 195 |
XPS_THUMBNAIL_SIZE_LARGE, |
| 196 |
partUri.get(), |
| 197 |
image), |
| 198 |
"Could not generate thumbnail."); |
| 199 |
|
| 200 |
return S_OK; |
| 201 |
} |
| 202 |
|
| 203 |
HRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize, |
| 204 |
IXpsOMPage** page) { |
| 205 |
constexpr size_t size = |
| 206 |
std::size(L"/Documents/1/Pages/.fpage") |
| 207 |
+ sk_digits_in<decltype(fCurrentPage)>(); |
| 208 |
wchar_t buffer[size]; |
| 209 |
swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage", |
| 210 |
this->fCurrentPage); |
| 211 |
SkTScopedComPtr<IOpcPartUri> partUri; |
| 212 |
HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri), |
| 213 |
"Could not create page part uri."); |
| 214 |
|
| 215 |
//If the language is unknown, use "und" (XPS Spec 2.3.5.1). |
| 216 |
HRM(this->fXpsFactory->CreatePage(&pageSize, |
| 217 |
L"und", |
| 218 |
partUri.get(), |
| 219 |
page), |
| 220 |
"Could not create page."); |
| 221 |
|
| 222 |
return S_OK; |
| 223 |
} |
| 224 |
|
| 225 |
HRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) { |
| 226 |
//Create package writer. |
| 227 |
{ |
| 228 |
SkTScopedComPtr<IOpcPartUri> partUri; |
| 229 |
HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq", |
| 230 |
&partUri), |
| 231 |
"Could not create document sequence part uri."); |
| 232 |
HRM(this->fXpsFactory->CreatePackageWriterOnStream( |
| 233 |
this->fOutputStream.get(), |
| 234 |
TRUE, |
| 235 |
XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON, |
| 236 |
partUri.get(), |
| 237 |
nullptr, |
| 238 |
image, |
| 239 |
nullptr, |
| 240 |
nullptr, |
| 241 |
&this->fPackageWriter), |
| 242 |
"Could not create package writer."); |
| 243 |
} |
| 244 |
|
| 245 |
//Begin the lone document. |
| 246 |
{ |
| 247 |
SkTScopedComPtr<IOpcPartUri> partUri; |
| 248 |
HRM(this->fXpsFactory->CreatePartUri( |
| 249 |
L"/Documents/1/FixedDocument.fdoc", |
| 250 |
&partUri), |
| 251 |
"Could not create fixed document part uri."); |
| 252 |
HRM(this->fPackageWriter->StartNewDocument(partUri.get(), |
| 253 |
nullptr, |
| 254 |
nullptr, |
| 255 |
nullptr, |
| 256 |
nullptr), |
| 257 |
"Could not start document."); |
| 258 |
} |
| 259 |
|
| 260 |
return S_OK; |
| 261 |
} |
| 262 |
|
| 263 |
bool SkXPSDevice::endSheet() { |
| 264 |
//XPS is fixed at 96dpi (XPS Spec 11.1). |
| 265 |
static const float xpsDPI = 96.0f; |
| 266 |
static const float inchesPerMeter = 10000.0f / 254.0f; |
| 267 |
static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter; |
| 268 |
const float scaleX = targetUnitsPerMeter |
| 269 |
/ SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX); |
| 270 |
const float scaleY = targetUnitsPerMeter |
| 271 |
/ SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY); |
| 272 |
|
| 273 |
//Create the scale canvas. |
| 274 |
SkTScopedComPtr<IXpsOMCanvas> scaleCanvas; |
| 275 |
HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas), |
| 276 |
"Could not create scale canvas."); |
| 277 |
SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals; |
| 278 |
HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals), |
| 279 |
"Could not get scale canvas visuals."); |
| 280 |
|
| 281 |
SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys; |
| 282 |
XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, }; |
| 283 |
HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys), |
| 284 |
"Could not create geometry to physical transform."); |
| 285 |
HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()), |
| 286 |
"Could not set transform on scale canvas."); |
| 287 |
|
| 288 |
//Add the content canvas to the scale canvas. |
| 289 |
HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()), |
| 290 |
"Could not add base canvas to scale canvas."); |
| 291 |
|
| 292 |
//Create the page. |
| 293 |
XPS_SIZE pageSize = { |
| 294 |
SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX, |
| 295 |
SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY, |
| 296 |
}; |
| 297 |
SkTScopedComPtr<IXpsOMPage> page; |
| 298 |
HRB(this->createXpsPage(pageSize, &page)); |
| 299 |
|
| 300 |
SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals; |
| 301 |
HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals."); |
| 302 |
|
| 303 |
//Add the scale canvas to the page. |
| 304 |
HRBM(pageVisuals->Append(scaleCanvas.get()), |
| 305 |
"Could not add scale canvas to page."); |
| 306 |
|
| 307 |
//Create the package writer if it hasn't been created yet. |
| 308 |
if (nullptr == this->fPackageWriter.get()) { |
| 309 |
SkTScopedComPtr<IXpsOMImageResource> image; |
| 310 |
//Ignore return, thumbnail is completely optional. |
| 311 |
this->createXpsThumbnail(page.get(), 0, &image); |
| 312 |
|
| 313 |
HRB(this->initXpsDocumentWriter(image.get())); |
| 314 |
} |
| 315 |
|
| 316 |
HRBM(this->fPackageWriter->AddPage(page.get(), |
| 317 |
&pageSize, |
| 318 |
nullptr, |
| 319 |
nullptr, |
| 320 |
nullptr, |
| 321 |
nullptr), |
| 322 |
"Could not write the page."); |
| 323 |
this->fCurrentXpsCanvas.reset(); |
| 324 |
|
| 325 |
return true; |
| 326 |
} |
| 327 |
|
| 328 |
static HRESULT subset_typeface(const SkXPSDevice::TypefaceUse& current) { |
| 329 |
//The CreateFontPackage API is only supported on desktop, not in UWP |
| 330 |
#if defined(SK_WINUWP) |
| 331 |
return E_NOTIMPL; |
| 332 |
#else |
| 333 |
//CreateFontPackage wants unsigned short. |
| 334 |
//Microsoft, Y U NO stdint.h? |
| 335 |
std::vector<unsigned short> keepList; |
| 336 |
current.glyphsUsed.forEachSetIndex([&keepList](size_t v) { |
| 337 |
keepList.push_back((unsigned short)v); |
| 338 |
}); |
| 339 |
|
| 340 |
int ttcCount = (current.ttcIndex + 1); |
| 341 |
|
| 342 |
//The following are declared with the types required by CreateFontPackage. |
| 343 |
unsigned char *fontPackageBufferRaw = nullptr; |
| 344 |
unsigned long fontPackageBufferSize; |
| 345 |
unsigned long bytesWritten; |
| 346 |
unsigned long result = CreateFontPackage( |
| 347 |
(const unsigned char *) current.fontData->getMemoryBase(), |
| 348 |
(unsigned long) current.fontData->getLength(), |
| 349 |
&fontPackageBufferRaw, |
| 350 |
&fontPackageBufferSize, |
| 351 |
&bytesWritten, |
| 352 |
TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0), |
| 353 |
current.ttcIndex, |
| 354 |
TTFCFP_SUBSET, |
| 355 |
0, |
| 356 |
0, |
| 357 |
0, |
| 358 |
keepList.data(), |
| 359 |
SkTo<unsigned short>(keepList.size()), |
| 360 |
sk_malloc_throw, |
| 361 |
sk_realloc_throw, |
| 362 |
sk_free, |
| 363 |
nullptr); |
| 364 |
AutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw); |
| 365 |
if (result != NO_ERROR) { |
| 366 |
SkDEBUGF("CreateFontPackage Error %lu", result); |
| 367 |
return E_UNEXPECTED; |
| 368 |
} |
| 369 |
|
| 370 |
// If it was originally a ttc, keep it a ttc. |
| 371 |
// CreateFontPackage over-allocates, realloc usually decreases the size substantially. |
| 372 |
size_t extra; |
| 373 |
if (ttcCount > 0) { |
| 374 |
// Create space for a ttc header. |
| 375 |
extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG)); |
| 376 |
fontPackageBuffer.realloc(bytesWritten + extra); |
| 377 |
//overlap is certain, use memmove |
| 378 |
memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten); |
| 379 |
|
| 380 |
// Write the ttc header. |
| 381 |
SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get()); |
| 382 |
ttcfHeader->ttcTag = SkTTCFHeader::TAG; |
| 383 |
ttcfHeader->version = SkTTCFHeader::version_1; |
| 384 |
ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount); |
| 385 |
SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader); |
| 386 |
for (int i = 0; i < ttcCount; ++i, ++offsetPtr) { |
| 387 |
*offsetPtr = SkEndian_SwapBE32(SkToU32(extra)); |
| 388 |
} |
| 389 |
|
| 390 |
// Fix up offsets in sfnt table entries. |
| 391 |
SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra); |
| 392 |
int numTables = SkEndian_SwapBE16(sfntHeader->numTables); |
| 393 |
SkSFNTHeader::TableDirectoryEntry* tableDirectory = |
| 394 |
SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader); |
| 395 |
for (int i = 0; i < numTables; ++i, ++tableDirectory) { |
| 396 |
tableDirectory->offset = SkEndian_SwapBE32( |
| 397 |
SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra)); |
| 398 |
} |
| 399 |
} else { |
| 400 |
extra = 0; |
| 401 |
fontPackageBuffer.realloc(bytesWritten); |
| 402 |
} |
| 403 |
|
| 404 |
std::unique_ptr<SkMemoryStream> newStream(new SkMemoryStream()); |
| 405 |
newStream->setMemoryOwned(fontPackageBuffer.release(), bytesWritten + extra); |
| 406 |
|
| 407 |
SkTScopedComPtr<IStream> newIStream; |
| 408 |
SkIStream::CreateFromSkStream(std::move(newStream), &newIStream); |
| 409 |
|
| 410 |
XPS_FONT_EMBEDDING embedding; |
| 411 |
HRM(current.xpsFont->GetEmbeddingOption(&embedding), |
| 412 |
"Could not get embedding option from font."); |
| 413 |
|
| 414 |
SkTScopedComPtr<IOpcPartUri> partUri; |
| 415 |
HRM(current.xpsFont->GetPartName(&partUri), |
| 416 |
"Could not get part uri from font."); |
| 417 |
|
| 418 |
HRM(current.xpsFont->SetContent( |
| 419 |
newIStream.get(), |
| 420 |
embedding, |
| 421 |
partUri.get()), |
| 422 |
"Could not set new stream for subsetted font."); |
| 423 |
|
| 424 |
return S_OK; |
| 425 |
#endif //SK_WINUWP |
| 426 |
} |
| 427 |
|
| 428 |
bool SkXPSDevice::endPortfolio() { |
| 429 |
//Subset fonts |
| 430 |
for (const TypefaceUse& current : *this->fTopTypefaces) { |
| 431 |
//Ignore return for now, if it didn't subset, let it be. |
| 432 |
subset_typeface(current); |
| 433 |
} |
| 434 |
|
| 435 |
if (this->fPackageWriter) { |
| 436 |
HRBM(this->fPackageWriter->Close(), "Could not close writer."); |
| 437 |
} |
| 438 |
|
| 439 |
return true; |
| 440 |
} |
| 441 |
|
| 442 |
static XPS_COLOR xps_color(const SkColor4f skColor) { |
| 443 |
//XPS uses non-pre-multiplied alpha (XPS Spec 11.4). |
| 444 |
XPS_COLOR xpsColor; |
| 445 |
xpsColor.colorType = XPS_COLOR_TYPE_SCRGB; |
| 446 |
xpsColor.value.scRGB.alpha = skColor.fA; |
| 447 |
xpsColor.value.scRGB.red = skColor.fR; |
| 448 |
xpsColor.value.scRGB.green = skColor.fG; |
| 449 |
xpsColor.value.scRGB.blue = skColor.fB; |
| 450 |
|
| 451 |
return xpsColor; |
| 452 |
} |
| 453 |
|
| 454 |
static XPS_POINT xps_point(const SkPoint& point) { |
| 455 |
XPS_POINT xpsPoint = { |
| 456 |
SkScalarToFLOAT(point.fX), |
| 457 |
SkScalarToFLOAT(point.fY), |
| 458 |
}; |
| 459 |
return xpsPoint; |
| 460 |
} |
| 461 |
|
| 462 |
static XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) { |
| 463 |
return xps_point(matrix.mapPoint(point)); |
| 464 |
} |
| 465 |
|
| 466 |
static XPS_SPREAD_METHOD xps_spread_method(SkTileMode tileMode) { |
| 467 |
switch (tileMode) { |
| 468 |
case SkTileMode::kClamp: |
| 469 |
return XPS_SPREAD_METHOD_PAD; |
| 470 |
case SkTileMode::kRepeat: |
| 471 |
return XPS_SPREAD_METHOD_REPEAT; |
| 472 |
case SkTileMode::kMirror: |
| 473 |
return XPS_SPREAD_METHOD_REFLECT; |
| 474 |
case SkTileMode::kDecal: |
| 475 |
// TODO: fake |
| 476 |
return XPS_SPREAD_METHOD_PAD; |
| 477 |
default: |
| 478 |
SkDEBUGFAIL("Unknown tile mode."); |
| 479 |
} |
| 480 |
return XPS_SPREAD_METHOD_PAD; |
| 481 |
} |
| 482 |
|
| 483 |
static void transform_offsets(SkScalar* stopOffsets, const int numOffsets, |
| 484 |
const SkPoint& start, const SkPoint& end, |
| 485 |
const SkMatrix& transform) { |
| 486 |
SkPoint startTransformed = transform.mapPoint(start); |
| 487 |
SkPoint endTransformed = transform.mapPoint(end); |
| 488 |
|
| 489 |
// Manhattan distance between transformed start and end. |
| 490 |
SkScalar startToEnd = (endTransformed.fX - startTransformed.fX) |
| 491 |
+ (endTransformed.fY - startTransformed.fY); |
| 492 |
if (SkScalarNearlyZero(startToEnd)) { |
| 493 |
for (int i = 0; i < numOffsets; ++i) { |
| 494 |
stopOffsets[i] = 0; |
| 495 |
} |
| 496 |
return; |
| 497 |
} |
| 498 |
|
| 499 |
for (int i = 0; i < numOffsets; ++i) { |
| 500 |
SkPoint stop; |
| 501 |
stop.fX = (end.fX - start.fX) * stopOffsets[i]; |
| 502 |
stop.fY = (end.fY - start.fY) * stopOffsets[i]; |
| 503 |
|
| 504 |
SkPoint stopTransformed = transform.mapPoint(stop); |
| 505 |
|
| 506 |
//Manhattan distance between transformed start and stop. |
| 507 |
SkScalar startToStop = (stopTransformed.fX - startTransformed.fX) |
| 508 |
+ (stopTransformed.fY - startTransformed.fY); |
| 509 |
//Percentage along transformed line. |
| 510 |
stopOffsets[i] = startToStop / startToEnd; |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
HRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix, |
| 515 |
IXpsOMMatrixTransform** xpsTransform) { |
| 516 |
SkScalar affine[6]; |
| 517 |
if (!matrix.asAffine(affine)) { |
| 518 |
*xpsTransform = nullptr; |
| 519 |
return S_FALSE; |
| 520 |
} |
| 521 |
XPS_MATRIX rawXpsMatrix = { |
| 522 |
SkScalarToFLOAT(affine[SkMatrix::kAScaleX]), |
| 523 |
SkScalarToFLOAT(affine[SkMatrix::kASkewY]), |
| 524 |
SkScalarToFLOAT(affine[SkMatrix::kASkewX]), |
| 525 |
SkScalarToFLOAT(affine[SkMatrix::kAScaleY]), |
| 526 |
SkScalarToFLOAT(affine[SkMatrix::kATransX]), |
| 527 |
SkScalarToFLOAT(affine[SkMatrix::kATransY]), |
| 528 |
}; |
| 529 |
HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform), |
| 530 |
"Could not create transform."); |
| 531 |
|
| 532 |
return S_OK; |
| 533 |
} |
| 534 |
|
| 535 |
HRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure, |
| 536 |
IXpsOMVisualCollection* visuals, |
| 537 |
IXpsOMPath** path) { |
| 538 |
SkTScopedComPtr<IXpsOMGeometry> geometry; |
| 539 |
HRM(this->fXpsFactory->CreateGeometry(&geometry), |
| 540 |
"Could not create geometry."); |
| 541 |
|
| 542 |
SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection; |
| 543 |
HRM(geometry->GetFigures(&figureCollection), "Could not get figures."); |
| 544 |
HRM(figureCollection->Append(figure), "Could not add figure."); |
| 545 |
|
| 546 |
HRM(this->fXpsFactory->CreatePath(path), "Could not create path."); |
| 547 |
HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry"); |
| 548 |
|
| 549 |
HRM(visuals->Append(*path), "Could not add path to visuals."); |
| 550 |
return S_OK; |
| 551 |
} |
| 552 |
|
| 553 |
HRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor4f skColor, |
| 554 |
const SkAlpha alpha, |
| 555 |
IXpsOMBrush** xpsBrush) { |
| 556 |
XPS_COLOR xpsColor = xps_color(skColor); |
| 557 |
SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush; |
| 558 |
HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, nullptr, &solidBrush), |
| 559 |
"Could not create solid color brush."); |
| 560 |
HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity."); |
| 561 |
HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail."); |
| 562 |
return S_OK; |
| 563 |
} |
| 564 |
|
| 565 |
HRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill, |
| 566 |
const XPS_RECT& imageViewBox, |
| 567 |
IXpsOMImageResource* image, |
| 568 |
IXpsOMVisualCollection* visuals) { |
| 569 |
SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure; |
| 570 |
HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure)); |
| 571 |
|
| 572 |
SkTScopedComPtr<IXpsOMPath> areaToFillPath; |
| 573 |
HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath)); |
| 574 |
|
| 575 |
SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush; |
| 576 |
HRM(this->fXpsFactory->CreateImageBrush(image, |
| 577 |
&imageViewBox, |
| 578 |
&imageViewBox, |
| 579 |
&areaToFillBrush), |
| 580 |
"Could not create brush for side of clamp."); |
| 581 |
HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY), |
| 582 |
"Could not set tile mode for side of clamp."); |
| 583 |
HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()), |
| 584 |
"Could not set brush for side of clamp"); |
| 585 |
|
| 586 |
return S_OK; |
| 587 |
} |
| 588 |
|
| 589 |
HRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill, |
| 590 |
const SkColor4f color, |
| 591 |
IXpsOMVisualCollection* visuals) { |
| 592 |
SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure; |
| 593 |
HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure)); |
| 594 |
|
| 595 |
SkTScopedComPtr<IXpsOMPath> areaToFillPath; |
| 596 |
HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath)); |
| 597 |
|
| 598 |
SkTScopedComPtr<IXpsOMBrush> areaToFillBrush; |
| 599 |
HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush)); |
| 600 |
HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()), |
| 601 |
"Could not set brush for corner of clamp."); |
| 602 |
|
| 603 |
return S_OK; |
| 604 |
} |
| 605 |
|
| 606 |
static const XPS_TILE_MODE XTM_N = XPS_TILE_MODE_NONE; |
| 607 |
static const XPS_TILE_MODE XTM_T = XPS_TILE_MODE_TILE; |
| 608 |
static const XPS_TILE_MODE XTM_X = XPS_TILE_MODE_FLIPX; |
| 609 |
static const XPS_TILE_MODE XTM_Y = XPS_TILE_MODE_FLIPY; |
| 610 |
static const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY; |
| 611 |
|
| 612 |
//TODO(bungeman): In the future, should skia add None, |
| 613 |
//handle None+Mirror and None+Repeat correctly. |
| 614 |
//None is currently an internal hack so masks don't repeat (None+None only). |
| 615 |
static XPS_TILE_MODE gSkToXpsTileMode[kSkTileModeCount+1] |
| 616 |
[kSkTileModeCount+1] = { |
| 617 |
//Clamp //Repeat //Mirror //None |
| 618 |
/*Clamp */ {XTM_N, XTM_T, XTM_Y, XTM_N}, |
| 619 |
/*Repeat*/ {XTM_T, XTM_T, XTM_Y, XTM_N}, |
| 620 |
/*Mirror*/ {XTM_X, XTM_X, XTM_XY, XTM_X}, |
| 621 |
/*None */ {XTM_N, XTM_N, XTM_Y, XTM_N}, |
| 622 |
}; |
| 623 |
|
| 624 |
static XPS_TILE_MODE SkToXpsTileMode(SkTileMode tmx, SkTileMode tmy) { |
| 625 |
return gSkToXpsTileMode[(unsigned)tmx][(unsigned)tmy]; |
| 626 |
} |
| 627 |
|
| 628 |
HRESULT SkXPSDevice::createXpsImageBrush( |
| 629 |
const SkPixmap& bitmap, |
| 630 |
const SkMatrix& localMatrix, |
| 631 |
const SkTileMode (&xy)[2], |
| 632 |
const SkAlpha alpha, |
| 633 |
IXpsOMTileBrush** xpsBrush) { |
| 634 |
SkDynamicMemoryWStream write; |
| 635 |
if (!fOpts.pngEncoder) { |
| 636 |
HRM(E_FAIL, "No PNG encoder registered when document created."); |
| 637 |
} |
| 638 |
if (!fOpts.pngEncoder(&write, bitmap)) { |
| 639 |
HRM(E_FAIL, "Unable to encode bitmap as png."); |
| 640 |
} |
| 641 |
SkTScopedComPtr<IStream> read; |
| 642 |
HRM(SkIStream::CreateFromSkStream(write.detachAsStream(), &read), |
| 643 |
"Could not create stream from png data."); |
| 644 |
|
| 645 |
const size_t size = |
| 646 |
std::size(L"/Documents/1/Resources/Images/" L_GUID_ID L".png"); |
| 647 |
wchar_t buffer[size]; |
| 648 |
wchar_t id[GUID_ID_LEN]; |
| 649 |
HR(this->createId(id, GUID_ID_LEN)); |
| 650 |
swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id); |
| 651 |
|
| 652 |
SkTScopedComPtr<IOpcPartUri> imagePartUri; |
| 653 |
HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri), |
| 654 |
"Could not create image part uri."); |
| 655 |
|
| 656 |
SkTScopedComPtr<IXpsOMImageResource> imageResource; |
| 657 |
HRM(this->fXpsFactory->CreateImageResource( |
| 658 |
read.get(), |
| 659 |
XPS_IMAGE_TYPE_PNG, |
| 660 |
imagePartUri.get(), |
| 661 |
&imageResource), |
| 662 |
"Could not create image resource."); |
| 663 |
|
| 664 |
XPS_RECT bitmapRect = { |
| 665 |
0.0, 0.0, |
| 666 |
static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height()) |
| 667 |
}; |
| 668 |
SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush; |
| 669 |
HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(), |
| 670 |
&bitmapRect, &bitmapRect, |
| 671 |
&xpsImageBrush), |
| 672 |
"Could not create image brush."); |
| 673 |
|
| 674 |
if (SkTileMode::kClamp != xy[0] && |
| 675 |
SkTileMode::kClamp != xy[1]) { |
| 676 |
|
| 677 |
HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode(xy[0], xy[1])), |
| 678 |
"Could not set image tile mode"); |
| 679 |
HRM(xpsImageBrush->SetOpacity(alpha / 255.0f), |
| 680 |
"Could not set image opacity."); |
| 681 |
HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed."); |
| 682 |
} else { |
| 683 |
//TODO(bungeman): compute how big this really needs to be. |
| 684 |
const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax; |
| 685 |
const FLOAT BIG_F = SkScalarToFLOAT(BIG); |
| 686 |
const SkScalar bWidth = SkIntToScalar(bitmap.width()); |
| 687 |
const SkScalar bHeight = SkIntToScalar(bitmap.height()); |
| 688 |
|
| 689 |
//create brush canvas |
| 690 |
SkTScopedComPtr<IXpsOMCanvas> brushCanvas; |
| 691 |
HRM(this->fXpsFactory->CreateCanvas(&brushCanvas), |
| 692 |
"Could not create image brush canvas."); |
| 693 |
SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals; |
| 694 |
HRM(brushCanvas->GetVisuals(&brushVisuals), |
| 695 |
"Could not get image brush canvas visuals collection."); |
| 696 |
|
| 697 |
//create central figure |
| 698 |
const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight); |
| 699 |
SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure; |
| 700 |
HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, ¢ralFigure)); |
| 701 |
|
| 702 |
SkTScopedComPtr<IXpsOMPath> centralPath; |
| 703 |
HR(this->createPath(centralFigure.get(), |
| 704 |
brushVisuals.get(), |
| 705 |
¢ralPath)); |
| 706 |
HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY), |
| 707 |
"Could not set tile mode for image brush central path."); |
| 708 |
HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()), |
| 709 |
"Could not set fill brush for image brush central path."); |
| 710 |
|
| 711 |
//add left/right |
| 712 |
if (SkTileMode::kClamp == xy[0]) { |
| 713 |
SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight); |
| 714 |
XPS_RECT leftImageViewBox = { |
| 715 |
0.0, 0.0, |
| 716 |
1.0, static_cast<FLOAT>(bitmap.height()), |
| 717 |
}; |
| 718 |
HR(this->sideOfClamp(leftArea, leftImageViewBox, |
| 719 |
imageResource.get(), |
| 720 |
brushVisuals.get())); |
| 721 |
|
| 722 |
SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight); |
| 723 |
XPS_RECT rightImageViewBox = { |
| 724 |
bitmap.width() - 1.0f, 0.0f, |
| 725 |
1.0f, static_cast<FLOAT>(bitmap.height()), |
| 726 |
}; |
| 727 |
HR(this->sideOfClamp(rightArea, rightImageViewBox, |
| 728 |
imageResource.get(), |
| 729 |
brushVisuals.get())); |
| 730 |
} |
| 731 |
|
| 732 |
//add top/bottom |
| 733 |
if (SkTileMode::kClamp == xy[1]) { |
| 734 |
SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0); |
| 735 |
XPS_RECT topImageViewBox = { |
| 736 |
0.0, 0.0, |
| 737 |
static_cast<FLOAT>(bitmap.width()), 1.0, |
| 738 |
}; |
| 739 |
HR(this->sideOfClamp(topArea, topImageViewBox, |
| 740 |
imageResource.get(), |
| 741 |
brushVisuals.get())); |
| 742 |
|
| 743 |
SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG); |
| 744 |
XPS_RECT bottomImageViewBox = { |
| 745 |
0.0f, bitmap.height() - 1.0f, |
| 746 |
static_cast<FLOAT>(bitmap.width()), 1.0f, |
| 747 |
}; |
| 748 |
HR(this->sideOfClamp(bottomArea, bottomImageViewBox, |
| 749 |
imageResource.get(), |
| 750 |
brushVisuals.get())); |
| 751 |
} |
| 752 |
|
| 753 |
//add tl, tr, bl, br |
| 754 |
if (SkTileMode::kClamp == xy[0] && |
| 755 |
SkTileMode::kClamp == xy[1]) { |
| 756 |
|
| 757 |
const SkColor4f tlColor = bitmap.getColor4f(0,0); |
| 758 |
const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0); |
| 759 |
HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get())); |
| 760 |
|
| 761 |
const SkColor4f trColor = bitmap.getColor4f(bitmap.width()-1,0); |
| 762 |
const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0); |
| 763 |
HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get())); |
| 764 |
|
| 765 |
const SkColor4f brColor = bitmap.getColor4f(bitmap.width()-1, |
| 766 |
bitmap.height()-1); |
| 767 |
const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG); |
| 768 |
HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get())); |
| 769 |
|
| 770 |
const SkColor4f blColor = bitmap.getColor4f(0,bitmap.height()-1); |
| 771 |
const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG); |
| 772 |
HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get())); |
| 773 |
} |
| 774 |
|
| 775 |
//create visual brush from canvas |
| 776 |
XPS_RECT bound = {}; |
| 777 |
if (SkTileMode::kClamp == xy[0] && |
| 778 |
SkTileMode::kClamp == xy[1]) { |
| 779 |
|
| 780 |
bound.x = BIG_F / -2; |
| 781 |
bound.y = BIG_F / -2; |
| 782 |
bound.width = BIG_F; |
| 783 |
bound.height = BIG_F; |
| 784 |
} else if (SkTileMode::kClamp == xy[0]) { |
| 785 |
bound.x = BIG_F / -2; |
| 786 |
bound.y = 0.0f; |
| 787 |
bound.width = BIG_F; |
| 788 |
bound.height = static_cast<FLOAT>(bitmap.height()); |
| 789 |
} else if (SkTileMode::kClamp == xy[1]) { |
| 790 |
bound.x = 0; |
| 791 |
bound.y = BIG_F / -2; |
| 792 |
bound.width = static_cast<FLOAT>(bitmap.width()); |
| 793 |
bound.height = BIG_F; |
| 794 |
} |
| 795 |
SkTScopedComPtr<IXpsOMVisualBrush> clampBrush; |
| 796 |
HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush), |
| 797 |
"Could not create visual brush for image brush."); |
| 798 |
HRM(clampBrush->SetVisualLocal(brushCanvas.get()), |
| 799 |
"Could not set canvas on visual brush for image brush."); |
| 800 |
HRM(clampBrush->SetTileMode(SkToXpsTileMode(xy[0], xy[1])), |
| 801 |
"Could not set tile mode on visual brush for image brush."); |
| 802 |
HRM(clampBrush->SetOpacity(alpha / 255.0f), |
| 803 |
"Could not set opacity on visual brush for image brush."); |
| 804 |
|
| 805 |
HRM(clampBrush->QueryInterface(xpsBrush), "QI failed."); |
| 806 |
} |
| 807 |
|
| 808 |
SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse; |
| 809 |
HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse)); |
| 810 |
if (xpsMatrixToUse.get()) { |
| 811 |
HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()), |
| 812 |
"Could not set transform for image brush."); |
| 813 |
} else { |
| 814 |
//TODO(bungeman): perspective bitmaps in general. |
| 815 |
} |
| 816 |
|
| 817 |
return S_OK; |
| 818 |
} |
| 819 |
|
| 820 |
HRESULT SkXPSDevice::createXpsGradientStop(const SkColor4f skColor, |
| 821 |
const SkScalar offset, |
| 822 |
IXpsOMGradientStop** xpsGradStop) { |
| 823 |
XPS_COLOR gradStopXpsColor = xps_color(skColor); |
| 824 |
HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor, |
| 825 |
nullptr, |
| 826 |
SkScalarToFLOAT(offset), |
| 827 |
xpsGradStop), |
| 828 |
"Could not create gradient stop."); |
| 829 |
return S_OK; |
| 830 |
} |
| 831 |
|
| 832 |
HRESULT SkXPSDevice::createXpsLinearGradient(SkShaderBase::GradientInfo info, |
| 833 |
const SkAlpha alpha, |
| 834 |
const SkMatrix& localMatrix, |
| 835 |
IXpsOMMatrixTransform* xpsMatrix, |
| 836 |
IXpsOMBrush** xpsBrush) { |
| 837 |
XPS_POINT startPoint; |
| 838 |
XPS_POINT endPoint; |
| 839 |
if (xpsMatrix) { |
| 840 |
startPoint = xps_point(info.fPoint[0]); |
| 841 |
endPoint = xps_point(info.fPoint[1]); |
| 842 |
} else { |
| 843 |
transform_offsets(info.fColorOffsets, info.fColorCount, |
| 844 |
info.fPoint[0], info.fPoint[1], |
| 845 |
localMatrix); |
| 846 |
startPoint = xps_point(info.fPoint[0], localMatrix); |
| 847 |
endPoint = xps_point(info.fPoint[1], localMatrix); |
| 848 |
} |
| 849 |
|
| 850 |
SkTScopedComPtr<IXpsOMGradientStop> gradStop0; |
| 851 |
HR(createXpsGradientStop(info.fColors[0], |
| 852 |
info.fColorOffsets[0], |
| 853 |
&gradStop0)); |
| 854 |
|
| 855 |
SkTScopedComPtr<IXpsOMGradientStop> gradStop1; |
| 856 |
HR(createXpsGradientStop(info.fColors[1], |
| 857 |
info.fColorOffsets[1], |
| 858 |
&gradStop1)); |
| 859 |
|
| 860 |
SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush; |
| 861 |
HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(), |
| 862 |
gradStop1.get(), |
| 863 |
&startPoint, |
| 864 |
&endPoint, |
| 865 |
&gradientBrush), |
| 866 |
"Could not create linear gradient brush."); |
| 867 |
if (xpsMatrix) { |
| 868 |
HRM(gradientBrush->SetTransformLocal(xpsMatrix), |
| 869 |
"Could not set transform on linear gradient brush."); |
| 870 |
} |
| 871 |
|
| 872 |
SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection; |
| 873 |
HRM(gradientBrush->GetGradientStops(&gradStopCollection), |
| 874 |
"Could not get linear gradient stop collection."); |
| 875 |
for (int i = 2; i < info.fColorCount; ++i) { |
| 876 |
SkTScopedComPtr<IXpsOMGradientStop> gradStop; |
| 877 |
HR(createXpsGradientStop(info.fColors[i], |
| 878 |
info.fColorOffsets[i], |
| 879 |
&gradStop)); |
| 880 |
HRM(gradStopCollection->Append(gradStop.get()), |
| 881 |
"Could not add linear gradient stop."); |
| 882 |
} |
| 883 |
|
| 884 |
HRM(gradientBrush->SetSpreadMethod(xps_spread_method((SkTileMode)info.fTileMode)), |
| 885 |
"Could not set spread method of linear gradient."); |
| 886 |
|
| 887 |
HRM(gradientBrush->SetOpacity(alpha / 255.0f), |
| 888 |
"Could not set opacity of linear gradient brush."); |
| 889 |
HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed"); |
| 890 |
|
| 891 |
return S_OK; |
| 892 |
} |
| 893 |
|
| 894 |
HRESULT SkXPSDevice::createXpsRadialGradient(SkShaderBase::GradientInfo info, |
| 895 |
const SkAlpha alpha, |
| 896 |
const SkMatrix& localMatrix, |
| 897 |
IXpsOMMatrixTransform* xpsMatrix, |
| 898 |
IXpsOMBrush** xpsBrush) { |
| 899 |
SkTScopedComPtr<IXpsOMGradientStop> gradStop0; |
| 900 |
HR(createXpsGradientStop(info.fColors[0], |
| 901 |
info.fColorOffsets[0], |
| 902 |
&gradStop0)); |
| 903 |
|
| 904 |
SkTScopedComPtr<IXpsOMGradientStop> gradStop1; |
| 905 |
HR(createXpsGradientStop(info.fColors[1], |
| 906 |
info.fColorOffsets[1], |
| 907 |
&gradStop1)); |
| 908 |
|
| 909 |
//TODO: figure out how to fake better if not affine |
| 910 |
XPS_POINT centerPoint; |
| 911 |
XPS_POINT gradientOrigin; |
| 912 |
XPS_SIZE radiiSizes; |
| 913 |
if (xpsMatrix) { |
| 914 |
centerPoint = xps_point(info.fPoint[0]); |
| 915 |
gradientOrigin = xps_point(info.fPoint[0]); |
| 916 |
radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]); |
| 917 |
radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]); |
| 918 |
} else { |
| 919 |
centerPoint = xps_point(info.fPoint[0], localMatrix); |
| 920 |
gradientOrigin = xps_point(info.fPoint[0], localMatrix); |
| 921 |
|
| 922 |
SkScalar radius = info.fRadius[0]; |
| 923 |
SkVector vec[2]; |
| 924 |
|
| 925 |
vec[0].set(radius, 0); |
| 926 |
vec[1].set(0, radius); |
| 927 |
localMatrix.mapVectors(vec); |
| 928 |
|
| 929 |
SkScalar d0 = vec[0].length(); |
| 930 |
SkScalar d1 = vec[1].length(); |
| 931 |
|
| 932 |
radiiSizes.width = SkScalarToFLOAT(d0); |
| 933 |
radiiSizes.height = SkScalarToFLOAT(d1); |
| 934 |
} |
| 935 |
|
| 936 |
SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush; |
| 937 |
HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(), |
| 938 |
gradStop1.get(), |
| 939 |
¢erPoint, |
| 940 |
&gradientOrigin, |
| 941 |
&radiiSizes, |
| 942 |
&gradientBrush), |
| 943 |
"Could not create radial gradient brush."); |
| 944 |
if (xpsMatrix) { |
| 945 |
HRM(gradientBrush->SetTransformLocal(xpsMatrix), |
| 946 |
"Could not set transform on radial gradient brush."); |
| 947 |
} |
| 948 |
|
| 949 |
SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection; |
| 950 |
HRM(gradientBrush->GetGradientStops(&gradStopCollection), |
| 951 |
"Could not get radial gradient stop collection."); |
| 952 |
for (int i = 2; i < info.fColorCount; ++i) { |
| 953 |
SkTScopedComPtr<IXpsOMGradientStop> gradStop; |
| 954 |
HR(createXpsGradientStop(info.fColors[i], |
| 955 |
info.fColorOffsets[i], |
| 956 |
&gradStop)); |
| 957 |
HRM(gradStopCollection->Append(gradStop.get()), |
| 958 |
"Could not add radial gradient stop."); |
| 959 |
} |
| 960 |
|
| 961 |
HRM(gradientBrush->SetSpreadMethod(xps_spread_method((SkTileMode)info.fTileMode)), |
| 962 |
"Could not set spread method of radial gradient."); |
| 963 |
|
| 964 |
HRM(gradientBrush->SetOpacity(alpha / 255.0f), |
| 965 |
"Could not set opacity of radial gradient brush."); |
| 966 |
HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed."); |
| 967 |
|
| 968 |
return S_OK; |
| 969 |
} |
| 970 |
|
| 971 |
HRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint, |
| 972 |
IXpsOMBrush** brush, |
| 973 |
const SkMatrix* parentTransform) { |
| 974 |
const SkShader *shader = skPaint.getShader(); |
| 975 |
if (nullptr == shader) { |
| 976 |
HR(this->createXpsSolidColorBrush(skPaint.getColor4f(), 0xFF, brush)); |
| 977 |
return S_OK; |
| 978 |
} |
| 979 |
|
| 980 |
//Gradient shaders. |
| 981 |
auto shaderBase = as_SB(shader); |
| 982 |
|
| 983 |
if (shaderBase->type() == SkShaderBase::ShaderType::kColor) { |
| 984 |
auto colorShader = static_cast<const SkColorShader*>(shader); |
| 985 |
SkAlpha alpha = skPaint.getAlpha(); |
| 986 |
HR(this->createXpsSolidColorBrush(colorShader->color(), alpha, brush)); |
| 987 |
return S_OK; |
| 988 |
} else if (shaderBase->asGradient() != SkShaderBase::GradientType::kNone) { |
| 989 |
SkShaderBase::GradientInfo info; |
| 990 |
SkShaderBase::GradientType gradientType = shaderBase->asGradient(&info); |
| 991 |
if (info.fColorCount == 0) { |
| 992 |
const SkColor4f color = skPaint.getColor4f(); |
| 993 |
HR(this->createXpsSolidColorBrush(color, 0xFF, brush)); |
| 994 |
return S_OK; |
| 995 |
} |
| 996 |
|
| 997 |
SkMatrix localMatrix; |
| 998 |
AutoTArray<SkColor4f> colors(info.fColorCount); |
| 999 |
AutoTArray<SkScalar> colorOffsets(info.fColorCount); |
| 1000 |
info.fColors = colors.get(); |
| 1001 |
info.fColorOffsets = colorOffsets.get(); |
| 1002 |
shaderBase->asGradient(&info, &localMatrix); |
| 1003 |
|
| 1004 |
if (1 == info.fColorCount) { |
| 1005 |
SkColor4f color = info.fColors[0]; |
| 1006 |
SkAlpha alpha = skPaint.getAlpha(); |
| 1007 |
HR(this->createXpsSolidColorBrush(color, alpha, brush)); |
| 1008 |
return S_OK; |
| 1009 |
} |
| 1010 |
|
| 1011 |
if (parentTransform) { |
| 1012 |
localMatrix.preConcat(*parentTransform); |
| 1013 |
} |
| 1014 |
SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse; |
| 1015 |
HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse)); |
| 1016 |
|
| 1017 |
if (gradientType == SkShaderBase::GradientType::kLinear) { |
| 1018 |
HR(this->createXpsLinearGradient(info, |
| 1019 |
skPaint.getAlpha(), |
| 1020 |
localMatrix, |
| 1021 |
xpsMatrixToUse.get(), |
| 1022 |
brush)); |
| 1023 |
return S_OK; |
| 1024 |
} |
| 1025 |
|
| 1026 |
if (gradientType == SkShaderBase::GradientType::kRadial) { |
| 1027 |
HR(this->createXpsRadialGradient(info, |
| 1028 |
skPaint.getAlpha(), |
| 1029 |
localMatrix, |
| 1030 |
xpsMatrixToUse.get(), |
| 1031 |
brush)); |
| 1032 |
return S_OK; |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (gradientType == SkShaderBase::GradientType::kConical) { |
| 1036 |
//simple if affine and one is 0, otherwise will have to fake |
| 1037 |
} |
| 1038 |
|
| 1039 |
if (gradientType == SkShaderBase::GradientType::kSweep) { |
| 1040 |
//have to fake |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
SkBitmap outTexture; |
| 1045 |
SkMatrix outMatrix; |
| 1046 |
SkTileMode xy[2]; |
| 1047 |
SkImage* image = shader->isAImage(&outMatrix, xy); |
| 1048 |
if (image && image->asLegacyBitmap(&outTexture)) { |
| 1049 |
if (parentTransform) { |
| 1050 |
outMatrix.postConcat(*parentTransform); |
| 1051 |
} |
| 1052 |
|
| 1053 |
SkTScopedComPtr<IXpsOMTileBrush> tileBrush; |
| 1054 |
HR(this->createXpsImageBrush(outTexture.pixmap(), outMatrix, xy, skPaint.getAlpha(), |
| 1055 |
&tileBrush)); |
| 1056 |
|
| 1057 |
HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed."); |
| 1058 |
} else { |
| 1059 |
HR(this->createXpsSolidColorBrush(skPaint.getColor4f(), 0xFF, brush)); |
| 1060 |
} |
| 1061 |
return S_OK; |
| 1062 |
} |
| 1063 |
|
| 1064 |
static bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) { |
| 1065 |
const bool zeroWidth = (0 == paint.getStrokeWidth()); |
| 1066 |
const bool stroke = (SkPaint::kFill_Style != paint.getStyle()); |
| 1067 |
|
| 1068 |
return paint.getPathEffect() || |
| 1069 |
paint.getMaskFilter() || |
| 1070 |
(stroke && ( |
| 1071 |
(matrix.hasPerspective() && !zeroWidth) || |
| 1072 |
SkPaint::kMiter_Join != paint.getStrokeJoin() || |
| 1073 |
(SkPaint::kMiter_Join == paint.getStrokeJoin() && |
| 1074 |
paint.getStrokeMiter() < SK_ScalarSqrt2) |
| 1075 |
)) |
| 1076 |
; |
| 1077 |
} |
| 1078 |
|
| 1079 |
HRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill, |
| 1080 |
IXpsOMGeometryFigure** xpsRect) { |
| 1081 |
const SkPoint points[4] = { |
| 1082 |
{ rect.fLeft, rect.fTop }, |
| 1083 |
{ rect.fRight, rect.fTop }, |
| 1084 |
{ rect.fRight, rect.fBottom }, |
| 1085 |
{ rect.fLeft, rect.fBottom }, |
| 1086 |
}; |
| 1087 |
return this->createXpsQuad(points, stroke, fill, xpsRect); |
| 1088 |
} |
| 1089 |
HRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4], |
| 1090 |
BOOL stroke, BOOL fill, |
| 1091 |
IXpsOMGeometryFigure** xpsQuad) { |
| 1092 |
// Define the start point. |
| 1093 |
XPS_POINT startPoint = xps_point(points[0]); |
| 1094 |
|
| 1095 |
// Create the figure. |
| 1096 |
HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad), |
| 1097 |
"Could not create quad geometry figure."); |
| 1098 |
|
| 1099 |
// Define the type of each segment. |
| 1100 |
XPS_SEGMENT_TYPE segmentTypes[3] = { |
| 1101 |
XPS_SEGMENT_TYPE_LINE, |
| 1102 |
XPS_SEGMENT_TYPE_LINE, |
| 1103 |
XPS_SEGMENT_TYPE_LINE, |
| 1104 |
}; |
| 1105 |
|
| 1106 |
// Define the x and y coordinates of each corner of the figure. |
| 1107 |
FLOAT segmentData[6] = { |
| 1108 |
SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY), |
| 1109 |
SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY), |
| 1110 |
SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY), |
| 1111 |
}; |
| 1112 |
|
| 1113 |
// Describe if the segments are stroked. |
| 1114 |
BOOL segmentStrokes[3] = { |
| 1115 |
stroke, stroke, stroke, |
| 1116 |
}; |
| 1117 |
|
| 1118 |
// Add the segment data to the figure. |
| 1119 |
HRM((*xpsQuad)->SetSegments( |
| 1120 |
3, 6, |
| 1121 |
segmentTypes , segmentData, segmentStrokes), |
| 1122 |
"Could not add segment data to quad."); |
| 1123 |
|
| 1124 |
// Set the closed and filled properties of the figure. |
| 1125 |
HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close."); |
| 1126 |
HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill."); |
| 1127 |
|
| 1128 |
return S_OK; |
| 1129 |
} |
| 1130 |
|
| 1131 |
void SkXPSDevice::drawPoints(SkCanvas::PointMode, SkSpan<const SkPoint>, const SkPaint&) { |
| 1132 |
//TODO |
| 1133 |
} |
| 1134 |
|
| 1135 |
void SkXPSDevice::drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) { |
| 1136 |
//TODO |
| 1137 |
} |
| 1138 |
|
| 1139 |
void SkXPSDevice::drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) { |
| 1140 |
// TODO |
| 1141 |
} |
| 1142 |
|
| 1143 |
void SkXPSDevice::drawPaint(const SkPaint& origPaint) { |
| 1144 |
const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize); |
| 1145 |
|
| 1146 |
//If trying to paint with a stroke, ignore that and fill. |
| 1147 |
SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint); |
| 1148 |
SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
| 1149 |
if (paint->getStyle() != SkPaint::kFill_Style) { |
| 1150 |
paint.writable()->setStyle(SkPaint::kFill_Style); |
| 1151 |
} |
| 1152 |
|
| 1153 |
this->internalDrawRect(r, false, *fillPaint); |
| 1154 |
} |
| 1155 |
|
| 1156 |
void SkXPSDevice::drawRect(const SkRect& r, |
| 1157 |
const SkPaint& paint) { |
| 1158 |
this->internalDrawRect(r, true, paint); |
| 1159 |
} |
| 1160 |
|
| 1161 |
void SkXPSDevice::drawRRect(const SkRRect& rr, |
| 1162 |
const SkPaint& paint) { |
| 1163 |
this->drawPath(SkPath::RRect(rr), paint); |
| 1164 |
} |
| 1165 |
|
| 1166 |
void SkXPSDevice::internalDrawRect(const SkRect& r, |
| 1167 |
bool transformRect, |
| 1168 |
const SkPaint& paint) { |
| 1169 |
//Exit early if there is nothing to draw. |
| 1170 |
if (this->isClipEmpty() || (paint.getAlpha() == 0 && paint.isSrcOver())) { |
| 1171 |
return; |
| 1172 |
} |
| 1173 |
|
| 1174 |
//Path the rect if we can't optimize it. |
| 1175 |
if (rect_must_be_pathed(paint, this->localToDevice())) { |
| 1176 |
this->drawPath(SkPath::Rect(r), paint); |
| 1177 |
return; |
| 1178 |
} |
| 1179 |
|
| 1180 |
//Create the shaded path. |
| 1181 |
SkTScopedComPtr<IXpsOMPath> shadedPath; |
| 1182 |
HRVM(this->fXpsFactory->CreatePath(&shadedPath), |
| 1183 |
"Could not create shaded path for rect."); |
| 1184 |
|
| 1185 |
//Create the shaded geometry. |
| 1186 |
SkTScopedComPtr<IXpsOMGeometry> shadedGeometry; |
| 1187 |
HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry), |
| 1188 |
"Could not create shaded geometry for rect."); |
| 1189 |
|
| 1190 |
//Add the geometry to the shaded path. |
| 1191 |
HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()), |
| 1192 |
"Could not set shaded geometry for rect."); |
| 1193 |
|
| 1194 |
//Set the brushes. |
| 1195 |
BOOL fill = FALSE; |
| 1196 |
BOOL stroke = FALSE; |
| 1197 |
HRV(this->shadePath(shadedPath.get(), paint, this->localToDevice(), &fill, &stroke)); |
| 1198 |
|
| 1199 |
bool xpsTransformsPath = true; |
| 1200 |
//Transform the geometry. |
| 1201 |
if (transformRect && xpsTransformsPath) { |
| 1202 |
SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform; |
| 1203 |
HRV(this->createXpsTransform(this->localToDevice(), &xpsTransform)); |
| 1204 |
if (xpsTransform.get()) { |
| 1205 |
HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()), |
| 1206 |
"Could not set transform for rect."); |
| 1207 |
} else { |
| 1208 |
xpsTransformsPath = false; |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
//Create the figure. |
| 1213 |
SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure; |
| 1214 |
{ |
| 1215 |
SkPoint points[4] = { |
| 1216 |
{ r.fLeft, r.fTop }, |
| 1217 |
{ r.fLeft, r.fBottom }, |
| 1218 |
{ r.fRight, r.fBottom }, |
| 1219 |
{ r.fRight, r.fTop }, |
| 1220 |
}; |
| 1221 |
if (!xpsTransformsPath && transformRect) { |
| 1222 |
this->localToDevice().mapPoints(points); |
| 1223 |
} |
| 1224 |
HRV(this->createXpsQuad(points, stroke, fill, &rectFigure)); |
| 1225 |
} |
| 1226 |
|
| 1227 |
//Get the figures of the shaded geometry. |
| 1228 |
SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures; |
| 1229 |
HRVM(shadedGeometry->GetFigures(&shadedFigures), |
| 1230 |
"Could not get shaded figures for rect."); |
| 1231 |
|
| 1232 |
//Add the figure to the shaded geometry figures. |
| 1233 |
HRVM(shadedFigures->Append(rectFigure.get()), |
| 1234 |
"Could not add shaded figure for rect."); |
| 1235 |
|
| 1236 |
HRV(this->clip(shadedPath.get())); |
| 1237 |
|
| 1238 |
//Add the shaded path to the current visuals. |
| 1239 |
SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals; |
| 1240 |
HRVM(this->fCurrentXpsCanvas->GetVisuals(¤tVisuals), |
| 1241 |
"Could not get current visuals for rect."); |
| 1242 |
HRVM(currentVisuals->Append(shadedPath.get()), |
| 1243 |
"Could not add rect to current visuals."); |
| 1244 |
} |
| 1245 |
|
| 1246 |
static HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes, |
| 1247 |
const SkTDArray<FLOAT>& segmentData, |
| 1248 |
const SkTDArray<BOOL>& segmentStrokes, |
| 1249 |
BOOL stroke, BOOL fill, |
| 1250 |
IXpsOMGeometryFigure* figure, |
| 1251 |
IXpsOMGeometryFigureCollection* figures) { |
| 1252 |
// Either all are empty or none are empty. |
| 1253 |
SkASSERT(( segmentTypes.empty() && segmentData.empty() && segmentStrokes.empty()) || |
| 1254 |
(!segmentTypes.empty() && !segmentData.empty() && !segmentStrokes.empty())); |
| 1255 |
|
| 1256 |
// SkTDArray::begin() may return nullptr when the segment is empty, |
| 1257 |
// but IXpsOMGeometryFigure::SetSegments returns E_POINTER if any of the pointers are nullptr |
| 1258 |
// even if the counts are all 0. |
| 1259 |
if (!segmentTypes.empty() && !segmentData.empty() && !segmentStrokes.empty()) { |
| 1260 |
// Add the segment data to the figure. |
| 1261 |
HRM(figure->SetSegments(segmentTypes.size(), segmentData.size(), |
| 1262 |
segmentTypes.begin(), segmentData.begin(), segmentStrokes.begin()), |
| 1263 |
"Could not set path segments."); |
| 1264 |
} |
| 1265 |
|
| 1266 |
// Set the closed and filled properties of the figure. |
| 1267 |
HRM(figure->SetIsClosed(stroke), "Could not set path closed."); |
| 1268 |
HRM(figure->SetIsFilled(fill), "Could not set path fill."); |
| 1269 |
|
| 1270 |
// Add the figure created above to this geometry. |
| 1271 |
HRM(figures->Append(figure), "Could not add path to geometry."); |
| 1272 |
return S_OK; |
| 1273 |
} |
| 1274 |
|
| 1275 |
HRESULT SkXPSDevice::addXpsPathGeometry( |
| 1276 |
IXpsOMGeometryFigureCollection* xpsFigures, |
| 1277 |
BOOL stroke, BOOL fill, const SkPath& path) { |
| 1278 |
SkTDArray<XPS_SEGMENT_TYPE> segmentTypes; |
| 1279 |
SkTDArray<FLOAT> segmentData; |
| 1280 |
SkTDArray<BOOL> segmentStrokes; |
| 1281 |
|
| 1282 |
SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure; |
| 1283 |
SkPath::Iter iter(path, true); |
| 1284 |
SkPoint points[4]; |
| 1285 |
SkPath::Verb verb; |
| 1286 |
while ((verb = iter.next(points)) != SkPath::kDone_Verb) { |
| 1287 |
switch (verb) { |
| 1288 |
case SkPath::kMove_Verb: { |
| 1289 |
if (xpsFigure.get()) { |
| 1290 |
HR(close_figure(segmentTypes, segmentData, segmentStrokes, |
| 1291 |
stroke, fill, |
| 1292 |
xpsFigure.get() , xpsFigures)); |
| 1293 |
segmentTypes.clear(); |
| 1294 |
segmentData.clear(); |
| 1295 |
segmentStrokes.clear(); |
| 1296 |
xpsFigure.reset(); |
| 1297 |
} |
| 1298 |
// Define the start point. |
| 1299 |
XPS_POINT startPoint = xps_point(points[0]); |
| 1300 |
// Create the figure. |
| 1301 |
HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, |
| 1302 |
&xpsFigure), |
| 1303 |
"Could not create path geometry figure."); |
| 1304 |
break; |
| 1305 |
} |
| 1306 |
case SkPath::kLine_Verb: |
| 1307 |
if (iter.isCloseLine()) break; //ignore the line, auto-closed |
| 1308 |
segmentTypes.push_back(XPS_SEGMENT_TYPE_LINE); |
| 1309 |
segmentData.push_back(SkScalarToFLOAT(points[1].fX)); |
| 1310 |
segmentData.push_back(SkScalarToFLOAT(points[1].fY)); |
| 1311 |
segmentStrokes.push_back(stroke); |
| 1312 |
break; |
| 1313 |
case SkPath::kQuad_Verb: |
| 1314 |
segmentTypes.push_back(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER); |
| 1315 |
segmentData.push_back(SkScalarToFLOAT(points[1].fX)); |
| 1316 |
segmentData.push_back(SkScalarToFLOAT(points[1].fY)); |
| 1317 |
segmentData.push_back(SkScalarToFLOAT(points[2].fX)); |
| 1318 |
segmentData.push_back(SkScalarToFLOAT(points[2].fY)); |
| 1319 |
segmentStrokes.push_back(stroke); |
| 1320 |
break; |
| 1321 |
case SkPath::kCubic_Verb: |
| 1322 |
segmentTypes.push_back(XPS_SEGMENT_TYPE_BEZIER); |
| 1323 |
segmentData.push_back(SkScalarToFLOAT(points[1].fX)); |
| 1324 |
segmentData.push_back(SkScalarToFLOAT(points[1].fY)); |
| 1325 |
segmentData.push_back(SkScalarToFLOAT(points[2].fX)); |
| 1326 |
segmentData.push_back(SkScalarToFLOAT(points[2].fY)); |
| 1327 |
segmentData.push_back(SkScalarToFLOAT(points[3].fX)); |
| 1328 |
segmentData.push_back(SkScalarToFLOAT(points[3].fY)); |
| 1329 |
segmentStrokes.push_back(stroke); |
| 1330 |
break; |
| 1331 |
case SkPath::kConic_Verb: { |
| 1332 |
const SkScalar tol = SK_Scalar1 / 4; |
| 1333 |
SkAutoConicToQuads converter; |
| 1334 |
const SkPoint* quads = |
| 1335 |
converter.computeQuads(points, iter.conicWeight(), tol); |
| 1336 |
for (int i = 0; i < converter.countQuads(); ++i) { |
| 1337 |
segmentTypes.push_back(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER); |
| 1338 |
segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 1].fX)); |
| 1339 |
segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 1].fY)); |
| 1340 |
segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 2].fX)); |
| 1341 |
segmentData.push_back(SkScalarToFLOAT(quads[2 * i + 2].fY)); |
| 1342 |
segmentStrokes.push_back(stroke); |
| 1343 |
} |
| 1344 |
break; |
| 1345 |
} |
| 1346 |
case SkPath::kClose_Verb: |
| 1347 |
// we ignore these, and just get the whole segment from |
| 1348 |
// the corresponding line/quad/cubic verbs |
| 1349 |
break; |
| 1350 |
default: |
| 1351 |
SkDEBUGFAIL("unexpected verb"); |
| 1352 |
break; |
| 1353 |
} |
| 1354 |
} |
| 1355 |
if (xpsFigure.get()) { |
| 1356 |
HR(close_figure(segmentTypes, segmentData, segmentStrokes, |
| 1357 |
stroke, fill, |
| 1358 |
xpsFigure.get(), xpsFigures)); |
| 1359 |
} |
| 1360 |
return S_OK; |
| 1361 |
} |
| 1362 |
|
| 1363 |
void SkXPSDevice::convertToPpm(const SkMaskFilter* filter, |
| 1364 |
SkMatrix* matrix, |
| 1365 |
SkVector* ppuScale, |
| 1366 |
const SkIRect& clip, SkIRect* clipIRect) { |
| 1367 |
//This action is in unit space, but the ppm is specified in physical space. |
| 1368 |
ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX, |
| 1369 |
fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY); |
| 1370 |
|
| 1371 |
matrix->postScale(ppuScale->fX, ppuScale->fY); |
| 1372 |
|
| 1373 |
const SkIRect& irect = clip; |
| 1374 |
SkRect clipRect = SkRect::MakeLTRB(SkIntToScalar(irect.fLeft) * ppuScale->fX, |
| 1375 |
SkIntToScalar(irect.fTop) * ppuScale->fY, |
| 1376 |
SkIntToScalar(irect.fRight) * ppuScale->fX, |
| 1377 |
SkIntToScalar(irect.fBottom) * ppuScale->fY); |
| 1378 |
clipRect.roundOut(clipIRect); |
| 1379 |
} |
| 1380 |
|
| 1381 |
HRESULT SkXPSDevice::applyMask(const SkMask& mask, |
| 1382 |
const SkVector& ppuScale, |
| 1383 |
IXpsOMPath* shadedPath) { |
| 1384 |
//Get the geometry object. |
| 1385 |
SkTScopedComPtr<IXpsOMGeometry> shadedGeometry; |
| 1386 |
HRM(shadedPath->GetGeometry(&shadedGeometry), |
| 1387 |
"Could not get mask shaded geometry."); |
| 1388 |
|
| 1389 |
//Get the figures from the geometry. |
| 1390 |
SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures; |
| 1391 |
HRM(shadedGeometry->GetFigures(&shadedFigures), |
| 1392 |
"Could not get mask shaded figures."); |
| 1393 |
|
| 1394 |
SkMatrix m; |
| 1395 |
m.reset(); |
| 1396 |
m.setTranslate(SkIntToScalar(mask.fBounds.fLeft), |
| 1397 |
SkIntToScalar(mask.fBounds.fTop)); |
| 1398 |
m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY)); |
| 1399 |
|
| 1400 |
SkTileMode xy[2]; |
| 1401 |
xy[0] = (SkTileMode)3; |
| 1402 |
xy[1] = (SkTileMode)3; |
| 1403 |
|
| 1404 |
SkASSERT(mask.fFormat == SkMask::kA8_Format); |
| 1405 |
SkPixmap pm(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()), |
| 1406 |
mask.fImage, mask.fRowBytes); |
| 1407 |
|
| 1408 |
SkTScopedComPtr<IXpsOMTileBrush> maskBrush; |
| 1409 |
HR(this->createXpsImageBrush(pm, m, xy, 0xFF, &maskBrush)); |
| 1410 |
HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()), |
| 1411 |
"Could not set mask."); |
| 1412 |
|
| 1413 |
const SkRect universeRect = SkRect::MakeLTRB(0, 0, |
| 1414 |
this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight); |
| 1415 |
SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure; |
| 1416 |
HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure), |
| 1417 |
"Could not create mask shaded figure."); |
| 1418 |
HRM(shadedFigures->Append(shadedFigure.get()), |
| 1419 |
"Could not add mask shaded figure."); |
| 1420 |
|
| 1421 |
HR(this->clip(shadedPath)); |
| 1422 |
|
| 1423 |
//Add the path to the active visual collection. |
| 1424 |
SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals; |
| 1425 |
HRM(this->fCurrentXpsCanvas->GetVisuals(¤tVisuals), |
| 1426 |
"Could not get mask current visuals."); |
| 1427 |
HRM(currentVisuals->Append(shadedPath), |
| 1428 |
"Could not add masked shaded path to current visuals."); |
| 1429 |
|
| 1430 |
return S_OK; |
| 1431 |
} |
| 1432 |
|
| 1433 |
HRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath, |
| 1434 |
const SkPaint& shaderPaint, |
| 1435 |
const SkMatrix& matrix, |
| 1436 |
BOOL* fill, BOOL* stroke) { |
| 1437 |
*fill = FALSE; |
| 1438 |
*stroke = FALSE; |
| 1439 |
|
| 1440 |
const SkPaint::Style style = shaderPaint.getStyle(); |
| 1441 |
const bool hasFill = SkPaint::kFill_Style == style |
| 1442 |
|| SkPaint::kStrokeAndFill_Style == style; |
| 1443 |
const bool hasStroke = SkPaint::kStroke_Style == style |
| 1444 |
|| SkPaint::kStrokeAndFill_Style == style; |
| 1445 |
|
| 1446 |
//TODO(bungeman): use dictionaries and lookups. |
| 1447 |
if (hasFill) { |
| 1448 |
*fill = TRUE; |
| 1449 |
SkTScopedComPtr<IXpsOMBrush> fillBrush; |
| 1450 |
HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix)); |
| 1451 |
HRM(shadedPath->SetFillBrushLocal(fillBrush.get()), |
| 1452 |
"Could not set fill for shaded path."); |
| 1453 |
} |
| 1454 |
|
| 1455 |
if (hasStroke) { |
| 1456 |
*stroke = TRUE; |
| 1457 |
SkTScopedComPtr<IXpsOMBrush> strokeBrush; |
| 1458 |
HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix)); |
| 1459 |
HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()), |
| 1460 |
"Could not set stroke brush for shaded path."); |
| 1461 |
HRM(shadedPath->SetStrokeThickness( |
| 1462 |
SkScalarToFLOAT(shaderPaint.getStrokeWidth())), |
| 1463 |
"Could not set shaded path stroke thickness."); |
| 1464 |
|
| 1465 |
if (0 == shaderPaint.getStrokeWidth()) { |
| 1466 |
//XPS hair width is a hack. (XPS Spec 11.6.12). |
| 1467 |
SkTScopedComPtr<IXpsOMDashCollection> dashes; |
| 1468 |
HRM(shadedPath->GetStrokeDashes(&dashes), |
| 1469 |
"Could not set dashes for shaded path."); |
| 1470 |
XPS_DASH dash; |
| 1471 |
dash.length = 1.0; |
| 1472 |
dash.gap = 0.0; |
| 1473 |
HRM(dashes->Append(&dash), "Could not add dashes to shaded path."); |
| 1474 |
HRM(shadedPath->SetStrokeDashOffset(-2.0), |
| 1475 |
"Could not set dash offset for shaded path."); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
return S_OK; |
| 1479 |
} |
| 1480 |
|
| 1481 |
void SkXPSDevice::drawPath(const SkPath& platonicPath, |
| 1482 |
const SkPaint& origPaint) { |
| 1483 |
bool pathIsMutable = false; |
| 1484 |
SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
| 1485 |
|
| 1486 |
// nothing to draw |
| 1487 |
if (this->isClipEmpty() || (paint->getAlpha() == 0 && paint->isSrcOver())) { |
| 1488 |
return; |
| 1489 |
} |
| 1490 |
|
| 1491 |
SkPath modifiedPath; |
| 1492 |
const bool paintHasPathEffect = paint->getPathEffect() |
| 1493 |
|| paint->getStyle() != SkPaint::kFill_Style; |
| 1494 |
|
| 1495 |
//Apply pre-path matrix [Platonic-path -> Skeletal-path]. |
| 1496 |
SkMatrix matrix = this->localToDevice(); |
| 1497 |
SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath); |
| 1498 |
|
| 1499 |
//Apply path effect [Skeletal-path -> Fillable-path]. |
| 1500 |
SkPath* fillablePath = skeletalPath; |
| 1501 |
if (paintHasPathEffect) { |
| 1502 |
if (!pathIsMutable) { |
| 1503 |
fillablePath = &modifiedPath; |
| 1504 |
pathIsMutable = true; |
| 1505 |
} |
| 1506 |
SkPathBuilder builder; |
| 1507 |
bool fill = skpathutils::FillPathWithPaint(*skeletalPath, *paint, &builder); |
| 1508 |
*fillablePath = builder.detach(); |
| 1509 |
|
| 1510 |
SkPaint* writablePaint = paint.writable(); |
| 1511 |
writablePaint->setPathEffect(nullptr); |
| 1512 |
if (fill) { |
| 1513 |
writablePaint->setStyle(SkPaint::kFill_Style); |
| 1514 |
} else { |
| 1515 |
writablePaint->setStyle(SkPaint::kStroke_Style); |
| 1516 |
writablePaint->setStrokeWidth(0); |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
//Create the shaded path. This will be the path which is painted. |
| 1521 |
SkTScopedComPtr<IXpsOMPath> shadedPath; |
| 1522 |
HRVM(this->fXpsFactory->CreatePath(&shadedPath), |
| 1523 |
"Could not create shaded path for path."); |
| 1524 |
|
| 1525 |
//Create the geometry for the shaded path. |
| 1526 |
SkTScopedComPtr<IXpsOMGeometry> shadedGeometry; |
| 1527 |
HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry), |
| 1528 |
"Could not create shaded geometry for path."); |
| 1529 |
|
| 1530 |
//Add the geometry to the shaded path. |
| 1531 |
HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()), |
| 1532 |
"Could not add the shaded geometry to shaded path."); |
| 1533 |
|
| 1534 |
SkMaskFilter* filter = paint->getMaskFilter(); |
| 1535 |
|
| 1536 |
//Determine if we will draw or shade and mask. |
| 1537 |
if (filter) { |
| 1538 |
if (paint->getStyle() != SkPaint::kFill_Style) { |
| 1539 |
paint.writable()->setStyle(SkPaint::kFill_Style); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
//Set the brushes. |
| 1544 |
BOOL fill; |
| 1545 |
BOOL stroke; |
| 1546 |
HRV(this->shadePath(shadedPath.get(), |
| 1547 |
*paint, |
| 1548 |
this->localToDevice(), |
| 1549 |
&fill, |
| 1550 |
&stroke)); |
| 1551 |
|
| 1552 |
//Mask filter |
| 1553 |
if (filter) { |
| 1554 |
SkIRect clipIRect; |
| 1555 |
SkVector ppuScale; |
| 1556 |
this->convertToPpm(filter, |
| 1557 |
&matrix, |
| 1558 |
&ppuScale, |
| 1559 |
this->devClipBounds(), |
| 1560 |
&clipIRect); |
| 1561 |
|
| 1562 |
//[Fillable-path -> Pixel-path] |
| 1563 |
SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath; |
| 1564 |
*pixelPath = fillablePath->makeTransform(matrix); |
| 1565 |
auto pixelRaw = SkPathPriv::Raw(*pixelPath, SkResolveConvexity::kYes); |
| 1566 |
if (!pixelRaw) { |
| 1567 |
return; |
| 1568 |
} |
| 1569 |
|
| 1570 |
SkMask* mask = nullptr; |
| 1571 |
|
| 1572 |
SkASSERT(SkPaint::kFill_Style == paint->getStyle() || |
| 1573 |
(SkPaint::kStroke_Style == paint->getStyle() && 0 == paint->getStrokeWidth())); |
| 1574 |
SkStrokeRec::InitStyle style = (SkPaint::kFill_Style == paint->getStyle()) |
| 1575 |
? SkStrokeRec::kFill_InitStyle |
| 1576 |
: SkStrokeRec::kHairline_InitStyle; |
| 1577 |
//[Pixel-path -> Mask] |
| 1578 |
SkMaskBuilder rasteredMask; |
| 1579 |
if (skcpu::DrawToMask(*pixelRaw, |
| 1580 |
clipIRect, |
| 1581 |
filter, //just to compute how much to draw. |
| 1582 |
&matrix, |
| 1583 |
&rasteredMask, |
| 1584 |
SkMaskBuilder::kComputeBoundsAndRenderImage_CreateMode, |
| 1585 |
style)) { |
| 1586 |
|
| 1587 |
SkAutoMaskFreeImage rasteredAmi(rasteredMask.image()); |
| 1588 |
mask = &rasteredMask; |
| 1589 |
|
| 1590 |
//[Mask -> Mask] |
| 1591 |
SkMaskBuilder filteredMask; |
| 1592 |
if (as_MFB(filter)->filterMask(&filteredMask, rasteredMask, matrix, nullptr)) { |
| 1593 |
mask = &filteredMask; |
| 1594 |
} |
| 1595 |
SkAutoMaskFreeImage filteredAmi(filteredMask.image()); |
| 1596 |
|
| 1597 |
//Draw mask. |
| 1598 |
HRV(this->applyMask(*mask, ppuScale, shadedPath.get())); |
| 1599 |
} |
| 1600 |
return; |
| 1601 |
} |
| 1602 |
|
| 1603 |
//Get the figures from the shaded geometry. |
| 1604 |
SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures; |
| 1605 |
HRVM(shadedGeometry->GetFigures(&shadedFigures), |
| 1606 |
"Could not get shaded figures for shaded path."); |
| 1607 |
|
| 1608 |
bool xpsTransformsPath = true; |
| 1609 |
|
| 1610 |
//Set the fill rule. |
| 1611 |
SkPath* xpsCompatiblePath = fillablePath; |
| 1612 |
XPS_FILL_RULE xpsFillRule; |
| 1613 |
switch (fillablePath->getFillType()) { |
| 1614 |
case SkPathFillType::kWinding: |
| 1615 |
xpsFillRule = XPS_FILL_RULE_NONZERO; |
| 1616 |
break; |
| 1617 |
case SkPathFillType::kEvenOdd: |
| 1618 |
xpsFillRule = XPS_FILL_RULE_EVENODD; |
| 1619 |
break; |
| 1620 |
case SkPathFillType::kInverseWinding: { |
| 1621 |
//[Fillable-path (inverse winding) -> XPS-path (inverse even odd)] |
| 1622 |
if (!pathIsMutable) { |
| 1623 |
xpsCompatiblePath = &modifiedPath; |
| 1624 |
pathIsMutable = true; |
| 1625 |
} |
| 1626 |
if (!Simplify(*fillablePath, xpsCompatiblePath)) { |
| 1627 |
SkDEBUGF("Could not simplify inverse winding path."); |
| 1628 |
return; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
[[fallthrough]]; // The xpsCompatiblePath is now inverse even odd, so fall through. |
| 1632 |
case SkPathFillType::kInverseEvenOdd: { |
| 1633 |
const SkRect universe = SkRect::MakeLTRB( |
| 1634 |
0, 0, |
| 1635 |
this->fCurrentCanvasSize.fWidth, |
| 1636 |
this->fCurrentCanvasSize.fHeight); |
| 1637 |
SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure; |
| 1638 |
HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure)); |
| 1639 |
HRVM(shadedFigures->Append(addOneFigure.get()), |
| 1640 |
"Could not add even-odd flip figure to shaded path."); |
| 1641 |
xpsTransformsPath = false; |
| 1642 |
xpsFillRule = XPS_FILL_RULE_EVENODD; |
| 1643 |
break; |
| 1644 |
} |
| 1645 |
default: |
| 1646 |
SkDEBUGFAIL("Unknown SkPath::FillType."); |
| 1647 |
} |
| 1648 |
HRVM(shadedGeometry->SetFillRule(xpsFillRule), |
| 1649 |
"Could not set fill rule for shaded path."); |
| 1650 |
|
| 1651 |
//Create the XPS transform, if possible. |
| 1652 |
if (xpsTransformsPath) { |
| 1653 |
SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform; |
| 1654 |
HRV(this->createXpsTransform(matrix, &xpsTransform)); |
| 1655 |
|
| 1656 |
if (xpsTransform.get()) { |
| 1657 |
HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()), |
| 1658 |
"Could not set transform on shaded path."); |
| 1659 |
} else { |
| 1660 |
xpsTransformsPath = false; |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
SkPath* devicePath = xpsCompatiblePath; |
| 1665 |
if (!xpsTransformsPath) { |
| 1666 |
//[Fillable-path -> Device-path] |
| 1667 |
devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath; |
| 1668 |
*devicePath = xpsCompatiblePath->makeTransform(matrix); |
| 1669 |
} |
| 1670 |
HRV(this->addXpsPathGeometry(shadedFigures.get(), |
| 1671 |
stroke, fill, *devicePath)); |
| 1672 |
|
| 1673 |
HRV(this->clip(shadedPath.get())); |
| 1674 |
|
| 1675 |
//Add the path to the active visual collection. |
| 1676 |
SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals; |
| 1677 |
HRVM(this->fCurrentXpsCanvas->GetVisuals(¤tVisuals), |
| 1678 |
"Could not get current visuals for shaded path."); |
| 1679 |
HRVM(currentVisuals->Append(shadedPath.get()), |
| 1680 |
"Could not add shaded path to current visuals."); |
| 1681 |
} |
| 1682 |
|
| 1683 |
HRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual) { |
| 1684 |
if (this->cs().isWideOpen()) { |
| 1685 |
return S_OK; |
| 1686 |
} |
| 1687 |
// clipPath.addRect(this->devClipBounds())); |
| 1688 |
SkPath clipPath = SkClipStack_AsPath(this->cs()); |
| 1689 |
// TODO: handle all the kinds of paths, like drawPath does |
| 1690 |
return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD); |
| 1691 |
} |
| 1692 |
HRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual, |
| 1693 |
const SkPath& clipPath, |
| 1694 |
XPS_FILL_RULE fillRule) { |
| 1695 |
//Create the geometry. |
| 1696 |
SkTScopedComPtr<IXpsOMGeometry> clipGeometry; |
| 1697 |
HRM(this->fXpsFactory->CreateGeometry(&clipGeometry), |
| 1698 |
"Could not create clip geometry."); |
| 1699 |
|
| 1700 |
//Get the figure collection of the geometry. |
| 1701 |
SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures; |
| 1702 |
HRM(clipGeometry->GetFigures(&clipFigures), |
| 1703 |
"Could not get the clip figures."); |
| 1704 |
|
| 1705 |
//Create the figures into the geometry. |
| 1706 |
HR(this->addXpsPathGeometry( |
| 1707 |
clipFigures.get(), |
| 1708 |
FALSE, TRUE, clipPath)); |
| 1709 |
|
| 1710 |
HRM(clipGeometry->SetFillRule(fillRule), |
| 1711 |
"Could not set fill rule."); |
| 1712 |
HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()), |
| 1713 |
"Could not set clip geometry."); |
| 1714 |
|
| 1715 |
return S_OK; |
| 1716 |
} |
| 1717 |
|
| 1718 |
HRESULT SkXPSDevice::CreateTypefaceUse(const SkFont& font, |
| 1719 |
TypefaceUse** typefaceUse) { |
| 1720 |
SkTypeface* typeface = font.getTypeface(); |
| 1721 |
|
| 1722 |
//Check cache. |
| 1723 |
const SkTypefaceID typefaceID = typeface->uniqueID(); |
| 1724 |
for (TypefaceUse& current : *this->fTopTypefaces) { |
| 1725 |
if (current.typefaceId == typefaceID) { |
| 1726 |
*typefaceUse = ¤t; |
| 1727 |
return S_OK; |
| 1728 |
} |
| 1729 |
} |
| 1730 |
|
| 1731 |
//TODO: create glyph only fonts |
| 1732 |
//and let the host deal with what kind of font we're looking at. |
| 1733 |
XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED; |
| 1734 |
|
| 1735 |
SkTScopedComPtr<IStream> fontStream; |
| 1736 |
int ttcIndex; |
| 1737 |
std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex); |
| 1738 |
if (!fontData) { |
| 1739 |
return E_NOTIMPL; |
| 1740 |
} |
| 1741 |
//TODO: cannot handle FON fonts. |
| 1742 |
HRM(SkIStream::CreateFromSkStream(fontData->duplicate(), &fontStream), |
| 1743 |
"Could not create font stream."); |
| 1744 |
|
| 1745 |
const size_t size = |
| 1746 |
std::size(L"/Resources/Fonts/" L_GUID_ID L".odttf"); |
| 1747 |
wchar_t buffer[size]; |
| 1748 |
wchar_t id[GUID_ID_LEN]; |
| 1749 |
HR(this->createId(id, GUID_ID_LEN)); |
| 1750 |
swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id); |
| 1751 |
|
| 1752 |
SkTScopedComPtr<IOpcPartUri> partUri; |
| 1753 |
HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri), |
| 1754 |
"Could not create font resource part uri."); |
| 1755 |
|
| 1756 |
SkTScopedComPtr<IXpsOMFontResource> xpsFontResource; |
| 1757 |
HRM(this->fXpsFactory->CreateFontResource(fontStream.get(), |
| 1758 |
embedding, |
| 1759 |
partUri.get(), |
| 1760 |
FALSE, |
| 1761 |
&xpsFontResource), |
| 1762 |
"Could not create font resource."); |
| 1763 |
|
| 1764 |
//TODO: change openStream to return -1 for non-ttc, get rid of this. |
| 1765 |
const uint8_t* data = (const uint8_t*)fontData->getMemoryBase(); |
| 1766 |
bool isTTC = (data && |
| 1767 |
fontData->getLength() >= sizeof(SkTTCFHeader) && |
| 1768 |
((const SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG); |
| 1769 |
|
| 1770 |
int glyphCount = typeface->countGlyphs(); |
| 1771 |
|
| 1772 |
TypefaceUse& newTypefaceUse = this->fTopTypefaces->emplace_back( |
| 1773 |
typefaceID, |
| 1774 |
isTTC ? ttcIndex : -1, |
| 1775 |
std::move(fontData), |
| 1776 |
std::move(xpsFontResource), |
| 1777 |
glyphCount); |
| 1778 |
|
| 1779 |
*typefaceUse = &newTypefaceUse; |
| 1780 |
return S_OK; |
| 1781 |
} |
| 1782 |
|
| 1783 |
HRESULT SkXPSDevice::AddGlyphs(IXpsOMObjectFactory* xpsFactory, |
| 1784 |
IXpsOMCanvas* canvas, |
| 1785 |
const TypefaceUse* font, |
| 1786 |
LPCWSTR text, |
| 1787 |
XPS_GLYPH_INDEX* xpsGlyphs, |
| 1788 |
UINT32 xpsGlyphsLen, |
| 1789 |
XPS_POINT *origin, |
| 1790 |
FLOAT fontSize, |
| 1791 |
XPS_STYLE_SIMULATION sims, |
| 1792 |
const SkMatrix& transform, |
| 1793 |
const SkPaint& paint) { |
| 1794 |
SkTScopedComPtr<IXpsOMGlyphs> glyphs; |
| 1795 |
HRM(xpsFactory->CreateGlyphs(font->xpsFont.get(), &glyphs), "Could not create glyphs."); |
| 1796 |
HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index."); |
| 1797 |
|
| 1798 |
//XPS uses affine transformations for everything... |
| 1799 |
//...except positioning text. |
| 1800 |
bool useCanvasForClip; |
| 1801 |
if (transform.isTranslate()) { |
| 1802 |
origin->x += SkScalarToFLOAT(transform.getTranslateX()); |
| 1803 |
origin->y += SkScalarToFLOAT(transform.getTranslateY()); |
| 1804 |
useCanvasForClip = false; |
| 1805 |
} else { |
| 1806 |
SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse; |
| 1807 |
HR(this->createXpsTransform(transform, &xpsMatrixToUse)); |
| 1808 |
if (xpsMatrixToUse.get()) { |
| 1809 |
HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()), |
| 1810 |
"Could not set transform matrix."); |
| 1811 |
useCanvasForClip = true; |
| 1812 |
} else { |
| 1813 |
SkDEBUGFAIL("Attempt to add glyphs in perspective."); |
| 1814 |
useCanvasForClip = false; |
| 1815 |
} |
| 1816 |
} |
| 1817 |
|
| 1818 |
SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor; |
| 1819 |
HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor."); |
| 1820 |
|
| 1821 |
if (text) { |
| 1822 |
HRM(glyphsEditor->SetUnicodeString(text), |
| 1823 |
"Could not set unicode string."); |
| 1824 |
} |
| 1825 |
|
| 1826 |
if (xpsGlyphs) { |
| 1827 |
HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs), |
| 1828 |
"Could not set glyphs."); |
| 1829 |
} |
| 1830 |
|
| 1831 |
HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits."); |
| 1832 |
|
| 1833 |
SkTScopedComPtr<IXpsOMBrush> xpsFillBrush; |
| 1834 |
HR(this->createXpsBrush( |
| 1835 |
paint, |
| 1836 |
&xpsFillBrush, |
| 1837 |
useCanvasForClip ? nullptr : &transform)); |
| 1838 |
|
| 1839 |
HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()), |
| 1840 |
"Could not set fill brush."); |
| 1841 |
|
| 1842 |
HRM(glyphs->SetOrigin(origin), "Could not set glyph origin."); |
| 1843 |
|
| 1844 |
HRM(glyphs->SetFontRenderingEmSize(fontSize), |
| 1845 |
"Could not set font size."); |
| 1846 |
|
| 1847 |
HRM(glyphs->SetStyleSimulations(sims), |
| 1848 |
"Could not set style simulations."); |
| 1849 |
|
| 1850 |
SkTScopedComPtr<IXpsOMVisualCollection> visuals; |
| 1851 |
HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals."); |
| 1852 |
|
| 1853 |
if (!useCanvasForClip) { |
| 1854 |
HR(this->clip(glyphs.get())); |
| 1855 |
HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas."); |
| 1856 |
} else { |
| 1857 |
SkTScopedComPtr<IXpsOMCanvas> glyphCanvas; |
| 1858 |
HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas), |
| 1859 |
"Could not create glyph canvas."); |
| 1860 |
|
| 1861 |
SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals; |
| 1862 |
HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals), |
| 1863 |
"Could not get glyph visuals collection."); |
| 1864 |
|
| 1865 |
HRM(glyphCanvasVisuals->Append(glyphs.get()), |
| 1866 |
"Could not add glyphs to page."); |
| 1867 |
HR(this->clip(glyphCanvas.get())); |
| 1868 |
|
| 1869 |
HRM(visuals->Append(glyphCanvas.get()), |
| 1870 |
"Could not add glyph canvas to page."); |
| 1871 |
} |
| 1872 |
|
| 1873 |
return S_OK; |
| 1874 |
} |
| 1875 |
|
| 1876 |
static bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) { |
| 1877 |
const SkPaint::Style style = paint.getStyle(); |
| 1878 |
return matrix.hasPerspective() |
| 1879 |
|| SkPaint::kStroke_Style == style |
| 1880 |
|| SkPaint::kStrokeAndFill_Style == style |
| 1881 |
|| paint.getMaskFilter() |
| 1882 |
; |
| 1883 |
} |
| 1884 |
|
| 1885 |
void SkXPSDevice::onDrawGlyphRunList(SkCanvas*, |
| 1886 |
const sktext::GlyphRunList& glyphRunList, |
| 1887 |
const SkPaint& paint) { |
| 1888 |
SkASSERT(!glyphRunList.hasRSXForm()); |
| 1889 |
|
| 1890 |
for (const auto& run : glyphRunList) { |
| 1891 |
const SkGlyphID* glyphIDs = run.glyphsIDs().data(); |
| 1892 |
size_t glyphCount = run.glyphsIDs().size(); |
| 1893 |
const SkFont& font = run.font(); |
| 1894 |
|
| 1895 |
if (!glyphCount || !glyphIDs || font.getSize() <= 0) { |
| 1896 |
continue; |
| 1897 |
} |
| 1898 |
|
| 1899 |
TypefaceUse* typeface; |
| 1900 |
if (FAILED(CreateTypefaceUse(font, &typeface)) || |
| 1901 |
text_must_be_pathed(paint, this->localToDevice())) { |
| 1902 |
SkPath path; |
| 1903 |
//TODO: make this work, Draw currently does not handle as well. |
| 1904 |
//paint.getTextPath(text, byteLength, x, y, &path); |
| 1905 |
//this->drawPath(path, paint, nullptr, true); |
| 1906 |
//TODO: add automation "text" |
| 1907 |
continue; |
| 1908 |
} |
| 1909 |
|
| 1910 |
//TODO: handle font scale and skew in x (text_scale_skew) |
| 1911 |
|
| 1912 |
// Advance width and offsets for glyphs measured in hundredths of the font em size |
| 1913 |
// (XPS Spec 5.1.3). |
| 1914 |
FLOAT centemPerUnit = 100.0f / SkScalarToFLOAT(font.getSize()); |
| 1915 |
AutoSTMalloc<32, XPS_GLYPH_INDEX> xpsGlyphs(glyphCount); |
| 1916 |
size_t numGlyphs = typeface->glyphsUsed.size(); |
| 1917 |
size_t actualGlyphCount = 0; |
| 1918 |
|
| 1919 |
for (size_t i = 0; i < glyphCount; ++i) { |
| 1920 |
if (numGlyphs <= glyphIDs[i]) { |
| 1921 |
continue; |
| 1922 |
} |
| 1923 |
const SkPoint& position = run.positions()[i]; |
| 1924 |
XPS_GLYPH_INDEX& xpsGlyph = xpsGlyphs[actualGlyphCount++]; |
| 1925 |
xpsGlyph.index = glyphIDs[i]; |
| 1926 |
xpsGlyph.advanceWidth = 0.0f; |
| 1927 |
xpsGlyph.horizontalOffset = (SkScalarToFloat(position.fX) * centemPerUnit); |
| 1928 |
xpsGlyph.verticalOffset = (SkScalarToFloat(position.fY) * -centemPerUnit); |
| 1929 |
typeface->glyphsUsed.set(xpsGlyph.index); |
| 1930 |
} |
| 1931 |
|
| 1932 |
if (actualGlyphCount == 0) { |
| 1933 |
return; |
| 1934 |
} |
| 1935 |
|
| 1936 |
XPS_POINT origin = { |
| 1937 |
glyphRunList.origin().x(), |
| 1938 |
glyphRunList.origin().y(), |
| 1939 |
}; |
| 1940 |
|
| 1941 |
HRV(AddGlyphs(this->fXpsFactory.get(), |
| 1942 |
this->fCurrentXpsCanvas.get(), |
| 1943 |
typeface, |
| 1944 |
nullptr, |
| 1945 |
xpsGlyphs.get(), actualGlyphCount, |
| 1946 |
&origin, |
| 1947 |
SkScalarToFLOAT(font.getSize()), |
| 1948 |
XPS_STYLE_SIMULATION_NONE, |
| 1949 |
this->localToDevice(), |
| 1950 |
paint)); |
| 1951 |
} |
| 1952 |
} |
| 1953 |
|
| 1954 |
void SkXPSDevice::drawDevice(SkDevice* dev, const SkSamplingOptions&, const SkPaint&) { |
| 1955 |
SkXPSDevice* that = static_cast<SkXPSDevice*>(dev); |
| 1956 |
SkASSERT(that->fTopTypefaces == this->fTopTypefaces); |
| 1957 |
|
| 1958 |
SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform; |
| 1959 |
HRVM(this->createXpsTransform(dev->getRelativeTransform(*this).asM33(), &xpsTransform), |
| 1960 |
"Could not create layer transform."); |
| 1961 |
HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()), |
| 1962 |
"Could not set layer transform."); |
| 1963 |
|
| 1964 |
//Get the current visual collection and add the layer to it. |
| 1965 |
SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals; |
| 1966 |
HRVM(this->fCurrentXpsCanvas->GetVisuals(¤tVisuals), |
| 1967 |
"Could not get current visuals for layer."); |
| 1968 |
HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()), |
| 1969 |
"Could not add layer to current visuals."); |
| 1970 |
} |
| 1971 |
|
| 1972 |
sk_sp<SkDevice> SkXPSDevice::createDevice(const CreateInfo& info, const SkPaint*) { |
| 1973 |
sk_sp<SkXPSDevice> dev = sk_make_sp<SkXPSDevice>(info.fInfo.dimensions(), fOpts); |
| 1974 |
dev->fXpsFactory.reset(SkRefComPtr(fXpsFactory.get())); |
| 1975 |
dev->fCurrentCanvasSize = this->fCurrentCanvasSize; |
| 1976 |
dev->fCurrentUnitsPerMeter = this->fCurrentUnitsPerMeter; |
| 1977 |
dev->fCurrentPixelsPerMeter = this->fCurrentPixelsPerMeter; |
| 1978 |
dev->fTopTypefaces = this->fTopTypefaces; |
| 1979 |
SkAssertResult(dev->createCanvasForLayer()); |
| 1980 |
return dev; |
| 1981 |
} |
| 1982 |
|
| 1983 |
void SkXPSDevice::drawOval( const SkRect& o, const SkPaint& p) { |
| 1984 |
this->drawPath(SkPath::Oval(o), p); |
| 1985 |
} |
| 1986 |
|
| 1987 |
void SkXPSDevice::drawImageRect(const SkImage* image, |
| 1988 |
const SkRect* src, |
| 1989 |
const SkRect& dst, |
| 1990 |
const SkSamplingOptions& sampling, |
| 1991 |
const SkPaint& paint, |
| 1992 |
SkCanvas::SrcRectConstraint constraint) { |
| 1993 |
SkASSERT(image); |
| 1994 |
|
| 1995 |
// TODO: support gpu images |
| 1996 |
SkBitmap bitmap; |
| 1997 |
auto imageBase = as_IB(image); |
| 1998 |
if (!imageBase->getROPixels(nullptr, &bitmap)) { |
| 1999 |
return; |
| 2000 |
} |
| 2001 |
|
| 2002 |
SkRect bitmapBounds = SkRect::Make(bitmap.bounds()); |
| 2003 |
SkRect srcBounds = src ? *src : bitmapBounds; |
| 2004 |
SkMatrix matrix = SkMatrix::RectToRectOrIdentity(srcBounds, dst); |
| 2005 |
SkRect actualDst; |
| 2006 |
if (!src || bitmapBounds.contains(*src)) { |
| 2007 |
actualDst = dst; |
| 2008 |
} else { |
| 2009 |
if (!srcBounds.intersect(bitmapBounds)) { |
| 2010 |
return; |
| 2011 |
} |
| 2012 |
matrix.mapRect(&actualDst, srcBounds); |
| 2013 |
} |
| 2014 |
|
| 2015 |
auto img = |
| 2016 |
SkImage_Raster::MakeFromBitmap(bitmap, SkCopyPixelsMode::kNever, imageBase->refMips()); |
| 2017 |
auto imgShader = img->makeShaderForPaint( |
| 2018 |
paint, SkTileMode::kClamp, SkTileMode::kClamp, sampling, &matrix); |
| 2019 |
SkASSERT(imgShader); |
| 2020 |
if (!imgShader) { |
| 2021 |
return; |
| 2022 |
} |
| 2023 |
SkPaint paintWithShader(paint); |
| 2024 |
paintWithShader.setStyle(SkPaint::kFill_Style); |
| 2025 |
paintWithShader.setShader(std::move(imgShader)); |
| 2026 |
this->drawRect(actualDst, paintWithShader); |
| 2027 |
} |
| 2028 |
#endif//defined(SK_BUILD_FOR_WIN) |