+-- step 1 最終行用
+local min, max = math.min, math.max
+local function aw_step1_last(p, total)
+ local head = getlist(p)
+ local x = node_tail(head); if not x then return total, false end
+ -- x: \rightskip
+ pf = node_prev(x); if not x then return total, false end
+ if getid(pf) ~= id_glue or getsubtype(pf) ~= 15 then return total, false end
+ x = node_prev(node_prev(pf)); xi = getid(x)
+ local xi, xc = getid(x)
+ if xi == id_glyph and getfield(x, 'lang')==lang_ja then
+ -- 和文文字
+ xc = x
+ elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
+ -- packed JAchar
+ xc = ltjd_glyph_from_packed(x)
+ while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
+ else
+ return total, false-- それ以外は対象外.
+ end
+ -- 続行条件1:無限の伸縮度を持つグルーは \parfillskipのみ
+ if total>0 and total_st.order>0 then
+ if total_st.order ~= getfield(pf, 'stretch_order') then return total, false end
+ if total_st[total_st.order*65536] ~= getfield(pf, 'stretch') then return total, false end
+ for i=total_st.order-1, 1, -1 do
+ if total_st[i*65536] ~= 0 then return total, false end
+ end
+ end
+ if total<0 and total_sh.order>0 then
+ if total_sh.order ~= getfield(pf, 'shrink_order') then return total, false end
+ if total_sh[total_sh.order*65536] ~= getfield(pf, 'shrink') then return total, false end
+ for i=total_sh.order-1, 1, -1 do
+ if total_sh[i*65536] ~= 0 then return total, false end
+ end
+ end
+ local eadt = ltjf_font_metric_table[getfont(xc)]
+ .char_type[has_attr(xc, attr_jchar_class) or 0].end_adjust
+ if not eadt then
+ return total, false
+ end
+ -- 続行条件2: min(eadt[1], 0)<= \parfillskip <= max(eadt[#eadt], 0)
+ local pfw = getfield(pf, 'width')
+ + (total>0 and getfield(pf, 'stretch') or -getfield(pf, 'shrink')) *getfield(p, 'glue_set')
+ if pfw<min(0,eadt[1]) or max(0,eadt[#eadt])<pfw then return total, false end
+ -- \parfillskip を 0 にする
+ total = total + getfield(pf, 'width')
+ total_st.order, total_sh.order = 0, 0
+ if getfield(pf, 'stretch_order')==0 then
+ local i = at2pr_st[-1]
+ total_st[0] = total_st[0] - getfield(pf, 'stretch')
+ total_st[i] = total_st[i] - getfield(pf, 'stretch')
+ total_st.order = (total_st[0]==0) and -1 or 0
+ end
+ if getfield(pf, 'shrink_order')==0 then
+ local i = at2pr_sh[-1]
+ total_sh[0] = total_sh[0] - getfield(pf, 'shrink')
+ total_sh[i] = total_sh[i] - getfield(pf, 'shrink')
+ total_sh.order = (total_sh[0]==0) and -1 or 0
+ end
+ setfield(pf, 'subtype', 1); setglue(pf)
+ local eadt_ratio = {}
+ for i, v in ipairs(eadt) do
+ local t = total - v
+ if t>0 then
+ eadt_ratio[i] = {i, t/total_st[65536*total_st.order], t, v}
+ else
+ eadt_ratio[i] = {i, t/total_sh[65536*total_sh.order], t, v}
+ end
+ end
+ table.sort(eadt_ratio,
+ function (a,b)
+ for i=2,4 do
+ local at, bt = abs(a[i]), abs(b[i])
+ if at~=bt then return at<bt end
+ end
+ return a[4]<b[4]
+ end)
+ if eadt[eadt_ratio[1][1]]~=0 then
+ local kn = node_new(id_kern, 1)
+ setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, LINEEND)