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 = token.create('ltj@@japanese')[2]
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)
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 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 luatexbase.add_to_callback('hpack_filter', extract,
163 'ltj.hpack_filter_otf',
164 luatexbase.priority_in_callback('pre_linebreak_filter',
165 'ltj.pre_linebreak_filter'))
166 luatexbase.add_to_callback('pre_linebreak_filter', extract,
167 'ltj.pre_linebreak_filter_otf',
168 luatexbase.priority_in_callback('pre_linebreak_filter',
169 'ltj.pre_linebreak_filter'))
172 -- additional callbacks
173 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
174 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
175 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
176 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
178 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
179 local function cid_to_char(fmtable, fn)
180 local fi = identifiers[fn]
181 if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
182 fmtable.cid_char_type = {}
183 for i, v in pairs(fmtable.chars) do
184 local j = string.match(i, "^AJ1%-([0-9]*)")
186 j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
188 fmtable.cid_char_type[j] = v
195 luatexbase.add_to_callback("luatexja.define_jfont",
196 cid_to_char, "ltj.otf.define_jfont", 1)
197 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
198 for fn, v in pairs(ltjf_font_metric_table) do
199 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
203 local function cid_set_char_class(arg, fmtable, char)
204 if arg~=0 then return arg
205 elseif fmtable.cid_char_type then
206 return fmtable.cid_char_type[char] or 0
210 luatexbase.add_to_callback("luatexja.find_char_class",
211 cid_set_char_class, "ltj.otf.find_char_class", 1)
213 -------------------- IVS
216 local is_ivs_enabled = false
218 local function ivs_jglyph(char, bp, pf, uid)
219 local p = node_new(id_whatsit,sid_user)
220 setfield(p, 'user_id', uid)
221 setfield(p, 'type', 100)
222 setfield(p, 'value', char)
226 local function do_ivs_repr(head)
227 head = to_direct(head)
229 local is_dir_tate = ltjs.list_dir == dir_tate
230 local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
231 local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
232 local attr_curfnt = is_dir_tate and attr_curtfnt or attr_curjfnt
235 if pid==id_glyph then
236 local pf = getfont(p)
237 local q = node_next(p) -- the next node of p
238 if q and getid(q)==id_glyph then
239 local qc = getchar(q)
240 if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
241 -- q is a variation selector
242 if qc>=0xE0100 then qc = qc - 0xE0100 end
243 local pt = ltjf_font_extra_info[pf]
244 pt = pt and pt[getchar(p)]; pt = pt and pt[qc]
245 head, r = node_remove(head,q)
248 local is_jachar = (getfield(p, 'lang')==lang_ja)
249 local np = ivs_jglyph(pt, p, pf,
250 is_jachar and OTF or VSR)
252 set_attr(np, attr_curfnt, pf)
253 set_attr(np, attr_kblshift, has_attr(p, attr_kblshift))
255 head = node_insert_after(head, p, np)
256 head = node_remove(head,p)
273 enable_ivs = function ()
274 if is_ivs_enabled then
275 ltjb.package_warning('luatexja-otf',
276 'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
278 luatexbase.add_to_callback('hpack_filter',
279 do_ivs_repr,'do_ivs',
280 luatexbase.priority_in_callback('hpack_filter',
281 'ltj.hpack_filter_pre')+1)
282 luatexbase.add_to_callback('pre_linebreak_filter',
283 do_ivs_repr, 'do_ivs',
284 luatexbase.priority_in_callback('pre_linebreak_filter',
285 'ltj.pre_linebreak_filter_pre')+1)
286 is_ivs_enabled = true
292 append_jglyph = append_jglyph,
293 enable_ivs = enable_ivs, -- 隠し機能: IVS