Debian Codegraph @

texk/ptexenc/kanjicnv.c

raw
1 /*
2 * KANJI Code conversion routines.
3 */
4
5 #include <ptexenc/c-auto.h>
6 #include <ptexenc/kanjicnv.h>
7
8 boolean isJISkanji1(int c)
9 {
10 c &= 0xff;
11 return (0x21 <= c && c <= 0x7e);
12 }
13
14 boolean isJISkanji2(int c)
15 {
16 c &= 0xff;
17 return (0x21 <= c && c <= 0x7e);
18 }
19
20 boolean isEUCkanji1(int c)
21 {
22 c &= 0xff;
23 return (0xa1 <= c && c <= 0xfe);
24 }
25
26 boolean isEUCkanji2(int c)
27 {
28 c &= 0xff;
29 return (0xa1 <= c && c <= 0xfe);
30 }
31
32 boolean isSJISkanji1(int c)
33 {
34 c &= 0xff;
35 return ((0x81 <= c && c <= 0x9f) || (0xe0 <= c && c <= 0xfc));
36 }
37
38 boolean isSJISkanji2(int c)
39 {
40 c &= 0xff;
41 return (0x40 <= c && c <= 0xfc && c != 0x7f);
42 }
43
44 boolean isISO8859(int c)
45 {
46 c &= 0xff;
47 return (0xa0 <= c && c <= 0xff);
48 }
49
50 /* EUC <=> JIS X 0208 code conversion */
51 int EUCtoJIS(int kcode)
52 {
53 if ((kcode<=0 || kcode>=0x10000)) return 0;
54 if (!isEUCkanji1(HI(kcode))) return 0;
55 if (!isEUCkanji2(LO(kcode))) return 0;
56 return (kcode & 0x7f7f);
57 }
58
59 int JIStoEUC(int kcode)
60 {
61 if ((kcode<=0 || kcode>=0x10000)) return 0;
62 if (!isJISkanji1(HI(kcode))) return 0;
63 if (!isJISkanji2(LO(kcode))) return 0;
64 return (kcode | 0x8080);
65 }
66
67 /* Shift JIS <=> JIS Kanji code conversion */
68 int SJIStoJIS(int kcode)
69 {
70 int byte1, byte2;
71
72 if ((kcode<=0 || kcode>=0x10000)) return 0;
73 byte1 = HI(kcode); if (!isSJISkanji1(byte1)) return 0;
74 byte2 = LO(kcode); if (!isSJISkanji2(byte2)) return 0;
75 byte1 -= ( byte1>=0xa0 ) ? 0xc1 : 0x81;
76 kcode = ((byte1<<1) + 0x21)<<8;
77 if ( byte2 >= 0x9f ) {
78 kcode += 0x0100;
79 kcode |= (byte2 - 0x7e) & 0xff;
80 } else {
81 kcode |= (byte2 - ((byte2<=0x7e) ? 0x1f : 0x20 )) & 0xff;
82 }
83 return kcode;
84 }
85
86 int JIStoSJIS(int kcode)
87 {
88 int high, low;
89 int nh, nl;
90
91 if ((kcode<=0 || kcode>=0x10000)) return 0;
92 high = HI(kcode); if (!isJISkanji1(high)) return 0;
93 low = LO(kcode); if (!isJISkanji2(low)) return 0;
94 nh = ((high-0x21)>>1) + 0x81;
95 if (nh > 0x9f) nh += 0x40;
96 if (high & 1) {
97 nl = low + 0x1f;
98 if (low > 0x5f) nl++;
99 } else
100 nl = low + 0x7e;
101 return HILO(nh, nl);
102 }
103
104 /* Shift JIS <=> EUC Kanji code conversion */
105 int SJIStoEUC(int kcode)
106 {
107 return JIStoEUC(SJIStoJIS(kcode));
108 }
109
110 int EUCtoSJIS(int kcode)
111 {
112 return JIStoSJIS(EUCtoJIS(kcode));
113 }
114
115 /* KUTEN to JIS kanji code conversion */
116 int KUTENtoJIS(int kcode)
117 {
118 /* in case of undefined in kuten code table */
119 if (HI(kcode) == 0 || HI(kcode) > 94) return 0;
120 if (LO(kcode) == 0 || LO(kcode) > 94) return 0;
121
122 return kcode + 0x2020;
123 }