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