OSDN Git Service

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