OSDN Git Service

29a04134db23924183c093f200d4f9c467f248bc
[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-16',
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 has_attr = node.direct.has_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,
223                   ratio=w.ratio,
224                   priority=FROM_JFM + w.priority,
225                   width = w[1], stretch = w[2], shrink = w[3],
226                   kanjiskip_natural = w.kanjiskip_natural,
227                   kanjiskip_stretch = w.kanjiskip_stretch,
228                   kanjiskip_shrink =  w.kanjiskip_shrink,
229                   round_threshold = w.round_threshold,
230                   }
231             end
232             for k,w in pairs(v.kern) do
233                v[k] = {w[1], ratio=w[2]/sz}
234             end
235          end
236          v.glue, v.kern = nil, nil
237       end
238       t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
239       t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
240       t.zw = round(metrics[j].zw*sz)
241       t.zh = round(metrics[j].zh*sz)
242       t.size = sz
243       size_cache_num = size_cache_num + 1
244       t.index = size_cache_num
245       return size_cache_num
246    end
247 end
248
249 luatexbase.create_callback("luatexja.find_char_class", "data",
250                            function (arg, fmtable, char)
251                               return 0
252                           end)
253 local find_char_class
254 do
255    local start_time_measure = ltjb.start_time_measure
256    local stop_time_measure = ltjb.stop_time_measure
257    local fcc_temp = { chars_cbcache = {} }
258    setmetatable(
259       fcc_temp.chars_cbcache,
260       {
261          __index = function () return 0 end,
262       })
263    function find_char_class(c,m)
264       -- c: character code, m:
265       local r = (m or fcc_temp).chars_cbcache[c]
266       if not r then
267          r = m.chars[c] or
268             luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
269          m.chars_cbcache[c or 0] = r
270       end
271       return r
272    end
273 end
274
275
276 ------------------------------------------------------------------------
277 -- LOADING JAPANESE FONTS
278 ------------------------------------------------------------------------
279
280 local load_jfont_metric, check_callback_order
281 local font_extra_info = {} -- defined later
282 do
283    local cstemp
284    local global_flag -- true if \globaljfont, false if \jfont
285    load_jfont_metric = function()
286      if jfm_name=='' then
287          ltjb.package_error('luatexja', 'no JFM specified',
288                             'To load and define a Japanese font, a JFM must be specified.'..
289                             "The JFM 'ujis' will be  used for now.")
290          jfm_name, jfm_spec = 'ujis', 'ujis'
291       end
292       for j,v in ipairs(metrics) do if v.name==jfm_spec then return j end end
293       luatexja.load_lua('jfm-' .. jfm_name .. '.lua')
294       if defjfm_res then
295          defjfm_res.name = jfm_spec; table.insert(metrics, defjfm_res)
296          return #metrics
297       else
298          return nil
299       end
300    end
301
302 -- EXT
303    local utfbyte = utf.byte
304    function luatexja.jfont.jfontdefX(g, dir, csname)
305       jfm_dir, is_def_jfont = dir, true
306       cstemp = csname:sub( (utfbyte(csname,1,1) == tex.escapechar) and 2 or 1, -1)
307       cstemp = cstemp:sub(1, ((cstemp:sub(-1,-1)==' ') and (cstemp:len()>=2)) and -2 or -1)
308       global_flag = g and '\\global' or ''
309       tex.sprint(cat_lp, '\\expandafter\\font\\csname ',
310                  (cstemp==' ') and '\\space' or cstemp, '\\endcsname')
311    end
312
313    luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
314
315 -- EXT
316    local fastcopy=table.fastcopy
317    function luatexja.jfont.jfontdefY()
318       local j = load_jfont_metric(jfm_dir)
319       local fn = font.id(cstemp)
320       local f = font_getfont(fn)
321       if not j then
322          ltjb.package_error('luatexja', "bad JFM `" .. jfm_name .. "'",
323                             'The JFM file you specified is not valid JFM file.\n'..
324                                'So defining Japanese font is cancelled.')
325          tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ',
326                     (cstemp==' ') and '\\space' or cstemp,
327                        '\\endcsname=\\relax')
328          return
329       end
330       if not f then return end
331       update_jfm_cache(j, f.size); check_callback_order()
332       local sz = metrics[j].size_cache[f.size]
333       local fmtable = { jfm = j, size = f.size, var = jfm_var,
334                         with_kanjiskip = jfm_ksp,
335                         zw = sz.zw, zh = sz.zh,
336                         chars = sz.chars, char_type = sz.char_type,
337                         kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
338                         chars_cbcache = {},
339                         vert_activated = vert_activated,
340                         rotation = fastcopy(font_extra_info[fn].rotation),
341       }
342       if auto_enable_vrt2 then
343          local vert_name = ltju.exist_feature(fn, 'vrt2') and 'vrt2' or 'vert'
344          local rot = fmtable.rotation 
345          ltju.enable_feature(fn, vert_name)
346          ltju.loop_over_feat(f, {[vert_name]=true}, function (i,k) rot[i] = nil end)
347       end
348
349       fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
350       font_metric_table[fn]=fmtable
351       tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
352                     (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
353                     (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
354       jfm_spec = nil
355    end
356 end
357
358 do
359     local ltb = luatexbase
360     local to_be_checked = { 'pre_linebreak_filter', 'hpack_filter' }
361     check_callback_order = function()
362         for i,n in pairs(to_be_checked) do
363             local lotf_cb = ltb.priority_in_callback(n, 'luaotfload.letterspace')
364             local ltj_cb  = ltb.priority_in_callback(n, 'ltj.main')
365             if lotf_cb then
366                 to_be_checked[i]=nil
367                 if ltj_cb<lotf_cb then
368                     local f = ltb.remove_from_callback(n,'luaotfload.letterspace')
369                     ltb.add_to_callback(n, f, 'luaotfload.letterspace', 
370                         ltb.priority_in_callback(n, 'luaotfload.node_processor') + 1)
371                 end
372             end
373         end
374     end
375 end
376
377 do
378    local get_dir_count = ltjd.get_dir_count
379    local dir_tate = luatexja.dir_table.dir_tate
380    local tex_get_attr = tex.getattribute
381    -- PUBLIC function
382    function luatexja.jfont.get_zw()
383       local a = font_metric_table[
384          tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
385       return a and a.zw or 0
386    end
387    function luatexja.jfont.get_zh()
388       local a = font_metric_table[
389          tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)]
390       return a and a.zw or 0
391    end
392 end
393
394 do
395     local P, R, S, V        = lpeg.P, lpeg.R, lpeg.S, lpeg.V
396     local lpegmatch         = lpeg.match
397     local C, Cc, Cf, Ct, Cg = lpeg.C, lpeg.Cc, lpeg.Cf, lpeg.Ct, lpeg.Cg
398     local Cs, B             = lpeg.Cs, lpeg.B
399     local spacing           = S" \t\v"
400     local ws                = spacing^0
401     local slash             = P'/'
402     local semicolon         = P';'
403     local comma             = P','
404     local equals            = P'='
405     local jf_field_char     = 1 - S'/{};,= \t\v'
406     local jf_field          = C(jf_field_char^1)
407     local jf_assignment     = jf_field * ws * equals * ws * jf_field
408     local jf_switch         = P'-' * jf_field * Cc(false) + P'+'^-1 * jf_field * Cc(true)
409     local jf_feature_expr   = Cg(jf_assignment + jf_switch) * ws * comma^0 * ws
410     local jf_feature_list   = ws * ( P'{' * ws * jf_feature_expr^0 * P'}' + jf_feature_expr^0 )
411     local jf_list           = C((1-slash)^1) * (slash * Cf(Ct'' * jf_feature_list, rawset))^-1
412     local jf_value          = (1 - semicolon)^1
413     local function rem(name,value)
414       if name=='jfm' then
415         local flag, t; jfm_name, t = lpegmatch(jf_list, value)
416         if type(t)=='table' then
417           for i,v in pairs(t) do
418             flag=true
419             if v=='true' then t[i]=true elseif v=='false' then t[i]=false end
420             end
421         end
422         luatexja.jfont.jfm_feature = flag and t
423       elseif name=='jfmvar' then jfm_var = value end
424       return ''
425     end
426     local jf_remainder      = Cs( ( ( B(S':;') *
427       C(P'jfm' * P'var'^-1) * ws * equals * ws * C(jf_value) * semicolon^-1 ) / rem +1 )^0 )
428
429    local parser=luaotfload.parsers.font_request
430    function is_feature_specified(s,fname)
431      local t = lpegmatch(parser,s); return t and t.features and t.features[fname]
432    end    
433    -- extract jfm_name, jfm_spec and jfm_var
434    -- normalize position of 'jfm=' and 'jfmvar=' keys
435    local function extract_jfm_spec(name)
436       name = (name:match '^{(.*)}$') or (name:match '^"(.*)"$') or name
437       jfm_spec, jfm_var = '', ''
438       local tmp, index = name:sub(1, 5), 1
439       if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
440          index = 6
441       end
442       name = lpegmatch(jf_remainder, name)
443       if jfm_name~='' then
444          if luatexja.jfont.jfm_feature then
445             local l, t2 = name:sub(-1), {}
446             for i,v in pairs(luatexja.jfont.jfm_feature) do
447                -- print(i,type(v),v)
448                t2[#t2+1] = (v==true) and i
449                   or ((v==false) and ('-'..i) or (i..'='..tostring(v)))
450             end
451             table.sort(t2); jfm_spec = jfm_name .. '/{' .. table.concat(t2, ',') .. '}'
452          else
453             jfm_spec = jfm_name
454          end
455          l = name:sub(-1)
456          name = name .. ((l==':' or l==';') and '' or ';') .. 'jfm=' .. jfm_spec
457          if jfm_var~='' then
458             name = name .. ';jfmvar=' .. jfm_var
459          end
460          -- print('NN>', name)
461       end
462       jfm_ksp = (is_feature_specified(name,'ltjksp')~=false)
463       if jfm_dir == 'tate' then
464          vert_activated = (is_feature_specified(name,'vert')~=false) and (is_feature_specified(name,'vrt2')~=false)
465          auto_enable_vrt2 
466            = (is_feature_specified(name,'vert')==nil) and (is_feature_specified(name,'vrt2')==nil)
467       else
468          vert_activated, auto_enable_vrt2 = nil, nil
469       end
470       return name
471    end
472
473    -- define_font callback
474    local otfl_fdr
475    local ltjr_font_callback = ltjr.font_callback
476    function luatexja.font_callback(name, size, id)
477       local new_name = is_def_jfont and extract_jfm_spec(name) or name
478       is_def_jfont = false
479       local res =  ltjr_font_callback(new_name, size, id, otfl_fdr)
480       luatexbase.call_callback('luatexja.define_font', res, new_name, size, id)
481       -- this callback processes variation selector, so we execute it always
482       return res
483    end
484    luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end)
485    otfl_fdr= luatexbase.remove_from_callback('define_font', 'luaotfload.define_font')
486    luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
487
488    local match, sp, scan_arg = string.match, tex.sp, token.scan_argument
489    local function load_jfmonly()
490       local spec, size = match(scan_arg(), '(.+)%s+at%s*([%.%w]*)')
491       local dir = scan_arg()
492       size = sp(size); extract_jfm_spec(spec)
493       jfm_dir = dir
494       local i = load_jfont_metric()
495       local j = -update_jfm_cache(i, size)
496       font_metric_table[j]=metrics[i].size_cache[size]
497       tex.sprint(cat_lp, '\\ltj@cur' .. (dir=='yoko' and 'j' or 't') .. 'fnt' .. tostring(j) .. '\\relax')
498    end
499    luatexja.jfont.load_jfmonly = load_jfmonly
500 end
501
502 ------------------------------------------------------------------------
503 -- LATEX INTERFACE
504 ------------------------------------------------------------------------
505 do
506    local scan_arg = token.scan_argument
507    -- these function are called from ltj-latex.sty
508    local fenc_list, kyenc_list, ktenc_list = {}, {}, {}
509    function luatexja.jfont.add_fenc_list() fenc_list[scan_arg()] = 'true ' end
510    function luatexja.jfont.add_kyenc_list() kyenc_list[scan_arg()] = 'true ' end
511    function luatexja.jfont.add_ktenc_list() ktenc_list[scan_arg()] = 'true ' end
512    function luatexja.jfont.is_kyenc()
513       tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[scan_arg()] or 'false '))
514    end
515    function luatexja.jfont.is_ktenc()
516       tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ktenc_list[scan_arg()] or 'false '))
517    end
518    function luatexja.jfont.is_kenc()
519       local enc = scan_arg()
520       tex.sprint(cat_lp, '\\let\\ifin@\\if'
521                  .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
522    end
523
524    local kfam_list, Nkfam_list = {}, {}
525    function luatexja.jfont.add_kfam()
526       kfam_list[scan_arg()]=true
527    end
528    function luatexja.jfont.search_kfam()
529       local fam = scan_arg()
530       local use_fd = (scan_arg() =='true')
531       if kfam_list[fam] then
532          tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
533       elseif Nkfam_list[fam] then
534          tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
535       elseif use_fd then
536          for i,_ in pairs(kyenc_list) do
537             if kpse.find_file(string.lower(i)..fam..'.fd') then
538                tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
539             end
540          end
541          for i,_ in pairs(ktenc_list) do
542             if kpse.find_file(string.lower(i)..fam..'.fd') then
543                tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
544             end
545          end
546          Nkfam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
547       else
548          tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
549       end
550    end
551    local ffam_list, Nffam_list = {}, {}
552    function luatexja.jfont.is_ffam()
553       tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (ffam_list[scan_arg()] or 'false '))
554    end
555    function luatexja.jfont.add_ffam()
556       ffam_list[scan_arg()]='true '
557    end
558    function luatexja.jfont.search_ffam_declared()
559      local s = ''
560      for i,_ in pairs(fenc_list) do
561         s = s .. '\\cdp@elt{' .. i .. '}'
562      end
563      tex.sprint(cat_lp, s)
564    end
565    function luatexja.jfont.search_ffam_fd()
566       local fam = scan_arg()
567       if Nffam_list[fam] then
568          tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
569       else
570          for i,_ in pairs(fenc_list) do
571             if kpse.find_file(string.lower(i)..fam..'.fd') then
572                tex.sprint(cat_lp, '\\let\\ifin@\\iftrue '); return
573             end
574          end
575          Nffam_list[fam]=true; tex.sprint(cat_lp, '\\let\\ifin@\\iffalse '); return
576       end
577    end
578
579 end
580 ------------------------------------------------------------------------
581 -- ALTERNATE FONTS
582 ------------------------------------------------------------------------
583 local alt_font_table = {}
584 local attr_curaltfnt = {}
585 local ucs_out = 0x110000
586
587 ------ for TeX interface
588 -- EXT
589 function luatexja.jfont.set_alt_font(b,e,ind,bfnt)
590    -- ind: 新フォント, bfnt: 基底フォント
591    if b>e then b, e = e, b end
592    if b*e<=0 then
593       ltjb.package_error('luatexja',
594                         'bad character range ([' .. b .. ',' .. e .. ']). ' ..
595                            'I take the intersection with [0x80, 0x10ffff].')
596       b, e = math.max(0x80,b),math.min(ucs_out-1,e)
597    elseif e<0 then -- b<e<0
598       -- do nothing
599    elseif b<0x80 or e>=ucs_out then
600       ltjb.package_warning('luatexja',
601                            'bad character range ([' .. b .. ',' .. e .. ']). ' ..
602                               'I take the intersection with [0x80, 0x10ffff].')
603       b, e = math.max(0x80,b), math.min(ucs_out-1,e)
604    end
605    if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
606    local t = alt_font_table[bfnt]
607    local ac = font_getfont(ind).characters
608    if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
609    if e>=0 then -- character range
610       for i=b, e do
611          if ac[i]then  t[i]=ind end
612       end
613    else
614       b, e = -e, -b
615       local tx = font_metric_table[bfnt].chars
616       for i,v in pairs(tx) do
617          if b<=v and v<=e and ac[i] then t[i]=ind end
618       end
619    end
620 end
621
622 -- EXT
623 function luatexja.jfont.clear_alt_font(bfnt)
624    if alt_font_table[bfnt] then
625       local t = alt_font_table[bfnt]
626       for i,_ in pairs(t) do t[i]=nil; end
627    end
628 end
629
630 ------ used in ltjp.suppress_hyphenate_ja callback
631 function luatexja.jfont.replace_altfont(pf, pc)
632    local a = alt_font_table[pf]
633    return a and a[pc] or pf
634 end
635
636 ------ for LaTeX interface
637
638 local alt_font_table_latex = {}
639
640 -- EXT
641 function luatexja.jfont.clear_alt_font_latex(bbase)
642    local t = alt_font_table_latex[bbase]
643    if t then
644       for j,v in pairs(t) do t[j] = nil end
645    end
646 end
647
648 -- EXT
649 function luatexja.jfont.set_alt_font_latex(b,e,ind,bbase)
650    -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
651    if b>e then b, e = e, b end
652    if b*e<=0 then
653       ltjb.package_error('luatexja',
654                         'bad character range ([' .. b .. ',' .. e .. ']). ' ..
655                            'I take the intersection with [0x80, 0x10ffff].')
656       b, e = math.max(0x80,b),math.min(ucs_out-1,e)
657    elseif e<0 then -- b<e<0
658       -- do nothing
659    elseif b<0x80 or e>=ucs_out then
660       ltjb.package_warning('luatexja',
661                            'bad character range ([' .. b .. ',' .. e .. ']). ' ..
662                               'I take the intersection with [0x80, 0x10ffff].')
663       b, e = math.max(0x80,b), math.min(ucs_out-1,e)
664    end
665
666    if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
667    local t = alt_font_table_latex[bbase]
668    if not t[ind] then t[ind] = {} end
669    for i=b, e do
670       for j,v in pairs(t) do
671          if v[i] then -- remove old entry
672             if j~=ind then v[i]=nil end; break
673          end
674       end
675       t[ind][i]=true
676    end
677    -- remove the empty tables
678    for j,v in pairs(t) do
679       local flag_clear = true
680       for k,_ in pairs(v) do flag_clear = false; break end
681       if flag_clear then t[j]=nil end
682    end
683    if ind==bbase  then t[bbase] = nil end
684 end
685
686 -- ここから先は 新 \selectfont の内部でしか実行されない
687 do
688    local scan_arg = token.scan_argument
689    local alt_font_base, alt_font_base_num
690    local aftl_base
691    -- EXT
692    function luatexja.jfont.does_alt_set()
693       aftl_base = alt_font_table_latex[scan_arg()]
694       tex.sprint(cat_lp, aftl_base and '\\@firstofone' or '\\@gobble')
695    end
696    -- EXT
697    function luatexja.jfont.print_aftl_address()
698       return ';ltjaltfont' .. tostring(aftl_base):sub(8)
699    end
700
701 -- EXT
702    function luatexja.jfont.output_alt_font_cmd(dir, bbase)
703       alt_font_base = bbase
704       alt_font_base_num = tex.getattribute((dir == 't') and attr_curtfnt or attr_curjfnt)
705       local t = alt_font_table[alt_font_base_num]
706       if t then
707          for i,_ in pairs(t) do t[i]=nil end
708       end
709       t = alt_font_table_latex[bbase]
710       if t then
711          for i,_ in pairs(t) do
712             tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}')
713          end
714       end
715    end
716
717 -- EXT
718    function luatexja.jfont.pickup_alt_font_a(size_str)
719       local t = alt_font_table_latex[alt_font_base]
720       if t then
721          for i,v in pairs(t) do
722             tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
723                           .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
724          end
725       end
726    end
727
728    local function pickup_alt_font_class(class, afnt_num, afnt_chars)
729       local t  = alt_font_table[alt_font_base_num]
730       local tx = font_metric_table[alt_font_base_num].chars
731       for i,v in pairs(tx) do
732          if v==class and afnt_chars[i] then t[i]=afnt_num end
733       end
734    end
735
736 -- EXT
737    function luatexja.jfont.pickup_alt_font_b(afnt_num, afnt_base)
738       local t = alt_font_table[alt_font_base_num]
739       local ac = font_getfont(afnt_num).characters
740       if not t then t = {}; alt_font_table[alt_font_base_num] = t end
741       for i,v in pairs(alt_font_table_latex[alt_font_base]) do
742          if i == afnt_base then
743             for j,_ in pairs(v) do
744                if j>=0 then
745                   if ac[j] then t[j]=afnt_num end
746                else  -- -n (n>=1) means that the character class n,
747                      -- which is defined in the JFM
748                   pickup_alt_font_class(-j, afnt_num, ac)
749                end
750             end
751             return
752          end
753       end
754    end
755
756 end
757 ------------------------------------------------------------------------
758 -- 終了時に各種ノードを破棄
759 ------------------------------------------------------------------------
760 do
761    function luatexja.jfont.cleanup_size_cache()
762       --local gs, ke = 0, 0
763       for _,n in pairs(metrics) do
764          for i,t in pairs(n.size_cache) do
765             for _,v in pairs(t.char_type) do
766                for k,w in pairs(v) do
767                   if type(k)=='number' then
768                      --if w[1] then gs = gs + 1 else ke = ke + 1 end
769                      if w[1] then node_free(w[1]) end
770                   end
771                end
772             end
773             n.size_cache[i]=nil
774          end
775       end
776    end
777 end
778
779 ------------------------------------------------------------------------
780 -- 追加のフォント情報
781 ------------------------------------------------------------------------
782 luatexja.jfont.font_extra_info = font_extra_info -- key: fontnumber
783 local font_extra_basename = {} -- key: basename
784
785 local rotate_exception = {
786   [0xFF1A]= { ['zht'] = true, },
787   [0xFF1B]= { ['zht'] = true, },
788 }
789 luatexja.jfont.rotate_exception = rotate_exception
790 local list_rotate_glyphs
791 do
792   local ceil = math.ceil
793   -- output of function_uax50.lua
794   -- UAX#50 for Unicode  14.0.0
795   -- t[0] = true
796   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 }
797   local function rotate_in_uax50(i)
798     local lo, hi = 1, #t
799     while lo < hi do
800       local mi = ceil((lo+hi)/2)
801       if t[mi]<=i then lo=mi else hi=mi-1 end 
802     end
803     return lo%2==1
804   end
805   local function list_rorate_dup (i, v, dest)
806     local f = dest[i]
807     if not f then
808       for j,_ in pairs(v) do if dest[j] then f=true; break end end
809     end
810     dest[i]=f; for j,_ in pairs(v) do dest[j]=f end
811   end
812   list_rotate_glyphs = function (tfmdata, dest)
813     local rot = {}
814     for i,_ in pairs(tfmdata.characters) do
815       if rotate_in_uax50(i) then rot[i] = true end
816     end
817     -- ↓「TeX Live 2019のLuaLaTeXで縦書きの三点リーダーが横書きになる」
818     -- (https://oku.edu.mie-u.ac.jp/tex/mod/forum/discuss.php?d=2722) により無効化
819     -- -- 同じグリフが複数の Unicode ポイントを持っている場合.
820     -- -- いずれかの Unicode ポイントで rot = true ならば全体で rotate
821     --ltju.loop_over_duplicates(tfmdata,
822     --   function (i, v)
823     --     local f = rot[i]
824     --     if not f then
825     --        for j,_ in pairs(v) do if rot[j] then f=true; break end end
826     --     end
827     --     rot[i]=f; for j,_ in pairs(v) do rot[j]=f end
828     --   end)
829     for i,_ in pairs(rot) do
830        dest = dest or {}; dest.rotation = dest.rotation or {}
831        dest.rotation[i] = rotate_exception[i] or true
832     end
833     return dest
834   end
835 end
836
837 do
838    local cache_ver = 24
839    local nameonly, lower = file.nameonly, string.lower
840    local lfs = require"lfs"
841    local file_attributes = lfs.attributes
842    local load_cache, save_cache = ltjb.load_cache, ltjb.save_cache
843    local function prepare_extra_data_base(tfmdata)
844       if (not tfmdata) or (not tfmdata.filename) then return end
845       local bname = tfmdata.psname or nameonly(tfmdata.filename)
846       if not font_extra_basename[bname] then
847          -- if the cache is present, read it
848          -- 
849          local newtime = file_attributes(tfmdata.filename,"modification")
850          local v = "extra_" .. string.lower(bname)
851          local dest = load_cache(
852             v,
853             function (t) 
854                 return (t.lotf_version~=luaotfload.version)
855                        or (t.version~=cache_ver) or (t.modtime~=newtime) 
856             end
857          )
858          -- if the cache is not found or outdated, save the cache
859          if dest then
860             font_extra_basename[bname] = dest[1] or {}
861             local vheight, vorigin = dest[1].vheight, dest[1].vorigin
862             local vhd, vod = vheight.default, vorigin.default
863             setmetatable(vheight, {__index = function () return vhd end } )
864             setmetatable(vorigin, {__index = function () return vod end } )
865          else
866             local dest = ltju.get_vmet_table(tfmdata, nil)
867             dest = list_rotate_glyphs(tfmdata, dest)
868             font_extra_basename[bname] = dest or {}
869             save_cache(v,
870                        { modtime = newtime,
871                          version = cache_ver,
872                          lotf_version = luaotfload.version,
873                          dest,
874                        })
875          end
876          return bname
877       end
878    end
879    local dummytable = { } -- dummy
880    local dtvo, dtvh = {}, {}
881    setmetatable(dtvo, {__index = function () return 0.88 end } )
882    setmetatable(dtvh, {__index = function () return 1 end } )
883    ltjr.vert_addfunc = function(id, s)
884        if not dummytable.rotation then
885            dummytable = list_rotate_glyphs(s, dummytable)
886            dummytable.vorigin, dummytable.vheight = dtvo, dtvh
887        end
888    end
889    
890    local function prepare_extra_data_font(id, res, name)
891       if type(res)=='table' and (res.psname or res.filename) then
892          if (res.embedding=='no') and (type(name)=='string') and (name:sub(1,5)=='psft:') then
893             font_extra_info[id] = res.resources.ltj_extra
894          else
895             local bname = res.psname or nameonly(res.filename)
896             local t = font_extra_basename[bname]
897             if not t then bname = prepare_extra_data_base(res) end
898             font_extra_info[id] = bname and (t or font_extra_basename[bname]) or dummytable
899          end
900       end
901    end
902     luatexbase.add_to_callback(
903        'luaotfload.patch_font',
904        function (tfmdata)
905           -- these function is executed one time per one fontfile
906           prepare_extra_data_base(tfmdata); return tfmdata
907        end,
908        'ltj.prepare_extra_data', 1)
909    luatexbase.add_to_callback(
910       'luatexja.define_font',
911       function (res, name, size, id)
912          prepare_extra_data_font(id, res, name)
913       end,
914       'ltj.prepare_extra_data', 1)
915
916    for i=1,font.nextid()-1 do
917       local t = font.getfont(i)
918       if t then
919          prepare_extra_data_base(t)
920          prepare_extra_data_font(i,t)
921       end
922    end
923 end
924
925 ------------------------------------------------------------------------
926 -- make table of vertical glyphs which does not covered by vert feature
927 -- nor UTR#50
928 ------------------------------------------------------------------------
929 do
930 ------------------------------------------------------------------------
931 -- VERT VARIANT TABLE
932 ------------------------------------------------------------------------
933   local provides_feature = luaotfload.aux.provides_feature
934   local vert_form_table = {
935      [0x3001]=0xFE11, [0x3002]=0xFE12, [0x3016]=0xFE17, [0x3017]=0xFE18,
936      [0x2026]=0xFE19,
937      [0x2025]=0xFE30, [0x2014]=0xFE31, [0x2013]=0xFE32, [0xFF3F]=0xFE33,
938      [0xFF08]=0xFE35, [0xFF09]=0xFE36, [0xFF5B]=0xFE37, [0xFF5D]=0xFE38,
939      [0x3014]=0xFE39, [0x3015]=0xFE3A, [0x3010]=0xFE3B, [0x3011]=0xFE3C,
940      [0x300A]=0xFE3D, [0x300B]=0xFE3E, [0x3008]=0xFE3F, [0x3009]=0xFE40,
941      [0x300C]=0xFE41, [0x300D]=0xFE42, [0x300E]=0xFE43, [0x300F]=0xFE44,
942      [0xFF3B]=0xFE47, [0xFF3D]=0xFE48, 
943   }
944   local vert_jpotf_table, vert_feat = {}, {vert=true}
945   local utfbyte, utfsub = utf.byte, utf.sub
946   luatexja.jfont.register_vert_replace = function(t)
947     for i,v in pairs(t) do
948       local ic = (type(i)=='number') and i or 
949         ((type(i)=='string') and utfbyte(utfsub(i,1,1)) or nil)
950       if ic then
951         vert_jpotf_table[ic] = (type(v)=='number') and v or 
952           ((type(v)=='string') and utfbyte(utfsub(v,1,1)) or nil)
953       end
954     end  
955   end
956
957 luatexbase.add_to_callback(
958    "luatexja.define_jfont",
959    function (fmtable, fnum)
960       local vform = {}; fmtable.vform = vform
961       local t = font_getfont(fnum)
962       if not t then return fmtable end
963       for i,v in pairs(vert_form_table) do
964         if t.characters[v] then vform[i] = v end
965       end
966       if ltju.specified_feature(fnum, 'jpotf') then
967         for i,v in pairs(vert_jpotf_table) do
968           if t.characters[v] then vform[i] = vform[v] or v end
969         end
970       end
971       if not ltju.exist_feature(fnum, 'vert') and not ltju.exist_feature(fnum, 'vrt2') then
972         -- 現在の (script, lang) で vert もvrt2 も有効にできない場合,
973         -- 全 (script,lang) の vert を強制的に適用
974         ltju.loop_over_feat(t, vert_feat, function (i,k) vform[i] = vform[i] or k end, true)
975       end
976       -- vform の中身を vert 適用結果に変える
977       ltju.loop_over_feat(t, vert_feat,
978         function (i,k)
979           for j,w in pairs(vform) do
980             if (i==j)and(w==k) then vform[j]=nil elseif w==i then vform[j] = k end
981           end
982       end)
983       return fmtable
984    end, 'ltj.get_vert_form', 1
985 )
986 end
987
988 ------------------------------------------------------------------------
989 -- supply tounicode entries
990 ------------------------------------------------------------------------
991 do
992   local ltjr_prepare_cid_font = ltjr.prepare_cid_font
993   luatexbase.add_to_callback(
994      'luaotfload.patch_font',
995      function (tfmdata)
996         local cidinfo = tfmdata.cidinfo or (tfmdata.resource and tfmdata.resources.cidinfo)
997         if cidinfo and cidinfo.registry and cidinfo.ordering then
998            local rd = ltjr_prepare_cid_font(cidinfo.registry, cidinfo.ordering)
999            if rd then
1000               local ru = rd.resources.unicodes -- defined by LuaTeX-ja
1001               local rc = rd.characters
1002               for i,v in pairs(tfmdata.characters) do
1003                  local w = ru[cidinfo.ordering .. "." .. tostring(v.index)]
1004                  if w then
1005                     v.tounicode = v.tounicode or rc[w]. tounicode
1006                  end
1007               end
1008            end
1009         end
1010
1011         return tfmdata
1012      end,
1013      'ltj.supply_tounicode', 1)
1014 end
1015
1016
1017 ------------------------------------------------------------------------
1018 -- MISC
1019 ------------------------------------------------------------------------
1020 do
1021    local getfont = node.direct.getfont
1022    local getchar = node.direct.getchar
1023    local get_dir_count = ltjd.get_dir_count
1024    local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
1025    local ensure_tex_attr = ltjb.ensure_tex_attr
1026    local node_write = node.direct.write
1027    local fonts, tex_nest = font.fonts, tex.nest
1028    local new_ic_kern = function(g)  return node_new(id_kern,3) end
1029    local dir_tate = luatexja.dir_table.dir_tate
1030    -- EXT: italic correction
1031    function luatexja.jfont.append_italic()
1032       local p = to_direct(tex_nest[tex_nest.ptr].tail)
1033       local TEMP = node_new(id_kern)
1034       if p and getid(p)==id_glyph then
1035          if is_ucs_in_japanese_char(p) then
1036             local j = font_metric_table[
1037                has_attr(p, (get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
1038                ]
1039             local g = new_ic_kern()
1040             setkern(g, j.char_type[find_char_class(getchar(p), j)].italic)
1041             node_write(g); ensure_tex_attr(attr_icflag, 0)
1042          else
1043             local f = getfont(p)
1044             local h = font_getfont(f) or fonts[f]
1045             if h then
1046                if h.characters[getchar(p)] and h.characters[getchar(p)].italic then
1047                   local g = new_ic_kern()
1048                   setkern(g, h.characters[getchar(p)].italic)
1049                   node_write(g); ensure_tex_attr(attr_icflag, 0)
1050                end
1051             end
1052          end
1053       end
1054       node_free(TEMP)
1055    end
1056 end
1057
1058 luatexja.jfont.metrics = metrics
1059 luatexja.jfont.font_metric_table = font_metric_table
1060 luatexja.jfont.find_char_class = find_char_class
1061
1062 luatexja.jfont.update_jfm_cache = update_jfm_cache