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