-
-
Notifications
You must be signed in to change notification settings - Fork 679
/
layout.ts
322 lines (266 loc) · 7.97 KB
/
layout.ts
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
import PDFFont from 'src/api/PDFFont';
import { CombedTextLayoutError } from 'src/api/errors';
import { TextAlignment } from 'src/api/text/alignment';
import { PDFHexString } from 'src/core';
import {
cleanText,
lineSplit,
mergeLines,
charAtIndex,
charSplit,
} from 'src/utils';
export interface TextPosition {
text: string;
encoded: PDFHexString;
x: number;
y: number;
width: number;
height: number;
}
export interface LayoutBounds {
x: number;
y: number;
width: number;
height: number;
}
const MIN_FONT_SIZE = 4;
const MAX_FONT_SIZE = 500;
const computeFontSize = (
lines: string[],
font: PDFFont,
bounds: LayoutBounds,
) => {
let fontSize = MIN_FONT_SIZE;
while (fontSize < MAX_FONT_SIZE) {
for (let idx = 0, len = lines.length; idx < len; idx++) {
const line = lines[idx];
const tooLong = font.widthOfTextAtSize(line, fontSize) > bounds.width;
if (tooLong) return fontSize - 1;
}
const height = font.heightAtSize(fontSize);
const lineHeight = height + height * 0.2;
const totalHeight = lines.length * lineHeight;
if (totalHeight > Math.abs(bounds.height)) return fontSize - 1;
fontSize += 1;
}
return fontSize;
};
const computeCombedFontSize = (
line: string,
font: PDFFont,
bounds: LayoutBounds,
cellCount: number,
) => {
const cellWidth = bounds.width / cellCount;
const cellHeight = bounds.height;
let fontSize = MIN_FONT_SIZE;
const chars = charSplit(line);
while (fontSize < MAX_FONT_SIZE) {
for (let idx = 0, len = chars.length; idx < len; idx++) {
const c = chars[idx];
const tooLong = font.widthOfTextAtSize(c, fontSize) > cellWidth * 0.75;
if (tooLong) return fontSize - 1;
}
const height = font.heightAtSize(fontSize, { descender: false });
if (height > cellHeight) return fontSize - 1;
fontSize += 1;
}
return fontSize;
};
export interface LayoutTextOptions {
alignment: TextAlignment;
fontSize?: number;
font: PDFFont;
bounds: LayoutBounds;
}
export interface MultilineTextLayout {
bounds: LayoutBounds;
lines: TextPosition[];
fontSize: number;
lineHeight: number;
}
const lastIndexOfWhitespace = (line: string) => {
for (let idx = line.length; idx > 0; idx--) {
if (/\s/.test(line[idx])) return idx;
}
return undefined;
};
const splitOutLines = (
input: string,
maxWidth: number,
font: PDFFont,
fontSize: number,
) => {
let lastWhitespaceIdx = input.length;
while (lastWhitespaceIdx > 0) {
const line = input.substring(0, lastWhitespaceIdx);
const encoded = font.encodeText(line);
const width = font.widthOfTextAtSize(line, fontSize);
if (width < maxWidth) {
const remainder = input.substring(lastWhitespaceIdx) || undefined;
return { line, encoded, width, remainder };
}
lastWhitespaceIdx = lastIndexOfWhitespace(line) ?? 0;
}
// We were unable to split the input enough to get a chunk that would fit
// within the specified `maxWidth` so we'll just return everything
return {
line: input,
encoded: font.encodeText(input),
width: font.widthOfTextAtSize(input, fontSize),
remainder: undefined,
};
};
export const layoutMultilineText = (
text: string,
{ alignment, fontSize, font, bounds }: LayoutTextOptions,
): MultilineTextLayout => {
const lines = lineSplit(cleanText(text));
if (fontSize === undefined || fontSize === 0) {
// fontSize = computeFontSize(lines, font, bounds);
// This is hardcoded to make it easier to perform automatic line-wrapping.
//
// TODO: Update `computeFontSize` to support automatic line-wrapping and
// automatic font size calculation.
fontSize = 12;
}
const height = font.heightAtSize(fontSize);
const lineHeight = height + height * 0.2;
const textLines: TextPosition[] = [];
let minX = bounds.x;
let minY = bounds.y;
let maxX = bounds.x + bounds.width;
let maxY = bounds.y + bounds.height;
let y = bounds.y + bounds.height;
for (let idx = 0, len = lines.length; idx < len; idx++) {
let prevRemainder: string | undefined = lines[idx];
while (prevRemainder !== undefined) {
const { line, encoded, width, remainder } = splitOutLines(
prevRemainder,
bounds.width,
font,
fontSize,
);
// prettier-ignore
const x = (
alignment === TextAlignment.Left ? bounds.x
: alignment === TextAlignment.Center ? bounds.x + (bounds.width / 2) - (width / 2)
: alignment === TextAlignment.Right ? bounds.x + bounds.width - width
: bounds.x
);
y -= lineHeight;
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x + width > maxX) maxX = x + width;
if (y + height > maxY) maxY = y + height;
textLines.push({ text: line, encoded, width, height, x, y });
// Only trim lines that we had to split ourselves. So we won't trim lines
// that the user provided themselves with whitespace.
prevRemainder = remainder?.trim();
}
}
return {
fontSize,
lineHeight,
lines: textLines,
bounds: {
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
},
};
};
export interface LayoutCombedTextOptions {
fontSize?: number;
font: PDFFont;
bounds: LayoutBounds;
cellCount: number;
}
export interface CombedTextLayout {
bounds: LayoutBounds;
cells: TextPosition[];
fontSize: number;
}
export const layoutCombedText = (
text: string,
{ fontSize, font, bounds, cellCount }: LayoutCombedTextOptions,
): CombedTextLayout => {
const line = mergeLines(cleanText(text));
if (line.length > cellCount) {
throw new CombedTextLayoutError(line.length, cellCount);
}
if (fontSize === undefined || fontSize === 0) {
fontSize = computeCombedFontSize(line, font, bounds, cellCount);
}
const cellWidth = bounds.width / cellCount;
const height = font.heightAtSize(fontSize, { descender: false });
const y = bounds.y + (bounds.height / 2 - height / 2);
const cells: TextPosition[] = [];
let minX = bounds.x;
let minY = bounds.y;
let maxX = bounds.x + bounds.width;
let maxY = bounds.y + bounds.height;
let cellOffset = 0;
let charOffset = 0;
while (cellOffset < cellCount) {
const [char, charLength] = charAtIndex(line, charOffset);
const encoded = font.encodeText(char);
const width = font.widthOfTextAtSize(char, fontSize);
const cellCenter = bounds.x + (cellWidth * cellOffset + cellWidth / 2);
const x = cellCenter - width / 2;
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x + width > maxX) maxX = x + width;
if (y + height > maxY) maxY = y + height;
cells.push({ text: line, encoded, width, height, x, y });
cellOffset += 1;
charOffset += charLength;
}
return {
fontSize,
cells,
bounds: {
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
},
};
};
export interface LayoutSinglelineTextOptions {
alignment: TextAlignment;
fontSize?: number;
font: PDFFont;
bounds: LayoutBounds;
}
export interface SinglelineTextLayout {
bounds: LayoutBounds;
line: TextPosition;
fontSize: number;
}
export const layoutSinglelineText = (
text: string,
{ alignment, fontSize, font, bounds }: LayoutSinglelineTextOptions,
): SinglelineTextLayout => {
const line = mergeLines(cleanText(text));
if (fontSize === undefined || fontSize === 0) {
fontSize = computeFontSize([line], font, bounds);
}
const encoded = font.encodeText(line);
const width = font.widthOfTextAtSize(line, fontSize);
const height = font.heightAtSize(fontSize, { descender: false });
// prettier-ignore
const x = (
alignment === TextAlignment.Left ? bounds.x
: alignment === TextAlignment.Center ? bounds.x + (bounds.width / 2) - (width / 2)
: alignment === TextAlignment.Right ? bounds.x + bounds.width - width
: bounds.x
);
const y = bounds.y + (bounds.height / 2 - height / 2);
return {
fontSize,
line: { text: line, encoded, width, height, x, y },
bounds: { x, y, width, height },
};
};