OSDN Git Service

fix "Command `\colon' already defined" error when fontspec is loaded without no-math...
[luatex-ja/luatexja.git] / src / ltj-otf.lua
1 --
2 -- luatexja/ltj-otf.lua
3 --
4 require('unicode')
5 require('lualibs')
6
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
13
14 local id_glyph = node.id('glyph')
15 local id_whatsit = node.id('whatsit')
16 local sid_user = node.subtype('user_defined')
17
18 local Dnode = node.direct or node
19
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
27
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
31
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
42
43
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
52
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
60
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
64
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 範囲外
68       return 0
69    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
70       return v
71    else
72       local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
73       -- must be non-nil!
74       local i = string.len(w)
75       if i==4 then -- UCS2
76          return tonumber(w,16)
77       elseif i==8 then
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)
81          else
82             return 0
83          end
84       end
85    end
86 end
87
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)
95    node_write(p)
96 end
97
98 local cid
99 do
100    local tex_get_attr = tex.getattribute
101    cid = function (key)
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))
114       end
115       local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
116       if not char then
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.')
122          char = 0
123       end
124       return append_jglyph(char)
125    end
126 end
127
128 local function extract(head)
129    head = to_direct(head)
130    local p = 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
135    while p do
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)
144                if puid==OTF then
145                   setfield(g, 'lang', lang_ja)
146                   set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
147                else
148                   set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
149                end
150                head = node_insert_after(head, p, g)
151                head = node_remove(head, p)
152                node_free(p); p = g
153             end
154          end
155       end
156       p = node_next(p)
157    end
158    return to_node(head)
159 end
160
161 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
162   luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
163 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
164   luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
165 -- additional callbacks
166 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
167 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
168 --   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
169 --   CID <-> グリフ 対応状況による変換テーブルが用意される.
170
171 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
172 local function cid_to_char(fmtable, fn)
173    local fi = identifiers[fn]
174    if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
175       for i, v in pairs(fmtable.chars) do
176          local j = string.match(i, "^AJ1%-([0-9]*)")
177          if j then
178             j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
179             if j then
180                fmtable.cid_char_type = fmtable.cid_char_type  or {}
181                fmtable.cid_char_type[j] = v
182             end
183          end
184       end
185    end
186    return fmtable
187 end
188 luatexbase.add_to_callback("luatexja.define_jfont",
189                            cid_to_char, "ltj.otf.define_jfont", 1)
190 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
191 for fn, v in pairs(ltjf_font_metric_table) do
192    ltjf_font_metric_table[fn] = cid_to_char(v, fn)
193 end
194
195
196 local function cid_set_char_class(arg, fmtable, char)
197    if arg~=0 then return arg
198    elseif fmtable.cid_char_type then
199       return fmtable.cid_char_type[char] or 0
200    else return 0
201    end
202 end
203 luatexbase.add_to_callback("luatexja.find_char_class",
204                            cid_set_char_class, "ltj.otf.find_char_class", 1)
205
206 -------------------- IVS
207 local enable_ivs, disable_ivs
208 do
209    local is_ivs_enabled = false
210 -- 組版時
211    local function ivs_jglyph(char, bp, pf, uid)
212       local p = node_new(id_whatsit,sid_user)
213       setfield(p, 'user_id', uid)
214       setfield(p, 'type', 100)
215       setfield(p, 'value', char)
216       return p
217    end
218
219    local function do_ivs_repr(h)
220       local head = to_direct(h)
221       local p, r = head
222       local is_dir_tate = (ltjs.list_dir == dir_tate)
223       local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
224       local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
225       local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
226       while p do
227          local pid = getid(p)
228          if pid==id_glyph then
229             local q = node_next(p) -- the next node of p
230             if q and getid(q)==id_glyph then
231                local qc = getchar(q)
232                if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
233                    -- q is a variation selector
234                   if qc>=0xE0100 then qc = qc - 0xE0100 end
235                   local pf = getfont(p)
236                   local pt = ltjf_font_extra_info[pf]
237                   pt = pt and pt[getchar(p)];  pt = pt and  pt[qc]
238                   head, r = node_remove(head,q)
239                   node_free(q)
240                   if pt then
241                      local is_jachar = (getfield(p, 'lang')==lang_ja)
242                      local np = ivs_jglyph(pt, p, pf,
243                                            is_jachar and OTF or VSR)
244                      if is_jachar then
245                         set_attr(np, attr_curfnt, pf)
246                         set_attr(np, attr_kblshift, has_attr(p, attr_kblshift))
247                      end
248                      head = node_insert_after(head, p, np)
249                      head = node_remove(head,p)
250                      node_free(p)
251                   end
252                   p = r
253                else
254                   p = q
255                end
256             else
257                p = node_next(p)
258             end
259          else
260             p = node_next(p)
261          end
262      end
263      return to_node(head)
264    end
265
266    enable_ivs = function ()
267       if is_ivs_enabled then
268          ltjb.package_warning('luatexja-otf',
269                               'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
270       else
271          ltjb.add_to_callback('hpack_filter', do_ivs_repr, 'ltj.do_ivs',
272             luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor'))
273          ltjb.add_to_callback('pre_linebreak_filter', do_ivs_repr, 'ltj.do_ivs',
274             luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor'))
275          is_ivs_enabled = true
276       end
277    end
278    disable_ivs = function ()
279       if is_ivs_enabled then
280          luatexbase.remove_from_callback('hpack_filter', 'ltj.do_ivs')
281          luatexbase.remove_from_callback('pre_linebreak_filter', 'ltj.do_ivs')
282          is_ivs_enabled = false
283       end
284    end
285 end
286
287 luatexja.otf = {
288   append_jglyph = append_jglyph,
289   enable_ivs = enable_ivs,  -- 隠し機能: IVS
290   disable_ivs = disable_ivs,  -- 隠し機能: IVS
291   cid = cid,
292 }
293
294 -- EOF