vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "SkPDFDevice.h" |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 9 | |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 10 | #include "SkAdvancedTypefaceMetrics.h" |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 11 | #include "SkAnnotationKeys.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 12 | #include "SkBitmapDevice.h" |
martina.kollarova | b8d6af1 | 2016-06-29 05:12:31 -0700 | [diff] [blame] | 13 | #include "SkBitmapKey.h" |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 15 | #include "SkClipOpPriv.h" |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 16 | #include "SkClusterator.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 17 | #include "SkColor.h" |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 18 | #include "SkColorFilter.h" |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 19 | #include "SkDraw.h" |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 20 | #include "SkDrawFilter.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 21 | #include "SkGlyphCache.h" |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 22 | #include "SkImageFilterCache.h" |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 23 | #include "SkJpegEncoder.h" |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 24 | #include "SkMakeUnique.h" |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 25 | #include "SkMaskFilterBase.h" |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 26 | #include "SkPDFBitmap.h" |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 27 | #include "SkPDFCanon.h" |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 28 | #include "SkPDFDocument.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 29 | #include "SkPDFFont.h" |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 30 | #include "SkPDFFormXObject.h" |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 31 | #include "SkPDFGraphicState.h" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 32 | #include "SkPDFResourceDict.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 33 | #include "SkPDFShader.h" |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 34 | #include "SkPDFTypes.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 35 | #include "SkPDFUtils.h" |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 36 | #include "SkPath.h" |
| 37 | #include "SkPathEffect.h" |
| 38 | #include "SkPathOps.h" |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 39 | #include "SkPixelRef.h" |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 40 | #include "SkRRect.h" |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 41 | #include "SkRasterClip.h" |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 42 | #include "SkScopeExit.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 43 | #include "SkString.h" |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 44 | #include "SkSurface.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 45 | #include "SkTemplates.h" |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 46 | #include "SkTextBlobRunIterator.h" |
| 47 | #include "SkTextFormatParams.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 48 | #include "SkTo.h" |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 49 | #include "SkUtils.h" |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 50 | #include "SkXfermodeInterpretation.h" |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 51 | |
| 52 | #ifndef SK_PDF_MASK_QUALITY |
| 53 | // If MASK_QUALITY is in [0,100], will be used for JpegEncoder. |
| 54 | // Otherwise, just encode masks losslessly. |
| 55 | #define SK_PDF_MASK_QUALITY 50 |
| 56 | // Since these masks are used for blurry shadows, we shouldn't need |
| 57 | // high quality. Raise this value if your shadows have visible JPEG |
| 58 | // artifacts. |
| 59 | // If SkJpegEncoder::Encode fails, we will fall back to the lossless |
| 60 | // encoding. |
| 61 | #endif |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 62 | |
| 63 | // Utility functions |
| 64 | |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 65 | // This function destroys the mask and either frees or takes the pixels. |
| 66 | sk_sp<SkImage> mask_to_greyscale_image(SkMask* mask) { |
| 67 | sk_sp<SkImage> img; |
| 68 | SkPixmap pm(SkImageInfo::Make(mask->fBounds.width(), mask->fBounds.height(), |
| 69 | kGray_8_SkColorType, kOpaque_SkAlphaType), |
| 70 | mask->fImage, mask->fRowBytes); |
| 71 | const int imgQuality = SK_PDF_MASK_QUALITY; |
| 72 | if (imgQuality <= 100 && imgQuality >= 0) { |
| 73 | SkDynamicMemoryWStream buffer; |
| 74 | SkJpegEncoder::Options jpegOptions; |
| 75 | jpegOptions.fQuality = imgQuality; |
| 76 | if (SkJpegEncoder::Encode(&buffer, pm, jpegOptions)) { |
| 77 | img = SkImage::MakeFromEncoded(buffer.detachAsData()); |
| 78 | SkASSERT(img); |
| 79 | if (img) { |
| 80 | SkMask::FreeImage(mask->fImage); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | if (!img) { |
| 85 | img = SkImage::MakeFromRaster(pm, [](const void* p, void*) { SkMask::FreeImage((void*)p); }, |
| 86 | nullptr); |
| 87 | } |
| 88 | *mask = SkMask(); // destructive; |
| 89 | return img; |
| 90 | } |
| 91 | |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 92 | sk_sp<SkImage> alpha_image_to_greyscale_image(const SkImage* mask) { |
| 93 | int w = mask->width(), h = mask->height(); |
| 94 | SkBitmap greyBitmap; |
| 95 | greyBitmap.allocPixels(SkImageInfo::Make(w, h, kGray_8_SkColorType, kOpaque_SkAlphaType)); |
| 96 | if (!mask->readPixels(SkImageInfo::MakeA8(w, h), |
| 97 | greyBitmap.getPixels(), greyBitmap.rowBytes(), 0, 0)) { |
| 98 | return nullptr; |
| 99 | } |
| 100 | return SkImage::MakeFromBitmap(greyBitmap); |
| 101 | } |
| 102 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 103 | static void draw_points(SkCanvas::PointMode mode, |
| 104 | size_t count, |
| 105 | const SkPoint* points, |
| 106 | const SkPaint& paint, |
| 107 | const SkIRect& bounds, |
| 108 | const SkMatrix& ctm, |
| 109 | SkBaseDevice* device) { |
| 110 | SkRasterClip rc(bounds); |
| 111 | SkDraw draw; |
| 112 | draw.fDst = SkPixmap(SkImageInfo::MakeUnknown(bounds.right(), bounds.bottom()), nullptr, 0); |
| 113 | draw.fMatrix = &ctm; |
| 114 | draw.fRC = &rc; |
| 115 | draw.drawPoints(mode, count, points, paint, device); |
| 116 | } |
| 117 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 118 | // If the paint will definitely draw opaquely, replace kSrc with |
| 119 | // kSrcOver. http://crbug.com/473572 |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 120 | static void replace_srcmode_on_opaque_paint(SkPaint* paint) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 121 | if (kSrcOver_SkXfermodeInterpretation == SkInterpretXfermode(*paint, false)) { |
| 122 | paint->setBlendMode(SkBlendMode::kSrcOver); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 126 | // A shader's matrix is: CTMM x LocalMatrix x WrappingLocalMatrix. We want to |
| 127 | // switch to device space, where CTM = I, while keeping the original behavior. |
| 128 | // |
| 129 | // I * LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix |
| 130 | // LocalMatrix * NewWrappingMatrix = CTM * LocalMatrix |
| 131 | // InvLocalMatrix * LocalMatrix * NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix |
| 132 | // NewWrappingMatrix = InvLocalMatrix * CTM * LocalMatrix |
| 133 | // |
| 134 | static void transform_shader(SkPaint* paint, const SkMatrix& ctm) { |
| 135 | SkMatrix lm = SkPDFUtils::GetShaderLocalMatrix(paint->getShader()); |
| 136 | SkMatrix lmInv; |
| 137 | if (lm.invert(&lmInv)) { |
| 138 | SkMatrix m = SkMatrix::Concat(SkMatrix::Concat(lmInv, ctm), lm); |
| 139 | paint->setShader(paint->getShader()->makeWithLocalMatrix(m)); |
| 140 | } |
| 141 | } |
| 142 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 143 | static void emit_pdf_color(SkColor color, SkWStream* result) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 144 | SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 145 | SkPDFUtils::AppendColorComponent(SkColorGetR(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 146 | result->writeText(" "); |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 147 | SkPDFUtils::AppendColorComponent(SkColorGetG(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 148 | result->writeText(" "); |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 149 | SkPDFUtils::AppendColorComponent(SkColorGetB(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 150 | result->writeText(" "); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 151 | } |
| 152 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 153 | static SkPaint calculate_text_paint(const SkPaint& paint) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 154 | SkPaint result = paint; |
| 155 | if (result.isFakeBoldText()) { |
| 156 | SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(), |
| 157 | kStdFakeBoldInterpKeys, |
| 158 | kStdFakeBoldInterpValues, |
| 159 | kStdFakeBoldInterpLength); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 160 | SkScalar width = result.getTextSize() * fakeBoldScale; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 161 | if (result.getStyle() == SkPaint::kFill_Style) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 162 | result.setStyle(SkPaint::kStrokeAndFill_Style); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 163 | } else { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 164 | width += result.getStrokeWidth(); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 165 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 166 | result.setStrokeWidth(width); |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 171 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 172 | // If the paint has a color filter, apply the color filter to the shader or the |
| 173 | // paint color. Remove the color filter. |
| 174 | void remove_color_filter(SkPaint* paint) { |
| 175 | if (SkColorFilter* cf = paint->getColorFilter()) { |
| 176 | if (SkShader* shader = paint->getShader()) { |
| 177 | paint->setShader(shader->makeWithColorFilter(paint->refColorFilter())); |
| 178 | } else { |
| 179 | paint->setColor(cf->filterColor(paint->getColor())); |
| 180 | } |
| 181 | paint->setColorFilter(nullptr); |
| 182 | } |
| 183 | } |
| 184 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 185 | SkPDFDevice::GraphicStateEntry::GraphicStateEntry() |
| 186 | : fColor(SK_ColorBLACK) |
| 187 | , fTextScaleX(SK_Scalar1) |
| 188 | , fTextFill(SkPaint::kFill_Style) |
| 189 | , fShaderIndex(-1) |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 190 | , fGraphicStateIndex(-1) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 191 | fMatrix.reset(); |
| 192 | } |
| 193 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 194 | bool SkPDFDevice::GraphicStateEntry::compareInitialState( |
| 195 | const GraphicStateEntry& cur) { |
commit-bot@chromium.org | b000d76 | 2014-02-07 19:39:57 +0000 | [diff] [blame] | 196 | return fColor == cur.fColor && |
| 197 | fShaderIndex == cur.fShaderIndex && |
| 198 | fGraphicStateIndex == cur.fGraphicStateIndex && |
| 199 | fMatrix == cur.fMatrix && |
| 200 | fClipStack == cur.fClipStack && |
| 201 | (fTextScaleX == 0 || |
| 202 | (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 203 | } |
| 204 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 205 | class GraphicStackState { |
| 206 | public: |
| 207 | GraphicStackState(const SkClipStack& existingClipStack, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 208 | SkWStream* contentStream) |
| 209 | : fStackDepth(0), |
| 210 | fContentStream(contentStream) { |
| 211 | fEntries[0].fClipStack = existingClipStack; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Hal Canary | 4e83ff1 | 2018-03-09 12:16:42 -0500 | [diff] [blame] | 214 | void updateClip(const SkClipStack& clipStack, const SkIRect& bounds); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 215 | void updateMatrix(const SkMatrix& matrix); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 216 | void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 217 | |
| 218 | void drainStack(); |
| 219 | |
| 220 | private: |
| 221 | void push(); |
| 222 | void pop(); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 223 | SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; } |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 224 | |
| 225 | // Conservative limit on save depth, see impl. notes in PDF 1.4 spec. |
| 226 | static const int kMaxStackDepth = 12; |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 227 | SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1]; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 228 | int fStackDepth; |
| 229 | SkWStream* fContentStream; |
| 230 | }; |
| 231 | |
| 232 | void GraphicStackState::drainStack() { |
| 233 | while (fStackDepth) { |
| 234 | pop(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void GraphicStackState::push() { |
| 239 | SkASSERT(fStackDepth < kMaxStackDepth); |
| 240 | fContentStream->writeText("q\n"); |
| 241 | fStackDepth++; |
| 242 | fEntries[fStackDepth] = fEntries[fStackDepth - 1]; |
| 243 | } |
| 244 | |
| 245 | void GraphicStackState::pop() { |
| 246 | SkASSERT(fStackDepth > 0); |
| 247 | fContentStream->writeText("Q\n"); |
| 248 | fStackDepth--; |
| 249 | } |
| 250 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 251 | /* Calculate an inverted path's equivalent non-inverted path, given the |
| 252 | * canvas bounds. |
| 253 | * outPath may alias with invPath (since this is supported by PathOps). |
| 254 | */ |
| 255 | static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath, |
| 256 | SkPath* outPath) { |
| 257 | SkASSERT(invPath.isInverseFillType()); |
| 258 | |
| 259 | SkPath clipPath; |
| 260 | clipPath.addRect(bounds); |
| 261 | |
reed | cdb42bb | 2015-06-26 10:23:07 -0700 | [diff] [blame] | 262 | return Op(clipPath, invPath, kIntersect_SkPathOp, outPath); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 265 | bool apply_clip(SkClipOp op, const SkPath& u, const SkPath& v, SkPath* r) { |
| 266 | switch (op) { |
| 267 | case SkClipOp::kDifference: |
| 268 | return Op(u, v, kDifference_SkPathOp, r); |
| 269 | case SkClipOp::kIntersect: |
| 270 | return Op(u, v, kIntersect_SkPathOp, r); |
Mike Reed | 14113bc | 2017-05-10 14:13:20 -0400 | [diff] [blame] | 271 | #ifdef SK_SUPPORT_DEPRECATED_CLIPOPS |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 272 | case SkClipOp::kUnion_deprecated: |
| 273 | return Op(u, v, kUnion_SkPathOp, r); |
| 274 | case SkClipOp::kXOR_deprecated: |
| 275 | return Op(u, v, kXOR_SkPathOp, r); |
| 276 | case SkClipOp::kReverseDifference_deprecated: |
| 277 | return Op(u, v, kReverseDifference_SkPathOp, r); |
| 278 | case SkClipOp::kReplace_deprecated: |
| 279 | *r = v; |
| 280 | return true; |
Mike Reed | 14113bc | 2017-05-10 14:13:20 -0400 | [diff] [blame] | 281 | #endif |
Hal Canary | 44a97d5 | 2017-02-22 10:45:25 -0500 | [diff] [blame] | 282 | default: |
| 283 | return false; |
| 284 | } |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Hal Canary | d00ef06 | 2018-06-05 11:53:58 -0400 | [diff] [blame] | 287 | static SkRect rect_intersect(SkRect u, SkRect v) { |
| 288 | if (u.isEmpty() || v.isEmpty()) { return {0, 0, 0, 0}; } |
| 289 | return u.intersect(v) ? u : SkRect{0, 0, 0, 0}; |
| 290 | } |
| 291 | |
| 292 | // Test to see if the clipstack is a simple rect, If so, we can avoid all PathOps code |
| 293 | // and speed thing up. |
| 294 | static bool is_rect(const SkClipStack& clipStack, const SkRect& bounds, SkRect* dst) { |
| 295 | SkRect currentClip = bounds; |
| 296 | SkClipStack::Iter iter(clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 297 | while (const SkClipStack::Element* element = iter.next()) { |
| 298 | SkRect elementRect{0, 0, 0, 0}; |
| 299 | switch (element->getDeviceSpaceType()) { |
| 300 | case SkClipStack::Element::DeviceSpaceType::kEmpty: |
| 301 | break; |
| 302 | case SkClipStack::Element::DeviceSpaceType::kRect: |
| 303 | elementRect = element->getDeviceSpaceRect(); |
| 304 | break; |
| 305 | default: |
| 306 | return false; |
| 307 | } |
| 308 | switch (element->getOp()) { |
| 309 | case kReplace_SkClipOp: |
| 310 | currentClip = rect_intersect(bounds, elementRect); |
| 311 | break; |
| 312 | case SkClipOp::kIntersect: |
| 313 | currentClip = rect_intersect(currentClip, elementRect); |
| 314 | break; |
| 315 | default: |
| 316 | return false; |
| 317 | } |
| 318 | } |
| 319 | *dst = currentClip; |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | static void append_clip(const SkClipStack& clipStack, |
| 324 | const SkIRect& bounds, |
| 325 | SkWStream* wStream) { |
| 326 | // The bounds are slightly outset to ensure this is correct in the |
| 327 | // face of floating-point accuracy and possible SkRegion bitmap |
| 328 | // approximations. |
| 329 | SkRect outsetBounds = SkRect::Make(bounds.makeOutset(1, 1)); |
| 330 | |
| 331 | SkRect clipStackRect; |
| 332 | if (is_rect(clipStack, outsetBounds, &clipStackRect)) { |
| 333 | SkPDFUtils::AppendRectangle(clipStackRect, wStream); |
| 334 | wStream->writeText("W* n\n"); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | SkPath clipPath; |
| 339 | (void)clipStack.asPath(&clipPath); |
| 340 | |
| 341 | SkPath clipBoundsPath; |
| 342 | clipBoundsPath.addRect(outsetBounds); |
| 343 | |
| 344 | if (Op(clipPath, clipBoundsPath, kIntersect_SkPathOp, &clipPath)) { |
| 345 | SkPDFUtils::EmitPath(clipPath, SkPaint::kFill_Style, wStream); |
| 346 | SkPath::FillType clipFill = clipPath.getFillType(); |
| 347 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false); |
| 348 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false); |
| 349 | if (clipFill == SkPath::kEvenOdd_FillType) { |
| 350 | wStream->writeText("W* n\n"); |
| 351 | } else { |
| 352 | wStream->writeText("W n\n"); |
| 353 | } |
| 354 | } |
| 355 | // If Op() fails (pathological case; e.g. input values are |
| 356 | // extremely large or NaN), emit no clip at all. |
| 357 | } |
| 358 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 359 | // TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 360 | // graphic state stack, and the fact that we can know all the clips used |
| 361 | // on the page to optimize this. |
| 362 | void GraphicStackState::updateClip(const SkClipStack& clipStack, |
Hal Canary | 4e83ff1 | 2018-03-09 12:16:42 -0500 | [diff] [blame] | 363 | const SkIRect& bounds) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 364 | if (clipStack == currentEntry()->fClipStack) { |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | while (fStackDepth > 0) { |
| 369 | pop(); |
| 370 | if (clipStack == currentEntry()->fClipStack) { |
| 371 | return; |
| 372 | } |
| 373 | } |
| 374 | push(); |
| 375 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 376 | currentEntry()->fClipStack = clipStack; |
Hal Canary | d00ef06 | 2018-06-05 11:53:58 -0400 | [diff] [blame] | 377 | append_clip(clipStack, bounds, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void GraphicStackState::updateMatrix(const SkMatrix& matrix) { |
| 381 | if (matrix == currentEntry()->fMatrix) { |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) { |
| 386 | SkASSERT(fStackDepth > 0); |
| 387 | SkASSERT(fEntries[fStackDepth].fClipStack == |
| 388 | fEntries[fStackDepth -1].fClipStack); |
| 389 | pop(); |
| 390 | |
| 391 | SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask); |
| 392 | } |
| 393 | if (matrix.getType() == SkMatrix::kIdentity_Mask) { |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | push(); |
| 398 | SkPDFUtils::AppendTransform(matrix, fContentStream); |
| 399 | currentEntry()->fMatrix = matrix; |
| 400 | } |
| 401 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 402 | void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 403 | // PDF treats a shader as a color, so we only set one or the other. |
| 404 | if (state.fShaderIndex >= 0) { |
| 405 | if (state.fShaderIndex != currentEntry()->fShaderIndex) { |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 406 | SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 407 | currentEntry()->fShaderIndex = state.fShaderIndex; |
| 408 | } |
| 409 | } else { |
| 410 | if (state.fColor != currentEntry()->fColor || |
| 411 | currentEntry()->fShaderIndex >= 0) { |
| 412 | emit_pdf_color(state.fColor, fContentStream); |
| 413 | fContentStream->writeText("RG "); |
| 414 | emit_pdf_color(state.fColor, fContentStream); |
| 415 | fContentStream->writeText("rg\n"); |
| 416 | currentEntry()->fColor = state.fColor; |
| 417 | currentEntry()->fShaderIndex = -1; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 422 | SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 423 | currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex; |
| 424 | } |
| 425 | |
| 426 | if (state.fTextScaleX) { |
| 427 | if (state.fTextScaleX != currentEntry()->fTextScaleX) { |
Hal Canary | 9b491b2 | 2017-11-28 15:10:13 -0500 | [diff] [blame] | 428 | SkScalar pdfScale = state.fTextScaleX * 100; |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 429 | SkPDFUtils::AppendScalar(pdfScale, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 430 | fContentStream->writeText(" Tz\n"); |
| 431 | currentEntry()->fTextScaleX = state.fTextScaleX; |
| 432 | } |
| 433 | if (state.fTextFill != currentEntry()->fTextFill) { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 434 | static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value"); |
| 435 | static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value"); |
| 436 | static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value"); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 437 | fContentStream->writeDecAsText(state.fTextFill); |
| 438 | fContentStream->writeText(" Tr\n"); |
| 439 | currentEntry()->fTextFill = state.fTextFill; |
| 440 | } |
| 441 | } |
| 442 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 443 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 444 | static bool not_supported_for_layers(const SkPaint& layerPaint) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 445 | // PDF does not support image filters, so render them on CPU. |
| 446 | // Note that this rendering is done at "screen" resolution (100dpi), not |
| 447 | // printer resolution. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 448 | // TODO: It may be possible to express some filters natively using PDF |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 449 | // to improve quality and file size (https://bug.skia.org/3043) |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 450 | |
| 451 | // TODO: should we return true if there is a colorfilter? |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 452 | return layerPaint.getImageFilter() != nullptr; |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) { |
reed | cd4051e | 2016-07-15 09:41:26 -0700 | [diff] [blame] | 456 | if (layerPaint && not_supported_for_layers(*layerPaint)) { |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 457 | // need to return a raster device, which we will detect in drawDevice() |
| 458 | return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 459 | } |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 460 | return new SkPDFDevice(cinfo.fInfo.dimensions(), fDocument); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 461 | } |
| 462 | |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 463 | SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); } |
| 464 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 465 | // A helper class to automatically finish a ContentEntry at the end of a |
| 466 | // drawing method and maintain the state needed between set up and finish. |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 467 | class ScopedContentEntry { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 468 | public: |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 469 | ScopedContentEntry(SkPDFDevice* device, |
| 470 | const SkClipStack& clipStack, |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 471 | const SkMatrix& matrix, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 472 | const SkPaint& paint, |
| 473 | bool hasText = false) |
| 474 | : fDevice(device) |
| 475 | , fContentEntry(nullptr) |
| 476 | , fBlendMode(SkBlendMode::kSrcOver) |
| 477 | , fDstFormXObject(nullptr) |
| 478 | { |
| 479 | if (matrix.hasPerspective()) { |
| 480 | NOT_IMPLEMENTED(!matrix.hasPerspective(), false); |
| 481 | return; |
| 482 | } |
| 483 | fBlendMode = paint.getBlendMode(); |
| 484 | fContentEntry = |
| 485 | fDevice->setUpContentEntry(clipStack, matrix, paint, hasText, &fDstFormXObject); |
Mike Reed | 27d07f0 | 2017-03-04 21:47:47 +0000 | [diff] [blame] | 486 | } |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 487 | ScopedContentEntry(SkPDFDevice* dev, const SkPaint& paint, bool hasText = false) |
| 488 | : ScopedContentEntry(dev, dev->cs(), dev->ctm(), paint, hasText) {} |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 489 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 490 | ~ScopedContentEntry() { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 491 | if (fContentEntry) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 492 | SkPath* shape = &fShape; |
| 493 | if (shape->isEmpty()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 494 | shape = nullptr; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 495 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 496 | fDevice->finishContentEntry(fBlendMode, std::move(fDstFormXObject), shape); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 500 | SkPDFDevice::ContentEntry* entry() { return fContentEntry; } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 501 | SkDynamicMemoryWStream* stream() { return &fContentEntry->fContent; } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 502 | |
| 503 | /* Returns true when we explicitly need the shape of the drawing. */ |
| 504 | bool needShape() { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 505 | switch (fBlendMode) { |
| 506 | case SkBlendMode::kClear: |
| 507 | case SkBlendMode::kSrc: |
| 508 | case SkBlendMode::kSrcIn: |
| 509 | case SkBlendMode::kSrcOut: |
| 510 | case SkBlendMode::kDstIn: |
| 511 | case SkBlendMode::kDstOut: |
| 512 | case SkBlendMode::kSrcATop: |
| 513 | case SkBlendMode::kDstATop: |
| 514 | case SkBlendMode::kModulate: |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 515 | return true; |
| 516 | default: |
| 517 | return false; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* Returns true unless we only need the shape of the drawing. */ |
| 522 | bool needSource() { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 523 | if (fBlendMode == SkBlendMode::kClear) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 524 | return false; |
| 525 | } |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | /* If the shape is different than the alpha component of the content, then |
| 530 | * setShape should be called with the shape. In particular, images and |
| 531 | * devices have rectangular shape. |
| 532 | */ |
| 533 | void setShape(const SkPath& shape) { |
| 534 | fShape = shape; |
| 535 | } |
| 536 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 537 | private: |
| 538 | SkPDFDevice* fDevice; |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 539 | SkPDFDevice::ContentEntry* fContentEntry; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 540 | SkBlendMode fBlendMode; |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 541 | sk_sp<SkPDFObject> fDstFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 542 | SkPath fShape; |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 543 | }; |
| 544 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 545 | //////////////////////////////////////////////////////////////////////////////// |
| 546 | |
Hal Canary | a062258 | 2017-06-29 18:51:35 -0400 | [diff] [blame] | 547 | SkPDFDevice::SkPDFDevice(SkISize pageSize, SkPDFDocument* doc) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 548 | : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()), |
| 549 | SkSurfaceProps(0, kUnknown_SkPixelGeometry)) |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 550 | , fPageSize(pageSize) |
Hal Canary | a062258 | 2017-06-29 18:51:35 -0400 | [diff] [blame] | 551 | , fInitialTransform(SkMatrix::I()) |
| 552 | , fDocument(doc) |
| 553 | { |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 554 | SkASSERT(!pageSize.isEmpty()); |
Hal Canary | a062258 | 2017-06-29 18:51:35 -0400 | [diff] [blame] | 555 | } |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 556 | |
Hal Canary | a062258 | 2017-06-29 18:51:35 -0400 | [diff] [blame] | 557 | void SkPDFDevice::setFlip() { |
| 558 | // Skia generally uses the top left as the origin but PDF |
| 559 | // natively has the origin at the bottom left. This matrix |
| 560 | // corrects for that. But that only needs to be done once, we |
| 561 | // don't do it when layering. |
Hal Canary | 8259114 | 2018-05-09 14:20:12 -0400 | [diff] [blame] | 562 | SkScalar rasterScale = SkPDFUtils::kDpiForRasterScaleOne / fDocument->rasterDpi(); |
| 563 | fInitialTransform.setConcat(SkMatrix::MakeScale(rasterScale, -rasterScale), |
| 564 | SkMatrix::MakeTrans(0, -fPageSize.fHeight)); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | SkPDFDevice::~SkPDFDevice() { |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 568 | this->cleanUp(); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | void SkPDFDevice::init() { |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 572 | fContentEntries.reset(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 573 | } |
| 574 | |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 575 | void SkPDFDevice::cleanUp() { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 576 | fGraphicStateResources.unrefAll(); |
| 577 | fXObjectResources.unrefAll(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 578 | fFontResources.unrefAll(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 579 | fShaderResources.unrefAll(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 582 | void SkPDFDevice::drawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
Hal Canary | 9cd2168 | 2017-02-22 15:55:06 -0500 | [diff] [blame] | 583 | if (!value) { |
| 584 | return; |
| 585 | } |
| 586 | if (rect.isEmpty()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 587 | if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) { |
| 588 | SkPoint transformedPoint; |
| 589 | this->ctm().mapXY(rect.x(), rect.y(), &transformedPoint); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 590 | fNamedDestinations.emplace_back(NamedDestination{sk_ref_sp(value), transformedPoint}); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 591 | } |
| 592 | return; |
| 593 | } |
| 594 | // Convert to path to handle non-90-degree rotations. |
| 595 | SkPath path; |
| 596 | path.addRect(rect); |
| 597 | path.transform(this->ctm(), &path); |
| 598 | SkPath clip; |
| 599 | (void)this->cs().asPath(&clip); |
| 600 | Op(clip, path, kIntersect_SkPathOp, &path); |
| 601 | // PDF wants a rectangle only. |
| 602 | SkRect transformedRect = path.getBounds(); |
| 603 | if (transformedRect.isEmpty()) { |
| 604 | return; |
| 605 | } |
| 606 | if (!strcmp(SkAnnotationKeys::URL_Key(), key)) { |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 607 | fLinkToURLs.emplace_back(RectWithData{transformedRect, sk_ref_sp(value)}); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 608 | } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) { |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 609 | fLinkToDestinations.emplace_back(RectWithData{transformedRect, sk_ref_sp(value)}); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 613 | void SkPDFDevice::drawPaint(const SkPaint& srcPaint) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 614 | if (this->hasEmptyClip()) { |
| 615 | return; |
| 616 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 617 | SkPaint newPaint = srcPaint; |
| 618 | remove_color_filter(&newPaint); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 619 | replace_srcmode_on_opaque_paint(&newPaint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 620 | newPaint.setStyle(SkPaint::kFill_Style); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 621 | |
| 622 | SkMatrix ctm = this->ctm(); |
| 623 | if (ctm.getType() & SkMatrix::kPerspective_Mask) { |
| 624 | if (newPaint.getShader()) { |
| 625 | transform_shader(&newPaint, ctm); |
| 626 | } |
| 627 | ctm = SkMatrix::I(); |
| 628 | } |
| 629 | ScopedContentEntry content(this, this->cs(), ctm, newPaint); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 630 | this->internalDrawPaint(newPaint, content.entry()); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 631 | } |
| 632 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 633 | void SkPDFDevice::internalDrawPaint(const SkPaint& paint, |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 634 | SkPDFDevice::ContentEntry* contentEntry) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 635 | if (!contentEntry) { |
| 636 | return; |
| 637 | } |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 638 | SkRect bbox = SkRect::Make(fPageSize); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 639 | SkMatrix inverse; |
commit-bot@chromium.org | d2cfa74 | 2013-09-20 18:58:30 +0000 | [diff] [blame] | 640 | if (!contentEntry->fState.fMatrix.invert(&inverse)) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 641 | return; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 642 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 643 | inverse.mapRect(&bbox); |
| 644 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 645 | SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 646 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 647 | &contentEntry->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 650 | void SkPDFDevice::drawPoints(SkCanvas::PointMode mode, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 651 | size_t count, |
| 652 | const SkPoint* points, |
| 653 | const SkPaint& srcPaint) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 654 | if (this->hasEmptyClip()) { |
| 655 | return; |
| 656 | } |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 657 | SkPaint passedPaint = srcPaint; |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 658 | remove_color_filter(&passedPaint); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 659 | replace_srcmode_on_opaque_paint(&passedPaint); |
Hal Canary | 80fa7ce | 2017-06-28 16:04:20 -0400 | [diff] [blame] | 660 | if (SkCanvas::kPoints_PointMode != mode) { |
| 661 | passedPaint.setStyle(SkPaint::kStroke_Style); |
| 662 | } |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 663 | if (count == 0) { |
| 664 | return; |
| 665 | } |
| 666 | |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 667 | // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath. |
| 668 | // We only use this when there's a path effect because of the overhead |
| 669 | // of multiple calls to setUpContentEntry it causes. |
| 670 | if (passedPaint.getPathEffect()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 671 | draw_points(mode, count, points, passedPaint, |
| 672 | this->devClipBounds(), this->ctm(), this); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 673 | return; |
| 674 | } |
| 675 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 676 | const SkPaint* paint = &passedPaint; |
| 677 | SkPaint modifiedPaint; |
| 678 | |
| 679 | if (mode == SkCanvas::kPoints_PointMode && |
| 680 | paint->getStrokeCap() != SkPaint::kRound_Cap) { |
| 681 | modifiedPaint = *paint; |
| 682 | paint = &modifiedPaint; |
| 683 | if (paint->getStrokeWidth()) { |
| 684 | // PDF won't draw a single point with square/butt caps because the |
| 685 | // orientation is ambiguous. Draw a rectangle instead. |
| 686 | modifiedPaint.setStyle(SkPaint::kFill_Style); |
| 687 | SkScalar strokeWidth = paint->getStrokeWidth(); |
| 688 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 689 | for (size_t i = 0; i < count; i++) { |
| 690 | SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0); |
| 691 | r.inset(-halfStroke, -halfStroke); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 692 | this->drawRect(r, modifiedPaint); |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 693 | } |
| 694 | return; |
| 695 | } else { |
| 696 | modifiedPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 697 | } |
| 698 | } |
| 699 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 700 | ScopedContentEntry content(this, *paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 701 | if (!content.entry()) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 702 | return; |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 703 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 704 | SkDynamicMemoryWStream* contentStream = content.stream(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 705 | switch (mode) { |
| 706 | case SkCanvas::kPolygon_PointMode: |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 707 | SkPDFUtils::MoveTo(points[0].fX, points[0].fY, contentStream); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 708 | for (size_t i = 1; i < count; i++) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 709 | SkPDFUtils::AppendLine(points[i].fX, points[i].fY, contentStream); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 710 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 711 | SkPDFUtils::StrokePath(contentStream); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 712 | break; |
| 713 | case SkCanvas::kLines_PointMode: |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 714 | for (size_t i = 0; i < count/2; i++) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 715 | SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, contentStream); |
| 716 | SkPDFUtils::AppendLine(points[i * 2 + 1].fX, points[i * 2 + 1].fY, contentStream); |
| 717 | SkPDFUtils::StrokePath(contentStream); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 718 | } |
| 719 | break; |
| 720 | case SkCanvas::kPoints_PointMode: |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 721 | SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap); |
| 722 | for (size_t i = 0; i < count; i++) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 723 | SkPDFUtils::MoveTo(points[i].fX, points[i].fY, contentStream); |
| 724 | SkPDFUtils::ClosePath(contentStream); |
| 725 | SkPDFUtils::StrokePath(contentStream); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 726 | } |
| 727 | break; |
| 728 | default: |
| 729 | SkASSERT(false); |
| 730 | } |
| 731 | } |
| 732 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 733 | static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 734 | auto annotation = sk_make_sp<SkPDFDict>("Annot"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 735 | annotation->insertName("Subtype", "Link"); |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 736 | annotation->insertInt("F", 4); // required by ISO 19005 |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 737 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 738 | auto border = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 739 | border->reserve(3); |
| 740 | border->appendInt(0); // Horizontal corner radius. |
| 741 | border->appendInt(0); // Vertical corner radius. |
| 742 | border->appendInt(0); // Width, 0 = no border. |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 743 | annotation->insertObject("Border", std::move(border)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 744 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 745 | auto rect = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 746 | rect->reserve(4); |
| 747 | rect->appendScalar(translatedRect.fLeft); |
| 748 | rect->appendScalar(translatedRect.fTop); |
| 749 | rect->appendScalar(translatedRect.fRight); |
| 750 | rect->appendScalar(translatedRect.fBottom); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 751 | annotation->insertObject("Rect", std::move(rect)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 752 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 753 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 754 | } |
| 755 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 756 | static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) { |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 757 | sk_sp<SkPDFDict> annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 758 | SkString url(static_cast<const char *>(urlData->data()), |
| 759 | urlData->size() - 1); |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 760 | auto action = sk_make_sp<SkPDFDict>("Action"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 761 | action->insertName("S", "URI"); |
| 762 | action->insertString("URI", url); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 763 | annotation->insertObject("A", std::move(action)); |
| 764 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 765 | } |
| 766 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 767 | static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData, |
| 768 | const SkRect& r) { |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 769 | sk_sp<SkPDFDict> annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 770 | SkString name(static_cast<const char *>(nameData->data()), |
| 771 | nameData->size() - 1); |
| 772 | annotation->insertName("Dest", name); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 773 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 776 | void SkPDFDevice::drawRect(const SkRect& rect, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 777 | const SkPaint& srcPaint) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 778 | if (this->hasEmptyClip()) { |
| 779 | return; |
| 780 | } |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 781 | SkPaint paint = srcPaint; |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 782 | remove_color_filter(&paint); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 783 | replace_srcmode_on_opaque_paint(&paint); |
commit-bot@chromium.org | 969fd6a | 2013-05-14 18:16:40 +0000 | [diff] [blame] | 784 | SkRect r = rect; |
| 785 | r.sort(); |
| 786 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 787 | if (paint.getPathEffect() || paint.getMaskFilter()) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 788 | SkPath path; |
| 789 | path.addRect(r); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 790 | this->drawPath(path, paint, nullptr, true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 791 | return; |
| 792 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 793 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 794 | ScopedContentEntry content(this, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 795 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 796 | return; |
| 797 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 798 | SkPDFUtils::AppendRectangle(r, content.stream()); |
| 799 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, content.stream()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 802 | void SkPDFDevice::drawRRect(const SkRRect& rrect, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 803 | const SkPaint& srcPaint) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 804 | if (this->hasEmptyClip()) { |
| 805 | return; |
| 806 | } |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 807 | SkPaint paint = srcPaint; |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 808 | remove_color_filter(&paint); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 809 | replace_srcmode_on_opaque_paint(&paint); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 810 | SkPath path; |
| 811 | path.addRRect(rrect); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 812 | this->drawPath(path, paint, nullptr, true); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 815 | void SkPDFDevice::drawOval(const SkRect& oval, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 816 | const SkPaint& srcPaint) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 817 | if (this->hasEmptyClip()) { |
| 818 | return; |
| 819 | } |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 820 | SkPaint paint = srcPaint; |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 821 | remove_color_filter(&paint); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 822 | replace_srcmode_on_opaque_paint(&paint); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 823 | SkPath path; |
| 824 | path.addOval(oval); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 825 | this->drawPath(path, paint, nullptr, true); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 828 | void SkPDFDevice::drawPath(const SkPath& origPath, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 829 | const SkPaint& srcPaint, |
| 830 | const SkMatrix* prePathMatrix, |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 831 | bool pathIsMutable) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 832 | this->internalDrawPath( |
| 833 | this->cs(), this->ctm(), origPath, srcPaint, prePathMatrix, pathIsMutable); |
| 834 | } |
| 835 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 836 | void SkPDFDevice::internalDrawPathWithFilter(const SkClipStack& clipStack, |
| 837 | const SkMatrix& ctm, |
| 838 | const SkPath& origPath, |
| 839 | const SkPaint& origPaint, |
| 840 | const SkMatrix* prePathMatrix) { |
| 841 | SkASSERT(origPaint.getMaskFilter()); |
| 842 | SkPath path(origPath); |
| 843 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
| 844 | if (prePathMatrix) { |
| 845 | path.transform(*prePathMatrix, &path); |
| 846 | } |
| 847 | SkStrokeRec::InitStyle initStyle = paint->getFillPath(path, &path) |
| 848 | ? SkStrokeRec::kFill_InitStyle |
| 849 | : SkStrokeRec::kHairline_InitStyle; |
| 850 | path.transform(ctm, &path); |
| 851 | |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 852 | SkIRect bounds = clipStack.bounds(this->bounds()).roundOut(); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 853 | SkMask sourceMask; |
| 854 | if (!SkDraw::DrawToMask(path, &bounds, paint->getMaskFilter(), &SkMatrix::I(), |
| 855 | &sourceMask, SkMask::kComputeBoundsAndRenderImage_CreateMode, |
| 856 | initStyle)) { |
| 857 | return; |
| 858 | } |
| 859 | SkAutoMaskFreeImage srcAutoMaskFreeImage(sourceMask.fImage); |
| 860 | SkMask dstMask; |
| 861 | SkIPoint margin; |
Mike Reed | 80747ef | 2018-01-23 15:29:32 -0500 | [diff] [blame] | 862 | if (!as_MFB(paint->getMaskFilter())->filterMask(&dstMask, sourceMask, ctm, &margin)) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 863 | return; |
| 864 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 865 | SkIRect dstMaskBounds = dstMask.fBounds; |
| 866 | sk_sp<SkImage> mask = mask_to_greyscale_image(&dstMask); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 867 | // PDF doesn't seem to allow masking vector graphics with an Image XObject. |
| 868 | // Must mask with a Form XObject. |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 869 | sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice(); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 870 | { |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 871 | SkCanvas canvas(maskDevice); |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 872 | canvas.drawImage(mask, dstMaskBounds.x(), dstMaskBounds.y()); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 873 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 874 | if (!ctm.isIdentity() && paint->getShader()) { |
| 875 | transform_shader(paint.writable(), ctm); // Since we are using identity matrix. |
| 876 | } |
| 877 | ScopedContentEntry content(this, clipStack, SkMatrix::I(), *paint); |
| 878 | if (!content.entry()) { |
| 879 | return; |
| 880 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 881 | this->addSMaskGraphicState(std::move(maskDevice), content.stream()); |
| 882 | SkPDFUtils::AppendRectangle(SkRect::Make(dstMaskBounds), content.stream()); |
| 883 | SkPDFUtils::PaintPath(SkPaint::kFill_Style, path.getFillType(), content.stream()); |
| 884 | this->clearMaskOnGraphicState(content.stream()); |
| 885 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 886 | |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 887 | void SkPDFDevice::addSMaskGraphicState(sk_sp<SkPDFDevice> maskDevice, |
| 888 | SkDynamicMemoryWStream* contentStream) { |
| 889 | sk_sp<SkPDFDict> sMaskGS = SkPDFGraphicState::GetSMaskGraphicState( |
Hal Canary | b4bd5ef | 2017-07-26 09:16:01 -0400 | [diff] [blame] | 890 | maskDevice->makeFormXObjectFromDevice(true), false, |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 891 | SkPDFGraphicState::kLuminosity_SMaskMode, this->getCanon()); |
| 892 | SkPDFUtils::ApplyGraphicState(this->addGraphicStateResource(sMaskGS.get()), contentStream); |
| 893 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 894 | |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 895 | void SkPDFDevice::clearMaskOnGraphicState(SkDynamicMemoryWStream* contentStream) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 896 | // The no-softmask graphic state is used to "turn off" the mask for later draw calls. |
Hal Canary | c02de0b | 2017-06-28 13:14:03 -0400 | [diff] [blame] | 897 | sk_sp<SkPDFDict>& noSMaskGS = this->getCanon()->fNoSmaskGraphicState; |
| 898 | if (!noSMaskGS) { |
| 899 | noSMaskGS = sk_make_sp<SkPDFDict>("ExtGState"); |
| 900 | noSMaskGS->insertName("SMask", "None"); |
| 901 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 902 | SkPDFUtils::ApplyGraphicState(this->addGraphicStateResource(noSMaskGS.get()), contentStream); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 903 | } |
| 904 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 905 | void SkPDFDevice::internalDrawPath(const SkClipStack& clipStack, |
| 906 | const SkMatrix& ctm, |
| 907 | const SkPath& origPath, |
| 908 | const SkPaint& srcPaint, |
| 909 | const SkMatrix* prePathMatrix, |
| 910 | bool pathIsMutable) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 911 | if (clipStack.isEmpty(this->bounds())) { |
| 912 | return; |
| 913 | } |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 914 | SkPaint paint = srcPaint; |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 915 | remove_color_filter(&paint); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 916 | replace_srcmode_on_opaque_paint(&paint); |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 917 | SkPath modifiedPath; |
| 918 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 919 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 920 | if (paint.getMaskFilter()) { |
| 921 | this->internalDrawPathWithFilter(clipStack, ctm, origPath, paint, prePathMatrix); |
| 922 | return; |
| 923 | } |
| 924 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 925 | SkMatrix matrix = ctm; |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 926 | if (prePathMatrix) { |
| 927 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 928 | if (!pathIsMutable) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 929 | pathPtr = &modifiedPath; |
| 930 | pathIsMutable = true; |
| 931 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 932 | origPath.transform(*prePathMatrix, pathPtr); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 933 | } else { |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 934 | matrix.preConcat(*prePathMatrix); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 937 | |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 938 | if (paint.getPathEffect()) { |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 939 | if (clipStack.isEmpty(this->bounds())) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 940 | return; |
| 941 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 942 | if (!pathIsMutable) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 943 | modifiedPath = origPath; |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 944 | pathPtr = &modifiedPath; |
| 945 | pathIsMutable = true; |
| 946 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 947 | if (paint.getFillPath(*pathPtr, pathPtr)) { |
| 948 | paint.setStyle(SkPaint::kFill_Style); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 949 | } else { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 950 | paint.setStyle(SkPaint::kStroke_Style); |
| 951 | paint.setStrokeWidth(0); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 952 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 953 | paint.setPathEffect(nullptr); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 954 | } |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 955 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 956 | if (this->handleInversePath(*pathPtr, paint, pathIsMutable, prePathMatrix)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 957 | return; |
| 958 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 959 | if (matrix.getType() & SkMatrix::kPerspective_Mask) { |
| 960 | if (!pathIsMutable) { |
| 961 | modifiedPath = origPath; |
| 962 | pathPtr = &modifiedPath; |
| 963 | pathIsMutable = true; |
| 964 | } |
| 965 | pathPtr->transform(matrix); |
| 966 | if (paint.getShader()) { |
| 967 | transform_shader(&paint, matrix); |
| 968 | } |
| 969 | matrix = SkMatrix::I(); |
| 970 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 971 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 972 | ScopedContentEntry content(this, clipStack, matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 973 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 974 | return; |
| 975 | } |
Hal Canary | dfaa057 | 2017-11-27 09:33:44 -0500 | [diff] [blame] | 976 | constexpr SkScalar kToleranceScale = 0.0625f; // smaller = better conics (circles). |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 977 | SkScalar matrixScale = matrix.mapRadius(1.0f); |
Hal Canary | dfaa057 | 2017-11-27 09:33:44 -0500 | [diff] [blame] | 978 | SkScalar tolerance = matrixScale > 0.0f ? kToleranceScale / matrixScale : kToleranceScale; |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 979 | bool consumeDegeratePathSegments = |
| 980 | paint.getStyle() == SkPaint::kFill_Style || |
| 981 | (paint.getStrokeCap() != SkPaint::kRound_Cap && |
| 982 | paint.getStrokeCap() != SkPaint::kSquare_Cap); |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 983 | SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(), consumeDegeratePathSegments, content.stream(), |
Hal Canary | 385468f | 2017-02-13 11:03:23 -0500 | [diff] [blame] | 984 | tolerance); |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 985 | SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(), content.stream()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 986 | } |
| 987 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 988 | //////////////////////////////////////////////////////////////////////////////// |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 989 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 990 | void SkPDFDevice::drawImageRect(const SkImage* image, |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 991 | const SkRect* src, |
| 992 | const SkRect& dst, |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 993 | const SkPaint& paint, |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 994 | SkCanvas::SrcRectConstraint) { |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 995 | SkASSERT(image); |
| 996 | this->internalDrawImageRect(SkKeyedImage(sk_ref_sp(const_cast<SkImage*>(image))), |
| 997 | src, dst, paint, this->ctm()); |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 998 | } |
| 999 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1000 | void SkPDFDevice::drawBitmapRect(const SkBitmap& bm, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1001 | const SkRect* src, |
| 1002 | const SkRect& dst, |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1003 | const SkPaint& paint, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1004 | SkCanvas::SrcRectConstraint) { |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1005 | SkASSERT(!bm.drawsNothing()); |
| 1006 | this->internalDrawImageRect(SkKeyedImage(bm), src, dst, paint, this->ctm()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1009 | void SkPDFDevice::drawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y, const SkPaint& paint) { |
| 1010 | SkASSERT(!bm.drawsNothing()); |
| 1011 | auto r = SkRect::MakeXYWH(x, y, bm.width(), bm.height()); |
| 1012 | this->internalDrawImageRect(SkKeyedImage(bm), nullptr, r, paint, this->ctm()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1015 | void SkPDFDevice::drawSprite(const SkBitmap& bm, int x, int y, const SkPaint& paint) { |
| 1016 | SkASSERT(!bm.drawsNothing()); |
| 1017 | auto r = SkRect::MakeXYWH(x, y, bm.width(), bm.height()); |
| 1018 | this->internalDrawImageRect(SkKeyedImage(bm), nullptr, r, paint, SkMatrix::I()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1021 | void SkPDFDevice::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint& paint) { |
| 1022 | SkASSERT(image); |
| 1023 | auto r = SkRect::MakeXYWH(x, y, image->width(), image->height()); |
| 1024 | this->internalDrawImageRect(SkKeyedImage(sk_ref_sp(const_cast<SkImage*>(image))), |
| 1025 | nullptr, r, paint, this->ctm()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 1028 | //////////////////////////////////////////////////////////////////////////////// |
| 1029 | |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1030 | namespace { |
| 1031 | class GlyphPositioner { |
| 1032 | public: |
| 1033 | GlyphPositioner(SkDynamicMemoryWStream* content, |
| 1034 | SkScalar textSkewX, |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1035 | bool wideChars, |
| 1036 | bool defaultPositioning, |
| 1037 | SkPoint origin) |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1038 | : fContent(content) |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1039 | , fCurrentMatrixOrigin(origin) |
| 1040 | , fTextSkewX(textSkewX) |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1041 | , fWideChars(wideChars) |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1042 | , fDefaultPositioning(defaultPositioning) { |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1043 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1044 | ~GlyphPositioner() { this->flush(); } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1045 | void flush() { |
| 1046 | if (fInText) { |
| 1047 | fContent->writeText("> Tj\n"); |
| 1048 | fInText = false; |
| 1049 | } |
| 1050 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1051 | void writeGlyph(SkPoint xy, |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1052 | SkScalar advanceWidth, |
| 1053 | uint16_t glyph) { |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1054 | if (!fInitialized) { |
| 1055 | // Flip the text about the x-axis to account for origin swap and include |
| 1056 | // the passed parameters. |
| 1057 | fContent->writeText("1 0 "); |
| 1058 | SkPDFUtils::AppendScalar(-fTextSkewX, fContent); |
| 1059 | fContent->writeText(" -1 "); |
| 1060 | SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.x(), fContent); |
| 1061 | fContent->writeText(" "); |
| 1062 | SkPDFUtils::AppendScalar(fCurrentMatrixOrigin.y(), fContent); |
| 1063 | fContent->writeText(" Tm\n"); |
| 1064 | fCurrentMatrixOrigin.set(0.0f, 0.0f); |
| 1065 | fInitialized = true; |
| 1066 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1067 | if (!fDefaultPositioning) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1068 | SkPoint position = xy - fCurrentMatrixOrigin; |
Hal Canary | 197ca9b | 2017-05-15 10:29:44 -0400 | [diff] [blame] | 1069 | if (position != SkPoint{fXAdvance, 0}) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1070 | this->flush(); |
Herb Derby | c00d862 | 2018-06-18 14:18:39 -0400 | [diff] [blame] | 1071 | SkPDFUtils::AppendScalar(position.x() - position.y() * fTextSkewX, fContent); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1072 | fContent->writeText(" "); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1073 | SkPDFUtils::AppendScalar(-position.y(), fContent); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1074 | fContent->writeText(" Td "); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1075 | fCurrentMatrixOrigin = xy; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1076 | fXAdvance = 0; |
| 1077 | } |
| 1078 | fXAdvance += advanceWidth; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1079 | } |
| 1080 | if (!fInText) { |
| 1081 | fContent->writeText("<"); |
| 1082 | fInText = true; |
| 1083 | } |
| 1084 | if (fWideChars) { |
| 1085 | SkPDFUtils::WriteUInt16BE(fContent, glyph); |
| 1086 | } else { |
| 1087 | SkASSERT(0 == glyph >> 8); |
| 1088 | SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph)); |
| 1089 | } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | private: |
| 1093 | SkDynamicMemoryWStream* fContent; |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1094 | SkPoint fCurrentMatrixOrigin; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1095 | SkScalar fXAdvance = 0.0f; |
| 1096 | SkScalar fTextSkewX; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1097 | bool fWideChars; |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1098 | bool fInText = false; |
| 1099 | bool fInitialized = false; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1100 | const bool fDefaultPositioning; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1101 | }; |
| 1102 | } // namespace |
| 1103 | |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1104 | static SkUnichar map_glyph(const std::vector<SkUnichar>& glyphToUnicode, SkGlyphID glyph) { |
| 1105 | return glyph < glyphToUnicode.size() ? glyphToUnicode[SkToInt(glyph)] : -1; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
halcanary | c2f9ec1 | 2016-09-12 08:55:29 -0700 | [diff] [blame] | 1108 | static void update_font(SkWStream* wStream, int fontIndex, SkScalar textSize) { |
| 1109 | wStream->writeText("/"); |
| 1110 | char prefix = SkPDFResourceDict::GetResourceTypePrefix(SkPDFResourceDict::kFont_ResourceType); |
| 1111 | wStream->write(&prefix, 1); |
| 1112 | wStream->writeDecAsText(fontIndex); |
| 1113 | wStream->writeText(" "); |
| 1114 | SkPDFUtils::AppendScalar(textSize, wStream); |
| 1115 | wStream->writeText(" Tf\n"); |
| 1116 | } |
| 1117 | |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 1118 | static SkPath draw_text_as_path(const void* sourceText, size_t sourceByteCount, |
| 1119 | const SkScalar pos[], SkTextBlob::GlyphPositioning positioning, |
| 1120 | SkPoint offset, const SkPaint& srcPaint) { |
| 1121 | SkPath path; |
| 1122 | int glyphCount; |
| 1123 | SkAutoTMalloc<SkPoint> tmpPoints; |
| 1124 | switch (positioning) { |
| 1125 | case SkTextBlob::kDefault_Positioning: |
| 1126 | srcPaint.getTextPath(sourceText, sourceByteCount, offset.x(), offset.y(), &path); |
| 1127 | break; |
| 1128 | case SkTextBlob::kHorizontal_Positioning: |
| 1129 | glyphCount = srcPaint.countText(sourceText, sourceByteCount); |
| 1130 | tmpPoints.realloc(glyphCount); |
| 1131 | for (int i = 0; i < glyphCount; ++i) { |
| 1132 | tmpPoints[i] = {pos[i] + offset.x(), offset.y()}; |
| 1133 | } |
| 1134 | srcPaint.getPosTextPath(sourceText, sourceByteCount, tmpPoints.get(), &path); |
| 1135 | break; |
| 1136 | case SkTextBlob::kFull_Positioning: |
| 1137 | srcPaint.getPosTextPath(sourceText, sourceByteCount, (const SkPoint*)pos, &path); |
| 1138 | path.offset(offset.x(), offset.y()); |
| 1139 | break; |
| 1140 | } |
| 1141 | return path; |
| 1142 | } |
| 1143 | |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1144 | static bool has_outline_glyph(SkGlyphID gid, SkGlyphCache* cache) { |
| 1145 | const SkGlyph& glyph = cache->getGlyphIDMetrics(gid); |
| 1146 | const SkPath* path = cache->findPath(glyph); |
| 1147 | return (path && !path->isEmpty()) || (glyph.fWidth == 0 && glyph.fHeight == 0); |
| 1148 | } |
| 1149 | |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 1150 | static SkRect get_glyph_bounds_device_space(SkGlyphID gid, SkGlyphCache* cache, |
| 1151 | SkScalar xScale, SkScalar yScale, |
| 1152 | SkPoint xy, const SkMatrix& ctm) { |
| 1153 | const SkGlyph& glyph = cache->getGlyphIDMetrics(gid); |
| 1154 | SkRect glyphBounds = {glyph.fLeft * xScale, |
| 1155 | glyph.fTop * yScale, |
| 1156 | (glyph.fLeft + glyph.fWidth) * xScale, |
| 1157 | (glyph.fTop + glyph.fHeight) * yScale}; |
| 1158 | glyphBounds.offset(xy); |
| 1159 | ctm.mapRect(&glyphBounds); // now in dev space. |
| 1160 | return glyphBounds; |
| 1161 | } |
| 1162 | |
| 1163 | static bool contains(const SkRect& r, SkPoint p) { |
| 1164 | return r.left() <= p.x() && p.x() <= r.right() && |
| 1165 | r.top() <= p.y() && p.y() <= r.bottom(); |
| 1166 | } |
| 1167 | |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1168 | static sk_sp<SkImage> image_from_mask(const SkMask& mask) { |
| 1169 | if (!mask.fImage) { |
| 1170 | return nullptr; |
| 1171 | } |
| 1172 | SkIRect bounds = mask.fBounds; |
| 1173 | SkBitmap bm; |
| 1174 | switch (mask.fFormat) { |
| 1175 | case SkMask::kBW_Format: |
| 1176 | bm.allocPixels(SkImageInfo::MakeA8(bounds.width(), bounds.height())); |
| 1177 | for (int y = 0; y < bm.height(); ++y) { |
| 1178 | for (int x8 = 0; x8 < bm.width(); x8 += 8) { |
| 1179 | uint8_t v = *mask.getAddr1(x8 + bounds.x(), y + bounds.y()); |
| 1180 | int e = SkTMin(x8 + 8, bm.width()); |
| 1181 | for (int x = x8; x < e; ++x) { |
| 1182 | *bm.getAddr8(x, y) = (v >> (x & 0x7)) & 0x1 ? 0xFF : 0x00; |
| 1183 | } |
| 1184 | } |
| 1185 | } |
| 1186 | bm.setImmutable(); |
| 1187 | return SkImage::MakeFromBitmap(bm); |
| 1188 | case SkMask::kA8_Format: |
| 1189 | bm.installPixels(SkImageInfo::MakeA8(bounds.width(), bounds.height()), |
| 1190 | mask.fImage, mask.fRowBytes); |
| 1191 | return SkMakeImageFromRasterBitmap(bm, kAlways_SkCopyPixelsMode); |
| 1192 | case SkMask::kARGB32_Format: |
| 1193 | bm.installPixels(SkImageInfo::MakeN32Premul(bounds.width(), bounds.height()), |
| 1194 | mask.fImage, mask.fRowBytes); |
| 1195 | return SkMakeImageFromRasterBitmap(bm, kAlways_SkCopyPixelsMode); |
| 1196 | case SkMask::k3D_Format: |
| 1197 | SkASSERT(false); |
| 1198 | return nullptr; |
| 1199 | case SkMask::kLCD16_Format: |
| 1200 | SkASSERT(false); |
| 1201 | return nullptr; |
| 1202 | default: |
| 1203 | SkASSERT(false); |
| 1204 | return nullptr; |
| 1205 | } |
| 1206 | } |
| 1207 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1208 | void SkPDFDevice::internalDrawText( |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1209 | const void* sourceText, size_t sourceByteCount, |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1210 | const SkScalar pos[], SkTextBlob::GlyphPositioning positioning, |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1211 | SkPoint offset, const SkPaint& srcPaint, const uint32_t* clusters, |
| 1212 | uint32_t textByteLength, const char* utf8Text) { |
Hal Canary | 29ebe7a | 2017-10-18 10:04:57 -0400 | [diff] [blame] | 1213 | if (0 == sourceByteCount || !sourceText || srcPaint.getTextSize() <= 0) { |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 1214 | return; |
| 1215 | } |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 1216 | if (this->hasEmptyClip()) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1217 | return; |
| 1218 | } |
halcanary | ea17dfe | 2016-08-24 09:20:57 -0700 | [diff] [blame] | 1219 | NOT_IMPLEMENTED(srcPaint.isVerticalText(), false); |
| 1220 | if (srcPaint.isVerticalText()) { |
| 1221 | // Don't pretend we support drawing vertical text. It is not |
| 1222 | // clear to me how to switch to "vertical writing" mode in PDF. |
| 1223 | // Currently neither Chromium or Android set this flag. |
| 1224 | // https://bug.skia.org/5665 |
halcanary | ea17dfe | 2016-08-24 09:20:57 -0700 | [diff] [blame] | 1225 | } |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 1226 | if (srcPaint.getPathEffect() |
| 1227 | || srcPaint.getMaskFilter() |
| 1228 | || SkPaint::kFill_Style != srcPaint.getStyle()) { |
| 1229 | // Stroked Text doesn't work well with Type3 fonts. |
| 1230 | SkPath path = draw_text_as_path(sourceText, sourceByteCount, pos, |
| 1231 | positioning, offset, srcPaint); |
| 1232 | this->drawPath(path, srcPaint, nullptr, true); |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1233 | return; |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1234 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1235 | SkPaint paint = calculate_text_paint(srcPaint); |
Hal Canary | d12a676 | 2017-05-26 17:01:16 -0400 | [diff] [blame] | 1236 | remove_color_filter(&paint); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1237 | replace_srcmode_on_opaque_paint(&paint); |
| 1238 | if (!paint.getTypeface()) { |
| 1239 | paint.setTypeface(SkTypeface::MakeDefault()); |
| 1240 | } |
| 1241 | SkTypeface* typeface = paint.getTypeface(); |
| 1242 | if (!typeface) { |
| 1243 | SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n"); |
| 1244 | return; |
| 1245 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1246 | |
| 1247 | const SkAdvancedTypefaceMetrics* metrics = |
| 1248 | SkPDFFont::GetMetrics(typeface, fDocument->canon()); |
| 1249 | if (!metrics) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1250 | return; |
| 1251 | } |
Hal Canary | 46cc3da | 2018-05-09 11:50:34 -0400 | [diff] [blame] | 1252 | const std::vector<SkUnichar>& glyphToUnicode = SkPDFFont::GetUnicodeMap( |
| 1253 | typeface, fDocument->canon()); |
| 1254 | |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 1255 | SkClusterator clusterator(sourceText, sourceByteCount, paint, |
| 1256 | clusters, textByteLength, utf8Text); |
| 1257 | const SkGlyphID* glyphs = clusterator.glyphs(); |
| 1258 | uint32_t glyphCount = clusterator.glyphCount(); |
| 1259 | if (glyphCount == 0) { |
halcanary | 2bd295e | 2016-09-16 08:15:56 -0700 | [diff] [blame] | 1260 | return; |
| 1261 | } |
halcanary | 3b294d5 | 2016-09-16 13:21:08 -0700 | [diff] [blame] | 1262 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1263 | bool defaultPositioning = (positioning == SkTextBlob::kDefault_Positioning); |
halcanary | 9df5a4c | 2016-08-24 10:08:13 -0700 | [diff] [blame] | 1264 | paint.setHinting(SkPaint::kNo_Hinting); |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 1265 | |
| 1266 | int emSize; |
Herb Derby | 1a605cd | 2018-03-22 11:16:25 -0400 | [diff] [blame] | 1267 | auto glyphCache = SkPDFFont::MakeVectorCache(typeface, &emSize); |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 1268 | |
| 1269 | SkScalar textSize = paint.getTextSize(); |
| 1270 | SkScalar advanceScale = textSize * paint.getTextScaleX() / emSize; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1271 | |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 1272 | // textScaleX and textScaleY are used to get a conservative bounding box for glyphs. |
| 1273 | SkScalar textScaleY = textSize / emSize; |
| 1274 | SkScalar textScaleX = advanceScale + paint.getTextSkewX() * textScaleY; |
| 1275 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1276 | SkPaint::Align alignment = paint.getTextAlign(); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1277 | float alignmentFactor = SkPaint::kLeft_Align == alignment ? 0.0f : |
| 1278 | SkPaint::kCenter_Align == alignment ? -0.5f : |
| 1279 | /* SkPaint::kRight_Align */ -1.0f; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1280 | if (defaultPositioning && alignment != SkPaint::kLeft_Align) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1281 | SkScalar advance = 0; |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 1282 | for (uint32_t i = 0; i < glyphCount; ++i) { |
Hal Canary | aa3af7b | 2017-03-06 16:18:49 -0500 | [diff] [blame] | 1283 | advance += advanceScale * glyphCache->getGlyphIDAdvance(glyphs[i]).fAdvanceX; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1284 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1285 | offset.offset(alignmentFactor * advance, 0); |
halcanary | 6059dc3 | 2016-08-15 11:45:36 -0700 | [diff] [blame] | 1286 | } |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 1287 | SkRect clipStackBounds = this->cs().bounds(this->bounds()); |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1288 | struct PositionedGlyph { |
| 1289 | SkPoint fPos; |
| 1290 | SkGlyphID fGlyph; |
| 1291 | }; |
| 1292 | SkTArray<PositionedGlyph> fMissingGlyphs; |
| 1293 | { |
| 1294 | ScopedContentEntry content(this, paint, true); |
| 1295 | if (!content.entry()) { |
| 1296 | return; |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1297 | } |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1298 | SkDynamicMemoryWStream* out = content.stream(); |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1299 | |
| 1300 | out->writeText("BT\n"); |
| 1301 | SK_AT_SCOPE_EXIT(out->writeText("ET\n")); |
| 1302 | |
| 1303 | const SkGlyphID maxGlyphID = SkToU16(typeface->countGlyphs() - 1); |
| 1304 | |
| 1305 | bool multiByteGlyphs = SkPDFFont::IsMultiByte(SkPDFFont::FontType(*metrics)); |
| 1306 | if (clusterator.reversedChars()) { |
| 1307 | out->writeText("/ReversedChars BMC\n"); |
| 1308 | } |
| 1309 | SK_AT_SCOPE_EXIT(if (clusterator.reversedChars()) { out->writeText("EMC\n"); } ); |
| 1310 | GlyphPositioner glyphPositioner(out, |
| 1311 | paint.getTextSkewX(), |
| 1312 | multiByteGlyphs, |
| 1313 | defaultPositioning, |
| 1314 | offset); |
| 1315 | SkPDFFont* font = nullptr; |
| 1316 | |
Hal Canary | 1521c8a | 2018-03-28 09:51:00 -0700 | [diff] [blame] | 1317 | while (SkClusterator::Cluster c = clusterator.next()) { |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1318 | int index = c.fGlyphIndex; |
| 1319 | int glyphLimit = index + c.fGlyphCount; |
| 1320 | |
| 1321 | bool actualText = false; |
| 1322 | SK_AT_SCOPE_EXIT(if (actualText) { |
| 1323 | glyphPositioner.flush(); |
| 1324 | out->writeText("EMC\n"); |
| 1325 | }); |
| 1326 | if (c.fUtf8Text) { // real cluster |
| 1327 | // Check if `/ActualText` needed. |
| 1328 | const char* textPtr = c.fUtf8Text; |
| 1329 | const char* textEnd = c.fUtf8Text + c.fTextByteLength; |
| 1330 | SkUnichar unichar = SkUTF8_NextUnicharWithError(&textPtr, textEnd); |
| 1331 | if (unichar < 0) { |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1332 | return; |
| 1333 | } |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1334 | if (textPtr < textEnd || // more characters left |
| 1335 | glyphLimit > index + 1 || // toUnicode wouldn't work |
| 1336 | unichar != map_glyph(glyphToUnicode, glyphs[index])) // test single Unichar map |
| 1337 | { |
| 1338 | glyphPositioner.flush(); |
| 1339 | out->writeText("/Span<</ActualText <"); |
| 1340 | SkPDFUtils::WriteUTF16beHex(out, 0xFEFF); // U+FEFF = BYTE ORDER MARK |
| 1341 | // the BOM marks this text as UTF-16BE, not PDFDocEncoding. |
| 1342 | SkPDFUtils::WriteUTF16beHex(out, unichar); // first char |
| 1343 | while (textPtr < textEnd) { |
| 1344 | unichar = SkUTF8_NextUnicharWithError(&textPtr, textEnd); |
| 1345 | if (unichar < 0) { |
| 1346 | break; |
| 1347 | } |
| 1348 | SkPDFUtils::WriteUTF16beHex(out, unichar); |
| 1349 | } |
| 1350 | out->writeText("> >> BDC\n"); // begin marked-content sequence |
| 1351 | // with an associated property list. |
| 1352 | actualText = true; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1353 | } |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1354 | } |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1355 | for (; index < glyphLimit; ++index) { |
| 1356 | SkGlyphID gid = glyphs[index]; |
| 1357 | if (gid > maxGlyphID) { |
| 1358 | continue; |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1359 | } |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1360 | if (!font || !font->hasGlyph(gid)) { |
| 1361 | // Not yet specified font or need to switch font. |
| 1362 | int fontIndex = this->getFontResourceIndex(typeface, gid); |
| 1363 | // All preconditions for SkPDFFont::GetFontResource are met. |
| 1364 | SkASSERT(fontIndex >= 0); |
| 1365 | if (fontIndex < 0) { |
| 1366 | return; |
| 1367 | } |
| 1368 | glyphPositioner.flush(); |
| 1369 | update_font(out, fontIndex, textSize); |
| 1370 | font = fFontResources[fontIndex]; |
| 1371 | SkASSERT(font); // All preconditions for SkPDFFont::GetFontResource are met. |
| 1372 | if (!font) { |
| 1373 | return; |
| 1374 | } |
| 1375 | SkASSERT(font->multiByteGlyphs() == multiByteGlyphs); |
| 1376 | } |
| 1377 | SkPoint xy = {0, 0}; |
| 1378 | SkScalar advance = advanceScale * glyphCache->getGlyphIDAdvance(gid).fAdvanceX; |
| 1379 | if (!defaultPositioning) { |
| 1380 | xy = SkTextBlob::kFull_Positioning == positioning |
| 1381 | ? SkPoint{pos[2 * index], pos[2 * index + 1]} |
| 1382 | : SkPoint{pos[index], 0}; |
| 1383 | if (alignment != SkPaint::kLeft_Align) { |
| 1384 | xy.offset(alignmentFactor * advance, 0); |
| 1385 | } |
| 1386 | // Do a glyph-by-glyph bounds-reject if positions are absolute. |
| 1387 | SkRect glyphBounds = get_glyph_bounds_device_space( |
| 1388 | gid, glyphCache.get(), textScaleX, textScaleY, |
| 1389 | xy + offset, this->ctm()); |
| 1390 | if (glyphBounds.isEmpty()) { |
| 1391 | if (!contains(clipStackBounds, {glyphBounds.x(), glyphBounds.y()})) { |
| 1392 | continue; |
| 1393 | } |
| 1394 | } else { |
| 1395 | if (!clipStackBounds.intersects(glyphBounds)) { |
| 1396 | continue; // reject glyphs as out of bounds |
| 1397 | } |
| 1398 | } |
| 1399 | if (!has_outline_glyph(gid, glyphCache.get())) { |
Hal Canary | 10ccb02 | 2017-07-19 10:12:11 -0400 | [diff] [blame] | 1400 | fMissingGlyphs.push_back({xy + offset, gid}); |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 1401 | } |
| 1402 | } else { |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1403 | if (!has_outline_glyph(gid, glyphCache.get())) { |
| 1404 | fMissingGlyphs.push_back({offset, gid}); |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 1405 | } |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1406 | offset += SkPoint{advance, 0}; |
Hal Canary | 699b873 | 2017-06-13 12:13:29 -0400 | [diff] [blame] | 1407 | } |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1408 | font->noteGlyphUsage(gid); |
| 1409 | |
| 1410 | SkGlyphID encodedGlyph = multiByteGlyphs ? gid : font->glyphToPDFFontEncoding(gid); |
| 1411 | glyphPositioner.writeGlyph(xy, advance, encodedGlyph); |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1412 | } |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | if (fMissingGlyphs.count() > 0) { |
| 1416 | // Fall back on images. |
| 1417 | SkPaint scaledGlyphCachePaint; |
| 1418 | scaledGlyphCachePaint.setTextSize(paint.getTextSize()); |
| 1419 | scaledGlyphCachePaint.setTextScaleX(paint.getTextScaleX()); |
| 1420 | scaledGlyphCachePaint.setTextSkewX(paint.getTextSkewX()); |
| 1421 | scaledGlyphCachePaint.setTypeface(sk_ref_sp(typeface)); |
Herb Derby | fa99690 | 2018-04-18 11:36:12 -0400 | [diff] [blame] | 1422 | auto scaledGlyphCache = SkStrikeCache::FindOrCreateStrikeExclusive(scaledGlyphCachePaint); |
Hal Canary | 9b9510a | 2017-07-18 09:39:00 -0400 | [diff] [blame] | 1423 | SkTHashMap<SkPDFCanon::BitmapGlyphKey, SkPDFCanon::BitmapGlyph>* map = |
| 1424 | &this->getCanon()->fBitmapGlyphImages; |
| 1425 | for (PositionedGlyph positionedGlyph : fMissingGlyphs) { |
| 1426 | SkPDFCanon::BitmapGlyphKey key = {typeface->uniqueID(), |
| 1427 | paint.getTextSize(), |
| 1428 | paint.getTextScaleX(), |
| 1429 | paint.getTextSkewX(), |
| 1430 | positionedGlyph.fGlyph, |
| 1431 | 0}; |
| 1432 | SkImage* img = nullptr; |
| 1433 | SkIPoint imgOffset = {0, 0}; |
| 1434 | if (SkPDFCanon::BitmapGlyph* ptr = map->find(key)) { |
| 1435 | img = ptr->fImage.get(); |
| 1436 | imgOffset = ptr->fOffset; |
| 1437 | } else { |
| 1438 | (void)scaledGlyphCache->findImage( |
| 1439 | scaledGlyphCache->getGlyphIDMetrics(positionedGlyph.fGlyph)); |
| 1440 | SkMask mask; |
| 1441 | scaledGlyphCache->getGlyphIDMetrics(positionedGlyph.fGlyph).toMask(&mask); |
| 1442 | imgOffset = {mask.fBounds.x(), mask.fBounds.y()}; |
| 1443 | img = map->set(key, {image_from_mask(mask), imgOffset})->fImage.get(); |
| 1444 | } |
| 1445 | if (img) { |
| 1446 | SkPoint pt = positionedGlyph.fPos + |
| 1447 | SkPoint{(SkScalar)imgOffset.x(), (SkScalar)imgOffset.y()}; |
| 1448 | this->drawImage(img, pt.x(), pt.y(), srcPaint); |
| 1449 | } |
halcanary | f59d18a | 2016-09-16 14:44:57 -0700 | [diff] [blame] | 1450 | } |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1451 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1454 | void SkPDFDevice::drawPosText(const void* text, size_t len, |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1455 | const SkScalar pos[], int scalarsPerPos, |
| 1456 | const SkPoint& offset, const SkPaint& paint) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1457 | this->internalDrawText(text, len, pos, (SkTextBlob::GlyphPositioning)scalarsPerPos, |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1458 | offset, paint, nullptr, 0, nullptr); |
| 1459 | } |
| 1460 | |
Robert Phillips | 3858045 | 2018-06-28 12:00:35 +0000 | [diff] [blame] | 1461 | void SkPDFDevice::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 1462 | const SkPaint &paint, SkDrawFilter* drawFilter) { |
| 1463 | for (SkTextBlobRunIterator it(blob); !it.done(); it.next()) { |
| 1464 | SkPaint runPaint(paint); |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1465 | it.applyFontToPaint(&runPaint); |
Robert Phillips | 3858045 | 2018-06-28 12:00:35 +0000 | [diff] [blame] | 1466 | if (drawFilter && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) { |
| 1467 | continue; |
| 1468 | } |
| 1469 | SkPoint offset = it.offset() + SkPoint{x, y}; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1470 | this->internalDrawText(it.glyphs(), sizeof(SkGlyphID) * it.glyphCount(), |
Robert Phillips | 3858045 | 2018-06-28 12:00:35 +0000 | [diff] [blame] | 1471 | it.pos(), it.positioning(), offset, runPaint, |
halcanary | e06ca96 | 2016-09-09 05:34:55 -0700 | [diff] [blame] | 1472 | it.clusters(), it.textSize(), it.text()); |
| 1473 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1476 | void SkPDFDevice::drawVertices(const SkVertices*, const SkMatrix*, int, SkBlendMode, |
| 1477 | const SkPaint&) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 1478 | if (this->hasEmptyClip()) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1479 | return; |
| 1480 | } |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1481 | // TODO: implement drawVertices |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1484 | void SkPDFDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& paint) { |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 1485 | SkASSERT(!paint.getImageFilter()); |
| 1486 | |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1487 | // Check if the source device is really a bitmapdevice (because that's what we returned |
| 1488 | // from createDevice (likely due to an imagefilter) |
| 1489 | SkPixmap pmap; |
| 1490 | if (device->peekPixels(&pmap)) { |
| 1491 | SkBitmap bitmap; |
| 1492 | bitmap.installPixels(pmap); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1493 | this->drawSprite(bitmap, x, y, paint); |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1494 | return; |
| 1495 | } |
| 1496 | |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 1497 | // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses. |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1498 | SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1499 | |
| 1500 | SkScalar scalarX = SkIntToScalar(x); |
| 1501 | SkScalar scalarY = SkIntToScalar(y); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1502 | for (const RectWithData& l : pdfDevice->fLinkToURLs) { |
| 1503 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 1504 | fLinkToURLs.emplace_back(RectWithData{r, l.data}); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1505 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1506 | for (const RectWithData& l : pdfDevice->fLinkToDestinations) { |
| 1507 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 1508 | fLinkToDestinations.emplace_back(RectWithData{r, l.data}); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1509 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1510 | for (const NamedDestination& d : pdfDevice->fNamedDestinations) { |
| 1511 | SkPoint p = d.point + SkPoint::Make(scalarX, scalarY); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 1512 | fNamedDestinations.emplace_back(NamedDestination{d.nameData, p}); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
ctguil@chromium.org | f4ff39c | 2011-05-24 19:55:05 +0000 | [diff] [blame] | 1515 | if (pdfDevice->isContentEmpty()) { |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1516 | return; |
| 1517 | } |
| 1518 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1519 | SkMatrix matrix = SkMatrix::MakeTrans(SkIntToScalar(x), SkIntToScalar(y)); |
| 1520 | ScopedContentEntry content(this, this->cs(), matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1521 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1522 | return; |
| 1523 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1524 | if (content.needShape()) { |
| 1525 | SkPath shape; |
| 1526 | shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
vandebo@chromium.org | fd3c8c2 | 2013-10-30 21:00:47 +0000 | [diff] [blame] | 1527 | SkIntToScalar(device->width()), |
| 1528 | SkIntToScalar(device->height()))); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1529 | content.setShape(shape); |
| 1530 | } |
| 1531 | if (!content.needSource()) { |
| 1532 | return; |
| 1533 | } |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 1534 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1535 | sk_sp<SkPDFObject> xObject = pdfDevice->makeFormXObjectFromDevice(); |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 1536 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()), content.stream()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1539 | sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 1540 | return SkSurface::MakeRaster(info, &props); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1543 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1544 | sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const { |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1545 | SkTDArray<SkPDFObject*> fonts; |
| 1546 | fonts.setReserve(fFontResources.count()); |
| 1547 | for (SkPDFFont* font : fFontResources) { |
| 1548 | fonts.push(font); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1549 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1550 | return SkPDFResourceDict::Make( |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1551 | &fGraphicStateResources, |
| 1552 | &fShaderResources, |
| 1553 | &fXObjectResources, |
| 1554 | &fonts); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1557 | sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1558 | auto mediaBox = sk_make_sp<SkPDFArray>(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1559 | mediaBox->reserve(4); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 1560 | mediaBox->appendInt(0); |
| 1561 | mediaBox->appendInt(0); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1562 | mediaBox->appendInt(fPageSize.width()); |
| 1563 | mediaBox->appendInt(fPageSize.height()); |
| 1564 | return mediaBox; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1567 | std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const { |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1568 | SkDynamicMemoryWStream buffer; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1569 | if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) { |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1570 | SkPDFUtils::AppendTransform(fInitialTransform, &buffer); |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 1571 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1572 | |
Hal Canary | 6d67396 | 2017-02-22 17:16:59 -0500 | [diff] [blame] | 1573 | GraphicStackState gsState(fExistingClipStack, &buffer); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1574 | for (const auto& entry : fContentEntries) { |
Hal Canary | 4e83ff1 | 2018-03-09 12:16:42 -0500 | [diff] [blame] | 1575 | gsState.updateClip(entry.fState.fClipStack, this->bounds()); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1576 | gsState.updateMatrix(entry.fState.fMatrix); |
| 1577 | gsState.updateDrawingState(entry.fState); |
| 1578 | |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1579 | entry.fContent.writeToStream(&buffer); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1580 | } |
| 1581 | gsState.drainStack(); |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame] | 1582 | if (buffer.bytesWritten() > 0) { |
| 1583 | return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream()); |
| 1584 | } else { |
| 1585 | return skstd::make_unique<SkMemoryStream>(); |
| 1586 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1589 | /* Draws an inverse filled path by using Path Ops to compute the positive |
| 1590 | * inverse using the current clip as the inverse bounds. |
| 1591 | * Return true if this was an inverse path and was properly handled, |
| 1592 | * otherwise returns false and the normal drawing routine should continue, |
| 1593 | * either as a (incorrect) fallback or because the path was not inverse |
| 1594 | * in the first place. |
| 1595 | */ |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1596 | bool SkPDFDevice::handleInversePath(const SkPath& origPath, |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1597 | const SkPaint& paint, bool pathIsMutable, |
| 1598 | const SkMatrix* prePathMatrix) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1599 | if (!origPath.isInverseFillType()) { |
| 1600 | return false; |
| 1601 | } |
| 1602 | |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 1603 | if (this->hasEmptyClip()) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1604 | return false; |
| 1605 | } |
| 1606 | |
| 1607 | SkPath modifiedPath; |
| 1608 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
| 1609 | SkPaint noInversePaint(paint); |
| 1610 | |
| 1611 | // Merge stroking operations into final path. |
| 1612 | if (SkPaint::kStroke_Style == paint.getStyle() || |
| 1613 | SkPaint::kStrokeAndFill_Style == paint.getStyle()) { |
| 1614 | bool doFillPath = paint.getFillPath(origPath, &modifiedPath); |
| 1615 | if (doFillPath) { |
| 1616 | noInversePaint.setStyle(SkPaint::kFill_Style); |
| 1617 | noInversePaint.setStrokeWidth(0); |
| 1618 | pathPtr = &modifiedPath; |
| 1619 | } else { |
| 1620 | // To be consistent with the raster output, hairline strokes |
| 1621 | // are rendered as non-inverted. |
| 1622 | modifiedPath.toggleInverseFillType(); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1623 | this->drawPath(modifiedPath, paint, nullptr, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1624 | return true; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // Get bounds of clip in current transform space |
| 1629 | // (clip bounds are given in device space). |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1630 | SkMatrix transformInverse; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1631 | SkMatrix totalMatrix = this->ctm(); |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1632 | if (prePathMatrix) { |
| 1633 | totalMatrix.preConcat(*prePathMatrix); |
| 1634 | } |
| 1635 | if (!totalMatrix.invert(&transformInverse)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1636 | return false; |
| 1637 | } |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 1638 | SkRect bounds = this->cs().bounds(this->bounds()); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1639 | transformInverse.mapRect(&bounds); |
| 1640 | |
| 1641 | // Extend the bounds by the line width (plus some padding) |
| 1642 | // so the edge doesn't cause a visible stroke. |
| 1643 | bounds.outset(paint.getStrokeWidth() + SK_Scalar1, |
| 1644 | paint.getStrokeWidth() + SK_Scalar1); |
| 1645 | |
| 1646 | if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) { |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1650 | this->drawPath(modifiedPath, noInversePaint, prePathMatrix, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1651 | return true; |
| 1652 | } |
| 1653 | |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1654 | void SkPDFDevice::appendAnnotations(SkPDFArray* array) const { |
| 1655 | array->reserve(fLinkToURLs.count() + fLinkToDestinations.count()); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1656 | for (const RectWithData& rectWithURL : fLinkToURLs) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1657 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1658 | fInitialTransform.mapRect(&r, rectWithURL.rect); |
Hal Canary | 7675b36 | 2018-06-15 15:32:14 -0400 | [diff] [blame] | 1659 | array->appendObjRef(create_link_to_url(rectWithURL.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1660 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1661 | for (const RectWithData& linkToDestination : fLinkToDestinations) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1662 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1663 | fInitialTransform.mapRect(&r, linkToDestination.rect); |
Hal Canary | 7675b36 | 2018-06-15 15:32:14 -0400 | [diff] [blame] | 1664 | array->appendObjRef( |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1665 | create_link_named_dest(linkToDestination.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1666 | } |
| 1667 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1668 | |
halcanary | 6d62270 | 2015-03-25 08:45:42 -0700 | [diff] [blame] | 1669 | void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const { |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1670 | for (const NamedDestination& dest : fNamedDestinations) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1671 | auto pdfDest = sk_make_sp<SkPDFArray>(); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1672 | pdfDest->reserve(5); |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 1673 | pdfDest->appendObjRef(sk_ref_sp(page)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1674 | pdfDest->appendName("XYZ"); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1675 | SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1676 | pdfDest->appendScalar(p.x()); |
| 1677 | pdfDest->appendScalar(p.y()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1678 | pdfDest->appendInt(0); // Leave zoom unchanged |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1679 | SkString name(static_cast<const char*>(dest.nameData->data())); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1680 | dict->insertObject(name, std::move(pdfDest)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1681 | } |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Hal Canary | b4bd5ef | 2017-07-26 09:16:01 -0400 | [diff] [blame] | 1684 | sk_sp<SkPDFObject> SkPDFDevice::makeFormXObjectFromDevice(bool alpha) { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1685 | SkMatrix inverseTransform = SkMatrix::I(); |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1686 | if (!fInitialTransform.isIdentity()) { |
| 1687 | if (!fInitialTransform.invert(&inverseTransform)) { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1688 | SkDEBUGFAIL("Layer initial transform should be invertible."); |
| 1689 | inverseTransform.reset(); |
| 1690 | } |
| 1691 | } |
Hal Canary | b4bd5ef | 2017-07-26 09:16:01 -0400 | [diff] [blame] | 1692 | const char* colorSpace = alpha ? "DeviceGray" : nullptr; |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1693 | sk_sp<SkPDFObject> xobject = |
| 1694 | SkPDFMakeFormXObject(this->content(), this->copyMediaBox(), |
Hal Canary | b4bd5ef | 2017-07-26 09:16:01 -0400 | [diff] [blame] | 1695 | this->makeResourceDict(), inverseTransform, colorSpace); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1696 | // We always draw the form xobjects that we create back into the device, so |
| 1697 | // we simply preserve the font usage instead of pulling it out and merging |
| 1698 | // it back in later. |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1699 | this->cleanUp(); // Reset this device to have no content. |
| 1700 | this->init(); |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1701 | return xobject; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1704 | void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex, |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1705 | sk_sp<SkPDFObject> mask, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1706 | const SkClipStack& clipStack, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1707 | SkBlendMode mode, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1708 | bool invertClip) { |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 1709 | if (!invertClip && clipStack.isEmpty(this->bounds())) { |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1710 | return; |
| 1711 | } |
| 1712 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1713 | sk_sp<SkPDFDict> sMaskGS = SkPDFGraphicState::GetSMaskGraphicState( |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1714 | std::move(mask), invertClip, |
| 1715 | SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon()); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1716 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1717 | SkPaint paint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1718 | paint.setBlendMode(mode); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1719 | ScopedContentEntry content(this, clipStack, SkMatrix::I(), paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1720 | if (!content.entry()) { |
| 1721 | return; |
| 1722 | } |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 1723 | SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), content.stream()); |
| 1724 | SkPDFUtils::DrawFormXObject(xObjectIndex, content.stream()); |
Hal Canary | c02de0b | 2017-06-28 13:14:03 -0400 | [diff] [blame] | 1725 | this->clearMaskOnGraphicState(content.stream()); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1728 | SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack& clipStack, |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 1729 | const SkMatrix& matrix, |
| 1730 | const SkPaint& paint, |
| 1731 | bool hasText, |
| 1732 | sk_sp<SkPDFObject>* dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1733 | *dst = nullptr; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1734 | SkBlendMode blendMode = paint.getBlendMode(); |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1735 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1736 | // For the following modes, we want to handle source and destination |
| 1737 | // separately, so make an object of what's already there. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1738 | if (blendMode == SkBlendMode::kClear || |
| 1739 | blendMode == SkBlendMode::kSrc || |
| 1740 | blendMode == SkBlendMode::kSrcIn || |
| 1741 | blendMode == SkBlendMode::kDstIn || |
| 1742 | blendMode == SkBlendMode::kSrcOut || |
| 1743 | blendMode == SkBlendMode::kDstOut || |
| 1744 | blendMode == SkBlendMode::kSrcATop || |
| 1745 | blendMode == SkBlendMode::kDstATop || |
| 1746 | blendMode == SkBlendMode::kModulate) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1747 | if (!isContentEmpty()) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1748 | *dst = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1749 | SkASSERT(isContentEmpty()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1750 | } else if (blendMode != SkBlendMode::kSrc && |
| 1751 | blendMode != SkBlendMode::kSrcOut) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1752 | // Except for Src and SrcOut, if there isn't anything already there, |
| 1753 | // then we're done. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1754 | return nullptr; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1755 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1756 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1757 | // TODO(vandebo): Figure out how/if we can handle the following modes: |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1758 | // Xor, Plus. |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1759 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1760 | // Dst xfer mode doesn't draw source at all. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1761 | if (blendMode == SkBlendMode::kDst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1762 | return nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1765 | SkPDFDevice::ContentEntry* entry; |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 1766 | if (fContentEntries.back() && fContentEntries.back()->fContent.bytesWritten() == 0) { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1767 | entry = fContentEntries.back(); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1768 | } else if (blendMode != SkBlendMode::kDstOver) { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1769 | entry = fContentEntries.emplace_back(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1770 | } else { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1771 | entry = fContentEntries.emplace_front(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1772 | } |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1773 | populateGraphicStateEntryFromPaint(matrix, clipStack, paint, hasText, &entry->fState); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1774 | return entry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1777 | void SkPDFDevice::finishContentEntry(SkBlendMode blendMode, |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1778 | sk_sp<SkPDFObject> dst, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1779 | SkPath* shape) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1780 | if (blendMode != SkBlendMode::kClear && |
| 1781 | blendMode != SkBlendMode::kSrc && |
| 1782 | blendMode != SkBlendMode::kDstOver && |
| 1783 | blendMode != SkBlendMode::kSrcIn && |
| 1784 | blendMode != SkBlendMode::kDstIn && |
| 1785 | blendMode != SkBlendMode::kSrcOut && |
| 1786 | blendMode != SkBlendMode::kDstOut && |
| 1787 | blendMode != SkBlendMode::kSrcATop && |
| 1788 | blendMode != SkBlendMode::kDstATop && |
| 1789 | blendMode != SkBlendMode::kModulate) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1790 | SkASSERT(!dst); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1791 | return; |
| 1792 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1793 | if (blendMode == SkBlendMode::kDstOver) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1794 | SkASSERT(!dst); |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 1795 | if (fContentEntries.front()->fContent.bytesWritten() == 0) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1796 | // For DstOver, an empty content entry was inserted before the rest |
| 1797 | // of the content entries. If nothing was drawn, it needs to be |
| 1798 | // removed. |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1799 | fContentEntries.pop_front(); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1800 | } |
| 1801 | return; |
| 1802 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1803 | if (!dst) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1804 | SkASSERT(blendMode == SkBlendMode::kSrc || |
| 1805 | blendMode == SkBlendMode::kSrcOut); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1806 | return; |
| 1807 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1808 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1809 | SkASSERT(dst); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1810 | SkASSERT(fContentEntries.count() == 1); |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1811 | // Changing the current content into a form-xobject will destroy the clip |
| 1812 | // objects which is fine since the xobject will already be clipped. However |
| 1813 | // if source has shape, we need to clip it too, so a copy of the clip is |
| 1814 | // saved. |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1815 | |
| 1816 | SkClipStack clipStack = fContentEntries.front()->fState.fClipStack; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1817 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1818 | SkPaint stockPaint; |
| 1819 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1820 | sk_sp<SkPDFObject> srcFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1821 | if (isContentEmpty()) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1822 | // If nothing was drawn and there's no shape, then the draw was a |
| 1823 | // no-op, but dst needs to be restored for that to be true. |
| 1824 | // If there is shape, then an empty source with Src, SrcIn, SrcOut, |
| 1825 | // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop |
| 1826 | // reduces to Dst. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1827 | if (shape == nullptr || blendMode == SkBlendMode::kDstOut || |
| 1828 | blendMode == SkBlendMode::kSrcATop) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1829 | ScopedContentEntry content(this, fExistingClipStack, SkMatrix::I(), stockPaint); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1830 | // TODO: addXObjectResource take sk_sp |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 1831 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()), content.stream()); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1832 | return; |
| 1833 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1834 | blendMode = SkBlendMode::kClear; |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1835 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1836 | } else { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1837 | SkASSERT(fContentEntries.count() == 1); |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1838 | srcFormXObject = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1841 | // TODO(vandebo) srcFormXObject may contain alpha, but here we want it |
| 1842 | // without alpha. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1843 | if (blendMode == SkBlendMode::kSrcATop) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1844 | // TODO(vandebo): In order to properly support SrcATop we have to track |
| 1845 | // the shape of what's been drawn at all times. It's the intersection of |
| 1846 | // the non-transparent parts of the device and the outlines (shape) of |
| 1847 | // all images and devices drawn. |
| 1848 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1849 | fExistingClipStack, SkBlendMode::kSrcOver, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1850 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1851 | if (shape != nullptr) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1852 | // Draw shape into a form-xobject. |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1853 | SkPaint filledPaint; |
| 1854 | filledPaint.setColor(SK_ColorBLACK); |
| 1855 | filledPaint.setStyle(SkPaint::kFill_Style); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1856 | this->internalDrawPath(clipStack, SkMatrix::I(), *shape, filledPaint, nullptr, true); |
| 1857 | this->drawFormXObjectWithMask(this->addXObjectResource(dst.get()), |
| 1858 | this->makeFormXObjectFromDevice(), |
| 1859 | fExistingClipStack, |
| 1860 | SkBlendMode::kSrcOver, true); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1861 | } else { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1862 | this->drawFormXObjectWithMask(this->addXObjectResource(dst.get()), |
| 1863 | srcFormXObject, |
| 1864 | fExistingClipStack, |
| 1865 | SkBlendMode::kSrcOver, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1866 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1869 | if (blendMode == SkBlendMode::kClear) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1870 | return; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1871 | } else if (blendMode == SkBlendMode::kSrc || |
| 1872 | blendMode == SkBlendMode::kDstATop) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1873 | ScopedContentEntry content(this, fExistingClipStack, SkMatrix::I(), stockPaint); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1874 | if (content.entry()) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 1875 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(srcFormXObject.get()), |
| 1876 | content.stream()); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1877 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1878 | if (blendMode == SkBlendMode::kSrc) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1879 | return; |
| 1880 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1881 | } else if (blendMode == SkBlendMode::kSrcATop) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1882 | ScopedContentEntry content(this, fExistingClipStack, |
| 1883 | SkMatrix::I(), stockPaint); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1884 | if (content.entry()) { |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 1885 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()), content.stream()); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1886 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1889 | SkASSERT(blendMode == SkBlendMode::kSrcIn || |
| 1890 | blendMode == SkBlendMode::kDstIn || |
| 1891 | blendMode == SkBlendMode::kSrcOut || |
| 1892 | blendMode == SkBlendMode::kDstOut || |
| 1893 | blendMode == SkBlendMode::kSrcATop || |
| 1894 | blendMode == SkBlendMode::kDstATop || |
| 1895 | blendMode == SkBlendMode::kModulate); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1896 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1897 | if (blendMode == SkBlendMode::kSrcIn || |
| 1898 | blendMode == SkBlendMode::kSrcOut || |
| 1899 | blendMode == SkBlendMode::kSrcATop) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1900 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), |
| 1901 | std::move(dst), |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1902 | fExistingClipStack, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1903 | SkBlendMode::kSrcOver, |
| 1904 | blendMode == SkBlendMode::kSrcOut); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1905 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1906 | } else { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1907 | SkBlendMode mode = SkBlendMode::kSrcOver; |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1908 | int resourceID = addXObjectResource(dst.get()); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1909 | if (blendMode == SkBlendMode::kModulate) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1910 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1911 | std::move(dst), fExistingClipStack, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1912 | SkBlendMode::kSrcOver, false); |
| 1913 | mode = SkBlendMode::kMultiply; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1914 | } |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1915 | drawFormXObjectWithMask(resourceID, std::move(srcFormXObject), |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1916 | fExistingClipStack, mode, |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1917 | blendMode == SkBlendMode::kDstOut); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1918 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1919 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1922 | bool SkPDFDevice::isContentEmpty() { |
Ben Wagner | 884300d | 2016-12-16 16:51:41 +0000 | [diff] [blame] | 1923 | if (!fContentEntries.front() || fContentEntries.front()->fContent.bytesWritten() == 0) { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1924 | SkASSERT(fContentEntries.count() <= 1); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1925 | return true; |
| 1926 | } |
| 1927 | return false; |
| 1928 | } |
| 1929 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1930 | void SkPDFDevice::populateGraphicStateEntryFromPaint( |
| 1931 | const SkMatrix& matrix, |
| 1932 | const SkClipStack& clipStack, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1933 | const SkPaint& paint, |
| 1934 | bool hasText, |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1935 | SkPDFDevice::GraphicStateEntry* entry) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1936 | NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false); |
| 1937 | NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false); |
| 1938 | NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1939 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1940 | entry->fMatrix = matrix; |
| 1941 | entry->fClipStack = clipStack; |
vandebo@chromium.org | da6c569 | 2012-06-28 21:37:20 +0000 | [diff] [blame] | 1942 | entry->fColor = SkColorSetA(paint.getColor(), 0xFF); |
| 1943 | entry->fShaderIndex = -1; |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 1944 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1945 | // PDF treats a shader as a color, so we only set one or the other. |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1946 | sk_sp<SkPDFObject> pdfShader; |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1947 | SkShader* shader = paint.getShader(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1948 | SkColor color = paint.getColor(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1949 | if (shader) { |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1950 | if (SkShader::kColor_GradientType == shader->asAGradient(nullptr)) { |
| 1951 | // We don't have to set a shader just for a color. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1952 | SkShader::GradientInfo gradientInfo; |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1953 | SkColor gradientColor = SK_ColorBLACK; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1954 | gradientInfo.fColors = &gradientColor; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1955 | gradientInfo.fColorOffsets = nullptr; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1956 | gradientInfo.fColorCount = 1; |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1957 | SkAssertResult(shader->asAGradient(&gradientInfo) == SkShader::kColor_GradientType); |
| 1958 | entry->fColor = SkColorSetA(gradientColor, 0xFF); |
| 1959 | color = gradientColor; |
| 1960 | } else { |
| 1961 | // PDF positions patterns relative to the initial transform, so |
| 1962 | // we need to apply the current transform to the shader parameters. |
| 1963 | SkMatrix transform = matrix; |
| 1964 | transform.postConcat(fInitialTransform); |
| 1965 | |
| 1966 | // PDF doesn't support kClamp_TileMode, so we simulate it by making |
| 1967 | // a pattern the size of the current clip. |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 1968 | SkRect clipStackBounds = clipStack.bounds(this->bounds()); |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1969 | |
| 1970 | // We need to apply the initial transform to bounds in order to get |
| 1971 | // bounds in a consistent coordinate system. |
Hal Canary | a41c2aa | 2017-02-22 16:32:34 -0500 | [diff] [blame] | 1972 | fInitialTransform.mapRect(&clipStackBounds); |
| 1973 | SkIRect bounds; |
| 1974 | clipStackBounds.roundOut(&bounds); |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1975 | |
Hal Canary | 7e872ca | 2017-07-19 15:51:18 -0400 | [diff] [blame] | 1976 | pdfShader = SkPDFMakeShader(fDocument, shader, transform, bounds, paint.getColor()); |
Hal Canary | c8f9180 | 2017-02-12 20:29:12 -0500 | [diff] [blame] | 1977 | |
| 1978 | if (pdfShader.get()) { |
| 1979 | // pdfShader has been canonicalized so we can directly compare |
| 1980 | // pointers. |
| 1981 | int resourceIndex = fShaderResources.find(pdfShader.get()); |
| 1982 | if (resourceIndex < 0) { |
| 1983 | resourceIndex = fShaderResources.count(); |
| 1984 | fShaderResources.push(pdfShader.get()); |
| 1985 | pdfShader.get()->ref(); |
| 1986 | } |
| 1987 | entry->fShaderIndex = resourceIndex; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1988 | } |
| 1989 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1990 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1991 | |
Hal Canary | 80fa7ce | 2017-06-28 16:04:20 -0400 | [diff] [blame] | 1992 | sk_sp<SkPDFDict> newGraphicState; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1993 | if (color == paint.getColor()) { |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 1994 | newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1995 | } else { |
| 1996 | SkPaint newPaint = paint; |
| 1997 | newPaint.setColor(color); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 1998 | newGraphicState = SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1999 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 2000 | int resourceIndex = addGraphicStateResource(newGraphicState.get()); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 2001 | entry->fGraphicStateIndex = resourceIndex; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2002 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 2003 | if (hasText) { |
| 2004 | entry->fTextScaleX = paint.getTextScaleX(); |
| 2005 | entry->fTextFill = paint.getStyle(); |
| 2006 | } else { |
| 2007 | entry->fTextScaleX = 0; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2008 | } |
| 2009 | } |
| 2010 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 2011 | int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 2012 | // Assumes that gs has been canonicalized (so we can directly compare |
| 2013 | // pointers). |
| 2014 | int result = fGraphicStateResources.find(gs); |
| 2015 | if (result < 0) { |
| 2016 | result = fGraphicStateResources.count(); |
| 2017 | fGraphicStateResources.push(gs); |
| 2018 | gs->ref(); |
| 2019 | } |
| 2020 | return result; |
| 2021 | } |
| 2022 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2023 | int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 2024 | // TODO(halcanary): make this take a sk_sp<SkPDFObject> |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2025 | // Assumes that xobject has been canonicalized (so we can directly compare |
| 2026 | // pointers). |
| 2027 | int result = fXObjectResources.find(xObject); |
| 2028 | if (result < 0) { |
| 2029 | result = fXObjectResources.count(); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 2030 | fXObjectResources.push(SkRef(xObject)); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2031 | } |
| 2032 | return result; |
| 2033 | } |
| 2034 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 2035 | int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 2036 | sk_sp<SkPDFFont> newFont = SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 2037 | if (!newFont) { |
| 2038 | return -1; |
| 2039 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2040 | int resourceIndex = fFontResources.find(newFont.get()); |
| 2041 | if (resourceIndex < 0) { |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 2042 | fDocument->registerFont(newFont.get()); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2043 | resourceIndex = fFontResources.count(); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 2044 | fFontResources.push(newFont.release()); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2045 | } |
| 2046 | return resourceIndex; |
| 2047 | } |
| 2048 | |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 2049 | static SkSize rect_to_size(const SkRect& r) { return {r.width(), r.height()}; } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2050 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2051 | static sk_sp<SkImage> color_filter(const SkImage* image, |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2052 | SkColorFilter* colorFilter) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2053 | auto surface = |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2054 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(image->dimensions())); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2055 | SkASSERT(surface); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2056 | SkCanvas* canvas = surface->getCanvas(); |
| 2057 | canvas->clear(SK_ColorTRANSPARENT); |
| 2058 | SkPaint paint; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 2059 | paint.setColorFilter(sk_ref_sp(colorFilter)); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2060 | canvas->drawImage(image, 0, 0, &paint); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2061 | return surface->makeImageSnapshot(); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | //////////////////////////////////////////////////////////////////////////////// |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2065 | |
| 2066 | static bool is_integer(SkScalar x) { |
| 2067 | return x == SkScalarTruncToScalar(x); |
| 2068 | } |
| 2069 | |
| 2070 | static bool is_integral(const SkRect& r) { |
| 2071 | return is_integer(r.left()) && |
| 2072 | is_integer(r.top()) && |
| 2073 | is_integer(r.right()) && |
| 2074 | is_integer(r.bottom()); |
| 2075 | } |
| 2076 | |
| 2077 | void SkPDFDevice::internalDrawImageRect(SkKeyedImage imageSubset, |
| 2078 | const SkRect* src, |
| 2079 | const SkRect& dst, |
| 2080 | const SkPaint& srcPaint, |
| 2081 | const SkMatrix& ctm) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 2082 | if (this->hasEmptyClip()) { |
| 2083 | return; |
| 2084 | } |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2085 | if (!imageSubset) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2086 | return; |
| 2087 | } |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2088 | |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 2089 | // First, figure out the src->dst transform and subset the image if needed. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2090 | SkIRect bounds = imageSubset.image()->bounds(); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2091 | SkRect srcRect = src ? *src : SkRect::Make(bounds); |
| 2092 | SkMatrix transform; |
| 2093 | transform.setRectToRect(srcRect, dst, SkMatrix::kFill_ScaleToFit); |
| 2094 | if (src && *src != SkRect::Make(bounds)) { |
| 2095 | if (!srcRect.intersect(SkRect::Make(bounds))) { |
| 2096 | return; |
| 2097 | } |
| 2098 | srcRect.roundOut(&bounds); |
| 2099 | transform.preTranslate(SkIntToScalar(bounds.x()), |
| 2100 | SkIntToScalar(bounds.y())); |
| 2101 | if (bounds != imageSubset.image()->bounds()) { |
| 2102 | imageSubset = imageSubset.subset(bounds); |
| 2103 | } |
| 2104 | if (!imageSubset) { |
| 2105 | return; |
| 2106 | } |
| 2107 | } |
| 2108 | |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 2109 | // If the image is opaque and the paint's alpha is too, replace |
| 2110 | // kSrc blendmode with kSrcOver. |
| 2111 | SkPaint paint = srcPaint; |
| 2112 | if (imageSubset.image()->isOpaque()) { |
| 2113 | replace_srcmode_on_opaque_paint(&paint); |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 2114 | } |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 2115 | |
| 2116 | // Alpha-only images need to get their color from the shader, before |
| 2117 | // applying the colorfilter. |
| 2118 | if (imageSubset.image()->isAlphaOnly() && paint.getColorFilter()) { |
| 2119 | // must blend alpha image and shader before applying colorfilter. |
| 2120 | auto surface = |
| 2121 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageSubset.image()->dimensions())); |
| 2122 | SkCanvas* canvas = surface->getCanvas(); |
| 2123 | SkPaint tmpPaint; |
| 2124 | // In the case of alpha images with shaders, the shader's coordinate |
| 2125 | // system is the image's coordiantes. |
| 2126 | tmpPaint.setShader(sk_ref_sp(paint.getShader())); |
| 2127 | tmpPaint.setColor(paint.getColor()); |
| 2128 | canvas->clear(0x00000000); |
| 2129 | canvas->drawImage(imageSubset.image().get(), 0, 0, &tmpPaint); |
| 2130 | paint.setShader(nullptr); |
| 2131 | imageSubset = SkKeyedImage(surface->makeImageSnapshot()); |
| 2132 | SkASSERT(!imageSubset.image()->isAlphaOnly()); |
| 2133 | } |
| 2134 | |
| 2135 | if (imageSubset.image()->isAlphaOnly()) { |
| 2136 | // The ColorFilter applies to the paint color/shader, not the alpha layer. |
| 2137 | SkASSERT(nullptr == paint.getColorFilter()); |
| 2138 | |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 2139 | sk_sp<SkImage> mask = alpha_image_to_greyscale_image(imageSubset.image().get()); |
| 2140 | if (!mask) { |
| 2141 | return; |
| 2142 | } |
| 2143 | // PDF doesn't seem to allow masking vector graphics with an Image XObject. |
| 2144 | // Must mask with a Form XObject. |
| 2145 | sk_sp<SkPDFDevice> maskDevice = this->makeCongruentDevice(); |
| 2146 | { |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 2147 | SkCanvas canvas(maskDevice); |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 2148 | if (paint.getMaskFilter()) { |
| 2149 | // This clip prevents the mask image shader from covering |
| 2150 | // entire device if unnecessary. |
| 2151 | canvas.clipRect(this->cs().bounds(this->bounds())); |
| 2152 | canvas.concat(ctm); |
| 2153 | SkPaint tmpPaint; |
| 2154 | tmpPaint.setShader(mask->makeShader(&transform)); |
| 2155 | tmpPaint.setMaskFilter(sk_ref_sp(paint.getMaskFilter())); |
| 2156 | canvas.drawRect(dst, tmpPaint); |
| 2157 | } else { |
| 2158 | canvas.concat(ctm); |
| 2159 | if (src && !is_integral(*src)) { |
| 2160 | canvas.clipRect(dst); |
| 2161 | } |
| 2162 | canvas.concat(transform); |
| 2163 | canvas.drawImage(mask, 0, 0); |
| 2164 | } |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 2165 | } |
Hal Canary | d425a1d | 2017-07-12 13:13:51 -0400 | [diff] [blame] | 2166 | if (!ctm.isIdentity() && paint.getShader()) { |
| 2167 | transform_shader(&paint, ctm); // Since we are using identity matrix. |
| 2168 | } |
| 2169 | ScopedContentEntry content(this, this->cs(), SkMatrix::I(), paint); |
| 2170 | if (!content.entry()) { |
| 2171 | return; |
| 2172 | } |
| 2173 | this->addSMaskGraphicState(std::move(maskDevice), content.stream()); |
| 2174 | SkPDFUtils::AppendRectangle(SkRect::Make(fPageSize), content.stream()); |
| 2175 | SkPDFUtils::PaintPath(SkPaint::kFill_Style, SkPath::kWinding_FillType, content.stream()); |
| 2176 | this->clearMaskOnGraphicState(content.stream()); |
| 2177 | return; |
| 2178 | } |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2179 | if (paint.getMaskFilter()) { |
| 2180 | paint.setShader(imageSubset.image()->makeShader(&transform)); |
| 2181 | SkPath path; |
Hal Canary | f0f4c0c | 2017-07-19 15:48:38 -0400 | [diff] [blame] | 2182 | path.addRect(dst); // handles non-integral clipping. |
| 2183 | this->internalDrawPath(this->cs(), this->ctm(), path, paint, nullptr, true); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2184 | return; |
| 2185 | } |
| 2186 | transform.postConcat(ctm); |
| 2187 | |
| 2188 | bool needToRestore = false; |
| 2189 | if (src && !is_integral(*src)) { |
| 2190 | // Need sub-pixel clipping to fix https://bug.skia.org/4374 |
| 2191 | this->cs().save(); |
| 2192 | this->cs().clipRect(dst, ctm, SkClipOp::kIntersect, true); |
| 2193 | needToRestore = true; |
| 2194 | } |
| 2195 | SK_AT_SCOPE_EXIT(if (needToRestore) { this->cs().restore(); }); |
| 2196 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2197 | #ifdef SK_PDF_IMAGE_STATS |
| 2198 | gDrawImageCalls.fetch_add(1); |
| 2199 | #endif |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2200 | SkMatrix matrix = transform; |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2201 | |
| 2202 | // Rasterize the bitmap using perspective in a new bitmap. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2203 | if (transform.hasPerspective()) { |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2204 | // Transform the bitmap in the new space, without taking into |
| 2205 | // account the initial transform. |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2206 | SkPath perspectiveOutline; |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2207 | SkRect imageBounds = SkRect::Make(imageSubset.image()->bounds()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2208 | perspectiveOutline.addRect(imageBounds); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2209 | perspectiveOutline.transform(transform); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2210 | |
| 2211 | // TODO(edisonn): perf - use current clip too. |
| 2212 | // Retrieve the bounds of the new shape. |
| 2213 | SkRect bounds = perspectiveOutline.getBounds(); |
| 2214 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2215 | // Transform the bitmap in the new space, taking into |
| 2216 | // account the initial transform. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2217 | SkMatrix total = transform; |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2218 | total.postConcat(fInitialTransform); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2219 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2220 | SkPath physicalPerspectiveOutline; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2221 | physicalPerspectiveOutline.addRect(imageBounds); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2222 | physicalPerspectiveOutline.transform(total); |
| 2223 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2224 | SkRect physicalPerspectiveBounds = |
| 2225 | physicalPerspectiveOutline.getBounds(); |
| 2226 | SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width(); |
| 2227 | SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2228 | |
| 2229 | // TODO(edisonn): A better approach would be to use a bitmap shader |
| 2230 | // (in clamp mode) and draw a rect over the entire bounding box. Then |
| 2231 | // intersect perspectiveOutline to the clip. That will avoid introducing |
| 2232 | // alpha to the image while still giving good behavior at the edge of |
| 2233 | // the image. Avoiding alpha will reduce the pdf size and generation |
| 2234 | // CPU time some. |
| 2235 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2236 | SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil(); |
| 2237 | |
Hal Canary | f50ff39 | 2016-09-30 10:25:39 -0400 | [diff] [blame] | 2238 | auto surface = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh)); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2239 | if (!surface) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 2240 | return; |
| 2241 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2242 | SkCanvas* canvas = surface->getCanvas(); |
| 2243 | canvas->clear(SK_ColorTRANSPARENT); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2244 | |
| 2245 | SkScalar deltaX = bounds.left(); |
| 2246 | SkScalar deltaY = bounds.top(); |
| 2247 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2248 | SkMatrix offsetMatrix = transform; |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2249 | offsetMatrix.postTranslate(-deltaX, -deltaY); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2250 | offsetMatrix.postScale(scaleX, scaleY); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2251 | |
| 2252 | // Translate the draw in the new canvas, so we perfectly fit the |
| 2253 | // shape in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2254 | canvas->setMatrix(offsetMatrix); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2255 | canvas->drawImage(imageSubset.image(), 0, 0); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2256 | // Make sure the final bits are in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2257 | canvas->flush(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2258 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2259 | // In the new space, we use the identity matrix translated |
| 2260 | // and scaled to reflect DPI. |
| 2261 | matrix.setScale(1 / scaleX, 1 / scaleY); |
| 2262 | matrix.postTranslate(deltaX, deltaY); |
| 2263 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2264 | imageSubset = SkKeyedImage(surface->makeImageSnapshot()); |
| 2265 | if (!imageSubset) { |
| 2266 | return; |
| 2267 | } |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2270 | SkMatrix scaled; |
| 2271 | // Adjust for origin flip. |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 2272 | scaled.setScale(SK_Scalar1, -SK_Scalar1); |
| 2273 | scaled.postTranslate(0, SK_Scalar1); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2274 | // Scale the image up from 1x1 to WxH. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2275 | SkIRect subset = imageSubset.image()->bounds(); |
| 2276 | scaled.postScale(SkIntToScalar(subset.width()), |
| 2277 | SkIntToScalar(subset.height())); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2278 | scaled.postConcat(matrix); |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2279 | ScopedContentEntry content(this, this->cs(), scaled, paint); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2280 | if (!content.entry()) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2281 | return; |
| 2282 | } |
| 2283 | if (content.needShape()) { |
| 2284 | SkPath shape; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2285 | shape.addRect(SkRect::Make(subset)); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2286 | shape.transform(matrix); |
| 2287 | content.setShape(shape); |
| 2288 | } |
| 2289 | if (!content.needSource()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 2290 | return; |
| 2291 | } |
| 2292 | |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2293 | if (SkColorFilter* colorFilter = paint.getColorFilter()) { |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2294 | sk_sp<SkImage> img = color_filter(imageSubset.image().get(), colorFilter); |
| 2295 | imageSubset = SkKeyedImage(std::move(img)); |
| 2296 | if (!imageSubset) { |
| 2297 | return; |
| 2298 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2299 | // TODO(halcanary): de-dupe this by caching filtered images. |
| 2300 | // (maybe in the resource cache?) |
| 2301 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2302 | |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2303 | SkBitmapKey key = imageSubset.key(); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 2304 | sk_sp<SkPDFObject>* pdfimagePtr = fDocument->canon()->fPDFBitmapMap.find(key); |
| 2305 | sk_sp<SkPDFObject> pdfimage = pdfimagePtr ? *pdfimagePtr : nullptr; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2306 | if (!pdfimage) { |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2307 | SkASSERT(imageSubset); |
| 2308 | pdfimage = SkPDFCreateBitmapObject(imageSubset.release(), |
Mike Reed | a4daf19 | 2017-12-14 13:25:04 -0500 | [diff] [blame] | 2309 | fDocument->metadata().fEncodingQuality); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2310 | if (!pdfimage) { |
| 2311 | return; |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2312 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2313 | fDocument->serialize(pdfimage); // serialize images early. |
Hal Canary | 7cbf5e3 | 2017-07-12 13:10:23 -0400 | [diff] [blame] | 2314 | SkASSERT((key != SkBitmapKey{{0, 0, 0, 0}, 0})); |
Hal Canary | 5c1b360 | 2017-04-17 16:30:06 -0400 | [diff] [blame] | 2315 | fDocument->canon()->fPDFBitmapMap.set(key, pdfimage); |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2316 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2317 | // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject> |
Hal Canary | 51329c9 | 2017-06-27 14:28:37 -0400 | [diff] [blame] | 2318 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()), content.stream()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2319 | } |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2320 | |
| 2321 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2322 | |
| 2323 | #include "SkSpecialImage.h" |
| 2324 | #include "SkImageFilter.h" |
| 2325 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 2326 | void SkPDFDevice::drawSpecial(SkSpecialImage* srcImg, int x, int y, const SkPaint& paint, |
| 2327 | SkImage* clipImage, const SkMatrix& clipMatrix) { |
Hal Canary | b4e528d | 2018-03-09 16:02:15 -0500 | [diff] [blame] | 2328 | if (this->hasEmptyClip()) { |
| 2329 | return; |
| 2330 | } |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2331 | SkASSERT(!srcImg->isTextureBacked()); |
| 2332 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 2333 | //TODO: clipImage support |
| 2334 | |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2335 | SkBitmap resultBM; |
| 2336 | |
| 2337 | SkImageFilter* filter = paint.getImageFilter(); |
| 2338 | if (filter) { |
| 2339 | SkIPoint offset = SkIPoint::Make(0, 0); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2340 | SkMatrix matrix = this->ctm(); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2341 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 2342 | const SkIRect clipBounds = |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 2343 | this->cs().bounds(this->bounds()).roundOut().makeOffset(-x, -y); |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 2344 | sk_sp<SkImageFilterCache> cache(this->getImageFilterCache()); |
Brian Osman | a50205f | 2018-07-06 13:57:01 -0400 | [diff] [blame^] | 2345 | // TODO: Should PDF be operating in a specified color type/space? For now, run the filter |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 2346 | // in the same color space as the source (this is different from all other backends). |
Brian Osman | a50205f | 2018-07-06 13:57:01 -0400 | [diff] [blame^] | 2347 | SkImageFilter::OutputProperties outputProperties(kN32_SkColorType, srcImg->getColorSpace()); |
brianosman | 2a75e5d | 2016-09-22 07:15:37 -0700 | [diff] [blame] | 2348 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get(), outputProperties); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2349 | |
| 2350 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset)); |
| 2351 | if (resultImg) { |
| 2352 | SkPaint tmpUnfiltered(paint); |
| 2353 | tmpUnfiltered.setImageFilter(nullptr); |
| 2354 | if (resultImg->getROPixels(&resultBM)) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2355 | this->drawSprite(resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2356 | } |
| 2357 | } |
| 2358 | } else { |
| 2359 | if (srcImg->getROPixels(&resultBM)) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2360 | this->drawSprite(resultBM, x, y, paint); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) { |
| 2366 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap); |
| 2367 | } |
| 2368 | |
| 2369 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) { |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 2370 | // TODO: See comment above in drawSpecial. The color mode we use for decode should be driven |
| 2371 | // by the destination where we're going to draw thing thing (ie this device). But we don't have |
| 2372 | // a color space, so we always decode in legacy mode for now. |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 2373 | SkColorSpace* legacyColorSpace = nullptr; |
Hal Canary | 22b2d8c | 2017-07-19 14:46:12 -0400 | [diff] [blame] | 2374 | return SkSpecialImage::MakeFromImage(image->bounds(), |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 2375 | image->makeNonTextureImage(), legacyColorSpace); |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() { |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2379 | return nullptr; |
| 2380 | } |
brianosman | 04a44d0 | 2016-09-21 09:46:57 -0700 | [diff] [blame] | 2381 | |
| 2382 | SkImageFilterCache* SkPDFDevice::getImageFilterCache() { |
| 2383 | // We always return a transient cache, so it is freed after each |
| 2384 | // filter traversal. |
| 2385 | return SkImageFilterCache::Create(SkImageFilterCache::kDefaultTransientSize); |
| 2386 | } |