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 ¶
- type Metrics
- func (m *Metrics) CaretOffset(displayText string, runePos int) float32
- func (m *Metrics) CaretOffsets(displayText string) []float32
- func (m *Metrics) CursorRect(contentRect geometry.Rect, displayText string, runePos int, ...) geometry.Rect
- func (m *Metrics) CursorX(contentRect geometry.Rect, displayText string, runePos int) float32
- func (m *Metrics) RuneIndexFromX(contentRect geometry.Rect, displayText string, x float32) int
- func (m *Metrics) SelectionRect(contentRect geometry.Rect, displayText string, start, end int) geometry.Rect
- func (m *Metrics) TextWidth(displayText string) float32
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
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
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 ¶
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 ¶
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.