OSDN Git Service

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