OSDN Git Service

0b69241334013b2c93415d0ecf76a152f56ee981
[luatex-ja/luatexja.git] / src / luatexja / otf.lua
1 --
2 -- luatexja/otf.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.otf',
6   date = '2011/09/09',
7   version = '0.1',
8   description = 'The OTF Lua module for LuaTeX-ja',
9 })
10 module('luatexja.otf', package.seeall)
11
12 require('luatexja.base');      local ltjb = luatexja.base
13 require('luatexja.jfont');     local ltjf = luatexja.jfont
14
15 local id_glyph = node.id('glyph')
16 local id_whatsit = node.id('whatsit')
17 local sid_user = node.subtype('user_defined')
18
19 local node_new = node.new
20 local node_remove = node.remove
21 local node_next = node.next
22 local node_free = node.free
23 local has_attr = node.has_attribute
24 local set_attr = node.set_attribute
25 local unset_attr = node.unset_attribute
26 local node_insert_after = node.insert_after
27
28 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
29 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
30 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
31 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
32
33 -- Append a whatsit node to the list.
34 -- This whatsit node will be extracted to a glyph_node
35 function append_jglyph(char)
36    local p = node_new(id_whatsit,sid_user)
37    local v = tex.attribute[attr_curjfnt]
38    p.user_id=30113; p.type=100; p.value=char
39    set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
40    node.write(p)
41 end
42
43 function cid(key)
44    local curjfnt = fonts.ids[tex.attribute[attr_curjfnt]]
45    if curjfnt.cidinfo.ordering ~= "Japan1" then
46       ltjb.package_error('luatexja-otf',
47                          'Current Japanese font "'..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1)', 
48                          'Select a CID-Keyed font using \jfont.')
49       return
50    end
51    local char = curjfnt.unicodes['Japan1.'..tostring(key)]
52    if not char then
53       ltjb.package_error('luatexja-otf',
54                          'Current Japanese font "'..curjfnt.psname..'" does not include the specified CID character ('..tostring(key)..')', 
55                          'Use a font including the specified CID character.')
56       return
57    end
58    append_jglyph(char)
59 end
60
61 function extract(head)
62    local p = head, v
63    while p do
64       if p.id==id_whatsit then
65          if p.subtype==sid_user and p.user_id==30113 then
66             local g = node_new(id_glyph)
67             g.subtype = 0; g.char = p.value
68             v = has_attr(p, attr_curjfnt); g.font = v
69             set_attr(g, attr_jchar_class,
70                      ltjf.find_char_class(g.char, ltjf.font_metric_table[v].jfm))
71             set_attr(g, attr_curjfnt, v)
72             v = has_attr(p, attr_yablshift)
73             if v then 
74                set_attr(g, attr_yablshift, v)
75             else
76                unset_attr(g, attr_yablshift)
77             end
78             head = node_insert_after(head, p, g)
79             head = node_remove(head, p)
80             node_free(p); p = g
81          end
82       end
83       p = node_next(p)
84    end
85    return head
86 end
87
88 luatexbase.add_to_callback('hpack_filter', 
89    function (head) return extract(head) end,'ltj.hpack_filter_otf',
90    luatexbase.priority_in_callback('pre_linebreak_filter',
91                                    'ltj.pre_linebreak_filter'))
92 luatexbase.add_to_callback('pre_linebreak_filter', 
93    function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
94    luatexbase.priority_in_callback('pre_linebreak_filter',
95                                    'ltj.pre_linebreak_filter'))
96
97
98 -------------------- all done
99 -- EOF