3 % Copyright
2006-2012 Taco Hoekwater
<taco@@luatex.org
>
5 % This file is part of LuaTeX.
7 % LuaTeX is free software
; you can redistribute it and
/or modify it under
8 % the terms of the GNU General Public License as published by the Free
9 % Software Foundation
; either version
2 of the License
, or
(at your
10 % option
) any later version.
12 % LuaTeX is distributed in the hope that it will be useful
, but WITHOUT
13 % ANY WARRANTY
; without even the implied warranty of MERCHANTABILITY or
14 % FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 % License for more details.
17 % You should have received a copy of the GNU General Public License along
18 % with LuaTeX
; if not
, see
<http
://www.gnu.org
/licenses
/>.
24 #include
"lua/luatex-api.h"
31 #define MAX_TEX_LANGUAGES
16384
33 #define ex_hyphen_char int_par
(ex_hyphen_char_code
)
35 static struct tex_language
*tex_languages
[MAX_TEX_LANGUAGES
] = { NULL };
37 static int next_lang_id
= 0;
39 struct tex_language
*new_language
(int n
)
41 struct tex_language
*lang
;
45 if
(l
!= (MAX_TEX_LANGUAGES
- 1))
46 if
(next_lang_id
<= n
)
49 while
(tex_languages
[next_lang_id
] != NULL)
51 l
= (unsigned
) next_lang_id
++;
53 if
(l
< (MAX_TEX_LANGUAGES
- 1) && tex_languages[l] == NULL) {
54 lang
= xmalloc
(sizeof
(struct tex_language
));
55 tex_languages
[l
] = lang
;
58 lang-
>patterns
= NULL;
59 lang-
>pre_hyphen_char
= '
-'
;
60 lang-
>post_hyphen_char
= 0;
61 lang-
>pre_exhyphen_char
= 0;
62 lang-
>post_exhyphen_char
= 0;
63 lang-
>hyphenation_min
= -1;
64 if
(int_par
(saving_hyph_codes_code
)) {
65 hj_codes_from_lc_codes
(l
); /* for now
, we might just use specific value for whatever task
*/
73 struct tex_language
*get_language
(int n
)
75 if
(n
>= 0 && n < MAX_TEX_LANGUAGES) {
76 if
(tex_languages
[n
] != NULL) {
77 return tex_languages
[n
];
79 return new_language
(n
);
87 void set_pre_hyphen_char
(int n
, int v
)
89 struct tex_language
*l
= get_language
((int
) n
);
91 l-
>pre_hyphen_char
= (int
) v
;
94 void set_post_hyphen_char
(int n
, int v
)
96 struct tex_language
*l
= get_language
((int
) n
);
98 l-
>post_hyphen_char
= (int
) v
;
101 void set_pre_exhyphen_char
(int n
, int v
)
103 struct tex_language
*l
= get_language
((int
) n
);
105 l-
>pre_exhyphen_char
= (int
) v
;
108 void set_post_exhyphen_char
(int n
, int v
)
110 struct tex_language
*l
= get_language
((int
) n
);
112 l-
>post_exhyphen_char
= (int
) v
;
115 int get_pre_hyphen_char
(int n
)
117 struct tex_language
*l
= get_language
((int
) n
);
120 return
(int
) l-
>pre_hyphen_char
;
123 int get_post_hyphen_char
(int n
)
125 struct tex_language
*l
= get_language
((int
) n
);
128 return
(int
) l-
>post_hyphen_char
;
131 int get_pre_exhyphen_char
(int n
)
133 struct tex_language
*l
= get_language
((int
) n
);
136 return
(int
) l-
>pre_exhyphen_char
;
139 int get_post_exhyphen_char
(int n
)
141 struct tex_language
*l
= get_language
((int
) n
);
144 return
(int
) l-
>post_exhyphen_char
;
147 void set_hyphenation_min
(int n
, int v
)
149 struct tex_language
*l
= get_language
((int
) n
);
151 l-
>hyphenation_min
= (int
) v
;
154 int get_hyphenation_min
(int n
)
156 struct tex_language
*l
= get_language
((int
) n
);
159 return
(int
) l-
>hyphenation_min
;
162 void load_patterns
(struct tex_language
*lang
, const unsigned char
*buff
)
164 if
(lang
== NULL || buff
== NULL || strlen
((const char
*) buff
) == 0)
166 if
(lang-
>patterns
== NULL) {
167 lang-
>patterns
= hnj_hyphen_new
();
169 hnj_hyphen_load
(lang-
>patterns
, buff
);
172 void clear_patterns
(struct tex_language
*lang
)
176 if
(lang-
>patterns
!= NULL) {
177 hnj_hyphen_clear
(lang-
>patterns
);
181 void load_tex_patterns
(int curlang
, halfword head
)
183 char
*s
= tokenlist_to_cstring
(head
, 1, NULL);
184 load_patterns
(get_language
(curlang
), (unsigned char
*) s
);
188 #define STORE_CHAR
(l
,x
) do
{ \
189 unsigned xx
= get_hj_code
(l
,x
); \
193 uindex
= uni2string
(uindex
, xx
); \
196 @ Cleans one word which is returned in |cleaned|
, returns the new offset into
200 const char
*clean_hyphenation
(int id
, const char
*buff
, char
**cleaned
)
203 unsigned char word
[MAX_WORD_LEN
+ 1]; /* work buffer for bytes
*/
204 unsigned uword
[MAX_WORD_LEN
+ 1] = { 0 }; /* work buffer for unicode
*/
205 int u
= 0; /* unicode buffer value
*/
206 int i
= 0; /* index into buffer
*/
207 char
*uindex
= (char
*)word
;
208 const char
*s
= buff
;
210 while
(*s
&& !isspace((unsigned char)*s)) {
211 word
[i
++] = (unsigned
)*s
;
213 if
((s-buff
)>MAX_WORD_LEN
) {
214 /* todo
: this is too strict
, should count unicode
, not bytes
*/
216 tex_error
("exception too long", NULL);
220 /* now convert the input to unicode
*/
222 utf2uni_strcpy
(uword
, (const char
*)word
);
224 /* build the new word string
*/
228 if
(u
== '
-'
) { /* skip
*/
229 } else if
(u
== '
='
) {
231 } else if
(u
== '
{'
) {
234 while
(u
&& u != '}') {
241 while
(u
&& u != '}') {
251 while
(u
&& u != '}') {
258 if
(items
!= 3) { /* syntax error
*/
260 tex_error
("exception syntax error", NULL);
268 *cleaned
= xstrdup
((char
*) word
);
273 void load_hyphenation
(struct tex_language
*lang
, const unsigned char
*buff
)
282 if
(lang-
>exceptions
== 0) {
284 lang-
>exceptions
= luaL_ref
(L
, LUA_REGISTRYINDEX
);
286 lua_rawgeti
(L
, LUA_REGISTRYINDEX
, lang-
>exceptions
);
287 s
= (const char
*) buff
;
290 while
(isspace
((unsigned char
)*s
))
294 s
= clean_hyphenation
(id
, s
, &cleaned);
295 if
(cleaned
!= NULL) {
296 if
((s
- value
) > 0) {
297 lua_pushstring
(L
, cleaned
);
298 lua_pushlstring
(L
, value
, (size_t
) (s
- value
));
304 formatted_warning
("hyphenation","skipping invalid hyphenation exception: %s", value
);
311 void clear_hyphenation
(struct tex_language
*lang
)
315 if
(lang-
>exceptions
!= 0) {
316 luaL_unref
(Luas
, LUA_REGISTRYINDEX
, lang-
>exceptions
);
317 lang-
>exceptions
= 0;
321 void load_tex_hyphenation
(int curlang
, halfword head
)
323 char
*s
= tokenlist_to_cstring
(head
, 1, NULL);
324 load_hyphenation
(get_language
(curlang
), (unsigned char
*) s
);
327 @ TODO
: clean this up. The |delete_attribute_ref
()| statements are not very nice
,
328 but needed. Also
, in the post-break
, it would be nicer to get the attribute list
329 from |vlink
(n
)|. No rush
, as it is currently not used much.
332 halfword insert_discretionary
(halfword t
, halfword pre
, halfword post
,
333 halfword replace
, int penalty
)
337 n
= new_node
(disc_node
, syllable_disc
);
338 disc_penalty
(n
) = penalty
;
339 try_couple_nodes
(n
, vlink
(t
));
344 f
= get_cur_font
(); /* for compound words following explicit hyphens
*/
345 for
(g
= pre
; g
!= null
; g
= vlink
(g
)) {
347 if
(node_attr
(t
) != null
) {
348 delete_attribute_ref
(node_attr
(g
));
349 node_attr
(g
) = node_attr
(t
);
350 attr_list_ref
(node_attr
(t
)) += 1;
353 for
(g
= post
; g
!= null
; g
= vlink
(g
)) {
355 if
(node_attr
(t
) != null
) {
356 delete_attribute_ref
(node_attr
(g
));
357 node_attr
(g
) = node_attr
(t
);
358 attr_list_ref
(node_attr
(t
)) += 1;
361 for
(g
= replace
; g
!= null
; g
= vlink
(g
)) {
362 if
(node_attr
(t
) != null
) {
363 delete_attribute_ref
(node_attr
(g
));
364 node_attr
(g
) = node_attr
(t
);
365 attr_list_ref
(node_attr
(t
)) += 1;
368 if
(node_attr
(t
) != null
) {
369 delete_attribute_ref
(node_attr
(vlink
(t
)));
370 node_attr
(vlink
(t
)) = node_attr
(t
);
371 attr_list_ref
(node_attr
(t
)) += 1;
374 set_disc_field
(pre_break
(t
), pre
);
375 set_disc_field
(post_break
(t
), post
);
376 set_disc_field
(no_break
(t
), replace
);
380 halfword insert_syllable_discretionary
(halfword t
, lang_variables
* lan
)
383 n
= new_node
(disc_node
, syllable_disc
);
384 disc_penalty
(n
) = int_par
(hyphen_penalty_code
);
385 couple_nodes
(n
, vlink
(t
));
387 delete_attribute_ref
(node_attr
(n
));
388 if
(node_attr
(t
) != null
) {
389 node_attr
(n
) = node_attr
(t
);
390 attr_list_ref
(node_attr
(t
))++;
394 if
(lan-
>pre_hyphen_char
> 0) {
395 g
= raw_glyph_node
();
397 character
(g
) = lan-
>pre_hyphen_char
;
399 lang_data
(g
) = lang_data
(t
);
400 if
(node_attr
(t
) != null
) {
401 node_attr
(g
) = node_attr
(t
);
402 attr_list_ref
(node_attr
(t
))++;
404 set_disc_field
(pre_break
(n
), g
);
407 if
(lan-
>post_hyphen_char
> 0) {
409 g
= raw_glyph_node
();
411 character
(g
) = lan-
>post_hyphen_char
;
413 lang_data
(g
) = lang_data
(t
);
414 if
(node_attr
(t
) != null
) {
415 node_attr
(g
) = node_attr
(t
);
416 attr_list_ref
(node_attr
(t
)) += 1;
418 set_disc_field
(post_break
(n
), g
);
423 halfword insert_word_discretionary
(halfword t
, lang_variables
* lan
)
425 halfword pre
= null
, pos
= null
;
426 if
(lan-
>pre_exhyphen_char
> 0)
427 pre
= insert_character
(null
, lan-
>pre_exhyphen_char
);
428 if
(lan-
>post_exhyphen_char
> 0)
429 pos
= insert_character
(null
, lan-
>post_exhyphen_char
);
430 return insert_discretionary
(t
, pre
, pos
, null
,int_par
(ex_hyphen_penalty_code
));
434 halfword compound_word_break
(halfword t
, int clang
)
437 lang_variables langdata
;
438 langdata.pre_exhyphen_char
= get_pre_exhyphen_char
(clang
);
439 langdata.post_exhyphen_char
= get_post_exhyphen_char
(clang
);
440 disc
= insert_word_discretionary
(t
, &langdata);
444 halfword insert_complex_discretionary
(halfword t
, lang_variables
* lan
,
445 halfword pre
, halfword pos
,
449 return insert_discretionary
(t
, pre
, pos
, replace
,int_par
(hyphen_penalty_code
));
452 halfword insert_character
(halfword t
, int c
)
455 p
= new_node
(glyph_node
, 0);
465 void set_disc_field
(halfword f
, halfword t
)
469 tlink
(f
) = tail_of_list
(t
);
477 static char
*hyphenation_exception
(int exceptions
, char
*w
)
481 lua_checkstack
(L
, 2);
482 lua_rawgeti
(L
, LUA_REGISTRYINDEX
, exceptions
);
483 if
(lua_istable
(L
, -1)) { /* ??
*/
484 lua_pushstring
(L
, w
); /* word table
*/
486 if
(lua_type
(L
, -1) == LUA_TSTRING
) {
487 ret
= xstrdup
(lua_tostring
(L
, -1));
497 char
*exception_strings
(struct tex_language
*lang
)
500 size_t size
= 0, current
= 0;
504 if
(lang-
>exceptions
== 0)
506 lua_checkstack
(L
, 2);
507 lua_rawgeti
(L
, LUA_REGISTRYINDEX
, lang-
>exceptions
);
508 if
(lua_istable
(L
, -1)) {
509 /* iterate and join
*/
510 lua_pushnil
(L
); /* first key
*/
511 while
(lua_next
(L
, -2) != 0) {
512 value
= lua_tolstring
(L
, -1, &l);
513 if
(current
+ 2 + l
> size
) {
514 ret
= xrealloc
(ret
, (unsigned
) ((size
+ size
/ 5) + current
+ l
+ 1024));
515 size
= (size
+ size
/ 5) + current
+ l
+ 1024;
517 *(ret
+ current
) = ' '
;
518 strcpy
(ret
+ current
+ 1, value
);
526 @ the sequence from |wordstart| to |r| can contain only normal characters it
527 could be faster to modify a halfword pointer and return an integer
530 static halfword find_exception_part
(unsigned int
*j
, unsigned int
*uword
, int len
)
532 halfword g
= null
, gg
= null
;
533 register unsigned i
= *j
;
534 i
++; /* this puts uword
[i
] on the |
{|
*/
535 while
(i
< (unsigned
) len
&& uword[i + 1] != '}') {
537 gg
= new_char
(0, (int
) uword
[i
+ 1]);
540 halfword s
= new_char
(0, (int
) uword
[i
+ 1]);
550 static int count_exception_part
(unsigned int
*j
, unsigned int
*uword
, int len
)
553 register unsigned i
= *j
;
554 i
++; /* this puts uword
[i
] on the |
{|
*/
555 while
(i
< (unsigned
) len
&& uword[i + 1] != '}') {
564 static const char
*PAT_ERROR
[] = {
565 "Exception discretionaries should contain three pairs of braced items.",
566 "No intervening spaces are allowed.",
571 The exceptions are taken as-is
: no min values are taken into account. One can
572 add normal patterns on-the-fly if needed.
575 static void do_exception
(halfword wordstart
, halfword r
, char
*replacement
)
581 lang_variables langdata
;
582 unsigned uword
[MAX_WORD_LEN
+ 1] = { 0 };
583 utf2uni_strcpy
(uword
, replacement
);
584 len
= u_length
(uword
);
587 clang
= char_lang
(wordstart
);
588 langdata.pre_hyphen_char
= get_pre_hyphen_char
(clang
);
589 langdata.post_hyphen_char
= get_post_hyphen_char
(clang
);
591 for
(i
= 0; i
< len
; i
++) {
592 if
(uword
[i
+ 1] == '
-'
) { /* a hyphen follows
*/
593 while
(vlink
(t
) != r
&& (type(t) != glyph_node || !is_simple_character(t)))
597 insert_syllable_discretionary
(t
, &langdata);
598 t
= vlink
(t
); /* skip the new disc
*/
599 } else if
(uword
[i
+ 1] == '
='
) {
602 } else if
(uword
[i
+ 1] == '
{'
) {
603 halfword gg
, hh
, replace
= null
;
605 gg
= find_exception_part
(&i, uword, (int) len);
606 if
(i
== len || uword
[i
+ 1] != '
{'
) {
607 tex_error
("broken pattern 1", PAT_ERROR
);
609 hh
= find_exception_part
(&i, uword, (int) len);
610 if
(i
== len || uword
[i
+ 1] != '
{'
) {
611 tex_error
("broken pattern 2", PAT_ERROR
);
613 repl
= count_exception_part
(&i, uword, (int) len);
615 tex_error
("broken pattern 3", PAT_ERROR
);
617 /*i
++; *//* jump over the last right brace
*/
623 while
(repl
> 0 && q != null) {
625 if
(type
(q
) == glyph_node
) {
629 try_couple_nodes
(t
, vlink
(q
));
632 t
= insert_discretionary
(t
, gg
, hh
, replace
,int_par
(hyphen_penalty_code
));
633 t
= vlink
(t
); /* skip the new disc
*/
640 @ This is a documentation section from the pascal web file. It is not true any
641 more
, but I do not have time right now to rewrite it
-- Taco
643 When the line-breaking routine is unable to find a feasible sequence of
644 breakpoints
, it makes a second pass over the paragraph
, attempting to hyphenate
645 the hyphenatable words. The goal of hyphenation is to insert discretionary
646 material into the paragraph so that there are more potential places to break.
648 The general rules for hyphenation are somewhat complex and technical
, because we
649 want to be able to hyphenate words that are preceded or followed by punctuation
650 marks
, and because we want the rules to work for languages other than English. We
651 also must contend with the fact that hyphens might radically alter the ligature
652 and kerning structure of a word.
654 A sequence of characters will be considered for hyphenation only if it belongs to
655 a ``potentially hyphenatable part'' of the current paragraph. This is a sequence
656 of nodes $p_0p_1\ldots p_m$ where $p_0$ is a glue node
, $p_1\ldots p_
{m-1
}$ are
657 either character or ligature or whatsit or implicit kern nodes
, and $p_m$ is a
658 glue or penalty or insertion or adjust or mark or whatsit or explicit kern node.
659 (Therefore hyphenation is disabled by boxes
, math formulas
, and discretionary
660 nodes already inserted by the user.
) The ligature nodes among $p_1\ldots p_
{m-1
}$
661 are effectively expanded into the original non-ligature characters
; the kern
662 nodes and whatsits are ignored. Each character |c| is now classified as either a
663 nonletter
(if |lc_code
(c
)=0|
), a lowercase letter
(if |lc_code
(c
)=c|
), or an
664 uppercase letter
(otherwise
); an uppercase letter is treated as if it were
665 |lc_code
(c
)| for purposes of hyphenation. The characters generated by $p_1\ldots
666 p_
{m-1
}$ may begin with nonletters
; let $c_1$ be the first letter that is not in
667 the middle of a ligature. Whatsit nodes preceding $c_1$ are ignored
; a whatsit
668 found after $c_1$ will be the terminating node $p_m$. All characters that do not
669 have the same font as $c_1$ will be treated as nonletters. The |hyphen_char| for
670 that font must be between
0 and
255, otherwise hyphenation will not be attempted.
671 \TeX\ looks ahead for as many consecutive letters $c_1\ldots c_n$ as possible
;
672 however
, |n| must be less than
64, so a character that would otherwise be
673 $c_
{64}$ is effectively not a letter. Furthermore $c_n$ must not be in the middle
674 of a ligature. In this way we obtain a string of letters $c_1\ldots c_n$ that are
675 generated by nodes $p_a\ldots p_b$
, where |
1<=a
<=b
+1<=m|. If |n
>=l_hyf
+r_hyf|
,
676 this string qualifies for hyphenation
; however
, |uc_hyph| must be positive
, if
679 The hyphenation process takes place in three stages. First
, the candidate
680 sequence $c_1\ldots c_n$ is found
; then potential positions for hyphens are
681 determined by referring to hyphenation tables
; and finally
, the nodes $p_a\ldots
682 p_b$ are replaced by a new sequence of nodes that includes the discretionary
685 Fortunately
, we do not have to do all this calculation very often
, because of the
686 way it has been taken out of \TeX's inner loop. For example
, when the second
687 edition of the author's
700-page book
{\sl Seminumerical Algorithms
} was typeset
688 by \TeX
, only about
1.2 hyphenations needed to be @^Knuth
, Donald Ervin@
> tried
689 per paragraph
, since the line breaking algorithm needed to use two passes on only
690 about
5 per cent of the paragraphs.
692 When a word been set up to contain a candidate for hyphenation
, \TeX\ first looks
693 to see if it is in the user's exception dictionary. If not
, hyphens are inserted
694 based on patterns that appear within the given word
, using an algorithm due to
695 Frank~M. Liang. @^Liang
, Franklin Mark@
>
697 @ This is incompatible with TEX because the first word of a paragraph can be
698 hyphenated
, but most european users seem to agree that prohibiting hyphenation
699 there was not the best idea ever.
702 static halfword find_next_wordstart
(halfword r
, halfword first_language
)
705 register int start_ok
= 1;
712 if
(subtype
(r
) == word_boundary
) {
722 while
(mathlevel
> 0) {
726 if
(type
(r
) == math_node
) {
727 if
(subtype
(r
) == before
) {
736 if
(is_simple_character
(r
)) {
738 if
(chr
== ex_hyphen_char
) {
740 We only accept an explicit hyphen when there is a preceding glyph and
741 we skip a sequence of explicit hyphens as that normally indicates a
742 -- or
--- ligature in which case we can in a worse case usage get bad
743 node lists later on due to messed up ligature building as these dashes
744 are ligatures in base fonts. This is a side effect of the separating the
745 hyphenation
, ligaturing and kerning steps. A test is cmr with
------.
748 if
((start_ok
> 0) && (t!=null) && (type(t) == glyph_node) && (character(t) != ex_hyphen_char)) {
749 t
= compound_word_break
(r
, char_lang
(r
));
750 subtype
(t
) = automatic_disc
;
755 } else if
(start_ok
&& (char_lang(r)>=first_language) && ((l = get_hj_code(char_lang(r),chr)) > 0)) {
756 if
(char_uchyph
(r
) || l
== chr
) {
774 static int valid_wordend
(halfword s
)
776 register halfword r
= s
;
777 register int clang
= char_lang
(s
);
780 while
((r
!= null
) && ( (type(r) == glyph_node && is_simple_character(r) && clang == char_lang(r))
781 ||
(type
(r
) == kern_node
&& (subtype(r) == normal))
785 if
(r
== null ||
(type
(r
) == glyph_node
&& is_simple_character(r) && clang != char_lang(r))
786 || type
(r
) == glue_node
787 || type
(r
) == boundary_node
788 || type
(r
) == whatsit_node
789 || type
(r
) == ins_node
790 || type
(r
) == adjust_node
791 || type
(r
) == penalty_node
792 ||
(type
(r
) == kern_node
&& (subtype(r) == explicit_kern ||
793 subtype
(r
) == italic_kern ||
794 subtype
(r
) == accent_kern
)))
800 void hnj_hyphenation
(halfword head
, halfword tail
)
803 struct tex_language
*lang
;
804 lang_variables langdata
;
805 char utf8word
[(4 * MAX_WORD_LEN
) + 1] = { 0 };
808 char
*replacement
= NULL;
809 boolean explicit_hyphen
= false
;
810 halfword first_language
= int_par
(first_valid_language_code
);
811 halfword s
, r
= head
, wordstart
= null
, save_tail1
= null
, left
= null
, right
= null
;
813 /* this first movement assures two things
:
814 \item
{a
)} that we won't waste lots of time on something that has been
815 handled already
(in that case
, none of the glyphs match |simple_character|
).
816 \item
{b
)} that the first word can be hyphenated. if the movement was
817 not explicit
, then the indentation at the start of a paragraph
818 list would make |find_next_wordstart
()| look too far ahead.
821 while
(r
!= null
&& (type(r) != glyph_node || !is_simple_character(r))) {
824 /* this will make |r| a glyph node with subtype character
*/
825 r
= find_next_wordstart
(r
,first_language
);
829 assert
(tail
!= null
);
830 save_tail1
= vlink
(tail
);
832 couple_nodes
(tail
, s
);
834 while
(r
!= null
) { /* could be while
(1), but let's be paranoid
*/
835 int clang
, lhmin
, rhmin
, hmin
;
837 halfword end_word
= r
;
839 assert
(is_simple_character
(wordstart
));
840 hyf_font
= font
(wordstart
);
841 if
(hyphen_char
(hyf_font
) < 0) /* for backward compat
*/
843 clang
= char_lang
(wordstart
);
844 lhmin
= char_lhmin
(wordstart
);
845 rhmin
= char_rhmin
(wordstart
);
846 hmin
= get_hyphenation_min
(clang
);
847 langdata.pre_hyphen_char
= get_pre_hyphen_char
(clang
);
848 langdata.post_hyphen_char
= get_post_hyphen_char
(clang
);
850 && type(r) == glyph_node
851 && is_simple_character(r)
852 && clang == char_lang(r)
853 && ( ( (clang >= first_language)
854 && (lchar = get_hj_code(clang,character(r))) > 0
856 ||
( character
(r
) == ex_hyphen_char
857 && (lchar = ex_hyphen_char)
861 if
(character
(r
) == ex_hyphen_char
)
862 explicit_hyphen
= true
;
864 hy
= uni2string
(hy
, (unsigned
) lchar
);
865 /* this should not be needed any more
*/
866 /*if
(vlink
(r
)!=null
) alink
(vlink
(r
))=r
; */
870 if
( valid_wordend
(r
)
871 && clang >= first_language
872 && wordlen >= lhmin + rhmin
873 && (hmin <= 0 || wordlen >= hmin)
875 && (lang = tex_languages[clang]) != NULL
878 if
( lang-
>exceptions
!= 0
879 && (replacement = hyphenation_exception(lang->exceptions, utf8word)) != NULL
882 formatted_warning
("hyphenation","replacing %s (c=%d) by %s", utf8word
, clang
, replacement
);
884 do_exception
(wordstart
, r
, replacement
);
886 } else if
(explicit_hyphen
== true
) {
887 /* insert an explicit discretionary after each of the last in a
888 set of explicit hyphens
*/
892 formatted_warning
("hyphenation","explicit hyphen(s) found in %s (c=%d)", utf8word
, clang
);
894 while
(rr
!= wordstart
) {
895 if
(is_simple_character
(rr
)) {
896 if
(character
(rr
) == ex_hyphen_char
) {
897 t
= compound_word_break
(rr
, clang
);
898 subtype
(t
) = automatic_disc
;
899 while
(character
(alink
(rr
)) == ex_hyphen_char
)
907 } else if
(lang-
>patterns
!= NULL) {
909 for
(i
= lhmin
; i
> 1; i--
) {
911 while
(!is_simple_character
(left
))
915 for
(i
= rhmin
; i
> 0; i--
) {
916 right
= alink
(right
);
917 while
(!is_simple_character
(right
))
918 right
= alink
(right
);
921 formatted_warning
("hyphenation","hyphenate %s (c=%d,l=%d,r=%d) from %c to %c",
922 utf8word
, clang
, lhmin
, rhmin
, character
(left
), character
(right
));
924 (void
) hnj_hyphen_hyphenate
(lang-
>patterns
, wordstart
, end_word
, wordlen
, left
, right
, &langdata);
927 explicit_hyphen
= false
;
932 r
= find_next_wordstart
(r
,first_language
);
934 flush_node
(vlink
(tail
));
935 vlink
(tail
) = save_tail1
;
939 void new_hyphenation
(halfword head
, halfword tail
)
941 register int callback_id
= 0;
942 if
(head
== null || vlink
(head
) == null
)
945 callback_id
= callback_defined
(hyphenate_callback
);
946 if
(callback_id
> 0) {
948 if
(!get_callback
(L
, callback_id
)) {
952 nodelist_to_lua
(L
, head
);
953 nodelist_to_lua
(L
, tail
);
954 if
(lua_pcall
(L
, 2, 0, 0) != 0) {
955 formatted_warning
("hyphenation","bad specification: %s",lua_tostring
(L
, -1));
961 } else if
(callback_id
== 0) {
962 hnj_hyphenation
(head
, tail
);
966 @ dumping and undumping languages
969 #define dump_string
(a
) \
971 x
= (int
)strlen
(a
)+1; \
972 dump_int
(x
); dump_things
(*a
, x
); \
974 x
= 0; dump_int
(x
); \
977 static void dump_one_language
(int i
)
981 struct tex_language
*lang
;
982 lang
= tex_languages
[i
];
984 dump_int
(lang-
>pre_hyphen_char
);
985 dump_int
(lang-
>post_hyphen_char
);
986 dump_int
(lang-
>pre_exhyphen_char
);
987 dump_int
(lang-
>post_exhyphen_char
);
988 dump_int
(lang-
>hyphenation_min
);
989 if
(lang-
>patterns
!= NULL) {
990 s
= (char
*) hnj_serialize
(lang-
>patterns
);
997 if
(lang-
>exceptions
!= 0)
998 s
= exception_strings
(lang
);
1006 void dump_language_data
(void
)
1009 dump_int
(next_lang_id
);
1010 for
(i
= 0; i
< next_lang_id
; i
++) {
1011 if
(tex_languages
[i
]) {
1013 dump_one_language
(i
);
1020 static void undump_one_language
(int i
)
1024 struct tex_language
*lang
= get_language
(i
);
1028 lang-
>pre_hyphen_char
= x
;
1030 lang-
>post_hyphen_char
= x
;
1032 lang-
>pre_exhyphen_char
= x
;
1034 lang-
>post_exhyphen_char
= x
;
1036 lang-
>hyphenation_min
= x
;
1040 s
= xmalloc
((unsigned
) x
);
1041 undump_things
(*s
, x
);
1042 load_patterns
(lang
, (unsigned char
*) s
);
1048 s
= xmalloc
((unsigned
) x
);
1049 undump_things
(*s
, x
);
1050 load_hyphenation
(lang
, (unsigned char
*) s
);
1055 void undump_language_data
(void
)
1058 undump_int
(numlangs
);
1059 next_lang_id
= numlangs
;
1060 for
(i
= 0; i
< numlangs
; i
++) {
1063 undump_one_language
(i
);
1068 @ When \TeX\ has scanned `\.
{\\hyphenation
}'
, it calls on a procedure named
1069 |new_hyph_exceptions| to do the right thing.
1072 void new_hyph_exceptions
(void
)
1073 { /* enters new exceptions
*/
1074 (void
) scan_toks
(false
, true
);
1075 load_tex_hyphenation
(int_par
(language_code
), def_ref
);
1076 flush_list
(def_ref
);
1079 @ Similarly
, when \TeX\ has scanned `\.
{\\patterns
}'
, it calls on a
1080 procedure named |new_patterns|.
1083 void new_patterns
(void
)
1084 { /* initializes the hyphenation pattern data
*/
1085 (void
) scan_toks
(false
, true
);
1086 load_tex_patterns
(int_par
(language_code
), def_ref
);
1087 flush_list
(def_ref
);
1090 @ `\.
{\\prehyphenchar
}'
, sets the |pre_break| character
, and
1091 `\.
{\\posthyphenchar
}' the |post_break| character. Their respective defaults are
1092 ascii hyphen
("-") and zero
(nul
).
1095 void new_pre_hyphen_char
(void
)
1097 scan_optional_equals
();
1099 set_pre_hyphen_char
(int_par
(language_code
), cur_val
);
1102 void new_post_hyphen_char
(void
)
1104 scan_optional_equals
();
1106 set_post_hyphen_char
(int_par
(language_code
), cur_val
);
1109 @ `\.
{\\preexhyphenchar
}'
, sets the |pre_break| character
, and
1110 `\.
{\\postexhyphenchar
}' the |post_break| character. Their defaults are both zero
1114 void new_pre_exhyphen_char
(void
)
1116 scan_optional_equals
();
1118 set_pre_exhyphen_char
(int_par
(language_code
), cur_val
);
1121 void new_post_exhyphen_char
(void
)
1123 scan_optional_equals
();
1125 set_post_exhyphen_char
(int_par
(language_code
), cur_val
);
1128 void new_hyphenation_min
(void
)
1130 scan_optional_equals
();
1132 set_hyphenation_min
(int_par
(language_code
), cur_val
);
1135 void new_hj_code
(void
)
1140 scan_optional_equals
();
1142 set_hj_code
(int_par
(language_code
), i
, cur_val
, -1);