text

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT 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 text measures text for the layout of zuzu (D11).

The okage peer writes the API that measures and shapes text as okage does, when zuzu needs it. Until then, Estimate measures a string from the advance widths of the bundled IBM Plex Sans (D16), with no shaping and no kerning. The layout depends on the interface Measurer, so that the measurement of okage can take its place.

A Fallback is a font for the characters that IBM Plex Sans does not have. The package text/emoji gives one with the emoji of Noto Color Emoji (D32). A character that no font has draws nothing and measures as nothing.

Index

Constants

View Source
const Ellipsis = "…"

Ellipsis is the text that Truncate puts at the end of a cut string, as Vega does.

View Source
const EmojiFamily = "Noto Color Emoji"

EmojiFamily is the family of the font of the package text/emoji. The SVG writer names it for a text that holds a character that IBM Plex Sans does not have, so that a viewer draws an emoji in color (D32).

View Source
const Family = "IBM Plex Sans"

Family is the family of the bundled font.

View Source
const MathGap = 3.0 / 18

MathGap is the space between a run of text and a run of math, in ems of the font of the text: a thin space of TeX.

Variables

View Source
var ErrNoMathFont = errors.New("no font of math")

ErrNoMathFont is the error of a run of math with no font of math, which zuzu.WithMath gives.

Functions

func Fonts

func Fonts() fs.FS

Fonts returns the bundled fonts and their license, so that a renderer such as okage can load them. The files are in the folder fonts. The six faces are one TrueType collection, IBMPlexSans.ttc. The package embeds the collection in WOFF2 and decodes it on the first call (D34).

func HasMath added in v0.7.5

func HasMath(s string) bool

HasMath reports whether a text holds math of KaTeX.

func MathCommand added in v0.7.5

func MathCommand(err error) string

MathCommand returns the command of TeX that an error of Layout names, such as \phase, or empty when it names none.

func Truncate

func Truncate(m Measurer, f Font, s string, limit float64) string

Truncate cuts s to the longest start that fits in limit with the ellipsis after it, as Vega cuts a text to its limit. A string that fits is returned as it is. A limit of zero or less is no limit.

func Weight

func Weight(s string) int

Weight reads a font weight as Vega writes it: a number, or normal or bold. Anything else is normal.

Types

type Estimate

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

Estimate measures text from the advance widths of the bundled font. It does not shape or kern, so a measured width can differ from the width that a renderer draws by a little. Each family measures as IBM Plex Sans, or as the main font of SetMain where it has the character. A character that neither has measures in the first fallback that has it, and as nothing when no fallback has it. It is safe for concurrent use, and it keeps the widths it measured.

func NewEstimate

func NewEstimate(fallbacks ...*Fallback) *Estimate

NewEstimate returns an Estimate, with the fallbacks for the characters that the bundled font does not have, in order.

func (*Estimate) Math added in v0.7.5

func (e *Estimate) Math() *MathFont

Math returns the font of math of SetMath, or nil.

func (*Estimate) Missing added in v0.6.0

func (e *Estimate) Missing(s string) []rune

Missing returns the characters of s that neither the bundled font nor a fallback has, each once, in the order of s. Such a character draws nothing. A character that draws nothing in any font, such as a space, a control character, a joiner or a selector of a variation, is never missing.

func (*Estimate) SetMain added in v0.8.0

func (e *Estimate) SetMain(f *Fallback)

SetMain gives the Estimate a main font, which measures each character that it has before the bundled font, in each family, weight and style. The look like Excalidraw measures its text in Excalifont this way, with IBM Plex Sans for the characters that Excalifont does not have (D40). A nil font measures in the bundled font. Call it before the first measure.

func (*Estimate) SetMath added in v0.7.5

func (e *Estimate) SetMath(f *MathFont)

SetMath gives the Estimate a font of math, which lays out the math of KaTeX in a label of Mermaid (D36). A nil font draws no math.

func (*Estimate) Split added in v0.6.0

func (e *Estimate) Split(s string) []string

Split splits s into pieces that each draw in one kind of font: the characters of the bundled font, or a sequence of the characters of the fallbacks with the characters that join them, such as a joiner, a selector of a variation or a modifier of skin tone. okage falls back to another font for one character at a time, and it draws a sequence of emoji, such as a flag, as one glyph only when it shapes the sequence on its own, so a renderer shapes each piece apart (D32). A string with no character of a fallback is one piece.

func (*Estimate) Width

func (e *Estimate) Width(f Font, s string) float64

Width returns the width of s in the font, in pixels.

type Fallback added in v0.6.0

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

Fallback is a font for the characters that IBM Plex Sans does not have, such as the font of the package text/emoji (D32). It is safe for concurrent use, and it decodes and parses its font once, on its first use.

func NewFallback added in v0.6.0

func NewFallback(family string, data []byte) *Fallback

NewFallback returns a fallback of a family from the bytes of a font file: TrueType, OpenType, WOFF, WOFF2 or EOT (D34). The bytes must not change after the call.

func (*Fallback) Data added in v0.6.0

func (f *Fallback) Data() []byte

Data returns the bytes of the font as a TrueType or OpenType file, so that a renderer such as okage can load it. It decodes a font in a web format on the first call. It returns nil when the font does not decode. Do not change the bytes.

func (*Fallback) Family added in v0.6.0

func (f *Fallback) Family() string

Family returns the family of the font.

func (*Fallback) Source added in v0.8.0

func (f *Fallback) Source() []byte

Source returns the bytes of the font file that NewFallback took, in their own format, such as WOFF2, so that the SVG writer can embed the font as it came (D43). Do not change the bytes.

type Font

type Font struct {
	Family string  // the family, such as IBM Plex Sans
	Size   float64 // the size in pixels
	Weight int     // the weight, from 100 to 900, where 400 is normal
	Italic bool
}

Font is a font at a size.

type Formula added in v0.7.5

type Formula struct {
	Width, Height, Depth float64
	// Paths are the outlines of the formula, in the syntax of the d
	// attribute of SVG, relative to its origin.
	Paths []MathPath
}

Formula is a formula of TeX laid out, in pixels. Its origin is the left end of its baseline. Height is its extent above the baseline, and Depth its extent below it.

type MathFont added in v0.7.5

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

MathFont is a font of math, with an OpenType table MATH, for the math of KaTeX in a label of Mermaid (D36). The package text/math gives STIX Two Math. A face of go-text is not safe for concurrent use, so a MathFont lays out one formula at a time.

func MathOf added in v0.7.5

func MathOf(m Measurer) *MathFont

MathOf returns the font of math of a Measurer that has one, such as an Estimate after SetMath, or nil.

func NewMathFont added in v0.7.5

func NewMathFont(data []byte) *MathFont

NewMathFont returns a font of math from the data of an OpenType font with a table MATH. The font is parsed on its first use.

func (*MathFont) Layout added in v0.7.5

func (f *MathFont) Layout(tex string, size float64) (*Formula, error)

Layout lays out a formula of TeX at a size in pixels, in the display style, as Mermaid 12.3 asks KaTeX to (D36). A formula with no color gives one path. A formula with \color gives a path for each item, so that each keeps its color. An error of the parse wraps okage.ErrTeXMathSyntax, and MathCommand names the command of it.

type MathLine added in v0.7.5

type MathLine struct {
	Pieces []Piece
	Width  float64
	Height float64
}

MathLine is a line of a label with math, laid out as Mermaid 12.3 lays it out: a row of its runs, each centered on the middle of the line, with no wrap. Height is the larger of the height of a line of text, when the line has text, and the height and the depth of its tallest formula. So a line of math alone is as high as its formula (D36).

func LayMathLine added in v0.7.5

func LayMathLine(m Measurer, f Font, runs []Run, lineHeight float64) (*MathLine, error)

LayMathLine lays out a line of a label with runs of math in a font, with a line of text lineHeight high. Mermaid draws each run as an item of a row, which drops the spaces at its ends, so a run of text loses them, and a run of spaces alone draws nothing. A thin space of 3/18 of the size of the font stands between a run of text and a run of math, so that the two do not touch (docs/STYLE.md). A run of text measures with m, and a run of math lays out in the font of math of m at the size of the font. A run of math that does not lay out gives its error, and the line is nil.

type MathPath added in v0.7.5

type MathPath struct {
	Path  string
	Color string
}

MathPath is an outline of a formula and its color. An empty color is the color of the text around the formula.

type Measurer

type Measurer interface {
	Width(f Font, s string) float64
}

Measurer measures the width of a string in a font, in pixels.

type Piece added in v0.7.5

type Piece struct {
	Run

	X, W     float64
	Formula  *Formula
	Baseline float64
}

Piece is a run of a line of a label laid out. X is its offset from the start of the line, and W its width. A piece of math holds its formula, whose baseline is Baseline below the middle of the line.

type Run added in v0.7.5

type Run struct {
	Text string
	Math bool
}

Run is a part of a line of a label: text, or the TeX of a formula.

func SplitMath added in v0.7.5

func SplitMath(s string) []Run

SplitMath splits a line of a label into runs of text and runs of math, where Mermaid 12.3 finds math: each text between two $$. The runs of math hold the TeX without the $$. A line with no math is one run of text.

Directories

Path Synopsis
Package math gives STIX Two Math, a font of math under the SIL Open Font License 1.1, so that zuzu draws the math of KaTeX in a label of Mermaid (D36).
Package math gives STIX Two Math, a font of math under the SIL Open Font License 1.1, so that zuzu draws the math of KaTeX in a label of Mermaid (D36).

Jump to

Keyboard shortcuts

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