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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/*
* 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 "src/pdf/SkPDFUtils.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkData.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/private/base/SkFixed.h"
#include "src/core/SkGeometry.h"
#include "src/core/SkPathPriv.h"
#include "src/image/SkImage_Base.h"
#include "src/pdf/SkPDFResourceDict.h"
#include "src/pdf/SkPDFTypes.h"
#include <cmath>
const char* SkPDFUtils::BlendModeName(SkBlendMode mode) {
// PDF32000.book section 11.3.5 "Blend Mode"
switch (mode) {
case SkBlendMode::kSrcOver: return "Normal";
case SkBlendMode::kXor: return "Normal"; // (unsupported mode)
case SkBlendMode::kPlus: return "Normal"; // (unsupported mode)
case SkBlendMode::kScreen: return "Screen";
case SkBlendMode::kOverlay: return "Overlay";
case SkBlendMode::kDarken: return "Darken";
case SkBlendMode::kLighten: return "Lighten";
case SkBlendMode::kColorDodge: return "ColorDodge";
case SkBlendMode::kColorBurn: return "ColorBurn";
case SkBlendMode::kHardLight: return "HardLight";
case SkBlendMode::kSoftLight: return "SoftLight";
case SkBlendMode::kDifference: return "Difference";
case SkBlendMode::kExclusion: return "Exclusion";
case SkBlendMode::kMultiply: return "Multiply";
case SkBlendMode::kHue: return "Hue";
case SkBlendMode::kSaturation: return "Saturation";
case SkBlendMode::kColor: return "Color";
case SkBlendMode::kLuminosity: return "Luminosity";
// Other blendmodes are handled in SkPDFDevice::setUpContentEntry.
default: return nullptr;
}
}
std::unique_ptr<SkPDFArray> SkPDFUtils::RectToArray(const SkRect& r) {
return SkPDFMakeArray(r.left(), r.top(), r.right(), r.bottom());
}
std::unique_ptr<SkPDFArray> SkPDFUtils::MatrixToArray(const SkMatrix& matrix) {
SkScalar a[6];
if (!matrix.asAffine(a)) {
SkMatrix::SetAffineIdentity(a);
}
return SkPDFMakeArray(a[0], a[1], a[2], a[3], a[4], a[5]);
}
void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) {
SkPDFUtils::AppendScalar(x, content);
content->writeText(" ");
SkPDFUtils::AppendScalar(y, content);
content->writeText(" m\n");
}
void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) {
SkPDFUtils::AppendScalar(x, content);
content->writeText(" ");
SkPDFUtils::AppendScalar(y, content);
content->writeText(" l\n");
}
static void append_cubic(SkScalar ctl1X, SkScalar ctl1Y,
SkScalar ctl2X, SkScalar ctl2Y,
SkScalar dstX, SkScalar dstY, SkWStream* content) {
SkString cmd("y\n");
SkPDFUtils::AppendScalar(ctl1X, content);
content->writeText(" ");
SkPDFUtils::AppendScalar(ctl1Y, content);
content->writeText(" ");
if (ctl2X != dstX || ctl2Y != dstY) {
cmd.set("c\n");
SkPDFUtils::AppendScalar(ctl2X, content);
content->writeText(" ");
SkPDFUtils::AppendScalar(ctl2Y, content);
content->writeText(" ");
}
SkPDFUtils::AppendScalar(dstX, content);
content->writeText(" ");
SkPDFUtils::AppendScalar(dstY, content);
content->writeText(" ");
content->writeText(cmd.c_str());
}
static void append_quad(const SkPoint quad[], SkWStream* content) {
SkPoint cubic[4];
SkConvertQuadToCubic(quad, cubic);
append_cubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY,
cubic[3].fX, cubic[3].fY, content);
}
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 = std::min(rect.fBottom, rect.fTop);
SkPDFUtils::AppendScalar(rect.fLeft, content);
content->writeText(" ");
SkPDFUtils::AppendScalar(bottom, content);
content->writeText(" ");
SkPDFUtils::AppendScalar(rect.width(), content);
content->writeText(" ");
SkPDFUtils::AppendScalar(rect.height(), content);
content->writeText(" re\n");
}
void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle,
bool doConsumeDegerates, SkWStream* content,
SkScalar tolerance) {
if (path.isEmpty() && SkPaint::kFill_Style == paintStyle) {
SkPDFUtils::AppendRectangle({0, 0, 0, 0}, content);
return;
}
// 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).
SkRect rect;
bool isClosed; // Both closure and direction need to be checked.
SkPathDirection direction;
if (path.isRect(&rect, &isClosed, &direction) &&
isClosed &&
(SkPathDirection::kCW == direction ||
SkPathFillType::kEvenOdd == path.getFillType()))
{
SkPDFUtils::AppendRectangle(rect, content);
return;
}
enum SkipFillState {
kEmpty_SkipFillState,
kSingleLine_SkipFillState,
kNonSingleLine_SkipFillState,
};
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:
if (!doConsumeDegerates || !SkPathPriv::AllPointsEq(args, 2)) {
AppendLine(args[1].fX, args[1].fY, ¤tSegment);
if ((fillState == kEmpty_SkipFillState) && (args[0] != lastMovePt)) {
fillState = kSingleLine_SkipFillState;
break;
}
fillState = kNonSingleLine_SkipFillState;
}
break;
case SkPath::kQuad_Verb:
if (!doConsumeDegerates || !SkPathPriv::AllPointsEq(args, 3)) {
append_quad(args, ¤tSegment);
fillState = kNonSingleLine_SkipFillState;
}
break;
case SkPath::kConic_Verb:
if (!doConsumeDegerates || !SkPathPriv::AllPointsEq(args, 3)) {
SkAutoConicToQuads converter;
const SkPoint* quads = converter.computeQuads(args, iter.conicWeight(), tolerance);
for (int i = 0; i < converter.countQuads(); ++i) {
append_quad(&quads[i * 2], ¤tSegment);
}
fillState = kNonSingleLine_SkipFillState;
}
break;
case SkPath::kCubic_Verb:
if (!doConsumeDegerates || !SkPathPriv::AllPointsEq(args, 4)) {
append_cubic(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:
ClosePath(¤tSegment);
currentSegment.writeToStream(content);
currentSegment.reset();
break;
default:
SkASSERT(false);
break;
}
}
if (currentSegment.bytesWritten() > 0) {
currentSegment.writeToStream(content);
}
}
void SkPDFUtils::ClosePath(SkWStream* content) {
content->writeText("h\n");
}
void SkPDFUtils::PaintPath(SkPaint::Style style, SkPathFillType 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 == SkPathFillType::kInverseEvenOdd, false);
NOT_IMPLEMENTED(fill == SkPathFillType::kInverseWinding, false);
if (fill == SkPathFillType::kEvenOdd) {
content->writeText("*");
}
}
content->writeText("\n");
}
void SkPDFUtils::StrokePath(SkWStream* content) {
SkPDFUtils::PaintPath(SkPaint::kStroke_Style, SkPathFillType::kWinding, content);
}
void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) {
SkPDFWriteResourceName(content, SkPDFResourceType::kExtGState, objectIndex);
content->writeText(" gs\n");
}
void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) {
// Select Pattern color space (CS, cs) and set pattern object as current
// color (SCN, scn)
content->writeText("/Pattern CS/Pattern cs");
SkPDFWriteResourceName(content, SkPDFResourceType::kPattern, objectIndex);
content->writeText(" SCN");
SkPDFWriteResourceName(content, SkPDFResourceType::kPattern, objectIndex);
content->writeText(" scn\n");
}
// return "x/pow(10, places)", given 0<x<pow(10, places)
// result points to places+2 chars.
static size_t print_permil_as_decimal(int x, char* result, unsigned places) {
result[0] = '.';
for (int i = places; i > 0; --i) {
result[i] = '0' + x % 10;
x /= 10;
}
int j;
for (j = places; j > 1; --j) {
if (result[j] != '0') {
break;
}
}
result[j + 1] = '\0';
return j + 1;
}
static constexpr int int_pow(int base, unsigned exp, int acc = 1) {
return exp < 1 ? acc
: int_pow(base * base,
exp / 2,
(exp % 2) ? acc * base : acc);
}
size_t SkPDFUtils::ColorToDecimalF(float value, char result[kFloatColorDecimalCount + 2]) {
static constexpr int kFactor = int_pow(10, kFloatColorDecimalCount);
int x = sk_float_round2int(value * kFactor);
if (x >= kFactor || x <= 0) { // clamp to 0-1
result[0] = x > 0 ? '1' : '0';
result[1] = '\0';
return 1;
}
return print_permil_as_decimal(x, result, kFloatColorDecimalCount);
}
size_t SkPDFUtils::ColorToDecimal(uint8_t value, char result[5]) {
if (value == 255 || value == 0) {
result[0] = value ? '1' : '0';
result[1] = '\0';
return 1;
}
// int x = 0.5 + (1000.0 / 255.0) * value;
int x = SkFixedRoundToInt((SK_Fixed1 * 1000 / 255) * value);
return print_permil_as_decimal(x, result, 3);
}
bool SkPDFUtils::InverseTransformBBox(const SkMatrix& matrix, SkRect* bbox) {
SkMatrix inverse;
if (!matrix.invert(&inverse)) {
return false;
}
inverse.mapRect(bbox);
return true;
}
void SkPDFUtils::PopulateTilingPatternDict(SkPDFDict* pattern,
SkRect& bbox,
std::unique_ptr<SkPDFDict> resources,
const SkMatrix& matrix) {
const int kTiling_PatternType = 1;
const int kColoredTilingPattern_PaintType = 1;
const int kConstantSpacing_TilingType = 1;
pattern->insertName("Type", "Pattern");
pattern->insertInt("PatternType", kTiling_PatternType);
pattern->insertInt("PaintType", kColoredTilingPattern_PaintType);
pattern->insertInt("TilingType", kConstantSpacing_TilingType);
pattern->insertObject("BBox", SkPDFUtils::RectToArray(bbox));
pattern->insertScalar("XStep", bbox.width());
pattern->insertScalar("YStep", bbox.height());
pattern->insertObject("Resources", std::move(resources));
if (!matrix.isIdentity()) {
pattern->insertObject("Matrix", SkPDFUtils::MatrixToArray(matrix));
}
}
bool SkPDFUtils::ToBitmap(const SkImage* img, SkBitmap* dst) {
SkASSERT(img);
SkASSERT(dst);
SkBitmap bitmap;
// TODO: support GPU images
if(as_IB(img)->getROPixels(nullptr, &bitmap)) {
SkASSERT(bitmap.dimensions() == img->dimensions());
SkASSERT(!bitmap.drawsNothing());
*dst = std::move(bitmap);
return true;
}
return false;
}
#ifdef SK_PDF_BASE85_BINARY
void SkPDFUtils::Base85Encode(std::unique_ptr<SkStreamAsset> stream, SkDynamicMemoryWStream* dst) {
SkASSERT(dst);
SkASSERT(stream);
dst->writeText("\n");
int column = 0;
while (true) {
uint8_t src[4] = {0, 0, 0, 0};
size_t count = stream->read(src, 4);
SkASSERT(count < 5);
if (0 == count) {
dst->writeText("~>\n");
return;
}
uint32_t v = ((uint32_t)src[0] << 24) | ((uint32_t)src[1] << 16) |
((uint32_t)src[2] << 8) | src[3];
if (v == 0 && count == 4) {
dst->writeText("z");
column += 1;
} else {
char buffer[5];
for (int n = 4; n > 0; --n) {
buffer[n] = (v % 85) + '!';
v /= 85;
}
buffer[0] = v + '!';
dst->write(buffer, count + 1);
column += count + 1;
}
if (column > 74) {
dst->writeText("\n");
column = 0;
}
}
}
#endif // SK_PDF_BASE85_BINARY
void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) {
SkScalar values[6];
if (!matrix.asAffine(values)) {
SkMatrix::SetAffineIdentity(values);
}
for (SkScalar v : values) {
SkPDFUtils::AppendScalar(v, content);
content->writeText(" ");
}
content->writeText("cm\n");
}
|