summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/luatex/hyph-utf8/luatex-hyphen.dtx
blob: ca1761ef2b62a8f2a517fbef961bb7c675792e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
% \iffalse meta-comment
%
% This is a module for hyphenation patterns loading for LuaTeX. It is under
% the CC0 license (http://creativecommons.org/publicdomain/zero/1.0/).
% 
%<*ignore>
\begingroup
  \def\x{LaTeX2e}%
\expandafter\endgroup
\ifcase 0\ifx\install y1\fi\expandafter
         \ifx\csname processbatchFile\endcsname\relax\else1\fi
         \ifx\fmtname\x\else 1\fi\relax
\else\csname fi\endcsname
%</ignore>
%<*install>
\input docstrip.tex

\keepsilent
\askforoverwritefalse

\def\MetaPrefix{-- }

\def\luapostamble{%
  \MetaPrefix^^J%
  \MetaPrefix\space End of File `\outFileName'.%
}

\def\currentpostamble{\luapostamble}%

\preamble

This is a generated file (source: luatex-hyphen.dtx).

Copyright (C) 2012-2013 by The LuaLaTeX development team.

This work is under the CC0 license.

\endpreamble

\generate{%
  \file{luatex-hyphen.lua}{\from{luatex-hyphen.dtx}{lua}}%
}

\endbatchfile
%</install>
%<*ignore>
\fi
%</ignore>
%<*driver>
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{luatex-hyphen.drv}
  [2013/05/16 v1.6 Hyphenation pattern loading for LuaTeX]
\documentclass{ltxdoc}
\usepackage[
     pdftitle={The luatex-hyphen package},
   pdfsubject={Hyphenation pattern loading for LuaTeX},
    pdfauthor={Manuel Pégourié-Gonnard, Khaled Hosny & Elie Roux},
  pdfkeywords={luatex, lualatex, unicode, hyphenation}
]{hyperref}
\usepackage{fontspec}
\setmainfont[
  Ligatures   = TeX,
  BoldFont    = {Linux Libertine O Bold},
  ItalicFont  = {Linux Libertine O Italic},
  SlantedFont = {Linux Libertine O Italic},
]{Linux Libertine O}
\setmonofont[Ligatures=TeX,Scale=MatchLowercase]{Liberation Mono}
\setsansfont[Ligatures=TeX,Scale=MatchLowercase]{Iwona Medium}
\EnableCrossrefs
\CodelineIndex
\begin{document}
  \DocInput{luatex-hyphen.dtx}%
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{luatex-hyphen.drv}
%
% \title{The \texttt{hyphen.cfg} file for Lua\TeX }
% \date{2013/05/16 v1.6}
% \author{Khaled Hosny, Élie Roux, and Manuel Pégourié-Gonnard\\
% \texttt{khaledhosny@eglug.org} \\
% \texttt{elie.roux@telecom-bretagne.eu} \\
% \texttt{mpg@elzevir.fr}}
%
% \maketitle
%
% \begin{abstract}
% This package is mainly a Lua module, to be used by \textsf{Babel} and
% \textsf{polyglossia} to adapt their hyphenation patterns loading mechanism to 
% Lua\TeX's dynamic pattern loading capabilities. It makes use of a 
% \texttt{language.dat.lua} file (whose format is described below) that should 
% be present in the distribution, in addition to the regular 
% \texttt{language.dat} file.
%
% \textsf{Babel} needed to be updated -- this used to be the goal of this 
% package -- before version \textsf{3.9} (\TeX Live 2013)  and 
% \textsf{polyglossia} handles Lua\TeX\ since version \textsf{1.3} (\TeX Live 
% 2013).
%
% There is a version of \texttt{etex.src} modified for the same reasons using
% similar code, which also makes use of the \texttt{luatex-hyphen.lua} and
% \texttt{language.dat.lua} files described here.
% \end{abstract}
%
% \section{Documentation}
%
% Hyphenation patterns should be loaded at runtime with Lua\TeX: if they appear
% in the format, they will be rehashed when the format is loaded anyway, which
% makes the format quite long to load (many seconds even on modern machines)
% and provides for bad user experience. Hence, it is desirable to load as few
% patterns as possible in the format, and load on-demand the needed patterns
% at runtime.
%
% This package provides a modified version of hyphen.cfg adapted to Lua\TeX,
% as well as a supporting Lua module. Since a lot of things, especially the
% catcodes, are not as predictable at runtime than at format creation time, we
% don't \verb+\input+ the usual pattern files, but rather load the patterns
% using the Lua interface, using a special plain text version of the pattern
% files if available.
%
% The existence and file name of such a version cannot be guessed, so we need
% a specific database: the file \texttt{language.dat.lua}. This file should be
% loadable by Lua and return a table whose keys are the canonical language
% names as found in \texttt{language.dat}, and the values are Lua tables
% consisting of:
% \begin{enumerate}
%   \item A fixed part with one mandatory field:
%     \begin{quote}
%       \verb+synonyms = { <string> alternative name, ...}+
%     \end{quote}
%     This field's value must be the same as in \texttt{language.dat}.
%   \item A variable part consisting of either:
%     \begin{itemize}
%       \item For most languages:
%         \begin{quote}
%           \verb+patterns = <string> filenames for patterns+\\
%           \verb+hyphenation = <string> filenames for exceptions+\\
%         \end{quote}
%         Each string contains a coma-separated list of file names (whitespace
%         before or after the coma is not accepted).
%         The files given by \verb+patterns+ (resp. \verb+hypenation+) must be
%         plain text files encoded in UTF-8, with only patterns (resp.
%         exceptions) and not even comments: their content will be used
%         directly without being parsed by \TeX.  If one of these keys is
%         missing or is the empty string, it is ignored and no patterns (resp.
%         exceptions) are loaded for this language.
%       \item Special cases are supported by a field \verb+special+. Currently,
%         the following kind of values are recognized:
%         \begin{description}
%           \item[\texttt{'disabled:<reason>'}] allows to disable specific
%             languages: when the user tries to load this language, an error
%             will be issued, with the \verb+<reason>+.
%           \item[\texttt{'language0'}] only \texttt{english} should use this
%           type of special, to indicate it is normally dumped in the format
%           as \verb+\language0+ (see below).
%         \end{description}
%         Special languages may have \texttt{*hyphenmin} information when it
%         makes sense (mostly \verb+\language0+).
%     \end{itemize}
%   \item Optional fields may be added. For example:
%     \begin{quote}
%       \verb+loader = <string> name of the TeX loader+\\
%       \verb+lefthyphenmin = <number> value for \lefthyphenmin+\\
%       \verb+righthyphenmin = <number> value for \righthyphenmin+
%     \end{quote}
%     Those fields are present in \texttt{language.dat.lua} as generated by
%     \texttt{tlmgr}, for example, but they \emph{are not} used by the present
%     code in any way.
% \end{enumerate}
% Languages that are mentioned in \texttt{language.dat} but not in
% \texttt{language.dat.lua} will be loaded in the format. So, if the
% \texttt{language.dat.lua} file is missing or incomplete, languages will just
% go back to the ``old'' behaviour, resulting in longer startup time, which
% seems less bad than complete breakage.
%
% For backward compatibility, Knuth's original patterns for US English are
% always loaded in the format, as \verb+\language0+.\footnote{It is assumed
% to be the first entry in \texttt{language.dat}.}
%
% \StopEventually{
% }
%
% \section{Implementation}
%
%    \begin{macrocode}
%<*lua>
%    \end{macrocode}
%
%    Start a Lua module, two functions for error and information reporting.
%
%    \begin{macrocode}
luatexhyphen = luatexhyphen or {}
local luatexhyphen = luatexhyphen
local function wlog(msg, ...)
    texio.write_nl('log', 'luatex-hyphen: '..msg:format(...))
end
local function err(msg, ...)
    error('luatex-hyphen: '..msg:format(...), 2)
end
%    \end{macrocode}
%
%    Load the \texttt{language.dat.lua} file with the Lua version of the
%    language database.
%
%    \begin{macrocode}
local dbname = "language.dat.lua"
local language_dat
local dbfile = kpse.find_file(dbname, 'lua')
if not dbfile then
    err("file not found: "..dbname)
else
    wlog('using data file: %s', dbfile)
    language_dat = dofile(dbfile)
end
%    \end{macrocode}
%
%    Look up a language in the database, and return the associated
%    information, as well as the canonical name of the language.
%
%    \begin{macrocode}
local function lookupname(name)
    if language_dat[name] then
        return language_dat[name], name
    else
        for canon, data in pairs(language_dat) do
            for _,syn in ipairs(data.synonyms) do
                if syn == name then
                    return data, canon
                end
            end
        end
    end
end
luatexhyphen.lookupname = lookupname
%    \end{macrocode}
%
%    Set hyphenation patterns and exceptions for a language given by its name
%    (in the database) and number (value of \verb+\language+). Doesn't return
%    anything, but will call \verb+error()+ if things go wrong.
%
%    \begin{macrocode}
local function loadlanguage(lname, id)
    if id == 0 then
        return
    end
    local msg = "loading%s patterns and exceptions for: %s (\\language%d)"
%    \end{macrocode}
%
%    Lookup the language in the database.
%
%    \begin{macrocode}
    local ldata, cname = lookupname(lname)
    if not ldata then
        err("no entry in %s for this language: %s", dbname, lname)
    end
%    \end{macrocode}
%
%    Handle special languages.
%
%    \begin{macrocode}
    if ldata.special then
        if ldata.special:find('^disabled:') then
            err("language disabled by %s: %s (%s)", dbname, cname,
                ldata.special:gsub('^disabled:', ''))
        elseif ldata.special == 'language0' then
            err("\\language0 should be dumped in the format")
        else
            err("bad entry in %s for language %s")
        end
    end
%    \end{macrocode}
%
%    The generic case: load hyphenation patterns and exceptions from files
%    given by the language code.
%
%    \begin{macrocode}
    wlog(msg, '', cname, id)
    for _, item in ipairs{'patterns', 'hyphenation'} do
        local filelist = ldata[item]
        if filelist ~= nil and filelist ~= '' then
          for _, file in ipairs(filelist:explode(',')) do
            local file = kpse.find_file(file) or err("file not found: %s", file)
            local fh = io.open(file, 'r')
            local data = fh:read('*a') or err("file not readable: %s", f)
            fh:close()
            lang[item](lang.new(id), data)
          end
        else
            if item == 'hyphenation' then item = item..' exceptions' end
            wlog("info: no %s for this language", item)
        end
    end
end
luatexhyphen.loadlanguage = loadlanguage
%    \end{macrocode}
%
%    Add \textsf{Babel}'s ``dialects'' as synonyms.
%
%    \begin{macrocode}
local function adddialect(dialect, language)
    if dialect ~= '0' then
        dialect = dialect:gsub('l@', '')
        language = language:gsub('l@', '')
        data = lookupname(language)
        if data then
            data.synonyms[#data.synonyms+1] = dialect
        end
    end
end
luatexhyphen.adddialect = adddialect
%    \end{macrocode}
%
%    \begin{macrocode}
%</lua>
%    \end{macrocode}
%
% \Finale
\endinput