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