OSDN Git Service

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