Skip to content

Commit

Permalink
Merge pull request #42 from chearon/utf16
Browse files Browse the repository at this point in the history
Change the buffer to use UTF16
  • Loading branch information
behdad authored Jun 28, 2021
2 parents 21959b7 + c2965c1 commit c141d58
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ clang \
-Wl,--export=hb_font_create \
-Wl,--export=hb_buffer_create \
-Wl,--export=hb_buffer_add_utf8 \
-Wl,--export=hb_buffer_add_utf16 \
-Wl,--export=hb_buffer_guess_segment_properties \
-Wl,--export=hb_buffer_set_direction \
-Wl,--export=hb_buffer_set_cluster_level \
Expand Down
15 changes: 13 additions & 2 deletions hbjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ function hbjs(instance) {
};
}

function createJsString(text) {
const ptr = exports.malloc(text.length * 2);
const words = new Uint16Array(exports.memory.buffer, ptr, text.length);
for (let i = 0; i < words.length; ++i) words[i] = text.charCodeAt(i);
return {
ptr: ptr,
length: words.length,
free: function () { exports.free(ptr); }
};
}

/**
* Create an object representing a Harfbuzz buffer.
**/
Expand All @@ -195,8 +206,8 @@ function hbjs(instance) {
* @param {string} text Text to be added to the buffer.
**/
addText: function (text) {
var str = createCString(text);
exports.hb_buffer_add_utf8(ptr, str.ptr, str.length, 0, str.length);
const str = createJsString(text);
exports.hb_buffer_add_utf16(ptr, str.ptr, str.length, 0, str.length);
str.free();
},
/**
Expand Down

0 comments on commit c141d58

Please sign in to comment.