summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/polyglossia/polyglossia.lua
blob: e26e7dc6ec8b12e934a6d6f28e0c1ef78ffa1175 (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
require('luatex-hyphen')

local luatexhyphen = luatexhyphen
local byte = unicode.utf8.byte

local module_name = "polyglossia"
local polyglossia_module = {
    name          = module_name,
    version       = 1.3,
    date          = "2013/05/11",
    description   = "Polyglossia",
    author        = "Elie Roux",
    copyright     = "Elie Roux",
    license       = "CC0"
}

luatexbase.provides_module(polyglossia_module)

local log_info = function(message)
	luatexbase.module_info(module_name, message)
end
local log_warning = function(message)
	luatexbase.module_warning(module_name, message)
end

polyglossia = polyglossia or {}
local polyglossia = polyglossia

-- predefined l@nohyphenation or dummy new language
local nohyphid = luatexbase.registernumber'l@nohyphenation' or lang.id(lang.new())
-- key `nohyphenation` is for .sty file when possibly undefined l@nohyphenation
local newloader_loaded_languages = { nohyphenation = nohyphid }

local newloader_available_languages = dofile(kpse.find_file('language.dat.lua'))
-- Suggestion by Dohyun Kim on #129
local t = { }
for k, v in pairs(newloader_available_languages) do
    t[k] = v
    for _, vv in pairs(v.synonyms) do
        t[vv] = v
    end
end
newloader_available_languages = t

local function loadlang(lang, id)
  if luatexhyphen.lookupname(lang) then
    luatexhyphen.loadlanguage(lang, id) 
  end
end

local function select_language(lang, id)
  loadlang(lang, id)
  polyglossia.current_language = lang
end

local function set_default_language(lang, id)
  polyglossia.default_language = lang
end

local check_char

if luaotfload and luaotfload.aux and luaotfload.aux.font_has_glyph then
  local font_has_glyph = luaotfload.aux.font_has_glyph
  function check_char(chr)
    local codepoint = tonumber(chr)
    if not codepoint then codepoint = byte(chr) end
    if font_has_glyph(font.current(), codepoint) then
      tex.sprint('1')
    else
      tex.sprint('0')
    end
  end
else
  local ids = fonts.identifiers or fonts.ids or fonts.hashes.identifiers
  function check_char(chr) -- always in current font
      local otfdata = ids[font.current()].characters
      local codepoint = tonumber(chr)
      if not codepoint then codepoint = byte(chr) end
      if otfdata and otfdata[codepoint] then
          tex.print('1')
      else
          tex.print('0')
      end
  end
end

local function load_frpt()
    require('polyglossia-frpt')
end

local function load_tibt_eol()
    require('polyglossia-tibt')
end

-- LaTeX's language register is \count19
local lang_register = 19

-- New hyphenation pattern loader: use language.dat.lua directly and the language identifiers
local function newloader(langentry)
    local loaded_language = newloader_loaded_languages[langentry]
    if loaded_language then
        log_info('Language ' .. langentry .. ' already loaded; id is ' .. lang.id(loaded_language))
        -- texio.write_nl('term and log', 'Language ' .. langentry .. ' already loaded with patterns ' .. tostring(loaded_language) .. '; id is ' .. lang.id(loaded_language))
        -- texio.write_nl('term and log', 'Language ' .. langentry .. ' already loaded with patterns ' .. loaded_language['patterns'] .. '; id is ' .. lang.id(loaded_language))
        return lang.id(loaded_language)
    else
        local langdata = newloader_available_languages[langentry]
        if langdata and langdata['special'] == 'language0' then return 0 end

        if langdata then
            local s = "Language data for " .. langentry
            for k, v in pairs(langdata) do
				s = s .. "\n" .. k .. "\t" .. tostring(v)
            end

            --
            -- LaTeX's \newlanguage increases language register (count19),
            -- whereas LuaTeX's lang.new() increases its own language id.
            -- So when a user has declared, say, \newlanguage\lang@xyz, then
            -- these two numbers do not match each other. If we do not consider
            -- this possible situation, our newloader() function will
            -- unfortunately overwrite the language \lang@xyz.
            --
            -- Threfore here we will compare LaTeX's \newlanguage number with
            -- LuaTeX's lang.new() id and select the bigger one for our new
            -- language object. Also we will update LaTeX's language register
            -- by this new id, so that another possible \newlanguage should not
            -- overwrite our language object.
            --
            -- get next \newlanguage allocation number
            local langcnt = tex.count[lang_register] + 1
            -- get new lang object
            local langobject = lang.new()
            local langid = lang.id(langobject)
            -- get bigger one between \newlanguage and new lang obj id
            local maxlangid = math.max(langcnt, langid)
            -- set language register for possible \newlanguage
            tex.setcount('global', lang_register, maxlangid)
            -- get new lang object if needeed
            if langid ~= maxlangid then
              langobject = lang.new(maxlangid)
            end
			s = s .. "\npatterns: " .. langdata.patterns
			log_info(s)
            if langdata.patterns and langdata.patterns ~= '' then
                local pattfilepath = kpse.find_file(langdata.patterns)
                if pattfilepath then
                    local pattfile = io.open(pattfilepath)
                    lang.patterns(langobject, pattfile:read('*all'))
                    pattfile:close()
                end
            end
            if langdata.hyphenation and langdata.hyphenation ~= '' then
                local hyphfilepath = kpse.find_file(langdata.hyphenation)
                if hyphfilepath then
                    local hyphfile = io.open(hyphfilepath)
                    lang.hyphenation(langobject, hyphfile:read('*all'))
                    hyphfile:close()
                end
            end
            newloader_loaded_languages[langentry] = langobject

            log_info('Language ' .. langentry .. ' was not yet loaded; created with id ' .. lang.id(langobject))
            return lang.id(langobject)
        else
            log_warning('Language ' .. langentry .. ' not found in language.dat.lua')
            return nohyphid
        end
    end
end

polyglossia.loadlang = loadlang
polyglossia.select_language = select_language
polyglossia.set_default_language = set_default_language
polyglossia.check_char = check_char
polyglossia.load_frpt = load_frpt
polyglossia.load_tibt_eol = load_tibt_eol
polyglossia.newloader = newloader
polyglossia.newloader_loaded_languages = newloader_loaded_languages
-- global variables:
-- polyglossia.default_language
-- polyglossia.current_language