OSDN Git Service

luatexja-adjust.sty: support \ltjenableadjust[...]
[luatex-ja/luatexja.git] / src / ltj-adjust.lua
index 7f5562f..49a4334 100644 (file)
@@ -103,12 +103,6 @@ local function get_total_stretched(p, line)
    end
    for i=4,0,-1 do if total_st[i*65536]~=0 then total_st.order=i; break end; end
    for i=4,0,-1 do if total_sh[i*65536]~=0 then total_sh.order=i; break end; end
-   print('gf', (gs==0) and ' ' or (gs==1 and '+' or '-') .. gf )
-   print('**STRETCH')
-   for i,v in pairs(total_st) do print(i, v); end
-   print('**shrink')
-   for i,v in pairs(total_sh) do print(i, v); end
-   print('****** END ******')
    if gs==0 then
       return 0, gf
    else 
@@ -117,7 +111,6 @@ local function get_total_stretched(p, line)
 end
 
 local function clear_stretch(p, ic, name)
-   print('clear', ic)
    for q in node_traverse_id(id_glue, getlist(p)) do
       local f = get_attr_icflag(q)
       if (f == ic) or ((ic ==KANJI_SKIP) and (f == KANJI_SKIP_JFM))
@@ -144,6 +137,7 @@ local function set_stretch(p, after, before, ic, name)
 end
 
 -- step 1: 行末に kern を挿入(句読点,中点用)
+local abs = math.abs
 local ltjd_glyph_from_packed = ltjd.glyph_from_packed
 local function aw_step1(p, total, ntr)
    local head = getlist(p)
@@ -164,54 +158,57 @@ local function aw_step1(p, total, ntr)
       xc = ltjd_glyph_from_packed(x)
       while getid(xc) == id_whatsit do xc = node_next(xc) end -- これはなんのために?
    else
-     return total, false-- それ以外は対象外.
+      return total, false-- それ以外は対象外.
    end
-   local xkst = ltjf_font_metric_table[getfont(xc)]
-     .char_type[has_attr(xc, attr_jchar_class) or 0]['end_stretch'] or 0
-   local xksh = ltjf_font_metric_table[getfont(xc)]
-     .char_type[has_attr(xc, attr_jchar_class) or 0]['end_shrink'] or 0
-   local xkni = ltjf_font_metric_table[getfont(xc)]
-     .char_type[has_attr(xc, attr_jchar_class) or 0]['end_natural_inhibit']
-
-   print(total, xkst, xksh, ntr)
-   if total>=xkst and xkst>0 then
-      local kn = node_new(id_kern)
-      setfield(kn, 'kern', xkst); set_attr(kn, attr_icflag, FROM_JFM)
-      insert_after(head, x, kn)
-      return total - xkst, true
-   elseif total<=-xksh and xksh<0 then
-      local kn = node_new(id_kern)
-      setfield(kn, 'kern', -xksh); set_attr(kn, attr_icflag, FROM_JFM)
-      insert_after(head, x, kn)
-      return total + xksh, true
-   else --    
-      local str = -(total-xkst)/total_sh[65536*total_sh.order] -- end_stretch を入れたときの glue_set (shrink)
-      local shr = (xksh+total)/total_st[65536*total_st.order] -- end_shrink を入れたときの glue_set (stretch)
-      print(xkni, str, ntr, shr)
-      if xkni then
-        if str<shr then 
-            local kn = node_new(id_kern)
-            setfield(kn, 'kern', xkst); set_attr(kn, attr_icflag, FROM_JFM)
-            insert_after(head, x, kn)
-           return total- xkst, true
-        else
-            local kn = node_new(id_kern)
-            setfield(kn, 'kern', -xksh); set_attr(kn, attr_icflag, FROM_JFM)
-            insert_after(head, x, kn)
-            return total + xksh, true  
-        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
+   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
-        return total, false   
+        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)
+   --print('min', eadt[eadt_ratio[1][1]], eadt_ratio[1][3])
+   if eadt[eadt_ratio[1][1]]~=0 then
+      local kn = node_new(id_kern)
+      setfield(kn, 'kern', eadt[eadt_ratio[1][1]]); set_attr(kn, attr_icflag, FROM_JFM)
+      insert_after(head, x, kn)
+      return eadt_ratio[1][3], true
+   else
+      return total, false
+   end
 end
 
 -- step 2: 行中の glue を変える
+local function aw_step2_dummy(p, _, added_flag)
+   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
+end
 local function aw_step2(p, total, added_flag)
    local name = (total>0) and 'stretch' or 'shrink'
    local res = total_stsh[(total>0) and 1 or 2]
-   print('STEP2', total)
-   if (total == 0) or res.order>0 then 
+   if total==0 or res.order > 0 then 
       -- もともと伸縮の必要なしか,残りの伸縮量は無限大
       if added_flag then -- 行末に kern 追加したので,それによる補正
         local f = node_hpack(getlist(p), getfield(p, 'width'), 'exactly')
@@ -233,10 +230,9 @@ local function aw_step2(p, total, added_flag)
       setfield(p, 'glue_sign', getfield(f, 'glue_sign'))
       node_free(f)
    else
-      total = total - res[-1]; print('のこり', total)
+      total = total - res[-1];
       for i = 1, #priority_table do
         local v = priority_table[i]
-        print('total vs v', total, res[v], v)
          if total <= res[v] then
             for j = i+1,#priority_table do
                clear_stretch(p, priority_table[j], name)
@@ -255,33 +251,36 @@ local function aw_step2(p, total, added_flag)
 end
 
 
-local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
-local function adjust_width(head)
-   if not head then return head end
-   local line = 1
-   for p in node_traverse_id(id_hlist, to_direct(head)) do
-      line = line + 1
-      aw_step2(p, aw_step1(p, get_total_stretched(p, line)))
-   end
-   return to_node(head)
-end
-
 do
-   luatexja.adjust = luatexja.adjust or {}
+   local myaw_atep1, myaw_step2
+   local dummy =  function(p,t,n) return t, false end
+   local ltjs_fast_get_stack_skip = ltjs.fast_get_stack_skip
+   local function adjust_width(head)
+      if not head then return head end
+      local line = 1
+      for p in node_traverse_id(id_hlist, to_direct(head)) do
+         line = line + 1
+         myaw_step2(p, myaw_step1(p, get_total_stretched(p, line)))
+      end
+      return to_node(head)
+   end
    local is_reg = false
-   function luatexja.adjust.enable_cb()
-      if not is_reg then
+   function enable_cb(status)
+      if status>0 and (not is_reg) then
         luatexbase.add_to_callback('post_linebreak_filter',
                                    adjust_width, 'Adjust width', 100)
         is_reg = true
-      end
-   end
-   function luatexja.adjust.disable_cb()
-      if is_reg then
+      elseif is_reg and status==0 then
         luatexbase.remove_from_callback('post_linebreak_filter', 'Adjust width')
         is_reg = false
       end
+      myaw_step1 = (status%2>0) and aw_step1 or dummy
+      myaw_step2 = (status>=2) and aw_step2 or aw_step2_dummy
+   end
+   function disable_cb() -- only for compatibility
+       enable_cs(0)
    end
+   luatexja.adjust = luatexja.adjust or {enable_cb=enable_cb, disable_cb=disable_cb}  
 end
 
 luatexja.unary_pars.adjust = function(t)