Chromium Code Reviews| Index: src/pdf/SkPDFDeviceFlattener.cpp | 
| diff --git a/src/pdf/SkPDFDeviceFlattener.cpp b/src/pdf/SkPDFDeviceFlattener.cpp | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..85c93a1893f5bdeebc66546c6f3673dfbea4ea45 | 
| --- /dev/null | 
| +++ b/src/pdf/SkPDFDeviceFlattener.cpp | 
| @@ -0,0 +1,134 @@ | 
| +/* | 
| + * Copyright 2013 Google Inc. | 
| + * | 
| + * Use of this source code is governed by a BSD-style license that can be | 
| + * found in the LICENSE file. | 
| + */ | 
| + | 
| +#include "SkPDFDeviceFlattener.h" | 
| + | 
| +#include "SkDraw.h" | 
| + | 
| +static SkISize SkSizeToISize(const SkSize& size) { | 
| + return SkISize::Make(SkScalarRoundToInt(size.width()), SkScalarRoundToInt(size.height())); | 
| +} | 
| + | 
| +SkPDFDeviceFlattener::SkPDFDeviceFlattener(const SkSize& pageSize, const SkRect* trimBox) | 
| + : SkPDFDevice(SkSizeToISize(pageSize), | 
| + SkSizeToISize(pageSize), | 
| + SkMatrix::I()) { | 
| + // TODO(edisonn): store the trimbox on emit. | 
| +} | 
| + | 
| +SkPDFDeviceFlattener::~SkPDFDeviceFlattener() { | 
| +} | 
| + | 
| +void SkPDFDeviceFlattener::drawPoints(const SkDraw& d, SkCanvas::PointMode mode, | 
| + size_t count, const SkPoint points[], | 
| + const SkPaint& paint) { | 
| + if (!mustFlatten(d)) { | 
| + INHERITED::drawPoints(d, mode, count, points, paint); | 
| + return; | 
| + } | 
| + | 
| 
 
vandebo (ex-Chrome)
2013/10/09 16:52:32
I think shaders can affect drawPoints too... lines
 
edisonn
2013/10/10 18:35:15
Done.
 
 | 
| + SkPoint* flattenedPoints = SkNEW_ARRAY(SkPoint, count); | 
| + d.fMatrix->mapPoints(flattenedPoints, points, count); | 
| + SkDraw draw(d); | 
| + SkMatrix identity = SkMatrix::I(); | 
| + draw.fMatrix = &identity; | 
| + INHERITED::drawPoints(draw, mode, count, flattenedPoints, paint); | 
| + SkDELETE_ARRAY(flattenedPoints); | 
| +} | 
| + | 
| +void SkPDFDeviceFlattener::drawRect(const SkDraw& d, const SkRect& r, const SkPaint& paint) { | 
| + if (!mustFlatten(d)) { | 
| + INHERITED::drawRect(d, r, paint); | 
| + return; | 
| + } | 
| + | 
| + SkPath path; | 
| + path.addRect(r); | 
| + path.transform(*d.fMatrix); | 
| + SkDraw draw(d); | 
| + SkMatrix matrix = SkMatrix::I(); | 
| + draw.fMatrix = &matrix; | 
| + | 
| + SkPaint paintFlatten = paint; | 
| + if (paintFlatten.getShader()) { | 
| 
 
vandebo (ex-Chrome)
2013/10/09 16:52:32
Consider pulling this block of code into a functio
 
edisonn
2013/10/10 18:35:15
Done.
 
 | 
| + SkMatrix local = paintFlatten.getShader()->getLocalMatrix(); | 
| + local.preConcat(*d.fMatrix); | 
| + paintFlatten.getShader()->setLocalMatrix(local); | 
| + } | 
| + | 
| + INHERITED::drawPath(draw, path, paintFlatten, NULL, true); | 
| +} | 
| + | 
| +void SkPDFDeviceFlattener::drawPath(const SkDraw& d, const SkPath& origpath, | 
| + const SkPaint& paint, const SkMatrix* prePathMatrix, | 
| + bool pathIsMutable) { | 
| + if (!mustFlatten(d) && !(prePathMatrix && prePathMatrix->hasPerspective())) { | 
| + INHERITED::drawPath(d, origpath, paint, prePathMatrix, pathIsMutable); | 
| + return; | 
| + } | 
| + | 
| + SkPath path; | 
| + path.addPath(origpath); | 
| 
 
reed1
2013/10/09 17:03:54
if your intention is to always make a copy of the
 
edisonn
2013/10/10 18:35:15
Done - using pathIsMutable
 
 | 
| + if (prePathMatrix) { | 
| + path.transform(*prePathMatrix); | 
| 
 
vandebo (ex-Chrome)
2013/10/09 16:52:32
The comment in SkDevice.h above drawPath implies t
 
reed1
2013/10/09 17:03:54
Before you can call transform (on line 79) you hav
 
edisonn
2013/10/10 18:35:15
Done.
 
 | 
| + } | 
| + path.transform(*d.fMatrix); | 
| + SkDraw draw(d); | 
| + SkMatrix matrix = SkMatrix::I(); | 
| + draw.fMatrix = &matrix; | 
| + | 
| + SkPaint paintFlatten = paint; | 
| + if (paintFlatten.getShader()) { | 
| + SkMatrix local = *d.fMatrix; | 
| + local.preConcat(paintFlatten.getShader()->getLocalMatrix()); | 
| + | 
| + paintFlatten.getShader()->setLocalMatrix(local); | 
| + } | 
| + | 
| + INHERITED::drawPath(draw, path, paintFlatten, NULL, true); | 
| +} | 
| + | 
| +void SkPDFDeviceFlattener::drawText(const SkDraw& d, const void* text, size_t len, | 
| + SkScalar x, SkScalar y, const SkPaint& paint) { | 
| + if (mustPathText(d, paint)) { | 
| + d.drawText_asPaths((const char*)text, len, x, y, paint); | 
| + return; | 
| + } | 
| + | 
| + INHERITED::drawText(d, text, len, x, y, paint); | 
| +} | 
| + | 
| +void SkPDFDeviceFlattener::drawPosText(const SkDraw& d, const void* text, size_t len, | 
| + const SkScalar pos[], SkScalar constY, | 
| + int scalarsPerPos, const SkPaint& paint) { | 
| + if (mustPathText(d, paint)) { | 
| + d.drawPosText_asPaths((const char*)text, len, pos, constY, scalarsPerPos, paint); | 
| + return; | 
| + } | 
| + INHERITED::drawPosText(d, text, len,pos, constY,scalarsPerPos, paint); | 
| +} | 
| + | 
| +void SkPDFDeviceFlattener::drawTextOnPath(const SkDraw& d, const void* text, size_t len, | 
| + const SkPath& path, const SkMatrix* matrix, | 
| + const SkPaint& paint) { | 
| + if (mustPathText(d, paint) || (matrix && matrix->hasPerspective())) { | 
| + d.drawTextOnPath((const char*)text, len, path, matrix, paint); | 
| + return; | 
| + } | 
| + INHERITED::drawTextOnPath(d, text, len, path, matrix, paint); | 
| +} | 
| + | 
| +bool SkPDFDeviceFlattener::mustFlatten(const SkDraw& d) const { | 
| + // TODO(edisonn): testability, add flag to force return true. | 
| + return d.fMatrix->hasPerspective(); | 
| +} | 
| + | 
| +bool SkPDFDeviceFlattener::mustPathText(const SkDraw& d, const SkPaint&) { | 
| + // TODO(edisonn): testability, add flag to force return true. | 
| + // TODO(edisonn): TBD: How to flatten MaskFilter. | 
| + return d.fMatrix->hasPerspective(); | 
| +} |