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