OSDN Git Service

optimized yjabaselineshift.
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Thu, 19 Jul 2012 21:17:30 +0000 (06:17 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Thu, 19 Jul 2012 21:17:30 +0000 (06:17 +0900)
src/ltj-jfmglue.lua
src/ltj-math.lua
src/ltj-pretreat.lua
src/ltj-setwidth.lua

index 4d9c6ca..93e6f41 100644 (file)
@@ -3,23 +3,27 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.jfmglue',
-  date = '2012/04/25',
-  version = '0.4',
+  date = '2012/07/19',
+  version = '0.5',
   description = 'Insertion process of JFM glues and kanjiskip',
 })
 module('luatexja.jfmglue', package.seeall)
 local err, warn, info, log = luatexbase.errwarinf(_NAME)
 local node = node
 
-luatexja.load_module('base');      local ltjb = luatexja.base
 luatexja.load_module('stack');     local ltjs = luatexja.stack
 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
 luatexja.load_module('pretreat');  local ltjp = luatexja.pretreat
 
 local has_attr = node.has_attribute
 local set_attr = node.set_attribute
+local insert_before = node.insert_before
+local node_next = node.next
 local round = tex.round
 local uniq_id = 0 -- unique id 
+local ltjs_get_penalty_table  = ltjs.get_penalty_table
+local ltjf_font_metric_table = ltjf.font_metric_table
+local ltjf_find_char_class = ltjf.find_char_class
 
 local id_glyph = node.id('glyph')
 local id_hlist = node.id('hlist')
@@ -175,9 +179,9 @@ local function check_box(box_ptr, box_end)
       local pid = p.id
       if pid==id_kern then
         if p.subtype==2 then
-           p = node.next(node.next(node.next(p))); pid = p.id
+           p = node_next(node_next(node_next(p))); pid = p.id
         elseif has_attr(p, attr_icflag)==IC_PROCESSED then
-           p = node.next(p); pid = p.id
+           p = node_next(p); pid = p.id
         end
       end
       if pid==id_glyph then
@@ -185,7 +189,7 @@ local function check_box(box_ptr, box_end)
            if find_first_char then 
               first_char = p; find_first_char = false
            end
-           last_char = p; found_visible_node = true; p=node.next(p)
+           last_char = p; found_visible_node = true; p=node_next(p)
            if (not p) or p==box_end then return found_visible_node end
         until p.id~=id_glyph
         pid = p.id
@@ -216,7 +220,7 @@ local function check_box(box_ptr, box_end)
            last_char = nil
         end
       end
-      p = node.next(p)
+      p = node_next(p)
    end
    return found_visible_node
 end 
@@ -259,7 +263,7 @@ local function check_next_ickern()
    if lp.id == id_kern and has_attr(lp, attr_icflag)==ITALIC then
       set_attr(lp, attr_icflag, IC_PROCESSED) 
       set_attr(lp, attr_uniqid, uniq_id) 
-      Np.last = lp; lp = node.next(lp)
+      Np.last = lp; lp = node_next(lp)
    else Np.last = Np.nuc end
 end
 
@@ -270,7 +274,7 @@ local function calc_np_pbox()
    while lp~=last and lpa>=PACKED and lpa~=BOXBDD
       and has_attr(lp, attr_uniqid) == uid do
       Np.nuc = lp; set_attr(lp, attr_uniqid, uniq_id) 
-      lp = node.next(lp); lpa = has_attr(lp, attr_icflag) or 0
+      lp = node_next(lp); lpa = has_attr(lp, attr_icflag) or 0
    end
    return check_next_ickern()
 end
@@ -278,16 +282,11 @@ end
 
 local calc_np_auxtable = {
    [id_glyph] = function() 
-                  Np.first = Np.first or lp
-                  if lp.font == has_attr(lp, attr_curjfnt) then 
-                     Np.id = id_jglyph 
-                  else 
-                     Np.id = id_glyph 
-                  end
-                  Np.nuc = lp; 
+                  Np.first, Np.nuc = (Np.first or lp), lp;
+                  Np.id = (lp.font==has_attr(lp, attr_curjfnt)) and id_jglyph or id_glyph
                   set_attr(lp, attr_uniqid, uniq_id) 
                   --set_attr_icflag_processed(lp) treated in ltj-setwidth.lua
-                  lp = node.next(lp); check_next_ickern(); return true
+                  lp = node_next(lp); check_next_ickern(); return true
                end,
    [id_hlist] = function() 
                   Np.first = Np.first or lp; Np.last = lp; Np.nuc = lp; 
@@ -297,45 +296,45 @@ local calc_np_auxtable = {
                   else 
                      Np.id = id_hlist 
                   end
-                  lp = node.next(lp); return true
+                  lp = node_next(lp); return true
                end,
    [id_vlist] = function()
                   Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
                   Np.id = id_box_like; set_attr_icflag_processed(lp); 
-                  lp = node.next(lp); return true
+                  lp = node_next(lp); return true
                end,
    [id_rule] = function()
                  Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
                  Np.id = id_box_like; set_attr_icflag_processed(lp); 
-                 lp = node.next(lp); return true
+                 lp = node_next(lp); return true
               end,
    [id_ins] = function() 
-                set_attr_icflag_processed(lp); lp = node.next(lp)
+                set_attr_icflag_processed(lp); lp = node_next(lp)
                 return false
              end,
    [id_mark] = function() 
-                 set_attr_icflag_processed(lp); lp = node.next(lp)
+                 set_attr_icflag_processed(lp); lp = node_next(lp)
                  return false
               end,
    [id_adjust] = function() 
-                   set_attr_icflag_processed(lp); lp = node.next(lp)
+                   set_attr_icflag_processed(lp); lp = node_next(lp)
                    return false
                 end,
    [id_disc] = function()
                  Np.first = Np.first or lp; 
                  Np.nuc = lp; set_attr_icflag_processed(lp); 
-                 Np.last = lp; Np.id = id_disc; lp = node.next(lp); return true
+                 Np.last = lp; Np.id = id_disc; lp = node_next(lp); return true
               end,
    [id_whatsit] = function() 
                  if lp.subtype==sid_user then
                     if lp.user_id==30111 then
-                       local lq = node.next(lp)
+                       local lq = node_next(lp)
                        head = node.remove(head, lp); node.free(lp); lp = lq; ihb_flag = true
                     else
                        set_attr_icflag_processed(lp)
                        luatexbase.call_callback("luatexja.jfmglue.whatsit_getinfo"
                                                 , Np, lp, Nq, box_stack_level)
-                       lp = node.next(lp)
+                       lp = node_next(lp)
                        if Np.nuc then 
                           Np.id = id_pbox_w; Np.first = Np.nuc; Np.last = Np.nuc; return true
                        end
@@ -347,65 +346,65 @@ local calc_np_auxtable = {
                     elseif lp.subtype == sid_end_link or lp.subtype == sid_end_thread then
                        Nq.last = lp; Np.first = nil
                     end
-                    set_attr_icflag_processed(lp); lp = node.next(lp)
+                    set_attr_icflag_processed(lp); lp = node_next(lp)
                  end
                  return false
                  end,
    [id_math] = function()
                  Np.first = Np.first or lp; Np.nuc = lp; 
-                 set_attr_icflag_processed(lp); lp  = node.next(lp) 
+                 set_attr_icflag_processed(lp); lp  = node_next(lp) 
                  while lp.id~=id_math do 
-                    set_attr_icflag_processed(lp); lp  = node.next(lp) 
+                    set_attr_icflag_processed(lp); lp  = node_next(lp) 
                  end
                  set_attr_icflag_processed(lp); 
-                 Np.last = lp; Np.id = id_math; lp = node.next(lp); 
+                 Np.last = lp; Np.id = id_math; lp = node_next(lp); 
                  return true
               end,
    [id_glue] = function()
                  Np.first = Np.first or lp; Np.nuc = lp; set_attr_icflag_processed(lp); 
-                 Np.last = lp; Np.id = id_glue; lp = node.next(lp); return true
+                 Np.last = lp; Np.id = id_glue; lp = node_next(lp); return true
               end,
    [id_kern] = function() 
                  Np.first = Np.first or lp
                  if lp.subtype==2 then
-                    set_attr_icflag_processed(lp); lp = node.next(lp)
-                    set_attr_icflag_processed(lp); lp = node.next(lp)
-                    set_attr_icflag_processed(lp); lp = node.next(lp)
+                    set_attr_icflag_processed(lp); lp = node_next(lp)
+                    set_attr_icflag_processed(lp); lp = node_next(lp)
+                    set_attr_icflag_processed(lp); lp = node_next(lp)
                     set_attr_icflag_processed(lp); Np.nuc = lp
                     if lp.font == has_attr(lp, attr_curjfnt) then 
                        Np.id = id_jglyph 
                     else
                        Np.id = id_glyph 
                     end
-                    lp = node.next(lp); check_next_ickern(); 
+                    lp = node_next(lp); check_next_ickern(); 
                  else
                     Np.id = id_kern; set_attr_icflag_processed(lp);
-                    Np.last = lp; lp = node.next(lp)
+                    Np.last = lp; lp = node_next(lp)
                  end
                  return true
               end,
    [id_penalty] = function()
                     Bp[#Bp+1] = lp; set_attr_icflag_processed(lp); 
-                    lp = node.next(lp); return false
+                    lp = node_next(lp); return false
                  end,
    [13] = function()
                  Np.first = Np.first or lp; Np.nuc = lp; Np.last = lp;
                  Np.id = id_box_like; set_attr_icflag_processed(lp); 
-                 lp = node.next(lp); return true
+                 lp = node_next(lp); return true
               end,
 }
 
 local function calc_np()
-   -- We assume lp = node.next(Np.last)
-   local Nr = Nq
-   for k in pairs(Nr) do Nr[k] = nil end; Nq = Np; Np = Nr
+   local last, k = last, nil
+   -- We assume lp = node_next(Np.last)
+   Np, Nq, ihb_flag = Nq, Np, false
+   for k in pairs(Np) do Np[k] = nil end;
    for k in pairs(Bp) do Bp[k] = nil end
-   ihb_flag = false 
    while lp ~= last do
       local lpa = has_attr(lp, attr_icflag) or 0
       if lpa>=PACKED then
          if lpa == BOXBDD then
-            local lq = node.next(lp)
+            local lq = node_next(lp)
             head = node.remove(head, lp); node.free(lp); lp = lq
          else calc_np_pbox(); return 
          end -- id_pbox
@@ -424,16 +423,16 @@ end
 
 -- 和文文字のデータを取得
 function set_np_xspc_jachar(Nx, x)
-   local m = ltjf.font_metric_table[x.font]
+   local m = ltjf_font_metric_table[x.font]
    local c = has_attr(x, attr_orig_char) or 0
-   local cls = ltjf.find_char_class(x.char, m) or 0
-   if cls==0 and c ~= x.char then cls = ltjf.find_char_class(-c, m) end
+   local cls = ltjf_find_char_class(x.char, m) or 0
+   if cls==0 and c ~= x.char then cls = ltjf_find_char_class(-c, m) end
    Nx.class = cls; set_attr(x, attr_jchar_class, cls)
    Nx.lend = m.size_cache.char_type[cls].kern[fast_find_char_class('lineend', m)] or 0
    Nx.met, Nx.var, Nx.char = m, m.var, c
-   Nx.pre = ltjs.get_penalty_table('pre', c, 0, box_stack_level)
-   Nx.post = ltjs.get_penalty_table('post', c, 0, box_stack_level)
-   local y = ltjs.get_penalty_table('xsp', c, 3, box_stack_level)
+   Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
+   Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
+   local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
    Nx.xspc_before, Nx.xspc_after = (y%2==1), (y>=2)
    Nx.auto_kspc, Nx.auto_xspc = (has_attr(x, attr_autospc)==1), (has_attr(x, attr_autoxspc)==1)
 end
@@ -452,14 +451,14 @@ function set_np_xspc_alchar(Nx, c,x, lig)
            x = node.tail(x.components); c = x.char
         end
       end
-      Nx.pre = ltjs.get_penalty_table('pre', c, 0, box_stack_level)
-      Nx.post = ltjs.get_penalty_table('post', c, 0, box_stack_level)
+      Nx.pre = ltjs_get_penalty_table('pre', c, 0, box_stack_level)
+      Nx.post = ltjs_get_penalty_table('post', c, 0, box_stack_level)
       Nx.char = 'jcharbdd'
    else
       Nx.pre = 0; Nx.post = 0; Nx.char = -1
    end
    Nx.met = nil
-   local y = ltjs.get_penalty_table('xsp', c, 3, box_stack_level)
+   local y = ltjs_get_penalty_table('xsp', c, 3, box_stack_level)
    Nx.xspc_before = (y%2==1)
    Nx.xspc_after  = (y>=2)
    Nx.auto_xspc = (has_attr(x, attr_autoxspc)==1)
@@ -471,7 +470,7 @@ local function extract_np()
    if i ==  id_jglyph then return set_np_xspc_jachar(Np, x)
    elseif i == id_glyph then return set_np_xspc_alchar(Np, x.char, x, ligature_head)
    elseif i == id_hlist then Np.last_char = check_box_high(Np, x.head, nil)
-   elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node.next(Np.last))
+   elseif i == id_pbox then Np.last_char = check_box_high(Np, Np.first, node_next(Np.last))
    elseif i == id_disc then Np.last_char = check_box_high(Np, x.replace, nil)
    elseif i == id_math then return set_np_xspc_alchar(Np, -1, x)
    end
@@ -525,7 +524,7 @@ local function handle_penalty_normal(post, pre, g)
         local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
         if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
         p.penalty = a
-        head = node.insert_before(head, Np.first, p)
+        head = insert_before(head, Np.first, p)
         Bp[#Bp+1]=p; 
      set_attr(p, attr_icflag, KINSOKU)
      set_attr(p, attr_uniqid, uniq_id) 
@@ -541,7 +540,7 @@ local function handle_penalty_always(post, pre, g)
         local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
         if a<-10000 then a = -10000 elseif a>10000 then a = 10000 end
         p.penalty = a
-        head = node.insert_before(head, Np.first, p)
+        head = insert_before(head, Np.first, p)
         Bp[#Bp+1]=p
      set_attr(p, attr_icflag, KINSOKU)
      set_attr(p, attr_uniqid, uniq_id) 
@@ -555,7 +554,7 @@ local function handle_penalty_suppress(post, pre, g)
    if #Bp == 0 then
       if g and g.id==id_glue then
         local p = node.new(id_penalty); copy_attr(p, Nq.nuc)
-        p.penalty = 10000; head = node.insert_before(head, Np.first, p)
+        p.penalty = 10000; head = insert_before(head, Np.first, p)
         Bp[#Bp+1]=p
      set_attr(p, attr_icflag, KINSOKU)
      set_attr(p, attr_uniqid, uniq_id) 
@@ -596,7 +595,7 @@ local function real_insert(w, g)
       head = node.insert_after(head, Nq.last, h)
    end
    if g then
-      head = node.insert_before(head, Np.first, g)
+      head = insert_before(head, Np.first, g)
       Np.first = g
    end
 end
@@ -760,7 +759,7 @@ local function handle_np_jachar()
       real_insert(0, g)
    end
    -- \jcharwidowpenalty 挿入予定箇所更新
-   if mode and ltjs.get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
+   if mode and ltjs_get_penalty_table('kcat', Np.char, 0, box_stack_level)%2~=1 then
       widow_Np.first = Np.first; 
       local Bpr = widow_Bp; widow_Bp = Bp; Bp = Bpr
    end
@@ -832,7 +831,7 @@ local function handle_list_tail()
       Bp = widow_Bp; Np = widow_Np; Nq.lend = 0
       if Np.first then
         handle_penalty_normal(0,
-                              ltjs.get_penalty_table('jwp', 0, 0, box_stack_level))
+                              ltjs_get_penalty_table('jwp', 0, 0, box_stack_level))
       end
    else
       -- the current list is the contents of a hbox
@@ -862,7 +861,7 @@ local function handle_list_head()
               local h = node.new(id_penalty); copy_attr(h, Np.nuc)
               h.penalty = 10000; set_attr(h, attr_icflag, BOXBDD)
            end
-           head = node.insert_before(head, Np.first, g)
+           head = insert_before(head, Np.first, g)
         end
       end
    end
@@ -893,7 +892,7 @@ local function init_var()
       while lp and ((lp.id==id_whatsit and lp.subtype~=sid_user) 
                 or ((lp.id==id_hlist) and (lp.subtype==3))) do
         if (lp.id==id_hlist) and (lp.subtype==3) then par_indented = true end
-        lp=node.next(lp) end
+        lp=node_next(lp) end
      last=node.tail(head)
    else 
       -- the current list is the contents of a hbox:
@@ -908,7 +907,7 @@ local function cleanup()
    tex.setattribute('global', attr_icflag, 0)
    node.free(kanji_skip); node.free(xkanji_skip)
    if mode then
-      local h = node.next(head)
+      local h = node_next(head)
       if h.id == id_penalty and h.penalty == 10000 then
         h = h.next
         if h.id == id_glue and h.subtype == 15 and not h.next then
index 784a55f..fd77dc4 100644 (file)
@@ -3,8 +3,8 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.math',
-  date = '2011/08/14',
-  version = '0.1',
+  date = '2012/07/19',
+  version = '0.2',
   description = 'Handling routines for Japanese characters in math mode',
 })
 module('luatexja.math', package.seeall)
@@ -20,6 +20,7 @@ local node_next = node.next
 local node_free = node.free
 local has_attr = node.has_attribute
 local set_attr = node.set_attribute
+local tex_getcount = tex.getcount
 
 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
 local attr_icflag = luatexbase.attributes['ltj@icflag']
@@ -102,12 +103,12 @@ function (p, sty)
         if sty == 0 then mode = 'mjtext'
         elseif sty == 1 then mode = 'mjscr'
         end
-        local f = ltjs.get_penalty_table(mode, fam, -1, tex.getcount('ltj@@stack'))
+        local f = ltjs.get_penalty_table(mode, fam, -1, tex_getcount('ltj@@stack'))
         if f ~= -1 then
            local q = node_new(id_sub_box)
            local r = node_new(id_glyph); r.next = nil
            r.char = p.char; r.font = f; r.subtype = 256
-           set_attr(r, attr_yablshift, 0)
+           set_attr(r, attr_ykblshift, 0)
            set_attr(r, attr_icflag, PROCESSED)
            local met = ltjf_font_metric_table[f]
            ltjw.head = r; ltjw.capsule_glyph(r, tex.mathdir , true, met, ltjf_find_char_class(p.char, met));
index 7bc41b2..d2b22fe 100644 (file)
@@ -3,8 +3,8 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.pretreat',
-  date = '2011/06/27',
-  version = '0.1',
+  date = '2012/07/19',
+  version = '0.2',
   description = '',
 })
 module('luatexja.pretreat', package.seeall)
@@ -22,6 +22,7 @@ local node_type = node.type
 local node_remove = node.remove
 local node_next = node.next
 local node_free = node.free
+local tex_getcount = tex.getcount
 
 local id_glyph = node.id('glyph')
 local id_math = node.id('math')
@@ -30,8 +31,6 @@ local sid_user = node.subtype('user_defined')
 
 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
 local attr_icflag = luatexbase.attributes['ltj@icflag']
-local attr_yablshift = luatexbase.attributes['ltj@yablshift']
-local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
 
 local ltjf_font_metric_table = ltjf.font_metric_table
 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
@@ -44,14 +43,13 @@ box_stack_level = 0
 -- This is used in jfmglue.lua.
 
 local function suppress_hyphenate_ja(head)
-   local non_math = true
+   local non_math, p = true, nil
    for p in node_traverse(head) do
       if p.id == id_glyph and non_math then
         if (has_attr(p, attr_icflag) or 0)<=0 and ltjc_is_ucs_in_japanese_char(p) then
            p.font = has_attr(p, attr_curjfnt) or p.font
+           p.subtype = floor(p.subtype*0.5)*2
            set_attr(p, attr_orig_char, p.char)
-           set_attr(p, attr_yablshift, has_attr(p, attr_ykblshift) or 0)
-           p.subtype = floor(p.subtype/2)*2
         end
       elseif p.id == id_math then 
         non_math = (p.subtype ~= 0)
@@ -73,7 +71,7 @@ function set_box_stack_level(head, mode)
         head, p = node_remove(head, g); node_free(g); break
       end
    end
-   box_stack_level = tex.getcount('ltj@@stack') + (box_set and 1 or 0)
+   box_stack_level = tex_getcount('ltj@@stack') + (box_set and 1 or 0)
    return head
 end
 
index d3e7c2e..615975e 100644 (file)
@@ -3,8 +3,8 @@
 --
 luatexbase.provides_module({
   name = 'luatexja.setwidth',
-  date = '2011/06/28',
-  version = '0.1',
+  date = '2012/07/19',
+  version = '0.2',
   description = '',
 })
 module('luatexja.setwidth', package.seeall)
@@ -33,6 +33,7 @@ local id_math = node.id('math')
 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
+local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
 local attr_icflag = luatexbase.attributes['ltj@icflag']
 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
 
@@ -59,6 +60,7 @@ local fshift =  { down = 0, left = 0}
 function capsule_glyph(p, dir, mode, met, class)
    local char_data = met.size_cache.char_type[class]
    if not char_data then return node_next(p) end
+   local fwidth
    if char_data.width ~= 'prop' then
       fwidth = char_data.width
    else fwidth = p.width end
@@ -66,7 +68,7 @@ function capsule_glyph(p, dir, mode, met, class)
    fshift.down = char_data.down; fshift.left = char_data.left
    fshift = luatexbase.call_callback("luatexja.set_width", fshift, met, class)
    if (mode or p.width ~= fwidth or p.height ~= fheight or p.depth ~= fdepth) then
-      local y_shift, total = - p.yoffset + (has_attr(p,attr_yablshift) or 0), fwidth - p.width
+      local y_shift, total = - p.yoffset + (has_attr(p,attr_ykblshift) or 0), fwidth - p.width
       local q; head, q = node.remove(head, p)
       p.yoffset, p.next = -fshift.down, nil
       if total ~= 0 and char_data.align~='left' then