OSDN Git Service

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