Skip to content

Commit 5225a57

Browse files
committed
Add face.listNames() and face.getName()
1 parent 48884cf commit 5225a57

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ em++ \
1818
-s MODULARIZE \
1919
-s EXPORT_NAME=createHarfBuzz \
2020
21-
-s EXPORTED_RUNTIME_METHODS='["addFunction", "removeFunction", "stackAlloc", "wasmMemory", "wasmExports", "HEAP8", "HEAPU8", "HEAP32", "HEAPU32", "HEAPF32"]' \
21+
-s EXPORTED_RUNTIME_METHODS='["addFunction", "removeFunction", "stackAlloc", "wasmMemory", "wasmExports", "HEAP8", "HEAPU8", "HEAPU16", "HEAP32", "HEAPU32", "HEAPF32"]' \
2222
-s INITIAL_MEMORY=256KB \
2323
-s ALLOW_MEMORY_GROWTH \
2424
-s ALLOW_TABLE_GROWTH \

config-override.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
#undef HB_NO_AVAR2
1010
#undef HB_NO_CUBIC_GLYF
1111
#undef HB_NO_VAR_COMPOSITES
12+
#undef HB_NO_NAME
1213
#define HB_BUFFER_MESSAGE_MORE 1

hb.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ _hb_ot_layout_table_get_script_tags
7070
_hb_ot_layout_table_get_feature_tags
7171
_hb_ot_layout_script_get_language_tags
7272
_hb_ot_layout_language_get_feature_tags
73+
_hb_ot_name_list_names
74+
_hb_ot_name_get_utf16
7375
_hb_ot_var_get_axis_infos
7476
_hb_script_from_string
7577
_hb_feature_from_string
78+
_hb_language_from_string
79+
_hb_language_to_string
7680
_hb_set_create
7781
_hb_set_destroy
7882
_hb_set_get_population

hbjs.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ function hbjs(Module) {
4949
return 0x0;
5050
}
5151

52+
function _language_to_string(language) {
53+
var ptr = exports.hb_language_to_string(language);
54+
var str = utf8Decoder.decode(Module.HEAPU8.subarray(ptr, Module.HEAPU8.indexOf(0, ptr)));
55+
return str;
56+
}
57+
58+
function _language_from_string(str) {
59+
var languageStr = createAsciiString(str);
60+
var languagePtr = exports.hb_language_from_string(languageStr.ptr, -1);
61+
languageStr.free();
62+
return languagePtr;
63+
}
64+
5265
/**
5366
* Create an object representing a Harfbuzz blob.
5467
* @param {string} blob A blob of binary data (usually the contents of a font file).
@@ -234,6 +247,41 @@ function hbjs(Module) {
234247
}
235248
return tags;
236249
},
250+
/**
251+
* Return all names in the specified face's name table.
252+
**/
253+
listNames: function () {
254+
var numEntriesPtr = Module.stackAlloc(4);
255+
var entriesPtr = exports.hb_ot_name_list_names(ptr, numEntriesPtr);
256+
var numEntries = Module.HEAPU32[numEntriesPtr / 4];
257+
var entries = [];
258+
for (var i = 0; i < numEntries; i++) {
259+
// FIXME: this depends on the struct memory layout.
260+
// A more robust way would involve ading helper C functions to access
261+
// the struct and use them here.
262+
entries.push({
263+
nameId: Module.HEAPU32[(entriesPtr / 4) + (i * 3)],
264+
language: _language_to_string(Module.HEAPU32[(entriesPtr / 4) + (i * 3) + 2])
265+
});
266+
}
267+
return entries;
268+
},
269+
/**
270+
* Get the name of the specified face.
271+
* @param {number} nameId The ID of the name to get.
272+
* @param {string} language The language of the name to get.
273+
**/
274+
getName: function (nameId, language) {
275+
var languagePtr = _language_from_string(language);
276+
var nameLen = exports.hb_ot_name_get_utf16(ptr, nameId, languagePtr, 0, 0) + 1;
277+
var textSizePtr = Module.stackAlloc(4);
278+
var textPtr = exports.malloc(nameLen * 2);
279+
Module.HEAPU32[textSizePtr / 4] = nameLen;
280+
exports.hb_ot_name_get_utf16(ptr, nameId, languagePtr, textSizePtr, textPtr);
281+
var name = String.fromCharCode.apply(null, Module.HEAPU16.subarray(textPtr / 2, textPtr / 2 + nameLen - 1));
282+
exports.free(textPtr);
283+
return name;
284+
},
237285
/**
238286
* Free the object.
239287
*/

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ describe('Face', function () {
110110
}
111111
});
112112
});
113+
114+
it('listNames fetches all names', function () {
115+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
116+
face = hb.createFace(blob);
117+
let names = face.listNames();
118+
expect(names.length).to.equal(38);
119+
expect(names[0]).to.deep.equal({ nameId: 0, language: 'en' });
120+
expect(names[37]).to.deep.equal({ nameId: 278, language: 'en' });
121+
})
122+
123+
it('getName fetches a name', function () {
124+
blob = hb.createBlob(fs.readFileSync(path.join(__dirname, 'fonts/noto/NotoSans-Regular.ttf')));
125+
face = hb.createFace(blob);
126+
expect(face.getName(1, 'en')).to.equal('Noto Sans');
127+
expect(face.getName(256, 'en')).to.equal('florin symbol');
128+
})
113129
});
114130

115131
describe('Font', function () {

0 commit comments

Comments
 (0)