bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkXPSDevice_DEFINED |
| 9 | #define SkXPSDevice_DEFINED |
| 10 | |
| 11 | #include "SkTypes.h" |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 12 | |
halcanary | 0cbe7ee | 2015-12-01 09:02:49 -0800 | [diff] [blame] | 13 | #ifdef SK_BUILD_FOR_WIN |
| 14 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 15 | #include <ObjBase.h> |
| 16 | #include <XpsObjectModel.h> |
| 17 | |
| 18 | #include "SkAutoCoInitialize.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 19 | #include "SkBitmapDevice.h" |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 20 | #include "SkBitSet.h" |
| 21 | #include "SkCanvas.h" |
| 22 | #include "SkColor.h" |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 23 | #include "SkPaint.h" |
| 24 | #include "SkPath.h" |
| 25 | #include "SkPoint.h" |
| 26 | #include "SkShader.h" |
| 27 | #include "SkSize.h" |
| 28 | #include "SkTArray.h" |
| 29 | #include "SkTScopedComPtr.h" |
| 30 | #include "SkTypeface.h" |
| 31 | |
Ben Wagner | da5a1b8 | 2014-08-22 15:07:06 -0400 | [diff] [blame] | 32 | //#define SK_XPS_USE_DETERMINISTIC_IDS |
| 33 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 34 | /** \class SkXPSDevice |
| 35 | |
| 36 | The drawing context for the XPS backend. |
| 37 | */ |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 38 | class SkXPSDevice : public SkBaseDevice { |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 39 | public: |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 40 | SK_API SkXPSDevice(SkISize); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 41 | SK_API virtual ~SkXPSDevice(); |
| 42 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 43 | bool beginPortfolio(SkWStream* outputStream); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 44 | /** |
| 45 | @param unitsPerMeter converts geometry units into physical units. |
| 46 | @param pixelsPerMeter resolution to use when geometry must be rasterized. |
| 47 | @param trimSize final page size in physical units. |
| 48 | The top left of the trim is the origin of physical space. |
| 49 | @param mediaBox The size of the physical media in physical units. |
| 50 | The top and left must be less than zero. |
| 51 | The bottom and right must be greater than the trimSize. |
| 52 | The default is to coincide with the trimSize. |
| 53 | @param bleedBox The size of the bleed box in physical units. |
| 54 | Must be contained within the mediaBox. |
| 55 | The default is to coincide with the mediaBox. |
| 56 | @param artBox The size of the content box in physical units. |
| 57 | Must be contained within the trimSize. |
| 58 | The default is to coincide with the trimSize. |
| 59 | @param cropBox The size of the recommended view port in physical units. |
| 60 | Must be contained within the mediaBox. |
| 61 | The default is to coincide with the mediaBox. |
| 62 | */ |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 63 | bool beginSheet( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 64 | const SkVector& unitsPerMeter, |
| 65 | const SkVector& pixelsPerMeter, |
| 66 | const SkSize& trimSize, |
| 67 | const SkRect* mediaBox = NULL, |
| 68 | const SkRect* bleedBox = NULL, |
| 69 | const SkRect* artBox = NULL, |
| 70 | const SkRect* cropBox = NULL); |
| 71 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 72 | bool endSheet(); |
| 73 | bool endPortfolio(); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 74 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 75 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | void drawPaint(const SkDraw&, const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 77 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 78 | void drawPoints( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 79 | const SkDraw&, |
| 80 | SkCanvas::PointMode mode, |
| 81 | size_t count, const SkPoint[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 82 | const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 83 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 84 | void drawRect( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 85 | const SkDraw&, |
| 86 | const SkRect& r, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 87 | const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 88 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 89 | void drawRRect( |
scroggo@google.com | cac8d01 | 2013-11-12 17:10:02 +0000 | [diff] [blame] | 90 | const SkDraw&, |
| 91 | const SkRRect&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 92 | const SkPaint& paint) override; |
scroggo@google.com | cac8d01 | 2013-11-12 17:10:02 +0000 | [diff] [blame] | 93 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 94 | void drawPath( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 95 | const SkDraw&, |
| 96 | const SkPath& platonicPath, |
| 97 | const SkPaint& paint, |
| 98 | const SkMatrix* prePathMatrix, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | bool pathIsMutable) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 100 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 101 | void drawBitmap( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 102 | const SkDraw&, |
| 103 | const SkBitmap& bitmap, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 104 | const SkMatrix& matrix, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 105 | const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 106 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 107 | void drawSprite( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 108 | const SkDraw&, |
| 109 | const SkBitmap& bitmap, |
| 110 | int x, int y, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 111 | const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 112 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 113 | void drawText( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 114 | const SkDraw&, |
| 115 | const void* text, size_t len, |
| 116 | SkScalar x, SkScalar y, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 117 | const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 118 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 119 | void drawPosText( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 120 | const SkDraw&, |
| 121 | const void* text, size_t len, |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 122 | const SkScalar pos[], int scalarsPerPos, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 123 | const SkPoint& offset, const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 124 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 125 | void drawVertices( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 126 | const SkDraw&, |
| 127 | SkCanvas::VertexMode, |
| 128 | int vertexCount, const SkPoint verts[], |
| 129 | const SkPoint texs[], const SkColor colors[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 130 | SkBlendMode, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 131 | const uint16_t indices[], int indexCount, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 132 | const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 133 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 134 | void drawDevice( |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 135 | const SkDraw&, |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 136 | SkBaseDevice* device, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 137 | int x, int y, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 138 | const SkPaint& paint) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 139 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 140 | void drawOval(const SkDraw&, const SkRect&, const SkPaint&) override; |
| 141 | |
| 142 | void drawBitmapRect(const SkDraw&, |
| 143 | const SkBitmap&, |
| 144 | const SkRect*, |
| 145 | const SkRect&, |
| 146 | const SkPaint&, |
| 147 | SkCanvas::SrcRectConstraint) override; |
| 148 | |
| 149 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 150 | private: |
| 151 | class TypefaceUse : ::SkNoncopyable { |
| 152 | public: |
| 153 | SkFontID typefaceId; |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 154 | int ttcIndex; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 155 | SkStream* fontData; |
| 156 | IXpsOMFontResource* xpsFont; |
| 157 | SkBitSet* glyphsUsed; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 158 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 159 | explicit TypefaceUse(); |
| 160 | ~TypefaceUse(); |
| 161 | }; |
halcanary | a634b74 | 2016-10-13 08:44:11 -0700 | [diff] [blame] | 162 | friend HRESULT subset_typeface(TypefaceUse* current); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 163 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 164 | SkXPSDevice(SkISize, IXpsOMObjectFactory*); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 165 | |
| 166 | SkAutoCoInitialize fAutoCo; |
| 167 | SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory; |
| 168 | SkTScopedComPtr<IStream> fOutputStream; |
| 169 | SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter; |
| 170 | |
| 171 | unsigned int fCurrentPage; |
| 172 | SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas; |
| 173 | SkSize fCurrentCanvasSize; |
| 174 | SkVector fCurrentUnitsPerMeter; |
| 175 | SkVector fCurrentPixelsPerMeter; |
| 176 | |
| 177 | SkTArray<TypefaceUse, true> fTypefaces; |
| 178 | |
Ben Wagner | da5a1b8 | 2014-08-22 15:07:06 -0400 | [diff] [blame] | 179 | /** Creates a GUID based id and places it into buffer. |
| 180 | buffer should have space for at least GUID_ID_LEN wide characters. |
| 181 | The string will always be wchar null terminated. |
| 182 | XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0 |
| 183 | The string may begin with a digit, |
| 184 | and so may not be suitable as a bare resource key. |
| 185 | */ |
| 186 | HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-'); |
| 187 | #ifdef SK_XPS_USE_DETERMINISTIC_IDS |
| 188 | decltype(GUID::Data1) fNextId = 0; |
| 189 | #endif |
| 190 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 191 | HRESULT initXpsDocumentWriter(IXpsOMImageResource* image); |
| 192 | |
| 193 | HRESULT createXpsPage( |
| 194 | const XPS_SIZE& pageSize, |
| 195 | IXpsOMPage** page); |
| 196 | |
| 197 | HRESULT createXpsThumbnail( |
| 198 | IXpsOMPage* page, const unsigned int pageNumber, |
| 199 | IXpsOMImageResource** image); |
| 200 | |
| 201 | void internalDrawRect( |
| 202 | const SkDraw&, |
| 203 | const SkRect& r, |
| 204 | bool transformRect, |
| 205 | const SkPaint& paint); |
| 206 | |
| 207 | HRESULT createXpsBrush( |
| 208 | const SkPaint& skPaint, |
| 209 | IXpsOMBrush** xpsBrush, |
| 210 | const SkMatrix* parentTransform = NULL); |
| 211 | |
| 212 | HRESULT createXpsSolidColorBrush( |
| 213 | const SkColor skColor, const SkAlpha alpha, |
| 214 | IXpsOMBrush** xpsBrush); |
| 215 | |
| 216 | HRESULT createXpsImageBrush( |
| 217 | const SkBitmap& bitmap, |
| 218 | const SkMatrix& localMatrix, |
| 219 | const SkShader::TileMode (&xy)[2], |
| 220 | const SkAlpha alpha, |
| 221 | IXpsOMTileBrush** xpsBrush); |
| 222 | |
| 223 | HRESULT createXpsLinearGradient( |
| 224 | SkShader::GradientInfo info, |
| 225 | const SkAlpha alpha, |
| 226 | const SkMatrix& localMatrix, |
| 227 | IXpsOMMatrixTransform* xpsMatrixToUse, |
| 228 | IXpsOMBrush** xpsBrush); |
| 229 | |
| 230 | HRESULT createXpsRadialGradient( |
| 231 | SkShader::GradientInfo info, |
| 232 | const SkAlpha alpha, |
| 233 | const SkMatrix& localMatrix, |
| 234 | IXpsOMMatrixTransform* xpsMatrixToUse, |
| 235 | IXpsOMBrush** xpsBrush); |
| 236 | |
| 237 | HRESULT createXpsGradientStop( |
| 238 | const SkColor skColor, |
| 239 | const SkScalar offset, |
| 240 | IXpsOMGradientStop** xpsGradStop); |
| 241 | |
| 242 | HRESULT createXpsTransform( |
| 243 | const SkMatrix& matrix, |
| 244 | IXpsOMMatrixTransform ** xpsTransform); |
| 245 | |
| 246 | HRESULT createXpsRect( |
| 247 | const SkRect& rect, |
| 248 | BOOL stroke, BOOL fill, |
| 249 | IXpsOMGeometryFigure** xpsRect); |
| 250 | |
| 251 | HRESULT createXpsQuad( |
| 252 | const SkPoint (&points)[4], |
| 253 | BOOL stroke, BOOL fill, |
| 254 | IXpsOMGeometryFigure** xpsQuad); |
| 255 | |
| 256 | HRESULT CreateTypefaceUse( |
| 257 | const SkPaint& paint, |
| 258 | TypefaceUse** fontResource); |
| 259 | |
| 260 | HRESULT AddGlyphs( |
| 261 | const SkDraw& d, |
| 262 | IXpsOMObjectFactory* xpsFactory, |
| 263 | IXpsOMCanvas* canvas, |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 264 | TypefaceUse* font, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 265 | LPCWSTR text, |
| 266 | XPS_GLYPH_INDEX* xpsGlyphs, |
| 267 | UINT32 xpsGlyphsLen, |
| 268 | XPS_POINT *origin, |
| 269 | FLOAT fontSize, |
| 270 | XPS_STYLE_SIMULATION sims, |
| 271 | const SkMatrix& transform, |
| 272 | const SkPaint& paint); |
| 273 | |
| 274 | HRESULT addXpsPathGeometry( |
| 275 | IXpsOMGeometryFigureCollection* figures, |
| 276 | BOOL stroke, BOOL fill, const SkPath& path); |
| 277 | |
| 278 | HRESULT createPath( |
| 279 | IXpsOMGeometryFigure* figure, |
| 280 | IXpsOMVisualCollection* visuals, |
| 281 | IXpsOMPath** path); |
| 282 | |
| 283 | HRESULT sideOfClamp( |
| 284 | const SkRect& leftPoints, const XPS_RECT& left, |
| 285 | IXpsOMImageResource* imageResource, |
| 286 | IXpsOMVisualCollection* visuals); |
| 287 | |
| 288 | HRESULT cornerOfClamp( |
| 289 | const SkRect& tlPoints, |
| 290 | const SkColor color, |
| 291 | IXpsOMVisualCollection* visuals); |
| 292 | |
| 293 | HRESULT clip( |
| 294 | IXpsOMVisual* xpsVisual, |
| 295 | const SkDraw& d); |
| 296 | HRESULT clipToPath( |
| 297 | IXpsOMVisual* xpsVisual, |
| 298 | const SkPath& clipPath, |
| 299 | XPS_FILL_RULE fillRule); |
| 300 | |
| 301 | HRESULT drawInverseWindingPath( |
| 302 | const SkDraw& d, |
| 303 | const SkPath& devicePath, |
| 304 | IXpsOMPath* xpsPath); |
| 305 | |
| 306 | HRESULT shadePath( |
| 307 | IXpsOMPath* shadedPath, |
| 308 | const SkPaint& shaderPaint, |
| 309 | const SkMatrix& matrix, |
| 310 | BOOL* fill, BOOL* stroke); |
| 311 | |
| 312 | void convertToPpm( |
| 313 | const SkMaskFilter* filter, |
| 314 | SkMatrix* matrix, |
| 315 | SkVector* ppuScale, |
| 316 | const SkIRect& clip, SkIRect* clipIRect); |
| 317 | |
| 318 | HRESULT applyMask( |
| 319 | const SkDraw& d, |
| 320 | const SkMask& mask, |
| 321 | const SkVector& ppuScale, |
| 322 | IXpsOMPath* shadedPath); |
| 323 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 324 | SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 325 | |
| 326 | // Disable the default copy and assign implementation. |
| 327 | SkXPSDevice(const SkXPSDevice&); |
| 328 | void operator=(const SkXPSDevice&); |
| 329 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame^] | 330 | typedef SkBaseDevice INHERITED; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 331 | }; |
| 332 | |
halcanary | 0cbe7ee | 2015-12-01 09:02:49 -0800 | [diff] [blame] | 333 | #endif // SK_BUILD_FOR_WIN |
| 334 | #endif // SkXPSDevice_DEFINED |