Options
All
  • Public
  • Public/Protected
  • All
Menu

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)
memberof

PIXI

Hierarchy

  • TextMetrics

Index

Constructors

constructor

  • new TextMetrics(text: string, style: TextStyle, width: number, height: number, lines: string[], lineWidths: number[], lineHeight: number, maxLineWidth: number, fontProperties: IFontMetrics): TextMetrics
  • Parameters

    • text: string

      the text that was measured

    • style: 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: IFontMetrics

      the font properties object from TextMetrics.measureFont

    Returns TextMetrics

Properties

fontProperties

fontProperties: IFontMetrics

height

height: number

lineHeight

lineHeight: number

lineWidths

lineWidths: number[]

lines

lines: string[]

maxLineWidth

maxLineWidth: number

style

style: TextStyle

text

text: string

width

width: number

Static BASELINE_MULTIPLIER

BASELINE_MULTIPLIER: number

Static BASELINE_SYMBOL

BASELINE_SYMBOL: string

Static HEIGHT_MULTIPLIER

HEIGHT_MULTIPLIER: number

Static METRICS_STRING

METRICS_STRING: string

Static _breakingSpaces

_breakingSpaces: number[]

Static _canvas

_canvas: HTMLCanvasElement | OffscreenCanvas

Static _context

_context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D

Static _fonts

_fonts: {}

Type declaration

  • [font: string]: IFontMetrics

Static _newlines

_newlines: number[]

Static Private addLine

addLine: any

Convienience function for logging each line added during the wordWrap method

param

The line of text to add

param

Add new line character to end

returns

A formatted line

Static Private collapseNewlines

collapseNewlines: any

Determines whether we should collapse newLine chars

param

The white space

returns

should collapse

Static Private collapseSpaces

collapseSpaces: any

Determines whether we should collapse breaking spaces

param

The TextStyle property whiteSpace

returns

should collapse

Static Private getFromCache

getFromCache: any

Gets & sets the widths of calculated characters in a cache object

param

The key

param

The letter spacing

param

The cache

param

The canvas context

returns

The from cache.

Static Private isNewline

isNewline: any

Determines if char is a newline.

param

The character

returns

True if newline, False otherwise.

Static Private tokenize

tokenize: any

Splits a string into words, breaking-spaces and newLine characters

param

The text

returns

A tokenized array

Static Private trimRight

trimRight: any

trims breaking whitespaces from string

param

The text

returns

trimmed string

Static Private wordWrap

wordWrap: any

Applies newlines to a string to have it optimally fit into the horizontal bounds set by the Text object's wordWrapWidth property.

param

String to apply word wrapping to

param

the style to use when wrapping

param

optional specification of the canvas to use for measuring.

returns

New string with new lines applied where required

Methods

Static canBreakChars

  • 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

    • _char: string
    • _nextChar: string
    • _token: string
    • _index: number
    • _breakWords: boolean

    Returns boolean

    whether to break word or not

Static canBreakWords

  • 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

    • _token: string
    • breakWords: boolean

      The style attr break words

    Returns boolean

    whether to break word or not

Static clearMetrics

  • clearMetrics(font?: string): void
  • Clear font metrics in metrics cache.

    static

    Parameters

    • Optional font: string

    Returns void

Static isBreakingSpace

  • 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

    • char: string

      The character

    • Optional _nextChar: string

    Returns boolean

    True if whitespace, False otherwise.

Static measureFont

  • measureFont(font: string): IFontMetrics
  • Calculates the ascent, descent and fontSize of a given font-style

    static

    Parameters

    • font: string

      String representing the style of the font

    Returns IFontMetrics

    Font properties object

Static measureText

  • measureText(text: string, style: TextStyle, wordWrap?: boolean, canvas?: HTMLCanvasElement | OffscreenCanvas): TextMetrics
  • Measures the supplied string of text and returns a Rectangle.

    Parameters

    • text: string

      the text to measure.

    • style: TextStyle

      the text style to use for measuring

    • Optional wordWrap: boolean
    • Optional canvas: HTMLCanvasElement | OffscreenCanvas

    Returns TextMetrics

    measured width and height of the text.

Static wordWrapSplit

  • 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.

    example

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

    Parameters

    • token: string

      The token to split

    Returns string[]

    The characters of the token

Generated using TypeDoc