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::drawPoints(const SkDraw& d, SkCanvas::PointMode mode, | |
27 size_t count, const SkPoint points[], | |
28 const SkPaint& paint) { | |
29 if (!mustFlatten(d)) { | |
30 INHERITED::drawPoints(d, mode, count, points, paint); | |
31 return; | |
32 } | |
33 | |
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.
| |
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 } | |
42 | |
43 void SkPDFDeviceFlattener::drawRect(const SkDraw& d, const SkRect& r, const SkPa int& paint) { | |
44 if (!mustFlatten(d)) { | |
45 INHERITED::drawRect(d, r, paint); | |
46 return; | |
47 } | |
48 | |
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()) { | |
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.
| |
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 } | |
65 | |
66 void SkPDFDeviceFlattener::drawPath(const SkDraw& d, const SkPath& origpath, | |
67 const SkPaint& paint, const SkMatrix* prePat hMatrix, | |
68 bool pathIsMutable) { | |
69 if (!mustFlatten(d) && !(prePathMatrix && prePathMatrix->hasPerspective())) { | |
70 INHERITED::drawPath(d, origpath, paint, prePathMatrix, pathIsMutable); | |
71 return; | |
72 } | |
73 | |
74 SkPath path; | |
75 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
| |
76 if (prePathMatrix) { | |
77 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.
| |
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 } | |
94 | |
95 void SkPDFDeviceFlattener::drawText(const SkDraw& d, const void* text, size_t le n, | |
96 SkScalar x, SkScalar y, const SkPaint& paint ) { | |
97 if (mustPathText(d, paint)) { | |
98 d.drawText_asPaths((const char*)text, len, x, y, paint); | |
99 return; | |
100 } | |
101 | |
102 INHERITED::drawText(d, text, len, x, y, paint); | |
103 } | |
104 | |
105 void SkPDFDeviceFlattener::drawPosText(const SkDraw& d, const void* text, size_t len, | |
106 const SkScalar pos[], SkScalar constY, | |
107 int scalarsPerPos, const SkPaint& paint) { | |
108 if (mustPathText(d, paint)) { | |
109 d.drawPosText_asPaths((const char*)text, len, pos, constY, scalarsPerPos , paint); | |
110 return; | |
111 } | |
112 INHERITED::drawPosText(d, text, len,pos, constY,scalarsPerPos, paint); | |
113 } | |
114 | |
115 void SkPDFDeviceFlattener::drawTextOnPath(const SkDraw& d, const void* text, siz e_t len, | |
116 const SkPath& path, const SkMatrix* ma trix, | |
117 const SkPaint& paint) { | |
118 if (mustPathText(d, paint) || (matrix && matrix->hasPerspective())) { | |
119 d.drawTextOnPath((const char*)text, len, path, matrix, paint); | |
120 return; | |
121 } | |
122 INHERITED::drawTextOnPath(d, text, len, path, matrix, paint); | |
123 } | |
124 | |
125 bool SkPDFDeviceFlattener::mustFlatten(const SkDraw& d) const { | |
126 // TODO(edisonn): testability, add flag to force return true. | |
127 return d.fMatrix->hasPerspective(); | |
128 } | |
129 | |
130 bool SkPDFDeviceFlattener::mustPathText(const SkDraw& d, const SkPaint&) { | |
131 // TODO(edisonn): testability, add flag to force return true. | |
132 // TODO(edisonn): TBD: How to flatten MaskFilter. | |
133 return d.fMatrix->hasPerspective(); | |
134 } | |
OLD | NEW |