OSDN Git Service

Stop use of stack system for direction.
[luatex-ja/luatexja.git] / src / ltj-pretreat.lua
1 --
2 -- luatexja/ltj-pretreat.lua
3 --
4
5 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
6 luatexja.load_module('stack');     local ltjs = luatexja.stack
7 luatexja.load_module('jfont');     local ltjf = luatexja.jfont
8 luatexja.load_module('direction'); local ltjd = luatexja.direction
9
10 local Dnode = node.direct or node
11
12 local nullfunc = function(n) return n end
13 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
14 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
15
16 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
17 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
18 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
19 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
20 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
21 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
22
23 local pairs = pairs
24 local floor = math.floor
25 local has_attr = Dnode.has_attribute
26 local set_attr = Dnode.set_attribute
27 local node_traverse = Dnode.traverse
28 local node_remove =luatexja.Dnode_remove -- Dnode.remove
29 local node_next = (Dnode ~= node) and Dnode.getnext or node.next
30 local node_free = Dnode.free
31 local node_end_of_math = Dnode.end_of_math
32 local tex_getcount = tex.getcount
33
34 local id_glyph = node.id('glyph')
35 local id_math = node.id('math')
36 local id_whatsit = node.id('whatsit')
37 local sid_user = node.subtype('user_defined')
38
39 local attr_dir = luatexbase.attributes['ltj@dir']
40 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
41 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
42 local attr_icflag = luatexbase.attributes['ltj@icflag']
43
44 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
45 local ltjd_get_vert_glyph = ltjd.get_vert_glyph
46 local ltjf_replace_altfont = ltjf.replace_altfont
47 local attr_orig_char = luatexbase.attributes['ltj@origchar']
48 local STCK = luatexja.userid_table.STCK
49 local DIR = luatexja.userid_table.DIR
50
51 local dir_tate = 3
52 local dir_yoko = 4
53
54 ------------------------------------------------------------------------
55 -- MAIN PROCESS STEP 1: replace fonts
56 ------------------------------------------------------------------------
57 local wt
58 do
59    local head
60    local is_dir_tate
61    local dir_frozen
62    local suppress_hyphenate_ja_aux = {}
63    suppress_hyphenate_ja_aux[id_glyph] = function(p)
64       if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
65          local pc = getchar(p)
66          local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
67          setfield(p, 'font', pf);  set_attr(p, attr_curjfnt, pf)
68          setfield(p, 'subtype', floor(getsubtype(p)*0.5)*2)
69          set_attr(p, attr_orig_char, pc)
70       end
71       return p
72    end
73    suppress_hyphenate_ja_aux[id_math] = function(p) 
74       dir_frozen = true
75       return node_end_of_math(node_next(p)) end
76    suppress_hyphenate_ja_aux[50] = function(p) return p end
77    suppress_hyphenate_ja_aux[id_whatsit] = function(p)
78       if getsubtype(p)==sid_user then 
79          local uid = getfield(p, 'user_id')
80          if uid==STCK then
81             wt[#wt+1] = p; head = node_remove(head, p)
82          elseif uid==DIR and not dir_frozen then
83             ltjs.list_dir = getfield(p, 'value')
84          end
85          dir_frozen = true
86       end
87       return p
88    end
89
90    local function suppress_hyphenate_ja (h)
91       local p = to_direct(h)
92       wt, head, dir_frozen = {}, p, false
93       ltjs.list_dir=ltjd.get_dir_count()
94       while p do
95          local pfunc = suppress_hyphenate_ja_aux[getid(p)]
96          if pfunc then p = pfunc(p) else dir_frozen = true end
97          p = node_next(p)
98       end
99       head = to_node(head)
100       lang.hyphenate(head)
101       return head
102    end
103
104    luatexbase.add_to_callback('hyphenate',
105                               function (head,tail)
106                                  return suppress_hyphenate_ja(head)
107                               end,'ltj.hyphenate')
108 end
109
110 -- mode: true iff this function is called from hpack_filter
111 local function set_box_stack_level(head, mode)
112    local box_set, cl = 0, tex.currentgrouplevel + 1
113    for _,p  in pairs(wt) do
114       if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
115    end
116    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
117    is_dir_tate = ltjs.list_dir == dir_tate
118    if is_dir_tate then
119       for p in Dnode.traverse_id(id_glyph,to_direct(head)) do
120          if (has_attr(p, attr_icflag) or 0)<=0 and has_attr(p, attr_curjfnt)==getfont(p) then
121             local pfn = has_attr(p, attr_curtfnt) or getfont(p)
122             local pc = getchar(p)
123             local pf = ltjf_replace_altfont(pfn, pc)
124             set_attr(p, attr_dir, pc)
125             pc = ltjd_get_vert_glyph(pf, pc) or pc
126             setfield(p, 'char', pc); set_attr(p, attr_orig_char, pc)
127             setfield(p, 'font', pf); set_attr(p, attr_curjfnt, pf)
128          end
129       end
130    end
131    return head
132 end
133
134 -- CALLBACKS
135 luatexbase.add_to_callback('hpack_filter',
136    function (head)
137      return set_box_stack_level(head, true)
138    end,'ltj.hpack_filter_pre',1)
139 luatexbase.add_to_callback('pre_linebreak_filter',
140   function (head)
141      return set_box_stack_level(head, false)
142   end,'ltj.pre_linebreak_filter_pre',1)
143
144 luatexja.pretreat = {
145    set_box_stack_level = set_box_stack_level,
146 }