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')
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
42 if not t.chars[w] then
45 defjfm_res= nil; return
49 if not (type(v.width)=='number' or v.width~='prop') then
50 defjfm_res= nil; return
51 elseif type(v.height)~='number' or type(v.depth)~='number' then
52 defjfm_res= nil; return
58 for j,w in pairs(v.glue) do
59 if v.kern[j] then defjfm_res= nil; return end
69 local function ljfm_find_char_class(c,m)
70 -- c: character code, m
71 if not ltj.metrics[m] then return 0 end
72 return ltj.metrics[m].chars[c] or 0
74 ltj.int_find_char_class = ljfm_find_char_class
76 local function ljfm_load_jfont_metric()
77 if jfm_file_name=='' then
78 ltj.error('no JFM specified',
79 {[1]='To load and define a Japanese font, the name of JFM must be specified.',
80 [2]="The JFM 'ujis' will be used for now."})
83 for j,v in ipairs(ltj.metrics) do
84 if v.name==jfm_file_name then return j end
86 ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
88 defjfm_res.name = jfm_file_name
89 table.insert(ltj.metrics,defjfm_res)
97 ------------------------------------------------------------------------
98 -- LOADING JAPANESE FONTS (prefix: ljft)
99 ------------------------------------------------------------------------
103 function ltj.ext_jfontdefX(g)
104 local t = token.get_next()
105 cstemp=token.csname_name(t)
106 if g then ltj.is_global = '\\global' else ltj.is_global = '' end
107 tex.sprint('\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
111 function ltj.ext_jfontdefY() -- for horizontal font
112 local j = ljfm_load_jfont_metric()
113 local fn = font.id(cstemp)
114 local f = font.fonts[fn]
116 ltj.error("bad JFM '" .. jfm_file_name .. "'",
117 {[1]='The JFM file you specified is not valid JFM file.',
118 [2]='Defining Japanese font is cancelled.'})
119 tex.sprint(ltj.is_global .. '\\expandafter\\let\\csname '
120 .. cstemp .. '\\endcsname=\\relax')
123 ltj.font_metric_table[fn]={}
124 ltj.font_metric_table[fn].jfm=j
125 ltj.font_metric_table[fn].size=f.size
126 ltj.font_metric_table[fn].var=jfm_var
127 tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
128 .. cstemp .. '\\endcsname'
129 .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
130 .. ' \\zw=' .. tex.round(f.size*ltj.metrics[j].zw) .. 'sp'
131 .. '\\zh=' .. tex.round(f.size*ltj.metrics[j].zh) .. 'sp\\relax}')
134 -- extract jfm_file_name and jfm_var
135 local function ljft_extract_metric(name)
137 local tmp = utf.sub(basename, 1, 5)
138 jfm_file_name = ''; jfm_var = ''
139 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
140 basename = utf.sub(basename, 6)
142 local p = utf.find(basename, ":")
144 basename = utf.sub(basename, p+1)
147 -- now basename contains 'features' only.
150 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
151 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
152 jfm_file_name = utf.sub(basename, p+4, q-1)
153 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
154 jfm_var = utf.sub(basename, p+7, q-1)
156 if utf.len(basename)+1==q then p = nil else p = q + 1 end
161 -- replace fonts.define.read()
162 local ljft_dr_orig = fonts.define.read
163 function fonts.define.read(name, size, id)
164 ljft_extract_metric(name)
165 -- In the present imple., we don't remove "jfm=..." from name.
166 return ljft_dr_orig(name, size, id)
169 ------------------------------------------------------------------------
170 -- MANAGING THE RANGE OF JAPANESE CHARACTERS (prefix: rgjc)
171 ------------------------------------------------------------------------
172 -- jcr_table_main[chr_code] = index
173 -- index : internal 0, 1, 2, ..., 216 0: 'other'
174 -- external 1 2 216, (out of range): 'other'
177 local jcr_table_main = {}
178 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
180 for i=0x80 ,0xFF do jcr_table_main[i]=1 end
181 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
183 -- EXT: add characters to a range
184 function ltj.ext_add_char_range(b,e,ind) -- ind: external range number
185 if ind<0 or ind>216 then
186 ltj.error('Invalid range number (' .. ind ..
187 '), should be in the range 1..216.',
190 for i=math.max(0x80,b),math.min(ucs_out-1,e) do
191 jcr_table_main[i]=ind
195 local function rgjc_char_to_range(c) -- return the (external) range number
196 if c<0x80 or c>=ucs_out then return -1
198 local i = jcr_table_main[c] or 0
199 if i==0 then return 217 else return i end
203 local function rgjc_get_range_setting(i) -- i: internal range number
204 return floor(tex.getattribute(
205 luatexbase.attributes['ltj@kcat'..floor(i/31)])
206 /math.pow(2, i%31))%2
208 ltj.int_get_range_setting = rgjc_get_range_setting
209 ltj.int_char_to_range = rgjc_char_to_range
211 -- glyph_node p は和文文字か?
212 local function rgjc_is_ucs_in_japanese_char(p)
214 if c<0x80 then return false
216 local i=jcr_table_main[c]
218 has_attr(p, luatexbase.attributes['ltj@kcat'..floor(i/31)])
219 /math.pow(2, i%31))%2 ~= jcr_noncjk)
222 ltj.int_is_ucs_in_japanese_char = rgjc_is_ucs_in_japanese_char
225 function ltj.ext_toggle_char_range(g, i) -- i: external range number
229 if i>0 then kc=0 else kc=1; i=-i end
230 if i>216 then i=0 end
231 local attr = luatexbase.attributes['ltj@kcat'..floor(i/31)]
232 local a = tex.getattribute(attr)
233 local k = math.pow(2, i%31)
234 tex.setattribute(g,attr,(floor(a/k/2)*2+kc)*k+a%k)
238 ------------------------------------------------------------------------
240 ------------------------------------------------------------------------
242 -- EXT: italic correction
243 function ltj.ext_append_italic()
244 local p = tex.nest[tex.nest.ptr].tail
245 if p and p.id==id_glyph then
247 local g = node_new(id_kern)
248 g.subtype = 1; node.set_attribute(g, attr_icflag, 1)
249 if rgjc_is_ucs_in_japanese_char(p) then
250 f = has_attr(p, attr_curjfnt)
252 local j = ltj.font_metric_table[f]
253 local c = ljfm_find_char_class(p.char, j.jfm)
254 g.kern = round(j.size * ltj.metrics[j.jfm].char_type[c].italic)
256 g.kern = font.fonts[f].characters[p.char].italic