2 -- luatexja/ltj-otf.lua
7 luatexja.load_module('base'); local ltjb = luatexja.base
8 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
9 luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
10 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
11 luatexja.load_module('direction'); local ltjd = luatexja.direction
12 luatexja.load_module('stack'); local ltjs = luatexja.stack
14 local id_glyph = node.id('glyph')
15 local id_whatsit = node.id('whatsit')
16 local sid_user = node.subtype('user_defined')
18 local setfield = node.direct.setfield
19 local getfield = node.direct.getfield
20 local getid = node.direct.getid
21 local getfont = node.direct.getfont
22 local getchar = node.direct.getchar
23 local getsubtype = node.direct.getsubtype
25 local to_node = node.direct.tonode
26 local to_direct = node.direct.todirect
28 local node_new = node.direct.new
29 local node_remove = node.direct.remove
30 local node_next = node.direct.getnext
31 local node_free = node.direct.free
32 local has_attr = node.direct.has_attribute
33 local set_attr = node.direct.set_attribute
34 local unset_attr = node.direct.unset_attribute
35 local node_insert_after = node.direct.insert_after
36 local node_write = node.direct.write
37 local node_traverse_id = node.direct.traverse_id
40 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
41 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
42 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
43 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
44 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
45 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
46 local lang_ja = luatexja.lang_ja
47 local identifiers = fonts.hashes.identifiers
49 local ltjf_font_metric_table = ltjf.font_metric_table
50 local ltjf_font_extra_info = ltjf.font_extra_info
51 local ltjf_find_char_class = ltjf.find_char_class
52 local ltjr_cidfont_data = ltjr.cidfont_data
53 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
54 local ltjd_get_dir_count = ltjd.get_dir_count
55 local dir_tate = luatexja.dir_table.dir_tate
57 luatexja.userid_table.OTF = luatexbase.newuserwhatsitid('char_by_cid', 'luatexja')
58 luatexja.userid_table.VSR = luatexbase.newuserwhatsitid('replace_vs', 'luatexja')
59 local OTF, VSR = luatexja.userid_table.OTF, luatexja.userid_table.VSR
61 local function get_ucs_from_rmlgbm(c)
62 local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
63 if not v then -- AJ1 範囲外
65 elseif v<0xF0000 then -- 素直に Unicode にマップ可能
68 local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
70 local i = string.len(w)
74 i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
75 if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
76 return (w-0xD800)*0x400 + (i-0xDC00)
84 -- Append a whatsit node to the list.
85 -- This whatsit node will be extracted to a glyph_node
86 local function append_jglyph(char)
87 local p = node_new(id_whatsit,sid_user)
88 setfield(p, 'user_id', OTF)
89 setfield(p, 'type', 100)
90 setfield(p, 'value', char)
96 local tex_get_attr = tex.getattribute
98 if key==0 then return append_jglyph(char) end
99 local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
100 and attr_curtfnt or attr_curjfnt)
101 local curjfnt = identifiers[curjfnt_num]
102 local cidinfo = curjfnt.resources.cidinfo
104 cidinfo.ordering ~= "Japan1" and
105 cidinfo.ordering ~= "GB1" and
106 cidinfo.ordering ~= "CNS1" and
107 cidinfo.ordering ~= "Korea1" then
108 -- ltjb.package_warning('luatexja-otf',
109 -- 'Current Japanese font (or other CJK font) "'
110 -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
111 return append_jglyph(get_ucs_from_rmlgbm(key))
113 local fe, char = ltjf_font_extra_info[curjfnt_num], nil
114 if fe and fe.unicodes then
115 char = fe.unicodes[cidinfo.ordering..'.'..tostring(key)]
118 ltjb.package_warning('luatexja-otf',
119 'Current Japanese font (or other CJK font) "'
120 ..curjfnt.psname..'" does not have the specified CID character ('
121 ..tostring(key)..')',
122 'Use a font including the specified CID character.')
125 return append_jglyph(char)
129 local function extract(head)
130 head = to_direct(head)
132 local is_dir_tate = ltjs.list_dir == dir_tate
133 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
134 local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
135 local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
137 if getid(p)==id_whatsit then
138 if getsubtype(p)==sid_user then
139 local puid = getfield(p, 'user_id')
140 if puid==OTF or puid==VSR then
141 local g = node_new(id_glyph)
142 setfield(g, 'subtype', 0)
143 setfield(g, 'char', getfield(p, 'value'))
144 local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
146 setfield(g, 'lang', lang_ja)
147 set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
149 set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
151 head = node_insert_after(head, p, g)
152 head = node_remove(head, p)
162 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
163 luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
164 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
165 luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
166 -- additional callbacks
167 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
168 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
169 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
170 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
172 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
173 local function cid_to_char(fmtable, fn)
174 local fi = identifiers[fn]
175 local fe = ltjf_font_extra_info[fn]
176 if (fi.resources and fi.resources.cidinfo and fi.resources.cidinfo.ordering == "Japan1" )
177 and (fe and fe.unicodes) then
178 for i, v in pairs(fmtable.chars) do
179 local j = string.match(i, "^AJ1%-([0-9]*)")
181 j = tonumber(fe.unicodes['Japan1.'..tostring(j)])
183 fmtable.cid_char_type = fmtable.cid_char_type or {}
184 fmtable.cid_char_type[j] = v
191 luatexbase.add_to_callback("luatexja.define_jfont",
192 cid_to_char, "ltj.otf.define_jfont", 1)
193 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
194 for fn, v in pairs(ltjf_font_metric_table) do
195 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
199 local function cid_set_char_class(arg, fmtable, char)
200 if arg~=0 then return arg
201 elseif fmtable.cid_char_type then
202 return fmtable.cid_char_type[char] or 0
206 luatexbase.add_to_callback("luatexja.find_char_class",
207 cid_set_char_class, "ltj.otf.find_char_class", 1)
209 -------------------- IVS
210 local enable_ivs, disable_ivs
212 local is_ivs_enabled = false
214 local function ivs_jglyph(char, bp, pf, uid)
215 local p = node_new(id_whatsit,sid_user)
216 setfield(p, 'user_id', uid)
217 setfield(p, 'type', 100)
218 setfield(p, 'value', char)
222 local function do_ivs_repr(h)
223 local head = to_direct(h)
225 local is_dir_tate = (ltjs.list_dir == dir_tate)
226 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
227 local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
228 local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
231 if pid==id_glyph then
232 local q = node_next(p) -- the next node of p
233 if q and getid(q)==id_glyph then
234 local qc = getchar(q)
235 if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
236 -- q is a variation selector
237 if qc>=0xE0100 then qc = qc - 0xE0100 end
238 local pf = getfont(p)
239 local pt = ltjf_font_extra_info[pf]
240 pt = pt and pt[getchar(p)]; pt = pt and pt[qc]
241 head, r = node_remove(head,q)
244 local is_jachar = (getfield(p, 'lang')==lang_ja)
245 local np = ivs_jglyph(pt, p, pf,
246 is_jachar and OTF or VSR)
248 set_attr(np, attr_curfnt, pf)
249 set_attr(np, attr_kblshift, has_attr(p, attr_kblshift))
251 head = node_insert_after(head, p, np)
252 head = node_remove(head,p)
269 enable_ivs = function ()
270 if is_ivs_enabled then
271 ltjb.package_warning('luatexja-otf',
272 'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
274 ltjb.add_to_callback('hpack_filter', do_ivs_repr, 'ltj.do_ivs',
275 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor'))
276 ltjb.add_to_callback('pre_linebreak_filter', do_ivs_repr, 'ltj.do_ivs',
277 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor'))
278 is_ivs_enabled = true
281 disable_ivs = function ()
282 if is_ivs_enabled then
283 luatexbase.remove_from_callback('hpack_filter', 'ltj.do_ivs')
284 luatexbase.remove_from_callback('pre_linebreak_filter', 'ltj.do_ivs')
285 is_ivs_enabled = false
291 append_jglyph = append_jglyph,
292 enable_ivs = enable_ivs, -- 隠し機能: IVS
293 disable_ivs = disable_ivs, -- 隠し機能: IVS