PIXI.TextMetrics

class TextMetrics

The TextMetrics object represents the measurement of a block of text with a specified style.

let style = new PIXI.TextStyle({fontFamily : 'Arial', fontSize: 24, fill : 0xff1010, align : 'center'})
let textMetrics = PIXI.TextMetrics.measureText('Your text', style)

Constructor


new PIXI.TextMetrics(text: string, style: PIXI.TextStyle, width: number, height: number, lines: string[], lineWidths: number[], lineHeight: number, maxLineWidth: number, fontProperties: PIXI.IFontMetrics) → {}
Parameters:
Name Type Description
text string

the text that was measured

style PIXI.TextStyle

the style that was measured

width number

the measured width of the text

height number

the measured height of the text

lines string[]

an array of the lines of text broken by new lines and wrapping if specified in style

lineWidths number[]

an array of the line widths for each line matched to lines

lineHeight number

the measured line height for this style

maxLineWidth number

the maximum line width for all measured lines

fontProperties PIXI.IFontMetrics

the font properties object from TextMetrics.measureFont

Summary


Properties from TextMetrics

number
static BASELINE_MULTIPLIER = 1.4
string
static BASELINE_SYMBOL = M
number
static HEIGHT_MULTIPLIER = 2.00
string
static METRICS_STRING = |ÉqÅ
PIXI.IFontMetrics
fontProperties
number
height

The measured height of the text.

number
lineHeight

The measured line height for this style.

string[]
lines

An array of lines of the text broken by new lines and wrapping is specified in style.

number[]
lineWidths

An array of the line widths for each line matched to lines.

number
maxLineWidth

The maximum line width for all measured lines.

PIXI.TextStyle
style

The style that was measured.

string
text

The text that was measured.

number
width

The measured width of the text.

Methods from TextMetrics

boolean
static canBreakChars(_char: string, _nextChar: string, _token: string, _index: number, _breakWords: boolean)

Overridable helper method used internally by TextMetrics, exposed to allow customizing the class's behavior.

boolean
static canBreakWords(_token: string, breakWords: boolean)

Overridable helper method used internally by TextMetrics, exposed to allow customizing the class's behavior.

void
static clearMetrics(font: string)
boolean
static isBreakingSpace(char: string, _nextChar: string)

Determines if char is a breaking whitespace.

IFontMetrics
static measureFont(font: string)
PIXI.TextMetrics
static measureText(text: string, style: PIXI.TextStyle, wordWrap: boolean, canvas: HTMLCanvasElement | OffscreenCanvas)
string[]
static wordWrapSplit(token: string)

Overridable helper method used internally by TextMetrics, exposed to allow customizing the class's behavior.

Public Properties


BASELINE_MULTIPLIER TextMetrics.ts:794
static BASELINE_MULTIPLIER: number = 1.4

Baseline multiplier for calculate font metrics.

BASELINE_SYMBOL TextMetrics.ts:784
static BASELINE_SYMBOL: string = M

Baseline symbol for calculate font metrics.

HEIGHT_MULTIPLIER TextMetrics.ts:804
static HEIGHT_MULTIPLIER: number = 2.00

Height multiplier for setting height of canvas to calculate font metrics.

METRICS_STRING TextMetrics.ts:773
static METRICS_STRING: string = |ÉqÅ

String used for calculate font metrics. These characters are all tall to help calculate the height required for text.

fontProperties TextMetrics.ts:48
fontProperties: PIXI.IFontMetrics

The font properties object from TextMetrics.measureFont.

height TextMetrics.ts:33
height: number

The measured height of the text.

lineHeight TextMetrics.ts:42
lineHeight: number

The measured line height for this style.

lines TextMetrics.ts:36
lines: string[]

An array of lines of the text broken by new lines and wrapping is specified in style.

lineWidths TextMetrics.ts:39
lineWidths: number[]

An array of the line widths for each line matched to lines.

maxLineWidth TextMetrics.ts:45
maxLineWidth: number

The maximum line width for all measured lines.

style TextMetrics.ts:27
style: PIXI.TextStyle

The style that was measured.

text TextMetrics.ts:24
text: string

The text that was measured.

width TextMetrics.ts:30
width: number

The measured width of the text.

Public Methods


canBreakChars TextMetrics.ts:543
static canBreakChars(_char: string, _nextChar: string, _token: string, _index: number, _breakWords: boolean) → {boolean}

Overridable helper method used internally by TextMetrics, exposed to allow customizing the class's behavior.

It allows one to determine whether a pair of characters should be broken by newlines For example certain characters in CJK langs or numbers. It must return a boolean.

Parameters:
Name Type Description
_char string

The character

_nextChar string

The next character

_token string

The token/word the characters are from

_index number

The index in the token of the char

_breakWords boolean

The style attr break words

Returns:
Type Description
boolean

whether to break word or not

canBreakWords TextMetrics.ts:528
static canBreakWords(_token: string, breakWords: boolean) → {boolean}

Overridable helper method used internally by TextMetrics, exposed to allow customizing the class's behavior.

It allows one to customise which words should break Examples are if the token is CJK or numbers. It must return a boolean.

Parameters:
Name Type Description
_token string

The token

breakWords boolean

The style attr break words

Returns:
Type Description
boolean

Whether to break word or not

clearMetrics TextMetrics.ts:687
static clearMetrics(font: string) → {void}

Clear font metrics in metrics cache.

Parameters:
Name Type Attributes Default Description
font string

<optional>

''

font name. If font name not set then clear cache for all fonts.

Returns:
Type Description
void
isBreakingSpace TextMetrics.ts:464
static isBreakingSpace(char: string, _nextChar: string) → {boolean}

Determines if char is a breaking whitespace.

It allows one to determine whether char should be a breaking whitespace For example certain characters in CJK langs or numbers. It must return a boolean.

Parameters:
Name Type Attributes Description
char string

The character

_nextChar string

<optional>

The next character

Returns:
Type Description
boolean

True if whitespace, False otherwise.

measureFont TextMetrics.ts:580
static measureFont(font: string) → {IFontMetrics}

Calculates the ascent, descent and fontSize of a given font-style

Parameters:
Name Type Description
font string

String representing the style of the font

Returns:
Type Description
IFontMetrics

Font properties object

measureText TextMetrics.ts:92
static measureText(text: string, style: PIXI.TextStyle, wordWrap: boolean, canvas: HTMLCanvasElement | OffscreenCanvas) → {PIXI.TextMetrics}

Measures the supplied string of text and returns a Rectangle.

Parameters:
Name Type Attributes Description
text string

The text to measure.

style PIXI.TextStyle

The text style to use for measuring

wordWrap boolean

<optional>

Override for if word-wrap should be applied to the text.

canvas HTMLCanvasElement | OffscreenCanvas

optional specification of the canvas to use for measuring.

Returns:
Type Description
PIXI.TextMetrics

Measured width and height of the text.

wordWrapSplit TextMetrics.ts:563
static wordWrapSplit(token: string) → {string[]}

Overridable helper method used internally by TextMetrics, exposed to allow customizing the class's behavior.

It is called when a token (usually a word) has to be split into separate pieces in order to determine the point to break a word. It must return an array of characters.


 // Correctly splits emojis, eg "🤪🤪" will result in two element array, each with one emoji.
 TextMetrics.wordWrapSplit = (token) => [...token];
Parameters:
Name Type Description
token string

The token to split

Returns:
Type Description
string[]

The characters of the token


Powered by webdoc!