3 local stderr = io.stderr
4 local function show_usage(s)
5 stderr:write('Error: ' .. s .. '\n');
6 stderr:write('Usage: jfm-convert [-J|-U] <ptex_jfm>\n');
7 stderr:write('-J: JIS mode, -U: UCS mode \n');
8 stderr:write(' * The output will be written to stdout.\n');
9 stderr:write(' * I do not read virtual fonts which corresponded to <ptex_jfm>.\n');
10 stderr:write(" You will need to adjust 'align', 'left', 'down' entries by hand.\n");
11 stderr:write(" * In JIS mode, characters which are not included in JIS X 0208\n");
12 stderr:write(" (e.g., 0x2257) are written as 0x202577.\n");
16 require('unicode'); local uchar = unicode.utf8.char
17 kpse.set_program_name('luatex')
18 jisx0208 = require('ltj-jisx0208.lua').table_jisx0208_uptex
19 local function pass_ucs(s)
20 return "'" .. uchar(s) .. "'"
22 local function jis_to_ucs(s)
23 local a = jisx0208[s-0x2020]
24 return a and pass_ucs(a) or string.format('0x%X',s+0x200000)
27 -------- 引数解釈 --------
34 if arg[i]=='-u' or arg[i]=='-U' then
36 elseif arg[i]=='-j' or arg[i]=='-J' then
39 show_usage('Multiple JFM files.')
45 if not filename then show_usage('Missing JFM file argument.') end
46 kpse.set_program_name('ptex')
47 local nf = kpse.find_file(filename, 'tfm')
48 if not nf then show_usage("JFM file can't be opened: " .. filename) end
50 -------- OPEN --------
52 local jfm_ptex = io.open(nf, 'rb')
53 local function get_word()
54 local d = table.pack(string.byte(jfm_ptex:read(4),1,4))
55 return d[1]*16777216+d[2]*65536+d[3]*256+d[4]
57 local function get_signed_word()
59 return (d>=2147483648) and -(4294967296-d) or d
61 local extract = bit32.extract
62 local function get_two()
64 return extract(d,16,16), extract(d,0,16)
66 local function get_four()
68 return extract(d,24,8), extract(d,16,8), extract(d,8,8), extract(d,0,8)
71 local id, nt = get_two()
72 local lf, lh = get_two()
73 local bc, ec = get_two()
74 local nw, nh = get_two()
75 local nd, ni = get_two()
76 local nl, nk = get_two()
77 local ng, np = get_two()
80 lf~= 7 + lh + nt + (ec - bc + 1) + nw + nh + nd + ni + nl + nk + ng + np or
81 (id~=11 and id~=9) then
82 stderr:write('Bad JFM "' .. filename .. '".\n'); jfm_ptex:close(); os.exit(1)
86 result.dir = (id==11) and 'yoko' or 'tate'
88 -------- HEADER --------
90 _ = get_word() -- checksum, unused
91 local designsize = get_word()/1048576 -- ignored
96 for i=1,math.min(10,lh-2) do encoding = encoding .. jfm_ptex:read(4) end
97 encoding = encoding:sub(2, 1+string.byte(encoding))
99 if not encoding then encoding = 'UNSPECIFIED' end
103 for i=1,math.min(5,lh-12) do family = family .. jfm_ptex:read(4) end
104 family = family:sub(2, 1+string.byte(family))
109 _, _, _, face = get_four()
110 for i=1,lh-19 do jfm_ptex:read(4) end -- ignored
113 -------- CHAR_TYPE --------
117 local ccode, ctype = get_two()
119 all_ctype[#all_ctype+1] = ccode
122 if not result[ctype] then result[ctype] = {} end
123 if not result[ctype].chars then result[ctype].chars = {} end
124 local t = result[ctype].chars
129 -------- CHAR_INFO --------
131 if not result[i] then result[i] = {} end
132 local t, info = result[i], get_word()
133 t.align, t.left, t.down = 'left', 0, 0
134 t.width = extract(info, 24, 8)
135 t.height = extract(info, 20, 4)
136 t.depth = extract(info, 16, 4)
137 t.italic = extract(info, 10, 6)
138 t.tag = extract(info, 8, 2)
139 t.rem = extract(info, 0, 8)
142 local wi, hi, di, ii = {}, {}, {}, {}
143 for i=0,nw-1 do wi[i] = get_signed_word() end
144 for i=0,nh-1 do hi[i] = get_signed_word() end
145 for i=0,nd-1 do di[i] = get_signed_word() end
146 for i=0,ni-1 do ii[i] = get_signed_word() end
149 -------- GLUE/KERN --------
152 for i=0,nl-1 do gk_table[i] = table.pack(get_four()) end
155 for i=0,nk-1 do kerns[i] = get_signed_word() end
158 for i=0,ng/3-1 do glues[i] = { get_signed_word(), get_signed_word(), get_signed_word() } end
161 -------- PARAM --------
163 for i=1,math.min(9, np) do param[i] = get_word() end
166 param[2]/zw, param[3]/zw, param[4]/zw
168 result.xkanjiskip = {
169 param[7]/zw, param[8]/zw, param[9]/zw
171 result.zw, result.zh = 1.0, param[5]/zw
175 -------- 各種 index の解決 --------
178 t.width = wi[t.width]/zw
179 t.height = hi[t.height]/zw
180 t.depth = di[t.depth]/zw
181 t.italic = ii[t.italic]/zw
185 local gkp = gk_table[j]
186 j = (gkp[1]<128) and j+gkp[1]+1 or nil
188 if not t.glue then t.glue = {} end
195 if not t.kern then t.kern = {} end
196 t.kern[gkp[2]] = kerns[gkp[4]]/zw
200 t.tag, t.rem = nil, nil
206 -------- モード判定 --------
209 for i=1, #all_ctype do
210 if not jisx0208[all_ctype[i]-0x2020] then
211 mode = pass_ucs; break
218 if type(a)=='number' then
219 return tostring(math.floor(a*1000000+0.5)/1000000)
220 elseif type(a)=='table' then -- glue
221 return '{ ' .. S(a[1]) .. ', ' .. S(a[2]) .. ', ' .. S(a[3]) .. '},'
222 elseif type(a)=='string' then
223 return "'" .. a .. "'"
229 print('-- -*- coding: utf-8 -*-')
230 print('-- converted from ' .. filename .. ' by jfm_convert.lua')
231 print('-- assumed encoding: ' .. (mode==jis_to_ucs and 'JIS' or 'UCS') .. '\n')
232 print('luatexja.jfont.define_jfm {')
233 print(' -- original design size = ' .. S(designsize))
234 print(' -- original encoding = (' .. encoding .. ')')
235 print(' -- original family = (' .. family .. ')')
236 print(" dir = " .. S(result.dir) .. ",")
237 print(' zw = ' .. S(result.zw) .. ', zh = ' .. S(result.zh) .. ', ')
238 print(' kanjiskip = ' .. S(result.kanjiskip))
239 print(' xkanjiskip = ' .. S(result.xkanjiskip))
242 print(' [' .. tostring(i) .. '] = {')
246 for j=1,#(t.chars) do
247 d = d .. mode(t.chars[j]) .. ', '
248 if j%8==0 and j<#(t.chars) then
255 print(' align = ' .. S(t.align) .. ', left = ' .. S(0.0)
256 .. ', down = ' .. S(0.0) .. ', ')
257 print(' width = ' .. S(t.width) .. ', height = ' .. S(t.height)
258 .. ', depth = ' .. S(t.depth) .. ', italic = ' .. S(t.italic) .. ',')
262 for m,_ in pairs(t.glue) do gi[#gi+1]=m end
264 for _,m in ipairs(gi) do
265 print(' [' .. tostring(m) .. '] = ' .. S(t.glue[m]))
272 for m,_ in pairs(t.kern) do gi[#gi+1]=m end
274 for _,m in ipairs(gi) do
275 print(' [' .. tostring(m) .. '] = ' .. S(t.kern[m]) .. ',')