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