OSDN Git Service

Revert 0c68dbd etc., since they are no longer needed.
[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 local getfont = font.getfont
20
21 local attr_icflag = luatexbase.attributes['ltj@icflag']
22 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
23 local id_glyph = node.id('glyph')
24 local id_kern = node.id('kern')
25 local id_glue_spec = node.id('glue_spec')
26 local id_glue = node.id('glue')
27 local cat_lp = luatexbase.catcodetables['latex-package']
28 local ITALIC       = luatexja.icflag_table.ITALIC
29 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
30
31 ------------------------------------------------------------------------
32 -- LOADING JFM
33 ------------------------------------------------------------------------
34
35 metrics={} -- this table stores all metric informations
36 font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
37
38 luatexbase.create_callback("luatexja.load_jfm", "data", function (ft, jn) return ft end)
39
40 local jfm_file_name, jfm_var
41 local defjfm_res
42
43 function define_jfm(t)
44    local real_char -- Does current character class have the 'real' character?
45    if t.dir~='yoko' then
46       defjfm_res= nil; return
47    elseif type(t.zw)~='number' or type(t.zh)~='number' then 
48       defjfm_res= nil; return
49    end
50    t.char_type = {}; t.chars = {}
51    for i,v in pairs(t) do
52       if type(i) == 'number' then -- char_type
53          if not v.chars then
54             if i ~= 0 then defjfm_res= nil; return  end
55             real_char = true
56          else
57             real_char = false
58             for j,w in pairs(v.chars) do
59                if w == 'lineend' then
60                   if #v.chars ~= 1 then defjfm_res= nil; return end
61                elseif type(w) == 'number' then
62                   real_char = true;
63                elseif type(w) == 'string' and utf.len(w)==1 then
64                   real_char = true; w = utf.byte(w)
65                elseif type(w) == 'string' and utf.len(w)==2 and utf.sub(w,2) == '*' then
66                   real_char = true; w = utf.byte(utf.sub(w,1,1))
67                   if not t.chars[-w] then 
68                      t.chars[-w] = i
69                   else 
70                      defjfm_res= nil; return
71                   end
72                end
73                if not t.chars[w] then
74                   t.chars[w] = i
75                else 
76                   defjfm_res= nil; return
77                end
78             end
79             if type(v.align)~='string' then 
80                v.align = 'left' -- left
81             end
82             if real_char then
83                if not (type(v.width)=='number' or v.width~='prop') then
84                   defjfm_res= nil; return
85                else
86                   if type(v.height)~='number' then
87                      v.height = 0.0
88                   end
89                   if type(v.depth)~='number' then
90                      v.depth = 0.0
91                   end
92                   if type(v.italic)~='number' then 
93                      v.italic = 0.0
94                   end
95                   if type(v.left)~='number' then 
96                      v.left = 0.0
97                   end
98                   if type(v.down)~='number' then 
99                      v.down = 0.0
100                   end
101                end
102             end
103             v.chars = nil
104          end
105          if not v.kern then v.kern = {} end
106          if not v.glue then v.glue = {} end
107          for j in pairs(v.glue) do
108             if v.kern[j] then defjfm_res= nil; return end
109          end
110          for j,x in pairs(v.kern) do
111             if type(x)=='number' then 
112                v.kern[j] = {x, 0}
113             elseif type(x)=='table' then 
114                v.kern[j] = {x[1], x[2] or 0}
115             end
116          end
117          t.char_type[i] = v
118          t[i] = nil
119       end
120    end
121    t = luatexbase.call_callback("luatexja.load_jfm", t, jfm_file_name)
122    t.size_cache = {}
123    defjfm_res = t
124 end
125
126 local function mult_table(old,scale) -- modified from table.fastcopy
127     if old then
128        local new = { }
129        for k,v in next, old do
130           if type(v) == "table" then
131              new[k] = mult_table(v,scale)
132           elseif type(v) == "number" then
133              new[k] = round(v*scale)
134           else
135              new[k] = v
136           end
137        end
138        return new
139     else return nil end
140 end
141
142 local function update_jfm_cache(j,sz)
143    if metrics[j].size_cache[sz] then return end
144    local t = {}
145    metrics[j].size_cache[sz] = t
146    t.chars = metrics[j].chars
147    t.char_type = mult_table(metrics[j].char_type, sz)
148    for i,v in pairs(t.char_type) do
149       if type(i) == 'number' then -- char_type
150          for k,w in pairs(v.glue) do
151             local g, h = node.new(id_glue), node_new(id_glue_spec)
152             v.glue[k] = {g, (w[5] and w[5]/sz or 0)}
153             h.width, h.stretch, h.shrink = w[1], w[2], w[3]
154             h.stretch_order, h.shrink_order = 0, 0
155             g.subtype = 0; g.spec = h; set_attr(g, attr_icflag, FROM_JFM + 
156                                                 (w[4] and w[4]/sz or 0)); 
157          end
158          for k,w in pairs(v.kern) do
159             w[2] = w[2]/sz
160          end
161       end
162    end
163    t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
164    t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
165    t.zw = round(metrics[j].zw*sz)
166    t.zh = round(metrics[j].zh*sz)
167 end
168
169 luatexbase.create_callback("luatexja.find_char_class", "data", 
170                            function (arg, fmtable, char)
171                               return 0
172                            end)
173
174 function find_char_class(c,m)
175 -- c: character code, m: 
176    if not m then return 0 end
177    return m.chars[c] or 
178       luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
179 end
180
181 local function load_jfont_metric()
182    if jfm_file_name=='' then 
183       ltjb.package_error('luatexja',
184                          'no JFM specified',
185                          'To load and define a Japanese font, a JFM must be specified.'..
186                           "The JFM 'ujis' will be  used for now.")
187       jfm_file_name='ujis'
188    end
189    for j,v in ipairs(metrics) do 
190       if v.name==jfm_file_name then return j end
191    end
192    luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
193    if defjfm_res then
194       defjfm_res.name = jfm_file_name
195       table.insert(metrics, defjfm_res)
196       return #metrics
197    else 
198       return nil
199    end
200 end
201
202
203 ------------------------------------------------------------------------
204 -- LOADING JAPANESE FONTS
205 ------------------------------------------------------------------------
206 local cstemp
207
208 -- EXT
209 function jfontdefX(g)
210   local t = token.get_next()
211   cstemp=token.csname_name(t)
212   if g then luatexja.is_global = '\\global' else luatexja.is_global = '' end
213   tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
214 end
215
216 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
217
218 -- EXT
219 function jfontdefY() -- for horizontal font
220    local j = load_jfont_metric()
221    local fn = font.id(cstemp)
222    local f = getfont(fn)
223    if not j then 
224       ltjb.package_error('luatexja',
225                          "bad JFM `" .. jfm_file_name .. "'",
226                          'The JFM file you specified is not valid JFM file.\n'..
227                             'So defining Japanese font is cancelled.')
228       tex.sprint(cat_lp, luatexja.is_global .. '\\expandafter\\let\\csname ' ..cstemp 
229              .. '\\endcsname=\\relax')
230      return 
231    end
232    update_jfm_cache(j, f.size)
233    local sz = metrics[j].size_cache[f.size]
234    local fmtable = { jfm = j, size = f.size, var = jfm_var, 
235                      zw = sz.zw, zh = sz.zh, 
236                      chars = sz.chars, char_type = sz.char_type,
237                      kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip, 
238                     }
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 function font_callback(name, size, id, fallback)
294    extract_metric(name)
295    -- In the present imple., we don't remove "jfm=..." from name.
296    return fallback(name, size, id)
297 end
298
299 ------------------------------------------------------------------------
300 -- MISC
301 ------------------------------------------------------------------------
302
303 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
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 is_ucs_in_japanese_char(p) then
312          f = has_attr(p, attr_curjfnt)
313          local j = font_metric_table[f]
314          g.kern = j.char_type[find_char_class(p.char, j)].italic
315       else
316          local h = getfont(f)
317          if h then
318             g.kern = h.characters[p.char].italic
319          else
320             tex.attribute[attr_icflag] = -(0x7FFFFFFF)
321             return node.free(g)
322          end
323       end
324       node.write(g)
325       tex.attribute[attr_icflag] = -(0x7FFFFFFF)
326    end
327 end