OSDN Git Service

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