OSDN Git Service

Fixed bug #25121 (I hope) and check_box.
[luatex-ja/luatexja.git] / src / luatexja-jfont.lua
1 local node_new = node.new
2 local has_attr = node.has_attribute
3 local floor = math.floor
4 local round = tex.round
5
6 local attr_icflag = luatexbase.attributes['ltj@icflag']
7 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
8 local id_glyph = node.id('glyph')
9 local id_kern = node.id('kern')
10
11 local ITALIC = 1
12 ------------------------------------------------------------------------
13 -- LOADING JFM (prefix: ljfm)
14 ------------------------------------------------------------------------
15
16 ltj.metrics={} -- this table stores all metric informations
17 ltj.font_metric_table={} -- [font number] -> jfm_name, jfm_var, size
18
19 local jfm_file_name, jfm_var
20 local defjfm_res
21
22 function ltj.define_jfm(t)
23    local real_char -- Does current character class have the 'real' character?
24    if t.dir~='yoko' then
25       defjfm_res= nil; return
26    elseif type(t.zw)~='number' or type(t.zh)~='number' then 
27       defjfm_res= nil; return
28    end
29    t.char_type = {}; t.chars = {}
30    for i,v in pairs(t) do
31       if type(i) == 'number' then -- char_type
32          if not v.chars then
33             if i ~= 0 then defjfm_res= nil; return  end
34             real_char = true
35          else
36             real_char = false
37             for j,w in pairs(v.chars) do
38                if w == 'lineend' then
39                   if #v.chars ~= 1 then defjfm_res= nil; return end
40                elseif type(w) == 'number' then
41                   real_char = true;
42                elseif type(w) == 'string' and utf.len(w)==1 then
43                   real_char = true; w = utf.byte(w)
44                end
45                if not t.chars[w] then
46                   t.chars[w] = i
47                else 
48                   defjfm_res= nil; return
49                end
50             end
51             if real_char then
52                if not (type(v.width)=='number' or v.width~='prop') then
53                   defjfm_res= nil; return
54                elseif type(v.height)~='number' or type(v.depth)~='number' then
55                   defjfm_res= nil; return
56                end
57             end
58             v.chars = nil
59          end
60          if v.kern and v.glue then
61             for j,w in pairs(v.glue) do
62                if v.kern[j] then defjfm_res= nil; return end
63             end
64          end
65          t.char_type[i] = v
66          t[i] = nil
67       end
68    end
69    defjfm_res= t
70 end
71
72 local function ljfm_find_char_class(c,m)
73 -- c: character code, m
74    if not ltj.metrics[m] then return 0 end
75    return ltj.metrics[m].chars[c] or 0
76 end
77 ltj.int_find_char_class = ljfm_find_char_class
78
79 local function ljfm_load_jfont_metric()
80    if jfm_file_name=='' then 
81       ltj.error('no JFM specified', 
82                 {[1]='To load and define a Japanese font, the name of JFM must be specified.',
83                  [2]="The JFM 'ujis' will be  used for now."})
84       jfm_file_name='ujis'
85    end
86    for j,v in ipairs(ltj.metrics) do 
87       if v.name==jfm_file_name then return j end
88    end
89    ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
90    if defjfm_res then
91       defjfm_res.name = jfm_file_name
92       table.insert(ltj.metrics,defjfm_res)
93       return #ltj.metrics
94    else 
95       return nil
96    end
97 end
98
99
100 ------------------------------------------------------------------------
101 -- LOADING JAPANESE FONTS (prefix: ljft)
102 ------------------------------------------------------------------------
103 local cstemp
104
105 -- EXT
106 function ltj.ext_jfontdefX(g)
107   local t = token.get_next()
108   cstemp=token.csname_name(t)
109   if g then ltj.is_global = '\\global' else ltj.is_global = '' end
110   tex.sprint('\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
111 end
112
113 -- EXT
114 function ltj.ext_jfontdefY() -- for horizontal font
115    local j = ljfm_load_jfont_metric()
116    local fn = font.id(cstemp)
117    local f = font.fonts[fn]
118    if not j then 
119      ltj.error("bad JFM '" .. jfm_file_name .. "'",
120                {[1]='The JFM file you specified is not valid JFM file.',
121                 [2]='Defining Japanese font is cancelled.'})
122      tex.sprint(ltj.is_global .. '\\expandafter\\let\\csname '
123                 .. cstemp .. '\\endcsname=\\relax')
124      return 
125    end
126    ltj.font_metric_table[fn]={}
127    ltj.font_metric_table[fn].jfm=j
128    ltj.font_metric_table[fn].size=f.size
129    ltj.font_metric_table[fn].var=jfm_var
130    tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
131               .. cstemp .. '\\endcsname'
132               .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
133               .. ' \\zw=' .. tex.round(f.size*ltj.metrics[j].zw) .. 'sp'
134               .. '\\zh=' .. tex.round(f.size*ltj.metrics[j].zh) .. 'sp\\relax}')
135 end
136
137 -- extract jfm_file_name and jfm_var
138 local function ljft_extract_metric(name)
139    local basename=name
140    local tmp = utf.sub(basename, 1, 5)
141    jfm_file_name = ''; jfm_var = ''
142    if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
143       basename = utf.sub(basename, 6)
144    end
145    local p = utf.find(basename, ":")
146    if p then 
147       basename = utf.sub(basename, p+1)
148    else return 
149    end
150    -- now basename contains 'features' only.
151    p=1
152    while p do
153       local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
154       if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
155          jfm_file_name = utf.sub(basename, p+4, q-1)
156       elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
157          jfm_var = utf.sub(basename, p+7, q-1)
158       end
159       if utf.len(basename)+1==q then p = nil else p = q + 1 end
160    end
161    return
162 end
163
164 -- replace fonts.define.read()
165 local ljft_dr_orig = fonts.define.read
166 function fonts.define.read(name, size, id)
167    ljft_extract_metric(name)
168    -- In the present imple., we don't remove "jfm=..." from name.
169    return ljft_dr_orig(name, size, id)
170 end
171
172 ------------------------------------------------------------------------
173 -- MANAGING THE RANGE OF JAPANESE CHARACTERS (prefix: rgjc)
174 ------------------------------------------------------------------------
175 -- jcr_table_main[chr_code] = index
176 -- index : internal 0, 1, 2, ..., 216               0: 'other'
177 --         external    1  2       216, (out of range): 'other'
178
179 -- initialize 
180 local jcr_table_main = {}
181 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
182
183 for i=0x80 ,0xFF      do jcr_table_main[i]=1 end
184 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
185
186 -- EXT: add characters to a range
187 function ltj.ext_add_char_range(b,e,ind) -- ind: external range number
188    if ind<0 or ind>216 then 
189       ltj.error('Invalid range number (' .. ind ..
190                 '), should be in the range 1..216.',
191              {}); return
192    end
193    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
194       jcr_table_main[i]=ind
195    end
196 end
197
198 local function rgjc_char_to_range(c) -- return the (external) range number
199    if c<0x80 or c>=ucs_out then return -1
200    else 
201       local i = jcr_table_main[c] or 0
202       if i==0 then return 217 else return i end
203    end
204 end
205
206 local function rgjc_get_range_setting(i) -- i: internal range number
207    return floor(tex.getattribute(
208                         luatexbase.attributes['ltj@kcat'..floor(i/31)])
209                      /math.pow(2, i%31))%2
210 end
211 ltj.int_get_range_setting = rgjc_get_range_setting
212 ltj.int_char_to_range = rgjc_char_to_range
213
214 --  glyph_node p は和文文字か?
215 local function rgjc_is_ucs_in_japanese_char(p)
216    local c = p.char
217    if c<0x80 then return false 
218    else 
219       local i=jcr_table_main[c] 
220       return (floor(
221                  has_attr(p, luatexbase.attributes['ltj@kcat'..floor(i/31)])
222                  /math.pow(2, i%31))%2 ~= jcr_noncjk) 
223    end
224 end
225 ltj.int_is_ucs_in_japanese_char = rgjc_is_ucs_in_japanese_char
226
227 -- EXT
228 function ltj.ext_toggle_char_range(g, i) -- i: external range number
229    if i==0 then return 
230    else
231       local kc
232       if i>0 then kc=0 else kc=1; i=-i end
233       if i>216 then i=0 end
234       local attr = luatexbase.attributes['ltj@kcat'..floor(i/31)]
235       local a = tex.getattribute(attr)
236       local k = math.pow(2, i%31)
237       tex.setattribute(g,attr,(floor(a/k/2)*2+kc)*k+a%k)
238    end
239 end
240
241 ------------------------------------------------------------------------
242 -- MISC
243 ------------------------------------------------------------------------
244
245 -- EXT: italic correction
246 function ltj.ext_append_italic()
247    local p = tex.nest[tex.nest.ptr].tail
248    if p and p.id==id_glyph then
249       local f = p.font
250       local g = node_new(id_kern)
251       g.subtype = 1; node.set_attribute(g, attr_icflag, ITALIC)
252       if rgjc_is_ucs_in_japanese_char(p) then
253          f = has_attr(p, attr_curjfnt)
254          local j = ltj.font_metric_table[f]
255          local c = ljfm_find_char_class(p.char, j.jfm)
256          g.kern = round(j.size * ltj.metrics[j.jfm].char_type[c].italic)
257       else
258          g.kern = font.fonts[f].characters[p.char].italic
259       end
260       node.write(g)
261    end
262 end