4 luatexbase.provides_module({
5 name = 'luatexja.jfont',
8 description = 'Loader for Japanese fonts',
10 module('luatexja.jfont', package.seeall)
12 luatexja.load_module('base'); local ltjb = luatexja.base
13 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
15 local node_new = node.new
16 local has_attr = node.has_attribute
17 local round = tex.round
19 local attr_icflag = luatexbase.attributes['ltj@icflag']
20 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
21 local id_glyph = node.id('glyph')
22 local id_kern = node.id('kern')
23 local cat_lp = luatexbase.catcodetables['latex-package']
25 ------------------------------------------------------------------------
27 ------------------------------------------------------------------------
29 metrics={} -- this table stores all metric informations
30 font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
32 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
34 local jfm_file_name, jfm_var
37 function define_jfm(t)
38 local real_char -- Does current character class have the 'real' character?
40 defjfm_res= nil; return
41 elseif type(t.zw)~='number' or type(t.zh)~='number' then
42 defjfm_res= nil; return
44 t.char_type = {}; t.chars = {}
45 for i,v in pairs(t) do
46 if type(i) == 'number' then -- char_type
48 if i ~= 0 then defjfm_res= nil; return end
52 for j,w in pairs(v.chars) do
53 if w == 'lineend' then
54 if #v.chars ~= 1 then defjfm_res= nil; return end
55 elseif type(w) == 'number' then
57 elseif type(w) == 'string' and utf.len(w)==1 then
58 real_char = true; w = utf.byte(w)
59 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
60 real_char = true; w = -utf.byte(utf.sub(w,1,1))
62 if not t.chars[w] then
65 defjfm_res= nil; return
69 if not (type(v.width)=='number' or v.width~='prop') then
70 defjfm_res= nil; return
72 if type(v.height)~='number' then
75 if type(v.depth)~='number' then
78 if type(v.italic)~='number' then
81 if type(v.left)~='number' then
84 if type(v.down)~='number' then
87 if type(v.align)=='nil' then
94 if not v.kern then v.kern = {} end
95 if not v.glue then v.glue = {} end
96 for j in pairs(v.glue) do
97 if v.kern[j] then defjfm_res= nil; return end
103 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
108 local function mult_table(old,scale) -- modified from table.fastcopy
111 for k,v in next, old do
112 if type(v) == "table" then
113 new[k] = mult_table(v,scale)
114 elseif type(v) == "number" then
115 new[k] = round(v*scale)
124 local function update_jfm_cache(j,sz)
125 if metrics[j].size_cache[sz] then return end
126 metrics[j].size_cache[sz] = {}
127 metrics[j].size_cache[sz].char_type = mult_table(metrics[j].char_type, sz)
128 metrics[j].size_cache[sz].kanjiskip = mult_table(metrics[j].kanjiskip, sz)
129 metrics[j].size_cache[sz].xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
130 metrics[j].size_cache[sz].zw = round(metrics[j].zw*sz)
131 metrics[j].size_cache[sz].zh = round(metrics[j].zh*sz)
134 luatexbase.create_callback("luatexja.find_char_class", "data",
135 function (arg, fmtable, char)
139 function find_char_class(c,m)
140 -- c: character code, m: index in font_metric table
141 if not metrics[m.jfm] then return 0 end
142 return metrics[m.jfm].chars[c] or
143 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
146 local function load_jfont_metric()
147 if jfm_file_name=='' then
148 ltjb.package_error('luatexja',
150 'To load and define a Japanese font, a JFM must be specified.'..
151 "The JFM 'ujis' will be used for now.")
154 for j,v in ipairs(metrics) do
155 if v.name==jfm_file_name then return j end
157 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
159 defjfm_res.name = jfm_file_name
160 table.insert(metrics, defjfm_res)
168 ------------------------------------------------------------------------
169 -- LOADING JAPANESE FONTS
170 ------------------------------------------------------------------------
174 function jfontdefX(g)
175 local t = token.get_next()
176 cstemp=token.csname_name(t)
177 if g then luatexja.is_global = '\\global' else luatexja.is_global = '' end
178 tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
181 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
184 function jfontdefY() -- for horizontal font
185 local j = load_jfont_metric()
186 local fn = font.id(cstemp)
187 local f = font.fonts[fn]
189 ltjb.package_error('luatexja',
190 "bad JFM `" .. jfm_file_name .. "'",
191 'The JFM file you specified is not valid JFM file.\n'..
192 'So defining Japanese font is cancelled.')
193 tex.sprint(cat_lp, luatexja.is_global .. '\\expandafter\\let\\csname ' ..cstemp
194 .. '\\endcsname=\\relax')
197 update_jfm_cache(j, f.size)
198 local fmtable = { jfm = j, size = f.size, var = jfm_var }
199 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
200 font_metric_table[fn]=fmtable
201 tex.sprint(cat_lp, luatexja.is_global .. '\\protected\\expandafter\\def\\csname '
202 .. cstemp .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
207 local a = font_metric_table[tex.attribute[attr_curjfnt]]
209 tex.setdimen('ltj@zw', metrics[a.jfm].size_cache[a.size].zw)
211 tex.setdimen('ltj@zw',0)
216 local a = font_metric_table[tex.attribute[attr_curjfnt]]
218 tex.setdimen('ltj@zh', metrics[a.jfm].size_cache[a.size].zh)
220 tex.setdimen('ltj@zh',0)
224 -- extract jfm_file_name and jfm_var
225 local function extract_metric(name)
227 local tmp = utf.sub(basename, 1, 5)
228 jfm_file_name = ''; jfm_var = ''
229 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
230 basename = utf.sub(basename, 6)
232 local p = utf.find(basename, ":")
234 basename = utf.sub(basename, p+1)
237 -- now basename contains 'features' only.
240 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
241 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
242 jfm_file_name = utf.sub(basename, p+4, q-1)
243 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
244 jfm_var = utf.sub(basename, p+7, q-1)
246 if utf.len(basename)+1==q then p = nil else p = q + 1 end
251 -- replace fonts.define.read()
252 local ljft_dr_orig = fonts.define.read
253 function fonts.define.read(name, size, id)
255 -- In the present imple., we don't remove "jfm=..." from name.
256 return ljft_dr_orig(name, size, id)
259 ------------------------------------------------------------------------
261 ------------------------------------------------------------------------
263 -- EXT: italic correction
264 function append_italic()
265 local p = tex.nest[tex.nest.ptr].tail
266 if p and p.id==id_glyph then
268 local g = node_new(id_kern)
269 g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
270 if ltjc.is_ucs_in_japanese_char(p) then
271 f = has_attr(p, attr_curjfnt)
272 local j = font_metric_table[f]
273 local c = find_char_class(p.char, j)
274 g.kern = metrics[j.jfm].size_cache[j.size].char_type[c].italic
276 g.kern = font.fonts[f].characters[p.char].italic
279 tex.attribute[attr_icflag] = -(0x7FFFFFFF)