src/toolbox/text/Text.ts
Properties |
Methods |
|
Public autoClear |
Default value : false
|
Inherited from
ToolBox
|
Defined in
ToolBox:9
|
Public history |
Type : HistoryCallback[]
|
Default value : []
|
Inherited from
ToolBox
|
Defined in
ToolBox:8
|
Public Readonly layer |
Type : Layer
|
Inherited from
ToolBox
|
Defined in
ToolBox:9
|
Public getFonts |
getFonts()
|
Defined in src/toolbox/text/Text.ts:18
|
Returns :
any
|
Public hasFont | ||||||
hasFont(name: string)
|
||||||
Defined in src/toolbox/text/Text.ts:22
|
||||||
Parameters :
Returns :
any
|
Public measure | ||||||
measure(text: string)
|
||||||
Defined in src/toolbox/text/Text.ts:92
|
||||||
Parameters :
Returns :
any
|
Public registerFont | |||||||||
registerFont(font: Buffer, nameAlias?: string)
|
|||||||||
Defined in src/toolbox/text/Text.ts:6
|
|||||||||
Parameters :
Returns :
any
|
Public registerFontPath |
registerFontPath(fontPath: string, nameAlias?: string)
|
Defined in src/toolbox/text/Text.ts:10
|
Returns :
any
|
Public registerFontsDir | ||||||
registerFontsDir(fontDir: string)
|
||||||
Defined in src/toolbox/text/Text.ts:14
|
||||||
Parameters :
Returns :
any
|
Public setColor | ||||||
setColor(color: string | CanvasGradient | CanvasPattern)
|
||||||
Defined in src/toolbox/text/Text.ts:58
|
||||||
Parameters :
Returns :
this
|
Public setDirection | ||||||
setDirection(direction: "inherit" | "ltr" | "rtl")
|
||||||
Defined in src/toolbox/text/Text.ts:26
|
||||||
Parameters :
Returns :
this
|
Public setFont |
setFont(name: string, size: string, style?: string)
|
Defined in src/toolbox/text/Text.ts:50
|
Returns :
this
|
Public setStrokeColor | ||||||
setStrokeColor(color: string | CanvasGradient | CanvasPattern)
|
||||||
Defined in src/toolbox/text/Text.ts:66
|
||||||
Parameters :
Returns :
this
|
Public setTextAlignment | ||||||
setTextAlignment(alignment: "center" | "end" | "left" | "right" | "start")
|
||||||
Defined in src/toolbox/text/Text.ts:34
|
||||||
Parameters :
Returns :
this
|
Public setTextBaseline | ||||||
setTextBaseline(alignment: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top")
|
||||||
Defined in src/toolbox/text/Text.ts:42
|
||||||
Parameters :
Returns :
this
|
Public strokeText |
strokeText(text: string, x: number, y: number, maxWidth?: number)
|
Defined in src/toolbox/text/Text.ts:83
|
Returns :
this
|
Public writeText |
writeText(text: string, x: number, y: number, maxWidth?: number)
|
Defined in src/toolbox/text/Text.ts:74
|
Returns :
this
|
Public render |
render()
|
Inherited from
ToolBox
|
Defined in
ToolBox:27
|
Returns :
void
|
Public restore |
restore()
|
Inherited from
ToolBox
|
Defined in
ToolBox:19
|
Returns :
this
|
Public save |
save()
|
Inherited from
ToolBox
|
Defined in
ToolBox:11
|
Returns :
this
|
import { ToolBox } from "../base/ToolBox";
import { GlobalFonts } from "@napi-rs/canvas";
import { makeArgs } from "../../utils/makeArgs";
export class TextTool extends ToolBox {
public registerFont(font: Buffer, nameAlias?: string) {
return GlobalFonts.register(font, nameAlias);
}
public registerFontPath(fontPath: string, nameAlias?: string) {
return GlobalFonts.registerFromPath(fontPath, nameAlias);
}
public registerFontsDir(fontDir: string) {
return GlobalFonts.loadFontsFromDir(fontDir);
}
public getFonts() {
return GlobalFonts.families;
}
public hasFont(name: string) {
return GlobalFonts.has(name);
}
public setDirection(direction: "inherit" | "ltr" | "rtl") {
this.history.push((ctx) => {
ctx.direction = direction;
});
return this;
}
public setTextAlignment(alignment: "center" | "end" | "left" | "right" | "start") {
this.history.push((ctx) => {
ctx.textAlign = alignment;
});
return this;
}
public setTextBaseline(alignment: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top") {
this.history.push((ctx) => {
ctx.textBaseline = alignment;
});
return this;
}
public setFont(name: string, size: string, style?: string) {
this.history.push((ctx) => {
ctx.font = `${style ? style + " " : ""}${name}${size ? " " + size : ""}`;
});
return this;
}
public setColor(color: string | CanvasGradient | CanvasPattern) {
this.history.push((ctx) => {
ctx.fillStyle = color;
});
return this;
}
public setStrokeColor(color: string | CanvasGradient | CanvasPattern) {
this.history.push((ctx) => {
ctx.strokeStyle = color;
});
return this;
}
public writeText(text: string, x: number, y: number, maxWidth?: number) {
this.history.push((ctx) => {
// @ts-expect-error
ctx.fillText(...makeArgs((el) => el != null, [text, x, y, maxWidth]));
});
return this;
}
public strokeText(text: string, x: number, y: number, maxWidth?: number) {
this.history.push((ctx) => {
// @ts-expect-error
ctx.strokeText(...makeArgs((el) => el != null, [text, x, y, maxWidth]));
});
return this;
}
public measure(text: string) {
return this.layer.utils.measureText(text);
}
}