Type TextEngine

type TextEngine interface {
	// Width is the advance of the whole line; 0 only for an empty line.
	Width(value string, size, weight float32) float32
	// Caret is the x of the caret at offset: the leading edge of the character at
	// offset, the trailing edge of the last one at the end, and where the display
	// puts that boundary in right-to-left text.
	Caret(value string, size, weight float32, offset int) float32
	// Selection appends the extents of [start, end) to dst, left to right: one span,
	// or several where the selection crosses a change of direction.
	Selection(dst []TextSpan, value string, size, weight float32, start, end int) []TextSpan
	// OffsetAt is the byte offset of the character boundary nearest x.
	OffsetAt(value string, size, weight, x float32) int
}

TextEngine measures and hit-tests one line of text with the platform's own shaped line: the same line that is drawn, with the platform's font fallback, so a width, a caret and a click agree with the glyphs on screen.

Sizes are logical font sizes and results are logical pixels from the left end of the line. Weight is TextStyle.Weight. Offsets are UTF-8 byte offsets into value; an offset inside a grapheme cluster means the start of that cluster, and one outside [0, len(value)] is clamped. An implementation is called on the paint goroutine only, must not allocate for a line it has measured before, and keeps its shaped lines itself (internal/textcache holds them for the backends).

Constructors

ReferenceTextEngine#

func ReferenceTextEngine() TextEngine

ReferenceTextEngine is the TextEngine over the reference metrics (internal/textmetrics): the same widths on every platform, whatever fonts are installed. It is what a canvas with no engine measures with, and the way for a test or a screenshot to ask for those widths explicitly.