OSDN Git Service

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