OSDN Git Service

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