1 local node_new = node.new
2 local has_attr = node.has_attribute
3 local round = tex.round
5 local attr_icflag = luatexbase.attributes['ltj@icflag']
6 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
7 local id_glyph = node.id('glyph')
8 local id_kern = node.id('kern')
11 ------------------------------------------------------------------------
12 -- LOADING JFM (prefix: ljfm)
13 ------------------------------------------------------------------------
15 ltj.metrics={} -- this table stores all metric informations
16 ltj.font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
18 local jfm_file_name, jfm_var
21 function ltj.define_jfm(t)
22 local real_char -- Does current character class have the 'real' character?
24 defjfm_res= nil; return
25 elseif type(t.zw)~='number' or type(t.zh)~='number' then
26 defjfm_res= nil; return
28 t.char_type = {}; t.chars = {}
29 for i,v in pairs(t) do
30 if type(i) == 'number' then -- char_type
32 if i ~= 0 then defjfm_res= nil; return end
36 for j,w in pairs(v.chars) do
37 if w == 'lineend' then
38 if #v.chars ~= 1 then defjfm_res= nil; return end
39 elseif type(w) == 'number' then
41 elseif type(w) == 'string' and utf.len(w)==1 then
42 real_char = true; w = utf.byte(w)
44 if not t.chars[w] then
47 defjfm_res= nil; return
51 if not (type(v.width)=='number' or v.width~='prop') then
52 defjfm_res= nil; return
53 elseif type(v.height)~='number' or type(v.depth)~='number' then
54 defjfm_res= nil; return
59 if v.kern and v.glue then
60 for j,w in pairs(v.glue) do
61 if v.kern[j] then defjfm_res= nil; return end
71 local function ljfm_find_char_class(c,m)
72 -- c: character code, m
73 if not ltj.metrics[m] then return 0 end
74 return ltj.metrics[m].chars[c] or 0
76 ltj.int_find_char_class = ljfm_find_char_class
78 local function ljfm_load_jfont_metric()
79 if jfm_file_name=='' then
80 ltj.error('no JFM specified',
81 {[1]='To load and define a Japanese font, the name of JFM must be specified.',
82 [2]="The JFM 'ujis' will be used for now."})
85 for j,v in ipairs(ltj.metrics) do
86 if v.name==jfm_file_name then return j end
88 ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
90 defjfm_res.name = jfm_file_name
91 table.insert(ltj.metrics,defjfm_res)
99 ------------------------------------------------------------------------
100 -- LOADING JAPANESE FONTS (prefix: ljft)
101 ------------------------------------------------------------------------
105 function ltj.ext_jfontdefX(g)
106 local t = token.get_next()
107 cstemp=token.csname_name(t)
108 if g then ltj.is_global = '\\global' else ltj.is_global = '' end
109 tex.sprint('\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
113 function ltj.ext_jfontdefY() -- for horizontal font
114 local j = ljfm_load_jfont_metric()
115 local fn = font.id(cstemp)
116 local f = font.fonts[fn]
118 ltj.error("bad JFM '" .. jfm_file_name .. "'",
119 {[1]='The JFM file you specified is not valid JFM file.',
120 [2]='Defining Japanese font is cancelled.'})
121 tex.sprint(ltj.is_global .. '\\expandafter\\let\\csname '
122 .. cstemp .. '\\endcsname=\\relax')
125 ltj.font_metric_table[fn]={}
126 ltj.font_metric_table[fn].jfm=j
127 ltj.font_metric_table[fn].size=f.size
128 ltj.font_metric_table[fn].var=jfm_var
129 tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
130 .. cstemp .. '\\endcsname'
131 .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
132 .. ' \\zw=' .. round(f.size*ltj.metrics[j].zw) .. 'sp'
133 .. '\\zh=' .. round(f.size*ltj.metrics[j].zh) .. 'sp\\relax}')
136 -- extract jfm_file_name and jfm_var
137 local function ljft_extract_metric(name)
139 local tmp = utf.sub(basename, 1, 5)
140 jfm_file_name = ''; jfm_var = ''
141 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
142 basename = utf.sub(basename, 6)
144 local p = utf.find(basename, ":")
146 basename = utf.sub(basename, p+1)
149 -- now basename contains 'features' only.
152 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
153 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
154 jfm_file_name = utf.sub(basename, p+4, q-1)
155 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
156 jfm_var = utf.sub(basename, p+7, q-1)
158 if utf.len(basename)+1==q then p = nil else p = q + 1 end
163 -- replace fonts.define.read()
164 local ljft_dr_orig = fonts.define.read
165 function fonts.define.read(name, size, id)
166 ljft_extract_metric(name)
167 -- In the present imple., we don't remove "jfm=..." from name.
168 return ljft_dr_orig(name, size, id)
171 ------------------------------------------------------------------------
173 ------------------------------------------------------------------------
175 -- EXT: italic correction
176 function ltj.ext_append_italic()
177 local p = tex.nest[tex.nest.ptr].tail
178 if p and p.id==id_glyph then
180 local g = node_new(id_kern)
181 g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
182 if luatexja.charrange.is_ucs_in_japanese_char(p) then
183 f = has_attr(p, attr_curjfnt)
184 local j = ltj.font_metric_table[f]
185 local c = ljfm_find_char_class(p.char, j.jfm)
186 g.kern = round(j.size * ltj.metrics[j.jfm].char_type[c].italic)
188 g.kern = font.fonts[f].characters[p.char].italic