typeset

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: GPL-3.0 Imports: 6 Imported by: 1

README

typeset

Typeset provides utilities for text layout, wrapping, and rendering.

The state of a text layout is stored in a TypeSetter, and it can be drawn to any image.Image using a Drawer which "extends" TypeSetter.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Align

type Align int

Align specifies a text alignment method.

const (
	//                              X     |     Y
	AlignStart  Align = iota // left      | top
	AlignMiddle              // center    | center
	AlignEnd                 // right     | bottom
	AlignEven                // justified | evenly spaced
)

type Drawer

type Drawer struct{ TypeSetter }

Drawer is an extended TypeSetter that is able to draw text. Much like TypeSetter, It has no constructor and its zero value can be used safely.

func (Drawer) Draw

func (drawer Drawer) Draw(
	destination draw.Image,
	color color.Color,
	offset image.Point,
) (
	updatedRegion image.Rectangle,
)

Draw draws the drawer's text onto the specified canvas at the given offset.

type LineLayout

type LineLayout struct {
	Y            fixed.Int26_6
	Width        fixed.Int26_6
	ContentWidth fixed.Int26_6
	SpaceAfter   fixed.Int26_6
	Words        []WordLayout
	BreakAfter   bool
}

LineLayout contains layout information for a single line.

func DoLine

func DoLine(text []rune, face font.Face, wrap bool, width fixed.Int26_6) (line LineLayout, remaining []rune)

DoLine consumes exactly one line from the given string, and produces a line layout according to the given font. It returns the remaining text as well. If wrap is set to true, this function will stop processing words once maxWidth is crossed. The word which would have crossed over the limit will not be processed.

func (*LineLayout) Align

func (line *LineLayout) Align(align Align)

Align aligns the text in the line according to the specified alignment method.

func (*LineLayout) Length added in v0.5.0

func (line *LineLayout) Length() int

Length returns the amount of runes within the line, including the trailing line break if it exists.

type RuneIterator

type RuneIterator func(
	index int,
	char rune,
	position fixed.Point26_6,
) (
	keepGoing bool,
)

RuneIterator is a function that can iterate accross a typesetter's runes.

type RuneLayout

type RuneLayout struct {
	X     fixed.Int26_6
	Width fixed.Int26_6
	Rune  rune
}

RuneLayout contains layout information for a single rune relative to its word.

type TypeSetter

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

TypeSetter manages several lines of text, and can perform layout operations on them. It automatically avoids performing redundant work. It has no constructor and its zero value can be used safely.

func (*TypeSetter) AtPosition

func (setter *TypeSetter) AtPosition(position fixed.Point26_6) (index int)

AtPosition returns the index of the rune at the specified position.

func (*TypeSetter) Em

func (setter *TypeSetter) Em() (width fixed.Int26_6)

Em returns the width of one emspace according to the typesetter's font, which is the width of the capital letter 'M'.

func (*TypeSetter) Face

func (setter *TypeSetter) Face() font.Face

Face returns the TypeSetter's font face as set by SetFace.

func (*TypeSetter) For

func (setter *TypeSetter) For(iterator RuneIterator)

For calls the specified iterator for every rune in the typesetter. If the iterator returns false, the loop will immediately stop.

func (*TypeSetter) Height added in v0.5.3

func (setter *TypeSetter) Height() int

Height returns the height of the typesetter as set by SetHeight.

func (*TypeSetter) LayoutBounds

func (setter *TypeSetter) LayoutBounds() image.Rectangle

LayoutBounds returns the semantic bounding box of the text. The origin point (0, 0) of the rectangle corresponds to the origin of the first line's baseline.

func (*TypeSetter) LayoutBoundsSpace

func (setter *TypeSetter) LayoutBoundsSpace() image.Rectangle

LayoutBoundsSpace is like LayoutBounds, but it also takes into account the trailing whitespace at the end of each line (if it exists).

func (*TypeSetter) Length

func (setter *TypeSetter) Length() int

Length returns the amount of runes in the typesetter.

func (*TypeSetter) LineHeight

func (setter *TypeSetter) LineHeight() fixed.Int26_6

LineHeight returns the height of one line according to the typesetter's font.

func (*TypeSetter) MinimumSize added in v0.4.0

func (setter *TypeSetter) MinimumSize() image.Point

MinimumSize returns the minimum width and height needed to display text. If wrapping is enabled, this method will return (Em(), 0)

func (*TypeSetter) PositionAt

func (setter *TypeSetter) PositionAt(index int) (position fixed.Point26_6)

PositionAt returns the position of the rune at the specified index.

func (*TypeSetter) ReccomendedHeightFor

func (setter *TypeSetter) ReccomendedHeightFor(width int) (height int)

ReccomendedHeightFor returns the reccomended max height if the text were to have its maximum width set to the given width. This does not alter the typesetter's state.

func (*TypeSetter) SetAlign

func (setter *TypeSetter) SetAlign(horizontal, vertical Align)

SetAlign sets the alignment method of the typesetter.

func (*TypeSetter) SetFace

func (setter *TypeSetter) SetFace(face font.Face)

SetFace sets the font face of the typesetter.

func (*TypeSetter) SetHeight added in v0.6.0

func (setter *TypeSetter) SetHeight(heignt int)

SetHeight sets the height of the typesetter. If the height is greater than zero, no lines will be laid out past it. If the height is zero, the text's maximum height will not be constrained.

func (*TypeSetter) SetText

func (setter *TypeSetter) SetText(text []rune)

SetText sets the text content of the typesetter.

func (*TypeSetter) SetWidth added in v0.6.0

func (setter *TypeSetter) SetWidth(width int)

SetWidth sets the width of the typesetter. Text will still be able to overflow outside of this width if wrapping is disabled.

func (*TypeSetter) SetWrap added in v0.4.0

func (setter *TypeSetter) SetWrap(wrap bool)

SetWrap sets whether or not the text wraps around and forms new lines.

func (*TypeSetter) Width added in v0.5.3

func (setter *TypeSetter) Width() int

Width returns the height of the typesetter as set by SetWidth.

type WordLayout

type WordLayout struct {
	X          fixed.Int26_6
	Width      fixed.Int26_6
	SpaceAfter fixed.Int26_6
	Runes      []RuneLayout
}

WordLayout contains layout information for a single word relative to its line.

func DoWord

func DoWord(text []rune, face font.Face) (word WordLayout, remaining []rune)

DoWord consumes exactly one word from the given string, and produces a word layout according to the given font. It returns the remaining text as well.

func (WordLayout) FirstRune

func (word WordLayout) FirstRune() rune

FirstRune returns the last rune in the word.

func (WordLayout) LastRune

func (word WordLayout) LastRune() rune

LastRune returns the last rune in the word.

Jump to

Keyboard shortcuts

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