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