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) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 710 | , fClipStack(nullptr) |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 711 | , fFontGlyphUsage(new SkPDFGlyphSetMap) |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 712 | , fRasterDpi(rasterDpi) |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 713 | , fDocument(doc) { |
halcanary | a1f1ee9 | 2015-02-20 06:17:26 -0800 | [diff] [blame] | 714 | SkASSERT(pageSize.width() > 0); |
| 715 | SkASSERT(pageSize.height() > 0); |
| 716 | fLegacyBitmap.setInfo( |
| 717 | SkImageInfo::MakeUnknown(pageSize.width(), pageSize.height())); |
| 718 | if (flip) { |
| 719 | // Skia generally uses the top left as the origin but PDF |
| 720 | // natively has the origin at the bottom left. This matrix |
| 721 | // corrects for that. But that only needs to be done once, we |
| 722 | // don't do it when layering. |
| 723 | fInitialTransform.setTranslate(0, SkIntToScalar(pageSize.fHeight)); |
| 724 | fInitialTransform.preScale(SK_Scalar1, -SK_Scalar1); |
| 725 | } else { |
| 726 | fInitialTransform.setIdentity(); |
| 727 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | SkPDFDevice::~SkPDFDevice() { |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 731 | this->cleanUp(true); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | void SkPDFDevice::init() { |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 735 | fContentEntries.reset(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 736 | fLastContentEntry = nullptr; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 737 | if (fFontGlyphUsage.get() == nullptr) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 738 | fFontGlyphUsage.reset(new SkPDFGlyphSetMap); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 739 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 740 | } |
| 741 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 742 | void SkPDFDevice::cleanUp(bool clearFontUsage) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 743 | fGraphicStateResources.unrefAll(); |
| 744 | fXObjectResources.unrefAll(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 745 | fFontResources.unrefAll(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 746 | fShaderResources.unrefAll(); |
reed@google.com | 2a006c1 | 2012-09-19 17:05:55 +0000 | [diff] [blame] | 747 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 748 | if (clearFontUsage) { |
| 749 | fFontGlyphUsage->reset(); |
| 750 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 751 | } |
| 752 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 753 | void SkPDFDevice::drawAnnotation(const SkDraw& d, const SkRect& rect, const char key[], |
| 754 | SkData* value) { |
| 755 | if (0 == rect.width() && 0 == rect.height()) { |
| 756 | handlePointAnnotation({ rect.x(), rect.y() }, *d.fMatrix, key, value); |
| 757 | } else { |
| 758 | SkPath path; |
| 759 | path.addRect(rect); |
| 760 | handlePathAnnotation(path, d, key, value); |
| 761 | } |
| 762 | } |
| 763 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 764 | void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 765 | SkPaint newPaint = paint; |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 766 | replace_srcmode_on_opaque_paint(&newPaint); |
| 767 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 768 | newPaint.setStyle(SkPaint::kFill_Style); |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 769 | ScopedContentEntry content(this, d, newPaint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 770 | internalDrawPaint(newPaint, content.entry()); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 771 | } |
| 772 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 773 | void SkPDFDevice::internalDrawPaint(const SkPaint& paint, |
| 774 | ContentEntry* contentEntry) { |
| 775 | if (!contentEntry) { |
| 776 | return; |
| 777 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 778 | SkRect bbox = SkRect::MakeWH(SkIntToScalar(this->width()), |
| 779 | SkIntToScalar(this->height())); |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 780 | SkMatrix inverse; |
commit-bot@chromium.org | d2cfa74 | 2013-09-20 18:58:30 +0000 | [diff] [blame] | 781 | if (!contentEntry->fState.fMatrix.invert(&inverse)) { |
vandebo@chromium.org | 386dfc0 | 2012-04-17 22:31:52 +0000 | [diff] [blame] | 782 | return; |
vandebo@chromium.org | b054990 | 2012-04-13 20:45:46 +0000 | [diff] [blame] | 783 | } |
vandebo@chromium.org | 77bcaa3 | 2011-04-15 20:57:37 +0000 | [diff] [blame] | 784 | inverse.mapRect(&bbox); |
| 785 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 786 | SkPDFUtils::AppendRectangle(bbox, &contentEntry->fContent); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 787 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 788 | &contentEntry->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 789 | } |
| 790 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 791 | void SkPDFDevice::drawPoints(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 792 | SkCanvas::PointMode mode, |
| 793 | size_t count, |
| 794 | const SkPoint* points, |
| 795 | const SkPaint& srcPaint) { |
| 796 | SkPaint passedPaint = srcPaint; |
| 797 | replace_srcmode_on_opaque_paint(&passedPaint); |
| 798 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 799 | if (count == 0) { |
| 800 | return; |
| 801 | } |
| 802 | |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 803 | // SkDraw::drawPoints converts to multiple calls to fDevice->drawPath. |
| 804 | // We only use this when there's a path effect because of the overhead |
| 805 | // of multiple calls to setUpContentEntry it causes. |
| 806 | if (passedPaint.getPathEffect()) { |
| 807 | if (d.fClip->isEmpty()) { |
| 808 | return; |
| 809 | } |
| 810 | SkDraw pointDraw(d); |
| 811 | pointDraw.fDevice = this; |
| 812 | pointDraw.drawPoints(mode, count, points, passedPaint, true); |
| 813 | return; |
| 814 | } |
| 815 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 816 | const SkPaint* paint = &passedPaint; |
| 817 | SkPaint modifiedPaint; |
| 818 | |
| 819 | if (mode == SkCanvas::kPoints_PointMode && |
| 820 | paint->getStrokeCap() != SkPaint::kRound_Cap) { |
| 821 | modifiedPaint = *paint; |
| 822 | paint = &modifiedPaint; |
| 823 | if (paint->getStrokeWidth()) { |
| 824 | // PDF won't draw a single point with square/butt caps because the |
| 825 | // orientation is ambiguous. Draw a rectangle instead. |
| 826 | modifiedPaint.setStyle(SkPaint::kFill_Style); |
| 827 | SkScalar strokeWidth = paint->getStrokeWidth(); |
| 828 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 829 | for (size_t i = 0; i < count; i++) { |
| 830 | SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, 0, 0); |
| 831 | r.inset(-halfStroke, -halfStroke); |
| 832 | drawRect(d, r, modifiedPaint); |
| 833 | } |
| 834 | return; |
| 835 | } else { |
| 836 | modifiedPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 837 | } |
| 838 | } |
| 839 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 840 | ScopedContentEntry content(this, d, *paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 841 | if (!content.entry()) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 842 | return; |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 843 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 844 | |
| 845 | switch (mode) { |
| 846 | case SkCanvas::kPolygon_PointMode: |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 847 | SkPDFUtils::MoveTo(points[0].fX, points[0].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 848 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 849 | for (size_t i = 1; i < count; i++) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 850 | SkPDFUtils::AppendLine(points[i].fX, points[i].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 851 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 852 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 853 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 854 | break; |
| 855 | case SkCanvas::kLines_PointMode: |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 856 | for (size_t i = 0; i < count/2; i++) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 857 | SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 858 | &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 859 | SkPDFUtils::AppendLine(points[i * 2 + 1].fX, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 860 | points[i * 2 + 1].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 861 | &content.entry()->fContent); |
| 862 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 863 | } |
| 864 | break; |
| 865 | case SkCanvas::kPoints_PointMode: |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 866 | SkASSERT(paint->getStrokeCap() == SkPaint::kRound_Cap); |
| 867 | for (size_t i = 0; i < count; i++) { |
| 868 | SkPDFUtils::MoveTo(points[i].fX, points[i].fY, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 869 | &content.entry()->fContent); |
| 870 | SkPDFUtils::ClosePath(&content.entry()->fContent); |
| 871 | SkPDFUtils::StrokePath(&content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 872 | } |
| 873 | break; |
| 874 | default: |
| 875 | SkASSERT(false); |
| 876 | } |
| 877 | } |
| 878 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 879 | static sk_sp<SkPDFDict> create_link_annotation(const SkRect& translatedRect) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 880 | auto annotation = sk_make_sp<SkPDFDict>("Annot"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 881 | annotation->insertName("Subtype", "Link"); |
| 882 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 883 | auto border = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 884 | border->reserve(3); |
| 885 | border->appendInt(0); // Horizontal corner radius. |
| 886 | border->appendInt(0); // Vertical corner radius. |
| 887 | border->appendInt(0); // Width, 0 = no border. |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 888 | annotation->insertObject("Border", std::move(border)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 889 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 890 | auto rect = sk_make_sp<SkPDFArray>(); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 891 | rect->reserve(4); |
| 892 | rect->appendScalar(translatedRect.fLeft); |
| 893 | rect->appendScalar(translatedRect.fTop); |
| 894 | rect->appendScalar(translatedRect.fRight); |
| 895 | rect->appendScalar(translatedRect.fBottom); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 896 | annotation->insertObject("Rect", std::move(rect)); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 897 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 898 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 899 | } |
| 900 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 901 | static sk_sp<SkPDFDict> create_link_to_url(const SkData* urlData, const SkRect& r) { |
| 902 | auto annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 903 | SkString url(static_cast<const char *>(urlData->data()), |
| 904 | urlData->size() - 1); |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 905 | auto action = sk_make_sp<SkPDFDict>("Action"); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 906 | action->insertName("S", "URI"); |
| 907 | action->insertString("URI", url); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 908 | annotation->insertObject("A", std::move(action)); |
| 909 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 910 | } |
| 911 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 912 | static sk_sp<SkPDFDict> create_link_named_dest(const SkData* nameData, |
| 913 | const SkRect& r) { |
| 914 | auto annotation = create_link_annotation(r); |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 915 | SkString name(static_cast<const char *>(nameData->data()), |
| 916 | nameData->size() - 1); |
| 917 | annotation->insertName("Dest", name); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 918 | return annotation; |
wangxianzhu | d76665d | 2015-07-17 17:23:15 -0700 | [diff] [blame] | 919 | } |
| 920 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 921 | void SkPDFDevice::drawRect(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 922 | const SkRect& rect, |
| 923 | const SkPaint& srcPaint) { |
| 924 | SkPaint paint = srcPaint; |
| 925 | replace_srcmode_on_opaque_paint(&paint); |
commit-bot@chromium.org | 969fd6a | 2013-05-14 18:16:40 +0000 | [diff] [blame] | 926 | SkRect r = rect; |
| 927 | r.sort(); |
| 928 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 929 | if (paint.getPathEffect()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 930 | if (d.fClip->isEmpty()) { |
| 931 | return; |
| 932 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 933 | SkPath path; |
| 934 | path.addRect(r); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 935 | drawPath(d, path, paint, nullptr, true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 936 | return; |
| 937 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 938 | |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 939 | ScopedContentEntry content(this, d, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 940 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 941 | return; |
| 942 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 943 | SkPDFUtils::AppendRectangle(r, &content.entry()->fContent); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 944 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 945 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 946 | } |
| 947 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 948 | void SkPDFDevice::drawRRect(const SkDraw& draw, |
| 949 | const SkRRect& rrect, |
| 950 | const SkPaint& srcPaint) { |
| 951 | SkPaint paint = srcPaint; |
| 952 | replace_srcmode_on_opaque_paint(&paint); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 953 | SkPath path; |
| 954 | path.addRRect(rrect); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 955 | this->drawPath(draw, path, paint, nullptr, true); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 956 | } |
| 957 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 958 | void SkPDFDevice::drawOval(const SkDraw& draw, |
| 959 | const SkRect& oval, |
| 960 | const SkPaint& srcPaint) { |
| 961 | SkPaint paint = srcPaint; |
| 962 | replace_srcmode_on_opaque_paint(&paint); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 963 | SkPath path; |
| 964 | path.addOval(oval); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 965 | this->drawPath(draw, path, paint, nullptr, true); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 966 | } |
| 967 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 968 | void SkPDFDevice::drawPath(const SkDraw& d, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 969 | const SkPath& origPath, |
| 970 | const SkPaint& srcPaint, |
| 971 | const SkMatrix* prePathMatrix, |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 972 | bool pathIsMutable) { |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 973 | SkPaint paint = srcPaint; |
| 974 | replace_srcmode_on_opaque_paint(&paint); |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 975 | SkPath modifiedPath; |
| 976 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 977 | |
| 978 | SkMatrix matrix = *d.fMatrix; |
| 979 | if (prePathMatrix) { |
| 980 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 981 | if (!pathIsMutable) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 982 | pathPtr = &modifiedPath; |
| 983 | pathIsMutable = true; |
| 984 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 985 | origPath.transform(*prePathMatrix, pathPtr); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 986 | } else { |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 987 | matrix.preConcat(*prePathMatrix); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 988 | } |
| 989 | } |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 990 | |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 991 | if (paint.getPathEffect()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 992 | if (d.fClip->isEmpty()) { |
| 993 | return; |
| 994 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 995 | if (!pathIsMutable) { |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 996 | pathPtr = &modifiedPath; |
| 997 | pathIsMutable = true; |
| 998 | } |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 999 | bool fill = paint.getFillPath(origPath, pathPtr); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 1000 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 1001 | SkPaint noEffectPaint(paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1002 | noEffectPaint.setPathEffect(nullptr); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 1003 | if (fill) { |
| 1004 | noEffectPaint.setStyle(SkPaint::kFill_Style); |
| 1005 | } else { |
| 1006 | noEffectPaint.setStyle(SkPaint::kStroke_Style); |
| 1007 | noEffectPaint.setStrokeWidth(0); |
| 1008 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1009 | drawPath(d, *pathPtr, noEffectPaint, nullptr, true); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 1010 | return; |
| 1011 | } |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 1012 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1013 | if (handleInversePath(d, origPath, paint, pathIsMutable, prePathMatrix)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1014 | return; |
| 1015 | } |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1016 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1017 | ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1018 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 1021 | bool consumeDegeratePathSegments = |
| 1022 | paint.getStyle() == SkPaint::kFill_Style || |
| 1023 | (paint.getStrokeCap() != SkPaint::kRound_Cap && |
| 1024 | paint.getStrokeCap() != SkPaint::kSquare_Cap); |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 1025 | SkPDFUtils::EmitPath(*pathPtr, paint.getStyle(), |
halcanary | 8b2bc25 | 2015-10-06 09:41:47 -0700 | [diff] [blame] | 1026 | consumeDegeratePathSegments, |
vandebo@chromium.org | 683001c | 2012-05-09 17:17:51 +0000 | [diff] [blame] | 1027 | &content.entry()->fContent); |
vandebo@chromium.org | ff39032 | 2011-05-17 18:58:44 +0000 | [diff] [blame] | 1028 | SkPDFUtils::PaintPath(paint.getStyle(), pathPtr->getFillType(), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1029 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1032 | void SkPDFDevice::drawBitmapRect(const SkDraw& draw, |
| 1033 | const SkBitmap& bitmap, |
| 1034 | const SkRect* src, |
| 1035 | const SkRect& dst, |
| 1036 | const SkPaint& srcPaint, |
| 1037 | SkCanvas::SrcRectConstraint constraint) { |
halcanary | 66be626 | 2016-03-21 13:01:34 -0700 | [diff] [blame] | 1038 | SkASSERT(false); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | void SkPDFDevice::drawBitmap(const SkDraw& d, |
| 1042 | const SkBitmap& bitmap, |
| 1043 | const SkMatrix& matrix, |
| 1044 | const SkPaint& srcPaint) { |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 1045 | SkPaint paint = srcPaint; |
| 1046 | if (bitmap.isOpaque()) { |
| 1047 | replace_srcmode_on_opaque_paint(&paint); |
| 1048 | } |
| 1049 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1050 | if (d.fClip->isEmpty()) { |
| 1051 | return; |
| 1052 | } |
edisonn@google.com | 2ae67e7 | 2013-02-12 01:06:38 +0000 | [diff] [blame] | 1053 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1054 | SkMatrix transform = matrix; |
| 1055 | transform.postConcat(*d.fMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1056 | SkImageBitmap imageBitmap(bitmap); |
| 1057 | this->internalDrawImage( |
| 1058 | transform, d.fClipStack, *d.fClip, imageBitmap, paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void SkPDFDevice::drawSprite(const SkDraw& d, |
| 1062 | const SkBitmap& bitmap, |
| 1063 | int x, |
| 1064 | int y, |
| 1065 | const SkPaint& srcPaint) { |
| 1066 | SkPaint paint = srcPaint; |
| 1067 | if (bitmap.isOpaque()) { |
| 1068 | replace_srcmode_on_opaque_paint(&paint); |
| 1069 | } |
| 1070 | |
| 1071 | if (d.fClip->isEmpty()) { |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | SkMatrix matrix; |
| 1076 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1077 | SkImageBitmap imageBitmap(bitmap); |
| 1078 | this->internalDrawImage( |
| 1079 | matrix, d.fClipStack, *d.fClip, imageBitmap, paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | void SkPDFDevice::drawImage(const SkDraw& draw, |
| 1083 | const SkImage* image, |
| 1084 | SkScalar x, |
| 1085 | SkScalar y, |
| 1086 | const SkPaint& srcPaint) { |
| 1087 | SkPaint paint = srcPaint; |
| 1088 | if (!image) { |
| 1089 | return; |
| 1090 | } |
| 1091 | if (image->isOpaque()) { |
| 1092 | replace_srcmode_on_opaque_paint(&paint); |
| 1093 | } |
| 1094 | if (draw.fClip->isEmpty()) { |
| 1095 | return; |
| 1096 | } |
| 1097 | SkMatrix transform = SkMatrix::MakeTrans(x, y); |
| 1098 | transform.postConcat(*draw.fMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 1099 | SkImageBitmap imageBitmap(const_cast<SkImage*>(image)); |
| 1100 | this->internalDrawImage( |
| 1101 | transform, draw.fClipStack, *draw.fClip, imageBitmap, paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | void SkPDFDevice::drawImageRect(const SkDraw& draw, |
| 1105 | const SkImage* image, |
| 1106 | const SkRect* src, |
| 1107 | const SkRect& dst, |
| 1108 | const SkPaint& srcPaint, |
| 1109 | SkCanvas::SrcRectConstraint constraint) { |
halcanary | 66be626 | 2016-03-21 13:01:34 -0700 | [diff] [blame] | 1110 | SkASSERT(false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 1113 | // Create a PDF string. Maximum length (in bytes) is 65,535. |
| 1114 | // @param input A string value. |
| 1115 | // @param len The length of the input array. |
| 1116 | // @param wideChars True iff the upper byte in each uint16_t is |
| 1117 | // significant and should be encoded and not |
| 1118 | // discarded. If true, the upper byte is encoded |
| 1119 | // first. Otherwise, we assert the upper byte is |
| 1120 | // zero. |
| 1121 | static SkString format_wide_string(const uint16_t* input, |
| 1122 | size_t len, |
| 1123 | bool wideChars) { |
| 1124 | if (wideChars) { |
| 1125 | SkASSERT(2 * len < 65535); |
| 1126 | static const char gHex[] = "0123456789ABCDEF"; |
| 1127 | SkString result(4 * len + 2); |
| 1128 | result[0] = '<'; |
| 1129 | for (size_t i = 0; i < len; i++) { |
| 1130 | result[4 * i + 1] = gHex[(input[i] >> 12) & 0xF]; |
| 1131 | result[4 * i + 2] = gHex[(input[i] >> 8) & 0xF]; |
| 1132 | result[4 * i + 3] = gHex[(input[i] >> 4) & 0xF]; |
| 1133 | result[4 * i + 4] = gHex[(input[i] ) & 0xF]; |
| 1134 | } |
| 1135 | result[4 * len + 1] = '>'; |
| 1136 | return result; |
| 1137 | } else { |
| 1138 | SkASSERT(len <= 65535); |
| 1139 | SkString tmp(len); |
| 1140 | for (size_t i = 0; i < len; i++) { |
| 1141 | SkASSERT(0 == input[i] >> 8); |
| 1142 | tmp[i] = static_cast<uint8_t>(input[i]); |
| 1143 | } |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 1144 | return SkPDFUtils::FormatString(tmp.c_str(), tmp.size()); |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1148 | static void draw_transparent_text(SkPDFDevice* device, |
| 1149 | const SkDraw& d, |
| 1150 | const void* text, size_t len, |
| 1151 | SkScalar x, SkScalar y, |
| 1152 | const SkPaint& srcPaint) { |
| 1153 | |
| 1154 | SkPaint transparent; |
| 1155 | if (!SkPDFFont::CanEmbedTypeface(transparent.getTypeface(), |
| 1156 | device->getCanon())) { |
| 1157 | SkDEBUGFAIL("default typeface should be embeddable"); |
| 1158 | return; // Avoid infinite loop in release. |
| 1159 | } |
| 1160 | transparent.setTextSize(srcPaint.getTextSize()); |
| 1161 | transparent.setColor(SK_ColorTRANSPARENT); |
| 1162 | switch (srcPaint.getTextEncoding()) { |
| 1163 | case SkPaint::kGlyphID_TextEncoding: { |
| 1164 | // Since a glyphId<->Unicode mapping is typeface-specific, |
| 1165 | // map back to Unicode first. |
| 1166 | size_t glyphCount = len / 2; |
| 1167 | SkAutoTMalloc<SkUnichar> unichars(glyphCount); |
| 1168 | srcPaint.glyphsToUnichars( |
| 1169 | (const uint16_t*)text, SkToInt(glyphCount), &unichars[0]); |
| 1170 | transparent.setTextEncoding(SkPaint::kUTF32_TextEncoding); |
| 1171 | device->drawText(d, &unichars[0], |
| 1172 | glyphCount * sizeof(SkUnichar), |
| 1173 | x, y, transparent); |
| 1174 | break; |
| 1175 | } |
| 1176 | case SkPaint::kUTF8_TextEncoding: |
| 1177 | case SkPaint::kUTF16_TextEncoding: |
| 1178 | case SkPaint::kUTF32_TextEncoding: |
| 1179 | transparent.setTextEncoding(srcPaint.getTextEncoding()); |
| 1180 | device->drawText(d, text, len, x, y, transparent); |
| 1181 | break; |
| 1182 | default: |
| 1183 | SkFAIL("unknown text encoding"); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 1188 | void SkPDFDevice::drawText(const SkDraw& d, const void* text, size_t len, |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 1189 | SkScalar x, SkScalar y, const SkPaint& srcPaint) { |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1190 | if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) { |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 1191 | // https://bug.skia.org/3866 |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1192 | SkPath path; |
| 1193 | srcPaint.getTextPath(text, len, x, y, &path); |
| 1194 | this->drawPath(d, path, srcPaint, &SkMatrix::I(), true); |
| 1195 | // Draw text transparently to make it copyable/searchable/accessable. |
| 1196 | draw_transparent_text(this, d, text, len, x, y, srcPaint); |
| 1197 | return; |
| 1198 | } |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 1199 | SkPaint paint = srcPaint; |
| 1200 | replace_srcmode_on_opaque_paint(&paint); |
| 1201 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1202 | NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false); |
| 1203 | if (paint.getMaskFilter() != nullptr) { |
edisonn@google.com | b62f93f | 2013-03-24 18:05:10 +0000 | [diff] [blame] | 1204 | // Don't pretend we support drawing MaskFilters, it makes for artifacts |
| 1205 | // making text unreadable (e.g. same text twice when using CSS shadows). |
| 1206 | return; |
| 1207 | } |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1208 | SkPaint textPaint = calculate_text_paint(paint); |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 1209 | ScopedContentEntry content(this, d, textPaint, true); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1210 | if (!content.entry()) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1211 | return; |
| 1212 | } |
| 1213 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 1214 | SkGlyphStorage storage(0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1215 | const uint16_t* glyphIDs = nullptr; |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 1216 | int numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 1217 | textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1218 | |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 1219 | SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true); |
bungeman@google.com | 9a87cee | 2011-08-23 17:02:18 +0000 | [diff] [blame] | 1220 | align_text(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1221 | content.entry()->fContent.writeText("BT\n"); |
| 1222 | set_text_transform(x, y, textPaint.getTextSkewX(), |
| 1223 | &content.entry()->fContent); |
reed@google.com | aec4066 | 2014-04-18 19:29:07 +0000 | [diff] [blame] | 1224 | int consumedGlyphCount = 0; |
halcanary | 2f912f3 | 2014-10-16 09:53:20 -0700 | [diff] [blame] | 1225 | |
| 1226 | SkTDArray<uint16_t> glyphIDsCopy(glyphIDs, numGlyphs); |
| 1227 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1228 | while (numGlyphs > consumedGlyphCount) { |
robertphillips | 8e0c150 | 2015-07-07 10:28:43 -0700 | [diff] [blame] | 1229 | this->updateFont(textPaint, glyphIDs[consumedGlyphCount], content.entry()); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1230 | SkPDFFont* font = content.entry()->fState.fFont; |
halcanary | 2f912f3 | 2014-10-16 09:53:20 -0700 | [diff] [blame] | 1231 | |
| 1232 | int availableGlyphs = font->glyphsToPDFFontEncoding( |
| 1233 | glyphIDsCopy.begin() + consumedGlyphCount, |
| 1234 | numGlyphs - consumedGlyphCount); |
| 1235 | fFontGlyphUsage->noteGlyphUsage( |
| 1236 | font, glyphIDsCopy.begin() + consumedGlyphCount, |
| 1237 | availableGlyphs); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 1238 | SkString encodedString = |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 1239 | format_wide_string(glyphIDsCopy.begin() + consumedGlyphCount, |
| 1240 | availableGlyphs, font->multiByteGlyphs()); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1241 | content.entry()->fContent.writeText(encodedString.c_str()); |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 1242 | consumedGlyphCount += availableGlyphs; |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1243 | content.entry()->fContent.writeText(" Tj\n"); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1244 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1245 | content.entry()->fContent.writeText("ET\n"); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 1248 | void SkPDFDevice::drawPosText(const SkDraw& d, const void* text, size_t len, |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 1249 | const SkScalar pos[], int scalarsPerPos, |
halcanary | 682ee01 | 2016-01-28 10:59:34 -0800 | [diff] [blame] | 1250 | const SkPoint& offset, const SkPaint& srcPaint) { |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1251 | if (!SkPDFFont::CanEmbedTypeface(srcPaint.getTypeface(), fDocument->canon())) { |
halcanary | 66a82f3 | 2015-10-12 13:05:04 -0700 | [diff] [blame] | 1252 | const SkPoint* positions = reinterpret_cast<const SkPoint*>(pos); |
| 1253 | SkAutoTMalloc<SkPoint> positionsBuffer; |
| 1254 | if (2 != scalarsPerPos) { |
| 1255 | int glyphCount = srcPaint.textToGlyphs(text, len, NULL); |
| 1256 | positionsBuffer.reset(glyphCount); |
| 1257 | for (int i = 0; i < glyphCount; ++i) { |
| 1258 | positionsBuffer[i].set(pos[i], 0.0f); |
| 1259 | } |
| 1260 | positions = &positionsBuffer[0]; |
| 1261 | } |
| 1262 | SkPath path; |
| 1263 | srcPaint.getPosTextPath(text, len, positions, &path); |
| 1264 | SkMatrix matrix; |
| 1265 | matrix.setTranslate(offset); |
| 1266 | this->drawPath(d, path, srcPaint, &matrix, true); |
| 1267 | // Draw text transparently to make it copyable/searchable/accessable. |
| 1268 | draw_transparent_text( |
| 1269 | this, d, text, len, offset.x() + positions[0].x(), |
| 1270 | offset.y() + positions[0].y(), srcPaint); |
| 1271 | return; |
| 1272 | } |
| 1273 | |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 1274 | SkPaint paint = srcPaint; |
| 1275 | replace_srcmode_on_opaque_paint(&paint); |
| 1276 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1277 | NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false); |
| 1278 | if (paint.getMaskFilter() != nullptr) { |
edisonn@google.com | b62f93f | 2013-03-24 18:05:10 +0000 | [diff] [blame] | 1279 | // Don't pretend we support drawing MaskFilters, it makes for artifacts |
| 1280 | // making text unreadable (e.g. same text twice when using CSS shadows). |
| 1281 | return; |
| 1282 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1283 | SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1284 | SkPaint textPaint = calculate_text_paint(paint); |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 1285 | ScopedContentEntry content(this, d, textPaint, true); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1286 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1287 | return; |
| 1288 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1289 | |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 1290 | SkGlyphStorage storage(0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1291 | const uint16_t* glyphIDs = nullptr; |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 1292 | size_t numGlyphs = force_glyph_encoding(paint, text, len, &storage, &glyphIDs); |
vandebo@chromium.org | 4e1cc6a | 2013-01-25 19:27:23 +0000 | [diff] [blame] | 1293 | textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1294 | |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 1295 | SkPaint::GlyphCacheProc glyphCacheProc = textPaint.getGlyphCacheProc(true); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1296 | content.entry()->fContent.writeText("BT\n"); |
robertphillips | 8e0c150 | 2015-07-07 10:28:43 -0700 | [diff] [blame] | 1297 | this->updateFont(textPaint, glyphIDs[0], content.entry()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1298 | for (size_t i = 0; i < numGlyphs; i++) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1299 | SkPDFFont* font = content.entry()->fState.fFont; |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 1300 | uint16_t encodedValue = glyphIDs[i]; |
| 1301 | if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) { |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 1302 | // The current pdf font cannot encode the current glyph. |
| 1303 | // Try to get a pdf font which can encode the current glyph. |
robertphillips | 8e0c150 | 2015-07-07 10:28:43 -0700 | [diff] [blame] | 1304 | this->updateFont(textPaint, glyphIDs[i], content.entry()); |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 1305 | font = content.entry()->fState.fFont; |
| 1306 | if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) { |
| 1307 | SkDEBUGFAIL("PDF could not encode glyph."); |
| 1308 | continue; |
| 1309 | } |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 1310 | } |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 1311 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1312 | fFontGlyphUsage->noteGlyphUsage(font, &encodedValue, 1); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 1313 | SkScalar x = offset.x() + pos[i * scalarsPerPos]; |
| 1314 | SkScalar y = offset.y() + (2 == scalarsPerPos ? pos[i * scalarsPerPos + 1] : 0); |
| 1315 | |
bungeman@google.com | 9a87cee | 2011-08-23 17:02:18 +0000 | [diff] [blame] | 1316 | align_text(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y); |
bungeman | 22edc83 | 2014-10-03 07:55:58 -0700 | [diff] [blame] | 1317 | set_text_transform(x, y, textPaint.getTextSkewX(), &content.entry()->fContent); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 1318 | SkString encodedString = |
halcanary | bb264b7 | 2015-04-07 10:40:03 -0700 | [diff] [blame] | 1319 | format_wide_string(&encodedValue, 1, font->multiByteGlyphs()); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1320 | content.entry()->fContent.writeText(encodedString.c_str()); |
| 1321 | content.entry()->fContent.writeText(" Tj\n"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 1322 | } |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1323 | content.entry()->fContent.writeText("ET\n"); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1326 | void SkPDFDevice::drawVertices(const SkDraw& d, SkCanvas::VertexMode, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1327 | int vertexCount, const SkPoint verts[], |
| 1328 | const SkPoint texs[], const SkColor colors[], |
| 1329 | SkXfermode* xmode, const uint16_t indices[], |
| 1330 | int indexCount, const SkPaint& paint) { |
vandebo@chromium.org | fb0b0ed | 2011-04-15 20:01:17 +0000 | [diff] [blame] | 1331 | if (d.fClip->isEmpty()) { |
| 1332 | return; |
| 1333 | } |
reed@google.com | 85e143c | 2013-12-30 15:51:25 +0000 | [diff] [blame] | 1334 | // TODO: implement drawVertices |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1337 | void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device, |
| 1338 | int x, int y, const SkPaint& paint) { |
fmalita | 6987dca | 2014-11-13 08:33:37 -0800 | [diff] [blame] | 1339 | // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses. |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1340 | SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1341 | |
| 1342 | SkScalar scalarX = SkIntToScalar(x); |
| 1343 | SkScalar scalarY = SkIntToScalar(y); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1344 | for (const RectWithData& l : pdfDevice->fLinkToURLs) { |
| 1345 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1346 | fLinkToURLs.emplace_back(r, l.data.get()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1347 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1348 | for (const RectWithData& l : pdfDevice->fLinkToDestinations) { |
| 1349 | SkRect r = l.rect.makeOffset(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1350 | fLinkToDestinations.emplace_back(r, l.data.get()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1351 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1352 | for (const NamedDestination& d : pdfDevice->fNamedDestinations) { |
| 1353 | SkPoint p = d.point + SkPoint::Make(scalarX, scalarY); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1354 | fNamedDestinations.emplace_back(d.nameData.get(), p); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
ctguil@chromium.org | f4ff39c | 2011-05-24 19:55:05 +0000 | [diff] [blame] | 1357 | if (pdfDevice->isContentEmpty()) { |
vandebo@chromium.org | ee7a956 | 2011-05-24 17:38:01 +0000 | [diff] [blame] | 1358 | return; |
| 1359 | } |
| 1360 | |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 1361 | SkMatrix matrix; |
reed@google.com | a6d59f6 | 2011-03-07 21:29:21 +0000 | [diff] [blame] | 1362 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
vandebo@chromium.org | 13d14a9 | 2011-05-24 23:12:41 +0000 | [diff] [blame] | 1363 | ScopedContentEntry content(this, d.fClipStack, *d.fClip, matrix, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1364 | if (!content.entry()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1365 | return; |
| 1366 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1367 | if (content.needShape()) { |
| 1368 | SkPath shape; |
| 1369 | shape.addRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
vandebo@chromium.org | fd3c8c2 | 2013-10-30 21:00:47 +0000 | [diff] [blame] | 1370 | SkIntToScalar(device->width()), |
| 1371 | SkIntToScalar(device->height()))); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1372 | content.setShape(shape); |
| 1373 | } |
| 1374 | if (!content.needSource()) { |
| 1375 | return; |
| 1376 | } |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 1377 | |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1378 | auto xObject = sk_make_sp<SkPDFFormXObject>(pdfDevice); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1379 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(xObject.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1380 | &content.entry()->fContent); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1381 | |
| 1382 | // Merge glyph sets from the drawn device. |
| 1383 | fFontGlyphUsage->merge(pdfDevice->getFontGlyphUsage()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 1386 | SkImageInfo SkPDFDevice::imageInfo() const { |
| 1387 | return fLegacyBitmap.info(); |
| 1388 | } |
| 1389 | |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 1390 | void SkPDFDevice::onAttachToCanvas(SkCanvas* canvas) { |
| 1391 | INHERITED::onAttachToCanvas(canvas); |
| 1392 | |
| 1393 | // Canvas promises that this ptr is valid until onDetachFromCanvas is called |
| 1394 | fClipStack = canvas->getClipStack(); |
| 1395 | } |
| 1396 | |
| 1397 | void SkPDFDevice::onDetachFromCanvas() { |
| 1398 | INHERITED::onDetachFromCanvas(); |
| 1399 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1400 | fClipStack = nullptr; |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1403 | sk_sp<SkSurface> SkPDFDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
| 1404 | return SkSurface::MakeRaster(info, &props); |
reed | 89443ab | 2014-06-27 11:34:19 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1407 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1408 | sk_sp<SkPDFDict> SkPDFDevice::makeResourceDict() const { |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1409 | SkTDArray<SkPDFObject*> fonts; |
| 1410 | fonts.setReserve(fFontResources.count()); |
| 1411 | for (SkPDFFont* font : fFontResources) { |
| 1412 | fonts.push(font); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1413 | } |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1414 | return SkPDFResourceDict::Make( |
halcanary | 2b86155 | 2015-04-09 13:27:40 -0700 | [diff] [blame] | 1415 | &fGraphicStateResources, |
| 1416 | &fShaderResources, |
| 1417 | &fXObjectResources, |
| 1418 | &fonts); |
vandebo@chromium.org | fc16667 | 2013-07-22 18:31:24 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
vandebo@chromium.org | f0ec266 | 2011-05-29 05:55:42 +0000 | [diff] [blame] | 1421 | const SkTDArray<SkPDFFont*>& SkPDFDevice::getFontResources() const { |
| 1422 | return fFontResources; |
| 1423 | } |
| 1424 | |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1425 | sk_sp<SkPDFArray> SkPDFDevice::copyMediaBox() const { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1426 | auto mediaBox = sk_make_sp<SkPDFArray>(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1427 | mediaBox->reserve(4); |
halcanary | 130444f | 2015-04-25 06:45:07 -0700 | [diff] [blame] | 1428 | mediaBox->appendInt(0); |
| 1429 | mediaBox->appendInt(0); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1430 | mediaBox->appendInt(fPageSize.width()); |
| 1431 | mediaBox->appendInt(fPageSize.height()); |
| 1432 | return mediaBox; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1435 | std::unique_ptr<SkStreamAsset> SkPDFDevice::content() const { |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1436 | SkDynamicMemoryWStream buffer; |
| 1437 | this->writeContent(&buffer); |
mtklein | 5f939ab | 2016-03-16 10:28:35 -0700 | [diff] [blame] | 1438 | return std::unique_ptr<SkStreamAsset>( |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1439 | buffer.bytesWritten() > 0 |
| 1440 | ? buffer.detachAsStream() |
| 1441 | : new SkMemoryStream); |
reed@google.com | 5667afc | 2011-06-27 14:42:15 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1444 | void SkPDFDevice::copyContentEntriesToData(ContentEntry* entry, |
| 1445 | SkWStream* data) const { |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1446 | // TODO(ctguil): For margins, I'm not sure fExistingClipStack/Region is the |
| 1447 | // right thing to pass here. |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1448 | GraphicStackState gsState(fExistingClipStack, fExistingClipRegion, data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1449 | while (entry != nullptr) { |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 1450 | SkPoint translation; |
| 1451 | translation.iset(this->getOrigin()); |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1452 | translation.negate(); |
| 1453 | gsState.updateClip(entry->fState.fClipStack, entry->fState.fClipRegion, |
| 1454 | translation); |
| 1455 | gsState.updateMatrix(entry->fState.fMatrix); |
| 1456 | gsState.updateDrawingState(entry->fState); |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1457 | |
halcanary | 7af2150 | 2015-02-23 12:17:59 -0800 | [diff] [blame] | 1458 | entry->fContent.writeToStream(data); |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1459 | entry = entry->fNext.get(); |
| 1460 | } |
| 1461 | gsState.drainStack(); |
| 1462 | } |
| 1463 | |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1464 | void SkPDFDevice::writeContent(SkWStream* out) const { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1465 | if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) { |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1466 | SkPDFUtils::AppendTransform(fInitialTransform, out); |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 1467 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1468 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1469 | // If the content area is the entire page, then we don't need to clip |
| 1470 | // the content area (PDF area clips to the page size). Otherwise, |
| 1471 | // we have to clip to the content area; we've already applied the |
| 1472 | // initial transform, so just clip to the device size. |
| 1473 | if (fPageSize != fContentSize) { |
robertphillips@google.com | 8637a36 | 2012-04-10 18:32:35 +0000 | [diff] [blame] | 1474 | SkRect r = SkRect::MakeWH(SkIntToScalar(this->width()), |
| 1475 | SkIntToScalar(this->height())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1476 | emit_clip(nullptr, &r, out); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1477 | } |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1478 | |
halcanary | 334fcbc | 2015-02-24 12:56:16 -0800 | [diff] [blame] | 1479 | SkPDFDevice::copyContentEntriesToData(fContentEntries.get(), out); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1482 | /* Draws an inverse filled path by using Path Ops to compute the positive |
| 1483 | * inverse using the current clip as the inverse bounds. |
| 1484 | * Return true if this was an inverse path and was properly handled, |
| 1485 | * otherwise returns false and the normal drawing routine should continue, |
| 1486 | * either as a (incorrect) fallback or because the path was not inverse |
| 1487 | * in the first place. |
| 1488 | */ |
| 1489 | bool SkPDFDevice::handleInversePath(const SkDraw& d, const SkPath& origPath, |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1490 | const SkPaint& paint, bool pathIsMutable, |
| 1491 | const SkMatrix* prePathMatrix) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1492 | if (!origPath.isInverseFillType()) { |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
| 1496 | if (d.fClip->isEmpty()) { |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | SkPath modifiedPath; |
| 1501 | SkPath* pathPtr = const_cast<SkPath*>(&origPath); |
| 1502 | SkPaint noInversePaint(paint); |
| 1503 | |
| 1504 | // Merge stroking operations into final path. |
| 1505 | if (SkPaint::kStroke_Style == paint.getStyle() || |
| 1506 | SkPaint::kStrokeAndFill_Style == paint.getStyle()) { |
| 1507 | bool doFillPath = paint.getFillPath(origPath, &modifiedPath); |
| 1508 | if (doFillPath) { |
| 1509 | noInversePaint.setStyle(SkPaint::kFill_Style); |
| 1510 | noInversePaint.setStrokeWidth(0); |
| 1511 | pathPtr = &modifiedPath; |
| 1512 | } else { |
| 1513 | // To be consistent with the raster output, hairline strokes |
| 1514 | // are rendered as non-inverted. |
| 1515 | modifiedPath.toggleInverseFillType(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1516 | drawPath(d, modifiedPath, paint, nullptr, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1517 | return true; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | // Get bounds of clip in current transform space |
| 1522 | // (clip bounds are given in device space). |
| 1523 | SkRect bounds; |
| 1524 | SkMatrix transformInverse; |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1525 | SkMatrix totalMatrix = *d.fMatrix; |
| 1526 | if (prePathMatrix) { |
| 1527 | totalMatrix.preConcat(*prePathMatrix); |
| 1528 | } |
| 1529 | if (!totalMatrix.invert(&transformInverse)) { |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1530 | return false; |
| 1531 | } |
| 1532 | bounds.set(d.fClip->getBounds()); |
| 1533 | transformInverse.mapRect(&bounds); |
| 1534 | |
| 1535 | // Extend the bounds by the line width (plus some padding) |
| 1536 | // so the edge doesn't cause a visible stroke. |
| 1537 | bounds.outset(paint.getStrokeWidth() + SK_Scalar1, |
| 1538 | paint.getStrokeWidth() + SK_Scalar1); |
| 1539 | |
| 1540 | if (!calculate_inverse_path(bounds, *pathPtr, &modifiedPath)) { |
| 1541 | return false; |
| 1542 | } |
| 1543 | |
edisonn@google.com | a9ebd16 | 2013-10-07 13:22:21 +0000 | [diff] [blame] | 1544 | drawPath(d, modifiedPath, noInversePaint, prePathMatrix, true); |
commit-bot@chromium.org | 92ffe7d | 2013-07-31 22:54:31 +0000 | [diff] [blame] | 1545 | return true; |
| 1546 | } |
| 1547 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1548 | void SkPDFDevice::handlePointAnnotation(const SkPoint& point, |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1549 | const SkMatrix& matrix, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1550 | const char key[], SkData* value) { |
| 1551 | if (!value) { |
| 1552 | return; |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1553 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1554 | |
| 1555 | if (!strcmp(SkAnnotationKeys::Define_Named_Dest_Key(), key)) { |
| 1556 | SkPoint transformedPoint; |
| 1557 | matrix.mapXY(point.x(), point.y(), &transformedPoint); |
| 1558 | fNamedDestinations.emplace_back(value, transformedPoint); |
| 1559 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1560 | } |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 1561 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1562 | void SkPDFDevice::handlePathAnnotation(const SkPath& path, |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1563 | const SkDraw& d, |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1564 | const char key[], SkData* value) { |
| 1565 | if (!value) { |
| 1566 | return; |
| 1567 | } |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1568 | |
| 1569 | SkPath transformedPath = path; |
| 1570 | transformedPath.transform(*d.fMatrix); |
| 1571 | SkRasterClip clip = *d.fRC; |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 1572 | clip.op(transformedPath, SkIRect::MakeWH(width(), height()), SkRegion::kIntersect_Op, |
| 1573 | false); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1574 | SkRect transformedRect = SkRect::Make(clip.getBounds()); |
| 1575 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1576 | if (!strcmp(SkAnnotationKeys::URL_Key(), key)) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1577 | if (!transformedRect.isEmpty()) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1578 | fLinkToURLs.emplace_back(transformedRect, value); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1579 | } |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1580 | } else if (!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) { |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1581 | if (!transformedRect.isEmpty()) { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 1582 | fLinkToDestinations.emplace_back(transformedRect, value); |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1583 | } |
reed | 1610835 | 2016-03-03 09:14:36 -0800 | [diff] [blame] | 1584 | } |
halcanary | 438de49 | 2015-04-28 06:21:01 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1587 | void SkPDFDevice::appendAnnotations(SkPDFArray* array) const { |
| 1588 | array->reserve(fLinkToURLs.count() + fLinkToDestinations.count()); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1589 | for (const RectWithData& rectWithURL : fLinkToURLs) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1590 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1591 | fInitialTransform.mapRect(&r, rectWithURL.rect); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1592 | array->appendObject(create_link_to_url(rectWithURL.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1593 | } |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1594 | for (const RectWithData& linkToDestination : fLinkToDestinations) { |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1595 | SkRect r; |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1596 | fInitialTransform.mapRect(&r, linkToDestination.rect); |
halcanary | d7b2885 | 2016-03-07 12:39:14 -0800 | [diff] [blame] | 1597 | array->appendObject( |
| 1598 | create_link_named_dest(linkToDestination.data.get(), r)); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1599 | } |
| 1600 | } |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1601 | |
halcanary | 6d62270 | 2015-03-25 08:45:42 -0700 | [diff] [blame] | 1602 | void SkPDFDevice::appendDestinations(SkPDFDict* dict, SkPDFObject* page) const { |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1603 | for (const NamedDestination& dest : fNamedDestinations) { |
halcanary | ece8392 | 2016-03-08 08:32:12 -0800 | [diff] [blame] | 1604 | auto pdfDest = sk_make_sp<SkPDFArray>(); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1605 | pdfDest->reserve(5); |
halcanary | e94ea62 | 2016-03-09 07:52:09 -0800 | [diff] [blame] | 1606 | pdfDest->appendObjRef(sk_ref_sp(page)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1607 | pdfDest->appendName("XYZ"); |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1608 | SkPoint p = fInitialTransform.mapXY(dest.point.x(), dest.point.y()); |
wangxianzhu | ef6c50a | 2015-09-17 20:38:02 -0700 | [diff] [blame] | 1609 | pdfDest->appendScalar(p.x()); |
| 1610 | pdfDest->appendScalar(p.y()); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1611 | pdfDest->appendInt(0); // Leave zoom unchanged |
halcanary | 91fcb3e | 2016-03-04 13:53:22 -0800 | [diff] [blame] | 1612 | SkString name(static_cast<const char*>(dest.nameData->data())); |
halcanary | 8103a34 | 2016-03-08 15:10:16 -0800 | [diff] [blame] | 1613 | dict->insertObject(name, std::move(pdfDest)); |
epoger@google.com | b58772f | 2013-03-08 09:09:10 +0000 | [diff] [blame] | 1614 | } |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1617 | SkPDFFormXObject* SkPDFDevice::createFormXObjectFromDevice() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1618 | SkPDFFormXObject* xobject = new SkPDFFormXObject(this); |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 1619 | // We always draw the form xobjects that we create back into the device, so |
| 1620 | // we simply preserve the font usage instead of pulling it out and merging |
| 1621 | // it back in later. |
| 1622 | cleanUp(false); // Reset this device to have no content. |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1623 | init(); |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1624 | return xobject; |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1627 | void SkPDFDevice::drawFormXObjectWithMask(int xObjectIndex, |
| 1628 | SkPDFFormXObject* mask, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1629 | const SkClipStack* clipStack, |
| 1630 | const SkRegion& clipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1631 | SkXfermode::Mode mode, |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1632 | bool invertClip) { |
| 1633 | if (clipRegion.isEmpty() && !invertClip) { |
| 1634 | return; |
| 1635 | } |
| 1636 | |
halcanary | 1437c1e | 2016-03-13 18:30:24 -0700 | [diff] [blame] | 1637 | auto sMaskGS = SkPDFGraphicState::GetSMaskGraphicState( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1638 | mask, invertClip, SkPDFGraphicState::kAlpha_SMaskMode, fDocument->canon()); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1639 | |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1640 | SkMatrix identity; |
| 1641 | identity.reset(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1642 | SkPaint paint; |
| 1643 | paint.setXfermodeMode(mode); |
| 1644 | ScopedContentEntry content(this, clipStack, clipRegion, identity, paint); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1645 | if (!content.entry()) { |
| 1646 | return; |
| 1647 | } |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1648 | SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1649 | &content.entry()->fContent); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1650 | SkPDFUtils::DrawFormXObject(xObjectIndex, &content.entry()->fContent); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1651 | |
halcanary | 1437c1e | 2016-03-13 18:30:24 -0700 | [diff] [blame] | 1652 | // Call makeNoSmaskGraphicState() instead of |
| 1653 | // SkPDFGraphicState::MakeNoSmaskGraphicState so that the canon |
| 1654 | // can deduplicate. |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1655 | sMaskGS = fDocument->canon()->makeNoSmaskGraphicState(); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1656 | SkPDFUtils::ApplyGraphicState(addGraphicStateResource(sMaskGS.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1657 | &content.entry()->fContent); |
vandebo@chromium.org | 466f3d6 | 2011-05-18 23:06:29 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1660 | ContentEntry* SkPDFDevice::setUpContentEntry(const SkClipStack* clipStack, |
| 1661 | const SkRegion& clipRegion, |
| 1662 | const SkMatrix& matrix, |
| 1663 | const SkPaint& paint, |
| 1664 | bool hasText, |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1665 | SkPDFFormXObject** dst) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1666 | *dst = nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1667 | if (clipRegion.isEmpty()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1668 | return nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1671 | // The clip stack can come from an SkDraw where it is technically optional. |
| 1672 | SkClipStack synthesizedClipStack; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1673 | if (clipStack == nullptr) { |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1674 | if (clipRegion == fExistingClipRegion) { |
| 1675 | clipStack = &fExistingClipStack; |
| 1676 | } else { |
| 1677 | // GraphicStackState::updateClip expects the clip stack to have |
| 1678 | // fExistingClip as a prefix, so start there, then set the clip |
| 1679 | // to the passed region. |
| 1680 | synthesizedClipStack = fExistingClipStack; |
| 1681 | SkPath clipPath; |
| 1682 | clipRegion.getBoundaryPath(&clipPath); |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 1683 | synthesizedClipStack.clipDevPath(clipPath, SkRegion::kReplace_Op, |
| 1684 | false); |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1685 | clipStack = &synthesizedClipStack; |
| 1686 | } |
| 1687 | } |
| 1688 | |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1689 | SkXfermode::Mode xfermode = SkXfermode::kSrcOver_Mode; |
| 1690 | if (paint.getXfermode()) { |
| 1691 | paint.getXfermode()->asMode(&xfermode); |
| 1692 | } |
| 1693 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1694 | // For the following modes, we want to handle source and destination |
| 1695 | // separately, so make an object of what's already there. |
| 1696 | if (xfermode == SkXfermode::kClear_Mode || |
| 1697 | xfermode == SkXfermode::kSrc_Mode || |
| 1698 | xfermode == SkXfermode::kSrcIn_Mode || |
| 1699 | xfermode == SkXfermode::kDstIn_Mode || |
| 1700 | xfermode == SkXfermode::kSrcOut_Mode || |
| 1701 | xfermode == SkXfermode::kDstOut_Mode || |
| 1702 | xfermode == SkXfermode::kSrcATop_Mode || |
| 1703 | xfermode == SkXfermode::kDstATop_Mode || |
| 1704 | xfermode == SkXfermode::kModulate_Mode) { |
| 1705 | if (!isContentEmpty()) { |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1706 | *dst = createFormXObjectFromDevice(); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1707 | SkASSERT(isContentEmpty()); |
| 1708 | } else if (xfermode != SkXfermode::kSrc_Mode && |
| 1709 | xfermode != SkXfermode::kSrcOut_Mode) { |
| 1710 | // Except for Src and SrcOut, if there isn't anything already there, |
| 1711 | // then we're done. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1712 | return nullptr; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1713 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1714 | } |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 1715 | // 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] | 1716 | // Xor, Plus. |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1717 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1718 | // Dst xfer mode doesn't draw source at all. |
| 1719 | if (xfermode == SkXfermode::kDst_Mode) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1720 | return nullptr; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1723 | ContentEntry* entry; |
commit-bot@chromium.org | e029440 | 2013-08-29 22:14:04 +0000 | [diff] [blame] | 1724 | SkAutoTDelete<ContentEntry> newEntry; |
ctguil@chromium.org | 8dcf74f | 2011-07-12 21:56:27 +0000 | [diff] [blame] | 1725 | |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1726 | if (fLastContentEntry && fLastContentEntry->fContent.getOffset() == 0) { |
| 1727 | entry = fLastContentEntry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1728 | } else { |
| 1729 | newEntry.reset(new ContentEntry); |
| 1730 | entry = newEntry.get(); |
| 1731 | } |
| 1732 | |
vandebo@chromium.org | 78dad54 | 2011-05-11 18:46:03 +0000 | [diff] [blame] | 1733 | populateGraphicStateEntryFromPaint(matrix, *clipStack, clipRegion, paint, |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1734 | hasText, &entry->fState); |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1735 | if (fLastContentEntry && xfermode != SkXfermode::kDstOver_Mode && |
| 1736 | entry->fState.compareInitialState(fLastContentEntry->fState)) { |
| 1737 | return fLastContentEntry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1740 | if (!fLastContentEntry) { |
| 1741 | fContentEntries.reset(entry); |
| 1742 | fLastContentEntry = entry; |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 1743 | } else if (xfermode == SkXfermode::kDstOver_Mode) { |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1744 | entry->fNext.reset(fContentEntries.release()); |
| 1745 | fContentEntries.reset(entry); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1746 | } else { |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1747 | fLastContentEntry->fNext.reset(entry); |
| 1748 | fLastContentEntry = entry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1749 | } |
halcanary | fcad44b | 2016-03-06 14:47:10 -0800 | [diff] [blame] | 1750 | newEntry.release(); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1751 | return entry; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1754 | void SkPDFDevice::finishContentEntry(SkXfermode::Mode xfermode, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1755 | SkPDFFormXObject* dst, |
| 1756 | SkPath* shape) { |
| 1757 | if (xfermode != SkXfermode::kClear_Mode && |
| 1758 | xfermode != SkXfermode::kSrc_Mode && |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1759 | xfermode != SkXfermode::kDstOver_Mode && |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1760 | xfermode != SkXfermode::kSrcIn_Mode && |
| 1761 | xfermode != SkXfermode::kDstIn_Mode && |
| 1762 | xfermode != SkXfermode::kSrcOut_Mode && |
| 1763 | xfermode != SkXfermode::kDstOut_Mode && |
| 1764 | xfermode != SkXfermode::kSrcATop_Mode && |
| 1765 | xfermode != SkXfermode::kDstATop_Mode && |
| 1766 | xfermode != SkXfermode::kModulate_Mode) { |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1767 | SkASSERT(!dst); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1768 | return; |
| 1769 | } |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1770 | if (xfermode == SkXfermode::kDstOver_Mode) { |
| 1771 | SkASSERT(!dst); |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1772 | if (fContentEntries->fContent.getOffset() == 0) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1773 | // For DstOver, an empty content entry was inserted before the rest |
| 1774 | // of the content entries. If nothing was drawn, it needs to be |
| 1775 | // removed. |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1776 | fContentEntries.reset(fContentEntries->fNext.release()); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1777 | } |
| 1778 | return; |
| 1779 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1780 | if (!dst) { |
| 1781 | SkASSERT(xfermode == SkXfermode::kSrc_Mode || |
| 1782 | xfermode == SkXfermode::kSrcOut_Mode); |
| 1783 | return; |
| 1784 | } |
ctguil@chromium.org | 9510ccc | 2011-07-27 00:10:51 +0000 | [diff] [blame] | 1785 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1786 | SkASSERT(dst); |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1787 | SkASSERT(!fContentEntries->fNext.get()); |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1788 | // Changing the current content into a form-xobject will destroy the clip |
| 1789 | // objects which is fine since the xobject will already be clipped. However |
| 1790 | // if source has shape, we need to clip it too, so a copy of the clip is |
| 1791 | // saved. |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1792 | SkClipStack clipStack = fContentEntries->fState.fClipStack; |
| 1793 | SkRegion clipRegion = fContentEntries->fState.fClipRegion; |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1794 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1795 | SkMatrix identity; |
| 1796 | identity.reset(); |
| 1797 | SkPaint stockPaint; |
| 1798 | |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1799 | sk_sp<SkPDFFormXObject> srcFormXObject; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1800 | if (isContentEmpty()) { |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1801 | // If nothing was drawn and there's no shape, then the draw was a |
| 1802 | // no-op, but dst needs to be restored for that to be true. |
| 1803 | // If there is shape, then an empty source with Src, SrcIn, SrcOut, |
| 1804 | // DstIn, DstAtop or Modulate reduces to Clear and DstOut or SrcAtop |
| 1805 | // reduces to Dst. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1806 | if (shape == nullptr || xfermode == SkXfermode::kDstOut_Mode || |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1807 | xfermode == SkXfermode::kSrcATop_Mode) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1808 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1809 | fExistingClipRegion, identity, |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1810 | stockPaint); |
| 1811 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst), |
| 1812 | &content.entry()->fContent); |
| 1813 | return; |
| 1814 | } else { |
| 1815 | xfermode = SkXfermode::kClear_Mode; |
| 1816 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1817 | } else { |
| 1818 | SkASSERT(!fContentEntries->fNext.get()); |
reed@google.com | fc641d0 | 2012-09-20 17:52:20 +0000 | [diff] [blame] | 1819 | srcFormXObject.reset(createFormXObjectFromDevice()); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1822 | // TODO(vandebo) srcFormXObject may contain alpha, but here we want it |
| 1823 | // without alpha. |
| 1824 | if (xfermode == SkXfermode::kSrcATop_Mode) { |
| 1825 | // TODO(vandebo): In order to properly support SrcATop we have to track |
| 1826 | // the shape of what's been drawn at all times. It's the intersection of |
| 1827 | // the non-transparent parts of the device and the outlines (shape) of |
| 1828 | // all images and devices drawn. |
| 1829 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst, |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1830 | &fExistingClipStack, fExistingClipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1831 | SkXfermode::kSrcOver_Mode, true); |
| 1832 | } else { |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1833 | sk_sp<SkPDFFormXObject> dstMaskStorage; |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1834 | SkPDFFormXObject* dstMask = srcFormXObject.get(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1835 | if (shape != nullptr) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1836 | // Draw shape into a form-xobject. |
| 1837 | SkDraw d; |
| 1838 | d.fMatrix = &identity; |
| 1839 | d.fClip = &clipRegion; |
| 1840 | d.fClipStack = &clipStack; |
| 1841 | SkPaint filledPaint; |
| 1842 | filledPaint.setColor(SK_ColorBLACK); |
| 1843 | filledPaint.setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1844 | this->drawPath(d, *shape, filledPaint, nullptr, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1845 | |
| 1846 | dstMaskStorage.reset(createFormXObjectFromDevice()); |
| 1847 | dstMask = dstMaskStorage.get(); |
| 1848 | } |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1849 | drawFormXObjectWithMask(addXObjectResource(dst), dstMask, |
| 1850 | &fExistingClipStack, fExistingClipRegion, |
| 1851 | SkXfermode::kSrcOver_Mode, true); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1854 | if (xfermode == SkXfermode::kClear_Mode) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1855 | return; |
| 1856 | } else if (xfermode == SkXfermode::kSrc_Mode || |
| 1857 | xfermode == SkXfermode::kDstATop_Mode) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1858 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1859 | fExistingClipRegion, identity, stockPaint); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1860 | if (content.entry()) { |
| 1861 | SkPDFUtils::DrawFormXObject( |
| 1862 | this->addXObjectResource(srcFormXObject.get()), |
| 1863 | &content.entry()->fContent); |
| 1864 | } |
| 1865 | if (xfermode == SkXfermode::kSrc_Mode) { |
| 1866 | return; |
| 1867 | } |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1868 | } else if (xfermode == SkXfermode::kSrcATop_Mode) { |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1869 | ScopedContentEntry content(this, &fExistingClipStack, |
| 1870 | fExistingClipRegion, identity, stockPaint); |
commit-bot@chromium.org | 7542dc8 | 2013-12-03 21:08:46 +0000 | [diff] [blame] | 1871 | if (content.entry()) { |
| 1872 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(dst), |
| 1873 | &content.entry()->fContent); |
| 1874 | } |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | SkASSERT(xfermode == SkXfermode::kSrcIn_Mode || |
| 1878 | xfermode == SkXfermode::kDstIn_Mode || |
| 1879 | xfermode == SkXfermode::kSrcOut_Mode || |
| 1880 | xfermode == SkXfermode::kDstOut_Mode || |
| 1881 | xfermode == SkXfermode::kSrcATop_Mode || |
| 1882 | xfermode == SkXfermode::kDstATop_Mode || |
| 1883 | xfermode == SkXfermode::kModulate_Mode); |
| 1884 | |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1885 | if (xfermode == SkXfermode::kSrcIn_Mode || |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1886 | xfermode == SkXfermode::kSrcOut_Mode || |
| 1887 | xfermode == SkXfermode::kSrcATop_Mode) { |
| 1888 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), dst, |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1889 | &fExistingClipStack, fExistingClipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1890 | SkXfermode::kSrcOver_Mode, |
| 1891 | xfermode == SkXfermode::kSrcOut_Mode); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1892 | } else { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1893 | SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode; |
| 1894 | if (xfermode == SkXfermode::kModulate_Mode) { |
| 1895 | drawFormXObjectWithMask(addXObjectResource(srcFormXObject.get()), |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1896 | dst, &fExistingClipStack, |
| 1897 | fExistingClipRegion, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1898 | SkXfermode::kSrcOver_Mode, false); |
| 1899 | mode = SkXfermode::kMultiply_Mode; |
| 1900 | } |
| 1901 | drawFormXObjectWithMask(addXObjectResource(dst), srcFormXObject.get(), |
commit-bot@chromium.org | 4e8f1e5 | 2013-12-17 23:38:28 +0000 | [diff] [blame] | 1902 | &fExistingClipStack, fExistingClipRegion, mode, |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 1903 | xfermode == SkXfermode::kDstOut_Mode); |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1904 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1907 | bool SkPDFDevice::isContentEmpty() { |
halcanary | 73557f6 | 2016-03-25 14:44:33 -0700 | [diff] [blame] | 1908 | if (!fContentEntries || fContentEntries->fContent.getOffset() == 0) { |
| 1909 | SkASSERT(!fContentEntries || !fContentEntries->fNext.get()); |
vandebo@chromium.org | 481aef6 | 2011-05-24 16:39:05 +0000 | [diff] [blame] | 1910 | return true; |
| 1911 | } |
| 1912 | return false; |
| 1913 | } |
| 1914 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1915 | void SkPDFDevice::populateGraphicStateEntryFromPaint( |
| 1916 | const SkMatrix& matrix, |
| 1917 | const SkClipStack& clipStack, |
| 1918 | const SkRegion& clipRegion, |
| 1919 | const SkPaint& paint, |
| 1920 | bool hasText, |
| 1921 | GraphicStateEntry* entry) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1922 | NOT_IMPLEMENTED(paint.getPathEffect() != nullptr, false); |
| 1923 | NOT_IMPLEMENTED(paint.getMaskFilter() != nullptr, false); |
| 1924 | NOT_IMPLEMENTED(paint.getColorFilter() != nullptr, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1925 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1926 | entry->fMatrix = matrix; |
| 1927 | entry->fClipStack = clipStack; |
| 1928 | entry->fClipRegion = clipRegion; |
vandebo@chromium.org | da6c569 | 2012-06-28 21:37:20 +0000 | [diff] [blame] | 1929 | entry->fColor = SkColorSetA(paint.getColor(), 0xFF); |
| 1930 | entry->fShaderIndex = -1; |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 1931 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1932 | // 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] | 1933 | sk_sp<SkPDFObject> pdfShader; |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1934 | SkShader* shader = paint.getShader(); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1935 | SkColor color = paint.getColor(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1936 | if (shader) { |
| 1937 | // PDF positions patterns relative to the initial transform, so |
| 1938 | // we need to apply the current transform to the shader parameters. |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1939 | SkMatrix transform = matrix; |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame] | 1940 | transform.postConcat(fInitialTransform); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1941 | |
| 1942 | // 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] | 1943 | // a pattern the size of the current clip. |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 1944 | SkIRect bounds = clipRegion.getBounds(); |
vandebo@chromium.org | 293a758 | 2012-03-16 19:50:37 +0000 | [diff] [blame] | 1945 | |
| 1946 | // We need to apply the initial transform to bounds in order to get |
| 1947 | // bounds in a consistent coordinate system. |
| 1948 | SkRect boundsTemp; |
| 1949 | boundsTemp.set(bounds); |
| 1950 | fInitialTransform.mapRect(&boundsTemp); |
| 1951 | boundsTemp.roundOut(&bounds); |
| 1952 | |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 1953 | SkScalar rasterScale = |
| 1954 | SkIntToScalar(fRasterDpi) / DPI_FOR_RASTER_SCALE_ONE; |
| 1955 | pdfShader.reset(SkPDFShader::GetPDFShader( |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 1956 | fDocument, fRasterDpi, shader, transform, bounds, rasterScale)); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1957 | |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 1958 | if (pdfShader.get()) { |
| 1959 | // pdfShader has been canonicalized so we can directly compare |
| 1960 | // pointers. |
| 1961 | int resourceIndex = fShaderResources.find(pdfShader.get()); |
| 1962 | if (resourceIndex < 0) { |
| 1963 | resourceIndex = fShaderResources.count(); |
| 1964 | fShaderResources.push(pdfShader.get()); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1965 | pdfShader.get()->ref(); |
vandebo@chromium.org | b88cfe5 | 2011-07-18 18:40:32 +0000 | [diff] [blame] | 1966 | } |
| 1967 | entry->fShaderIndex = resourceIndex; |
| 1968 | } else { |
| 1969 | // A color shader is treated as an invalid shader so we don't have |
| 1970 | // to set a shader just for a color. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1971 | SkShader::GradientInfo gradientInfo; |
| 1972 | SkColor gradientColor; |
| 1973 | gradientInfo.fColors = &gradientColor; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1974 | gradientInfo.fColorOffsets = nullptr; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1975 | gradientInfo.fColorCount = 1; |
| 1976 | if (shader->asAGradient(&gradientInfo) == |
| 1977 | SkShader::kColor_GradientType) { |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1978 | entry->fColor = SkColorSetA(gradientColor, 0xFF); |
| 1979 | color = gradientColor; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1980 | } |
| 1981 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 1982 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1983 | |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 1984 | sk_sp<SkPDFGraphicState> newGraphicState; |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1985 | if (color == paint.getColor()) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1986 | newGraphicState.reset( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1987 | SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), paint)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1988 | } else { |
| 1989 | SkPaint newPaint = paint; |
| 1990 | newPaint.setColor(color); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 1991 | newGraphicState.reset( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 1992 | SkPDFGraphicState::GetGraphicStateForPaint(fDocument->canon(), newPaint)); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1993 | } |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 1994 | int resourceIndex = addGraphicStateResource(newGraphicState.get()); |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1995 | entry->fGraphicStateIndex = resourceIndex; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1996 | |
vandebo@chromium.org | 9fbdf87 | 2011-05-09 07:55:58 +0000 | [diff] [blame] | 1997 | if (hasText) { |
| 1998 | entry->fTextScaleX = paint.getTextScaleX(); |
| 1999 | entry->fTextFill = paint.getStyle(); |
| 2000 | } else { |
| 2001 | entry->fTextScaleX = 0; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | |
halcanary | be27a11 | 2015-04-01 13:31:19 -0700 | [diff] [blame] | 2005 | int SkPDFDevice::addGraphicStateResource(SkPDFObject* gs) { |
vandebo@chromium.org | 6112c21 | 2011-05-13 03:50:38 +0000 | [diff] [blame] | 2006 | // Assumes that gs has been canonicalized (so we can directly compare |
| 2007 | // pointers). |
| 2008 | int result = fGraphicStateResources.find(gs); |
| 2009 | if (result < 0) { |
| 2010 | result = fGraphicStateResources.count(); |
| 2011 | fGraphicStateResources.push(gs); |
| 2012 | gs->ref(); |
| 2013 | } |
| 2014 | return result; |
| 2015 | } |
| 2016 | |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2017 | int SkPDFDevice::addXObjectResource(SkPDFObject* xObject) { |
| 2018 | // Assumes that xobject has been canonicalized (so we can directly compare |
| 2019 | // pointers). |
| 2020 | int result = fXObjectResources.find(xObject); |
| 2021 | if (result < 0) { |
| 2022 | result = fXObjectResources.count(); |
| 2023 | fXObjectResources.push(xObject); |
| 2024 | xObject->ref(); |
| 2025 | } |
| 2026 | return result; |
| 2027 | } |
| 2028 | |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 2029 | void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID, |
| 2030 | ContentEntry* contentEntry) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 2031 | SkTypeface* typeface = paint.getTypeface(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2032 | if (contentEntry->fState.fFont == nullptr || |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 2033 | contentEntry->fState.fTextSize != paint.getTextSize() || |
| 2034 | !contentEntry->fState.fFont->hasGlyph(glyphID)) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 2035 | int fontIndex = getFontResourceIndex(typeface, glyphID); |
commit-bot@chromium.org | 4740135 | 2013-07-23 21:49:29 +0000 | [diff] [blame] | 2036 | contentEntry->fContent.writeText("/"); |
| 2037 | contentEntry->fContent.writeText(SkPDFResourceDict::getResourceName( |
| 2038 | SkPDFResourceDict::kFont_ResourceType, |
| 2039 | fontIndex).c_str()); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 2040 | contentEntry->fContent.writeText(" "); |
halcanary | bc4696b | 2015-05-06 10:56:04 -0700 | [diff] [blame] | 2041 | SkPDFUtils::AppendScalar(paint.getTextSize(), &contentEntry->fContent); |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 2042 | contentEntry->fContent.writeText(" Tf\n"); |
| 2043 | contentEntry->fState.fFont = fFontResources[fontIndex]; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 2047 | int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { |
halcanary | 48810a0 | 2016-03-07 14:57:50 -0800 | [diff] [blame] | 2048 | sk_sp<SkPDFFont> newFont( |
halcanary | 989da4a | 2016-03-21 14:33:17 -0700 | [diff] [blame] | 2049 | SkPDFFont::GetFontResource(fDocument->canon(), typeface, glyphID)); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2050 | int resourceIndex = fFontResources.find(newFont.get()); |
| 2051 | if (resourceIndex < 0) { |
| 2052 | resourceIndex = fFontResources.count(); |
| 2053 | fFontResources.push(newFont.get()); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 2054 | newFont.get()->ref(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 2055 | } |
| 2056 | return resourceIndex; |
| 2057 | } |
| 2058 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2059 | static SkSize rect_to_size(const SkRect& r) { |
| 2060 | return SkSize::Make(r.width(), r.height()); |
| 2061 | } |
| 2062 | |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2063 | static sk_sp<SkImage> color_filter(const SkImageBitmap& imageBitmap, |
| 2064 | SkColorFilter* colorFilter) { |
| 2065 | auto surface = |
| 2066 | SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(imageBitmap.dimensions())); |
| 2067 | SkASSERT(surface); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2068 | SkCanvas* canvas = surface->getCanvas(); |
| 2069 | canvas->clear(SK_ColorTRANSPARENT); |
| 2070 | SkPaint paint; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 2071 | paint.setColorFilter(sk_ref_sp(colorFilter)); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2072 | imageBitmap.draw(canvas, &paint); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2073 | canvas->flush(); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2074 | return surface->makeImageSnapshot(); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | //////////////////////////////////////////////////////////////////////////////// |
| 2078 | void SkPDFDevice::internalDrawImage(const SkMatrix& origMatrix, |
| 2079 | const SkClipStack* clipStack, |
| 2080 | const SkRegion& origClipRegion, |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2081 | SkImageBitmap imageBitmap, |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2082 | const SkPaint& paint) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2083 | if (imageBitmap.dimensions().isZero()) { |
| 2084 | return; |
| 2085 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2086 | #ifdef SK_PDF_IMAGE_STATS |
| 2087 | gDrawImageCalls.fetch_add(1); |
| 2088 | #endif |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2089 | SkMatrix matrix = origMatrix; |
| 2090 | SkRegion perspectiveBounds; |
| 2091 | const SkRegion* clipRegion = &origClipRegion; |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2092 | sk_sp<SkImage> autoImageUnref; |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2093 | |
| 2094 | // Rasterize the bitmap using perspective in a new bitmap. |
| 2095 | if (origMatrix.hasPerspective()) { |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2096 | if (fRasterDpi == 0) { |
| 2097 | return; |
| 2098 | } |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2099 | // Transform the bitmap in the new space, without taking into |
| 2100 | // account the initial transform. |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2101 | SkPath perspectiveOutline; |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2102 | SkRect imageBounds = SkRect::Make(imageBitmap.bounds()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2103 | perspectiveOutline.addRect(imageBounds); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2104 | perspectiveOutline.transform(origMatrix); |
| 2105 | |
| 2106 | // TODO(edisonn): perf - use current clip too. |
| 2107 | // Retrieve the bounds of the new shape. |
| 2108 | SkRect bounds = perspectiveOutline.getBounds(); |
| 2109 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2110 | // Transform the bitmap in the new space, taking into |
| 2111 | // account the initial transform. |
| 2112 | SkMatrix total = origMatrix; |
| 2113 | total.postConcat(fInitialTransform); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2114 | SkScalar dpiScale = SkIntToScalar(fRasterDpi) / |
| 2115 | SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE); |
| 2116 | total.postScale(dpiScale, dpiScale); |
| 2117 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2118 | SkPath physicalPerspectiveOutline; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2119 | physicalPerspectiveOutline.addRect(imageBounds); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2120 | physicalPerspectiveOutline.transform(total); |
| 2121 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2122 | SkRect physicalPerspectiveBounds = |
| 2123 | physicalPerspectiveOutline.getBounds(); |
| 2124 | SkScalar scaleX = physicalPerspectiveBounds.width() / bounds.width(); |
| 2125 | SkScalar scaleY = physicalPerspectiveBounds.height() / bounds.height(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2126 | |
| 2127 | // TODO(edisonn): A better approach would be to use a bitmap shader |
| 2128 | // (in clamp mode) and draw a rect over the entire bounding box. Then |
| 2129 | // intersect perspectiveOutline to the clip. That will avoid introducing |
| 2130 | // alpha to the image while still giving good behavior at the edge of |
| 2131 | // the image. Avoiding alpha will reduce the pdf size and generation |
| 2132 | // CPU time some. |
| 2133 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2134 | SkISize wh = rect_to_size(physicalPerspectiveBounds).toCeil(); |
| 2135 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 2136 | auto surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(wh))); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2137 | if (!surface) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 2138 | return; |
| 2139 | } |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2140 | SkCanvas* canvas = surface->getCanvas(); |
| 2141 | canvas->clear(SK_ColorTRANSPARENT); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2142 | |
| 2143 | SkScalar deltaX = bounds.left(); |
| 2144 | SkScalar deltaY = bounds.top(); |
| 2145 | |
| 2146 | SkMatrix offsetMatrix = origMatrix; |
| 2147 | offsetMatrix.postTranslate(-deltaX, -deltaY); |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2148 | offsetMatrix.postScale(scaleX, scaleY); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2149 | |
| 2150 | // Translate the draw in the new canvas, so we perfectly fit the |
| 2151 | // shape in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2152 | canvas->setMatrix(offsetMatrix); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2153 | imageBitmap.draw(canvas, nullptr); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2154 | // Make sure the final bits are in the bitmap. |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2155 | canvas->flush(); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2156 | |
edisonn@google.com | 73a7ea3 | 2013-11-11 20:55:15 +0000 | [diff] [blame] | 2157 | // In the new space, we use the identity matrix translated |
| 2158 | // and scaled to reflect DPI. |
| 2159 | matrix.setScale(1 / scaleX, 1 / scaleY); |
| 2160 | matrix.postTranslate(deltaX, deltaY); |
| 2161 | |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2162 | perspectiveBounds.setRect(bounds.roundOut()); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2163 | clipRegion = &perspectiveBounds; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2164 | |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2165 | autoImageUnref = surface->makeImageSnapshot(); |
| 2166 | imageBitmap = SkImageBitmap(autoImageUnref.get()); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2169 | SkMatrix scaled; |
| 2170 | // Adjust for origin flip. |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 2171 | scaled.setScale(SK_Scalar1, -SK_Scalar1); |
| 2172 | scaled.postTranslate(0, SK_Scalar1); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2173 | // Scale the image up from 1x1 to WxH. |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2174 | SkIRect subset = imageBitmap.bounds(); |
| 2175 | scaled.postScale(SkIntToScalar(imageBitmap.dimensions().width()), |
| 2176 | SkIntToScalar(imageBitmap.dimensions().height())); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 2177 | scaled.postConcat(matrix); |
edisonn@google.com | 9cf0cb1 | 2013-10-16 18:32:35 +0000 | [diff] [blame] | 2178 | ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint); |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2179 | if (!content.entry()) { |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2180 | return; |
| 2181 | } |
| 2182 | if (content.needShape()) { |
| 2183 | SkPath shape; |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2184 | shape.addRect(SkRect::Make(subset)); |
vandebo@chromium.org | 3b41621 | 2013-10-30 20:48:05 +0000 | [diff] [blame] | 2185 | shape.transform(matrix); |
| 2186 | content.setShape(shape); |
| 2187 | } |
| 2188 | if (!content.needSource()) { |
vandebo@chromium.org | 25adce8 | 2011-05-09 08:05:01 +0000 | [diff] [blame] | 2189 | return; |
| 2190 | } |
| 2191 | |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2192 | if (SkColorFilter* colorFilter = paint.getColorFilter()) { |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 2193 | // TODO(https://bug.skia.org/4378): implement colorfilter on other |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2194 | // draw calls. This code here works for all |
| 2195 | // drawBitmap*()/drawImage*() calls amd ImageFilters (which |
| 2196 | // rasterize a layer on this backend). Fortuanely, this seems |
| 2197 | // to be how Chromium impements most color-filters. |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2198 | autoImageUnref = color_filter(imageBitmap, colorFilter); |
| 2199 | imageBitmap = SkImageBitmap(autoImageUnref.get()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2200 | // TODO(halcanary): de-dupe this by caching filtered images. |
| 2201 | // (maybe in the resource cache?) |
| 2202 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2203 | |
| 2204 | SkBitmapKey key = imageBitmap.getKey(); |
| 2205 | sk_sp<SkPDFObject> pdfimage = fDocument->canon()->findPDFBitmap(key); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2206 | if (!pdfimage) { |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2207 | auto img = imageBitmap.makeImage(); |
| 2208 | if (!img) { |
| 2209 | return; |
| 2210 | } |
| 2211 | pdfimage = SkPDFCreateBitmapObject( |
| 2212 | std::move(img), fDocument->canon()->getPixelSerializer()); |
halcanary | 7a14b31 | 2015-10-01 07:28:13 -0700 | [diff] [blame] | 2213 | if (!pdfimage) { |
| 2214 | return; |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2215 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2216 | fDocument->serialize(pdfimage); // serialize images early. |
| 2217 | fDocument->canon()->addPDFBitmap(key, pdfimage); |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 2218 | } |
halcanary | a50151d | 2016-03-25 11:57:49 -0700 | [diff] [blame] | 2219 | // TODO(halcanary): addXObjectResource() should take a sk_sp<SkPDFObject> |
halcanary | 3d8c33c | 2015-10-01 11:06:22 -0700 | [diff] [blame] | 2220 | SkPDFUtils::DrawFormXObject(this->addXObjectResource(pdfimage.get()), |
vandebo@chromium.org | b069c8c | 2011-05-24 17:19:38 +0000 | [diff] [blame] | 2221 | &content.entry()->fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 2222 | } |