OSDN Git Service

ltj-adjust.lua: add 'end_natural_inhibit' key in JFM
[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 total_stsh = {{},{}}
73 local total_st, total_sh = total_stsh[1], total_stsh[2]
74 local function get_total_stretched(p, line)
75 -- return value: <補正値(sp)>
76    local go, gf, gs
77      = getfield(p, 'glue_order'), getfield(p, 'glue_set'), getfield(p, 'glue_sign')
78    for i,_ in pairs(total_st) do total_st[i]=nil; total_sh[i]=nil end
79    for i=1,#priority_table do 
80       total_st[priority_table[i]]=0; total_sh[priority_table[i]]=0; 
81    end
82    for i=0,4 do total_st[i*65536]=0; total_sh[i*65536]=0 end
83    total_st[-1]=0; total_sh[-1]=0;
84    for q in node_traverse_id(id_glue, getlist(p)) do
85        local a = getfield(q, 'stretch_order')
86       if a>0 then a=a*65536 else 
87          total_st[0] = total_st[0]+getfield(q, 'stretch')
88          a = get_attr_icflag(q)
89          if a == KANJI_SKIP_JFM  then a = KANJI_SKIP
90          elseif a == XKANJI_SKIP_JFM  then a = XKANJI_SKIP
91          elseif type(total_st[a])~='number' then a = -1 end
92       end
93       total_st[a] = total_st[a]+getfield(q, 'stretch')
94       local a = getfield(q, 'shrink_order')
95       if a>0 then a=a*65536 else 
96          total_sh[0] = total_sh[0]+getfield(q, 'shrink')
97          a = get_attr_icflag(q)
98          if a == KANJI_SKIP_JFM  then a = KANJI_SKIP
99          elseif a == XKANJI_SKIP_JFM  then a = XKANJI_SKIP
100          elseif type(total_sh[a])~='number' then a = -1 end
101       end
102       total_sh[a] = total_sh[a]+getfield(q, 'shrink')
103    end
104    for i=4,0,-1 do if total_st[i*65536]~=0 then total_st.order=i; break end; end
105    for i=4,0,-1 do if total_sh[i*65536]~=0 then total_sh.order=i; break end; end
106    print('gf', (gs==0) and ' ' or (gs==1 and '+' or '-') .. gf )
107    print('**STRETCH')
108    for i,v in pairs(total_st) do print(i, v); end
109    print('**shrink')
110    for i,v in pairs(total_sh) do print(i, v); end
111    print('****** END ******')
112    if gs==0 then
113       return 0, gf
114    else 
115       return round((3-2*gs)*total_stsh[gs][go*65536]*gf), gf
116    end
117 end
118
119 local function clear_stretch(p, ic, name)
120    print('clear', ic)
121    for q in node_traverse_id(id_glue, getlist(p)) do
122       local f = get_attr_icflag(q)
123       if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
124            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
125          setfield(q, name..'_order', 0)
126          setfield(q, name, 0)
127       end
128    end
129 end
130
131 local function set_stretch(p, after, before, ic, name)
132    if before > 0 then
133       local ratio = after/before
134       for q in node_traverse_id(id_glue, getlist(p)) do
135          local f = get_attr_icflag(q)
136          if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
137            or ((ic ==XKANJI_SKIP) and (f == XKANJI_SKIP_JFM)) then
138             if getfield(q, name..'_order')==0 then
139                setfield(q, name, getfield(q, name)*ratio)
140             end
141          end
142       end
143    end
144 end
145
146 -- step 1: 行末に kern を挿入(句読点,中点用)
147 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
148 local function aw_step1(p, total, ntr)
149    local head = getlist(p)
150    local x = node_tail(head); if not x then return total, false end
151    -- x: \rightskip
152    x = node_prev(x); if not x then return total, false end
153    local xi, xc = getid(x)
154    if xi == id_glue and getsubtype(x) == 15 then
155       -- 段落最終行のときは,\penalty10000 \parfillskip が入るので,
156       -- その前の node が本来の末尾文字となる
157       x = node_prev(node_prev(x)); xi = getid(x)
158    end
159    if xi == id_glyph and getfield(x, 'lang')==lang_ja then
160       -- 和文文字
161       xc = x
162    elseif xi == id_hlist and get_attr_icflag(x) == PACKED then
163       -- packed JAchar
164       xc = ltjd_glyph_from_packed(x)
165       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
166    else
167      return total, false-- それ以外は対象外.
168    end
169    local xkst = ltjf_font_metric_table[getfont(xc)]
170      .char_type[has_attr(xc, attr_jchar_class) or 0]['end_stretch'] or 0
171    local xksh = ltjf_font_metric_table[getfont(xc)]
172      .char_type[has_attr(xc, attr_jchar_class) or 0]['end_shrink'] or 0
173    local xkni = ltjf_font_metric_table[getfont(xc)]
174      .char_type[has_attr(xc, attr_jchar_class) or 0]['end_natural_inhibit']
175
176    print(total, xkst, xksh, ntr)
177    if total>=xkst and xkst>0 then
178       local kn = node_new(id_kern)
179       setfield(kn, 'kern', xkst); set_attr(kn, attr_icflag, FROM_JFM)
180       insert_after(head, x, kn)
181       return total - xkst, true
182    elseif total<=-xksh and xksh<0 then
183       local kn = node_new(id_kern)
184       setfield(kn, 'kern', -xksh); set_attr(kn, attr_icflag, FROM_JFM)
185       insert_after(head, x, kn)
186       return total + xksh, true
187    else --    
188       local str = -(total-xkst)/total_sh[65536*total_sh.order] -- end_stretch を入れたときの glue_set (shrink)
189       local shr = (xksh+total)/total_st[65536*total_st.order] -- end_shrink を入れたときの glue_set (stretch)
190       print(xkni, str, ntr, shr)
191       if xkni then
192          if str<shr then 
193             local kn = node_new(id_kern)
194             setfield(kn, 'kern', xkst); set_attr(kn, attr_icflag, FROM_JFM)
195             insert_after(head, x, kn)
196             return total- xkst, true
197          else
198             local kn = node_new(id_kern)
199             setfield(kn, 'kern', -xksh); set_attr(kn, attr_icflag, FROM_JFM)
200             insert_after(head, x, kn)
201             return total + xksh, true  
202          end
203       else
204          return total, false   
205       end
206    end
207 end
208
209 -- step 2: 行中の glue を変える
210 local function aw_step2(p, total, added_flag)
211    local name = (total>0) and 'stretch' or 'shrink'
212    local res = total_stsh[(total>0) and 1 or 2]
213    print('STEP2', total)
214    if (total == 0) or res.order>0 then 
215       -- もともと伸縮の必要なしか,残りの伸縮量は無限大
216       if added_flag then -- 行末に kern 追加したので,それによる補正
217          local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
218          setfield(f, 'head', nil)
219          setfield(p, 'glue_set', getfield(f, 'glue_set'))
220          setfield(p, 'glue_order', getfield(f, 'glue_order'))
221          setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
222          node_free(f)
223          return
224       end
225    end
226    total = math.abs(total)
227    if total <= res[-1] then -- 和文処理グルー以外で足りる
228       for _,v in pairs(priority_table) do clear_stretch(p, v, name) end
229       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
230       setfield(f, 'head', nil)
231       setfield(p, 'glue_set', getfield(f, 'glue_set'))
232       setfield(p, 'glue_order', getfield(f, 'glue_order'))
233       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
234       node_free(f)
235    else
236       total = total - res[-1]; print('のこり', total)
237       for i = 1, #priority_table do
238          local v = priority_table[i]
239          print('total vs v', total, res[v], v)
240          if total <= res[v] then
241             for j = i+1,#priority_table do
242                clear_stretch(p, priority_table[j], name)
243             end
244             set_stretch(p, total, res[v], v, name); break
245          end
246          total = total - res[v]
247       end
248       local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
249       setfield(f, 'head', nil)
250       setfield(p, 'glue_set', getfield(f, 'glue_set'))
251       setfield(p, 'glue_order', getfield(f, 'glue_order'))
252       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
253       node_free(f)
254    end
255 end
256
257
258 local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
259 local function adjust_width(head)
260    if not head then return head end
261    local line = 1
262    for p in node_traverse_id(id_hlist, to_direct(head)) do
263       line = line + 1
264       aw_step2(p, aw_step1(p, get_total_stretched(p, line)))
265    end
266    return to_node(head)
267 end
268
269 do
270    luatexja.adjust = luatexja.adjust or {}
271    local is_reg = false
272    function luatexja.adjust.enable_cb()
273       if not is_reg then
274          luatexbase.add_to_callback('post_linebreak_filter',
275                                     adjust_width, 'Adjust width', 100)
276          is_reg = true
277       end
278    end
279    function luatexja.adjust.disable_cb()
280       if is_reg then
281          luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
282          is_reg = false
283       end
284    end
285 end
286
287 luatexja.unary_pars.adjust = function(t)
288    return is_reg and 1 or 0
289 end