OSDN Git Service

Updated a document; added lltjlisting.sty in doc/
[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    defjfm_res= t
100 end
101
102 function find_char_class(c,m)
103 -- c: character code, m
104    if not metrics[m] then return 0 end
105    return metrics[m].chars[c] or 0
106 end
107
108 local function load_jfont_metric()
109    if jfm_file_name=='' then 
110       ltjb.package_error('luatexja',
111                          'no JFM specified',
112                          {'To load and define a Japanese font, a JFM must be specified.',
113                           "The JFM 'ujis' will be  used for now."})
114       jfm_file_name='ujis'
115    end
116    for j,v in ipairs(metrics) do 
117       if v.name==jfm_file_name then return j end
118    end
119    ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
120    if defjfm_res then
121       defjfm_res.name = jfm_file_name
122       table.insert(metrics, defjfm_res)
123       return #metrics
124    else 
125       return nil
126    end
127 end
128
129
130 ------------------------------------------------------------------------
131 -- LOADING JAPANESE FONTS
132 ------------------------------------------------------------------------
133 local cstemp
134
135 -- EXT
136 function jfontdefX(g)
137   local t = token.get_next()
138   cstemp=token.csname_name(t)
139   if g then ltj.is_global = '\\global' else ltj.is_global = '' end
140   tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
141 end
142
143 -- EXT
144 function jfontdefY() -- for horizontal font
145    local j = load_jfont_metric()
146    local fn = font.id(cstemp)
147    local f = font.fonts[fn]
148    if not j then 
149       ltjb.package_error('luatexja',
150                          "bad JFM `" .. jfm_file_name .. "'",
151                          'The JFM file you specified is not valid JFM file.\n'..
152                             'So defining Japanese font is cancelled.')
153       tex.sprint(cat_lp, ltj.is_global .. '\\expandafter\\let\\csname ' ..cstemp 
154              .. '\\endcsname=\\relax')
155      return 
156    end
157    font_metric_table[fn]={}
158    font_metric_table[fn].jfm=j
159    font_metric_table[fn].size=f.size
160    font_metric_table[fn].var=jfm_var
161    tex.sprint(cat_lp, ltj.is_global .. '\\protected\\expandafter\\def\\csname ' 
162           .. cstemp  .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
163 end
164
165 -- zw, zh
166 function load_zw()
167    local a = font_metric_table[tex.attribute[attr_curjfnt]]
168    if a then
169       tex.setdimen('ltj@zw', round(a.size*metrics[a.jfm].zw))
170    else 
171       tex.setdimen('ltj@zw',0)
172    end
173 end
174
175 function load_zh()
176    local a = font_metric_table[tex.attribute[attr_curjfnt]]
177    if a then
178       tex.setdimen('ltj@zh', round(a.size*metrics[a.jfm].zh))
179    else 
180       tex.setdimen('ltj@zh', round(a.size*metrics[a.jfm].zh))
181    end
182 end
183
184 -- extract jfm_file_name and jfm_var
185 local function extract_metric(name)
186    local basename=name
187    local tmp = utf.sub(basename, 1, 5)
188    jfm_file_name = ''; jfm_var = ''
189    if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
190       basename = utf.sub(basename, 6)
191    end
192    local p = utf.find(basename, ":")
193    if p then 
194       basename = utf.sub(basename, p+1)
195    else return 
196    end
197    -- now basename contains 'features' only.
198    p=1
199    while p do
200       local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
201       if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
202          jfm_file_name = utf.sub(basename, p+4, q-1)
203       elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
204          jfm_var = utf.sub(basename, p+7, q-1)
205       end
206       if utf.len(basename)+1==q then p = nil else p = q + 1 end
207    end
208    return
209 end
210
211 -- replace fonts.define.read()
212 local ljft_dr_orig = fonts.define.read
213 function fonts.define.read(name, size, id)
214    extract_metric(name)
215    -- In the present imple., we don't remove "jfm=..." from name.
216    return ljft_dr_orig(name, size, id)
217 end
218
219 ------------------------------------------------------------------------
220 -- MISC
221 ------------------------------------------------------------------------
222
223 -- EXT: italic correction
224 function append_italic()
225    local p = tex.nest[tex.nest.ptr].tail
226    if p and p.id==id_glyph then
227       local f = p.font
228       local g = node_new(id_kern)
229       g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
230       if ltjc.is_ucs_in_japanese_char(p) then
231          f = has_attr(p, attr_curjfnt)
232          local j = font_metric_table[f]
233          local c = find_char_class(p.char, j.jfm)
234          g.kern = round(j.size * metrics[j.jfm].char_type[c].italic)
235       else
236          g.kern = font.fonts[f].characters[p.char].italic
237       end
238       node.write(g)
239    end
240 end