OSDN Git Service

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