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 jfm={}; jfm.char_type={}; jfm.glue={}; jfm.kern={}; jfm.chars = {}
21 local jfm_file_name, jfm_var
23 function jfm.define_char_type(t,lt)
24 if not jfm.char_type[t] then jfm.char_type[t]={} end
25 jfm.char_type[t].chars=lt
26 for i,v in pairs(lt) do
27 if v == 'linebdd' then
28 if #lt ~= 1 then ljfm_jfm_cons = false; return end
29 elseif not jfm.chars[v] then
32 ljfm_jfm_cons = false ; return
36 function jfm.define_type_dim(t,l,x,w,h,d,i)
37 if not jfm.char_type[t] then jfm.char_type[t]={} end
38 jfm.char_type[t].width=w; jfm.char_type[t].height=h;
39 jfm.char_type[t].depth=d; jfm.char_type[t].italic=i;
40 jfm.char_type[t].left=l; jfm.char_type[t].down=x
42 function jfm.define_glue(b,a,w,st,sh)
44 if not jfm.glue[j] then jfm.glue[j]={} end
45 jfm.glue[j][0]=w; jfm.glue[j][1]=st;
48 function jfm.define_kern(b,a,w)
50 if not jfm.kern[j] then jfm.kern[j]=w end
53 -- return nil iff ltj.metrics[ind] is a bad metric
54 local function ljfm_consistency_check(ind)
55 local t = ltj.metrics[ind]
57 if ljfm_jfm_cons then r = ind end
58 if t.dir~='yoko' then -- TODO: tate?
60 elseif type(t.zw)~='number' or type(t.zh)~='number' then
61 r=nil -- .zw, .zh must be present
63 if not r then ltj.metrics[ind] = nil end
67 local function ljfm_find_char_class(c,m)
68 -- c: character code, m
69 if not ltj.metrics[m] then return 0 end
70 return ltj.metrics[m].chars[c] or 0
72 ltj.int_find_char_class = ljfm_find_char_class
74 local function ljfm_load_jfont_metric()
75 if jfm_file_name=='' then
76 ltj.error('no JFM specified',
77 {[1]='To load and define a Japanese font, the name of JFM must be specified.',
78 [2]="The JFM 'ujis' will be used for now."})
81 local name=jfm_file_name .. ':' .. jfm_var
83 for j,v in ipairs(ltj.metrics) do
84 if v.name==name then i=j; break end
87 if i then return i end
88 jfm.char_type={}; jfm.glue={}; jfm.kern={}; jfm.chars = {}
90 ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
92 t.dir=jfm.dir; t.zw=jfm.zw; t.zh=jfm.zh
93 t.char_type=jfm.char_type; t.chars=jfm.chars
94 t.glue=jfm.glue; t.kern=jfm.kern
95 table.insert(ltj.metrics,t)
96 return ljfm_consistency_check(#ltj.metrics)
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; ltj.font_metric_table[fn].size=f.size
128 tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
129 .. cstemp .. '\\endcsname'
130 .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
131 .. ' \\zw=' .. tex.round(f.size*ltj.metrics[j].zw) .. 'sp'
132 .. '\\zh=' .. tex.round(f.size*ltj.metrics[j].zh) .. 'sp\\relax}')
135 -- extract jfm_file_name and jfm_var
136 local function ljft_extract_metric(name)
138 local tmp = utf.sub(basename, 1, 5)
139 jfm_file_name = ''; jfm_var = ''
140 if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
141 basename = utf.sub(basename, 6)
143 local p = utf.find(basename, ":")
145 basename = utf.sub(basename, p+1)
148 -- now basename contains 'features' only.
151 local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
152 if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
153 jfm_file_name = utf.sub(basename, p+4, q-1)
154 elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
155 jfm_var = utf.sub(basename, p+7, q-1)
157 if utf.len(basename)+1==q then p = nil else p = q + 1 end
162 -- replace fonts.define.read()
163 local ljft_dr_orig = fonts.define.read
164 function fonts.define.read(name, size, id)
165 ljft_extract_metric(name)
166 -- In the present imple., we don't remove "jfm=..." from name.
167 return ljft_dr_orig(name, size, id)
170 ------------------------------------------------------------------------
171 -- MANAGING THE RANGE OF JAPANESE CHARACTERS (prefix: rgjc)
172 ------------------------------------------------------------------------
173 -- jcr_table_main[chr_code] = index
174 -- index : internal 0, 1, 2, ..., 216 0: 'other'
175 -- external 1 2 216, (out of range): 'other'
178 local jcr_table_main = {}
179 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
181 for i=0x80 ,0xFF do jcr_table_main[i]=1 end
182 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
184 -- EXT: add characters to a range
185 function ltj.ext_add_char_range(b,e,ind) -- ind: external range number
186 if ind<0 or ind>216 then
187 ltj.error('Invalid range number (' .. ind ..
188 '), should be in the range 1..216.',
191 for i=math.max(0x80,b),math.min(ucs_out-1,e) do
192 jcr_table_main[i]=ind
196 local function rgjc_char_to_range(c) -- return the (external) range number
197 if c<0x80 or c>=ucs_out then return -1
199 local i = jcr_table_main[c] or 0
200 if i==0 then return 217 else return i end
204 local function rgjc_get_range_setting(i) -- i: internal range number
205 return floor(tex.getattribute(
206 luatexbase.attributes['ltj@kcat'..floor(i/31)])
207 /math.pow(2, i%31))%2
209 ltj.int_get_range_setting = rgjc_get_range_setting
210 ltj.int_char_to_range = rgjc_char_to_range
212 -- glyph_node p は和文文字か?
213 local function rgjc_is_ucs_in_japanese_char(p)
215 if c<0x80 then return false
217 local i=jcr_table_main[c]
219 has_attr(p, luatexbase.attributes['ltj@kcat'..floor(i/31)])
220 /math.pow(2, i%31))%2 ~= jcr_noncjk)
223 ltj.int_is_ucs_in_japanese_char = rgjc_is_ucs_in_japanese_char
226 function ltj.ext_toggle_char_range(g, i) -- i: external range number
230 if i>0 then kc=0 else kc=1; i=-i end
231 if i>216 then i=0 end
232 local attr = luatexbase.attributes['ltj@kcat'..floor(i/31)]
233 local a = tex.getattribute(attr)
234 local k = math.pow(2, i%31)
235 tex.setattribute(g,attr,(floor(a/k/2)*2+kc)*k+a%k)
239 ------------------------------------------------------------------------
241 ------------------------------------------------------------------------
243 -- EXT: italic correction
244 function ltj.ext_append_italic()
245 local p = tex.nest[tex.nest.ptr].tail
246 if p and p.id==id_glyph then
248 local g = node_new(id_kern)
249 g.subtype = 1; node.set_attribute(g, attr_icflag, 1)
250 if rgjc_is_ucs_in_japanese_char(p) then
251 f = has_attr(p, attr_curjfnt)
253 local j = ltj.font_metric_table[f]
254 local c = ljfm_find_char_class(p.char, j.jfm)
255 g.kern = round(j.size * ltj.metrics[j.jfm].char_type[c].italic)
257 g.kern = font.fonts[f].characters[p.char].italic