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 set_attr = node.set_attribute
18 local round = tex.round
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 w == 'lineend' then
59 if #v.chars ~= 1 then defjfm_res= nil; return end
60 elseif type(w) == 'number' then
62 elseif type(w) == 'string' and utf.len(w)==1 then
63 real_char = true; w = utf.byte(w)
64 elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
65 real_char = true; w = utf.byte(utf.sub(w,1,1))
66 if not t.chars[-w] then
69 defjfm_res= nil; return
72 if not t.chars[w] then
75 defjfm_res= nil; return
78 if type(v.align)~='string' then
79 v.align = 'left' -- left
82 if not (type(v.width)=='number' or v.width~='prop') then
83 defjfm_res= nil; return
85 if type(v.height)~='number' then
88 if type(v.depth)~='number' then
91 if type(v.italic)~='number' then
94 if type(v.left)~='number' then
97 if type(v.down)~='number' then
104 if not v.kern then v.kern = {} end
105 if not v.glue then v.glue = {} end
106 for j in pairs(v.glue) do
107 if v.kern[j] then defjfm_res= nil; return end
109 for j,x in pairs(v.kern) do
110 if type(x)=='number' then
112 elseif type(x)=='table' then
113 v.kern[j] = {x[1], x[2] or 0}
120 t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
125 local function mult_table(old,scale) -- modified from table.fastcopy
128 for k,v in next, old do
129 if type(v) == "table" then
130 new[k] = mult_table(v,scale)
131 elseif type(v) == "number" then
132 new[k] = round(v*scale)
141 local function update_jfm_cache(j,sz)
142 if metrics[j].size_cache[sz] then return end
144 metrics[j].size_cache[sz] = t
145 t.chars = metrics[j].chars
146 t.char_type = mult_table(metrics[j].char_type, sz)
147 for i,v in pairs(t.char_type) do
148 if type(i) == 'number' then -- char_type
149 for k,w in pairs(v.glue) do
150 local g, h = node.new(id_glue), node_new(id_glue_spec)
151 v.glue[k] = {g, (w[5] and w[5]/sz or 0)}
152 h.width, h.stretch, h.shrink = w[1], w[2], w[3]
153 h.stretch_order, h.shrink_order = 0, 0
154 g.subtype = 0; g.spec = h; set_attr(g, attr_icflag, FROM_JFM +
155 (w[4] and w[4]/sz or 0));
157 for k,w in pairs(v.kern) do
162 t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
163 t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
164 t.zw = round(metrics[j].zw*sz)
165 t.zh = round(metrics[j].zh*sz)
168 luatexbase.create_callback("luatexja.find_char_class", "data",
169 function (arg, fmtable, char)
173 function find_char_class(c,m)
174 -- c: character code, m:
175 if not m then return 0 end
177 luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
180 local function load_jfont_metric()
181 if jfm_file_name=='' then
182 ltjb.package_error('luatexja',
184 'To load and define a Japanese font, a JFM must be specified.'..
185 "The JFM 'ujis' will be used for now.")
188 for j,v in ipairs(metrics) do
189 if v.name==jfm_file_name then return j end
191 luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
193 defjfm_res.name = jfm_file_name
194 table.insert(metrics, defjfm_res)
202 ------------------------------------------------------------------------
203 -- LOADING JAPANESE FONTS
204 ------------------------------------------------------------------------
208 function jfontdefX(g)
209 local t = token.get_next()
210 cstemp=token.csname_name(t)
211 if g then luatexja.is_global = '\\global' else luatexja.is_global = '' end
212 tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
215 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
218 function jfontdefY() -- for horizontal font
219 local j = load_jfont_metric()
220 local fn = font.id(cstemp)
221 local f = font.fonts[fn]
223 ltjb.package_error('luatexja',
224 "bad JFM `" .. jfm_file_name .. "'",
225 'The JFM file you specified is not valid JFM file.\n'..
226 'So defining Japanese font is cancelled.')
227 tex.sprint(cat_lp, luatexja.is_global .. '\\expandafter\\let\\csname ' ..cstemp
228 .. '\\endcsname=\\relax')
231 update_jfm_cache(j, f.size)
232 local sz = metrics[j].size_cache[f.size]
233 local fmtable = { jfm = j, size = f.size, var = jfm_var,
234 zw = sz.zw, zh = sz.zh,
235 chars = sz.chars, char_type = sz.char_type,
236 kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip,
238 fmtable.mono_flag = fonts.ids[fn].cidinfo
239 and (not string.match((fonts.ids[fn].name or ''), "+[(pwid)(palt)]"))
241 fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
242 font_metric_table[fn]=fmtable
243 tex.sprint(cat_lp, luatexja.is_global .. '\\protected\\expandafter\\def\\csname '
244 .. cstemp .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
249 local a = font_metric_table[tex.attribute[attr_curjfnt]]
251 tex.setdimen('ltj@zw', a.zw)
253 tex.setdimen('ltj@zw',0)
258 local a = font_metric_table[tex.attribute[attr_curjfnt]]
260 tex.setdimen('ltj@zh', a.zh)
262 tex.setdimen('ltj@zh',0)
266 -- extract jfm_file_name and jfm_var
267 local function extract_metric(name)
269 local tmp = utf.sub(basename, 1, 5)
270 jfm_file_name = ''; jfm_var = ''
271 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
272 basename = utf.sub(basename, 6)
274 local p = utf.find(basename, ":")
276 basename = utf.sub(basename, p+1)
279 -- now basename contains 'features' only.
282 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
283 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
284 jfm_file_name = utf.sub(basename, p+4, q-1)
285 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
286 jfm_var = utf.sub(basename, p+7, q-1)
288 if utf.len(basename)+1==q then p = nil else p = q + 1 end
293 -- replace fonts.define.read()
294 function font_callback(name, size, id, fallback)
296 -- In the present imple., we don't remove "jfm=..." from name.
297 return fallback(name, size, id)
300 ------------------------------------------------------------------------
302 ------------------------------------------------------------------------
304 -- EXT: italic correction
305 function append_italic()
306 local p = tex.nest[tex.nest.ptr].tail
307 if p and p.id==id_glyph then
309 local g = node_new(id_kern)
310 g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
311 if ltjc.is_ucs_in_japanese_char(p) then
312 f = has_attr(p, attr_curjfnt)
313 local j = font_metric_table[f]
314 local c = find_char_class(p.char, j)
315 g.kern = j.char_type[c].italic
317 g.kern = font.fonts[f].characters[p.char].italic
320 tex.attribute[attr_icflag] = -(0x7FFFFFFF)