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