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
847ef4d5d934bba86848aa238efab21f54a160c1ahalcanary#include "SkTypes.h"
947ef4d5d934bba86848aa238efab21f54a160c1ahalcanary
10b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#ifndef UNICODE
11b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define UNICODE
12b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#endif
13b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#ifndef _UNICODE
14b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define _UNICODE
15b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#endif
16b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <ObjBase.h>
17b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <XpsObjectModel.h>
18b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <T2EmbApi.h>
19b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include <FontSub.h>
20b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
21b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkColor.h"
22b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkConstexprMath.h"
23b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkData.h"
24b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkDraw.h"
25b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkDrawProcs.h"
26b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org#include "SkEndian.h"
2747ef4d5d934bba86848aa238efab21f54a160c1ahalcanary#include "SkGeometry.h"
28b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkGlyphCache.h"
29b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkHRESULT.h"
30b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkImageEncoder.h"
31b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkIStream.h"
32b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkMaskFilter.h"
33b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkPaint.h"
34da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#include "SkPathOps.h"
35b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkPoint.h"
36b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkRasterizer.h"
37b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org#include "SkSFNTHeader.h"
38b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkShader.h"
39b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkSize.h"
40b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkStream.h"
41b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkTDArray.h"
42b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkTLazy.h"
43b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkTScopedComPtr.h"
44b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org#include "SkTTCFHeader.h"
45398de9ad9cb1ed3aa97aaf023d7f8287b8e62601reed@google.com#include "SkTypefacePriv.h"
46b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkUtils.h"
47b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#include "SkXPSDevice.h"
48b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
49b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//Windows defines a FLOAT type,
50b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//make it clear when converting a scalar that this is what is wanted.
51b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define SkScalarToFLOAT(n) SkScalarToFloat(n)
52b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
53da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner//Dummy representation of a GUID from createId.
54b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define L_GUID_ID L"XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX"
55da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner//Length of GUID representation from createId, including NULL terminator.
56b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com#define GUID_ID_LEN SK_ARRAY_COUNT(L_GUID_ID)
57b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
58b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/**
59b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   Formats a GUID and places it into buffer.
60b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   buffer should have space for at least GUID_ID_LEN wide characters.
61b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   The string will always be wchar null terminated.
62b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0
63b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com   @return -1 if there was an error, > 0 if success.
64b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com */
65b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic int format_guid(const GUID& guid,
66b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       wchar_t* buffer, size_t bufferSize,
67b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       wchar_t sep = '-') {
68b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkASSERT(bufferSize >= GUID_ID_LEN);
69b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return swprintf_s(buffer,
70b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      bufferSize,
71b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      L"%08lX%c%04X%c%04X%c%02X%02X%c%02X%02X%02X%02X%02X%02X",
72b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data1,
73b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
74b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data2,
75b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
76b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data3,
77b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
78b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[0],
79b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[1],
80b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      sep,
81b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[2],
82b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[3],
83b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[4],
84b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[5],
85b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[6],
86b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      guid.Data4[7]);
87b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
88b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
89da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen WagnerHRESULT SkXPSDevice::createId(wchar_t* buffer, size_t bufferSize, wchar_t sep) {
90b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    GUID guid = {};
91da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#ifdef SK_XPS_USE_DETERMINISTIC_IDS
92da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    guid.Data1 = fNextId++;
93da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    // The following make this a valid Type4 UUID.
94da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    guid.Data3 = 0x4000;
95da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    guid.Data4[0] = 0x80;
96da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#else
97b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(CoCreateGuid(&guid), "Could not create GUID for id.");
98da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner#endif
9974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
100b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (format_guid(guid, buffer, bufferSize, sep) == -1) {
101b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(E_UNEXPECTED, "Could not format GUID into id.");
102b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
10374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
104b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
105b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
106b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
107b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic SkBitmap make_fake_bitmap(int width, int height) {
108b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkBitmap bitmap;
109a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org    bitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
110b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return bitmap;
111b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
112b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
113900ecf2f1579d42c9d2959831787af0346320f86reed@google.com// TODO: should inherit from SkBaseDevice instead of SkBitmapDevice...
114b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::SkXPSDevice()
1151f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    : SkBitmapDevice(make_fake_bitmap(10000, 10000))
116b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , fCurrentPage(0) {
117b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
118b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
119b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::~SkXPSDevice() {
120b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
121b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
122b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::TypefaceUse::TypefaceUse()
123b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    : typefaceId(0xffffffff)
124b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , fontData(NULL)
125b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , xpsFont(NULL)
126b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , glyphsUsed(NULL) {
127b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
128b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
129b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::TypefaceUse::~TypefaceUse() {
130b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //xpsFont owns fontData ref
131b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->xpsFont->Release();
132b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    delete this->glyphsUsed;
133b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
134b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
135b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::beginPortfolio(SkWStream* outputStream) {
136b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!this->fAutoCo.succeeded()) return false;
13774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
138b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create XPS Factory.
139b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(CoCreateInstance(
140b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSID_XpsOMObjectFactory,
141b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             NULL,
142b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSCTX_INPROC_SERVER,
143b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             IID_PPV_ARGS(&this->fXpsFactory)),
144b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create XPS factory.");
14574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
146b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(SkWIStream::CreateFromSkWStream(outputStream, &this->fOutputStream),
147b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not convert SkStream to IStream.");
14874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
149b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
150b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
151b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
152b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::beginSheet(
153b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkVector& unitsPerMeter,
154b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkVector& pixelsPerMeter,
155b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkSize& trimSize,
156b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* mediaBox,
157b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* bleedBox,
158b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* artBox,
159b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect* cropBox) {
160b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ++this->fCurrentPage;
16174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
162b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //For simplicity, just write everything out in geometry units,
163b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //then have a base canvas do the scale to physical units.
164b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentCanvasSize = trimSize;
165b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentUnitsPerMeter = unitsPerMeter;
166b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentPixelsPerMeter = pixelsPerMeter;
16774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
168b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentXpsCanvas.reset();
169b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
170b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create base canvas.");
17174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
172b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
173b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
174b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
175b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsThumbnail(IXpsOMPage* page,
176b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                        const unsigned int pageNum,
177b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                        IXpsOMImageResource** image) {
178b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMThumbnailGenerator> thumbnailGenerator;
179b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(CoCreateInstance(
180b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            CLSID_XpsOMThumbnailGenerator,
181b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            NULL,
182b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            CLSCTX_INPROC_SERVER,
183b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            IID_PPV_ARGS(&thumbnailGenerator)),
184b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create thumbnail generator.");
18574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
186b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
187b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    static const size_t size = SkTUMax<
188b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Documents/1/Metadata/.png") + SK_DIGITS_IN(pageNum),
189b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Metadata/" L_GUID_ID L".png")
190b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    >::value;
191b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
192b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (pageNum > 0) {
193b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        swprintf_s(buffer, size, L"/Documents/1/Metadata/%u.png", pageNum);
194b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
195b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        wchar_t id[GUID_ID_LEN];
196da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner        HR(this->createId(id, GUID_ID_LEN));
197b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        swprintf_s(buffer, size, L"/Metadata/%s.png", id);
198b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
199b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
200b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create thumbnail part uri.");
20174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
202b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(thumbnailGenerator->GenerateThumbnail(page,
203b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              XPS_IMAGE_TYPE_PNG,
204b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              XPS_THUMBNAIL_SIZE_LARGE,
205b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              partUri.get(),
206b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              image),
207b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not generate thumbnail.");
20874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
209b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
210b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
211b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
212b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsPage(const XPS_SIZE& pageSize,
213b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMPage** page) {
214b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const size_t size = SK_ARRAY_COUNT(L"/Documents/1/Pages/.fpage")
215b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             + SK_DIGITS_IN(fCurrentPage);
216b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
217b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    swprintf_s(buffer, size, L"/Documents/1/Pages/%u.fpage",
218b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             this->fCurrentPage);
219b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
220b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
221b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create page part uri.");
22274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
223b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //If the language is unknown, use "und" (XPS Spec 2.3.5.1).
224b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePage(&pageSize,
225b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      L"und",
226b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      partUri.get(),
227b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      page),
228b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create page.");
22974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
230b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
231b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
232b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
233b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::initXpsDocumentWriter(IXpsOMImageResource* image) {
234b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create package writer.
235b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    {
236b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IOpcPartUri> partUri;
237b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreatePartUri(L"/FixedDocumentSequence.fdseq",
238b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             &partUri),
239b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create document sequence part uri.");
240b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreatePackageWriterOnStream(
241b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                this->fOutputStream.get(),
242b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                TRUE,
243b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                XPS_INTERLEAVING_OFF, //XPS_INTERLEAVING_ON,
244b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                partUri.get(),
245b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                NULL,
246b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                image,
247b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                NULL,
248b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                NULL,
249b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &this->fPackageWriter),
250b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create package writer.");
251b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
25274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
253b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Begin the lone document.
254b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    {
255b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IOpcPartUri> partUri;
256b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreatePartUri(
257b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                L"/Documents/1/FixedDocument.fdoc",
258b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &partUri),
259b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create fixed document part uri.");
260b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fPackageWriter->StartNewDocument(partUri.get(),
261b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL,
262b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL,
263b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL,
264b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                   NULL),
265b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not start document.");
266b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
26774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
268b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
269b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
270b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
271b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::endSheet() {
272b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //XPS is fixed at 96dpi (XPS Spec 11.1).
273b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const float xpsDPI = 96.0f;
274b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const float inchesPerMeter = 10000.0f / 254.0f;
275b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    static const float targetUnitsPerMeter = xpsDPI * inchesPerMeter;
276b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const float scaleX = targetUnitsPerMeter
277b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fX);
278b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const float scaleY = targetUnitsPerMeter
279b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                       / SkScalarToFLOAT(this->fCurrentUnitsPerMeter.fY);
28074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
281b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the scale canvas.
282b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMCanvas> scaleCanvas;
283b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fXpsFactory->CreateCanvas(&scaleCanvas),
284b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create scale canvas.");
285b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> scaleCanvasVisuals;
286b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(scaleCanvas->GetVisuals(&scaleCanvasVisuals),
287b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get scale canvas visuals.");
28874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
289b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> geomToPhys;
290b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_MATRIX rawGeomToPhys = { scaleX, 0, 0, scaleY, 0, 0, };
291b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fXpsFactory->CreateMatrixTransform(&rawGeomToPhys, &geomToPhys),
292b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create geometry to physical transform.");
293b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(scaleCanvas->SetTransformLocal(geomToPhys.get()),
294b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set transform on scale canvas.");
29574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
296b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the content canvas to the scale canvas.
297b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(scaleCanvasVisuals->Append(this->fCurrentXpsCanvas.get()),
298b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add base canvas to scale canvas.");
29974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
300b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the page.
301b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_SIZE pageSize = {
302b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(this->fCurrentCanvasSize.width()) * scaleX,
303b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(this->fCurrentCanvasSize.height()) * scaleY,
304b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
305b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPage> page;
306b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRB(this->createXpsPage(pageSize, &page));
30774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
308b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> pageVisuals;
309b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(page->GetVisuals(&pageVisuals), "Could not get page visuals.");
31074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
311b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the scale canvas to the page.
312b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(pageVisuals->Append(scaleCanvas.get()),
313b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add scale canvas to page.");
31474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
315b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the package writer if it hasn't been created yet.
316b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (NULL == this->fPackageWriter.get()) {
317b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMImageResource> image;
318b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Ignore return, thumbnail is completely optional.
319b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->createXpsThumbnail(page.get(), 0, &image);
32074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
321b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRB(this->initXpsDocumentWriter(image.get()));
322b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
32374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
324b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fPackageWriter->AddPage(page.get(),
325b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       &pageSize,
326b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL,
327b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL,
328b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL,
329b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       NULL),
330b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not write the page.");
331b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->fCurrentXpsCanvas.reset();
33274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
333b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
334b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
335b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
336b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic HRESULT subset_typeface(SkXPSDevice::TypefaceUse* current) {
337b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //CreateFontPackage wants unsigned short.
338b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Microsoft, Y U NO stdint.h?
339b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<unsigned short> keepList;
340b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    current->glyphsUsed->exportTo(&keepList);
34174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
342b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    int ttcCount = (current->ttcIndex + 1);
343b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
344b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //The following are declared with the types required by CreateFontPackage.
345b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    unsigned char *fontPackageBufferRaw = NULL;
346b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    unsigned long fontPackageBufferSize;
347b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    unsigned long bytesWritten;
348b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    unsigned long result = CreateFontPackage(
349b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        (unsigned char *) current->fontData->getMemoryBase(),
350b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        (unsigned long) current->fontData->getLength(),
351b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        &fontPackageBufferRaw,
352b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        &fontPackageBufferSize,
353b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        &bytesWritten,
354b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        TTFCFP_FLAGS_SUBSET | TTFCFP_FLAGS_GLYPHLIST | (ttcCount > 0 ? TTFCFP_FLAGS_TTC : 0),
355b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        current->ttcIndex,
356b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        TTFCFP_SUBSET,
357b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0,
358b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0,
359b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0,
360b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        keepList.begin(),
361b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        keepList.count(),
362b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        sk_malloc_throw,
363b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        sk_realloc_throw,
364b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        sk_free,
365b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        NULL);
366b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    SkAutoTMalloc<unsigned char> fontPackageBuffer(fontPackageBufferRaw);
367b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (result != NO_ERROR) {
368b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkDEBUGF(("CreateFontPackage Error %lu", result));
369b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return E_UNEXPECTED;
370b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
37174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
372b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    // If it was originally a ttc, keep it a ttc.
373b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    // CreateFontPackage over-allocates, realloc usually decreases the size substantially.
374b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    size_t extra;
375b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    if (ttcCount > 0) {
376b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        // Create space for a ttc header.
377b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        extra = sizeof(SkTTCFHeader) + (ttcCount * sizeof(SK_OT_ULONG));
378b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        fontPackageBuffer.realloc(bytesWritten + extra);
379b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        //overlap is certain, use memmove
380b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        memmove(fontPackageBuffer.get() + extra, fontPackageBuffer.get(), bytesWritten);
381b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
382b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        // Write the ttc header.
383b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SkTTCFHeader* ttcfHeader = reinterpret_cast<SkTTCFHeader*>(fontPackageBuffer.get());
384b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        ttcfHeader->ttcTag = SkTTCFHeader::TAG;
385b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        ttcfHeader->version = SkTTCFHeader::version_1;
386b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        ttcfHeader->numOffsets = SkEndian_SwapBE32(ttcCount);
387b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SK_OT_ULONG* offsetPtr = SkTAfter<SK_OT_ULONG>(ttcfHeader);
388b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        for (int i = 0; i < ttcCount; ++i, ++offsetPtr) {
3890aa5cea8694d3686b6742a36eab81ab9001de954bsalomon            *offsetPtr = SkEndian_SwapBE32(SkToU32(extra));
390b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        }
391b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
392b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        // Fix up offsets in sfnt table entries.
393b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SkSFNTHeader* sfntHeader = SkTAddOffset<SkSFNTHeader>(fontPackageBuffer.get(), extra);
394b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        int numTables = SkEndian_SwapBE16(sfntHeader->numTables);
395b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        SkSFNTHeader::TableDirectoryEntry* tableDirectory =
396b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org            SkTAfter<SkSFNTHeader::TableDirectoryEntry>(sfntHeader);
397b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        for (int i = 0; i < numTables; ++i, ++tableDirectory) {
398b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org            tableDirectory->offset = SkEndian_SwapBE32(
3990aa5cea8694d3686b6742a36eab81ab9001de954bsalomon                SkToU32(SkEndian_SwapBE32(SkToU32(tableDirectory->offset)) + extra));
400b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        }
401b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    } else {
402b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        extra = 0;
403b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org        fontPackageBuffer.realloc(bytesWritten);
404b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    }
405b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
406a1193e4b0e34a7e4e1bd33e9708d7341679f8321scroggo    SkAutoTDelete<SkMemoryStream> newStream(new SkMemoryStream());
407b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    newStream->setMemoryOwned(fontPackageBuffer.detach(), bytesWritten + extra);
408b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
409b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IStream> newIStream;
410b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    SkIStream::CreateFromSkStream(newStream.detach(), true, &newIStream);
41174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
412b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_FONT_EMBEDDING embedding;
413b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(current->xpsFont->GetEmbeddingOption(&embedding),
414b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get embedding option from font.");
41574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
416b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
417b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(current->xpsFont->GetPartName(&partUri),
418b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get part uri from font.");
41974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
420b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(current->xpsFont->SetContent(
421b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            newIStream.get(),
422b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            embedding,
423b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            partUri.get()),
424b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set new stream for subsetted font.");
42574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
426b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
427b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
428b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
429b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.combool SkXPSDevice::endPortfolio() {
430b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Subset fonts
431b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!this->fTypefaces.empty()) {
432b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkXPSDevice::TypefaceUse* current = &this->fTypefaces.front();
433b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const TypefaceUse* last = &this->fTypefaces.back();
434b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        for (; current <= last; ++current) {
435b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //Ignore return for now, if it didn't subset, let it be.
436b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            subset_typeface(current);
437b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
438b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
43974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
440b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRBM(this->fPackageWriter->Close(), "Could not close writer.");
44174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
442b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return true;
443b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
444b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
445b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_COLOR xps_color(const SkColor skColor) {
446b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //XPS uses non-pre-multiplied alpha (XPS Spec 11.4).
447b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_COLOR xpsColor;
448b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.colorType = XPS_COLOR_TYPE_SRGB;
449b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.alpha = SkColorGetA(skColor);
450b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.red = SkColorGetR(skColor);
451b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.green = SkColorGetG(skColor);
452b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsColor.value.sRGB.blue = SkColorGetB(skColor);
45374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
454b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return xpsColor;
455b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
456b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
457b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_POINT xps_point(const SkPoint& point) {
458b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT xpsPoint = {
459b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(point.fX),
460b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(point.fY),
461b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
462b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return xpsPoint;
463b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
464b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
465b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_POINT xps_point(const SkPoint& point, const SkMatrix& matrix) {
466b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint skTransformedPoint;
467b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    matrix.mapXY(point.fX, point.fY, &skTransformedPoint);
468b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return xps_point(skTransformedPoint);
469b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
470b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
471b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_SPREAD_METHOD xps_spread_method(SkShader::TileMode tileMode) {
472b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    switch (tileMode) {
473b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    case SkShader::kClamp_TileMode:
474b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return XPS_SPREAD_METHOD_PAD;
475b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    case SkShader::kRepeat_TileMode:
476b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return XPS_SPREAD_METHOD_REPEAT;
477b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    case SkShader::kMirror_TileMode:
478b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return XPS_SPREAD_METHOD_REFLECT;
479b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    default:
480330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com        SkDEBUGFAIL("Unknown tile mode.");
481b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
482b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return XPS_SPREAD_METHOD_PAD;
483b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
484b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
485b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic void transform_offsets(SkScalar* stopOffsets, const int numOffsets,
486b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const SkPoint& start, const SkPoint& end,
487b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const SkMatrix& transform) {
488b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint startTransformed;
489b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    transform.mapXY(start.fX, start.fY, &startTransformed);
490b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint endTransformed;
491b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    transform.mapXY(end.fX, end.fY, &endTransformed);
49274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
493b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Manhattan distance between transformed start and end.
494b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkScalar startToEnd = (endTransformed.fX - startTransformed.fX)
495b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        + (endTransformed.fY - startTransformed.fY);
496b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (SkScalarNearlyZero(startToEnd)) {
497b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        for (int i = 0; i < numOffsets; ++i) {
498b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            stopOffsets[i] = 0;
499b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
500b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
501b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
50274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
503b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    for (int i = 0; i < numOffsets; ++i) {
504b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPoint stop;
505b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        stop.fX = SkScalarMul(end.fX - start.fX, stopOffsets[i]);
506b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        stop.fY = SkScalarMul(end.fY - start.fY, stopOffsets[i]);
50774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
508b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPoint stopTransformed;
509b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        transform.mapXY(stop.fX, stop.fY, &stopTransformed);
51074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
511b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Manhattan distance between transformed start and stop.
512b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar startToStop = (stopTransformed.fX - startTransformed.fX)
513b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             + (stopTransformed.fY - startTransformed.fY);
514b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Percentage along transformed line.
51580ea19ca4bdd68c1493666a5fe7e4ce9d43ded8breed        stopOffsets[i] = startToStop / startToEnd;
516b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
517b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
518b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
519b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsTransform(const SkMatrix& matrix,
520b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                        IXpsOMMatrixTransform** xpsTransform) {
521b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkScalar affine[6];
522b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!matrix.asAffine(affine)) {
523b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        *xpsTransform = NULL;
524b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return S_FALSE;
525b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
526b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_MATRIX rawXpsMatrix = {
527b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kAScaleX]),
528b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kASkewY]),
529b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kASkewX]),
530b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kAScaleY]),
531b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kATransX]),
532b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(affine[SkMatrix::kATransY]),
533b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
534b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateMatrixTransform(&rawXpsMatrix, xpsTransform),
535b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create transform.");
53674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
537b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
538b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
539b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
540b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createPath(IXpsOMGeometryFigure* figure,
541b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                IXpsOMVisualCollection* visuals,
542b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                IXpsOMPath** path) {
543b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> geometry;
544b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGeometry(&geometry),
545b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create geometry.");
54674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
547b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> figureCollection;
548b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(geometry->GetFigures(&figureCollection), "Could not get figures.");
549b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figureCollection->Append(figure), "Could not add figure.");
55074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
551b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePath(path), "Could not create path.");
552b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*path)->SetGeometryLocal(geometry.get()), "Could not set geometry");
55374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
554b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(visuals->Append(*path), "Could not add path to visuals.");
555b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
556b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
557b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
558b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsSolidColorBrush(const SkColor skColor,
559b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              const SkAlpha alpha,
560b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              IXpsOMBrush** xpsBrush) {
561b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_COLOR xpsColor = xps_color(skColor);
562b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMSolidColorBrush> solidBrush;
563b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateSolidColorBrush(&xpsColor, NULL, &solidBrush),
564b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create solid color brush.");
565b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(solidBrush->SetOpacity(alpha / 255.0f), "Could not set opacity.");
566b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(solidBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI Fail.");
567b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
568b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
569b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
570b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::sideOfClamp(const SkRect& areaToFill,
571b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 const XPS_RECT& imageViewBox,
572b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 IXpsOMImageResource* image,
573b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 IXpsOMVisualCollection* visuals) {
574b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
575b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
57674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
577b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> areaToFillPath;
578b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
57974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
580b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMImageBrush> areaToFillBrush;
581b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateImageBrush(image,
582b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &imageViewBox,
583b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &imageViewBox,
584b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &areaToFillBrush),
585b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create brush for side of clamp.");
586b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(areaToFillBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
587b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set tile mode for side of clamp.");
588b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
589b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set brush for side of clamp");
59074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
591b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
592b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
593b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
594b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::cornerOfClamp(const SkRect& areaToFill,
595b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   const SkColor color,
596b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMVisualCollection* visuals) {
597b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> areaToFillFigure;
598b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsRect(areaToFill, FALSE, TRUE, &areaToFillFigure));
59974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
600b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> areaToFillPath;
601b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createPath(areaToFillFigure.get(), visuals, &areaToFillPath));
60274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
603b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMBrush> areaToFillBrush;
604b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsSolidColorBrush(color, 0xFF, &areaToFillBrush));
605b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(areaToFillPath->SetFillBrushLocal(areaToFillBrush.get()),
606b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set brush for corner of clamp.");
60774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
608b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
609b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
610b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
611b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_N  = XPS_TILE_MODE_NONE;
612b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_T  = XPS_TILE_MODE_TILE;
613b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_X  = XPS_TILE_MODE_FLIPX;
614b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_Y  = XPS_TILE_MODE_FLIPY;
615b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic const XPS_TILE_MODE XTM_XY = XPS_TILE_MODE_FLIPXY;
616b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
617b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//TODO(bungeman): In the future, should skia add None,
618b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//handle None+Mirror and None+Repeat correctly.
619b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com//None is currently an internal hack so masks don't repeat (None+None only).
620b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic XPS_TILE_MODE SkToXpsTileMode[SkShader::kTileModeCount+1]
621b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    [SkShader::kTileModeCount+1] = {
622b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           //Clamp //Repeat //Mirror //None
623b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*Clamp */ XTM_N,  XTM_T,   XTM_Y,   XTM_N,
624b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*Repeat*/ XTM_T,  XTM_T,   XTM_Y,   XTM_N,
625b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*Mirror*/ XTM_X,  XTM_X,   XTM_XY,  XTM_X,
626b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com/*None  */ XTM_N,  XTM_N,   XTM_Y,   XTM_N,
627b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com};
628b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
629b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsImageBrush(
630b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkBitmap& bitmap,
631b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkMatrix& localMatrix,
632b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkShader::TileMode (&xy)[2],
633b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkAlpha alpha,
634b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        IXpsOMTileBrush** xpsBrush) {
635b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDynamicMemoryWStream write;
636b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!SkImageEncoder::EncodeStream(&write, bitmap,
637b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      SkImageEncoder::kPNG_Type, 100)) {
638b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(E_FAIL, "Unable to encode bitmap as png.");
639b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
640b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMemoryStream* read = new SkMemoryStream;
641b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    read->setData(write.copyToData())->unref();
642b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IStream> readWrapper;
643b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(SkIStream::CreateFromSkStream(read, true, &readWrapper),
644b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create stream from png data.");
64574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
646b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const size_t size =
647b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Documents/1/Resources/Images/" L_GUID_ID L".png");
648b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
649b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t id[GUID_ID_LEN];
650da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    HR(this->createId(id, GUID_ID_LEN));
651b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    swprintf_s(buffer, size, L"/Documents/1/Resources/Images/%s.png", id);
65274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
653b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> imagePartUri;
654b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &imagePartUri),
655b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create image part uri.");
65674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
657b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMImageResource> imageResource;
658b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateImageResource(
659b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            readWrapper.get(),
660b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_IMAGE_TYPE_PNG,
661b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            imagePartUri.get(),
662b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            &imageResource),
663b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create image resource.");
66474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
665b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_RECT bitmapRect = {
666b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0.0, 0.0,
667b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        static_cast<FLOAT>(bitmap.width()), static_cast<FLOAT>(bitmap.height())
668b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
669b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMImageBrush> xpsImageBrush;
670b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateImageBrush(imageResource.get(),
671b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &bitmapRect, &bitmapRect,
672b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                            &xpsImageBrush),
673b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create image brush.");
67474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
675b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (SkShader::kClamp_TileMode != xy[0] &&
676b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkShader::kClamp_TileMode != xy[1]) {
67774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
678b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
679b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set image tile mode");
680b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->SetOpacity(alpha / 255.0f),
681b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set image opacity.");
682b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->QueryInterface(xpsBrush), "QI failed.");
683b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
684b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO(bungeman): compute how big this really needs to be.
685b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkScalar BIG = SkIntToScalar(1000); //SK_ScalarMax;
686b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const FLOAT BIG_F = SkScalarToFLOAT(BIG);
687b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkScalar bWidth = SkIntToScalar(bitmap.width());
688b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkScalar bHeight = SkIntToScalar(bitmap.height());
68974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
690b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //create brush canvas
691b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMCanvas> brushCanvas;
692b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreateCanvas(&brushCanvas),
693b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create image brush canvas.");
694b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMVisualCollection> brushVisuals;
695b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(brushCanvas->GetVisuals(&brushVisuals),
696b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not get image brush canvas visuals collection.");
69774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
698b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //create central figure
699b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect bitmapPoints = SkRect::MakeLTRB(0, 0, bWidth, bHeight);
700b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMGeometryFigure> centralFigure;
701b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsRect(bitmapPoints, FALSE, TRUE, &centralFigure));
70274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
703b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMPath> centralPath;
704b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createPath(centralFigure.get(),
705b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            brushVisuals.get(),
706b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            &centralPath));
707b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(xpsImageBrush->SetTileMode(XPS_TILE_MODE_FLIPXY),
708b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set tile mode for image brush central path.");
709b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(centralPath->SetFillBrushLocal(xpsImageBrush.get()),
710b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set fill brush for image brush central path.");
71174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
712b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //add left/right
713b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[0]) {
714b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect leftArea = SkRect::MakeLTRB(-BIG, 0, 0, bHeight);
715b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT leftImageViewBox = {
716b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0.0, 0.0,
717b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                1.0, static_cast<FLOAT>(bitmap.height()),
718b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
719b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(leftArea, leftImageViewBox,
720b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
721b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
722b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
723b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect rightArea = SkRect::MakeLTRB(bWidth, 0, BIG, bHeight);
724b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT rightImageViewBox = {
725b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                bitmap.width() - 1.0f, 0.0f,
726b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                1.0f, static_cast<FLOAT>(bitmap.height()),
727b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
728b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(rightArea, rightImageViewBox,
729b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
730b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
731b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
73274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
733b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //add top/bottom
734b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[1]) {
735b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect topArea = SkRect::MakeLTRB(0, -BIG, bWidth, 0);
736b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT topImageViewBox = {
737b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0.0, 0.0,
738b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<FLOAT>(bitmap.width()), 1.0,
739b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
740b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(topArea, topImageViewBox,
741b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
742b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
74374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
744b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkRect bottomArea = SkRect::MakeLTRB(0, bHeight, bWidth, BIG);
745b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_RECT bottomImageViewBox = {
746b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0.0f, bitmap.height() - 1.0f,
747b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<FLOAT>(bitmap.width()), 1.0f,
748b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            };
749b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->sideOfClamp(bottomArea, bottomImageViewBox,
750b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 imageResource.get(),
751b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 brushVisuals.get()));
752b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
75374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
754b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //add tl, tr, bl, br
755b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[0] &&
756b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode == xy[1]) {
75774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
758b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoLockPixels alp(bitmap);
75974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
760b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor tlColor = bitmap.getColor(0,0);
761b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect tlArea = SkRect::MakeLTRB(-BIG, -BIG, 0, 0);
762b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(tlArea, tlColor, brushVisuals.get()));
76374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
764b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor trColor = bitmap.getColor(bitmap.width()-1,0);
765b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect trArea = SkRect::MakeLTRB(bWidth, -BIG, BIG, 0);
766b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(trArea, trColor, brushVisuals.get()));
76774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
768b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor brColor = bitmap.getColor(bitmap.width()-1,
769b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                    bitmap.height()-1);
770b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect brArea = SkRect::MakeLTRB(bWidth, bHeight, BIG, BIG);
771b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(brArea, brColor, brushVisuals.get()));
77274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
773b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor blColor = bitmap.getColor(0,bitmap.height()-1);
774b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect blArea = SkRect::MakeLTRB(-BIG, bHeight, 0, BIG);
775b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->cornerOfClamp(blArea, blColor, brushVisuals.get()));
776b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
77774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
778b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //create visual brush from canvas
779b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_RECT bound = {};
780b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kClamp_TileMode == xy[0] &&
781b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode == xy[1]) {
78274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
783b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.x = BIG_F / -2;
784b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.y = BIG_F / -2;
785b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.width = BIG_F;
786b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.height = BIG_F;
787b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else if (SkShader::kClamp_TileMode == xy[0]) {
788b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.x = BIG_F / -2;
789b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.y = 0.0f;
790b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.width = BIG_F;
791b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.height = static_cast<FLOAT>(bitmap.height());
792b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else if (SkShader::kClamp_TileMode == xy[1]) {
793b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.x = 0;
794b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.y = BIG_F / -2;
795b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.width = static_cast<FLOAT>(bitmap.width());
796b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            bound.height = BIG_F;
797b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
798b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMVisualBrush> clampBrush;
799b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreateVisualBrush(&bound, &bound, &clampBrush),
800b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create visual brush for image brush.");
801b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->SetVisualLocal(brushCanvas.get()),
802b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set canvas on visual brush for image brush.");
803b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->SetTileMode(SkToXpsTileMode[xy[0]][xy[1]]),
804b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set tile mode on visual brush for image brush.");
805b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->SetOpacity(alpha / 255.0f),
806b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set opacity on visual brush for image brush.");
80774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
808b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(clampBrush->QueryInterface(xpsBrush), "QI failed.");
809b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
81074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
811b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
812b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
81349f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrixToUse.get()) {
814b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM((*xpsBrush)->SetTransformLocal(xpsMatrixToUse.get()),
815b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set transform for image brush.");
816b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
817b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO(bungeman): perspective bitmaps in general.
818b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
81974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
820b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
821b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
822b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
823b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsGradientStop(const SkColor skColor,
824b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                           const SkScalar offset,
825b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                           IXpsOMGradientStop** xpsGradStop) {
826b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_COLOR gradStopXpsColor = xps_color(skColor);
827b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGradientStop(&gradStopXpsColor,
828b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              NULL,
829b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              SkScalarToFLOAT(offset),
830b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              xpsGradStop),
831b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create gradient stop.");
832b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
833b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
834b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
835b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsLinearGradient(SkShader::GradientInfo info,
836b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkAlpha alpha,
837b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkMatrix& localMatrix,
838b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMMatrixTransform* xpsMatrix,
839b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMBrush** xpsBrush) {
840b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT startPoint;
841b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT endPoint;
84249f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
843b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        startPoint = xps_point(info.fPoint[0]);
844b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        endPoint = xps_point(info.fPoint[1]);
845b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
846b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        transform_offsets(info.fColorOffsets, info.fColorCount,
847b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                          info.fPoint[0], info.fPoint[1],
848b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                          localMatrix);
849b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        startPoint = xps_point(info.fPoint[0], localMatrix);
850b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        endPoint = xps_point(info.fPoint[1], localMatrix);
851b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
85274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
853b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
854b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[0],
855b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[0],
856b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop0));
85774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
858b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
859b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[1],
860b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[1],
861b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop1));
86274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
863b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMLinearGradientBrush> gradientBrush;
864b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateLinearGradientBrush(gradStop0.get(),
865b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     gradStop1.get(),
866b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &startPoint,
867b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &endPoint,
868b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &gradientBrush),
869b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create linear gradient brush.");
87049f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
871b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradientBrush->SetTransformLocal(xpsMatrix),
872b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set transform on linear gradient brush.");
873b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
87474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
875b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
876b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->GetGradientStops(&gradStopCollection),
877b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get linear gradient stop collection.");
878b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    for (int i = 2; i < info.fColorCount; ++i) {
879b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMGradientStop> gradStop;
880b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(createXpsGradientStop(info.fColors[i],
881b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 info.fColorOffsets[i],
882b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 &gradStop));
883b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradStopCollection->Append(gradStop.get()),
884b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add linear gradient stop.");
885b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
88674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
887b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
888b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set spread method of linear gradient.");
88974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
890b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetOpacity(alpha / 255.0f),
891b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set opacity of linear gradient brush.");
892b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed");
89374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
894b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
895b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
896b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
897b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsRadialGradient(SkShader::GradientInfo info,
898b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkAlpha alpha,
899b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             const SkMatrix& localMatrix,
900b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMMatrixTransform* xpsMatrix,
901b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             IXpsOMBrush** xpsBrush) {
902b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop0;
903b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[0],
904b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[0],
905b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop0));
90674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
907b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStop> gradStop1;
908b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(createXpsGradientStop(info.fColors[1],
909b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             info.fColorOffsets[1],
910b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             &gradStop1));
91174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
912b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: figure out how to fake better if not affine
913b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT centerPoint;
914b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT gradientOrigin;
915b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_SIZE radiiSizes;
91649f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
917b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        centerPoint = xps_point(info.fPoint[0]);
918b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        gradientOrigin = xps_point(info.fPoint[0]);
919b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.width = SkScalarToFLOAT(info.fRadius[0]);
920b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.height = SkScalarToFLOAT(info.fRadius[0]);
921b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
922b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        centerPoint = xps_point(info.fPoint[0], localMatrix);
923b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        gradientOrigin = xps_point(info.fPoint[0], localMatrix);
92474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
925b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar radius = info.fRadius[0];
926b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkVector vec[2];
92774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
928b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        vec[0].set(radius, 0);
929b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        vec[1].set(0, radius);
930b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        localMatrix.mapVectors(vec, 2);
93174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
932b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar d0 = vec[0].length();
933b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalar d1 = vec[1].length();
93474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
935b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.width = SkScalarToFLOAT(d0);
936b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        radiiSizes.height = SkScalarToFLOAT(d1);
937b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
93874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
939b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMRadialGradientBrush> gradientBrush;
940b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateRadialGradientBrush(gradStop0.get(),
941b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     gradStop1.get(),
942b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &centerPoint,
943b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &gradientOrigin,
944b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &radiiSizes,
945b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                     &gradientBrush),
946b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create radial gradient brush.");
94749f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsMatrix) {
948b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradientBrush->SetTransformLocal(xpsMatrix),
949b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set transform on radial gradient brush.");
950b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
95174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
952b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGradientStopCollection> gradStopCollection;
953b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->GetGradientStops(&gradStopCollection),
954b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get radial gradient stop collection.");
955b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    for (int i = 2; i < info.fColorCount; ++i) {
956b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMGradientStop> gradStop;
957b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(createXpsGradientStop(info.fColors[i],
958b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 info.fColorOffsets[i],
959b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 &gradStop));
960b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(gradStopCollection->Append(gradStop.get()),
961b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add radial gradient stop.");
962b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
96374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
964b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetSpreadMethod(xps_spread_method(info.fTileMode)),
965b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set spread method of radial gradient.");
96674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
967b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->SetOpacity(alpha / 255.0f),
968b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set opacity of radial gradient brush.");
969b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(gradientBrush->QueryInterface<IXpsOMBrush>(xpsBrush), "QI failed.");
97074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
971b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
972b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
973b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
974b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsBrush(const SkPaint& skPaint,
975b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    IXpsOMBrush** brush,
976b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    const SkMatrix* parentTransform) {
977b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkShader *shader = skPaint.getShader();
978b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (NULL == shader) {
979b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
980b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return S_OK;
981b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
98274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
983b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Gradient shaders.
984b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::GradientInfo info;
985b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    info.fColorCount = 0;
986b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    info.fColors = NULL;
987b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    info.fColorOffsets = NULL;
988b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::GradientType gradientType = shader->asAGradient(&info);
98974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
990b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (SkShader::kNone_GradientType == gradientType) {
991b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //Nothing to see, move along.
99274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
993b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else if (SkShader::kColor_GradientType == gradientType) {
994b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkASSERT(1 == info.fColorCount);
995b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkColor color;
996b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        info.fColors = &color;
997b58a639b2fbe919489654bb506efdb024a308a8ebsalomon@google.com        shader->asAGradient(&info);
998b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkAlpha alpha = skPaint.getAlpha();
999b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsSolidColorBrush(color, alpha, brush));
1000b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return S_OK;
100174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1002b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
1003b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (info.fColorCount == 0) {
1004b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkColor color = skPaint.getColor();
1005b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsSolidColorBrush(color, 0xFF, brush));
1006b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1007b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
100874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1009b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkAutoTArray<SkColor> colors(info.fColorCount);
1010b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkAutoTArray<SkScalar> colorOffsets(info.fColorCount);
1011b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        info.fColors = colors.get();
1012b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        info.fColorOffsets = colorOffsets.get();
1013b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        shader->asAGradient(&info);
101474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1015b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (1 == info.fColorCount) {
1016b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkColor color = info.fColors[0];
1017b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAlpha alpha = skPaint.getAlpha();
1018b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsSolidColorBrush(color, alpha, brush));
1019b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1020b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
102174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1022f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com        SkMatrix localMatrix = shader->getLocalMatrix();
102349f085dddff10473b6ebf832a974288300224e60bsalomon        if (parentTransform) {
1024b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            localMatrix.preConcat(*parentTransform);
1025b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1026b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1027b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsTransform(localMatrix, &xpsMatrixToUse));
102874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1029b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kLinear_GradientType == gradientType) {
1030b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsLinearGradient(info,
1031b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             skPaint.getAlpha(),
1032b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             localMatrix,
1033b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             xpsMatrixToUse.get(),
1034b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             brush));
1035b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1036b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
103774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1038b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkShader::kRadial_GradientType == gradientType) {
1039b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsRadialGradient(info,
1040b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             skPaint.getAlpha(),
1041b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             localMatrix,
1042b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             xpsMatrixToUse.get(),
1043b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                             brush));
1044b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1045b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
104674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
104771a6cbfc585959738dc0b375603696ca7f60605freed        if (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::kDefault_BitmapType: {
1064b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //TODO: outMatrix??
1065f94b3a4cebd4adab09c40ebe23c02a615e10c394bsalomon@google.com            SkMatrix localMatrix = shader->getLocalMatrix();
106649f085dddff10473b6ebf832a974288300224e60bsalomon            if (parentTransform) {
1067b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                localMatrix.preConcat(*parentTransform);
1068b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
106974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1070b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkTScopedComPtr<IXpsOMTileBrush> tileBrush;
1071b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HR(this->createXpsImageBrush(outTexture,
1072b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         localMatrix,
1073b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         xy,
1074b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         skPaint.getAlpha(),
1075b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                         &tileBrush));
107674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1077b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(tileBrush->QueryInterface<IXpsOMBrush>(brush), "QI failed.");
107874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1079b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return S_OK;
1080b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1081b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        default:
1082b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1083b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
108474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1085b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsSolidColorBrush(skPaint.getColor(), 0xFF, brush));
1086b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1087b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1088b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1089b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic bool rect_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
1090b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool zeroWidth = (0 == paint.getStrokeWidth());
1091b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool stroke = (SkPaint::kFill_Style != paint.getStyle());
109274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1093b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return paint.getPathEffect() ||
1094b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           paint.getMaskFilter() ||
1095b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           paint.getRasterizer() ||
1096b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           (stroke && (
1097b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com               (matrix.hasPerspective() && !zeroWidth) ||
1098b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com               SkPaint::kMiter_Join != paint.getStrokeJoin() ||
1099b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com               (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
1100b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                paint.getStrokeMiter() < SK_ScalarSqrt2)
1101b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com           ))
1102b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ;
1103b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1104b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1105b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsRect(const SkRect& rect, BOOL stroke, BOOL fill,
1106b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMGeometryFigure** xpsRect) {
1107b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkPoint points[4] = {
1108b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fLeft, rect.fTop },
1109b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fRight, rect.fTop },
1110b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fRight, rect.fBottom },
1111b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        { rect.fLeft, rect.fBottom },
1112b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
1113b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return this->createXpsQuad(points, stroke, fill, xpsRect);
1114b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1115b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::createXpsQuad(const SkPoint (&points)[4],
1116b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   BOOL stroke, BOOL fill,
1117b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   IXpsOMGeometryFigure** xpsQuad) {
1118b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Define the start point.
1119b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT startPoint = xps_point(points[0]);
112074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1121b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Create the figure.
1122b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint, xpsQuad),
1123b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create quad geometry figure.");
112474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1125b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Define the type of each segment.
1126b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_SEGMENT_TYPE segmentTypes[3] = {
1127b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_SEGMENT_TYPE_LINE,
1128b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_SEGMENT_TYPE_LINE,
1129b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        XPS_SEGMENT_TYPE_LINE,
1130b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
113174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1132b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Define the x and y coordinates of each corner of the figure.
1133b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    FLOAT segmentData[6] = {
1134b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(points[1].fX), SkScalarToFLOAT(points[1].fY),
1135b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(points[2].fX), SkScalarToFLOAT(points[2].fY),
1136b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarToFLOAT(points[3].fX), SkScalarToFLOAT(points[3].fY),
1137b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
113874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1139b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Describe if the segments are stroked.
1140b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL segmentStrokes[3] = {
1141b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        stroke, stroke, stroke,
1142b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
114374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1144b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Add the segment data to the figure.
1145b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*xpsQuad)->SetSegments(
1146b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            3, 6,
1147b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            segmentTypes , segmentData, segmentStrokes),
1148b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not add segment data to quad.");
114974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1150b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Set the closed and filled properties of the figure.
1151b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*xpsQuad)->SetIsClosed(stroke), "Could not set quad close.");
1152b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM((*xpsQuad)->SetIsFilled(fill), "Could not set quad fill.");
115374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1154b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1155b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1156b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1157b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode,
1158b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             size_t count, const SkPoint points[],
1159b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkPaint& paint) {
1160b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //This will call back into the device to do the drawing.
1161b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    d.drawPoints(mode, count, points, paint, true);
1162b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1163b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1164b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode,
1165b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               int vertexCount, const SkPoint verts[],
1166b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkPoint texs[], const SkColor colors[],
1167b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               SkXfermode* xmode, const uint16_t indices[],
1168b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               int indexCount, const SkPaint& paint) {
1169b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: override this for XPS
1170b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDEBUGF(("XPS drawVertices not yet implemented."));
1171b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1172b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
11735dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.comvoid SkXPSDevice::drawPaint(const SkDraw& d, const SkPaint& origPaint) {
1174b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkRect r = SkRect::MakeSize(this->fCurrentCanvasSize);
117574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1176b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //If trying to paint with a stroke, ignore that and fill.
11775dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkPaint* fillPaint = const_cast<SkPaint*>(&origPaint);
11785dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
11795dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    if (paint->getStyle() != SkPaint::kFill_Style) {
11805dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        paint.writable()->setStyle(SkPaint::kFill_Style);
1181b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
118274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1183b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->internalDrawRect(d, r, false, *fillPaint);
1184b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1185b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1186b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawRect(const SkDraw& d,
1187b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkRect& r,
1188b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkPaint& paint) {
1189b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    this->internalDrawRect(d, r, true, paint);
1190b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1191b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1192cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.comvoid SkXPSDevice::drawRRect(const SkDraw& d,
1193cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com                            const SkRRect& rr,
1194cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com                            const SkPaint& paint) {
1195cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com    SkPath path;
1196cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com    path.addRRect(rr);
1197cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com    this->drawPath(d, path, paint, NULL, true);
1198cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com}
1199cac8d01eabd05d11365f56b6af3f30bccb173487scroggo@google.com
1200b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::internalDrawRect(const SkDraw& d,
1201b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   const SkRect& r,
1202b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   bool transformRect,
1203b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   const SkPaint& paint) {
1204b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Exit early if there is nothing to draw.
1205b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (d.fClip->isEmpty() ||
1206b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
1207b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1208b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
120974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1210b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Path the rect if we can't optimize it.
1211b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (rect_must_be_pathed(paint, *d.fMatrix)) {
1212b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath tmp;
1213b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        tmp.addRect(r);
1214b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        tmp.setFillType(SkPath::kWinding_FillType);
1215b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->drawPath(d, tmp, paint, NULL, true);
1216b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1217b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
121874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1219b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded path.
1220b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> shadedPath;
1221b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1222b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded path for rect.");
122374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1224b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded geometry.
1225b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1226b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1227b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded geometry for rect.");
122874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1229b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the geometry to the shaded path.
1230b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1231b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set shaded geometry for rect.");
123274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1233b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Set the brushes.
1234b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL fill = FALSE;
1235b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL stroke = FALSE;
1236b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->shadePath(shadedPath.get(), paint, *d.fMatrix, &fill, &stroke));
123774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1238b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    bool xpsTransformsPath = true;
1239b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Transform the geometry.
1240b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (transformRect && xpsTransformsPath) {
1241b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1242b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsTransform(*d.fMatrix, &xpsTransform));
1243b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (xpsTransform.get()) {
1244b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1245b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                 "Could not set transform for rect.");
1246b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
1247b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsTransformsPath = false;
1248b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1249b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
125074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1251b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the figure.
1252b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
1253b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    {
1254b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPoint points[4] = {
1255b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fLeft, r.fTop },
1256b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fLeft, r.fBottom },
1257b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fRight, r.fBottom },
1258b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            { r.fRight, r.fTop },
1259b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        };
1260b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (!xpsTransformsPath && transformRect) {
1261b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            d.fMatrix->mapPoints(points, SK_ARRAY_COUNT(points));
1262b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1263b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsQuad(points, stroke, fill, &rectFigure));
1264b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
126574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1266b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figures of the shaded geometry.
1267b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1268b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->GetFigures(&shadedFigures),
1269b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get shaded figures for rect.");
127074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1271b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the figure to the shaded geometry figures.
1272b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedFigures->Append(rectFigure.get()),
1273b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add shaded figure for rect.");
127474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1275b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->clip(shadedPath.get(), d));
127674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1277b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the shaded path to the current visuals.
1278b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1279b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1280b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for rect.");
1281b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(shadedPath.get()),
1282b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add rect to current visuals.");
1283b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1284b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1285b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic HRESULT close_figure(const SkTDArray<XPS_SEGMENT_TYPE>& segmentTypes,
1286b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            const SkTDArray<BOOL>& segmentStrokes,
1287b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            const SkTDArray<FLOAT>& segmentData,
1288b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            BOOL stroke, BOOL fill,
1289b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            IXpsOMGeometryFigure* figure,
1290b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            IXpsOMGeometryFigureCollection* figures) {
1291b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Add the segment data to the figure.
1292b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figure->SetSegments(segmentTypes.count(), segmentData.count(),
1293b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            segmentTypes.begin() , segmentData.begin(),
1294b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                            segmentStrokes.begin()),
1295b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set path segments.");
129674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1297b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Set the closed and filled properties of the figure.
1298b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figure->SetIsClosed(stroke), "Could not set path closed.");
1299b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figure->SetIsFilled(fill), "Could not set path fill.");
130074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1301b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // Add the figure created above to this geometry.
1302b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(figures->Append(figure), "Could not add path to geometry.");
1303b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1304b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1305b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1306b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::addXpsPathGeometry(
1307b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        IXpsOMGeometryFigureCollection* xpsFigures,
1308b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        BOOL stroke, BOOL fill, const SkPath& path) {
1309b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<XPS_SEGMENT_TYPE> segmentTypes;
1310b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<BOOL> segmentStrokes;
1311b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<FLOAT> segmentData;
131274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1313b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> xpsFigure;
1314b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath::Iter iter(path, true);
1315b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPoint points[4];
1316b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath::Verb verb;
1317b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    while ((verb = iter.next(points)) != SkPath::kDone_Verb) {
1318b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        switch (verb) {
1319b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kMove_Verb: {
132049f085dddff10473b6ebf832a974288300224e60bsalomon                if (xpsFigure.get()) {
1321b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1322b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    stroke, fill,
1323b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                    xpsFigure.get() , xpsFigures));
1324b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    xpsFigure.reset();
1325b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    segmentTypes.rewind();
1326b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    segmentStrokes.rewind();
1327b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    segmentData.rewind();
1328b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                }
1329b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // Define the start point.
1330b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                XPS_POINT startPoint = xps_point(points[0]);
1331b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // Create the figure.
1332b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                HRM(this->fXpsFactory->CreateGeometryFigure(&startPoint,
1333b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                                            &xpsFigure),
1334b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                    "Could not create path geometry figure.");
1335b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1336b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1337b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kLine_Verb:
1338b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                if (iter.isCloseLine()) break; //ignore the line, auto-closed
1339b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentTypes.push(XPS_SEGMENT_TYPE_LINE);
1340b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentStrokes.push(stroke);
1341b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fX));
1342b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fY));
1343b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1344b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kQuad_Verb:
1345b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
1346b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentStrokes.push(stroke);
1347b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fX));
1348b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fY));
1349b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fX));
1350b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fY));
1351b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1352b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kCubic_Verb:
1353b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentTypes.push(XPS_SEGMENT_TYPE_BEZIER);
1354b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentStrokes.push(stroke);
1355b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fX));
1356b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[1].fY));
1357b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fX));
1358b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[2].fY));
1359b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[3].fX));
1360b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                segmentData.push(SkScalarToFLOAT(points[3].fY));
1361b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
136247ef4d5d934bba86848aa238efab21f54a160c1ahalcanary            case SkPath::kConic_Verb: {
136347ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                const SkScalar tol = SK_Scalar1 / 4;
136447ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                SkAutoConicToQuads converter;
136547ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                const SkPoint* quads =
136647ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                    converter.computeQuads(points, iter.conicWeight(), tol);
136747ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                for (int i = 0; i < converter.countQuads(); ++i) {
136847ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                    segmentTypes.push(XPS_SEGMENT_TYPE_QUADRATIC_BEZIER);
136947ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                    segmentStrokes.push(stroke);
137047ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                    segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fX));
137147ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                    segmentData.push(SkScalarToFLOAT(quads[2 * i + 1].fY));
137247ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                    segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fX));
137347ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                    segmentData.push(SkScalarToFLOAT(quads[2 * i + 2].fY));
137447ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                }
137547ef4d5d934bba86848aa238efab21f54a160c1ahalcanary                break;
137647ef4d5d934bba86848aa238efab21f54a160c1ahalcanary            }
1377b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            case SkPath::kClose_Verb:
1378b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // we ignore these, and just get the whole segment from
1379b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                // the corresponding line/quad/cubic verbs
1380b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1381b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            default:
1382330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com                SkDEBUGFAIL("unexpected verb");
1383b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                break;
1384b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1385b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
138649f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsFigure.get()) {
1387b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(close_figure(segmentTypes, segmentStrokes, segmentData,
1388b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        stroke, fill,
1389b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        xpsFigure.get(), xpsFigures));
1390b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1391b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1392b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1393b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1394b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::convertToPpm(const SkMaskFilter* filter,
1395b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               SkMatrix* matrix,
1396b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               SkVector* ppuScale,
1397b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkIRect& clip, SkIRect* clipIRect) {
1398b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //This action is in unit space, but the ppm is specified in physical space.
139980ea19ca4bdd68c1493666a5fe7e4ce9d43ded8breed    ppuScale->set(fCurrentPixelsPerMeter.fX / fCurrentUnitsPerMeter.fX,
140080ea19ca4bdd68c1493666a5fe7e4ce9d43ded8breed                  fCurrentPixelsPerMeter.fY / fCurrentUnitsPerMeter.fY);
140174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1402b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    matrix->postScale(ppuScale->fX, ppuScale->fY);
140374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1404b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkIRect& irect = clip;
1405b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkRect clipRect = SkRect::MakeLTRB(
1406b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fLeft), ppuScale->fX),
1407b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fTop), ppuScale->fY),
1408b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fRight), ppuScale->fX),
1409b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkScalarMul(SkIntToScalar(irect.fBottom), ppuScale->fY));
1410b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    clipRect.roundOut(clipIRect);
1411b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1412b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1413b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::applyMask(const SkDraw& d,
1414b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkMask& mask,
1415b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkVector& ppuScale,
1416b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               IXpsOMPath* shadedPath) {
1417b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the geometry object.
1418b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1419b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedPath->GetGeometry(&shadedGeometry),
1420b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get mask shaded geometry.");
142174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1422b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figures from the geometry.
1423b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1424b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedGeometry->GetFigures(&shadedFigures),
1425b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get mask shaded figures.");
142674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1427b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMatrix m;
1428b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    m.reset();
1429b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    m.setTranslate(SkIntToScalar(mask.fBounds.fLeft),
1430b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                   SkIntToScalar(mask.fBounds.fTop));
1431b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    m.postScale(SkScalarInvert(ppuScale.fX), SkScalarInvert(ppuScale.fY));
143274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1433b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkShader::TileMode xy[2];
1434b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xy[0] = (SkShader::TileMode)3;
1435b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xy[1] = (SkShader::TileMode)3;
143674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1437b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkBitmap bm;
1438a3264e53ee3f3c5d6a2c813df7e44b5b96d207f2commit-bot@chromium.org    bm.installMaskPixels(mask);
143974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1440b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMTileBrush> maskBrush;
1441b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsImageBrush(bm, m, xy, 0xFF, &maskBrush));
1442b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedPath->SetOpacityMaskBrushLocal(maskBrush.get()),
1443b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set mask.");
144474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1445b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkRect universeRect = SkRect::MakeLTRB(0, 0,
1446b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->fCurrentCanvasSize.fWidth, this->fCurrentCanvasSize.fHeight);
1447b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> shadedFigure;
1448b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->createXpsRect(universeRect, FALSE, TRUE, &shadedFigure),
1449b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create mask shaded figure.");
1450b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(shadedFigures->Append(shadedFigure.get()),
1451b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not add mask shaded figure.");
145274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1453b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->clip(shadedPath, d));
145474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1455b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the path to the active visual collection.
1456b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1457b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1458b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get mask current visuals.");
1459b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(currentVisuals->Append(shadedPath),
1460b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not add masked shaded path to current visuals.");
146174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1462b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1463b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1464b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1465b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::shadePath(IXpsOMPath* shadedPath,
1466b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkPaint& shaderPaint,
1467b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkMatrix& matrix,
1468b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               BOOL* fill, BOOL* stroke) {
1469b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    *fill = FALSE;
1470b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    *stroke = FALSE;
147174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1472b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkPaint::Style style = shaderPaint.getStyle();
1473b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool hasFill = SkPaint::kFill_Style == style
1474b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                      || SkPaint::kStrokeAndFill_Style == style;
1475b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const bool hasStroke = SkPaint::kStroke_Style == style
1476b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        || SkPaint::kStrokeAndFill_Style == style;
147774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1478b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO(bungeman): use dictionaries and lookups.
1479b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (hasFill) {
1480b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        *fill = TRUE;
1481b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMBrush> fillBrush;
1482b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsBrush(shaderPaint, &fillBrush, &matrix));
1483b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(shadedPath->SetFillBrushLocal(fillBrush.get()),
1484b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set fill for shaded path.");
1485b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
148674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1487b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (hasStroke) {
1488b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        *stroke = TRUE;
1489b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMBrush> strokeBrush;
1490b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsBrush(shaderPaint, &strokeBrush, &matrix));
1491b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(shadedPath->SetStrokeBrushLocal(strokeBrush.get()),
1492b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set stroke brush for shaded path.");
1493b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(shadedPath->SetStrokeThickness(
1494b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                SkScalarToFLOAT(shaderPaint.getStrokeWidth())),
1495b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set shaded path stroke thickness.");
149674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1497b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (0 == shaderPaint.getStrokeWidth()) {
1498b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //XPS hair width is a hack. (XPS Spec 11.6.12).
1499b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkTScopedComPtr<IXpsOMDashCollection> dashes;
1500b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(shadedPath->GetStrokeDashes(&dashes),
1501b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                "Could not set dashes for shaded path.");
1502b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            XPS_DASH dash;
1503b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            dash.length = 1.0;
1504b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            dash.gap = 0.0;
1505b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(dashes->Append(&dash), "Could not add dashes to shaded path.");
1506b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(shadedPath->SetStrokeDashOffset(-2.0),
1507b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                "Could not set dash offset for shaded path.");
1508b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1509b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1510b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1511b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1512b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1513b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawPath(const SkDraw& d,
1514b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkPath& platonicPath,
15155dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                           const SkPaint& origPaint,
1516b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkMatrix* prePathMatrix,
1517b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           bool pathIsMutable) {
15185dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
15195dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com
1520b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    // nothing to draw
1521b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (d.fClip->isEmpty() ||
15225dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
1523b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1524b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
152574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1526b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath modifiedPath;
15275dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    const bool paintHasPathEffect = paint->getPathEffect()
15285dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                                 || paint->getStyle() != SkPaint::kFill_Style;
152974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1530b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Apply pre-path matrix [Platonic-path -> Skeletal-path].
1531b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMatrix matrix = *d.fMatrix;
1532b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath* skeletalPath = const_cast<SkPath*>(&platonicPath);
1533b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (prePathMatrix) {
15345dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        if (paintHasPathEffect || paint->getRasterizer()) {
1535b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (!pathIsMutable) {
1536b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                skeletalPath = &modifiedPath;
1537b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                pathIsMutable = true;
1538b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1539b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            platonicPath.transform(*prePathMatrix, skeletalPath);
1540b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
154192362383a4de7b0d819c88fa8b74242bb2507602commit-bot@chromium.org            matrix.preConcat(*prePathMatrix);
1542b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1543b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
154474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1545b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Apply path effect [Skeletal-path -> Fillable-path].
1546b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath* fillablePath = skeletalPath;
1547b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (paintHasPathEffect) {
1548b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (!pathIsMutable) {
1549b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            fillablePath = &modifiedPath;
1550b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            pathIsMutable = true;
1551b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
15525dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        bool fill = paint->getFillPath(*skeletalPath, fillablePath);
155374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
15545dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        SkPaint* writablePaint = paint.writable();
15555dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        writablePaint->setPathEffect(NULL);
1556b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (fill) {
15575dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            writablePaint->setStyle(SkPaint::kFill_Style);
1558b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
15595dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            writablePaint->setStyle(SkPaint::kStroke_Style);
15605dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            writablePaint->setStrokeWidth(0);
1561b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1562b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
156374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1564b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded path. This will be the path which is painted.
1565b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> shadedPath;
1566b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1567b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded path for path.");
156874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1569b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the geometry for the shaded path.
1570b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1571b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1572b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create shaded geometry for path.");
157374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1574b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the geometry to the shaded path.
1575b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1576b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add the shaded geometry to shaded path.");
157774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
15785dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkRasterizer* rasterizer = paint->getRasterizer();
15795dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com    SkMaskFilter* filter = paint->getMaskFilter();
1580d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com
1581d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    //Determine if we will draw or shade and mask.
1582d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    if (rasterizer || filter) {
15835dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com        if (paint->getStyle() != SkPaint::kFill_Style) {
15845dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com            paint.writable()->setStyle(SkPaint::kFill_Style);
1585d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com        }
1586d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    }
1587d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com
1588b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Set the brushes.
1589b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL fill;
1590b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    BOOL stroke;
1591b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->shadePath(shadedPath.get(),
15925dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                        *paint,
1593b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        *d.fMatrix,
1594b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &fill,
1595b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &stroke));
159674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1597b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Rasterizer
1598d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com    if (rasterizer) {
1599b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkIRect clipIRect;
1600b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkVector ppuScale;
1601b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->convertToPpm(filter,
1602b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &matrix,
1603b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &ppuScale,
1604b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           d.fClip->getBounds(),
1605b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &clipIRect);
160674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1607b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask* mask = NULL;
160874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1609b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Fillable-path -> Mask]
1610b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask rasteredMask;
1611d998cbdbb73ac11b10081a640e0a7975ac05262cbungeman@google.com        if (rasterizer->rasterize(
1612b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                *fillablePath,
1613b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                matrix,
1614b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &clipIRect,
1615b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filter,  //just to compute how much to draw.
1616b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                &rasteredMask,
1617b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
161874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1619b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1620b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            mask = &rasteredMask;
162174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1622b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //[Mask -> Mask]
1623b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkMask filteredMask;
1624b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (filter &&
1625b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filter->filterMask(&filteredMask, *mask, *d.fMatrix, NULL)) {
162674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1627b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                mask = &filteredMask;
1628b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            } else {
1629b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filteredMask.fImage = NULL;
1630b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1631b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
163274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1633b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //Draw mask.
1634b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1635b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1636b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1637b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
163874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1639b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Mask filter
1640b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (filter) {
1641b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkIRect clipIRect;
1642b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkVector ppuScale;
1643b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->convertToPpm(filter,
1644b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &matrix,
1645b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &ppuScale,
1646b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           d.fClip->getBounds(),
1647b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           &clipIRect);
164874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1649b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Fillable-path -> Pixel-path]
1650b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath* pixelPath = pathIsMutable ? fillablePath : &modifiedPath;
1651b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        fillablePath->transform(matrix, pixelPath);
165274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1653b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask* mask = NULL;
165474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1655b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Pixel-path -> Mask]
1656b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkMask rasteredMask;
1657b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (SkDraw::DrawToMask(
1658b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        *pixelPath,
1659b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &clipIRect,
1660b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        filter,  //just to compute how much to draw.
1661b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &matrix,
1662b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                        &rasteredMask,
16632ac4ef5e6e0c9c95c9200408ba25a95ca758eac2junov@chromium.org                        SkMask::kComputeBoundsAndRenderImage_CreateMode,
16645dc26b97366934ba0f896cea02a3fec027d5d5c1bsalomon@google.com                        paint->getStyle())) {
166574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1666b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage rasteredAmi(rasteredMask.fImage);
1667b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            mask = &rasteredMask;
166874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1669b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //[Mask -> Mask]
1670b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkMask filteredMask;
1671b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (filter->filterMask(&filteredMask,
1672b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   rasteredMask,
1673b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   matrix,
1674b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   NULL)) {
1675b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                mask = &filteredMask;
1676b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            } else {
1677b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                filteredMask.fImage = NULL;
1678b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1679b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkAutoMaskFreeImage filteredAmi(filteredMask.fImage);
168074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1681b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            //Draw mask.
1682b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRV(this->applyMask(d, *mask, ppuScale, shadedPath.get()));
1683b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1684b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1685b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
168674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1687b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figures from the shaded geometry.
1688b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1689b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->GetFigures(&shadedFigures),
1690b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get shaded figures for shaded path.");
169174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1692b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    bool xpsTransformsPath = true;
169374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1694b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Set the fill rule.
169576db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman    SkPath* xpsCompatiblePath = fillablePath;
1696b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_FILL_RULE xpsFillRule;
169776db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman    switch (fillablePath->getFillType()) {
1698b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kWinding_FillType:
1699b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsFillRule = XPS_FILL_RULE_NONZERO;
1700b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1701b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kEvenOdd_FillType:
1702b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsFillRule = XPS_FILL_RULE_EVENODD;
1703b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1704b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kInverseWinding_FillType: {
170576db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            //[Fillable-path (inverse winding) -> XPS-path (inverse even odd)]
170676db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            if (!pathIsMutable) {
170776db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                xpsCompatiblePath = &modifiedPath;
170876db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                pathIsMutable = true;
170976db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            }
171076db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            if (!Simplify(*fillablePath, xpsCompatiblePath)) {
171176db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                SkDEBUGF(("Could not simplify inverse winding path."));
171276db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman                return;
171376db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman            }
1714b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
171576db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman        // The xpsCompatiblePath is noW inverse even odd, so fall through.
1716b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPath::kInverseEvenOdd_FillType: {
1717b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            const SkRect universe = SkRect::MakeLTRB(
1718b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                0, 0,
1719b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                this->fCurrentCanvasSize.fWidth,
1720b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                this->fCurrentCanvasSize.fHeight);
1721b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkTScopedComPtr<IXpsOMGeometryFigure> addOneFigure;
1722b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRV(this->createXpsRect(universe, FALSE, TRUE, &addOneFigure));
1723b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRVM(shadedFigures->Append(addOneFigure.get()),
1724b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                 "Could not add even-odd flip figure to shaded path.");
1725b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsTransformsPath = false;
1726b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsFillRule = XPS_FILL_RULE_EVENODD;
1727b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
1728b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1729b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        default:
1730330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com            SkDEBUGFAIL("Unknown SkPath::FillType.");
1731b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1732b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->SetFillRule(xpsFillRule),
1733b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set fill rule for shaded path.");
173474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1735b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the XPS transform, if possible.
1736b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (xpsTransformsPath) {
1737b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1738b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsTransform(matrix, &xpsTransform));
173974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1740b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (xpsTransform.get()) {
1741b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1742b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                 "Could not set transform on shaded path.");
1743b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
1744b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            xpsTransformsPath = false;
1745b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1746b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
174774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
174876db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman    SkPath* devicePath = xpsCompatiblePath;
1749b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!xpsTransformsPath) {
1750b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //[Fillable-path -> Device-path]
175176db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman        devicePath = pathIsMutable ? xpsCompatiblePath : &modifiedPath;
175276db31a2ca11da4dd45016e964b2e49f39f3dd49bungeman        xpsCompatiblePath->transform(matrix, devicePath);
1753b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1754b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->addXpsPathGeometry(shadedFigures.get(),
1755b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                 stroke, fill, *devicePath));
175674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1757b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->clip(shadedPath.get(), d));
175874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1759b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the path to the active visual collection.
1760b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1761b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1762b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for shaded path.");
1763b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(shadedPath.get()),
1764b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add shaded path to current visuals.");
1765b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1766b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1767b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::clip(IXpsOMVisual* xpsVisual, const SkDraw& d) {
1768b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkPath clipPath;
1769b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkAssertResult(d.fClip->getBoundaryPath(&clipPath));
177074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1771b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return this->clipToPath(xpsVisual, clipPath, XPS_FILL_RULE_EVENODD);
1772b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1773b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::clipToPath(IXpsOMVisual* xpsVisual,
1774b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                const SkPath& clipPath,
1775b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                XPS_FILL_RULE fillRule) {
1776b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the geometry.
1777b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> clipGeometry;
1778b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateGeometry(&clipGeometry),
1779b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create clip geometry.");
178074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1781b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the figure collection of the geometry.
1782b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> clipFigures;
1783b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(clipGeometry->GetFigures(&clipFigures),
1784b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not get the clip figures.");
178574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1786b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the figures into the geometry.
1787b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->addXpsPathGeometry(
1788b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        clipFigures.get(),
1789b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        FALSE, TRUE, clipPath));
179074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1791b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(clipGeometry->SetFillRule(fillRule),
1792b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set fill rule.");
1793b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(xpsVisual->SetClipGeometryLocal(clipGeometry.get()),
1794b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set clip geometry.");
179574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1796b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1797b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1798b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1799b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawBitmap(const SkDraw& d, const SkBitmap& bitmap,
1800b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkMatrix& matrix, const SkPaint& paint) {
1801b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (d.fClip->isEmpty()) {
1802b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
1803b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
180474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1805b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkIRect srcRect;
18069bf380ce7f848dfb5886dd52b82746521454b739robertphillips@google.com    srcRect.set(0, 0, bitmap.width(), bitmap.height());
180774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1808b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the new shaded path.
1809b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMPath> shadedPath;
1810b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreatePath(&shadedPath),
1811b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create path for bitmap.");
181274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1813b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Create the shaded geometry.
1814b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometry> shadedGeometry;
1815b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateGeometry(&shadedGeometry),
1816b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create geometry for bitmap.");
181774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1818b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Add the shaded geometry to the shaded path.
1819b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedPath->SetGeometryLocal(shadedGeometry.get()),
1820b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set the geometry for bitmap.");
182174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1822b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the shaded figures from the shaded geometry.
1823b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigureCollection> shadedFigures;
1824b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedGeometry->GetFigures(&shadedFigures),
1825b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get the figures for bitmap.");
182674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1827b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkMatrix transform = matrix;
1828b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    transform.postConcat(*d.fMatrix);
182974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1830b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
1831b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->createXpsTransform(transform, &xpsTransform));
1832b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (xpsTransform.get()) {
1833b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRVM(shadedGeometry->SetTransformLocal(xpsTransform.get()),
1834b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             "Could not set transform for bitmap.");
1835b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
1836b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: perspective that bitmap!
1837b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
183874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1839b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGeometryFigure> rectFigure;
184049f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsTransform.get()) {
1841b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkShader::TileMode xy[2] = {
1842b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode,
1843b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkShader::kClamp_TileMode,
1844b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        };
1845b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMTileBrush> xpsImageBrush;
18469bf380ce7f848dfb5886dd52b82746521454b739robertphillips@google.com        HRV(this->createXpsImageBrush(bitmap,
1847b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      transform,
1848b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      xy,
1849b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      paint.getAlpha(),
1850b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                      &xpsImageBrush));
1851b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRVM(shadedPath->SetFillBrushLocal(xpsImageBrush.get()),
1852b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             "Could not set bitmap brush.");
185374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1854b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const SkRect bitmapRect = SkRect::MakeLTRB(0, 0,
1855b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1856b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRV(this->createXpsRect(bitmapRect, FALSE, TRUE, &rectFigure));
1857b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
1858b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(shadedFigures->Append(rectFigure.get()),
1859b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add bitmap figure.");
186074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1861b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the current visual collection and add the shaded path to it.
1862b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
1863b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
1864b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for bitmap");
1865b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(shadedPath.get()),
1866b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add bitmap to current visuals.");
186774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1868b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(this->clip(shadedPath.get(), d));
1869b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1870b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1871b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap,
1872b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             int x, int y,
1873b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkPaint& paint) {
1874b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: override this for XPS
1875b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDEBUGF(("XPS drawSprite not yet implemented."));
1876b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1877b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1878b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::CreateTypefaceUse(const SkPaint& paint,
1879b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                       TypefaceUse** typefaceUse) {
1880398de9ad9cb1ed3aa97aaf023d7f8287b8e62601reed@google.com    SkAutoResolveDefaultTypeface typeface(paint.getTypeface());
188174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1882b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Check cache.
1883398de9ad9cb1ed3aa97aaf023d7f8287b8e62601reed@google.com    const SkFontID typefaceID = typeface->uniqueID();
1884b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!this->fTypefaces.empty()) {
1885b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        TypefaceUse* current = &this->fTypefaces.front();
1886b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const TypefaceUse* last = &this->fTypefaces.back();
1887b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        for (; current <= last; ++current) {
1888b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            if (current->typefaceId == typefaceID) {
1889b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                *typefaceUse = current;
1890b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                return S_OK;
1891b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
1892b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1893b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
189474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1895b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //TODO: create glyph only fonts
1896b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //and let the host deal with what kind of font we're looking at.
1897b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_FONT_EMBEDDING embedding = XPS_FONT_EMBEDDING_RESTRICTED;
189874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1899b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IStream> fontStream;
1900b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    int ttcIndex;
1901b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    SkStream* fontData = typeface->openStream(&ttcIndex);
1902635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com    //TODO: cannot handle FON fonts.
19038cddc8d0ea02b3476fd57668cf9b25ba5b9770d8bungeman@google.com    HRM(SkIStream::CreateFromSkStream(fontData, true, &fontStream),
1904b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create font stream.");
190574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1906b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const size_t size =
1907b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_ARRAY_COUNT(L"/Resources/Fonts/" L_GUID_ID L".odttf");
1908b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t buffer[size];
1909b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    wchar_t id[GUID_ID_LEN];
1910da5a1b8cd80bbea1dfdb474c6917a1f13a0e7f9eBen Wagner    HR(this->createId(id, GUID_ID_LEN));
1911b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    swprintf_s(buffer, size, L"/Resources/Fonts/%s.odttf", id);
191274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1913b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IOpcPartUri> partUri;
1914b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreatePartUri(buffer, &partUri),
1915b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create font resource part uri.");
191674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1917b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMFontResource> xpsFontResource;
1918b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(this->fXpsFactory->CreateFontResource(fontStream.get(),
1919b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              embedding,
1920b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              partUri.get(),
1921b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              FALSE,
1922b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                              &xpsFontResource),
1923b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not create font resource.");
192474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1925b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    //TODO: change openStream to return -1 for non-ttc, get rid of this.
1926b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    uint8_t* data = (uint8_t*)fontData->getMemoryBase();
1927b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    bool isTTC = (data &&
1928b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                  fontData->getLength() >= sizeof(SkTTCFHeader) &&
1929b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                  ((SkTTCFHeader*)data)->ttcTag == SkTTCFHeader::TAG);
1930b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org
1931b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    TypefaceUse& newTypefaceUse = this->fTypefaces.push_back();
1932b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.typefaceId = typefaceID;
1933b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    newTypefaceUse.ttcIndex = isTTC ? ttcIndex : -1;
1934b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.fontData = fontData;
1935b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.xpsFont = xpsFontResource.release();
193674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1937cf4d0fb8bb5628c655ee97ce098d2fd3fd2bb448mtklein@google.com    SkAutoGlyphCache agc(paint, NULL, &SkMatrix::I());
1938b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkGlyphCache* glyphCache = agc.getCache();
1939b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    unsigned int glyphCount = glyphCache->getGlyphCount();
1940b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    newTypefaceUse.glyphsUsed = new SkBitSet(glyphCount);
194174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1942b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    *typefaceUse = &newTypefaceUse;
1943b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
1944b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
1945b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
1946b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comHRESULT SkXPSDevice::AddGlyphs(const SkDraw& d,
1947b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               IXpsOMObjectFactory* xpsFactory,
1948b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               IXpsOMCanvas* canvas,
1949b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                               TypefaceUse* font,
1950b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               LPCWSTR text,
1951b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               XPS_GLYPH_INDEX* xpsGlyphs,
1952b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               UINT32 xpsGlyphsLen,
1953b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               XPS_POINT *origin,
1954b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               FLOAT fontSize,
1955b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               XPS_STYLE_SIMULATION sims,
1956b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkMatrix& transform,
1957b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                               const SkPaint& paint) {
1958b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGlyphs> glyphs;
1959b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    HRM(xpsFactory->CreateGlyphs(font->xpsFont, &glyphs), "Could not create glyphs.");
1960b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org    HRM(glyphs->SetFontFaceIndex(font->ttcIndex), "Could not set glyph font face index.");
196174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1962b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //XPS uses affine transformations for everything...
1963b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //...except positioning text.
1964b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    bool useCanvasForClip;
1965b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if ((transform.getType() & ~SkMatrix::kTranslate_Mask) == 0) {
1966b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        origin->x += SkScalarToFLOAT(transform.getTranslateX());
1967b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        origin->y += SkScalarToFLOAT(transform.getTranslateY());
1968b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        useCanvasForClip = false;
1969b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
1970b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMMatrixTransform> xpsMatrixToUse;
1971b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->createXpsTransform(transform, &xpsMatrixToUse));
1972b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (xpsMatrixToUse.get()) {
1973b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            HRM(glyphs->SetTransformLocal(xpsMatrixToUse.get()),
1974b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                "Could not set transform matrix.");
1975b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            useCanvasForClip = true;
1976b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        } else {
1977330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com            SkDEBUGFAIL("Attempt to add glyphs in perspective.");
1978b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            useCanvasForClip = false;
1979b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
1980b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
198174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1982b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMGlyphsEditor> glyphsEditor;
1983b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->GetGlyphsEditor(&glyphsEditor), "Could not get glyph editor.");
198474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
198549f085dddff10473b6ebf832a974288300224e60bsalomon    if (text) {
1986b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphsEditor->SetUnicodeString(text),
1987b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set unicode string.");
1988b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
198974b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
199049f085dddff10473b6ebf832a974288300224e60bsalomon    if (xpsGlyphs) {
1991b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphsEditor->SetGlyphIndices(xpsGlyphsLen, xpsGlyphs),
1992b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not set glyphs.");
1993b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
199474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1995b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphsEditor->ApplyEdits(), "Could not apply glyph edits.");
199674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
1997b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMBrush> xpsFillBrush;
1998b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HR(this->createXpsBrush(
1999b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            paint,
2000b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            &xpsFillBrush,
2001b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            useCanvasForClip ? NULL : &transform));
200274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2003b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetFillBrushLocal(xpsFillBrush.get()),
2004b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set fill brush.");
200574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2006b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetOrigin(origin), "Could not set glyph origin.");
200774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2008b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetFontRenderingEmSize(fontSize),
2009b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set font size.");
201074b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2011b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(glyphs->SetStyleSimulations(sims),
2012b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        "Could not set style simulations.");
201374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2014b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> visuals;
2015b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRM(canvas->GetVisuals(&visuals), "Could not get glyph canvas visuals.");
201674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2017b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (!useCanvasForClip) {
2018b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->clip(glyphs.get(), d));
2019b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(visuals->Append(glyphs.get()), "Could not add glyphs to canvas.");
2020b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
2021b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMCanvas> glyphCanvas;
2022b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(this->fXpsFactory->CreateCanvas(&glyphCanvas),
2023b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not create glyph canvas.");
202474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2025b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkTScopedComPtr<IXpsOMVisualCollection> glyphCanvasVisuals;
2026b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphCanvas->GetVisuals(&glyphCanvasVisuals),
2027b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not get glyph visuals collection.");
202874b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2029b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(glyphCanvasVisuals->Append(glyphs.get()),
2030b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add glyphs to page.");
2031b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HR(this->clip(glyphCanvas.get(), d));
203274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2033b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        HRM(visuals->Append(glyphCanvas.get()),
2034b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            "Could not add glyph canvas to page.");
2035b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
203674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2037b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return S_OK;
2038b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2039b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2040b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstruct SkXPSDrawProcs : public SkDrawProcs {
2041b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.compublic:
2042b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    /** [in] Advance width and offsets for glyphs measured in
2043b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    hundredths of the font em size (XPS Spec 5.1.3). */
2044b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    FLOAT centemPerUnit;
2045b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    /** [in,out] The accumulated glyphs used in the current typeface. */
2046b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkBitSet* glyphUse;
2047b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    /** [out] The glyphs to draw. */
2048b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTDArray<XPS_GLYPH_INDEX> xpsGlyphs;
2049b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com};
2050b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2051b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic void xps_draw_1_glyph(const SkDraw1Glyph& state,
205279738cc7bf12d212bef4ff80591d1bf6f383663dbungeman                             Sk48Dot16 fx, Sk48Dot16 fy,
2053b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkGlyph& skGlyph) {
2054b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkASSERT(skGlyph.fWidth > 0 && skGlyph.fHeight > 0);
205574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2056b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDrawProcs* procs = static_cast<SkXPSDrawProcs*>(state.fDraw->fProcs);
20572211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com
20582211b623274e24d0025763cfa855c9eb53d5b900bungeman@google.com    //Draw pre-adds half the sampling frequency for floor rounding.
205979738cc7bf12d212bef4ff80591d1bf6f383663dbungeman    SkScalar x = Sk48Dot16ToScalar(fx) - state.fHalfSampleX;
206079738cc7bf12d212bef4ff80591d1bf6f383663dbungeman    SkScalar y = Sk48Dot16ToScalar(fy) - state.fHalfSampleY;
206174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2062b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_GLYPH_INDEX* xpsGlyph = procs->xpsGlyphs.append();
2063b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    uint16_t glyphID = skGlyph.getGlyphID();
2064b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs->glyphUse->setBit(glyphID, true);
2065b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    xpsGlyph->index = glyphID;
2066b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (1 == procs->xpsGlyphs.count()) {
2067b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->advanceWidth = 0.0f;
206879738cc7bf12d212bef4ff80591d1bf6f383663dbungeman        xpsGlyph->horizontalOffset = SkScalarToFloat(x) * procs->centemPerUnit;
206979738cc7bf12d212bef4ff80591d1bf6f383663dbungeman        xpsGlyph->verticalOffset = SkScalarToFloat(y) * -procs->centemPerUnit;
2070b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    } else {
2071b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        const XPS_GLYPH_INDEX& first = procs->xpsGlyphs[0];
2072b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        xpsGlyph->advanceWidth = 0.0f;
207379738cc7bf12d212bef4ff80591d1bf6f383663dbungeman        xpsGlyph->horizontalOffset = (SkScalarToFloat(x) * procs->centemPerUnit)
2074b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                     - first.horizontalOffset;
207579738cc7bf12d212bef4ff80591d1bf6f383663dbungeman        xpsGlyph->verticalOffset = (SkScalarToFloat(y) * -procs->centemPerUnit)
2076b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                                   - first.verticalOffset;
2077b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
2078b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2079b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2080b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic void text_draw_init(const SkPaint& paint,
2081b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const void* text, size_t byteLength,
2082b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           SkBitSet& glyphsUsed,
2083b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           SkDraw& myDraw, SkXPSDrawProcs& procs) {
2084b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.fD1GProc = xps_draw_1_glyph;
20850aa5cea8694d3686b6742a36eab81ab9001de954bsalomon    int numGlyphGuess;
2086b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    switch (paint.getTextEncoding()) {
2087b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPaint::kUTF8_TextEncoding:
2088b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            numGlyphGuess = SkUTF8_CountUnichars(
2089b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<const char *>(text),
2090b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                byteLength);
2091b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
2092b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPaint::kUTF16_TextEncoding:
2093b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            numGlyphGuess = SkUTF16_CountUnichars(
2094b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                static_cast<const uint16_t *>(text),
20950aa5cea8694d3686b6742a36eab81ab9001de954bsalomon                SkToInt(byteLength));
2096b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
2097b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        case SkPaint::kGlyphID_TextEncoding:
20980aa5cea8694d3686b6742a36eab81ab9001de954bsalomon            numGlyphGuess = SkToInt(byteLength / 2);
2099b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            break;
2100b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        default:
210145d1d1d9a71253e0ed28ba87f5f946b5845f1d6dcommit-bot@chromium.org            SK_ALWAYSBREAK(true);
2102b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
2103b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs.setReserve(numGlyphGuess);
2104b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.glyphUse = &glyphsUsed;
2105b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.centemPerUnit = 100.0f / SkScalarToFLOAT(paint.getTextSize());
210674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2107b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    myDraw.fProcs = &procs;
2108b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2109b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2110b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comstatic bool text_must_be_pathed(const SkPaint& paint, const SkMatrix& matrix) {
2111b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    const SkPaint::Style style = paint.getStyle();
2112b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return matrix.hasPerspective()
2113b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || SkPaint::kStroke_Style == style
2114b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || SkPaint::kStrokeAndFill_Style == style
2115b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || paint.getMaskFilter()
2116b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        || paint.getRasterizer()
2117b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    ;
2118b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2119b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2120b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawText(const SkDraw& d,
2121b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const void* text, size_t byteLen,
2122b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           SkScalar x, SkScalar y,
2123b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                           const SkPaint& paint) {
2124b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (byteLen < 1) return;
212574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2126b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (text_must_be_pathed(paint, *d.fMatrix)) {
2127b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath path;
2128b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        paint.getTextPath(text, byteLen, x, y, &path);
2129b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        this->drawPath(d, path, paint, NULL, true);
2130b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: add automation "text"
2131b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
2132b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
213374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2134b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    TypefaceUse* typeface;
2135b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(CreateTypefaceUse(paint, &typeface));
213674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2137b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDraw myDraw(d);
2138635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com    myDraw.fMatrix = &SkMatrix::I();
2139b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDrawProcs procs;
2140b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    text_draw_init(paint, text, byteLen, *typeface->glyphsUsed, myDraw, procs);
214174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2142b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    myDraw.drawText(static_cast<const char*>(text), byteLen, x, y, paint);
2143089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com
2144089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    // SkDraw may have clipped out the glyphs, so we need to check
2145089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    if (procs.xpsGlyphs.count() == 0) {
2146089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com        return;
2147089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com    }
2148089130c24e29dd0ffc53f0396acdc8f0b50dabd4reed@google.com
2149b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_POINT origin = {
2150b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        procs.xpsGlyphs[0].horizontalOffset / procs.centemPerUnit,
2151b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        procs.xpsGlyphs[0].verticalOffset / -procs.centemPerUnit,
2152b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
2153b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs[0].horizontalOffset = 0.0f;
2154b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    procs.xpsGlyphs[0].verticalOffset = 0.0f;
215574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2156b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(AddGlyphs(d,
2157b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  this->fXpsFactory.get(),
2158b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  this->fCurrentXpsCanvas.get(),
2159b5e34e22aa0e019e25f9f913f0e119a9a97e5562commit-bot@chromium.org                  typeface,
2160b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  NULL,
2161b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  procs.xpsGlyphs.begin(), procs.xpsGlyphs.count(),
2162b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  &origin,
2163b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  SkScalarToFLOAT(paint.getTextSize()),
2164b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  XPS_STYLE_SIMULATION_NONE,
2165b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  *d.fMatrix,
2166b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                  paint));
2167b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2168b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2169b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comvoid SkXPSDevice::drawPosText(const SkDraw& d,
2170b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                              const void* text, size_t byteLen,
217105c4a4322e7d4f3417b7df33825bab8603d52051fmalita                              const SkScalar pos[], int scalarsPerPos,
217205c4a4322e7d4f3417b7df33825bab8603d52051fmalita                              const SkPoint& offset, const SkPaint& paint) {
2173b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (byteLen < 1) return;
217474b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2175b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    if (text_must_be_pathed(paint, *d.fMatrix)) {
2176b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SkPath path;
2177b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: make this work, Draw currently does not handle as well.
2178b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //paint.getTextPath(text, byteLength, x, y, &path);
2179b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //this->drawPath(d, path, paint, NULL, true);
2180b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //TODO: add automation "text"
2181b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        return;
2182b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
218374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2184b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    TypefaceUse* typeface;
2185b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRV(CreateTypefaceUse(paint, &typeface));
218674b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2187b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkDraw myDraw(d);
2188635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com    myDraw.fMatrix = &SkMatrix::I();
2189b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDrawProcs procs;
2190b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    text_draw_init(paint, text, byteLen, *typeface->glyphsUsed, myDraw, procs);
219174b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
219205c4a4322e7d4f3417b7df33825bab8603d52051fmalita    myDraw.drawPosText(static_cast<const char*>(text), byteLen, pos, scalarsPerPos, offset, 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
22191f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.comvoid SkXPSDevice::drawDevice(const SkDraw& d, SkBaseDevice* dev,
2220b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             int x, int y,
2221b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                             const SkPaint&) {
2222b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkXPSDevice* that = static_cast<SkXPSDevice*>(dev);
222374b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2224b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMMatrixTransform> xpsTransform;
2225b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    XPS_MATRIX rawTransform = {
2226b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        1.0f,
2227b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0.0f,
2228b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        0.0f,
2229b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        1.0f,
2230b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        static_cast<FLOAT>(x),
2231b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        static_cast<FLOAT>(y),
2232b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    };
2233b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateMatrixTransform(&rawTransform, &xpsTransform),
2234b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create layer transform.");
2235b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(that->fCurrentXpsCanvas->SetTransformLocal(xpsTransform.get()),
2236b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not set layer transform.");
223774b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2238b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    //Get the current visual collection and add the layer to it.
2239b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    SkTScopedComPtr<IXpsOMVisualCollection> currentVisuals;
2240b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fCurrentXpsCanvas->GetVisuals(&currentVisuals),
2241b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not get current visuals for layer.");
2242b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(currentVisuals->Append(that->fCurrentXpsCanvas.get()),
2243b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not add layer to current visuals.");
2244b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2245b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
224676033be81b82c44fd5d4fdf2672eb22e505da1f0reedSkBaseDevice* SkXPSDevice::onCreateDevice(const CreateInfo& info, const SkPaint*) {
2247635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com//Conditional for bug compatibility with PDF device.
2248635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com#if 0
22496987dcaf257dd7c2c8e0014cf7452fde82bcba5bfmalita    if (SkBaseDevice::kGeneral_Usage == info.fUsage) {
2250b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com        return NULL;
2251b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        SK_CRASH();
2252b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //To what stream do we write?
2253b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //SkXPSDevice* dev = new SkXPSDevice(this);
2254b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //SkSize s = SkSize::Make(width, height);
2255b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //dev->BeginCanvas(s, s, SkMatrix::I());
2256b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        //return dev;
2257b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
2258635091f0a98b7d86a74baff11ae5e304062d8420bungeman@google.com#endif
2259b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    return new SkXPSDevice(this->fXpsFactory.get());
2260b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2261b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
2262b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comSkXPSDevice::SkXPSDevice(IXpsOMObjectFactory* xpsFactory)
22631f2f338e23789f3eef168dcbd8171a28820ba6c1robertphillips@google.com    : SkBitmapDevice(make_fake_bitmap(10000, 10000))
2264b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    , fCurrentPage(0) {
226574b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2266b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(CoCreateInstance(
2267b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSID_XpsOMObjectFactory,
2268b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             NULL,
2269b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             CLSCTX_INPROC_SERVER,
2270b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com             IID_PPV_ARGS(&this->fXpsFactory)),
2271b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create factory for layer.");
227274b461961607fa57a150a9282c410ef0cab38764vandebo@chromium.org
2273b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    HRVM(this->fXpsFactory->CreateCanvas(&this->fCurrentXpsCanvas),
2274b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com         "Could not create canvas for layer.");
2275b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com}
2276b55deeb1c7c692023603639a9b29c0e3de124eacreed@google.com
2277