+do
+ local start_time_measure = ltjb.start_time_measure
+ local stop_time_measure = ltjb.stop_time_measure
+ local fcc_temp = { chars_cbcache = {} }
+ setmetatable(
+ fcc_temp.chars_cbcache,
+ {
+ __index = function () return 0 end,
+ })
+ function find_char_class(c,m)
+ -- c: character code, m:
+ local r = (m or fcc_temp).chars_cbcache[c]
+ if not r then
+ r = m.chars[c] or
+ luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
+ m.chars_cbcache[c or 0] = r
+ end
+ return r
+ end
+end
+
+
+------------------------------------------------------------------------
+-- LOADING JAPANESE FONTS
+------------------------------------------------------------------------
+
+do
+ local cstemp
+ local global_flag -- true if \globaljfont, false if \jfont
+ local function load_jfont_metric()
+ if jfm_file_name=='' then
+ ltjb.package_error('luatexja',
+ 'no JFM specified',
+ 'To load and define a Japanese font, a JFM must be specified.'..
+ "The JFM 'ujis' will be used for now.")
+ jfm_file_name='ujis'
+ end
+ for j,v in ipairs(metrics) do
+ if v.name==jfm_file_name then return j end
+ end
+ luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
+ if defjfm_res then
+ defjfm_res.name = jfm_file_name
+ table.insert(metrics, defjfm_res)
+ return #metrics
+ else
+ return nil
+ end
+ end
+
+-- EXT
+ local utf8 = unicode.utf8
+ function jfontdefX(g, dir, csname)
+ jfm_dir, is_def_jfont = dir, true
+ cstemp = csname:sub( (utf8.byte(csname,1,1) == tex.escapechar) and 2 or 1, -1)
+ cstemp = cstemp:sub(1, ((cstemp:sub(-1,-1)==' ') and (cstemp:len()>=2)) and -2 or -1)
+ global_flag = g and '\\global' or ''
+ tex.sprint(cat_lp, '\\expandafter\\font\\csname ',
+ (cstemp==' ') and '\\space' or cstemp, '\\endcsname')
+ end
+
+ luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
+
+-- EXT
+ local identifiers = fonts.hashes.identifiers
+ function jfontdefY()
+ local j = load_jfont_metric(jfm_dir)
+ local fn = font.id(cstemp)
+ local f = font_getfont(fn)
+ if not j then
+ ltjb.package_error('luatexja',
+ "bad JFM `" .. jfm_file_name .. "'",
+ 'The JFM file you specified is not valid JFM file.\n'..
+ 'So defining Japanese font is cancelled.')
+ tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ',
+ (cstemp==' ') and '\\space' or cstemp,
+ '\\endcsname=\\relax')
+ return
+ end
+ update_jfm_cache(j, f.size)
+ local ad = identifiers[fn].parameters
+ local sz = metrics[j].size_cache[f.size]
+ local fmtable = { jfm = j, size = f.size, var = jfm_var,
+ with_kanjiskip = jfm_ksp,
+ zw = sz.zw, zh = sz.zh,
+ ascent = ad.ascender,
+ descent = ad.descender,
+ chars = sz.chars, char_type = sz.char_type,
+ kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
+ chars_cbcache = {},
+ vert_activated = is_vert_enabled,
+ }
+
+ fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
+ font_metric_table[fn]=fmtable
+ tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ',
+ (cstemp==' ') and '\\space' or cstemp, '\\endcsname{\\ltj@cur'..
+ (jfm_dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}')
+ end
+end