OSDN Git Service

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