4 luatexbase.provides_module({
8 description = 'The OTF Lua module for LuaTeX-ja',
10 module('luatexja.otf', package.seeall)
12 require('luatexja.base'); local ltjb = luatexja.base
13 require('luatexja.jfont'); local ltjf = luatexja.jfont
15 local id_glyph = node.id('glyph')
16 local id_whatsit = node.id('whatsit')
17 local sid_user = node.subtype('user_defined')
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
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']
33 local ltjf_font_metric_table = ltjf.font_metric_table
34 local ltjf_find_char_class = ltjf.find_char_class
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])
48 local curjfnt = fonts.ids[tex.attribute[attr_curjfnt]]
49 if curjfnt.cidinfo.ordering ~= "Japan1" then
50 ltjb.package_error('luatexja-otf',
51 'Current Japanese font "'..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1)',
52 'Select a CID-Keyed font using \jfont.')
55 local char = curjfnt.unicodes['Japan1.'..tostring(key)]
57 ltjb.package_warning('luatexja-otf',
58 'Current Japanese font "'..curjfnt.psname..'" does not include the specified CID character ('..tostring(key)..')',
59 'Use a font including the specified CID character.')
65 function extract(head)
68 if p.id==id_whatsit then
69 if p.subtype==sid_user and p.user_id==30113 then
70 local g = node_new(id_glyph)
71 g.subtype = 0; g.char = p.value
72 v = has_attr(p, attr_curjfnt); g.font = v
73 set_attr(g, attr_jchar_class,
74 ltjf_find_char_class(g.char, ltjf_font_metric_table[v]))
75 set_attr(g, attr_curjfnt, v)
76 v = has_attr(p, attr_yablshift)
78 set_attr(g, attr_yablshift, v)
80 unset_attr(g, attr_yablshift)
82 head = node_insert_after(head, p, g)
83 head = node_remove(head, p)
92 luatexbase.add_to_callback('hpack_filter',
93 function (head) return extract(head) end,'ltj.hpack_filter_otf',
94 luatexbase.priority_in_callback('pre_linebreak_filter',
95 'ltj.pre_linebreak_filter'))
96 luatexbase.add_to_callback('pre_linebreak_filter',
97 function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
98 luatexbase.priority_in_callback('pre_linebreak_filter',
99 'ltj.pre_linebreak_filter'))
102 -- additional callbacks
103 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
104 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
105 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
106 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
109 local function cid_to_char(fmtable, fn)
110 local fi = fonts.ids[fn]
111 if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
112 fmtable.cid_char_type = {}
113 for i, v in pairs(ltjf.metrics[fmtable.jfm].chars) do
114 local j = string.match(i, "^AJ1%-([0-9]*)")
116 j = tonumber(fi.unicodes['Japan1.'..tostring(j)])
118 fmtable.cid_char_type[j] = v
125 luatexbase.add_to_callback("luatexja.define_jfont",
126 cid_to_char, "ltj.otf.define_jfont", 1)
127 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
128 for fn, v in pairs(ltjf_font_metric_table) do
129 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
133 local function cid_set_char_class(arg, fmtable, char)
134 if arg~=0 then return arg
135 elseif fmtable.cid_char_type then
136 return fmtable.cid_char_type[char] or 0
140 luatexbase.add_to_callback("luatexja.find_char_class",
141 cid_set_char_class, "ltj.otf.find_char_class", 1)
143 -------------------- all done