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.

Hierarchy

  • TextHelper

Constructors

Properties

_dirty: boolean = false

Has the text (or properties associated with it) changed?

_height: number = 0

The current total text height. May be outdated.

_lineHeight: number = 0

The current lineHeight. May be outdated

_lineRanges: LineRange[] = []

See lineRanges. For internal use only.

_lineSpacing: number = 0

The current lineSpacing. May be outdated

_tabWidth: number = 0

The actual 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 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.

Decorator

@multiFlagField(['_dirty', 'measureDirty', 'lineHeightSpacingDirty', 'tabWidthDirty'])

hasWrappedLines: boolean = false

Are any of the lines wrapped? For internal use only.

lineHeight: null | number = null

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

Decorator

@multiFlagField(['_dirty', 'measureDirty', 'lineHeightSpacingDirty'])

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.

Decorator

@multiFlagField(['_dirty', 'measureDirty', 'lineHeightSpacingDirty'])

maxWidth: number = Infinity

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

Decorator

@multiFlagField(['_dirty', 'maxWidthDirty'])

maxWidthDirty: boolean = false

Has the maximum width been changed?

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.

Decorator

@multiFlagField(['_dirty', 'measureDirty', 'tabWidthDirty'])

tabWidthDirty: boolean = true

Does the tab width need to be re-measured?

text: string = ''

The current string of text.

Decorator

@multiFlagField(['_dirty', 'measureDirty'])

wrapMode: WrapMode = WrapMode.Normal

The mode for text wrapping

Decorator

@multiFlagField(['_dirty', 'measureDirty'])

Accessors

  • get actualTabWidth(): number
  • Get the current tab width in pixels. Re-measures if neccessary

    Returns number

  • 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), 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 findOffsetFromIndex for the opposite.

    Returns

    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.

    Parameters

    • offset: [number, number]

    Returns [number, [number, number]]

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

    See findIndexOffsetFromOffset for the opposite.

    Returns

    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.

    Parameters

    • index: number

    Returns [x: number, yTop: number]

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

    Returns

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

    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

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

    Returns

    Returns the new horizontal offset

    Parameters

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

    Returns number

  • Paint all line ranges.

    Parameters

    • ctx: CanvasRenderingContext2D
    • fillStyle: FillStyle
    • x: number
    • y: number

    Returns void

  • 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

Generated using TypeDoc