OSDN Git Service

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