OSDN Git Service

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