OSDN Git Service

First attempt for new luaotfload/luatexbase
[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('jfont');     local ltjf = luatexja.jfont
7 luatexja.load_module('stack');     local ltjs = luatexja.stack
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 local fonts_ids = fonts.ids
19
20 local id_glyph = node.id('glyph')
21 local id_math = node.id('math')
22 local id_whatsit = node.id('whatsit')
23 local sid_user = node.subtype('user_defined')
24
25 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
26 local attr_icflag = luatexbase.attributes['ltj@icflag']
27
28 local ltjf_font_metric_table = ltjf.font_metric_table
29 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
30 local attr_orig_char = luatexbase.attributes['ltj@origchar']
31 local STCK = luatexja.userid_table.STCK
32
33 local fwglyph = {
34    ["Japan1"] = {
35       [0x00A4] = 16280,
36       [0x00A9] =  8059,
37       [0x00AC] =  8008,
38       [0x00AE] =  8060,
39       [0x00B5] = 12093,
40       [0x00BC] =  8185,
41       [0x00BD] =  8184,
42       [0x00BE] =  9783,
43       [0x2012] = 16206,
44       [0x201C] =   672,
45       [0x201D] =   673,
46       [0x201E] =  8280,
47       [0x2022] = 12256,
48       [0x2026] =   668,
49       [0x20AC] =  9779,
50       [0x2122] = 11853,
51       [0x2127] = 16204,
52       [0x2153] =  9781,
53       [0x2154] =  9782,
54       [0x2155] =  9784,
55       [0x215B] =  9796,
56       [0x215C] =  9797,
57       [0x215D] =  9798,
58       [0x215E] =  9799,
59       [0x2209] = 16299,
60       [0x2225] = 16196,
61       [0x2226] = 16300,
62       [0x2245] = 16301,
63       [0x2248] = 16302,
64       [0x2262] = 16303,
65       [0x2276] = 16304,
66       [0x2277] = 16305,
67       [0x2284] = 16306,
68       [0x2285] = 16307,
69       [0x228A] = 16308,
70       [0x228B] = 16309,
71       [0x22DA] = 16310,
72       [0x22DB] = 16311,
73       [0x2318] = 16271,
74    }
75 }
76
77
78 ------------------------------------------------------------------------
79 -- MAIN PROCESS STEP 1: replace fonts
80 ------------------------------------------------------------------------
81 local wt
82
83 local function suppress_hyphenate_ja(head)
84    local non_math, p = true, head
85    wt = {}
86    while p do
87       local pid = p.id
88       if pid == id_glyph then
89          if (has_attr(p, attr_icflag) or 0)<=0 and ltjc_is_ucs_in_japanese_char(p) then
90             local pf = has_attr(p, attr_curjfnt) or p.font
91             p.font = pf
92             p.subtype = floor(p.subtype*0.5)*2
93             set_attr(p, attr_orig_char, p.char)
94             if ltjf_font_metric_table[pf] and ltjf_font_metric_table[pf].mono_flag then
95                local pfd = fonts_ids[pf]
96                local pco = pfd.cidinfo.ordering
97                for i,v in pairs(fwglyph) do
98                   if pco == i and pfd.shared.resources then
99                      -- print(pf, pfd.fullname, pfd)
100                      -- for j,w in pairs(pfd.resources) do
101                      --         print('["' .. j .. '"] = ' .. tostring(w))
102                      --         if type(w) == 'table' and j~='characters' then
103                      --            for k,x in pairs(w) do
104                      --               print('    ["' .. k .. '"] = ' .. tostring(x))
105                      --            end
106                      --         end
107                      -- end
108                      local fwc = pfd.shared.resources.unicodes[pco .. '.'.. tostring(v[p.char])]
109                      if fwc then p.char = fwc end
110                      break
111                   end
112                end
113             end
114          end
115       elseif pid == id_math then 
116          p = node_next(p) -- skip math on
117          while p and p.id~=id_math do p = node_next(p) end
118       elseif pid == id_whatsit and p.subtype==sid_user and p.user_id==STCK then
119          wt[#wt+1] = p; head = node_remove(head, p)
120       end
121       p = node_next(p)
122    end
123    lang.hyphenate(head)
124    return head
125 end
126
127 -- mode: true iff this function is called from hpack_filter
128 local function set_box_stack_level(head, mode)
129    local box_set, cl = 0, tex.currentgrouplevel + 1
130    for _,p  in pairs(wt) do
131       if mode and p.value==cl then box_set = 1 end; node_free(p)
132    end
133    ltjs.report_stack_level(tex_getcount('ltj@@stack') + box_set)
134    return head
135 end
136
137 -- CALLBACKS
138 luatexbase.add_to_callback('hpack_filter', 
139    function (head)
140      return set_box_stack_level(head, true)
141    end,'ltj.hpack_filter_pre',1)
142 luatexbase.add_to_callback('pre_linebreak_filter', 
143   function (head)
144      return set_box_stack_level(head, false)
145   end,'ltj.pre_linebreak_filter_pre',1)
146 luatexbase.add_to_callback('hyphenate', 
147  function (head,tail)
148     return suppress_hyphenate_ja(head)
149  end,'ltj.hyphenate')
150
151 luatexja.pretreat = {
152    set_box_stack_level = set_box_stack_level,
153 }