Skip to content

Commit 579742a

Browse files
committed
Bind hb_font_get_glyph() and friends
Bind hb_font_get_glyph(), hb_font_get_nominal_glyph(), and hb_font_get_variation_glyph().
1 parent fe7b2a0 commit 579742a

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

harfbuzz.symbols

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ _hb_font_set_variations
3838
_hb_font_get_h_extents
3939
_hb_font_get_v_extents
4040
_hb_font_get_glyph_extents
41+
_hb_font_get_glyph
4142
_hb_font_get_glyph_from_name
43+
_hb_font_get_nominal_glyph
44+
_hb_font_get_variation_glyph
4245
_hb_font_get_glyph_h_advance
4346
_hb_font_get_glyph_v_advance
4447
_hb_font_get_glyph_h_origin

src/font.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,88 @@ export class Font {
295295
return extents;
296296
}
297297

298+
/**
299+
* Fetches the glyph ID for a Unicode code point in the specified
300+
* font, with an optional variation selector.
301+
*
302+
* If `variationSelector` is 0, it is equivalent to
303+
* {@link Font.nominalGlyph}; otherwise it is equivalent to
304+
* {@link Font.variationGlyph}.
305+
*
306+
* @param unicode The Unicode code point to query.
307+
* @param variationSelector A variation-selector code point.
308+
* @returns The glyph ID, or undefined if not found.
309+
*/
310+
glyph(unicode: number, variationSelector: number = 0): number | undefined {
311+
const sp = Module.stackSave();
312+
const glyphIdPtr = Module.stackAlloc(4);
313+
let glyphId: number | undefined;
314+
if (
315+
exports.hb_font_get_glyph(
316+
this.ptr,
317+
unicode,
318+
variationSelector,
319+
glyphIdPtr,
320+
)
321+
) {
322+
glyphId = Module.HEAPU32[glyphIdPtr / 4];
323+
}
324+
Module.stackRestore(sp);
325+
return glyphId;
326+
}
327+
328+
/**
329+
* Fetches the nominal glyph ID for a Unicode code point in the
330+
* specified font.
331+
*
332+
* This version of the function should not be used to fetch glyph IDs
333+
* for code points modified by variation selectors. For variation-selector
334+
* support, use {@link Font.variationGlyph} or {@link Font.glyph}.
335+
*
336+
* @param unicode The Unicode code point to query.
337+
* @returns The glyph ID, or undefined if not found.
338+
*/
339+
nominalGlyph(unicode: number): number | undefined {
340+
const sp = Module.stackSave();
341+
const glyphIdPtr = Module.stackAlloc(4);
342+
let glyphId: number | undefined;
343+
if (exports.hb_font_get_nominal_glyph(this.ptr, unicode, glyphIdPtr)) {
344+
glyphId = Module.HEAPU32[glyphIdPtr / 4];
345+
}
346+
Module.stackRestore(sp);
347+
return glyphId;
348+
}
349+
350+
/**
351+
* Fetches the glyph ID for a Unicode code point when followed by
352+
* by the specified variation-selector code point, in the specified
353+
* font.
354+
*
355+
* @param unicode The Unicode code point to query.
356+
* @param variationSelector The variation-selector code point to query.
357+
* @returns The glyph ID, or undefined if not found.
358+
*/
359+
variationGlyph(
360+
unicode: number,
361+
variationSelector: number,
362+
): number | undefined {
363+
const sp = Module.stackSave();
364+
const glyphIdPtr = Module.stackAlloc(4);
365+
let glyphId: number | undefined;
366+
if (
367+
exports.hb_font_get_variation_glyph(
368+
this.ptr,
369+
unicode,
370+
variationSelector,
371+
glyphIdPtr,
372+
)
373+
) {
374+
glyphId = Module.HEAPU32[glyphIdPtr / 4];
375+
}
376+
Module.stackRestore(sp);
377+
return glyphId;
378+
}
379+
298380
/**
299381
* Return glyph ID from name.
300382
* @param name Name of the requested glyph in the font.

test/index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,23 @@ describe("Font", function () {
491491
expect(font.glyphFromName("NonExistentGlyph")).to.equal(undefined);
492492
});
493493

494+
it("glyph/nominalGlyph/variationGlyph return ids for code points", function () {
495+
let blob = new hb.Blob(
496+
fs.readFileSync(path.join(__dirname, "fonts/noto/NotoSans-Regular.ttf")),
497+
);
498+
let face = new hb.Face(blob);
499+
let font = new hb.Font(face);
500+
const codepoint = "A".codePointAt(0);
501+
expect(font.nominalGlyph(codepoint)).to.equal(36);
502+
expect(font.nominalGlyph(0x10ffff)).to.equal(undefined);
503+
expect(font.variationGlyph(codepoint, 0xfe00)).to.equal(undefined);
504+
expect(font.glyph(codepoint)).to.equal(font.nominalGlyph(codepoint));
505+
expect(font.glyph(codepoint, 0xfe00)).to.equal(
506+
font.variationGlyph(codepoint, 0xfe00),
507+
);
508+
expect(font.glyph(0x10ffff)).to.equal(font.nominalGlyph(0x10ffff));
509+
});
510+
494511
it("setVariations affects advances", function () {
495512
let blob = new hb.Blob(
496513
fs.readFileSync(

0 commit comments

Comments
 (0)