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