OSDN Git Service

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