OSDN Git Service

d13cff277b44c519f0580d18cd867a268a23feb6
[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 = identifiers[tex_get_attr((ltjd_get_dir_count()==dir_tate)
100                                                   and attr_curtfnt or attr_curjfnt)]
101       if not curjfnt.cidinfo or
102          curjfnt.cidinfo.ordering ~= "Japan1" and
103          curjfnt.cidinfo.ordering ~= "GB1" and
104          curjfnt.cidinfo.ordering ~= "CNS1" and
105          curjfnt.cidinfo.ordering ~= "Korea1" then
106          --      ltjb.package_warning('luatexja-otf',
107          --                        'Current Japanese font (or other CJK font) "'
108          --                           ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
109             return append_jglyph(get_ucs_from_rmlgbm(key))
110       end
111       local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
112       if not char then
113          ltjb.package_warning('luatexja-otf',
114                               'Current Japanese font (or other CJK font) "'
115                                  ..curjfnt.psname..'" does not have the specified CID character ('
116                                  ..tostring(key)..')',
117                               'Use a font including the specified CID character.')
118          char = 0
119       end
120       return append_jglyph(char)
121    end
122 end
123
124 local function extract(head)
125    head = to_direct(head)
126    local p = head
127    local is_dir_tate = ltjs.list_dir == dir_tate
128    local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
129    local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
130    local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
131    while p do
132       if getid(p)==id_whatsit then
133          if getsubtype(p)==sid_user then
134             local puid = getfield(p, 'user_id')
135             if puid==OTF or puid==VSR then
136                local g = node_new(id_glyph)
137                setfield(g, 'subtype', 0)
138                setfield(g, 'char', getfield(p, 'value'))
139                local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
140                if puid==OTF then
141                   setfield(g, 'lang', lang_ja)
142                   set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
143                else
144                   set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
145                end
146                head = node_insert_after(head, p, g)
147                head = node_remove(head, p)
148                node_free(p); p = g
149             end
150          end
151       end
152       p = node_next(p)
153    end
154    return to_node(head)
155 end
156
157 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
158   luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
159 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
160   luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
161 -- additional callbacks
162 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
163 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
164 --   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
165 --   CID <-> グリフ 対応状況による変換テーブルが用意される.
166
167 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
168 local function cid_to_char(fmtable, fn)
169    local fi = identifiers[fn]
170    if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
171       for i, v in pairs(fmtable.chars) do
172          local j = string.match(i, "^AJ1%-([0-9]*)")
173          if j then
174             j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
175             if j then
176                fmtable.cid_char_type = fmtable.cid_char_type  or {}
177                fmtable.cid_char_type[j] = v
178             end
179          end
180       end
181    end
182    return fmtable
183 end
184 luatexbase.add_to_callback("luatexja.define_jfont",
185                            cid_to_char, "ltj.otf.define_jfont", 1)
186 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
187 for fn, v in pairs(ltjf_font_metric_table) do
188    ltjf_font_metric_table[fn] = cid_to_char(v, fn)
189 end
190
191
192 local function cid_set_char_class(arg, fmtable, char)
193    if arg~=0 then return arg
194    elseif fmtable.cid_char_type then
195       return fmtable.cid_char_type[char] or 0
196    else return 0
197    end
198 end
199 luatexbase.add_to_callback("luatexja.find_char_class",
200                            cid_set_char_class, "ltj.otf.find_char_class", 1)
201
202 -------------------- IVS
203 local enable_ivs, disable_ivs
204 do
205    local is_ivs_enabled = false
206 -- 組版時
207    local function ivs_jglyph(char, bp, pf, uid)
208       local p = node_new(id_whatsit,sid_user)
209       setfield(p, 'user_id', uid)
210       setfield(p, 'type', 100)
211       setfield(p, 'value', char)
212       return p
213    end
214
215    local function do_ivs_repr(h)
216       local head = to_direct(h)
217       local p, r = head
218       local is_dir_tate = (ltjs.list_dir == dir_tate)
219       local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
220       local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
221       local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
222       while p do
223          local pid = getid(p)
224          if pid==id_glyph then
225             local q = node_next(p) -- the next node of p
226             if q and getid(q)==id_glyph then
227                local qc = getchar(q)
228                if (qc>=0xFE00 and qc<=0xFE0F) or (qc>=0xE0100 and qc<0xE01F0) then
229                    -- q is a variation selector
230                   if qc>=0xE0100 then qc = qc - 0xE0100 end
231                   local pf = getfont(p)
232                   local pt = ltjf_font_extra_info[pf]
233                   pt = pt and pt[getchar(p)];  pt = pt and  pt[qc]
234                   head, r = node_remove(head,q)
235                   node_free(q)
236                   if pt then
237                      local is_jachar = (getfield(p, 'lang')==lang_ja)
238                      local np = ivs_jglyph(pt, p, pf,
239                                            is_jachar and OTF or VSR)
240                      if is_jachar then
241                         set_attr(np, attr_curfnt, pf)
242                         set_attr(np, attr_kblshift, has_attr(p, attr_kblshift))
243                      end
244                      head = node_insert_after(head, p, np)
245                      head = node_remove(head,p)
246                      node_free(p)
247                   end
248                   p = r
249                else
250                   p = q
251                end
252             else
253                p = node_next(p)
254             end
255          else
256             p = node_next(p)
257          end
258      end
259      return to_node(head)
260    end
261
262    enable_ivs = function ()
263       if is_ivs_enabled then
264          ltjb.package_warning('luatexja-otf',
265                               'luatexja.otf.enable_ivs() was already called, so this call is ignored', '')
266       else
267          ltjb.add_to_callback('hpack_filter', do_ivs_repr, 'ltj.do_ivs',
268             luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor'))
269          ltjb.add_to_callback('pre_linebreak_filter', do_ivs_repr, 'ltj.do_ivs',
270             luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor'))
271          is_ivs_enabled = true
272       end
273    end
274    disable_ivs = function ()
275       if is_ivs_enabled then
276          luatexbase.remove_from_callback('hpack_filter', 'ltj.do_ivs')
277          luatexbase.remove_from_callback('pre_linebreak_filter', 'ltj.do_ivs')
278          is_ivs_enabled = false
279       end
280    end
281 end
282
283 luatexja.otf = {
284   append_jglyph = append_jglyph,
285   enable_ivs = enable_ivs,  -- 隠し機能: IVS
286   disable_ivs = disable_ivs,  -- 隠し機能: IVS
287   cid = cid,
288 }
289
290 -- EOF