OSDN Git Service

Merged test/ivs.lua into ltj-otf.lua
authorHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Wed, 18 Dec 2013 00:58:51 +0000 (09:58 +0900)
committerHironori Kitagawa <h_kitagawa2001@yahoo.co.jp>
Wed, 18 Dec 2013 00:58:51 +0000 (09:58 +0900)
However, IVS-related code are not enabled if luatexja-otf.sty is only loaded.
To eneble it, one need to execute
  \directlua{luatexja.otf.enable_ivs()}
in the document.

I found that current IVS code does not work for OpenType fonts
  (e.g., Kozuka Mincho and  Hanazono Mincho OT).

src/ltj-otf.lua
test/ivs.lua [deleted file]
test/test19-ivs.pdf
test/test19-ivs.tex
tool/ivslist.tex [new file with mode: 0644]

index cdcd7e2..bc41a90 100644 (file)
@@ -7,9 +7,13 @@ luatexbase.provides_module({
   description = 'The OTF Lua module for LuaTeX-ja',
 })
 
+require('unicode')
+
+
 luatexja.load_module('base');      local ltjb = luatexja.base
 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
 luatexja.load_module('rmlgbm');    local ltjr = luatexja.rmlgbm
+luatexja.load_module('charrange'); local ltjc = luatexja.charrange
 
 local id_glyph = node.id('glyph')
 local id_whatsit = node.id('whatsit')
@@ -23,6 +27,7 @@ local has_attr = node.has_attribute
 local set_attr = node.set_attribute
 local unset_attr = node.unset_attribute
 local node_insert_after = node.insert_after
+local identifiers = fonts.hashes.identifiers
 
 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
@@ -32,6 +37,7 @@ local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
 local ltjf_font_metric_table = ltjf.font_metric_table
 local ltjf_find_char_class = ltjf.find_char_class
 local ltjr_cidfont_data = ltjr.cidfont_data
+local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
 
 local OTF = luatexja.userid_table.OTF
 
@@ -70,7 +76,7 @@ end
 
 local function cid(key)
    if key==0 then return append_jglyph(char) end
-   local curjfnt = fonts.hashes.identifiers[tex.attribute[attr_curjfnt]]
+   local curjfnt = identifiers[tex.attribute[attr_curjfnt]]
    if not curjfnt.cidinfo or 
       curjfnt.cidinfo.ordering ~= "Japan1" and
       curjfnt.cidinfo.ordering ~= "GB1" and
@@ -137,7 +143,7 @@ luatexbase.add_to_callback('pre_linebreak_filter',
 
 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
 local function cid_to_char(fmtable, fn)
-   local fi = fonts.hashes.identifiers[fn]
+   local fi = identifiers[fn]
    if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
       fmtable.cid_char_type = {}
       for i, v in pairs(fmtable.chars) do
@@ -170,9 +176,51 @@ end
 luatexbase.add_to_callback("luatexja.find_char_class", 
                           cid_set_char_class, "ltj.otf.find_char_class", 1)
 
+-------------------- IVS
+do
+   local get_node_font = function(p)
+      return (ltjc_is_ucs_in_japanese_char(p) and (has_attr(p, attr_curjfnt) or 0) or p.font)
+   end
+   local get_current_font = function() return tex.attribute[attr_curjfnt] or 0 end
+   local function do_ivs_repr(head)
+      local p = head
+      while p do
+        local pid = p.id
+        if pid==id_glyph then
+           local pf = get_node_font(p)
+           local pt = identifiers[pf]
+           pt = pt and pt.resources; pt = pt and pt.variants
+           if pt then
+              local q = node_next(p) -- the next node of p
+              if q and q.id==id_glyph then
+                 local qc = q.char
+                 if qc>=0xE0100 and qc<0xE01F0 then -- q is an IVS selector
+                    pt = pt[qc];  pt = pt and  pt[p.char]
+                 if pt then
+                    p.char = pt or p.char
+                 end
+                 head = node_remove(head,q)
+                 end
+              end
+        end
+        end
+        p = node_next(p)
+      end
+      return head
+   end
+   function enable_ivs()
+      luatexbase.add_to_callback('hpack_filter', 
+                                function (head) return do_ivs_repr(head) end,'do_ivs', 1)
+      luatexbase.add_to_callback('pre_linebreak_filter', 
+                                function (head) return do_ivs_repr(head) end, 'do_ivs', 1)
+   end
+
+end
+
 -------------------- all done
 luatexja.otf = {
   append_jglyph = append_jglyph,
+  enable_ivs = enable_ivs,  -- 隠し機能: IVS(TTF しか現状では使えない)
   cid = cid,
 }
 
diff --git a/test/ivs.lua b/test/ivs.lua
deleted file mode 100644 (file)
index 7f3ac4e..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-require('unicode')
-
-local id_glyph = node.id('glyph')
-local has_attr = node.has_attribute
-local identifiers = fonts.hashes.identifiers
-local node_next = node.next
-local node_remove = node.remove
-
-local get_node_font, get_current_font
-if luatexja then -- test if LuaTeX-ja is loaded
-   luatexja.load_module('charrange'); local ltjc = luatexja.charrange
-   local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
-   local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
-   get_node_font = function(p)
-      return (ltjc_is_ucs_in_japanese_char(p) and (has_attr(p, attr_curjfnt) or 0) or p.font)
-   end
-   get_current_font = function() return tex.attribute[attr_curjfnt] or 0 end
-else
-   get_node_font = function(p) return p.font end
-   get_current_font = font.current
-end
-
-local function do_ivs_repr(head)
-  local p = head
-  while p do
-     local pid = p.id
-     if pid==id_glyph then
-        local pf = get_node_font(p)
-        local pt = identifiers[pf]
-        pt = pt and pt.resources; pt = pt and pt.variants
-        if pt then
-           local q = node_next(p) -- the next node of p
-           if q and q.id==id_glyph then
-              local qc = q.char
-              if qc>=0xE0100 and qc<0xE01F0 then -- q is an IVS selector
-                 pt = pt[qc];  pt = pt and  pt[p.char]
-                 if pt then
-                    p.char = pt or p.char
-                 end
-                 head = node_remove(head,q)
-              end
-           end
-        end
-     end
-     p = node_next(p)
-  end
-  return head
-end
--- callback
-luatexbase.add_to_callback('hpack_filter', 
-   function (head) return do_ivs_repr(head) end,'do_ivs', 1)
-luatexbase.add_to_callback('pre_linebreak_filter', 
-   function (head) return do_ivs_repr(head) end, 'do_ivs', 1)
-
-ivs = {}
-
-do
-   local ubyte = unicode.utf8.byte
-   local uchar = unicode.utf8.char
-   local sort = table.sort
-   function ivs.list_ivs(s)
-      local c = ubyte(s)
-      local pt = identifiers[get_current_font()]
-      pt = pt and pt.resources; pt = pt and pt.variants
-      if pt then
-         local t = {}
-         for i,v in pairs(pt) do
-            if v[c] then t[1+#t]=i end
-         end
-         sort(t)
-         for _,v in ipairs(t) do 
-            tex.sprint('\\oalign{' .. s .. uchar(v) 
-                          .. '\\crcr\\hss\\tiny' .. tostring(v-0xE0100) .. '\\hss\\crcr}') 
-         end
-      end
-   end
-end
index ab0ab73..97749dc 100644 (file)
Binary files a/test/test19-ivs.pdf and b/test/test19-ivs.pdf differ
index 38e4d89..1796a85 100644 (file)
@@ -1,9 +1,33 @@
 %#!lualatex
 \documentclass{ltjsarticle}
-\usepackage{luatexja-fontspec,booktabs,array}
+\usepackage{luatexja-fontspec,luatexja-otf, luacode, booktabs,array}
+
+\begin{luacode}
+   local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
+   local identifiers = fonts.hashes.identifiers
+   local ubyte = unicode.utf8.byte
+   local uchar = unicode.utf8.char
+   local sort = table.sort
+   function list_ivs(s)
+      local c = ubyte(s)
+      local pt = identifiers[tex.attribute[attr_curjfnt]]
+      pt = pt and pt.resources; pt = pt and pt.variants
+      if pt then
+         local t = {}
+         for i,v in pairs(pt) do
+            if v[c] then t[1+#t]=i end
+         end
+         sort(t)
+         for _,v in ipairs(t) do 
+            tex.sprint('\\oalign{' .. s .. uchar(v) 
+                          .. '\\crcr\\hss\\tiny' .. tostring(v-0xE0100) .. '\\hss\\crcr}') 
+         end
+      end
+   end
+\end{luacode}
 
 \def\MJI[#1]#2{#2\char\numexpr "E0100+#1\relax}%"
-\def\IVSL#1{\directlua{ivs.list_ivs('#1')}}
+\def\IVSL#1{\directlua{list_ivs('#1')}}
 \begin{document}
 \jfontspec{ipamjm.ttf} % IPA MJ 明朝
 
@@ -18,7 +42,7 @@
 \end{quote}
 
 \paragraph{IVS処理コードをここで読み込んだ.}\ 
-\directlua{dofile('ivs.lua')}
+\directlua{luatexja.otf.enable_ivs()}
 
 \begin{quote}
 \LARGE
diff --git a/tool/ivslist.tex b/tool/ivslist.tex
new file mode 100644 (file)
index 0000000..b901433
--- /dev/null
@@ -0,0 +1,56 @@
+%#!lualatex
+\documentclass[twocolumn]{article}
+\batchmode
+\usepackage{luaotfload,booktabs,array,luacode,longtable}
+\usepackage[ratio=0.80]{geometry}
+\errorstopmode
+
+{\newlinechar=`@
+\message{@Name of the font to test = }
+\read-1 to\fontname \global\let\fontname\fontname}
+\message{@}
+\font\test=\fontname at 12pt
+{\test\directlua{testfn = font.current()}}
+
+\def\FMT#1#2{{\oalign{\test#1\crcr\rm\tiny\hss#2\hss\crcr}}}
+\def\OUT#1{\leavevmode\hbox to 7em{{\test\char"#1}\ ({\tt U+#1})\hss}}%"
+\begin{document}
+{\noindent\Large\bf \fontname\\\null\hfill (%
+  \directlua{tex.sprint(fonts.hashes.identifiers[testfn].fullname)}%
+)}
+
+\bigskip
+\baselineskip18pt
+
+\begin{luacode*}
+   local identifiers = fonts.hashes.identifiers
+   local ubyte = unicode.utf8.byte
+   local uchar = unicode.utf8.char
+   local sort = table.sort
+   local t, ti = {}, {}
+   local sort_func = function(a,b) return a[1]<b[1] end
+   local pt = identifiers[testfn]
+   pt = pt and pt.resources; pt = pt and pt.variants
+   if pt then
+      for vs,v in pairs(pt) do
+         if vs>=0xe0100 and vs<0xe01f0 then
+           for bc, rc in pairs(v) do
+              if not t[bc] then t[bc] = {}; ti[1+#ti] = bc end
+              t[bc][1+#(t[bc])]={ vs-0xe0100, rc }
+           end
+         end
+      end
+      sort(ti)
+      for _,v in pairs(ti) do 
+         local s = '\\OUT{' .. string.format('%X', v) .. '}'
+         local tx = t[v]; sort(tx, sort_func)
+         for _,x in ipairs(tx) do
+           s = s .. '\\FMT{' .. uchar(x[2]) .. '}{' .. tostring(x[1]) .. '}'
+         end
+         tex.sprint(s .. '\\par')
+      end
+   end
+\end{luacode*}
+
+
+\end{document}
\ No newline at end of file