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