OSDN Git Service

initial version of luatexja-zhfonts.sty.
[luatex-ja/luatexja.git] / src / ltj-inputbuf.lua
1 --
2 -- luatexja/ltj-inputbuf.lua
3 --
4
5 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
6
7 local utf = unicode.utf8
8 local node_new = node.new
9 local id_glyph = node.id('glyph')
10 local getcatcode = tex.getcatcode
11 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
12
13 --- the following function is modified from jafontspec.lua (by K. Maeda).
14 --- Instead of "%", we use U+FFFFF for suppressing spaces.
15 local function add_comment(buffer)
16    local i = utf.len(buffer)
17    while (i>0) and (getcatcode(utf.byte(buffer, i))==1 
18                  or getcatcode(utf.byte(buffer, i))==2) do
19       i=i-1
20    end
21    if i>0 then
22       local c = utf.byte(buffer, i)
23       if c>0x80 then
24          local ct = getcatcode(c)
25          local te = tex.endlinechar
26          local ctl = (te ~= -1) and (getcatcode(te)==5) and (getcatcode(0xFFFFF)==14)
27          -- Is the catcode of endline character is 5 (end-of-line)?
28          -- Is the catcode of U+FFFFF (new comment char) is 14 (comment)?
29          if ((ct==11) or (ct==12)) and ctl then
30             local p = node_new(id_glyph)
31             p.char = c
32             if ltjc_is_ucs_in_japanese_char(p) then
33                buffer = buffer .. string.char(0xF3,0xBF,0xBF,0xBF) -- U+FFFFF
34             end
35             node.free(p)
36          end
37       end
38    end
39    return buffer
40 end
41
42 luatexbase.add_to_callback('process_input_buffer', 
43    add_comment,'ltj.process_input_buffer')
44
45 --EOF