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" |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 9 | #include "SkAnnotationKeys.h" |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 10 | #include "SkBitmapDevice.h" |
martina.kollarova | b8d6af1 | 2016-06-29 05:12:31 -0700 | [diff] [blame] | 11 | #include "SkBitmapKey.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 12 | #include "SkColor.h" |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 13 | #include "SkColorFilter.h" |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 14 | #include "SkDraw.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 15 | #include "SkGlyphCache.h" |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame^] | 16 | #include "SkMakeUnique.h" |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 17 | #include "SkPath.h" |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 18 | #include "SkPathEffect.h" |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 19 | #include "SkPathOps.h" |
halcanary | db0dcc7 | 2015-03-20 12:31:52 -0700 | [diff] [blame] | 20 | #include "SkPDFBitmap.h" |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 21 | #include "SkPDFCanon.h" |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 22 | #include "SkPDFDocument.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 23 | #include "SkPDFFont.h" |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 24 | #include "SkPDFFormXObject.h" |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 25 | #include "SkPDFGraphicState.h" |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 26 | #include "SkPDFResourceDict.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 27 | #include "SkPDFShader.h" |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 28 | #include "SkPDFTypes.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 29 | #include "SkPDFUtils.h" |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 30 | #include "SkRasterClip.h" |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 31 | #include "SkRRect.h" |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 32 | #include "SkScopeExit.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 33 | #include "SkString.h" |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 34 | #include "SkSurface.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 35 | #include "SkTextFormatParams.h" |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 36 | #include "SkTemplates.h" |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 37 | #include "SkXfermodeInterpretation.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 38 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 39 | #define DPI_FOR_RASTER_SCALE_ONE 72 |
| 40 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 41 | // Utility functions |
| 42 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 43 | // If the paint will definitely draw opaquely, replace kSrc_Mode with |
| 44 | // kSrcOver_Mode. http://crbug.com/473572 |
| 45 | static void replace_srcmode_on_opaque_paint(SkPaint* paint) { |
| 46 | if (kSrcOver_SkXfermodeInterpretation |
| 47 | == SkInterpretXfermode(*paint, false)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | paint->setXfermode(nullptr); |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 52 | static void emit_pdf_color(SkColor color, SkWStream* result) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 53 | SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 54 | SkPDFUtils::AppendColorComponent(SkColorGetR(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 55 | result->writeText(" "); |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 56 | SkPDFUtils::AppendColorComponent(SkColorGetG(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 57 | result->writeText(" "); |
halcanary | eb92cb3 | 2016-07-15 13:41:27 -0700 | [diff] [blame] | 58 | SkPDFUtils::AppendColorComponent(SkColorGetB(color), result); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 59 | result->writeText(" "); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 60 | } |
| 61 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 62 | static SkPaint calculate_text_paint(const SkPaint& paint) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 63 | SkPaint result = paint; |
| 64 | if (result.isFakeBoldText()) { |
| 65 | SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(), |
| 66 | kStdFakeBoldInterpKeys, |
| 67 | kStdFakeBoldInterpValues, |
| 68 | kStdFakeBoldInterpLength); |
| 69 | SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 70 | if (result.getStyle() == SkPaint::kFill_Style) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 71 | result.setStyle(SkPaint::kStrokeAndFill_Style); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 72 | } else { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 73 | width += result.getStrokeWidth(); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 74 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 75 | result.setStrokeWidth(width); |
| 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 80 | SkPDFDevice::GraphicStateEntry::GraphicStateEntry() |
| 81 | : fColor(SK_ColorBLACK) |
| 82 | , fTextScaleX(SK_Scalar1) |
| 83 | , fTextFill(SkPaint::kFill_Style) |
| 84 | , fShaderIndex(-1) |
| 85 | , fGraphicStateIndex(-1) |
| 86 | , fFont(nullptr) |
| 87 | , fTextSize(SK_ScalarNaN) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 88 | fMatrix.reset(); |
| 89 | } |
| 90 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 91 | bool SkPDFDevice::GraphicStateEntry::compareInitialState( |
| 92 | const GraphicStateEntry& cur) { |
commit-bot@chromium.org | b000d76 | 2014-02-07 19:39:57 +0000 | [diff] [blame] | 93 | return fColor == cur.fColor && |
| 94 | fShaderIndex == cur.fShaderIndex && |
| 95 | fGraphicStateIndex == cur.fGraphicStateIndex && |
| 96 | fMatrix == cur.fMatrix && |
| 97 | fClipStack == cur.fClipStack && |
| 98 | (fTextScaleX == 0 || |
| 99 | (fTextScaleX == cur.fTextScaleX && fTextFill == cur.fTextFill)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 100 | } |
| 101 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 102 | class GraphicStackState { |
| 103 | public: |
| 104 | GraphicStackState(const SkClipStack& existingClipStack, |
| 105 | const SkRegion& existingClipRegion, |
| 106 | SkWStream* contentStream) |
| 107 | : fStackDepth(0), |
| 108 | fContentStream(contentStream) { |
| 109 | fEntries[0].fClipStack = existingClipStack; |
| 110 | fEntries[0].fClipRegion = existingClipRegion; |
| 111 | } |
| 112 | |
| 113 | void updateClip(const SkClipStack& clipStack, const SkRegion& clipRegion, |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 114 | const SkPoint& translation); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 115 | void updateMatrix(const SkMatrix& matrix); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 116 | void updateDrawingState(const SkPDFDevice::GraphicStateEntry& state); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 117 | |
| 118 | void drainStack(); |
| 119 | |
| 120 | private: |
| 121 | void push(); |
| 122 | void pop(); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 123 | SkPDFDevice::GraphicStateEntry* currentEntry() { return &fEntries[fStackDepth]; } |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 124 | |
| 125 | // Conservative limit on save depth, see impl. notes in PDF 1.4 spec. |
| 126 | static const int kMaxStackDepth = 12; |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 127 | SkPDFDevice::GraphicStateEntry fEntries[kMaxStackDepth + 1]; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 128 | int fStackDepth; |
| 129 | SkWStream* fContentStream; |
| 130 | }; |
| 131 | |
| 132 | void GraphicStackState::drainStack() { |
| 133 | while (fStackDepth) { |
| 134 | pop(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void GraphicStackState::push() { |
| 139 | SkASSERT(fStackDepth < kMaxStackDepth); |
| 140 | fContentStream->writeText("q\n"); |
| 141 | fStackDepth++; |
| 142 | fEntries[fStackDepth] = fEntries[fStackDepth - 1]; |
| 143 | } |
| 144 | |
| 145 | void GraphicStackState::pop() { |
| 146 | SkASSERT(fStackDepth > 0); |
| 147 | fContentStream->writeText("Q\n"); |
| 148 | fStackDepth--; |
| 149 | } |
| 150 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 151 | // This function initializes iter to be an iterator on the "stack" argument |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 152 | // and then skips over the leading entries as specified in prefix. It requires |
| 153 | // and asserts that "prefix" will be a prefix to "stack." |
| 154 | static void skip_clip_stack_prefix(const SkClipStack& prefix, |
| 155 | const SkClipStack& stack, |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 156 | SkClipStack::Iter* iter) { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 157 | SkClipStack::B2TIter prefixIter(prefix); |
| 158 | iter->reset(stack, SkClipStack::Iter::kBottom_IterStart); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 159 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 160 | const SkClipStack::Element* prefixEntry; |
| 161 | const SkClipStack::Element* iterEntry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 162 | |
| 163 | for (prefixEntry = prefixIter.next(); prefixEntry; |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 164 | prefixEntry = prefixIter.next()) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 165 | iterEntry = iter->next(); |
| 166 | SkASSERT(iterEntry); |
vandebo@chromium.org | 8887ede | 2011-05-25 01:27:52 +0000 | [diff] [blame] | 167 | // Because of SkClipStack does internal intersection, the last clip |
| 168 | // entry may differ. |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 169 | if (*prefixEntry != *iterEntry) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 170 | SkASSERT(prefixEntry->getOp() == SkRegion::kIntersect_Op); |
| 171 | SkASSERT(iterEntry->getOp() == SkRegion::kIntersect_Op); |
| 172 | SkASSERT(iterEntry->getType() == prefixEntry->getType()); |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 173 | // back up the iterator by one |
| 174 | iter->prev(); |
vandebo@chromium.org | 8887ede | 2011-05-25 01:27:52 +0000 | [diff] [blame] | 175 | prefixEntry = prefixIter.next(); |
| 176 | break; |
| 177 | } |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 178 | } |
| 179 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 180 | SkASSERT(prefixEntry == nullptr); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void emit_clip(SkPath* clipPath, SkRect* clipRect, |
| 184 | SkWStream* contentStream) { |
| 185 | SkASSERT(clipPath || clipRect); |
| 186 | |
| 187 | SkPath::FillType clipFill; |
| 188 | if (clipPath) { |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 189 | SkPDFUtils::EmitPath(*clipPath, SkPaint::kFill_Style, contentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 190 | clipFill = clipPath->getFillType(); |
vandebo@chromium.org | 3e7b280 | 2011-06-27 18:12:31 +0000 | [diff] [blame] | 191 | } else { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 192 | SkPDFUtils::AppendRectangle(*clipRect, contentStream); |
| 193 | clipFill = SkPath::kWinding_FillType; |
| 194 | } |
| 195 | |
| 196 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, false); |
| 197 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, false); |
| 198 | if (clipFill == SkPath::kEvenOdd_FillType) { |
| 199 | contentStream->writeText("W* n\n"); |
| 200 | } else { |
| 201 | contentStream->writeText("W n\n"); |
| 202 | } |
| 203 | } |
| 204 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 205 | /* Calculate an inverted path's equivalent non-inverted path, given the |
| 206 | * canvas bounds. |
| 207 | * outPath may alias with invPath (since this is supported by PathOps). |
| 208 | */ |
| 209 | static bool calculate_inverse_path(const SkRect& bounds, const SkPath& invPath, |
| 210 | SkPath* outPath) { |
| 211 | SkASSERT(invPath.isInverseFillType()); |
| 212 | |
| 213 | SkPath clipPath; |
| 214 | clipPath.addRect(bounds); |
| 215 | |
reed | cdb42bb | 2015-06-26 10:23:07 -0700 | [diff] [blame] | 216 | return Op(clipPath, invPath, kIntersect_SkPathOp, outPath); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Sanity check the numerical values of the SkRegion ops and PathOps ops |
| 220 | // enums so region_op_to_pathops_op can do a straight passthrough cast. |
| 221 | // If these are failing, it may be necessary to make region_op_to_pathops_op |
| 222 | // do more. |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 223 | static_assert(SkRegion::kDifference_Op == (int)kDifference_SkPathOp, "region_pathop_mismatch"); |
| 224 | static_assert(SkRegion::kIntersect_Op == (int)kIntersect_SkPathOp, "region_pathop_mismatch"); |
| 225 | static_assert(SkRegion::kUnion_Op == (int)kUnion_SkPathOp, "region_pathop_mismatch"); |
| 226 | static_assert(SkRegion::kXOR_Op == (int)kXOR_SkPathOp, "region_pathop_mismatch"); |
| 227 | static_assert(SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkPathOp, |
| 228 | "region_pathop_mismatch"); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 229 | |
| 230 | static SkPathOp region_op_to_pathops_op(SkRegion::Op op) { |
| 231 | SkASSERT(op >= 0); |
| 232 | SkASSERT(op <= SkRegion::kReverseDifference_Op); |
| 233 | return (SkPathOp)op; |
| 234 | } |
| 235 | |
| 236 | /* Uses Path Ops to calculate a vector SkPath clip from a clip stack. |
| 237 | * Returns true if successful, or false if not successful. |
| 238 | * If successful, the resulting clip is stored in outClipPath. |
| 239 | * If not successful, outClipPath is undefined, and a fallback method |
| 240 | * should be used. |
| 241 | */ |
| 242 | static bool get_clip_stack_path(const SkMatrix& transform, |
| 243 | const SkClipStack& clipStack, |
| 244 | const SkRegion& clipRegion, |
| 245 | SkPath* outClipPath) { |
| 246 | outClipPath->reset(); |
| 247 | outClipPath->setFillType(SkPath::kInverseWinding_FillType); |
| 248 | |
| 249 | const SkClipStack::Element* clipEntry; |
| 250 | SkClipStack::Iter iter; |
| 251 | iter.reset(clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 252 | for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) { |
| 253 | SkPath entryPath; |
| 254 | if (SkClipStack::Element::kEmpty_Type == clipEntry->getType()) { |
| 255 | outClipPath->reset(); |
| 256 | outClipPath->setFillType(SkPath::kInverseWinding_FillType); |
| 257 | continue; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 258 | } else { |
| 259 | clipEntry->asPath(&entryPath); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 260 | } |
| 261 | entryPath.transform(transform); |
| 262 | |
| 263 | if (SkRegion::kReplace_Op == clipEntry->getOp()) { |
| 264 | *outClipPath = entryPath; |
| 265 | } else { |
| 266 | SkPathOp op = region_op_to_pathops_op(clipEntry->getOp()); |
| 267 | if (!Op(*outClipPath, entryPath, op, outClipPath)) { |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (outClipPath->isInverseFillType()) { |
| 274 | // The bounds are slightly outset to ensure this is correct in the |
| 275 | // face of floating-point accuracy and possible SkRegion bitmap |
| 276 | // approximations. |
| 277 | SkRect clipBounds = SkRect::Make(clipRegion.getBounds()); |
| 278 | clipBounds.outset(SK_Scalar1, SK_Scalar1); |
| 279 | if (!calculate_inverse_path(clipBounds, *outClipPath, outClipPath)) { |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | return true; |
| 284 | } |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 285 | |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 286 | // TODO(vandebo): Take advantage of SkClipStack::getSaveCount(), the PDF |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 287 | // graphic state stack, and the fact that we can know all the clips used |
| 288 | // on the page to optimize this. |
| 289 | void GraphicStackState::updateClip(const SkClipStack& clipStack, |
| 290 | const SkRegion& clipRegion, |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 291 | const SkPoint& translation) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 292 | if (clipStack == currentEntry()->fClipStack) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | while (fStackDepth > 0) { |
| 297 | pop(); |
| 298 | if (clipStack == currentEntry()->fClipStack) { |
| 299 | return; |
| 300 | } |
| 301 | } |
| 302 | push(); |
| 303 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 304 | currentEntry()->fClipStack = clipStack; |
| 305 | currentEntry()->fClipRegion = clipRegion; |
| 306 | |
| 307 | SkMatrix transform; |
| 308 | transform.setTranslate(translation.fX, translation.fY); |
| 309 | |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 310 | SkPath clipPath; |
| 311 | if (get_clip_stack_path(transform, clipStack, clipRegion, &clipPath)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 312 | emit_clip(&clipPath, nullptr, fContentStream); |
commit-bot@chromium.org | d2623a1 | 2013-08-08 02:52:05 +0000 | [diff] [blame] | 313 | return; |
| 314 | } |
halcanary | dda239e | 2016-03-31 07:33:57 -0700 | [diff] [blame] | 315 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 316 | // gsState->initialEntry()->fClipStack/Region specifies the clip that has |
| 317 | // already been applied. (If this is a top level device, then it specifies |
| 318 | // a clip to the content area. If this is a layer, then it specifies |
| 319 | // the clip in effect when the layer was created.) There's no need to |
| 320 | // reapply that clip; SKCanvas's SkDrawIter will draw anything outside the |
| 321 | // initial clip on the parent layer. (This means there's a bug if the user |
| 322 | // expands the clip and then uses any xfer mode that uses dst: |
| 323 | // http://code.google.com/p/skia/issues/detail?id=228 ) |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 324 | SkClipStack::Iter iter; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 325 | skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter); |
| 326 | |
| 327 | // If the clip stack does anything other than intersect or if it uses |
| 328 | // an inverse fill type, we have to fall back to the clip region. |
| 329 | bool needRegion = false; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 330 | const SkClipStack::Element* clipEntry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 331 | for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 332 | if (clipEntry->getOp() != SkRegion::kIntersect_Op || |
| 333 | clipEntry->isInverseFilled()) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 334 | needRegion = true; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (needRegion) { |
| 340 | SkPath clipPath; |
| 341 | SkAssertResult(clipRegion.getBoundaryPath(&clipPath)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 342 | emit_clip(&clipPath, nullptr, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 343 | } else { |
| 344 | skip_clip_stack_prefix(fEntries[0].fClipStack, clipStack, &iter); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 345 | const SkClipStack::Element* clipEntry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 346 | for (clipEntry = iter.next(); clipEntry; clipEntry = iter.next()) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 347 | SkASSERT(clipEntry->getOp() == SkRegion::kIntersect_Op); |
| 348 | switch (clipEntry->getType()) { |
| 349 | case SkClipStack::Element::kRect_Type: { |
| 350 | SkRect translatedClip; |
| 351 | transform.mapRect(&translatedClip, clipEntry->getRect()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 352 | emit_clip(nullptr, &translatedClip, fContentStream); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
commit-bot@chromium.org | 5a346a8 | 2014-02-16 16:01:14 +0000 | [diff] [blame] | 355 | default: { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 356 | SkPath translatedPath; |
commit-bot@chromium.org | 5a346a8 | 2014-02-16 16:01:14 +0000 | [diff] [blame] | 357 | clipEntry->asPath(&translatedPath); |
| 358 | translatedPath.transform(transform, &translatedPath); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 359 | emit_clip(&translatedPath, nullptr, fContentStream); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 360 | break; |
| 361 | } |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | } |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void GraphicStackState::updateMatrix(const SkMatrix& matrix) { |
| 368 | if (matrix == currentEntry()->fMatrix) { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | if (currentEntry()->fMatrix.getType() != SkMatrix::kIdentity_Mask) { |
| 373 | SkASSERT(fStackDepth > 0); |
| 374 | SkASSERT(fEntries[fStackDepth].fClipStack == |
| 375 | fEntries[fStackDepth -1].fClipStack); |
| 376 | pop(); |
| 377 | |
| 378 | SkASSERT(currentEntry()->fMatrix.getType() == SkMatrix::kIdentity_Mask); |
| 379 | } |
| 380 | if (matrix.getType() == SkMatrix::kIdentity_Mask) { |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | push(); |
| 385 | SkPDFUtils::AppendTransform(matrix, fContentStream); |
| 386 | currentEntry()->fMatrix = matrix; |
| 387 | } |
| 388 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 389 | void GraphicStackState::updateDrawingState(const SkPDFDevice::GraphicStateEntry& state) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 390 | // PDF treats a shader as a color, so we only set one or the other. |
| 391 | if (state.fShaderIndex >= 0) { |
| 392 | if (state.fShaderIndex != currentEntry()->fShaderIndex) { |
commit-bot@chromium.org | 93a2e21 | 2013-07-23 23:16:03 +0000 | [diff] [blame] | 393 | SkPDFUtils::ApplyPattern(state.fShaderIndex, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 394 | currentEntry()->fShaderIndex = state.fShaderIndex; |
| 395 | } |
| 396 | } else { |
| 397 | if (state.fColor != currentEntry()->fColor || |
| 398 | currentEntry()->fShaderIndex >= 0) { |
| 399 | emit_pdf_color(state.fColor, fContentStream); |
| 400 | fContentStream->writeText("RG "); |
| 401 | emit_pdf_color(state.fColor, fContentStream); |
| 402 | fContentStream->writeText("rg\n"); |
| 403 | currentEntry()->fColor = state.fColor; |
| 404 | currentEntry()->fShaderIndex = -1; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if (state.fGraphicStateIndex != currentEntry()->fGraphicStateIndex) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 409 | SkPDFUtils::ApplyGraphicState(state.fGraphicStateIndex, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 410 | currentEntry()->fGraphicStateIndex = state.fGraphicStateIndex; |
| 411 | } |
| 412 | |
| 413 | if (state.fTextScaleX) { |
| 414 | if (state.fTextScaleX != currentEntry()->fTextScaleX) { |
| 415 | SkScalar pdfScale = SkScalarMul(state.fTextScaleX, |
| 416 | SkIntToScalar(100)); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 417 | SkPDFUtils::AppendScalar(pdfScale, fContentStream); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 418 | fContentStream->writeText(" Tz\n"); |
| 419 | currentEntry()->fTextScaleX = state.fTextScaleX; |
| 420 | } |
| 421 | if (state.fTextFill != currentEntry()->fTextFill) { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 422 | static_assert(SkPaint::kFill_Style == 0, "enum_must_match_value"); |
| 423 | static_assert(SkPaint::kStroke_Style == 1, "enum_must_match_value"); |
| 424 | static_assert(SkPaint::kStrokeAndFill_Style == 2, "enum_must_match_value"); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 425 | fContentStream->writeDecAsText(state.fTextFill); |
| 426 | fContentStream->writeText(" Tr\n"); |
| 427 | currentEntry()->fTextFill = state.fTextFill; |
| 428 | } |
| 429 | } |
| 430 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 431 | |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 432 | static bool not_supported_for_layers(const SkPaint& layerPaint) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 433 | // PDF does not support image filters, so render them on CPU. |
| 434 | // Note that this rendering is done at "screen" resolution (100dpi), not |
| 435 | // printer resolution. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 436 | // TODO: It may be possible to express some filters natively using PDF |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 437 | // to improve quality and file size (https://bug.skia.org/3043) |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 438 | |
| 439 | // TODO: should we return true if there is a colorfilter? |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 440 | return layerPaint.getImageFilter() != nullptr; |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) { |
reed | cd4051e | 2016-07-15 09:41:26 -0700 | [diff] [blame] | 444 | if (layerPaint && not_supported_for_layers(*layerPaint)) { |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 445 | // need to return a raster device, which we will detect in drawDevice() |
| 446 | return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry)); |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 447 | } |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 448 | SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height()); |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 449 | return SkPDFDevice::Create(size, fRasterDpi, fDocument); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 450 | } |
| 451 | |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 452 | SkPDFCanon* SkPDFDevice::getCanon() const { return fDocument->canon(); } |
| 453 | |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 454 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 455 | |
| 456 | // A helper class to automatically finish a ContentEntry at the end of a |
| 457 | // 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] | 458 | class ScopedContentEntry { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 459 | public: |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 460 | ScopedContentEntry(SkPDFDevice* device, const SkDraw& draw, |
| 461 | const SkPaint& paint, bool hasText = false) |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 462 | : fDevice(device), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 463 | fContentEntry(nullptr), |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 464 | fXfermode(SkXfermode::kSrcOver_Mode), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 465 | fDstFormXObject(nullptr) { |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 466 | init(draw.fClipStack, draw.fRC->bwRgn(), *draw.fMatrix, paint, hasText); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 467 | } |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 468 | ScopedContentEntry(SkPDFDevice* device, const SkClipStack* clipStack, |
| 469 | const SkRegion& clipRegion, const SkMatrix& matrix, |
| 470 | const SkPaint& paint, bool hasText = false) |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 471 | : fDevice(device), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 472 | fContentEntry(nullptr), |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 473 | fXfermode(SkXfermode::kSrcOver_Mode), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 474 | fDstFormXObject(nullptr) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 475 | init(clipStack, clipRegion, matrix, paint, hasText); |
| 476 | } |
| 477 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 478 | ~ScopedContentEntry() { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 479 | if (fContentEntry) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 480 | SkPath* shape = &fShape; |
| 481 | if (shape->isEmpty()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 482 | shape = nullptr; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 483 | } |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 484 | fDevice->finishContentEntry(fXfermode, std::move(fDstFormXObject), shape); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 488 | SkPDFDevice::ContentEntry* entry() { return fContentEntry; } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 489 | |
| 490 | /* Returns true when we explicitly need the shape of the drawing. */ |
| 491 | bool needShape() { |
| 492 | switch (fXfermode) { |
| 493 | case SkXfermode::kClear_Mode: |
| 494 | case SkXfermode::kSrc_Mode: |
| 495 | case SkXfermode::kSrcIn_Mode: |
| 496 | case SkXfermode::kSrcOut_Mode: |
| 497 | case SkXfermode::kDstIn_Mode: |
| 498 | case SkXfermode::kDstOut_Mode: |
| 499 | case SkXfermode::kSrcATop_Mode: |
| 500 | case SkXfermode::kDstATop_Mode: |
| 501 | case SkXfermode::kModulate_Mode: |
| 502 | return true; |
| 503 | default: |
| 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* Returns true unless we only need the shape of the drawing. */ |
| 509 | bool needSource() { |
| 510 | if (fXfermode == SkXfermode::kClear_Mode) { |
| 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | /* If the shape is different than the alpha component of the content, then |
| 517 | * setShape should be called with the shape. In particular, images and |
| 518 | * devices have rectangular shape. |
| 519 | */ |
| 520 | void setShape(const SkPath& shape) { |
| 521 | fShape = shape; |
| 522 | } |
| 523 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 524 | private: |
| 525 | SkPDFDevice* fDevice; |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 526 | SkPDFDevice::ContentEntry* fContentEntry; |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 527 | SkXfermode::Mode fXfermode; |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 528 | sk_sp<SkPDFObject> fDstFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 529 | SkPath fShape; |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 530 | |
| 531 | void init(const SkClipStack* clipStack, const SkRegion& clipRegion, |
| 532 | const SkMatrix& matrix, const SkPaint& paint, bool hasText) { |
edisonn@google.com | 83d8eda | 2013-10-24 13:19:28 +0000 | [diff] [blame] | 533 | // Shape has to be flatten before we get here. |
| 534 | if (matrix.hasPerspective()) { |
| 535 | NOT_IMPLEMENTED(!matrix.hasPerspective(), false); |
vandebo@chromium.org | dc37e20 | 2013-10-18 20:16:34 +0000 | [diff] [blame] | 536 | return; |
| 537 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 538 | if (paint.getXfermode()) { |
| 539 | paint.getXfermode()->asMode(&fXfermode); |
| 540 | } |
| 541 | fContentEntry = fDevice->setUpContentEntry(clipStack, clipRegion, |
| 542 | matrix, paint, hasText, |
| 543 | &fDstFormXObject); |
| 544 | } |
| 545 | }; |
| 546 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 547 | //////////////////////////////////////////////////////////////////////////////// |
| 548 | |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 549 | SkPDFDevice::SkPDFDevice(SkISize pageSize, SkScalar rasterDpi, SkPDFDocument* doc, bool flip) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 550 | : INHERITED(SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height()), |
| 551 | SkSurfaceProps(0, kUnknown_SkPixelGeometry)) |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 552 | , fPageSize(pageSize) |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 553 | , fExistingClipRegion(SkIRect::MakeSize(pageSize)) |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 554 | , fRasterDpi(rasterDpi) |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 555 | , fDocument(doc) { |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 556 | SkASSERT(pageSize.width() > 0); |
| 557 | SkASSERT(pageSize.height() > 0); |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 558 | |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 559 | if (flip) { |
| 560 | // Skia generally uses the top left as the origin but PDF |
| 561 | // natively has the origin at the bottom left. This matrix |
| 562 | // corrects for that. But that only needs to be done once, we |
| 563 | // don't do it when layering. |
| 564 | fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight)); |
| 565 | fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1); |
| 566 | } else { |
| 567 | fInitialTransform.setIdentity(); |
| 568 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | SkPDFDevice::~SkPDFDevice() { |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 572 | this->cleanUp(); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void SkPDFDevice::init() { |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 576 | fContentEntries.reset(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 577 | } |
| 578 | |
halcanary | 3c35fb3 | 2016-06-30 11:55:07 -0700 | [diff] [blame] | 579 | void SkPDFDevice::cleanUp() { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 580 | fGraphicStateResources.unrefAll(); |
| 581 | fXObjectResources.unrefAll(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 582 | fFontResources.unrefAll(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 583 | fShaderResources.unrefAll(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 584 | } |
| 585 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 586 | void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[], |
| 587 | SkData* value) { |
| 588 | if (0 == rect.width() && 0 == rect.height()) { |
| 589 | handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value); |
| 590 | } else { |
| 591 | SkPath path; |
| 592 | path.addRect(rect); |
| 593 | handlePathAnnotation(path, d, key, value); |
| 594 | } |
| 595 | } |
| 596 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 597 | void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 598 | SkPaint newPaint = paint; |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 599 | replace_srcmode_on_opaque_paint(&newPaint); |
| 600 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 601 | newPaint.setStyle(SkPaint::kFill_Style); |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 602 | ScopedContentEntry content(this, d, newPaint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 603 | internalDrawPaint(newPaint, content.entry()); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 604 | } |
| 605 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 606 | void SkPDFDevice::internalDrawPaint(const SkPaint& paint, |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 607 | SkPDFDevice::ContentEntry* contentEntry) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 608 | if (!contentEntry) { |
| 609 | return; |
| 610 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 611 | SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()), |
| 612 | SkIntToScalar(this->height())); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 613 | SkMatrix inverse; |
commit-bot@chromium.org | d2cfa74 | 2013-09-20 18:58:30 +0000 | [diff] [blame] | 614 | if (!contentEntry->fState.fMatrix.invert(&inverse)) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 615 | return; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 616 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 617 | inverse.mapRect(&bbox); |
| 618 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 619 | SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 620 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 621 | &contentEntry->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 622 | } |
| 623 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 624 | void SkPDFDevice::drawPoints(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 625 | SkCanvas::PointMode mode, |
| 626 | size_t count, |
| 627 | const SkPoint* points, |
| 628 | const SkPaint& srcPaint) { |
| 629 | SkPaint passedPaint = srcPaint; |
| 630 | replace_srcmode_on_opaque_paint(&passedPaint); |
| 631 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 632 | if (count == 0) { |
| 633 | return; |
| 634 | } |
| 635 | |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 636 | // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath. |
| 637 | // We only use this when there's a path effect because of the overhead |
| 638 | // of multiple calls to setUpContentEntry it causes. |
| 639 | if (passedPaint.getPathEffect()) { |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 640 | if (d.fRC->isEmpty()) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 641 | return; |
| 642 | } |
| 643 | SkDraw pointDraw(d); |
| 644 | pointDraw.fDevice = this; |
| 645 | pointDraw.drawPoints(mode, count, points, passedPaint, true); |
| 646 | return; |
| 647 | } |
| 648 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 649 | const SkPaint* paint = &passedPaint; |
| 650 | SkPaint modifiedPaint; |
| 651 | |
| 652 | if (mode == SkCanvas::kPoints_PointMode && |
| 653 | paint->getStrokeCap() != SkPaint::kRound_Cap) { |
| 654 | modifiedPaint = *paint; |
| 655 | paint = &modifiedPaint; |
| 656 | if (paint->getStrokeWidth()) { |
| 657 | // PDF won't draw a single point with square/butt caps because the |
| 658 | // orientation is ambiguous. Draw a rectangle instead. |
| 659 | modifiedPaint.setStyle(SkPaint::kFill_Style); |
| 660 | SkScalar strokeWidth = paint->getStrokeWidth(); |
| 661 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 662 | for (size_t i = 0; i < count; i++) { |
| 663 | SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0); |
| 664 | r.inset(-halfStroke, -halfStroke); |
| 665 | drawRect(d, r, modifiedPaint); |
| 666 | } |
| 667 | return; |
| 668 | } else { |
| 669 | modifiedPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 670 | } |
| 671 | } |
| 672 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 673 | ScopedContentEntry content(this, d, *paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 674 | if (!content.entry()) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 675 | return; |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 676 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 677 | |
| 678 | switch (mode) { |
| 679 | case SkCanvas::kPolygon_PointMode: |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 680 | SkPDFUtils::MoveTo(points[0].fX, points[0].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 681 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 682 | for (size_t i = 1; i < count; i++) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 683 | SkPDFUtils::AppendLine(points[i].fX, points[i].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 684 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 685 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 686 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 687 | break; |
| 688 | case SkCanvas::kLines_PointMode: |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 689 | for (size_t i = 0; i < count/2; i++) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 690 | SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 691 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 692 | SkPDFUtils::AppendLine(points[i * 2 + 1].fX, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 693 | points[i * 2 + 1].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 694 | &content.entry()->fContent); |
| 695 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 696 | } |
| 697 | break; |
| 698 | case SkCanvas::kPoints_PointMode: |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 699 | SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap); |
| 700 | for (size_t i = 0; i < count; i++) { |
| 701 | SkPDFUtils::MoveTo(points[i].fX, points[i].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 702 | &content.entry()->fContent); |
| 703 | SkPDFUtils::ClosePath(&content.entry()->fContent); |
| 704 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 705 | } |
| 706 | break; |
| 707 | default: |
| 708 | SkASSERT(false); |
| 709 | } |
| 710 | } |
| 711 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 712 | static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 713 | auto annotation = sk_make_sp<SkPDFDict>("Annot"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 714 | annotation->insertName("Subtype", "Link"); |
halcanary | 488165e | 2016-04-22 06:10:21 -0700 | [diff] [blame] | 715 | annotation->insertInt("F", 4); // required by ISO 19005 |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 716 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 717 | auto border = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 718 | border->reserve(3); |
| 719 | border->appendInt(0); // Horizontal corner radius. |
| 720 | border->appendInt(0); // Vertical corner radius. |
| 721 | border->appendInt(0); // Width, 0 = no border. |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 722 | annotation->insertObject("Border", std::move(border)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 723 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 724 | auto rect = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 725 | rect->reserve(4); |
| 726 | rect->appendScalar(translatedRect.fLeft); |
| 727 | rect->appendScalar(translatedRect.fTop); |
| 728 | rect->appendScalar(translatedRect.fRight); |
| 729 | rect->appendScalar(translatedRect.fBottom); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 730 | annotation->insertObject("Rect", std::move(rect)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 731 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 732 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 733 | } |
| 734 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 735 | 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] | 736 | sk_sp<SkPDFDict> annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 737 | SkString url(static_cast<const char *>(urlData->data()), |
| 738 | urlData->size() - 1); |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 739 | auto action = sk_make_sp<SkPDFDict>("Action"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 740 | action->insertName("S", "URI"); |
| 741 | action->insertString("URI", url); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 742 | annotation->insertObject("A", std::move(action)); |
| 743 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 744 | } |
| 745 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 746 | static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData, |
| 747 | const SkRect& r) { |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 748 | sk_sp<SkPDFDict> annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 749 | SkString name(static_cast<const char *>(nameData->data()), |
| 750 | nameData->size() - 1); |
| 751 | annotation->insertName("Dest", name); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 752 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 753 | } |
| 754 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 755 | void SkPDFDevice::drawRect(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 756 | const SkRect& rect, |
| 757 | const SkPaint& srcPaint) { |
| 758 | SkPaint paint = srcPaint; |
| 759 | replace_srcmode_on_opaque_paint(&paint); |
commit-bot@chromium.org | 969fd6a | 2013-05-14 18:16:40 +0000 | [diff] [blame] | 760 | SkRect r = rect; |
| 761 | r.sort(); |
| 762 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 763 | if (paint.getPathEffect()) { |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 764 | if (d.fRC->isEmpty()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 765 | return; |
| 766 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 767 | SkPath path; |
| 768 | path.addRect(r); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 769 | drawPath(d, path, paint, nullptr, true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 770 | return; |
| 771 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 772 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 773 | ScopedContentEntry content(this, d, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 774 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 775 | return; |
| 776 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 777 | SkPDFUtils::AppendRectangle(r, &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 778 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 779 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 780 | } |
| 781 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 782 | void SkPDFDevice::drawRRect(const SkDraw& draw, |
| 783 | const SkRRect& rrect, |
| 784 | const SkPaint& srcPaint) { |
| 785 | SkPaint paint = srcPaint; |
| 786 | replace_srcmode_on_opaque_paint(&paint); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 787 | SkPath path; |
| 788 | path.addRRect(rrect); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 789 | this->drawPath(draw, path, paint, nullptr, true); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 790 | } |
| 791 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 792 | void SkPDFDevice::drawOval(const SkDraw& draw, |
| 793 | const SkRect& oval, |
| 794 | const SkPaint& srcPaint) { |
| 795 | SkPaint paint = srcPaint; |
| 796 | replace_srcmode_on_opaque_paint(&paint); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 797 | SkPath path; |
| 798 | path.addOval(oval); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 799 | this->drawPath(draw, path, paint, nullptr, true); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 800 | } |
| 801 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 802 | void SkPDFDevice::drawPath(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 803 | const SkPath& origPath, |
| 804 | const SkPaint& srcPaint, |
| 805 | const SkMatrix* prePathMatrix, |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 806 | bool pathIsMutable) { |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 807 | SkPaint paint = srcPaint; |
| 808 | replace_srcmode_on_opaque_paint(&paint); |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 809 | SkPath modifiedPath; |
| 810 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 811 | |
| 812 | SkMatrix matrix = *d.fMatrix; |
| 813 | if (prePathMatrix) { |
| 814 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 815 | if (!pathIsMutable) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 816 | pathPtr = &modifiedPath; |
| 817 | pathIsMutable = true; |
| 818 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 819 | origPath.transform(*prePathMatrix, pathPtr); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 820 | } else { |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 821 | matrix.preConcat(*prePathMatrix); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 822 | } |
| 823 | } |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 824 | |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 825 | if (paint.getPathEffect()) { |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 826 | if (d.fRC->isEmpty()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 827 | return; |
| 828 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 829 | if (!pathIsMutable) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 830 | pathPtr = &modifiedPath; |
| 831 | pathIsMutable = true; |
| 832 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 833 | bool fill = paint.getFillPath(origPath, pathPtr); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 834 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 835 | SkPaint noEffectPaint(paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 836 | noEffectPaint.setPathEffect(nullptr); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 837 | if (fill) { |
| 838 | noEffectPaint.setStyle(SkPaint::kFill_Style); |
| 839 | } else { |
| 840 | noEffectPaint.setStyle(SkPaint::kStroke_Style); |
| 841 | noEffectPaint.setStrokeWidth(0); |
| 842 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 843 | drawPath(d, *pathPtr, noEffectPaint, nullptr, true); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 844 | return; |
| 845 | } |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 846 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 847 | if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 848 | return; |
| 849 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 850 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 851 | ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 852 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 853 | return; |
| 854 | } |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 855 | bool consumeDegeratePathSegments = |
| 856 | paint.getStyle() == SkPaint::kFill_Style || |
| 857 | (paint.getStrokeCap() != SkPaint::kRound_Cap && |
| 858 | paint.getStrokeCap() != SkPaint::kSquare_Cap); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 859 | SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(), |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 860 | consumeDegeratePathSegments, |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 861 | &content.entry()->fContent); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 862 | SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 863 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 864 | } |
| 865 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 866 | void SkPDFDevice::drawBitmapRect(const SkDraw& draw, |
| 867 | const SkBitmap& bitmap, |
| 868 | const SkRect* src, |
| 869 | const SkRect& dst, |
| 870 | const SkPaint& srcPaint, |
| 871 | SkCanvas::SrcRectConstraint constraint) { |
halcanary | 66be626 | 2016-03-21 13:01:34 -0700 | [diff] [blame] | 872 | SkASSERT(false); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | void SkPDFDevice::drawBitmap(const SkDraw& d, |
| 876 | const SkBitmap& bitmap, |
| 877 | const SkMatrix& matrix, |
| 878 | const SkPaint& srcPaint) { |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 879 | SkPaint paint = srcPaint; |
| 880 | if (bitmap.isOpaque()) { |
| 881 | replace_srcmode_on_opaque_paint(&paint); |
| 882 | } |
| 883 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 884 | if (d.fRC->isEmpty()) { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 885 | return; |
| 886 | } |
edisonn@google.com | 2ae67e7 | 2013-02-12 01:06:38 +0000 | [diff] [blame] | 887 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 888 | SkMatrix transform = matrix; |
| 889 | transform.postConcat(*d.fMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 890 | SkImageBitmap imageBitmap(bitmap); |
| 891 | this->internalDrawImage( |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 892 | transform, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void SkPDFDevice::drawSprite(const SkDraw& d, |
| 896 | const SkBitmap& bitmap, |
| 897 | int x, |
| 898 | int y, |
| 899 | const SkPaint& srcPaint) { |
| 900 | SkPaint paint = srcPaint; |
| 901 | if (bitmap.isOpaque()) { |
| 902 | replace_srcmode_on_opaque_paint(&paint); |
| 903 | } |
| 904 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 905 | if (d.fRC->isEmpty()) { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 906 | return; |
| 907 | } |
| 908 | |
| 909 | SkMatrix matrix; |
| 910 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 911 | SkImageBitmap imageBitmap(bitmap); |
| 912 | this->internalDrawImage( |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 913 | matrix, d.fClipStack, d.fRC->bwRgn(), imageBitmap, paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | void SkPDFDevice::drawImage(const SkDraw& draw, |
| 917 | const SkImage* image, |
| 918 | SkScalar x, |
| 919 | SkScalar y, |
| 920 | const SkPaint& srcPaint) { |
| 921 | SkPaint paint = srcPaint; |
| 922 | if (!image) { |
| 923 | return; |
| 924 | } |
| 925 | if (image->isOpaque()) { |
| 926 | replace_srcmode_on_opaque_paint(&paint); |
| 927 | } |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 928 | if (draw.fRC->isEmpty()) { |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | SkMatrix transform = SkMatrix::MakeTrans(x, y); |
| 932 | transform.postConcat(*draw.fMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 933 | SkImageBitmap imageBitmap(const_cast<SkImage*>(image)); |
| 934 | this->internalDrawImage( |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 935 | transform, draw.fClipStack, draw.fRC->bwRgn(), imageBitmap, paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void SkPDFDevice::drawImageRect(const SkDraw& draw, |
| 939 | const SkImage* image, |
| 940 | const SkRect* src, |
| 941 | const SkRect& dst, |
| 942 | const SkPaint& srcPaint, |
| 943 | SkCanvas::SrcRectConstraint constraint) { |
halcanary | 66be626 | 2016-03-21 13:01:34 -0700 | [diff] [blame] | 944 | SkASSERT(false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 945 | } |
| 946 | |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 947 | namespace { |
| 948 | class GlyphPositioner { |
| 949 | public: |
| 950 | GlyphPositioner(SkDynamicMemoryWStream* content, |
| 951 | SkScalar textSkewX, |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 952 | bool wideChars, |
| 953 | bool defaultPositioning, |
| 954 | SkPoint origin) |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 955 | : fContent(content) |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 956 | , fCurrentMatrixOrigin{0.0f, 0.0f} |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 957 | , fXAdvance(0.0f) |
| 958 | , fWideChars(wideChars) |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 959 | , fInText(false) |
| 960 | , fDefaultPositioning(defaultPositioning) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 961 | // Flip the text about the x-axis to account for origin swap and include |
| 962 | // the passed parameters. |
| 963 | fContent->writeText("1 0 "); |
| 964 | SkPDFUtils::AppendScalar(0 - textSkewX, fContent); |
| 965 | fContent->writeText(" -1 "); |
| 966 | SkPDFUtils::AppendScalar(origin.x(), fContent); |
| 967 | fContent->writeText(" "); |
| 968 | SkPDFUtils::AppendScalar(origin.y(), fContent); |
| 969 | fContent->writeText(" Tm\n"); |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 970 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 971 | ~GlyphPositioner() { this->flush(); } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 972 | void flush() { |
| 973 | if (fInText) { |
| 974 | fContent->writeText("> Tj\n"); |
| 975 | fInText = false; |
| 976 | } |
| 977 | } |
| 978 | void setWideChars(bool wideChars) { |
| 979 | if (fWideChars != wideChars) { |
| 980 | SkASSERT(!fInText); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 981 | SkASSERT(fWideChars == wideChars); |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 982 | fWideChars = wideChars; |
| 983 | } |
| 984 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 985 | void writeGlyph(SkPoint xy, |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 986 | SkScalar advanceWidth, |
| 987 | uint16_t glyph) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 988 | if (!fDefaultPositioning) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 989 | SkPoint position = xy - fCurrentMatrixOrigin; |
| 990 | if (position != SkPoint{fXAdvance, 0}) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 991 | this->flush(); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 992 | SkPDFUtils::AppendScalar(position.x(), fContent); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 993 | fContent->writeText(" "); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 994 | SkPDFUtils::AppendScalar(-position.y(), fContent); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 995 | fContent->writeText(" Td "); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 996 | fCurrentMatrixOrigin = xy; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 997 | fXAdvance = 0; |
| 998 | } |
| 999 | fXAdvance += advanceWidth; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1000 | } |
| 1001 | if (!fInText) { |
| 1002 | fContent->writeText("<"); |
| 1003 | fInText = true; |
| 1004 | } |
| 1005 | if (fWideChars) { |
| 1006 | SkPDFUtils::WriteUInt16BE(fContent, glyph); |
| 1007 | } else { |
| 1008 | SkASSERT(0 == glyph >> 8); |
| 1009 | SkPDFUtils::WriteUInt8(fContent, static_cast<uint8_t>(glyph)); |
| 1010 | } |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | private: |
| 1014 | SkDynamicMemoryWStream* fContent; |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1015 | SkPoint fCurrentMatrixOrigin; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1016 | SkScalar fXAdvance; |
| 1017 | bool fWideChars; |
| 1018 | bool fInText; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1019 | const bool fDefaultPositioning; |
halcanary | f0c30f5 | 2016-07-15 13:35:45 -0700 | [diff] [blame] | 1020 | }; |
| 1021 | } // namespace |
| 1022 | |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1023 | static void draw_transparent_text(SkPDFDevice* device, |
| 1024 | const SkDraw& d, |
| 1025 | const void* text, size_t len, |
| 1026 | SkScalar x, SkScalar y, |
| 1027 | const SkPaint& srcPaint) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1028 | sk_sp<SkTypeface> defaultFace = SkTypeface::MakeDefault(); |
| 1029 | if (!SkPDFFont::CanEmbedTypeface(defaultFace.get(), device->getCanon())) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1030 | SkDebugf("SkPDF: default typeface should be embeddable"); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1031 | return; // Avoid infinite loop in release. |
| 1032 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1033 | SkPaint transparent; |
| 1034 | transparent.setTypeface(std::move(defaultFace)); |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1035 | transparent.setTextSize(srcPaint.getTextSize()); |
| 1036 | transparent.setColor(SK_ColorTRANSPARENT); |
| 1037 | switch (srcPaint.getTextEncoding()) { |
| 1038 | case SkPaint::kGlyphID_TextEncoding: { |
| 1039 | // Since a glyphId<->Unicode mapping is typeface-specific, |
| 1040 | // map back to Unicode first. |
| 1041 | size_t glyphCount = len / 2; |
| 1042 | SkAutoTMalloc<SkUnichar> unichars(glyphCount); |
| 1043 | srcPaint.glyphsToUnichars( |
| 1044 | (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]); |
| 1045 | transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1046 | // TODO(halcanary): deal with case where default typeface |
| 1047 | // does not have glyphs for these unicode code points. |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1048 | device->drawText(d, &unichars[0], |
| 1049 | glyphCount * sizeof(SkUnichar), |
| 1050 | x, y, transparent); |
| 1051 | break; |
| 1052 | } |
| 1053 | case SkPaint::kUTF8_TextEncoding: |
| 1054 | case SkPaint::kUTF16_TextEncoding: |
| 1055 | case SkPaint::kUTF32_TextEncoding: |
| 1056 | transparent.setTextEncoding(srcPaint.getTextEncoding()); |
| 1057 | device->drawText(d, text, len, x, y, transparent); |
| 1058 | break; |
| 1059 | default: |
| 1060 | SkFAIL("unknown text encoding"); |
| 1061 | } |
| 1062 | } |
| 1063 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1064 | void SkPDFDevice::internalDrawText( |
| 1065 | const SkDraw& d, const void* sourceText, size_t sourceByteCount, |
| 1066 | const SkScalar pos[], SkTextBlob::GlyphPositioning positioning, |
| 1067 | SkPoint offset, const SkPaint& srcPaint) { |
| 1068 | NOT_IMPLEMENTED(srcPaint.getMaskFilter() != nullptr, false); |
| 1069 | if (srcPaint.getMaskFilter() != nullptr) { |
| 1070 | // Don't pretend we support drawing MaskFilters, it makes for artifacts |
| 1071 | // making text unreadable (e.g. same text twice when using CSS shadows). |
| 1072 | return; |
| 1073 | } |
halcanary | ea17dfe | 2016-08-24 09:20:57 -0700 | [diff] [blame] | 1074 | NOT_IMPLEMENTED(srcPaint.isVerticalText(), false); |
| 1075 | if (srcPaint.isVerticalText()) { |
| 1076 | // Don't pretend we support drawing vertical text. It is not |
| 1077 | // clear to me how to switch to "vertical writing" mode in PDF. |
| 1078 | // Currently neither Chromium or Android set this flag. |
| 1079 | // https://bug.skia.org/5665 |
| 1080 | return; |
| 1081 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1082 | SkPaint paint = calculate_text_paint(srcPaint); |
| 1083 | replace_srcmode_on_opaque_paint(&paint); |
| 1084 | if (!paint.getTypeface()) { |
| 1085 | paint.setTypeface(SkTypeface::MakeDefault()); |
| 1086 | } |
| 1087 | SkTypeface* typeface = paint.getTypeface(); |
| 1088 | if (!typeface) { |
| 1089 | SkDebugf("SkPDF: SkTypeface::MakeDefault() returned nullptr.\n"); |
| 1090 | return; |
| 1091 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1092 | |
| 1093 | const SkAdvancedTypefaceMetrics* metrics = |
| 1094 | SkPDFFont::GetMetrics(typeface, fDocument->canon()); |
| 1095 | if (!metrics) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1096 | return; |
| 1097 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1098 | // TODO(halcanary): use metrics->fGlyphToUnicode to check Unicode mapping. |
| 1099 | const SkGlyphID maxGlyphID = metrics->fLastGlyphID; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1100 | if (!SkPDFFont::CanEmbedTypeface(typeface, fDocument->canon())) { |
| 1101 | SkPath path; // https://bug.skia.org/3866 |
| 1102 | paint.getTextPath(sourceText, sourceByteCount, |
| 1103 | offset.x(), offset.y(), &path); |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1104 | this->drawPath(d, path, srcPaint, &SkMatrix::I(), true); |
| 1105 | // Draw text transparently to make it copyable/searchable/accessable. |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1106 | draw_transparent_text(this, d, sourceText, sourceByteCount, |
| 1107 | offset.x(), offset.y(), paint); |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1108 | return; |
| 1109 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1110 | int glyphCount = paint.textToGlyphs(sourceText, sourceByteCount, nullptr); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1111 | if (glyphCount <= 0) { |
| 1112 | return; |
| 1113 | } |
| 1114 | SkAutoSTMalloc<128, SkGlyphID> glyphStorage; |
| 1115 | const SkGlyphID* glyphs = nullptr; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1116 | if (paint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1117 | glyphs = (const SkGlyphID*)sourceText; |
| 1118 | // validate input later. |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1119 | } else { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1120 | glyphStorage.reset(SkToSizeT(glyphCount)); |
| 1121 | (void)paint.textToGlyphs(sourceText, sourceByteCount, glyphStorage.get()); |
| 1122 | glyphs = glyphStorage.get(); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1123 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 1124 | } |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1125 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1126 | bool defaultPositioning = (positioning == SkTextBlob::kDefault_Positioning); |
halcanary | 9df5a4c | 2016-08-24 10:08:13 -0700 | [diff] [blame] | 1127 | paint.setHinting(SkPaint::kNo_Hinting); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1128 | SkAutoGlyphCache glyphCache(paint, nullptr, nullptr); |
| 1129 | |
| 1130 | SkPaint::Align alignment = paint.getTextAlign(); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1131 | float alignmentFactor = SkPaint::kLeft_Align == alignment ? 0.0f : |
| 1132 | SkPaint::kCenter_Align == alignment ? -0.5f : |
| 1133 | /* SkPaint::kRight_Align */ -1.0f; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1134 | if (defaultPositioning && alignment != SkPaint::kLeft_Align) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1135 | SkScalar advance = 0; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1136 | for (int i = 0; i < glyphCount; ++i) { |
| 1137 | advance += glyphCache->getGlyphIDAdvance(glyphs[i]).fAdvanceX; |
| 1138 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1139 | offset.offset(alignmentFactor * advance, 0); |
halcanary | 6059dc3 | 2016-08-15 11:45:36 -0700 | [diff] [blame] | 1140 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1141 | ScopedContentEntry content(this, d, paint, true); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1142 | if (!content.entry()) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1143 | return; |
| 1144 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1145 | SkDynamicMemoryWStream* out = &content.entry()->fContent; |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1146 | SkScalar textSize = paint.getTextSize(); |
| 1147 | |
| 1148 | int index = 0; |
| 1149 | while (glyphs[index] > maxGlyphID) { // Invalid glyphID for this font. |
| 1150 | ++index; // Skip this glyphID |
| 1151 | if (index == glyphCount) { |
| 1152 | return; // all glyphIDs were bad. |
| 1153 | } |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1154 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1155 | |
| 1156 | out->writeText("BT\n"); |
| 1157 | SK_AT_SCOPE_EXIT(out->writeText("ET\n")); |
| 1158 | |
| 1159 | SkPDFFont* font = this->updateFont( |
| 1160 | typeface, textSize, glyphs[index], content.entry()); |
| 1161 | SkASSERT(font); // All preconditions for SkPDFFont::GetFontResource are met. |
| 1162 | if (!font) { return; } |
| 1163 | |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1164 | GlyphPositioner glyphPositioner(out, |
| 1165 | paint.getTextSkewX(), |
| 1166 | font->multiByteGlyphs(), |
| 1167 | defaultPositioning, |
| 1168 | offset); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1169 | |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1170 | while (index < glyphCount) { |
| 1171 | int stretch = font->countStretch(&glyphs[index], glyphCount - index, maxGlyphID); |
| 1172 | SkASSERT(index + stretch <= glyphCount); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1173 | if (stretch < 1) { |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1174 | // The current pdf font cannot encode the next glyph. |
| 1175 | // Try to get a pdf font which can encode the next glyph. |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1176 | glyphPositioner.flush(); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1177 | // first, validate the next glyph |
| 1178 | while (glyphs[index] > maxGlyphID) { |
| 1179 | ++index; // Skip this glyphID |
| 1180 | if (index == glyphCount) { |
| 1181 | return; // all remainng glyphIDs were bad. |
| 1182 | } |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1183 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1184 | SkASSERT(index < glyphCount); |
| 1185 | font = this->updateFont(typeface, textSize, glyphs[index], content.entry()); |
| 1186 | SkASSERT(font); // preconditions for SkPDFFont::GetFontResource met. |
| 1187 | if (!font) { return; } |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1188 | glyphPositioner.setWideChars(font->multiByteGlyphs()); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1189 | // Get stretch for this new font. |
| 1190 | stretch = font->countStretch(&glyphs[index], glyphCount - index, maxGlyphID); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1191 | if (stretch < 1) { |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1192 | SkDEBUGFAIL("PDF could not encode glyph."); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1193 | return; |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1194 | } |
| 1195 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1196 | while (stretch-- > 0) { |
| 1197 | SkGlyphID gid = glyphs[index]; |
| 1198 | if (gid <= maxGlyphID) { |
| 1199 | font->noteGlyphUsage(gid); |
| 1200 | SkGlyphID encodedGlyph = font->glyphToPDFFontEncoding(gid); |
| 1201 | if (defaultPositioning) { |
| 1202 | glyphPositioner.writeGlyph(SkPoint{0, 0}, 0, encodedGlyph); |
| 1203 | } else { |
| 1204 | SkScalar advance = glyphCache->getGlyphIDAdvance(gid).fAdvanceX; |
| 1205 | SkPoint xy = SkTextBlob::kFull_Positioning == positioning |
| 1206 | ? SkPoint{pos[2 * index], pos[2 * index + 1]} |
| 1207 | : SkPoint{pos[index], 0}; |
| 1208 | if (alignment != SkPaint::kLeft_Align) { |
| 1209 | xy.offset(alignmentFactor * advance, 0); |
| 1210 | } |
| 1211 | glyphPositioner.writeGlyph(xy, advance, encodedGlyph); |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1212 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1213 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1214 | ++index; |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1215 | } |
robertphillips | 5ba165e | 2016-08-15 15:36:58 -0700 | [diff] [blame] | 1216 | } |
halcanary | 4ed2f01 | 2016-08-15 18:40:07 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len, |
| 1220 | SkScalar x, SkScalar y, const SkPaint& paint) { |
| 1221 | this->internalDrawText(d, text, len, nullptr, SkTextBlob::kDefault_Positioning, |
| 1222 | SkPoint{x, y}, paint); |
| 1223 | } |
| 1224 | |
| 1225 | void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len, |
| 1226 | const SkScalar pos[], int scalarsPerPos, |
| 1227 | const SkPoint& offset, const SkPaint& paint) { |
| 1228 | this->internalDrawText(d, text, len, pos, (SkTextBlob::GlyphPositioning)scalarsPerPos, |
| 1229 | offset, paint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1232 | void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1233 | int vertexCount, const SkPoint verts[], |
| 1234 | const SkPoint texs[], const SkColor colors[], |
| 1235 | SkXfermode* xmode, const uint16_t indices[], |
| 1236 | int indexCount, const SkPaint& paint) { |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1237 | if (d.fRC->isEmpty()) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1238 | return; |
| 1239 | } |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1240 | // TODO: implement drawVertices |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1243 | void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device, |
| 1244 | int x, int y, const SkPaint& paint) { |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 1245 | SkASSERT(!paint.getImageFilter()); |
| 1246 | |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1247 | // Check if the source device is really a bitmapdevice (because that's what we returned |
| 1248 | // from createDevice (likely due to an imagefilter) |
| 1249 | SkPixmap pmap; |
| 1250 | if (device->peekPixels(&pmap)) { |
| 1251 | SkBitmap bitmap; |
| 1252 | bitmap.installPixels(pmap); |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 1253 | this->drawSprite(d, bitmap, x, y, paint); |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1254 | return; |
| 1255 | } |
| 1256 | |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 1257 | // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses. |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1258 | SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1259 | |
| 1260 | SkScalar scalarX = SkIntToScalar(x); |
| 1261 | SkScalar scalarY = SkIntToScalar(y); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1262 | for (const RectWithData& l : pdfDevice->fLinkToURLs) { |
| 1263 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1264 | fLinkToURLs.emplace_back(r, l.data.get()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1265 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1266 | for (const RectWithData& l : pdfDevice->fLinkToDestinations) { |
| 1267 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1268 | fLinkToDestinations.emplace_back(r, l.data.get()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1269 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1270 | for (const NamedDestination& d : pdfDevice->fNamedDestinations) { |
| 1271 | SkPoint p = d.point + SkPoint::Make(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1272 | fNamedDestinations.emplace_back(d.nameData.get(), p); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
ctguil@chromium.org | f4ff39c | 2011-05-24 19:55:05 +0000 | [diff] [blame] | 1275 | if (pdfDevice->isContentEmpty()) { |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1276 | return; |
| 1277 | } |
| 1278 | |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 1279 | SkMatrix matrix; |
reed@google.com | a6d59f6 | 2011-03-07 21:29:21 +0000 | [diff] [blame] | 1280 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1281 | ScopedContentEntry content(this, d.fClipStack, d.fRC->bwRgn(), matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1282 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1283 | return; |
| 1284 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1285 | if (content.needShape()) { |
| 1286 | SkPath shape; |
| 1287 | shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
vandebo@chromium.org | fd3c8c2 | 2013-10-30 21:00:47 +0000 | [diff] [blame] | 1288 | SkIntToScalar(device->width()), |
| 1289 | SkIntToScalar(device->height()))); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1290 | content.setShape(shape); |
| 1291 | } |
| 1292 | if (!content.needSource()) { |
| 1293 | return; |
| 1294 | } |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 1295 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1296 | sk_sp<SkPDFObject> xObject = pdfDevice->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1297 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1298 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1301 | sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 1302 | return SkSurface::MakeRaster(info, &props); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1305 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1306 | sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const { |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1307 | SkTDArray<SkPDFObject*> fonts; |
| 1308 | fonts.setReserve(fFontResources.count()); |
| 1309 | for (SkPDFFont* font : fFontResources) { |
| 1310 | fonts.push(font); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1311 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1312 | return SkPDFResourceDict::Make( |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1313 | &fGraphicStateResources, |
| 1314 | &fShaderResources, |
| 1315 | &fXObjectResources, |
| 1316 | &fonts); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1319 | sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1320 | auto mediaBox = sk_make_sp<SkPDFArray>(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1321 | mediaBox->reserve(4); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 1322 | mediaBox->appendInt(0); |
| 1323 | mediaBox->appendInt(0); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1324 | mediaBox->appendInt(fPageSize.width()); |
| 1325 | mediaBox->appendInt(fPageSize.height()); |
| 1326 | return mediaBox; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1329 | std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const { |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1330 | SkDynamicMemoryWStream buffer; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1331 | if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) { |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1332 | SkPDFUtils::AppendTransform(fInitialTransform, &buffer); |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 1333 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1334 | |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1335 | GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, &buffer); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1336 | for (const auto& entry : fContentEntries) { |
| 1337 | SkPoint translation; |
| 1338 | translation.iset(this->getOrigin()); |
| 1339 | translation.negate(); |
| 1340 | gsState.updateClip(entry.fState.fClipStack, entry.fState.fClipRegion, |
| 1341 | translation); |
| 1342 | gsState.updateMatrix(entry.fState.fMatrix); |
| 1343 | gsState.updateDrawingState(entry.fState); |
| 1344 | |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1345 | entry.fContent.writeToStream(&buffer); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1346 | } |
| 1347 | gsState.drainStack(); |
halcanary | 022c2bd | 2016-09-02 11:29:46 -0700 | [diff] [blame^] | 1348 | if (buffer.bytesWritten() > 0) { |
| 1349 | return std::unique_ptr<SkStreamAsset>(buffer.detachAsStream()); |
| 1350 | } else { |
| 1351 | return skstd::make_unique<SkMemoryStream>(); |
| 1352 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1355 | /* Draws an inverse filled path by using Path Ops to compute the positive |
| 1356 | * inverse using the current clip as the inverse bounds. |
| 1357 | * Return true if this was an inverse path and was properly handled, |
| 1358 | * otherwise returns false and the normal drawing routine should continue, |
| 1359 | * either as a (incorrect) fallback or because the path was not inverse |
| 1360 | * in the first place. |
| 1361 | */ |
| 1362 | bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath, |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1363 | const SkPaint& paint, bool pathIsMutable, |
| 1364 | const SkMatrix* prePathMatrix) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1365 | if (!origPath.isInverseFillType()) { |
| 1366 | return false; |
| 1367 | } |
| 1368 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1369 | if (d.fRC->isEmpty()) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1370 | return false; |
| 1371 | } |
| 1372 | |
| 1373 | SkPath modifiedPath; |
| 1374 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
| 1375 | SkPaint noInversePaint(paint); |
| 1376 | |
| 1377 | // Merge stroking operations into final path. |
| 1378 | if (SkPaint::kStroke_Style == paint.getStyle() || |
| 1379 | SkPaint::kStrokeAndFill_Style == paint.getStyle()) { |
| 1380 | bool doFillPath = paint.getFillPath(origPath, &modifiedPath); |
| 1381 | if (doFillPath) { |
| 1382 | noInversePaint.setStyle(SkPaint::kFill_Style); |
| 1383 | noInversePaint.setStrokeWidth(0); |
| 1384 | pathPtr = &modifiedPath; |
| 1385 | } else { |
| 1386 | // To be consistent with the raster output, hairline strokes |
| 1387 | // are rendered as non-inverted. |
| 1388 | modifiedPath.toggleInverseFillType(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1389 | drawPath(d, modifiedPath, paint, nullptr, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1390 | return true; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | // Get bounds of clip in current transform space |
| 1395 | // (clip bounds are given in device space). |
| 1396 | SkRect bounds; |
| 1397 | SkMatrix transformInverse; |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1398 | SkMatrix totalMatrix = *d.fMatrix; |
| 1399 | if (prePathMatrix) { |
| 1400 | totalMatrix.preConcat(*prePathMatrix); |
| 1401 | } |
| 1402 | if (!totalMatrix.invert(&transformInverse)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1403 | return false; |
| 1404 | } |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1405 | bounds.set(d.fRC->getBounds()); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1406 | transformInverse.mapRect(&bounds); |
| 1407 | |
| 1408 | // Extend the bounds by the line width (plus some padding) |
| 1409 | // so the edge doesn't cause a visible stroke. |
| 1410 | bounds.outset(paint.getStrokeWidth() + SK_Scalar1, |
| 1411 | paint.getStrokeWidth() + SK_Scalar1); |
| 1412 | |
| 1413 | if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) { |
| 1414 | return false; |
| 1415 | } |
| 1416 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1417 | drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1418 | return true; |
| 1419 | } |
| 1420 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1421 | void SkPDFDevice::handlePointAnnotation(const SkPoint& point, |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1422 | const SkMatrix& matrix, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1423 | const char key[], SkData* value) { |
| 1424 | if (!value) { |
| 1425 | return; |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1426 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1427 | |
| 1428 | if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) { |
| 1429 | SkPoint transformedPoint; |
| 1430 | matrix.mapXY(point.x(), point.y(), &transformedPoint); |
| 1431 | fNamedDestinations.emplace_back(value, transformedPoint); |
| 1432 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1433 | } |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 1434 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1435 | void SkPDFDevice::handlePathAnnotation(const SkPath& path, |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1436 | const SkDraw& d, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1437 | const char key[], SkData* value) { |
| 1438 | if (!value) { |
| 1439 | return; |
| 1440 | } |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1441 | |
| 1442 | SkPath transformedPath = path; |
| 1443 | transformedPath.transform(*d.fMatrix); |
| 1444 | SkRasterClip clip = *d.fRC; |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 1445 | clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op, |
| 1446 | false); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1447 | SkRect transformedRect = SkRect::Make(clip.getBounds()); |
| 1448 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1449 | if (!strcmp(SkAnnotationKeys::URL_Key(), key)) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1450 | if (!transformedRect.isEmpty()) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1451 | fLinkToURLs.emplace_back(transformedRect, value); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1452 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1453 | } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) { |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1454 | if (!transformedRect.isEmpty()) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1455 | fLinkToDestinations.emplace_back(transformedRect, value); |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1456 | } |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1457 | } |
halcanary | 438de49 | 2015-04-28 06:21:01 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1460 | void SkPDFDevice::appendAnnotations(SkPDFArray* array) const { |
| 1461 | array->reserve(fLinkToURLs.count() + fLinkToDestinations.count()); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1462 | for (const RectWithData& rectWithURL : fLinkToURLs) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1463 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1464 | fInitialTransform.mapRect(&r, rectWithURL.rect); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1465 | array->appendObject(create_link_to_url(rectWithURL.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1466 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1467 | for (const RectWithData& linkToDestination : fLinkToDestinations) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1468 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1469 | fInitialTransform.mapRect(&r, linkToDestination.rect); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1470 | array->appendObject( |
| 1471 | create_link_named_dest(linkToDestination.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1472 | } |
| 1473 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1474 | |
halcanary | 6d62270 | 2015-03-25 08:45:42 -0700 | [diff] [blame] | 1475 | void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const { |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1476 | for (const NamedDestination& dest : fNamedDestinations) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1477 | auto pdfDest = sk_make_sp<SkPDFArray>(); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1478 | pdfDest->reserve(5); |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 1479 | pdfDest->appendObjRef(sk_ref_sp(page)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1480 | pdfDest->appendName("XYZ"); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1481 | SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1482 | pdfDest->appendScalar(p.x()); |
| 1483 | pdfDest->appendScalar(p.y()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1484 | pdfDest->appendInt(0); // Leave zoom unchanged |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1485 | SkString name(static_cast<const char*>(dest.nameData->data())); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1486 | dict->insertObject(name, std::move(pdfDest)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1487 | } |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1490 | sk_sp<SkPDFObject> SkPDFDevice::makeFormXObjectFromDevice() { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1491 | SkMatrix inverseTransform = SkMatrix::I(); |
halcanary | afdc177 | 2016-08-23 09:02:12 -0700 | [diff] [blame] | 1492 | if (!fInitialTransform.isIdentity()) { |
| 1493 | if (!fInitialTransform.invert(&inverseTransform)) { |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1494 | SkDEBUGFAIL("Layer initial transform should be invertible."); |
| 1495 | inverseTransform.reset(); |
| 1496 | } |
| 1497 | } |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1498 | sk_sp<SkPDFObject> xobject = |
| 1499 | SkPDFMakeFormXObject(this->content(), this->copyMediaBox(), |
halcanary | 5abbb44 | 2016-07-29 08:41:33 -0700 | [diff] [blame] | 1500 | this->makeResourceDict(), inverseTransform, nullptr); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1501 | // We always draw the form xobjects that we create back into the device, so |
| 1502 | // we simply preserve the font usage instead of pulling it out and merging |
| 1503 | // it back in later. |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1504 | this->cleanUp(); // Reset this device to have no content. |
| 1505 | this->init(); |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1506 | return xobject; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1509 | void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex, |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1510 | sk_sp<SkPDFObject> mask, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1511 | const SkClipStack* clipStack, |
| 1512 | const SkRegion& clipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1513 | SkXfermode::Mode mode, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1514 | bool invertClip) { |
| 1515 | if (clipRegion.isEmpty() && !invertClip) { |
| 1516 | return; |
| 1517 | } |
| 1518 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1519 | sk_sp<SkPDFDict> sMaskGS = SkPDFGraphicState::GetSMaskGraphicState( |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1520 | std::move(mask), invertClip, |
| 1521 | SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon()); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1522 | |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1523 | SkMatrix identity; |
| 1524 | identity.reset(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1525 | SkPaint paint; |
| 1526 | paint.setXfermodeMode(mode); |
| 1527 | ScopedContentEntry content(this, clipStack, clipRegion, identity, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1528 | if (!content.entry()) { |
| 1529 | return; |
| 1530 | } |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1531 | SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1532 | &content.entry()->fContent); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1533 | SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1534 | |
halcanary | 1437c1e | 2016-03-13 18:30:24 -0700 | [diff] [blame] | 1535 | // Call makeNoSmaskGraphicState() instead of |
| 1536 | // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon |
| 1537 | // can deduplicate. |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1538 | sMaskGS = fDocument->canon()->makeNoSmaskGraphicState(); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1539 | SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1540 | &content.entry()->fContent); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1543 | SkPDFDevice::ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1544 | const SkRegion& clipRegion, |
| 1545 | const SkMatrix& matrix, |
| 1546 | const SkPaint& paint, |
| 1547 | bool hasText, |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1548 | sk_sp<SkPDFObject>* dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1549 | *dst = nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1550 | if (clipRegion.isEmpty()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1551 | return nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1554 | // The clip stack can come from an SkDraw where it is technically optional. |
| 1555 | SkClipStack synthesizedClipStack; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1556 | if (clipStack == nullptr) { |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1557 | if (clipRegion == fExistingClipRegion) { |
| 1558 | clipStack = &fExistingClipStack; |
| 1559 | } else { |
| 1560 | // GraphicStackState::updateClip expects the clip stack to have |
| 1561 | // fExistingClip as a prefix, so start there, then set the clip |
| 1562 | // to the passed region. |
| 1563 | synthesizedClipStack = fExistingClipStack; |
| 1564 | SkPath clipPath; |
| 1565 | clipRegion.getBoundaryPath(&clipPath); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1566 | synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op, |
| 1567 | false); |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1568 | clipStack = &synthesizedClipStack; |
| 1569 | } |
| 1570 | } |
| 1571 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1572 | SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode; |
| 1573 | if (paint.getXfermode()) { |
| 1574 | paint.getXfermode()->asMode(&xfermode); |
| 1575 | } |
| 1576 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1577 | // For the following modes, we want to handle source and destination |
| 1578 | // separately, so make an object of what's already there. |
| 1579 | if (xfermode == SkXfermode::kClear_Mode || |
| 1580 | xfermode == SkXfermode::kSrc_Mode || |
| 1581 | xfermode == SkXfermode::kSrcIn_Mode || |
| 1582 | xfermode == SkXfermode::kDstIn_Mode || |
| 1583 | xfermode == SkXfermode::kSrcOut_Mode || |
| 1584 | xfermode == SkXfermode::kDstOut_Mode || |
| 1585 | xfermode == SkXfermode::kSrcATop_Mode || |
| 1586 | xfermode == SkXfermode::kDstATop_Mode || |
| 1587 | xfermode == SkXfermode::kModulate_Mode) { |
| 1588 | if (!isContentEmpty()) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1589 | *dst = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1590 | SkASSERT(isContentEmpty()); |
| 1591 | } else if (xfermode != SkXfermode::kSrc_Mode && |
| 1592 | xfermode != SkXfermode::kSrcOut_Mode) { |
| 1593 | // Except for Src and SrcOut, if there isn't anything already there, |
| 1594 | // then we're done. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1595 | return nullptr; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1596 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1597 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1598 | // TODO(vandebo): Figure out how/if we can handle the following modes: |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1599 | // Xor, Plus. |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1600 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1601 | // Dst xfer mode doesn't draw source at all. |
| 1602 | if (xfermode == SkXfermode::kDst_Mode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1603 | return nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1606 | SkPDFDevice::ContentEntry* entry; |
| 1607 | if (fContentEntries.back() && fContentEntries.back()->fContent.getOffset() == 0) { |
| 1608 | entry = fContentEntries.back(); |
| 1609 | } else if (xfermode != SkXfermode::kDstOver_Mode) { |
| 1610 | entry = fContentEntries.emplace_back(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1611 | } else { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1612 | entry = fContentEntries.emplace_front(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1613 | } |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1614 | populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1615 | hasText, &entry->fState); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1616 | return entry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1619 | void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode, |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1620 | sk_sp<SkPDFObject> dst, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1621 | SkPath* shape) { |
| 1622 | if (xfermode != SkXfermode::kClear_Mode && |
| 1623 | xfermode != SkXfermode::kSrc_Mode && |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1624 | xfermode != SkXfermode::kDstOver_Mode && |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1625 | xfermode != SkXfermode::kSrcIn_Mode && |
| 1626 | xfermode != SkXfermode::kDstIn_Mode && |
| 1627 | xfermode != SkXfermode::kSrcOut_Mode && |
| 1628 | xfermode != SkXfermode::kDstOut_Mode && |
| 1629 | xfermode != SkXfermode::kSrcATop_Mode && |
| 1630 | xfermode != SkXfermode::kDstATop_Mode && |
| 1631 | xfermode != SkXfermode::kModulate_Mode) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1632 | SkASSERT(!dst); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1633 | return; |
| 1634 | } |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1635 | if (xfermode == SkXfermode::kDstOver_Mode) { |
| 1636 | SkASSERT(!dst); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1637 | if (fContentEntries.front()->fContent.getOffset() == 0) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1638 | // For DstOver, an empty content entry was inserted before the rest |
| 1639 | // of the content entries. If nothing was drawn, it needs to be |
| 1640 | // removed. |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1641 | fContentEntries.pop_front(); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1642 | } |
| 1643 | return; |
| 1644 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1645 | if (!dst) { |
| 1646 | SkASSERT(xfermode == SkXfermode::kSrc_Mode || |
| 1647 | xfermode == SkXfermode::kSrcOut_Mode); |
| 1648 | return; |
| 1649 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1650 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1651 | SkASSERT(dst); |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1652 | SkASSERT(fContentEntries.count() == 1); |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1653 | // Changing the current content into a form-xobject will destroy the clip |
| 1654 | // objects which is fine since the xobject will already be clipped. However |
| 1655 | // if source has shape, we need to clip it too, so a copy of the clip is |
| 1656 | // saved. |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1657 | |
| 1658 | SkClipStack clipStack = fContentEntries.front()->fState.fClipStack; |
| 1659 | SkRegion clipRegion = fContentEntries.front()->fState.fClipRegion; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1660 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1661 | SkMatrix identity; |
| 1662 | identity.reset(); |
| 1663 | SkPaint stockPaint; |
| 1664 | |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1665 | sk_sp<SkPDFObject> srcFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1666 | if (isContentEmpty()) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1667 | // If nothing was drawn and there's no shape, then the draw was a |
| 1668 | // no-op, but dst needs to be restored for that to be true. |
| 1669 | // If there is shape, then an empty source with Src, SrcIn, SrcOut, |
| 1670 | // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop |
| 1671 | // reduces to Dst. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1672 | if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode || |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1673 | xfermode == SkXfermode::kSrcATop_Mode) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1674 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1675 | fExistingClipRegion, identity, |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1676 | stockPaint); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1677 | // TODO: addXObjectResource take sk_sp |
| 1678 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()), |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1679 | &content.entry()->fContent); |
| 1680 | return; |
| 1681 | } else { |
| 1682 | xfermode = SkXfermode::kClear_Mode; |
| 1683 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1684 | } else { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1685 | SkASSERT(fContentEntries.count() == 1); |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 1686 | srcFormXObject = this->makeFormXObjectFromDevice(); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1689 | // TODO(vandebo) srcFormXObject may contain alpha, but here we want it |
| 1690 | // without alpha. |
| 1691 | if (xfermode == SkXfermode::kSrcATop_Mode) { |
| 1692 | // TODO(vandebo): In order to properly support SrcATop we have to track |
| 1693 | // the shape of what's been drawn at all times. It's the intersection of |
| 1694 | // the non-transparent parts of the device and the outlines (shape) of |
| 1695 | // all images and devices drawn. |
| 1696 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst, |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1697 | &fExistingClipStack, fExistingClipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1698 | SkXfermode::kSrcOver_Mode, true); |
| 1699 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1700 | if (shape != nullptr) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1701 | // Draw shape into a form-xobject. |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1702 | SkRasterClip rc(clipRegion); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1703 | SkDraw d; |
| 1704 | d.fMatrix = &identity; |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 1705 | d.fRC = &rc; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1706 | d.fClipStack = &clipStack; |
| 1707 | SkPaint filledPaint; |
| 1708 | filledPaint.setColor(SK_ColorBLACK); |
| 1709 | filledPaint.setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1710 | this->drawPath(d, *shape, filledPaint, nullptr, true); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1711 | drawFormXObjectWithMask(addXObjectResource(dst.get()), |
| 1712 | this->makeFormXObjectFromDevice(), |
| 1713 | &fExistingClipStack, fExistingClipRegion, |
| 1714 | SkXfermode::kSrcOver_Mode, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1715 | |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1716 | } else { |
| 1717 | drawFormXObjectWithMask(addXObjectResource(dst.get()), srcFormXObject, |
| 1718 | &fExistingClipStack, fExistingClipRegion, |
| 1719 | SkXfermode::kSrcOver_Mode, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1720 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1723 | if (xfermode == SkXfermode::kClear_Mode) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1724 | return; |
| 1725 | } else if (xfermode == SkXfermode::kSrc_Mode || |
| 1726 | xfermode == SkXfermode::kDstATop_Mode) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1727 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1728 | fExistingClipRegion, identity, stockPaint); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1729 | if (content.entry()) { |
| 1730 | SkPDFUtils::DrawFormXObject( |
| 1731 | this->addXObjectResource(srcFormXObject.get()), |
| 1732 | &content.entry()->fContent); |
| 1733 | } |
| 1734 | if (xfermode == SkXfermode::kSrc_Mode) { |
| 1735 | return; |
| 1736 | } |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1737 | } else if (xfermode == SkXfermode::kSrcATop_Mode) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1738 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1739 | fExistingClipRegion, identity, stockPaint); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1740 | if (content.entry()) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1741 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst.get()), |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1742 | &content.entry()->fContent); |
| 1743 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | SkASSERT(xfermode == SkXfermode::kSrcIn_Mode || |
| 1747 | xfermode == SkXfermode::kDstIn_Mode || |
| 1748 | xfermode == SkXfermode::kSrcOut_Mode || |
| 1749 | xfermode == SkXfermode::kDstOut_Mode || |
| 1750 | xfermode == SkXfermode::kSrcATop_Mode || |
| 1751 | xfermode == SkXfermode::kDstATop_Mode || |
| 1752 | xfermode == SkXfermode::kModulate_Mode); |
| 1753 | |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1754 | if (xfermode == SkXfermode::kSrcIn_Mode || |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1755 | xfermode == SkXfermode::kSrcOut_Mode || |
| 1756 | xfermode == SkXfermode::kSrcATop_Mode) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1757 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), |
| 1758 | std::move(dst), |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1759 | &fExistingClipStack, fExistingClipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1760 | SkXfermode::kSrcOver_Mode, |
| 1761 | xfermode == SkXfermode::kSrcOut_Mode); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1762 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1763 | } else { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1764 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode; |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1765 | int resourceID = addXObjectResource(dst.get()); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1766 | if (xfermode == SkXfermode::kModulate_Mode) { |
| 1767 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1768 | std::move(dst), &fExistingClipStack, |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1769 | fExistingClipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1770 | SkXfermode::kSrcOver_Mode, false); |
| 1771 | mode = SkXfermode::kMultiply_Mode; |
| 1772 | } |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1773 | drawFormXObjectWithMask(resourceID, std::move(srcFormXObject), |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1774 | &fExistingClipStack, fExistingClipRegion, mode, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1775 | xfermode == SkXfermode::kDstOut_Mode); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1776 | return; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1777 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1780 | bool SkPDFDevice::isContentEmpty() { |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1781 | if (!fContentEntries.front() || fContentEntries.front()->fContent.getOffset() == 0) { |
| 1782 | SkASSERT(fContentEntries.count() <= 1); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1783 | return true; |
| 1784 | } |
| 1785 | return false; |
| 1786 | } |
| 1787 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1788 | void SkPDFDevice::populateGraphicStateEntryFromPaint( |
| 1789 | const SkMatrix& matrix, |
| 1790 | const SkClipStack& clipStack, |
| 1791 | const SkRegion& clipRegion, |
| 1792 | const SkPaint& paint, |
| 1793 | bool hasText, |
halcanary | 2be7e01 | 2016-03-28 11:58:08 -0700 | [diff] [blame] | 1794 | SkPDFDevice::GraphicStateEntry* entry) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1795 | NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false); |
| 1796 | NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false); |
| 1797 | NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1798 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1799 | entry->fMatrix = matrix; |
| 1800 | entry->fClipStack = clipStack; |
| 1801 | entry->fClipRegion = clipRegion; |
vandebo@chromium.org | da6c569 | 2012-06-28 21:37:20 +0000 | [diff] [blame] | 1802 | entry->fColor = SkColorSetA(paint.getColor(), 0xFF); |
| 1803 | entry->fShaderIndex = -1; |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 1804 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1805 | // PDF treats a shader as a color, so we only set one or the other. |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1806 | sk_sp<SkPDFObject> pdfShader; |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1807 | SkShader* shader = paint.getShader(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1808 | SkColor color = paint.getColor(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1809 | if (shader) { |
| 1810 | // PDF positions patterns relative to the initial transform, so |
| 1811 | // we need to apply the current transform to the shader parameters. |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1812 | SkMatrix transform = matrix; |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 1813 | transform.postConcat(fInitialTransform); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1814 | |
| 1815 | // PDF doesn't support kClamp_TileMode, so we simulate it by making |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1816 | // a pattern the size of the current clip. |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1817 | SkIRect bounds = clipRegion.getBounds(); |
vandebo@chromium.org | 293a758 | 2012-03-16 19:50:37 +0000 | [diff] [blame] | 1818 | |
| 1819 | // We need to apply the initial transform to bounds in order to get |
| 1820 | // bounds in a consistent coordinate system. |
| 1821 | SkRect boundsTemp; |
| 1822 | boundsTemp.set(bounds); |
| 1823 | fInitialTransform.mapRect(&boundsTemp); |
| 1824 | boundsTemp.roundOut(&bounds); |
| 1825 | |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 1826 | SkScalar rasterScale = |
| 1827 | SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE; |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1828 | pdfShader = SkPDFShader::GetPDFShader( |
| 1829 | fDocument, fRasterDpi, shader, transform, bounds, rasterScale); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1830 | |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 1831 | if (pdfShader.get()) { |
| 1832 | // pdfShader has been canonicalized so we can directly compare |
| 1833 | // pointers. |
| 1834 | int resourceIndex = fShaderResources.find(pdfShader.get()); |
| 1835 | if (resourceIndex < 0) { |
| 1836 | resourceIndex = fShaderResources.count(); |
| 1837 | fShaderResources.push(pdfShader.get()); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1838 | pdfShader.get()->ref(); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 1839 | } |
| 1840 | entry->fShaderIndex = resourceIndex; |
| 1841 | } else { |
| 1842 | // A color shader is treated as an invalid shader so we don't have |
| 1843 | // to set a shader just for a color. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1844 | SkShader::GradientInfo gradientInfo; |
| 1845 | SkColor gradientColor; |
| 1846 | gradientInfo.fColors = &gradientColor; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1847 | gradientInfo.fColorOffsets = nullptr; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1848 | gradientInfo.fColorCount = 1; |
| 1849 | if (shader->asAGradient(&gradientInfo) == |
| 1850 | SkShader::kColor_GradientType) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1851 | entry->fColor = SkColorSetA(gradientColor, 0xFF); |
| 1852 | color = gradientColor; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1853 | } |
| 1854 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1855 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1856 | |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1857 | sk_sp<SkPDFGraphicState> newGraphicState; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1858 | if (color == paint.getColor()) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1859 | newGraphicState.reset( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1860 | SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1861 | } else { |
| 1862 | SkPaint newPaint = paint; |
| 1863 | newPaint.setColor(color); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1864 | newGraphicState.reset( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1865 | SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1866 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1867 | int resourceIndex = addGraphicStateResource(newGraphicState.get()); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1868 | entry->fGraphicStateIndex = resourceIndex; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1869 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1870 | if (hasText) { |
| 1871 | entry->fTextScaleX = paint.getTextScaleX(); |
| 1872 | entry->fTextFill = paint.getStyle(); |
| 1873 | } else { |
| 1874 | entry->fTextScaleX = 0; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 1878 | int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1879 | // Assumes that gs has been canonicalized (so we can directly compare |
| 1880 | // pointers). |
| 1881 | int result = fGraphicStateResources.find(gs); |
| 1882 | if (result < 0) { |
| 1883 | result = fGraphicStateResources.count(); |
| 1884 | fGraphicStateResources.push(gs); |
| 1885 | gs->ref(); |
| 1886 | } |
| 1887 | return result; |
| 1888 | } |
| 1889 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1890 | int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) { |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1891 | // TODO(halcanary): make this take a sk_sp<SkPDFObject> |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1892 | // Assumes that xobject has been canonicalized (so we can directly compare |
| 1893 | // pointers). |
| 1894 | int result = fXObjectResources.find(xObject); |
| 1895 | if (result < 0) { |
| 1896 | result = fXObjectResources.count(); |
halcanary | dabd4f0 | 2016-08-03 11:16:56 -0700 | [diff] [blame] | 1897 | fXObjectResources.push(SkRef(xObject)); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1898 | } |
| 1899 | return result; |
| 1900 | } |
| 1901 | |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1902 | SkPDFFont* SkPDFDevice::updateFont(SkTypeface* typeface, |
| 1903 | SkScalar textSize, |
| 1904 | uint16_t glyphID, |
| 1905 | SkPDFDevice::ContentEntry* contentEntry) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1906 | if (contentEntry->fState.fFont == nullptr || |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1907 | contentEntry->fState.fTextSize != textSize || |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1908 | !contentEntry->fState.fFont->hasGlyph(glyphID)) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1909 | int fontIndex = getFontResourceIndex(typeface, glyphID); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1910 | if (fontIndex < 0) { |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1911 | SkDebugf("SkPDF: Font error."); |
| 1912 | return nullptr; |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1913 | } |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 1914 | contentEntry->fContent.writeText("/"); |
| 1915 | contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName( |
| 1916 | SkPDFResourceDict::kFont_ResourceType, |
| 1917 | fontIndex).c_str()); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1918 | contentEntry->fContent.writeText(" "); |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1919 | SkPDFUtils::AppendScalar(textSize, &contentEntry->fContent); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1920 | contentEntry->fContent.writeText(" Tf\n"); |
| 1921 | contentEntry->fState.fFont = fFontResources[fontIndex]; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1922 | } |
halcanary | 4871f22 | 2016-08-26 13:17:44 -0700 | [diff] [blame] | 1923 | return contentEntry->fState.fFont; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 1926 | int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1927 | sk_sp<SkPDFFont> newFont( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1928 | SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID)); |
halcanary | 7e8d5d3 | 2016-08-12 07:59:38 -0700 | [diff] [blame] | 1929 | if (!newFont) { |
| 1930 | return -1; |
| 1931 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1932 | int resourceIndex = fFontResources.find(newFont.get()); |
| 1933 | if (resourceIndex < 0) { |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 1934 | fDocument->registerFont(newFont.get()); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1935 | resourceIndex = fFontResources.count(); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 1936 | fFontResources.push(newFont.release()); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1937 | } |
| 1938 | return resourceIndex; |
| 1939 | } |
| 1940 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1941 | static SkSize rect_to_size(const SkRect& r) { |
| 1942 | return SkSize::Make(r.width(), r.height()); |
| 1943 | } |
| 1944 | |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1945 | static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap, |
| 1946 | SkColorFilter* colorFilter) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1947 | auto surface = |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1948 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions())); |
| 1949 | SkASSERT(surface); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1950 | SkCanvas* canvas = surface->getCanvas(); |
| 1951 | canvas->clear(SK_ColorTRANSPARENT); |
| 1952 | SkPaint paint; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 1953 | paint.setColorFilter(sk_ref_sp(colorFilter)); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1954 | imageBitmap.draw(canvas, &paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1955 | canvas->flush(); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1956 | return surface->makeImageSnapshot(); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | //////////////////////////////////////////////////////////////////////////////// |
| 1960 | void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix, |
| 1961 | const SkClipStack* clipStack, |
| 1962 | const SkRegion& origClipRegion, |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1963 | SkImageBitmap imageBitmap, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1964 | const SkPaint& paint) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1965 | if (imageBitmap.dimensions().isZero()) { |
| 1966 | return; |
| 1967 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1968 | #ifdef SK_PDF_IMAGE_STATS |
| 1969 | gDrawImageCalls.fetch_add(1); |
| 1970 | #endif |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1971 | SkMatrix matrix = origMatrix; |
| 1972 | SkRegion perspectiveBounds; |
| 1973 | const SkRegion* clipRegion = &origClipRegion; |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1974 | sk_sp<SkImage> autoImageUnref; |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Rasterize the bitmap using perspective in a new bitmap. |
| 1977 | if (origMatrix.hasPerspective()) { |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 1978 | if (fRasterDpi == 0) { |
| 1979 | return; |
| 1980 | } |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 1981 | // Transform the bitmap in the new space, without taking into |
| 1982 | // account the initial transform. |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1983 | SkPath perspectiveOutline; |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1984 | SkRect imageBounds = SkRect::Make(imageBitmap.bounds()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1985 | perspectiveOutline.addRect(imageBounds); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 1986 | perspectiveOutline.transform(origMatrix); |
| 1987 | |
| 1988 | // TODO(edisonn): perf - use current clip too. |
| 1989 | // Retrieve the bounds of the new shape. |
| 1990 | SkRect bounds = perspectiveOutline.getBounds(); |
| 1991 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 1992 | // Transform the bitmap in the new space, taking into |
| 1993 | // account the initial transform. |
| 1994 | SkMatrix total = origMatrix; |
| 1995 | total.postConcat(fInitialTransform); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1996 | SkScalar dpiScale = SkIntToScalar(fRasterDpi) / |
| 1997 | SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE); |
| 1998 | total.postScale(dpiScale, dpiScale); |
| 1999 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2000 | SkPath physicalPerspectiveOutline; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2001 | physicalPerspectiveOutline.addRect(imageBounds); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2002 | physicalPerspectiveOutline.transform(total); |
| 2003 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2004 | SkRect physicalPerspectiveBounds = |
| 2005 | physicalPerspectiveOutline.getBounds(); |
| 2006 | SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width(); |
| 2007 | SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2008 | |
| 2009 | // TODO(edisonn): A better approach would be to use a bitmap shader |
| 2010 | // (in clamp mode) and draw a rect over the entire bounding box. Then |
| 2011 | // intersect perspectiveOutline to the clip. That will avoid introducing |
| 2012 | // alpha to the image while still giving good behavior at the edge of |
| 2013 | // the image. Avoiding alpha will reduce the pdf size and generation |
| 2014 | // CPU time some. |
| 2015 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2016 | SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil(); |
| 2017 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2018 | auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh))); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2019 | if (!surface) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 2020 | return; |
| 2021 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2022 | SkCanvas* canvas = surface->getCanvas(); |
| 2023 | canvas->clear(SK_ColorTRANSPARENT); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2024 | |
| 2025 | SkScalar deltaX = bounds.left(); |
| 2026 | SkScalar deltaY = bounds.top(); |
| 2027 | |
| 2028 | SkMatrix offsetMatrix = origMatrix; |
| 2029 | offsetMatrix.postTranslate(-deltaX, -deltaY); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2030 | offsetMatrix.postScale(scaleX, scaleY); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2031 | |
| 2032 | // Translate the draw in the new canvas, so we perfectly fit the |
| 2033 | // shape in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2034 | canvas->setMatrix(offsetMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2035 | imageBitmap.draw(canvas, nullptr); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2036 | // Make sure the final bits are in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2037 | canvas->flush(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2038 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2039 | // In the new space, we use the identity matrix translated |
| 2040 | // and scaled to reflect DPI. |
| 2041 | matrix.setScale(1 / scaleX, 1 / scaleY); |
| 2042 | matrix.postTranslate(deltaX, deltaY); |
| 2043 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2044 | perspectiveBounds.setRect(bounds.roundOut()); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2045 | clipRegion = &perspectiveBounds; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2046 | |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2047 | autoImageUnref = surface->makeImageSnapshot(); |
| 2048 | imageBitmap = SkImageBitmap(autoImageUnref.get()); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2051 | SkMatrix scaled; |
| 2052 | // Adjust for origin flip. |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 2053 | scaled.setScale(SK_Scalar1, -SK_Scalar1); |
| 2054 | scaled.postTranslate(0, SK_Scalar1); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2055 | // Scale the image up from 1x1 to WxH. |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2056 | SkIRect subset = imageBitmap.bounds(); |
| 2057 | scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()), |
| 2058 | SkIntToScalar(imageBitmap.dimensions().height())); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2059 | scaled.postConcat(matrix); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2060 | ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2061 | if (!content.entry()) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | } |
| 2064 | if (content.needShape()) { |
| 2065 | SkPath shape; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2066 | shape.addRect(SkRect::Make(subset)); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2067 | shape.transform(matrix); |
| 2068 | content.setShape(shape); |
| 2069 | } |
| 2070 | if (!content.needSource()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 2071 | return; |
| 2072 | } |
| 2073 | |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2074 | if (SkColorFilter* colorFilter = paint.getColorFilter()) { |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 2075 | // TODO(https://bug.skia.org/4378): implement colorfilter on other |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2076 | // draw calls. This code here works for all |
| 2077 | // drawBitmap*()/drawImage*() calls amd ImageFilters (which |
| 2078 | // rasterize a layer on this backend). Fortuanely, this seems |
| 2079 | // to be how Chromium impements most color-filters. |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2080 | autoImageUnref = color_filter(imageBitmap, colorFilter); |
| 2081 | imageBitmap = SkImageBitmap(autoImageUnref.get()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2082 | // TODO(halcanary): de-dupe this by caching filtered images. |
| 2083 | // (maybe in the resource cache?) |
| 2084 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2085 | |
| 2086 | SkBitmapKey key = imageBitmap.getKey(); |
| 2087 | sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2088 | if (!pdfimage) { |
halcanary | 4b1e17e | 2016-07-27 14:49:46 -0700 | [diff] [blame] | 2089 | sk_sp<SkImage> img = imageBitmap.makeImage(); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2090 | if (!img) { |
| 2091 | return; |
| 2092 | } |
| 2093 | pdfimage = SkPDFCreateBitmapObject( |
| 2094 | std::move(img), fDocument->canon()->getPixelSerializer()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2095 | if (!pdfimage) { |
| 2096 | return; |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2097 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2098 | fDocument->serialize(pdfimage); // serialize images early. |
| 2099 | fDocument->canon()->addPDFBitmap(key, pdfimage); |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2100 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2101 | // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject> |
halcanary | 3d8c33c | 2015-10-01 11:06:22 -0700 | [diff] [blame] | 2102 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 2103 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2104 | } |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2105 | |
| 2106 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2107 | |
| 2108 | #include "SkSpecialImage.h" |
| 2109 | #include "SkImageFilter.h" |
| 2110 | |
| 2111 | void SkPDFDevice::drawSpecial(const SkDraw& draw, SkSpecialImage* srcImg, int x, int y, |
| 2112 | const SkPaint& paint) { |
| 2113 | SkASSERT(!srcImg->isTextureBacked()); |
| 2114 | |
| 2115 | SkBitmap resultBM; |
| 2116 | |
| 2117 | SkImageFilter* filter = paint.getImageFilter(); |
| 2118 | if (filter) { |
| 2119 | SkIPoint offset = SkIPoint::Make(0, 0); |
| 2120 | SkMatrix matrix = *draw.fMatrix; |
| 2121 | matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y)); |
| 2122 | const SkIRect clipBounds = draw.fRC->getBounds().makeOffset(-x, -y); |
| 2123 | // SkAutoTUnref<SkImageFilterCache> cache(this->getImageFilterCache()); |
| 2124 | SkImageFilter::Context ctx(matrix, clipBounds, nullptr /*cache.get()*/); |
| 2125 | |
| 2126 | sk_sp<SkSpecialImage> resultImg(filter->filterImage(srcImg, ctx, &offset)); |
| 2127 | if (resultImg) { |
| 2128 | SkPaint tmpUnfiltered(paint); |
| 2129 | tmpUnfiltered.setImageFilter(nullptr); |
| 2130 | if (resultImg->getROPixels(&resultBM)) { |
| 2131 | this->drawSprite(draw, resultBM, x + offset.x(), y + offset.y(), tmpUnfiltered); |
| 2132 | } |
| 2133 | } |
| 2134 | } else { |
| 2135 | if (srcImg->getROPixels(&resultBM)) { |
| 2136 | this->drawSprite(draw, resultBM, x, y, paint); |
| 2137 | } |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkBitmap& bitmap) { |
| 2142 | return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap); |
| 2143 | } |
| 2144 | |
| 2145 | sk_sp<SkSpecialImage> SkPDFDevice::makeSpecial(const SkImage* image) { |
| 2146 | return SkSpecialImage::MakeFromImage(SkIRect::MakeWH(image->width(), image->height()), |
| 2147 | image->makeNonTextureImage()); |
| 2148 | } |
| 2149 | |
| 2150 | sk_sp<SkSpecialImage> SkPDFDevice::snapSpecial() { |
reed | e51c356 | 2016-07-19 14:33:20 -0700 | [diff] [blame] | 2151 | return nullptr; |
| 2152 | } |