OSDN Git Service

test: flatten the stack table
[luatex-ja/luatexja.git] / src / ltj-jfont.lua
1 --
2 -- luatexja/jfont.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.jfont',
6   date = '2011/06/27',
7   version = '0.1',
8   description = 'Loader for Japanese fonts',
9 })
10 module('luatexja.jfont', package.seeall)
11
12 luatexja.load_module('base');      local ltjb = luatexja.base
13 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
14
15 local node_new = node.new
16 local has_attr = node.has_attribute
17 local set_attr = node.set_attribute
18 local round = tex.round
19
20 local attr_icflag = luatexbase.attributes['ltj@icflag']
21 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
22 local id_glyph = node.id('glyph')
23 local id_kern = node.id('kern')
24 local id_glue_spec = node.id('glue_spec')
25 local id_glue = node.id('glue')
26 local cat_lp = luatexbase.catcodetables['latex-package']
27 local ITALIC       = luatexja.icflag_table.ITALIC
28 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
29
30 ------------------------------------------------------------------------
31 -- LOADING JFM
32 ------------------------------------------------------------------------
33
34 metrics={} -- this table stores all metric informations
35 font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
36
37 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
38
39 local jfm_file_name, jfm_var
40 local defjfm_res
41
42 function define_jfm(t)
43    local real_char -- Does current character class have the 'real' character?
44    if t.dir~='yoko' then
45       defjfm_res= nil; return
46    elseif type(t.zw)~='number' or type(t.zh)~='number' then 
47       defjfm_res= nil; return
48    end
49    t.char_type = {}; t.chars = {}
50    for i,v in pairs(t) do
51       if type(i) == 'number' then -- char_type
52          if not v.chars then
53             if i ~= 0 then defjfm_res= nil; return  end
54             real_char = true
55          else
56             real_char = false
57             for j,w in pairs(v.chars) do
58                if w == 'lineend' then
59                   if #v.chars ~= 1 then defjfm_res= nil; return end
60                elseif type(w) == 'number' then
61                   real_char = true;
62                elseif type(w) == 'string' and utf.len(w)==1 then
63                   real_char = true; w = utf.byte(w)
64                elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
65                   real_char = true; w = utf.byte(utf.sub(w,1,1))
66                   if not t.chars[-w] then 
67                      t.chars[-w] = i
68                   else 
69                      defjfm_res= nil; return
70                   end
71                end
72                if not t.chars[w] then
73                   t.chars[w] = i
74                else 
75                   defjfm_res= nil; return
76                end
77             end
78             if type(v.align)~='string' then 
79                v.align = 'left' -- left
80             end
81             if real_char then
82                if not (type(v.width)=='number' or v.width~='prop') then
83                   defjfm_res= nil; return
84                else
85                   if type(v.height)~='number' then
86                      v.height = 0.0
87                   end
88                   if type(v.depth)~='number' then
89                      v.depth = 0.0
90                   end
91                   if type(v.italic)~='number' then 
92                      v.italic = 0.0
93                   end
94                   if type(v.left)~='number' then 
95                      v.left = 0.0
96                   end
97                   if type(v.down)~='number' then 
98                      v.down = 0.0
99                   end
100                end
101             end
102             v.chars = nil
103          end
104          if not v.kern then v.kern = {} end
105          if not v.glue then v.glue = {} end
106          for j in pairs(v.glue) do
107             if v.kern[j] then defjfm_res= nil; return end
108          end
109          for j,x in pairs(v.kern) do
110             if type(x)=='number' then 
111                v.kern[j] = {x, 0}
112             elseif type(x)=='table' then 
113                v.kern[j] = {x[1], x[2] or 0}
114             end
115          end
116          t.char_type[i] = v
117          t[i] = nil
118       end
119    end
120    t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
121    t.size_cache = {}
122    defjfm_res = t
123 end
124
125 local function mult_table(old,scale) -- modified from table.fastcopy
126     if old then
127        local new = { }
128        for k,v in next, old do
129           if type(v) == "table" then
130              new[k] = mult_table(v,scale)
131           elseif type(v) == "number" then
132              new[k] = round(v*scale)
133           else
134              new[k] = v
135           end
136        end
137        return new
138     else return nil end
139 end
140
141 local function update_jfm_cache(j,sz)
142    if metrics[j].size_cache[sz] then return end
143    local t = {}
144    metrics[j].size_cache[sz] = t
145    t.chars = metrics[j].chars
146    t.char_type = mult_table(metrics[j].char_type, sz)
147    for i,v in pairs(t.char_type) do
148       if type(i) == 'number' then -- char_type
149          for k,w in pairs(v.glue) do
150             local g, h = node.new(id_glue), node_new(id_glue_spec)
151             v.glue[k] = {g, (w[5] and w[5]/sz or 0)}
152             h.width, h.stretch, h.shrink = w[1], w[2], w[3]
153             h.stretch_order, h.shrink_order = 0, 0
154             g.subtype = 0; g.spec = h; set_attr(g, attr_icflag, FROM_JFM + 
155                                                 (w[4] and w[4]/sz or 0)); 
156          end
157          for k,w in pairs(v.kern) do
158             w[2] = w[2]/sz
159          end
160       end
161    end
162    t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
163    t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
164    t.zw = round(metrics[j].zw*sz)
165    t.zh = round(metrics[j].zh*sz)
166 end
167
168 luatexbase.create_callback("luatexja.find_char_class", "data", 
169                            function (arg, fmtable, char)
170                               return 0
171                            end)
172
173 function find_char_class(c,m)
174 -- c: character code, m: 
175    if not m then return 0 end
176 --   print("**")
177 --   for i,v in pairs(m) do print(i,v) end
178    return m.chars[c] or 
179       luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
180 end
181
182 local function load_jfont_metric()
183    if jfm_file_name=='' then 
184       ltjb.package_error('luatexja',
185                          'no JFM specified',
186                          'To load and define a Japanese font, a JFM must be specified.'..
187                           "The JFM 'ujis' will be  used for now.")
188       jfm_file_name='ujis'
189    end
190    for j,v in ipairs(metrics) do 
191       if v.name==jfm_file_name then return j end
192    end
193    luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
194    if defjfm_res then
195       defjfm_res.name = jfm_file_name
196       table.insert(metrics, defjfm_res)
197       return #metrics
198    else 
199       return nil
200    end
201 end
202
203
204 ------------------------------------------------------------------------
205 -- LOADING JAPANESE FONTS
206 ------------------------------------------------------------------------
207 local cstemp
208
209 -- EXT
210 function jfontdefX(g)
211   local t = token.get_next()
212   cstemp=token.csname_name(t)
213   if g then luatexja.is_global = '\\global' else luatexja.is_global = '' end
214   tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
215 end
216
217 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
218
219 -- EXT
220 function jfontdefY() -- for horizontal font
221    local j = load_jfont_metric()
222    local fn = font.id(cstemp)
223    local f = font.fonts[fn]
224    if not j then 
225       ltjb.package_error('luatexja',
226                          "bad JFM `" .. jfm_file_name .. "'",
227                          'The JFM file you specified is not valid JFM file.\n'..
228                             'So defining Japanese font is cancelled.')
229       tex.sprint(cat_lp, luatexja.is_global .. '\\expandafter\\let\\csname ' ..cstemp 
230              .. '\\endcsname=\\relax')
231      return 
232    end
233    update_jfm_cache(j, f.size)
234    local sz = metrics[j].size_cache[f.size]
235    local fmtable = { jfm = j, size = f.size, var = jfm_var, 
236                      zw = sz.zw, zh = sz.zh, 
237                      chars = sz.chars, char_type = sz.char_type,
238                      kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip, 
239                     }
240    fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
241    font_metric_table[fn]=fmtable
242    tex.sprint(cat_lp, luatexja.is_global .. '\\protected\\expandafter\\def\\csname ' 
243           .. cstemp  .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
244 end
245
246 -- zw, zh
247 function load_zw()
248    local a = font_metric_table[tex.attribute[attr_curjfnt]]
249    if a then
250       tex.setdimen('ltj@zw', a.zw)
251    else 
252       tex.setdimen('ltj@zw',0)
253    end
254 end
255
256 function load_zh()
257    local a = font_metric_table[tex.attribute[attr_curjfnt]]
258    if a then
259       tex.setdimen('ltj@zh', a.zh)
260    else 
261       tex.setdimen('ltj@zh',0)
262    end
263 end
264
265 -- extract jfm_file_name and jfm_var
266 local function extract_metric(name)
267    local basename=name
268    local tmp = utf.sub(basename, 1, 5)
269    jfm_file_name = ''; jfm_var = ''
270    if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
271       basename = utf.sub(basename, 6)
272    end
273    local p = utf.find(basename, ":")
274    if p then 
275       basename = utf.sub(basename, p+1)
276    else return 
277    end
278    -- now basename contains 'features' only.
279    p=1
280    while p do
281       local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
282       if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
283          jfm_file_name = utf.sub(basename, p+4, q-1)
284       elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
285          jfm_var = utf.sub(basename, p+7, q-1)
286       end
287       if utf.len(basename)+1==q then p = nil else p = q + 1 end
288    end
289    return
290 end
291
292 -- replace fonts.define.read()
293 local ljft_dr_orig = fonts.define.read
294 function fonts.define.read(name, size, id)
295    extract_metric(name)
296    -- In the present imple., we don't remove "jfm=..." from name.
297    return ljft_dr_orig(name, size, id)
298 end
299
300 ------------------------------------------------------------------------
301 -- MISC
302 ------------------------------------------------------------------------
303
304 -- EXT: italic correction
305 function append_italic()
306    local p = tex.nest[tex.nest.ptr].tail
307    if p and p.id==id_glyph then
308       local f = p.font
309       local g = node_new(id_kern)
310       g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
311       if ltjc.is_ucs_in_japanese_char(p) then
312          f = has_attr(p, attr_curjfnt)
313          local j = font_metric_table[f]
314          local c = find_char_class(p.char, j)
315          g.kern = j.char_type[c].italic
316       else
317          g.kern = font.fonts[f].characters[p.char].italic
318       end
319       node.write(g)
320       tex.attribute[attr_icflag] = -(0x7FFFFFFF)
321    end
322 end