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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkData.h"
#include "SkGeometry.h"
#include "SkPaint.h"
#include "SkPath.h"
#include "SkPDFResourceDict.h"
#include "SkPDFUtils.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkPDFTypes.h"
//static
SkPDFArray* SkPDFUtils::RectToArray(const SkRect& rect) {
SkPDFArray* result = new SkPDFArray();
result->reserve(4);
result->appendScalar(rect.fLeft);
result->appendScalar(rect.fTop);
result->appendScalar(rect.fRight);
result->appendScalar(rect.fBottom);
return result;
}
// static
SkPDFArray* SkPDFUtils::MatrixToArray(const SkMatrix& matrix) {
SkScalar values[6];
if (!matrix.asAffine(values)) {
SkMatrix::SetAffineIdentity(values);
}
SkPDFArray* result = new SkPDFArray;
result->reserve(6);
for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) {
result->appendScalar(values[i]);
}
return result;
}
// static
void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) {
SkScalar values[6];
if (!matrix.asAffine(values)) {
SkMatrix::SetAffineIdentity(values);
}
for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) {
SkPDFScalar::Append(values[i], content);
content->writeText(" ");
}
content->writeText("cm\n");
}
// static
void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) {
SkPDFScalar::Append(x, content);
content->writeText(" ");
SkPDFScalar::Append(y, content);
content->writeText(" m\n");
}
// static
void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) {
SkPDFScalar::Append(x, content);
content->writeText(" ");
SkPDFScalar::Append(y, content);
content->writeText(" l\n");
}
// static
void SkPDFUtils::AppendCubic(SkScalar ctl1X, SkScalar ctl1Y,
SkScalar ctl2X, SkScalar ctl2Y,
SkScalar dstX, SkScalar dstY, SkWStream* content) {
SkString cmd("y\n");
SkPDFScalar::Append(ctl1X, content);
content->writeText(" ");
SkPDFScalar::Append(ctl1Y, content);
content->writeText(" ");
if (ctl2X != dstX || ctl2Y != dstY) {
cmd.set("c\n");
SkPDFScalar::Append(ctl2X, content);
content->writeText(" ");
SkPDFScalar::Append(ctl2Y, content);
content->writeText(" ");
}
SkPDFScalar::Append(dstX, content);
content->writeText(" ");
SkPDFScalar::Append(dstY, content);
content->writeText(" ");
content->writeText(cmd.c_str());
}
static void append_quad(const SkPoint quad[], SkWStream* content) {
SkPoint cubic[4];
SkConvertQuadToCubic(quad, cubic);
SkPDFUtils::AppendCubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY,
cubic[3].fX, cubic[3].fY, content);
}
// static
void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) {
// Skia has 0,0 at top left, pdf at bottom left. Do the right thing.
SkScalar bottom = SkMinScalar(rect.fBottom, rect.fTop);
SkPDFScalar::Append(rect.fLeft, content);
content->writeText(" ");
SkPDFScalar::Append(bottom, content);
content->writeText(" ");
SkPDFScalar::Append(rect.width(), content);
content->writeText(" ");
SkPDFScalar::Append(rect.height(), content);
content->writeText(" re\n");
}
// static
void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle,
SkWStream* content) {
// Filling a path with no area results in a drawing in PDF renderers but
// Chrome expects to be able to draw some such entities with no visible
// result, so we detect those cases and discard the drawing for them.
// Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y).
enum SkipFillState {
kEmpty_SkipFillState = 0,
kSingleLine_SkipFillState = 1,
kNonSingleLine_SkipFillState = 2,
};
SkipFillState fillState = kEmpty_SkipFillState;
if (paintStyle != SkPaint::kFill_Style) {
fillState = kNonSingleLine_SkipFillState;
}
SkPoint lastMovePt = SkPoint::Make(0,0);
SkDynamicMemoryWStream currentSegment;
SkPoint args[4];
SkPath::Iter iter(path, false);
for (SkPath::Verb verb = iter.next(args); verb != SkPath::kDone_Verb; verb = iter.next(args)) {
// args gets all the points, even the implicit first point.
switch (verb) {
case SkPath::kMove_Verb:
MoveTo(args[0].fX, args[0].fY, ¤tSegment);
lastMovePt = args[0];
fillState = kEmpty_SkipFillState;
break;
case SkPath::kLine_Verb:
AppendLine(args[1].fX, args[1].fY, ¤tSegment);
if (fillState == kEmpty_SkipFillState) {
if (args[0] != lastMovePt) {
fillState = kSingleLine_SkipFillState;
}
} else if (fillState == kSingleLine_SkipFillState) {
fillState = kNonSingleLine_SkipFillState;
}
break;
case SkPath::kQuad_Verb:
append_quad(args, ¤tSegment);
fillState = kNonSingleLine_SkipFillState;
break;
case SkPath::kConic_Verb: {
const SkScalar tol = SK_Scalar1 / 4;
SkAutoConicToQuads converter;
const SkPoint* quads = converter.computeQuads(args, iter.conicWeight(), tol);
for (int i = 0; i < converter.countQuads(); ++i) {
append_quad(&quads[i * 2], ¤tSegment);
}
} break;
case SkPath::kCubic_Verb:
AppendCubic(args[1].fX, args[1].fY, args[2].fX, args[2].fY,
args[3].fX, args[3].fY, ¤tSegment);
fillState = kNonSingleLine_SkipFillState;
break;
case SkPath::kClose_Verb:
if (fillState != kSingleLine_SkipFillState) {
ClosePath(¤tSegment);
currentSegment.writeToStream(content);
}
currentSegment.reset();
break;
default:
SkASSERT(false);
break;
}
}
if (currentSegment.bytesWritten() > 0) {
currentSegment.writeToStream(content);
}
}
// static
void SkPDFUtils::ClosePath(SkWStream* content) {
content->writeText("h\n");
}
// static
void SkPDFUtils::PaintPath(SkPaint::Style style, SkPath::FillType fill,
SkWStream* content) {
if (style == SkPaint::kFill_Style) {
content->writeText("f");
} else if (style == SkPaint::kStrokeAndFill_Style) {
content->writeText("B");
} else if (style == SkPaint::kStroke_Style) {
content->writeText("S");
}
if (style != SkPaint::kStroke_Style) {
NOT_IMPLEMENTED(fill == SkPath::kInverseEvenOdd_FillType, false);
NOT_IMPLEMENTED(fill == SkPath::kInverseWinding_FillType, false);
if (fill == SkPath::kEvenOdd_FillType) {
content->writeText("*");
}
}
content->writeText("\n");
}
// static
void SkPDFUtils::StrokePath(SkWStream* content) {
SkPDFUtils::PaintPath(
SkPaint::kStroke_Style, SkPath::kWinding_FillType, content);
}
// static
void SkPDFUtils::DrawFormXObject(int objectIndex, SkWStream* content) {
content->writeText("/");
content->writeText(SkPDFResourceDict::getResourceName(
SkPDFResourceDict::kXObject_ResourceType,
objectIndex).c_str());
content->writeText(" Do\n");
}
// static
void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) {
content->writeText("/");
content->writeText(SkPDFResourceDict::getResourceName(
SkPDFResourceDict::kExtGState_ResourceType,
objectIndex).c_str());
content->writeText(" gs\n");
}
// static
void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) {
// Select Pattern color space (CS, cs) and set pattern object as current
// color (SCN, scn)
SkString resourceName = SkPDFResourceDict::getResourceName(
SkPDFResourceDict::kPattern_ResourceType,
objectIndex);
content->writeText("/Pattern CS/Pattern cs/");
content->writeText(resourceName.c_str());
content->writeText(" SCN/");
content->writeText(resourceName.c_str());
content->writeText(" scn\n");
}
|