lang-hyp.mkiv /size: 9618 b    last modification: 2021-10-28 13:50
1%D \module
2%D   [       file=lang-ini,
3%D        version=2014.08.10,
4%D          title=\CONTEXT\ Language Macros,
5%D       subtitle=Experimental Patterns,
6%D         author=Hans Hagen,
7%D           date=\currentdate,
8%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
9%C
10%C This module is part of the \CONTEXT\ macro||package and is
11%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
12%C details.
13
14%D This is an experimental module. We often have to deal with titles
15%D that have conflicting demands:
16%D
17%D \startitemize
18%D \startitem They go into a dedicated space (often a graphic). \stopitem
19%D \startitem The words cannot be hyphenated. \stopitem
20%D \startitem But as an escape they can get hyphenated. \stopitem
21%D \startitem In that case we want at least an extra word on the last line. \stopitem
22%D \stopitemize
23%D
24%D These and maybe more cases can be dealt with using dedicated hyphenation
25%D mechanisms. At he same time we want to experiment with more extensive patterns
26%D as discussed in {\em TUGboat, Volume 27 (2006), No. 2—Proceedings of EuroTEX2006}.
27
28% lua: 5.341 5.354
29% tex: 5.174 5.262
30
31\writestatus{loading}{ConTeXt Language Macros / Initialization}
32
33\registerctxluafile{lang-dis}{autosuffix}
34\registerctxluafile{lang-hyp}{autosuffix}
35
36\unprotect
37
38\definesystemattribute[hyphenation][public]
39
40%D After a decade of playing with these things in \LUATEX|/|\MKIV\ it's time to
41%D finish the way we deal with discretionaries. Apart from the fact that they play a
42%D role in hyphenation they also need to be dealt with in fonts. Flattening, cleanup
43%D and such are now more or less default in \CONTEXT\ so we can simplify some of the
44%D code. We also use the new penalty mechanism.
45
46\newcount\compoundhyphenpenalty
47
48\ifcase\contextlmtxmode
49    \hyphenpenaltymode\plusfour
50    \automatichyphenmode\plusone
51\fi
52
53\hyphenpenalty            50 % hyphenator
54\automatichyphenpenalty   50 % -
55\explicithyphenpenalty    50 % \-
56\compoundhyphenpenalty    50
57\exceptionpenalty       1000
58
59%D This command can change! At some point we will keep the setting with the
60%D paragraph and then the \type {\par} can go.
61
62% \unexpanded\def\atleastoneword#1%
63%   {\begingroup
64%    \enabledirectives[hyphenators.method=traditional]%
65%    \enabledirectives[hyphenators.rightwordsmin=1]%
66%    \lefthyphenmin \plusfour
67%    \righthyphenmin\plusfour
68%    #1\par
69%    \disabledirectives[hyphenators.rightwordsmin]%
70%    \enabledirectives[hyphenators.method]%
71%    \endgroup}
72
73% \exhyphenchar    \hyphenasciicode
74% \preexhyphenchar \lessthanasciicode
75% \postexhyphenchar\morethanasciicode
76
77%D Here is the real way:
78
79\installcorenamespace{hyphenation}
80\installcorenamespace{hyphenationfeatures}
81
82\installparameterhandler \??hyphenation {hyphenation}
83\installsetuphandler     \??hyphenation {hyphenation}
84
85\setuphyphenation
86  [\c!method=\s!default,
87   \s!righthyphenchar=0, % number tzt g: etc
88   \s!lefthyphenchar=0]  % number
89
90\appendtoks
91    \clf_sethyphenationmethod{\hyphenationparameter\c!method}%
92\to \everysetuphyphenation
93
94%D These are mostly meant for manuals:
95
96\unexpanded\def\starthyphenation[#1]%
97  {\begingroup
98   \clf_pushhyphenation{#1}}
99
100\unexpanded\def\stophyphenation
101  {\ifhmode\par\fi
102   \clf_pophyphenation
103   \endgroup}
104
105% This is a global setting, so we need to disable it when needed. However, as
106% we are (hopefully) compatible and attribute driven one can also just keep it
107% enabled.
108%
109% \setuphyphenation
110%   [\c!method=\s!traditional] % no translations
111
112\unexpanded\def\definehyphenationfeatures
113  {\dodoubleargument\lang_hyphenation_define_features}
114
115\unexpanded\def\lang_hyphenation_define_features[#1][#2]%
116  {\begingroup
117   \letdummyparameter\c!characters\empty           % maybe \s!
118   \letdummyparameter\c!hyphens\empty              % maybe \s!
119   \letdummyparameter\c!joiners\empty              % maybe \s!
120   \letdummyparameter\c!rightwords\zerocount       % maybe \s!
121   \letdummyparameter\s!lefthyphenmin\zerocount
122   \letdummyparameter\s!righthyphenmin\zerocount
123   \letdummyparameter\s!hyphenmin\zerocount
124   \letdummyparameter\s!lefthyphenchar\zerocount
125   \letdummyparameter\s!righthyphenchar\zerocount
126   \letdummyparameter\c!alternative\empty
127   \letdummyparameter\c!rightedge\empty
128   \letdummyparameter\c!rightchars\empty
129   \getdummyparameters[#2]%
130   \clf_definehyphenationfeatures
131      {#1}%
132      {
133        characters   {\dummyparameter\c!characters}%
134        hyphens      {\dummyparameter\c!hyphens}%
135        joiners      {\dummyparameter\c!joiners}%
136        rightwordmin \numexpr\dummyparameter\c!rightwords\relax
137        rightchars   {\dummyparameter\c!rightchars}%
138        charmin      \numexpr\dummyparameter\s!hyphenmin\relax
139        leftcharmin  \numexpr\dummyparameter\s!lefthyphenmin\relax
140        rightcharmin \numexpr\dummyparameter\s!righthyphenmin\relax
141        leftchar     \numexpr\dummyparameter\s!lefthyphenchar\relax
142        rightchar    \numexpr\dummyparameter\s!righthyphenchar\relax
143        alternative  {\dummyparameter\c!alternative}%
144rightedge    {\dummyparameter\c!rightedge}%
145% autohyphen   {\dummyparameter\c!autohyphen}
146% hyphenonly   {\dummyparameter\c!hyphenonly}
147      }%
148   \relax
149   \endgroup}
150
151\unexpanded\def\sethyphenationfeatures[#1]%
152  {\clf_sethyphenationfeatures{#1}}
153
154\unexpanded\def\resethyphenationfeatures
155  {\attribute\hyphenationattribute\attributeunsetvalue}
156
157\resethyphenationfeatures
158
159% todo: \start ... \stop too
160
161\unexpanded\def\registerhyphenationpattern
162  {\dodoubleempty\lang_hyphenation_register_pattern}
163
164\def\lang_hyphenation_register_pattern[#1][#2]%
165  {\clf_registerhyphenationpattern\ifsecondargument{#1}{#2}\else{\currentlanguage}{#1}\fi\s!true\relax}
166
167\unexpanded\def\unregisterhyphenationpattern
168  {\dodoubleempty\lang_hyphenation_unregister_pattern}
169
170\def\lang_hyphenation_unregister_pattern[#1][#2]%
171  {\clf_registerhyphenationpattern\ifsecondargument{#1}{#2}\else{\currentlanguage}{#1}\fi\s!false\relax}
172
173\unexpanded\def\registerhyphenationexception
174  {\dodoubleempty\lang_hyphenation_register_exception}
175
176\def\lang_hyphenation_register_exception[#1][#2]%
177  {\clf_registerhyphenationexception\ifsecondargument{#1}{#2}\else{\currentlanguage}{#1}\fi\relax}
178
179\unexpanded\def\showhyphenationtrace
180  {\dodoubleempty\lang_hyphenation_show_trace}
181
182\def\lang_hyphenation_show_trace[#1][#2]%
183  {\begingroup
184   \tt
185   \clf_showhyphenationtrace\ifsecondargument{#1}{#2}\else{\currentlanguage}{#1}\fi\relax
186   \endgroup}
187
188% For old times sake:
189
190\unexpanded\def\atleastoneword#1%
191  {\begingroup
192   \starthyphenation[traditional]% this might become default or a faster switch
193   \sethyphenationfeatures[words]%
194   #1\par
195   \stophyphenation
196   \endgroup}
197
198%D For me:
199
200\unexpanded\def\showdiscretionaries
201  {\clf_showdiscretionaries}
202
203%D These are (at least now) not cummulative:
204
205\definehyphenationfeatures % just an example
206  [fences]
207  [\c!characters={[]()}]
208
209\definehyphenationfeatures
210  [words]
211  [\c!rightwords=1,
212   \s!lefthyphenmin=4,
213   \s!righthyphenmin=4]
214
215\definehyphenationfeatures
216  [default]
217  [%c!rightedge=\v!tex,
218   \c!hyphens=\v!yes,
219   \c!joiners=\v!yes]
220
221\definehyphenationfeatures
222  [strict]
223  [\c!rightedge=\s!tex]
224
225% \sethyphenationfeatures
226%   [fences]
227
228% \sethyphenationfeatures
229%   [default,fences]
230
231% \setuphyphenation % will be default
232%   [method=expanded]
233
234\protect \endinput
235
236% \starttext
237%
238% \enabledirectives[hyphenators.method=traditional]
239%
240% % \dorecurse{1000}{\input tufte \par}
241%
242% \setupalign[verytolerant,flushleft]
243% \setuplayout[width=140pt] \showframe
244%
245% longword longword long word longword longwordword \blank
246%
247% \enabledirectives[hyphenators.rightwordsmin=1]
248%
249% longword longword long word longword longwordword\blank
250%
251% \disabledirectives[hyphenators.rightwordsmin]
252%
253% longword longword long word longword longwordword\blank
254%
255% \atleastoneword{longword longword long word longword longwordword}
256%
257% \enabledirectives[hyphenators.method=traditional]
258%
259% \stoptext
260
261% \startluacode
262%     -- e1ë/e=e             reëel      re-eel
263%     -- a1atje./a=t,1,3     omaatje    oma-tje
264%     -- schif1f/ff=f,5,2    Schiffahrt Schiff-fahrt
265%
266%     languages.hyphenators.traditional.registerpattern("en","a1b",      { start = 1, length = 2, before = "CD", after = "EF"  } )
267%     languages.hyphenators.traditional.registerpattern("en","e1ë",      { start = 1, length = 2, before = "e",  after = "e"  } )
268%     languages.hyphenators.traditional.registerpattern("en","oo1ë",     { start = 2, length = 2, before = "o",  after = "e"  } )
269%     languages.hyphenators.traditional.registerpattern("en","qqxc9xkqq",{ start = 3, length = 4, before = "ab", after = "cd" } ) -- replacement start length
270%
271%     --  print("reëel",       injecthyphens(dictionaries.nl,"reëel",       2,2))
272%     --  print("reeëel",      injecthyphens(dictionaries.nl,"reeëel",      2,2))
273%     --  print("rooëel",      injecthyphens(dictionaries.nl,"rooëel",      2,2))
274%     --  print(   "QXcXkQ",   injecthyphens(dictionaries.de,   "QXcXkQ",   2,2))
275%     --  print(  "QQXcXkQQ",  injecthyphens(dictionaries.de,  "QQXcXkQQ",  2,2))
276%     --  print( "QQQXcXkQQQ", injecthyphens(dictionaries.de, "QQQXcXkQQQ", 2,2))
277%     --  print("QQQQXcXkQQQQ",injecthyphens(dictionaries.de,"QQQQXcXkQQQQ",2,2))
278%     --
279%     --  print(  "QQXcXkQQ QQXcXkQQ",  injecthyphens(dictionaries.de,  "QQXcXkQQ QQXcXkQQ",  2,2))
280% \stopluacode
281%
282% \starttext
283%
284% \blank
285%
286% xreëel rooëel \par xxabxx xxxabxxx \par
287%
288% \hsize1mm \lefthyphenmin2 \righthyphenmin2
289%
290% \blank Capacity \blank capacity \blank xyabxy \blank xreëel \blank rooëel \blank
291%
292% xy\discretionary{CD}{EF}{ab}xy % xxacceedxxx
293%
294% \stoptext
295