formula

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package formula typesets math, written in TeX or Typst, with Untie (github.com/caelyreth/untie), and draws it in the font size and the text color it inherits: inline, on its text's baseline, or as a display formula of its own. Its font is New Computer Modern Math Book, embedded and loaded off the engine's goroutine as a formula first shows, or the app's through NewRenderer. textview.TextView draws its formulas so with RenderMath: formula.RenderMath.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderMath

func RenderMath(cx *core.Scope, m textview.Math) core.Node

RenderMath is Default().RenderMath, for textview.TextView{RenderMath: formula.RenderMath}.

Types

type Dialect

type Dialect uint8

Dialect is the language a formula is written in.

const (
	// TeX is LaTeX's math, as $…$ holds it; the zero value.
	TeX Dialect = iota
	// Typst is Typst's math mode.
	Typst
)

type Fonts

type Fonts struct {
	Math      []byte
	Text      []byte
	Fallbacks [][]byte
}

Fonts are a renderer's fonts, OpenType files: Math, with a MATH table, lays the formulas out; Text, else Math, sets \text and \operatorname; and Fallbacks are tried in order for characters the others lack. Text runs these fonts cannot cover use the platform's system fonts. The app is responsible for its fonts' licenses.

type HitEvent

type HitEvent struct {
	Source      string
	Dialect     Dialect
	Phase       HitPhase
	Hit         untie.Hit
	Found       bool
	Point       untie.Point
	Caret       untie.CaretStop
	HasCaret    bool
	Diagnostics []untie.Diagnostic
	Position    core.Point
	Clicks      int
	Modifiers   core.Modifiers
}

HitEvent carries the Untie source part and nearest caret under a formula's pointer. Diagnostics contains supplied diagnostics whose ranges cover that part, so an editor can route presses, caret placement and diagnostics without reproducing Untie's geometry.

type HitPhase

type HitPhase uint8

HitPhase identifies the pointer interaction reported by HitEvent.

const (
	// HitHover reports pointer movement over the formula.
	HitHover HitPhase = iota
	// HitPress reports the primary press that starts a click.
	HitPress
	// HitClick reports the completed primary click at its press point.
	HitClick
)

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer lays formulas out in one set of fonts and keeps the last layouts it made, as shapes ready to draw. Missing text runs are measured and drawn by each View with its platform text engine. It is safe for concurrent use, so the engines of a process share one.

func Default

func Default() *Renderer

Default returns the renderer of the embedded New Computer Modern Math Book, whose font loads on first use: a View waits for it off the engine's goroutine.

func NewRenderer

func NewRenderer(fonts Fonts) (*Renderer, error)

NewRenderer prepares a renderer for fonts, parsing and validating them, which takes a while for a large font: about 120 ms for New Computer Modern Math on an M-series Mac, and about 0.7 s in WebAssembly. It fails when Math has no usable MATH table.

func (*Renderer) RenderMath

func (r *Renderer) RenderMath(cx *core.Scope, m textview.Math) core.Node

RenderMath shows a text view's formula with r, as textview.TextView.RenderMath takes it: an inline one, or a display one with Display's larger operators and limits, wrapped to its block's padded viewport. TextView centers its lines and scrolls any unbreakable overflow. A copy takes them as their TeX between $ and $$.

type View

type View struct {
	// Source is the formula without its delimiters ($…$); SourceFunc follows what it reads.
	Source     string
	SourceFunc func() string
	Dialect    Dialect
	Display    bool
	// Streaming hides recoverable error and placeholder ink while Source is still arriving. SourceFunc continues to lay out each update. Set it false when the source is complete to show remaining errors; StreamingFunc follows what it reads.
	Streaming     bool
	StreamingFunc func() bool
	// Renderer lays it out; nil is Default().
	Renderer *Renderer
	// Label is what assistive technology reads in place of what the formula says.
	Label     string
	Class     string
	Style     core.Style
	StyleFunc func() core.Style
	// ErrorStyle's color marks what failed to parse; ErrorClass and ErrorStyle style the source a formula past Untie's limits, or whose fonts did not load, shows instead.
	ErrorClass string
	ErrorStyle core.Style
	// Diagnostics are source ranges an editor wants associated with the formula; HitEvent reports the entries whose ranges cover the hit part. DiagnosticsFunc follows what it reads.
	Diagnostics     []untie.Diagnostic
	DiagnosticsFunc func() []untie.Diagnostic
	// OnHit receives hover, press and click events resolved through Untie's Box. It runs on the engine goroutine.
	OnHit  func(HitEvent)
	TestID string
}

View draws a formula. It sizes itself in ems of the font size it inherits, following the user's text size as text does, and paints in the text color it inherits, keeping the colors the formula names (\color{red}). As a core.Span's attachment it sits on the line's baseline. With Display it is a formula on a line of its own: larger operators, and limits above and below. Display formulas wrap at safe relations and operators when wider than the available width; an unbreakable part keeps its overflow. What failed to parse is marked in ErrorStyle's color, the text color without one; a formula past Untie's limits shows its source as text in ErrorClass and ErrorStyle. It keeps its size in a container that stretches its children, standing at the start of the cross axis unless its style sets AlignSelf. Until its renderer's font has loaded, it is an empty em square. Assistive technology reads it as an image labelled with what the formula says, in the language the app shows.

func (View) Render

func (v View) Render(cx *core.Scope) core.Node

Render implements core.Node.

Directories

Path Synopsis
Package locales says what base/formula's formulas read as, to assistive technology, in the language the app shows.
Package locales says what base/formula's formulas read as, to assistive technology, in the language the app shows.

Jump to

Keyboard shortcuts

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