OSDN Git Service

transformation of kana seq. into LuaTeX-ja itself.
[luatex-ja/luatexja.git] / src / ltj-inputbuf.lua
1 --
2 -- ltj-inputbuf.lua
3 --
4
5 luatexja.load_module 'base';      local ltjb = luatexja.base
6 luatexja.load_module 'charrange'; local ltjc = luatexja.charrange
7
8 local utflen = utf.len
9 local utfbyte = utf.byte
10 local utfchar = utf.char
11 local node_new = node.new
12 local node_free = node.free
13 local id_glyph = node.id 'glyph'
14 local getcatcode, getcount = tex.getcatcode, tex.getcount
15 local ltjc_is_japanese_char_curlist = ltjc.is_japanese_char_curlist
16
17 local substituter
18 do
19     local uchar = utf.char
20     local cd, cp = uchar(0x3099), uchar(0x309A)
21     substituter = (utf.substituter or utf.subtituter)      -- typo in lualibs?
22     {
23       ['ウ'..cd] = 'ヴ', ['う'..cd] = uchar(0x30F4),
24       ['か'..cd] = 'が', ['カ'..cd] = 'ガ',
25       ['き'..cd] = 'ぎ', ['キ'..cd] = 'ギ',
26       ['く'..cd] = 'ぐ', ['ク'..cd] = 'グ',
27       ['け'..cd] = 'げ', ['ケ'..cd] = 'ゲ',
28       ['こ'..cd] = 'ご', ['コ'..cd] = 'ゴ',
29       --
30       ['さ'..cd] = 'ざ', ['サ'..cd] = 'ザ',
31       ['し'..cd] = 'じ', ['シ'..cd] = 'ジ',
32       ['す'..cd] = 'ず', ['ス'..cd] = 'ズ',
33       ['せ'..cd] = 'ぜ', ['セ'..cd] = 'ゼ',
34       ['そ'..cd] = 'ぞ', ['ソ'..cd] = 'ゾ',
35       --
36       ['た'..cd] = 'だ', ['タ'..cd] = 'ダ',
37       ['ち'..cd] = 'ぢ', ['チ'..cd] = 'ヂ',
38       ['つ'..cd] = 'づ', ['ツ'..cd] = 'ヅ',
39       ['て'..cd] = 'で', ['テ'..cd] = 'デ',
40       ['と'..cd] = 'ど', ['ト'..cd] = 'ド',
41       --
42       ['は'..cd] = 'ば', ['ハ'..cd] = 'バ', ['は'..cp] = 'ぱ', ['ハ'..cp] = 'パ',
43       ['ひ'..cd] = 'び', ['ヒ'..cd] = 'ビ', ['ひ'..cp] = 'ぴ', ['ヒ'..cp] = 'ピ',
44       ['ふ'..cd] = 'ぶ', ['フ'..cd] = 'ブ', ['ふ'..cp] = 'ぷ', ['フ'..cp] = 'プ',
45       ['へ'..cd] = 'べ', ['ヘ'..cd] = 'ベ', ['へ'..cp] = 'ぺ', ['ヘ'..cp] = 'ペ',
46       ['ほ'..cd] = 'ぼ', ['ホ'..cd] = 'ボ', ['ほ'..cp] = 'ぽ', ['ホ'..cp] = 'ポ',
47       --
48       ['ゝ'..cd] = 'ゞ', ['ヽ'..cd] = 'ヾ',
49       ['ワ'..cd] = uchar(0x30F7), ['ヰ'..cd] = uchar(0x30F8),
50       ['ヱ'..cd] = uchar(0x30F9), ['ヲ'..cd] = uchar(0x30FA),
51     }
52 end
53
54 --- the following function is modified from jafontspec.lua (by K. Maeda).
55 --- Instead of "%", we use U+FFFFF for suppressing spaces.
56 --DEBUG require"socket"
57 local time_line = 0
58 local start_time_measure, stop_time_measure
59    = ltjb.start_time_measure, ltjb.stop_time_measure
60 local function add_comment(buffer)
61    start_time_measure 'inputbuf'; buffer = substituter(buffer)
62    local i = utflen(buffer)
63    local c = utfbyte(buffer, i)
64    while (i>0) and (getcatcode(c)==1 or getcatcode(c)==2) do
65       i=i-1; if (i>0) then c = utfbyte(buffer, i) end;
66    end
67    if i>0 then
68       if c>=0x80 then
69          local te = tex.endlinechar
70          -- Is the catcode of endline character is 5 (end-of-line)?
71          if (te ~= -1) and (getcatcode(te)==5) then
72             local ct = getcatcode(c)
73             if (ct==11) or (ct==12) then
74                local lec = getcount 'ltjlineendcomment'
75                -- Is the catcode of \ltjlineendcomment (new comment char) is 14 (comment)?
76                if ltjc_is_japanese_char_curlist(c) and (getcatcode(lec)==14) then
77                   stop_time_measure 'inputbuf'; return buffer .. utfchar(lec)
78                end
79             end
80          end
81       end
82    end
83    stop_time_measure 'inputbuf'
84    return buffer
85 end
86
87 luatexbase.add_to_callback('process_input_buffer',
88    add_comment,'ltj.process_input_buffer')
89
90 --EOF