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