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 ¶
- func AppendHTML(dst []byte, text string, runs *Runs) []byte
- type Run
- type Runs
- func (r *Runs) All() iter.Seq[Run]
- func (r *Runs) ApplyAt(src *Runs, offset int)
- func (r *Runs) ApplyStyle(start, end int, style Style)
- func (r *Runs) Clear()
- func (r *Runs) CopyFrom(src *Runs)
- func (r *Runs) CopyMergedFrom(src *Runs, base Style, start, end int)
- func (r *Runs) CopyRangeFrom(src *Runs, start, end int)
- func (r *Runs) Equal(other *Runs) bool
- func (r *Runs) IsEmpty() bool
- func (r *Runs) Replace(start, end, newLen int)
- func (r *Runs) ReplaceRange(src *Runs, start, end int)
- func (r *Runs) Reset(start, end int)
- func (r *Runs) SetBackgroundColor(start, end int, clr color.Color)
- func (r *Runs) SetColor(start, end int, clr color.Color)
- func (r *Runs) SetFamily(start, end int, family *font.Family)
- func (r *Runs) SetFeature(start, end int, tag text.Tag, value uint32)
- func (r *Runs) SetItalic(start, end int, italic bool)
- func (r *Runs) SetLang(start, end int, lang language.Tag)
- func (r *Runs) SetScale(start, end int, scale float64)
- func (r *Runs) SetStrikethrough(start, end int, strikethrough bool)
- func (r *Runs) SetUnderline(start, end int, underline bool)
- func (r *Runs) SetVariation(start, end int, tag text.Tag, value float32)
- func (r *Runs) StyleAt(index int) Style
- func (r *Runs) UniformColor(start, end int, fallback color.Color) (clr color.Color, uniform bool)
- func (r *Runs) UniformItalic(start, end int, fallback bool) (italic, uniform bool)
- func (r *Runs) UniformScale(start, end int, fallback float64) (scale float64, uniform bool)
- func (r *Runs) UniformStrikethrough(start, end int, fallback bool) (strikethrough, uniform bool)
- func (r *Runs) UniformUnderline(start, end int, fallback bool) (underline, uniform bool)
- func (r *Runs) UniformVariation(start, end int, tag text.Tag, fallback float32) (value float32, uniform bool)
- func (r *Runs) UnsetBackgroundColor(start, end int)
- func (r *Runs) UnsetColor(start, end int)
- func (r *Runs) UnsetFamily(start, end int)
- func (r *Runs) UnsetFeature(start, end int, tag text.Tag)
- func (r *Runs) UnsetItalic(start, end int)
- func (r *Runs) UnsetLang(start, end int)
- func (r *Runs) UnsetScale(start, end int)
- func (r *Runs) UnsetStrikethrough(start, end int)
- func (r *Runs) UnsetUnderline(start, end int)
- func (r *Runs) UnsetVariation(start, end int, tag text.Tag)
- func (r *Runs) WriteMetricStateKey(w Writer)
- func (r *Runs) WriteStateKey(w Writer)
- type Style
- func (s Style) AffectsFaceSelection() bool
- func (s Style) BackgroundColor() (color.Color, bool)
- func (s Style) Color() (color.Color, bool)
- func (s Style) Equal(other Style) bool
- func (s Style) Family() (*font.Family, bool)
- func (s Style) Feature(tag text.Tag) (uint32, bool)
- func (s Style) Features() []font.Feature
- func (s Style) IsZero() bool
- func (s Style) Italic() (bool, bool)
- func (s Style) Lang() (language.Tag, bool)
- func (s Style) Merge(other Style) Style
- func (s Style) Scale() (float64, bool)
- func (s Style) Strikethrough() (bool, bool)
- func (s Style) Underline() (bool, bool)
- func (s Style) Variation(tag text.Tag) (float32, bool)
- func (s Style) Variations() []font.Variation
- func (s Style) WithBackgroundColor(clr color.Color) Style
- func (s Style) WithColor(clr color.Color) Style
- func (s Style) WithFamily(family *font.Family) Style
- func (s Style) WithFeature(tag text.Tag, value uint32) Style
- func (s Style) WithItalic(italic bool) Style
- func (s Style) WithLang(lang language.Tag) Style
- func (s Style) WithScale(scale float64) Style
- func (s Style) WithStrikethrough(strikethrough bool) Style
- func (s Style) WithUnderline(underline bool) Style
- func (s Style) WithVariation(tag text.Tag, value float32) Style
- func (s Style) WithoutBackgroundColor() Style
- func (s Style) WithoutColor() Style
- func (s Style) WithoutFamily() Style
- func (s Style) WithoutFeature(tag text.Tag) Style
- func (s Style) WithoutItalic() Style
- func (s Style) WithoutLang() Style
- func (s Style) WithoutScale() Style
- func (s Style) WithoutStrikethrough() Style
- func (s Style) WithoutUnderline() Style
- func (s Style) WithoutVariation(tag text.Tag) Style
- func (s Style) WriteMetricStateKey(w Writer)
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendHTML ¶
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 ¶
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 ¶
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 ¶
ApplyStyle applies style's set properties over [start, end) on top of the existing overrides.
func (*Runs) CopyMergedFrom ¶
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 ¶
CopyRangeFrom replaces the run list with a copy of src's overrides in [start, end), rebased so that start maps to 0.
func (*Runs) Replace ¶
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 ¶
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) SetBackgroundColor ¶
SetBackgroundColor overrides the background color in [start, end).
func (*Runs) SetFeature ¶
SetFeature overrides the OpenType feature tag in [start, end) with value.
func (*Runs) SetScale ¶
SetScale overrides the multiplier of the base font size in [start, end). scale must be positive.
func (*Runs) SetStrikethrough ¶
SetStrikethrough overrides whether a strikethrough is drawn in [start, end).
func (*Runs) SetUnderline ¶
SetUnderline overrides whether an underline is drawn in [start, end).
func (*Runs) SetVariation ¶
SetVariation overrides the OpenType variation axis tag in [start, end) with value.
func (*Runs) StyleAt ¶
StyleAt returns the style overriding the byte at index, or a zero Style if none.
func (*Runs) UniformColor ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
UnsetBackgroundColor removes the background color override in [start, end).
func (*Runs) UnsetColor ¶
UnsetColor removes the text color override in [start, end).
func (*Runs) UnsetFamily ¶
UnsetFamily removes the font family override in [start, end).
func (*Runs) UnsetFeature ¶
UnsetFeature removes the override of the OpenType feature tag in [start, end).
func (*Runs) UnsetItalic ¶
UnsetItalic removes the italic override in [start, end).
func (*Runs) UnsetScale ¶
UnsetScale removes the font size multiplier override in [start, end).
func (*Runs) UnsetStrikethrough ¶
UnsetStrikethrough removes the strikethrough override in [start, end).
func (*Runs) UnsetUnderline ¶
UnsetUnderline removes the underline override in [start, end).
func (*Runs) UnsetVariation ¶
UnsetVariation removes the override of the OpenType variation axis tag in [start, end).
func (*Runs) WriteMetricStateKey ¶
WriteMetricStateKey writes the runs' metric-affecting properties into w. Runs without any contribute nothing.
func (*Runs) WriteStateKey ¶
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 ¶
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 ¶
BackgroundColor returns the background color override and whether it is set.
func (Style) Feature ¶
Feature returns the override value of the OpenType feature tag and whether it is set.
func (Style) Merge ¶
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) Strikethrough ¶
Strikethrough returns the strikethrough override and whether it is set.
func (Style) Variation ¶
Variation returns the override value of the OpenType variation axis tag and whether it is set.
func (Style) Variations ¶
Variations returns the OpenType variation axis overrides, sorted by tag.
func (Style) WithBackgroundColor ¶
WithBackgroundColor returns s with the background color set to clr.
func (Style) WithFamily ¶
WithFamily returns s with the font family set to family.
func (Style) WithFeature ¶
WithFeature returns s with the OpenType feature tag set to value.
func (Style) WithItalic ¶
WithItalic returns s with the italic face selection set to italic.
func (Style) WithStrikethrough ¶
WithStrikethrough returns s with the strikethrough state set to strikethrough.
func (Style) WithUnderline ¶
WithUnderline returns s with the underline state set to underline.
func (Style) WithVariation ¶
WithVariation returns s with the OpenType variation axis tag set to value.
func (Style) WithoutBackgroundColor ¶
WithoutBackgroundColor returns s with the background color unset.
func (Style) WithoutColor ¶
WithoutColor returns s with the text color unset.
func (Style) WithoutFamily ¶
WithoutFamily returns s with the font family unset.
func (Style) WithoutFeature ¶
WithoutFeature returns s with the OpenType feature tag removed.
func (Style) WithoutItalic ¶
WithoutItalic returns s with the italic face selection unset.
func (Style) WithoutLang ¶
WithoutLang returns s with the language unset.
func (Style) WithoutScale ¶
WithoutScale returns s with the base font size multiplier unset.
func (Style) WithoutStrikethrough ¶
WithoutStrikethrough returns s with the strikethrough state unset.
func (Style) WithoutUnderline ¶
WithoutUnderline returns s with the underline state unset.
func (Style) WithoutVariation ¶
WithoutVariation returns s with the OpenType variation axis tag removed.
func (Style) WriteMetricStateKey ¶
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.