-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtext.spec.js
More file actions
215 lines (196 loc) · 6.54 KB
/
text.spec.js
File metadata and controls
215 lines (196 loc) · 6.54 KB
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
import { runDocTest } from './helpers';
describe('text', function () {
test('simple text', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.text('Really simple text', 100, 100);
});
});
test('alignment', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.text('Left aligned text', { align: 'left' });
doc.text('Right aligned text', { align: 'right' });
doc.text(
'Justified aligned text - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus.',
{ align: 'justify' },
);
});
});
test('soft hyphen', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.text(
'Text with soft hyphen - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lo ip\u00ADsum',
{ align: 'justify' },
);
doc.text(
'Text with soft hyphen on the edge - ttttestttestttestttestttestttestttestttestttestttestttes\u00ADtt\u00ADt',
{ align: 'justify' },
);
});
});
test('decoration', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.fillColor('blue').text('Here is a link!', 100, 100, {
link: 'http://google.com/',
underline: true,
});
doc.text('Strike', 100, 130, {
strike: true,
});
doc.text('Strike', 100, 160, {
underline: true,
strike: true,
});
});
});
test('list', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.fillColor('#000').list(['One', 'Two', 'Three'], 100, 150);
});
});
test('list with line breaks in items', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.list(['Foo\nBar', 'Foo\rBar', 'Foo\r\nBar'], [100, 150]);
});
});
test('list (numbered)', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc
.fillColor('#000')
.list(['One', 'Two', 'Three'], 100, 150, { listType: 'numbered' });
});
});
test('list (lettered)', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc
.fillColor('#000')
.list(['One', 'Two', 'Three'], 100, 150, { listType: 'lettered' });
});
});
test('list with sub-list (unordered)', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc
.fillColor('#000')
.list(['One', ['One.One', 'One.Two'], 'Three'], 100, 150);
});
});
test('list with sub-list (ordered)', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc
.fillColor('#000')
.list(['One', ['One.One', 'One.Two'], 'Three'], 100, 150, {
listType: 'numbered',
});
});
});
test('continued text with OpenType features', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.text('Really simple', 100, 100, {
features: ['smcp'],
continued: true,
lineBreak: false,
});
doc.text(' text', { features: [], lineBreak: false });
});
});
test('rotated text', function () {
let i = 0;
const cols = [
'#292f56',
'#492d73',
'#8c2f94',
'#b62d78',
'#d82d31',
'#e69541',
'#ecf157',
'#acfa70',
];
function randColor() {
return cols[i++ % cols.length];
}
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
for (let i = -360; i < 360; i += 5) {
const withLabel = i % 45 === 0;
const margin = i < 0 ? ' ' : ' ';
let text = `—————————> ${withLabel ? `${margin}${i}` : ''}`;
if (withLabel) {
const bounds = doc.boundsOfString(text, 200, 200, { rotation: i });
doc
.save()
.rect(bounds.x, bounds.y, bounds.width, bounds.height)
.stroke(randColor())
.restore();
}
doc
.save()
.fill(withLabel ? 'red' : 'black')
.text(text, 200, 200, { rotation: i })
.restore();
}
doc.save().circle(200, 200, 1).fill('blue').restore();
});
});
test('rotated multi line text', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
let text = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.`;
// Compute text rect
const textBounds = doc.boundsOfString(text, 300, 300, { rotation: 0 });
// Compute rotated bounds
const bounds = doc.boundsOfString(text, 300, 300, { rotation: 45 });
doc
// Draw rotated bounds
.rect(bounds.x, bounds.y, bounds.width, bounds.height)
.stroke()
// Draw text
.text(text, 300, 300, { rotation: 45 })
// Draw text rect (rotation is negative because of the flipped axis
.rotate(-45, { origin: [300, 300] })
.rect(300, 300, textBounds.width, textBounds.height)
.stroke('pink')
// Draw origin
.circle(300, 300, 2)
.fill('blue');
});
});
test('ignore trailing line break - issue #1620', function () {
return runDocTest(
{
systemFonts: true,
failureThreshold: 0.002,
failureThresholdType: 'percent',
},
function (doc) {
const text = 'test\ntest';
let heightWithout = doc.heightOfString(text, {
trailingLineBreak: false,
});
doc
.save()
.rect(doc.x, doc.y, doc.page.contentWidth, heightWithout)
.strokeColor('red', 0.3)
.stroke()
.restore();
let height = doc.heightOfString(text);
doc
.save()
.rect(doc.x, doc.y, doc.page.contentWidth, height)
.strokeColor('blue', 0.3)
.stroke()
.restore();
doc.text(text, { height });
},
);
});
});