summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/luatexja/ltj-compat.lua
blob: 6f66b99891ac9ea081ffb0769d6c68ead65ce77c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
--
-- luatexja/ltj-compat.lua
--

luatexja.load_module('base');   local ltjb = luatexja.base
luatexja.load_module('stack');  local ltjs = luatexja.stack

-- \kuten, \jis, \euc, \sjis, \ucs, \kansuji
local function to_kansuji(num)
   if not num then num=0; return
   elseif num<0 then 
      num = -num; tex.write('-')
   end
   local s = ""
   while num~=0 do
      s = utf.char(
	 ltjs.get_penalty_table(luatexja.stack_table_index.KSJ + num%10,
				'', tex.getcount('ltj@@stack'))) .. s
      num=math.floor(num/10)
   end
   tex.write(s)
end

-- \ucs: 単なる identity
local function from_ucs(i)
   if type(i)~='number' then 
      ltjb.package_error('luatexja',
			 "invalid character code (".. tostring(i) .. ")",
			 "I'm going to use 0 instead of that illegal character code.")
      i=0 
   end
   tex.write(i)
end

-- \kuten: 面区点 (それぞれで16進2桁を使用)=> Unicode 符号位置
local function from_kuten(i)
   if type(i)~='number' then 
      ltjb.package_error('luatexja',
			 "invalid character code (".. tostring(i) .. ")",
			 "I'm going to use 0 instead of that illegal character code.")
      i=0 
   end
   tex.write(tostring(luatexja.jisx0208.table_jisx0208_uptex[i] or 0))
end

-- \euc: EUC-JP による符号位置 => Unicode 符号位置
local function from_euc(i)
   if type(i)~='number' then 
      ltjb.package_error('luatexja',
			 "invalid character code (".. tostring(i) .. ")",
			 "I'm going to use 0 instead of that illegal character code.")
      i=0
   elseif i>=0x10000 or i<0xa0a0 then 
      i=0
   end
   from_kuten(i-0xa0a0)
end

-- \jis: ISO-2022-JP による符号位置 => Unicode 符号位置
local function from_jis(i)
   if (type(i)~='number') or i>=0x10000 or i<0 then 
      ltjb.package_error('luatexja',
			 "invalid character code (".. tostring(i) .. ")",
			 "I'm going to use 0 instead of that illegal character code.")
      i=0
   end
   from_kuten(i-0x2020)
end

-- \sjis: Shift_JIS による符号位置 => Unicode 符号位置
local function from_sjis(i)
   if (type(i)~='number') or i>=0x10000 or i<0 then 
      ltjb.package_error('luatexja',
			 "invalid character code (".. tostring(i) .. ")",
			 "I'm going to use 0 instead of that illegal character code.")
      tex.write('0'); return 
   end
   local c2 = math.floor(i/256)
   local c1 = i%256
   local shift_jisx0213_s1a3_table = {
      { [false]= 1, [true]= 8}, 
      { [false]= 3, [true]= 4}, 
      { [false]= 5, [true]=12}, 
      { [false]=13, [true]=14}, 
      { [false]=15 } }
   if c2 >= 0x81 then
      if c2 >= 0xF0 then -- this if block won't be true
	 if (c2 <= 0xF3 or (c2 == 0xF4 and c1 < 0x9F)) then
	    c2 = 0x100 + shift_jisx0213_s1a3_table[c2 - 0xF0 + 1][(0x9E < c1)];
	 else -- 78<=k<=94
	    c2 = c2 * 2 - 413 + 0x100; if 0x9E < c1 then c2=c2+1 end
	 end
     else
	if c2<=0x9f then i=0x101 else i=0x181 end
	c2 = c2 + c2 - i; if 0x9E < c1 then c2=c2+1 end
     end
     if c1 < 0x9F then
	if c1>0x7f then i=0x40 else i=0x3f end
	c1 = c1 - i
     else
	c1 = c1 - 0x7e
     end
     from_kuten(c2*256+c1)
  end
end

local t = {
   from_euc   = from_euc,
   from_kuten = from_kuten,
   from_jis   = from_jis,
   from_sjis  = from_sjis,
   from_ucs   = from_ucs,
   to_kansuji = to_kansuji,
}
luatexja.compat = t