Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript typings #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
74 changes: 74 additions & 0 deletions hbjs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @fileoverview Created manually by @tomasdev using:
* ```
* npx -p typescript tsc hbjs.js --declaration --allowJs --emitDeclarationOnly
* ```
* And then renaming the output to re-use types, and fix types (i.e. updating
* the type if comment documentation had it as _object_ but in reality it was
* _string_) to remove all `any` usages.
*/

declare type HarfbuzzPointer = string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be number

declare type HarfbuzzModule = WebAssembly.Instance;
declare interface HarfbuzzBlob {
ptr: HarfbuzzPointer;
destroy: () => void;
}
declare interface HarfbuzzAxis {
min: number;
default: number;
max: number;
}
declare interface HarfbuzzFace {
ptr: HarfbuzzPointer;
reference_table: (table: string) => Uint8Array;
getAxisInfos: () => Record<string, HarfbuzzAxis>;
destroy: () => void;
}
declare interface HarfbuzzVariations {
[axisName: string]: number;
}
declare interface HarfbuzzFont {
ptr: HarfbuzzPointer;
glyphToPath: (glyphId: number) => string;
glyphToJson: (glyphId: number) => Array<{type: string; values: number[];}>;
setScale: (xScale: number, yScale: number) => void;
setVariations: (variations: HarfbuzzVariations) => void;
destroy: () => void;
}
declare type HarfbuzzDirection = 'ltr' | 'rtl' | 'ttb' | 'btt';
declare type HarfbuzzFlag = 'BOT' | 'EOT' | 'PRESERVE_DEFAULT_IGNORABLES' |
'REMOVE_DEFAULT_IGNORABLES' | 'DO_NOT_INSERT_DOTTED_CIRCLE';
declare interface HarfbuzzBuffer {
ptr: HarfbuzzPointer;
addText: (text: string) => void;
guessSegmentProperties: () =>
any; // any = return of exports.hb_buffer_guess_segment_properties(ptr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C++ hb_buffer_guess_segment_properties returns void, this can just be void too

setDirection: (dir: HarfbuzzDirection) => void;
setFlags: (flags: HarfbuzzFlag[]) => void;
setLanguage: (language: string) => void;
setScript: (script: string) => void;
setClusterLevel: (level: number) => void;
json: (font?: HarfbuzzFont) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function doesn't have an argument, should just be ()

g: number;
cl: number;
ax: number;
ay: number;
dx: number;
dy: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add flags: number; (recently added, not yet released)?

}[];
destroy: () => void;
}
declare type HarfbuzzJson = object;
declare interface HarfbuzzJs {
createBlob: (blob: ArrayBufferLike) => HarfbuzzBlob;
createFace: (blob: HarfbuzzBlob, index: number) => HarfbuzzFace;
createFont: (face: HarfbuzzFace) => HarfbuzzFont;
createBuffer: () => HarfbuzzBuffer;
shape:
(font: HarfbuzzFont, buffer: HarfbuzzBuffer, features?: string) => void;
shapeWithTrace:
(font: HarfbuzzFont, buffer: HarfbuzzBuffer, features: string,
stop_at: number, stop_phase: number) => HarfbuzzJson;
}
declare function hbjs(instance: HarfbuzzModule): HarfbuzzJs;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most people will be doing require('harfbuzzjs'), but this doesn't appear to make that typed. It looks like it's for require('./hbjs'). Does there need to be another file index.d.ts?

I tried a few things but couldn't get hbjs.d.ts to work (for example correct hover types in VS Code). If it helps, a while ago I wrote my own types via a root-level types.d.ts that looks like this:

declare namespace HarfBuzzJsInit {
  type HbFace = {};
  ...
  type Harfbuzz = {
    createBlob
    ...
  };
}

// declaring a const with the same name as the namespace above
// lets people do `import {HbFace} from 'harfbuzzjs'
declare const HarfbuzzJsInit: Promise<HarfBuzzJsInit.HarfBuzz>;

export = HarfBuzzJsInit;