4 luatexbase.provides_module({
8 description = 'The OTF Lua module for LuaTeX-ja',
11 luatexja.load_module('base'); local ltjb = luatexja.base
12 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
13 luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
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
35 local ltjr_cidfont_data = ltjr.cidfont_data
37 local OTF = luatexja.userid_table.OTF
39 local function get_ucs_from_rmlgbm(c)
40 local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
41 if not v then -- AJ1 範囲外
43 elseif v<0xF0000 then -- 素直に Unicode にマップ可能
46 local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
48 local i = string.len(w)
52 i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
53 if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
54 return (w-0xD800)*0x400 + (i-0xDC00)
62 -- Append a whatsit node to the list.
63 -- This whatsit node will be extracted to a glyph_node
64 local function append_jglyph(char)
65 local p = node_new(id_whatsit,sid_user)
66 local v = tex.attribute[attr_curjfnt]
67 p.user_id=OTF; p.type=100; p.value=char
68 set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
72 local function cid(key)
73 if key==0 then return append_jglyph(char) end
74 local curjfnt = fonts.ids[tex.attribute[attr_curjfnt]]
75 if not curjfnt.cidinfo or
76 curjfnt.cidinfo.ordering ~= "Japan1" and
77 curjfnt.cidinfo.ordering ~= "GB1" and
78 curjfnt.cidinfo.ordering ~= "CNS1" and
79 curjfnt.cidinfo.ordering ~= "Korea1" then
80 -- ltjb.package_warning('luatexja-otf',
81 -- 'Current Japanese font (or other CJK font) "'
82 -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
83 return append_jglyph(get_ucs_from_rmlgbm(key))
85 local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
87 ltjb.package_warning('luatexja-otf',
88 'Current Japanese font (or other CJK font) "'
89 ..curjfnt.psname..'" does not have the specified CID character ('
91 'Use a font including the specified CID character.')
94 return append_jglyph(char)
97 local function extract(head)
101 if p.id==id_whatsit then
102 if p.subtype==sid_user and p.user_id==OTF then
103 local g = node_new(id_glyph)
104 g.subtype = 0; g.char = p.value
105 v = has_attr(p, attr_curjfnt); g.font = v
106 set_attr(g, attr_curjfnt, v)
107 v = has_attr(p, attr_yablshift)
109 set_attr(g, attr_yablshift, v)
111 unset_attr(g, attr_yablshift)
113 head = node_insert_after(head, p, g)
114 head = node_remove(head, p)
123 luatexbase.add_to_callback('hpack_filter',
124 function (head) return extract(head) end,'ltj.hpack_filter_otf',
125 luatexbase.priority_in_callback('pre_linebreak_filter',
126 'ltj.pre_linebreak_filter'))
127 luatexbase.add_to_callback('pre_linebreak_filter',
128 function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
129 luatexbase.priority_in_callback('pre_linebreak_filter',
130 'ltj.pre_linebreak_filter'))
133 -- additional callbacks
134 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
135 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
136 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
137 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
140 local function cid_to_char(fmtable, fn)
141 local fi = fonts.ids[fn]
142 if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
143 fmtable.cid_char_type = {}
144 for i, v in pairs(fmtable.chars) do
145 local j = string.match(i, "^AJ1%-([0-9]*)")
147 j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
149 fmtable.cid_char_type[j] = v
156 luatexbase.add_to_callback("luatexja.define_jfont",
157 cid_to_char, "ltj.otf.define_jfont", 1)
158 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
159 for fn, v in pairs(ltjf_font_metric_table) do
160 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
164 local function cid_set_char_class(arg, fmtable, char)
165 if arg~=0 then return arg
166 elseif fmtable.cid_char_type then
167 return fmtable.cid_char_type[char] or 0
171 luatexbase.add_to_callback("luatexja.find_char_class",
172 cid_set_char_class, "ltj.otf.find_char_class", 1)
174 -------------------- all done
176 append_jglyph = append_jglyph,