OSDN Git Service

ltj-jfont.lua: use luatexbase.remove_from_callback to get fontloader
[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 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
21 local pairs = pairs
22 local floor = math.floor
23 local has_attr = node.direct.has_attribute
24 local set_attr = node.direct.set_attribute
25 local node_traverse = node.direct.traverse
26 local node_remove = node.direct.remove
27 local node_next =  node.direct.getnext
28 local node_free = node.direct.free
29 local node_end_of_math = node.direct.end_of_math
30 local tex_getcount = tex.getcount
31
32 local id_glyph = node.id('glyph')
33 local id_math = node.id('math')
34 local id_whatsit = node.id('whatsit')
35 local sid_user = node.subtype('user_defined')
36
37 local attr_dir = luatexbase.attributes['ltj@dir']
38 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
39 local attr_curtfnt = luatexbase.attributes['ltj@curtfnt']
40 local attr_icflag = luatexbase.attributes['ltj@icflag']
41
42 local is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char_direct
43 local ltjs_orig_char_table = ltjs.orig_char_table
44 local ltjf_replace_altfont = ltjf.replace_altfont
45 local attr_orig_char = luatexbase.attributes['ltj@origchar']
46 local STCK  = luatexja.userid_table.STCK
47 local DIR   = luatexja.userid_table.DIR
48 local PROCESSED_BEGIN_FLAG = luatexja.icflag_table.PROCESSED_BEGIN_FLAG
49
50 local dir_tate = luatexja.dir_table.dir_tate
51 local lang_ja = luatexja.lang_ja
52
53 ------------------------------------------------------------------------
54 -- MAIN PROCESS STEP 1: replace fonts
55 ------------------------------------------------------------------------
56 local wt, wtd = {}, {}
57 do
58    local ltjd_get_dir_count = ltjd.get_dir_count
59    local start_time_measure, stop_time_measure
60       = ltjb.start_time_measure, ltjb.stop_time_measure
61    local head
62    local suppress_hyphenate_ja_aux = {
63       [id_math] = function(p) return node_next(node_end_of_math(node_next(p))) end,
64       [id_whatsit] = function(p)
65          if getsubtype(p)==sid_user then
66             local uid = getfield(p, 'user_id')
67             if uid==STCK then
68                wt[#wt+1] = p; node_remove(head, p)
69             elseif uid==DIR then
70                if has_attr(p, attr_icflag)<PROCESSED_BEGIN_FLAG  then
71                   ltjs.list_dir = has_attr(p, attr_dir)
72                else -- こっちのケースは通常使用では起こらない
73                   wtd[#wtd+1] = p; node_remove(head, p)
74                end
75             end
76          end
77          return node_next(p)
78       end,
79    }
80    setmetatable(suppress_hyphenate_ja_aux, 
81                 {
82                    __index = function() return node_next end,
83                 })
84    local function suppress_hyphenate_ja (h)
85       start_time_measure('ltj_hyphenate')
86       head = to_direct(h)
87       for i = 1,#wt do wt[i]=nil end
88       for i = 1,#wtd do wtd[i]=nil end
89       for i,_ in pairs(ltjs_orig_char_table) do
90          ltjs_orig_char_table[i] = nil
91       end
92       ltjs.list_dir=ltjd_get_dir_count()
93       do
94          local p = head
95          while p do
96             local pid = getid(p)
97             while pid==id_glyph do
98                local pc = getchar(p)
99                if (has_attr(p, attr_icflag) or 0)<=0 and is_ucs_in_japanese_char(p, pc) then
100                   setfield(p, 'font', 
101                            ltjf_replace_altfont(has_attr(p, attr_curjfnt) or getfont(p), pc))
102                   setfield(p, 'lang', lang_ja)
103                   ltjs_orig_char_table[p] = pc
104                end
105                p = node_next(p); pid = getid(p)
106             end
107             p = (suppress_hyphenate_ja_aux[pid])(p)
108          end
109       end
110       stop_time_measure('ltj_hyphenate'); start_time_measure('tex_hyphenate')
111       lang.hyphenate(h, nil)
112       stop_time_measure('tex_hyphenate')
113       return h
114    end
115
116    ltjb.add_to_callback('hyphenate',
117                               function (head,tail)
118                                  return suppress_hyphenate_ja(head)
119                               end,'ltj.hyphenate')
120 end
121
122 -- mode: true iff this function is called from hpack_filter
123 local ltjs_report_stack_level = ltjs.report_stack_level
124 local ltjf_vert_form_table    = ltjf.vert_form_table
125 local ltjf_font_metric_table  = ltjf.font_metric_table
126 local font_getfont = font.getfont
127 local function set_box_stack_level(head, mode)
128    local box_set, cl = 0, tex.currentgrouplevel + 1
129    for _,p  in pairs(wt) do
130       if mode and getfield(p, 'value')==cl then box_set = 1 end; node_free(p)
131    end
132    ltjs_report_stack_level(tex_getcount('ltj@@stack') + box_set)
133    for _,p  in pairs(wtd) do
134       node_free(p)
135    end
136    if ltjs.list_dir == dir_tate then
137       for p in node.direct.traverse_id(id_glyph,to_direct(head)) do
138          if (has_attr(p, attr_icflag) or 0)<=0 and getfield(p, 'lang')==lang_ja then
139             local pc = ltjs_orig_char_table[p]
140             local nf = ltjf_replace_altfont( has_attr(p, attr_curtfnt) or getfont(p) , pc)
141             setfield(p, 'font', nf)
142             if ltjf_font_metric_table[nf].vert_activated then
143                pc = ltjf_vert_form_table [getchar(p)]
144                if font_getfont(nf).characters[pc] then setfield(p, 'char', pc) end
145             end
146          end
147       end
148    end
149    return head
150 end
151
152 -- CALLBACKS
153 ltjb.add_to_callback('hpack_filter',
154    function (head)
155      return set_box_stack_level(head, true)
156    end,'ltj.set_stack_level',1)
157 ltjb.add_to_callback('pre_linebreak_filter',
158   function (head)
159      return set_box_stack_level(head, false)
160   end,'ltj.set_stack_level',1)
161
162 luatexja.pretreat = {
163    set_box_stack_level = set_box_stack_level,
164    orig_char_table = orig_char_table,
165 }