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