OSDN Git Service

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