OSDN Git Service

Moved Lua and TeX codes in mfonttest.tex into the core of LuaTeX-ja.
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Mon, 30 Dec 2013 13:28:46 +0000 (22:28 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Mon, 30 Dec 2013 13:28:46 +0000 (22:28 +0900)
src/ltj-jfont.lua
src/luatexja-core.sty
src/luatexja.lua
src/patches/lltjfont.sty
test/mfonttest.pdf
test/mfonttest.tex

index 1b4d04d..74758ed 100644 (file)
@@ -122,49 +122,51 @@ function define_jfm(t)
    defjfm_res = t
 end
 
-local function mult_table(old,scale) -- modified from table.fastcopy
-    if old then
-       local new = { }
-       for k,v in next, old do
-         if type(v) == "table" then
-            new[k] = mult_table(v,scale)
-         elseif type(v) == "number" then
-            new[k] = round(v*scale)
-          else
-             new[k] = v
-         end
-       end
-       return new
-    else return nil end
-end
-
-local function update_jfm_cache(j,sz)
-   if metrics[j].size_cache[sz] then return end
-   local t = {}
-   metrics[j].size_cache[sz] = t
-   t.chars = metrics[j].chars
-   t.char_type = mult_table(metrics[j].char_type, sz)
-   for i,v in pairs(t.char_type) do
-      if type(i) == 'number' then -- char_type
-        for k,w in pairs(v.glue) do
-           local g, h = node.new(id_glue), node_new(id_glue_spec)
-            v.glue[k] = {g, (w[5] and w[5]/sz or 0)}
-            h.width, h.stretch, h.shrink = w[1], w[2], w[3]
-            h.stretch_order, h.shrink_order = 0, 0
-            g.subtype = 0; g.spec = h; set_attr(g, attr_icflag, FROM_JFM + 
-                                                (w[4] and w[4]/sz or 0)); 
+do
+   local function mult_table(old,scale) -- modified from table.fastcopy
+      if old then
+        local new = { }
+        for k,v in next, old do
+           if type(v) == "table" then
+              new[k] = mult_table(v,scale)
+           elseif type(v) == "number" then
+              new[k] = round(v*scale)
+           else
+              new[k] = v
+           end
         end
-        for k,w in pairs(v.kern) do
-           w[2] = w[2]/sz
+        return new
+      else return nil end
+   end
+   
+   function update_jfm_cache(j,sz)
+      if metrics[j].size_cache[sz] then return end
+      local t = {}
+      metrics[j].size_cache[sz] = t
+      t.chars = metrics[j].chars
+      t.char_type = mult_table(metrics[j].char_type, sz)
+      for i,v in pairs(t.char_type) do
+        if type(i) == 'number' then -- char_type
+           for k,w in pairs(v.glue) do
+              local g, h = node.new(id_glue), node_new(id_glue_spec)
+              v.glue[k] = {g, (w[5] and w[5]/sz or 0)}
+              h.width, h.stretch, h.shrink = w[1], w[2], w[3]
+              h.stretch_order, h.shrink_order = 0, 0
+              g.subtype = 0; g.spec = h; set_attr(g, attr_icflag, FROM_JFM + 
+                                                     (w[4] and w[4]/sz or 0)); 
+           end
+           for k,w in pairs(v.kern) do
+              w[2] = w[2]/sz
+           end
         end
       end
+      t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
+      t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
+      t.zw = round(metrics[j].zw*sz)
+      t.zh = round(metrics[j].zh*sz)
    end
-   t.kanjiskip = mult_table(metrics[j].kanjiskip, sz)
-   t.xkanjiskip = mult_table(metrics[j].xkanjiskip,sz)
-   t.zw = round(metrics[j].zw*sz)
-   t.zh = round(metrics[j].zh*sz)
 end
-
+local update_jfm_cache = update_jfm_cache
 luatexbase.create_callback("luatexja.find_char_class", "data", 
                           function (arg, fmtable, char)
                              return 0
@@ -177,123 +179,259 @@ function find_char_class(c,m)
       luatexbase.call_callback("luatexja.find_char_class", 0, m, c)
 end
 
-local function load_jfont_metric()
-   if jfm_file_name=='' then 
-      ltjb.package_error('luatexja',
-                        'no JFM specified',
-                        'To load and define a Japanese font, a JFM must be specified.'..
-                         "The JFM 'ujis' will be  used for now.")
-      jfm_file_name='ujis'
+
+------------------------------------------------------------------------
+-- LOADING JAPANESE FONTS
+------------------------------------------------------------------------
+
+do
+   local cstemp
+   local function load_jfont_metric()
+      if jfm_file_name=='' then 
+        ltjb.package_error('luatexja',
+                           'no JFM specified',
+                           'To load and define a Japanese font, a JFM must be specified.'..
+                           "The JFM 'ujis' will be  used for now.")
+        jfm_file_name='ujis'
+      end
+      for j,v in ipairs(metrics) do 
+        if v.name==jfm_file_name then return j end
+      end
+      luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
+      if defjfm_res then
+        defjfm_res.name = jfm_file_name
+        table.insert(metrics, defjfm_res)
+        return #metrics
+      else 
+        return nil
+      end
+   end
+
+-- EXT
+   function jfontdefX(g)
+      local t = token.get_next()
+      cstemp=token.csname_name(t)
+      if g then luatexja.is_global = '\\global' else luatexja.is_global = '' end
+      tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
+   end
+   
+   luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
+
+-- EXT
+   function jfontdefY() -- for horizontal font
+      local j = load_jfont_metric()
+      local fn = font.id(cstemp)
+      local f = getfont(fn)
+      if not j then 
+        ltjb.package_error('luatexja',
+                           "bad JFM `" .. jfm_file_name .. "'",
+                           'The JFM file you specified is not valid JFM file.\n'..
+                              'So defining Japanese font is cancelled.')
+        tex.sprint(cat_lp, luatexja.is_global .. '\\expandafter\\let\\csname ' ..cstemp 
+                      .. '\\endcsname=\\relax')
+        return 
+      end
+      update_jfm_cache(j, f.size)
+      local sz = metrics[j].size_cache[f.size]
+      local fmtable = { jfm = j, size = f.size, var = jfm_var, 
+                       zw = sz.zw, zh = sz.zh, 
+                       chars = sz.chars, char_type = sz.char_type,
+                       kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip, 
+      }
+      
+      fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
+      font_metric_table[fn]=fmtable
+      tex.sprint(cat_lp, luatexja.is_global .. '\\protected\\expandafter\\def\\csname ' 
+                   .. cstemp  .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
    end
-   for j,v in ipairs(metrics) do 
-      if v.name==jfm_file_name then return j end
+end
+
+do
+-- EXT: zw, zh
+   function load_zw()
+      local a = font_metric_table[tex.attribute[attr_curjfnt]]
+      tex.setdimen('ltj@zw', a and a.zw or 0)
    end
-   luatexja.load_lua('jfm-' .. jfm_file_name .. '.lua')
-   if defjfm_res then
-      defjfm_res.name = jfm_file_name
-      table.insert(metrics, defjfm_res)
-      return #metrics
-   else 
-      return nil
+   
+   function load_zh()
+      local a = font_metric_table[tex.attribute[attr_curjfnt]]
+      tex.setdimen('ltj@zh', a and a.zh or 0)
    end
 end
 
+do
+   -- extract jfm_file_name and jfm_var
+   local function extract_metric(name)
+      local basename=name
+      local tmp = utf.sub(basename, 1, 5)
+      jfm_file_name = ''; jfm_var = ''
+      if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
+        basename = utf.sub(basename, 6)
+      end
+      local p = utf.find(basename, ":")
+      if p then 
+        basename = utf.sub(basename, p+1)
+      else return 
+      end
+      -- now basename contains 'features' only.
+      p=1
+      while p do
+        local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
+        if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
+           jfm_file_name = utf.sub(basename, p+4, q-1)
+        elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
+           jfm_var = utf.sub(basename, p+7, q-1)
+        end
+        if utf.len(basename)+1==q then p = nil else p = q + 1 end
+      end
+      return
+   end
+   
+   -- replace fonts.define.read()
+   function font_callback(name, size, id, fallback)
+      extract_metric(name)
+      -- In the present imple., we don't remove "jfm=..." from name.
+      return fallback(name, size, id)
+   end
+end
 
 ------------------------------------------------------------------------
--- LOADING JAPANESE FONTS
+-- ALTERNATE FONTS
 ------------------------------------------------------------------------
-local cstemp
+alt_font_table = {}
+local alt_font_table = alt_font_table
+local attr_curaltfnt = {}
+local ucs_out = 0x110000
 
+------ for TeX interface
 -- EXT
-function jfontdefX(g)
-  local t = token.get_next()
-  cstemp=token.csname_name(t)
-  if g then luatexja.is_global = '\\global' else luatexja.is_global = '' end
-  tex.sprint(cat_lp, '\\expandafter\\font\\csname ' .. cstemp .. '\\endcsname')
+function set_alt_font(b,e,ind,bfnt)
+   -- ind: 新フォント, bfnt: 基底フォント
+  if b<0x80 or e>=ucs_out then
+      ltjb.package_warning('luatexja',
+                        'bad character range ([' .. b .. ',' .. e .. ']). ' ..
+                          'I take the intersection with [0x80, 0x10ffff].')
+   elseif b>e then
+      local j=b; e=b; b=j
+   end
+   if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
+   if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
+   local t = alt_font_table[bfnt]
+   for i=math.max(0x80,b),math.min(ucs_out-1,e) do
+      t[i]=ind
+   end
 end
 
-luatexbase.create_callback("luatexja.define_jfont", "data", function (ft, fn) return ft end)
-
 -- EXT
-function jfontdefY() -- for horizontal font
-   local j = load_jfont_metric()
-   local fn = font.id(cstemp)
-   local f = getfont(fn)
-   if not j then 
-      ltjb.package_error('luatexja',
-                        "bad JFM `" .. jfm_file_name .. "'",
-                        'The JFM file you specified is not valid JFM file.\n'..
-                           'So defining Japanese font is cancelled.')
-      tex.sprint(cat_lp, luatexja.is_global .. '\\expandafter\\let\\csname ' ..cstemp 
-             .. '\\endcsname=\\relax')
-     return 
+function clear_alt_font(bfnt)
+   if alt_font_table[bfnt] then 
+      local t = alt_font_table[bfnt]
+      for i,_ in pairs(t) do t[i]=nil; end
    end
-   update_jfm_cache(j, f.size)
-   local sz = metrics[j].size_cache[f.size]
-   local fmtable = { jfm = j, size = f.size, var = jfm_var, 
-                    zw = sz.zw, zh = sz.zh, 
-                    chars = sz.chars, char_type = sz.char_type,
-                    kanjiskip = sz.kanjiskip, xkanjiskip = sz.xkanjiskip, 
-                   }
-      
-   fmtable = luatexbase.call_callback("luatexja.define_jfont", fmtable, fn)
-   font_metric_table[fn]=fmtable
-   tex.sprint(cat_lp, luatexja.is_global .. '\\protected\\expandafter\\def\\csname ' 
-          .. cstemp  .. '\\endcsname{\\ltj@curjfnt=' .. fn .. '\\relax}')
 end
 
--- zw, zh
-function load_zw()
-   local a = font_metric_table[tex.attribute[attr_curjfnt]]
-   if a then
-      tex.setdimen('ltj@zw', a.zw)
-   else 
-      tex.setdimen('ltj@zw',0)
+------ callback
+function replace_altfont(head)
+   for p in node.traverse_id(id_glyph, head) do
+      local pf = p.font
+      if p.font == (has_attr(p, attr_curjfnt) or 0) then
+         if alt_font_table[pf] and alt_font_table[pf][p.char] then
+            local n = alt_font_table[pf][p.char]
+            if n then 
+               p.font = n; set_attr(p, attr_curjfnt, n)
+            end
+         end
+      end
    end
+   return head
 end
 
-function load_zh()
-   local a = font_metric_table[tex.attribute[attr_curjfnt]]
-   if a then
-      tex.setdimen('ltj@zh', a.zh)
-   else 
-      tex.setdimen('ltj@zh',0)
+------ for LaTeX interface
+
+local alt_font_table_latex = {}
+
+-- EXT
+function clear_alt_font_latex(bbase)
+   local t = alt_font_table_latex[bbase]
+   if t then
+      for j,v in pairs(t) do t[j] = nil end 
    end
 end
 
--- extract jfm_file_name and jfm_var
-local function extract_metric(name)
-   local basename=name
-   local tmp = utf.sub(basename, 1, 5)
-   jfm_file_name = ''; jfm_var = ''
-   if tmp == 'file:' or tmp == 'name:' or tmp == 'psft:' then
-      basename = utf.sub(basename, 6)
+-- EXT
+function set_alt_font_latex(b,e,ind,bbase)
+   -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
+  if b<0x80 or e>=ucs_out then
+      ltjb.package_warning('luatexja',
+                        'bad character range ([' .. b .. ',' .. e .. ']). ' ..
+                          'I take the intersection with [0x80, 0x10ffff].')
+   elseif b>e then
+      local j=b; e=b; b=j
    end
-   local p = utf.find(basename, ":")
-   if p then 
-      basename = utf.sub(basename, p+1)
-   else return 
+   if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
+   local t = alt_font_table_latex[bbase]
+   if not t[ind] then t[ind] = {} end
+   for i=math.max(0x80,b),math.min(ucs_out-1,e) do
+      for j,v in pairs(t) do
+         if v[i] then -- remove old entry
+            if j~=ind then v[i]=nil end; break
+         end
+      end
+      t[ind][i]=true
+    end
+    -- remove the empty tables
+    for j,v in pairs(t) do
+       local flag_clear = true
+       for k,_ in pairs(v) do flag_clear = false; break end
+       if flag_clear then t[j]=nil end
+    end
+    if ind==bbase  then t[bbase] = nil end
+end
+
+-- ここから先は 新 \selectfont の内部でしか実行されない
+do
+   local alt_font_base, alt_font_base_num
+   
+-- EXT
+   function output_alt_font_cmd(bbase)
+      alt_font_base = bbase
+      alt_font_base_num = tex.getattribute(attr_curjfnt)
+      local t = alt_font_table[alt_font_base_num]
+      if t then 
+        for i,_ in pairs(t) do t[i]=nil end
+      end
+      t = alt_font_table_latex[bbase]
+      if t then
+        for i,_ in pairs(t) do
+           tex.sprint(cat_lp, '\\ltj@pickup@altfont@aux{' .. i .. '}')
+        end
+      end
    end
-   -- now basename contains 'features' only.
-   p=1
-   while p do
-      local q = utf.find(basename, ";", p+1) or utf.len(basename)+1
-      if utf.sub(basename, p, p+3)=='jfm=' and q>p+4 then
-        jfm_file_name = utf.sub(basename, p+4, q-1)
-      elseif utf.sub(basename, p, p+6)=='jfmvar=' and q>p+6 then
-        jfm_var = utf.sub(basename, p+7, q-1)
+   
+-- EXT
+   function pickup_alt_font_a(size_str)
+      local t = alt_font_table_latex[alt_font_base]
+      if t then
+        for i,v in pairs(t) do
+           tex.sprint(cat_lp, '\\expandafter\\ltj@pickup@altfont@copy'
+                         .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
+        end
+      end
+   end
+   
+-- EXT
+   function pickup_alt_font_b(afnt_num, afnt_base)
+      local t = alt_font_table[alt_font_base_num] 
+      if not t then t = {}; alt_font_table[alt_font_base_num] = t end
+      for i,v in pairs(alt_font_table_latex[alt_font_base]) do
+        if i == afnt_base then
+           for j,_ in pairs(v) do t[j]=afnt_num end
+           return
+        end
       end
-      if utf.len(basename)+1==q then p = nil else p = q + 1 end
    end
-   return
 end
 
--- replace fonts.define.read()
-function font_callback(name, size, id, fallback)
-   extract_metric(name)
-   -- In the present imple., we don't remove "jfm=..." from name.
-   return fallback(name, size, id)
-end
 
 ------------------------------------------------------------------------
 -- MISC
index d4487af..7ff7d1f 100644 (file)
 \protected\def\ltjsetmathletter#1{\directlua{luatexja.math.is_math_letters[#1]=true}}
 \protected\def\ltjunsetmathletter#1{\directlua{luatexja.math.is_math_letters[#1]=false}}
 
+
+%%%%%%%% \ltjdeclarealtfont<base_font_cs><alt_font_cs>{100-200,3000-,5000,...}
+% <base_font_cs> の 第 3 引数の文字達は <alt_font_cs> で組む
+% いつも global
+\def\ltj@@getjfontnumber#1{% result in \ltj@tempcntc 
+  \bgroup#1\directlua{luatexja.__temp = \the\ltj@curjfnt}\egroup
+  \ltj@tempcntc=\directlua{tex.sprint(luatexja.__temp)}\relax}
+\protected\def\ltjdeclarealtfont#1#2#3{%
+  {\ltj@@getjfontnumber#1\@tempcnta=\ltj@tempcntc% \@tempcnta: 基底フォント
+   \ltj@@getjfontnumber#2%
+   \expandafter\ltj@@altfont#3,,}\ignorespaces}
+\def\ltj@@altfont#1,{\def\ltj@temp{#1}%
+  \ifx\ltj@temp\empty\let\@next=\relax\else
+  \ltj@@altfontA{#1}\let\@next=\ltj@@altfont\fi\@next}
+\def\ltj@@altfontA#1{\ltj@@altfontB#1--\@nil}
+\def\ltj@@altfontB#1-#2-#3\@nil{\def\ltj@temp{#3}%
+  \ifx\ltj@temp\empty
+    \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
+  \else
+    \def\ltj@temp{#1}%
+    \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
+    \def\ltj@temp{#2}%
+    \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
+  \fi
+  \directlua{luatexja.jfont.set_alt_font(
+    \the\ltj@tempcnta,\the\ltj@tempcntb,
+    \the\ltj@tempcntc, \the\@tempcnta)}%
+  }
+
+%%%%%%%% \ltjclearaltfont<font_cs>
+% <font_cs> の文字は全部 <font_cs> 本来の文字で組む
+\protected\def\ltjclearaltfont#1{%
+  {\ltj@@getjfontnumber#1%
+   \directlua{luatexja.jfont.clear_alt_font(\the\ltj@tempcntc)}}\ignorespaces}
+
+
 %%------------------ Load module for the format
 
 \ifltj@in@latex                 %<*LaTeX>
index 9a09db9..7833878 100644 (file)
@@ -219,41 +219,56 @@ function luatexja.ext_print_global()
 end
 
 -- main process
--- mode = true iff main_process is called from pre_linebreak_filter
-local function main_process(head, mode, dir)
-   local p = head
-   p = ltjj.main(p,mode)
-   if p then p = ltjw.set_ja_width(p, dir) end
-   return p
+do
+   -- mode = true iff main_process is called from pre_linebreak_filter
+   local function main_process(head, mode, dir)
+      local p = head
+      p = ltjj.main(p,mode)
+      if p then p = ltjw.set_ja_width(p, dir) end
+      return p
+   end
+   
+   -- callbacks
+   
+   luatexbase.add_to_callback(
+      'pre_linebreak_filter', 
+      function (head,groupcode)
+        return main_process(head, true, tex.textdir)
+      end,'ltj.pre_linebreak_filter',
+      luatexbase.priority_in_callback('pre_linebreak_filter',
+                                     'luaotfload.node_processor') + 1)
+   luatexbase.add_to_callback(
+      'hpack_filter', 
+      function (head,groupcode,size,packtype, dir)
+        return main_process(head, false, dir)
+      end,'ltj.hpack_filter',
+      luatexbase.priority_in_callback('hpack_filter',
+                                     'luaotfload.node_processor') + 1)
 end
 
--- callbacks
-
-luatexbase.add_to_callback('pre_linebreak_filter', 
-   function (head,groupcode)
-      return main_process(head, true, tex.textdir)
-   end,'ltj.pre_linebreak_filter',
-   luatexbase.priority_in_callback('pre_linebreak_filter',
-                                  'luaotfload.node_processor') + 1)
-luatexbase.add_to_callback('hpack_filter', 
-  function (head,groupcode,size,packtype, dir)
-     return main_process(head, false, dir)
-  end,'ltj.hpack_filter',
-   luatexbase.priority_in_callback('hpack_filter',
-                                  'luaotfload.node_processor') + 1)
+-- hyphenate
+do
+   local replace_altfont = ltjf.replace_altfont
+   luatexbase.add_to_callback(
+      'hyphenate', 
+      function (head,tail)
+        return replace_altfont(head)
+      end,'ltj.replace_altfont',
+      luatexbase.priority_in_callback('hyphenate', 'ltj.hyphenate')+1)
+end
 
 -- define_font
-
-local otfl_fdr = fonts.definers.read
-function luatexja.font_callback(name, size, id)
-  return ltjf.font_callback(
-     name, size, id, 
-     function (name, size, id) return ltjr.font_callback(name, size, id, otfl_fdr) end
-  )
+do
+   local otfl_fdr = fonts.definers.read
+   local ltjr_font_callback = ltjr.font_callback
+   function luatexja.font_callback(name, size, id)
+      return ltjf.font_callback(
+        name, size, id, 
+        function (name, size, id) return ltjr_font_callback(name, size, id, otfl_fdr) end
+      )
+   end
+   luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
 end
---luatexbase.remove_from_callback('define_font',"luaotfload.define_font")
-luatexbase.add_to_callback('define_font',luatexja.font_callback,"luatexja.font_callback", 1)
-
 
 
 
index 00cb175..24bfa8b 100644 (file)
     \ifin@\jfam#2\relax\else\mathgroup#2\relax\fi
   \expandafter\math@egroup\fi}%
 
+%%%%%% Alternate Japanese Fonts
+%%%% \DeclareAlternateKanjiFont<Benc><Bfam><Bser><Bshape><Aenc><Afam><Aser><Ashape><range>
+% いつも global
+\def\DeclareAlternateKanjiFont#1#2#3#4#5#6#7#8#9{%
+  {\def\ltj@temp@bfont{#1/#2/#3/#4}\def\ltj@temp@afont{#5/#6/#7/#8}%
+   \expandafter\ltj@@altfont@latex#9,,}\ignorespaces}
+\def\ltj@@altfont@latex#1,{\def\ltj@temp{#1}%
+  \ifx\ltj@temp\empty\let\@next=\relax\else
+  \ltj@@altfont@latexA{#1}\let\@next=\ltj@@altfont@latex\fi\@next}
+\def\ltj@@altfont@latexA#1{\ltj@@altfont@latexB#1--\@nil}
+\def\ltj@@altfont@latexB#1-#2-#3\@nil{\def\ltj@temp{#3}%
+  \ifx\ltj@temp\empty
+    \ltj@tempcnta=#1\relax\ltj@tempcntb=\ltj@tempcnta
+  \else
+    \def\ltj@temp{#1}%
+    \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
+    \def\ltj@temp{#2}%
+    \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
+  \fi
+  \directlua{luatexja.jfont.set_alt_font_latex(
+    \the\ltj@tempcnta,\the\ltj@tempcntb, 
+    '\luatexluaescapestring{\ltj@temp@afont}',
+    '\luatexluaescapestring{\ltj@temp@bfont}')}%
+  }
+
+%%%% \ClearAlternateKanjiFont<Benc><Bfam><Bser><Bshape>
+% いつも global
+\def\ClearAlternateKanjiFont#1#2#3#4{%
+   \directlua{luatexja.jfont.clear_alt_font_latex(
+      '\luatexluaescapestring{#1/#2/#3/#4}')}}
+
+% \selectfont 上書き
+\let\ltj@@alt@selectfont@orig=\selectfont
+\def\selectfont{%
+  \ltj@@alt@selectfont@orig
+  % alt fonts の定義
+  \directlua{luatexja.jfont.output_alt_font_cmd('\luatexluaescapestring{\curr@kfontshape}')}%
+  % 定義した alt fonts のデータを \ltjdeclarealtfont に渡すのと同等の処理をする
+  \directlua{luatexja.jfont.pickup_alt_font_a('\f@size')}%
+}
+\def\ltj@pickup@altfont@aux#1{%
+  {\edef\font@name{\csname #1/\f@size\endcsname}\pickup@jfont}%
+}
+\def\ltj@pickup@altfont@copy#1#2{%
+  \ltj@@getjfontnumber#1%
+  \directlua{luatexja.jfont.pickup_alt_font_b(\the\ltj@tempcntc,'\luatexluaescapestring{#2}')}%
+}
+
 \endinput
index 6eb94dc..446649e 100644 (file)
Binary files a/test/mfonttest.pdf and b/test/mfonttest.pdf differ
index 03f904e..01be0b4 100644 (file)
@@ -1,240 +1,6 @@
 %#!lualatex
 \documentclass{ltjsarticle}
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%% TeX interface
-
-\makeatletter
-\def\ltj@@getjfontnumber#1{% result in \ltj@tempcntc 
-  \bgroup#1\directlua{luatexja.__temp = \the\ltj@curjfnt}\egroup
-  \ltj@tempcntc=\directlua{tex.sprint(luatexja.__temp)}\relax}
-
-%%%%%%%% 公開命令
-%%%% \ltjdeclarealtfont<base_font_cs><alt_font_cs>{100-200,3000-,5000,...}
-% <base_font_cs> の 第 3 引数の文字達は <alt_font_cs> で組む
-% いつも global
-\protected\def\ltjdeclarealtfont#1#2#3{%
-  {\ltj@@getjfontnumber#1\@tempcnta=\ltj@tempcntc% \@tempcnta: 基底フォント
-   \ltj@@getjfontnumber#2%
-   \expandafter\ltj@@altfont#3,,}\ignorespaces}
-\def\ltj@@altfont#1,{\def\ltj@temp{#1}%
-  \ifx\ltj@temp\empty\let\@next=\relax\else
-  \ltj@@altfontA{#1}\let\@next=\ltj@@altfont\fi\@next}
-\def\ltj@@altfontA#1{\ltj@@altfontB#1--\@nil}
-\def\ltj@@altfontB#1-#2-#3\@nil{\def\ltj@temp{#3}%
-  \ifx\ltj@temp\empty
-    \ltj@tempcnta=#1 \ltj@tempcntb=\ltj@tempcnta
-  \else
-    \def\ltj@temp{#1}%
-    \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
-    \def\ltj@temp{#2}%
-    \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
-  \fi
-  \directlua{luatexja.jfont.set_alt_font(
-    \the\ltj@tempcnta,\the\ltj@tempcntb,
-    \the\ltj@tempcntc, \the\@tempcnta)}%
-  }
-
-%%%% \ltjclearaltfont<font_cs>
-% <font_cs> の文字は全部 <font_cs> 本来の文字で組む
-\protected\def\ltjclearaltfont#1{%
-  {\ltj@@getjfontnumber#1%
-   \directlua{luatexja.jfont.clear_alt_font(\the\ltj@tempcntc)}}\ignorespaces}
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% ここから Lua code
-\usepackage{luacode}
-
-\begin{luacode*} -- この行は 47 行目
-local alt_font_table = {}; luatexja.jfont.alt_font_table = alt_font_table
-local attr_curaltfnt = {}
-
-local ucs_out = 0x110000
-local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
-local attr_icflag = luatexbase.attributes['ltj@icflag']
-local has_attr = node.has_attribute
-local set_attr = node.set_attribute
-local id_glyph = node.id('glyph')
-local ltjb = luatexja.base
-
-local function set_alt_font(b,e,ind,bfnt)
-   -- ind: 新フォント, bfnt: 基底フォント
-  if b<0x80 or e>=ucs_out then
-      ltjb.package_warning('luatexja',
-                        'bad character range ([' .. b .. ',' .. e .. ']). ' ..
-                          'I take the intersection with [0x80, 0x10ffff].')
-   elseif b>e then
-      local j=b; e=b; b=j
-   end
-   if bfnt==ind then ind = nil end -- ind == bfnt の場合はテーブルから削除
-   if not alt_font_table[bfnt] then alt_font_table[bfnt]={} end
-   local t = alt_font_table[bfnt]
-   for i=math.max(0x80,b),math.min(ucs_out-1,e) do
-      t[i]=ind
-   end
-end
-luatexja.jfont.set_alt_font = set_alt_font
-
-local function clear_alt_font(bfnt)
-   if alt_font_table[bfnt] then 
-      local t = alt_font_table[bfnt]
-      for i,_ in pairs(t) do t[i]=nil; end
-   end
-end
-luatexja.jfont.clear_alt_font = clear_alt_font
-
---%%%%%%%% callback
-local function replace_altfont(head)
-   for p in node.traverse_id(id_glyph, head) do
-      local pf = p.font
-      if p.font == (has_attr(p, attr_curjfnt) or 0) then
-         if alt_font_table[pf] and alt_font_table[pf][p.char] then
-            local n = alt_font_table[pf][p.char]
-            if n then 
-               p.font = n; set_attr(p, attr_curjfnt, n)
-            end
-         end
-      end
-   end
-   return head
-end
-luatexbase.add_to_callback('hyphenate', 
- function (head,tail)
-    return replace_altfont(head)
- end,'ltj.replace_altfont',
- luatexbase.priority_in_callback('hyphenate', 'ltj.hyphenate')+1)
-
-\end{luacode*}
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%%%%%%%% LaTeX interface
-
-
-\begin{luacode*} -- 113 行目
-local alt_font_table_latex = {}
-local alt_font_table = luatexja.jfont.alt_font_table
-
-local ucs_out = 0x110000
-local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
-local ltjb = luatexja.base
-
-local function set_alt_font_latex(b,e,ind,bbase)
-   -- ind: Alt font の enc/fam/ser/shape, bbase: 基底フォントの enc/fam/ser/shape
-  if b<0x80 or e>=ucs_out then
-      ltjb.package_warning('luatexja',
-                        'bad character range ([' .. b .. ',' .. e .. ']). ' ..
-                          'I take the intersection with [0x80, 0x10ffff].')
-   elseif b>e then
-      local j=b; e=b; b=j
-   end
-   if not alt_font_table_latex[bbase] then alt_font_table_latex[bbase]={} end
-   local t = alt_font_table_latex[bbase]
-   if not t[ind] then t[ind] = {} end
-   for i=math.max(0x80,b),math.min(ucs_out-1,e) do
-      for j,v in pairs(t) do
-         if v[i] then -- remove old entry
-            if j~=ind then v[i]=nil end; break
-         end
-      end
-      t[ind][i]=true
-    end
-    -- remove the empty tables
-    for j,v in pairs(t) do
-       local flag_clear = true
-       for k,_ in pairs(v) do flag_clear = false; break end
-       if flag_clear then t[j]=nil end
-    end
-    if ind==bbase  then t[bbase] = nil end
-end
-luatexja.jfont.set_alt_font_latex = set_alt_font_latex
-
--- ここから先は 新 \selectfont の内部でしか実行されない
-
-local alt_font_base, alt_font_base_num
-
-local function output_alt_font_cmd(bbase)
-   alt_font_base = bbase
-   alt_font_base_num = tex.getattribute(attr_curjfnt)
-   local t = alt_font_table[alt_font_base_num]
-   if t then 
-      for i,_ in pairs(t) do t[i]=nil end
-   end
-   t = alt_font_table_latex[bbase]
-   if t then
-      for i,_ in pairs(t) do
-         tex.sprint('\\ltj@pickup@altfont@aux{' .. i .. '}')
-      end
-   end
-end
-luatexja.jfont.output_alt_font_cmd = output_alt_font_cmd
-
-local function pickup_alt_font_a(size_str)
-   local t = alt_font_table_latex[alt_font_base]
-   if t then
-      for i,v in pairs(t) do
-         tex.sprint('\\expandafter\\ltj@pickup@altfont@copy'
-            .. '\\csname ' .. i .. '/' .. size_str .. '\\endcsname{' .. i .. '}')
-      end
-   end
-end
-luatexja.jfont.pickup_alt_font_a = pickup_alt_font_a
-
-local function pickup_alt_font_b(afnt_num, afnt_base) -- base font: attr_jfnt の値
-   local t = alt_font_table[alt_font_base_num] 
-   if not t then t = {}; alt_font_table[alt_font_base_num] = t end
-   for i,v in pairs(alt_font_table_latex[alt_font_base]) do
-      if i == afnt_base then
-         for j,_ in pairs(v) do t[j]=afnt_num end
-         return
-      end
-   end
-end
-luatexja.jfont.pickup_alt_font_b = pickup_alt_font_b
- \end{luacode*}
-
-%%%%%%%% 公開命令
-%%%% \DeclareAlternateKanjiFont<Benc><Bfam><Bser><Bshape><Aenc><Afam><Aser><Ashape><range>
-% いつも global
-\def\DeclareAlternateKanjiFont#1#2#3#4#5#6#7#8#9{%
-  {\def\ltj@temp@bfont{#1/#2/#3/#4}\def\ltj@temp@afont{#5/#6/#7/#8}%
-   \expandafter\ltj@@altfont@latex#9,,}\ignorespaces}
-\def\ltj@@altfont@latex#1,{\def\ltj@temp{#1}%
-  \ifx\ltj@temp\empty\let\@next=\relax\else
-  \ltj@@altfont@latexA{#1}\let\@next=\ltj@@altfont@latex\fi\@next}
-\def\ltj@@altfont@latexA#1{\ltj@@altfont@latexB#1--\@nil}
-\def\ltj@@altfont@latexB#1-#2-#3\@nil{\def\ltj@temp{#3}%
-  \ifx\ltj@temp\empty
-    \ltj@tempcnta=#1\relax\ltj@tempcntb=\ltj@tempcnta
-  \else
-    \def\ltj@temp{#1}%
-    \ifx\ltj@temp\empty\ltj@tempcnta='200\else\ltj@tempcnta=#1\fi\relax
-    \def\ltj@temp{#2}%
-    \ifx\ltj@temp\empty\ltj@tempcntb="10FFFF\else\ltj@tempcntb=#2\fi\relax%"
-  \fi
-  \directlua{luatexja.jfont.set_alt_font_latex(
-    \the\ltj@tempcnta,\the\ltj@tempcntb, 
-    '\luatexluaescapestring{\ltj@temp@afont}',
-    '\luatexluaescapestring{\ltj@temp@bfont}')}%
-  }
-
-% \selectfont 上書き
-\let\ltj@@alt@selectfont@orig=\selectfont
-\def\selectfont{%
-  \ltj@@alt@selectfont@orig
-  % alt fonts の定義
-  \directlua{luatexja.jfont.output_alt_font_cmd('\luatexluaescapestring{\curr@kfontshape}')}%
-  % 定義した alt fonts のデータを \ltjdeclarealtfont に渡すのと同等の処理をする
-  \directlua{luatexja.jfont.pickup_alt_font_a('\f@size')}%
-}
-\def\ltj@pickup@altfont@aux#1{%
-  {\edef\curr@fontshape{#1}\xdef\font@name{\csname #1/\f@size\endcsname}\pickup@jfont}%
-}
-\def\ltj@pickup@altfont@copy#1#2{%
-  \ltj@@getjfontnumber#1%
-  \directlua{luatexja.jfont.pickup_alt_font_b(\the\ltj@tempcntc,'\luatexluaescapestring{#2}')}%
-}
-
-
 \begin{document}
 
 %%%%%%%% plain TeX
@@ -245,7 +11,7 @@ luatexja.jfont.pickup_alt_font_b = pickup_alt_font_b
 \jaA 
 あア漢字% 置換なし
 \ltjdeclarealtfont\jaA\jaB{"3000-"30FF}
-あア漢字% 「あ」のみ置換(setaltfont は段落末尾の状況が全段落で通用)
+あア漢字% 「あ」のみ置換(段落末尾の状況が全段落で通用)
 \ltjdeclarealtfont\jaA\jaA{"30A0-"30FF}
 あア漢字% 「あ」のみ置換
 
@@ -265,12 +31,20 @@ luatexja.jfont.pickup_alt_font_b = pickup_alt_font_b
 %%%%%%%% LaTeX
 
 \DeclareAlternateKanjiFont{JY3}{mc}{m}{n}{JY3}{gt}{m}{n}{`い,`う}
+\mc
 あいうえお%             \selectfont しないと有効にはならない
 \selectfont あいうえお% 「い」「う」が置換
+{\Large  あいうえお}
 
 {%
   \DeclareAlternateKanjiFont{JY3}{mc}{m}{n}{JY3}{mc}{m}{n}{`い}%
 }% always global
 \DeclareAlternateKanjiFont{JY3}{mc}{m}{n}{JY3}{gt}{m}{n}{`お}
 \selectfont あいうえお% 「う」「お」が置換
+{\Large  あいうえお}
+
+\ClearAlternateKanjiFont{JY3}{mc}{m}{n}
+{\Large  あいうえお}%    置換なし
+あいうえお%              まだ「う」「お」が置換のまま
+
 \end{document}