OSDN Git Service

Merge branch 'vert_vrt2' into kitagawa_test
[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 local stack_table_index = luatexja.stack_table_index
8
9 -- load jisx0208 table
10 local cache_ver = 2
11
12 local cache_outdate_fn = function (t) return t.version~=cache_ver end
13 local jisx0208 = ltjb.load_cache('ltj-jisx0208',cache_outdate_fn)
14 if not jisx0208 then -- make cache
15    jisx0208 = require('ltj-jisx0208.lua')
16    ltjb.save_cache_luc('ltj-jisx0208', jisx0208)
17 end
18
19
20 -- \kuten, \jis, \euc, \sjis, \ucs, \kansuji
21 local function to_kansuji(num)
22    if not num then num=0; return
23    elseif num<0 then
24       num = -num; tex.write('-')
25    end
26    local s = ""
27    while num~=0 do
28       s = utf.char(
29          ltjs.get_stack_table(luatexja.stack_table_index.KSJ + num%10,
30                                 '', tex.getcount('ltj@@stack'))) .. s
31       num=math.floor(num/10)
32    end
33    tex.write(s)
34 end
35
36 -- \ucs: 単なる identity
37 local function from_ucs(i)
38    if type(i)~='number' then
39       ltjb.package_error('luatexja',
40                          "invalid character code (".. tostring(i) .. ")",
41                          "I'm going to use 0 instead of that illegal character code.")
42       i=0
43    end
44    tex.write(i)
45 end
46
47 -- \kuten: 面区点 (それぞれで16進2桁を使用)=> Unicode 符号位置
48 local function from_kuten(i)
49    if type(i)~='number' then
50       ltjb.package_error('luatexja',
51                          "invalid character code (".. tostring(i) .. ")",
52                          "I'm going to use 0 instead of that illegal character code.")
53       i=0
54    end
55    tex.write(tostring(jisx0208.table_jisx0208_uptex[i] or 0))
56 end
57
58 -- \euc: EUC-JP による符号位置 => Unicode 符号位置
59 local function from_euc(i)
60    if type(i)~='number' 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    elseif i>=0x10000 or i<0xa0a0 then
66       i=0
67    end
68    from_kuten(i-0xa0a0)
69 end
70
71 -- \jis: ISO-2022-JP による符号位置 => Unicode 符号位置
72 local function from_jis(i)
73    if (type(i)~='number') or i>=0x10000 or i<0 then
74       ltjb.package_error('luatexja',
75                          "invalid character code (".. tostring(i) .. ")",
76                          "I'm going to use 0 instead of that illegal character code.")
77       i=0
78    end
79    from_kuten(i-0x2020)
80 end
81
82 -- \sjis: Shift_JIS による符号位置 => Unicode 符号位置
83 local function from_sjis(i)
84    if (type(i)~='number') or i>=0x10000 or i<0 then
85       ltjb.package_error('luatexja',
86                          "invalid character code (".. tostring(i) .. ")",
87                          "I'm going to use 0 instead of that illegal character code.")
88       tex.write('0'); return
89    end
90    local c2 = math.floor(i/256)
91    local c1 = i%256
92    local shift_jisx0213_s1a3_table = {
93       { [false]= 1, [true]= 8},
94       { [false]= 3, [true]= 4},
95       { [false]= 5, [true]=12},
96       { [false]=13, [true]=14},
97       { [false]=15 } }
98    if c2 >= 0x81 then
99       if c2 >= 0xF0 then -- this if block won't be true
100          if (c2 <= 0xF3 or (c2 == 0xF4 and c1 < 0x9F)) then
101             c2 = 0x100 + shift_jisx0213_s1a3_table[c2 - 0xF0 + 1][(0x9E < c1)];
102          else -- 78<=k<=94
103             c2 = c2 * 2 - 413 + 0x100; if 0x9E < c1 then c2=c2+1 end
104          end
105      else
106         if c2<=0x9f then i=0x101 else i=0x181 end
107         c2 = c2 + c2 - i; if 0x9E < c1 then c2=c2+1 end
108      end
109      if c1 < 0x9F then
110         if c1>0x7f then i=0x40 else i=0x3f end
111         c1 = c1 - i
112      else
113         c1 = c1 - 0x9e
114      end
115      from_kuten(c2*256+c1)
116   end
117 end
118
119 luatexja.binary_pars.kansujichar = function(c, t)
120    if type(c)~='number' or c<0 or c>9 then
121       ltjb.package_error('luatexja',
122                          'Invalid KANSUJI number (' .. tostring(c) .. ')',
123                          'A KANSUJI number should be in the range 0..9.\n'..
124                             'So I changed this one to zero.')
125       c=0
126    end
127    return ltjs.get_stack_table(stack_table_index.KSJ + c, 0, t)
128 end
129
130
131 local t = {
132    from_euc   = from_euc,
133    from_kuten = from_kuten,
134    from_jis   = from_jis,
135    from_sjis  = from_sjis,
136    from_ucs   = from_ucs,
137    to_kansuji = to_kansuji,
138 }
139 luatexja.compat = t