OSDN Git Service

Merged test/ivs.lua into ltj-otf.lua
[luatex-ja/luatexja.git] / src / ltj-otf.lua
1 --
2 -- luatexja/otf.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.otf',
6   date = '2013/05/11',
7   description = 'The OTF Lua module for LuaTeX-ja',
8 })
9
10 require('unicode')
11
12
13 luatexja.load_module('base');      local ltjb = luatexja.base
14 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
15 luatexja.load_module('rmlgbm');    local ltjr = luatexja.rmlgbm
16 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
17
18 local id_glyph = node.id('glyph')
19 local id_whatsit = node.id('whatsit')
20 local sid_user = node.subtype('user_defined')
21
22 local node_new = node.new
23 local node_remove = node.remove
24 local node_next = node.next
25 local node_free = node.free
26 local has_attr = node.has_attribute
27 local set_attr = node.set_attribute
28 local unset_attr = node.unset_attribute
29 local node_insert_after = node.insert_after
30 local identifiers = fonts.hashes.identifiers
31
32 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
33 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
34 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
35 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
36
37 local ltjf_font_metric_table = ltjf.font_metric_table
38 local ltjf_find_char_class = ltjf.find_char_class
39 local ltjr_cidfont_data = ltjr.cidfont_data
40 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
41
42 local OTF = luatexja.userid_table.OTF
43
44 local function get_ucs_from_rmlgbm(c)
45    local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
46    if not v then -- AJ1 範囲外
47       return 0
48    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
49       return v
50    else
51       local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
52       -- must be non-nil!
53       local i = string.len(w)
54       if i==4 then -- UCS2
55          return tonumber(w,16)
56       elseif i==8 then 
57          i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
58          if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
59             return (w-0xD800)*0x400 + (i-0xDC00)
60          else
61             return 0
62          end
63       end
64    end
65 end
66
67 -- Append a whatsit node to the list.
68 -- This whatsit node will be extracted to a glyph_node
69 local function append_jglyph(char)
70    local p = node_new(id_whatsit,sid_user)
71    local v = tex.attribute[attr_curjfnt]
72    p.user_id=OTF; p.type=100; p.value=char
73    set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
74    node.write(p)
75 end
76
77 local function cid(key)
78    if key==0 then return append_jglyph(char) end
79    local curjfnt = identifiers[tex.attribute[attr_curjfnt]]
80    if not curjfnt.cidinfo or 
81       curjfnt.cidinfo.ordering ~= "Japan1" and
82       curjfnt.cidinfo.ordering ~= "GB1" and
83       curjfnt.cidinfo.ordering ~= "CNS1" and
84       curjfnt.cidinfo.ordering ~= "Korea1" then
85 --      ltjb.package_warning('luatexja-otf',
86 --                         'Current Japanese font (or other CJK font) "'
87 --                            ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
88       return append_jglyph(get_ucs_from_rmlgbm(key))
89    end
90    local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
91    if not char then
92       ltjb.package_warning('luatexja-otf',
93                            'Current Japanese font (or other CJK font) "'
94                               ..curjfnt.psname..'" does not have the specified CID character ('
95                               ..tostring(key)..')', 
96                            'Use a font including the specified CID character.')
97       char = 0
98    end
99    return append_jglyph(char)
100 end
101
102 local function extract(head)
103    local p = head
104    local v
105    while p do
106       if p.id==id_whatsit then
107          if p.subtype==sid_user and p.user_id==OTF then
108             local g = node_new(id_glyph)
109             g.subtype = 0; g.char = p.value
110             v = has_attr(p, attr_curjfnt); g.font = v
111             set_attr(g, attr_curjfnt, v)
112             v = has_attr(p, attr_yablshift)
113             if v then 
114                set_attr(g, attr_yablshift, v)
115             else
116                unset_attr(g, attr_yablshift)
117             end
118             head = node_insert_after(head, p, g)
119             head = node_remove(head, p)
120             node_free(p); p = g
121          end
122       end
123       p = node_next(p)
124    end
125    return head
126 end
127
128 luatexbase.add_to_callback('hpack_filter', 
129    function (head) return extract(head) end,'ltj.hpack_filter_otf',
130    luatexbase.priority_in_callback('pre_linebreak_filter',
131                                    'ltj.pre_linebreak_filter'))
132 luatexbase.add_to_callback('pre_linebreak_filter', 
133    function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
134    luatexbase.priority_in_callback('pre_linebreak_filter',
135                                    'ltj.pre_linebreak_filter'))
136
137
138 -- additional callbacks
139 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
140 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
141 --   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
142 --   CID <-> グリフ 対応状況による変換テーブルが用意される.
143
144 -- 和文フォント読み込み時に,CID -> unicode 対応をとっておく.
145 local function cid_to_char(fmtable, fn)
146    local fi = identifiers[fn]
147    if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
148       fmtable.cid_char_type = {}
149       for i, v in pairs(fmtable.chars) do
150          local j = string.match(i, "^AJ1%-([0-9]*)")
151          if j then
152             j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
153             if j then
154                fmtable.cid_char_type[j] = v 
155             end
156          end
157       end
158    end
159    return fmtable
160 end
161 luatexbase.add_to_callback("luatexja.define_jfont", 
162                            cid_to_char, "ltj.otf.define_jfont", 1)
163 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
164 for fn, v in pairs(ltjf_font_metric_table) do
165    ltjf_font_metric_table[fn] = cid_to_char(v, fn)
166 end
167
168
169 local function cid_set_char_class(arg, fmtable, char)
170    if arg~=0 then return arg
171    elseif fmtable.cid_char_type then
172       return fmtable.cid_char_type[char] or 0
173    else return 0
174    end
175 end
176 luatexbase.add_to_callback("luatexja.find_char_class", 
177                            cid_set_char_class, "ltj.otf.find_char_class", 1)
178
179 -------------------- IVS
180 do
181    local get_node_font = function(p)
182       return (ltjc_is_ucs_in_japanese_char(p) and (has_attr(p, attr_curjfnt) or 0) or p.font)
183    end
184    local get_current_font = function() return tex.attribute[attr_curjfnt] or 0 end
185    local function do_ivs_repr(head)
186       local p = head
187       while p do
188          local pid = p.id
189          if pid==id_glyph then
190             local pf = get_node_font(p)
191             local pt = identifiers[pf]
192             pt = pt and pt.resources; pt = pt and pt.variants
193             if pt then
194                local q = node_next(p) -- the next node of p
195                if q and q.id==id_glyph then
196                   local qc = q.char
197                   if qc>=0xE0100 and qc<0xE01F0 then -- q is an IVS selector
198                      pt = pt[qc];  pt = pt and  pt[p.char]
199                  if pt then
200                     p.char = pt or p.char
201                  end
202                  head = node_remove(head,q)
203                   end
204                end
205         end
206          end
207          p = node_next(p)
208       end
209       return head
210    end
211    function enable_ivs()
212       luatexbase.add_to_callback('hpack_filter', 
213                                  function (head) return do_ivs_repr(head) end,'do_ivs', 1)
214       luatexbase.add_to_callback('pre_linebreak_filter', 
215                                  function (head) return do_ivs_repr(head) end, 'do_ivs', 1)
216    end
217
218 end
219
220 -------------------- all done
221 luatexja.otf = {
222   append_jglyph = append_jglyph,
223   enable_ivs = enable_ivs,  -- 隠し機能: IVS(TTF しか現状では使えない)
224   cid = cid,
225 }
226
227 -- EOF