OSDN Git Service

f527f341c71ce39fb8d257334c181c231da2db2c
[luatex-ja/luatexja.git] / src / ltj-inputbuf.lua
1 --
2 -- luatexja/inputbuf.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.inputbuf',
6   date = '2011/04/01',
7   version = '0.1',
8   description = 'Supressing a space by newline after Japanese characters',
9 })
10 module('luatexja.inputbuf', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 luatexja.load_module('charrange'); local ltjc = luatexja.charrange
14
15 local utf = unicode.utf8
16 local node_new = node.new
17 local id_glyph = node.id('glyph')
18 local getcatcode = tex.getcatcode
19 local ltjc_is_ucs_in_japanese_char = ltjc.is_ucs_in_japanese_char
20
21 --- the following function is modified from jafontspec.lua (by K. Maeda).
22 --- Instead of "%", we use U+FFFFF for suppressing spaces.
23 function add_comment(buffer)
24    local i = utf.len(buffer)
25    while (i>0) and (getcatcode(utf.byte(buffer, i))==1 
26                  or getcatcode(utf.byte(buffer, i))==2) do
27       i=i-1
28    end
29    if i>0 then
30       local c = utf.byte(buffer, i)
31       if c>0x80 then
32          local ct = getcatcode(c)
33          local te = tex.endlinechar
34          local ctl = (te ~= -1) and (getcatcode(te)==5) and (getcatcode(0xFFFFF)==14)
35          -- Is the catcode of endline character is 5 (end-of-line)?
36          -- Is the catcode of U+FFFFF (new comment char) is 14 (comment)?
37          if ((ct==11) or (ct==12)) and ctl then
38             local p = node_new(id_glyph)
39             p.char = c
40             if ltjc_is_ucs_in_japanese_char(p) then
41                buffer = buffer .. string.char(0xF3,0xBF,0xBF,0xBF) -- U+FFFFF
42             end
43             node.free(p)
44          end
45       end
46    end
47    return buffer
48 end
49
50 luatexbase.add_to_callback('process_input_buffer', 
51    add_comment,'ltj.process_input_buffer')
52
53 --EOF