hyphenation

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: BSD-3-Clause Imports: 1 Imported by: 0

README

hyphenation — go-typeset

License Go Coverage

Liang's hyphenation algorithm, in pure Go (no cgo, no dependencies.)

It answers one question: given a word, where may a hyphen fall? That is the method TeX uses, and it reads the same pattern files — the ones distributed for every language TeX supports. Extracted from go-tex/engine, where it feeds the paragraph builder.

How it works

A pattern carries priority digits between letters: hy3ph says a break after hy scores 3. Every pattern matching any substring of the word (with . marking a word boundary) contributes, the highest value at each position wins, and an ODD value means a break is allowed there. \lefthyphenmin and \righthyphenmin then forbid breaks too close to either end — 2 and 3 by default, which is why a five-letter word is never hyphenated.

Use

import "github.com/go-typeset/hyphenation"

h := hyphenation.New()
for _, p := range patterns {   // e.g. the lines of hyph-en-gb.pat.txt
    h.AddPattern(p)
}

h.Points("hyphenation")   // → [2 6]: hy-phen-ation

Each value t means a hyphen may follow the first t letters.

Tests

go test ./... — 100% statement coverage, run on six 64-bit architectures (amd64, arm64, riscv64, loong64, ppc64le, s390x), three operating systems, and both wasm targets.

Licence

BSD-3-Clause.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hyphenator

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

Hyphenator holds the loaded patterns and the min-affix limits.

func New

func New() *Hyphenator

func (*Hyphenator) AddPattern

func (h *Hyphenator) AddPattern(p string)

addPattern parses one Liang pattern (e.g. "a1bc3d" or ".ach4") into its letter key and inter-letter value array (length = letters+1).

func (*Hyphenator) Mins

func (h *Hyphenator) Mins() (left, right int)

Mins reports the current \lefthyphenmin and \righthyphenmin.

func (*Hyphenator) Points

func (h *Hyphenator) Points(word string) []int

points returns the break positions in word: each value t means a hyphen is allowed after the first t letters (so between word[t-1] and word[t]).

func (*Hyphenator) SetMins

func (h *Hyphenator) SetMins(left, right int)

SetMins sets \lefthyphenmin and \righthyphenmin: the number of letters that must remain before the first hyphen and after the last. TeX's defaults are 2 and 3, which is why it never hyphenates a five-letter word; a document (or a language's conventions) may want otherwise.

Jump to

Keyboard shortcuts

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