OSDN Git Service

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