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