OSDN Git Service

Renamed control sequences and parameters. (e.g. yabaselineshift => yalbaselineshift)
[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 jfm={}; jfm.char_type={}; jfm.glue={}; jfm.kern={}; jfm.chars = {}
19
20 local ljfm_jfm_cons
21 local jfm_file_name, jfm_var
22
23 function jfm.define_char_type(t,lt) 
24    if not jfm.char_type[t] then jfm.char_type[t]={} end
25    jfm.char_type[t].chars=lt 
26    for i,v in pairs(lt) do
27       if v == 'linebdd' then
28          if #lt ~= 1 then ljfm_jfm_cons = false; return end
29       elseif not jfm.chars[v] then 
30          jfm.chars[v] = t 
31       else 
32          ljfm_jfm_cons = false ; return
33       end
34    end
35 end
36 function jfm.define_type_dim(t,l,x,w,h,d,i)
37    if not jfm.char_type[t] then jfm.char_type[t]={} end
38    jfm.char_type[t].width=w; jfm.char_type[t].height=h;
39    jfm.char_type[t].depth=d; jfm.char_type[t].italic=i; 
40    jfm.char_type[t].left=l; jfm.char_type[t].down=x
41 end
42 function jfm.define_glue(b,a,w,st,sh)
43    local j=b*0x800+a
44    if not jfm.glue[j] then jfm.glue[j]={} end
45    jfm.glue[j][0]=w; jfm.glue[j][1]=st; 
46    jfm.glue[j][2]=sh
47 end
48 function jfm.define_kern(b,a,w)
49    local j=b*0x800+a
50    if not jfm.kern[j] then jfm.kern[j]=w end
51 end
52
53 -- return nil iff ltj.metrics[ind] is a bad metric
54 local function ljfm_consistency_check(ind)
55    local t = ltj.metrics[ind]
56    local r = nil
57    if ljfm_jfm_cons then r = ind end
58    if t.dir~='yoko' then -- TODO: tate?
59       r=nil
60    elseif type(t.zw)~='number' or type(t.zh)~='number' then 
61       r=nil -- .zw, .zh must be present
62    end
63    if not r then ltj.metrics[ind] = nil end
64    return r
65 end
66
67 local function ljfm_find_char_class(c,m)
68 -- c: character code, m
69    if not ltj.metrics[m] then return 0 end
70    return ltj.metrics[m].chars[c] or 0
71 end
72 ltj.int_find_char_class = ljfm_find_char_class
73
74 local function ljfm_load_jfont_metric()
75    if jfm_file_name=='' then 
76       ltj.error('no JFM specified', 
77                 {[1]='To load and define a Japanese font, the name of JFM must be specified.',
78                  [2]="The JFM 'ujis' will be  used for now."})
79       jfm_file_name='ujis'
80    end
81    local name=jfm_file_name .. ':' .. jfm_var
82    local i = nil
83    for j,v in ipairs(ltj.metrics) do 
84       if v.name==name then i=j; break end
85    end
86    local t = {}
87    if i then  return i end
88    jfm.char_type={}; jfm.glue={}; jfm.kern={}; jfm.chars = {}
89    ljfm_jfm_cons = true
90    ltj.loadlua('jfm-' .. jfm_file_name .. '.lua')
91    t.name=name
92    t.dir=jfm.dir; t.zw=jfm.zw; t.zh=jfm.zh
93    t.char_type=jfm.char_type; t.chars=jfm.chars
94    t.glue=jfm.glue; t.kern=jfm.kern
95    table.insert(ltj.metrics,t)
96    return ljfm_consistency_check(#ltj.metrics)
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; ltj.font_metric_table[fn].size=f.size
128    tex.sprint(ltj.is_global .. '\\protected\\expandafter\\def\\csname '
129               .. cstemp .. '\\endcsname'
130               .. '{\\csname ltj@curjfnt\\endcsname=' .. fn
131               .. ' \\zw=' .. tex.round(f.size*ltj.metrics[j].zw) .. 'sp'
132               .. '\\zh=' .. tex.round(f.size*ltj.metrics[j].zh) .. 'sp\\relax}')
133 end
134
135 -- extract jfm_file_name and jfm_var
136 local function ljft_extract_metric(name)
137    local basename=name
138    local tmp = utf.sub(basename, 1, 5)
139    jfm_file_name = ''; jfm_var = ''
140    if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
141       basename = utf.sub(basename, 6)
142    end
143    local p = utf.find(basename, ":")
144    if p then 
145       basename = utf.sub(basename, p+1)
146    else return 
147    end
148    -- now basename contains 'features' only.
149    p=1
150    while p do
151       local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
152       if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
153          jfm_file_name = utf.sub(basename, p+4, q-1)
154       elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
155          jfm_var = utf.sub(basename, p+7, q-1)
156       end
157       if utf.len(basename)+1==q then p = nil else p = q + 1 end
158    end
159    return
160 end
161
162 -- replace fonts.define.read()
163 local ljft_dr_orig = fonts.define.read
164 function fonts.define.read(name, size, id)
165    ljft_extract_metric(name)
166    -- In the present imple., we don't remove "jfm=..." from name.
167    return ljft_dr_orig(name, size, id)
168 end
169
170 ------------------------------------------------------------------------
171 -- MANAGING THE RANGE OF JAPANESE CHARACTERS (prefix: rgjc)
172 ------------------------------------------------------------------------
173 -- jcr_table_main[chr_code] = index
174 -- index : internal 0, 1, 2, ..., 216               0: 'other'
175 --         external    1  2       216, (out of range): 'other'
176
177 -- initialize 
178 local jcr_table_main = {}
179 local jcr_cjk = 0; local jcr_noncjk = 1; local ucs_out = 0x110000
180
181 for i=0x80 ,0xFF      do jcr_table_main[i]=1 end
182 for i=0x100,ucs_out-1 do jcr_table_main[i]=0 end
183
184 -- EXT: add characters to a range
185 function ltj.ext_add_char_range(b,e,ind) -- ind: external range number
186    if ind<0 or ind>216 then 
187       ltj.error('Invalid range number (' .. ind ..
188                 '), should be in the range 1..216.',
189              {}); return
190    end
191    for i=math.max(0x80,b),math.min(ucs_out-1,e) do
192       jcr_table_main[i]=ind
193    end
194 end
195
196 local function rgjc_char_to_range(c) -- return the (external) range number
197    if c<0x80 or c>=ucs_out then return -1
198    else 
199       local i = jcr_table_main[c] or 0
200       if i==0 then return 217 else return i end
201    end
202 end
203
204 local function rgjc_get_range_setting(i) -- i: internal range number
205    return floor(tex.getattribute(
206                         luatexbase.attributes['ltj@kcat'..floor(i/31)])
207                      /math.pow(2, i%31))%2
208 end
209 ltj.int_get_range_setting = rgjc_get_range_setting
210 ltj.int_char_to_range = rgjc_char_to_range
211
212 --  glyph_node p は和文文字か?
213 local function rgjc_is_ucs_in_japanese_char(p)
214    local c = p.char
215    if c<0x80 then return false 
216    else 
217       local i=jcr_table_main[c] 
218       return (floor(
219                  has_attr(p, luatexbase.attributes['ltj@kcat'..floor(i/31)])
220                  /math.pow(2, i%31))%2 ~= jcr_noncjk) 
221    end
222 end
223 ltj.int_is_ucs_in_japanese_char = rgjc_is_ucs_in_japanese_char
224
225 -- EXT
226 function ltj.ext_toggle_char_range(g, i) -- i: external range number
227    if i==0 then return 
228    else
229       local kc
230       if i>0 then kc=0 else kc=1; i=-i end
231       if i>216 then i=0 end
232       local attr = luatexbase.attributes['ltj@kcat'..floor(i/31)]
233       local a = tex.getattribute(attr)
234       local k = math.pow(2, i%31)
235       tex.setattribute(g,attr,(floor(a/k/2)*2+kc)*k+a%k)
236    end
237 end
238
239 ------------------------------------------------------------------------
240 -- MISC
241 ------------------------------------------------------------------------
242
243 -- EXT: italic correction
244 function ltj.ext_append_italic()
245    local p = tex.nest[tex.nest.ptr].tail
246    if p and p.id==id_glyph then
247       local f = p.font
248       local g = node_new(id_kern)
249       g.subtype = 1; node.set_attribute(g, attr_icflag, 1)
250       if rgjc_is_ucs_in_japanese_char(p) then
251          f = has_attr(p, attr_curjfnt)
252          print(f, p.char)
253          local j = ltj.font_metric_table[f]
254          local c = ljfm_find_char_class(p.char, j.jfm)
255          g.kern = round(j.size * ltj.metrics[j.jfm].char_type[c].italic)
256       else
257          g.kern = font.fonts[f].characters[p.char].italic
258       end
259       node.write(g)
260    end
261 end