OSDN Git Service

use helpers which exists in LuaTeX 1.0.3 (WIP)
[luatex-ja/luatexja.git] / src / ltj-setwidth.lua
1 --
2 -- src/ltj-setwidth.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('stack');     local ltjs = luatexja.stack
7 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
8 luatexja.load_module('direction'); local ltjd = luatexja.direction
9
10 local setfield = node.direct.setfield
11 local getfield = node.direct.getfield
12 local getid = node.direct.getid
13 local getfont = node.direct.getfont
14 local getlist = node.direct.getlist
15 local getchar = node.direct.getchar
16 local getsubtype = node.direct.getsubtype
17 local getwhd = node.direct.getwhd or function(n)
18   return getfield(n,'width'), getfield(n,'height'),getfield(n,'depth') end
19 local setwhd = node.direct.setwhd or function(n,w,h,d)
20   setfield(n,'width',w); setfield(n,'height',h); setfield(n,'depth',d) end
21 local setchar = node.direct.setchar or function(n,c) setfield(n,'char',c) end
22 local setoffsets = node.direct.setoffsets or function(n,x,y)
23   setfield(n,'xoffset',x); setfield(n,'yoffset',y)  end
24 local getoffsets = node.direct.getoffsets or function(n)
25   return getfield(n,'xoffset'), getfield(n,'yoffset')  end
26
27 local node_traverse_id = node.direct.traverse_id
28 local node_traverse = node.direct.traverse
29 local node_new = node.direct.new
30 local node_copy = node.direct.copy
31 local node_remove = node.direct.remove
32 local node_tail = node.direct.tail
33 local node_next = node.direct.getnext or node.next
34 local has_attr = node.direct.has_attribute
35 local set_attr = node.direct.set_attribute
36 local node_insert_before = node.direct.insert_before
37 local node_insert_after = node.direct.insert_after
38 local round = tex.round
39
40 local id_glyph = node.id('glyph')
41 local id_kern = node.id('kern')
42 local id_hlist = node.id('hlist')
43 local id_vlist = node.id('vlist')
44 local id_rule = node.id('rule')
45 local id_math = node.id('math')
46 local id_whatsit = node.id('whatsit')
47 local sid_save = node.subtype('pdf_save')
48 local sid_restore = node.subtype('pdf_restore')
49 local sid_matrix = node.subtype('pdf_setmatrix')
50 local dir_tate = luatexja.dir_table.dir_tate
51
52 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
53 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
54 local attr_icflag = luatexbase.attributes['ltj@icflag']
55
56 local ltjf_font_metric_table = ltjf.font_metric_table
57 local ltjf_font_extra_info = ltjf.font_extra_info
58
59 local PACKED       = luatexja.icflag_table.PACKED
60 local PROCESSED    = luatexja.icflag_table.PROCESSED
61
62 local get_pr_begin_flag
63 do
64    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
65    local floor = math.floor
66    get_pr_begin_flag = function (p)
67       local i = has_attr(p, attr_icflag) or 0
68       return i - i%PROCESSED_BEGIN_FLAG
69    end
70 end
71
72 local ltjw = {} --export
73 luatexja.setwidth = ltjw
74
75 luatexbase.create_callback("luatexja.set_width", "data",
76                            function (fstable, fmtable, char_data)
77                               return fstable
78                            end)
79 local call_callback = luatexbase.call_callback
80
81 local fshift =  { down = 0, left = 0}
82
83 local min, max = math.min, math.max
84
85 local rule_subtype = (status.luatex_version>=85) and 3 or 0
86
87 -- 和文文字の位置補正(横)
88 local function capsule_glyph_yoko(p, met, char_data, head, dir)
89    if not char_data then return node_next(p), head, p end
90    fshift.down = char_data.down; fshift.left = char_data.left
91    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
92    local kbl = has_attr(p, attr_ykblshift) or 0
93    --
94    -- f*: whd specified in JFM
95    local pwidth, pheight,pdepth = getwhd(p)
96    local fwidth = char_data.width or pwidth
97    local fheight= char_data.height or pheight
98    local fdepth = char_data.depth or pdepth
99    if pwidth==fwidth then
100       -- 補正後glyph node は ht: p.height - kbl - down, dp: p.depth + min(0, kbl+down) を持つ
101       -- 設定されるべき寸法: ht: fheight - kbl, dp: fdepth + kbl
102       local ht_diff = fheight + fshift.down - pheight
103       local dp_diff = fdepth  + kbl - pdepth - min(kbl + fshift.down, 0)
104       if ht_diff == 0 and dp_diff ==0 then -- offset only
105          set_attr(p, attr_icflag, PROCESSED)
106          local xo, yo = getoffsets(p)
107          setoffsets(p, xo - fshift.left, yo - kbl - fshift.down)
108          return node_next(p), head, p
109       elseif ht_diff >= 0 and dp_diff >=0 then -- rule
110          local box = node_new(id_rule,rule_subtype)
111          local xo, yo = getoffsets(p)
112          setoffsets(p, xo, yo - kbl - fshift.down)
113          setwhd(box, 0, fheight - kbl, fdepth + kbl)
114          setfield(box, 'dir', dir)
115          set_attr(box, attr_icflag, PACKED)
116          set_attr(p, attr_icflag, PROCESSED)
117          head = p and node_insert_before(head, p, box)
118             or node_insert_after(head, node_tail(head), box)
119          return node_next(p), head, p, box
120       end
121    end
122
123    local q
124    head, q = node_remove(head, p)
125    setfield(p, 'yoffset', getfield(p, 'yoffset') -fshift.down);
126    setfield(p, 'next', nil)
127    setfield(p, 'xoffset', getfield(p, 'xoffset')
128                + char_data.align*(fwidth-pwidth) - fshift.left)
129    local box = node_new(id_hlist)
130    setwhd(box, fwidth, fheight, fdepth)
131    setfield(box, 'head', p)
132    setfield(box, 'shift', kbl)
133    setfield(box, 'dir', dir)
134    set_attr(box, attr_icflag, PACKED)
135    head = q and node_insert_before(head, q, box)
136       or node_insert_after(head, node_tail(head), box)
137    return q, head, box
138 end
139
140 luatexja.setwidth.capsule_glyph_yoko = capsule_glyph_yoko
141
142 -- 和文文字の位置補正(縦)
143 local function capsule_glyph_tate(p, met, char_data, head, dir)
144    if not char_data then return node_next(p), head end
145    local ascent, descent = met.ascent, met.descent
146    local fwidth, pwidth = char_data.width
147    do
148       local pf = getfont(p)
149       local pc = getchar(p)
150       setchar(p, pc)
151       pwidth = ltjf_font_extra_info[pf] and  ltjf_font_extra_info[pf][pc]
152          and ltjf_font_extra_info[pf][pc].vwidth
153          and ltjf_font_extra_info[pf][pc].vwidth * met.size or (ascent+descent)
154       pwidth = pwidth + (met.v_advance[pc] or 0)
155       ascent = met.v_origin[pc] and ascent - met.v_origin[pc] or ascent
156    end
157    fwidth = fwidth or pwidth
158    fshift.down = char_data.down; fshift.left = char_data.left
159    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
160    local fheight = char_data.height or 0
161    local fdepth  = char_data.depth or 0
162    local y_shift
163       = getfield(p, 'xoffset') + (has_attr(p,attr_tkblshift) or 0)
164    local q
165    head, q = node_remove(head, p)
166    local box = node_new(id_hlist)
167    setwhd(box, fwidth, fheight, fdepth)
168    setfield(box, 'shift', y_shift)
169    setfield(box, 'dir', dir)
170
171    setfield(p, 'xoffset', - fshift.down)
172    setfield(p, 'yoffset', getfield(p, 'yoffset') -(ascent
173                                 + char_data.align*(fwidth-pwidth) - fshift.left) )
174    local ws = node_new(id_whatsit, sid_save)
175    local wm = node_new(id_whatsit, sid_matrix)
176    setfield(wm, 'data', '0 1 -1 0')
177    local pwnh = -round(0.5*getfield(p, 'width'))
178    local k2 = node_new(id_kern, 1); setfield(k2, 'kern', pwnh)
179    local k3 = node_new(id_kern, 1); setfield(k3, 'kern', -getfield(p, 'width')-pwnh)
180    local wr = node_new(id_whatsit, sid_restore)
181    setfield(box, 'head', ws)
182    setfield(ws, 'next', wm);  setfield(wm, 'next', k2);
183    setfield(k2, 'next', p);   setfield(p, 'next', k3);
184    setfield(k3, 'next', wr);
185
186    set_attr(box, attr_icflag, PACKED)
187    head = q and node_insert_before(head, q, box)
188       or node_insert_after(head, node_tail(head), box)
189    return q, head, box
190 end
191 luatexja.setwidth.capsule_glyph_tate = capsule_glyph_tate
192
193 local function capsule_glyph_math(p, met, char_data)
194    if not char_data then return nil end
195    local fwidth, pwidth = char_data.width, getfield(p, 'width')
196    fwidth = fwidth or pwidth
197    fshift.down = char_data.down; fshift.left = char_data.left
198    fshift = call_callback("luatexja.set_width", fshift, met, char_data)
199    local fheight, fdepth = char_data.height, char_data.depth
200    local y_shift
201       = - getfield(p, 'yoffset') + (has_attr(p,attr_ykblshift) or 0)
202    setfield(p, 'yoffset', -fshift.down)
203    setfield(p, 'xoffset', getfield(p, 'xoffset') + char_data.align*(fwidth-pwidth) - fshift.left)
204    local box = node_new(id_hlist);
205    setfield(box, fwidth, fheight, fdepth)
206    setfield(box, 'head', p)
207    setfield(box, 'shift', y_shift)
208    setfield(box, 'dir', tex.mathdir)
209    set_attr(box, attr_icflag, PACKED)
210    return box
211 end
212 luatexja.setwidth.capsule_glyph_math = capsule_glyph_math
213
214 -- 数式の位置補正
215 function luatexja.setwidth.apply_ashift_math(head, last, attr_ablshift)
216    for p in node_traverse(head) do
217       local pid = getid(p)
218       if p==last then
219          return
220       elseif (has_attr(p, attr_icflag) or 0) ~= PROCESSED then
221          if pid==id_hlist or pid==id_vlist then
222             setfield(p, 'shift', getfield(p, 'shift') +  (has_attr(p,attr_ablshift) or 0)) 
223          elseif pid==id_rule then
224             local v = has_attr(p,attr_ablshift) or 0
225             setfield(p, 'height', getfield(p, 'height')-v)
226             setfield(p, 'depth', getfield(p, 'depth')+v)
227             set_attr(p, attr_icflag, PROCESSED)
228          elseif pid==id_glyph then
229             -- 欧文文字; 和文文字は pid == id_hlist の場合で処理される
230             -- (see conv_jchar_to_hbox_A in ltj-math.lua)
231             setfield(p, 'yoffset',
232                      getfield(p, 'yoffset') - (has_attr(p,attr_ablshift) or 0))
233          end
234          set_attr(p, attr_icflag, PROCESSED)
235       end
236    end
237 end
238
239 -- discretionary の位置補正
240 do
241    local attr_yablshift = luatexbase.attributes['ltj@yablshift']
242    local attr_tablshift = luatexbase.attributes['ltj@tablshift']
243    local attr_ablshift
244    local disc, tex_dir
245    local function ashift_disc_inner(field)
246       local head = getfield(disc, field)
247       if not head then return end
248       local y_adjust, node_depth, adj_depth = 0, 0, 0
249       for lp in node_traverse_id(id_glyph, head) do
250          y_adjust = has_attr(lp,attr_ablshift) or 0
251          node_depth = max(getfield(lp, 'depth') + min(y_adjust, 0), node_depth)
252          adj_depth = (y_adjust>0) and max(getfield(lp, 'depth') + y_adjust, adj_depth) or adj_depth
253          setfield(lp, 'yoffset', getfield(lp, 'yoffset') - y_adjust)
254       end
255       if adj_depth>node_depth then
256          local r = node_new(id_rule,rule_subtype)
257          setfield(r, 'width', 0); setfield(r, 'height', 0)
258          setfield(r, 'depth', adj_depth); setfield(r, 'dir', tex_dir)
259          set_attr(r, attr_icflag, PROCESSED)
260          if field=='post' then
261             node_insert_after(head, head, r)
262          else
263             setfield(disc, field, (node_insert_before(head, head, r)))
264          end
265       end
266    end
267    function luatexja.setwidth.apply_ashift_disc(d, is_dir_tate, dir)
268       attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
269       disc, tex_dir = d, dir
270       ashift_disc_inner('pre')
271       ashift_disc_inner('post')
272       ashift_disc_inner('replace')
273    end
274 end