![]() |
Hyphenation | ![]() |
TeXmacs hyphenates words with Liang's algorithm, the one used by TeX, and with the same pattern files. The implementation is in
System/Language/hyphenate.cpp
; the text languages of
text_language.cpp
load the patterns in their constructors and call the algorithm from get_hyphens and hyphenate.
The patterns live in
src/TeXmacs/langs/natural/hyphen/
as files hyphen.name, where name is the second argument given to the language constructor in text_language (for instance us for English and ukenglish for British English). They are ordinary TeX pattern files in UTF-8, with a \patterns{…} block and an optional \hyphenation{…} block of exceptions.
load_hyphen_tables (name, patterns, hyphenations, toCork) reads such a file into two hash tables:
For each pattern such as 1ba or .ach4, the key is the pattern without its digits (ba, .ach) and the value is the pattern itself. The TeX escapes ^^xx are decoded first.
For each exception such as uni-ver-sity, the key is the word without hyphens and the value is the word with hyphens.
The file is cut into tokens at white space; % starts a comment up to the end of the line; a block starts at the token \patterns{ or \hyphenation{ and ends at a token consisting of a single }. If toCork is set, which is the case for all text languages except those of ucs_text_language_rep (Bulgarian, Russian, Ukrainian), the file is converted from UTF-8 to the Cork encoding before parsing, so that patterns can be compared directly with TeXmacs strings. For the other languages the patterns stay in UTF-8 and words are converted to UTF-8 before hyphenation.
get_hyphens (s, patterns, hyphenations, utf8) returns an array of penalties, one for each position between two consecutive characters of the word s: the entry with index \(k\) is the penalty for breaking after the first \(k + 1\) characters. It proceeds as follows:
The word is converted to lower case (and to UTF-8 in the utf8 case).
If the word is an exception, the penalties are read from the hyphenated form: HYPH_STD where the exception has a hyphen, HYPH_INVALID elsewhere.
Otherwise, the word is surrounded by dots, and for every substring of length 1 to 9 the pattern table is consulted. The digits of each matching pattern are stored in an array of the inter-letter positions, keeping the maximum. A position with an odd value is a hyphenation point (penalty HYPH_STD), any other position is forbidden (HYPH_INVALID).
Finally, breaks after the first one or two characters and before the last one, two or three characters are forbidden. As implemented, a hyphenated word therefore keeps at least three characters before the hyphen and four after it. This restriction does not apply to exceptions.
std_hyphenate (s, after, left, right, penalty, utf8) performs the actual split: left receives the first after+1 characters followed by a hyphen -, and right receives the rest. In the utf8 case positions are counted in characters, where a <…> entity counts as one character. (If the given penalty is HYPH_INVALID, a backslash is appended instead of the hyphen; the line breaker never asks for such a split.)
Their get_hyphens forbids all breaks; lines are broken between the units returned by advance instead.
Forbids all breaks inside words; used by hgroup.
Uses an explicit hyphenation for one word, given by the primitive hyphenate-as. Its break penalty is 0 rather than HYPH_STD, so these breaks are preferred to ordinary hyphenation points.
The concatenation typesetter stores the language in every string line item. When the line breaker (
Typeset/Line/line_breaker.cpp
) looks for break points inside a word that does not fit, it calls item->lan->get_hyphens (s) and tries the positions with a penalty below HYPH_INVALID, splitting the item with item->lan->hyphenate. The penalty enters the cost of the line break. The details, including the influence of the par-hyphen variable, are in line breaking.
The pattern loader only recognizes the end of a block when the closing brace is a token on its own. In hyphen.ukenglish the exception list ends with some-thing}, so the exception is stored under the key something} and never matches; the comment in
hyphenate.cpp
(“bug: shows the hyphenation something} –> some-thing}”) refers to this. Exceptions and patterns which come after such a token would also be misclassified.
In the Cork case, letters which have no Cork code (for instance Greek letters) become entities <…>, both in the patterns and in the words. The algorithm does not handle this well: substrings are taken with a length in bytes (at most 9), while the array of scores is indexed by characters in one place and by bytes in another (hyphenate.cpp:224-234). For words containing entities, hyphenation points are therefore missing or misplaced. The UTF-8 branch used by ucs_text_language_rep does not have this problem.
The index returned by get_hyphens counts characters in the utf8 case and bytes otherwise; callers must not mix the two.