Class TextMetrics

文本度量

用于度量指定样式的文本的宽度。

从pixi.js移植

See

https://github.com/pixijs/pixi.js/blob/dev/packages/text/src/TextMetrics.js

Hierarchy

  • TextMetrics

Constructors

  • Parameters

    • text: string

      the text that was measured

    • style: TextStyle<TextStyleEventMap>

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

The font properties object from TextMetrics.measureFont

height: number

测量出的高度。

lineHeight: number

The measured line height for this style

lineWidths: number[]

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

lines: string[]

根据样式分割成的多行文本。

maxLineWidth: number

The maximum line width for all measured lines

被测量的样式。

text: string

被测量的文本。

width: number

测量出的宽度。

BASELINE_MULTIPLIER: number = 2

Baseline multiplier for calculate font metrics.

BASELINE_SYMBOL: string = 'M'

Baseline symbol for calculate font metrics.

METRICS_STRING: string = '|ÉqÅ'

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

_breakingSpaces: number[] = ...

Cache of breaking spaces.

_canvas: HTMLCanvasElement = ...

Cached canvas element for measuring text

_context: CanvasRenderingContext2D = ...

Cache for context to use.

_fonts: {
    [key: string]: IFontMetrics;
} = {}

Cache of {@see PIXI.TextMetrics.FontMetrics} objects.

Type declaration

  • [key: string]: IFontMetrics
_newlines: number[] = ...

Cache of new line chars.

Methods

  • Private

    Convienience function for logging each line added during the wordWrap method

    Returns

    A formatted line

    Parameters

    • line: string

      The line of text to add

    • newLine: boolean = true

      Add new line character to end

    Returns string

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

    Returns

    whether to break word or not

    Parameters

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

    Returns

    whether to break word or not

    Parameters

    • _token: string

      The token

    • breakWords: boolean

      The style attr break words

    Returns boolean

  • Clear font metrics in metrics cache.

    Parameters

    • font: string = ''

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

    Returns void

  • Private

    Determines whether we should collapse newLine chars

    Returns

    should collapse

    Parameters

    • whiteSpace: string

      The white space

    Returns boolean

  • Private

    Determines whether we should collapse breaking spaces

    Returns

    should collapse

    Parameters

    • whiteSpace: string

      The TextStyle property whiteSpace

    Returns boolean

  • Private

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

    Returns

    The from cache.

    Parameters

    • key: string

      The key

    • letterSpacing: number

      The letter spacing

    • cache: {
          [key: string]: number;
      }

      The cache

      • [key: string]: number
    • context: CanvasRenderingContext2D

      The canvas context

    Returns number

  • Private

    Determines if char is a breaking whitespace.

    Returns

    True if whitespace, False otherwise.

    Parameters

    • char: string

      The character

    Returns boolean

  • Private

    Determines if char is a newline.

    Returns

    True if newline, False otherwise.

    Parameters

    • char: string

      The character

    Returns boolean

  • Measures the supplied string of text and returns a Rectangle.

    Returns

    measured width and height of the text.

    Parameters

    • text: string

      the text to measure.

    • style: TextStyle<TextStyleEventMap>

      the text style to use for measuring

    • wordWrap: boolean

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

    • canvas: HTMLCanvasElement = TextMetrics._canvas

      optional specification of the canvas to use for measuring.

    Returns TextMetrics

  • Private

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

    Returns

    A tokenized array

    Parameters

    • text: string

      The text

    Returns string[]

  • Private

    trims breaking whitespaces from string

    Returns

    trimmed string

    Parameters

    • text: string

      The text

    Returns string

  • Private

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

    Returns

    New string with new lines applied where required

    Parameters

    • text: string

      String to apply word wrapping to

    • style: TextStyle<TextStyleEventMap>

      the style to use when wrapping

    • canvas: HTMLCanvasElement = TextMetrics._canvas

      optional specification of the canvas to use for measuring.

    Returns 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];

    Returns

    The characters of the token

    Parameters

    • token: string

      The token to split

    Returns string[]

Generated using TypeDoc