OSDN Git Service

First attempt for new luaotfload/luatexbase
[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    return m.chars[c] or 
177       luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
178 end
179
180 local function load_jfont_metric()
181    if jfm_file_name=='' then 
182       ltjb.package_error('luatexja',
183                          'no JFM specified',
184                          'To load and define a Japanese font, a JFM must be specified.'..
185                           "The JFM 'ujis' will be  used for now.")
186       jfm_file_name='ujis'
187    end
188    for j,v in ipairs(metrics) do 
189       if v.name==jfm_file_name then return j end
190    end
191    luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
192    if defjfm_res then
193       defjfm_res.name = jfm_file_name
194       table.insert(metrics, defjfm_res)
195       return #metrics
196    else 
197       return nil
198    end
199 end
200
201
202 ------------------------------------------------------------------------
203 -- LOADING JAPANESE FONTS
204 ------------------------------------------------------------------------
205 local cstemp
206
207 -- EXT
208 function jfontdefX(g)
209   local t = token.get_next()
210   cstemp=token.csname_name(t)
211   if g then luatexja.is_global = '\\global' else luatexja.is_global = '' end
212   tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
213 end
214
215 luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
216
217 -- EXT
218 function jfontdefY() -- for horizontal font
219    local j = load_jfont_metric()
220    local fn = font.id(cstemp)
221    local f = font.fonts[fn]
222    if not j then 
223       ltjb.package_error('luatexja',
224                          "bad JFM `" .. jfm_file_name .. "'",
225                          'The JFM file you specified is not valid JFM file.\n'..
226                             'So defining Japanese font is cancelled.')
227       tex.sprint(cat_lp, luatexja.is_global .. '\\expandafter\\let\\csname ' ..cstemp 
228              .. '\\endcsname=\\relax')
229      return 
230    end
231    update_jfm_cache(j, f.size)
232    local sz = metrics[j].size_cache[f.size]
233    local fmtable = { jfm = j, size = f.size, var = jfm_var, 
234                      zw = sz.zw, zh = sz.zh, 
235                      chars = sz.chars, char_type = sz.char_type,
236                      kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip, 
237                     }
238    fmtable.mono_flag = fonts.ids[fn].cidinfo
239       and (not string.match((fonts.ids[fn].name or ''), "+[(pwid)(palt)]"))
240       
241    fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
242    font_metric_table[fn]=fmtable
243    tex.sprint(cat_lp, luatexja.is_global .. '\\protected\\expandafter\\def\\csname ' 
244           .. cstemp  .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
245 end
246
247 -- zw, zh
248 function load_zw()
249    local a = font_metric_table[tex.attribute[attr_curjfnt]]
250    if a then
251       tex.setdimen('ltj@zw', a.zw)
252    else 
253       tex.setdimen('ltj@zw',0)
254    end
255 end
256
257 function load_zh()
258    local a = font_metric_table[tex.attribute[attr_curjfnt]]
259    if a then
260       tex.setdimen('ltj@zh', a.zh)
261    else 
262       tex.setdimen('ltj@zh',0)
263    end
264 end
265
266 -- extract jfm_file_name and jfm_var
267 local function extract_metric(name)
268    local basename=name
269    local tmp = utf.sub(basename, 1, 5)
270    jfm_file_name = ''; jfm_var = ''
271    if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
272       basename = utf.sub(basename, 6)
273    end
274    local p = utf.find(basename, ":")
275    if p then 
276       basename = utf.sub(basename, p+1)
277    else return 
278    end
279    -- now basename contains 'features' only.
280    p=1
281    while p do
282       local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
283       if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
284          jfm_file_name = utf.sub(basename, p+4, q-1)
285       elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
286          jfm_var = utf.sub(basename, p+7, q-1)
287       end
288       if utf.len(basename)+1==q then p = nil else p = q + 1 end
289    end
290    return
291 end
292
293 -- replace fonts.define.read()
294 function font_callback(name, size, id, fallback)
295    extract_metric(name)
296    -- In the present imple., we don't remove "jfm=..." from name.
297    return fallback(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