OSDN Git Service

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