aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pdf/SkPDFDeviceFlattener.cpp
blob: 91c9803c320a583c31c262b97a86bbe85cab7f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * 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() {
}

static void flattenPaint(const SkDraw& d, SkPaint* paint) {
    if (paint->getShader()) {
        SkMatrix local = paint->getShader()->getLocalMatrix();
        local.preConcat(*d.fMatrix);
        paint->getShader()->setLocalMatrix(local);
    }
}

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;
    }

    SkPaint paintFlatten(paint);
    flattenPaint(d, &paintFlatten);

    SkPoint* flattenedPoints = SkNEW_ARRAY(SkPoint, count);
    d.fMatrix->mapPoints(flattenedPoints, points, SkToS32(count));
    SkDraw draw(d);
    SkMatrix identity = SkMatrix::I();
    draw.fMatrix = &identity;
    INHERITED::drawPoints(draw, mode, count, flattenedPoints, paintFlatten);
    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);
    flattenPaint(d, &paintFlatten);

    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* pathPtr = (SkPath*)&origPath;
    SkPath tmpPath;

    if (!pathIsMutable) {
        tmpPath = origPath;
        pathPtr = &tmpPath;
    }

    if (prePathMatrix) {
        pathPtr->transform(*prePathMatrix);
    }

    SkPaint paintFlatten(paint);
    flattenPaint(d, &paintFlatten);

    bool fill = paintFlatten.getFillPath(*pathPtr, &tmpPath);
    SkDEBUGCODE(pathPtr = (SkPath*)0x12345678);  // Don't use pathPtr after this point.

    paintFlatten.setPathEffect(NULL);
    if (fill) {
        paintFlatten.setStyle(SkPaint::kFill_Style);
    } else {
        paintFlatten.setStyle(SkPaint::kStroke_Style);
        paintFlatten.setStrokeWidth(0);
    }

    tmpPath.transform(*d.fMatrix);

    SkDraw draw(d);
    SkMatrix matrix = SkMatrix::I();
    draw.fMatrix = &matrix;

    INHERITED::drawPath(draw, tmpPath, 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();
}