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