OSDN Git Service

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