summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luatexja/ltj-otf.lua
blob: 71f974979e9c00d49eb0f853a990382dfee65e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--
-- luatexja/otf.lua
--
luatexbase.provides_module({
  name = 'luatexja.otf',
  date = '2011/09/09',
  version = '0.1',
  description = 'The OTF Lua module for LuaTeX-ja',
})
module('luatexja.otf', package.seeall)

luatexja.load_module('base');      local ltjb = luatexja.base
luatexja.load_module('jfont');     local ltjf = luatexja.jfont

local id_glyph = node.id('glyph')
local id_whatsit = node.id('whatsit')
local sid_user = node.subtype('user_defined')

local node_new = node.new
local node_remove = node.remove
local node_next = node.next
local node_free = node.free
local has_attr = node.has_attribute
local set_attr = node.set_attribute
local unset_attr = node.unset_attribute
local node_insert_after = node.insert_after

local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
local attr_jchar_class = luatexbase.attributes['ltj@charclass']
local attr_yablshift = luatexbase.attributes['ltj@yablshift']
local attr_ykblshift = luatexbase.attributes['ltj@ykblshift']

local ltjf_font_metric_table = ltjf.font_metric_table
local ltjf_find_char_class = ltjf.find_char_class


-- Append a whatsit node to the list.
-- This whatsit node will be extracted to a glyph_node
function append_jglyph(char)
   local p = node_new(id_whatsit,sid_user)
   local v = tex.attribute[attr_curjfnt]
   p.user_id=30113; p.type=100; p.value=char
   set_attr(p, attr_yablshift, tex.attribute[attr_ykblshift])
   node.write(p)
end

function cid(key)
   local curjfnt = fonts.ids[tex.attribute[attr_curjfnt]]
   if curjfnt.cidinfo.ordering ~= "Japan1" and
	   curjfnt.cidinfo.ordering ~= "GB1" and
	   curjfnt.cidinfo.ordering ~= "CNS1" and
	   curjfnt.cidinfo.ordering ~= "Korea1" then
      ltjb.package_error('luatexja-otf',
                         'Current Japanese font (or other CJK font) "'..curjfnt.psname..'" is not a CID-Keyed font (Adobe-Japan1 etc.)', 
                         'Select a CID-Keyed font using \jfont.')
      return
   end
   local char = curjfnt.unicodes[curjfnt.cidinfo.ordering..'.'..tostring(key)]
   if not char then
      ltjb.package_warning('luatexja-otf',
                         'Current Japanese font (or other CJK font) "'..curjfnt.psname..'" does not include the specified CID character ('..tostring(key)..')', 
                         'Use a font including the specified CID character.')
      return
   end
   append_jglyph(char)
end

function extract(head)
   local p = head
   local v
   while p do
      if p.id==id_whatsit then
	 if p.subtype==sid_user and p.user_id==30113 then
	    local g = node_new(id_glyph)
	    g.subtype = 0; g.char = p.value
	    v = has_attr(p, attr_curjfnt); g.font = v
	    set_attr(g, attr_curjfnt, v)
	    v = has_attr(p, attr_yablshift)
	    if v then 
	       set_attr(g, attr_yablshift, v)
	    else
	       unset_attr(g, attr_yablshift)
	    end
	    head = node_insert_after(head, p, g)
	    head = node_remove(head, p)
	    node_free(p); p = g
	 end
      end
      p = node_next(p)
   end
   return head
end

luatexbase.add_to_callback('hpack_filter', 
   function (head) return extract(head) end,'ltj.hpack_filter_otf',
   luatexbase.priority_in_callback('pre_linebreak_filter',
				   'ltj.pre_linebreak_filter'))
luatexbase.add_to_callback('pre_linebreak_filter', 
   function (head) return extract(head) end, 'ltj.pre_linebreak_filter_otf',
   luatexbase.priority_in_callback('pre_linebreak_filter',
				   'ltj.pre_linebreak_filter'))


-- additional callbacks
-- 以下は,LuaTeX-ja に用意された callback のサンプルになっている.
--   JFM の文字クラスの指定の所で,"AJ1-xxx" 形式での指定を可能とした.
--   これらの文字指定は,和文フォント定義ごとに,それぞれのフォントの
--   CID <-> グリフ 対応状況による変換テーブルが用意される.

-- フォント読み込み時に,CID
local function cid_to_char(fmtable, fn)
   local fi = fonts.ids[fn]
   if fi.cidinfo and fi.cidinfo.ordering == "Japan1" then
      fmtable.cid_char_type = {}
      for i, v in pairs(fmtable.size_cache.chars) do
	 local j = string.match(i, "^AJ1%-([0-9]*)")
	 if j then
	    j = tonumber(fi.unicodes['Japan1.'..tostring(j)])
	    if j then
	       fmtable.cid_char_type[j] = v 
	    end
	 end
      end
   end
   return fmtable
end
luatexbase.add_to_callback("luatexja.define_jfont", 
			   cid_to_char, "ltj.otf.define_jfont", 1)
--  既に読み込まれているフォントに対しても,同じことをやらないといけない
for fn, v in pairs(ltjf_font_metric_table) do
   ltjf_font_metric_table[fn] = cid_to_char(v, fn)
end


local function cid_set_char_class(arg, fmtable, char)
   if arg~=0 then return arg
   elseif fmtable.cid_char_type then
      return fmtable.cid_char_type[char] or 0
   else return 0
   end
end
luatexbase.add_to_callback("luatexja.find_char_class", 
			   cid_set_char_class, "ltj.otf.find_char_class", 1)

-------------------- all done
-- EOF