OSDN Git Service

Releases 20240514.0
[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
21
22 local pairs = pairs
23 local floor = math.floor
24 local get_attr = node.direct.get_attribute
25 local has_attr = node.direct.has_attribute
26 local set_attr = node.direct.set_attribute
27 local node_traverse = node.direct.traverse
28 local node_remove = node.direct.remove
29 local node_next =  node.direct.getnext
30 local node_free = node.direct.flush_node or node.direct.free
31 local node_end_of_math = node.direct.end_of_math
32 local 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 ltjs_orig_char_table = ltjs.orig_char_table
46 local ltjf_replace_altfont = ltjf.replace_altfont
47 local STCK  = luatexja.userid_table.STCK
48 local DIR   = luatexja.userid_table.DIR
49 local JA_AL_BDD = luatexja.userid_table.JA_AL_BDD
50 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
51
52 local dir_tate = luatexja.dir_table.dir_tate
53 local lang_ja = luatexja.lang_ja
54
55 local setlang = node.direct.setlang
56 local setfont = node.direct.setfont
57 local setchar = node.direct.setchar
58
59 ------------------------------------------------------------------------
60 -- MAIN PROCESS STEP 1: replace fonts
61 ------------------------------------------------------------------------
62 local wt, wtd = {}, {}
63 do
64    local ltjd_get_dir_count = ltjd.get_dir_count
65    local start_time_measure, stop_time_measure
66       = ltjb.start_time_measure, ltjb.stop_time_measure
67    local head
68    local suppress_hyphenate_ja_aux = {
69       [id_math] = function(p) return node_next(node_end_of_math(node_next(p))) end,
70       [id_whatsit] = function(p)
71          if getsubtype(p)==sid_user then
72             local uid = getfield(p, 'user_id')
73             if uid==STCK then
74                wt[#wt+1] = p; node_remove(head, p)
75             elseif uid==DIR then
76                if get_attr(p, attr_icflag)<PROCESSED_BEGIN_FLAG  then
77                   ltjs.list_dir = get_attr(p, attr_dir)
78                else -- こっちのケースは通常使用では起こらない
79                   wtd[#wtd+1] = p; node_remove(head, p)
80                end
81             end
82          end
83          return node_next(p)
84       end,
85    }
86    setmetatable(suppress_hyphenate_ja_aux,
87                 { __index = function() return node_next end, })
88    local id_boundary = node.id 'boundary'
89    local node_new, insert_before = node.direct.new, node.direct.insert_before
90    local setsubtype = node.direct.setsubtype
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
95       for i = 1,#wtd do wtd[i]=nil end
96       for i,_ in pairs(ltjs_orig_char_table) do
97          ltjs_orig_char_table[i] = nil
98       end
99       ltjs.list_dir=ltjd_get_dir_count()
100       do
101          local p = head
102          while p do
103             local pid, prev_chartype = getid(p), 0
104             -- prev_chartype: 0: not char 1: ALchar 2: JAchar
105             while pid==id_glyph do
106                local pc = getchar(p)
107                if has_attr(p, attr_icflag, 0) and is_ucs_in_japanese_char(p, pc) then
108                   if prev_chartype==1 then
109                      local b = node_new(id_whatsit,sid_user);
110                      setfield(b, 'type', 100); setfield(b, 'user_id', JA_AL_BDD);
111                      insert_before(head, p, b)
112                   end
113                   setlang(p, lang_ja);
114                   ltjs_orig_char_table[p], prev_chartype = pc, 2
115                elseif prev_chartype==2 then
116                   local b = node_new(id_whatsit,sid_user);
117                   setfield(b, 'type', 100); setfield(b, 'user_id', JA_AL_BDD);
118                   insert_before(head, p, b); prev_chartype = 1
119                else prev_chartype = 1
120                end
121                p = node_next(p); pid = getid(p)
122             end
123             p = (suppress_hyphenate_ja_aux[pid])(p)
124          end
125       end
126       stop_time_measure 'ltj_hyphenate'; start_time_measure 'tex_hyphenate'
127       lang.hyphenate(h, nil)
128       stop_time_measure 'tex_hyphenate'
129       return h
130    end
131
132    ltjb.add_to_callback('hyphenate', suppress_hyphenate_ja, 'ltj.hyphenate')
133 end
134
135 -- mode: true iff this function is called from hpack_filter
136 local set_box_stack_level
137 do
138 local ltjs_report_stack_level = ltjs.report_stack_level
139 local ltjf_font_metric_table  = ltjf.font_metric_table
140 local traverse_glyph = node.direct.traverse_glyph
141 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
142 local texget, getvalue = tex.get, node.direct.getdata
143 function set_box_stack_level(head, mode)
144    local box_set = 0
145    if mode then
146       local cl = (texget 'currentgrouplevel') + 1
147       for i=1,#wt do
148          local p = wt[i]
149          if getvalue(p)==cl then box_set = 1 end; node_free(p)
150       end
151    else
152       for i=1,#wt do node_free(wt[i]) end
153    end
154    ltjs_report_stack_level(getcount(cnt_stack) + box_set)
155    for _,p  in pairs(wtd) do node_free(p) end
156    if ltjs.list_dir == dir_tate then
157       for p in traverse_glyph(to_direct(head)) do
158          if getlang(p)==lang_ja and has_attr(p, attr_icflag, 0) then
159             local pf = ltjf_replace_altfont(attr_curtfnt, getchar(p), p)
160             if ltjf_font_metric_table[pf].vert_activated then
161                local pc = ltjf_font_metric_table[pf].vform[pc]; if pc then setchar(p,  pc) end
162             end
163          end
164       end
165    else
166       for p in traverse_glyph(to_direct(head)) do
167          if getlang(p)==lang_ja and has_attr(p, attr_icflag, 0) then
168             ltjf_replace_altfont(attr_curjfnt, getchar(p), p)
169          end
170       end
171    end
172    return head
173 end
174 end
175
176 -- CALLBACKS
177 ltjb.add_to_callback('hpack_filter',
178    function (head)
179      return set_box_stack_level(head, true)
180    end, 'ltj.set_stack_level', 1)
181 ltjb.add_to_callback('pre_linebreak_filter',
182    function (head)
183       return set_box_stack_level(head, false)
184    end, 'ltj.set_stack_level', 1)
185
186 luatexja.pretreat = {
187    set_box_stack_level = set_box_stack_level,
188    orig_char_table = orig_char_table,
189 }