4 luatexbase.provides_module({
5 name = 'luatexja.jfont',
8 description = 'Loader for Japanese fonts',
10 module('luatexja.jfont', package.seeall)
12 require('luatexja.base'); local ltjb = luatexja.base
13 require('luatexja.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 local jfm_file_name, jfm_var
35 function define_jfm(t)
36 local real_char -- Does current character class have the 'real' character?
38 defjfm_res= nil; return
39 elseif type(t.zw)~='number' or type(t.zh)~='number' then
40 defjfm_res= nil; return
42 t.char_type = {}; t.chars = {}
43 for i,v in pairs(t) do
44 if type(i) == 'number' then -- char_type
46 if i ~= 0 then defjfm_res= nil; return end
50 for j,w in pairs(v.chars) do
51 if w == 'lineend' then
52 if #v.chars ~= 1 then defjfm_res= nil; return end
53 elseif type(w) == 'number' then
55 elseif type(w) == 'string' and utf.len(w)==1 then
56 real_char = true; w = utf.byte(w)
58 if not t.chars[w] then
61 defjfm_res= nil; return
65 if not (type(v.width)=='number' or v.width~='prop') then
66 defjfm_res= nil; return
68 if type(v.height)~='number' then
71 if type(v.depth)~='number' then
74 if type(v.italic)~='number' then
77 if type(v.left)~='number' then
80 if type(v.down)~='number' then
83 if type(v.align)=='nil' then
90 if v.kern and v.glue then
91 for j,w in pairs(v.glue) do
92 if v.kern[j] then defjfm_res= nil; return end
103 local function mult_table(old,scale) -- modified from table.fastcopy
106 for k,v in next, old do
107 if type(v) == "table" then
108 new[k] = mult_table(v,scale)
109 elseif type(v) == "number" then
110 new[k] = round(v*scale)
119 local function update_jfm_cache(j,sz)
120 if metrics[j].size_cache[sz] then return end
121 metrics[j].size_cache[sz] = {}
122 metrics[j].size_cache[sz].char_type = mult_table(metrics[j].char_type, sz)
123 metrics[j].size_cache[sz].kanjijskip = mult_table(metrics[j].kanjiskip, sz)
124 metrics[j].size_cache[sz].xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
125 metrics[j].size_cache[sz].zw = round(metrics[j].zw*sz)
126 metrics[j].size_cache[sz].zh = round(metrics[j].zh*sz)
129 function find_char_class(c,m)
130 -- c: character code, m
131 if not metrics[m] then return 0 end
132 return metrics[m].chars[c] or 0
135 local function load_jfont_metric()
136 if jfm_file_name=='' then
137 ltjb.package_error('luatexja',
139 {'To load and define a Japanese font, a JFM must be specified.',
140 "The JFM 'ujis' will be used for now."})
143 for j,v in ipairs(metrics) do
144 if v.name==jfm_file_name then return j end
146 ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
148 defjfm_res.name = jfm_file_name
149 table.insert(metrics, defjfm_res)
157 ------------------------------------------------------------------------
158 -- LOADING JAPANESE FONTS
159 ------------------------------------------------------------------------
163 function jfontdefX(g)
164 local t = token.get_next()
165 cstemp=token.csname_name(t)
166 if g then ltj.is_global = '\\global' else ltj.is_global = '' end
167 tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
171 function jfontdefY() -- for horizontal font
172 local j = load_jfont_metric()
173 local fn = font.id(cstemp)
174 local f = font.fonts[fn]
176 ltjb.package_error('luatexja',
177 "bad JFM `" .. jfm_file_name .. "'",
178 'The JFM file you specified is not valid JFM file.\n'..
179 'So defining Japanese font is cancelled.')
180 tex.sprint(cat_lp, ltj.is_global .. '\\expandafter\\let\\csname ' ..cstemp
181 .. '\\endcsname=\\relax')
184 font_metric_table[fn]={}
185 font_metric_table[fn].jfm=j
186 font_metric_table[fn].size=f.size
187 font_metric_table[fn].var=jfm_var
188 update_jfm_cache(j, f.size)
189 tex.sprint(cat_lp, ltj.is_global .. '\\protected\\expandafter\\def\\csname '
190 .. cstemp .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
195 local a = font_metric_table[tex.attribute[attr_curjfnt]]
197 tex.setdimen('ltj@zw', metrics[a.jfm].size_cache[a.size].zw)
199 tex.setdimen('ltj@zw',0)
204 local a = font_metric_table[tex.attribute[attr_curjfnt]]
206 tex.setdimen('ltj@zh', metrics[a.jfm].size_cache[a.size].zh)
208 tex.setdimen('ltj@zh',0)
212 -- extract jfm_file_name and jfm_var
213 local function extract_metric(name)
215 local tmp = utf.sub(basename, 1, 5)
216 jfm_file_name = ''; jfm_var = ''
217 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
218 basename = utf.sub(basename, 6)
220 local p = utf.find(basename, ":")
222 basename = utf.sub(basename, p+1)
225 -- now basename contains 'features' only.
228 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
229 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
230 jfm_file_name = utf.sub(basename, p+4, q-1)
231 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
232 jfm_var = utf.sub(basename, p+7, q-1)
234 if utf.len(basename)+1==q then p = nil else p = q + 1 end
239 -- replace fonts.define.read()
240 local ljft_dr_orig = fonts.define.read
241 function fonts.define.read(name, size, id)
243 -- In the present imple., we don't remove "jfm=..." from name.
244 return ljft_dr_orig(name, size, id)
247 ------------------------------------------------------------------------
249 ------------------------------------------------------------------------
251 -- EXT: italic correction
252 function append_italic()
253 local p = tex.nest[tex.nest.ptr].tail
254 if p and p.id==id_glyph then
256 local g = node_new(id_kern)
257 g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
258 if ltjc.is_ucs_in_japanese_char(p) then
259 f = has_attr(p, attr_curjfnt)
260 local j = font_metric_table[f]
261 local c = find_char_class(p.char, j.jfm)
262 g.kern = metrics[j.jfm].size_cache[j.size].char_type[c].italic
264 g.kern = font.fonts[f].characters[p.char].italic