OSDN Git Service

typo
[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 = tex_get_attr((ltjd_get_dir_count()==dir_tate)
77                                         and attr_curtfnt or attr_curjfnt)
78       local tfmdata = font_getfont(curjfnt)
79       if tfmdata and tfmdata.resources then
80         local base, ivs = v % 0x200000, 0xE00FF + math.floor(v/0x200000)
81         curjfnt = tfmdata and tfmdata.variants
82         curjfnt = curjfnt and curjfnt[ivs]
83         return curjfnt and curjfnt[base] or base
84       else return base
85       end
86    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
87       return v
88    else -- privete use area
89       local r, aj = nil, ltjr_cidfont_data["Adobe-Japan1"] 
90       -- 先に ltj_vert_table を見る
91       for i,w in pairs(aj.ltj_vert_table) do
92          if w==v then r=i; break end
93       end
94       if not r then
95          -- なければ ToUnicode から引く
96          local w = aj.characters[v].tounicode -- must be non-nil!
97          local i = string.len(w)
98          if i==4 then -- UCS2
99             r = tonumber(w,16)
100          elseif i==8 then
101             i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
102             if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
103                r = (w-0xD800)*0x400 + (i-0xDC00)
104             else
105                r = 0
106             end
107          end
108       end
109       if aj.ltj_vert_table[r] then
110          -- CID が縦組用字形だった場合
111          return ltju.replace_vert_variant(
112             tex_get_attr((ltjd_get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt),
113             r)
114       end
115       return r
116    end
117 end
118
119 -- Append a whatsit node to the list.
120 -- This whatsit node will be extracted to a glyph_node
121 local function append_jglyph(char)
122    local p = node_new(id_whatsit,sid_user)
123    setfield(p, 'user_id', OTF)
124    setfield(p, 'type', 100)
125    setfield(p, 'value', char)
126    node_write(p)
127 end
128
129 local utf
130 do
131    utf = function (ucs)
132       if ltjd_get_dir_count()==dir_tate then
133          ucs = ltju.replace_vert_variant(
134             tex_get_attr((ltjd_get_dir_count()==dir_tate) and attr_curtfnt or attr_curjfnt),
135             ucs)
136       end
137       return append_jglyph(ucs)
138    end
139 end
140
141 local cid
142 do
143    cid = function (key)
144       if key==0 then return append_jglyph(0) end
145       local curjfnt = tex_get_attr((ltjd_get_dir_count()==dir_tate)
146                                         and attr_curtfnt or attr_curjfnt)
147       local cidinfo = ltju.get_cidinfo(cudjfnt)
148       if type(cidinfo)~="table" or
149          cidinfo.ordering ~= "Japan1" and
150          cidinfo.ordering ~= "GB1" and
151          cidinfo.ordering ~= "CNS1" and
152          cidinfo.ordering ~= "Korea1" and
153          cidinfo.ordering ~= "KR" then
154             return append_jglyph(get_ucs_from_rmlgbm(key))
155       else
156          local char = ltjf_font_extra_info[curjfnt].ind_to_uni[key] or 0
157          return append_jglyph(char)
158       end
159    end
160 end
161
162 local function extract(head)
163    head = to_direct(head)
164    local p = head
165    local is_dir_tate = ltjs.list_dir == dir_tate
166    local attr_ablshift = is_dir_tate and attr_tablshift or attr_yablshift
167    local attr_kblshift = is_dir_tate and attr_tkblshift or attr_ykblshift
168    local attr_curfnt =   is_dir_tate and attr_curtfnt or attr_curjfnt
169    while p do
170       if getid(p)==id_whatsit then
171          if getsubtype(p)==sid_user then
172             local puid = getfield(p, 'user_id')
173             if puid==OTF then
174             --if puid==OTF or puid==VSR then
175                local g = node_new(id_glyph)
176                setfield(g, 'subtype', 0)
177                setfield(g, 'char', getfield(p, 'value'))
178                local v = has_attr(p, attr_curfnt); setfield(g, 'font',v)
179                --if puid==OTF then
180                   setfield(g, 'lang', lang_ja)
181                   set_attr(g, attr_kblshift, has_attr(p, attr_kblshift))
182                --else
183                --   set_attr(g, attr_ablshift, has_attr(p, attr_ablshift))
184                --end
185                head = node_insert_after(head, p, g)
186                head = node_remove(head, p)
187                node_free(p); p = g
188             end
189          end
190       end
191       p = node_next(p)
192    end
193    return to_node(head)
194 end
195
196 ltjb.add_to_callback('hpack_filter', extract,'ltj.otf',
197   luatexbase.priority_in_callback('hpack_filter', 'ltj.main'))
198 ltjb.add_to_callback('pre_linebreak_filter', extract,'ltj.otf',
199   luatexbase.priority_in_callback('pre_linebreak_filter', 'ltj.main'))
200 -- additional callbacks
201 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
202 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
203
204 -- 和文フォント読み込み時に,ind -> unicode 対応をとっておく.
205 local function ind_to_uni(fmtable, fn)
206    local cid = ltju.get_cidinfo(fn)
207    local t = ltjf_font_extra_info[fn].ind_to_uni
208    if t and cid.ordering == "Japan1" then
209       for i, v in pairs(fmtable.chars) do
210          local j = string.match(i, "^AJ1%-([0-9]*)")
211          if j then
212             j = t[i]
213             if j then
214                fmtable.cid_char_type = fmtable.cid_char_type  or {}
215                fmtable.cid_char_type[j] = v
216             end
217          end
218       end
219    end
220    return fmtable
221 end
222 luatexbase.add_to_callback("luatexja.define_jfont",
223                            ind_to_uni, "ltj.otf.define_jfont", 1)
224 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
225 for fn, v in pairs(ltjf_font_metric_table) do
226    ltjf_font_metric_table[fn] = ind_to_uni(v, fn)
227 end
228
229
230 local function cid_set_char_class(arg, fmtable, char)
231    if arg~=0 then return arg
232    elseif fmtable.cid_char_type then
233       return fmtable.cid_char_type[char] or 0
234    else return 0
235    end
236 end
237 luatexbase.add_to_callback("luatexja.find_char_class",
238                            cid_set_char_class, "ltj.otf.find_char_class", 1)
239
240 --IVS
241 local function enable_ivs()
242   ltjb.package_warning('luatexja-otf',
243     'luatexja.otf.enable_ivs() has now no effect.')
244 end
245 local disable_ivs = enable_ivs
246
247 luatexja.otf = {
248   append_jglyph = append_jglyph,
249   enable_ivs = enable_ivs,  -- 隠し機能: IVS
250   disable_ivs = disable_ivs,  -- 隠し機能: IVS
251   cid = cid, utf = utf,
252 }
253
254
255 -- EOF