-function adjust_width(head)
- if not head then return head end
- for p in node.traverse_id(id_hlist, head) do
- local res = get_total_stretched(p)
- --print(table.serialize(res))
- if res then
- -- 調整量の合計
- local total = 0
- for i,v in pairs(res) do
- if type(i)=='number' then
- total = total + v
- end
- end; total = tex.round(total * res.glue_set)
- if total <= res[0] then -- 和文処理グルー以外で足りる
- for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
- local f = node.hpack(p.head, p.width, 'exactly')
- f.head, p.glue_set, p.glue_sign, p.glue_order
- = nil, f.glue_set, f.glue_sign, f.glue_order
- node.free(f)
- else
- total = total - res[0]
- for i = 1, #priority_table do
- local v = priority_table[i]
- if total <= res[v] then
- for j = i+1,#priority_table do
- clear_stretch(p, priority_table[j], res.name)
- end
- set_stretch(p, total, res[v], v, res.name)
- local f = node.hpack(p.head, p.width, 'exactly')
- f.head, p.glue_set, p.glue_sign, p.glue_order
- = nil, f.glue_set, f.glue_sign, f.glue_order
- node.free(f)
- --print(p.glue_set, p.glue_sign, p.glue_order)
- return head
- end
- total = total - res[v]
+ if xk>0 and total>=xk then
+ total = total - xk
+ local kn = node_new(id_kern)
+ setfield(kn, 'kern', (res.name=='shrink' and -1 or 1) * xk)
+ set_attr(kn, attr_icflag, FROM_JFM)
+ insert_after(head, x, kn)
+ return true
+ else return false
+ end
+end
+
+-- step 2: 行中の glue を変える
+local function aw_step2(p, res, total, added_flag)
+ if total == 0 then -- もともと伸縮の必要なし
+ if added_flag then -- 行末に kern 追加したので,それによる補正
+ local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
+ setfield(f, 'head', nil)
+ setfield(p, 'glue_set', getfield(f, 'glue_set'))
+ setfield(p, 'glue_order', getfield(f, 'glue_order'))
+ setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
+ node_free(f)
+ return
+ end
+ elseif total <= res[0] then -- 和文処理グルー以外で足りる
+ for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
+ local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
+ setfield(f, 'head', nil)
+ setfield(p, 'glue_set', getfield(f, 'glue_set'))
+ setfield(p, 'glue_order', getfield(f, 'glue_order'))
+ setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
+ node_free(f)
+ else
+ total = total - res[0]
+ for i = 1, #priority_table do
+ local v = priority_table[i]
+ if total <= res[v] then
+ for j = i+1,#priority_table do
+ clear_stretch(p, priority_table[j], res.name)