OSDN Git Service

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