Skip to content

Commit 5fc9036

Browse files
committed
Add createFontFuncs() and font.setFuncs()
Allows setting various font functions.
1 parent f842733 commit 5fc9036

File tree

3 files changed

+531
-4
lines changed

3 files changed

+531
-4
lines changed

hb.symbols

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ _hb_face_get_upem
2525
_hb_face_reference_table
2626
_hb_font_create
2727
_hb_font_destroy
28+
_hb_font_reference
2829
_hb_font_glyph_to_string
2930
_hb_font_set_scale
3031
_hb_font_set_variations
@@ -36,6 +37,24 @@ _hb_font_get_glyph_h_advance
3637
_hb_font_get_glyph_v_advance
3738
_hb_font_get_glyph_h_origin
3839
_hb_font_get_glyph_v_origin
40+
_hb_font_set_funcs
41+
_hb_font_funcs_create
42+
_hb_font_funcs_destroy
43+
_hb_font_funcs_set_glyph_extents_func
44+
_hb_font_funcs_set_glyph_from_name_func
45+
_hb_font_funcs_set_glyph_h_advance_func
46+
_hb_font_funcs_set_glyph_h_advances_func
47+
_hb_font_funcs_set_glyph_h_origin_func
48+
_hb_font_funcs_set_glyph_v_advance_func
49+
_hb_font_funcs_set_glyph_v_advances_func
50+
_hb_font_funcs_set_glyph_h_kerning_func
51+
_hb_font_funcs_set_glyph_v_origin_func
52+
_hb_font_funcs_set_nominal_glyph_func
53+
_hb_font_funcs_set_nominal_glyphs_func
54+
_hb_font_funcs_set_variation_glyph_func
55+
_hb_font_funcs_set_font_h_extents_func
56+
_hb_font_funcs_set_font_v_extents_func
57+
_hb_font_funcs_set_glyph_name_func
3958
_hb_font_draw_glyph
4059
_hb_draw_funcs_create
4160
_hb_draw_funcs_destroy

hbjs.js

Lines changed: 309 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ function hbjs(Module) {
150150
/**
151151
* Create an object representing a Harfbuzz font.
152152
* @param {object} blob An object returned from `createFace`.
153+
* @param {number} ptr Optional pointer to an existing font.
153154
**/
154-
function createFont(face) {
155-
var ptr = exports.hb_font_create(face.ptr);
155+
function createFont(face, ptr) {
156+
var ptr = ptr ? exports.hb_font_reference(ptr) : exports.hb_font_create(face.ptr);
156157
var drawFuncsPtr = null;
157158
var moveToPtr = null;
158159
var lineToPtr = null;
@@ -350,6 +351,13 @@ function hbjs(Module) {
350351
exports.free(vars);
351352
},
352353
/**
354+
* Set the font's font functions.
355+
* @param {object} fontFuncs The font functions.
356+
**/
357+
setFuncs: function (fontFuncs) {
358+
exports.hb_font_set_funcs(ptr, fontFuncs.ptr);
359+
},
360+
/**
353361
* Free the object.
354362
*/
355363
destroy: function () {
@@ -367,6 +375,304 @@ function hbjs(Module) {
367375
};
368376
}
369377

378+
/**
379+
* Create a object representing a HarfBuzz font functions.
380+
**/
381+
function createFontFuncs() {
382+
var ptr = exports.hb_font_funcs_create();
383+
return {
384+
ptr: ptr,
385+
/**
386+
* Set the font's glyph extents function.
387+
* @param {function} func The function to set.
388+
*
389+
* The function takes the following arguments:
390+
* @param {object} font The font.
391+
* @param {number} glyph The glyph ID.
392+
*
393+
* The function should return an object with the following properties, or null on failure:
394+
* @param {number} xBearing The x bearing of the glyph.
395+
* @param {number} yBearing The y bearing of the glyph.
396+
* @param {number} width The width of the glyph.
397+
* @param {number} height The height of the glyph.
398+
**/
399+
setGlyphExtentsFunc: function (func) {
400+
let funcPtr = addFunction(function (fontPtr, font_data, glyph, extentsPtr, user_data) {
401+
let font = createFont(null, fontPtr);
402+
let extents = func(font, glyph);
403+
font.destroy();
404+
if (extents) {
405+
Module.HEAP32[extentsPtr / 4] = extents.xBearing;
406+
Module.HEAP32[extentsPtr / 4 + 1] = extents.yBearing;
407+
Module.HEAP32[extentsPtr / 4 + 2] = extents.width;
408+
Module.HEAP32[extentsPtr / 4 + 3] = extents.height;
409+
return 1;
410+
}
411+
return 0;
412+
}, 'ippipp');
413+
exports.hb_font_funcs_set_glyph_extents_func(ptr, funcPtr, 0, 0);
414+
},
415+
/**
416+
* Set the font's glyph from name function.
417+
* @param {function} func The function to set.
418+
*
419+
* The function takes the following arguments:
420+
* @param {object} font The font.
421+
* @param {string} name The glyph name.
422+
*
423+
* The function should return an object with the following properties, or null on failure:
424+
* @param {number} glyph The glyph ID.
425+
**/
426+
setGlyphFromNameFunc: function (func) {
427+
let funcPtr = addFunction(function (fontPtr, font_data, namePtr, len, glyphPtr, user_data) {
428+
let font = createFont(null, fontPtr);
429+
let name = utf8Decoder.decode(Module.HEAPU8.subarray(namePtr, namePtr + len));
430+
let glyph = func(font, name);
431+
font.destroy();
432+
if (glyph) {
433+
Module.HEAPU32[glyphPtr / 4] = glyph;
434+
return 1;
435+
}
436+
return 0;
437+
}, 'ipppipp');
438+
exports.hb_font_funcs_set_glyph_from_name_func(ptr, funcPtr, 0, 0);
439+
},
440+
/**
441+
* Set the font's glyph horizontal advance function.
442+
* @param {function} func The function to set.
443+
*
444+
* The function takes the following arguments:
445+
* @param {object} font The font.
446+
* @param {number} glyph The glyph ID.
447+
*
448+
* The function should return the horizontal advance of the glyph.
449+
**/
450+
setGlyphHAdvanceFunc: function (func) {
451+
let funcPtr = addFunction(function (fontPtr, font_data, glyph, user_data) {
452+
let font = createFont(null, fontPtr);
453+
let advance = func(font, glyph);
454+
font.destroy();
455+
return advance;
456+
}, 'ippip');
457+
exports.hb_font_funcs_set_glyph_h_advance_func(ptr, funcPtr, 0, 0);
458+
},
459+
/**
460+
* Set the font's glyph vertical advance function.
461+
* @param {function} func The function to set.
462+
*
463+
* The function takes the following arguments:
464+
* @param {object} font The font.
465+
* @param {number} glyph The glyph ID.
466+
*
467+
* The function should return the vertical advance of the glyph.
468+
**/
469+
setGlyphVAdvanceFunc: function (func) {
470+
let funcPtr = addFunction(function (fontPtr, font_data, glyph, user_data) {
471+
let font = createFont(null, fontPtr);
472+
let advance = func(font, glyph);
473+
font.destroy();
474+
return advance;
475+
}, 'ippip');
476+
exports.hb_font_funcs_set_glyph_v_advance_func(ptr, funcPtr, 0, 0);
477+
},
478+
/**
479+
* Set the font's glyph horizontal origin function.
480+
* @param {function} func The function to set.
481+
*
482+
* The function takes the following arguments:
483+
* @param {object} font The font.
484+
* @param {number} glyph The glyph ID.
485+
*
486+
* The function should return the x and y horizontal origin of the glyph, or null on failure.
487+
**/
488+
setGlyphHOriginFunc: function (func) {
489+
let funcPtr = addFunction(function (fontPtr, font_data, glyph, xPtr, yPtr, user_data) {
490+
let font = createFont(null, fontPtr);
491+
let origin = func(font, glyph);
492+
font.destroy();
493+
if (origin) {
494+
Module.HEAP32[xPtr / 4] = origin[0];
495+
Module.HEAP32[yPtr / 4] = origin[1];
496+
return 1;
497+
}
498+
return 0;
499+
}, 'ippippp');
500+
exports.hb_font_funcs_set_glyph_h_origin_func(ptr, funcPtr, 0, 0);
501+
},
502+
/**
503+
* Set the font's glyph vertical origin function.
504+
* @param {function} func The function to set.
505+
*
506+
* The function takes the following arguments:
507+
* @param {object} font The font.
508+
* @param {number} glyph The glyph ID.
509+
*
510+
* The function should return the x and y vertical origin of the glyph, or null on failure.
511+
**/
512+
setGlyphVOriginFunc: function (func) {
513+
let funcPtr = addFunction(function (fontPtr, font_data, glyph, xPtr, yPtr, user_data) {
514+
let font = createFont(null, fontPtr);
515+
let origin = func(font, glyph);
516+
font.destroy();
517+
if (origin) {
518+
Module.HEAP32[xPtr / 4] = origin[0];
519+
Module.HEAP32[yPtr / 4] = origin[1];
520+
return 1;
521+
}
522+
return 0;
523+
}, 'ippippp');
524+
exports.hb_font_funcs_set_glyph_v_origin_func(ptr, funcPtr, 0, 0);
525+
},
526+
/**
527+
* Set the font's glyph horizontal kerning function.
528+
* @param {function} func The function to set.
529+
*
530+
* The function takes the following arguments:
531+
* @param {object} font The font.
532+
* @param {number} firstGlyph The first glyph ID.
533+
* @param {number} secondGlyph The second glyph ID.
534+
*
535+
* The function should return the horizontal kerning of the glyphs.
536+
**/
537+
setGlyphHKerningFunc: function (func) {
538+
let funcPtr = addFunction(function (fontPtr, font_data, firstGlyph, secondGlyph, user_data) {
539+
let font = createFont(null, fontPtr);
540+
let kerning = func(font, firstGlyph, secondGlyph);
541+
font.destroy();
542+
return kerning;
543+
}, 'ippiip');
544+
exports.hb_font_funcs_set_glyph_h_kerning_func(ptr, funcPtr, 0, 0);
545+
},
546+
/**
547+
* Set the font's glyph name function.
548+
* @param {function} func The function to set.
549+
*
550+
* The function takes the following arguments:
551+
* @param {object} font The font.
552+
* @param {number} glyph The glyph ID.
553+
*
554+
* The function should return the name of the glyph, or null on failure.
555+
**/
556+
setGlyphNameFunc: function (func) {
557+
let funcPtr = addFunction(function (fontPtr, font_data, glyph, namePtr, size, user_data) {
558+
let font = createFont(null, fontPtr);
559+
let name = func(font, glyph);
560+
font.destroy();
561+
if (name) {
562+
utf8Encoder.encodeInto(name, Module.HEAPU8.subarray(namePtr, namePtr + size));
563+
return 1;
564+
}
565+
return 0;
566+
}, 'ippipip');
567+
exports.hb_font_funcs_set_glyph_name_func(ptr, funcPtr, 0, 0);
568+
},
569+
/**
570+
* Set the font's nominal glyph function.
571+
* @param {function} func The function to set.
572+
*
573+
* The function takes the following arguments:
574+
* @param {object} font The font.
575+
* @param {number} unicode The unicode.
576+
*
577+
* The function should return the nominal glyph of the unicode, or null on failure.
578+
**/
579+
setNominalGlyphFunc: function (func) {
580+
let funcPtr = addFunction(function (fontPtr, font_data, unicode, glyphPtr, user_data) {
581+
let font = createFont(null, fontPtr);
582+
let glyph = func(font, unicode);
583+
font.destroy();
584+
if (glyph) {
585+
Module.HEAPU32[glyphPtr / 4] = glyph;
586+
return 1;
587+
}
588+
return 0;
589+
}, 'ippipp');
590+
exports.hb_font_funcs_set_nominal_glyph_func(ptr, funcPtr, 0, 0);
591+
},
592+
/**
593+
* Set the font's variation glyph function.
594+
* @param {function} func The function to set.
595+
*
596+
* The function takes the following arguments:
597+
* @param {object} font The font.
598+
* @param {number} unicode The unicode.
599+
* @param {number} variationSelector The variation selector.
600+
*
601+
* The function should return the variation glyph of the unicode, or null on failure.
602+
**/
603+
setVariationGlyphFunc: function (func) {
604+
let funcPtr = addFunction(function (fontPtr, font_data, unicode, variationSelector, glyphPtr, user_data) {
605+
let font = createFont(null, fontPtr);
606+
let glyph = func(font, unicode, variationSelector);
607+
font.destroy();
608+
if (glyph) {
609+
Module.HEAPU32[glyphPtr / 4] = glyph;
610+
return 1;
611+
}
612+
return 0;
613+
}, 'ippiipp');
614+
exports.hb_font_funcs_set_variation_glyph_func(ptr, funcPtr, 0, 0);
615+
},
616+
/**
617+
* Set the font's horizontal extents function.
618+
* @param {function} func The function to set.
619+
*
620+
* The function takes the following arguments:
621+
* @param {object} font The font.
622+
*
623+
* The function should return an object with the following properties, or null on failure:
624+
* @param {number} ascender The ascender of the font.
625+
* @param {number} descender The descender of the font.
626+
* @param {number} lineGap The line gap of the font.
627+
**/
628+
setFontHExtentsFunc: function (func) {
629+
let funcPtr = addFunction(function (fontPtr, font_data, extentsPtr, user_data) {
630+
let font = createFont(null, fontPtr);
631+
let extents = func(font);
632+
font.destroy();
633+
if (extents) {
634+
Module.HEAP32[extentsPtr / 4] = extents.ascender;
635+
Module.HEAP32[extentsPtr / 4 + 1] = extents.descender;
636+
Module.HEAP32[extentsPtr / 4 + 2] = extents.lineGap;
637+
return 1;
638+
}
639+
return 0;
640+
}, 'ipppp');
641+
exports.hb_font_funcs_set_font_h_extents_func(ptr, funcPtr, 0, 0);
642+
},
643+
/**
644+
* Set the font's vertical extents function.
645+
* @param {function} func The function to set.
646+
*
647+
* The function takes the following arguments:
648+
* @param {object} font The font.
649+
*
650+
* The function should return an object with the following properties, or null on failure:
651+
* @param {number} ascender The ascender of the font.
652+
* @param {number} descender The descender of the font.
653+
* @param {number} lineGap The line gap of the font.
654+
**/
655+
setFontVExtentsFunc: function (func) {
656+
let funcPtr = addFunction(function (fontPtr, font_data, extentsPtr, user_data) {
657+
let font = createFont(null, fontPtr);
658+
let extents = func(font);
659+
font.destroy();
660+
if (extents) {
661+
Module.HEAP32[extentsPtr / 4] = extents.ascender;
662+
Module.HEAP32[extentsPtr / 4 + 1] = extents.descender;
663+
Module.HEAP32[extentsPtr / 4 + 2] = extents.lineGap;
664+
return 1;
665+
}
666+
return 0;
667+
}, 'ipppp');
668+
exports.hb_font_funcs_set_font_v_extents_func(ptr, funcPtr, 0, 0);
669+
},
670+
destroy: function () {
671+
exports.hb_font_funcs_destroy(ptr);
672+
}
673+
};
674+
}
675+
370676
/**
371677
* Use when you know the input range should be ASCII.
372678
* Faster than encoding to UTF-8
@@ -644,6 +950,7 @@ function hbjs(Module) {
644950
createBlob: createBlob,
645951
createFace: createFace,
646952
createFont: createFont,
953+
createFontFuncs: createFontFuncs,
647954
createBuffer: createBuffer,
648955
shape: shape,
649956
shapeWithTrace: shapeWithTrace,

0 commit comments

Comments
 (0)