OSDN Git Service

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