X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=src%2Fltj-jfont.lua;h=0669b83557453aca128a146350c19e9bb931345e;hb=79e952ea40e3ac4ebd79a2b1c65c3a3745b0289a;hp=afa2889845f9fdd4182394b1db030ac9e41bc075;hpb=18d63a454075caae8a075d5f341f12e4fbf41770;p=luatex-ja%2Fluatexja.git diff --git a/src/ltj-jfont.lua b/src/ltj-jfont.lua index afa2889..0669b83 100644 --- a/src/ltj-jfont.lua +++ b/src/ltj-jfont.lua @@ -3,15 +3,16 @@ -- luatexbase.provides_module({ name = 'luatexja.jfont', - date = '2014/02/01', + date = '2014/10/03', description = 'Loader for Japanese fonts', }) module('luatexja.jfont', package.seeall) luatexja.load_module('base'); local ltjb = luatexja.base luatexja.load_module('charrange'); local ltjc = luatexja.charrange +luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm +luatexja.load_module('direction'); local ltjd = luatexja.direction -local mem_leak_glue, mem_leak_gs, mem_leak_kern = 0, 0, 0 local Dnode = node.direct or node @@ -33,6 +34,7 @@ local font_getfont = font.getfont local attr_icflag = luatexbase.attributes['ltj@icflag'] local attr_curjfnt = luatexbase.attributes['ltj@curjfnt'] +local attr_curtfnt = luatexbase.attributes['ltj@curtfnt'] local id_glyph = node.id('glyph') local id_kern = node.id('kern') local id_glue_spec = node.id('glue_spec') @@ -70,7 +72,7 @@ function define_jfm(t) else real_char = false for j,w in pairs(v.chars) do - if type(w) == 'number' then + if type(w) == 'number' and w~=-1 then real_char = true; elseif type(w) == 'string' and utf.len(w)==1 then real_char = true; w = utf.byte(w) @@ -92,10 +94,12 @@ function define_jfm(t) v.align = 'left' -- left end if real_char then - if not (type(v.width)=='number' - or (jfm_dir=='yoko' and v.width~='prop')) then + if type(v.width)~='number' and v.width~='prop' then defjfm_res= nil; return else + if v.width=='prop' and jfm_dir=='tate' then + v.width = 1.0 + end if type(v.height)~='number' then v.height = 0.0 end @@ -193,12 +197,25 @@ luatexbase.create_callback("luatexja.find_char_class", "data", function (arg, fmtable, char) return 0 end) - -function find_char_class(c,m) --- c: character code, m: - if not m then return 0 end - return m.chars[c] or - luatexbase.call_callback("luatexja.find_char_class", 0, m, c) +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 @@ -235,7 +252,7 @@ do local t = token.get_next() cstemp=token.csname_name(t) global_flag = g and '\\global' or '' - tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname') + tex.sprint(cat_lp, '\\expandafter\\font\\csname ', cstemp, '\\endcsname') end luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end) @@ -252,8 +269,8 @@ do "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 - .. '\\endcsname=\\relax') + tex.sprint(cat_lp, global_flag, '\\expandafter\\let\\csname ', cstemp, + '\\endcsname=\\relax') return end update_jfm_cache(j, f.size) @@ -265,62 +282,90 @@ do descent = ad.descender, chars = sz.chars, char_type = sz.char_type, kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip, + chars_cbcache = {}, } 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 .. '\\endcsname{\\ltj@cur' - .. (dir == 'yoko' and 'j' or 't') .. 'fnt=' .. fn .. '\\relax}') + tex.sprint(cat_lp, global_flag, '\\protected\\expandafter\\def\\csname ', + cstemp , '\\endcsname{\\ltj@cur'.. + (dir == 'yoko' and 'j' or 't') .. 'fnt', fn, '\\relax}') end end do + local get_dir_count = ltjd.get_dir_count + local dir_tate = luatexja.dir_table.dir_tate + local tex_get_attr = tex.getattribute -- PUBLIC function function get_zw() - local a = font_metric_table[tex.attribute[attr_curjfnt]] + local a = font_metric_table[ + tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)] return a and a.zw or 0 end function get_zh() - local a = font_metric_table[tex.attribute[attr_curjfnt]] + local a = font_metric_table[ + tex_get_attr((get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)] return a and a.zw or 0 end end do -- extract jfm_file_name and jfm_var + -- normalize position of 'jfm=' and 'jfmvar=' keys local function extract_metric(name) - local basename=name - local tmp = utf.sub(basename, 1, 5) jfm_file_name = ''; jfm_var = '' + local tmp, index = name:sub(1, 5), 1 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then - basename = utf.sub(basename, 6) + index = 6 end - local p = utf.find(basename, ":") - if p then - basename = utf.sub(basename, p+1) - else return + local p = name:find(":", index); index = p and (p+1) or index + while index do + local l = name:len()+1 + local q = name:find(";", index+1) or l + if name:sub(index, index+3)=='jfm=' and q>index+4 then + jfm_file_name = name:sub(index+4, q-1) + if l~=q then + name = name:sub(1,index-1) .. name:sub(q+1) + else + name = name:sub(1,index-1) + index = nil + end + elseif name:sub(index, index+6)=='jfmvar=' and q>index+6 then + jfm_var = name:sub(index+7, q-1) + if l~=q then + name = name:sub(1,index-1) .. name:sub(q+1) + else + name = name:sub(1,index-1) + index = nil + end + else + index = (l~=q) and (q+1) or nil + end end - -- now basename contains 'features' only. - p=1 - while p do - local q = utf.find(basename, ";", p+1) or utf.len(basename)+1 - if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then - jfm_file_name = utf.sub(basename, p+4, q-1) - elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then - jfm_var = utf.sub(basename, p+7, q-1) + if jfm_file_name~='' then + local l = name:sub(-1) + name = name + .. ((l==':' or l==';') and '' or ';') + .. 'jfm=' .. jfm_file_name + if jfm_var~='' then + name = name .. 'jfmvar=' .. jfm_var end - if utf.len(basename)+1==q then p = nil else p = q + 1 end end - return + return name end - -- replace fonts.define.read() - luatexbase.add_to_callback('luatexja.define_font', - function (res, name) - extract_metric(name) - end, - 'extract_jfm_name', 1) + -- define_font callback + local otfl_fdr = fonts.definers.read + local ltjr_font_callback = ltjr.font_callback + function luatexja.font_callback(name, size, id) + local new_name = extract_metric(name) + local res = ltjr_font_callback(new_name, size, id, otfl_fdr) + luatexbase.call_callback('luatexja.define_font', res, new_name, size, id) + return res + end + luatexbase.create_callback('luatexja.define_font', 'simple', function (n) return n end) + luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1) end ------------------------------------------------------------------------ @@ -496,17 +541,21 @@ do end -- EXT - function output_alt_font_cmd(bbase) + function output_alt_font_cmd(dir, bbase) alt_font_base = bbase - alt_font_base_num = tex.getattribute(attr_curjfnt) + if dir == 't' then + alt_font_base_num = tex.getattribute(attr_curtfnt) + else + alt_font_base_num = tex.getattribute(attr_curjfnt) + end local t = alt_font_table[alt_font_base_num] if t then for i,_ in pairs(t) do t[i]=nil end end t = alt_font_table_latex[bbase] if t then - for i,_ in pairs(t) do - tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux{' .. i .. '}') + for i,_ in pairs(t) do + tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux' .. dir .. '{' .. i .. '}') end end end @@ -554,32 +603,94 @@ end ------------------------------------------------------------------------ --- MISC +-- 縦書き用字形への変換テーブル ------------------------------------------------------------------------ +local font_vert_table = {} -- key: fontnumber +do + local font_vert_basename = {} -- key: basename + local function add_feature_table(tname, src, dest) + for i,v in pairs(src) do + if type(v.slookups)=='table' then + local s = v.slookups[tname] + if s and not dest[i] then + dest[i] = s + end + end + end + end -local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct --- EXT: italic correction -function append_italic() - local p = to_direct(tex.nest[tex.nest.ptr].tail) - if p and getid(p)==id_glyph then - local f = getfont(p) - local g = node_new(id_kern) - setfield(g, 'subtype', 1) - set_attr(g, attr_icflag, ITALIC) - if is_ucs_in_japanese_char(p) then - f = has_attr(p, attr_curjfnt) - local j = font_metric_table[f] - setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic) - else - local h = font_getfont(f) - if h then - setfield(g, 'kern', h.characters[getchar(p)].italic) + local function prepare_vert_data(n, id) + -- test if already loaded + if type(id)=='number' then -- sometimes id is an integer + return + elseif (not id) or font_vert_table[n] then return + end + local fname = id.filename + local bname = file.basename(fname) + if not fname then + font_vert_table[n] = {}; return + elseif font_vert_basename[bname] then + font_vert_table[n] = font_vert_basename[bname]; return + end + local vtable = {} + local a = id.resources.sequences + if a then + local s = id.shared.rawdata.descriptions + for i,v in pairs(a) do + if v.features.vert then + add_feature_table(v.subtables[1], s, vtable) + end + end + end + font_vert_basename[bname] = vtable + font_vert_table[n] = vtable + end + -- 縦書き用字形への変換 + function get_vert_glyph(n, chr) + local fn = font_vert_table[n] + return fn and fn[chr] or chr + end + luatexbase.add_to_callback('luatexja.define_font', + function (res, name, size, id) + prepare_vert_data(id, res) + end, + 'prepare_vert_data', 1) + + local function a (n, dat) font_vert_table[n] = dat end + ltjr.vert_addfunc = a + +end + +------------------------------------------------------------------------ +-- MISC +------------------------------------------------------------------------ +do + local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct + local tex_set_attr = tex.setattribute + local font = font + -- EXT: italic correction + function append_italic() + local p = to_direct(tex.nest[tex.nest.ptr].tail) + if p and getid(p)==id_glyph then + local f = getfont(p) + local g = node_new(id_kern) + setfield(g, 'subtype', 1) + set_attr(g, attr_icflag, ITALIC) + if is_ucs_in_japanese_char(p) then + f = has_attr(p, attr_curjfnt) + local j = font_metric_table[f] + setfield(g, 'kern', j.char_type[find_char_class(getchar(p), j)].italic) else - tex.attribute[attr_icflag] = 0 - return node_free(g) + local h = font_getfont(f) or font.fonts[f] + if h then + setfield(g, 'kern', h.characters[getchar(p)].italic) + else + tex_set_attr(attr_icflag, 0) + return node_free(g) + end end + node_write(g) + tex_set_attr(attr_icflag, 0) end - node_write(g) - tex.attribute[attr_icflag] = 0 end end