OSDN Git Service

test19-ivs.tex, ivs.lua: test of variation selectors
[luatex-ja/luatexja.git] / test / ivs.lua
1 require('unicode')
2
3 local id_glyph = node.id('glyph')
4 local has_attr = node.has_attribute
5 local identifiers = fonts.hashes.identifiers
6 local node_next = node.next
7 local node_remove = node.remove
8
9 local get_node_font, get_current_font
10 if luatexja then -- test if LuaTeX-ja is loaded
11    luatexja.load_module('charrange'); local ltjc = luatexja.charrange
12    local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
13    local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
14    get_node_font = function(p)
15       return (ltjc_is_ucs_in_japanese_char(p) and (has_attr(p, attr_curjfnt) or 0) or p.font)
16    end
17    get_current_font = function() return tex.attribute[attr_curjfnt] or 0 end
18 else
19    get_node_font = function(p) return p.font end
20    get_current_font = font.current
21 end
22
23 local function do_ivs_repr(head)
24   local p = head
25   while p do
26      local pid = p.id
27      if pid==id_glyph then
28         local pf = get_node_font(p)
29         local pt = identifiers[pf]
30         pt = pt and pt.resources; pt = pt and pt.variants
31         if pt then
32            local q = node_next(p) -- the next node of p
33            if q and q.id==id_glyph then
34               local qc = q.char
35               if qc>=0xE0100 and qc<0xE01F0 then -- q is an IVS selector
36                  pt = pt[qc];  pt = pt and  pt[p.char]
37                  if pt then
38                     p.char = pt or p.char
39                  end
40                  head = node_remove(head,q)
41               end
42            end
43         end
44      end
45      p = node_next(p)
46   end
47   return head
48 end
49 -- callback
50 luatexbase.add_to_callback('hpack_filter', 
51    function (head) return do_ivs_repr(head) end,'do_ivs', 1)
52 luatexbase.add_to_callback('pre_linebreak_filter', 
53    function (head) return do_ivs_repr(head) end, 'do_ivs', 1)
54
55 ivs = {}
56
57 do
58    local ubyte = unicode.utf8.byte
59    local uchar = unicode.utf8.char
60    local sort = table.sort
61    function ivs.list_ivs(s)
62       local c = ubyte(s)
63       local pt = identifiers[get_current_font()]
64       pt = pt and pt.resources; pt = pt and pt.variants
65       if pt then
66          local t = {}
67          for i,v in pairs(pt) do
68             if v[c] then t[1+#t]=i end
69          end
70          sort(t)
71          for _,v in ipairs(t) do 
72             tex.sprint('\\oalign{' .. s .. uchar(v) 
73                           .. '\\crcr\\hss\\tiny' .. tostring(v-0xE0100) .. '\\hss\\crcr}') 
74          end
75       end
76    end
77 end