--
luatexbase.provides_module({
name = 'luatexja.adjust',
- date = '2014/01/24',
+ date = '2014/01/26',
description = 'Advanced line adjustment for LuaTeX-ja',
})
module('luatexja.adjust', package.seeall)
local PACKED = luatexja.icflag_table.PACKED
local FROM_JFM = luatexja.icflag_table.FROM_JFM
local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
+local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
+local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
+local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
local priority_table = {
FROM_JFM + 2,
KANJI_SKIP
}
-local PROCESSED_BEGIN_FLAG = 32
local function get_attr_icflag(p)
return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
end
end
local res = {}
-local is_skip_normal = {}
local function get_total_stretched(p)
local go, gf, gs
= getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
-- それはここでは望ましくないので,各 glue ごとに異なる spec を使う.
-- (この仮定でメモリリークを起こしている!)
-- JFM グルーはそれぞれ異なる glue_spec を用いているので,問題ない.
- res[ic] = res[ic] + a
if (ic == KANJI_SKIP or ic == XKANJI_SKIP) and getsubtype(q)==0 then
local qs = getfield(q, 'spec')
- if is_skip_normal[ic] and qs ~= spec_zero_glue then
+ if qs ~= spec_zero_glue then
+ local f = node_new(id_glue)
+ setfield(f, 'spec', qs)
setfield(q, 'spec', node_copy(qs))
+ node_free(f)
end
+ elseif ic == KANJI_SKIP_JFM then ic = KANJI_SKIP
+ elseif ic == XKANJI_SKIP_JFM then ic = XKANJI_SKIP
end
+ res[ic] = res[ic] + a
else
res[0] = res[0] + a
end
set_stretch_table[i] = nil
end
for q in node_traverse_id(id_glue, getlist(p)) do
- if get_attr_icflag(q) == ic then
+ local f = get_attr_icflag(q)
+ if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
+ or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
local qs, do_flag = getfield(q, 'spec'), true
for i=1,#set_stretch_table do
if set_stretch_table[i]==qs then do_flag = false end
end
-function adjust_width(head)
+local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
+local function adjust_width(head)
if not head then return head end
- is_skip_normal[KANJI_SKIP] = (ltjs.fast_get_stack_skip('kanjiskip').width ~= 1073741823)
- is_skip_normal[XKANJI_SKIP] = (ltjs.fast_get_stack_skip('xkanjiskip').width ~= 1073741823)
for p in node_traverse_id(id_hlist, to_direct(head)) do
- local res = get_total_stretched(p)
+ local res = get_total_stretched(p)
+ -- this is the same table as the table which is def'd in l. 92
if res then
-- 調整量の合計
local total = 0
total = total + v
end
end; total = round(total * res.glue_set)
- local added_flag = aw_step1(p, res, total)
- --print(total, res[0], res[KANJI_SKIP], res[FROM_JFM])
- aw_step2(p, res, total, added_flag)
+ aw_step2(p, res, total, aw_step1(p, res, total))
end
end
return to_node(head)
end
-local is_reg = false
-function enable_cb()
- if not is_reg then
- luatexbase.add_to_callback('post_linebreak_filter', adjust_width, 'Adjust width', 100)
- is_reg = true
+do
+ local is_reg = false
+ function enable_cb()
+ if not is_reg then
+ luatexbase.add_to_callback('post_linebreak_filter',
+ adjust_width, 'Adjust width', 100)
+ is_reg = true
+ end
end
-end
-function disable_cb()
- if is_reg then
- luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
- is_reg = false
+ function disable_cb()
+ if is_reg then
+ luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
+ is_reg = false
+ end
end
end
local PACKED = luatexja.icflag_table.PACKED
local KINSOKU = luatexja.icflag_table.KINSOKU
local FROM_JFM = luatexja.icflag_table.FROM_JFM
-local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
-local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
local PROCESSED = luatexja.icflag_table.PROCESSED
local IC_PROCESSED = luatexja.icflag_table.IC_PROCESSED
local BOXBDD = luatexja.icflag_table.BOXBDD
-- get kanjiskip
local get_kanjiskip
-
-local function get_kanjiskip_normal()
- if Np.auto_kspc or Nq.auto_kspc then
- return node_copy(kanji_skip)
- else
- local g = node_copy(zero_glue)
- set_attr(g, attr_icflag, KANJI_SKIP)
- return g
+local get_kanjiskip_normal, get_kanjiskip_jfm
+do
+ local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
+ local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
+ get_kanjiskip_normal = function ()
+ if Np.auto_kspc or Nq.auto_kspc then
+ return node_copy(kanji_skip)
+ else
+ local g = node_copy(zero_glue)
+ set_attr(g, attr_icflag, KANJI_SKIP)
+ return g
+ end
end
-end
-local function get_kanjiskip_jfm()
- local g
- if Np.auto_kspc or Nq.auto_kspc then
- g = node_new(id_glue); --copy_attr(g, Nq.nuc)
- local gx = node_new(id_glue_spec);
- setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
- local pm, qm = Np.met, Nq.met
- local bk = qm.kanjiskip or {0, 0, 0}
- if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
- setfield(gx, 'width', bk[1])
- setfield(gx, 'stretch', bk[2])
- setfield(gx, 'shrink', bk[3])
+ get_kanjiskip_jfm = function ()
+ local g
+ if Np.auto_kspc or Nq.auto_kspc then
+ g = node_new(id_glue); --copy_attr(g, Nq.nuc)
+ local gx = node_new(id_glue_spec);
+ setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
+ local pm, qm = Np.met, Nq.met
+ local bk = qm.kanjiskip or {0, 0, 0}
+ if (pm.char_type==qm.char_type) and (qm.var==pm.var) then
+ setfield(gx, 'width', bk[1])
+ setfield(gx, 'stretch', bk[2])
+ setfield(gx, 'shrink', bk[3])
+ else
+ local ak = pm.kanjiskip or {0, 0, 0}
+ setfield(gx, 'width', round(diffmet_rule(bk[1], ak[1])))
+ setfield(gx, 'stretch', round(diffmet_rule(bk[2], ak[2])))
+ setfield(gx, 'shrink', -round(diffmet_rule(-bk[3], -ak[3])))
+ end
+ setfield(g, 'spec', gx)
else
- local ak = pm.kanjiskip or {0, 0, 0}
- setfield(gx, 'width', round(diffmet_rule(bk[1], ak[1])))
- setfield(gx, 'stretch', round(diffmet_rule(bk[2], ak[2])))
- setfield(gx, 'shrink', -round(diffmet_rule(-bk[3], -ak[3])))
+ g = node_copy(zero_glue)
end
- setfield(g, 'spec', gx)
- else
- g = node_copy(zero_glue)
+ set_attr(g, attr_icflag, KANJI_SKIP_JFM)
+ return g
end
- set_attr(g, attr_icflag, KANJI_SKIP)
- return g
end
local calc_ja_ja_aux
-- get xkanjiskip
local get_xkanjiskip
-local function get_xkanjiskip_normal(Nn)
- if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
- local f = node_copy(xkanji_skip)
- return f
- else
- local g = node_copy(zero_glue)
- set_attr(g, attr_icflag, XKANJI_SKIP)
- return g
+local get_xkanjiskip_normal, get_xkanjiskip_jfm
+do
+ local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
+ local XKANJI_SKIP_JFM = luatexja.icflag_table.XKANJI_SKIP_JFM
+ get_xkanjiskip_normal = function (Nn)
+ if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
+ local f = node_copy(xkanji_skip)
+ return f
+ else
+ local g = node_copy(zero_glue)
+ set_attr(g, attr_icflag, XKANJI_SKIP)
+ return g
+ end
end
-end
-local function get_xkanjiskip_jfm(Nn)
- local g
- if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
- g = node_new(id_glue)
- local gx = node_new(id_glue_spec);
- setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
- local bk = Nn.met.xkanjiskip or {0, 0, 0}
- setfield(gx, 'width', bk[1])
- setfield(gx, 'stretch', bk[2])
- setfield(gx, 'shrink', bk[3])
- setfield(g, 'spec', gx)
- else
- g = node_copy(zero_glue)
+ get_xkanjiskip_jfm = function (Nn)
+ local g
+ if (Nq.xspc>=2) and (Np.xspc%2==1) and (Nq.auto_xspc or Np.auto_xspc) then
+ g = node_new(id_glue)
+ local gx = node_new(id_glue_spec);
+ setfield(gx, 'stretch_order', 0); setfield(gx, 'shrink_order', 0)
+ local bk = Nn.met.xkanjiskip or {0, 0, 0}
+ setfield(gx, 'width', bk[1])
+ setfield(gx, 'stretch', bk[2])
+ setfield(gx, 'shrink', bk[3])
+ setfield(g, 'spec', gx)
+ else
+ g = node_copy(zero_glue)
+ end
+ set_attr(g, attr_icflag, XKANJI_SKIP_JFM)
+ return g
end
- set_attr(g, attr_icflag, XKANJI_SKIP)
- return g
end
-------------------- 隣接した「塊」間の処理
-- initialize
-- return value: (the initial cursor lp), (last node)
-local function init_var(mode)
- -- 1073741823: max_dimen
- Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
- table_current_stack = ltjs.table_current_stack
-
- kanji_skip = node_new(id_glue)
- setfield(kanji_skip, 'spec', skip_table_to_spec('kanjiskip'))
- set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
- get_kanjiskip = (getfield(getfield(kanji_skip, 'spec'), 'width') == 1073741823)
- and get_kanjiskip_jfm or get_kanjiskip_normal
-
- xkanji_skip = node_new(id_glue)
- setfield(xkanji_skip, 'spec', skip_table_to_spec('xkanjiskip'))
- set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
- get_xkanjiskip = (getfield(getfield(xkanji_skip, 'spec'), 'width') == 1073741823)
- and get_xkanjiskip_jfm or get_xkanjiskip_normal
-
- Np = {
- auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
- first=nil, id=nil, last=nil, met=nil, nuc=nil,
- post=nil, pre=nil, xspc=nil,
- }
- Nq = {
- auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
- first=nil, id=nil, last=nil, met=nil, nuc=nil,
- post=nil, pre=nil, xspc=nil,
- }
- if mode then
- -- the current list is to be line-breaked:
- -- hbox from \parindent is skipped.
- local lp, par_indented, lpi, lps = head, 'boxbdd', getid(head), getsubtype(head)
- while lp and ((lpi==id_whatsit and lps~=sid_user)
- or ((lpi==id_hlist) and (lps==3))) do
- if (lpi==id_hlist) and (lps==3) then par_indented = 'parbdd' end
- lp=node_next(lp); lpi, lps = getid(lp), getsubtype(lp) end
- return lp, par_indented
- -- return lp, node_tail(head), par_indented
- else
- -- the current list is the contents of a hbox:
- return head, 'boxbdd'
+local init_var
+do
+ local KANJI_SKIP = luatexja.icflag_table.KANJI_SKIP
+ local XKANJI_SKIP = luatexja.icflag_table.XKANJI_SKIP
+ init_var = function (mode)
+ -- 1073741823: max_dimen
+ Bp, widow_Bp, widow_Np = {}, {}, {first = nil}
+ table_current_stack = ltjs.table_current_stack
+
+ kanji_skip = node_new(id_glue)
+ setfield(kanji_skip, 'spec', skip_table_to_spec('kanjiskip'))
+ set_attr(kanji_skip, attr_icflag, KANJI_SKIP)
+ get_kanjiskip = (getfield(getfield(kanji_skip, 'spec'), 'width') == 1073741823)
+ and get_kanjiskip_jfm or get_kanjiskip_normal
+
+ xkanji_skip = node_new(id_glue)
+ setfield(xkanji_skip, 'spec', skip_table_to_spec('xkanjiskip'))
+ set_attr(xkanji_skip, attr_icflag, XKANJI_SKIP)
+ get_xkanjiskip = (getfield(getfield(xkanji_skip, 'spec'), 'width') == 1073741823)
+ and get_xkanjiskip_jfm or get_xkanjiskip_normal
+
+ Np = {
+ auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
+ first=nil, id=nil, last=nil, met=nil, nuc=nil,
+ post=nil, pre=nil, xspc=nil,
+ }
+ Nq = {
+ auto_kspc=nil, auto_xspc=nil, char=nil, class=nil,
+ first=nil, id=nil, last=nil, met=nil, nuc=nil,
+ post=nil, pre=nil, xspc=nil,
+ }
+ if mode then
+ -- the current list is to be line-breaked:
+ -- hbox from \parindent is skipped.
+ local lp, par_indented, lpi, lps = head, 'boxbdd', getid(head), getsubtype(head)
+ while lp and ((lpi==id_whatsit and lps~=sid_user)
+ or ((lpi==id_hlist) and (lps==3))) do
+ if (lpi==id_hlist) and (lps==3) then par_indented = 'parbdd' end
+ lp=node_next(lp); lpi, lps = getid(lp), getsubtype(lp) end
+ return lp, par_indented
+ else
+ return head, 'boxbdd'
+ end
end
end