forked from vanielf/pdf-lib
-
Notifications
You must be signed in to change notification settings - Fork 29
/
test11.html
178 lines (157 loc) · 5.56 KB
/
test11.html
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
<!doctype html>
<html lang="en">
<head>
<meta
http-equiv="Content-Security-Policy"
content="
default-src 'self' 'unsafe-inline' blob: resource: https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.js;
object-src 'self' blob:;
frame-src 'self' blob:;
"
/>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" type="text/css" href="/apps/web/index.css" />
<title>Test 11</title>
<script type="text/javascript" src="/dist/pdf-lib.min.js"></script>
<script type="text/javascript" src="/apps/web/utils.js"></script>
<script
type="text/javascript"
src="https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.js"
></script>
</head>
<body>
<div id="button-container">
<button onclick="window.location.href = '/apps/web/test10.html'">
Prev
</button>
<button onclick="test()">Run Test</button>
<button onclick="window.location.href = '/apps/web/test12.html'">
Next
</button>
</div>
<div id="animation-target"></div>
<iframe id="iframe"></iframe>
</body>
<script type="text/javascript">
startFpsTracker('animation-target');
const fetchAsset = (asset) =>
fetch(`/assets/${asset}`)
.then((res) => res.arrayBuffer())
.then((res) => new Uint8Array(res));
const renderInIframe = (pdfBytes) => {
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const blobUrl = URL.createObjectURL(blob);
document.getElementById('iframe').src = blobUrl;
};
const breakTextIntoLines = (text, size, font, maxWidth) => {
const lines = [];
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] = PDFLib.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, lineHeight, maxHeight) => {
const { last } = PDFLib;
const linesPerGroup = Math.floor(maxHeight / lineHeight);
const groups = [[]];
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;
};
async function test() {
const { PDFDocument, StandardFonts } = PDFLib;
const [sourceHanBytes] = await Promise.all([
fetchAsset('fonts/source_hans_jp/SourceHanSerifJP-Regular.otf'),
]);
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(sourceHanBytes);
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();
renderInIframe(pdfBytes);
}
</script>
</html>