Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions harfbuzz.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/face.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 11 additions & 1 deletion src/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -29,6 +29,7 @@ interface DrawPtrs {
*/
export class Font {
readonly ptr: number;
private _face?: Face;
private drawPtrs: DrawPtrs = {
pathBuffer: "",
};
Expand All @@ -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;
Expand All @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down