-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
vector.spec.js
189 lines (158 loc) · 4.07 KB
/
vector.spec.js
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
import PDFDocument from '../../lib/document';
import { logData } from './helpers';
describe('Vector Graphics', () => {
let document;
beforeEach(() => {
document = new PDFDocument({
info: { CreationDate: new Date(Date.UTC(2018, 1, 1)) },
compress: false
});
});
describe('dash', () => {
test('with numeric length argument', () => {
const docData = logData(document);
const vectorStream = Buffer.from(
'1 0 0 -1 0 792 cm\n50 20 m\n[2 2] 0 d\nS\n',
'binary'
);
document
.moveTo(50, 20)
.dash(2)
.stroke();
document.end();
expect(docData).toContainChunk([
`5 0 obj`,
`<<
/Length 38
>>`,
`stream`,
vectorStream,
`\nendstream`,
`endobj`
]);
});
test('with array length argument', () => {
const docData = logData(document);
const vectorStream = Buffer.from(
'1 0 0 -1 0 792 cm\n50 20 m\n[1 2] 0 d\nS\n',
'binary'
);
document
.moveTo(50, 20)
.dash([1, 2])
.stroke();
document.end();
expect(docData).toContainChunk([
`5 0 obj`,
`<<
/Length 38
>>`,
`stream`,
vectorStream,
`\nendstream`,
`endobj`
]);
});
test('with space option', () => {
const docData = logData(document);
const vectorStream = Buffer.from(
'1 0 0 -1 0 792 cm\n50 20 m\n[2 10] 0 d\nS\n',
'binary'
);
document
.moveTo(50, 20)
.dash(2, { space: 10 })
.stroke();
document.end();
expect(docData).toContainChunk([
`5 0 obj`,
`<<
/Length 39
>>`,
`stream`,
vectorStream,
`\nendstream`,
`endobj`
]);
});
test('with phase option', () => {
const docData = logData(document);
const vectorStream = Buffer.from(
'1 0 0 -1 0 792 cm\n50 20 m\n[2 2] 8 d\nS\n',
'binary'
);
document
.moveTo(50, 20)
.dash(2, { phase: 8 })
.stroke();
document.end();
expect(docData).toContainChunk([
`5 0 obj`,
`<<
/Length 38
>>`,
`stream`,
vectorStream,
`\nendstream`,
`endobj`
]);
});
describe('validation', () => {
test('length 1', () => {
const doc = new PDFDocument();
expect(() => doc.dash(1)).not.toThrow();
});
test('length 1.5', () => {
const doc = new PDFDocument();
expect(() => doc.dash(1.5)).not.toThrow();
});
test('length 0 throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash(0)).toThrow(
'dash(0, {}) invalid, lengths must be numeric and greater than zero'
);
});
test('length -1 throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash(-1)).toThrow(
'dash(-1, {}) invalid, lengths must be numeric and greater than zero'
);
});
test('length null throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash(null)).toThrow(
'dash(null, {}) invalid, lengths must be numeric and greater than zero'
);
});
test('length array', () => {
const doc = new PDFDocument();
expect(() => doc.dash([2, 3])).not.toThrow();
});
test('length array containing zeros throws', () => {
const doc = new PDFDocument();
expect(() => doc.dash([2, 0, 3])).toThrow(
'dash([2,0,3], {}) invalid, lengths must be numeric and greater than zero'
);
});
});
});
describe('translate', () => {
test('identity transform is ignored', () => {
const docData = logData(document);
const vectorStream = Buffer.from(`1 0 0 -1 0 792 cm\n1 0 0 1 0 0 cm\n`, 'binary');
document
.translate(0, 0);
document.end();
expect(docData).not.toContainChunk([
`5 0 obj`,
`<<
/Length 33
>>`,
`stream`,
vectorStream,
`\nendstream`,
`endobj`
]);
});
});
});