]> git.kianting.info Git - uann/blob - src/harfbuzz.ts
initial commit
[uann] / src / harfbuzz.ts
1 var hb = require('harfbuzzjs/hbjs');
2 var fs = require('fs');
3
4 export function harfbuzzTest(inputString: string){
5 WebAssembly.instantiate(fs.readFileSync(__dirname+"/../3rdparty/harfbuzzjs/hb.wasm"))
6 .then(function (wsm) {
7
8 hb = hb(wsm.instance);
9
10
11 let fontdata = fs.readFileSync("/usr/share/fonts/truetype/freefont/FreeSerif.ttf");
12
13 //hbjs(fontdata.instance);
14
15 //console.log(a);
16
17 var blob = hb.createBlob(fontdata); // Load the font data into something Harfbuzz can use
18 var face = hb.createFace(blob, 0); // Select the first font in the file (there's normally only one!)
19 var font = hb.createFont(face); // Create a Harfbuzz font object from the face
20 var buffer = hb.createBuffer(); // Make a buffer to hold some text
21 buffer.addText(inputString); // Fill it with some stuff
22 buffer.guessSegmentProperties(); // Set script, language and direction
23 hb.shape(font, buffer); // Shape the text, determining glyph IDs and positions
24 var output : Array<{g : number,
25 ax : number,
26 dx : number,
27 dy : number}> = buffer.json();
28
29 // Enumerate the glyphs
30 console.log("id\tax\tdx\tdy");
31
32 var xCursor = 0;
33 var yCursor = 0;
34 for (var glyph of output) {
35 var glyphId = glyph.g;
36 var xAdvance = glyph.ax;
37 var xDisplacement = glyph.dx;
38 var yDisplacement = glyph.dy;
39
40 var svgPath = font.glyphToPath(glyphId);
41
42 console.log(glyphId + "\t" + xAdvance + "\t" + xDisplacement + "\t" + yDisplacement);
43
44 // You need to supply this bit
45 //drawAGlyph(svgPath, xCursor + xDisplacement, yDisplacement);
46
47 // xCursor += xAdvance;
48 }
49
50 // Release memory
51 buffer.destroy();
52 font.destroy();
53 face.destroy();
54 blob.destroy();
55 });
56 }