diff --git a/harfbuzz.symbols b/harfbuzz.symbols index 3315155..ef27bc7 100644 --- a/harfbuzz.symbols +++ b/harfbuzz.symbols @@ -27,10 +27,12 @@ _hb_face_create _hb_face_collect_unicodes _hb_face_destroy _hb_face_get_upem +_hb_face_reference _hb_face_reference_table _hb_font_create _hb_font_destroy _hb_font_reference +_hb_font_get_face _hb_font_create_sub_font _hb_font_glyph_to_string _hb_font_set_scale diff --git a/src/face.ts b/src/face.ts index 0778d18..9f92f58 100644 --- a/src/face.ts +++ b/src/face.ts @@ -39,8 +39,15 @@ export class Face { * @param index The index of the font in the blob. (0 for most files, * or a 0-indexed font number if the `blob` came from a font collection file.) */ - constructor(blob: Blob, index: number = 0) { - this.ptr = exports.hb_face_create(blob.ptr, index); + constructor(blob: Blob, index?: number); + /** @internal Wrap an existing face pointer. */ + constructor(existingPtr: number); + constructor(arg: Blob | number, index: number = 0) { + if (typeof arg === "number") { + this.ptr = exports.hb_face_reference(arg); + } else { + this.ptr = exports.hb_face_create(arg.ptr, index); + } this.upem = exports.hb_face_get_upem(this.ptr); track(this, exports.hb_face_destroy); } diff --git a/src/font.ts b/src/font.ts index d14d87c..91ea76c 100644 --- a/src/font.ts +++ b/src/font.ts @@ -7,7 +7,7 @@ import { } from "./helpers"; import type { FontExtents, GlyphExtents, SvgPathCommand } from "./types"; import type { Direction } from "./buffer"; -import type { Face } from "./face"; +import { Face } from "./face"; import type { FontFuncs } from "./font-funcs"; import type { Variation } from "./variation"; @@ -29,6 +29,7 @@ interface DrawPtrs { */ export class Font { readonly ptr: number; + private _face?: Face; private drawPtrs: DrawPtrs = { pathBuffer: "", }; @@ -44,6 +45,7 @@ export class Font { this.ptr = exports.hb_font_reference(arg); } else { this.ptr = exports.hb_font_create(arg.ptr); + this._face = arg; } const ptr = this.ptr; const drawState = this.drawPtrs; @@ -60,6 +62,14 @@ export class Font { }); } + /** The {@link Face} associated with this font. */ + get face(): Face { + if (!this._face) { + this._face = new Face(exports.hb_font_get_face(this.ptr)); + } + return this._face; + } + /** * Create a sub font that inherits this font's properties. * @returns A new Font object representing the sub font. diff --git a/test/index.test.js b/test/index.test.js index c8020d8..7cd44b1 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -363,6 +363,16 @@ describe("Face", function () { }); describe("Font", function () { + it("exposes its face", function () { + let blob = new hb.Blob( + fs.readFileSync(path.join(__dirname, "fonts/noto/NotoSans-Regular.ttf")), + ); + let face = new hb.Face(blob); + let font = new hb.Font(face); + expect(font.face).to.equal(face); + expect(font.subFont().face.ptr).to.equal(face.ptr); + }); + it("subFont creates a sub font", function () { let blob = new hb.Blob( fs.readFileSync(path.join(__dirname, "fonts/noto/NotoSans-Regular.ttf")),