OSDN Git Service

095d969630883de3af2813d4352b561a06ee8cea
[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 function get_ucs_from_rmlgbm(c)
62    local v = (luatexja.otf.ivd_aj1 and luatexja.otf.ivd_aj1[c]
63      or ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)])
64      or 0
65    if type(v)~='number' then -- table
66       local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
67                                         and attr_curtfnt or attr_curjfnt)
68       local curjfnt = identifiers[curjfnt_num].resources
69       curjfnt = curjfnt and curjfnt.variants
70       curjfnt = curjfnt and curjfnt[v[2]]
71       return curjfnt and curjfnt[v[1]] or v[1]
72    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
73       return v
74    else -- privete use area
75       local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
76       -- must be non-nil!
77       local i = string.len(w)
78       if i==4 then -- UCS2
79          return tonumber(w,16)
80       elseif i==8 then
81          i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
82          if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
83             return (w-0xD800)*0x400 + (i-0xDC00)
84          else
85             return 0
86          end
87       end
88    end
89 end
90
91 -- Append a whatsit node to the list.
92 -- This whatsit node will be extracted to a glyph_node
93 local function append_jglyph(char)
94    local p = node_new(id_whatsit,sid_user)
95    setfield(p, 'user_id', OTF)
96    setfield(p, 'type', 100)
97    setfield(p, 'value', char)
98    node_write(p)
99 end
100
101 local cid
102 do
103    cid = function (key)
104       if key==0 then return append_jglyph(char) end
105       local curjfnt_num = tex_get_attr((ltjd_get_dir_count()==dir_tate)
106                                         and attr_curtfnt or attr_curjfnt)
107       local curjfnt = identifiers[curjfnt_num]
108       local cidinfo = curjfnt.resources.cidinfo
109       if not cidinfo or
110          cidinfo.ordering ~= "Japan1" and
111          cidinfo.ordering ~= "GB1" and
112          cidinfo.ordering ~= "CNS1" and
113          cidinfo.ordering ~= "Korea1" then
114          --      ltjb.package_warning('luatexja-otf',
115          --                        'Current Japanese font (or other CJK font) "'
116          --                           ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
117             return append_jglyph(get_ucs_from_rmlgbm(key))
118       end
119       local fe, char = ltjf_font_extra_info[curjfnt_num], nil
120       if fe and fe.unicodes then 
121          char = fe.unicodes[cidinfo.ordering..'.'..tostring(key)]
122       end
123       if not char then
124          ltjb.package_warning('luatexja-otf',
125                               'Current Japanese font (or other CJK font) "'
126                                  ..curjfnt.psname..'" does not have the specified CID character ('
127                                  ..tostring(key)..')',
128                               'Use a font including the specified CID character.')
129          char = 0
130       end
131       return append_jglyph(char)
132    end
133 end
134
135 local function extract(head)
136    head = to_direct(head)
137    local p = head
138    local is_dir_tate = ltjs.list_dir == dir_tate
139    local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
140    local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
141    local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
142    while p do
143       if getid(p)==id_whatsit then
144          if getsubtype(p)==sid_user then
145             local puid = getfield(p, 'user_id')
146             if puid==OTF then
147             --if puid==OTF or puid==VSR then
148                local g = node_new(id_glyph)
149                setfield(g, 'subtype', 0)
150                setfield(g, 'char', getfield(p, 'value'))
151                local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
152                --if puid==OTF then
153                   setfield(g, 'lang', lang_ja)
154                   set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
155                --else
156                --   set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
157                --end
158                head = node_insert_after(head, p, g)
159                head = node_remove(head, p)
160                node_free(p); p = g
161             end
162          end
163       end
164       p = node_next(p)
165    end
166    return to_node(head)
167 end
168
169 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
170   luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
171 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
172   luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
173 -- additional callbacks
174 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
175 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
176 --   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
177 --   CID <-> グリフ 対応状況による変換テーブルが用意される.
178
179 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
180 local function cid_to_char(fmtable, fn)
181    local fi = identifiers[fn]
182    local fe = ltjf_font_extra_info[fn]
183    if (fi.resources and fi.resources.cidinfo and fi.resources.cidinfo.ordering == "Japan1" )
184       and (fe and fe.unicodes) then
185       for i, v in pairs(fmtable.chars) do
186          local j = string.match(i, "^AJ1%-([0-9]*)")
187          if j then
188             j = tonumber(fe.unicodes['Japan1.'..tostring(j)])
189             if j then
190                fmtable.cid_char_type = fmtable.cid_char_type  or {}
191                fmtable.cid_char_type[j] = v
192             end
193          end
194       end
195    end
196    return fmtable
197 end
198 luatexbase.add_to_callback("luatexja.define_jfont",
199                            cid_to_char, "ltj.otf.define_jfont", 1)
200 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
201 for fn, v in pairs(ltjf_font_metric_table) do
202    ltjf_font_metric_table[fn] = cid_to_char(v, fn)
203 end
204
205
206 local function cid_set_char_class(arg, fmtable, char)
207    if arg~=0 then return arg
208    elseif fmtable.cid_char_type then
209       return fmtable.cid_char_type[char] or 0
210    else return 0
211    end
212 end
213 luatexbase.add_to_callback("luatexja.find_char_class",
214                            cid_set_char_class, "ltj.otf.find_char_class", 1)
215
216 --IVS
217 local function enable_ivs()
218   ltjb.package_warning('luatexja-otf',
219     'luatexja.otf.enable_ivs() has now no effect.')
220 end
221 local disable_ivs = enable_ivs
222
223 luatexja.otf = {
224   append_jglyph = append_jglyph,
225   enable_ivs = enable_ivs,  -- 隠し機能: IVS
226   disable_ivs = disable_ivs,  -- 隠し機能: IVS
227   cid = cid,
228 }
229
230 luatexja.load_module('ivd_aj1')
231
232
233 -- EOF