Skip to content

Commit 16c4659

Browse files
committed
Add Font.face property
1 parent cce8c12 commit 16c4659

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

harfbuzz.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ _hb_face_create
2727
_hb_face_collect_unicodes
2828
_hb_face_destroy
2929
_hb_face_get_upem
30+
_hb_face_reference
3031
_hb_face_reference_table
3132
_hb_font_create
3233
_hb_font_destroy
3334
_hb_font_reference
35+
_hb_font_get_face
3436
_hb_font_create_sub_font
3537
_hb_font_glyph_to_string
3638
_hb_font_set_scale

src/face.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ export class Face {
3939
* @param index The index of the font in the blob. (0 for most files,
4040
* or a 0-indexed font number if the `blob` came from a font collection file.)
4141
*/
42-
constructor(blob: Blob, index: number = 0) {
43-
this.ptr = exports.hb_face_create(blob.ptr, index);
42+
constructor(blob: Blob, index?: number);
43+
/** @internal Wrap an existing face pointer. */
44+
constructor(existingPtr: number);
45+
constructor(arg: Blob | number, index: number = 0) {
46+
if (typeof arg === "number") {
47+
this.ptr = exports.hb_face_reference(arg);
48+
} else {
49+
this.ptr = exports.hb_face_create(arg.ptr, index);
50+
}
4451
this.upem = exports.hb_face_get_upem(this.ptr);
4552
track(this, exports.hb_face_destroy);
4653
}

src/font.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "./helpers";
88
import type { FontExtents, GlyphExtents, SvgPathCommand } from "./types";
99
import type { Direction } from "./buffer";
10-
import type { Face } from "./face";
10+
import { Face } from "./face";
1111
import type { FontFuncs } from "./font-funcs";
1212
import type { Variation } from "./variation";
1313

@@ -29,6 +29,7 @@ interface DrawPtrs {
2929
*/
3030
export class Font {
3131
readonly ptr: number;
32+
private _face?: Face;
3233
private drawPtrs: DrawPtrs = {
3334
pathBuffer: "",
3435
};
@@ -44,6 +45,7 @@ export class Font {
4445
this.ptr = exports.hb_font_reference(arg);
4546
} else {
4647
this.ptr = exports.hb_font_create(arg.ptr);
48+
this._face = arg;
4749
}
4850
const ptr = this.ptr;
4951
const drawState = this.drawPtrs;
@@ -60,6 +62,14 @@ export class Font {
6062
});
6163
}
6264

65+
/** The {@link Face} associated with this font. */
66+
get face(): Face {
67+
if (!this._face) {
68+
this._face = new Face(exports.hb_font_get_face(this.ptr));
69+
}
70+
return this._face;
71+
}
72+
6373
/**
6474
* Create a sub font that inherits this font's properties.
6575
* @returns A new Font object representing the sub font.

test/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,16 @@ describe("Face", function () {
363363
});
364364

365365
describe("Font", function () {
366+
it("exposes its face", function () {
367+
let blob = new hb.Blob(
368+
fs.readFileSync(path.join(__dirname, "fonts/noto/NotoSans-Regular.ttf")),
369+
);
370+
let face = new hb.Face(blob);
371+
let font = new hb.Font(face);
372+
expect(font.face).to.equal(face);
373+
expect(font.subFont().face.ptr).to.equal(face.ptr);
374+
});
375+
366376
it("subFont creates a sub font", function () {
367377
let blob = new hb.Blob(
368378
fs.readFileSync(path.join(__dirname, "fonts/noto/NotoSans-Regular.ttf")),

0 commit comments

Comments
 (0)