textmetrics

package
v0.1.55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package textmetrics provides text-to-pixel coordinate mapping for text editing widgets. It is the single source of truth for cursor positioning, selection highlighting, and mouse-click-to-rune conversion.

Positions come from widget.TextCaretPositioner when the canvas implements it, so the caret sits where the renderer actually puts each glyph, and fall back to widget.Canvas.MeasureText on the prefix otherwise. Both replace the approximate charWidthRatio constants that were previously hardcoded in individual theme painters.

This package is INTERNAL — not part of the public API. Widget implementations (TextField, future Editor) use it; theme painters do not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Metrics

type Metrics struct {
	Canvas   widget.Canvas
	FontSize float32
	// contains filtered or unexported fields
}

Metrics provides text measurement for a specific canvas and font size.

A Metrics value memoizes the caret offsets of the last string it was asked about, so one paint measures the text once rather than once per cursor, selection edge and hit test.

func (*Metrics) CaretOffset added in v0.1.55

func (m *Metrics) CaretOffset(displayText string, runePos int) float32

CaretOffset returns the caret offset, in pixels from the text origin, before the rune at runePos. runePos is clamped to the text.

func (*Metrics) CaretOffsets added in v0.1.55

func (m *Metrics) CaretOffsets(displayText string) []float32

CaretOffsets returns the caret offset, in pixels from the text origin, before each rune of displayText, plus a final entry after the last rune.

The canvas supplies them when it implements widget.TextCaretPositioner; that path shapes the whole string, so a kerning pair or ligature spanning the caret is accounted for exactly as the renderer draws it. Otherwise each offset is the measured width of the prefix before it, which is all a canvas exposing only widget.Canvas.MeasureText can offer.

func (*Metrics) CursorRect

func (m *Metrics) CursorRect(contentRect geometry.Rect, displayText string, runePos int, cursorWidth float32) geometry.Rect

CursorRect returns the cursor line rectangle for the given rune position. Height is based on fontSize * lineHeightRatio minus caretHeightOffset on each side (Flutter _kCaretHeightOffset pattern), vertically centered in contentRect.

func (*Metrics) CursorX

func (m *Metrics) CursorX(contentRect geometry.Rect, displayText string, runePos int) float32

CursorX returns the X coordinate for a cursor at the given rune position within the content rect.

The base X is rounded to match DrawText's pixel-grid rounding (math.Round), ensuring the cursor aligns with the rendered text origin.

func (*Metrics) RuneIndexFromX

func (m *Metrics) RuneIndexFromX(contentRect geometry.Rect, displayText string, x float32) int

RuneIndexFromX converts an X coordinate to a rune index (for hit-testing). Returns the rune position closest to the given X within the content rect.

The base X is rounded to match DrawText's pixel-grid rounding.

func (*Metrics) SelectionRect

func (m *Metrics) SelectionRect(contentRect geometry.Rect, displayText string, start, end int) geometry.Rect

SelectionRect returns the selection highlight rectangle for the given rune range [start, end). Vertically spans the full content rect height.

func (*Metrics) TextWidth added in v0.1.55

func (m *Metrics) TextWidth(displayText string) float32

TextWidth returns the advance width of displayText as the renderer lays it out, which is the caret offset past the last rune.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL