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