shaped

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NewShaper returns the correct type of shaper.
	NewShaper func() Shaper

	// ShapeMath is a function that returns a path representing the
	// given math expression, in TeX syntax.
	// Import _ github.com/MobinYengejehi/core/text/tex to set this function
	// (incurs a significant additional memory footprint due to fonts
	// and other packages).
	ShapeMath func(expr string, fontHeight float32) (*ppath.Path, error)
)

Functions

func GoTextDirection

func GoTextDirection(rdir rich.Directions, tsty *text.Style) di.Direction

GoTextDirection gets the proper go-text direction value from styles.

func WrapSizeEstimate

func WrapSizeEstimate(csz math32.Vector2, nChars int, ratio float32, sty *rich.Style, tsty *text.Style) math32.Vector2

WrapSizeEstimate is the size to use for layout during the SizeUp pass, for word wrap case, where the sizing actually matters, based on trying to fit the given number of characters into the given content size with given font height, and ratio of width to height. Ratio is used when csz is 0: 1.618 is golden, and smaller numbers to allow for narrower, taller text columns.

Types

type Line

type Line struct {

	// Source is the input source corresponding to the line contents,
	// derived from the original Lines Source. The style information for
	// each Run is embedded here.
	Source rich.Text

	// SourceRange is the range of runes in the original [Lines.Source] that
	// are represented in this line.
	SourceRange textpos.Range

	// Runs are the shaped [Run] elements.
	Runs []Run

	// Offset specifies the relative offset from the Lines Position
	// determining where to render the line in a target render image.
	// This is the baseline position (not the upper left: see Bounds for that).
	Offset math32.Vector2

	// Bounds is the bounding box for the Line of rendered text,
	// relative to the baseline rendering position (excluding any contribution
	// of Offset). This is centered at the baseline and the upper left
	// typically has a negative Y. Use Size() method to get the size
	// and ToRect() to get an [image.Rectangle]. This is based on the output
	// LineBounds, not the actual GlyphBounds.
	Bounds math32.Box2

	// Selections specifies region(s) of runes within this line that are selected,
	// and will be rendered with the [Lines.SelectionColor] background,
	// replacing any other background color that might have been specified.
	Selections []textpos.Range

	// Highlights specifies region(s) of runes within this line that are highlighted,
	// and will be rendered with the [Lines.HighlightColor] background,
	// replacing any other background color that might have been specified.
	Highlights []textpos.Range
}

Line is one line of shaped text, containing multiple Runs. This is not an independent render target: see Lines (can always use one Line per Lines as needed).

func (*Line) RunStyle

func (ln *Line) RunStyle(tx rich.Text, ri int) *rich.Style

RunStyle returns the rich text style for given run index.

func (*Line) String

func (ln *Line) String() string

func (*Line) UpdateStyle

func (ln *Line) UpdateStyle(tx rich.Text, tsty *text.Style)

UpdateStyle updates the Decoration, Fill and Stroke colors from the current rich.Text Style for each run and given text style. This rich.Text must match the content of the shaped one, and differ only in these non-layout styles.

type Lines

type Lines struct {

	// Source is the original input source that generated this set of lines.
	// Each Line has its own set of spans that describes the Line contents.
	Source rich.Text

	// Lines are the shaped lines.
	Lines []Line

	// Offset is an optional offset to add to the position given when rendering.
	Offset math32.Vector2

	// Bounds is the bounding box for the entire set of rendered text,
	// relative to a rendering Position (and excluding any contribution
	// of Offset). Use Size() method to get the size and ToRect() to get
	// an [image.Rectangle].
	Bounds math32.Box2

	// FontSize is the [rich.Context] StandardSize from the Context used
	// at the time of shaping. Actual lines can be larger depending on font
	// styling parameters.
	FontSize float32

	// LineHeight is the line height used at the time of shaping.
	LineHeight float32

	// Truncated indicates whether any lines were truncated.
	Truncated bool

	// Direction is the default text rendering direction from the Context.
	Direction rich.Directions

	// Links holds any hyperlinks within shaped text.
	Links []rich.Hyperlink

	// Color is the default fill color to use for inking text.
	Color color.Color

	// SelectionColor is the color to use for rendering selected regions.
	SelectionColor image.Image

	// HighlightColor is the color to use for rendering highlighted regions.
	HighlightColor image.Image
}

Lines is a list of Lines of shaped text, with an overall bounding box and position for the entire collection. This is the renderable unit of text, although it is not a [render.Item] because it lacks a position, and it can potentially be re-used in different positions.

func (*Lines) AlignX

func (ls *Lines) AlignX(tsty *text.Style)

AlignX aligns the lines along X axis according to text style.

func (*Lines) AlignXFactor

func (ls *Lines) AlignXFactor(fact float32)

AlignXFactor aligns the lines along X axis according to alignment factor, as a proportion of size difference to add to offset (0.5 = center, 1 = right)

func (*Lines) Clone

func (ls *Lines) Clone() *Lines

Clone returns a Clone copy of the Lines, with new Lines elements that still point to the same underlying Runs.

func (ls *Lines) GetLinks() []rich.Hyperlink

GetLinks gets the links for these lines, which are cached in Links.

func (*Lines) HighlightRegion

func (ls *Lines) HighlightRegion(r textpos.Range)

HighlightRegion adds the selection to given region of runes from the original source runes. Use HighlightReset to clear first if desired.

func (*Lines) HighlightReset

func (ls *Lines) HighlightReset()

HighlightReset removes all existing selected regions.

func (*Lines) RuneAtLineDelta

func (ls *Lines) RuneAtLineDelta(ti, lineDelta int) int

RuneAtLineDelta returns the rune index in Lines source at given relative vertical offset in lines from the current line for given rune. It uses pixel locations of glyphs and the LineHeight to find the rune at given vertical offset with the same horizontal position. If the delta goes out of range, it will return the appropriate in-range rune index at the closest horizontal position.

func (*Lines) RuneAtPoint

func (ls *Lines) RuneAtPoint(pt math32.Vector2, start math32.Vector2) int

RuneAtPoint returns the rune index in Lines source, at given rendered location, based on given starting location for rendering. If the point is out of the line bounds, the nearest point is returned (e.g., start of line based on Y coordinate).

func (*Lines) RuneBounds

func (ls *Lines) RuneBounds(ti int) math32.Box2

RuneBounds returns the glyph bounds for given rune index in Lines source, relative to the upper-left corner of the lines bounding box. If the index is >= the source length, it returns a box at the end of the rendered text (i.e., where a cursor should be to add more text).

func (*Lines) RuneFromLinePos

func (ls *Lines) RuneFromLinePos(tp textpos.Pos) int

RuneFromLinePos returns the rune index in Lines source for given textpos.Pos line and character position. Returns Len() of source if it goes past that.

func (*Lines) RuneToLinePos

func (ls *Lines) RuneToLinePos(ti int) textpos.Pos

RuneToLinePos returns the textpos.Pos line and character position for given rune index in Lines source. If ti >= source Len(), returns a position just after the last actual rune.

func (*Lines) SelectRegion

func (ls *Lines) SelectRegion(r textpos.Range)

SelectRegion adds the selection to given region of runes from the original source runes. Use SelectReset to clear first if desired.

func (*Lines) SelectReset

func (ls *Lines) SelectReset()

SelectReset removes all existing selected regions.

func (*Lines) SetGlyphXAdvance

func (ls *Lines) SetGlyphXAdvance(adv fixed.Int26_6)

SetGlyphXAdvance sets the x advance on all glyphs to given value: for monospaced case.

func (*Lines) StartAtBaseline

func (ls *Lines) StartAtBaseline()

StartAtBaseline removes the offset from the first line that causes the lines to be rendered starting at the upper left corner, so they will instead be rendered starting at the baseline position.

func (*Lines) String

func (ls *Lines) String() string

func (*Lines) UpdateStyle

func (ls *Lines) UpdateStyle(tx rich.Text, tsty *text.Style)

UpdateStyle updates the Decoration, Fill and Stroke colors from the given rich.Text Styles for each line and given text style. This rich.Text must match the content of the shaped one, and differ only in these non-layout styles.

type Math

type Math struct {
	Path *ppath.Path
	BBox math32.Box2
}

Math holds the output of a TeX math expression.

type Run

type Run interface {

	// AsBase returns the base type with relevant shaped text information.
	AsBase() *RunBase

	// LineBounds returns the Line-level Bounds for given Run as rect bounding box.
	LineBounds() math32.Box2

	// Runes returns our rune range in original source using textpos.Range.
	Runes() textpos.Range

	// Advance returns the total distance to advance in going from one run to the next.
	Advance() float32

	// RuneBounds returns the maximal line-bounds level bounding box for given rune index.
	RuneBounds(ri int) math32.Box2

	// RuneAtPoint returns the rune index in Lines source, at given rendered location,
	// based on given starting location for rendering. If the point is out of the
	// line bounds, the nearest point is returned (e.g., start of line based on Y coordinate).
	RuneAtPoint(src rich.Text, pt math32.Vector2, start math32.Vector2) int

	// SetGlyphXAdvance sets the x advance on all glyphs to given value:
	// for monospaced case.
	SetGlyphXAdvance(adv fixed.Int26_6)
}

Run is a span of shaped text with the same font properties, with layout information to enable GUI interaction with shaped text.

type RunBase

type RunBase struct {

	// Font is the [text.Font] compact encoding of the font to use for rendering.
	Font text.Font

	// MaxBounds are the maximal line-level bounds for this run, suitable for region
	// rendering and mouse interaction detection.
	MaxBounds math32.Box2

	// Decoration are the decorations from the style to apply to this run.
	Decoration rich.Decorations

	// Math holds the output of Math formatting via the tex package.
	Math Math

	// FillColor is the color to use for glyph fill (i.e., the standard "ink" color).
	// Will only be non-nil if set for this run; Otherwise use default.
	FillColor image.Image

	// StrokeColor is the color to use for glyph outline stroking, if non-nil.
	StrokeColor image.Image

	// Background is the color to use for the background region, if non-nil.
	Background image.Image
}

Run is a span of text with the same font properties, with full rendering information.

func (*RunBase) SetFromStyle

func (run *RunBase) SetFromStyle(sty *rich.Style, tsty *text.Style)

SetFromStyle sets the run styling parameters from given styles. Will also update non-Font elements, but font can only be set first time in the initial shaping process, otherwise the render is off.

type Shaper

type Shaper interface {

	// Shape turns given input spans into [Runs] of rendered text,
	// using given context needed for complete styling.
	// The results are only valid until the next call to Shape or WrapParagraph:
	// use slices.Clone if needed longer than that.
	// This is called under a mutex lock, so it is safe for parallel use.
	Shape(tx rich.Text, tsty *text.Style, rts *rich.Settings) []Run

	// WrapLines performs line wrapping and shaping on the given rich text source,
	// using the given style information, where the [rich.Style] provides the default
	// style information reflecting the contents of the source (e.g., the default family,
	// weight, etc), for use in computing the default line height. Paragraphs are extracted
	// first using standard newline markers, assumed to coincide with separate spans in the
	// source text, and wrapped separately. For horizontal text, the Lines will render with
	// a position offset at the upper left corner of the overall bounding box of the text.
	// This is called under a mutex lock, so it is safe for parallel use.
	WrapLines(tx rich.Text, defSty *rich.Style, tsty *text.Style, rts *rich.Settings, size math32.Vector2) *Lines

	// FontFamilies returns a list of available font family names on this system.
	FontList() []fonts.Info
}

Shaper is a text shaping system that can shape the layout of rich.Text, including line wrapping. All functions are protected by a mutex.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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