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