1b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*
2b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com * Copyright 2011 Google Inc.
3b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com *
4b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com * Use of this source code is governed by a BSD-style license that can be
5b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com * found in the LICENSE file.
6b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com */
7b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
8b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#ifndef UNICODE
9b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define UNICODE
10b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#endif
11b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#ifndef _UNICODE
12b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define _UNICODE
13b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#endif
14b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkTypes.h"
15b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <ObjBase.h>
16b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <XpsObjectModel.h>
17b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <T2EmbApi.h>
18b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <FontSub.h>
19b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
20b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkColor.h"
21b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkConstexprMath.h"
22b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkData.h"
23b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkDraw.h"
24b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkDrawProcs.h"
25b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org#include "SkEndian.h"
26b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkFontHost.h"
27b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkGlyphCache.h"
28b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkHRESULT.h"
29b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkImageEncoder.h"
30b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkIStream.h"
31b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkMaskFilter.h"
32b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkPaint.h"
33da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#include "SkPathOps.h"
34b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkPoint.h"
35b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkRasterizer.h"
36b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org#include "SkSFNTHeader.h"
37b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkShader.h"
38b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkSize.h"
39b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkStream.h"
40b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkTDArray.h"
41b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkTLazy.h"
42b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkTScopedComPtr.h"
43b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org#include "SkTTCFHeader.h"
44398de9ad9cb1ed3aa97aaf023d7f8287b8e62601reed@google.com#include "SkTypefacePriv.h"
45b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkUtils.h"
46b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkXPSDevice.h"
47b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
48b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//Windows defines a FLOAT type,
49b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//make it clear when converting a scalar that this is what is wanted.
50b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define SkScalarToFLOAT(n) SkScalarToFloat(n)
51b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
52da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner//Dummy representation of a GUID from createId.
53b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
54da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner//Length of GUID representation from createId, including NULL terminator.
55b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
56b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
57b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/**
58b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   Formats a GUID and places it into buffer.
59b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   buffer should have space for at least GUID_ID_LEN wide characters.
60b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   The string will always be wchar null terminated.
61b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
62b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   @return -1 if there was an error, > 0 if success.
63b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com */
64b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic int format_guid(const GUID& guid,
65b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       wchar_t* buffer, size_t bufferSize,
66b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       wchar_t sep = '-') {
67b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkASSERT(bufferSize >= GUID_ID_LEN);
68b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return swprintf_s(buffer,
69b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      bufferSize,
70b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
71b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data1,
72b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
73b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data2,
74b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
75b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data3,
76b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
77b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[0],
78b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[1],
79b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
80b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[2],
81b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[3],
82b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[4],
83b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[5],
84b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[6],
85b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[7]);
86b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
87b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
88da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen WagnerHRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
89b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    GUID guid = {};
90da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#ifdef SK_XPS_USE_DETERMINISTIC_IDS
91da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    guid.Data1 = fNextId++;
92da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    // The following make this a valid Type4 UUID.
93da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    guid.Data3 = 0x4000;
94da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    guid.Data4[0] = 0x80;
95da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#else
96b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
97da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#endif
9874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
99b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (format_guid(guid, buffer, bufferSize, sep) == -1) {
100b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(E_UNEXPECTED, "Could not format GUID into id.");
101b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
10274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
103b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
104b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
105b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
106b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic SkBitmap make_fake_bitmap(int width, int height) {
107b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkBitmap bitmap;
108a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org    bitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
109b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return bitmap;
110b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
111b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
112900ecf2f1579d42c9d2959831787af0346320f86reed@google.com// TODO: should inherit from SkBaseDevice instead of SkBitmapDevice...
113b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::SkXPSDevice()
1141f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    : SkBitmapDevice(make_fake_bitmap(10000, 10000))
115b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , fCurrentPage(0) {
116b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
117b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
118b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::~SkXPSDevice() {
119b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
120b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
121b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::TypefaceUse::TypefaceUse()
122b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    : typefaceId(0xffffffff)
123b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , fontData(NULL)
124b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , xpsFont(NULL)
125b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , glyphsUsed(NULL) {
126b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
127b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
128b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::TypefaceUse::~TypefaceUse() {
129b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //xpsFont owns fontData ref
130b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->xpsFont->Release();
131b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    delete this->glyphsUsed;
132b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
133b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
134b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::beginPortfolio(SkWStream* outputStream) {
135b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!this->fAutoCo.succeeded()) return false;
13674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
137b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create XPS Factory.
138b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(CoCreateInstance(
139b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSID_XpsOMObjectFactory,
140b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             NULL,
141b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSCTX_INPROC_SERVER,
142b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             IID_PPV_ARGS(&this->fXpsFactory)),
143b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create XPS factory.");
14474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
145b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream),
146b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not convert SkStream to IStream.");
14774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
148b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
149b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
150b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
151b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::beginSheet(
152b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkVector& unitsPerMeter,
153b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkVector& pixelsPerMeter,
154b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkSize& trimSize,
155b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* mediaBox,
156b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* bleedBox,
157b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* artBox,
158b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* cropBox) {
159b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ++this->fCurrentPage;
16074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
161b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //For simplicity, just write everything out in geometry units,
162b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //then have a base canvas do the scale to physical units.
163b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentCanvasSize = trimSize;
164b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentUnitsPerMeter = unitsPerMeter;
165b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentPixelsPerMeter = pixelsPerMeter;
16674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
167b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentXpsCanvas.reset();
168b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
169b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create base canvas.");
17074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
171b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
172b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
173b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
174b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
175b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                        const unsigned int pageNum,
176b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                        IXpsOMImageResource** image) {
177b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
178b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(CoCreateInstance(
179b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            CLSID_XpsOMThumbnailGenerator,
180b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            NULL,
181b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            CLSCTX_INPROC_SERVER,
182b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            IID_PPV_ARGS(&thumbnailGenerator)),
183b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create thumbnail generator.");
18474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
185b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
186b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    static const size_t size = SkTUMax<
187b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + SK_DIGITS_IN(pageNum),
188b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png")
189b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    >::value;
190b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
191b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (pageNum > 0) {
192b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
193b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
194b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        wchar_t id[GUID_ID_LEN];
195da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner        HR(this->createId(id, GUID_ID_LEN));
196b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        swprintf_s(buffer, size, L"/Metadata/%s.png", id);
197b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
198b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
199b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create thumbnail part uri.");
20074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
201b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(thumbnailGenerator->GenerateThumbnail(page,
202b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              XPS_IMAGE_TYPE_PNG,
203b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              XPS_THUMBNAIL_SIZE_LARGE,
204b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              partUri.get(),
205b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              image),
206b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not generate thumbnail.");
20774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
208b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
209b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
210b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
211b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
212b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMPage** page) {
213b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const size_t size = SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
214b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             + SK_DIGITS_IN(fCurrentPage);
215b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
216b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
217b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             this->fCurrentPage);
218b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
219b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
220b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create page part uri.");
22174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
222b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
223b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePage(&pageSize,
224b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      L"und",
225b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      partUri.get(),
226b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      page),
227b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create page.");
22874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
229b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
230b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
231b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
232b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
233b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create package writer.
234b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    {
235b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IOpcPartUri> partUri;
236b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
237b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             &partUri),
238b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create document sequence part uri.");
239b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreatePackageWriterOnStream(
240b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                this->fOutputStream.get(),
241b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                TRUE,
242b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
243b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                partUri.get(),
244b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                NULL,
245b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                image,
246b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                NULL,
247b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                NULL,
248b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &this->fPackageWriter),
249b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create package writer.");
250b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
25174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
252b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Begin the lone document.
253b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    {
254b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IOpcPartUri> partUri;
255b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreatePartUri(
256b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                L"/Documents/1/FixedDocument.fdoc",
257b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &partUri),
258b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create fixed document part uri.");
259b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
260b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL,
261b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL,
262b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL,
263b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL),
264b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not start document.");
265b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
26674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
267b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
268b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
269b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
270b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::endSheet() {
271b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //XPS is fixed at 96dpi (XPS Spec 11.1).
272b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const float xpsDPI = 96.0f;
273b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const float inchesPerMeter = 10000.0f / 254.0f;
274b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
275b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const float scaleX = targetUnitsPerMeter
276b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
277b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const float scaleY = targetUnitsPerMeter
278b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
27974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
280b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the scale canvas.
281b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
282b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
283b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create scale canvas.");
284b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
285b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
286b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get scale canvas visuals.");
28774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
288b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
289b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
290b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
291b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create geometry to physical transform.");
292b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
293b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set transform on scale canvas.");
29474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
295b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the content canvas to the scale canvas.
296b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
297b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add base canvas to scale canvas.");
29874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
299b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the page.
300b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_SIZE pageSize = {
301b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
302b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
303b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
304b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPage> page;
305b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRB(this->createXpsPage(pageSize, &page));
30674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
307b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
308b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
30974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
310b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the scale canvas to the page.
311b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(pageVisuals->Append(scaleCanvas.get()),
312b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add scale canvas to page.");
31374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
314b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the package writer if it hasn't been created yet.
315b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (NULL == this->fPackageWriter.get()) {
316b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMImageResource> image;
317b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Ignore return, thumbnail is completely optional.
318b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->createXpsThumbnail(page.get(), 0, &image);
31974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
320b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRB(this->initXpsDocumentWriter(image.get()));
321b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
32274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
323b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fPackageWriter->AddPage(page.get(),
324b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       &pageSize,
325b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL,
326b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL,
327b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL,
328b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL),
329b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not write the page.");
330b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentXpsCanvas.reset();
33174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
332b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
333b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
334b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
335b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
336b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //CreateFontPackage wants unsigned short.
337b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Microsoft, Y U NO stdint.h?
338b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<unsigned short> keepList;
339b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    current->glyphsUsed->exportTo(&keepList);
34074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
341b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    int ttcCount = (current->ttcIndex + 1);
342b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
343b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //The following are declared with the types required by CreateFontPackage.
344b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    unsigned char *fontPackageBufferRaw = NULL;
345b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    unsigned long fontPackageBufferSize;
346b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    unsigned long bytesWritten;
347b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    unsigned long result = CreateFontPackage(
348b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        (unsigned char *) current->fontData->getMemoryBase(),
349b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        (unsigned long) current->fontData->getLength(),
350b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        &fontPackageBufferRaw,
351b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        &fontPackageBufferSize,
352b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        &bytesWritten,
353b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
354b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        current->ttcIndex,
355b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        TTFCFP_SUBSET,
356b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0,
357b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0,
358b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0,
359b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        keepList.begin(),
360b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        keepList.count(),
361b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        sk_malloc_throw,
362b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        sk_realloc_throw,
363b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        sk_free,
364b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        NULL);
365b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
366b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (result != NO_ERROR) {
367b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkDEBUGF(("CreateFontPackage Error %lu", result));
368b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return E_UNEXPECTED;
369b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
37074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
371b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    // If it was originally a ttc, keep it a ttc.
372b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
373b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    size_t extra;
374b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    if (ttcCount > 0) {
375b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        // Create space for a ttc header.
376b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
377b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        fontPackageBuffer.realloc(bytesWritten + extra);
378b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        //overlap is certain, use memmove
379b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
380b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
381b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        // Write the ttc header.
382b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
383b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        ttcfHeader->ttcTag = SkTTCFHeader::TAG;
384b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        ttcfHeader->version = SkTTCFHeader::version_1;
385b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
386b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
387b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
388b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org            *offsetPtr = SkEndian_SwapBE32(extra);
389b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        }
390b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
391b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        // Fix up offsets in sfnt table entries.
392b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
393b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
394b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SkSFNTHeader::TableDirectoryEntry* tableDirectory =
395b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org            SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
396b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        for (int i = 0; i < numTables; ++i, ++tableDirectory) {
397b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org            tableDirectory->offset = SkEndian_SwapBE32(
398b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                SkEndian_SwapBE32(tableDirectory->offset) + extra);
399b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        }
400b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    } else {
401b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        extra = 0;
402b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        fontPackageBuffer.realloc(bytesWritten);
403b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    }
404b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
405b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    SkAutoTUnref<SkMemoryStream> newStream(new SkMemoryStream());
406b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    newStream->setMemoryOwned(fontPackageBuffer.detach(), bytesWritten + extra);
407b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
408b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IStream> newIStream;
409b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    SkIStream::CreateFromSkStream(newStream.detach(), true, &newIStream);
41074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
411b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_FONT_EMBEDDING embedding;
412b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(current->xpsFont->GetEmbeddingOption(&embedding),
413b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get embedding option from font.");
41474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
415b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
416b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(current->xpsFont->GetPartName(&partUri),
417b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get part uri from font.");
41874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
419b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(current->xpsFont->SetContent(
420b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            newIStream.get(),
421b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            embedding,
422b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            partUri.get()),
423b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set new stream for subsetted font.");
42474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
425b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
426b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
427b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
428b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::endPortfolio() {
429b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Subset fonts
430b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!this->fTypefaces.empty()) {
431b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
432b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const TypefaceUse* last = &this->fTypefaces.back();
433b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        for (; current <= last; ++current) {
434b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //Ignore return for now, if it didn't subset, let it be.
435b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            subset_typeface(current);
436b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
437b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
43874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
439b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fPackageWriter->Close(), "Could not close writer.");
44074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
441b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
442b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
443b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
444b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_COLOR xps_color(const SkColor skColor) {
445b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
446b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_COLOR xpsColor;
447b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
448b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
449b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.red = SkColorGetR(skColor);
450b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.green = SkColorGetG(skColor);
451b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.blue = SkColorGetB(skColor);
45274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
453b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return xpsColor;
454b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
455b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
456b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_POINT xps_point(const SkPoint& point) {
457b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT xpsPoint = {
458b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(point.fX),
459b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(point.fY),
460b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
461b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return xpsPoint;
462b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
463b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
464b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
465b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint skTransformedPoint;
466b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
467b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return xps_point(skTransformedPoint);
468b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
469b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
470b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
471b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    switch (tileMode) {
472b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    case SkShader::kClamp_TileMode:
473b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return XPS_SPREAD_METHOD_PAD;
474b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    case SkShader::kRepeat_TileMode:
475b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return XPS_SPREAD_METHOD_REPEAT;
476b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    case SkShader::kMirror_TileMode:
477b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return XPS_SPREAD_METHOD_REFLECT;
478b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    default:
479330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com        SkDEBUGFAIL("Unknown tile mode.");
480b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
481b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return XPS_SPREAD_METHOD_PAD;
482b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
483b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
484b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
485b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const SkPoint& start, const SkPoint& end,
486b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const SkMatrix& transform) {
487b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint startTransformed;
488b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    transform.mapXY(start.fX, start.fY, &startTransformed);
489b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint endTransformed;
490b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    transform.mapXY(end.fX, end.fY, &endTransformed);
49174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
492b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Manhattan distance between transformed start and end.
493b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
494b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        + (endTransformed.fY - startTransformed.fY);
495b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (SkScalarNearlyZero(startToEnd)) {
496b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        for (int i = 0; i < numOffsets; ++i) {
497b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            stopOffsets[i] = 0;
498b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
499b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
500b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
50174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
502b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    for (int i = 0; i < numOffsets; ++i) {
503b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPoint stop;
504b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        stop.fX = SkScalarMul(end.fX - start.fX, stopOffsets[i]);
505b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        stop.fY = SkScalarMul(end.fY - start.fY, stopOffsets[i]);
50674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
507b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPoint stopTransformed;
508b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        transform.mapXY(stop.fX, stop.fY, &stopTransformed);
50974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
510b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Manhattan distance between transformed start and stop.
511b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
512b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             + (stopTransformed.fY - startTransformed.fY);
513b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Percentage along transformed line.
514b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        stopOffsets[i] = SkScalarDiv(startToStop, startToEnd);
515b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
516b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
517b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
518b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
519b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                        IXpsOMMatrixTransform** xpsTransform) {
520b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkScalar affine[6];
521b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!matrix.asAffine(affine)) {
522b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        *xpsTransform = NULL;
523b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return S_FALSE;
524b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
525b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_MATRIX rawXpsMatrix = {
526b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
527b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
528b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
529b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
530b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kATransX]),
531b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kATransY]),
532b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
533b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
534b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create transform.");
53574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
536b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
537b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
538b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
539b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
540b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                IXpsOMVisualCollection* visuals,
541b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                IXpsOMPath** path) {
542b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> geometry;
543b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGeometry(&geometry),
544b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create geometry.");
54574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
546b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
547b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
548b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figureCollection->Append(figure), "Could not add figure.");
54974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
550b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
551b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
55274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
553b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(visuals->Append(*path), "Could not add path to visuals.");
554b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
555b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
556b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
557b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
558b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              const SkAlpha alpha,
559b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              IXpsOMBrush** xpsBrush) {
560b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_COLOR xpsColor = xps_color(skColor);
561b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
562b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, NULL, &solidBrush),
563b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create solid color brush.");
564b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
565b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
566b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
567b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
568b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
569b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
570b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 const XPS_RECT& imageViewBox,
571b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 IXpsOMImageResource* image,
572b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 IXpsOMVisualCollection* visuals) {
573b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
574b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
57574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
576b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> areaToFillPath;
577b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
57874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
579b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
580b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateImageBrush(image,
581b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &imageViewBox,
582b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &imageViewBox,
583b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &areaToFillBrush),
584b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create brush for side of clamp.");
585b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
586b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set tile mode for side of clamp.");
587b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
588b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set brush for side of clamp");
58974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
590b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
591b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
592b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
593b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
594b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   const SkColor color,
595b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMVisualCollection* visuals) {
596b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
597b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
59874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
599b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> areaToFillPath;
600b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
60174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
602b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
603b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
604b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
605b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set brush for corner of clamp.");
60674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
607b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
608b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
609b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
610b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_N  = XPS_TILE_MODE_NONE;
611b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_T  = XPS_TILE_MODE_TILE;
612b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_X  = XPS_TILE_MODE_FLIPX;
613b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_Y  = XPS_TILE_MODE_FLIPY;
614b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
615b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
616b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//TODO(bungeman): In the future, should skia add None,
617b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//handle None+Mirror and None+Repeat correctly.
618b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//None is currently an internal hack so masks don't repeat (None+None only).
619b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
620b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    [SkShader::kTileModeCount+1] = {
621b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           //Clamp //Repeat //Mirror //None
622b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*Clamp */ XTM_N,  XTM_T,   XTM_Y,   XTM_N,
623b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*Repeat*/ XTM_T,  XTM_T,   XTM_Y,   XTM_N,
624b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*Mirror*/ XTM_X,  XTM_X,   XTM_XY,  XTM_X,
625b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*None  */ XTM_N,  XTM_N,   XTM_Y,   XTM_N,
626b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com};
627b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
628b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsImageBrush(
629b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkBitmap& bitmap,
630b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkMatrix& localMatrix,
631b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkShader::TileMode (&xy)[2],
632b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkAlpha alpha,
633b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        IXpsOMTileBrush** xpsBrush) {
634b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDynamicMemoryWStream write;
635b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!SkImageEncoder::EncodeStream(&write, bitmap,
636b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      SkImageEncoder::kPNG_Type, 100)) {
637b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(E_FAIL, "Unable to encode bitmap as png.");
638b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
639b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMemoryStream* read = new SkMemoryStream;
640b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    read->setData(write.copyToData())->unref();
641b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IStream> readWrapper;
642b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
643b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create stream from png data.");
64474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
645b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const size_t size =
646b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
647b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
648b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t id[GUID_ID_LEN];
649da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    HR(this->createId(id, GUID_ID_LEN));
650b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
65174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
652b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> imagePartUri;
653b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
654b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create image part uri.");
65574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
656b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMImageResource> imageResource;
657b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateImageResource(
658b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            readWrapper.get(),
659b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_IMAGE_TYPE_PNG,
660b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            imagePartUri.get(),
661b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            &imageResource),
662b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create image resource.");
66374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
664b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_RECT bitmapRect = {
665b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0.0, 0.0,
666b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
667b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
668b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
669b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
670b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &bitmapRect, &bitmapRect,
671b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &xpsImageBrush),
672b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create image brush.");
67374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
674b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (SkShader::kClamp_TileMode != xy[0] &&
675b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkShader::kClamp_TileMode != xy[1]) {
67674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
677b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
678b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set image tile mode");
679b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
680b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set image opacity.");
681b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
682b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
683b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO(bungeman): compute how big this really needs to be.
684b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
685b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const FLOAT BIG_F = SkScalarToFLOAT(BIG);
686b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkScalar bWidth = SkIntToScalar(bitmap.width());
687b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkScalar bHeight = SkIntToScalar(bitmap.height());
68874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
689b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //create brush canvas
690b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
691b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
692b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create image brush canvas.");
693b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
694b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(brushCanvas->GetVisuals(&brushVisuals),
695b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not get image brush canvas visuals collection.");
69674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
697b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //create central figure
698b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
699b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
700b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
70174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
702b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMPath> centralPath;
703b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createPath(centralFigure.get(),
704b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            brushVisuals.get(),
705b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            &centralPath));
706b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
707b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set tile mode for image brush central path.");
708b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
709b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set fill brush for image brush central path.");
71074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
711b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //add left/right
712b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[0]) {
713b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
714b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT leftImageViewBox = {
715b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0.0, 0.0,
716b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                1.0, static_cast<FLOAT>(bitmap.height()),
717b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
718b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(leftArea, leftImageViewBox,
719b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
720b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
721b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
722b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
723b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT rightImageViewBox = {
724b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                bitmap.width() - 1.0f, 0.0f,
725b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                1.0f, static_cast<FLOAT>(bitmap.height()),
726b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
727b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(rightArea, rightImageViewBox,
728b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
729b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
730b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
73174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
732b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //add top/bottom
733b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[1]) {
734b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
735b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT topImageViewBox = {
736b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0.0, 0.0,
737b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<FLOAT>(bitmap.width()), 1.0,
738b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
739b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(topArea, topImageViewBox,
740b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
741b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
74274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
743b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
744b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT bottomImageViewBox = {
745b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0.0f, bitmap.height() - 1.0f,
746b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<FLOAT>(bitmap.width()), 1.0f,
747b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
748b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
749b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
750b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
751b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
75274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
753b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //add tl, tr, bl, br
754b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[0] &&
755b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode == xy[1]) {
75674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
757b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoLockPixels alp(bitmap);
75874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
759b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor tlColor = bitmap.getColor(0,0);
760b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
761b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
76274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
763b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
764b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
765b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
76674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
767b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor brColor = bitmap.getColor(bitmap.width()-1,
768b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                    bitmap.height()-1);
769b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
770b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
77174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
772b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
773b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
774b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
775b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
77674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
777b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //create visual brush from canvas
778b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_RECT bound = {};
779b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[0] &&
780b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode == xy[1]) {
78174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
782b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.x = BIG_F / -2;
783b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.y = BIG_F / -2;
784b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.width = BIG_F;
785b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.height = BIG_F;
786b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else if (SkShader::kClamp_TileMode == xy[0]) {
787b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.x = BIG_F / -2;
788b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.y = 0.0f;
789b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.width = BIG_F;
790b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.height = static_cast<FLOAT>(bitmap.height());
791b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else if (SkShader::kClamp_TileMode == xy[1]) {
792b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.x = 0;
793b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.y = BIG_F / -2;
794b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.width = static_cast<FLOAT>(bitmap.width());
795b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.height = BIG_F;
796b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
797b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
798b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
799b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create visual brush for image brush.");
800b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
801b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set canvas on visual brush for image brush.");
802b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
803b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set tile mode on visual brush for image brush.");
804b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->SetOpacity(alpha / 255.0f),
805b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set opacity on visual brush for image brush.");
80674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
807b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
808b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
80974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
810b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
811b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
81249f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrixToUse.get()) {
813b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
814b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set transform for image brush.");
815b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
816b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO(bungeman): perspective bitmaps in general.
817b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
81874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
819b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
820b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
821b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
822b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
823b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                           const SkScalar offset,
824b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                           IXpsOMGradientStop** xpsGradStop) {
825b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_COLOR gradStopXpsColor = xps_color(skColor);
826b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
827b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              NULL,
828b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              SkScalarToFLOAT(offset),
829b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              xpsGradStop),
830b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create gradient stop.");
831b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
832b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
833b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
834b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
835b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkAlpha alpha,
836b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkMatrix& localMatrix,
837b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMMatrixTransform* xpsMatrix,
838b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMBrush** xpsBrush) {
839b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT startPoint;
840b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT endPoint;
84149f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
842b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        startPoint = xps_point(info.fPoint[0]);
843b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        endPoint = xps_point(info.fPoint[1]);
844b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
845b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        transform_offsets(info.fColorOffsets, info.fColorCount,
846b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                          info.fPoint[0], info.fPoint[1],
847b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                          localMatrix);
848b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        startPoint = xps_point(info.fPoint[0], localMatrix);
849b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        endPoint = xps_point(info.fPoint[1], localMatrix);
850b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
85174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
852b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
853b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[0],
854b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[0],
855b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop0));
85674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
857b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
858b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[1],
859b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[1],
860b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop1));
86174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
862b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
863b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
864b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     gradStop1.get(),
865b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &startPoint,
866b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &endPoint,
867b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &gradientBrush),
868b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create linear gradient brush.");
86949f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
870b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradientBrush->SetTransformLocal(xpsMatrix),
871b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set transform on linear gradient brush.");
872b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
87374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
874b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
875b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->GetGradientStops(&gradStopCollection),
876b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get linear gradient stop collection.");
877b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    for (int i = 2; i < info.fColorCount; ++i) {
878b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMGradientStop> gradStop;
879b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(createXpsGradientStop(info.fColors[i],
880b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 info.fColorOffsets[i],
881b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 &gradStop));
882b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradStopCollection->Append(gradStop.get()),
883b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add linear gradient stop.");
884b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
88574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
886b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
887b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set spread method of linear gradient.");
88874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
889b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetOpacity(alpha / 255.0f),
890b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set opacity of linear gradient brush.");
891b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
89274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
893b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
894b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
895b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
896b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
897b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkAlpha alpha,
898b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkMatrix& localMatrix,
899b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMMatrixTransform* xpsMatrix,
900b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMBrush** xpsBrush) {
901b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
902b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[0],
903b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[0],
904b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop0));
90574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
906b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
907b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[1],
908b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[1],
909b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop1));
91074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
911b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: figure out how to fake better if not affine
912b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT centerPoint;
913b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT gradientOrigin;
914b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_SIZE radiiSizes;
91549f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
916b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        centerPoint = xps_point(info.fPoint[0]);
917b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        gradientOrigin = xps_point(info.fPoint[0]);
918b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
919b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
920b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
921b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        centerPoint = xps_point(info.fPoint[0], localMatrix);
922b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        gradientOrigin = xps_point(info.fPoint[0], localMatrix);
92374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
924b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar radius = info.fRadius[0];
925b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkVector vec[2];
92674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
927b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        vec[0].set(radius, 0);
928b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        vec[1].set(0, radius);
929b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        localMatrix.mapVectors(vec, 2);
93074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
931b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar d0 = vec[0].length();
932b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar d1 = vec[1].length();
93374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
934b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.width = SkScalarToFLOAT(d0);
935b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.height = SkScalarToFLOAT(d1);
936b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
93774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
938b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
939b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
940b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     gradStop1.get(),
941b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &centerPoint,
942b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &gradientOrigin,
943b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &radiiSizes,
944b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &gradientBrush),
945b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create radial gradient brush.");
94649f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
947b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradientBrush->SetTransformLocal(xpsMatrix),
948b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set transform on radial gradient brush.");
949b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
95074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
951b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
952b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->GetGradientStops(&gradStopCollection),
953b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get radial gradient stop collection.");
954b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    for (int i = 2; i < info.fColorCount; ++i) {
955b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMGradientStop> gradStop;
956b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(createXpsGradientStop(info.fColors[i],
957b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 info.fColorOffsets[i],
958b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 &gradStop));
959b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradStopCollection->Append(gradStop.get()),
960b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add radial gradient stop.");
961b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
96274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
963b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
964b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set spread method of radial gradient.");
96574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
966b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetOpacity(alpha / 255.0f),
967b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set opacity of radial gradient brush.");
968b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
96974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
970b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
971b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
972b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
973b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
974b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    IXpsOMBrush** brush,
975b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    const SkMatrix* parentTransform) {
976b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkShader *shader = skPaint.getShader();
977b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (NULL == shader) {
978b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
979b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return S_OK;
980b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
98174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
982b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Gradient shaders.
983b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::GradientInfo info;
984b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    info.fColorCount = 0;
985b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    info.fColors = NULL;
986b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    info.fColorOffsets = NULL;
987b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::GradientType gradientType = shader->asAGradient(&info);
98874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
989b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (SkShader::kNone_GradientType == gradientType) {
990b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Nothing to see, move along.
99174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
992b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else if (SkShader::kColor_GradientType == gradientType) {
993b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkASSERT(1 == info.fColorCount);
994b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkColor color;
995b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        info.fColors = &color;
996b58a639b2fbe919489654bb506efdb024a308a8ebsalomon@google.com        shader->asAGradient(&info);
997b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkAlpha alpha = skPaint.getAlpha();
998b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsSolidColorBrush(color, alpha, brush));
999b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return S_OK;
100074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1001b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
1002b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (info.fColorCount == 0) {
1003b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor color = skPaint.getColor();
1004b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
1005b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1006b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
100774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1008b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkAutoTArray<SkColor> colors(info.fColorCount);
1009b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1010b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        info.fColors = colors.get();
1011b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        info.fColorOffsets = colorOffsets.get();
1012b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        shader->asAGradient(&info);
101374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1014b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (1 == info.fColorCount) {
1015b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkColor color = info.fColors[0];
1016b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAlpha alpha = skPaint.getAlpha();
1017b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsSolidColorBrush(color, alpha, brush));
1018b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1019b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
102074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1021f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix localMatrix = shader->getLocalMatrix();
102249f085dddff10473b6ebf832a974288300224e60bsalomon        if (parentTransform) {
1023b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            localMatrix.preConcat(*parentTransform);
1024b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1025b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1026b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
102774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1028b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kLinear_GradientType == gradientType) {
1029b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsLinearGradient(info,
1030b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             skPaint.getAlpha(),
1031b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             localMatrix,
1032b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             xpsMatrixToUse.get(),
1033b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             brush));
1034b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1035b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
103674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1037b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kRadial_GradientType == gradientType) {
1038b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsRadialGradient(info,
1039b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             skPaint.getAlpha(),
1040b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             localMatrix,
1041b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             xpsMatrixToUse.get(),
1042b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             brush));
1043b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1044b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
104574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
104683226976b532141b26ff3a40f381a5d08ce3259dreed@google.com        if (SkShader::kRadial2_GradientType == gradientType ||
104783226976b532141b26ff3a40f381a5d08ce3259dreed@google.com            SkShader::kConical_GradientType == gradientType) {
1048b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //simple if affine and one is 0, otherwise will have to fake
1049b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
105074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1051b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kSweep_GradientType == gradientType) {
1052b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //have to fake
1053b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1054b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
105574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1056b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkBitmap outTexture;
1057b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMatrix outMatrix;
1058b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::TileMode xy[2];
1059b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::BitmapType bitmapType = shader->asABitmap(&outTexture,
1060b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                        &outMatrix,
106191f319c5dc4493384f0a52aaeef3dcc311ef6ed0rileya@google.com                                                        xy);
1062b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    switch (bitmapType) {
1063b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkShader::kNone_BitmapType:
1064b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1065b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkShader::kDefault_BitmapType: {
1066b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //TODO: outMatrix??
1067f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            SkMatrix localMatrix = shader->getLocalMatrix();
106849f085dddff10473b6ebf832a974288300224e60bsalomon            if (parentTransform) {
1069b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                localMatrix.preConcat(*parentTransform);
1070b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
107174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1072b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1073b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsImageBrush(outTexture,
1074b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         localMatrix,
1075b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         xy,
1076b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         skPaint.getAlpha(),
1077b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         &tileBrush));
107874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1079b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
108074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1081b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1082b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1083b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkShader::kRadial_BitmapType:
1084b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkShader::kSweep_BitmapType:
1085b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkShader::kTwoPointRadial_BitmapType:
1086b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        default:
1087b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1088b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
108974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1090b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1091b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1092b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1093b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1094b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1095b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool zeroWidth = (0 == paint.getStrokeWidth());
1096b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
109774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1098b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return paint.getPathEffect() ||
1099b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           paint.getMaskFilter() ||
1100b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           paint.getRasterizer() ||
1101b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           (stroke && (
1102b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com               (matrix.hasPerspective() && !zeroWidth) ||
1103b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com               SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1104b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com               (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1105b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                paint.getStrokeMiter() < SK_ScalarSqrt2)
1106b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           ))
1107b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ;
1108b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1109b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1110b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1111b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMGeometryFigure** xpsRect) {
1112b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkPoint points[4] = {
1113b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fLeft, rect.fTop },
1114b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fRight, rect.fTop },
1115b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fRight, rect.fBottom },
1116b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fLeft, rect.fBottom },
1117b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
1118b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return this->createXpsQuad(points, stroke, fill, xpsRect);
1119b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1120b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1121b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   BOOL stroke, BOOL fill,
1122b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMGeometryFigure** xpsQuad) {
1123b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Define the start point.
1124b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT startPoint = xps_point(points[0]);
112574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1126b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Create the figure.
1127b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1128b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create quad geometry figure.");
112974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1130b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Define the type of each segment.
1131b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_SEGMENT_TYPE segmentTypes[3] = {
1132b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_SEGMENT_TYPE_LINE,
1133b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_SEGMENT_TYPE_LINE,
1134b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_SEGMENT_TYPE_LINE,
1135b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
113674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1137b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Define the x and y coordinates of each corner of the figure.
1138b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    FLOAT segmentData[6] = {
1139b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1140b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1141b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1142b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
114374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1144b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Describe if the segments are stroked.
1145b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL segmentStrokes[3] = {
1146b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        stroke, stroke, stroke,
1147b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
114874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1149b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Add the segment data to the figure.
1150b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*xpsQuad)->SetSegments(
1151b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            3, 6,
1152b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            segmentTypes , segmentData, segmentStrokes),
1153b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not add segment data to quad.");
115474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1155b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Set the closed and filled properties of the figure.
1156b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1157b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
115874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1159b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1160b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1161b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1162b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::clear(SkColor color) {
1163b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: override this for XPS
1164b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDEBUGF(("XPS clear not yet implemented."));
1165b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1166b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1167b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1168b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             size_t count, const SkPoint points[],
1169b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkPaint& paint) {
1170b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //This will call back into the device to do the drawing.
1171b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    d.drawPoints(mode, count, points, paint, true);
1172b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1173b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1174b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1175b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               int vertexCount, const SkPoint verts[],
1176b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkPoint texs[], const SkColor colors[],
1177b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               SkXfermode* xmode, const uint16_t indices[],
1178b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               int indexCount, const SkPaint& paint) {
1179b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: override this for XPS
1180b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDEBUGF(("XPS drawVertices not yet implemented."));
1181b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1182b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
11835dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.comvoid SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
1184b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
118574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1186b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //If trying to paint with a stroke, ignore that and fill.
11875dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
11885dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
11895dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    if (paint->getStyle() != SkPaint::kFill_Style) {
11905dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        paint.writable()->setStyle(SkPaint::kFill_Style);
1191b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
119274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1193b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->internalDrawRect(d, r, false, *fillPaint);
1194b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1195b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1196b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawRect(const SkDraw& d,
1197b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkRect& r,
1198b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkPaint& paint) {
1199b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->internalDrawRect(d, r, true, paint);
1200b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1201b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1202cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.comvoid SkXPSDevice::drawRRect(const SkDraw& d,
1203cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com                            const SkRRect& rr,
1204cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com                            const SkPaint& paint) {
1205cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com    SkPath path;
1206cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com    path.addRRect(rr);
1207cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com    this->drawPath(d, path, paint, NULL, true);
1208cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com}
1209cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com
1210b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::internalDrawRect(const SkDraw& d,
1211b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   const SkRect& r,
1212b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   bool transformRect,
1213b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   const SkPaint& paint) {
1214b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Exit early if there is nothing to draw.
1215b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (d.fClip->isEmpty() ||
1216b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
1217b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1218b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
121974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1220b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Path the rect if we can't optimize it.
1221b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (rect_must_be_pathed(paint, *d.fMatrix)) {
1222b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath tmp;
1223b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        tmp.addRect(r);
1224b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        tmp.setFillType(SkPath::kWinding_FillType);
1225b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->drawPath(d, tmp, paint, NULL, true);
1226b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1227b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
122874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1229b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded path.
1230b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> shadedPath;
1231b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1232b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded path for rect.");
123374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1234b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded geometry.
1235b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1236b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1237b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded geometry for rect.");
123874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1239b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the geometry to the shaded path.
1240b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1241b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set shaded geometry for rect.");
124274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1243b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Set the brushes.
1244b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL fill = FALSE;
1245b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL stroke = FALSE;
1246b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
124774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1248b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    bool xpsTransformsPath = true;
1249b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Transform the geometry.
1250b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (transformRect && xpsTransformsPath) {
1251b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1252b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1253b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (xpsTransform.get()) {
1254b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1255b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                 "Could not set transform for rect.");
1256b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
1257b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsTransformsPath = false;
1258b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1259b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
126074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1261b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the figure.
1262b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1263b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    {
1264b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPoint points[4] = {
1265b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fLeft, r.fTop },
1266b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fLeft, r.fBottom },
1267b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fRight, r.fBottom },
1268b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fRight, r.fTop },
1269b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        };
1270b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (!xpsTransformsPath && transformRect) {
1271b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1272b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1273b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1274b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
127574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1276b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figures of the shaded geometry.
1277b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1278b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->GetFigures(&shadedFigures),
1279b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get shaded figures for rect.");
128074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1281b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the figure to the shaded geometry figures.
1282b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedFigures->Append(rectFigure.get()),
1283b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add shaded figure for rect.");
128474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1285b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->clip(shadedPath.get(), d));
128674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1287b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the shaded path to the current visuals.
1288b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1289b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1290b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for rect.");
1291b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(shadedPath.get()),
1292b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add rect to current visuals.");
1293b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1294b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1295b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1296b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            const SkTDArray<BOOL>& segmentStrokes,
1297b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            const SkTDArray<FLOAT>& segmentData,
1298b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            BOOL stroke, BOOL fill,
1299b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            IXpsOMGeometryFigure* figure,
1300b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            IXpsOMGeometryFigureCollection* figures) {
1301b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Add the segment data to the figure.
1302b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1303b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            segmentTypes.begin() , segmentData.begin(),
1304b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            segmentStrokes.begin()),
1305b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set path segments.");
130674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1307b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Set the closed and filled properties of the figure.
1308b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1309b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figure->SetIsFilled(fill), "Could not set path fill.");
131074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1311b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Add the figure created above to this geometry.
1312b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figures->Append(figure), "Could not add path to geometry.");
1313b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1314b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1315b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1316b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::addXpsPathGeometry(
1317b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        IXpsOMGeometryFigureCollection* xpsFigures,
1318b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        BOOL stroke, BOOL fill, const SkPath& path) {
1319b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1320b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<BOOL> segmentStrokes;
1321b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<FLOAT> segmentData;
132274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1323b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1324b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath::Iter iter(path, true);
1325b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint points[4];
1326b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath::Verb verb;
1327b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1328b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        switch (verb) {
1329b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kMove_Verb: {
133049f085dddff10473b6ebf832a974288300224e60bsalomon                if (xpsFigure.get()) {
1331b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1332b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    stroke, fill,
1333b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    xpsFigure.get() , xpsFigures));
1334b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    xpsFigure.reset();
1335b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    segmentTypes.rewind();
1336b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    segmentStrokes.rewind();
1337b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    segmentData.rewind();
1338b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                }
1339b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // Define the start point.
1340b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                XPS_POINT startPoint = xps_point(points[0]);
1341b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // Create the figure.
1342b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1343b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                            &xpsFigure),
1344b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    "Could not create path geometry figure.");
1345b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1346b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1347b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kLine_Verb:
1348b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                if (iter.isCloseLine()) break; //ignore the line, auto-closed
1349b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1350b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentStrokes.push(stroke);
1351b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fX));
1352b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fY));
1353b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1354b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kQuad_Verb:
1355b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1356b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentStrokes.push(stroke);
1357b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fX));
1358b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fY));
1359b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fX));
1360b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fY));
1361b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1362b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kCubic_Verb:
1363b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1364b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentStrokes.push(stroke);
1365b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fX));
1366b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fY));
1367b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fX));
1368b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fY));
1369b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[3].fX));
1370b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[3].fY));
1371b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1372b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kClose_Verb:
1373b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // we ignore these, and just get the whole segment from
1374b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // the corresponding line/quad/cubic verbs
1375b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1376b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            default:
1377330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com                SkDEBUGFAIL("unexpected verb");
1378b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1379b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1380b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
138149f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsFigure.get()) {
1382b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1383b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        stroke, fill,
1384b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        xpsFigure.get(), xpsFigures));
1385b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1386b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1387b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1388b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1389b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1390b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               SkMatrix* matrix,
1391b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               SkVector* ppuScale,
1392b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkIRect& clip, SkIRect* clipIRect) {
1393b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //This action is in unit space, but the ppm is specified in physical space.
1394b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ppuScale->fX = SkScalarDiv(this->fCurrentPixelsPerMeter.fX,
1395b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               this->fCurrentUnitsPerMeter.fX);
1396b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ppuScale->fY = SkScalarDiv(this->fCurrentPixelsPerMeter.fY,
1397b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               this->fCurrentUnitsPerMeter.fY);
139874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1399b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    matrix->postScale(ppuScale->fX, ppuScale->fY);
140074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1401b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkIRect& irect = clip;
1402b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkRect clipRect = SkRect::MakeLTRB(
1403b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fLeft), ppuScale->fX),
1404b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fTop), ppuScale->fY),
1405b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fRight), ppuScale->fX),
1406b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fBottom), ppuScale->fY));
1407b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    clipRect.roundOut(clipIRect);
1408b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1409b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1410b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::applyMask(const SkDraw& d,
1411b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkMask& mask,
1412b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkVector& ppuScale,
1413b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               IXpsOMPath* shadedPath) {
1414b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the geometry object.
1415b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1416b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedPath->GetGeometry(&shadedGeometry),
1417b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get mask shaded geometry.");
141874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1419b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figures from the geometry.
1420b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1421b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedGeometry->GetFigures(&shadedFigures),
1422b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get mask shaded figures.");
142374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1424b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMatrix m;
1425b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    m.reset();
1426b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1427b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                   SkIntToScalar(mask.fBounds.fTop));
1428b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
142974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1430b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::TileMode xy[2];
1431b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xy[0] = (SkShader::TileMode)3;
1432b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xy[1] = (SkShader::TileMode)3;
143374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1434b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkBitmap bm;
1435a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org    bm.installMaskPixels(mask);
143674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1437b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1438b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1439b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1440b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set mask.");
144174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1442b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1443b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1444b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1445b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1446b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create mask shaded figure.");
1447b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedFigures->Append(shadedFigure.get()),
1448b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not add mask shaded figure.");
144974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1450b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->clip(shadedPath, d));
145174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1452b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the path to the active visual collection.
1453b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1454b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1455b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get mask current visuals.");
1456b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(currentVisuals->Append(shadedPath),
1457b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not add masked shaded path to current visuals.");
145874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1459b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1460b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1461b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1462b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1463b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkPaint& shaderPaint,
1464b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkMatrix& matrix,
1465b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               BOOL* fill, BOOL* stroke) {
1466b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    *fill = FALSE;
1467b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    *stroke = FALSE;
146874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1469b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkPaint::Style style = shaderPaint.getStyle();
1470b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool hasFill = SkPaint::kFill_Style == style
1471b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      || SkPaint::kStrokeAndFill_Style == style;
1472b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool hasStroke = SkPaint::kStroke_Style == style
1473b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        || SkPaint::kStrokeAndFill_Style == style;
147474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1475b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO(bungeman): use dictionaries and lookups.
1476b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (hasFill) {
1477b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        *fill = TRUE;
1478b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMBrush> fillBrush;
1479b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1480b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1481b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set fill for shaded path.");
1482b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
148374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1484b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (hasStroke) {
1485b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        *stroke = TRUE;
1486b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1487b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1488b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1489b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set stroke brush for shaded path.");
1490b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(shadedPath->SetStrokeThickness(
1491b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1492b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set shaded path stroke thickness.");
149374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1494b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (0 == shaderPaint.getStrokeWidth()) {
1495b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //XPS hair width is a hack. (XPS Spec 11.6.12).
1496b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkTScopedComPtr<IXpsOMDashCollection> dashes;
1497b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(shadedPath->GetStrokeDashes(&dashes),
1498b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                "Could not set dashes for shaded path.");
1499b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_DASH dash;
1500b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            dash.length = 1.0;
1501b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            dash.gap = 0.0;
1502b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1503b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(shadedPath->SetStrokeDashOffset(-2.0),
1504b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                "Could not set dash offset for shaded path.");
1505b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1506b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1507b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1508b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1509b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1510b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawPath(const SkDraw& d,
1511b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkPath& platonicPath,
15125dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                           const SkPaint& origPaint,
1513b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkMatrix* prePathMatrix,
1514b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           bool pathIsMutable) {
15155dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
15165dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com
1517b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // nothing to draw
1518b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (d.fClip->isEmpty() ||
15195dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
1520b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1521b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
152274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1523b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath modifiedPath;
15245dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    const bool paintHasPathEffect = paint->getPathEffect()
15255dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                                 || paint->getStyle() != SkPaint::kFill_Style;
152674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1527b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1528b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMatrix matrix = *d.fMatrix;
1529b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1530b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (prePathMatrix) {
15315dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        if (paintHasPathEffect || paint->getRasterizer()) {
1532b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (!pathIsMutable) {
1533b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                skeletalPath = &modifiedPath;
1534b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                pathIsMutable = true;
1535b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1536b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            platonicPath.transform(*prePathMatrix, skeletalPath);
1537b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
153892362383a4de7b0d819c88fa8b74242bb2507602commit-bot@chromium.org            matrix.preConcat(*prePathMatrix);
1539b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1540b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
154174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1542b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Apply path effect [Skeletal-path -> Fillable-path].
1543b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath* fillablePath = skeletalPath;
1544b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (paintHasPathEffect) {
1545b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (!pathIsMutable) {
1546b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            fillablePath = &modifiedPath;
1547b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            pathIsMutable = true;
1548b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
15495dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        bool fill = paint->getFillPath(*skeletalPath, fillablePath);
155074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
15515dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        SkPaint* writablePaint = paint.writable();
15525dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        writablePaint->setPathEffect(NULL);
1553b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (fill) {
15545dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            writablePaint->setStyle(SkPaint::kFill_Style);
1555b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
15565dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            writablePaint->setStyle(SkPaint::kStroke_Style);
15575dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            writablePaint->setStrokeWidth(0);
1558b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1559b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
156074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1561b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded path. This will be the path which is painted.
1562b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> shadedPath;
1563b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1564b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded path for path.");
156574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1566b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the geometry for the shaded path.
1567b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1568b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1569b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded geometry for path.");
157074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1571b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the geometry to the shaded path.
1572b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1573b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add the shaded geometry to shaded path.");
157474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
15755dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkRasterizer* rasterizer = paint->getRasterizer();
15765dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkMaskFilter* filter = paint->getMaskFilter();
1577d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com
1578d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    //Determine if we will draw or shade and mask.
1579d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    if (rasterizer || filter) {
15805dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        if (paint->getStyle() != SkPaint::kFill_Style) {
15815dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            paint.writable()->setStyle(SkPaint::kFill_Style);
1582d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com        }
1583d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    }
1584d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com
1585b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Set the brushes.
1586b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL fill;
1587b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL stroke;
1588b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->shadePath(shadedPath.get(),
15895dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                        *paint,
1590b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        *d.fMatrix,
1591b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &fill,
1592b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &stroke));
159374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1594b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Rasterizer
1595d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    if (rasterizer) {
1596b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkIRect clipIRect;
1597b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkVector ppuScale;
1598b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->convertToPpm(filter,
1599b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &matrix,
1600b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &ppuScale,
1601b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           d.fClip->getBounds(),
1602b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &clipIRect);
160374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1604b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask* mask = NULL;
160574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1606b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Fillable-path -> Mask]
1607b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask rasteredMask;
1608d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com        if (rasterizer->rasterize(
1609b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                *fillablePath,
1610b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                matrix,
1611b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &clipIRect,
1612b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filter,  //just to compute how much to draw.
1613b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &rasteredMask,
1614b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
161574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1616b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1617b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            mask = &rasteredMask;
161874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1619b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //[Mask -> Mask]
1620b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkMask filteredMask;
1621b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (filter &&
1622b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filter->filterMask(&filteredMask, *mask, *d.fMatrix, NULL)) {
162374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1624b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                mask = &filteredMask;
1625b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            } else {
1626b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filteredMask.fImage = NULL;
1627b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1628b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
162974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1630b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //Draw mask.
1631b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1632b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1633b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1634b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
163574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1636b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Mask filter
1637b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (filter) {
1638b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkIRect clipIRect;
1639b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkVector ppuScale;
1640b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->convertToPpm(filter,
1641b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &matrix,
1642b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &ppuScale,
1643b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           d.fClip->getBounds(),
1644b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &clipIRect);
164574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1646b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Fillable-path -> Pixel-path]
1647b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1648b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        fillablePath->transform(matrix, pixelPath);
164974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1650b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask* mask = NULL;
165174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1652b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Pixel-path -> Mask]
1653b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask rasteredMask;
1654b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkDraw::DrawToMask(
1655b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        *pixelPath,
1656b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &clipIRect,
1657b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        filter,  //just to compute how much to draw.
1658b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &matrix,
1659b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &rasteredMask,
16602ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkMask::kComputeBoundsAndRenderImage_CreateMode,
16615dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                        paint->getStyle())) {
166274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1663b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1664b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            mask = &rasteredMask;
166574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1666b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //[Mask -> Mask]
1667b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkMask filteredMask;
1668b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (filter->filterMask(&filteredMask,
1669b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   rasteredMask,
1670b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   matrix,
1671b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   NULL)) {
1672b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                mask = &filteredMask;
1673b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            } else {
1674b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filteredMask.fImage = NULL;
1675b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1676b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
167774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1678b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //Draw mask.
1679b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1680b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1681b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1682b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
168374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1684b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figures from the shaded geometry.
1685b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1686b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->GetFigures(&shadedFigures),
1687b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get shaded figures for shaded path.");
168874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1689b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    bool xpsTransformsPath = true;
169074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1691b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Set the fill rule.
169276db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman    SkPath* xpsCompatiblePath = fillablePath;
1693b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_FILL_RULE xpsFillRule;
169476db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman    switch (fillablePath->getFillType()) {
1695b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kWinding_FillType:
1696b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsFillRule = XPS_FILL_RULE_NONZERO;
1697b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1698b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kEvenOdd_FillType:
1699b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsFillRule = XPS_FILL_RULE_EVENODD;
1700b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1701b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kInverseWinding_FillType: {
170276db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
170376db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            if (!pathIsMutable) {
170476db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                xpsCompatiblePath = &modifiedPath;
170576db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                pathIsMutable = true;
170676db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            }
170776db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            if (!Simplify(*fillablePath, xpsCompatiblePath)) {
170876db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                SkDEBUGF(("Could not simplify inverse winding path."));
170976db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                return;
171076db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            }
1711b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
171276db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman        // The xpsCompatiblePath is noW inverse even odd, so fall through.
1713b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kInverseEvenOdd_FillType: {
1714b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect universe = SkRect::MakeLTRB(
1715b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0, 0,
1716b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                this->fCurrentCanvasSize.fWidth,
1717b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                this->fCurrentCanvasSize.fHeight);
1718b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1719b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1720b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRVM(shadedFigures->Append(addOneFigure.get()),
1721b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                 "Could not add even-odd flip figure to shaded path.");
1722b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsTransformsPath = false;
1723b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsFillRule = XPS_FILL_RULE_EVENODD;
1724b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1725b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1726b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        default:
1727330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com            SkDEBUGFAIL("Unknown SkPath::FillType.");
1728b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1729b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1730b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set fill rule for shaded path.");
173174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1732b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the XPS transform, if possible.
1733b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (xpsTransformsPath) {
1734b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1735b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsTransform(matrix, &xpsTransform));
173674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1737b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (xpsTransform.get()) {
1738b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1739b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                 "Could not set transform on shaded path.");
1740b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
1741b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsTransformsPath = false;
1742b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1743b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
174474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
174576db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman    SkPath* devicePath = xpsCompatiblePath;
1746b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!xpsTransformsPath) {
1747b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Fillable-path -> Device-path]
174876db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman        devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
174976db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman        xpsCompatiblePath->transform(matrix, devicePath);
1750b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1751b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->addXpsPathGeometry(shadedFigures.get(),
1752b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 stroke, fill, *devicePath));
175374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1754b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->clip(shadedPath.get(), d));
175574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1756b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the path to the active visual collection.
1757b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1758b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1759b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for shaded path.");
1760b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(shadedPath.get()),
1761b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add shaded path to current visuals.");
1762b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1763b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1764b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1765b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath clipPath;
1766b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkAssertResult(d.fClip->getBoundaryPath(&clipPath));
176774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1768b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1769b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1770b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1771b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                const SkPath& clipPath,
1772b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                XPS_FILL_RULE fillRule) {
1773b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the geometry.
1774b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1775b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1776b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create clip geometry.");
177774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1778b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figure collection of the geometry.
1779b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1780b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(clipGeometry->GetFigures(&clipFigures),
1781b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get the clip figures.");
178274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1783b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the figures into the geometry.
1784b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->addXpsPathGeometry(
1785b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        clipFigures.get(),
1786b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        FALSE, TRUE, clipPath));
178774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1788b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(clipGeometry->SetFillRule(fillRule),
1789b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set fill rule.");
1790b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1791b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set clip geometry.");
179274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1793b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1794b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1795b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1796b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
1797b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkMatrix& matrix, const SkPaint& paint) {
1798b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (d.fClip->isEmpty()) {
1799b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1800b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
180174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1802b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkIRect srcRect;
18039bf380ce7f848dfb5886dd52b82746521454b739robertphillips@google.com    srcRect.set(0, 0, bitmap.width(), bitmap.height());
180474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1805b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the new shaded path.
1806b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> shadedPath;
1807b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1808b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create path for bitmap.");
180974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1810b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded geometry.
1811b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1812b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1813b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create geometry for bitmap.");
181474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1815b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the shaded geometry to the shaded path.
1816b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1817b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set the geometry for bitmap.");
181874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1819b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the shaded figures from the shaded geometry.
1820b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1821b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->GetFigures(&shadedFigures),
1822b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get the figures for bitmap.");
182374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1824b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMatrix transform = matrix;
1825b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    transform.postConcat(*d.fMatrix);
182674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1827b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1828b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->createXpsTransform(transform, &xpsTransform));
1829b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (xpsTransform.get()) {
1830b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1831b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             "Could not set transform for bitmap.");
1832b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
1833b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: perspective that bitmap!
1834b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
183574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1836b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
183749f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsTransform.get()) {
1838b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkShader::TileMode xy[2] = {
1839b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode,
1840b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode,
1841b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        };
1842b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
18439bf380ce7f848dfb5886dd52b82746521454b739robertphillips@google.com        HRV(this->createXpsImageBrush(bitmap,
1844b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      transform,
1845b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      xy,
1846b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      paint.getAlpha(),
1847b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      &xpsImageBrush));
1848b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
1849b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             "Could not set bitmap brush.");
185074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1851b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
1852b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1853b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
1854b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1855b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedFigures->Append(rectFigure.get()),
1856b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add bitmap figure.");
185774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1858b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the current visual collection and add the shaded path to it.
1859b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1860b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1861b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for bitmap");
1862b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(shadedPath.get()),
1863b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add bitmap to current visuals.");
186474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1865b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->clip(shadedPath.get(), d));
1866b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1867b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1868b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
1869b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             int x, int y,
1870b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkPaint& paint) {
1871b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: override this for XPS
1872b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDEBUGF(("XPS drawSprite not yet implemented."));
1873b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1874b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1875b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
1876b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       TypefaceUse** typefaceUse) {
1877398de9ad9cb1ed3aa97aaf023d7f8287b8e62601reed@google.com    SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
187874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1879b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Check cache.
1880398de9ad9cb1ed3aa97aaf023d7f8287b8e62601reed@google.com    const SkFontID typefaceID = typeface->uniqueID();
1881b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!this->fTypefaces.empty()) {
1882b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        TypefaceUse* current = &this->fTypefaces.front();
1883b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const TypefaceUse* last = &this->fTypefaces.back();
1884b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        for (; current <= last; ++current) {
1885b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (current->typefaceId == typefaceID) {
1886b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                *typefaceUse = current;
1887b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                return S_OK;
1888b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1889b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1890b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
189174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1892b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: create glyph only fonts
1893b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //and let the host deal with what kind of font we're looking at.
1894b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
189574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1896b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IStream> fontStream;
1897b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    int ttcIndex;
1898b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    SkStream* fontData = typeface->openStream(&ttcIndex);
1899635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com    //TODO: cannot handle FON fonts.
19008cddc8d0ea02b3476fd57668cf9b25ba5b9770d8bungeman@google.com    HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
1901b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create font stream.");
190274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1903b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const size_t size =
1904b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1905b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
1906b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t id[GUID_ID_LEN];
1907da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    HR(this->createId(id, GUID_ID_LEN));
1908b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
190974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1910b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
1911b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1912b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create font resource part uri.");
191374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1914b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1915b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1916b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              embedding,
1917b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              partUri.get(),
1918b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              FALSE,
1919b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              &xpsFontResource),
1920b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create font resource.");
192174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1922b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    //TODO: change openStream to return -1 for non-ttc, get rid of this.
1923b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1924b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    bool isTTC = (data &&
1925b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                  fontData->getLength() >= sizeof(SkTTCFHeader) &&
1926b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                  ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1927b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
1928b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
1929b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.typefaceId = typefaceID;
1930b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
1931b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.fontData = fontData;
1932b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.xpsFont = xpsFontResource.release();
193374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1934cf4d0fb8bb5628c655ee97ce098d2fd3fd2bb448mtklein@google.com    SkAutoGlyphCache agc(paint, NULL, &SkMatrix::I());
1935b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkGlyphCache* glyphCache = agc.getCache();
1936b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    unsigned int glyphCount = glyphCache->getGlyphCount();
1937b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
193874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1939b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    *typefaceUse = &newTypefaceUse;
1940b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1941b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1942b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1943b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
1944b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               IXpsOMObjectFactory* xpsFactory,
1945b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               IXpsOMCanvas* canvas,
1946b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                               TypefaceUse* font,
1947b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               LPCWSTR text,
1948b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               XPS_GLYPH_INDEX* xpsGlyphs,
1949b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               UINT32 xpsGlyphsLen,
1950b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               XPS_POINT *origin,
1951b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               FLOAT fontSize,
1952b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               XPS_STYLE_SIMULATION sims,
1953b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkMatrix& transform,
1954b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkPaint& paint) {
1955b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGlyphs> glyphs;
1956b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
1957b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
195874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1959b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //XPS uses affine transformations for everything...
1960b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //...except positioning text.
1961b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    bool useCanvasForClip;
1962b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
1963b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        origin->x += SkScalarToFLOAT(transform.getTranslateX());
1964b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        origin->y += SkScalarToFLOAT(transform.getTranslateY());
1965b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        useCanvasForClip = false;
1966b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
1967b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1968b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1969b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (xpsMatrixToUse.get()) {
1970b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1971b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                "Could not set transform matrix.");
1972b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            useCanvasForClip = true;
1973b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
1974330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com            SkDEBUGFAIL("Attempt to add glyphs in perspective.");
1975b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            useCanvasForClip = false;
1976b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1977b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
197874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1979b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1980b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
198174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
198249f085dddff10473b6ebf832a974288300224e60bsalomon    if (text) {
1983b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphsEditor->SetUnicodeString(text),
1984b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set unicode string.");
1985b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
198674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
198749f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsGlyphs) {
1988b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
1989b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set glyphs.");
1990b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
199174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1992b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
199374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1994b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
1995b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsBrush(
1996b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            paint,
1997b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            &xpsFillBrush,
1998b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            useCanvasForClip ? NULL : &transform));
199974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2000b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
2001b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set fill brush.");
200274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2003b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
200474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2005b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetFontRenderingEmSize(fontSize),
2006b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set font size.");
200774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2008b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetStyleSimulations(sims),
2009b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set style simulations.");
201074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2011b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2012b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
201374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2014b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!useCanvasForClip) {
2015b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->clip(glyphs.get(), d));
2016b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2017b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
2018b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2019b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2020b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create glyph canvas.");
202174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2022b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2023b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2024b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not get glyph visuals collection.");
202574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2026b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphCanvasVisuals->Append(glyphs.get()),
2027b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add glyphs to page.");
2028b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->clip(glyphCanvas.get(), d));
202974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2030b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(visuals->Append(glyphCanvas.get()),
2031b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add glyph canvas to page.");
2032b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
203374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2034b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
2035b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2036b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2037b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstruct SkXPSDrawProcs : public SkDrawProcs {
2038b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.compublic:
2039b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    /** [in] Advance width and offsets for glyphs measured in
2040b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    hundredths of the font em size (XPS Spec 5.1.3). */
2041b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    FLOAT centemPerUnit;
2042b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    /** [in,out] The accumulated glyphs used in the current typeface. */
2043b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkBitSet* glyphUse;
2044b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    /** [out] The glyphs to draw. */
2045b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<XPS_GLYPH_INDEX> xpsGlyphs;
2046b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com};
2047b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2048b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic void xps_draw_1_glyph(const SkDraw1Glyph& state,
2049b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             SkFixed x, SkFixed y,
2050b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkGlyph& skGlyph) {
2051b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkASSERT(skGlyph.fWidth > 0 && skGlyph.fHeight > 0);
205274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2053b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDrawProcs* procs = static_cast<SkXPSDrawProcs*>(state.fDraw->fProcs);
20542211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com
20552211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    //Draw pre-adds half the sampling frequency for floor rounding.
20569447103029273a9f8dd7f5997e8af7a1e3ee7488bungeman@google.com    x -= state.fHalfSampleX;
20579447103029273a9f8dd7f5997e8af7a1e3ee7488bungeman@google.com    y -= state.fHalfSampleY;
205874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2059b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_GLYPH_INDEX* xpsGlyph = procs->xpsGlyphs.append();
2060b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    uint16_t glyphID = skGlyph.getGlyphID();
2061b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs->glyphUse->setBit(glyphID, true);
2062b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsGlyph->index = glyphID;
2063b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (1 == procs->xpsGlyphs.count()) {
2064b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->advanceWidth = 0.0f;
2065b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->horizontalOffset = SkFixedToFloat(x) * procs->centemPerUnit;
2066b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->verticalOffset = SkFixedToFloat(y) * -procs->centemPerUnit;
2067b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
2068b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const XPS_GLYPH_INDEX& first = procs->xpsGlyphs[0];
2069b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->advanceWidth = 0.0f;
2070b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->horizontalOffset = (SkFixedToFloat(x) * procs->centemPerUnit)
2071b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                     - first.horizontalOffset;
2072b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->verticalOffset = (SkFixedToFloat(y) * -procs->centemPerUnit)
2073b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   - first.verticalOffset;
2074b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
2075b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2076b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2077b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic void text_draw_init(const SkPaint& paint,
2078b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const void* text, size_t byteLength,
2079b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           SkBitSet& glyphsUsed,
2080b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           SkDraw& myDraw, SkXPSDrawProcs& procs) {
2081b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.fD1GProc = xps_draw_1_glyph;
20824b18f5767ab1df7fedf0ff2e3a9eac7f7c500c36bungeman@google.com    size_t numGlyphGuess;
2083b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    switch (paint.getTextEncoding()) {
2084b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPaint::kUTF8_TextEncoding:
2085b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            numGlyphGuess = SkUTF8_CountUnichars(
2086b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<const char *>(text),
2087b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                byteLength);
2088b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
2089b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPaint::kUTF16_TextEncoding:
2090b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            numGlyphGuess = SkUTF16_CountUnichars(
2091b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<const uint16_t *>(text),
2092b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                byteLength);
2093b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
2094b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPaint::kGlyphID_TextEncoding:
2095b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            numGlyphGuess = byteLength / 2;
2096b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
2097b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        default:
209845d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org            SK_ALWAYSBREAK(true);
2099b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
2100b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs.setReserve(numGlyphGuess);
2101b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.glyphUse = &glyphsUsed;
2102b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
210374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2104b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    myDraw.fProcs = &procs;
2105b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2106b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2107b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2108b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkPaint::Style style = paint.getStyle();
2109b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return matrix.hasPerspective()
2110b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || SkPaint::kStroke_Style == style
2111b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || SkPaint::kStrokeAndFill_Style == style
2112b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || paint.getMaskFilter()
2113b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || paint.getRasterizer()
2114b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ;
2115b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2116b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2117b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawText(const SkDraw& d,
2118b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const void* text, size_t byteLen,
2119b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           SkScalar x, SkScalar y,
2120b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkPaint& paint) {
2121b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (byteLen < 1) return;
212274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2123b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (text_must_be_pathed(paint, *d.fMatrix)) {
2124b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath path;
2125b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        paint.getTextPath(text, byteLen, x, y, &path);
2126b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->drawPath(d, path, paint, NULL, true);
2127b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: add automation "text"
2128b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
2129b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
213074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2131b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    TypefaceUse* typeface;
2132b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(CreateTypefaceUse(paint, &typeface));
213374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2134b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDraw myDraw(d);
2135635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com    myDraw.fMatrix = &SkMatrix::I();
2136b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDrawProcs procs;
2137b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    text_draw_init(paint, text, byteLen, *typeface->glyphsUsed, myDraw, procs);
213874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2139b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    myDraw.drawText(static_cast<const char*>(text), byteLen, x, y, paint);
2140089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com
2141089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    // SkDraw may have clipped out the glyphs, so we need to check
2142089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    if (procs.xpsGlyphs.count() == 0) {
2143089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com        return;
2144089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    }
2145089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com
2146b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT origin = {
2147b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        procs.xpsGlyphs[0].horizontalOffset / procs.centemPerUnit,
2148b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        procs.xpsGlyphs[0].verticalOffset / -procs.centemPerUnit,
2149b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
2150b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs[0].horizontalOffset = 0.0f;
2151b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs[0].verticalOffset = 0.0f;
215274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2153b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(AddGlyphs(d,
2154b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  this->fXpsFactory.get(),
2155b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  this->fCurrentXpsCanvas.get(),
2156b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                  typeface,
2157b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  NULL,
2158b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  procs.xpsGlyphs.begin(), procs.xpsGlyphs.count(),
2159b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  &origin,
2160b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  SkScalarToFLOAT(paint.getTextSize()),
2161b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  XPS_STYLE_SIMULATION_NONE,
2162b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  *d.fMatrix,
2163b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  paint));
2164b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2165b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2166b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawPosText(const SkDraw& d,
2167b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const void* text, size_t byteLen,
2168b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const SkScalar pos[],
2169b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              SkScalar constY, int scalarsPerPos,
2170b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const SkPaint& paint) {
2171b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (byteLen < 1) return;
217274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2173b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (text_must_be_pathed(paint, *d.fMatrix)) {
2174b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath path;
2175b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: make this work, Draw currently does not handle as well.
2176b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //paint.getTextPath(text, byteLength, x, y, &path);
2177b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //this->drawPath(d, path, paint, NULL, true);
2178b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: add automation "text"
2179b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
2180b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
218174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2182b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    TypefaceUse* typeface;
2183b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(CreateTypefaceUse(paint, &typeface));
218474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2185b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDraw myDraw(d);
2186635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com    myDraw.fMatrix = &SkMatrix::I();
2187b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDrawProcs procs;
2188b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    text_draw_init(paint, text, byteLen, *typeface->glyphsUsed, myDraw, procs);
218974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2190b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    myDraw.drawPosText(static_cast<const char*>(text), byteLen,
2191b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       pos, constY, scalarsPerPos,
2192b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       paint);
219374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2194089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    // SkDraw may have clipped out the glyphs, so we need to check
2195089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    if (procs.xpsGlyphs.count() == 0) {
2196089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com        return;
2197089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    }
2198089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com
2199b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT origin = {
2200b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        procs.xpsGlyphs[0].horizontalOffset / procs.centemPerUnit,
2201b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        procs.xpsGlyphs[0].verticalOffset / -procs.centemPerUnit,
2202b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
2203b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs[0].horizontalOffset = 0.0f;
2204b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs[0].verticalOffset = 0.0f;
220574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2206b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(AddGlyphs(d,
2207b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  this->fXpsFactory.get(),
2208b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  this->fCurrentXpsCanvas.get(),
2209b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                  typeface,
2210b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  NULL,
2211b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  procs.xpsGlyphs.begin(), procs.xpsGlyphs.count(),
2212b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  &origin,
2213b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  SkScalarToFLOAT(paint.getTextSize()),
2214b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  XPS_STYLE_SIMULATION_NONE,
2215b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  *d.fMatrix,
2216b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  paint));
2217b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2218b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2219b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawTextOnPath(const SkDraw& d, const void* text, size_t len,
2220b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 const SkPath& path, const SkMatrix* matrix,
2221b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 const SkPaint& paint) {
2222b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //This will call back into the device to do the drawing.
2223b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com     d.drawTextOnPath((const char*)text, len, path, matrix, paint);
2224b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2225b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
22261f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.comvoid SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
2227b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             int x, int y,
2228b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkPaint&) {
2229b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
223074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2231b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2232b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_MATRIX rawTransform = {
2233b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        1.0f,
2234b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0.0f,
2235b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0.0f,
2236b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        1.0f,
2237b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        static_cast<FLOAT>(x),
2238b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        static_cast<FLOAT>(y),
2239b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
2240b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2241b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create layer transform.");
2242b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2243b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set layer transform.");
224474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2245b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the current visual collection and add the layer to it.
2246b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2247b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2248b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for layer.");
2249b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2250b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add layer to current visuals.");
2251b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2252b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
225315a140599942f70e47380e3f700a825c7cece3b4commit-bot@chromium.orgSkBaseDevice* SkXPSDevice::onCreateDevice(const SkImageInfo&, Usage) {
2254635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com//Conditional for bug compatibility with PDF device.
2255635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com#if 0
22561f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    if (SkBaseDevice::kGeneral_Usage == usage) {
2257b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com        return NULL;
2258b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_CRASH();
2259b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //To what stream do we write?
2260b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //SkXPSDevice* dev = new SkXPSDevice(this);
2261b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //SkSize s = SkSize::Make(width, height);
2262b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //dev->BeginCanvas(s, s, SkMatrix::I());
2263b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //return dev;
2264b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
2265635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com#endif
2266b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return new SkXPSDevice(this->fXpsFactory.get());
2267b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2268b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2269b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::SkXPSDevice(IXpsOMObjectFactory* xpsFactory)
22701f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    : SkBitmapDevice(make_fake_bitmap(10000, 10000))
2271b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , fCurrentPage(0) {
227274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2273b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(CoCreateInstance(
2274b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSID_XpsOMObjectFactory,
2275b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             NULL,
2276b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSCTX_INPROC_SERVER,
2277b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             IID_PPV_ARGS(&this->fXpsFactory)),
2278b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create factory for layer.");
227974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2280b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
2281b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create canvas for layer.");
2282b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2283b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com
2284e629c7bc9f822351c39be99444839481083ee17fsenorblanco@chromium.orgbool SkXPSDevice::allowImageFilter(const SkImageFilter*) {
2285b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com    return false;
2286b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com}
2287