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