Debian Codegraph @

texmf-dist/tex/latex/polyglossia/polyglossia.lua

raw
1 --
2 -- polyglossia.lua
3 -- part of polyglossia v2.10 -- 2026/04/18
4 --
5
6 local module_name = "polyglossia"
7 local polyglossia_module = {
8 name = module_name,
9 version = 1.3,
10 date = "2013/05/11",
11 description = "Polyglossia",
12 author = "Elie Roux",
13 copyright = "Elie Roux",
14 license = "CC0"
15 }
16
17 luatexbase.provides_module(polyglossia_module)
18
19 local log_info = function(message, ...)
20 luatexbase.module_info(module_name, message:format(...))
21 end
22 local log_warn = function(message, ...)
23 luatexbase.module_warning(module_name, message:format(...))
24 end
25
26 polyglossia = polyglossia or {}
27 local polyglossia = polyglossia
28
29 local newloader_available_languages = require'language.dat.lua'
30 -- Suggestion by Dohyun Kim on #129
31 local t = { }
32 for k, v in pairs(newloader_available_languages) do
33 t[k] = v
34 for _, vv in pairs(v.synonyms) do
35 t[vv] = v
36 end
37 end
38 newloader_available_languages = t
39
40 -- LaTeX's language register is \count19
41 local lang_register = 19
42
43 local nohyphid = luatexbase.registernumber'l@nohyphenation'
44 local newloader_loaded_languages = { }
45
46 -- New hyphenation pattern loader: use language.dat.lua directly and the language identifiers
47 local function newloader(langentry)
48 local loaded_language = newloader_loaded_languages[langentry]
49 if loaded_language then
50 return lang.id(loaded_language)
51 else
52 local langdata = newloader_available_languages[langentry]
53 if langdata then
54
55 local special = langdata.special
56 if special then
57 -- language0 (USenglish) is already included in the format
58 if special == 'language0' then
59 return 0
60
61 -- disabled language should not be used for utf-8 text
62 elseif special:find'^disabled:' then
63 log_warn('Hyphenation of language %s %s', langentry, special)
64 return nohyphid
65 end
66 end
67
68 -- language info will be written into the .log file
69 local s = { }
70 for k, v in pairs(langdata) do
71 if type(v) == 'table' then -- for 'synonyms'
72 s[#s+1] = k .. "\t" .. table.concat(v,',')
73 else
74 s[#s+1] = k .. "\t" .. tostring(v)
75 end
76 end
77 local a = {}
78 for _,n in pairs(s) do table.insert(a, n) end
79 table.sort(a)
80 log_info("Language data for " .. langentry .. "\n" .. table.concat(a,"\n"))
81
82 --
83 -- LaTeX's \newlanguage increases language register (count19),
84 -- whereas LuaTeX's lang.new() increases its own language id.
85 -- So when a user has declared, say, \newlanguage\lang@xyz, then
86 -- these two numbers do not match each other. If we do not consider
87 -- this possible situation, our newloader() function will
88 -- unfortunately overwrite the language \lang@xyz.
89 --
90 -- Therefore here we will compare LaTeX's \newlanguage number with
91 -- LuaTeX's lang.new() id and select the bigger one for our new
92 -- language object. Also we will update LaTeX's language register
93 -- by this new id, so that another possible \newlanguage should not
94 -- overwrite our language object.
95 --
96 -- get next \newlanguage allocation number
97 local langcnt = tex.count[lang_register] + 1
98 -- get new lang object
99 local langobject = lang.new()
100 local langid = lang.id(langobject)
101 -- get bigger one between \newlanguage and new lang obj id
102 local newlangid = math.max(langcnt, langid)
103 -- set language register for possible \newlanguage
104 tex.setcount('global', lang_register, newlangid)
105 -- get new lang object if needed
106 if langid ~= newlangid then
107 langobject = lang.new(newlangid)
108 end
109
110 -- load hyphenation patterns and exceptions
111 for _,v in ipairs{ 'patterns', 'hyphenation' } do
112 local data = langdata[v]
113 if data and data ~= '' then
114 -- cope with comma separated list, such as serbian
115 for _,vv in ipairs(data:explode',+') do
116 local filepath = kpse.find_file(vv)
117 if filepath then
118 local fh = io.open(filepath)
119 lang[v](langobject, fh:read'a')
120 fh:close()
121 else
122 log_warn('Hyphenation file %s not found', vv)
123 end
124 end
125 end
126 end
127
128 newloader_loaded_languages[langentry] = langobject
129
130 log_info('Language %s was not yet loaded; created with id %i',
131 langentry, newlangid)
132 return newlangid
133 else
134 log_info('Language %s not found in language.dat.lua', langentry)
135 return nohyphid
136 end
137 end
138 end
139
140 polyglossia.newloader = newloader
141 polyglossia.newloader_loaded_languages = newloader_loaded_languages