4 luatexbase.provides_module({
5 name = 'luatexja.jfont',
7 description = 'Loader for Japanese fonts',
9 module('luatexja.jfont', package.seeall)
11 luatexja.load_module('base'); local ltjb = luatexja.base
12 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
14 local node_new = node.new
15 local has_attr = node.has_attribute
16 local set_attr = node.set_attribute
17 local round = tex.round
18 local getfont = font.getfont
20 local attr_icflag = luatexbase.attributes['ltj@icflag']
21 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
22 local id_glyph = node.id('glyph')
23 local id_kern = node.id('kern')
24 local id_glue_spec = node.id('glue_spec')
25 local id_glue = node.id('glue')
26 local cat_lp = luatexbase.catcodetables['latex-package']
27 local ITALIC = luatexja.icflag_table.ITALIC
28 local FROM_JFM = luatexja.icflag_table.FROM_JFM
30 ------------------------------------------------------------------------
32 ------------------------------------------------------------------------
34 metrics={} -- this table stores all metric informations
35 font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
37 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
39 local jfm_file_name, jfm_var
42 function define_jfm(t)
43 local real_char -- Does current character class have the 'real' character?
45 defjfm_res= nil; return
46 elseif type(t.zw)~='number' or type(t.zh)~='number' then
47 defjfm_res= nil; return
49 t.char_type = {}; t.chars = {}
50 for i,v in pairs(t) do
51 if type(i) == 'number' then -- char_type
53 if i ~= 0 then defjfm_res= nil; return end
57 for j,w in pairs(v.chars) do
58 if type(w) == 'number' then
60 elseif type(w) == 'string' and utf.len(w)==1 then
61 real_char = true; w = utf.byte(w)
62 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
63 real_char = true; w = utf.byte(utf.sub(w,1,1))
64 if not t.chars[-w] then
67 defjfm_res= nil; return
70 if not t.chars[w] then
73 defjfm_res= nil; return
76 if type(v.align)~='string' then
77 v.align = 'left' -- left
80 if not (type(v.width)=='number' or v.width~='prop') then
81 defjfm_res= nil; return
83 if type(v.height)~='number' then
86 if type(v.depth)~='number' then
89 if type(v.italic)~='number' then
92 if type(v.left)~='number' then
95 if type(v.down)~='number' then
102 v.kern = v.kern or {}; v.glue = v.glue or {}
103 for j in pairs(v.glue) do
104 if v.kern[j] then defjfm_res= nil; return end
106 for j,x in pairs(v.kern) do
107 if type(x)=='number' then
109 elseif type(x)=='table' then
110 v.kern[j] = {x[1], x[2] or 0}
117 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
123 local function mult_table(old,scale) -- modified from table.fastcopy
126 for k,v in next, old do
127 if type(v) == "table" then
128 new[k] = mult_table(v,scale)
129 elseif type(v) == "number" then
130 new[k] = round(v*scale)
139 function update_jfm_cache(j,sz)
140 if metrics[j].size_cache[sz] then return end
142 metrics[j].size_cache[sz] = t
143 t.chars = metrics[j].chars
144 t.char_type = mult_table(metrics[j].char_type, sz)
145 for i,v in pairs(t.char_type) do
146 if type(i) == 'number' then -- char_type
147 for k,w in pairs(v.glue) do
148 local g, h = node.new(id_glue), node_new(id_glue_spec)
149 v[k] = {true, g, (w[5] and w[5]/sz or 0)}
150 h.width, h.stretch, h.shrink = w[1], w[2], w[3]
151 h.stretch_order, h.shrink_order = 0, 0
152 g.subtype = 0; g.spec = h;
153 set_attr(g, attr_icflag, FROM_JFM + (w[4] and w[4]/sz or 0));
155 for k,w in pairs(v.kern) do
156 local g = node.new(id_kern)
157 g.kern, g.subtype = w[1], 1
158 set_attr(g, attr_icflag, FROM_JFM)
159 v[k] = {false, g, w[2]/sz}
163 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
164 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
165 t.zw = round(metrics[j].zw*sz)
166 t.zh = round(metrics[j].zh*sz)
169 local update_jfm_cache = update_jfm_cache
170 luatexbase.create_callback("luatexja.find_char_class", "data",
171 function (arg, fmtable, char)
175 function find_char_class(c,m)
176 -- c: character code, m:
177 if not m then return 0 end
179 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
183 ------------------------------------------------------------------------
184 -- LOADING JAPANESE FONTS
185 ------------------------------------------------------------------------
189 local global_flag -- true if \globaljfont, false if \jfont
190 local function load_jfont_metric()
191 if jfm_file_name=='' then
192 ltjb.package_error('luatexja',
194 'To load and define a Japanese font, a JFM must be specified.'..
195 "The JFM 'ujis' will be used for now.")
198 for j,v in ipairs(metrics) do
199 if v.name==jfm_file_name then return j end
201 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
203 defjfm_res.name = jfm_file_name
204 table.insert(metrics, defjfm_res)
212 function jfontdefX(g)
213 local t = token.get_next()
214 cstemp=token.csname_name(t)
215 global_flag = g and '\\global' or ''
216 tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
219 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
222 function jfontdefY() -- for horizontal font
223 local j = load_jfont_metric()
224 local fn = font.id(cstemp)
225 local f = getfont(fn)
227 ltjb.package_error('luatexja',
228 "bad JFM `" .. jfm_file_name .. "'",
229 'The JFM file you specified is not valid JFM file.\n'..
230 'So defining Japanese font is cancelled.')
231 tex.sprint(cat_lp, global_flag .. '\\expandafter\\let\\csname ' ..cstemp
232 .. '\\endcsname=\\relax')
235 update_jfm_cache(j, f.size)
236 local sz = metrics[j].size_cache[f.size]
237 local fmtable = { jfm = j, size = f.size, var = jfm_var,
238 zw = sz.zw, zh = sz.zh,
239 chars = sz.chars, char_type = sz.char_type,
240 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
243 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
244 font_metric_table[fn]=fmtable
245 tex.sprint(cat_lp, global_flag .. '\\protected\\expandafter\\def\\csname '
246 .. cstemp .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
253 local a = font_metric_table[tex.attribute[attr_curjfnt]]
254 tex.setdimen('ltj@zw', a and a.zw or 0)
258 local a = font_metric_table[tex.attribute[attr_curjfnt]]
259 tex.setdimen('ltj@zh', a and a.zh or 0)
264 -- extract jfm_file_name and jfm_var
265 local function extract_metric(name)
267 local tmp = utf.sub(basename, 1, 5)
268 jfm_file_name = ''; jfm_var = ''
269 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
270 basename = utf.sub(basename, 6)
272 local p = utf.find(basename, ":")
274 basename = utf.sub(basename, p+1)
277 -- now basename contains 'features' only.
280 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
281 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
282 jfm_file_name = utf.sub(basename, p+4, q-1)
283 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
284 jfm_var = utf.sub(basename, p+7, q-1)
286 if utf.len(basename)+1==q then p = nil else p = q + 1 end
291 -- replace fonts.define.read()
292 function font_callback(name, size, id, fallback)
294 -- In the present imple., we don't remove "jfm=..." from name.
295 return fallback(name, size, id)
299 ------------------------------------------------------------------------
301 ------------------------------------------------------------------------
303 local kyenc_list, ktenc_list = {}, {}
304 function add_kyenc_list(enc) kyenc_list[enc] = 'true ' end
305 function add_ktenc_list(enc) ktenc_list[enc] = 'true ' end
306 function is_kyenc(enc)
307 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
309 function is_kyenc(enc)
310 tex.sprint(cat_lp, '\\let\\ifin@\\if' .. (kyenc_list[enc] or 'false '))
312 function is_kenc(enc)
313 tex.sprint(cat_lp, '\\let\\ifin@\\if'
314 .. (kyenc_list[enc] or ktenc_list[enc] or 'false '))
317 local kfam_list, Nkfam_list = {}, {}
318 function add_kfam_list(enc, fam)
319 if not kfam_list[enc] then kfam_list[enc] = {} end
320 kfam_list[enc][fam] = 'true '
322 function add_Nkfam_list(enc, fam)
323 if not Nkfam_list[enc] then Nkfam_list[enc] = {} end
324 Nkfam_list[enc][fam] = 'true '
326 function is_kfam(enc, fam)
327 tex.sprint(cat_lp, '\\let\\ifin@\\if'
328 .. (kfam_list[enc] and kfam_list[enc][fam] or 'false ')) end
329 function is_Nkfam(enc, fam)
330 tex.sprint(cat_lp, '\\let\\ifin@\\if'
331 .. (Nkfam_list[enc] and Nkfam_list[enc][fam] or 'false ')) end
333 local ffam_list, Nffam_list = {}, {}
334 function add_ffam_list(enc, fam)
335 if not ffam_list[enc] then ffam_list[enc] = {} end
336 ffam_list[enc][fam] = 'true '
338 function add_Nffam_list(enc, fam)
339 if not Nffam_list[enc] then Nffam_list[enc] = {} end
340 Nffam_list[enc][fam] = 'true '
342 function is_ffam(enc, fam)
343 tex.sprint(cat_lp, '\\let\\ifin@\\if'
344 .. (ffam_list[enc] and ffam_list[enc][fam] or 'false ')) end
345 function is_Nffam(enc, fam)
346 tex.sprint(cat_lp, '\\let\\ifin@\\if'
347 .. (Nffam_list[enc] and Nffam_list[enc][fam] or 'false ')) end
349 ------------------------------------------------------------------------
351 ------------------------------------------------------------------------
353 local alt_font_table = alt_font_table
354 local attr_curaltfnt = {}
355 local ucs_out = 0x110000
357 ------ for TeX interface
359 function set_alt_font(b,e,ind,bfnt)
360 -- ind: 新フォント, bfnt: 基底フォント
361 if b>e then b, e = e, b end
363 ltjb.package_eror('luatexja',
364 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
365 'I take the intersection with [0x80, 0x10ffff].')
366 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
367 elseif e<0 then -- b<e<0
369 elseif b<0x80 or e>=ucs_out then
370 ltjb.package_warning('luatexja',
371 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
372 'I take the intersection with [0x80, 0x10ffff].')
373 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
375 if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
376 local t = alt_font_table[bfnt]
377 local ac = getfont(ind).characters
378 if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
379 if e>=0 then -- character range
381 if ac[i]then t[i]=ind end
385 local tx = font_metric_table[bfnt].chars
386 for i,v in pairs(tx) do
387 if b<=v and v<=e and ac[i] then t[i]=ind end
393 function clear_alt_font(bfnt)
394 if alt_font_table[bfnt] then
395 local t = alt_font_table[bfnt]
396 for i,_ in pairs(t) do t[i]=nil; end
400 ------ used in ltjp.suppress_hyphenate_ja callback
401 function replace_altfont(pf, pc)
402 return (alt_font_table[pf] and alt_font_table[pf][pc])
403 and alt_font_table[pf][pc] or pf
406 ------ for LaTeX interface
408 local alt_font_table_latex = {}
411 function clear_alt_font_latex(bbase)
412 local t = alt_font_table_latex[bbase]
414 for j,v in pairs(t) do t[j] = nil end
419 function set_alt_font_latex(b,e,ind,bbase)
420 -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
421 if b>e then b, e = e, b end
423 ltjb.package_eror('luatexja',
424 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
425 'I take the intersection with [0x80, 0x10ffff].')
426 b, e = math.max(0x80,b),math.min(ucs_out-1,e)
427 elseif e<0 then -- b<e<0
429 elseif b<0x80 or e>=ucs_out then
430 ltjb.package_warning('luatexja',
431 'bad character range ([' .. b .. ',' .. e .. ']). ' ..
432 'I take the intersection with [0x80, 0x10ffff].')
433 b, e = math.max(0x80,b), math.min(ucs_out-1,e)
436 if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
437 local t = alt_font_table_latex[bbase]
438 if not t[ind] then t[ind] = {} end
440 for j,v in pairs(t) do
441 if v[i] then -- remove old entry
442 if j~=ind then v[i]=nil end; break
447 -- remove the empty tables
448 for j,v in pairs(t) do
449 local flag_clear = true
450 for k,_ in pairs(v) do flag_clear = false; break end
451 if flag_clear then t[j]=nil end
453 if ind==bbase then t[bbase] = nil end
456 -- ここから先は 新 \selectfont の内部でしか実行されない
458 local alt_font_base, alt_font_base_num
461 function does_alt_set(bbase)
462 aftl_base = alt_font_table_latex[bbase]
463 tex.sprint(cat_lp, '\\if' .. (aftl_base and 'true' or 'false'))
466 function print_aftl_address()
467 tex.sprint(cat_lp, ';ltjaltfont' .. tostring(aftl_base):sub(8))
471 function output_alt_font_cmd(bbase)
472 alt_font_base = bbase
473 alt_font_base_num = tex.getattribute(attr_curjfnt)
474 local t = alt_font_table[alt_font_base_num]
476 for i,_ in pairs(t) do t[i]=nil end
478 t = alt_font_table_latex[bbase]
480 for i,_ in pairs(t) do
481 tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux{' .. i .. '}')
487 function pickup_alt_font_a(size_str)
488 local t = alt_font_table_latex[alt_font_base]
490 for i,v in pairs(t) do
491 tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
492 .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
497 local function pickup_alt_font_class(class, afnt_num, afnt_chars)
498 local t = alt_font_table[alt_font_base_num]
499 local tx = font_metric_table[alt_font_base_num].chars
500 for i,v in pairs(tx) do
501 if v==class and afnt_chars[i] then t[i]=afnt_num end
506 function pickup_alt_font_b(afnt_num, afnt_base)
507 local t = alt_font_table[alt_font_base_num]
508 local ac = getfont(afnt_num).characters
509 if not t then t = {}; alt_font_table[alt_font_base_num] = t end
510 for i,v in pairs(alt_font_table_latex[alt_font_base]) do
511 if i == afnt_base then
512 for j,_ in pairs(v) do
514 if ac[j] then t[j]=afnt_num end
515 else -- -n (n>=1) means that the character class n,
516 -- which is defined in the JFM
517 pickup_alt_font_class(-j, afnt_num, ac)
528 ------------------------------------------------------------------------
530 ------------------------------------------------------------------------
532 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
533 -- EXT: italic correction
534 function append_italic()
535 local p = tex.nest[tex.nest.ptr].tail
536 if p and p.id==id_glyph then
538 local g = node_new(id_kern)
539 g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
540 if is_ucs_in_japanese_char(p) then
541 f = has_attr(p, attr_curjfnt)
542 local j = font_metric_table[f]
543 g.kern = j.char_type[find_char_class(p.char, j)].italic
547 g.kern = h.characters[p.char].italic
549 tex.attribute[attr_icflag] = -(0x7FFFFFFF)
554 tex.attribute[attr_icflag] = -(0x7FFFFFFF)