OSDN Git Service

Merge remote-tracking branch 'takotakot/master' into kitagawa_test
[luatex-ja/luatexja.git] / src / ltj-pretreat.lua
1 --
2 -- ltj-pretreat.lua
3 --
4
5 luatexja.load_module('base');      local ltjb = luatexja.base
6 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
7 luatexja.load_module('stack');     local ltjs = luatexja.stack
8 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
9 luatexja.load_module('direction'); local ltjd = luatexja.direction
10
11 local to_node =  node.direct.tonode
12 local to_direct =  node.direct.todirect
13
14 local setfield =  node.direct.setfield
15 local getid =  node.direct.getid
16 local getfont =  node.direct.getfont
17 local getchar =  node.direct.getchar
18 local getfield =  node.direct.getfield
19 local getsubtype =  node.direct.getsubtype
20 local getlang = node.direct.getlang or function (n) return getfield(n,'lang') end
21
22 local pairs = pairs
23 local floor = math.floor
24 local has_attr = node.direct.has_attribute
25 local set_attr = node.direct.set_attribute
26 local node_traverse = node.direct.traverse
27 local node_remove = node.direct.remove
28 local node_next =  node.direct.getnext
29 local node_free = node.direct.free
30 local node_end_of_math = node.direct.end_of_math
31 local tex_getcount = tex.getcount
32
33 local id_glyph = node.id('glyph')
34 local id_math = node.id('math')
35 local id_whatsit = node.id('whatsit')
36 local sid_user = node.subtype('user_defined')
37
38 local attr_dir = luatexbase.attributes['ltj@dir']
39 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
40 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
41 local attr_icflag = luatexbase.attributes['ltj@icflag']
42
43 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
44 local ltjs_orig_char_table = ltjs.orig_char_table
45 local ltjf_replace_altfont = ltjf.replace_altfont
46 local STCK  = luatexja.userid_table.STCK
47 local DIR   = luatexja.userid_table.DIR
48 local JA_AL_BDD = luatexja.userid_table.JA_AL_BDD
49 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
50
51 local dir_tate = luatexja.dir_table.dir_tate
52 local lang_ja = luatexja.lang_ja
53
54 local setlang = node.direct.setlang or function(n,l) setfield(n,'lang',l) end 
55 local setfont = node.direct.setfont or function(n,l) setfield(n,'font',l) end 
56 local setchar = node.direct.setchar or function(n,l) setfield(n,'char',l) end 
57
58 ------------------------------------------------------------------------
59 -- MAIN PROCESS STEP 1: replace fonts
60 ------------------------------------------------------------------------
61 local wt, wtd = {}, {}
62 do
63    local ltjd_get_dir_count = ltjd.get_dir_count
64    local start_time_measure, stop_time_measure
65       = ltjb.start_time_measure, ltjb.stop_time_measure
66    local head
67    local suppress_hyphenate_ja_aux = {
68       [id_math] = function(p) return node_next(node_end_of_math(node_next(p))) end,
69       [id_whatsit] = function(p)
70          if getsubtype(p)==sid_user then
71             local uid = getfield(p, 'user_id')
72             if uid==STCK then
73                wt[#wt+1] = p; node_remove(head, p)
74             elseif uid==DIR then
75                if has_attr(p, attr_icflag)<PROCESSED_BEGIN_FLAG  then
76                   ltjs.list_dir = has_attr(p, attr_dir)
77                else -- こっちのケースは通常使用では起こらない
78                   wtd[#wtd+1] = p; node_remove(head, p)
79                end
80             end
81          end
82          return node_next(p)
83       end,
84    }
85    setmetatable(suppress_hyphenate_ja_aux, 
86                 {
87                    __index = function() return node_next end,
88                 })
89    local id_boundary = node.id('boundary')
90    local node_new, insert_before = node.direct.new, node.direct.insert_before
91    local setsubtype = node.direct.setsubtype or function(n,l) setfield(n,'subtype',l) end 
92    local function suppress_hyphenate_ja (h)
93       start_time_measure('ltj_hyphenate')
94       head = to_direct(h)
95       for i = 1,#wt do wt[i]=nil end
96       for i = 1,#wtd do wtd[i]=nil end
97       for i,_ in pairs(ltjs_orig_char_table) do
98          ltjs_orig_char_table[i] = nil
99       end
100       ltjs.list_dir=ltjd_get_dir_count()
101       do
102          local p = head
103          while p do
104             local pid, prev_chartype = getid(p), 0
105             -- prev_chartype: 0: not char 1: ALchar 2: JAchar
106             while pid==id_glyph do
107                local pc = getchar(p)
108                if has_attr(p, attr_icflag, 0) and is_ucs_in_japanese_char(p, pc) then
109                   if prev_chartype==1 then
110                      local b = node_new(id_whatsit,sid_user);
111                      setfield(b, 'type', 100); setfield(b, 'user_id', JA_AL_BDD);
112                      insert_before(head, p, b)
113                   end
114                   local pf = has_attr(p, attr_curjfnt)
115                   pf = (pf and pf>0 and pf) or getfont(p)
116                   setfont(p, ltjf_replace_altfont(pf, pc))
117                   setlang(p, lang_ja)
118                   ltjs_orig_char_table[p], prev_chartype = pc, 2
119                elseif prev_chartype==2 then
120                   local b = node_new(id_whatsit,sid_user);
121                   setfield(b, 'type', 100); setfield(b, 'user_id', JA_AL_BDD);
122                   insert_before(head, p, b); prev_chartype = 1
123                else prev_chartype = 1
124                end
125                p = node_next(p); pid = getid(p)
126             end
127             p = (suppress_hyphenate_ja_aux[pid])(p)
128          end
129       end
130       stop_time_measure('ltj_hyphenate'); start_time_measure('tex_hyphenate')
131       lang.hyphenate(h, nil)
132       stop_time_measure('tex_hyphenate')
133       return h
134    end
135
136    ltjb.add_to_callback('hyphenate',
137                               function (head,tail)
138                                  return suppress_hyphenate_ja(head)
139                               end,'ltj.hyphenate')
140 end
141
142 -- mode: true iff this function is called from hpack_filter
143 local ltjs_report_stack_level = ltjs.report_stack_level
144 local ltjf_font_metric_table  = ltjf.font_metric_table
145 local font_getfont = font.getfont
146 local function set_box_stack_level(head, mode)
147    local box_set, cl = 0, tex.currentgrouplevel + 1
148    if mode then
149       for _,p  in pairs(wt) do
150          if getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
151       end
152    else
153       for _,p  in pairs(wt) do node_free(p) end
154    end
155    ltjs_report_stack_level(tex_getcount('ltj@@stack') + box_set)
156    for _,p  in pairs(wtd) do
157       node_free(p)
158    end
159    if ltjs.list_dir == dir_tate then
160       for p in node.direct.traverse_id(id_glyph,to_direct(head)) do
161          if has_attr(p, attr_icflag, 0) and getlang(p)==lang_ja then
162             local nf = ltjf_replace_altfont( has_attr(p, attr_curtfnt) or getfont(p) , ltjs_orig_char_table[p])
163             setfont(p, nf)
164             if ltjf_font_metric_table[nf].vert_activated then
165                local pc = getchar(p)
166                pc = ltjf_font_metric_table[nf].vform[pc]
167                if pc then setchar(p,  pc) end
168             end
169          end
170       end
171    end
172    return head
173 end
174
175 -- CALLBACKS
176 ltjb.add_to_callback('hpack_filter',
177    function (head)
178      return set_box_stack_level(head, true)
179    end,'ltj.set_stack_level',1)
180 ltjb.add_to_callback('pre_linebreak_filter',
181   function (head)
182      return set_box_stack_level(head, false)
183   end,'ltj.set_stack_level',1)
184
185 luatexja.pretreat = {
186    set_box_stack_level = set_box_stack_level,
187    orig_char_table = orig_char_table,
188 }