OSDN Git Service

Fixed ticket #25935
[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
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                elseif type(v.height)~='number' or type(v.depth)~='number' then
68                   defjfm_res= nil; return
69                end
70             end
71             v.chars = nil
72          end
73          if v.kern and v.glue then
74             for j,w in pairs(v.glue) do
75                if v.kern[j] then defjfm_res= nil; return end
76             end
77          end
78          t.char_type[i] = v
79          t[i] = nil
80       end
81    end
82    defjfm_res= t
83 end
84
85 function find_char_class(c,m)
86 -- c: character code, m
87    if not metrics[m] then return 0 end
88    return metrics[m].chars[c] or 0
89 end
90
91 local function load_jfont_metric()
92    if jfm_file_name=='' then 
93       ltjb.package_error('luatexja',
94                          'no JFM specified',
95                          {'To load and define a Japanese font, a JFM must be specified.',
96                           "The JFM 'ujis' will be  used for now."})
97       jfm_file_name='ujis'
98    end
99    for j,v in ipairs(metrics) do 
100       if v.name==jfm_file_name then return j end
101    end
102    ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
103    if defjfm_res then
104       defjfm_res.name = jfm_file_name
105       table.insert(metrics, defjfm_res)
106       return #metrics
107    else 
108       return nil
109    end
110 end
111
112
113 ------------------------------------------------------------------------
114 -- LOADING JAPANESE FONTS
115 ------------------------------------------------------------------------
116 local cstemp
117
118 -- EXT
119 function jfontdefX(g)
120   local t = token.get_next()
121   cstemp=token.csname_name(t)
122   if g then ltj.is_global = '\\global' else ltj.is_global = '' end
123   tex.sprint('\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
124 end
125
126 -- EXT
127 function jfontdefY() -- for horizontal font
128    local j = load_jfont_metric()
129    local fn = font.id(cstemp)
130    local f = font.fonts[fn]
131    if not j then 
132       ltjb.package_error('luatexja',
133                          "bad JFM `" .. jfm_file_name .. "'",
134                          'The JFM file you specified is not valid JFM file.\n'..
135                             'So defining Japanese font is cancelled.')
136       tex.sprint(ltj.is_global .. '\\expandafter\\let\\csname '
137                  .. cstemp .. '\\endcsname=\\relax')
138      return 
139    end
140    font_metric_table[fn]={}
141    font_metric_table[fn].jfm=j
142    font_metric_table[fn].size=f.size
143    font_metric_table[fn].var=jfm_var
144    tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
145               .. cstemp .. '\\endcsname'
146               .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
147               .. ' \\zw=' .. round(f.size*metrics[j].zw) .. 'sp'
148               .. '\\zh=' .. round(f.size*metrics[j].zh) .. 'sp\\relax}')
149 end
150
151 -- extract jfm_file_name and jfm_var
152 local function extract_metric(name)
153    local basename=name
154    local tmp = utf.sub(basename, 1, 5)
155    jfm_file_name = ''; jfm_var = ''
156    if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
157       basename = utf.sub(basename, 6)
158    end
159    local p = utf.find(basename, ":")
160    if p then 
161       basename = utf.sub(basename, p+1)
162    else return 
163    end
164    -- now basename contains 'features' only.
165    p=1
166    while p do
167       local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
168       if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
169          jfm_file_name = utf.sub(basename, p+4, q-1)
170       elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
171          jfm_var = utf.sub(basename, p+7, q-1)
172       end
173       if utf.len(basename)+1==q then p = nil else p = q + 1 end
174    end
175    return
176 end
177
178 -- replace fonts.define.read()
179 local ljft_dr_orig = fonts.define.read
180 function fonts.define.read(name, size, id)
181    extract_metric(name)
182    -- In the present imple., we don't remove "jfm=..." from name.
183    return ljft_dr_orig(name, size, id)
184 end
185
186 ------------------------------------------------------------------------
187 -- MISC
188 ------------------------------------------------------------------------
189
190 -- EXT: italic correction
191 function append_italic()
192    local p = tex.nest[tex.nest.ptr].tail
193    if p and p.id==id_glyph then
194       local f = p.font
195       local g = node_new(id_kern)
196       g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
197       if ltjc.is_ucs_in_japanese_char(p) then
198          f = has_attr(p, attr_curjfnt)
199          local j = font_metric_table[f]
200          local c = find_char_class(p.char, j.jfm)
201          g.kern = round(j.size * metrics[j.jfm].char_type[c].italic)
202       else
203          g.kern = font.fonts[f].characters[p.char].italic
204       end
205       node.write(g)
206    end
207 end