formula

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 14 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 as shapes 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, such as a CJK font's. The app is responsible for their licenses.

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. 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, which TextView centers in its block's sideways scroll. 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
	// 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
	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. 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