4 luatexbase.provides_module({
7 description = 'The OTF Lua module for LuaTeX-ja',
14 luatexja.load_module('base'); local ltjb = luatexja.base
15 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
16 luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
17 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
19 local id_glyph = node.id('glyph')
20 local id_whatsit = node.id('whatsit')
21 local sid_user = node.subtype('user_defined')
23 local node_new = node.new
24 local node_remove = node.remove
25 local node_next = node.next
26 local node_free = node.free
27 local has_attr = node.has_attribute
28 local set_attr = node.set_attribute
29 local unset_attr = node.unset_attribute
30 local node_insert_after = node.insert_after
31 local identifiers = fonts.hashes.identifiers
33 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
34 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
35 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
36 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
38 local ltjf_font_metric_table = ltjf.font_metric_table
39 local ltjf_find_char_class = ltjf.find_char_class
40 local ltjr_cidfont_data = ltjr.cidfont_data
41 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
43 local OTF = luatexja.userid_table.OTF
45 local function get_ucs_from_rmlgbm(c)
46 local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
47 if not v then -- AJ1 範囲外
49 elseif v<0xF0000 then -- 素直に Unicode にマップ可能
52 local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
54 local i = string.len(w)
58 i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
59 if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
60 return (w-0xD800)*0x400 + (i-0xDC00)
68 -- Append a whatsit node to the list.
69 -- This whatsit node will be extracted to a glyph_node
70 local function append_jglyph(char)
71 local p = node_new(id_whatsit,sid_user)
72 local v = tex.attribute[attr_curjfnt]
73 p.user_id=OTF; p.type=100; p.value=char
74 set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
78 local function cid(key)
79 if key==0 then return append_jglyph(char) end
80 local curjfnt = identifiers[tex.attribute[attr_curjfnt]]
81 if not curjfnt.cidinfo or
82 curjfnt.cidinfo.ordering ~= "Japan1" and
83 curjfnt.cidinfo.ordering ~= "GB1" and
84 curjfnt.cidinfo.ordering ~= "CNS1" and
85 curjfnt.cidinfo.ordering ~= "Korea1" then
86 -- ltjb.package_warning('luatexja-otf',
87 -- 'Current Japanese font (or other CJK font) "'
88 -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
89 return append_jglyph(get_ucs_from_rmlgbm(key))
91 local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
93 ltjb.package_warning('luatexja-otf',
94 'Current Japanese font (or other CJK font) "'
95 ..curjfnt.psname..'" does not have the specified CID character ('
97 'Use a font including the specified CID character.')
100 return append_jglyph(char)
103 local function extract(head)
107 if p.id==id_whatsit then
108 if p.subtype==sid_user and p.user_id==OTF then
109 local g = node_new(id_glyph)
110 g.subtype = 0; g.char = p.value
111 v = has_attr(p, attr_curjfnt); g.font = v
112 set_attr(g, attr_curjfnt, v)
113 v = has_attr(p, attr_yablshift)
115 set_attr(g, attr_yablshift, v)
117 unset_attr(g, attr_yablshift)
119 head = node_insert_after(head, p, g)
120 head = node_remove(head, p)
129 luatexbase.add_to_callback('hpack_filter',
130 function (head) return extract(head) end,'ltj.hpack_filter_otf',
131 luatexbase.priority_in_callback('pre_linebreak_filter',
132 'ltj.pre_linebreak_filter'))
133 luatexbase.add_to_callback('pre_linebreak_filter',
134 function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
135 luatexbase.priority_in_callback('pre_linebreak_filter',
136 'ltj.pre_linebreak_filter'))
139 -- additional callbacks
140 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
141 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
142 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
143 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
145 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
146 local function cid_to_char(fmtable, fn)
147 local fi = identifiers[fn]
148 if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
149 fmtable.cid_char_type = {}
150 for i, v in pairs(fmtable.chars) do
151 local j = string.match(i, "^AJ1%-([0-9]*)")
153 j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
155 fmtable.cid_char_type[j] = v
162 luatexbase.add_to_callback("luatexja.define_jfont",
163 cid_to_char, "ltj.otf.define_jfont", 1)
164 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
165 for fn, v in pairs(ltjf_font_metric_table) do
166 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
170 local function cid_set_char_class(arg, fmtable, char)
171 if arg~=0 then return arg
172 elseif fmtable.cid_char_type then
173 return fmtable.cid_char_type[char] or 0
177 luatexbase.add_to_callback("luatexja.find_char_class",
178 cid_set_char_class, "ltj.otf.find_char_class", 1)
180 -------------------- IVS
181 local font_ivs_table = {} -- key: fontnumber
185 local sort = table.sort
187 local function add_ivs_table(tg, unitable)
188 for gu, gv in pairs(tg) do
191 for _,at in pairs(ga) do
192 local bu, vsel = at.unicode, (at.variant or -1)
194 if not ivs[bu] then ivs[bu] = {} end
196 for i,_ in pairs(ivs[bu]) do
197 if i==vs then uniq_flag = false; break end
200 ivs[bu][vsel] = unitable[gv.name]
207 local function make_ivs_table(id, fname)
209 local fl = fontloader.open(fname)
210 local ft = fontloader.to_table(fl)
211 local unicodes = id.resources.unicodes
212 add_ivs_table(ft.glyphs, id.resources.unicodes)
214 for _,v in pairs(ft.subfonts) do
215 add_ivs_table(v.glyphs, id.resources.unicodes)
222 -- loading and saving
223 local font_ivs_basename = {} -- key: basename
224 local cache_ver = '3'
225 local checksum = file.checksum
227 local function prepare_ivs_data(n, id)
228 -- test if already loaded
229 if type(id)=='number' then
230 font_ivs_table[n] = font_ivs_table[id]; return
232 local fname = id.filename
233 local bname = file.basename(fname)
235 font_ivs_table[n] = {}; return
236 elseif font_ivs_basename[bname] then
237 font_ivs_table[n] = font_ivs_basename[bname]; return
240 -- if cache is present; read them
241 local newsum = checksum(fname)
242 local v = "ivs_" .. string.lower(file.nameonly(fname))
243 local dat = ltjb.load_cache(v,
244 function (t) return (t.version~=cache_ver) or (t.chksum~=newsum) end
246 -- if cache is not found or outdated, save the cache
248 font_ivs_basename[bname] = dat[1] or {}
250 dat = make_ivs_table(id, fname)
251 font_ivs_basename[bname] = dat or {}
254 chksum = checksum(fname),
259 font_ivs_table[n] = font_ivs_basename[bname]
263 local function ivs_jglyph(char, bp, pf)
264 local p = node_new(id_whatsit,sid_user)
265 p.user_id=OTF; p.type=100; p.value=char
266 set_attr(p, attr_curjfnt, pf)
267 set_attr(p, attr_yablshift, has_attr(bp, attr_ykblshift) or 0)
271 local function do_ivs_repr(head)
275 if pid==id_glyph then
277 if (has_attr(p, attr_curjfnt) or 0) == pf then
278 -- only works with JAchars
279 local pt = font_ivs_table[pf]
280 local q = node_next(p) -- the next node of p
281 if q and q.id==id_glyph then
283 if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
284 -- q is a variation selector
285 pt = pt and pt[p.char]; pt = pt and pt[qc]
286 head = node_remove(head,q)
288 local np = ivs_jglyph(pt, p, pf)
289 head = node_insert_after(head, p, np)
290 head = node_remove(head,p)
303 local function font_callback(name, size, id, fallback)
304 local d = fallback(name, size, id)
305 prepare_ivs_data(id, d)
309 enable_ivs = function ()
310 luatexbase.add_to_callback('hpack_filter',
311 function (head) return do_ivs_repr(head) end,'do_ivs', 1)
312 luatexbase.add_to_callback('pre_linebreak_filter',
313 function (head) return do_ivs_repr(head) end, 'do_ivs', 1)
314 local ivs_callback = function (name, size, id)
315 return font_callback(
317 function (name, size, id) return luatexja.font_callback(name, size, id) end
320 luatexbase.add_to_callback('define_font',ivs_callback,"luatexja.ivs_font_callback", 1)
321 for i=1,font.nextid()-1 do
322 if identifiers[i] then prepare_ivs_data(i, identifiers[i]) end
327 -------------------- all done
329 append_jglyph = append_jglyph,
330 enable_ivs = enable_ivs, -- 隠し機能: IVS
331 font_ivs_table = font_ivs_table,