OSDN Git Service

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