OSDN Git Service

Rewrited process_input_buffer callback (ticket #25231).
[luatex-ja/luatexja.git] / src / luatexja-jfont.lua
1 local node_new = node.new
2 local has_attr = node.has_attribute
3 local round = tex.round
4
5 local attr_icflag = luatexbase.attributes['ltj@icflag']
6 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
7 local id_glyph = node.id('glyph')
8 local id_kern = node.id('kern')
9
10 local ITALIC = 1
11 ------------------------------------------------------------------------
12 -- LOADING JFM (prefix: ljfm)
13 ------------------------------------------------------------------------
14
15 ltj.metrics={} -- this table stores all metric informations
16 ltj.font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
17
18 local jfm_file_name, jfm_var
19 local defjfm_res
20
21 function ltj.define_jfm(t)
22    local real_char -- Does current character class have the 'real' character?
23    if t.dir~='yoko' then
24       defjfm_res= nil; return
25    elseif type(t.zw)~='number' or type(t.zh)~='number' then 
26       defjfm_res= nil; return
27    end
28    t.char_type = {}; t.chars = {}
29    for i,v in pairs(t) do
30       if type(i) == 'number' then -- char_type
31          if not v.chars then
32             if i ~= 0 then defjfm_res= nil; return  end
33             real_char = true
34          else
35             real_char = false
36             for j,w in pairs(v.chars) do
37                if w == 'lineend' then
38                   if #v.chars ~= 1 then defjfm_res= nil; return end
39                elseif type(w) == 'number' then
40                   real_char = true;
41                elseif type(w) == 'string' and utf.len(w)==1 then
42                   real_char = true; w = utf.byte(w)
43                end
44                if not t.chars[w] then
45                   t.chars[w] = i
46                else 
47                   defjfm_res= nil; return
48                end
49             end
50             if real_char then
51                if not (type(v.width)=='number' or v.width~='prop') then
52                   defjfm_res= nil; return
53                elseif type(v.height)~='number' or type(v.depth)~='number' then
54                   defjfm_res= nil; return
55                end
56             end
57             v.chars = nil
58          end
59          if v.kern and v.glue then
60             for j,w in pairs(v.glue) do
61                if v.kern[j] then defjfm_res= nil; return end
62             end
63          end
64          t.char_type[i] = v
65          t[i] = nil
66       end
67    end
68    defjfm_res= t
69 end
70
71 local function ljfm_find_char_class(c,m)
72 -- c: character code, m
73    if not ltj.metrics[m] then return 0 end
74    return ltj.metrics[m].chars[c] or 0
75 end
76 ltj.int_find_char_class = ljfm_find_char_class
77
78 local function ljfm_load_jfont_metric()
79    if jfm_file_name=='' then 
80       ltj.error('no JFM specified', 
81                 {[1]='To load and define a Japanese font, the name of JFM must be specified.',
82                  [2]="The JFM 'ujis' will be  used for now."})
83       jfm_file_name='ujis'
84    end
85    for j,v in ipairs(ltj.metrics) do 
86       if v.name==jfm_file_name then return j end
87    end
88    ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
89    if defjfm_res then
90       defjfm_res.name = jfm_file_name
91       table.insert(ltj.metrics,defjfm_res)
92       return #ltj.metrics
93    else 
94       return nil
95    end
96 end
97
98
99 ------------------------------------------------------------------------
100 -- LOADING JAPANESE FONTS (prefix: ljft)
101 ------------------------------------------------------------------------
102 local cstemp
103
104 -- EXT
105 function ltj.ext_jfontdefX(g)
106   local t = token.get_next()
107   cstemp=token.csname_name(t)
108   if g then ltj.is_global = '\\global' else ltj.is_global = '' end
109   tex.sprint('\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
110 end
111
112 -- EXT
113 function ltj.ext_jfontdefY() -- for horizontal font
114    local j = ljfm_load_jfont_metric()
115    local fn = font.id(cstemp)
116    local f = font.fonts[fn]
117    if not j then 
118      ltj.error("bad JFM '" .. jfm_file_name .. "'",
119                {[1]='The JFM file you specified is not valid JFM file.',
120                 [2]='Defining Japanese font is cancelled.'})
121      tex.sprint(ltj.is_global .. '\\expandafter\\let\\csname '
122                 .. cstemp .. '\\endcsname=\\relax')
123      return 
124    end
125    ltj.font_metric_table[fn]={}
126    ltj.font_metric_table[fn].jfm=j
127    ltj.font_metric_table[fn].size=f.size
128    ltj.font_metric_table[fn].var=jfm_var
129    tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
130               .. cstemp .. '\\endcsname'
131               .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
132               .. ' \\zw=' .. round(f.size*ltj.metrics[j].zw) .. 'sp'
133               .. '\\zh=' .. round(f.size*ltj.metrics[j].zh) .. 'sp\\relax}')
134 end
135
136 -- extract jfm_file_name and jfm_var
137 local function ljft_extract_metric(name)
138    local basename=name
139    local tmp = utf.sub(basename, 1, 5)
140    jfm_file_name = ''; jfm_var = ''
141    if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
142       basename = utf.sub(basename, 6)
143    end
144    local p = utf.find(basename, ":")
145    if p then 
146       basename = utf.sub(basename, p+1)
147    else return 
148    end
149    -- now basename contains 'features' only.
150    p=1
151    while p do
152       local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
153       if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
154          jfm_file_name = utf.sub(basename, p+4, q-1)
155       elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
156          jfm_var = utf.sub(basename, p+7, q-1)
157       end
158       if utf.len(basename)+1==q then p = nil else p = q + 1 end
159    end
160    return
161 end
162
163 -- replace fonts.define.read()
164 local ljft_dr_orig = fonts.define.read
165 function fonts.define.read(name, size, id)
166    ljft_extract_metric(name)
167    -- In the present imple., we don't remove "jfm=..." from name.
168    return ljft_dr_orig(name, size, id)
169 end
170
171 ------------------------------------------------------------------------
172 -- MISC
173 ------------------------------------------------------------------------
174
175 -- EXT: italic correction
176 function ltj.ext_append_italic()
177    local p = tex.nest[tex.nest.ptr].tail
178    if p and p.id==id_glyph then
179       local f = p.font
180       local g = node_new(id_kern)
181       g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
182       if luatexja.charrange.is_ucs_in_japanese_char(p) then
183          f = has_attr(p, attr_curjfnt)
184          local j = ltj.font_metric_table[f]
185          local c = ljfm_find_char_class(p.char, j.jfm)
186          g.kern = round(j.size * ltj.metrics[j.jfm].char_type[c].italic)
187       else
188          g.kern = font.fonts[f].characters[p.char].italic
189       end
190       node.write(g)
191    end
192 end