OSDN Git Service

eb63eb8ff07e82fe00cea3a5bccdef363719e57d
[luatex-ja/luatexja.git] / src / ltj-compat.lua
1 --
2 -- luatexja/ltj-compat.lua
3 --
4
5 luatexja.load_module('base');  local ltjb = luatexja.base
6
7 -- \kuten, \jis, \euc, \sjis, \ucs, \kansuji
8 local function to_kansuji(num)
9    if not num then num=0; return
10    elseif num<0 then 
11       num = -num; tex.write('-')
12    end
13    local s = ""
14    while num~=0 do
15       s = utf.char(
16          ltjs.get_penalty_table(luatexja.stack_table_index.KSJ + num%10,
17                                 '', tex.getcount('ltj@@stack'))) .. s
18       num=math.floor(num/10)
19    end
20    tex.write(s)
21 end
22
23 -- \ucs: 単なる identity
24 local function from_ucs(i)
25    if type(i)~='number' then 
26       ltjb.package_error('luatexja',
27                          "invalid character code (".. tostring(i) .. ")",
28                          "I'm going to use 0 instead of that illegal character code.")
29       i=0 
30    end
31    tex.write(i)
32 end
33
34 -- \kuten: 面区点 (それぞれで16進2桁を使用)=> Unicode 符号位置
35 local function from_kuten(i)
36    if type(i)~='number' then 
37       ltjb.package_error('luatexja',
38                          "invalid character code (".. tostring(i) .. ")",
39                          "I'm going to use 0 instead of that illegal character code.")
40       i=0 
41    end
42    tex.write(tostring(luatexja.jisx0208.table_jisx0208_uptex[i] or 0))
43 end
44
45 -- \euc: EUC-JP による符号位置 => Unicode 符号位置
46 local function from_euc(i)
47    if type(i)~='number' then 
48       ltjb.package_error('luatexja',
49                          "invalid character code (".. tostring(i) .. ")",
50                          "I'm going to use 0 instead of that illegal character code.")
51       i=0
52    elseif i>=0x10000 or i<0xa0a0 then 
53       i=0
54    end
55    from_kuten(i-0xa0a0)
56 end
57
58 -- \jis: ISO-2022-JP による符号位置 => Unicode 符号位置
59 local function from_jis(i)
60    if (type(i)~='number') or i>=0x10000 or i<0 then 
61       ltjb.package_error('luatexja',
62                          "invalid character code (".. tostring(i) .. ")",
63                          "I'm going to use 0 instead of that illegal character code.")
64       i=0
65    end
66    from_kuten(i-0x2020)
67 end
68
69 -- \sjis: Shift_JIS による符号位置 => Unicode 符号位置
70 local function from_sjis(i)
71    if (type(i)~='number') or i>=0x10000 or i<0 then 
72       ltjb.package_error('luatexja',
73                          "invalid character code (".. tostring(i) .. ")",
74                          "I'm going to use 0 instead of that illegal character code.")
75       tex.write('0'); return 
76    end
77    local c2 = math.floor(i/256)
78    local c1 = i%256
79    local shift_jisx0213_s1a3_table = {
80       { [false]= 1, [true]= 8}, 
81       { [false]= 3, [true]= 4}, 
82       { [false]= 5, [true]=12}, 
83       { [false]=13, [true]=14}, 
84       { [false]=15 } }
85    if c2 >= 0x81 then
86       if c2 >= 0xF0 then -- this if block won't be true
87          if (c2 <= 0xF3 or (c2 == 0xF4 and c1 < 0x9F)) then
88             c2 = 0x100 + shift_jisx0213_s1a3_table[c2 - 0xF0 + 1][(0x9E < c1)];
89          else -- 78<=k<=94
90             c2 = c2 * 2 - 413 + 0x100; if 0x9E < c1 then c2=c2+1 end
91          end
92      else
93         if c2<=0x9f then i=0x101 else i=0x181 end
94         c2 = c2 + c2 - i; if 0x9E < c1 then c2=c2+1 end
95      end
96      if c1 < 0x9F then
97         if c1>0x7f then i=0x40 else i=0x3f end
98         c1 = c1 - i
99      else
100         c1 = c1 - 0x7e
101      end
102      from_kuten(c2*256+c1)
103   end
104 end
105
106 local t = {
107    from_euc   = from_euc,
108    from_kuten = from_kuten,
109    from_jis   = from_jis,
110    from_sjis  = from_sjis,
111    from_ucs   = from_ucs,
112    to_kansuji = to_kansuji,
113 }
114 luatexja.compat = t