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 = identifiers[tex_get_attr((ltjd_get_dir_count()==dir_tate)
100 and attr_curtfnt or attr_curjfnt)]
101 if not curjfnt.cidinfo or
102 curjfnt.cidinfo.ordering ~= "Japan1" and
103 curjfnt.cidinfo.ordering ~= "GB1" and
104 curjfnt.cidinfo.ordering ~= "CNS1" and
105 curjfnt.cidinfo.ordering ~= "Korea1" then
106 -- ltjb.package_warning('luatexja-otf',
107 -- 'Current Japanese font (or other CJK font) "'
108 -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
109 return append_jglyph(get_ucs_from_rmlgbm(key))
111 local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
113 ltjb.package_warning('luatexja-otf',
114 'Current Japanese font (or other CJK font) "'
115 ..curjfnt.psname..'" does not have the specified CID character ('
116 ..tostring(key)..')',
117 'Use a font including the specified CID character.')
120 return append_jglyph(char)
124 local function extract(head)
125 head = to_direct(head)
127 local is_dir_tate = ltjs.list_dir == dir_tate
128 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
129 local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
130 local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
132 if getid(p)==id_whatsit then
133 if getsubtype(p)==sid_user then
134 local puid = getfield(p, 'user_id')
135 if puid==OTF or puid==VSR then
136 local g = node_new(id_glyph)
137 setfield(g, 'subtype', 0)
138 setfield(g, 'char', getfield(p, 'value'))
139 local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
141 setfield(g, 'lang', lang_ja)
142 set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
144 set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
146 head = node_insert_after(head, p, g)
147 head = node_remove(head, p)
157 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
158 luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
159 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
160 luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
161 -- additional callbacks
162 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
163 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
164 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
165 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
167 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
168 local function cid_to_char(fmtable, fn)
169 local fi = identifiers[fn]
170 if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
171 for i, v in pairs(fmtable.chars) do
172 local j = string.match(i, "^AJ1%-([0-9]*)")
174 j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
176 fmtable.cid_char_type = fmtable.cid_char_type or {}
177 fmtable.cid_char_type[j] = v
184 luatexbase.add_to_callback("luatexja.define_jfont",
185 cid_to_char, "ltj.otf.define_jfont", 1)
186 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
187 for fn, v in pairs(ltjf_font_metric_table) do
188 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
192 local function cid_set_char_class(arg, fmtable, char)
193 if arg~=0 then return arg
194 elseif fmtable.cid_char_type then
195 return fmtable.cid_char_type[char] or 0
199 luatexbase.add_to_callback("luatexja.find_char_class",
200 cid_set_char_class, "ltj.otf.find_char_class", 1)
202 -------------------- IVS
203 local enable_ivs, disable_ivs
205 local is_ivs_enabled = false
207 local function ivs_jglyph(char, bp, pf, uid)
208 local p = node_new(id_whatsit,sid_user)
209 setfield(p, 'user_id', uid)
210 setfield(p, 'type', 100)
211 setfield(p, 'value', char)
215 local function do_ivs_repr(h)
216 local head = to_direct(h)
218 local is_dir_tate = (ltjs.list_dir == dir_tate)
219 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
220 local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
221 local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
224 if pid==id_glyph then
225 local q = node_next(p) -- the next node of p
226 if q and getid(q)==id_glyph then
227 local qc = getchar(q)
228 if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
229 -- q is a variation selector
230 if qc>=0xE0100 then qc = qc - 0xE0100 end
231 local pf = getfont(p)
232 local pt = ltjf_font_extra_info[pf]
233 pt = pt and pt[getchar(p)]; pt = pt and pt[qc]
234 head, r = node_remove(head,q)
237 local is_jachar = (getfield(p, 'lang')==lang_ja)
238 local np = ivs_jglyph(pt, p, pf,
239 is_jachar and OTF or VSR)
241 set_attr(np, attr_curfnt, pf)
242 set_attr(np, attr_kblshift, has_attr(p, attr_kblshift))
244 head = node_insert_after(head, p, np)
245 head = node_remove(head,p)
262 enable_ivs = function ()
263 if is_ivs_enabled then
264 ltjb.package_warning('luatexja-otf',
265 'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
267 ltjb.add_to_callback('hpack_filter', do_ivs_repr, 'ltj.do_ivs',
268 luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor'))
269 ltjb.add_to_callback('pre_linebreak_filter', do_ivs_repr, 'ltj.do_ivs',
270 luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor'))
271 is_ivs_enabled = true
274 disable_ivs = function ()
275 if is_ivs_enabled then
276 luatexbase.remove_from_callback('hpack_filter', 'ltj.do_ivs')
277 luatexbase.remove_from_callback('pre_linebreak_filter', 'ltj.do_ivs')
278 is_ivs_enabled = false
284 append_jglyph = append_jglyph,
285 enable_ivs = enable_ivs, -- 隠し機能: IVS
286 disable_ivs = disable_ivs, -- 隠し機能: IVS