OSDN Git Service

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