OSDN Git Service

Dnode -> node.direct, stopped to use 'spec'
[luatex-ja/luatexja.git] / src / ltj-adjust.lua
1 --
2 -- ltj-adjust.lua
3 --
4 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
5 luatexja.load_module('jfmglue');   local ltjj = luatexja.jfmglue
6 luatexja.load_module('stack');     local ltjs = luatexja.stack
7 luatexja.load_module('direction'); local ltjd = luatexja.direction
8
9 local to_node = node.direct.tonode
10 local to_direct = node.direct.todirect
11
12 local setfield = node.direct.setfield
13 local setglue = luatexja.setglue
14 local getfield = node.direct.getfield
15 local is_zero_glue = node.direct.is_zero_glue
16 local getlist = node.direct.getlist
17 local getid = node.direct.getid
18 local getfont = node.direct.getfont
19 local getsubtype = node.direct.getsubtype
20
21 local node_traverse_id = node.direct.traverse_id
22 local node_new = node.direct.new
23 local node_copy = node.direct.copy
24 local node_hpack = node.direct.hpack
25 local node_next = node.direct.getnext
26 local node_free = node.direct.free
27 local node_prev = node.direct.getprev
28 local node_tail = node.direct.tail
29 local has_attr = node.direct.has_attribute
30 local set_attr = node.direct.set_attribute
31 local insert_after = node.direct.insert_after
32
33 local id_glyph = node.id('glyph')
34 local id_kern = node.id('kern')
35 local id_hlist = node.id('hlist')
36 local id_glue  = node.id('glue')
37 local id_whatsit = node.id('whatsit')
38 local attr_icflag = luatexbase.attributes['ltj@icflag']
39 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
40 local lang_ja = luatexja.lang_ja
41
42 local ltjf_font_metric_table = ltjf.font_metric_table
43 local round, pairs = tex.round, pairs
44
45 local PACKED       = luatexja.icflag_table.PACKED
46 local FROM_JFM     = luatexja.icflag_table.FROM_JFM
47 local KANJI_SKIP   = luatexja.icflag_table.KANJI_SKIP
48 local KANJI_SKIP_JFM = luatexja.icflag_table.KANJI_SKIP_JFM
49 local XKANJI_SKIP  = luatexja.icflag_table.XKANJI_SKIP
50 local XKANJI_SKIP_JFM  = luatexja.icflag_table.XKANJI_SKIP_JFM
51
52 local priority_table = {
53    FROM_JFM + 2,
54    FROM_JFM + 1,
55    FROM_JFM,
56    FROM_JFM - 1,
57    FROM_JFM - 2,
58    XKANJI_SKIP,
59    KANJI_SKIP
60 }
61
62 local get_attr_icflag
63 do
64    local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
65    get_attr_icflag = function(p)
66       return (has_attr(p, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG
67    end
68 end
69
70 -- box 内で伸縮された glue の合計値を計算
71
72 local function get_stretched(q, go, gs)
73    if gs == 1 then -- stretching
74       if getfield(q, 'stretch_order') == go then
75          return getfield(q, 'stretch')
76       else return 0
77       end
78    else -- shrinking
79       if getfield(q, 'shrink_order') == go then
80          return getfield(q, 'shrink')
81       else return 0
82       end
83    end
84 end
85
86 local res = {}
87 local function get_total_stretched(p, line)
88    local go, gf, gs
89       = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
90    if go ~= 0 then return nil end
91    res[0], res.glue_set, res.name = 0, gf, (gs==1) and 'stretch' or 'shrink'
92    for i=1,#priority_table do res[priority_table[i]]=0 end
93    if gs ~= 1 and gs ~= 2 then return res, 0 end
94    local total = 0
95    for q in node_traverse_id(id_glue, getlist(p)) do
96       local a, ic = get_stretched(q, go, gs), get_attr_icflag(q)
97       if ic == KANJI_SKIP_JFM  then ic = KANJI_SKIP
98       elseif ic == XKANJI_SKIP_JFM  then ic = XKANJI_SKIP
99       end
100       if   type(res[ic]) == 'number' then
101          res[ic], total = res[ic] + a, total + a
102       else
103          res[0], total = res[0]  + a, total + a
104       end
105    end
106    return res, total
107 end
108
109 local function clear_stretch(p, ic, name)
110    for q in node_traverse_id(id_glue, getlist(p)) do
111       local f = get_attr_icflag(q)
112       if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
113            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
114          setfield(q, name..'_order', 0)
115          setfield(q, name, 0)
116       end
117    end
118 end
119
120 local function set_stretch(p, after, before, ic, name)
121    if before > 0 then
122       local ratio = after/before
123       for q in node_traverse_id(id_glue, getlist(p)) do
124          local f = get_attr_icflag(q)
125          if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
126            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
127             if getfield(q, name..'_order')==0 then
128                setfield(q, name, getfield(q, name)*ratio)
129             end
130          end
131       end
132    end
133 end
134
135 -- step 1: 行末に kern を挿入(句読点,中点用)
136 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
137 local function aw_step1(p, res, total)
138    local head = getlist(p)
139    local x = node_tail(head); if not x then return false end
140    -- x: \rightskip
141    x = node_prev(x); if not x then return false end
142    local xi, xc = getid(x)
143    if xi == id_glue and getsubtype(x) == 15 then
144       -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
145       -- その前の node が本来の末尾文字となる
146       x = node_prev(node_prev(x)); xi = getid(x)
147    end
148    -- local xi = getid(x)
149    -- while (get_attr_icflag(x) == PACKED)
150    --    and  ((xi == id_penalty) or (xi == id_kern) or (xi == id_kern)) do
151    --       x = node_prev(x); xi = getid(x)
152    -- end
153    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
154       -- 和文文字
155       xc = x
156    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
157       -- packed JAchar
158       xc = ltjd_glyph_from_packed(x)
159       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
160    else
161      return false-- それ以外は対象外.
162    end
163    local xk = ltjf_font_metric_table[getfont(xc)]
164      .char_type[has_attr(xc, attr_jchar_class) or 0]['end_' .. res.name] or 0
165
166    if xk>0 and total>=xk then
167       total = total - xk
168       local kn = node_new(id_kern)
169       setfield(kn, 'kern', (res.name=='shrink' and -1 or 1) * xk)
170       set_attr(kn, attr_icflag, FROM_JFM)
171       insert_after(head, x, kn)
172       return true
173    else return false
174    end
175 end
176
177 -- step 2: 行中の glue を変える
178 local function aw_step2(p, res, total, added_flag)
179    if total == 0 then -- もともと伸縮の必要なし
180       if added_flag then -- 行末に kern 追加したので,それによる補正
181          local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
182          setfield(f, 'head', nil)
183          setfield(p, 'glue_set', getfield(f, 'glue_set'))
184          setfield(p, 'glue_order', getfield(f, 'glue_order'))
185          setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
186          node_free(f)
187          return
188       end
189    elseif total <= res[0] then -- 和文処理グルー以外で足りる
190       for _,v in pairs(priority_table) do clear_stretch(p, v, res.name) end
191       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
192       setfield(f, 'head', nil)
193       setfield(p, 'glue_set', getfield(f, 'glue_set'))
194       setfield(p, 'glue_order', getfield(f, 'glue_order'))
195       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
196       node_free(f)
197    else
198       total = total - res[0]
199       for i = 1, #priority_table do
200          local v = priority_table[i]
201          if total <= res[v] then
202             for j = i+1,#priority_table do
203                clear_stretch(p, priority_table[j], res.name)
204             end
205             set_stretch(p, total, res[v], v, res.name); break
206          end
207          total = total - res[v]
208       end
209       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
210       setfield(f, 'head', nil)
211       setfield(p, 'glue_set', getfield(f, 'glue_set'))
212       setfield(p, 'glue_order', getfield(f, 'glue_order'))
213       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
214       node_free(f)
215    end
216 end
217
218
219 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
220 local function adjust_width(head)
221    if not head then return head end
222    local line = 1
223    for p in node_traverse_id(id_hlist, to_direct(head)) do
224       line = line + 1
225       local res, total = get_total_stretched(p, line)
226         -- this is the same table as the table which is def'd in l. 92
227       if res and res.glue_set<1 then
228          total = round(total * res.glue_set)
229          aw_step2(p, res, total, aw_step1(p, res, total))
230       end
231    end
232    return to_node(head)
233 end
234
235 do
236    luatexja.adjust = luatexja.adjust or {}
237    local is_reg = false
238    function luatexja.adjust.enable_cb()
239       if not is_reg then
240          luatexbase.add_to_callback('post_linebreak_filter',
241                                     adjust_width, 'Adjust width', 100)
242          is_reg = true
243       end
244    end
245    function luatexja.adjust.disable_cb()
246       if is_reg then
247          luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
248          is_reg = false
249       end
250    end
251 end
252
253 luatexja.unary_pars.adjust = function(t)
254    return is_reg and 1 or 0
255 end