-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathregister-font.html
More file actions
74 lines (71 loc) · 2.7 KB
/
register-font.html
File metadata and controls
74 lines (71 loc) · 2.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PAG</title>
<link rel="icon" href="https://pag.io/img/favicon.png" />
<style>
body {
padding: 12px;
}
.header {
height: 64px;
border-bottom: 1px solid rgb(193, 193, 193);
margin-bottom: 24px;
}
</style>
</head>
<body>
<div class="header">
<img src="../assets/logo.png" alt="logo" width="133" height="48" />
</div>
<div>
<span>Unknown font family:</span>
<canvas class="canvas" id="pag-1"></canvas>
<span>Registered font:</span>
<canvas class="canvas" id="pag-2"></canvas>
</div>
<script type="module" src="https://cdn.jsdelivr.net/npm/libpag@latest/lib/libpag.min.js"></script>
<script>
window.onload = async () => {
const pagUrl = '../assets/simple_text.pag';
const PAG = await window.libpag.PAGInit();
const buffer = await fetch(pagUrl).then((response) => response.arrayBuffer());
// Unknown font
const pagFile1 = await PAG.PAGFile.load(buffer);
// Get TextDate by editableIndex.
// About editableIndex look for `editable-index.html`
const textDoc1 = pagFile1.getTextData(0);
textDoc1.fontFamily = 'unknown';
// Replace text by editableIndex
pagFile1.replaceText(0, textDoc1);
const canvas1 = document.getElementById('pag-1');
canvas1.width = 360;
canvas1.height = 360;
const pagView1 = await PAG.PAGView.init(pagFile1, canvas1);
pagView1.setRepeatCount(0);
await pagView1.play();
// Registered font
const fontUrl = '../assets/SourceHanSerifCN-Regular.ttf';
const fontBlob = await fetch(fontUrl).then((response) => response.blob());
const fontFile = new window.File([fontBlob], fontUrl.replace(/(.*\/)*([^.]+)/i, '$2'));
await PAG.PAGFont.registerFont('SourceHanSerifCN-Regular', fontFile);
const pagFile2 = await PAG.PAGFile.load(buffer);
// Get TextDate by editableIndex.
// About editableIndex look for `editable-index.html`
const textDoc2 = pagFile2.getTextData(0);
textDoc2.fontFamily = 'SourceHanSerifCN-Regular';
// Replace text by editableIndex
pagFile2.replaceText(0, textDoc2);
const canvas2 = document.getElementById('pag-2');
canvas2.width = 360;
canvas2.height = 360;
const pagView2 = await PAG.PAGView.init(pagFile2, canvas2);
pagView2.setRepeatCount(0);
await pagView2.play();
};
</script>
</body>
</html>