OSDN Git Service

fix ticket #36934
[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 setfield = node.direct.setfield
19 local getfield = node.direct.getfield
20 local getid = node.direct.getid
21 local getfont = node.direct.getfont
22 local getchar = node.direct.getchar
23 local getsubtype = node.direct.getsubtype
24
25 local to_node = node.direct.tonode
26 local to_direct = node.direct.todirect
27
28 local node_new = node.direct.new
29 local node_remove = node.direct.remove
30 local node_next = node.direct.getnext
31 local node_free = node.direct.free
32 local has_attr = node.direct.has_attribute
33 local set_attr = node.direct.set_attribute
34 local unset_attr = node.direct.unset_attribute
35 local node_insert_after = node.direct.insert_after
36 local node_write = node.direct.write
37 local node_traverse_id = node.direct.traverse_id
38
39
40 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
41 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
42 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
43 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
44 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
45 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
46 local lang_ja = luatexja.lang_ja
47 local identifiers = fonts.hashes.identifiers
48
49 local ltjf_font_metric_table = ltjf.font_metric_table
50 local ltjf_font_extra_info = ltjf.font_extra_info
51 local ltjf_find_char_class = ltjf.find_char_class
52 local ltjr_cidfont_data = ltjr.cidfont_data
53 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
54 local ltjd_get_dir_count = ltjd.get_dir_count
55 local dir_tate = luatexja.dir_table.dir_tate
56
57 luatexja.userid_table.OTF = luatexbase.newuserwhatsitid('char_by_cid',  'luatexja')
58 luatexja.userid_table.VSR = luatexbase.newuserwhatsitid('replace_vs',  'luatexja')
59 local OTF, VSR = luatexja.userid_table.OTF, luatexja.userid_table.VSR
60
61 local function get_ucs_from_rmlgbm(c)
62    local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
63    if not v then -- AJ1 範囲外
64       return 0
65    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
66       return v
67    else
68       local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
69       -- must be non-nil!
70       local i = string.len(w)
71       if i==4 then -- UCS2
72          return tonumber(w,16)
73       elseif i==8 then
74          i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
75          if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
76             return (w-0xD800)*0x400 + (i-0xDC00)
77          else
78             return 0
79          end
80       end
81    end
82 end
83
84 -- Append a whatsit node to the list.
85 -- This whatsit node will be extracted to a glyph_node
86 local function append_jglyph(char)
87    local p = node_new(id_whatsit,sid_user)
88    setfield(p, 'user_id', OTF)
89    setfield(p, 'type', 100)
90    setfield(p, 'value', char)
91    node_write(p)
92 end
93
94 local cid
95 do
96    local tex_get_attr = tex.getattribute
97    cid = function (key)
98       if key==0 then return append_jglyph(char) end
99       local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
100                                         and attr_curtfnt or attr_curjfnt)
101       local curjfnt = identifiers[curjfnt_num]
102       local cidinfo = curjfnt.resources.cidinfo
103       if not cidinfo or
104          cidinfo.ordering ~= "Japan1" and
105          cidinfo.ordering ~= "GB1" and
106          cidinfo.ordering ~= "CNS1" and
107          cidinfo.ordering ~= "Korea1" then
108          --      ltjb.package_warning('luatexja-otf',
109          --                        'Current Japanese font (or other CJK font) "'
110          --                           ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
111             return append_jglyph(get_ucs_from_rmlgbm(key))
112       end
113       local fe, char = ltjf_font_extra_info[curjfnt_num], nil
114       if fe and fe.unicodes then 
115          char = fe.unicodes[cidinfo.ordering..'.'..tostring(key)]
116       end
117       if not char then
118          ltjb.package_warning('luatexja-otf',
119                               'Current Japanese font (or other CJK font) "'
120                                  ..curjfnt.psname..'" does not have the specified CID character ('
121                                  ..tostring(key)..')',
122                               'Use a font including the specified CID character.')
123          char = 0
124       end
125       return append_jglyph(char)
126    end
127 end
128
129 local function extract(head)
130    head = to_direct(head)
131    local p = 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
136    while p do
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                local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
145                if puid==OTF then
146                   setfield(g, 'lang', lang_ja)
147                   set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
148                else
149                   set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
150                end
151                head = node_insert_after(head, p, g)
152                head = node_remove(head, p)
153                node_free(p); p = g
154             end
155          end
156       end
157       p = node_next(p)
158    end
159    return to_node(head)
160 end
161
162 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
163   luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
164 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
165   luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
166 -- additional callbacks
167 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
168 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
169 --   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
170 --   CID <-> グリフ 対応状況による変換テーブルが用意される.
171
172 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
173 local function cid_to_char(fmtable, fn)
174    local fi = identifiers[fn]
175    local fe = ltjf_font_extra_info[fn]
176    if (fi.resources and fi.resources.cidinfo and fi.resources.cidinfo.ordering == "Japan1" )
177       and (fe and fe.unicodes) then
178       for i, v in pairs(fmtable.chars) do
179          local j = string.match(i, "^AJ1%-([0-9]*)")
180          if j then
181             j = tonumber(fe.unicodes['Japan1.'..tostring(j)])
182             if j then
183                fmtable.cid_char_type = fmtable.cid_char_type  or {}
184                fmtable.cid_char_type[j] = v
185             end
186          end
187       end
188    end
189    return fmtable
190 end
191 luatexbase.add_to_callback("luatexja.define_jfont",
192                            cid_to_char, "ltj.otf.define_jfont", 1)
193 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
194 for fn, v in pairs(ltjf_font_metric_table) do
195    ltjf_font_metric_table[fn] = cid_to_char(v, fn)
196 end
197
198
199 local function cid_set_char_class(arg, fmtable, char)
200    if arg~=0 then return arg
201    elseif fmtable.cid_char_type then
202       return fmtable.cid_char_type[char] or 0
203    else return 0
204    end
205 end
206 luatexbase.add_to_callback("luatexja.find_char_class",
207                            cid_set_char_class, "ltj.otf.find_char_class", 1)
208
209 -------------------- IVS
210 local enable_ivs, disable_ivs
211 do
212    local is_ivs_enabled = false
213 -- 組版時
214    local function ivs_jglyph(char, bp, pf, uid)
215       local p = node_new(id_whatsit,sid_user)
216       setfield(p, 'user_id', uid)
217       setfield(p, 'type', 100)
218       setfield(p, 'value', char)
219       return p
220    end
221
222    local function do_ivs_repr(h)
223       local head = to_direct(h)
224       local p, r = head
225       local is_dir_tate = (ltjs.list_dir == dir_tate)
226       local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
227       local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
228       local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
229       while p do
230          local pid = getid(p)
231          if pid==id_glyph then
232             local q = node_next(p) -- the next node of p
233             if q and getid(q)==id_glyph then
234                local qc = getchar(q)
235                if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
236                    -- q is a variation selector
237                   if qc>=0xE0100 then qc = qc - 0xE0100 end
238                   local pf = getfont(p)
239                   local pt = ltjf_font_extra_info[pf]
240                   pt = pt and pt[getchar(p)];  pt = pt and  pt[qc]
241                   head, r = node_remove(head,q)
242                   node_free(q)
243                   if pt then
244                      local is_jachar = (getfield(p, 'lang')==lang_ja)
245                      local np = ivs_jglyph(pt, p, pf,
246                                            is_jachar and OTF or VSR)
247                      if is_jachar then
248                         set_attr(np, attr_curfnt, pf)
249                         set_attr(np, attr_kblshift, has_attr(p, attr_kblshift))
250                      end
251                      head = node_insert_after(head, p, np)
252                      head = node_remove(head,p)
253                      node_free(p)
254                   end
255                   p = r
256                else
257                   p = q
258                end
259             else
260                p = node_next(p)
261             end
262          else
263             p = node_next(p)
264          end
265      end
266      return to_node(head)
267    end
268
269    enable_ivs = function ()
270       if is_ivs_enabled then
271          ltjb.package_warning('luatexja-otf',
272                               'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
273       else
274          ltjb.add_to_callback('hpack_filter', do_ivs_repr, 'ltj.do_ivs',
275             luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor'))
276          ltjb.add_to_callback('pre_linebreak_filter', do_ivs_repr, 'ltj.do_ivs',
277             luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor'))
278          is_ivs_enabled = true
279       end
280    end
281    disable_ivs = function ()
282       if is_ivs_enabled then
283          luatexbase.remove_from_callback('hpack_filter', 'ltj.do_ivs')
284          luatexbase.remove_from_callback('pre_linebreak_filter', 'ltj.do_ivs')
285          is_ivs_enabled = false
286       end
287    end
288 end
289
290 luatexja.otf = {
291   append_jglyph = append_jglyph,
292   enable_ivs = enable_ivs,  -- 隠し機能: IVS
293   disable_ivs = disable_ivs,  -- 隠し機能: IVS
294   cid = cid,
295 }
296
297 -- EOF