Class TextHelper

An aggregate helper class for widgets that contain text.

Contains utilities for measuring text dimensions, converting between offsets in pixels and text indices and painting.

Constructors

Properties

_dirty: boolean = false

TextHelper#dirty but for internal use.

_height: number = 0

The current total text height. May be outdated.

_lineHeight: number = 0

The current TextHelper#lineHeight. May be outdated

_lineRanges: LineRange[] = []

See TextHelper#lineRanges. For internal use only.

_lineSpacing: number = 0

The current TextHelper#lineSpacing. May be outdated

_spaceWidth: number = 0

The width of a space in pixels. May be outdated

_tabWidth: number = 0

The actual TextHelper#tabWidth in pixels. May be outdated

_width: number = 0

The current largest text width. May be outdated.

alignMode: number = TextAlignMode.Start

The text alignment mode. Can also be a ratio.

Note that this only aligns text in the text's width. If TextHelper#maxWidth is infinite, then you may still need to align the widget that uses this text helper with a BaseContainer because the width will be set to the longest line range's width.

font: string = ''

The current font used for rendering text.

lineHeight: null | number = null

The height of each line of text when wrapped. If null, then the helper will try to automatically detect it.

lineHeightSpacingDirty: boolean = true

Does the line height or spacing need to be re-measured?

lineSpacing: null | number = null

The amount of spacing between lines. If null, then the helper will try to automatically detect it.

maxWidth: number = Infinity

The current maximum text width. If not Infinite and TextHelper#wrapMode is not WrapMode.None and not WrapMode.Ellipsis, then text will be wrapped and width will be set to maxWidth.

measureDirty: boolean = true

Does the text need to be re-measured?

tabWidth: number = 4

The amount of spaces that each tab character is equivalent to. By default, it is equivalent to 4 spaces.

tabWidthDirty: boolean = true

Do the space and tab widths need to be re-measured?

text: string = ''

The current string of text.

wrapMode: WrapMode = WrapMode.Normal

The mode for text wrapping

Accessors

  • get dirty(): boolean
  • Has the text (or properties associated with it) changed? Resets to false after reading the current value.

    Returns boolean

  • get lineRanges(): LineRange[]
  • Which range of text indices are used for each line.

    If there is no text wrapping (maxWidth is Infinity, or wrapMode is WrapMode.None or wrapMode is WrapMode.Ellipsis), then this will contain a single tuple containing [0, (text length)].

    If there is text wrapping, then this will be an array where each member is a tuple containing the starting index of a line of text and the ending index (exclusive) of a line of text.

    Returns LineRange[]

Methods

  • Get the index and horizontal offset, in pixels, of the beginning of a character at a given offset.

    See TextHelper#findOffsetFromIndex for the opposite.

    Parameters

    • offset: [number, number]

    Returns [number, [number, number]]

    Returns a 2-tuple containing the index of the character at the offset and a 2-tuple containing the offset, in pixels. Note that this is not neccessarily an integer. Note that the returned offset is not the same as the input offset. The returned offset is exactly at the beginning of the character. This is useful for implementing selectable text.

  • Get the horizontal offset, in pixels, of the beginning of a character at a given index.

    See TextHelper#findIndexOffsetFromOffset for the opposite.

    Parameters

    • index: number
    • preferLineEnd: boolean = false

      If true, the offset at the end of the line will be returned, instead of at the beginning of the line. Only applies to line ranges that were wrapped.

    Returns [x: number, yTop: number]

    Returns a 2-tuple containing the offset, in pixels. Vertical offset in the tuple is at the top of the character. Note that this is not neccessarily an integer.

  • Get a line number from a given cursor index. If out of bounds, returns nearest in-bounds line. Line numbers start at 0.

    Parameters

    • index: number

    Returns number

  • Get the index of the end of a line.

    Parameters

    • line: number
    • includeNewlines: boolean = true

      If false, newline characters will be ignored and the end will be at their index, instead of after their index

    Returns number

  • Get width from line range start to index. Handles out of bounds indices, but keeps them in the same line

    Parameters

    Returns number

  • Get the horizontal offset, in pixels, of the start of a line. Takes text wrapping into account. Line indices before first line will be treated as the first line, after the last line will be treated as a new empty line.

    Parameters

    • line: number

    Returns number

  • Get the index of the start of a line. If out of bounds, returns the nearest in-bounds index

    Parameters

    • line: number

    Returns number

  • Similar to measureTextDims, but uses text render groups for optimisation purposes and for the ability of individual characters to override their natively measured size; tabs having a dynamic size that aligns them to multiples of a value and newlines having no length.

    Parameters

    • start: number

      The inclusive index to start measuring at. If there are render groups and unmeasured text before this index, then this value will be overridden to include the unmeasured text. Render groups will also be merged if they don't override width.

    • end: number

      The exclusive index to stop measuring at.

    • maxWidth: number

      The maximum width of a line of text. If the line contains a single character, this will be ignored.

    • lineRange: LineRange

      The current text render groups for this line of text. This will be updated in place.

    Returns boolean

    Returns true if the line range was modified and it fit into the maximum width

  • Measure a slice of text taking left offset into account. If left offset is 0, then this will also add the left bounding box overhang. If not, then it will just return the width.

    Only for slices of text which have no width-overriding characters, else, you will get wrong measurements.

    Parameters

    • left: number
    • start: number
    • end: number

    Returns number

    Returns the new horizontal offset

  • Paint a single text render group with a given offset and left value for checking if the group is zero-width. left value must not be shifted.

    Used mainly for injecting debug code; you won't get much use out of this method unless you have a very specific need.

    Parameters

    • ctx: CanvasRenderingContext2D
    • group: TextRenderGroup
    • left: number
    • x: number
    • y: number

    Returns void