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 Dnode = node.direct or node
20 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
21 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
22 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
23 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
24 --local getlist = (Dnode ~= node) and Dnode.getlist or function(n) return n.head end
25 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
26 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
28 local nullfunc = function(n) return n end
29 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
30 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
32 local node_new = Dnode.new
33 local node_remove = Dnode.remove
34 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
35 local node_free = Dnode.free
36 local has_attr = Dnode.has_attribute
37 local set_attr = Dnode.set_attribute
38 local unset_attr = Dnode.unset_attribute
39 local node_insert_after = Dnode.insert_after
40 local node_write = Dnode.write
41 local node_traverse_id = Dnode.traverse_id
44 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
45 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
46 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
47 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
48 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
49 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
50 local lang_ja = luatexja.lang_ja
51 local identifiers = fonts.hashes.identifiers
53 local ltjf_font_metric_table = ltjf.font_metric_table
54 local ltjf_font_extra_info = ltjf.font_extra_info
55 local ltjf_find_char_class = ltjf.find_char_class
56 local ltjr_cidfont_data = ltjr.cidfont_data
57 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
58 local ltjd_get_dir_count = ltjd.get_dir_count
59 local dir_tate = luatexja.dir_table.dir_tate
61 luatexja.userid_table.OTF = luatexbase.newuserwhatsitid('char_by_cid', 'luatexja')
62 luatexja.userid_table.VSR = luatexbase.newuserwhatsitid('replace_vs', 'luatexja')
63 local OTF, VSR = luatexja.userid_table.OTF, luatexja.userid_table.VSR
65 local function get_ucs_from_rmlgbm(c)
66 local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
67 if not v then -- AJ1 範囲外
69 elseif v<0xF0000 then -- 素直に Unicode にマップ可能
72 local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
74 local i = string.len(w)
78 i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
79 if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
80 return (w-0xD800)*0x400 + (i-0xDC00)
88 -- Append a whatsit node to the list.
89 -- This whatsit node will be extracted to a glyph_node
90 local function append_jglyph(char)
91 local p = node_new(id_whatsit,sid_user)
92 setfield(p, 'user_id', OTF)
93 setfield(p, 'type', 100)
94 setfield(p, 'value', char)
100 local tex_get_attr = tex.getattribute
102 if key==0 then return append_jglyph(char) end
103 local curjfnt = identifiers[tex_get_attr((ltjd_get_dir_count()==dir_tate)
104 and attr_curtfnt or attr_curjfnt)]
105 if not curjfnt.cidinfo or
106 curjfnt.cidinfo.ordering ~= "Japan1" and
107 curjfnt.cidinfo.ordering ~= "GB1" and
108 curjfnt.cidinfo.ordering ~= "CNS1" and
109 curjfnt.cidinfo.ordering ~= "Korea1" then
110 -- ltjb.package_warning('luatexja-otf',
111 -- 'Current Japanese font (or other CJK font) "'
112 -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
113 return append_jglyph(get_ucs_from_rmlgbm(key))
115 local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
117 ltjb.package_warning('luatexja-otf',
118 'Current Japanese font (or other CJK font) "'
119 ..curjfnt.psname..'" does not have the specified CID character ('
120 ..tostring(key)..')',
121 'Use a font including the specified CID character.')
124 return append_jglyph(char)
128 local function extract(head)
129 head = to_direct(head)
131 local is_dir_tate = ltjs.list_dir == dir_tate
132 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
133 local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
134 local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
136 if getid(p)==id_whatsit then
137 if getsubtype(p)==sid_user then
138 local puid = getfield(p, 'user_id')
139 if puid==OTF or puid==VSR then
140 local g = node_new(id_glyph)
141 setfield(g, 'subtype', 0)
142 setfield(g, 'char', getfield(p, 'value'))
143 local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
145 setfield(g, 'lang', lang_ja)
146 set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
148 set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
150 head = node_insert_after(head, p, g)
151 head = node_remove(head, p)
161 luatexbase.add_to_callback('hpack_filter', extract,
162 'ltj.hpack_filter_otf',
163 luatexbase.priority_in_callback('pre_linebreak_filter',
164 'ltj.pre_linebreak_filter'))
165 luatexbase.add_to_callback('pre_linebreak_filter', extract,
166 'ltj.pre_linebreak_filter_otf',
167 luatexbase.priority_in_callback('pre_linebreak_filter',
168 'ltj.pre_linebreak_filter'))
171 -- additional callbacks
172 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
173 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
174 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
175 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
177 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
178 local function cid_to_char(fmtable, fn)
179 local fi = identifiers[fn]
180 if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
181 for i, v in pairs(fmtable.chars) do
182 local j = string.match(i, "^AJ1%-([0-9]*)")
184 j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
186 fmtable.cid_char_type = fmtable.cid_char_type or {}
187 fmtable.cid_char_type[j] = v
194 luatexbase.add_to_callback("luatexja.define_jfont",
195 cid_to_char, "ltj.otf.define_jfont", 1)
196 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
197 for fn, v in pairs(ltjf_font_metric_table) do
198 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
202 local function cid_set_char_class(arg, fmtable, char)
203 if arg~=0 then return arg
204 elseif fmtable.cid_char_type then
205 return fmtable.cid_char_type[char] or 0
209 luatexbase.add_to_callback("luatexja.find_char_class",
210 cid_set_char_class, "ltj.otf.find_char_class", 1)
212 -------------------- IVS
215 local is_ivs_enabled = false
217 local function ivs_jglyph(char, bp, pf, uid)
218 local p = node_new(id_whatsit,sid_user)
219 setfield(p, 'user_id', uid)
220 setfield(p, 'type', 100)
221 setfield(p, 'value', char)
225 local function do_ivs_repr(head)
226 head = to_direct(head)
228 local is_dir_tate = ltjs.list_dir == dir_tate
229 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
230 local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
231 local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
234 if pid==id_glyph then
235 local pf = getfont(p)
236 local q = node_next(p) -- the next node of p
237 if q and getid(q)==id_glyph then
238 local qc = getchar(q)
239 if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
240 -- q is a variation selector
241 if qc>=0xE0100 then qc = qc - 0xE0100 end
242 local pt = ltjf_font_extra_info[pf]
243 pt = pt and pt[getchar(p)]; pt = pt and pt[qc]
244 head, r = node_remove(head,q)
247 local is_jachar = (getfield(p, 'lang')==lang_ja)
248 local np = ivs_jglyph(pt, p, pf,
249 is_jachar and OTF or VSR)
251 set_attr(np, attr_curfnt, pf)
252 set_attr(np, attr_kblshift, has_attr(p, attr_kblshift))
254 head = node_insert_after(head, p, np)
255 head = node_remove(head,p)
272 enable_ivs = function ()
273 if is_ivs_enabled then
274 ltjb.package_warning('luatexja-otf',
275 'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
277 luatexbase.add_to_callback('hpack_filter',
278 do_ivs_repr,'do_ivs',
279 luatexbase.priority_in_callback('hpack_filter',
280 'ltj.hpack_filter_pre')+1)
281 luatexbase.add_to_callback('pre_linebreak_filter',
282 do_ivs_repr, 'do_ivs',
283 luatexbase.priority_in_callback('pre_linebreak_filter',
284 'ltj.pre_linebreak_filter_pre')+1)
285 is_ivs_enabled = true
291 append_jglyph = append_jglyph,
292 enable_ivs = enable_ivs, -- 隠し機能: IVS