File

src/toolbox/text/Text.ts

Extends

ToolBox

Index

Properties
Methods

Properties

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

Methods

Public getFonts
getFonts()
Returns : any
Public hasFont
hasFont(name: string)
Parameters :
Name Type Optional
name string No
Returns : any
Public measure
measure(text: string)
Parameters :
Name Type Optional
text string No
Returns : any
Public registerFont
registerFont(font: Buffer, nameAlias?: string)
Parameters :
Name Type Optional
font Buffer No
nameAlias string Yes
Returns : any
Public registerFontPath
registerFontPath(fontPath: string, nameAlias?: string)
Parameters :
Name Type Optional
fontPath string No
nameAlias string Yes
Returns : any
Public registerFontsDir
registerFontsDir(fontDir: string)
Parameters :
Name Type Optional
fontDir string No
Returns : any
Public setColor
setColor(color: string | CanvasGradient | CanvasPattern)
Parameters :
Name Type Optional
color string | CanvasGradient | CanvasPattern No
Returns : this
Public setDirection
setDirection(direction: "inherit" | "ltr" | "rtl")
Parameters :
Name Type Optional
direction "inherit" | "ltr" | "rtl" No
Returns : this
Public setFont
setFont(name: string, size: string, style?: string)
Parameters :
Name Type Optional
name string No
size string No
style string Yes
Returns : this
Public setStrokeColor
setStrokeColor(color: string | CanvasGradient | CanvasPattern)
Parameters :
Name Type Optional
color string | CanvasGradient | CanvasPattern No
Returns : this
Public setTextAlignment
setTextAlignment(alignment: "center" | "end" | "left" | "right" | "start")
Parameters :
Name Type Optional
alignment "center" | "end" | "left" | "right" | "start" No
Returns : this
Public setTextBaseline
setTextBaseline(alignment: "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top")
Parameters :
Name Type Optional
alignment "alphabetic" | "bottom" | "hanging" | "ideographic" | "middle" | "top" No
Returns : this
Public strokeText
strokeText(text: string, x: number, y: number, maxWidth?: number)
Parameters :
Name Type Optional
text string No
x number No
y number No
maxWidth number Yes
Returns : this
Public writeText
writeText(text: string, x: number, y: number, maxWidth?: number)
Parameters :
Name Type Optional
text string No
x number No
y number No
maxWidth number Yes
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);
    }
}

results matching ""

    No results matching ""