OSDN Git Service

899a5eec0d583f068480b825e3967ce57593583c
[putex/putex.git] / src / texsourc / libtoua / kanji.c
1 /*
2  *  KANJI Code conversion routines.
3  *  (for pTeX and e-pTeX)
4  */
5
6 #include "kanji.h"
7
8 #if !defined(WIN32)
9   int sjisterminal;
10 #endif
11
12 /* TOKEN */
13 boolean check_kanji(integer c)
14 {
15   return is_char_kanji(c);
16 }
17
18 boolean is_char_ascii(integer c)
19 {
20   return (0 <= c && c < 0x100);
21 }
22
23 boolean is_char_kanji(integer c)
24 {
25   return (iskanji1(Hi(c)) && iskanji2(Lo(c)));
26 }
27
28 boolean ismultiprn(integer c)
29 {
30   if (iskanji1(c) || iskanji2(c))
31     return true;
32
33   return false;
34 }
35
36 #ifdef OLDSTYLE
37 integer calc_pos(integer c)
38 {
39   int c1, c2;
40
41   if (c < 256)
42     return (c << 1);
43
44   c1 = c >> 8;
45   c2 = c & 0xff;
46
47   if (c1)
48   {
49     if (is_internalSJIS())
50       return ((c2 + (c2 << (c1 - 0x81)) & 0xff) << 1);
51     else
52       return ((c2 + (c2 << (c1 - 0xa1)) & 0xff) << 1);
53   }
54   else
55     return (((c2 + c2 + 1) & 0xff) << 1);
56 }
57 #else /* OLDSTYLE */
58 integer calc_pos(integer c)
59 {
60   unsigned char c1, c2;
61
62   if(c >= 0 && c <= 255)
63     return (c);
64
65   c1 = (c >> 8) & 0xff;
66   c2 = c & 0xff;
67
68   if(iskanji1(c1))
69   {
70     if (is_internalSJIS())
71     {
72       c1 = ((c1 - 0x81) % 4) * 64;  /* c1 = 0, 64, 128, 192 */
73       c2 = c2 % 64;                 /* c2 = 0..63 */
74     }
75     else
76     {
77       c1 = ((c1 - 0xa1) % 4) * 64;  /* c1 = 0, 64, 128, 192 */
78       c2 = c2 % 64;                 /* c2 = 0..63 */
79     }
80     return (c1 + c2);              /* ret = 0..255 */
81   }
82   else
83     return (c2);
84 }
85 #endif /* OLDSTYLE */
86
87 integer kcatcodekey(integer c)
88 {
89   return Hi(toDVI(c));
90 }
91
92 void init_default_kanji (const_string file_str, const_string internal_str)
93 {
94   char *p;
95
96   enable_UPTEX (false); /* disable */
97
98   if (!set_enc_string (file_str, internal_str))
99   {
100     fprintf (stderr, "Bad kanji encoding \"%s\" or \"%s\".\n",
101         file_str ? file_str  : "NULL",
102         internal_str ? internal_str : "NULL");
103     uexit(1);
104   }
105
106   p = getenv ("PTEX_KANJI_ENC");
107
108   if (p)
109   {
110     if (!set_enc_string (p, NULL))
111       fprintf (stderr, "Ignoring bad kanji encoding \"%s\".\n", p);
112   }
113
114 #ifdef WIN32
115   p = kpse_var_value ("guess_input_kanji_encoding");
116
117   if (p)
118   {
119     if (*p == '1' || *p == 'y' || *p == 't')
120       infile_enc_auto = 1;
121
122     free(p);
123   }
124 #endif
125 }