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