1 local node_new = node.new
2 local has_attr = node.has_attribute
3 local floor = math.floor
4 local round = tex.round
6 local attr_icflag = luatexbase.attributes['ltj@icflag']
7 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
8 local id_glyph = node.id('glyph')
9 local id_kern = node.id('kern')
12 ------------------------------------------------------------------------
13 -- LOADING JFM (prefix: ljfm)
14 ------------------------------------------------------------------------
16 ltj.metrics={} -- this table stores all metric informations
17 ltj.font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
19 local jfm_file_name, jfm_var
22 function ltj.define_jfm(t)
23 local real_char -- Does current character class have the 'real' character?
25 defjfm_res= nil; return
26 elseif type(t.zw)~='number' or type(t.zh)~='number' then
27 defjfm_res= nil; return
29 t.char_type = {}; t.chars = {}
30 for i,v in pairs(t) do
31 if type(i) == 'number' then -- char_type
33 if i ~= 0 then defjfm_res= nil; return end
37 for j,w in pairs(v.chars) do
38 if w == 'lineend' then
39 if #v.chars ~= 1 then defjfm_res= nil; return end
40 elseif type(w) == 'number' then
42 elseif type(w) == 'string' and utf.len(w)==1 then
43 real_char = true; w = utf.byte(w)
45 if not t.chars[w] then
48 defjfm_res= nil; return
52 if not (type(v.width)=='number' or v.width~='prop') then
53 defjfm_res= nil; return
54 elseif type(v.height)~='number' or type(v.depth)~='number' then
55 defjfm_res= nil; return
60 if v.kern and v.glue then
61 for j,w in pairs(v.glue) do
62 if v.kern[j] then defjfm_res= nil; return end
72 local function ljfm_find_char_class(c,m)
73 -- c: character code, m
74 if not ltj.metrics[m] then return 0 end
75 return ltj.metrics[m].chars[c] or 0
77 ltj.int_find_char_class = ljfm_find_char_class
79 local function ljfm_load_jfont_metric()
80 if jfm_file_name=='' then
81 ltj.error('no JFM specified',
82 {[1]='To load and define a Japanese font, the name of JFM must be specified.',
83 [2]="The JFM 'ujis' will be used for now."})
86 for j,v in ipairs(ltj.metrics) do
87 if v.name==jfm_file_name then return j end
89 ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
91 defjfm_res.name = jfm_file_name
92 table.insert(ltj.metrics,defjfm_res)
100 ------------------------------------------------------------------------
101 -- LOADING JAPANESE FONTS (prefix: ljft)
102 ------------------------------------------------------------------------
106 function ltj.ext_jfontdefX(g)
107 local t = token.get_next()
108 cstemp=token.csname_name(t)
109 if g then ltj.is_global = '\\global' else ltj.is_global = '' end
110 tex.sprint('\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
114 function ltj.ext_jfontdefY() -- for horizontal font
115 local j = ljfm_load_jfont_metric()
116 local fn = font.id(cstemp)
117 local f = font.fonts[fn]
119 ltj.error("bad JFM '" .. jfm_file_name .. "'",
120 {[1]='The JFM file you specified is not valid JFM file.',
121 [2]='Defining Japanese font is cancelled.'})
122 tex.sprint(ltj.is_global .. '\\expandafter\\let\\csname '
123 .. cstemp .. '\\endcsname=\\relax')
126 ltj.font_metric_table[fn]={}
127 ltj.font_metric_table[fn].jfm=j
128 ltj.font_metric_table[fn].size=f.size
129 ltj.font_metric_table[fn].var=jfm_var
130 tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
131 .. cstemp .. '\\endcsname'
132 .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
133 .. ' \\zw=' .. tex.round(f.size*ltj.metrics[j].zw) .. 'sp'
134 .. '\\zh=' .. tex.round(f.size*ltj.metrics[j].zh) .. 'sp\\relax}')
137 -- extract jfm_file_name and jfm_var
138 local function ljft_extract_metric(name)
140 local tmp = utf.sub(basename, 1, 5)
141 jfm_file_name = ''; jfm_var = ''
142 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
143 basename = utf.sub(basename, 6)
145 local p = utf.find(basename, ":")
147 basename = utf.sub(basename, p+1)
150 -- now basename contains 'features' only.
153 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
154 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
155 jfm_file_name = utf.sub(basename, p+4, q-1)
156 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
157 jfm_var = utf.sub(basename, p+7, q-1)
159 if utf.len(basename)+1==q then p = nil else p = q + 1 end
164 -- replace fonts.define.read()
165 local ljft_dr_orig = fonts.define.read
166 function fonts.define.read(name, size, id)
167 ljft_extract_metric(name)
168 -- In the present imple., we don't remove "jfm=..." from name.
169 return ljft_dr_orig(name, size, id)
172 ------------------------------------------------------------------------
173 -- MANAGING THE RANGE OF JAPANESE CHARACTERS (prefix: rgjc)
174 ------------------------------------------------------------------------
175 -- jcr_table_main[chr_code] = index
176 -- index : internal 0, 1, 2, ..., 216 0: 'other'
177 -- external 1 2 216, (out of range): 'other'
180 local jcr_table_main = {}
181 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
183 for i=0x80 ,0xFF do jcr_table_main[i]=1 end
184 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
186 -- EXT: add characters to a range
187 function ltj.ext_add_char_range(b,e,ind) -- ind: external range number
188 if ind<0 or ind>216 then
189 ltj.error('Invalid range number (' .. ind ..
190 '), should be in the range 1..216.',
193 for i=math.max(0x80,b),math.min(ucs_out-1,e) do
194 jcr_table_main[i]=ind
198 local function rgjc_char_to_range(c) -- return the (external) range number
199 if c<0x80 or c>=ucs_out then return -1
201 local i = jcr_table_main[c] or 0
202 if i==0 then return 217 else return i end
206 local function rgjc_get_range_setting(i) -- i: internal range number
207 return floor(tex.getattribute(
208 luatexbase.attributes['ltj@kcat'..floor(i/31)])
209 /math.pow(2, i%31))%2
211 ltj.int_get_range_setting = rgjc_get_range_setting
212 ltj.int_char_to_range = rgjc_char_to_range
214 -- glyph_node p は和文文字か?
215 local function rgjc_is_ucs_in_japanese_char(p)
217 if c<0x80 then return false
219 local i=jcr_table_main[c]
221 has_attr(p, luatexbase.attributes['ltj@kcat'..floor(i/31)])
222 /math.pow(2, i%31))%2 ~= jcr_noncjk)
225 ltj.int_is_ucs_in_japanese_char = rgjc_is_ucs_in_japanese_char
228 function ltj.ext_toggle_char_range(g, i) -- i: external range number
232 if i>0 then kc=0 else kc=1; i=-i end
233 if i>216 then i=0 end
234 local attr = luatexbase.attributes['ltj@kcat'..floor(i/31)]
235 local a = tex.getattribute(attr)
236 local k = math.pow(2, i%31)
237 tex.setattribute(g,attr,(floor(a/k/2)*2+kc)*k+a%k)
241 ------------------------------------------------------------------------
243 ------------------------------------------------------------------------
245 -- EXT: italic correction
246 function ltj.ext_append_italic()
247 local p = tex.nest[tex.nest.ptr].tail
248 if p and p.id==id_glyph then
250 local g = node_new(id_kern)
251 g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
252 if rgjc_is_ucs_in_japanese_char(p) then
253 f = has_attr(p, attr_curjfnt)
254 local j = ltj.font_metric_table[f]
255 local c = ljfm_find_char_class(p.char, j.jfm)
256 g.kern = round(j.size * ltj.metrics[j.jfm].char_type[c].italic)
258 g.kern = font.fonts[f].characters[p.char].italic