text

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT, Unlicense Imports: 29 Imported by: 421

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alignment

type Alignment uint8
const (
	Start Alignment = iota
	End
	Middle
)

func (Alignment) Align

func (a Alignment) Align(dir system.TextDirection, width fixed.Int26_6, maxWidth int) fixed.Int26_6

Align returns the x offset that should be applied to text with width so that it appears correctly aligned within a space of size maxWidth and with the primary text direction dir.

func (Alignment) String

func (a Alignment) String() string

type Flags

type Flags uint16
const (
	// FlagTowardOrigin is set for glyphs in runs that flow
	// towards the origin (RTL).
	FlagTowardOrigin Flags = 1 << iota
	// FlagLineBreak is set for the last glyph in a line.
	FlagLineBreak
	// FlagRunBreak is set for the last glyph in a run. A run is a sequence of
	// glyphs sharing constant style properties (same size, same face, same
	// direction, etc...).
	FlagRunBreak
	// FlagClusterBreak is set for the last glyph in a glyph cluster. A glyph cluster is a
	// sequence of glyphs which are logically a single unit, but require multiple
	// symbols from a font to display.
	FlagClusterBreak
	// FlagParagraphBreak indicates that the glyph cluster does not represent actual
	// font glyphs, but was inserted by the shaper to represent line-breaking
	// whitespace characters. After a glyph with FlagParagraphBreak set, the shaper
	// will always return a glyph with FlagParagraphStart providing the X and Y
	// coordinates of the start of the next line, even if that line has no contents.
	FlagParagraphBreak
	// FlagParagraphStart indicates that the glyph starts a new paragraph.
	FlagParagraphStart
	// FlagTruncator indicates that the glyph is part of a special truncator run that
	// represents the portion of text removed due to truncation. A glyph with both
	// FlagTruncator and FlagClusterBreak will have a Runes field accounting for all
	// runes truncated.
	FlagTruncator
)

func (Flags) String

func (f Flags) String() string

type FontFace

type FontFace = giofont.FontFace

type Glyph

type Glyph struct {
	// ID is a unique, per-shaper identifier for the shape of the glyph.
	// Glyphs from the same shaper will share an ID when they are from
	// the same face and represent the same glyph at the same size.
	ID GlyphID

	// X is the x coordinate of the dot for this glyph in document coordinates.
	X fixed.Int26_6
	// Y is the y coordinate of the dot for this glyph in document coordinates.
	Y int32

	// Advance is the logical width of the glyph. The glyph may be visually
	// wider than this.
	Advance fixed.Int26_6
	// Ascent is the distance from the dot to the logical top of glyphs in
	// this glyph's face. The specific glyph may be shorter than this.
	Ascent fixed.Int26_6
	// Descent is the distance from the dot to the logical bottom of glyphs
	// in this glyph's face. The specific glyph may descend less than this.
	Descent fixed.Int26_6
	// Offset encodes the origin of the drawing coordinate space for this glyph
	// relative to the dot. This value is used when converting glyphs to paths.
	Offset fixed.Point26_6
	// Bounds encodes the visual dimensions of the glyph relative to the dot.
	Bounds fixed.Rectangle26_6
	// Runes is the number of runes represented by the glyph cluster this glyph
	// belongs to. If Flags does not contain FlagClusterBreak, this value will
	// always be zero. The final glyph in the cluster contains the runes count
	// for the entire cluster.
	Runes uint16
	// Flags encode special properties of this glyph.
	Flags Flags
}

Glyph describes a shaped font glyph. Many fields are distances relative to the "dot", which is a point on the baseline (the line upon which glyphs visually rest) for the line of text containing the glyph.

Glyphs are organized into "glyph clusters," which are sequences that may represent an arbitrary number of runes.

Sequences of glyph clusters that share style parameters are grouped into "runs."

"Document coordinates" are pixel values relative to the text's origin at (0,0) in the upper-left corner" Displaying each shaped glyph at the document coordinates of its dot will correctly visualize the text.

type GlyphID

type GlyphID uint64

type Parameters

type Parameters struct {
	// Font describes the preferred typeface.
	Font giofont.Font
	// Alignment characterizes the positioning of text within the line. It does not directly
	// impact shaping, but is provided in order to allow efficient offset computation.
	Alignment Alignment
	// PxPerEm is the pixels-per-em to shape the text with.
	PxPerEm fixed.Int26_6
	// MaxLines limits the quantity of shaped lines. Zero means no limit.
	MaxLines int
	// Truncator is a string of text to insert where the shaped text was truncated, which
	// can currently ohly happen if MaxLines is nonzero and the text on the final line is
	// truncated.
	Truncator string

	// WrapPolicy configures how line breaks will be chosen when wrapping text across lines.
	WrapPolicy WrapPolicy

	// MinWidth and MaxWidth provide the minimum and maximum horizontal space constraints
	// for the shaped text.
	MinWidth, MaxWidth int
	// Locale provides primary direction and language information for the shaped text.
	Locale system.Locale

	// LineHeightScale is a scaling factor applied to the LineHeight of a paragraph. If zero, a default
	// value of 1.2 will be used.
	LineHeightScale float32

	// LineHeight is the distance between the baselines of two lines of text. If zero, the PxPerEm
	// of the any given paragraph will set the LineHeight of that paragraph. This value will be
	// scaled by LineHeightScale, so applications desiring a specific fixed value
	// should set LineHeightScale to 1.
	LineHeight fixed.Int26_6
	// contains filtered or unexported fields
}

Parameters are static text shaping attributes applied to the entire shaped text.

type Range

type Range struct {
	// Count describes the number of items represented by the Range.
	Count int
	// Offset describes the start position of the represented
	// items within a larger list.
	Offset int
}

Range describes the position and quantity of a range of text elements within a larger slice. The unit is usually runes of unicode data or glyphs of shaped font data.

type Shaper

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

Shaper converts strings of text into glyphs that can be displayed.

func NewShaper

func NewShaper(options ...ShaperOption) *Shaper

NewShaper constructs a shaper with the provided options.

NewShaper must be called after [app.NewWindow], unless the NoSystemFonts option is specified. This is an unfortunate restriction caused by some platforms such as Android.

func (*Shaper) Bitmaps

func (l *Shaper) Bitmaps(gs []Glyph) op.CallOp

Bitmaps extracts bitmap glyphs from the provided slice and creates an op.CallOp to present them. The returned op.CallOp will align correctly with the return value of Shape() for the same gs slice. All glyphs are expected to be from a single line of text (their Y offsets are ignored).

func (*Shaper) Layout

func (l *Shaper) Layout(params Parameters, txt io.Reader)

Layout text from an io.Reader according to a set of options. Results can be retrieved by iteratively calling NextGlyph.

func (*Shaper) LayoutString

func (l *Shaper) LayoutString(params Parameters, str string)

LayoutString is Layout for strings.

func (*Shaper) NextGlyph

func (l *Shaper) NextGlyph() (_ Glyph, ok bool)

NextGlyph returns the next glyph from the most recent shaping operation, if any. If there are no more glyphs, ok will be false.

func (*Shaper) Shape

func (l *Shaper) Shape(gs []Glyph) clip.PathSpec

Shape converts the provided glyphs into a path. The path will enclose the forms of all vector glyphs. All glyphs are expected to be from a single line of text (their Y offsets are ignored).

type ShaperOption added in v0.2.0

type ShaperOption func(*Shaper)

ShaperOptions configure text shapers.

func NoSystemFonts added in v0.2.0

func NoSystemFonts() ShaperOption

NoSystemFonts can be used to disable system font loading.

func WithCollection added in v0.2.0

func WithCollection(collection []FontFace) ShaperOption

WithCollection can be used to provide a collection of pre-loaded fonts to the shaper.

type WrapPolicy

type WrapPolicy uint8

WrapPolicy configures strategies for choosing where to break lines of text for line wrapping.

const (
	// WrapHeuristically tries to minimize breaking within words (UAX#14 text segments)
	// while also ensuring that text fits within the given MaxWidth. It will only break
	// a line within a word (on a UAX#29 grapheme cluster boundary) when that word cannot
	// fit on a line by itself. Additionally, when the final word of a line is being
	// truncated, this policy will preserve as many symbols of that word as
	// possible before the truncator.
	WrapHeuristically WrapPolicy = iota
	// WrapWords does not permit words (UAX#14 text segments) to be broken across lines.
	// This means that sometimes long words will exceed the MaxWidth they are wrapped with.
	WrapWords
	// WrapGraphemes will maximize the amount of text on each line at the expense of readability,
	// breaking any word across lines on UAX#29 grapheme cluster boundaries to maximize the number of
	// grapheme clusters on each line.
	WrapGraphemes
)

Jump to

Keyboard shortcuts

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