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 | 025e244 | 2017-03-04 19:43:23 +0000 | [diff] [blame] | 19 | #include "SkBitmapDevice.h" |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 20 | #include "SkBitSet.h" |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 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 | */ |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [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 | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 43 | bool beginPortfolio(SkWStream* outputStream, IXpsOMObjectFactory*); |
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: |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 76 | void drawPaint(const SkDraw&, const SkPaint& paint) override; |
| 77 | |
| 78 | void drawPoints( |
| 79 | const SkDraw&, |
| 80 | SkCanvas::PointMode mode, |
| 81 | size_t count, const SkPoint[], |
| 82 | const SkPaint& paint) override; |
| 83 | |
| 84 | void drawRect( |
| 85 | const SkDraw&, |
| 86 | const SkRect& r, |
| 87 | const SkPaint& paint) override; |
| 88 | |
| 89 | void drawRRect( |
| 90 | const SkDraw&, |
| 91 | const SkRRect&, |
| 92 | const SkPaint& paint) override; |
| 93 | |
| 94 | void drawPath( |
| 95 | const SkDraw&, |
| 96 | const SkPath& platonicPath, |
| 97 | const SkPaint& paint, |
| 98 | const SkMatrix* prePathMatrix, |
| 99 | bool pathIsMutable) override; |
| 100 | |
| 101 | void drawBitmap( |
| 102 | const SkDraw&, |
| 103 | const SkBitmap& bitmap, |
| 104 | const SkMatrix& matrix, |
| 105 | const SkPaint& paint) override; |
| 106 | |
| 107 | void drawSprite( |
| 108 | const SkDraw&, |
| 109 | const SkBitmap& bitmap, |
| 110 | int x, int y, |
| 111 | const SkPaint& paint) override; |
| 112 | |
| 113 | void drawText( |
| 114 | const SkDraw&, |
| 115 | const void* text, size_t len, |
| 116 | SkScalar x, SkScalar y, |
| 117 | const SkPaint& paint) override; |
| 118 | |
| 119 | void drawPosText( |
| 120 | const SkDraw&, |
| 121 | const void* text, size_t len, |
| 122 | const SkScalar pos[], int scalarsPerPos, |
| 123 | const SkPoint& offset, const SkPaint& paint) override; |
| 124 | |
| 125 | void drawVertices( |
| 126 | const SkDraw&, |
| 127 | SkCanvas::VertexMode, |
| 128 | int vertexCount, const SkPoint verts[], |
| 129 | const SkPoint texs[], const SkColor colors[], |
| 130 | SkBlendMode, |
| 131 | const uint16_t indices[], int indexCount, |
| 132 | const SkPaint& paint) override; |
| 133 | |
| 134 | void drawDevice( |
| 135 | const SkDraw&, |
| 136 | SkBaseDevice* device, |
| 137 | int x, int y, |
| 138 | const SkPaint& paint) override; |
| 139 | |
| 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&, |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame] | 147 | SkCanvas::SrcRectConstraint) override; |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 148 | |
Hal Canary | b39b09e | 2017-02-01 17:04:44 -0500 | [diff] [blame] | 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 | abc88d2 | 2017-02-06 09:26:49 -0500 | [diff] [blame] | 164 | bool createCanvasForLayer(); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 165 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 166 | SkTScopedComPtr<IXpsOMObjectFactory> fXpsFactory; |
| 167 | SkTScopedComPtr<IStream> fOutputStream; |
| 168 | SkTScopedComPtr<IXpsOMPackageWriter> fPackageWriter; |
| 169 | |
| 170 | unsigned int fCurrentPage; |
| 171 | SkTScopedComPtr<IXpsOMCanvas> fCurrentXpsCanvas; |
| 172 | SkSize fCurrentCanvasSize; |
| 173 | SkVector fCurrentUnitsPerMeter; |
| 174 | SkVector fCurrentPixelsPerMeter; |
| 175 | |
| 176 | SkTArray<TypefaceUse, true> fTypefaces; |
| 177 | |
Ben Wagner | da5a1b8 | 2014-08-22 15:07:06 -0400 | [diff] [blame] | 178 | /** Creates a GUID based id and places it into buffer. |
| 179 | buffer should have space for at least GUID_ID_LEN wide characters. |
| 180 | The string will always be wchar null terminated. |
| 181 | XXXXXXXXsXXXXsXXXXsXXXXsXXXXXXXXXXXX0 |
| 182 | The string may begin with a digit, |
| 183 | and so may not be suitable as a bare resource key. |
| 184 | */ |
| 185 | HRESULT createId(wchar_t* buffer, size_t bufferSize, wchar_t sep = '-'); |
| 186 | #ifdef SK_XPS_USE_DETERMINISTIC_IDS |
| 187 | decltype(GUID::Data1) fNextId = 0; |
| 188 | #endif |
| 189 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 190 | HRESULT initXpsDocumentWriter(IXpsOMImageResource* image); |
| 191 | |
| 192 | HRESULT createXpsPage( |
| 193 | const XPS_SIZE& pageSize, |
| 194 | IXpsOMPage** page); |
| 195 | |
| 196 | HRESULT createXpsThumbnail( |
| 197 | IXpsOMPage* page, const unsigned int pageNumber, |
| 198 | IXpsOMImageResource** image); |
| 199 | |
| 200 | void internalDrawRect( |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 201 | const SkDraw&, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 202 | const SkRect& r, |
| 203 | bool transformRect, |
| 204 | const SkPaint& paint); |
| 205 | |
| 206 | HRESULT createXpsBrush( |
| 207 | const SkPaint& skPaint, |
| 208 | IXpsOMBrush** xpsBrush, |
| 209 | const SkMatrix* parentTransform = NULL); |
| 210 | |
| 211 | HRESULT createXpsSolidColorBrush( |
| 212 | const SkColor skColor, const SkAlpha alpha, |
| 213 | IXpsOMBrush** xpsBrush); |
| 214 | |
| 215 | HRESULT createXpsImageBrush( |
| 216 | const SkBitmap& bitmap, |
| 217 | const SkMatrix& localMatrix, |
| 218 | const SkShader::TileMode (&xy)[2], |
| 219 | const SkAlpha alpha, |
| 220 | IXpsOMTileBrush** xpsBrush); |
| 221 | |
| 222 | HRESULT createXpsLinearGradient( |
| 223 | SkShader::GradientInfo info, |
| 224 | const SkAlpha alpha, |
| 225 | const SkMatrix& localMatrix, |
| 226 | IXpsOMMatrixTransform* xpsMatrixToUse, |
| 227 | IXpsOMBrush** xpsBrush); |
| 228 | |
| 229 | HRESULT createXpsRadialGradient( |
| 230 | SkShader::GradientInfo info, |
| 231 | const SkAlpha alpha, |
| 232 | const SkMatrix& localMatrix, |
| 233 | IXpsOMMatrixTransform* xpsMatrixToUse, |
| 234 | IXpsOMBrush** xpsBrush); |
| 235 | |
| 236 | HRESULT createXpsGradientStop( |
| 237 | const SkColor skColor, |
| 238 | const SkScalar offset, |
| 239 | IXpsOMGradientStop** xpsGradStop); |
| 240 | |
| 241 | HRESULT createXpsTransform( |
| 242 | const SkMatrix& matrix, |
| 243 | IXpsOMMatrixTransform ** xpsTransform); |
| 244 | |
| 245 | HRESULT createXpsRect( |
| 246 | const SkRect& rect, |
| 247 | BOOL stroke, BOOL fill, |
| 248 | IXpsOMGeometryFigure** xpsRect); |
| 249 | |
| 250 | HRESULT createXpsQuad( |
| 251 | const SkPoint (&points)[4], |
| 252 | BOOL stroke, BOOL fill, |
| 253 | IXpsOMGeometryFigure** xpsQuad); |
| 254 | |
| 255 | HRESULT CreateTypefaceUse( |
| 256 | const SkPaint& paint, |
| 257 | TypefaceUse** fontResource); |
| 258 | |
| 259 | HRESULT AddGlyphs( |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 260 | const SkDraw& d, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 261 | IXpsOMObjectFactory* xpsFactory, |
| 262 | IXpsOMCanvas* canvas, |
commit-bot@chromium.org | b5e34e2 | 2013-05-07 15:28:15 +0000 | [diff] [blame] | 263 | TypefaceUse* font, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 264 | LPCWSTR text, |
| 265 | XPS_GLYPH_INDEX* xpsGlyphs, |
| 266 | UINT32 xpsGlyphsLen, |
| 267 | XPS_POINT *origin, |
| 268 | FLOAT fontSize, |
| 269 | XPS_STYLE_SIMULATION sims, |
| 270 | const SkMatrix& transform, |
| 271 | const SkPaint& paint); |
| 272 | |
| 273 | HRESULT addXpsPathGeometry( |
| 274 | IXpsOMGeometryFigureCollection* figures, |
| 275 | BOOL stroke, BOOL fill, const SkPath& path); |
| 276 | |
| 277 | HRESULT createPath( |
| 278 | IXpsOMGeometryFigure* figure, |
| 279 | IXpsOMVisualCollection* visuals, |
| 280 | IXpsOMPath** path); |
| 281 | |
| 282 | HRESULT sideOfClamp( |
| 283 | const SkRect& leftPoints, const XPS_RECT& left, |
| 284 | IXpsOMImageResource* imageResource, |
| 285 | IXpsOMVisualCollection* visuals); |
| 286 | |
| 287 | HRESULT cornerOfClamp( |
| 288 | const SkRect& tlPoints, |
| 289 | const SkColor color, |
| 290 | IXpsOMVisualCollection* visuals); |
| 291 | |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 292 | HRESULT clip( |
| 293 | IXpsOMVisual* xpsVisual, |
| 294 | const SkDraw& d); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 295 | HRESULT clipToPath( |
| 296 | IXpsOMVisual* xpsVisual, |
| 297 | const SkPath& clipPath, |
| 298 | XPS_FILL_RULE fillRule); |
| 299 | |
| 300 | HRESULT drawInverseWindingPath( |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 301 | const SkDraw& d, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 302 | const SkPath& devicePath, |
| 303 | IXpsOMPath* xpsPath); |
| 304 | |
| 305 | HRESULT shadePath( |
| 306 | IXpsOMPath* shadedPath, |
| 307 | const SkPaint& shaderPaint, |
| 308 | const SkMatrix& matrix, |
| 309 | BOOL* fill, BOOL* stroke); |
| 310 | |
| 311 | void convertToPpm( |
| 312 | const SkMaskFilter* filter, |
| 313 | SkMatrix* matrix, |
| 314 | SkVector* ppuScale, |
| 315 | const SkIRect& clip, SkIRect* clipIRect); |
| 316 | |
| 317 | HRESULT applyMask( |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 318 | const SkDraw& d, |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 319 | const SkMask& mask, |
| 320 | const SkVector& ppuScale, |
| 321 | IXpsOMPath* shadedPath); |
| 322 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 323 | SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 324 | |
| 325 | // Disable the default copy and assign implementation. |
| 326 | SkXPSDevice(const SkXPSDevice&); |
| 327 | void operator=(const SkXPSDevice&); |
| 328 | |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 329 | typedef SkBaseDevice INHERITED; |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 330 | }; |
| 331 | |
halcanary | 0cbe7ee | 2015-12-01 09:02:49 -0800 | [diff] [blame] | 332 | #endif // SK_BUILD_FOR_WIN |
| 333 | #endif // SkXPSDevice_DEFINED |