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..30ea71b8c797711a4e7dfce563b4073afd6f3d2e |
| --- /dev/null |
| +++ b/src/pdf/SkPDFDeviceFlattener.cpp |
| @@ -0,0 +1,152 @@ |
| +/* |
| + * 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::drawPaint(const SkDraw& d, const SkPaint& paint) { |
|
vandebo (ex-Chrome)
2013/10/08 23:37:06
Omit virtual methods that just call the parent.
|
| + INHERITED::drawPaint(d, paint); |
| +} |
| + |
| +void SkPDFDeviceFlattener::drawPoints(const SkDraw& d, SkCanvas::PointMode mode, |
| + size_t count, const SkPoint points[], |
| + const SkPaint& paint) { |
| + 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.
|
| + 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); |
| + return; |
| + } |
| + |
| + INHERITED::drawPoints(d, mode, count, points, paint); |
| +} |
| + |
| +void SkPDFDeviceFlattener::drawRect(const SkDraw& d, const SkRect& r, const SkPaint& paint) { |
| + if (mustFlatten(d)) { |
| + 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()) { |
| + SkMatrix local = paintFlatten.getShader()->getLocalMatrix(); |
| + local.preConcat(*d.fMatrix); |
| + paintFlatten.getShader()->setLocalMatrix(local); |
| + } |
| + |
| + INHERITED::drawPath(draw, path, paintFlatten, NULL, true); |
| + return; |
| + } |
| + |
| + INHERITED::drawRect(d, r, paint); |
| +} |
| + |
| +void SkPDFDeviceFlattener::drawPath(const SkDraw& d, const SkPath& origpath, |
| + const SkPaint& paint, const SkMatrix* prePathMatrix, |
| + bool pathIsMutable) { |
| + if (mustFlatten(d) || (prePathMatrix && prePathMatrix->hasPerspective())) { |
| + SkPath path; |
| + path.addPath(origpath); |
| + if (prePathMatrix) { |
| + path.transform(*prePathMatrix); |
| + } |
| + 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); |
| + return; |
| + } |
| + |
| + INHERITED::drawPath(d, origpath, paint, prePathMatrix, pathIsMutable); |
| +} |
| + |
| +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); |
| +} |
| + |
| +void SkPDFDeviceFlattener::drawVertices(const SkDraw& d, SkCanvas::VertexMode mode, |
| + int vertexCount, const SkPoint verts[], |
| + const SkPoint texs[], const SkColor colors[], |
| + SkXfermode* xmode, const uint16_t indices[], |
| + int indexCount, const SkPaint& paint) { |
| + INHERITED::drawVertices(d, mode, vertexCount, verts, texs, colors, xmode, indices, indexCount, |
| + paint); |
| +} |
| + |
| +void SkPDFDeviceFlattener::drawDevice(const SkDraw& d, SkBaseDevice* dev, int x, int y, |
| + const SkPaint& paint) { |
| + INHERITED::drawDevice(d, dev, x, y, 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(); |
| +} |