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