OSDN Git Service

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