OSDN Git Service

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