textstyle

package
v0.2.0-alpha Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package textstyle provides the attributed-run model for ranged text styling: a set of style overrides laid over byte ranges of a single text value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendHTML

func AppendHTML(dst []byte, text string, runs *Runs) []byte

AppendHTML appends an HTML fragment representing text with runs' style overrides applied and returns the extended slice. Hard line breaks become br elements. Style properties without a clean HTML mapping (font family, language, OpenType features, and variations other than weight) are dropped.

Types

type Run

type Run struct {
	// Start is the inclusive start of the range in bytes.
	Start int

	// End is the exclusive end of the range in bytes.
	End int

	// Style is the merged style overriding the range.
	Style Style
}

Run is a style override applied to a byte range.

type Runs

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

Runs is a set of style overrides over byte ranges of a single text value. The zero value is an empty set ready for use.

Runs holds byte offsets only; it does not know the text or its length. Callers may use ranges extending past the text's length.

func (*Runs) All

func (r *Runs) All() iter.Seq[Run]

All returns an iterator over the runs in ascending range order. The yielded runs never overlap or nest each other: styles applied over intersecting ranges have been split and merged into disjoint runs.

func (*Runs) ApplyAt

func (r *Runs) ApplyAt(src *Runs, offset int)

ApplyAt applies src's overrides on top of the existing overrides, with src's byte ranges shifted by offset. src must not be r.

func (*Runs) ApplyStyle

func (r *Runs) ApplyStyle(start, end int, style Style)

ApplyStyle applies style's set properties over [start, end) on top of the existing overrides.

func (*Runs) Clear

func (r *Runs) Clear()

Clear removes all style overrides.

func (*Runs) CopyFrom

func (r *Runs) CopyFrom(src *Runs)

CopyFrom replaces the run list with a copy of src's.

func (*Runs) CopyMergedFrom

func (r *Runs) CopyMergedFrom(src *Runs, base Style, start, end int)

CopyMergedFrom replaces the run list with runs covering every byte of [0, end-start), each holding base merged with src's override of the corresponding byte of [start, end). Bytes whose merged style overrides nothing are left uncovered. An empty range clears the run list.

func (*Runs) CopyRangeFrom

func (r *Runs) CopyRangeFrom(src *Runs, start, end int)

CopyRangeFrom replaces the run list with a copy of src's overrides in [start, end), rebased so that start maps to 0.

func (*Runs) Equal

func (r *Runs) Equal(other *Runs) bool

Equal reports whether r and other hold the same overrides.

func (*Runs) IsEmpty

func (r *Runs) IsEmpty() bool

IsEmpty reports whether the run list holds no overrides.

func (*Runs) Replace

func (r *Runs) Replace(start, end, newLen int)

Replace adjusts the runs for a replacement of the byte range [start, end) with newLen bytes of text: styles keep covering the text around the replacement. Inserted text adopts the style right before the insertion point, extending the run it is inserted inside or at the end of; replacing text adopts the style of the first replaced byte.

func (*Runs) ReplaceRange

func (r *Runs) ReplaceRange(src *Runs, start, end int)

ReplaceRange replaces the overrides in [start, end) with src's overrides in [0, end-start), shifted so that 0 maps to start. src overrides outside [0, end-start) are ignored. src must not be r.

func (*Runs) Reset

func (r *Runs) Reset(start, end int)

Reset removes all style overrides in [start, end).

func (*Runs) SetBackgroundColor

func (r *Runs) SetBackgroundColor(start, end int, clr color.Color)

SetBackgroundColor overrides the background color in [start, end).

func (*Runs) SetColor

func (r *Runs) SetColor(start, end int, clr color.Color)

SetColor overrides the text color in [start, end).

func (*Runs) SetFamily

func (r *Runs) SetFamily(start, end int, family *font.Family)

SetFamily overrides the font family in [start, end).

func (*Runs) SetFeature

func (r *Runs) SetFeature(start, end int, tag text.Tag, value uint32)

SetFeature overrides the OpenType feature tag in [start, end) with value.

func (*Runs) SetItalic

func (r *Runs) SetItalic(start, end int, italic bool)

SetItalic overrides the italic face selection in [start, end).

func (*Runs) SetLang

func (r *Runs) SetLang(start, end int, lang language.Tag)

SetLang overrides the language used for rendering in [start, end).

func (*Runs) SetScale

func (r *Runs) SetScale(start, end int, scale float64)

SetScale overrides the multiplier of the base font size in [start, end). scale must be positive.

func (*Runs) SetStrikethrough

func (r *Runs) SetStrikethrough(start, end int, strikethrough bool)

SetStrikethrough overrides whether a strikethrough is drawn in [start, end).

func (*Runs) SetUnderline

func (r *Runs) SetUnderline(start, end int, underline bool)

SetUnderline overrides whether an underline is drawn in [start, end).

func (*Runs) SetVariation

func (r *Runs) SetVariation(start, end int, tag text.Tag, value float32)

SetVariation overrides the OpenType variation axis tag in [start, end) with value.

func (*Runs) StyleAt

func (r *Runs) StyleAt(index int) Style

StyleAt returns the style overriding the byte at index, or a zero Style if none.

func (*Runs) UniformColor

func (r *Runs) UniformColor(start, end int, fallback color.Color) (clr color.Color, uniform bool)

UniformColor returns the text color every byte of [start, end) takes and whether the color is uniform. A byte with a color override takes the override's value; every other byte takes fallback. An empty range is uniform with fallback.

func (*Runs) UniformItalic

func (r *Runs) UniformItalic(start, end int, fallback bool) (italic, uniform bool)

UniformItalic returns the italic state every byte of [start, end) takes and whether the state is uniform. A byte with an italic override takes the override's value; every other byte takes fallback. An empty range is uniform with fallback.

func (*Runs) UniformScale

func (r *Runs) UniformScale(start, end int, fallback float64) (scale float64, uniform bool)

UniformScale returns the font size multiplier every byte of [start, end) takes and whether the multiplier is uniform. A byte with a scale override takes the override's value; every other byte takes fallback. An empty range is uniform with fallback.

func (*Runs) UniformStrikethrough

func (r *Runs) UniformStrikethrough(start, end int, fallback bool) (strikethrough, uniform bool)

UniformStrikethrough returns the strikethrough state every byte of [start, end) takes and whether the state is uniform. A byte with a strikethrough override takes the override's value; every other byte takes fallback. An empty range is uniform with fallback.

func (*Runs) UniformUnderline

func (r *Runs) UniformUnderline(start, end int, fallback bool) (underline, uniform bool)

UniformUnderline returns the underline state every byte of [start, end) takes and whether the state is uniform. A byte with an underline override takes the override's value; every other byte takes fallback. An empty range is uniform with fallback.

func (*Runs) UniformVariation

func (r *Runs) UniformVariation(start, end int, tag text.Tag, fallback float32) (value float32, uniform bool)

UniformVariation returns the value of the OpenType variation axis tag every byte of [start, end) takes and whether the value is uniform. A byte with an override of the axis takes the override's value; every other byte takes fallback. An empty range is uniform with fallback.

func (*Runs) UnsetBackgroundColor

func (r *Runs) UnsetBackgroundColor(start, end int)

UnsetBackgroundColor removes the background color override in [start, end).

func (*Runs) UnsetColor

func (r *Runs) UnsetColor(start, end int)

UnsetColor removes the text color override in [start, end).

func (*Runs) UnsetFamily

func (r *Runs) UnsetFamily(start, end int)

UnsetFamily removes the font family override in [start, end).

func (*Runs) UnsetFeature

func (r *Runs) UnsetFeature(start, end int, tag text.Tag)

UnsetFeature removes the override of the OpenType feature tag in [start, end).

func (*Runs) UnsetItalic

func (r *Runs) UnsetItalic(start, end int)

UnsetItalic removes the italic override in [start, end).

func (*Runs) UnsetLang

func (r *Runs) UnsetLang(start, end int)

UnsetLang removes the language override in [start, end).

func (*Runs) UnsetScale

func (r *Runs) UnsetScale(start, end int)

UnsetScale removes the font size multiplier override in [start, end).

func (*Runs) UnsetStrikethrough

func (r *Runs) UnsetStrikethrough(start, end int)

UnsetStrikethrough removes the strikethrough override in [start, end).

func (*Runs) UnsetUnderline

func (r *Runs) UnsetUnderline(start, end int)

UnsetUnderline removes the underline override in [start, end).

func (*Runs) UnsetVariation

func (r *Runs) UnsetVariation(start, end int, tag text.Tag)

UnsetVariation removes the override of the OpenType variation axis tag in [start, end).

func (*Runs) WriteMetricStateKey

func (r *Runs) WriteMetricStateKey(w Writer)

WriteMetricStateKey writes the runs' metric-affecting properties into w. Runs without any contribute nothing.

func (*Runs) WriteStateKey

func (r *Runs) WriteStateKey(w Writer)

WriteStateKey writes the runs into w.

type Style

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

Style is a set of style overrides for a byte range of a text value. The zero value overrides nothing. Styles are produced by Runs; an unset property does not override anything.

func (Style) AffectsFaceSelection

func (s Style) AffectsFaceSelection() bool

AffectsFaceSelection reports whether the style overrides a property that affects font face selection, such as a variation setting or the font family.

func (Style) BackgroundColor

func (s Style) BackgroundColor() (color.Color, bool)

BackgroundColor returns the background color override and whether it is set.

func (Style) Color

func (s Style) Color() (color.Color, bool)

Color returns the text color override and whether it is set.

func (Style) Equal

func (s Style) Equal(other Style) bool

Equal reports whether two styles are identical.

func (Style) Family

func (s Style) Family() (*font.Family, bool)

Family returns the font family override and whether it is set.

func (Style) Feature

func (s Style) Feature(tag text.Tag) (uint32, bool)

Feature returns the override value of the OpenType feature tag and whether it is set.

func (Style) Features

func (s Style) Features() []font.Feature

Features returns the OpenType feature overrides, sorted by tag.

func (Style) IsZero

func (s Style) IsZero() bool

IsZero reports whether the style overrides nothing.

func (Style) Italic

func (s Style) Italic() (bool, bool)

Italic returns the italic face selection override and whether it is set.

func (Style) Lang

func (s Style) Lang() (language.Tag, bool)

Lang returns the language override and whether it is set.

func (Style) Merge

func (s Style) Merge(other Style) Style

Merge returns s with other's set properties applied on top. Features and variations are merged by tag, with other winning on a shared tag.

func (Style) Scale

func (s Style) Scale() (float64, bool)

Scale returns the base font size multiplier override and whether it is set.

func (Style) Strikethrough

func (s Style) Strikethrough() (bool, bool)

Strikethrough returns the strikethrough override and whether it is set.

func (Style) Underline

func (s Style) Underline() (bool, bool)

Underline returns the underline override and whether it is set.

func (Style) Variation

func (s Style) Variation(tag text.Tag) (float32, bool)

Variation returns the override value of the OpenType variation axis tag and whether it is set.

func (Style) Variations

func (s Style) Variations() []font.Variation

Variations returns the OpenType variation axis overrides, sorted by tag.

func (Style) WithBackgroundColor

func (s Style) WithBackgroundColor(clr color.Color) Style

WithBackgroundColor returns s with the background color set to clr.

func (Style) WithColor

func (s Style) WithColor(clr color.Color) Style

WithColor returns s with the text color set to clr.

func (Style) WithFamily

func (s Style) WithFamily(family *font.Family) Style

WithFamily returns s with the font family set to family.

func (Style) WithFeature

func (s Style) WithFeature(tag text.Tag, value uint32) Style

WithFeature returns s with the OpenType feature tag set to value.

func (Style) WithItalic

func (s Style) WithItalic(italic bool) Style

WithItalic returns s with the italic face selection set to italic.

func (Style) WithLang

func (s Style) WithLang(lang language.Tag) Style

WithLang returns s with the language set to lang.

func (Style) WithScale

func (s Style) WithScale(scale float64) Style

WithScale returns s with the base font size multiplier set to scale.

func (Style) WithStrikethrough

func (s Style) WithStrikethrough(strikethrough bool) Style

WithStrikethrough returns s with the strikethrough state set to strikethrough.

func (Style) WithUnderline

func (s Style) WithUnderline(underline bool) Style

WithUnderline returns s with the underline state set to underline.

func (Style) WithVariation

func (s Style) WithVariation(tag text.Tag, value float32) Style

WithVariation returns s with the OpenType variation axis tag set to value.

func (Style) WithoutBackgroundColor

func (s Style) WithoutBackgroundColor() Style

WithoutBackgroundColor returns s with the background color unset.

func (Style) WithoutColor

func (s Style) WithoutColor() Style

WithoutColor returns s with the text color unset.

func (Style) WithoutFamily

func (s Style) WithoutFamily() Style

WithoutFamily returns s with the font family unset.

func (Style) WithoutFeature

func (s Style) WithoutFeature(tag text.Tag) Style

WithoutFeature returns s with the OpenType feature tag removed.

func (Style) WithoutItalic

func (s Style) WithoutItalic() Style

WithoutItalic returns s with the italic face selection unset.

func (Style) WithoutLang

func (s Style) WithoutLang() Style

WithoutLang returns s with the language unset.

func (Style) WithoutScale

func (s Style) WithoutScale() Style

WithoutScale returns s with the base font size multiplier unset.

func (Style) WithoutStrikethrough

func (s Style) WithoutStrikethrough() Style

WithoutStrikethrough returns s with the strikethrough state unset.

func (Style) WithoutUnderline

func (s Style) WithoutUnderline() Style

WithoutUnderline returns s with the underline state unset.

func (Style) WithoutVariation

func (s Style) WithoutVariation(tag text.Tag) Style

WithoutVariation returns s with the OpenType variation axis tag removed.

func (Style) WriteMetricStateKey

func (s Style) WriteMetricStateKey(w Writer)

WriteMetricStateKey writes the style's metric-affecting properties into w.

type Writer

type Writer interface {
	WriteBool(v bool)
	WriteInt(v int)
	WriteUint16(v uint16)
	WriteUint32(v uint32)
	WriteUint64(v uint64)
	WriteFloat64(v float64)
	WriteString(v string)
}

Writer receives the serialized style properties of Runs.WriteStateKey, Runs.WriteMetricStateKey and Style.WriteMetricStateKey. guigui.StateKeyWriter satisfies it.

Jump to

Keyboard shortcuts

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