OSDN Git Service

387fcc8cea3464c2c298ccbedf9a9159ff5f1255
[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
9 local Dnode = node.direct or node
10
11 local nullfunc = function(n) return n end
12 local to_node = (Dnode ~= node) and Dnode.tonode or nullfunc
13 local to_direct = (Dnode ~= node) and Dnode.todirect or nullfunc
14
15 local setfield = (Dnode ~= node) and Dnode.setfield or function(n, i, c) n[i] = c end
16 local getid = (Dnode ~= node) and Dnode.getid or function(n) return n.id end
17 local getfont = (Dnode ~= node) and Dnode.getfont or function(n) return n.font end
18 local getchar = (Dnode ~= node) and Dnode.getchar or function(n) return n.char end
19 local getfield = (Dnode ~= node) and Dnode.getfield or function(n, i) return n[i] end
20 local getsubtype = (Dnode ~= node) and Dnode.getsubtype or function(n) return n.subtype end
21
22 local pairs = pairs
23 local floor = math.floor
24 local has_attr = Dnode.has_attribute
25 local set_attr = Dnode.set_attribute
26 local node_traverse = Dnode.traverse
27 local node_remove =luatexja.Dnode_remove -- Dnode.remove
28 local node_next = Dnode.getnext
29 local node_free = Dnode.free
30 local node_end_of_math = Dnode.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_curjfnt = luatexbase.attributes['ltj@curjfnt']
39 local attr_icflag = luatexbase.attributes['ltj@icflag']
40
41 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
42 local ltjf_replace_altfont = ltjf.replace_altfont
43 local attr_orig_char = luatexbase.attributes['ltj@origchar']
44 local STCK = luatexja.userid_table.STCK
45
46 ------------------------------------------------------------------------
47 -- MAIN PROCESS STEP 1: replace fonts
48 ------------------------------------------------------------------------
49 local wt
50 do
51    local head
52
53    local suppress_hyphenate_ja_aux = {}
54    suppress_hyphenate_ja_aux[id_glyph] = function(p)
55       if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p) then
56          local pc = getchar(p)
57          local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc)
58          setfield(p, 'font', pf);  set_attr(p, attr_curjfnt, pf)
59          setfield(p, 'subtype', floor(getsubtype(p)*0.5)*2)
60          set_attr(p, attr_orig_char, pc)
61       end
62       return p
63    end
64    suppress_hyphenate_ja_aux[id_math] = function(p) return node_end_of_math(node_next(p)) end
65    suppress_hyphenate_ja_aux[id_whatsit] = function(p)
66       if getsubtype(p)==sid_user and getfield(p, 'user_id')==STCK then
67          wt[#wt+1] = p; head = node_remove(head, p)
68       end
69       return p
70    end
71
72    local function suppress_hyphenate_ja (h)
73       local p = to_direct(h)
74       wt, head = {}, p
75       while p do
76          local pfunc = suppress_hyphenate_ja_aux[getid(p)]
77          if pfunc then p = pfunc(p) end
78          p = node_next(p)
79       end
80       head = to_node(head)
81       lang.hyphenate(head)
82       return head
83    end
84    
85    luatexbase.add_to_callback('hyphenate', 
86                               function (head,tail)
87                                  return suppress_hyphenate_ja(head)
88                               end,'ltj.hyphenate')
89 end
90
91 -- mode: true iff this function is called from hpack_filter
92 local function set_box_stack_level(head, mode)
93    local box_set, cl = 0, tex.currentgrouplevel + 1
94    for _,p  in pairs(wt) do
95       if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
96    end
97    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
98    return head
99 end
100
101 -- CALLBACKS
102 luatexbase.add_to_callback('hpack_filter', 
103    function (head)
104      return set_box_stack_level(head, true)
105    end,'ltj.hpack_filter_pre',1)
106 luatexbase.add_to_callback('pre_linebreak_filter', 
107   function (head)
108      return set_box_stack_level(head, false)
109   end,'ltj.pre_linebreak_filter_pre',1)
110
111 luatexja.pretreat = {
112    set_box_stack_level = set_box_stack_level,
113 }