OSDN Git Service

Implemented \euc, \jis, and so on in luatex-compat.sty.
[luatex-ja/luatexja.git] / src / luatexja / 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 -- \kuten, \jis, \euc, \sjis, \ucs, \kansuji
14 function to_kansuji(num)
15    if not num then num=0; return
16    elseif num<0 then 
17       num = -num; tex.write('-')
18    end
19    local s = ""
20    while num~=0 do
21       s = utf.char(
22          luatexja.stack.get_penalty_table('ksj', num%10,
23                                           '', tex.getcount('ltj@@stack'))) .. s
24       num=math.floor(num/10)
25    end
26    tex.write(s)
27 end
28
29
30 -- jisx0213.lua: JIS X 0213:2004 から Unicode への対応テーブル.
31 -- 次のファイルより自動生成した:
32 --   "JIS X 0213:2004 8-bit code vs Unicode mapping table".
33 --   (by Project X0213, http://x0213.org/codetable/jisx0213-2004-8bit-std.txt)
34
35 -- \kuten: 面区点 (それぞれで16進2桁を使用)=> Unicode 符号位置
36 function from_kuten(i)
37    if not i then i=0 end
38    tex.write(luatexja.jisx0213.table_jisx0213_2004[i] or 0)
39 end
40
41 -- \euc: EUC-JIS-2004 による符号位置 => Unicode 符号位置
42 -- 第2面の文字は 0x8f で始まる 16進6桁で指定
43 -- JIS X 0201 (0x8e に続く1バイト)はサポートせず.
44 function from_euc(i)
45    -- EUC-JIS-2004: (8f)a1a1 => 1(2) 
46    if not i then i=0
47    elseif i>=0x8f0000 then 
48       i=i-0x8e0000 
49    elseif i>=0x10000 or i<0 then 
50       i=0
51    end
52    from_kuten(i-0xa0a0)
53 end
54
55 -- \jis: ISO-2011-JP-2004 による符号位置 => Unicode 符号位置
56 -- エスケープシーケンスはサポートせず,第1面の文字のみ指定可能.
57 function from_jis(i)
58    -- ISO-2022-JP
59    if (not i) or i>=0x10000 or i<0 then 
60       i=0
61    end
62    from_kuten(i-0x2020)
63 end
64
65 -- \sjis: Shift JIS-2004 による符号位置 => Unicode 符号位置
66 -- JIS X 0201 はサポートせず.
67 function from_sjis(i)
68    -- Shift JIS
69    if (not i) or i>=0x10000 or i<0 then 
70       tex.write('0'); return 
71    end
72    local c2 = math.floor(i/256)
73    local c1 = i%256
74    local shift_jisx0213_s1a3_table = {
75       { [false]= 1, [true]= 8}, 
76       { [false]= 3, [true]= 4}, 
77       { [false]= 5, [true]=12}, 
78       { [false]=13, [true]=14}, 
79       { [false]=15 } }
80    if c2 >= 0x81 then
81       if c2 >= 0xF0 then
82          if (c2 <= 0xF3 or (c2 == 0xF4 and c1 < 0x9F)) then
83            c2 = 0x100 + shift_jisx0213_s1a3_table[c2 - 0xF0 + 1][(0x9E < c1)];
84         else -- 78<=k<=94
85            c2 = c2 * 2 - 413 + 0x100; if 0x9E < c1 then c2=c2+1 end
86         end
87      else
88         if c2<=0x9f then i=0x101 else i=0x181 end
89         c2 = c2 + c2 - i; if 0x9E < c1 then c2=c2+1 end
90      end
91      if c1 < 0x9F then
92         if c1>0x7f then i=0x40 else i=0x3f end
93         c1 = c1 - i
94      else
95         c1 = c1 - 0x7e
96      end
97      from_kuten(c2*256+c1)
98   end
99 end