OSDN Git Service

c16e15b8f9ca8df75fd70df077456fc768292d7b
[luatex-ja/luatexja.git] / src / ltj-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 luatexja.load_module('base');      local ltjb = luatexja.base
13 luatexja.load_module('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 local ltjf_font_metric_table = ltjf.font_metric_table
34 local ltjf_find_char_class = ltjf.find_char_class
35
36 local OTF = luatexja.userid_table.OTF
37
38 -- Append a whatsit node to the list.
39 -- This whatsit node will be extracted to a glyph_node
40 function append_jglyph(char)
41    local p = node_new(id_whatsit,sid_user)
42    local v = tex.attribute[attr_curjfnt]
43    p.user_id=OTF; p.type=100; p.value=char
44    set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
45    node.write(p)
46 end
47
48 function cid(key)
49    local curjfnt = fonts.ids[tex.attribute[attr_curjfnt]]
50    if curjfnt.cidinfo.ordering ~= "Japan1" and
51            curjfnt.cidinfo.ordering ~= "GB1" and
52            curjfnt.cidinfo.ordering ~= "CNS1" and
53            curjfnt.cidinfo.ordering ~= "Korea1" then
54       ltjb.package_error('luatexja-otf',
55                          'Current Japanese font (or other CJK font) "'..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)', 
56                          'Select a CID-Keyed font using \\jfont.')
57       return
58    end
59    local char = curjfnt.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
60    if not char then
61       ltjb.package_warning('luatexja-otf',
62                          'Current Japanese font (or other CJK font) "'..curjfnt.psname..'" does not include the specified CID character ('..tostring(key)..')', 
63                          'Use a font including the specified CID character.')
64       return
65    end
66    append_jglyph(char)
67 end
68
69 function extract(head)
70    local p = head
71    local v
72    while p do
73       if p.id==id_whatsit then
74          if p.subtype==sid_user and p.user_id==OTF then
75             local g = node_new(id_glyph)
76             g.subtype = 0; g.char = p.value
77             v = has_attr(p, attr_curjfnt); g.font = v
78             set_attr(g, attr_curjfnt, v)
79             v = has_attr(p, attr_yablshift)
80             if v then 
81                set_attr(g, attr_yablshift, v)
82             else
83                unset_attr(g, attr_yablshift)
84             end
85             head = node_insert_after(head, p, g)
86             head = node_remove(head, p)
87             node_free(p); p = g
88          end
89       end
90       p = node_next(p)
91    end
92    return head
93 end
94
95 luatexbase.add_to_callback('hpack_filter', 
96    function (head) return extract(head) end,'ltj.hpack_filter_otf',
97    luatexbase.priority_in_callback('pre_linebreak_filter',
98                                    'ltj.pre_linebreak_filter'))
99 luatexbase.add_to_callback('pre_linebreak_filter', 
100    function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
101    luatexbase.priority_in_callback('pre_linebreak_filter',
102                                    'ltj.pre_linebreak_filter'))
103
104
105 -- additional callbacks
106 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
107 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
108 --   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
109 --   CID <-> グリフ 対応状況による変換テーブルが用意される.
110
111 -- フォント読み込み時に,CID
112 local function cid_to_char(fmtable, fn)
113    local fi = fonts.ids[fn]
114    if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
115       fmtable.cid_char_type = {}
116       for i, v in pairs(fmtable.chars) do
117          local j = string.match(i, "^AJ1%-([0-9]*)")
118          if j then
119             j = tonumber(fi.unicodes['Japan1.'..tostring(j)])
120             if j then
121                fmtable.cid_char_type[j] = v 
122             end
123          end
124       end
125    end
126    return fmtable
127 end
128 luatexbase.add_to_callback("luatexja.define_jfont", 
129                            cid_to_char, "ltj.otf.define_jfont", 1)
130 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
131 for fn, v in pairs(ltjf_font_metric_table) do
132    ltjf_font_metric_table[fn] = cid_to_char(v, fn)
133 end
134
135
136 local function cid_set_char_class(arg, fmtable, char)
137    if arg~=0 then return arg
138    elseif fmtable.cid_char_type then
139       return fmtable.cid_char_type[char] or 0
140    else return 0
141    end
142 end
143 luatexbase.add_to_callback("luatexja.find_char_class", 
144                            cid_set_char_class, "ltj.otf.find_char_class", 1)
145
146 -------------------- all done
147 -- EOF