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