OSDN Git Service

Regenerated test*.pdf (and fixed several bugs).
[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(luatexja.stack_table_index.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    if type(i)~='number' then 
35       ltjb.package_error('luatexja',
36                          "invalid character code (".. tostring(i) .. ")",
37                          "I'm going to use 0 instead of that illegal character code.")
38       i=0 
39    end
40    tex.write(i)
41 end
42
43 -- \kuten: 面区点 (それぞれで16進2桁を使用)=> Unicharacter code 符号位置
44 function from_kuten(i)
45    if type(i)~='number' then 
46       ltjb.package_error('luatexja',
47                          "invalid character code (".. tostring(i) .. ")",
48                          "I'm going to use 0 instead of that illegal character code.")
49       i=0 
50    end
51    tex.write(tostring(luatexja.jisx0208.table_jisx0208_uptex[i] or 0))
52 end
53
54 -- \euc: EUC-JP による符号位置 => Unicharacter code 符号位置
55 function from_euc(i)
56    if type(i)~='number' then 
57       ltjb.package_error('luatexja',
58                          "invalid character code (".. tostring(i) .. ")",
59                          "I'm going to use 0 instead of that illegal character code.")
60       i=0
61    elseif i>=0x10000 or i<0xa0a0 then 
62       i=0
63    end
64    from_kuten(i-0xa0a0)
65 end
66
67 -- \jis: ISO-2022-JP による符号位置 => Unicharacter code 符号位置
68 function from_jis(i)
69    if (type(i)~='number') or i>=0x10000 or i<0 then 
70       ltjb.package_error('luatexja',
71                          "invalid character code (".. tostring(i) .. ")",
72                          "I'm going to use 0 instead of that illegal character code.")
73       i=0
74    end
75    from_kuten(i-0x2020)
76 end
77
78 -- \sjis: Shift_JIS による符号位置 => Unicharacter code 符号位置
79 function from_sjis(i)
80    if (type(i)~='number') or i>=0x10000 or i<0 then 
81       ltjb.package_error('luatexja',
82                          "invalid character code (".. tostring(i) .. ")",
83                          "I'm going to use 0 instead of that illegal character code.")
84       tex.write('0'); return 
85    end
86    local c2 = math.floor(i/256)
87    local c1 = i%256
88    local shift_jisx0213_s1a3_table = {
89       { [false]= 1, [true]= 8}, 
90       { [false]= 3, [true]= 4}, 
91       { [false]= 5, [true]=12}, 
92       { [false]=13, [true]=14}, 
93       { [false]=15 } }
94    if c2 >= 0x81 then
95       if c2 >= 0xF0 then -- this if block won't be true
96          if (c2 <= 0xF3 or (c2 == 0xF4 and c1 < 0x9F)) then
97             c2 = 0x100 + shift_jisx0213_s1a3_table[c2 - 0xF0 + 1][(0x9E < c1)];
98          else -- 78<=k<=94
99             c2 = c2 * 2 - 413 + 0x100; if 0x9E < c1 then c2=c2+1 end
100          end
101      else
102         if c2<=0x9f then i=0x101 else i=0x181 end
103         c2 = c2 + c2 - i; if 0x9E < c1 then c2=c2+1 end
104      end
105      if c1 < 0x9F then
106         if c1>0x7f then i=0x40 else i=0x3f end
107         c1 = c1 - i
108      else
109         c1 = c1 - 0x7e
110      end
111      from_kuten(c2*256+c1)
112   end
113 end