text

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DirectionInvalid Direction = 0
	LeftToRight                = Direction(harfbuzz.LeftToRight)
	RightToLeft                = Direction(harfbuzz.RightToLeft)
	TopToBottom                = Direction(harfbuzz.TopToBottom)
	BottomToTop                = Direction(harfbuzz.BottomToTop)
)

see Direction

View Source
const (
	ScriptInvalid   Script = 0
	ScriptCommon           = Script(language.Common)
	ScriptInherited        = Script(language.Inherited)
	ScriptUnknown          = Script(language.Unknown)

	Arabic     = Script(language.Arabic)
	Armenian   = Script(language.Armenian)
	Bengali    = Script(language.Bengali)
	Cyrillic   = Script(language.Cyrillic)
	Devanagari = Script(language.Devanagari)
	Georgian   = Script(language.Georgian)
	Greek      = Script(language.Greek)
	Gujarati   = Script(language.Gujarati)
	Gurmukhi   = Script(language.Gurmukhi)
	Hangul     = Script(language.Hangul)
	Han        = Script(language.Han)
	Hebrew     = Script(language.Hebrew)
	Hiragana   = Script(language.Hiragana)
	Kannada    = Script(language.Kannada)
	Katakana   = Script(language.Katakana)
	Lao        = Script(language.Lao)
	Latin      = Script(language.Latin)
	Malayalam  = Script(language.Malayalam)
	Oriya      = Script(language.Oriya)
	Tamil      = Script(language.Tamil)
	Telugu     = Script(language.Telugu)
	Thai       = Script(language.Thai)

	Tibetan = Script(language.Tibetan)

	Bopomofo          = Script(language.Bopomofo)
	Braille           = Script(language.Braille)
	CanadianSyllabics = Script(language.Canadian_Aboriginal)
	Cherokee          = Script(language.Cherokee)
	Ethiopic          = Script(language.Ethiopic)
	Khmer             = Script(language.Khmer)
	Mongolian         = Script(language.Mongolian)
	Myanmar           = Script(language.Myanmar)
	Ogham             = Script(language.Ogham)
	Runic             = Script(language.Runic)
	Sinhala           = Script(language.Sinhala)
	Syriac            = Script(language.Syriac)
	Thaana            = Script(language.Thaana)
	Yi                = Script(language.Yi)

	Deseret   = Script(language.Deseret)
	Gothic    = Script(language.Gothic)
	OldItalic = Script(language.Old_Italic)

	Buhid    = Script(language.Buhid)
	Hanunoo  = Script(language.Hanunoo)
	Tagalog  = Script(language.Tagalog)
	Tagbanwa = Script(language.Tagbanwa)

	Cypriot  = Script(language.Cypriot)
	Limbu    = Script(language.Limbu)
	LinearB  = Script(language.Linear_B)
	Osmanya  = Script(language.Osmanya)
	Shavian  = Script(language.Shavian)
	TaiLe    = Script(language.Tai_Le)
	Ugaritic = Script(language.Ugaritic)

	Buginese    = Script(language.Buginese)
	Coptic      = Script(language.Coptic)
	Glagolitic  = Script(language.Glagolitic)
	Kharoshthi  = Script(language.Kharoshthi)
	NewTaiLue   = Script(language.New_Tai_Lue)
	OldPersian  = Script(language.Old_Persian)
	SylotiNagri = Script(language.Syloti_Nagri)
	Tifinagh    = Script(language.Tifinagh)

	Balinese   = Script(language.Balinese)
	Cuneiform  = Script(language.Cuneiform)
	Nko        = Script(language.Nko)
	PhagsPa    = Script(language.Phags_Pa)
	Phoenician = Script(language.Phoenician)

	Carian     = Script(language.Carian)
	Cham       = Script(language.Cham)
	KayahLi    = Script(language.Kayah_Li)
	Lepcha     = Script(language.Lepcha)
	Lycian     = Script(language.Lycian)
	Lydian     = Script(language.Lydian)
	OlChiki    = Script(language.Ol_Chiki)
	Rejang     = Script(language.Rejang)
	Saurashtra = Script(language.Saurashtra)
	Sundanese  = Script(language.Sundanese)
	Vai        = Script(language.Vai)

	Avestan               = Script(language.Avestan)
	Bamum                 = Script(language.Bamum)
	EgyptianHieroglyphs   = Script(language.Egyptian_Hieroglyphs)
	ImperialAramaic       = Script(language.Imperial_Aramaic)
	InscriptionalPahlavi  = Script(language.Inscriptional_Pahlavi)
	InscriptionalParthian = Script(language.Inscriptional_Parthian)
	Javanese              = Script(language.Javanese)
	Kaithi                = Script(language.Kaithi)
	Lisu                  = Script(language.Lisu)
	MeeteiMayek           = Script(language.Meetei_Mayek)
	OldSouthArabian       = Script(language.Old_South_Arabian)
	OldTurkic             = Script(language.Old_Turkic)
	Samaritan             = Script(language.Samaritan)
	TaiTham               = Script(language.Tai_Tham)
	TaiViet               = Script(language.Tai_Viet)

	Batak   = Script(language.Batak)
	Brahmi  = Script(language.Brahmi)
	Mandaic = Script(language.Mandaic)

	Chakma              = Script(language.Chakma)
	MeroiticCursive     = Script(language.Meroitic_Cursive)
	MeroiticHieroglyphs = Script(language.Meroitic_Hieroglyphs)
	Miao                = Script(language.Miao)
	Sharada             = Script(language.Sharada)
	SoraSompeng         = Script(language.Sora_Sompeng)
	Takri               = Script(language.Takri)

	BassaVah          = Script(language.Bassa_Vah)
	CaucasianAlbanian = Script(language.Caucasian_Albanian)
	Duployan          = Script(language.Duployan)
	Elbasan           = Script(language.Elbasan)
	Grantha           = Script(language.Grantha)
	Khojki            = Script(language.Khojki)
	Khudawadi         = Script(language.Khudawadi)
	LinearA           = Script(language.Linear_A)
	Mahajani          = Script(language.Mahajani)
	Manichaean        = Script(language.Manichaean)
	MendeKikakui      = Script(language.Mende_Kikakui)
	Modi              = Script(language.Modi)
	Mro               = Script(language.Mro)
	Nabataean         = Script(language.Nabataean)
	OldNorthArabian   = Script(language.Old_North_Arabian)
	OldPermic         = Script(language.Old_Permic)
	PahawhHmong       = Script(language.Pahawh_Hmong)
	Palmyrene         = Script(language.Palmyrene)
	PauCinHau         = Script(language.Pau_Cin_Hau)
	PsalterPahlavi    = Script(language.Psalter_Pahlavi)
	Siddham           = Script(language.Siddham)
	Tirhuta           = Script(language.Tirhuta)
	WarangCiti        = Script(language.Warang_Citi)

	Adlam     = Script(language.Adlam)
	Bhaiksuki = Script(language.Bhaiksuki)
	Marchen   = Script(language.Marchen)
	Osage     = Script(language.Osage)
	Tangut    = Script(language.Tangut)
	Newa      = Script(language.Newa)

	MasaramGondi    = Script(language.Masaram_Gondi)
	Nushu           = Script(language.Nushu)
	Soyombo         = Script(language.Soyombo)
	ZanabazarSquare = Script(language.Zanabazar_Square)

	Dogra          = Script(language.Dogra)
	GunjalaGondi   = Script(language.Gunjala_Gondi)
	HanifiRohingya = Script(language.Hanifi_Rohingya)
	Makasar        = Script(language.Makasar)
	Medefaidrin    = Script(language.Medefaidrin)
	OldSogdian     = Script(language.Old_Sogdian)
	Sogdian        = Script(language.Sogdian)

	Elymaic              = Script(language.Elymaic)
	Nandinagari          = Script(language.Nandinagari)
	NyiakengPuachueHmong = Script(language.Nyiakeng_Puachue_Hmong)
	Wancho               = Script(language.Wancho)

	Chorasmian        = Script(language.Chorasmian)
	DivesAkuru        = Script(language.Dives_Akuru)
	KhitanSmallScript = Script(language.Khitan_Small_Script)
	Yezidi            = Script(language.Yezidi)
)

see Script

View Source
const FairyTales = "" /* 678-byte string literal not displayed */

FairyTales is an example text.

Variables

View Source
var (
	SentenceFactor  = 3.0
	ColonFactor     = 2.0
	SemicolonFactor = 1.5
	CommaFactor     = 1.25
)

Stretchability and shrinkability factors for inter-sentence and other types of spaces, not used if FrenchSpacing is set.

View Source
var DemeritsFitness = 100.0

DemeritsFitness is the badness rating for very different fitness ratings for consecutive lines. Fitness is a categorization of four types for ratio ranges.

View Source
var DemeritsFlagged = 100.0

DemeritsFlagged is the badness rating for two consecutive lines ending in hyphens.

View Source
var DemeritsLine = 10.0

DemeritsLine is the badness rating for an extra line.

View Source
var Epsilon = 1e-6
View Source
var FrenchSpacing = false

FrenchSpacing enforces equal widths for inter-word and inter-sentence spaces.

View Source
var HyphenPenalty = 50.0

HyphenPenalty is the aesthetic cost of ending a line in a hyphen.

View Source
var Infinity = 1000.0 // in case of ratio, demerits become about 1e22

Infinity specifies infinity as something finite to prevent numerical errors.

View Source
var SpaceRaggedStretch = 3.0

SpaceRaggedStretch is the stretchiness of glue (spaces) at the start or end of a line for ragged alignments (left, centre, right).

View Source
var SpaceShrink = 1.0 / 3.0 // ratio of the space that can be removed

SpaceShrink is the shrinkability of spaces.

View Source
var SpaceStretch = 1.0 / 2.0 // ratio of the space that can be added

SpaceStretch is the stretchability of spaces.

Functions

func EmbeddingLevels

func EmbeddingLevels(str []rune) []int

EmbeddingLevels returns the embedding levels for each rune of a mixed LTR/RTL string. A change in level means a change in direction.

func IsNewline

func IsNewline(r rune) bool

func IsParagraph added in v1.0.10

func IsParagraph(r rune) bool

func IsParagraphSeparator

func IsParagraphSeparator(r rune) bool

IsParagraphSeparator returns true for paragraph separator runes.

func IsSpace

func IsSpace(r rune) bool

func IsSpacelessScript

func IsSpacelessScript(script Script) bool

func IsVerticalScript

func IsVerticalScript(script Script) bool

func LinebreakGlyphs

func LinebreakGlyphs(sfnt *font.SFNT, size float64, glyphs []Glyph, width float64, opts Options, linebreaker Linebreaker) [][]Glyph

LinebreakGlyphs breaks a slice of glyphs uing the given SFNT font and font size. The indent and width specify the first line's indentation and the maximum line's width respectively. Align sets the horizontal alignment of the text. The looseness specifies whether it is desirable to have less or more lines than optimal.

func SpaceGlue added in v1.0.10

func SpaceGlue(glyphs []Glyph, i int) (float64, float64, float64)

Types

type Align

type Align int

Align is te text alignment.

const (
	Left Align = iota
	Right
	Centered
	Justified
)

see Align

func (Align) String added in v1.0.10

func (a Align) String() string

type Break added in v1.0.10

type Break struct {
	Position               int
	Width, Stretch, Shrink float64
	Ratio                  float64
}

func GreedyLinebreak added in v1.0.10

func GreedyLinebreak(items Items, width float64) []Break

GreedyLinebreak breaks a list of items using a greedy line breaking algorithm. This is much faster than Knuth's algorithm. TODO: instead of width accept widthFunction(int) float64

func KnuthLinebreak added in v1.0.10

func KnuthLinebreak(items Items, width, tolerance float64, looseness int) []Break

KnuthLinebreak breaks a list of items using Donald Knuth's line breaking algorithm. See Donald E. Knuth and Michael F. Plass, "Breaking Paragraphs into Lines", 1981 TODO: instead of width accept widthFunction(int) float64

type Breakpoint

type Breakpoint struct {
	Break
	Line     int
	Fitness  int
	W, Y, Z  float64
	Demerits float64
	// contains filtered or unexported fields
}

Breakpoint is a (possible) break point in the string.

func (*Breakpoint) String

func (br *Breakpoint) String() string

type Breakpoints

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

Breakpoints is a list of break points.

func (*Breakpoints) Has

func (list *Breakpoints) Has(b *Breakpoint) bool

Has return true if it contains break point b.

func (*Breakpoints) InsertBefore

func (list *Breakpoints) InsertBefore(b *Breakpoint, at *Breakpoint)

InsertBefore inserts break point b before at.

func (*Breakpoints) Push

func (list *Breakpoints) Push(b *Breakpoint)

Push adds break point b to the end of the list.

func (*Breakpoints) Remove

func (list *Breakpoints) Remove(b *Breakpoint)

Remove removes break point b.

func (*Breakpoints) String

func (list *Breakpoints) String() string

type Direction

type Direction int

Direction is the text direction.

type Glyph

type Glyph struct {
	SFNT *font.SFNT
	Size float64
	Script
	Vertical bool // is false for Latin/Mongolian/etc in a vertical layout

	ID       uint16
	Cluster  uint32
	XAdvance int32
	YAdvance int32
	XOffset  int32
	YOffset  int32
	Text     rune
}

Glyph is a shaped glyph for the given font and font size. It specified the glyph ID, the cluster ID, its X and Y advance and offset in font units, and its representation as text.

func (Glyph) Advance

func (g Glyph) Advance() float64

func (Glyph) Rotation

func (g Glyph) Rotation() Rotation

func (Glyph) String

func (g Glyph) String() string

type Item

type Item struct {
	Type
	Width, Stretch, Shrink float64 // Width is the natural width, Stretch the width that can be added, and Shrink that can be removed
	Penalty                float64
	Flagged                bool
	Size                   int // number of boxes (glyphs) compressed into one
}

Item is a box, glue or penalty item.

func Box

func Box(width float64) Item

Box returns a box item (a word) of the given fixed width.

func Glue

func Glue(width, stretch, shrink float64) Item

Glue returns a glue item (a space) where width is the default width, stretch*Tolerance the maximum width, and shrink*Tolerance the minimum width.

func Penalty

func Penalty(width, penalty float64, flagged bool) Item

Penalty returns a penalty item (explicit or possible newline, hyphen) with a given penalization factor. For hyphen insertion, width is the hyphen width and flagged should be set to discourage multiple hyphened lines next to each other. For explicit newlines the penalty is -Infinity.

func (Item) String

func (item Item) String() string

type Items added in v1.0.10

type Items []Item

func GlyphsToItems

func GlyphsToItems(glyphs []Glyph, opts Options) Items

GlyphsToItems converts a slice of glyphs into the box/glue/penalty items model as used by Knuth's line breaking algorithm. The SFNT and Size of each glyph must be set. Indent and align specify the indentation width of the first line and the alignment (left, right, centered, justified) of the lines respectively.

type Linebreaker added in v1.0.10

type Linebreaker interface {
	Linebreak([]Item, float64) []Break
}

Linebreaker is an interface for line breaking algorithms. Given a set of items and a desired text width, it will break lines to remain within the given width. It returns the breakpoints and whether it succesfully broke all lines to fit within the width; it returns falso if it overflows.

type Options added in v1.0.10

type Options struct {
	Align                Align
	Indent               float64
	PunctuationInMargins bool
}

type Rotation

type Rotation float64
const (
	NoRotation Rotation = 0.0
	CW         Rotation = -90.0
	CCW        Rotation = 90.0
)

func ScriptRotation

func ScriptRotation(script Script) Rotation

type Script

type Script uint32

Script is the script.

func LookupScript

func LookupScript(r rune) Script

func (Script) String

func (i Script) String() string

type ScriptItem

type ScriptItem struct {
	Script
	Level int
	Text  string
}

func ScriptItemizer

func ScriptItemizer(runes []rune, embeddingLevels []int) []ScriptItem

ScriptItemizer divides the string in parts for each different script. Also separates on different embedding levels and unicode.ReplacementChar (replaced by object).

func (*ScriptItem) String added in v1.0.10

func (item *ScriptItem) String() string

type Shaper

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

Shaper is a text shaper formatting a string in properly positioned glyphs.

func NewShaper

func NewShaper(b []byte, _ int) (Shaper, error)

NewShaper returns a new text shaper.

func NewShaperSFNT

func NewShaperSFNT(sfnt *font.SFNT) (Shaper, error)

NewShaperSFNT returns a new text shaper using a SFNT structure.

func (Shaper) Destroy

func (s Shaper) Destroy()

Destroy destroys the allocated C memory.

func (Shaper) Shape

func (s Shaper) Shape(text string, ppem uint16, direction Direction, script Script, lang string, features string, variations string) []Glyph

Shape shapes the string for a given direction, script, and language.

type Type

type Type int

Type is the item type.

const (
	BoxType Type = iota
	GlueType
	PenaltyType
)

see Type

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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