rich

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: 16 Imported by: 0

README

Rich Text

The rich.Text type is the standard representation for formatted text, used as the input to the shaped package for text layout and rendering. It is encoded purely using []rune slices for each span, with the style information represented with special rune values at the start of each span. This is an efficient and GPU-friendly pure-value format that avoids any issues of style struct pointer management etc.

It provides basic font styling properties (bold, italic, underline, font family, size, color) and some basic, essential broader formatting information.

It is physically a flat format, with no hierarchical nesting of spans: any mechanism that creates rich.Text must compile the relevant nested spans down into a flat final representation, as the htmltext package does.

However, the Specials elements are indeed special, acting more like html start / end tags, using the special End value to end any previous special that was started (these must therefore be generated in a strictly nested manner). Use StartSpecial and EndSpecial to set these values safely, and helper methods for setting simple Link, Super, Sub, and Math spans are provided.

The \n newline is used to mark the end of a paragraph, and in general text will be automatically wrapped to fit a given size, in the shaped package. If the text starting after a newline has a ParagraphStart decoration, then it will be styled according to the text.Style paragraph styles (indent and paragraph spacing). The HTML parser sets this as appropriate based on <br> vs <p> tags.

Documentation

Index

Constants

View Source
const (
	DecorationStart = 0
	DecorationMask  = 0x000007FF // 11 bits reserved for deco
	SlantStart      = 11
	SlantMask       = 0x00000800 // 1 bit for slant
	SpecialStart    = 12
	SpecialMask     = 0x0000F000
	StretchStart    = 16
	StretchMask     = 0x000F0000
	WeightStart     = 20
	WeightMask      = 0x00F00000
	FamilyStart     = 24
	FamilyMask      = 0x0F000000
	DirectionStart  = 28
	DirectionMask   = 0xF0000000
)

Variables

View Source
var FontSizes = map[string]float32{
	"xx-small": 6.0 / 12.0,
	"x-small":  8.0 / 12.0,
	"small":    10.0 / 12.0,
	"smallf":   10.0 / 12.0,
	"medium":   1,
	"large":    14.0 / 12.0,
	"x-large":  18.0 / 12.0,
	"xx-large": 24.0 / 12.0,
}

FontSizePoints maps standard font names to standard point sizes -- we use dpi zoom scaling instead of rescaling "medium" font size, so generally use these values as-is. smaller and larger relative scaling can move in 2pt increments

Functions

func AddFamily

func AddFamily(rts FontName, fam string) string

AddFamily adds a family specifier to the given font string, handling the comma properly.

func ColorFromRune

func ColorFromRune(r rune) color.RGBA

ColorFromRune converts given color from a rune uint32 value.

func ColorToRune

func ColorToRune(c color.Color) rune

ColorToRune converts given color to a rune uint32 value.

func FamiliesToList

func FamiliesToList(fam string) []string

FamiliesToList returns a list of the families, split by comma and space removed.

func RuneFromDecoration

func RuneFromDecoration(d Decorations) rune

RuneFromDecoration returns the rune bit values for given decoration.

func RuneFromDirection

func RuneFromDirection(d Directions) rune

RuneFromDirection returns the rune bit values for given direction.

func RuneFromFamily

func RuneFromFamily(d Family) rune

RuneFromFamily returns the rune bit values for given family.

func RuneFromSlant

func RuneFromSlant(d Slants) rune

RuneFromSlant returns the rune bit values for given slant.

func RuneFromSpecial

func RuneFromSpecial(d Specials) rune

RuneFromSpecial returns the rune bit values for given special.

func RuneFromStretch

func RuneFromStretch(d Stretch) rune

RuneFromStretch returns the rune bit values for given stretch.

func RuneFromStyle

func RuneFromStyle(s *Style) rune

RuneFromStyle returns the style rune that encodes the given style values.

func RuneFromWeight

func RuneFromWeight(d Weights) rune

RuneFromWeight returns the rune bit values for given weight.

func RuneToStyle

func RuneToStyle(s *Style, r rune)

RuneToStyle sets all the style values decoded from given rune.

func SpanLen

func SpanLen(s []rune) (sn int, rn int)

SpanLen returns the length of the starting style runes and following content runes for given slice of span runes. Does not need to decode full style, so is very efficient.

Types

type Decorations

type Decorations int64 //enums:bitflag -transform kebab

Decorations are underline, line-through, etc, as bit flags that must be set using [Font.SetDecoration].

const (
	// Underline indicates to place a line below text.
	Underline Decorations = iota

	// Overline indicates to place a line above text.
	Overline

	// LineThrough indicates to place a line through text.
	LineThrough

	// DottedUnderline is used for abbr tag.
	DottedUnderline

	// ParagraphStart indicates that this text is the start of a paragraph,
	// and therefore may be indented according to [text.Style] settings.
	ParagraphStart
)
const DecorationsN Decorations = 5

DecorationsN is the highest valid value for type Decorations, plus one.

func DecorationsValues

func DecorationsValues() []Decorations

DecorationsValues returns all possible values for the type Decorations.

func RuneToDecoration

func RuneToDecoration(r rune) Decorations

RuneToDecoration returns the Decoration bit values from given rune.

func (Decorations) BitIndexString

func (i Decorations) BitIndexString() string

BitIndexString returns the string representation of this Decorations value if it is a bit index value (typically an enum constant), and not an actual bit flag value.

func (Decorations) Desc

func (i Decorations) Desc() string

Desc returns the description of the Decorations value.

func (*Decorations) HasFlag

func (i *Decorations) HasFlag(f enums.BitFlag) bool

HasFlag returns whether these bit flags have the given bit flag set.

func (Decorations) Int64

func (i Decorations) Int64() int64

Int64 returns the Decorations value as an int64.

func (Decorations) MarshalText

func (i Decorations) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Decorations) NumColors

func (d Decorations) NumColors() int

NumColors returns the number of colors used by this decoration setting.

func (*Decorations) SetFlag

func (i *Decorations) SetFlag(on bool, f ...enums.BitFlag)

SetFlag sets the value of the given flags in these flags to the given value.

func (*Decorations) SetInt64

func (i *Decorations) SetInt64(in int64)

SetInt64 sets the Decorations value from an int64.

func (*Decorations) SetString

func (i *Decorations) SetString(s string) error

SetString sets the Decorations value from its string representation, and returns an error if the string is invalid.

func (*Decorations) SetStringOr

func (i *Decorations) SetStringOr(s string) error

SetStringOr sets the Decorations value from its string representation while preserving any bit flags already set, and returns an error if the string is invalid.

func (Decorations) String

func (i Decorations) String() string

String returns the string representation of this Decorations value.

func (*Decorations) UnmarshalText

func (i *Decorations) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Decorations) Values

func (i Decorations) Values() []enums.Enum

Values returns all possible values for the type Decorations.

type Directions

type Directions int32 //enums:enum -transform kebab

Directions specifies the text layout direction.

const (
	// LTR is Left-to-Right text.
	LTR Directions = iota

	// RTL is Right-to-Left text.
	RTL

	// TTB is Top-to-Bottom text.
	TTB

	// BTT is Bottom-to-Top text.
	BTT

	// Default uses the [text.Style] default direction.
	Default
)
const DirectionsN Directions = 5

DirectionsN is the highest valid value for type Directions, plus one.

func DirectionsValues

func DirectionsValues() []Directions

DirectionsValues returns all possible values for the type Directions.

func RuneToDirection

func RuneToDirection(r rune) Directions

RuneToDirection returns the Directions value from given rune.

func (Directions) Desc

func (i Directions) Desc() string

Desc returns the description of the Directions value.

func (Directions) Int64

func (i Directions) Int64() int64

Int64 returns the Directions value as an int64.

func (Directions) IsVertical

func (d Directions) IsVertical() bool

IsVertical returns true if given text is vertical.

func (Directions) MarshalText

func (i Directions) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Directions) SetInt64

func (i *Directions) SetInt64(in int64)

SetInt64 sets the Directions value from an int64.

func (*Directions) SetString

func (i *Directions) SetString(s string) error

SetString sets the Directions value from its string representation, and returns an error if the string is invalid.

func (Directions) String

func (i Directions) String() string

String returns the string representation of this Directions value.

func (Directions) ToGoText

func (d Directions) ToGoText() di.Direction

ToGoText returns the go-text version of direction.

func (*Directions) UnmarshalText

func (i *Directions) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Directions) Values

func (i Directions) Values() []enums.Enum

Values returns all possible values for the type Directions.

type Family

type Family int32 //enums:enum -trim-prefix Family -transform kebab

Family specifies the generic family of typeface to use, where the specific named values to use for each are provided in the Settings.

const (
	// SansSerif is a font without serifs, where glyphs have plain stroke endings,
	// without ornamentation. Example sans-serif fonts include Arial, Helvetica,
	// Open Sans, Fira Sans, Lucida Sans, Lucida Sans Unicode, Trebuchet MS,
	// Liberation Sans, and Nimbus Sans L.
	SansSerif Family = iota

	// Serif is a small line or stroke attached to the end of a larger stroke
	// in a letter. In serif fonts, glyphs have finishing strokes, flared or
	// tapering ends. Examples include Times New Roman, Lucida Bright,
	// Lucida Fax, Palatino, Palatino Linotype, Palladio, and URW Palladio.
	Serif

	// Monospace fonts have all glyphs with he same fixed width.
	// Example monospace fonts include Fira Mono, DejaVu Sans Mono,
	// Menlo, Consolas, Liberation Mono, Monaco, and Lucida Console.
	Monospace

	// Cursive glyphs generally have either joining strokes or other cursive
	// characteristics beyond those of italic typefaces. The glyphs are partially
	// or completely connected, and the result looks more like handwritten pen or
	// brush writing than printed letter work. Example cursive fonts include
	// Brush Script MT, Brush Script Std, Lucida Calligraphy, Lucida Handwriting,
	// and Apple Chancery.
	Cursive

	// Fantasy fonts are primarily decorative fonts that contain playful
	// representations of characters. Example fantasy fonts include Papyrus,
	// Herculanum, Party LET, Curlz MT, and Harrington.
	Fantasy

	//	Math fonts are for displaying mathematical expressions, for example
	// superscript and subscript, brackets that cross several lines, nesting
	// expressions, and double-struck glyphs with distinct meanings.
	Math

	// Emoji fonts are specifically designed to render emoji.
	Emoji

	// Fangsong are a particular style of Chinese characters that are between
	// serif-style Song and cursive-style Kai forms. This style is often used
	// for government documents.
	Fangsong

	// Custom is a custom font name that is specified in the [text.Style]
	// CustomFont name.
	Custom
)
const FamilyN Family = 9

FamilyN is the highest valid value for type Family, plus one.

func FamilyValues

func FamilyValues() []Family

FamilyValues returns all possible values for the type Family.

func RuneToFamily

func RuneToFamily(r rune) Family

RuneToFamily returns the Familys value from given rune.

func (Family) Desc

func (i Family) Desc() string

Desc returns the description of the Family value.

func (Family) Int64

func (i Family) Int64() int64

Int64 returns the Family value as an int64.

func (Family) MarshalText

func (i Family) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Family) SetInt64

func (i *Family) SetInt64(in int64)

SetInt64 sets the Family value from an int64.

func (*Family) SetString

func (i *Family) SetString(s string) error

SetString sets the Family value from its string representation, and returns an error if the string is invalid.

func (Family) String

func (i Family) String() string

String returns the string representation of this Family value.

func (*Family) UnmarshalText

func (i *Family) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Family) Values

func (i Family) Values() []enums.Enum

Values returns all possible values for the type Family.

type FontName

type FontName string

FontName is a special string that provides a font chooser. It is aliased to [core.FontName] as well.

type Hyperlink struct {
	// Label is the text label for the link.
	Label string

	// URL is the full URL for the link.
	URL string

	// Range defines the starting and ending positions of the link,
	// in terms of source rune indexes.
	Range textpos.Range
}

Hyperlink represents a hyperlink within shaped text.

type Settings

type Settings struct {

	// Language is the preferred language used for rendering text.
	Language language.Language

	// Script is the specific writing system used for rendering text.
	// todo: no idea how to set this based on language or anything else.
	Script language.Script `display:"-"`

	// SansSerif is a font without serifs, where glyphs have plain stroke endings,
	// without ornamentation. Example sans-serif fonts include Arial, Helvetica,
	// Noto Sans, Open Sans, Fira Sans, Lucida Sans, Lucida Sans Unicode, Trebuchet MS,
	// Liberation Sans, Nimbus Sans L, Roboto.
	// This can be a list of comma-separated names, tried in order.
	// "sans-serif" will be added automatically as a final backup.
	SansSerif FontName `default:"Noto Sans"`

	// Serif is a small line or stroke attached to the end of a larger stroke
	// in a letter. In serif fonts, glyphs have finishing strokes, flared or
	// tapering ends. Examples include Times New Roman, Lucida Bright,
	// Lucida Fax, Palatino, Palatino Linotype, Palladio, and URW Palladio.
	// This can be a list of comma-separated names, tried in order.
	// "serif" will be added automatically as a final backup.
	Serif FontName

	// Monospace fonts have all glyphs with he same fixed width.
	// Example monospace fonts include Roboto Mono, Fira Mono, DejaVu Sans Mono,
	// Menlo, Consolas, Liberation Mono, Monaco, and Lucida Console.
	// This can be a list of comma-separated names. serif will be added
	// automatically as a final backup.
	// This can be a list of comma-separated names, tried in order.
	// "monospace" will be added automatically as a final backup.
	Monospace FontName `default:"Roboto Mono"`

	// Cursive glyphs generally have either joining strokes or other cursive
	// characteristics beyond those of italic typefaces. The glyphs are partially
	// or completely connected, and the result looks more like handwritten pen or
	// brush writing than printed letter work. Example cursive fonts include
	// Brush Script MT, Brush Script Std, Lucida Calligraphy, Lucida Handwriting,
	// and Apple Chancery.
	// This can be a list of comma-separated names, tried in order.
	// "cursive" will be added automatically as a final backup.
	Cursive FontName

	// Fantasy fonts are primarily decorative fonts that contain playful
	// representations of characters. Example fantasy fonts include Papyrus,
	// Herculanum, Party LET, Curlz MT, and Harrington.
	// This can be a list of comma-separated names, tried in order.
	// "fantasy" will be added automatically as a final backup.
	Fantasy FontName

	// Math fonts are for displaying mathematical expressions, for example
	// superscript and subscript, brackets that cross several lines, nesting
	// expressions, and double-struck glyphs with distinct meanings.
	// This can be a list of comma-separated names, tried in order.
	// "math" will be added automatically as a final backup.
	Math FontName

	// Emoji fonts are specifically designed to render emoji.
	// This can be a list of comma-separated names, tried in order.
	// "emoji" will be added automatically as a final backup.
	Emoji FontName

	// Fangsong are a particular style of Chinese characters that are between
	// serif-style Song and cursive-style Kai forms. This style is often used
	// for government documents.
	// This can be a list of comma-separated names, tried in order.
	// "fangsong" will be added automatically as a final backup.
	Fangsong FontName
}

Settings holds the global settings for rich text styling, including language, script, and preferred font faces for each category of font.

var DefaultSettings Settings

DefaultSettings contains the default global text settings. This will be updated from rich.DefaultSettings.

func (*Settings) Defaults

func (rts *Settings) Defaults()

func (*Settings) Family

func (rts *Settings) Family(fam Family) string

Family returns the font family specified by the given Family enum.

type Slants

type Slants int32 //enums:enum -trim-prefix Slant -transform kebab

Slants (also called style) allows italic or oblique faces to be selected.

const (

	// A face that is neither italic not obliqued.
	SlantNormal Slants = iota

	// A form that is generally cursive in nature or slanted.
	// This groups what is usually called Italic or Oblique.
	Italic
)
const SlantsN Slants = 2

SlantsN is the highest valid value for type Slants, plus one.

func RuneToSlant

func RuneToSlant(r rune) Slants

RuneToSlant returns the Slants value from given rune.

func SlantsValues

func SlantsValues() []Slants

SlantsValues returns all possible values for the type Slants.

func (Slants) Desc

func (i Slants) Desc() string

Desc returns the description of the Slants value.

func (Slants) Int64

func (i Slants) Int64() int64

Int64 returns the Slants value as an int64.

func (Slants) MarshalText

func (i Slants) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Slants) SetInt64

func (i *Slants) SetInt64(in int64)

SetInt64 sets the Slants value from an int64.

func (*Slants) SetString

func (i *Slants) SetString(s string) error

SetString sets the Slants value from its string representation, and returns an error if the string is invalid.

func (Slants) String

func (i Slants) String() string

String returns the string representation of this Slants value.

func (*Slants) UnmarshalText

func (i *Slants) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Slants) Values

func (i Slants) Values() []enums.Enum

Values returns all possible values for the type Slants.

type Specials

type Specials int32 //enums:enum -transform kebab

Specials are special additional mutually exclusive formatting factors that are not otherwise captured by changes in font rendering properties or decorations. Each special must be terminated by an End span element, on its own, which pops the stack on the last special that was started. Use Text.StartSpecial and Text.EndSpecial to manage the specials, avoiding the potential for repeating the start of a given special.

const (
	// Nothing special.
	Nothing Specials = iota

	// Super starts super-scripted text.
	Super

	// Sub starts sub-scripted text.
	Sub

	// Link starts a hyperlink, which is in the URL field of the
	// style, and encoded in the runes after the style runes.
	// It also identifies this span for functional interactions
	// such as hovering and clicking. It does not specify the styling,
	// which therefore must be set in addition.
	Link

	// MathInline starts a TeX formatted math sequence, styled for
	// inclusion inline with other text.
	MathInline

	// MathDisplay starts a TeX formatted math sequence, styled as
	// a larger standalone display.
	MathDisplay

	// Quote starts an indented paragraph-level quote.
	Quote

	// End must be added to terminate the last Special started: use [Text.AddEnd].
	// The renderer maintains a stack of special elements.
	End
)
const SpecialsN Specials = 8

SpecialsN is the highest valid value for type Specials, plus one.

func RuneToSpecial

func RuneToSpecial(r rune) Specials

RuneToSpecial returns the Specials value from given rune.

func SpecialsValues

func SpecialsValues() []Specials

SpecialsValues returns all possible values for the type Specials.

func (Specials) Desc

func (i Specials) Desc() string

Desc returns the description of the Specials value.

func (Specials) Int64

func (i Specials) Int64() int64

Int64 returns the Specials value as an int64.

func (Specials) MarshalText

func (i Specials) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Specials) SetInt64

func (i *Specials) SetInt64(in int64)

SetInt64 sets the Specials value from an int64.

func (*Specials) SetString

func (i *Specials) SetString(s string) error

SetString sets the Specials value from its string representation, and returns an error if the string is invalid.

func (Specials) String

func (i Specials) String() string

String returns the string representation of this Specials value.

func (*Specials) UnmarshalText

func (i *Specials) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Specials) Values

func (i Specials) Values() []enums.Enum

Values returns all possible values for the type Specials.

type Stretch

type Stretch int32 //enums:enum -trim-prefix Stretch -transform kebab

Stretch is the width of a font as an approximate fraction of the normal width. Widths range from 0.5 to 2.0 inclusive, with 1.0 as the normal width.

const (

	// Ultra-condensed width (50%), the narrowest possible.
	UltraCondensed Stretch = iota

	// Extra-condensed width (62.5%).
	ExtraCondensed

	// Condensed width (75%).
	Condensed

	// Semi-condensed width (87.5%).
	SemiCondensed

	// Normal width (100%).
	StretchNormal

	// Semi-expanded width (112.5%).
	SemiExpanded

	// Expanded width (125%).
	Expanded

	// Extra-expanded width (150%).
	ExtraExpanded

	// Ultra-expanded width (200%), the widest possible.
	UltraExpanded
)
const StretchN Stretch = 9

StretchN is the highest valid value for type Stretch, plus one.

func RuneToStretch

func RuneToStretch(r rune) Stretch

RuneToStretch returns the Stretch value from given rune.

func StretchValues

func StretchValues() []Stretch

StretchValues returns all possible values for the type Stretch.

func (Stretch) Desc

func (i Stretch) Desc() string

Desc returns the description of the Stretch value.

func (Stretch) Int64

func (i Stretch) Int64() int64

Int64 returns the Stretch value as an int64.

func (Stretch) MarshalText

func (i Stretch) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Stretch) SetInt64

func (i *Stretch) SetInt64(in int64)

SetInt64 sets the Stretch value from an int64.

func (*Stretch) SetString

func (i *Stretch) SetString(s string) error

SetString sets the Stretch value from its string representation, and returns an error if the string is invalid.

func (Stretch) String

func (i Stretch) String() string

String returns the string representation of this Stretch value.

func (Stretch) ToFloat32

func (s Stretch) ToFloat32() float32

ToFloat32 converts the stretch to its numerical multiplier value

func (*Stretch) UnmarshalText

func (i *Stretch) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Stretch) Values

func (i Stretch) Values() []enums.Enum

Values returns all possible values for the type Stretch.

type Style

type Style struct {

	// Size is the font size multiplier relative to the standard font size
	// specified in the [text.Style].
	Size float32

	// Family indicates the generic family of typeface to use, where the
	// specific named values to use for each are provided in the [Settings],
	// or [text.Style] for [Custom].
	Family Family

	// Slant allows italic or oblique faces to be selected.
	Slant Slants

	// Weights are the degree of blackness or stroke thickness of a font.
	// This value ranges from 100.0 to 900.0, with 400.0 as normal.
	Weight Weights

	// Stretch is the width of a font as an approximate fraction of the normal width.
	// Widths range from 0.5 to 2.0 inclusive, with 1.0 as the normal width.
	Stretch Stretch

	// Special additional formatting factors that are not otherwise
	// captured by changes in font rendering properties or decorations.
	// See [Specials] for usage information: use [Text.StartSpecial]
	// and [Text.EndSpecial] to set.
	Special Specials

	// Decorations are underline, line-through, etc, as bit flags
	// that must be set using [Decorations.SetFlag].
	Decoration Decorations `set:"-"`

	// Direction is the direction to render the text.
	Direction Directions

	// URL is the URL for a link element. It is encoded in runes after the style runes.
	URL string
	// contains filtered or unexported fields
}

Style contains all of the rich text styling properties, that apply to one span of text. These are encoded into a uint32 rune value in rich.Text. See [text.Style] and Settings for additional context needed for full specification.

func NewStyle

func NewStyle() *Style

func NewStyleFromRunes

func NewStyleFromRunes(rs []rune) (*Style, []rune)

NewStyleFromRunes returns a new style initialized with data from given runes, returning the remaining actual rune string content after style data.

func (*Style) Background

func (s *Style) Background() color.Color

func (*Style) Clone

func (s *Style) Clone() *Style

Clone returns a copy of this style.

func (*Style) Defaults

func (s *Style) Defaults()

func (*Style) FillColor

func (s *Style) FillColor() color.Color

func (*Style) FontFamily

func (s *Style) FontFamily(ctx *Settings) string

FontFamily returns the font family name(s) based on [Style.Family] and the values specified in the given Settings.

func (*Style) FromProperties

func (s *Style) FromProperties(parent *Style, properties map[string]any, ctxt colors.Context)

FromProperties sets style field values based on the given property list.

func (*Style) FromProperty

func (s *Style) FromProperty(parent *Style, key string, val any, cc colors.Context)

FromProperty sets style field values based on the given property key and value.

func (*Style) FromRunes

func (s *Style) FromRunes(rs []rune) []rune

FromRunes sets the Style properties from the given rune encodings which must be the proper length including colors. Any remaining runes after the style runes are returned: this is the source string.

func (*Style) InheritFields

func (s *Style) InheritFields(parent *Style)

InheritFields from parent

func (*Style) IsMath

func (s *Style) IsMath() bool

IsMath returns true if is a Special MathInline or MathDisplay.

func (*Style) SetBackground

func (s *Style) SetBackground(clr color.Color) *Style

SetBackground sets the background color to given color, setting the Decoration flag and the color value. The background is not normally colored so it renders over any background.

func (*Style) SetDecoration

func (s *Style) SetDecoration(deco ...Decorations) *Style

SetDecoration sets given decoration flag(s) on.

func (*Style) SetDirection

func (t *Style) SetDirection(v Directions) *Style

SetDirection sets the [Style.Direction]: Direction is the direction to render the text.

func (*Style) SetFamily

func (t *Style) SetFamily(v Family) *Style

SetFamily sets the [Style.Family]: Family indicates the generic family of typeface to use, where the specific named values to use for each are provided in the Settings, or [text.Style] for Custom.

func (*Style) SetFillColor

func (s *Style) SetFillColor(clr color.Color) *Style

SetFillColor sets the fill color to given color, setting the Decoration flag and the color value.

func (*Style) SetFromHTMLTag

func (s *Style) SetFromHTMLTag(tag string) bool

SetFromHTMLTag sets the styling parameters for simple HTML style tags. Returns true if handled.

func (s *Style) SetLink(url string) *Style

SetLink sets the given style as a hyperlink, with given URL, and default link styling.

func (*Style) SetLinkStyle

func (s *Style) SetLinkStyle() *Style

SetLinkStyle sets the default hyperlink styling: primary.Base color (e.g., blue) and Underline.

func (*Style) SetSize

func (t *Style) SetSize(v float32) *Style

SetSize sets the [Style.Size]: Size is the font size multiplier relative to the standard font size specified in the [text.Style].

func (*Style) SetSlant

func (t *Style) SetSlant(v Slants) *Style

SetSlant sets the [Style.Slant]: Slant allows italic or oblique faces to be selected.

func (*Style) SetSpecial

func (t *Style) SetSpecial(v Specials) *Style

SetSpecial sets the [Style.Special]: Special additional formatting factors that are not otherwise captured by changes in font rendering properties or decorations. See Specials for usage information: use Text.StartSpecial and Text.EndSpecial to set.

func (*Style) SetStretch

func (t *Style) SetStretch(v Stretch) *Style

SetStretch sets the [Style.Stretch]: Stretch is the width of a font as an approximate fraction of the normal width. Widths range from 0.5 to 2.0 inclusive, with 1.0 as the normal width.

func (*Style) SetStrokeColor

func (s *Style) SetStrokeColor(clr color.Color) *Style

SetStrokeColor sets the stroke color to given color, setting the Decoration flag and the color value. This is normally not set: it looks like an outline of the glyph at larger font sizes, and will make smaller font sizes look significantly thicker.

func (*Style) SetURL

func (t *Style) SetURL(v string) *Style

SetURL sets the [Style.URL]: URL is the URL for a link element. It is encoded in runes after the style runes.

func (*Style) SetWeight

func (t *Style) SetWeight(v Weights) *Style

SetWeight sets the [Style.Weight]: Weights are the degree of blackness or stroke thickness of a font. This value ranges from 100.0 to 900.0, with 400.0 as normal.

func (*Style) String

func (s *Style) String() string

func (*Style) StrokeColor

func (s *Style) StrokeColor() color.Color

func (*Style) ToRunes

func (s *Style) ToRunes() []rune

ToRunes returns the rune(s) that encode the given style including any additional colors beyond the style and size runes, and the URL for a link.

type Text

type Text [][]rune

Text is the basic rich text representation, with spans of []rune unicode characters that share a common set of text styling properties, which are represented by the first rune(s) in each span. If custom colors are used, they are encoded after the first style and size runes. This compact and efficient representation can be Join'd back into the raw unicode source, and indexing by rune index in the original is fast. It provides a GPU-compatible representation, and is the text equivalent of the [ppath.Path] encoding.

func Join

func Join(txts ...Text) Text

Join joins multiple texts into one text. Just appends the spans.

func NewPlainText

func NewPlainText(r []rune) Text

NewPlainText returns a new Text starting with default style and runes string, which can be empty.

func NewText

func NewText(s *Style, r []rune) Text

NewText returns a new Text starting with given style and runes string, which can be empty.

func (tx *Text) AddLink(s *Style, url, label string) *Text

AddLink adds a Link special with given url and label text. This calls StartSpecial and EndSpecial for you. If the link requires further formatting, use those functions separately.

func (*Text) AddMathDisplay

func (tx *Text) AddMathDisplay(s *Style, text string) *Text

AddMathDisplay adds a MathDisplay special with given text. This calls StartSpecial and EndSpecial for you. If the Math requires further formatting, use those functions separately.

func (*Text) AddMathInline

func (tx *Text) AddMathInline(s *Style, text string) *Text

AddMathInline adds a MathInline special with given text. This calls StartSpecial and EndSpecial for you. If the Math requires further formatting, use those functions separately.

func (*Text) AddRunes

func (tx *Text) AddRunes(r []rune) *Text

AddRunes adds given runes to current span. If no existing span, then a new default one is made.

func (*Text) AddSpan

func (tx *Text) AddSpan(s *Style, r []rune) *Text

AddSpan adds a span to the Text using the given Style and runes. The Text is modified for convenience in the high-frequency use-case. Clone first to avoid changing the original.

func (*Text) AddSpanString

func (tx *Text) AddSpanString(s *Style, r string) *Text

AddSpanString adds a span to the Text using the given Style and string content. The Text is modified for convenience in the high-frequency use-case. Clone first to avoid changing the original.

func (*Text) AddSub

func (tx *Text) AddSub(s *Style, text string) *Text

AddSub adds a Sub special with given text. This calls StartSpecial and EndSpecial for you. If the Sub requires further formatting, use those functions separately.

func (*Text) AddSuper

func (tx *Text) AddSuper(s *Style, text string) *Text

AddSuper adds a Super special with given text. This calls StartSpecial and EndSpecial for you. If the Super requires further formatting, use those functions separately.

func (Text) At

func (tx Text) At(li int) rune

At returns the rune at given logical index into the original source rune slice without any styling elements. Returns 0 if index is invalid. See AtTry for a version that also returns a bool indicating whether the index is valid.

func (Text) AtTry

func (tx Text) AtTry(li int) (rune, bool)

AtTry returns the rune at given logical index, as in the original source rune slice without any styling elements. Returns 0 and false if index is invalid.

func (Text) Clone

func (tx Text) Clone() Text

Clone returns a deep copy clone of the current text, safe for subsequent modification without affecting this one.

func (Text) DebugDump

func (tx Text) DebugDump()

func (*Text) EndSpecial

func (tx *Text) EndSpecial() *Text

EndSpecial adds an End Special to the Text, to terminate the current Special. All Specials must be terminated with this empty end tag.

func (tx Text) GetLinks() []Hyperlink

GetLinks gets all the links from the source.

func (Text) Index

func (tx Text) Index(li int) (span, stylen, ridx int)

Index returns the span index, number of style runes at start of span, and index into actual runes within the span after style runes, for the given logical index into the original source rune slice without spans or styling elements. If the logical index is invalid for the text returns -1,-1,-1.

func (*Text) InsertEndSpecial

func (tx *Text) InsertEndSpecial(at int) *Text

InsertEndSpecial inserts an End Special to the Text at given span index, to terminate the current Special. All Specials must be terminated with this empty end tag.

func (*Text) InsertSpan

func (tx *Text) InsertSpan(at int, s *Style, r []rune) *Text

InsertSpan inserts a span to the Text at given span index, using the given Style and runes. The Text is modified for convenience in the high-frequency use-case. Clone first to avoid changing the original.

func (Text) Join

func (tx Text) Join() []rune

Join returns a single slice of runes with the contents of all span runes.

func (Text) Len

func (tx Text) Len() int

Len returns the total number of runes in this Text.

func (Text) NumSpans

func (tx Text) NumSpans() int

NumSpans returns the number of spans in this Text.

func (Text) Range

func (tx Text) Range(span int) (start, end int)

Range returns the start, end range of indexes into original source for given span index.

func (*Text) SetSpanRunes

func (tx *Text) SetSpanRunes(si int, r []rune) *Text

SetSpanRunes sets the runes for given span.

func (*Text) SetSpanStyle

func (tx *Text) SetSpanStyle(si int, nsty *Style) *Text

SetSpanStyle sets the style for given span, updating the runes to encode it.

func (Text) Span

func (tx Text) Span(si int) (*Style, []rune)

Span returns the Style and []rune content for given span index. Returns nil if out of range.

func (Text) SpecialRange

func (tx Text) SpecialRange(si int) textpos.Range

SpecialRange returns the range of spans for the special starting at given span index. Returns -1 if span at given index is not a special.

func (Text) Split

func (tx Text) Split() [][]rune

Split returns the raw rune spans without any styles. The rune span slices here point directly into the Text rune slices. See SplitCopy for a version that makes a copy instead.

func (Text) SplitCopy

func (tx Text) SplitCopy() [][]rune

SplitCopy returns the raw rune spans without any styles. The rune span slices here are new copies; see also Text.Split.

func (*Text) SplitSpaces

func (tx *Text) SplitSpaces()

SplitSpaces splits this text after first unicode space after non-space.

func (*Text) SplitSpan

func (tx *Text) SplitSpan(li int) int

SplitSpan splits an existing span at the given logical source index, with the span containing that logical index truncated to contain runes just before the index, and a new span inserted starting at that index, with the remaining contents of the original containing span. If that logical index is already the start of a span, or the logical index is invalid, nothing happens. Returns the index of span, which will be negative if the logical index is out of range.

func (*Text) StartSpecial

func (tx *Text) StartSpecial(s *Style, special Specials, r []rune) *Text

StartSpecial adds a Span of given Special type to the Text, using given style and rune text. This creates a new style with the special value set, to avoid accidentally repeating the start of new specials.

func (Text) String

func (tx Text) String() string

type Weights

type Weights int32 //enums:enum -transform kebab

Weights are the degree of blackness or stroke thickness of a font. The corresponding value ranges from 100.0 to 900.0, with 400.0 as normal.

const (
	// Thin weight (100), the thinnest value.
	Thin Weights = iota

	// Extra light weight (200).
	ExtraLight

	// Light weight (300).
	Light

	// Normal (400).
	Normal

	// Medium weight (500, higher than normal).
	Medium

	// Semibold weight (600).
	Semibold

	// Bold weight (700).
	Bold

	// Extra-bold weight (800).
	ExtraBold

	// Black weight (900), the thickest value.
	Black
)
const WeightsN Weights = 9

WeightsN is the highest valid value for type Weights, plus one.

func RuneToWeight

func RuneToWeight(r rune) Weights

RuneToWeight returns the Weights value from given rune.

func WeightsValues

func WeightsValues() []Weights

WeightsValues returns all possible values for the type Weights.

func (Weights) Desc

func (i Weights) Desc() string

Desc returns the description of the Weights value.

func (Weights) HTMLTag

func (w Weights) HTMLTag() string

func (Weights) Int64

func (i Weights) Int64() int64

Int64 returns the Weights value as an int64.

func (Weights) MarshalText

func (i Weights) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Weights) SetInt64

func (i *Weights) SetInt64(in int64)

SetInt64 sets the Weights value from an int64.

func (*Weights) SetString

func (i *Weights) SetString(s string) error

SetString sets the Weights value from its string representation, and returns an error if the string is invalid.

func (Weights) String

func (i Weights) String() string

String returns the string representation of this Weights value.

func (Weights) ToFloat32

func (w Weights) ToFloat32() float32

ToFloat32 converts the weight to its numerical 100x value

func (*Weights) UnmarshalText

func (i *Weights) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Weights) Values

func (i Weights) Values() []enums.Enum

Values returns all possible values for the type Weights.

Jump to

Keyboard shortcuts

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