Debian Codegraph @

debian/missing-sources/ttf-parser/examples/wasm/index.html

raw symbols SCIP
1 <h2>ttf-parser in WebAssembly</h2>
2 <p><small>(supports font files drag and drop)</small></p>
3 <p><span id="fileName">TTC.ttc</span>:</p>
4 <p><code>ttfp_fonts_in_collection():</code> <code id="fontsInCollection"></code></p>
5 <p><code>ttfp_is_variable():</code> <code id="isVariable"></code></p>
6 <p><code>ttfp_get_weight():</code> <code id="fontWeight"></code></p>
7 <script>
8 'use strict';
9
10 let wasm;
11
12 function update(fontBlob) {
13 const exports = wasm.instance.exports;
14
15 // How our heaped is structured, as ttf-parser doesn't allocate anything
16 // it is all ours and we can decide how to use it
17 const heapStart = exports.__heap_base.value;
18 const fontHandlerAddress = heapStart;
19 const fontHandlerLength = exports.ttfp_face_size_of();
20 const fontDataAddress = heapStart + fontHandlerLength;
21 const fontDataLength = fontBlob.length;
22
23 // Copy the fetched blob into WebAssembly machine
24 const heapu8 = new Uint8Array(exports.memory.buffer);
25 heapu8.set(fontBlob, fontDataAddress);
26
27 fontsInCollection.textContent = exports.ttfp_fonts_in_collection(fontDataAddress, fontDataLength);
28
29 // Create font handler
30 exports.ttfp_face_init(fontDataAddress, fontDataLength, 0/*face index*/, fontHandlerAddress);
31
32 isVariable.textContent = exports.ttfp_is_variable(fontHandlerAddress);
33 fontWeight.textContent = exports.ttfp_get_weight(fontHandlerAddress);
34 }
35
36 fetch('ttfparser.wasm').then(x => x.arrayBuffer()).then(WebAssembly.instantiate).then(result => {
37 wasm = result;
38 // Extend wasm machine heap once now that we are here, each page is 64kb
39 wasm.instance.exports.memory.grow(400);
40
41 // Could be done in parallel using Promise.all
42 fetch('TTC.ttc').then(x => x.arrayBuffer()).then(result => {
43 update(new Uint8Array(result));
44 });
45 });
46
47 document.addEventListener('dragover', e => {
48 e.stopPropagation(); e.preventDefault();
49 }, false);
50 document.addEventListener('dragleave', e => {
51 e.stopPropagation(); e.preventDefault();
52 }, false);
53 document.addEventListener('drop', e => {
54 e.stopPropagation(); e.preventDefault();
55 handleFontUpdate(e.dataTransfer.files[0]);
56 });
57 // document.addEventListener('paste', e => {
58 // handleFontUpdate(e.clipboardData.files[0]);
59 // });
60 function handleFontUpdate(file) {
61 if (!file) return;
62 fileName.textContent = file.name;
63 const reader = new FileReader();
64 reader.addEventListener('load', () => update(new Uint8Array(reader.result)));
65 reader.readAsArrayBuffer(file);
66 }
67 </script>