vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 1 | /* |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 Google Inc. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "SkPDFDevice.h" |
| 18 | |
| 19 | #include "SkColor.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 20 | #include "SkGlyphCache.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 21 | #include "SkPaint.h" |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 22 | #include "SkPath.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 23 | #include "SkPDFImage.h" |
| 24 | #include "SkPDFGraphicState.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 25 | #include "SkPDFFont.h" |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 26 | #include "SkPDFFormXObject.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 27 | #include "SkPDFTypes.h" |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 28 | #include "SkPDFShader.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 29 | #include "SkPDFStream.h" |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 30 | #include "SkPDFUtils.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 31 | #include "SkRect.h" |
| 32 | #include "SkString.h" |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 33 | #include "SkTextFormatParams.h" |
| 34 | #include "SkTypeface.h" |
| 35 | #include "SkTypes.h" |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 36 | |
| 37 | // Utility functions |
| 38 | |
| 39 | namespace { |
| 40 | |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 41 | void emitPDFColor(SkColor color, SkWStream* result) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 42 | SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. |
| 43 | SkScalar colorMax = SkIntToScalar(0xFF); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 44 | SkPDFScalar::Append( |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 45 | SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result); |
| 46 | result->writeText(" "); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 47 | SkPDFScalar::Append( |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 48 | SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result); |
| 49 | result->writeText(" "); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 50 | SkPDFScalar::Append( |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 51 | SkScalarDiv(SkIntToScalar(SkColorGetB(color)), colorMax), result); |
| 52 | result->writeText(" "); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 53 | } |
| 54 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 55 | SkPaint calculateTextPaint(const SkPaint& paint) { |
| 56 | SkPaint result = paint; |
| 57 | if (result.isFakeBoldText()) { |
| 58 | SkScalar fakeBoldScale = SkScalarInterpFunc(result.getTextSize(), |
| 59 | kStdFakeBoldInterpKeys, |
| 60 | kStdFakeBoldInterpValues, |
| 61 | kStdFakeBoldInterpLength); |
| 62 | SkScalar width = SkScalarMul(result.getTextSize(), fakeBoldScale); |
| 63 | if (result.getStyle() == SkPaint::kFill_Style) |
| 64 | result.setStyle(SkPaint::kStrokeAndFill_Style); |
| 65 | else |
| 66 | width += result.getStrokeWidth(); |
| 67 | result.setStrokeWidth(width); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | // Stolen from measure_text in SkDraw.cpp and then tweaked. |
| 73 | void alignText(SkDrawCacheProc glyphCacheProc, const SkPaint& paint, |
| 74 | const uint16_t* glyphs, size_t len, SkScalar* x, SkScalar* y, |
| 75 | SkScalar* width) { |
| 76 | if (paint.getTextAlign() == SkPaint::kLeft_Align && width == NULL) |
| 77 | return; |
| 78 | |
| 79 | SkMatrix ident; |
| 80 | ident.reset(); |
| 81 | SkAutoGlyphCache autoCache(paint, &ident); |
| 82 | SkGlyphCache* cache = autoCache.getCache(); |
| 83 | |
| 84 | const char* start = (char*)glyphs; |
| 85 | const char* stop = (char*)(glyphs + len); |
| 86 | SkFixed xAdv = 0, yAdv = 0; |
| 87 | |
| 88 | // TODO(vandebo) This probably needs to take kerning into account. |
| 89 | while (start < stop) { |
| 90 | const SkGlyph& glyph = glyphCacheProc(cache, &start, 0, 0); |
| 91 | xAdv += glyph.fAdvanceX; |
| 92 | yAdv += glyph.fAdvanceY; |
| 93 | }; |
| 94 | if (width) |
| 95 | *width = SkFixedToScalar(xAdv); |
| 96 | if (paint.getTextAlign() == SkPaint::kLeft_Align) |
| 97 | return; |
| 98 | |
| 99 | SkScalar xAdj = SkFixedToScalar(xAdv); |
| 100 | SkScalar yAdj = SkFixedToScalar(yAdv); |
| 101 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 102 | xAdj = SkScalarHalf(xAdj); |
| 103 | yAdj = SkScalarHalf(yAdj); |
| 104 | } |
| 105 | *x = *x - xAdj; |
| 106 | *y = *y - yAdj; |
| 107 | } |
| 108 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 109 | } // namespace |
| 110 | |
| 111 | //////////////////////////////////////////////////////////////////////////////// |
| 112 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 113 | SkDevice* SkPDFDeviceFactory::newDevice(SkCanvas*, SkBitmap::Config config, |
| 114 | int width, int height, bool isOpaque, |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 115 | bool isForLayer) { |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 116 | SkMatrix initialTransform; |
| 117 | initialTransform.reset(); |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 118 | if (isForLayer) { |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 119 | initialTransform.setTranslate(0, height); |
| 120 | initialTransform.preScale(1, -1); |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 121 | } |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 122 | return SkNEW_ARGS(SkPDFDevice, (width, height, initialTransform)); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 123 | } |
| 124 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 125 | static inline SkBitmap makeABitmap(int width, int height) { |
| 126 | SkBitmap bitmap; |
| 127 | bitmap.setConfig(SkBitmap::kNo_Config, width, height); |
| 128 | return bitmap; |
| 129 | } |
| 130 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 131 | SkPDFDevice::SkPDFDevice(int width, int height, |
| 132 | const SkMatrix& initialTransform) |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 133 | : SkDevice(NULL, makeABitmap(width, height), false), |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 134 | fWidth(width), |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 135 | fHeight(height), |
reed@google.com | 0770044 | 2010-12-20 19:46:07 +0000 | [diff] [blame] | 136 | fGraphicStackIndex(0) { |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 137 | fGraphicStack[0].fColor = SK_ColorBLACK; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 138 | fGraphicStack[0].fTextSize = SK_ScalarNaN; // This has no default value. |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 139 | fGraphicStack[0].fTextScaleX = SK_Scalar1; |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 140 | fGraphicStack[0].fTextFill = SkPaint::kFill_Style; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 141 | fGraphicStack[0].fFont = NULL; |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 142 | fGraphicStack[0].fShader = NULL; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 143 | fGraphicStack[0].fGraphicState = NULL; |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 144 | fGraphicStack[0].fClip.setRect(0,0, width, height); |
| 145 | fGraphicStack[0].fTransform.reset(); |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 146 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 147 | // Skia generally uses the top left as the origin but PDF natively has the |
| 148 | // origin at the bottom left. This matrix corrects for that. When layering, |
| 149 | // we specify an inverse correction to cancel this out. |
| 150 | fInitialTransform.setTranslate(0, height); |
| 151 | fInitialTransform.preScale(1, -1); |
| 152 | fInitialTransform.preConcat(initialTransform); |
| 153 | if (fInitialTransform.getType() != SkMatrix::kIdentity_Mask) { |
| 154 | SkPDFUtils::AppendTransform(fInitialTransform, &fContent); |
vandebo@chromium.org | f60a001 | 2011-02-24 23:14:04 +0000 | [diff] [blame] | 155 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | SkPDFDevice::~SkPDFDevice() { |
| 159 | fGraphicStateResources.unrefAll(); |
| 160 | fXObjectResources.unrefAll(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 161 | fFontResources.unrefAll(); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 162 | fShaderResources.unrefAll(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void SkPDFDevice::setMatrixClip(const SkMatrix& matrix, |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 166 | const SkRegion& region, |
| 167 | const SkClipStack&) { |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 168 | // See the comment in the header file above GraphicStackEntry. |
| 169 | if (region != fGraphicStack[fGraphicStackIndex].fClip) { |
| 170 | while (fGraphicStackIndex > 0) |
| 171 | popGS(); |
| 172 | pushGS(); |
| 173 | |
| 174 | SkPath clipPath; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 175 | if (region.getBoundaryPath(&clipPath)) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 176 | SkPDFUtils::EmitPath(clipPath, &fContent); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 177 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 178 | SkPath::FillType clipFill = clipPath.getFillType(); |
| 179 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseEvenOdd_FillType, |
| 180 | false); |
| 181 | NOT_IMPLEMENTED(clipFill == SkPath::kInverseWinding_FillType, |
| 182 | false); |
| 183 | if (clipFill == SkPath::kEvenOdd_FillType) |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 184 | fContent.writeText("W* n "); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 185 | else |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 186 | fContent.writeText("W n "); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 187 | } |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 188 | |
| 189 | fGraphicStack[fGraphicStackIndex].fClip = region; |
| 190 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 191 | setTransform(matrix); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void SkPDFDevice::drawPaint(const SkDraw& d, const SkPaint& paint) { |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 195 | SkMatrix identityTransform; |
| 196 | identityTransform.reset(); |
| 197 | SkMatrix curTransform = setTransform(identityTransform); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 198 | |
| 199 | SkPaint newPaint = paint; |
| 200 | newPaint.setStyle(SkPaint::kFill_Style); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 201 | updateGSFromPaint(newPaint, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 202 | |
reed@google.com | a6d59f6 | 2011-03-07 21:29:21 +0000 | [diff] [blame] | 203 | SkRect all = SkRect::MakeWH(SkIntToScalar(this->width()), |
| 204 | SkIntToScalar(this->height())); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 205 | drawRect(d, all, newPaint); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 206 | setTransform(curTransform); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void SkPDFDevice::drawPoints(const SkDraw& d, SkCanvas::PointMode mode, |
| 210 | size_t count, const SkPoint* points, |
| 211 | const SkPaint& paint) { |
| 212 | if (count == 0) |
| 213 | return; |
| 214 | |
| 215 | switch (mode) { |
| 216 | case SkCanvas::kPolygon_PointMode: |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 217 | updateGSFromPaint(paint, false); |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 218 | SkPDFUtils::MoveTo(points[0].fX, points[0].fY, &fContent); |
| 219 | for (size_t i = 1; i < count; i++) { |
| 220 | SkPDFUtils::AppendLine(points[i].fX, points[i].fY, &fContent); |
| 221 | } |
| 222 | SkPDFUtils::StrokePath(&fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 223 | break; |
| 224 | case SkCanvas::kLines_PointMode: |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 225 | updateGSFromPaint(paint, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 226 | for (size_t i = 0; i < count/2; i++) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 227 | SkPDFUtils::MoveTo(points[i * 2].fX, points[i * 2].fY, |
| 228 | &fContent); |
| 229 | SkPDFUtils::AppendLine(points[i * 2 + 1].fX, |
| 230 | points[i * 2 + 1].fY, &fContent); |
| 231 | SkPDFUtils::StrokePath(&fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 232 | } |
| 233 | break; |
| 234 | case SkCanvas::kPoints_PointMode: |
| 235 | if (paint.getStrokeCap() == SkPaint::kRound_Cap) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 236 | updateGSFromPaint(paint, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 237 | for (size_t i = 0; i < count; i++) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 238 | SkPDFUtils::MoveTo(points[i].fX, points[i].fY, &fContent); |
| 239 | SkPDFUtils::StrokePath(&fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 240 | } |
| 241 | } else { |
| 242 | // PDF won't draw a single point with square/butt caps because |
| 243 | // the orientation is ambiguous. Draw a rectangle instead. |
| 244 | SkPaint newPaint = paint; |
| 245 | newPaint.setStyle(SkPaint::kFill_Style); |
| 246 | SkScalar strokeWidth = paint.getStrokeWidth(); |
| 247 | SkScalar halfStroke = strokeWidth * SK_ScalarHalf; |
| 248 | for (size_t i = 0; i < count; i++) { |
| 249 | SkRect r = SkRect::MakeXYWH(points[i].fX, points[i].fY, |
| 250 | 0, 0); |
| 251 | r.inset(-halfStroke, -halfStroke); |
| 252 | drawRect(d, r, newPaint); |
| 253 | } |
| 254 | } |
| 255 | break; |
| 256 | default: |
| 257 | SkASSERT(false); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void SkPDFDevice::drawRect(const SkDraw& d, const SkRect& r, |
| 262 | const SkPaint& paint) { |
| 263 | if (paint.getPathEffect()) { |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 264 | // Create a path for the rectangle and apply the path effect to it. |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 265 | SkPath path; |
| 266 | path.addRect(r); |
| 267 | paint.getFillPath(path, &path); |
| 268 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 269 | SkPaint noEffectPaint(paint); |
| 270 | SkSafeUnref(noEffectPaint.setPathEffect(NULL)); |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 271 | drawPath(d, path, noEffectPaint, NULL, true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 272 | return; |
| 273 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 274 | updateGSFromPaint(paint, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 275 | |
| 276 | // Skia has 0,0 at top left, pdf at bottom left. Do the right thing. |
| 277 | SkScalar bottom = r.fBottom < r.fTop ? r.fBottom : r.fTop; |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 278 | SkPDFUtils::AppendRectangle(r.fLeft, bottom, r.width(), r.height(), |
| 279 | &fContent); |
| 280 | SkPDFUtils::PaintPath(paint.getStyle(), SkPath::kWinding_FillType, |
| 281 | &fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 282 | } |
| 283 | |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 284 | void SkPDFDevice::drawPath(const SkDraw& d, const SkPath& path, |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 285 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
| 286 | bool pathIsMutable) { |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 287 | NOT_IMPLEMENTED(prePathMatrix != NULL, true); |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 288 | |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 289 | if (paint.getPathEffect()) { |
| 290 | // Apply the path effect to path and draw it that way. |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 291 | SkPath noEffectPath; |
| 292 | paint.getFillPath(path, &noEffectPath); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 293 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 294 | SkPaint noEffectPaint(paint); |
| 295 | SkSafeUnref(noEffectPaint.setPathEffect(NULL)); |
vandebo@chromium.org | 02cc5aa | 2011-01-25 22:06:29 +0000 | [diff] [blame] | 296 | drawPath(d, noEffectPath, noEffectPaint, NULL, true); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 297 | return; |
| 298 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 299 | updateGSFromPaint(paint, false); |
vandebo@chromium.org | 7d71f7f | 2010-10-26 19:51:44 +0000 | [diff] [blame] | 300 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 301 | SkPDFUtils::EmitPath(path, &fContent); |
| 302 | SkPDFUtils::PaintPath(paint.getStyle(), path.getFillType(), &fContent); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void SkPDFDevice::drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 306 | const SkIRect* srcRect, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 307 | const SkMatrix& matrix, const SkPaint& paint) { |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 308 | SkMatrix transform = matrix; |
| 309 | transform.postConcat(fGraphicStack[fGraphicStackIndex].fTransform); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 310 | internalDrawBitmap(transform, bitmap, srcRect, paint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void SkPDFDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 314 | int x, int y, const SkPaint& paint) { |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 315 | SkMatrix matrix; |
reed@google.com | a6d59f6 | 2011-03-07 21:29:21 +0000 | [diff] [blame] | 316 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 317 | internalDrawBitmap(matrix, bitmap, NULL, paint); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 318 | } |
| 319 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 320 | 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] | 321 | SkScalar x, SkScalar y, const SkPaint& paint) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 322 | SkPaint textPaint = calculateTextPaint(paint); |
| 323 | updateGSFromPaint(textPaint, true); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 324 | |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 325 | // We want the text in glyph id encoding and a writable buffer, so we end |
| 326 | // up making a copy either way. |
| 327 | size_t numGlyphs = paint.textToGlyphs(text, len, NULL); |
| 328 | uint16_t* glyphIDs = |
| 329 | (uint16_t*)sk_malloc_flags(numGlyphs * 2, |
| 330 | SK_MALLOC_TEMP | SK_MALLOC_THROW); |
| 331 | SkAutoFree autoFreeGlyphIDs(glyphIDs); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 332 | if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 333 | paint.textToGlyphs(text, len, glyphIDs); |
| 334 | textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 335 | } else { |
| 336 | SkASSERT((len & 1) == 0); |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 337 | SkASSERT(len / 2 == numGlyphs); |
| 338 | memcpy(glyphIDs, text, len); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 339 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 340 | |
| 341 | SkScalar width; |
| 342 | SkScalar* widthPtr = NULL; |
| 343 | if (textPaint.isUnderlineText() || textPaint.isStrikeThruText()) |
| 344 | widthPtr = &width; |
| 345 | |
| 346 | SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 347 | alignText(glyphCacheProc, textPaint, glyphIDs, numGlyphs, &x, &y, widthPtr); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 348 | fContent.writeText("BT\n"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 349 | setTextTransform(x, y, textPaint.getTextSkewX()); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 350 | size_t consumedGlyphCount = 0; |
| 351 | while (numGlyphs > consumedGlyphCount) { |
| 352 | updateFont(textPaint, glyphIDs[consumedGlyphCount]); |
| 353 | SkPDFFont* font = fGraphicStack[fGraphicStackIndex].fFont; |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 354 | size_t availableGlyphs = |
| 355 | font->glyphsToPDFFontEncoding(glyphIDs + consumedGlyphCount, |
| 356 | numGlyphs - consumedGlyphCount); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 357 | SkString encodedString = |
| 358 | SkPDFString::formatString(glyphIDs + consumedGlyphCount, |
| 359 | availableGlyphs, font->multiByteGlyphs()); |
| 360 | fContent.writeText(encodedString.c_str()); |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 361 | consumedGlyphCount += availableGlyphs; |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 362 | fContent.writeText(" Tj\n"); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 363 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 364 | fContent.writeText("ET\n"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 365 | |
| 366 | // Draw underline and/or strikethrough if the paint has them. |
| 367 | // drawPosText() and drawTextOnPath() don't draw underline or strikethrough |
| 368 | // because the raster versions don't. Use paint instead of textPaint |
| 369 | // because we may have changed strokeWidth to do fakeBold text. |
| 370 | if (paint.isUnderlineText() || paint.isStrikeThruText()) { |
| 371 | SkScalar textSize = paint.getTextSize(); |
| 372 | SkScalar height = SkScalarMul(textSize, kStdUnderline_Thickness); |
| 373 | |
| 374 | if (paint.isUnderlineText()) { |
| 375 | SkScalar top = SkScalarMulAdd(textSize, kStdUnderline_Offset, y); |
| 376 | SkRect r = SkRect::MakeXYWH(x, top - height, width, height); |
| 377 | drawRect(d, r, paint); |
| 378 | } |
| 379 | if (paint.isStrikeThruText()) { |
| 380 | SkScalar top = SkScalarMulAdd(textSize, kStdStrikeThru_Offset, y); |
| 381 | SkRect r = SkRect::MakeXYWH(x, top - height, width, height); |
| 382 | drawRect(d, r, paint); |
| 383 | } |
| 384 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void SkPDFDevice::drawPosText(const SkDraw&, const void* text, size_t len, |
| 388 | const SkScalar pos[], SkScalar constY, |
| 389 | int scalarsPerPos, const SkPaint& paint) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 390 | SkASSERT(1 == scalarsPerPos || 2 == scalarsPerPos); |
| 391 | SkPaint textPaint = calculateTextPaint(paint); |
| 392 | updateGSFromPaint(textPaint, true); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 393 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 394 | // Make sure we have a glyph id encoding. |
| 395 | SkAutoFree glyphStorage; |
| 396 | uint16_t* glyphIDs; |
| 397 | size_t numGlyphs; |
| 398 | if (paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) { |
| 399 | numGlyphs = paint.textToGlyphs(text, len, NULL); |
| 400 | glyphIDs = (uint16_t*)sk_malloc_flags(numGlyphs * 2, |
| 401 | SK_MALLOC_TEMP | SK_MALLOC_THROW); |
| 402 | glyphStorage.set(glyphIDs); |
| 403 | paint.textToGlyphs(text, len, glyphIDs); |
| 404 | textPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 405 | } else { |
| 406 | SkASSERT((len & 1) == 0); |
| 407 | numGlyphs = len / 2; |
| 408 | glyphIDs = (uint16_t*)text; |
| 409 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 410 | |
| 411 | SkDrawCacheProc glyphCacheProc = textPaint.getDrawCacheProc(); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 412 | fContent.writeText("BT\n"); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 413 | updateFont(textPaint, glyphIDs[0]); |
| 414 | for (size_t i = 0; i < numGlyphs; i++) { |
| 415 | SkPDFFont* font = fGraphicStack[fGraphicStackIndex].fFont; |
vandebo@chromium.org | 0129410 | 2011-02-28 19:52:18 +0000 | [diff] [blame] | 416 | uint16_t encodedValue = glyphIDs[i]; |
| 417 | if (font->glyphsToPDFFontEncoding(&encodedValue, 1) != 1) { |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 418 | updateFont(textPaint, glyphIDs[i]); |
| 419 | i--; |
| 420 | continue; |
| 421 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 422 | SkScalar x = pos[i * scalarsPerPos]; |
| 423 | SkScalar y = scalarsPerPos == 1 ? constY : pos[i * scalarsPerPos + 1]; |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 424 | alignText(glyphCacheProc, textPaint, glyphIDs + i, 1, &x, &y, NULL); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 425 | setTextTransform(x, y, textPaint.getTextSkewX()); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 426 | SkString encodedString = |
| 427 | SkPDFString::formatString(&encodedValue, 1, |
| 428 | font->multiByteGlyphs()); |
| 429 | fContent.writeText(encodedString.c_str()); |
| 430 | fContent.writeText(" Tj\n"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 431 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 432 | fContent.writeText("ET\n"); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void SkPDFDevice::drawTextOnPath(const SkDraw&, const void* text, size_t len, |
| 436 | const SkPath& path, const SkMatrix* matrix, |
| 437 | const SkPaint& paint) { |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 438 | NOT_IMPLEMENTED("drawTextOnPath", true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void SkPDFDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode, |
| 442 | int vertexCount, const SkPoint verts[], |
| 443 | const SkPoint texs[], const SkColor colors[], |
| 444 | SkXfermode* xmode, const uint16_t indices[], |
| 445 | int indexCount, const SkPaint& paint) { |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 446 | NOT_IMPLEMENTED("drawVerticies", true); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 447 | } |
| 448 | |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 449 | void SkPDFDevice::drawDevice(const SkDraw& d, SkDevice* device, int x, int y, |
| 450 | const SkPaint& paint) { |
| 451 | if ((device->getDeviceCapabilities() & kVector_Capability) == 0) { |
| 452 | // If we somehow get a raster device, do what our parent would do. |
| 453 | SkDevice::drawDevice(d, device, x, y, paint); |
| 454 | return; |
| 455 | } |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 456 | // Assume that a vector capable device means that it's a PDF Device. |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 457 | SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device); |
| 458 | |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 459 | SkMatrix matrix; |
reed@google.com | a6d59f6 | 2011-03-07 21:29:21 +0000 | [diff] [blame] | 460 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 461 | SkMatrix curTransform = setTransform(matrix); |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 462 | updateGSFromPaint(paint, false); |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 463 | |
| 464 | SkPDFFormXObject* xobject = new SkPDFFormXObject(pdfDevice); |
vandebo@chromium.org | eb6c759 | 2010-10-26 19:54:45 +0000 | [diff] [blame] | 465 | fXObjectResources.push(xobject); // Transfer reference. |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 466 | fContent.writeText("/X"); |
| 467 | fContent.writeDecAsText(fXObjectResources.count() - 1); |
| 468 | fContent.writeText(" Do\n"); |
vandebo@chromium.org | 1aef2ed | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 469 | setTransform(curTransform); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | const SkRefPtr<SkPDFDict>& SkPDFDevice::getResourceDict() { |
| 473 | if (fResourceDict.get() == NULL) { |
| 474 | fResourceDict = new SkPDFDict; |
| 475 | fResourceDict->unref(); // SkRefPtr and new both took a reference. |
| 476 | |
| 477 | if (fGraphicStateResources.count()) { |
| 478 | SkRefPtr<SkPDFDict> extGState = new SkPDFDict(); |
| 479 | extGState->unref(); // SkRefPtr and new both took a reference. |
| 480 | for (int i = 0; i < fGraphicStateResources.count(); i++) { |
| 481 | SkString nameString("G"); |
| 482 | nameString.appendS32(i); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 483 | extGState->insert( |
| 484 | nameString.c_str(), |
| 485 | new SkPDFObjRef(fGraphicStateResources[i]))->unref(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 486 | } |
| 487 | fResourceDict->insert("ExtGState", extGState.get()); |
| 488 | } |
| 489 | |
| 490 | if (fXObjectResources.count()) { |
| 491 | SkRefPtr<SkPDFDict> xObjects = new SkPDFDict(); |
| 492 | xObjects->unref(); // SkRefPtr and new both took a reference. |
| 493 | for (int i = 0; i < fXObjectResources.count(); i++) { |
| 494 | SkString nameString("X"); |
| 495 | nameString.appendS32(i); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 496 | xObjects->insert( |
| 497 | nameString.c_str(), |
| 498 | new SkPDFObjRef(fXObjectResources[i]))->unref(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 499 | } |
| 500 | fResourceDict->insert("XObject", xObjects.get()); |
| 501 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 502 | |
| 503 | if (fFontResources.count()) { |
| 504 | SkRefPtr<SkPDFDict> fonts = new SkPDFDict(); |
| 505 | fonts->unref(); // SkRefPtr and new both took a reference. |
| 506 | for (int i = 0; i < fFontResources.count(); i++) { |
| 507 | SkString nameString("F"); |
| 508 | nameString.appendS32(i); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 509 | fonts->insert(nameString.c_str(), |
| 510 | new SkPDFObjRef(fFontResources[i]))->unref(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 511 | } |
| 512 | fResourceDict->insert("Font", fonts.get()); |
| 513 | } |
| 514 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 515 | if (fShaderResources.count()) { |
| 516 | SkRefPtr<SkPDFDict> patterns = new SkPDFDict(); |
| 517 | patterns->unref(); // SkRefPtr and new both took a reference. |
| 518 | for (int i = 0; i < fShaderResources.count(); i++) { |
| 519 | SkString nameString("P"); |
| 520 | nameString.appendS32(i); |
| 521 | patterns->insert(nameString.c_str(), |
| 522 | new SkPDFObjRef(fShaderResources[i]))->unref(); |
| 523 | } |
| 524 | fResourceDict->insert("Pattern", patterns.get()); |
| 525 | } |
| 526 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 527 | // For compatibility, add all proc sets (only used for output to PS |
| 528 | // devices). |
| 529 | const char procs[][7] = {"PDF", "Text", "ImageB", "ImageC", "ImageI"}; |
| 530 | SkRefPtr<SkPDFArray> procSets = new SkPDFArray(); |
| 531 | procSets->unref(); // SkRefPtr and new both took a reference. |
| 532 | procSets->reserve(SK_ARRAY_COUNT(procs)); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 533 | for (size_t i = 0; i < SK_ARRAY_COUNT(procs); i++) |
| 534 | procSets->append(new SkPDFName(procs[i]))->unref(); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 535 | fResourceDict->insert("ProcSet", procSets.get()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 536 | } |
| 537 | return fResourceDict; |
| 538 | } |
| 539 | |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 540 | void SkPDFDevice::getResources(SkTDArray<SkPDFObject*>* resourceList) const { |
| 541 | resourceList->setReserve(resourceList->count() + |
| 542 | fGraphicStateResources.count() + |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 543 | fXObjectResources.count() + |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 544 | fFontResources.count() + |
| 545 | fShaderResources.count()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 546 | for (int i = 0; i < fGraphicStateResources.count(); i++) { |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 547 | resourceList->push(fGraphicStateResources[i]); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 548 | fGraphicStateResources[i]->ref(); |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 549 | fGraphicStateResources[i]->getResources(resourceList); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 550 | } |
| 551 | for (int i = 0; i < fXObjectResources.count(); i++) { |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 552 | resourceList->push(fXObjectResources[i]); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 553 | fXObjectResources[i]->ref(); |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 554 | fXObjectResources[i]->getResources(resourceList); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 555 | } |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 556 | for (int i = 0; i < fFontResources.count(); i++) { |
| 557 | resourceList->push(fFontResources[i]); |
| 558 | fFontResources[i]->ref(); |
| 559 | fFontResources[i]->getResources(resourceList); |
| 560 | } |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 561 | for (int i = 0; i < fShaderResources.count(); i++) { |
| 562 | resourceList->push(fShaderResources[i]); |
| 563 | fShaderResources[i]->ref(); |
| 564 | fShaderResources[i]->getResources(resourceList); |
| 565 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 566 | } |
| 567 | |
vandebo@chromium.org | a518086 | 2010-10-26 19:48:49 +0000 | [diff] [blame] | 568 | SkRefPtr<SkPDFArray> SkPDFDevice::getMediaBox() const { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 569 | SkRefPtr<SkPDFInt> zero = new SkPDFInt(0); |
| 570 | zero->unref(); // SkRefPtr and new both took a reference. |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 571 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 572 | SkRefPtr<SkPDFArray> mediaBox = new SkPDFArray(); |
| 573 | mediaBox->unref(); // SkRefPtr and new both took a reference. |
| 574 | mediaBox->reserve(4); |
| 575 | mediaBox->append(zero.get()); |
| 576 | mediaBox->append(zero.get()); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 577 | mediaBox->append(new SkPDFInt(fWidth))->unref(); |
| 578 | mediaBox->append(new SkPDFInt(fHeight))->unref(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 579 | return mediaBox; |
| 580 | } |
| 581 | |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 582 | SkStream* SkPDFDevice::content() const { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 583 | size_t offset = fContent.getOffset(); |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 584 | char* data = (char*)sk_malloc_throw(offset + fGraphicStackIndex * 2); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 585 | fContent.copyTo(data); |
vandebo@chromium.org | c2a9b7f | 2011-02-24 23:22:30 +0000 | [diff] [blame] | 586 | for (int i = 0; i < fGraphicStackIndex; i++) { |
| 587 | data[offset++] = 'Q'; |
| 588 | data[offset++] = '\n'; |
| 589 | } |
| 590 | SkMemoryStream* result = new SkMemoryStream; |
| 591 | result->setMemoryOwned(data, offset); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 592 | return result; |
| 593 | } |
| 594 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 595 | void SkPDFDevice::updateGSFromPaint(const SkPaint& paint, bool forText) { |
| 596 | SkASSERT(paint.getPathEffect() == NULL); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 597 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 598 | NOT_IMPLEMENTED(paint.getMaskFilter() != NULL, false); |
| 599 | NOT_IMPLEMENTED(paint.getColorFilter() != NULL, false); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 600 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 601 | SkPaint newPaint = paint; |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 602 | |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 603 | // PDF treats a shader as a color, so we only set one or the other. |
| 604 | SkRefPtr<SkPDFShader> pdfShader; |
| 605 | const SkShader* shader = newPaint.getShader(); |
| 606 | if (shader) { |
| 607 | // PDF positions patterns relative to the initial transform, so |
| 608 | // we need to apply the current transform to the shader parameters. |
| 609 | SkMatrix transform = fGraphicStack[fGraphicStackIndex].fTransform; |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 610 | transform.postConcat(fInitialTransform); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 611 | |
| 612 | // PDF doesn't support kClamp_TileMode, so we simulate it by making |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 613 | // a pattern the size of the drawing surface. |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 614 | SkIRect bounds = fGraphicStack[fGraphicStackIndex].fClip.getBounds(); |
| 615 | pdfShader = SkPDFShader::getPDFShader(*shader, transform, bounds); |
| 616 | SkSafeUnref(pdfShader.get()); // getShader and SkRefPtr both took a ref |
| 617 | |
| 618 | // A color shader is treated as an invalid shader so we don't have |
| 619 | // to set a shader just for a color. |
| 620 | if (pdfShader.get() == NULL) { |
| 621 | newPaint.setColor(0); |
| 622 | |
| 623 | // Check for a color shader. |
| 624 | SkShader::GradientInfo gradientInfo; |
| 625 | SkColor gradientColor; |
| 626 | gradientInfo.fColors = &gradientColor; |
| 627 | gradientInfo.fColorOffsets = NULL; |
| 628 | gradientInfo.fColorCount = 1; |
| 629 | if (shader->asAGradient(&gradientInfo) == |
| 630 | SkShader::kColor_GradientType) { |
| 631 | newPaint.setColor(gradientColor); |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | if (pdfShader) { |
| 637 | // pdfShader has been canonicalized so we can directly compare |
| 638 | // pointers. |
| 639 | if (fGraphicStack[fGraphicStackIndex].fShader != pdfShader.get()) { |
| 640 | int resourceIndex = fShaderResources.find(pdfShader.get()); |
| 641 | if (resourceIndex < 0) { |
| 642 | resourceIndex = fShaderResources.count(); |
| 643 | fShaderResources.push(pdfShader.get()); |
| 644 | pdfShader->ref(); |
| 645 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 646 | fContent.writeText("/Pattern CS /Pattern cs /P"); |
| 647 | fContent.writeDecAsText(resourceIndex); |
| 648 | fContent.writeText(" SCN /P"); |
| 649 | fContent.writeDecAsText(resourceIndex); |
| 650 | fContent.writeText(" scn\n"); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 651 | fGraphicStack[fGraphicStackIndex].fShader = pdfShader.get(); |
| 652 | } |
| 653 | } else { |
| 654 | SkColor newColor = newPaint.getColor(); |
| 655 | newColor = SkColorSetA(newColor, 0xFF); |
| 656 | if (fGraphicStack[fGraphicStackIndex].fShader || |
| 657 | fGraphicStack[fGraphicStackIndex].fColor != newColor) { |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 658 | emitPDFColor(newColor, &fContent); |
| 659 | fContent.writeText("RG "); |
| 660 | emitPDFColor(newColor, &fContent); |
| 661 | fContent.writeText("rg\n"); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 662 | fGraphicStack[fGraphicStackIndex].fColor = newColor; |
| 663 | fGraphicStack[fGraphicStackIndex].fShader = NULL; |
| 664 | } |
| 665 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 666 | |
| 667 | SkRefPtr<SkPDFGraphicState> newGraphicState = |
| 668 | SkPDFGraphicState::getGraphicStateForPaint(newPaint); |
| 669 | newGraphicState->unref(); // getGraphicState and SkRefPtr both took a ref. |
| 670 | // newGraphicState has been canonicalized so we can directly compare |
| 671 | // pointers. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 672 | if (fGraphicStack[fGraphicStackIndex].fGraphicState != |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 673 | newGraphicState.get()) { |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 674 | int resourceIndex = fGraphicStateResources.find(newGraphicState.get()); |
| 675 | if (resourceIndex < 0) { |
| 676 | resourceIndex = fGraphicStateResources.count(); |
| 677 | fGraphicStateResources.push(newGraphicState.get()); |
| 678 | newGraphicState->ref(); |
| 679 | } |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 680 | fContent.writeText("/G"); |
| 681 | fContent.writeDecAsText(resourceIndex); |
| 682 | fContent.writeText(" gs\n"); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 683 | fGraphicStack[fGraphicStackIndex].fGraphicState = newGraphicState.get(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 684 | } |
| 685 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 686 | if (forText) { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 687 | if (fGraphicStack[fGraphicStackIndex].fTextScaleX != |
| 688 | newPaint.getTextScaleX()) { |
| 689 | SkScalar scale = newPaint.getTextScaleX(); |
| 690 | SkScalar pdfScale = SkScalarMul(scale, SkIntToScalar(100)); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 691 | SkPDFScalar::Append(pdfScale, &fContent); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 692 | fContent.writeText(" Tz\n"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 693 | fGraphicStack[fGraphicStackIndex].fTextScaleX = scale; |
| 694 | } |
| 695 | |
| 696 | if (fGraphicStack[fGraphicStackIndex].fTextFill != |
| 697 | newPaint.getStyle()) { |
| 698 | SK_COMPILE_ASSERT(SkPaint::kFill_Style == 0, enum_must_match_value); |
| 699 | SK_COMPILE_ASSERT(SkPaint::kStroke_Style == 1, |
| 700 | enum_must_match_value); |
| 701 | SK_COMPILE_ASSERT(SkPaint::kStrokeAndFill_Style == 2, |
| 702 | enum_must_match_value); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 703 | fContent.writeDecAsText(newPaint.getStyle()); |
| 704 | fContent.writeText(" Tr\n"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 705 | fGraphicStack[fGraphicStackIndex].fTextFill = newPaint.getStyle(); |
| 706 | } |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 710 | void SkPDFDevice::updateFont(const SkPaint& paint, uint16_t glyphID) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 711 | SkTypeface* typeface = paint.getTypeface(); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 712 | if (fGraphicStack[fGraphicStackIndex].fTextSize != paint.getTextSize() || |
| 713 | fGraphicStack[fGraphicStackIndex].fFont == NULL || |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 714 | fGraphicStack[fGraphicStackIndex].fFont->typeface() != typeface || |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 715 | !fGraphicStack[fGraphicStackIndex].fFont->hasGlyph(glyphID)) { |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 716 | int fontIndex = getFontResourceIndex(typeface, glyphID); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 717 | fContent.writeText("/F"); |
| 718 | fContent.writeDecAsText(fontIndex); |
| 719 | fContent.writeText(" "); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 720 | SkPDFScalar::Append(paint.getTextSize(), &fContent); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 721 | fContent.writeText(" Tf\n"); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 722 | fGraphicStack[fGraphicStackIndex].fTextSize = paint.getTextSize(); |
| 723 | fGraphicStack[fGraphicStackIndex].fFont = fFontResources[fontIndex]; |
| 724 | } |
| 725 | } |
| 726 | |
ctguil@chromium.org | 9db86bb | 2011-03-04 21:43:27 +0000 | [diff] [blame] | 727 | int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) { |
| 728 | SkRefPtr<SkPDFFont> newFont = SkPDFFont::getFontResource(typeface, glyphID); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 729 | newFont->unref(); // getFontResource and SkRefPtr both took a ref. |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 730 | int resourceIndex = fFontResources.find(newFont.get()); |
| 731 | if (resourceIndex < 0) { |
| 732 | resourceIndex = fFontResources.count(); |
| 733 | fFontResources.push(newFont.get()); |
| 734 | newFont->ref(); |
| 735 | } |
| 736 | return resourceIndex; |
| 737 | } |
| 738 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 739 | void SkPDFDevice::pushGS() { |
| 740 | SkASSERT(fGraphicStackIndex < 2); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 741 | fContent.writeText("q\n"); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 742 | fGraphicStackIndex++; |
| 743 | fGraphicStack[fGraphicStackIndex] = fGraphicStack[fGraphicStackIndex - 1]; |
| 744 | } |
| 745 | |
| 746 | void SkPDFDevice::popGS() { |
| 747 | SkASSERT(fGraphicStackIndex > 0); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 748 | fContent.writeText("Q\n"); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 749 | fGraphicStackIndex--; |
| 750 | } |
| 751 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 752 | void SkPDFDevice::setTextTransform(SkScalar x, SkScalar y, SkScalar textSkewX) { |
| 753 | // Flip the text about the x-axis to account for origin swap and include |
| 754 | // the passed parameters. |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 755 | fContent.writeText("1 0 "); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 756 | SkPDFScalar::Append(0 - textSkewX, &fContent); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 757 | fContent.writeText(" -1 "); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 758 | SkPDFScalar::Append(x, &fContent); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 759 | fContent.writeText(" "); |
vandebo@chromium.org | 094316b | 2011-03-04 03:15:13 +0000 | [diff] [blame] | 760 | SkPDFScalar::Append(y, &fContent); |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 761 | fContent.writeText(" Tm\n"); |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 762 | } |
| 763 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 764 | void SkPDFDevice::internalDrawBitmap(const SkMatrix& matrix, |
| 765 | const SkBitmap& bitmap, |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 766 | const SkIRect* srcRect, |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 767 | const SkPaint& paint) { |
vandebo@chromium.org | befebb8 | 2011-01-29 01:38:50 +0000 | [diff] [blame] | 768 | SkIRect subset = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 769 | if (srcRect && !subset.intersect(*srcRect)) |
| 770 | return; |
| 771 | |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 772 | SkPDFImage* image = SkPDFImage::CreateImage(bitmap, subset, paint); |
| 773 | if (!image) |
| 774 | return; |
| 775 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 776 | SkMatrix scaled; |
| 777 | // Adjust for origin flip. |
| 778 | scaled.setScale(1, -1); |
| 779 | scaled.postTranslate(0, 1); |
| 780 | // Scale the image up from 1x1 to WxH. |
reed@google.com | a6d59f6 | 2011-03-07 21:29:21 +0000 | [diff] [blame] | 781 | scaled.postScale(SkIntToScalar(subset.width()), |
| 782 | SkIntToScalar(subset.height())); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 783 | scaled.postConcat(matrix); |
| 784 | SkMatrix curTransform = setTransform(scaled); |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 785 | updateGSFromPaint(paint, false); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 786 | |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 787 | fXObjectResources.push(image); // Transfer reference. |
vandebo@chromium.org | cae5fba | 2011-03-28 19:03:50 +0000 | [diff] [blame] | 788 | fContent.writeText("/X"); |
| 789 | fContent.writeDecAsText(fXObjectResources.count() - 1); |
| 790 | fContent.writeText(" Do\n"); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 791 | setTransform(curTransform); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 792 | } |
| 793 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 794 | SkMatrix SkPDFDevice::setTransform(const SkMatrix& m) { |
| 795 | SkMatrix old = fGraphicStack[fGraphicStackIndex].fTransform; |
| 796 | if (old == m) |
| 797 | return old; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 798 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 799 | if (old.getType() != SkMatrix::kIdentity_Mask) { |
| 800 | SkASSERT(fGraphicStackIndex > 0); |
| 801 | SkASSERT(fGraphicStack[fGraphicStackIndex - 1].fTransform.getType() == |
| 802 | SkMatrix::kIdentity_Mask); |
| 803 | SkASSERT(fGraphicStack[fGraphicStackIndex].fClip == |
| 804 | fGraphicStack[fGraphicStackIndex - 1].fClip); |
| 805 | popGS(); |
| 806 | } |
| 807 | if (m.getType() == SkMatrix::kIdentity_Mask) |
| 808 | return old; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 809 | |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 810 | if (fGraphicStackIndex == 0 || fGraphicStack[fGraphicStackIndex].fClip != |
| 811 | fGraphicStack[fGraphicStackIndex - 1].fClip) |
| 812 | pushGS(); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 813 | |
vandebo@chromium.org | 75f97e4 | 2011-04-11 23:24:18 +0000 | [diff] [blame^] | 814 | SkPDFUtils::AppendTransform(m, &fContent); |
vandebo@chromium.org | 7e2ff7c | 2010-11-03 23:55:28 +0000 | [diff] [blame] | 815 | fGraphicStack[fGraphicStackIndex].fTransform = m; |
| 816 | |
| 817 | return old; |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 818 | } |