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