OSDN Git Service

28e21dd3c6f065dd1836b7b95e7ccc92a4f71b38
[luatex-ja/luatexja.git] / src / ltj-jfont.lua
1 --
2 -- ltj-jfont.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.jfont',
6   date = '2022-08-20',
7   description = 'Loader for Japanese fonts',
8 })
9
10 luatexja.load_module 'lotf_aux';  local ltju = luatexja.lotf_aux
11 luatexja.load_module 'base';      local ltjb = luatexja.base
12 luatexja.load_module 'charrange'; local ltjc = luatexja.charrange
13 luatexja.load_module 'rmlgbm';    local ltjr = luatexja.rmlgbm
14 luatexja.load_module 'direction'; local ltjd = luatexja.direction
15
16 local setfield = node.direct.setfield
17 local getid = node.direct.getid
18 local to_direct = node.direct.todirect
19
20 local node_new = node.direct.new
21 local node_free = node.direct.flush_node or node.direct.free
22 local get_attr = node.direct.get_attribute
23 local set_attr = node.direct.set_attribute
24 local round = tex.round
25 local font_getfont = font.getfont
26 local setkern = node.direct.setkern
27
28 local attr_icflag = luatexbase.attributes['ltj@icflag']
29 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
30 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
31 local id_glyph = node.id('glyph')
32 local id_kern = node.id('kern')
33 local cat_lp = luatexbase.catcodetables['latex-package']
34 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
35
36 luatexja.jfont = luatexja.jfont or {}
37 ------------------------------------------------------------------------
38 -- LOADING JFM
39 ------------------------------------------------------------------------
40
41 local metrics = {} -- this table stores all metric informations
42 local font_metric_table = ltju.font_metric_table -- [font number] -> jfm_name, jfm_var, size
43
44 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
45
46 local jfm_spec, jfm_name, jfm_var, jfm_ksp
47 local defjfm_res
48 local jfm_dir, is_def_jfont, vert_activated, auto_enable_vrt2
49
50 local function norm_val(a)
51    if (not a) or (a==0.) then
52       return nil
53    elseif a==true then
54       return 1
55    else
56       return a
57    end
58 end
59
60 local fastcopy=table.fastcopy
61 function luatexja.jfont.define_jfm(to)
62    local t = fastcopy(to)
63    local real_char -- Does current character class have the 'real' character?
64    if t.dir~=jfm_dir then
65       defjfm_res= nil; return
66    elseif type(t.zw)~='number' or type(t.zh)~='number' then
67       defjfm_res= nil; return
68    end
69    t.version = (type(t.version)=='number') and t.version or 1
70    t.char_type = {}; t.chars = {}
71    for i,v in pairs(t) do
72       if type(i) == 'number' then -- char_type
73          if not v.chars then
74             if i ~= 0 then defjfm_res= nil; return  end
75          else
76             for j,w in pairs(v.chars) do
77                if type(w) == 'number' and w~=-1 then
78                elseif type(w) == 'string' and utf.len(w)==1 then
79                   w = utf.byte(w)
80                elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
81                   w = utf.byte(utf.sub(w,1,1))
82                end
83                if not t.chars[w] then
84                   t.chars[w] = i
85                else
86                   defjfm_res= nil; return
87                end
88             end
89             v.chars = nil
90          end
91          if type(v.align)~='string' then
92             v.align = 'left' -- left
93          end
94          if type(v.width)~='number' then
95             v.width = nil
96          end
97          if type(v.height)~='number' then
98             v.height = (jfm_dir=='tate') and  0.0 or nil
99          end
100          if type(v.depth)~='number' then
101             v.depth =  (jfm_dir=='tate') and  0.0 or nil
102          end
103          if type(v.italic)~='number' then
104             v.italic = 0.0
105          end
106          if type(v.left)~='number' then
107             v.left = 0.0
108          end
109          if type(v.down)~='number' then
110             v.down = 0.0
111          end
112          v.round_threshold = ((t.version>=3)and(i==0)and(type(v.round_threshold)=='number'))
113             and v.round_threshold or nil
114          if t.version>=2 then
115             if v.end_stretch then defjfm_res= nil; return end
116             if v.end_shrink  then defjfm_res= nil; return end
117             if v.end_adjust then
118                if type(v.end_adjust)~='table' then
119                   v.end_adjust = nil
120                elseif #(v.end_adjust)==0 then
121                   v.end_adjust = nil
122                else
123                   table.sort(v.end_adjust)
124                end
125             end
126          else
127             v.end_adjust = nil
128             if v.end_stretch and v.end_stretch~=0.0 then
129                v.end_adjust = (v.end_adjust or {})
130                v.end_adjust[#(v.end_adjust)+1] = v.end_stretch
131             end
132             if v.end_shrink and v.end_ahrink~=0.0 then
133                v.end_adjust = (v.end_adjust or {})
134                v.end_adjust[#(v.end_adjust)+1] = -v.end_shrink
135             end
136             if v.end_adjust then v.end_adjust[#(v.end_adjust)+1] = 0.0 end
137          end
138          v.kern = v.kern or {}; v.glue = v.glue or {}
139          for j,x in pairs(v.glue) do
140             if v.kern[j] then defjfm_res= nil; return end
141             x.ratio, x[5] = (x.ratio or (x[5] and 0.5*(1+x[5]) or 0.5)), nil
142             do
143                local xp
144                xp, x[4] = (x.priority or x[4]), nil
145                if type(xp)=='table' and t.version>=2 then
146                   if type(xp[1])~='number' or xp[1]<-4 or xp[1]>3 then defjfm_res=nil end  -- stretch
147                   if type(xp[2])~='number' or xp[2]<-4 or xp[2]>3 then defjfm_res=nil end  -- shrink
148                   xp = (xp[1]+4)*8+(xp[2]+4)
149                elseif xp and type(xp)~='number' then
150                   defjfm_res = nil
151                else
152                   xp = (xp or 0)*9+36
153                   if xp<0 or xp>=64 then defjfm_res=nil end
154                end
155                x.priority = xp
156             end
157             x.kanjiskip_natural = norm_val(x.kanjiskip_natural)
158             x.kanjiskip_stretch = norm_val(x.kanjiskip_stretch)
159             x.kanjiskip_shrink = norm_val(x.kanjiskip_shrink)
160          end
161          for j,x in pairs(v.kern) do
162             if type(x)=='number' then
163                v.kern[j] = {x, 0.5}
164             elseif type(x)=='table' then
165                v.kern[j] = { x[1], (x.ratio or (x[2] and 0.5*(1+x[2]) or 0.5)) }
166             end
167          end
168          t.char_type[i] = v
169          t[i] = nil
170       end
171    end
172    if t.version<3 then
173       -- In version 3, 'jcharbdd' is divided into
174       -- 'alchar': ALchar (or math boundary)
175       -- 'nox_alchar': ALchar (or math boundary), where xkanjiskip won't inserted
176       -- 'glue': glue/kern, 'jcharbdd': other cases (和文B, rule, ...)
177       t.chars.alchar = t.chars.jcharbdd
178       t.chars.nox_alchar = t.chars.jcharbdd
179       t.chars.glue = t.chars.jcharbdd
180    end
181    t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_spec, luatexja.jfont.jfm_feature)
182    t.size_cache = {}
183    defjfm_res = t
184 end
185
186 local update_jfm_cache
187 do
188    local floor = math.floor
189    local function myround(a) return floor(a+0.5) end
190    local mult_ignore_key = {
191      round_threshold=true, kanjiskip_natural=true, kanjiskip_stretch=true, kanjiskip_shrink=true,
192      ratio=true, priority=true
193    }
194    local function mult_table(old,scale) -- modified from table.fastcopy
195       if old then
196          local new = { }
197          for k,v in next, old do
198             if type(v) == "table" then
199                new[k] = mult_table(v,scale)
200             elseif type(v) == "number" and not mult_ignore_key[k] then
201                new[k] = myround(v*scale)
202             else
203                new[k] = v
204             end
205          end
206          return new
207       else return nil end
208    end
209    local size_cache_num = 1
210    update_jfm_cache = function (j,sz)
211       if metrics[j].size_cache[sz] then return metrics[j].size_cache[sz].index end
212       local t = {}
213       metrics[j].size_cache[sz] = t
214       t.chars = metrics[j].chars
215       t.char_type = mult_table(metrics[j].char_type, sz)
216       for i,v in pairs(t.char_type) do
217          v.align = (v.align=='left') and 0 or
218             ((v.align=='right') and 1 or 0.5)
219          if type(i) == 'number' then -- char_type
220             for k,w in pairs(v.glue) do
221                v[k] = {
222                   nil, w[1], w[2], w[3],
223                   ratio=w.ratio,
224                   priority=FROM_JFM + w.priority,
225                   kanjiskip_natural = w.kanjiskip_natural,
226                   kanjiskip_stretch = w.kanjiskip_stretch,
227                   kanjiskip_shrink =  w.kanjiskip_shrink,
228                   round_threshold = w.round_threshold,
229                   }
230             end
231             for k,w in pairs(v.kern) do
232                v[k] = {w[1], ratio=w[2]/sz}
233             end
234          end
235          v.glue, v.kern = nil, nil
236       end
237       t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
238       t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
239       t.zw = round(metrics[j].zw*sz)
240       t.zh = round(metrics[j].zh*sz)
241       t.size = sz
242       size_cache_num = size_cache_num + 1
243       t.index = size_cache_num
244       return size_cache_num
245    end
246 end
247
248 luatexbase.create_callback("luatexja.find_char_class", "data",
249                            function (arg, fmtable, char)
250                               return 0
251                           end)
252 local find_char_class
253 do
254    local start_time_measure = ltjb.start_time_measure
255    local stop_time_measure = ltjb.stop_time_measure
256    local fcc_temp = { chars_cbcache = {} }
257    setmetatable(
258       fcc_temp.chars_cbcache,
259       {
260          __index = function () return 0 end,
261       })
262    function find_char_class(c,m)
263       -- c: character code, m:
264       local r = (m or fcc_temp).chars_cbcache[c]
265       if not r then
266          r = m.chars[c] or
267             luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
268          m.chars_cbcache[c or 0] = r
269       end
270       return r
271    end
272 end
273
274
275 ------------------------------------------------------------------------
276 -- LOADING JAPANESE FONTS
277 ------------------------------------------------------------------------
278
279 local load_jfont_metric, check_callback_order
280 local font_extra_info = {} -- defined later
281 do
282    local global_flag -- true if \globaljfont, false if \jfont
283    load_jfont_metric = function()
284      if jfm_name=='' then
285          ltjb.package_error('luatexja', 'no JFM specified',
286                             'To load and define a Japanese font, a JFM must be specified.'..
287                             "The JFM 'ujis' will be  used for now.")
288          jfm_name, jfm_spec = 'ujis', 'ujis'
289       end
290       for j,v in ipairs(metrics) do if v.name==jfm_spec then return j end end
291       luatexja.load_lua('jfm-' .. jfm_name .. '.lua')
292       if defjfm_res then
293          defjfm_res.name = jfm_spec; table.insert(metrics, defjfm_res)
294          return #metrics
295       else
296          return nil
297       end
298    end
299
300 -- EXT
301    local utfbyte = utf.byte
302    function luatexja.jfont.jfontdefX(g, dir)
303       jfm_dir, is_def_jfont = dir, true
304       global_flag = g and '\\global' or ''
305       tex.sprint(cat_lp, '\\expandafter\\font\\ltj@temp')
306    end
307
308    luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
309
310 -- EXT
311    local fastcopy=table.fastcopy
312    function luatexja.jfont.jfontdefY()
313       local j = load_jfont_metric(jfm_dir)
314       local fn = token.get_next().mode;  local f = font_getfont(fn)
315       if not j then
316          ltjb.package_error('luatexja', "bad JFM `" .. jfm_name .. "'",
317                             'The JFM file you specified is not valid JFM file.\n'..
318                                'So defining Japanese font is cancelled.')
319          tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\ltj@temp\\relax')
320          return
321       end
322       if not f then return end
323       update_jfm_cache(j, f.size); check_callback_order()
324       local sz = metrics[j].size_cache[f.size]
325       local fmtable = { jfm = j, size = f.size, var = jfm_var,
326                         with_kanjiskip = jfm_ksp,
327                         zw = sz.zw, zh = sz.zh,
328                         chars = sz.chars, char_type = sz.char_type,
329                         kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
330                         chars_cbcache = {},
331                         vert_activated = vert_activated,
332                         rotation = fastcopy(font_extra_info[fn].rotation),
333       }
334       if auto_enable_vrt2 then
335          local vert_name = ltju.exist_feature(fn, 'vrt2') and 'vrt2' or 'vert'
336          local rot = fmtable.rotation
337          ltju.enable_feature(fn, vert_name)
338          ltju.loop_over_feat(f, {[vert_name]=true}, function (i,k) rot[i] = nil end)
339       end
340
341       fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
342       font_metric_table[fn]=fmtable
343       tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\ltj@temp',
344         '{\\ltj@cur'.. (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
345       jfm_spec = nil
346    end
347 end
348
349 do
350     local ltb = luatexbase
351     local to_be_checked = { 'pre_linebreak_filter', 'hpack_filter' }
352     check_callback_order = function()
353         for i,n in pairs(to_be_checked) do
354             local lotf_cb = ltb.priority_in_callback(n, 'luaotfload.letterspace')
355             local ltj_cb  = ltb.priority_in_callback(n, 'ltj.main')
356             if lotf_cb then
357                 to_be_checked[i]=nil
358                 if ltj_cb<lotf_cb then
359                     local f = ltb.remove_from_callback(n,'luaotfload.letterspace')
360                     ltb.add_to_callback(n, f, 'luaotfload.letterspace',
361                         ltb.priority_in_callback(n, 'luaotfload.node_processor') + 1)
362                 end
363             end
364         end
365     end
366 end
367
368 do
369    local get_dir_count = ltjd.get_dir_count
370    local dir_tate = luatexja.dir_table.dir_tate
371    local tex_get_attr = tex.getattribute
372    -- PUBLIC function
373    function luatexja.jfont.get_zw()
374       local a = font_metric_table[
375          tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
376       return a and a.zw or 0
377    end
378    function luatexja.jfont.get_zh()
379       local a = font_metric_table[
380          tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
381       return a and a.zw or 0
382    end
383 end
384
385 do
386     local P, R, S, V        = lpeg.P, lpeg.R, lpeg.S, lpeg.V
387     local lpegmatch         = lpeg.match
388     local C, Cc, Cf, Ct, Cg = lpeg.C, lpeg.Cc, lpeg.Cf, lpeg.Ct, lpeg.Cg
389     local Cs, B             = lpeg.Cs, lpeg.B
390     local spacing           = S" \t\v"
391     local ws                = spacing^0
392     local slash             = P'/'
393     local semicolon         = P';'
394     local comma             = P','
395     local equals            = P'='
396     local jf_field_char     = 1 - S'/{};,= \t\v'
397     local jf_field          = C(jf_field_char^1)
398     local jf_assignment     = jf_field * ws * equals * ws * jf_field
399     local jf_switch         = P'-' * jf_field * Cc(false) + P'+'^-1 * jf_field * Cc(true)
400     local jf_feature_expr   = Cg(jf_assignment + jf_switch) * ws * comma^0 * ws
401     local jf_feature_list   = ws * ( P'{' * ws * jf_feature_expr^0 * P'}' + jf_feature_expr^0 )
402     local jf_list           = C((1-slash)^1) * (slash * Cf(Ct'' * jf_feature_list, rawset))^-1
403     local jf_value          = (1 - semicolon)^1
404     local function rem(name,value)
405       if name=='jfm' then
406         local flag, t; jfm_name, t = lpegmatch(jf_list, value)
407         if type(t)=='table' then
408           for i,v in pairs(t) do
409             flag=true
410             if v=='true' then t[i]=true elseif v=='false' then t[i]=false end
411             end
412         end
413         luatexja.jfont.jfm_feature = flag and t
414       elseif name=='jfmvar' then jfm_var = value end
415       return ''
416     end
417     local jf_remainder      = Cs( ( ( B(S':;') *
418       C(P'jfm' * P'var'^-1) * ws * equals * ws * C(jf_value) * semicolon^-1 ) / rem +1 )^0 )
419
420    local parser=luaotfload.parsers.font_request
421    function is_feature_specified(s,fname)
422      local t = lpegmatch(parser,s); return t and t.features and t.features[fname]
423    end
424    -- extract jfm_name, jfm_spec and jfm_var
425    -- normalize position of 'jfm=' and 'jfmvar=' keys
426    local function extract_jfm_spec(name)
427       name = (name:match '^{(.*)}$') or (name:match '^"(.*)"$') or name
428       jfm_spec, jfm_var = '', ''
429       local tmp, index = name:sub(1, 5), 1
430       if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
431          index = 6
432       end
433       name = lpegmatch(jf_remainder, name)
434       if jfm_name~='' then
435          if luatexja.jfont.jfm_feature then
436             local l, t2 = name:sub(-1), {}
437             for i,v in pairs(luatexja.jfont.jfm_feature) do
438                -- print(i,type(v),v)
439                t2[#t2+1] = (v==true) and i
440                   or ((v==false) and ('-'..i) or (i..'='..tostring(v)))
441             end
442             table.sort(t2); jfm_spec = jfm_name .. '/{' .. table.concat(t2, ',') .. '}'
443          else
444             jfm_spec = jfm_name
445          end
446          l = name:sub(-1)
447          name = name .. ((l==':' or l==';') and '' or ';') .. 'jfm=' .. jfm_spec
448          if jfm_var~='' then
449             name = name .. ';jfmvar=' .. jfm_var
450          end
451          -- print('NN>', name)
452       end
453       jfm_ksp = (is_feature_specified(name,'ltjksp')~=false)
454       if jfm_dir == 'tate' then
455          vert_activated = (is_feature_specified(name,'vert')~=false) and (is_feature_specified(name,'vrt2')~=false)
456          auto_enable_vrt2
457            = (is_feature_specified(name,'vert')==nil) and (is_feature_specified(name,'vrt2')==nil)
458       else
459          vert_activated, auto_enable_vrt2 = nil, nil
460       end
461       return name
462    end
463
464    -- define_font callback
465    local otfl_fdr
466    local ltjr_font_callback = ltjr.font_callback
467    function luatexja.font_callback(name, size, id)
468       local new_name = is_def_jfont and extract_jfm_spec(name) or name
469       is_def_jfont = false
470       local res =  ltjr_font_callback(new_name, size, id, otfl_fdr)
471       luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
472       -- this callback processes variation selector, so we execute it always
473       return res
474    end
475    luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
476    otfl_fdr= luatexbase.remove_from_callback('define_font', 'luaotfload.define_font')
477    luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
478
479    local match, sp, scan_arg = string.match, tex.sp, token.scan_argument
480    local function load_jfmonly()
481       local spec, size = match(scan_arg(), '(.+)%s+at%s*([%.%w]*)')
482       local dir = scan_arg()
483       size = sp(size); extract_jfm_spec(spec)
484       jfm_dir = dir
485       local i = load_jfont_metric()
486       local j = -update_jfm_cache(i, size)
487       font_metric_table[j]=metrics[i].size_cache[size]
488       tex.sprint(cat_lp, '\\ltj@cur' .. (dir=='yoko' and 'j' or 't') .. 'fnt' .. tostring(j) .. '\\relax')
489    end
490    luatexja.jfont.load_jfmonly = load_jfmonly
491 end
492
493 ------------------------------------------------------------------------
494 -- LATEX INTERFACE
495 ------------------------------------------------------------------------
496 do
497    local scan_arg = token.scan_argument
498    -- these function are called from ltj-latex.sty
499    local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
500    function luatexja.jfont.add_fenc_list() fenc_list[scan_arg()] = 'true ' end
501    function luatexja.jfont.add_kyenc_list() kyenc_list[scan_arg()] = 'true ' end
502    function luatexja.jfont.add_ktenc_list() ktenc_list[scan_arg()] = 'true ' end
503    function luatexja.jfont.is_kyenc()
504       tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[scan_arg()] or 'false '))
505    end
506    function luatexja.jfont.is_ktenc()
507       tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[scan_arg()] or 'false '))
508    end
509    function luatexja.jfont.is_kenc()
510       local enc = scan_arg()
511       tex.sprint(cat_lp, '\\let\\ifin@\\if'
512                  .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
513    end
514
515    local kfam_list, Nkfam_list = {}, {}
516    function luatexja.jfont.add_kfam()
517       kfam_list[scan_arg()]=true
518    end
519    function luatexja.jfont.search_kfam()
520       local fam = scan_arg()
521       local use_fd = (scan_arg() =='true')
522       if kfam_list[fam] then
523          tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
524       elseif Nkfam_list[fam] then
525          tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
526       elseif use_fd then
527          for i,_ in pairs(kyenc_list) do
528             if kpse.find_file(string.lower(i)..fam..'.fd') then
529                tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
530             end
531          end
532          for i,_ in pairs(ktenc_list) do
533             if kpse.find_file(string.lower(i)..fam..'.fd') then
534                tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
535             end
536          end
537          Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
538       else
539          tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
540       end
541    end
542    local ffam_list, Nffam_list = {}, {}
543    function luatexja.jfont.is_ffam()
544       tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[scan_arg()] or 'false '))
545    end
546    function luatexja.jfont.add_ffam()
547       ffam_list[scan_arg()]='true '
548    end
549    function luatexja.jfont.search_ffam_declared()
550      local s = ''
551      for i,_ in pairs(fenc_list) do
552         s = s .. '\\cdp@elt{' .. i .. '}'
553      end
554      tex.sprint(cat_lp, s)
555    end
556    function luatexja.jfont.search_ffam_fd()
557       local fam = scan_arg()
558       if Nffam_list[fam] then
559          tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
560       else
561          for i,_ in pairs(fenc_list) do
562             if kpse.find_file(string.lower(i)..fam..'.fd') then
563                tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
564             end
565          end
566          Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
567       end
568    end
569
570 end
571 ------------------------------------------------------------------------
572 -- ALTERNATE FONTS
573 ------------------------------------------------------------------------
574 local alt_font_table = {}
575 local attr_curaltfnt = {}
576 local ucs_out = 0x110000
577
578 ------ for TeX interface
579 -- EXT
580 function luatexja.jfont.set_alt_font(b,e,ind,bfnt)
581    -- ind: 新フォント, bfnt: 基底フォント
582    if b>e then b, e = e, b end
583    if b*e<=0 then
584       ltjb.package_error('luatexja',
585                         'bad character range ([' .. b .. ',' .. e .. ']). ' ..
586                            'I take the intersection with [0x80, 0x10ffff].')
587       b, e = math.max(0x80,b),math.min(ucs_out-1,e)
588    elseif e<0 then -- b<e<0
589       -- do nothing
590    elseif b<0x80 or e>=ucs_out then
591       ltjb.package_warning('luatexja',
592                            'bad character range ([' .. b .. ',' .. e .. ']). ' ..
593                               'I take the intersection with [0x80, 0x10ffff].')
594       b, e = math.max(0x80,b), math.min(ucs_out-1,e)
595    end
596    if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
597    local t = alt_font_table[bfnt]
598    local ac = font_getfont(ind).characters
599    if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
600    if e>=0 then -- character range
601       for i=b, e do
602          if ac[i]then  t[i]=ind end
603       end
604    else
605       b, e = -e, -b
606       local tx = font_metric_table[bfnt].chars
607       for i,v in pairs(tx) do
608          if b<=v and v<=e and ac[i] then t[i]=ind end
609       end
610    end
611 end
612
613 -- EXT
614 function luatexja.jfont.clear_alt_font(bfnt)
615    if alt_font_table[bfnt] then
616       local t = alt_font_table[bfnt]
617       for i,_ in pairs(t) do t[i]=nil; end
618    end
619 end
620
621 ------ used in ltjp.suppress_hyphenate_ja callback
622 function luatexja.jfont.replace_altfont(pf, pc)
623    local a = alt_font_table[pf]
624    return a and a[pc] or pf
625 end
626
627 ------ for LaTeX interface
628
629 local alt_font_table_latex = {}
630
631 -- EXT
632 function luatexja.jfont.clear_alt_font_latex(bbase)
633    local t = alt_font_table_latex[bbase]
634    if t then
635       for j,v in pairs(t) do t[j] = nil end
636    end
637 end
638
639 -- EXT
640 function luatexja.jfont.set_alt_font_latex(b,e,ind,bbase)
641    -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
642    if b>e then b, e = e, b end
643    if b*e<=0 then
644       ltjb.package_error('luatexja',
645                         'bad character range ([' .. b .. ',' .. e .. ']). ' ..
646                            'I take the intersection with [0x80, 0x10ffff].')
647       b, e = math.max(0x80,b),math.min(ucs_out-1,e)
648    elseif e<0 then -- b<e<0
649       -- do nothing
650    elseif b<0x80 or e>=ucs_out then
651       ltjb.package_warning('luatexja',
652                            'bad character range ([' .. b .. ',' .. e .. ']). ' ..
653                               'I take the intersection with [0x80, 0x10ffff].')
654       b, e = math.max(0x80,b), math.min(ucs_out-1,e)
655    end
656
657    if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
658    local t = alt_font_table_latex[bbase]
659    if not t[ind] then t[ind] = {} end
660    for i=b, e do
661       for j,v in pairs(t) do
662          if v[i] then -- remove old entry
663             if j~=ind then v[i]=nil end; break
664          end
665       end
666       t[ind][i]=true
667    end
668    -- remove the empty tables
669    for j,v in pairs(t) do
670       local flag_clear = true
671       for k,_ in pairs(v) do flag_clear = false; break end
672       if flag_clear then t[j]=nil end
673    end
674    if ind==bbase  then t[bbase] = nil end
675 end
676
677 -- ここから先は 新 \selectfont の内部でしか実行されない
678 do
679    local scan_arg = token.scan_argument
680    local alt_font_base, alt_font_base_num
681    local aftl_base
682    -- EXT
683    function luatexja.jfont.does_alt_set()
684       aftl_base = alt_font_table_latex[scan_arg()]
685       tex.sprint(cat_lp, aftl_base and '\\@firstofone' or '\\@gobble')
686    end
687    -- EXT
688    function luatexja.jfont.print_aftl_address()
689       return ';ltjaltfont' .. tostring(aftl_base):sub(8)
690    end
691
692 -- EXT
693    function luatexja.jfont.output_alt_font_cmd(dir, bbase)
694       alt_font_base = bbase
695       alt_font_base_num = tex.getattribute((dir == 't') and attr_curtfnt or attr_curjfnt)
696       local t = alt_font_table[alt_font_base_num]
697       if t then
698          for i,_ in pairs(t) do t[i]=nil end
699       end
700       t = alt_font_table_latex[bbase]
701       if t then
702          for i,_ in pairs(t) do
703             tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
704          end
705       end
706    end
707
708 -- EXT
709    function luatexja.jfont.pickup_alt_font_a(size_str)
710       local t = alt_font_table_latex[alt_font_base]
711       if t then
712          for i,v in pairs(t) do
713             tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
714                           .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
715          end
716       end
717    end
718
719    local function pickup_alt_font_class(class, afnt_num, afnt_chars)
720       local t  = alt_font_table[alt_font_base_num]
721       local tx = font_metric_table[alt_font_base_num].chars
722       for i,v in pairs(tx) do
723          if v==class and afnt_chars[i] then t[i]=afnt_num end
724       end
725    end
726
727 -- EXT
728    function luatexja.jfont.pickup_alt_font_b(afnt_num, afnt_base)
729       local t = alt_font_table[alt_font_base_num]
730       local ac = font_getfont(afnt_num).characters
731       if not t then t = {}; alt_font_table[alt_font_base_num] = t end
732       for i,v in pairs(alt_font_table_latex[alt_font_base]) do
733          if i == afnt_base then
734             for j,_ in pairs(v) do
735                if j>=0 then
736                   if ac[j] then t[j]=afnt_num end
737                else  -- -n (n>=1) means that the character class n,
738                      -- which is defined in the JFM
739                   pickup_alt_font_class(-j, afnt_num, ac)
740                end
741             end
742             return
743          end
744       end
745    end
746
747 end
748 ------------------------------------------------------------------------
749 -- 終了時に各種ノードを破棄
750 ------------------------------------------------------------------------
751 do
752    function luatexja.jfont.cleanup_size_cache()
753       --local gs, ke = 0, 0
754       for _,n in pairs(metrics) do
755          for i,t in pairs(n.size_cache) do
756             for _,v in pairs(t.char_type) do
757                for k,w in pairs(v) do
758                   if type(k)=='number' then
759                      --if w[1] then gs = gs + 1 else ke = ke + 1 end
760                      if w[1] then node_free(w[1]) end
761                   end
762                end
763             end
764             n.size_cache[i]=nil
765          end
766       end
767    end
768 end
769
770 ------------------------------------------------------------------------
771 -- 追加のフォント情報
772 ------------------------------------------------------------------------
773 luatexja.jfont.font_extra_info = font_extra_info -- key: fontnumber
774 local font_extra_basename = {} -- key: basename
775
776 local rotate_exception = {
777   [0xFF1A]= { ['zht'] = true, },
778   [0xFF1B]= { ['zht'] = true, },
779 }
780 luatexja.jfont.rotate_exception = rotate_exception
781 local list_rotate_glyphs
782 do
783   local ceil = math.ceil
784   -- output of function_uax50.lua
785   -- UAX#50 for Unicode  14.0.0
786   -- t[0] = true
787   local t={ 0, 167, 168, 169, 170, 174, 175, 177, 178, 188, 191, 215, 216, 247, 248, 746, 748, 888, 890, 896, 900, 907, 908, 909, 910, 930, 931, 1328, 1329, 1367, 1369, 1419, 1421, 1424, 1425, 1480, 1488, 1515, 1519, 1525, 1536, 1806, 1807, 1867, 1869, 1970, 1984, 2043, 2045, 2094, 2096, 2111, 2112, 2140, 2142, 2143, 2144, 2155, 2160, 2191, 2192, 2194, 2200, 2436, 2437, 2445, 2447, 2449, 2451, 2473, 2474, 2481, 2482, 2483, 2486, 2490, 2492, 2501, 2503, 2505, 2507, 2511, 2519, 2520, 2524, 2526, 2527, 2532, 2534, 2559, 2561, 2564, 2565, 2571, 2575, 2577, 2579, 2601, 2602, 2609, 2610, 2612, 2613, 2615, 2616, 2618, 2620, 2621, 2622, 2627, 2631, 2633, 2635, 2638, 2641, 2642, 2649, 2653, 2654, 2655, 2662, 2679, 2689, 2692, 2693, 2702, 2703, 2706, 2707, 2729, 2730, 2737, 2738, 2740, 2741, 2746, 2748, 2758, 2759, 2762, 2763, 2766, 2768, 2769, 2784, 2788, 2790, 2802, 2809, 2816, 2817, 2820, 2821, 2829, 2831, 2833, 2835, 2857, 2858, 2865, 2866, 2868, 2869, 2874, 2876, 2885, 2887, 2889, 2891, 2894, 2901, 2904, 2908, 2910, 2911, 2916, 2918, 2936, 2946, 2948, 2949, 2955, 2958, 2961, 2962, 2966, 2969, 2971, 2972, 2973, 2974, 2976, 2979, 2981, 2984, 2987, 2990, 3002, 3006, 3011, 3014, 3017, 3018, 3022, 3024, 3025, 3031, 3032, 3046, 3067, 3072, 3085, 3086, 3089, 3090, 3113, 3114, 3130, 3132, 3141, 3142, 3145, 3146, 3150, 3157, 3159, 3160, 3163, 3165, 3166, 3168, 3172, 3174, 3184, 3191, 3213, 3214, 3217, 3218, 3241, 3242, 3252, 3253, 3258, 3260, 3269, 3270, 3273, 3274, 3278, 3285, 3287, 3293, 3295, 3296, 3300, 3302, 3312, 3313, 3315, 3328, 3341, 3342, 3345, 3346, 3397, 3398, 3401, 3402, 3408, 3412, 3428, 3430, 3456, 3457, 3460, 3461, 3479, 3482, 3506, 3507, 3516, 3517, 3518, 3520, 3527, 3530, 3531, 3535, 3541, 3542, 3543, 3544, 3552, 3558, 3568, 3570, 3573, 3585, 3643, 3647, 3676, 3713, 3715, 3716, 3717, 3718, 3723, 3724, 3748, 3749, 3750, 3751, 3774, 3776, 3781, 3782, 3783, 3784, 3790, 3792, 3802, 3804, 3808, 3840, 3912, 3913, 3949, 3953, 3992, 3993, 4029, 4030, 4045, 4046, 4059, 4096, 4294, 4295, 4296, 4301, 4302, 4304, 4352, 4608, 4681, 4682, 4686, 4688, 4695, 4696, 4697, 4698, 4702, 4704, 4745, 4746, 4750, 4752, 4785, 4786, 4790, 4792, 4799, 4800, 4801, 4802, 4806, 4808, 4823, 4824, 4881, 4882, 4886, 4888, 4955, 4957, 4989, 4992, 5018, 5024, 5110, 5112, 5118, 5120, 5121, 5760, 5789, 5792, 5881, 5888, 5910, 5919, 5943, 5952, 5972, 5984, 5997, 5998, 6001, 6002, 6004, 6016, 6110, 6112, 6122, 6128, 6138, 6144, 6170, 6176, 6265, 6272, 6315, 6400, 6431, 6432, 6444, 6448, 6460, 6464, 6465, 6468, 6510, 6512, 6517, 6528, 6572, 6576, 6602, 6608, 6619, 6622, 6684, 6686, 6751, 6752, 6781, 6783, 6794, 6800, 6810, 6816, 6830, 6832, 6863, 6912, 6989, 6992, 7039, 7040, 7156, 7164, 7224, 7227, 7242, 7245, 7305, 7312, 7355, 7357, 7368, 7376, 7419, 7424, 7958, 7960, 7966, 7968, 8006, 8008, 8014, 8016, 8024, 8025, 8026, 8027, 8028, 8029, 8030, 8031, 8062, 8064, 8117, 8118, 8133, 8134, 8148, 8150, 8156, 8157, 8176, 8178, 8181, 8182, 8191, 8192, 8214, 8215, 8224, 8226, 8240, 8242, 8251, 8253, 8258, 8259, 8263, 8266, 8273, 8274, 8293, 8294, 8306, 8308, 8335, 8336, 8349, 8352, 8385, 8400, 8413, 8417, 8418, 8421, 8433, 8450, 8451, 8458, 8463, 8464, 8467, 8469, 8470, 8472, 8478, 8484, 8485, 8486, 8487, 8488, 8489, 8490, 8494, 8495, 8501, 8512, 8517, 8523, 8524, 8526, 8527, 8586, 8588, 8592, 8734, 8735, 8756, 8758, 8960, 8968, 8972, 8992, 8996, 9001, 9003, 9004, 9085, 9115, 9150, 9166, 9167, 9168, 9169, 9180, 9186, 9251, 9252, 9472, 9632, 9754, 9760, 10088, 10102, 10132, 11026, 11056, 11088, 11098, 11124, 11126, 11158, 11160, 11192, 11218, 11219, 11244, 11248, 11264, 11508, 11513, 11558, 11559, 11560, 11565, 11566, 11568, 11624, 11631, 11633, 11647, 11671, 11680, 11687, 11688, 11695, 11696, 11703, 11704, 11711, 11712, 11719, 11720, 11727, 11728, 11735, 11736, 11743, 11744, 11856, 11858, 11870, 12296, 12306, 12308, 12320, 12336, 12337, 12448, 12449, 12540, 12541, 42192, 42540, 42560, 42744, 42752, 42955, 42960, 42962, 42963, 42964, 42965, 42970, 42994, 43053, 43056, 43066, 43072, 43128, 43136, 43206, 43214, 43226, 43232, 43348, 43359, 43360, 43392, 43470, 43471, 43482, 43486, 43519, 43520, 43575, 43584, 43598, 43600, 43610, 43612, 43715, 43739, 43767, 43777, 43783, 43785, 43791, 43793, 43799, 43808, 43815, 43816, 43823, 43824, 43884, 43888, 44014, 44016, 44026, 55296, 57344, 64256, 64263, 64275, 64280, 64285, 64311, 64312, 64317, 64318, 64319, 64320, 64322, 64323, 64325, 64326, 64451, 64467, 64912, 64914, 64968, 64975, 64976, 65008, 65040, 65056, 65072, 65097, 65104, 65112, 65119, 65123, 65127, 65136, 65141, 65142, 65277, 65279, 65280, 65288, 65290, 65293, 65294, 65306, 65311, 65339, 65340, 65341, 65342, 65343, 65344, 65371, 65471, 65474, 65480, 65482, 65488, 65490, 65496, 65498, 65501, 65507, 65508, 65512, 65519, 65529, 65532, 65536, 65548, 65549, 65575, 65576, 65595, 65596, 65598, 65599, 65614, 65616, 65630, 65664, 65787, 65792, 65795, 65799, 65844, 65847, 65935, 65936, 65949, 65952, 65953, 66000, 66046, 66176, 66205, 66208, 66257, 66272, 66300, 66304, 66340, 66349, 66379, 66384, 66427, 66432, 66462, 66463, 66500, 66504, 66518, 66560, 66718, 66720, 66730, 66736, 66772, 66776, 66812, 66816, 66856, 66864, 66916, 66927, 66939, 66940, 66955, 66956, 66963, 66964, 66966, 66967, 66978, 66979, 66994, 66995, 67002, 67003, 67005, 67072, 67383, 67392, 67414, 67424, 67432, 67456, 67462, 67463, 67505, 67506, 67515, 67584, 67590, 67592, 67593, 67594, 67638, 67639, 67641, 67644, 67645, 67647, 67670, 67671, 67743, 67751, 67760, 67808, 67827, 67828, 67830, 67835, 67868, 67871, 67898, 67903, 67904, 68000, 68024, 68028, 68048, 68050, 68100, 68101, 68103, 68108, 68116, 68117, 68120, 68121, 68150, 68152, 68155, 68159, 68169, 68176, 68185, 68192, 68256, 68288, 68327, 68331, 68343, 68352, 68406, 68409, 68438, 68440, 68467, 68472, 68498, 68505, 68509, 68521, 68528, 68608, 68681, 68736, 68787, 68800, 68851, 68858, 68904, 68912, 68922, 69216, 69247, 69248, 69290, 69291, 69294, 69296, 69298, 69376, 69416, 69424, 69466, 69488, 69514, 69552, 69580, 69600, 69623, 69632, 69710, 69714, 69750, 69759, 69827, 69837, 69838, 69840, 69865, 69872, 69882, 69888, 69941, 69942, 69960, 69968, 70007, 70016, 70112, 70113, 70133, 70144, 70162, 70163, 70207, 70272, 70279, 70280, 70281, 70282, 70286, 70287, 70302, 70303, 70314, 70320, 70379, 70384, 70394, 70400, 70404, 70405, 70413, 70415, 70417, 70419, 70441, 70442, 70449, 70450, 70452, 70453, 70458, 70459, 70469, 70471, 70473, 70475, 70478, 70480, 70481, 70487, 70488, 70493, 70500, 70502, 70509, 70512, 70517, 70656, 70748, 70749, 70754, 70784, 70856, 70864, 70874, 71168, 71237, 71248, 71258, 71264, 71277, 71296, 71354, 71360, 71370, 71424, 71451, 71453, 71468, 71472, 71495, 71680, 71740, 71840, 71923, 71935, 71943, 71945, 71946, 71948, 71956, 71957, 71959, 71960, 71990, 71991, 71993, 71995, 72007, 72016, 72026, 72096, 72104, 72106, 72152, 72154, 72165, 72384, 72441, 72704, 72713, 72714, 72759, 72760, 72774, 72784, 72813, 72816, 72848, 72850, 72872, 72873, 72887, 72960, 72967, 72968, 72970, 72971, 73015, 73018, 73019, 73020, 73022, 73023, 73032, 73040, 73050, 73056, 73062, 73063, 73065, 73066, 73103, 73104, 73106, 73107, 73113, 73120, 73130, 73440, 73465, 73648, 73649, 73664, 73714, 73727, 74650, 74752, 74863, 74864, 74869, 74880, 75076, 77712, 77811, 92160, 92729, 92736, 92767, 92768, 92778, 92782, 92863, 92864, 92874, 92880, 92910, 92912, 92918, 92928, 92998, 93008, 93018, 93019, 93026, 93027, 93048, 93053, 93072, 93760, 93851, 93952, 94027, 94031, 94088, 94095, 94112, 113664, 113771, 113776, 113789, 113792, 113801, 113808, 113818, 113820, 113828, 119296, 119366, 119808, 119893, 119894, 119965, 119966, 119968, 119970, 119971, 119973, 119975, 119977, 119981, 119982, 119994, 119995, 119996, 119997, 120004, 120005, 120070, 120071, 120075, 120077, 120085, 120086, 120093, 120094, 120122, 120123, 120127, 120128, 120133, 120134, 120135, 120138, 120145, 120146, 120486, 120488, 120780, 120782, 120832, 122624, 122655, 122880, 122887, 122888, 122905, 122907, 122914, 122915, 122917, 122918, 122923, 123136, 123181, 123184, 123198, 123200, 123210, 123214, 123216, 123536, 123567, 123584, 123642, 123647, 123648, 124896, 124903, 124904, 124908, 124909, 124911, 124912, 124927, 124928, 125125, 125127, 125143, 125184, 125260, 125264, 125274, 125278, 125280, 126065, 126133, 126209, 126270, 126464, 126468, 126469, 126496, 126497, 126499, 126500, 126501, 126503, 126504, 126505, 126515, 126516, 126520, 126521, 126522, 126523, 126524, 126530, 126531, 126535, 126536, 126537, 126538, 126539, 126540, 126541, 126544, 126545, 126547, 126548, 126549, 126551, 126552, 126553, 126554, 126555, 126556, 126557, 126558, 126559, 126560, 126561, 126563, 126564, 126565, 126567, 126571, 126572, 126579, 126580, 126584, 126585, 126589, 126590, 126591, 126592, 126602, 126603, 126620, 126625, 126628, 126629, 126634, 126635, 126652, 126704, 126706, 129024, 129036, 129040, 129096, 129104, 129114, 129120, 129160, 129168, 129198, 129200, 129202, 129792, 129939, 129940, 129995, 130032, 130042, 917505, 917506, 917536, 917632, 917760, 918000 }
788   local function rotate_in_uax50(i)
789     local lo, hi = 1, #t
790     while lo < hi do
791       local mi = ceil((lo+hi)/2)
792       if t[mi]<=i then lo=mi else hi=mi-1 end
793     end
794     return lo%2==1
795   end
796   local function list_rorate_dup (i, v, dest)
797     local f = dest[i]
798     if not f then
799       for j,_ in pairs(v) do if dest[j] then f=true; break end end
800     end
801     dest[i]=f; for j,_ in pairs(v) do dest[j]=f end
802   end
803   list_rotate_glyphs = function (tfmdata, dest)
804     local rot = {}
805     for i,_ in pairs(tfmdata.characters) do
806       if rotate_in_uax50(i) then rot[i] = true end
807     end
808     -- ↓「TeX Live 2019のLuaLaTeXで縦書きの三点リーダーが横書きになる」
809     -- (https://oku.edu.mie-u.ac.jp/tex/mod/forum/discuss.php?d=2722) により無効化
810     -- -- 同じグリフが複数の Unicode ポイントを持っている場合.
811     -- -- いずれかの Unicode ポイントで rot = true ならば全体で rotate
812     --ltju.loop_over_duplicates(tfmdata,
813     --   function (i, v)
814     --     local f = rot[i]
815     --     if not f then
816     --        for j,_ in pairs(v) do if rot[j] then f=true; break end end
817     --     end
818     --     rot[i]=f; for j,_ in pairs(v) do rot[j]=f end
819     --   end)
820     for i,_ in pairs(rot) do
821        dest = dest or {}; dest.rotation = dest.rotation or {}
822        dest.rotation[i] = rotate_exception[i] or true
823     end
824     return dest
825   end
826 end
827
828 do
829    local cache_ver = 24
830    local nameonly, lower = file.nameonly, string.lower
831    local lfs = require"lfs"
832    local file_attributes = lfs.attributes
833    local load_cache, save_cache = ltjb.load_cache, ltjb.save_cache
834    local function prepare_extra_data_base(tfmdata)
835       if (not tfmdata) or (not tfmdata.filename) then return end
836       local bname = tfmdata.psname or nameonly(tfmdata.filename)
837       if not font_extra_basename[bname] then
838          -- if the cache is present, read it
839          --
840          local newtime = file_attributes(tfmdata.filename,"modification")
841          local v = "extra_" .. string.lower(bname)
842          local dest = load_cache(
843             v,
844             function (t)
845                 return (t.lotf_version~=luaotfload.version)
846                        or (t.version~=cache_ver) or (t.modtime~=newtime)
847             end
848          )
849          -- if the cache is not found or outdated, save the cache
850          if dest then
851             font_extra_basename[bname] = dest[1] or {}
852             local vheight, vorigin = dest[1].vheight, dest[1].vorigin
853             local vhd, vod = vheight.default, vorigin.default
854             setmetatable(vheight, {__index = function () return vhd end } )
855             setmetatable(vorigin, {__index = function () return vod end } )
856          else
857             local dest = ltju.get_vmet_table(tfmdata, nil)
858             dest = list_rotate_glyphs(tfmdata, dest)
859             font_extra_basename[bname] = dest or {}
860             save_cache(v,
861                        { modtime = newtime,
862                          version = cache_ver,
863                          lotf_version = luaotfload.version,
864                          dest,
865                        })
866          end
867          return bname
868       end
869    end
870    local dummytable = { } -- dummy
871    local dtvo, dtvh = {}, {}
872    setmetatable(dtvo, {__index = function () return 0.88 end } )
873    setmetatable(dtvh, {__index = function () return 1 end } )
874    ltjr.vert_addfunc = function(id, s)
875        if not dummytable.rotation then
876            dummytable = list_rotate_glyphs(s, dummytable)
877            dummytable.vorigin, dummytable.vheight = dtvo, dtvh
878        end
879    end
880
881    local function prepare_extra_data_font(id, res, name)
882       if type(res)=='table' and (res.psname or res.filename) then
883          if (res.embedding=='no') and (type(name)=='string') and (name:sub(1,5)=='psft:') then
884             font_extra_info[id] = res.resources.ltj_extra
885          else
886             local bname = res.psname or nameonly(res.filename)
887             local t = font_extra_basename[bname]
888             if not t then bname = prepare_extra_data_base(res) end
889             font_extra_info[id] = bname and (t or font_extra_basename[bname]) or dummytable
890          end
891       end
892    end
893     luatexbase.add_to_callback(
894        'luaotfload.patch_font',
895        function (tfmdata)
896           -- these function is executed one time per one fontfile
897           prepare_extra_data_base(tfmdata); return tfmdata
898        end,
899        'ltj.prepare_extra_data', 1)
900    luatexbase.add_to_callback(
901       'luatexja.define_font',
902       function (res, name, size, id)
903          prepare_extra_data_font(id, res, name)
904       end,
905       'ltj.prepare_extra_data', 1)
906
907    for i=1,font.nextid()-1 do
908       local t = font.getfont(i)
909       if t then
910          prepare_extra_data_base(t)
911          prepare_extra_data_font(i,t)
912       end
913    end
914 end
915
916 ------------------------------------------------------------------------
917 -- make table of vertical glyphs which does not covered by vert feature
918 -- nor UTR#50
919 ------------------------------------------------------------------------
920 do
921 ------------------------------------------------------------------------
922 -- VERT VARIANT TABLE
923 ------------------------------------------------------------------------
924   local provides_feature = luaotfload.aux.provides_feature
925   local vert_form_table = {
926      [0x3001]=0xFE11, [0x3002]=0xFE12, [0x3016]=0xFE17, [0x3017]=0xFE18,
927      [0x2026]=0xFE19,
928      [0x2025]=0xFE30, [0x2014]=0xFE31, [0x2013]=0xFE32, [0xFF3F]=0xFE33,
929      [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
930      [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
931      [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
932      [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
933      [0xFF3B]=0xFE47, [0xFF3D]=0xFE48,
934   }
935   local vert_jpotf_table, vert_feat = {}, {vert=true}
936   local utfbyte, utfsub = utf.byte, utf.sub
937   luatexja.jfont.register_vert_replace = function(t)
938     for i,v in pairs(t) do
939       local ic = (type(i)=='number') and i or
940         ((type(i)=='string') and utfbyte(utfsub(i,1,1)) or nil)
941       if ic then
942         vert_jpotf_table[ic] = (type(v)=='number') and v or
943           ((type(v)=='string') and utfbyte(utfsub(v,1,1)) or nil)
944       end
945     end
946   end
947
948 luatexbase.add_to_callback(
949    "luatexja.define_jfont",
950    function (fmtable, fnum)
951       local vform = {}; fmtable.vform = vform
952       local t = font_getfont(fnum)
953       if not t then return fmtable end
954       for i,v in pairs(vert_form_table) do
955         if t.characters[v] then vform[i] = v end
956       end
957       if ltju.specified_feature(fnum, 'jpotf') then
958         for i,v in pairs(vert_jpotf_table) do
959           if t.characters[v] then vform[i] = vform[v] or v end
960         end
961       end
962       if not ltju.exist_feature(fnum, 'vert') and not ltju.exist_feature(fnum, 'vrt2') then
963         -- 現在の (script, lang) で vert もvrt2 も有効にできない場合,
964         -- 全 (script,lang) の vert を強制的に適用
965         ltju.loop_over_feat(t, vert_feat, function (i,k) vform[i] = vform[i] or k end, true)
966       end
967       -- vform の中身を vert 適用結果に変える
968       ltju.loop_over_feat(t, vert_feat,
969         function (i,k)
970           for j,w in pairs(vform) do
971             if (i==j)and(w==k) then vform[j]=nil elseif w==i then vform[j] = k end
972           end
973       end)
974       return fmtable
975    end, 'ltj.get_vert_form', 1
976 )
977 end
978
979 ------------------------------------------------------------------------
980 -- supply tounicode entries
981 ------------------------------------------------------------------------
982 do
983   local ltjr_prepare_cid_font = ltjr.prepare_cid_font
984   luatexbase.add_to_callback(
985      'luaotfload.patch_font',
986      function (tfmdata)
987         local cidinfo = tfmdata.cidinfo or (tfmdata.resource and tfmdata.resources.cidinfo)
988         if cidinfo and cidinfo.registry and cidinfo.ordering then
989            local rd = ltjr_prepare_cid_font(cidinfo.registry, cidinfo.ordering)
990            if rd then
991               local ru = rd.resources.unicodes -- defined by LuaTeX-ja
992               local rc = rd.characters
993               for i,v in pairs(tfmdata.characters) do
994                  local w = ru[cidinfo.ordering .. "." .. tostring(v.index)]
995                  if w then
996                     v.tounicode = v.tounicode or rc[w]. tounicode
997                  end
998               end
999            end
1000         end
1001
1002         return tfmdata
1003      end,
1004      'ltj.supply_tounicode', 1)
1005 end
1006
1007
1008 ------------------------------------------------------------------------
1009 -- MISC
1010 ------------------------------------------------------------------------
1011 do
1012    local getfont = node.direct.getfont
1013    local getchar = node.direct.getchar
1014    local get_dir_count = ltjd.get_dir_count
1015    local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
1016    local ensure_tex_attr = ltjb.ensure_tex_attr
1017    local node_write = node.direct.write
1018    local fonts, getnest = font.fonts, tex.getnest
1019    local new_ic_kern = function(g)  return node_new(id_kern,3) end
1020    local dir_tate = luatexja.dir_table.dir_tate
1021    -- EXT: italic correction
1022    function luatexja.jfont.append_italic()
1023       local p = to_direct(getnest().tail)
1024       local TEMP = node_new(id_kern)
1025       if p and getid(p)==id_glyph then
1026          if is_ucs_in_japanese_char(p) then
1027             local j = font_metric_table[
1028                get_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1029                ]
1030             local g = new_ic_kern()
1031             setkern(g, j.char_type[find_char_class(getchar(p), j)].italic)
1032             node_write(g); ensure_tex_attr(attr_icflag, 0)
1033          else
1034             local f = getfont(p)
1035             local h = font_getfont(f) or fonts[f]
1036             if h then
1037                if h.characters[getchar(p)] and h.characters[getchar(p)].italic then
1038                   local g = new_ic_kern()
1039                   setkern(g, h.characters[getchar(p)].italic)
1040                   node_write(g); ensure_tex_attr(attr_icflag, 0)
1041                end
1042             end
1043          end
1044       end
1045       node_free(TEMP)
1046    end
1047 end
1048
1049 luatexja.jfont.metrics = metrics
1050 luatexja.jfont.font_metric_table = font_metric_table
1051 luatexja.jfont.find_char_class = find_char_class
1052
1053 luatexja.jfont.update_jfm_cache = update_jfm_cache