Documentation
¶
Overview ¶
Package layout turns the neutral math IR into a deterministic display list.
It deliberately knows nothing about TeX, Typst, font file formats, or UI toolkits. Font data enters through the narrow ports in source.go. All intermediate geometry is fixed-point in ems; only flatten.go converts to the public float32 display contract.
Index ¶
- Variables
- func MathSpacing(left, right ir.MathClass, style Style) geometry.Em
- func NormalizeClass(previous *ir.MathClass, current ir.MathClass) ir.MathClass
- type Axis
- type Fonts
- type Fragment
- type Glyph
- type GlyphFragment
- type GlyphRequest
- type GlyphSource
- type GlyphStretcher
- type KernCorner
- type LayoutOptions
- type MathKern
- type MathKernInfo
- type MathMetrics
- type MathSize
- type Metrics
- type OperatorRequest
- type OperatorSource
- type Options
- type Path
- type Point
- type PositionedGlyph
- type Renderer
- func (r *Renderer) Layout(root ir.Node, mode ir.Mode, style display.Style) (*display.Box, error)
- func (r *Renderer) LayoutContext(ctx context.Context, root ir.Node, mode ir.Mode, style display.Style) (*display.Box, error)
- func (r *Renderer) LayoutFormulaContext(ctx context.Context, formula *ir.Formula, mode ir.Mode, style display.Style) (*display.Box, error)
- func (r *Renderer) LayoutFormulaContextOptions(ctx context.Context, formula *ir.Formula, mode ir.Mode, style display.Style, ...) (*display.Box, error)
- func (r *Renderer) LayoutParsedFormulaContext(ctx context.Context, formula *ir.Formula, stats ir.Stats, mode ir.Mode, ...) (*display.Box, error)
- func (r *Renderer) LayoutParsedFormulaContextOptions(ctx context.Context, formula *ir.Formula, stats ir.Stats, mode ir.Mode, ...) (*display.Box, error)
- type Segment
- type StretchRequest
- type Style
- type StyleFont
- type TableLine
- type TextCluster
- type TextRequest
- type TextRun
- type TextSource
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidInput reports malformed layout input. ErrInvalidInput = errors.New("layout: invalid input") // ErrMissingGlyph reports a glyph source miss. ErrMissingGlyph = errors.New("layout: missing glyph") // ErrUnsupported reports a construct owned by a later work package. ErrUnsupported = errors.New("layout: unsupported construct") // ErrInsufficientStretch reports a required delimiter or accent that the // configured font ports could not extend to its target. ErrInsufficientStretch = errors.New("layout: insufficient stretch") // ErrLimit reports a configured resource limit. ErrLimit = errors.New("layout: limit exceeded") )
Functions ¶
func MathSpacing ¶
MathSpacing returns the automatic neutral inter-atom space in local ems. Cramped affects constructions, not inter-atom spacing.
Types ¶
type Fonts ¶
type Fonts struct {
Glyphs GlyphSource
Metrics StyleFont
Stretch GlyphStretcher
Text TextSource
Operators OperatorSource
}
Fonts are the ports required by the layout engine.
type Fragment ¶
type Fragment struct {
Metrics
Class ir.MathClass
Size MathSize
Scale geometry.Em
Italic geometry.Em
BaseAscent geometry.Em
BaseDescent geometry.Em
AccentTop geometry.Em
AccentBottom geometry.Em
InkBottom geometry.Em
HasInkBottom bool
TextLike bool
// StretchableHorizontal mirrors the base glyph's MATH construction.
StretchableHorizontal bool
// contains filtered or unexported fields
}
Fragment is the internal, immutable-after-construction layout tree. Items are stored in deterministic back-to-front order and positioned relative to the fragment's baseline origin.
func NewFragment ¶
func NewFragment(metrics Metrics, class ir.MathClass, size MathSize, scale geometry.Em) (*Fragment, error)
NewFragment constructs an empty fragment for an extension port. Callers can add measured child groups, rules, or paths before returning it.
type Glyph ¶
type Glyph struct {
ID uint16
FontID string
Text string
Advance geometry.Em
Ascent geometry.Em
Descent geometry.Em
Italic geometry.Em
// OriginY is the glyph origin's signed offset from the fragment baseline.
// It is non-zero when a vertical MATH construction is centered on the
// math axis while its outline stays at the font's original baseline.
OriginY geometry.Em
AdvanceIncludesItalic bool
TopAccent geometry.Em
HasTopAccent bool
BottomAccent geometry.Em
HasBottomAccent bool
// InkBottom is the signed y coordinate of the outline's lower edge relative
// to the baseline. It may be negative for accents whose ink sits above the
// baseline. Fill Outline or set HasInkBottom to preserve exact placement.
InkBottom geometry.Em
HasInkBottom bool
Kerns MathKernInfo
// StretchableHorizontal reports a MATH horizontal construction. Script
// layout uses it to select the stretch-stack limit constants.
StretchableHorizontal bool
// ExactGlyphRef reports that ID, FontID, Advance, ascent, and descent
// describe the complete drawn glyph, so hosts may cache and draw it by ID.
ExactGlyphRef bool
TextLike bool
Outline Path
// Parts carries the individual glyphs of a MATH assembly. Positions are
// baseline-relative local ems. Outline is empty for a part-backed glyph.
Parts []PositionedGlyph
}
Glyph is a shaped glyph in local ems. Advance is the final layout advance. AdvanceIncludesItalic reports that Italic has already been incorporated into Advance. Outline may be empty when the source only exposes a host glyph reference.
type GlyphFragment ¶
GlyphFragment is one placed glyph in root ems. It is the internal bridge between a GlyphSource result and the flattening stage.
type GlyphRequest ¶
type GlyphRequest struct {
Context context.Context
Text string
Style Style
Variant ir.Variant
Font ir.FontRole
Features []string
}
GlyphRequest identifies one glyph lookup or shaping operation.
type GlyphSource ¶
type GlyphSource interface {
Glyph(req GlyphRequest) (Glyph, error)
}
GlyphSource is the only glyph-facing dependency of layout.
type GlyphStretcher ¶
type GlyphStretcher interface {
Stretch(req StretchRequest) (Glyph, error)
}
GlyphStretcher is an optional extension port for OpenType MATH variants and assemblies. Without it, an ordinary glyph is used only if it meets the target; otherwise layout returns ErrInsufficientStretch.
type KernCorner ¶
type KernCorner uint8
KernCorner identifies one of the four corners in an OpenType MATH MathKernInfo table.
const ( KernTopLeft KernCorner = iota KernTopRight KernBottomRight KernBottomLeft )
type LayoutOptions ¶ added in v0.4.0
type LayoutOptions struct {
RecoverVisible bool
}
LayoutOptions controls recoverable behavior for one layout call.
type MathKern ¶
MathKern is one corner's MathKernInfo table. Heights are strictly increasing and Kerns has exactly one more element.
type MathKernInfo ¶
type MathKernInfo struct {
TopLeft *MathKern
TopRight *MathKern
BottomRight *MathKern
BottomLeft *MathKern
}
MathKernInfo groups the optional kern tables attached to a glyph.
type MathMetrics ¶
type MathMetrics struct {
ScriptPercentScaleDown int16
ScriptScriptPercentScaleDown int16
SpaceWidth geometry.Em
XHeight geometry.Em
// CapHeight drives the minimum inline equation ascent.
CapHeight geometry.Em
// AccentBaseHeight and FlattenedAccentBaseHeight come from the font's
// typographic metrics. Zero uses the New Computer Modern-compatible
// fallbacks in normalized.
AccentBaseHeight geometry.Em
FlattenedAccentBaseHeight geometry.Em
// DisplayOperatorMinHeight is the minimum vertical extent for large
// operators in display style.
DisplayOperatorMinHeight geometry.Em
AxisHeight geometry.Em
SuperscriptShiftUp geometry.Em
SuperscriptShiftUpCramped geometry.Em
SuperscriptBottomMin geometry.Em
SuperscriptBaselineDropMax geometry.Em
SubscriptShiftDown geometry.Em
SubscriptTopMax geometry.Em
SubscriptBaselineDropMin geometry.Em
SubSuperscriptGapMin geometry.Em
SuperscriptBottomMaxWithSub geometry.Em
SpaceAfterScript geometry.Em
UpperLimitGapMin geometry.Em
UpperLimitBaselineRiseMin geometry.Em
LowerLimitGapMin geometry.Em
LowerLimitBaselineDropMin geometry.Em
StretchStackTopShiftUp geometry.Em
StretchStackBottomShiftDown geometry.Em
StretchStackGapAboveMin geometry.Em
StretchStackGapBelowMin geometry.Em
StackTopShiftUp geometry.Em
StackTopDisplayStyleShiftUp geometry.Em
StackBottomShiftDown geometry.Em
StackBottomDisplayShiftDown geometry.Em
StackGapMin geometry.Em
StackDisplayStyleGapMin geometry.Em
FractionNumeratorShiftUp geometry.Em
FractionNumeratorDisplayUp geometry.Em
FractionDenominatorShiftDown geometry.Em
FractionDenominatorDisplayDn geometry.Em
FractionNumeratorGapMin geometry.Em
FractionNumeratorDisplayGap geometry.Em
FractionRuleThickness geometry.Em
FractionDenominatorGapMin geometry.Em
FractionDenominatorDisplayGap geometry.Em
SkewedFractionVerticalGap geometry.Em
SkewedFractionHorizontalGap geometry.Em
OverbarVerticalGap geometry.Em
OverbarRuleThickness geometry.Em
OverbarExtraAscender geometry.Em
UnderbarVerticalGap geometry.Em
UnderbarRuleThickness geometry.Em
UnderbarExtraDescender geometry.Em
RadicalVerticalGap geometry.Em
RadicalDisplayStyleGap geometry.Em
RadicalRuleThickness geometry.Em
RadicalExtraAscender geometry.Em
RadicalKernBeforeDegree geometry.Em
RadicalKernAfterDegree geometry.Em
RadicalDegreeBottomRaisePct int16
}
MathMetrics contains the font constants used by this layout layer. All values are base ems, independent of the math style. Layout applies the style scale exactly once; adapters must not pre-scale values by style.
This is intentionally a data port, not a font representation. A FontSet adapter extracts these constants from the OpenType MATH table once.
type OperatorRequest ¶
type OperatorRequest struct {
Context context.Context
Text string
Style Style
Variant ir.Variant
Features []string
}
OperatorRequest identifies one named operator run. Operator text is set as a single upright run so scripts and limits attach to the complete name.
type OperatorSource ¶
type OperatorSource interface {
ShapeOperator(req OperatorRequest) (TextRun, error)
}
OperatorSource is an optional extension port for named operator runs.
type Path ¶
type Path []Segment
Path is a glyph outline in local ems, y down, relative to the font glyph origin. Glyph.OriginY places that origin relative to the fragment baseline.
type PositionedGlyph ¶
type PositionedGlyph struct {
Glyph Glyph
At Point
// Cluster is the zero-based index into TextRun.Clusters.
Cluster int
// Missing marks one synthetic glyph substituting for an unavailable text
// cluster. Layout paints it with the error role.
Missing bool
}
PositionedGlyph is one glyph in a shaped text run, in local ems relative to the run baseline. Cluster indexes TextRun.Clusters and is meaningful only when that slice is non-empty.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer is an immutable layout engine. It is safe for concurrent use if its supplied font ports are safe for concurrent use.
func (*Renderer) LayoutContext ¶
func (r *Renderer) LayoutContext(ctx context.Context, root ir.Node, mode ir.Mode, style display.Style) (*display.Box, error)
LayoutContext lays out root with cancellation and deterministic resource limits.
func (*Renderer) LayoutFormulaContext ¶
func (r *Renderer) LayoutFormulaContext(ctx context.Context, formula *ir.Formula, mode ir.Mode, style display.Style) (*display.Box, error)
LayoutFormulaContext lays out a complete formula with cancellation and deterministic resource limits.
func (*Renderer) LayoutFormulaContextOptions ¶ added in v0.4.0
func (r *Renderer) LayoutFormulaContextOptions( ctx context.Context, formula *ir.Formula, mode ir.Mode, style display.Style, options LayoutOptions, ) (*display.Box, error)
LayoutFormulaContextOptions lays out a formula with per-call recovery behavior.
func (*Renderer) LayoutParsedFormulaContext ¶
func (r *Renderer) LayoutParsedFormulaContext(ctx context.Context, formula *ir.Formula, stats ir.Stats, mode ir.Mode, style display.Style) (*display.Box, error)
LayoutParsedFormulaContext lays out a formula that was structurally validated and frozen by the parser boundary. Engine-configured limits and final output invariants are still enforced.
func (*Renderer) LayoutParsedFormulaContextOptions ¶ added in v0.4.0
func (r *Renderer) LayoutParsedFormulaContextOptions( ctx context.Context, formula *ir.Formula, stats ir.Stats, mode ir.Mode, style display.Style, options LayoutOptions, ) (*display.Box, error)
LayoutParsedFormulaContextOptions lays out parser-validated formula IR with per-call recovery behavior.
type Segment ¶
Segment is one outline command in local ems. The Op field uses the public path operation vocabulary so flattening does not invent a second one.
type StretchRequest ¶
StretchRequest asks for a MATH size variant or assembly in local ems.
type Style ¶
type Style struct {
Size MathSize
Cramped bool
// SizeScale is an absolute font-size multiplier relative to the root em.
// Zero means the default multiplier (one). It is multiplied by the
// display/text/script/script-script style scale at layout time.
SizeScale geometry.Em
}
Style is one of the eight OpenType math styles: four sizes, each normal or cramped.
func (Style) Denominator ¶
Denominator returns the style for a vertical fraction denominator.
func (Style) Superscript ¶
Superscript returns the style for a superscript. Display and text move to script; both script sizes move to script-script. Cramping is inherited.
func (Style) WithCramped ¶
WithCramped replaces the cramped flag.
type StyleFont ¶
type StyleFont interface {
MathMetrics(style Style, variant ir.Variant) MathMetrics
}
StyleFont supplies metrics for a math style and alphabet variant.
type TextCluster ¶ added in v0.4.0
type TextCluster struct {
RuneStart int
RuneEnd int
Source source.Range
FaceID string
Fallback bool
Missing bool
// RTL reports that this cluster was shaped in a right-to-left run. X still
// describes its left edge in visual order.
RTL bool
// X and Advance are the cluster's horizontal extent in local ems relative
// to the run origin. They are independent of per-glyph positioning offsets.
X geometry.Em
Advance geometry.Em
}
TextCluster preserves the source extent of one shaping cluster. Byte offsets are relative to the text passed in TextRequest or OperatorRequest, not to the complete document source.
type TextRequest ¶
type TextRequest struct {
Context context.Context
Text string
Clusters []ir.TextClusterSource
Style Style
Variant ir.Variant
Font ir.FontRole
// Tracking is unscaled base ems, independent of Style.Size. Shaping
// produces base-em glyph positions; layout applies the style scale once.
Tracking geometry.Em
Features []string
}
TextRequest identifies a shaped text run.
type TextRun ¶
type TextRun struct {
Width geometry.Em
Ascent geometry.Em
Descent geometry.Em
Italic geometry.Em
// Clusters is in visual glyph order and preserves source extents for editor
// hit testing. It may be empty for non-text providers and synthetic runs.
Clusters []TextCluster
Glyphs []PositionedGlyph
// MissingFaces is the ordered face ID list attempted for this run. It is
// populated only when at least one cluster is missing.
MissingFaces []string
}
TextRun is a shaped one-line text run in local ems.
type TextSource ¶
type TextSource interface {
ShapeText(req TextRequest) (TextRun, error)
}
TextSource is an optional extension port for deterministic text shaping.
Source Files
¶
- accent.go
- aligned.go
- arithmetic.go
- box.go
- breaks.go
- cancel.go
- caret.go
- doc.go
- fenced.go
- flatten.go
- font_usage.go
- fraction.go
- fragment.go
- glyph.go
- hit.go
- length.go
- line.go
- lines.go
- metrics.go
- multiline.go
- overlay.go
- path.go
- phantom.go
- radical.go
- raise.go
- renderer.go
- row.go
- scratch.go
- scripts.go
- sized_delimiter.go
- source.go
- source_error.go
- spacing.go
- style.go
- table.go
- table_grid.go
- tags.go
- text.go