4 luatexbase.provides_module({
7 description = 'The OTF Lua module for LuaTeX-ja',
13 luatexja.load_module('base'); local ltjb = luatexja.base
14 luatexja.load_module('jfont'); local ltjf = luatexja.jfont
15 luatexja.load_module('rmlgbm'); local ltjr = luatexja.rmlgbm
16 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
18 local id_glyph = node.id('glyph')
19 local id_whatsit = node.id('whatsit')
20 local sid_user = node.subtype('user_defined')
22 local node_new = node.new
23 local node_remove = node.remove
24 local node_next = node.next
25 local node_free = node.free
26 local has_attr = node.has_attribute
27 local set_attr = node.set_attribute
28 local unset_attr = node.unset_attribute
29 local node_insert_after = node.insert_after
30 local identifiers = fonts.hashes.identifiers
32 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
33 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
34 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
35 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
37 local ltjf_font_metric_table = ltjf.font_metric_table
38 local ltjf_find_char_class = ltjf.find_char_class
39 local ltjr_cidfont_data = ltjr.cidfont_data
40 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
42 local OTF = luatexja.userid_table.OTF
44 local function get_ucs_from_rmlgbm(c)
45 local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
46 if not v then -- AJ1 範囲外
48 elseif v<0xF0000 then -- 素直に Unicode にマップ可能
51 local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
53 local i = string.len(w)
57 i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
58 if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
59 return (w-0xD800)*0x400 + (i-0xDC00)
67 -- Append a whatsit node to the list.
68 -- This whatsit node will be extracted to a glyph_node
69 local function append_jglyph(char)
70 local p = node_new(id_whatsit,sid_user)
71 local v = tex.attribute[attr_curjfnt]
72 p.user_id=OTF; p.type=100; p.value=char
73 set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
77 local function cid(key)
78 if key==0 then return append_jglyph(char) end
79 local curjfnt = identifiers[tex.attribute[attr_curjfnt]]
80 if not curjfnt.cidinfo or
81 curjfnt.cidinfo.ordering ~= "Japan1" and
82 curjfnt.cidinfo.ordering ~= "GB1" and
83 curjfnt.cidinfo.ordering ~= "CNS1" and
84 curjfnt.cidinfo.ordering ~= "Korea1" then
85 -- ltjb.package_warning('luatexja-otf',
86 -- 'Current Japanese font (or other CJK font) "'
87 -- ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
88 return append_jglyph(get_ucs_from_rmlgbm(key))
90 local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
92 ltjb.package_warning('luatexja-otf',
93 'Current Japanese font (or other CJK font) "'
94 ..curjfnt.psname..'" does not have the specified CID character ('
96 'Use a font including the specified CID character.')
99 return append_jglyph(char)
102 local function extract(head)
106 if p.id==id_whatsit then
107 if p.subtype==sid_user and p.user_id==OTF then
108 local g = node_new(id_glyph)
109 g.subtype = 0; g.char = p.value
110 v = has_attr(p, attr_curjfnt); g.font = v
111 set_attr(g, attr_curjfnt, v)
112 v = has_attr(p, attr_yablshift)
114 set_attr(g, attr_yablshift, v)
116 unset_attr(g, attr_yablshift)
118 head = node_insert_after(head, p, g)
119 head = node_remove(head, p)
128 luatexbase.add_to_callback('hpack_filter',
129 function (head) return extract(head) end,'ltj.hpack_filter_otf',
130 luatexbase.priority_in_callback('pre_linebreak_filter',
131 'ltj.pre_linebreak_filter'))
132 luatexbase.add_to_callback('pre_linebreak_filter',
133 function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
134 luatexbase.priority_in_callback('pre_linebreak_filter',
135 'ltj.pre_linebreak_filter'))
138 -- additional callbacks
139 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
140 -- JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
141 -- これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
142 -- CID <-> グリフ 対応状況による変換テーブルが用意される.
144 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
145 local function cid_to_char(fmtable, fn)
146 local fi = identifiers[fn]
147 if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
148 fmtable.cid_char_type = {}
149 for i, v in pairs(fmtable.chars) do
150 local j = string.match(i, "^AJ1%-([0-9]*)")
152 j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
154 fmtable.cid_char_type[j] = v
161 luatexbase.add_to_callback("luatexja.define_jfont",
162 cid_to_char, "ltj.otf.define_jfont", 1)
163 -- 既に読み込まれているフォントに対しても,同じことをやらないといけない
164 for fn, v in pairs(ltjf_font_metric_table) do
165 ltjf_font_metric_table[fn] = cid_to_char(v, fn)
169 local function cid_set_char_class(arg, fmtable, char)
170 if arg~=0 then return arg
171 elseif fmtable.cid_char_type then
172 return fmtable.cid_char_type[char] or 0
176 luatexbase.add_to_callback("luatexja.find_char_class",
177 cid_set_char_class, "ltj.otf.find_char_class", 1)
179 -------------------- IVS
181 local get_node_font = function(p)
182 return (ltjc_is_ucs_in_japanese_char(p) and (has_attr(p, attr_curjfnt) or 0) or p.font)
184 local get_current_font = function() return tex.attribute[attr_curjfnt] or 0 end
185 local function do_ivs_repr(head)
189 if pid==id_glyph then
190 local pf = get_node_font(p)
191 local pt = identifiers[pf]
192 pt = pt and pt.resources; pt = pt and pt.variants
194 local q = node_next(p) -- the next node of p
195 if q and q.id==id_glyph then
197 if qc>=0xE0100 and qc<0xE01F0 then -- q is an IVS selector
198 pt = pt[qc]; pt = pt and pt[p.char]
200 p.char = pt or p.char
202 head = node_remove(head,q)
211 function enable_ivs()
212 luatexbase.add_to_callback('hpack_filter',
213 function (head) return do_ivs_repr(head) end,'do_ivs', 1)
214 luatexbase.add_to_callback('pre_linebreak_filter',
215 function (head) return do_ivs_repr(head) end, 'do_ivs', 1)
220 -------------------- all done
222 append_jglyph = append_jglyph,
223 enable_ivs = enable_ivs, -- 隠し機能: IVS(TTF しか現状では使えない)