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