OSDN Git Service

Merge branch 'zh-maqiyuan' into kitagawa_test
[luatex-ja/luatexja.git] / src / ltj-compat.lua
1 --
2 -- luatexja/compat.lua
3 --
4 luatexbase.provides_module({
5   name = 'luatexja.compat',
6   date = '2011/06/03',
7   version = '0.1',
8   description = 'Partial implementation of primitives of pTeX',
9 })
10 module('luatexja.compat', package.seeall)
11 local err, warn, info, log = luatexbase.errwarinf(_NAME)
12
13 luatexja.load_module('base');      local ltjb = luatexja.base
14 luatexja.load_module('stack');     local ltjs = luatexja.stack
15
16 -- \kuten, \jis, \euc, \sjis, \ucs, \kansuji
17 function to_kansuji(num)
18    if not num then num=0; return
19    elseif num<0 then 
20       num = -num; tex.write('-')
21    end
22    local s = ""
23    while num~=0 do
24       s = utf.char(
25          ltjs.get_penalty_table('ksj', num%10,
26                                 '', tex.getcount('ltj@@stack'))) .. s
27       num=math.floor(num/10)
28    end
29    tex.write(s)
30 end
31
32 -- \ucs: 単なる identity
33 function from_ucs(i)
34    tex.write(i)
35 end
36
37 -- \kuten: 面区点 (それぞれで16進2桁を使用)=> Unicode 符号位置
38 function from_kuten(i)
39    if not i then i=0 end
40    tex.write(tostring(luatexja.jisx0208.table_jisx0208_uptex[i] or 0))
41 end
42
43 -- \euc: EUC-JP による符号位置 => Unicode 符号位置
44 function from_euc(i)
45    if not i then i=0
46    elseif i>=0x10000 or i<0xa0a0 then 
47       i=0
48    end
49    from_kuten(i-0xa0a0)
50 end
51
52 -- \jis: ISO-2022-JP による符号位置 => Unicode 符号位置
53 function from_jis(i)
54    if (not i) or i>=0x10000 or i<0 then 
55       i=0
56    end
57    from_kuten(i-0x2020)
58 end
59
60 -- \sjis: Shift_JIS による符号位置 => Unicode 符号位置
61 function from_sjis(i)
62    if (not i) or i>=0x10000 or i<0 then 
63       tex.write('0'); return 
64    end
65    local c2 = math.floor(i/256)
66    local c1 = i%256
67    local shift_jisx0213_s1a3_table = {
68       { [false]= 1, [true]= 8}, 
69       { [false]= 3, [true]= 4}, 
70       { [false]= 5, [true]=12}, 
71       { [false]=13, [true]=14}, 
72       { [false]=15 } }
73    if c2 >= 0x81 then
74       if c2 >= 0xF0 then -- this if block won't be true
75          if (c2 <= 0xF3 or (c2 == 0xF4 and c1 < 0x9F)) then
76             c2 = 0x100 + shift_jisx0213_s1a3_table[c2 - 0xF0 + 1][(0x9E < c1)];
77          else -- 78<=k<=94
78             c2 = c2 * 2 - 413 + 0x100; if 0x9E < c1 then c2=c2+1 end
79          end
80      else
81         if c2<=0x9f then i=0x101 else i=0x181 end
82         c2 = c2 + c2 - i; if 0x9E < c1 then c2=c2+1 end
83      end
84      if c1 < 0x9F then
85         if c1>0x7f then i=0x40 else i=0x3f end
86         c1 = c1 - i
87      else
88         c1 = c1 - 0x7e
89      end
90      from_kuten(c2*256+c1)
91   end
92 end