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