OSDN Git Service

Support \ltj@std(mc|gt)font and luatexja.cfg.
[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 luatexja.load_module('base');      local ltjb = luatexja.base
11 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
12 luatexja.load_module('rmlgbm');    local ltjr = luatexja.rmlgbm
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 node_new = node.new
19 local node_remove = node.remove
20 local node_next = node.next
21 local node_free = node.free
22 local has_attr = node.has_attribute
23 local set_attr = node.set_attribute
24 local unset_attr = node.unset_attribute
25 local node_insert_after = node.insert_after
26
27 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
28 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
29 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
30 local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']
31
32 local ltjf_font_metric_table = ltjf.font_metric_table
33 local ltjf_find_char_class = ltjf.find_char_class
34 local ltjr_cidfont_data = ltjr.cidfont_data
35
36 local OTF = luatexja.userid_table.OTF
37
38 local function get_ucs_from_rmlgbm(c)
39    local v = ltjr_cidfont_data["Adobe-Japan1"].resources.unicodes["Japan1." .. tostring(c)]
40    if not v then -- AJ1 範囲外
41       return 0
42    elseif v<0xF0000 then -- 素直に Unicode にマップ可能
43       return v
44    else
45       local w = ltjr_cidfont_data["Adobe-Japan1"].characters[v]. tounicode
46       -- must be non-nil!
47       local i = string.len(w)
48       if i==4 then -- UCS2
49          return tonumber(w,16)
50       elseif i==8 then 
51          i,w = tonumber(string.sub(w,1,4),16), tonumber(string.sub(w,-4),16)
52          if (w>=0xD800) and (w<=0xDB7F) and (i>=0xDC00) and (i<=0xDFFF) then -- Surrogate pair
53             return (w-0xD800)*0x400 + (i-0xDC00)
54          else
55             return 0
56          end
57       end
58    end
59 end
60
61 -- Append a whatsit node to the list.
62 -- This whatsit node will be extracted to a glyph_node
63 local function append_jglyph(char)
64    local p = node_new(id_whatsit,sid_user)
65    local v = tex.attribute[attr_curjfnt]
66    p.user_id=OTF; p.type=100; p.value=char
67    set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
68    node.write(p)
69 end
70
71 local function cid(key)
72    if key==0 then return append_jglyph(char) end
73    local curjfnt = fonts.ids[tex.attribute[attr_curjfnt]]
74    if not curjfnt.cidinfo or 
75       curjfnt.cidinfo.ordering ~= "Japan1" and
76       curjfnt.cidinfo.ordering ~= "GB1" and
77       curjfnt.cidinfo.ordering ~= "CNS1" and
78       curjfnt.cidinfo.ordering ~= "Korea1" then
79 --      ltjb.package_warning('luatexja-otf',
80 --                         'Current Japanese font (or other CJK font) "'
81 --                            ..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)')
82       return append_jglyph(get_ucs_from_rmlgbm(key))
83    end
84    local char = curjfnt.resources.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
85    if not char then
86       ltjb.package_warning('luatexja-otf',
87                            'Current Japanese font (or other CJK font) "'
88                               ..curjfnt.psname..'" does not have the specified CID character ('
89                               ..tostring(key)..')', 
90                            'Use a font including the specified CID character.')
91       char = 0
92    end
93    return append_jglyph(char)
94 end
95
96 local function extract(head)
97    local p = head
98    local v
99    while p do
100       if p.id==id_whatsit then
101          if p.subtype==sid_user and p.user_id==OTF then
102             local g = node_new(id_glyph)
103             g.subtype = 0; g.char = p.value
104             v = has_attr(p, attr_curjfnt); g.font = v
105             set_attr(g, attr_curjfnt, v)
106             v = has_attr(p, attr_yablshift)
107             if v then 
108                set_attr(g, attr_yablshift, v)
109             else
110                unset_attr(g, attr_yablshift)
111             end
112             head = node_insert_after(head, p, g)
113             head = node_remove(head, p)
114             node_free(p); p = g
115          end
116       end
117       p = node_next(p)
118    end
119    return head
120 end
121
122 luatexbase.add_to_callback('hpack_filter', 
123    function (head) return extract(head) end,'ltj.hpack_filter_otf',
124    luatexbase.priority_in_callback('pre_linebreak_filter',
125                                    'ltj.pre_linebreak_filter'))
126 luatexbase.add_to_callback('pre_linebreak_filter', 
127    function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
128    luatexbase.priority_in_callback('pre_linebreak_filter',
129                                    'ltj.pre_linebreak_filter'))
130
131
132 -- additional callbacks
133 -- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
134 --   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
135 --   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
136 --   CID <-> グリフ 対応状況による変換テーブルが用意される.
137
138 -- フォント読み込み時に,CID
139 local function cid_to_char(fmtable, fn)
140    local fi = fonts.ids[fn]
141    if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
142       fmtable.cid_char_type = {}
143       for i, v in pairs(fmtable.chars) do
144          local j = string.match(i, "^AJ1%-([0-9]*)")
145          if j then
146             j = tonumber(fi.resources.unicodes['Japan1.'..tostring(j)])
147             if j then
148                fmtable.cid_char_type[j] = v 
149             end
150          end
151       end
152    end
153    return fmtable
154 end
155 luatexbase.add_to_callback("luatexja.define_jfont", 
156                            cid_to_char, "ltj.otf.define_jfont", 1)
157 --  既に読み込まれているフォントに対しても,同じことをやらないといけない
158 for fn, v in pairs(ltjf_font_metric_table) do
159    ltjf_font_metric_table[fn] = cid_to_char(v, fn)
160 end
161
162
163 local function cid_set_char_class(arg, fmtable, char)
164    if arg~=0 then return arg
165    elseif fmtable.cid_char_type then
166       return fmtable.cid_char_type[char] or 0
167    else return 0
168    end
169 end
170 luatexbase.add_to_callback("luatexja.find_char_class", 
171                            cid_set_char_class, "ltj.otf.find_char_class", 1)
172
173 -------------------- all done
174 luatexja.otf = {
175   append_jglyph = append_jglyph,
176   cid = cid,
177 }
178
179 -- EOF