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