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 = luatexja.Dnode_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
43 local identifiers = fonts.hashes.identifiers
45 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
46 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
47 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
48 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
49 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
50 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
51 local lang_ja = token.create('ltj@@japanese')[2]
53 local ltjf_font_metric_table = ltjf.font_metric_table
54 local ltjf_find_char_class = ltjf.find_char_class
55 local ltjr_cidfont_data = ltjr.cidfont_data
56 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
57 local ltjd_get_dir_count = ltjd.get_dir_count
59 luatexja.userid_table.OTF = luatexbase.newuserwhatsitid('char_by_cid', 'luatexja')
60 luatexja.userid_table.VSR = luatexbase.newuserwhatsitid('replace_vs', 'luatexja')
61 local OTF, VSR = luatexja.userid_table.OTF, luatexja.userid_table.VSR
63 local function get_ucs_from_rmlgbm(c)
64 local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
65 if not v then -- AJ1 範囲外
67 elseif v<0xF0000 then -- 素直に Unicode にマップ可能
70 local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
72 local i = string.len(w)
76 i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
77 if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
78 return (w-0xD800)*0x400 + (i-0xDC00)
86 -- Append a whatsit node to the list.
87 -- This whatsit node will be extracted to a glyph_node
88 local function append_jglyph(char)
89 local p = node_new(id_whatsit,sid_user)
90 setfield(p, 'user_id', OTF)
91 setfield(p, 'type', 100)
92 setfield(p, 'value', char)
98 local dir_tate = luatexja.dir_table.dir_tate
99 local tex_get_attr = tex.getattribute
101 if key==0 then return append_jglyph(char) end
102 local curjfnt = identifiers[tex_get_attr((ltjd_get_dir_count()==dir_tate)
103 and attr_curtfnt or attr_curjfnt)]
104 if not curjfnt.cidinfo or
105 curjfnt.cidinfo.ordering ~= "Japan1" and
106 curjfnt.cidinfo.ordering ~= "GB1" and
107 curjfnt.cidinfo.ordering ~= "CNS1" and
108 curjfnt.cidinfo.ordering ~= "Korea1" then
109 -- ltjb.package_warning('luatexja-otf',
110 -- 'Current Japanese font (or other CJK font) "'
111 -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
112 return append_jglyph(get_ucs_from_rmlgbm(key))
114 local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
116 ltjb.package_warning('luatexja-otf',
117 'Current Japanese font (or other CJK font) "'
118 ..curjfnt.psname..'" does not have the specified CID character ('
119 ..tostring(key)..')',
120 'Use a font including the specified CID character.')
123 return append_jglyph(char)
127 local function extract(head)
128 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 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 fmtable.cid_char_type = {}
182 for i, v in pairs(fmtable.chars) do
183 local j = string.match(i, "^AJ1%-([0-9]*)")
185 j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
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
213 local font_ivs_table = {} -- key: fontnumber
216 local is_ivs_enabled = false
217 local ivs -- temp table
218 local sort = table.sort
220 local function add_ivs_table(tg, unitable, glyphmax)
221 for i = 0, glyphmax-1 do
225 for _,at in pairs(gv.altuni) do
226 local bu, vsel = at.unicode, at.variant
228 if vsel>=0xE0100 then vsel = vsel - 0xE0100 end
229 if not ivs[bu] then ivs[bu] = {} end
231 for i,_ in pairs(ivs[bu]) do
232 if i==vs then uniq_flag = false; break end
235 ivs[bu][vsel] = unitable[gv.name]
243 local function make_ivs_table(id, fname)
245 local fl = fontloader.open(fname)
246 local unicodes = id.resources.unicodes
248 add_ivs_table(fl.glyphs, id.resources.unicodes, fl.glyphmax)
251 for _,v in pairs(fl.subfonts) do
252 add_ivs_table(v.glyphs, id.resources.unicodes, v.glyphmax)
259 -- loading and saving
260 local font_ivs_basename = {} -- key: basename
262 local checksum = file.checksum
264 local function prepare_ivs_data(n, id)
265 -- test if already loaded
266 if type(id)=='number' then -- sometimes id is an integer
267 font_ivs_table[n] = font_ivs_table[id]; return
268 elseif not id then return
270 local fname = id.filename
271 local bname = file.basename(fname)
273 font_ivs_table[n] = {}; return
274 elseif font_ivs_basename[bname] then
275 font_ivs_table[n] = font_ivs_basename[bname]; return
278 -- if the cache is present, read it
279 local newsum = checksum(fname) -- MD5 checksum of the fontfile
280 local v = "ivs_" .. string.lower(file.nameonly(fname))
281 local dat = ltjb.load_cache(v,
282 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
284 -- if the cache is not found or outdated, save the cache
286 font_ivs_basename[bname] = dat[1] or {}
288 dat = make_ivs_table(id, fname)
289 font_ivs_basename[bname] = dat or {}
292 chksum = checksum(fname),
297 font_ivs_table[n] = font_ivs_basename[bname]
301 local function ivs_jglyph(char, bp, pf, uid)
302 local p = node_new(id_whatsit,sid_user)
303 setfield(p, 'user_id', uid)
304 setfield(p, 'type', 100)
305 setfield(p, 'value', char)
309 local function do_ivs_repr(head)
310 head = to_direct(head)
314 if pid==id_glyph then
315 local pf = getfont(p)
316 local q = node_next(p) -- the next node of p
317 if q and getid(q)==id_glyph then
318 local qc = getchar(q)
319 if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
320 -- q is a variation selector
321 if qc>=0xE0100 then qc = qc - 0xE0100 end
322 local pt = font_ivs_table[pf]
323 pt = pt and pt[getchar(p)]; pt = pt and pt[qc]
324 head, r = node_remove(head,q)
327 local np = ivs_jglyph(pt, p, pf,
328 (getfield(p, 'lang') or 0)==lang_ja and OTF or VSR)
329 head = node_insert_after(head, p, np)
330 head = node_remove(head,p)
347 enable_ivs = function ()
348 if is_ivs_enabled then
349 ltjb.package_warning('luatexja-otf',
350 'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
352 luatexbase.add_to_callback('hpack_filter',
353 do_ivs_repr,'do_ivs', 1)
354 luatexbase.add_to_callback('pre_linebreak_filter',
355 do_ivs_repr, 'do_ivs', 1)
356 local ivs_callback = function (name, size, id)
357 return font_callback(
359 function (name, size, id) return luatexja.font_callback(name, size, id) end
362 luatexbase.add_to_callback('luatexja.define_font',
363 function (res, name, size, id)
364 prepare_ivs_data(id, res)
366 'prepare_ivs_data', 1)
367 for i=1,font.nextid()-1 do
368 if identifiers[i] then prepare_ivs_data(i, identifiers[i]) end
370 is_ivs_enabled = true
376 append_jglyph = append_jglyph,
377 enable_ivs = enable_ivs, -- 隠し機能: IVS
378 font_ivs_table = font_ivs_table,