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