OSDN Git Service

Merge branch 'master' into kitagawa_test
[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 local OTF = luatexja.userid_table.OTF
59 local tex_get_attr = tex.getattribute
60
61 local cache_var = 2
62 local cache_outdate_fn = function (t) return t.version~=cache_ver end
63 local ivd_aj1 = ltjb.load_cache('ltj-ivd_aj1',cache_outdate_fn)
64 if not ivd_aj1 then -- make cache
65    ivd_aj1 = require('ltj-ivd_aj1.lua')
66    ltjb.save_cache_luc('ltj-ivd_aj1', ivd_aj1)
67 end
68
69
70 local function get_ucs_from_rmlgbm(c)
71    local v = (ivd_aj1 and ivd_aj1.table_ivd_aj1[c]
72       or ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)])
73       or 0
74    if v>=0x200000 then -- table
75       local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
76                                         and attr_curtfnt or attr_curjfnt)
77       local curjfnt = identifiers[curjfnt_num].resources
78       local base, ivs = v % 0x200000, 0xE00FF + math.floor(v/0x200000)
79       curjfnt = curjfnt and curjfnt.variants
80       curjfnt = curjfnt and curjfnt[ivs]
81       return curjfnt and curjfnt[base] or base
82    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
83       return v
84    else -- privete use area
85       local r, aj = nil, ltjr_cidfont_data["Adobe-Japan1"] 
86       -- 先に ltj_vert_table を見る
87       for i,w in pairs(aj.shared.ltj_vert_table) do
88          if w==v then r=i; break end
89       end
90       if not r then
91          -- なければ ToUnicode から引く
92          local w = aj.characters[v].tounicode -- must be non-nil!
93          local i = string.len(w)
94          if i==4 then -- UCS2
95             r = tonumber(w,16)
96          elseif i==8 then
97             i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
98             if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
99                r = (w-0xD800)*0x400 + (i-0xDC00)
100             else
101                r = 0
102             end
103          end
104       end
105       if aj.shared.ltj_vert_table[r] then
106          -- CID が縦組用字形だった場合
107          local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
108                                         and attr_curtfnt or attr_curjfnt)
109          local t = identifiers[curjfnt_num]
110          if t.resources.sequences then
111             for _,i in pairs(t.resources.sequences) do
112                if (i.order[1]=='vert' or i.order[1]=='vrt2')
113                   and i.type == 'gsub_single' and i.steps then
114                   for _,j in pairs(i.steps) do
115                      if type(j)=='table' then 
116                         if type(j.coverage)=='table' then
117                            for i,k in pairs(j.coverage) do
118                               if i==r then return k end
119                            end
120                         end
121                      end
122                   end
123                end
124             end
125          end
126       end
127       return r
128    end
129 end
130
131 -- Append a whatsit node to the list.
132 -- This whatsit node will be extracted to a glyph_node
133 local function append_jglyph(char)
134    local p = node_new(id_whatsit,sid_user)
135    setfield(p, 'user_id', OTF)
136    setfield(p, 'type', 100)
137    setfield(p, 'value', char)
138    node_write(p)
139 end
140
141 local utf
142 do
143    utf = function (ucs)
144       local char = ucs
145       if ltjd_get_dir_count()==dir_tate then
146          local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
147                                         and attr_curtfnt or attr_curjfnt)
148          local t = identifiers[curjfnt_num]
149          if t.resources.sequences then
150             for _,i in pairs(t.resources.sequences) do
151                if (i.order[1]=='vert' or i.order[1]=='vrt2')
152                   and i.type == 'gsub_single' and i.steps then
153                   for _,j in pairs(i.steps) do
154                      if type(j)=='table' then 
155                         if type(j.coverage)=='table' then
156                            for i,k in pairs(j.coverage) do
157                               if i==char then return append_jglyph(k) end
158                            end
159                         end
160                      end
161                   end
162                end
163             end
164          end
165       end
166       return append_jglyph(char)
167    end
168 end
169
170 local cid
171 do
172    cid = function (key)
173       if key==0 then return append_jglyph(0) end
174       local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
175                                         and attr_curtfnt or attr_curjfnt)
176       local curjfnt = identifiers[curjfnt_num]
177       local cidinfo = curjfnt.resources.cidinfo
178       if not cidinfo or
179          cidinfo.ordering ~= "Japan1" and
180          cidinfo.ordering ~= "GB1" and
181          cidinfo.ordering ~= "CNS1" and
182          cidinfo.ordering ~= "Korea1" and
183          cidinfo.ordering ~= "KR" then
184          --      ltjb.package_warning('luatexja-otf',
185          --                        'Current Japanese font (or other CJK font) "'
186          --                           ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
187             return append_jglyph(get_ucs_from_rmlgbm(key))
188       else
189          local char = ltjf_font_extra_info[curjfnt_num].ind_to_uni[key]
190          if not char then
191 --          ltjb.package_warning('luatexja-otf',
192 --               '"' ..curjfnt.psname..'" does not have CID character '
193 --                ..tostring(key),
194 --             'Use a font including the specified CID character.')
195          char = 0
196          end
197          return append_jglyph(char)
198       end
199    end
200 end
201
202 local function extract(head)
203    head = to_direct(head)
204    local p = head
205    local is_dir_tate = ltjs.list_dir == dir_tate
206    local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
207    local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
208    local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
209    while p do
210       if getid(p)==id_whatsit then
211          if getsubtype(p)==sid_user then
212             local puid = getfield(p, 'user_id')
213             if puid==OTF then
214             --if puid==OTF or puid==VSR then
215                local g = node_new(id_glyph)
216                setfield(g, 'subtype', 0)
217                setfield(g, 'char', getfield(p, 'value'))
218                local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
219                --if puid==OTF then
220                   setfield(g, 'lang', lang_ja)
221                   set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
222                --else
223                --   set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
224                --end
225                head = node_insert_after(head, p, g)
226                head = node_remove(head, p)
227                node_free(p); p = g
228             end
229          end
230       end
231       p = node_next(p)
232    end
233    return to_node(head)
234 end
235
236 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
237   luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
238 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
239   luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
240 -- additional callbacks
241 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
242 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
243
244 -- 和文フォント読み込み時に,ind -> unicode 対応をとっておく.
245 local function ind_to_uni(fmtable, fn)
246    local fi = identifiers[fn]
247    local t = ltjf_font_extra_info[fn].ind_to_uni
248    if t and fi.resources and fi.resources.cidinfo 
249       and fi.resources.cidinfo.ordering == "Japan1" then
250       for i, v in pairs(fmtable.chars) do
251          local j = string.match(i, "^AJ1%-([0-9]*)")
252          if j then
253             j = t[i]
254             if j then
255                fmtable.cid_char_type = fmtable.cid_char_type  or {}
256                fmtable.cid_char_type[j] = v
257             end
258          end
259       end
260    end
261    return fmtable
262 end
263 luatexbase.add_to_callback("luatexja.define_jfont",
264                            ind_to_uni, "ltj.otf.define_jfont", 1)
265 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
266 for fn, v in pairs(ltjf_font_metric_table) do
267    ltjf_font_metric_table[fn] = ind_to_uni(v, fn)
268 end
269
270
271 local function cid_set_char_class(arg, fmtable, char)
272    if arg~=0 then return arg
273    elseif fmtable.cid_char_type then
274       return fmtable.cid_char_type[char] or 0
275    else return 0
276    end
277 end
278 luatexbase.add_to_callback("luatexja.find_char_class",
279                            cid_set_char_class, "ltj.otf.find_char_class", 1)
280
281 --IVS
282 local function enable_ivs()
283   ltjb.package_warning('luatexja-otf',
284     'luatexja.otf.enable_ivs() has now no effect.')
285 end
286 local disable_ivs = enable_ivs
287
288 luatexja.otf = {
289   append_jglyph = append_jglyph,
290   enable_ivs = enable_ivs,  -- 隠し機能: IVS
291   disable_ivs = disable_ivs,  -- 隠し機能: IVS
292   cid = cid, utf = utf,
293 }
294
295
296 -- EOF