OSDN Git Service

Merge branch 'master' into kitagawa_test
[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 floor = math.floor
10 local has_attr = node.has_attribute
11 local set_attr = node.set_attribute
12 local node_traverse = node.traverse
13 local node_type = node.type
14 local node_remove = node.remove
15 local node_next = node.next
16 local node_free = node.free
17 local tex_getcount = tex.getcount
18
19 local id_glyph = node.id('glyph')
20 local id_math = node.id('math')
21 local id_whatsit = node.id('whatsit')
22 local sid_user = node.subtype('user_defined')
23
24 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
25 local attr_icflag = luatexbase.attributes['ltj@icflag']
26
27 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
28 local ltjf_replace_altfont = ltjf.replace_altfont
29 local attr_orig_char = luatexbase.attributes['ltj@origchar']
30 local STCK = luatexja.userid_table.STCK
31
32 ------------------------------------------------------------------------
33 -- MAIN PROCESS STEP 1: replace fonts
34 ------------------------------------------------------------------------
35 local wt
36 do
37    local head
38    local end_math = node.end_of_math or 
39       function(p)
40          while p and p.id~=id_math do p = node_next(p) end
41          return p
42       end
43
44    local suppress_hyphenate_ja_aux = {}
45    suppress_hyphenate_ja_aux[id_glyph] = function(p)
46       if (has_attr(p, attr_icflag) or 0)<=0 and ltjc_is_ucs_in_japanese_char(p) then
47          local pc = p.char
48          local pf = ltjf_replace_altfont(has_attr(p, attr_curjfnt) or p.font, pc)
49          p.font = pf;  set_attr(p, attr_curjfnt, pf)
50          p.subtype = floor(p.subtype*0.5)*2
51          set_attr(p, attr_orig_char, pc)
52       end
53       return p
54    end
55    suppress_hyphenate_ja_aux[id_math] = function(p) return end_math(node_next(p)) end
56    suppress_hyphenate_ja_aux[id_whatsit] = function(p)
57       if p.subtype==sid_user and p.user_id==STCK then
58          wt[#wt+1] = p; head = node_remove(head, p)
59       end
60       return p
61    end
62
63    local function suppress_hyphenate_ja (h)
64       local p = h
65       wt, head = {}, h
66       while p do
67          local pfunc = suppress_hyphenate_ja_aux[p.id]
68          if pfunc then p = pfunc(p) end
69          p = node_next(p)
70       end
71       lang.hyphenate(head)
72       return head
73    end
74    
75    luatexbase.add_to_callback('hyphenate', 
76                               function (head,tail)
77                                  return suppress_hyphenate_ja(head)
78                               end,'ltj.hyphenate')
79 end
80
81 -- mode: true iff this function is called from hpack_filter
82 local function set_box_stack_level(head, mode)
83    local box_set, cl = 0, tex.currentgrouplevel + 1
84    for _,p  in pairs(wt) do
85       if mode and p.value==cl then box_set = 1 end; node_free(p)
86    end
87    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
88    return head
89 end
90
91 -- CALLBACKS
92 luatexbase.add_to_callback('hpack_filter', 
93    function (head)
94      return set_box_stack_level(head, true)
95    end,'ltj.hpack_filter_pre',1)
96 luatexbase.add_to_callback('pre_linebreak_filter', 
97   function (head)
98      return set_box_stack_level(head, false)
99   end,'ltj.pre_linebreak_filter_pre',1)
100
101 luatexja.pretreat = {
102    set_box_stack_level = set_box_stack_level,
103 }