OSDN Git Service

use node.direct.{g/s}etdata
[luatex-ja/luatexja.git] / src / ltj-otf.lua
1 --
2 -- ltj-otf.lua
3 --
4 require 'lualibs'
5
6 luatexja.load_module 'base';      local ltjb = luatexja.base
7 luatexja.load_module 'jfont';     local ltjf = luatexja.jfont
8 luatexja.load_module 'rmlgbm';    local ltjr = luatexja.rmlgbm
9 luatexja.load_module 'charrange'; local ltjc = luatexja.charrange
10 luatexja.load_module 'direction'; local ltjd = luatexja.direction
11 luatexja.load_module 'stack';     local ltjs = luatexja.stack
12 luatexja.load_module 'lotf_aux';  local ltju = luatexja.lotf_aux
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 local getvalue = node.direct.getdata
25 local setchar = node.direct.setchar
26 local setfont = node.direct.setfont
27 local setlang = node.direct.setlang
28 local setvalue = node.direct.setdata
29
30 local to_node = node.direct.tonode
31 local to_direct = node.direct.todirect
32 local node_new = node.direct.new
33 local node_remove = node.direct.remove
34 local node_next = node.direct.getnext
35 local node_free = node.direct.flush_node or node.direct.free
36 local get_attr = node.direct.get_attribute
37 local set_attr = node.direct.set_attribute
38 local unset_attr = node.direct.unset_attribute
39 local node_insert_after = node.direct.insert_after
40 local node_write = node.direct.write
41 local node_traverse_id = node.direct.traverse_id
42
43 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
44 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
45 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
46 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
47 local attr_tablshift = luatexbase.attributes['ltj@tablshift']
48 local attr_tkblshift = luatexbase.attributes['ltj@tkblshift']
49 local lang_ja = luatexja.lang_ja
50 local font_getfont = font.getfont
51
52 local ltjf_font_metric_table = ltjf.font_metric_table
53 local ltjf_font_extra_info = ltjf.font_extra_info
54 local ltjf_find_char_class = ltjf.find_char_class
55 local ltjr_cidfont_data = ltjr.cidfont_data
56 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
57 local ltjd_get_dir_count = ltjd.get_dir_count
58 local dir_tate = luatexja.dir_table.dir_tate
59
60 luatexja.userid_table.OTF = luatexbase.newuserwhatsitid('char_by_cid',  'luatexja')
61 local OTF = luatexja.userid_table.OTF
62 local tex_get_attr = tex.getattribute
63
64 local cache_ver = 3
65 local ivd_aj1 = ltjb.load_cache('ltj-ivd_aj1',
66    function (t) return t.version~=cache_ver end)
67 if not ivd_aj1 then -- make cache
68    ivd_aj1 = require('ltj-ivd_aj1.lua')
69    ltjb.save_cache_luc('ltj-ivd_aj1', ivd_aj1)
70 end
71
72
73 local function get_ucs_from_rmlgbm(c)
74    local v = (ivd_aj1 and ivd_aj1.table_ivd_aj1[c]
75       or ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)])
76       or 0
77    if v>=0x200000 then -- table
78       local curjfnt = tex_get_attr(
79         (ltjd_get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
80       local tfmdata = font_getfont(curjfnt)
81       if tfmdata and tfmdata.resources then
82         local base, ivs = v % 0x200000, 0xE00FF + math.floor(v/0x200000)
83         curjfnt = tfmdata.resources.variants; curjfnt = curjfnt and curjfnt[ivs]
84         return curjfnt and curjfnt[base] or base
85       else return base
86       end
87    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
88       return v
89    else -- privete use area
90       local r, aj = nil, ltjr_cidfont_data["Adobe-Japan1"]
91       -- 先に ltj_vert_table を見る
92       for i,w in pairs(aj.ltj_vert_table) do
93          if w==v then r=i; break end
94       end
95       if not r then
96          -- なければ ToUnicode から引く
97          local w = aj.characters[v].tounicode -- must be non-nil!
98          local i = string.len(w)
99          if i==4 then -- UCS2
100             r = tonumber(w,16)
101          elseif i==8 then
102             i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
103             if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
104                r = (w-0xD800)*0x400 + (i-0xDC00)
105             else
106                r = 0
107             end
108          end
109       end
110       if aj.ltj_vert_table[r] then
111          -- CID が縦組用字形だった場合
112          return ltju.replace_vert_variant(
113             tex_get_attr((ltjd_get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt),
114             r)
115       end
116       return r
117    end
118 end
119
120 -- Append a whatsit node to the list.
121 -- This whatsit node will be extracted to a glyph_node
122 local function append_jglyph(char)
123    local p = node_new(id_whatsit,sid_user)
124    setfield(p, 'user_id', OTF); setfield(p, 'type', 100)
125    setvalue(p, char);  node_write(p)
126 end
127
128 local myutf
129 do
130    myutf = function (ucs)
131       if ltjd_get_dir_count()==dir_tate then
132          ucs = ltju.replace_vert_variant(
133             tex_get_attr((ltjd_get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt),
134             ucs)
135       end
136       return append_jglyph(ucs)
137    end
138 end
139
140 local cid
141 do
142    local ord = {
143       ['Japan1']=true, ['GB1']=true, ['CNS1']=true, ['Korea1']=true, ['KR']=true
144    }
145    cid = function (key)
146       if key==0 then return append_jglyph(0) end
147       local curjfnt = tex_get_attr(
148          (ltjd_get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt)
149       local cidinfo = ltju.get_cidinfo(curjfnt)
150       if type(cidinfo)~="table" or not ord[cidinfo.ordering] then
151             return append_jglyph(get_ucs_from_rmlgbm(key))
152       else
153          local char = ltjf_font_extra_info[curjfnt].ind_to_uni[key] or 0
154          return append_jglyph(char)
155       end
156    end
157 end
158
159 local function extract(head)
160    head = to_direct(head)
161    local p = head
162    local is_dir_tate = ltjs.list_dir == dir_tate
163    local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
164    local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
165    local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
166    while p do
167       if getid(p)==id_whatsit then
168          if getsubtype(p)==sid_user then
169             local puid = getfield(p, 'user_id')
170             if puid==OTF then
171                local g = node_new(id_glyph, 0)
172                setfont(g, get_attr(p, attr_curfnt), getvalue(p))
173                setlang(g, lang_ja)
174                set_attr(g, attr_kblshift, get_attr(p, attr_kblshift))
175                head = node_insert_after(head, p, g)
176                head = node_remove(head, p)
177                node_free(p); p = g
178             end
179          end
180       end
181       p = node_next(p)
182    end
183    return to_node(head)
184 end
185
186 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
187   luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
188 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
189   luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
190 -- additional callbacks
191 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
192 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
193
194 -- 和文フォント読み込み時に,ind -> unicode 対応をとっておく.
195 local function ind_to_uni(fmtable, fn)
196    if fn<0 then return end
197    local cid = ltju.get_cidinfo(fn)
198    local t = ltjf_font_extra_info[fn]; t = t and t.ind_to_uni
199    if t and cid.ordering == "Japan1" then
200       for i, v in pairs(fmtable.chars) do
201          local j = string.match(i, "^AJ1%-([0-9]*)")
202          if j then
203             j = t[tonumber(j)]
204             if j then
205                fmtable.cid_char_type = fmtable.cid_char_type  or {}
206                fmtable.cid_char_type[j] = v
207             end
208          end
209       end
210    end
211    return fmtable
212 end
213 luatexbase.add_to_callback("luatexja.define_jfont",
214                            ind_to_uni, "ltj.otf.define_jfont", 1)
215 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
216 for fn, v in pairs(ltjf_font_metric_table) do
217    ltjf_font_metric_table[fn] = ind_to_uni(v, fn)
218 end
219
220
221 local function cid_set_char_class(arg, fmtable, char)
222    if arg~=0 then return arg
223    elseif fmtable.cid_char_type then
224       return fmtable.cid_char_type[char] or 0
225    else return 0
226    end
227 end
228 luatexbase.add_to_callback("luatexja.find_char_class",
229                            cid_set_char_class, "ltj.otf.find_char_class", 1)
230
231 --IVS
232 local function enable_ivs()
233   ltjb.package_warning('luatexja-otf',
234      'luatexja.otf.enable_ivs() has now no effect.')
235 end
236 local disable_ivs = enable_ivs
237
238 luatexja.otf = {
239   append_jglyph = append_jglyph,
240   enable_ivs = enable_ivs, disable_ivs = disable_ivs,
241   cid = cid, utf = myutf,
242 }
243
244
245 -- EOF