annotate font/codeconv.cc @ 55:f1a27ee7e03c

* started the same changes on scn2k_text.cc * handle opcodes childObj*. In fact, it was handled (in a strange way, but it worked) before the previous changeset
author thib
date Wed, 22 Apr 2009 15:01:42 +0000
parents 15a18fbe6f21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
223b71206888 Initial import
thib
parents:
diff changeset
1 /*
223b71206888 Initial import
thib
parents:
diff changeset
2 * Copyright (c) 2000 Yuki Sawada
223b71206888 Initial import
thib
parents:
diff changeset
3 * All rights reserved.
223b71206888 Initial import
thib
parents:
diff changeset
4 *
223b71206888 Initial import
thib
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
223b71206888 Initial import
thib
parents:
diff changeset
6 * modification, are permitted provided that the following conditions
223b71206888 Initial import
thib
parents:
diff changeset
7 * are met:
223b71206888 Initial import
thib
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
223b71206888 Initial import
thib
parents:
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
223b71206888 Initial import
thib
parents:
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
223b71206888 Initial import
thib
parents:
diff changeset
12 * documentation and/or other materials provided with the distribution.
223b71206888 Initial import
thib
parents:
diff changeset
13 * 3. The name of the author may not be used to endorse or promote products
223b71206888 Initial import
thib
parents:
diff changeset
14 * derived from this software without specific prior written permission.
223b71206888 Initial import
thib
parents:
diff changeset
15 *
223b71206888 Initial import
thib
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
223b71206888 Initial import
thib
parents:
diff changeset
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223b71206888 Initial import
thib
parents:
diff changeset
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223b71206888 Initial import
thib
parents:
diff changeset
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
223b71206888 Initial import
thib
parents:
diff changeset
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223b71206888 Initial import
thib
parents:
diff changeset
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223b71206888 Initial import
thib
parents:
diff changeset
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223b71206888 Initial import
thib
parents:
diff changeset
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223b71206888 Initial import
thib
parents:
diff changeset
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223b71206888 Initial import
thib
parents:
diff changeset
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
223b71206888 Initial import
thib
parents:
diff changeset
26 */
223b71206888 Initial import
thib
parents:
diff changeset
27
223b71206888 Initial import
thib
parents:
diff changeset
28 #include "codeconv.h"
223b71206888 Initial import
thib
parents:
diff changeset
29 #include "codeconv_tbl.h"
223b71206888 Initial import
thib
parents:
diff changeset
30
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
31 unsigned int codeconv_euc_to_jis(unsigned int euc)
0
223b71206888 Initial import
thib
parents:
diff changeset
32 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
33 unsigned int hi, low;
0
223b71206888 Initial import
thib
parents:
diff changeset
34
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
35 hi = (euc >> 8) & 0xff;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
36 low = euc & 0xff;
0
223b71206888 Initial import
thib
parents:
diff changeset
37
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
38 if (hi < 0x81) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
39 hi = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
40 } else if (low == 0x8e)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
41 hi = 0;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
42 else {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
43 hi -= 0x80;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
44 low -= 0x80;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
45 }
0
223b71206888 Initial import
thib
parents:
diff changeset
46
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
47 return (hi << 8) | low;
0
223b71206888 Initial import
thib
parents:
diff changeset
48 }
223b71206888 Initial import
thib
parents:
diff changeset
49
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
50 static unsigned int codeconv_jis_to_unicode(unsigned int jis)
0
223b71206888 Initial import
thib
parents:
diff changeset
51 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
52 int k0, k1;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
53
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
54 if (jis < 0x80) return jis; // ASCII
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
55
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
56 k0 = (jis >> 8) - 0x20;
0
223b71206888 Initial import
thib
parents:
diff changeset
57
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
58 if (k0 < 1 || k0 > 92)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
59 return 0;
0
223b71206888 Initial import
thib
parents:
diff changeset
60
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
61 k1 = (jis % 0x100) - 0x20;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
62 if (k1 < 1 || k1 > 94)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
63 return 0;
0
223b71206888 Initial import
thib
parents:
diff changeset
64
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
65 return unicode_tbl[k0 - 1][k1 - 1];
0
223b71206888 Initial import
thib
parents:
diff changeset
66 }
223b71206888 Initial import
thib
parents:
diff changeset
67
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
68 unsigned int codeconv_euc_to_unicode(unsigned int euc)
0
223b71206888 Initial import
thib
parents:
diff changeset
69 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
70 unsigned int jis, unicode;
0
223b71206888 Initial import
thib
parents:
diff changeset
71
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
72 jis = codeconv_euc_to_jis(euc);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
73 unicode = codeconv_jis_to_unicode(jis);
0
223b71206888 Initial import
thib
parents:
diff changeset
74
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
75 return unicode;
0
223b71206888 Initial import
thib
parents:
diff changeset
76 }
223b71206888 Initial import
thib
parents:
diff changeset
77
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
78 static unsigned int codeconv_unicode_to_jis(unsigned int unicode)
0
223b71206888 Initial import
thib
parents:
diff changeset
79 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
80 int k0, k1;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
81 unsigned int jis;
0
223b71206888 Initial import
thib
parents:
diff changeset
82
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
83 k0 = (unicode >> 8) & 0xff;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
84 k1 = unicode & 0xff;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
85 jis = unicode_rev_table[k0][k1];
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
86
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
87 return jis;
0
223b71206888 Initial import
thib
parents:
diff changeset
88 }
223b71206888 Initial import
thib
parents:
diff changeset
89
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
90 static unsigned int codeconv_jis_to_euc(unsigned int jis)
0
223b71206888 Initial import
thib
parents:
diff changeset
91 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
92 unsigned int hi, low;
0
223b71206888 Initial import
thib
parents:
diff changeset
93
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
94 hi = (jis >> 8) & 0x7f | 0x80;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
95 low = jis & 0x7f | 0x80;
0
223b71206888 Initial import
thib
parents:
diff changeset
96
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
97 return (hi << 8) | low;
0
223b71206888 Initial import
thib
parents:
diff changeset
98 }
223b71206888 Initial import
thib
parents:
diff changeset
99
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
100 unsigned int codeconv_unicode_to_euc(unsigned int unicode)
0
223b71206888 Initial import
thib
parents:
diff changeset
101 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
102 unsigned int jis, euc;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
103
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
104 if (unicode >= 0xff61 && unicode <= 0xff9f)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
105 return unicode - 0xff61 + 0x8ea1;
0
223b71206888 Initial import
thib
parents:
diff changeset
106
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
107 jis = codeconv_unicode_to_jis(unicode);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
108 if (jis == 0)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
109 return 0x7878;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
110 euc = codeconv_jis_to_euc(jis);
0
223b71206888 Initial import
thib
parents:
diff changeset
111
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
112 return euc;
0
223b71206888 Initial import
thib
parents:
diff changeset
113 }
223b71206888 Initial import
thib
parents:
diff changeset
114
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
115 static unsigned int codeconv_jis_to_sjis(unsigned int jis)
0
223b71206888 Initial import
thib
parents:
diff changeset
116 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
117 unsigned int hi, low;
0
223b71206888 Initial import
thib
parents:
diff changeset
118
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
119 hi = (jis >> 8) & 0xff;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
120 low = jis & 0xff;
0
223b71206888 Initial import
thib
parents:
diff changeset
121
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
122 low += (hi & 0x01) ? 0x1f : 0x7d;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
123 if (low >= 0x7f)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
124 low++;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
125 hi = ((hi - 0x21) >> 1) + 0x81;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
126 if (hi > 0x9f)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
127 hi += 0x40;
0
223b71206888 Initial import
thib
parents:
diff changeset
128
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
129 return (hi << 8) | low;
0
223b71206888 Initial import
thib
parents:
diff changeset
130 }
223b71206888 Initial import
thib
parents:
diff changeset
131
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
132 unsigned int codeconv_euc_to_sjis(unsigned int euc)
0
223b71206888 Initial import
thib
parents:
diff changeset
133 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
134 unsigned int jis, sjis;
0
223b71206888 Initial import
thib
parents:
diff changeset
135
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
136 jis = codeconv_euc_to_jis(euc);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
137 sjis = codeconv_jis_to_sjis(jis);
0
223b71206888 Initial import
thib
parents:
diff changeset
138
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
139 return sjis;
0
223b71206888 Initial import
thib
parents:
diff changeset
140 }
223b71206888 Initial import
thib
parents:
diff changeset
141
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
142 static unsigned int codeconv_sjis_to_jis(unsigned int sjis)
0
223b71206888 Initial import
thib
parents:
diff changeset
143 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
144 unsigned int hi, low;
0
223b71206888 Initial import
thib
parents:
diff changeset
145
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
146 hi = (sjis >> 8) & 0xff;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
147 low = sjis & 0xff;
0
223b71206888 Initial import
thib
parents:
diff changeset
148
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
149 hi -= (hi <= 0x9f) ? 0x71 : 0xb1;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
150 hi = (hi << 1) + 1;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
151 if (low > 0x7f)
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
152 low--;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
153 if (low >= 0x9e) {
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
154 low -= 0x7d;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
155 hi++;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
156 } else low -= 0x1f;
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
157
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
158 return (hi << 8) | low;
0
223b71206888 Initial import
thib
parents:
diff changeset
159 }
223b71206888 Initial import
thib
parents:
diff changeset
160
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
161 unsigned int codeconv_sjis_to_euc(unsigned int sjis)
0
223b71206888 Initial import
thib
parents:
diff changeset
162 {
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
163 unsigned int jis, euc;
0
223b71206888 Initial import
thib
parents:
diff changeset
164
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
165 jis = codeconv_sjis_to_jis(sjis);
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
166 euc = codeconv_jis_to_euc(jis);
0
223b71206888 Initial import
thib
parents:
diff changeset
167
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
168 return euc;
0
223b71206888 Initial import
thib
parents:
diff changeset
169 }
223b71206888 Initial import
thib
parents:
diff changeset
170
52
15a18fbe6f21 * Known bugs added to the README
thib
parents: 0
diff changeset
171 unsigned int codeconv_euc_to_latin1(unsigned int euc)
0
223b71206888 Initial import
thib
parents:
diff changeset
172 {
223b71206888 Initial import
thib
parents:
diff changeset
173 int high = (euc>>8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
174 if (high) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
175 return euc & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
176 }