textmetrics

package
v0.1.48 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 2 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.

All methods use widget.Canvas.MeasureText for accurate measurement, replacing 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
}

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

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. Uses MeasureText for accurate positioning.

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.

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.

Jump to

Keyboard shortcuts

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