Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2013 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "SkPDFDeviceFlattener.h" | |
| 9 | |
| 10 #include "SkDraw.h" | |
| 11 | |
| 12 static SkISize SkSizeToISize(const SkSize& size) { | |
| 13 return SkISize::Make(SkScalarRoundToInt(size.width()), SkScalarRoundToInt(si ze.height())); | |
| 14 } | |
| 15 | |
| 16 SkPDFDeviceFlattener::SkPDFDeviceFlattener(const SkSize& pageSize, const SkRect* trimBox) | |
| 17 : SkPDFDevice(SkSizeToISize(pageSize), | |
| 18 SkSizeToISize(pageSize), | |
| 19 SkMatrix::I()) { | |
| 20 // TODO(edisonn): store the trimbox on emit. | |
| 21 } | |
| 22 | |
| 23 SkPDFDeviceFlattener::~SkPDFDeviceFlattener() { | |
| 24 } | |
| 25 | |
| 26 void SkPDFDeviceFlattener::drawPaint(const SkDraw& d, const SkPaint& paint) { | |
|
vandebo (ex-Chrome)
2013/10/08 23:37:06
Omit virtual methods that just call the parent.
| |
| 27 INHERITED::drawPaint(d, paint); | |
| 28 } | |
| 29 | |
| 30 void SkPDFDeviceFlattener::drawPoints(const SkDraw& d, SkCanvas::PointMode mode, | |
| 31 size_t count, const SkPoint points[], | |
| 32 const SkPaint& paint) { | |
| 33 if (mustFlatten(d)) { | |
|
vandebo (ex-Chrome)
2013/10/08 23:37:06
consider using this pattern instead:
if(!mustFlat
edisonn
2013/10/09 13:25:55
Done.
| |
| 34 SkPoint* flattenedPoints = SkNEW_ARRAY(SkPoint, count); | |
| 35 d.fMatrix->mapPoints(flattenedPoints, points, count); | |
| 36 SkDraw draw(d); | |
| 37 SkMatrix identity = SkMatrix::I(); | |
| 38 draw.fMatrix = &identity; | |
| 39 INHERITED::drawPoints(draw, mode, count, flattenedPoints, paint); | |
| 40 SkDELETE_ARRAY(flattenedPoints); | |
| 41 return; | |
| 42 } | |
| 43 | |
| 44 INHERITED::drawPoints(d, mode, count, points, paint); | |
| 45 } | |
| 46 | |
| 47 void SkPDFDeviceFlattener::drawRect(const SkDraw& d, const SkRect& r, const SkPa int& paint) { | |
| 48 if (mustFlatten(d)) { | |
| 49 SkPath path; | |
| 50 path.addRect(r); | |
| 51 path.transform(*d.fMatrix); | |
| 52 SkDraw draw(d); | |
| 53 SkMatrix matrix = SkMatrix::I(); | |
| 54 draw.fMatrix = &matrix; | |
| 55 | |
| 56 SkPaint paintFlatten = paint; | |
| 57 if (paintFlatten.getShader()) { | |
| 58 SkMatrix local = paintFlatten.getShader()->getLocalMatrix(); | |
| 59 local.preConcat(*d.fMatrix); | |
| 60 paintFlatten.getShader()->setLocalMatrix(local); | |
| 61 } | |
| 62 | |
| 63 INHERITED::drawPath(draw, path, paintFlatten, NULL, true); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 INHERITED::drawRect(d, r, paint); | |
| 68 } | |
| 69 | |
| 70 void SkPDFDeviceFlattener::drawPath(const SkDraw& d, const SkPath& origpath, | |
| 71 const SkPaint& paint, const SkMatrix* prePat hMatrix, | |
| 72 bool pathIsMutable) { | |
| 73 if (mustFlatten(d) || (prePathMatrix && prePathMatrix->hasPerspective())) { | |
| 74 SkPath path; | |
| 75 path.addPath(origpath); | |
| 76 if (prePathMatrix) { | |
| 77 path.transform(*prePathMatrix); | |
| 78 } | |
| 79 path.transform(*d.fMatrix); | |
| 80 SkDraw draw(d); | |
| 81 SkMatrix matrix = SkMatrix::I(); | |
| 82 draw.fMatrix = &matrix; | |
| 83 | |
| 84 SkPaint paintFlatten = paint; | |
| 85 if (paintFlatten.getShader()) { | |
| 86 SkMatrix local = *d.fMatrix; | |
| 87 local.preConcat(paintFlatten.getShader()->getLocalMatrix()); | |
| 88 | |
| 89 paintFlatten.getShader()->setLocalMatrix(local); | |
| 90 } | |
| 91 | |
| 92 INHERITED::drawPath(draw, path, paintFlatten, NULL, true); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 INHERITED::drawPath(d, origpath, paint, prePathMatrix, pathIsMutable); | |
| 97 } | |
| 98 | |
| 99 void SkPDFDeviceFlattener::drawText(const SkDraw& d, const void* text, size_t le n, | |
| 100 SkScalar x, SkScalar y, const SkPaint& paint ) { | |
| 101 if (mustPathText(d, paint)) { | |
| 102 d.drawText_asPaths((const char*)text, len, x, y, paint); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 INHERITED::drawText(d, text, len, x, y, paint); | |
| 107 } | |
| 108 | |
| 109 void SkPDFDeviceFlattener::drawPosText(const SkDraw& d, const void* text, size_t len, | |
| 110 const SkScalar pos[], SkScalar constY, | |
| 111 int scalarsPerPos, const SkPaint& paint) { | |
| 112 if (mustPathText(d, paint)) { | |
| 113 d.drawPosText_asPaths((const char*)text, len, pos, constY, scalarsPerPos , paint); | |
| 114 return; | |
| 115 } | |
| 116 INHERITED::drawPosText(d, text, len,pos, constY,scalarsPerPos, paint); | |
| 117 } | |
| 118 | |
| 119 void SkPDFDeviceFlattener::drawTextOnPath(const SkDraw& d, const void* text, siz e_t len, | |
| 120 const SkPath& path, const SkMatrix* ma trix, | |
| 121 const SkPaint& paint) { | |
| 122 if (mustPathText(d, paint) || (matrix && matrix->hasPerspective())) { | |
| 123 d.drawTextOnPath((const char*)text, len, path, matrix, paint); | |
| 124 return; | |
| 125 } | |
| 126 INHERITED::drawTextOnPath(d, text, len, path, matrix, paint); | |
| 127 } | |
| 128 | |
| 129 void SkPDFDeviceFlattener::drawVertices(const SkDraw& d, SkCanvas::VertexMode mo de, | |
| 130 int vertexCount, const SkPoint verts[], | |
| 131 const SkPoint texs[], const SkColor colo rs[], | |
| 132 SkXfermode* xmode, const uint16_t indice s[], | |
| 133 int indexCount, const SkPaint& paint) { | |
| 134 INHERITED::drawVertices(d, mode, vertexCount, verts, texs, colors, xmode, in dices, indexCount, | |
| 135 paint); | |
| 136 } | |
| 137 | |
| 138 void SkPDFDeviceFlattener::drawDevice(const SkDraw& d, SkBaseDevice* dev, int x, int y, | |
| 139 const SkPaint& paint) { | |
| 140 INHERITED::drawDevice(d, dev, x, y, paint); | |
| 141 } | |
| 142 | |
| 143 bool SkPDFDeviceFlattener::mustFlatten(const SkDraw& d) const { | |
| 144 // TODO(edisonn): testability, add flag to force return true. | |
| 145 return d.fMatrix->hasPerspective(); | |
| 146 } | |
| 147 | |
| 148 bool SkPDFDeviceFlattener::mustPathText(const SkDraw& d, const SkPaint&) { | |
| 149 // TODO(edisonn): testability, add flag to force return true. | |
| 150 // TODO(edisonn): TBD: How to flatten MaskFilter. | |
| 151 return d.fMatrix->hasPerspective(); | |
| 152 } | |
| OLD | NEW |