forked from Hopding/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test11.ts
134 lines (119 loc) · 3.56 KB
/
test11.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
import fontkit from '@pdf-lib/fontkit';
import { Assets } from '..';
import {
last,
PDFDocument,
PDFFont,
StandardFonts,
charAtIndex,
} from '../../..';
const breakTextIntoLines = (
text: string,
size: number,
font: PDFFont,
maxWidth: number,
) => {
const lines: string[] = [];
let textIdx = 0;
while (textIdx < text.length) {
let line = '';
while (textIdx < text.length) {
if (text.charAt(textIdx) === '\n') {
lines.push(line);
textIdx += 1;
line = '';
continue;
}
const [glyph] = charAtIndex(text, textIdx);
const newLine = line + glyph;
if (font.widthOfTextAtSize(newLine, size) > maxWidth) break;
line = newLine;
textIdx += glyph.length;
}
lines.push(line);
}
return lines;
};
const breakLinesIntoGroups = (
lines: string[],
lineHeight: number,
maxHeight: number,
) => {
const linesPerGroup = Math.floor(maxHeight / lineHeight);
const groups: string[][] = [[]];
for (let idx = 0, len = lines.length; idx < len; idx++) {
const line = lines[idx];
if (last(groups).length === linesPerGroup) {
groups.push([]);
}
last(groups).push(line);
}
return groups;
};
export default async (assets: Assets) => {
const { fonts } = assets;
const pdfDoc = await PDFDocument.create();
pdfDoc.registerFontkit(fontkit);
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
const helveticaBoldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
const title = 'Embedded UTF-16 Font Demo';
const description = `
In addition to the standard 14 fonts provided by PDF readers, the PDF
specification allows PDF documents to embed their own fonts. The standard
14 fonts only support a very limited latin and symbolic character set, but
embedded fonts can support arbitrary character sets and glyphs.
This document is a demonstration of an embedded font. Specifically, the
Source Han Serif Japanese Regular font. The following pages render all
characters supported by this font.
The characters are rendered from greatest to least (ordered by their
code points). Take special note of the first 1.25 pages of glyphs, as these
glyphs represent UTF-16 code points (the rest of the glyphs in this document
are UTF-8).`;
const descriptionLines = breakTextIntoLines(
description,
16,
helveticaFont,
600,
);
const titlePage = pdfDoc.addPage([650, 700]);
titlePage.drawText(title, {
font: helveticaBoldFont,
size: 35,
y: 700 - 100,
x: 650 / 2 - helveticaBoldFont.widthOfTextAtSize(title, 35) / 2,
});
titlePage.drawText(descriptionLines.join('\n'), {
font: helveticaFont,
size: 16,
y: 525,
x: 25,
});
const sourceHanFont = await pdfDoc.embedFont(fonts.otf.source_hans_jp);
const sourceHanFontSize = 20;
const sourceHanString = String.fromCodePoint(
...sourceHanFont.getCharacterSet().reverse(),
);
const sourceHanLines = breakTextIntoLines(
sourceHanString,
sourceHanFontSize,
sourceHanFont,
600,
);
const sourceHanLineGroups = breakLinesIntoGroups(
sourceHanLines,
sourceHanFont.heightAtSize(sourceHanFontSize) + 10,
675,
);
sourceHanLineGroups.forEach((lines) => {
const page = pdfDoc.addPage([650, 700]);
page.drawText(lines.join('\n'), {
font: sourceHanFont,
size: sourceHanFontSize,
x: 25,
y: 700 - 25 - sourceHanFontSize,
lineHeight: sourceHanFont.heightAtSize(sourceHanFontSize) + 10,
});
});
const pdfBytes = await pdfDoc.save();
return pdfBytes;
};