annotate font/codeconv.cc @ 51:cbb301016a4e

* oops... finishing what was started and not said with the other patch: fixing memory leaks
author thib
date Fri, 17 Apr 2009 18:40:39 +0000
parents 223b71206888
children 15a18fbe6f21
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
223b71206888 Initial import
thib
parents:
diff changeset
31 static unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
32 codeconv_euc_to_jis(unsigned int euc)
223b71206888 Initial import
thib
parents:
diff changeset
33 {
223b71206888 Initial import
thib
parents:
diff changeset
34 unsigned int hi, low;
223b71206888 Initial import
thib
parents:
diff changeset
35
223b71206888 Initial import
thib
parents:
diff changeset
36 hi = (euc >> 8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
37 low = euc & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
38
223b71206888 Initial import
thib
parents:
diff changeset
39 if (hi < 0x81) {
223b71206888 Initial import
thib
parents:
diff changeset
40 hi = 0;
223b71206888 Initial import
thib
parents:
diff changeset
41 } else if (low == 0x8e)
223b71206888 Initial import
thib
parents:
diff changeset
42 hi = 0;
223b71206888 Initial import
thib
parents:
diff changeset
43 else {
223b71206888 Initial import
thib
parents:
diff changeset
44 hi -= 0x80;
223b71206888 Initial import
thib
parents:
diff changeset
45 low -= 0x80;
223b71206888 Initial import
thib
parents:
diff changeset
46 }
223b71206888 Initial import
thib
parents:
diff changeset
47
223b71206888 Initial import
thib
parents:
diff changeset
48 return (hi << 8) | low;
223b71206888 Initial import
thib
parents:
diff changeset
49 }
223b71206888 Initial import
thib
parents:
diff changeset
50
223b71206888 Initial import
thib
parents:
diff changeset
51 static unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
52 codeconv_jis_to_unicode(unsigned int jis)
223b71206888 Initial import
thib
parents:
diff changeset
53 {
223b71206888 Initial import
thib
parents:
diff changeset
54 int k0, k1;
223b71206888 Initial import
thib
parents:
diff changeset
55
223b71206888 Initial import
thib
parents:
diff changeset
56 if (jis < 0x80) return jis; // ASCII
223b71206888 Initial import
thib
parents:
diff changeset
57 k0 = (jis >> 8) - 0x20;
223b71206888 Initial import
thib
parents:
diff changeset
58 if (k0 < 1 || k0 > 92)
223b71206888 Initial import
thib
parents:
diff changeset
59 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
60
223b71206888 Initial import
thib
parents:
diff changeset
61 k1 = (jis % 0x100) - 0x20;
223b71206888 Initial import
thib
parents:
diff changeset
62 if (k1 < 1 || k1 > 94)
223b71206888 Initial import
thib
parents:
diff changeset
63 return 0;
223b71206888 Initial import
thib
parents:
diff changeset
64
223b71206888 Initial import
thib
parents:
diff changeset
65 return unicode_tbl[k0 - 1][k1 - 1];
223b71206888 Initial import
thib
parents:
diff changeset
66 }
223b71206888 Initial import
thib
parents:
diff changeset
67
223b71206888 Initial import
thib
parents:
diff changeset
68 unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
69 codeconv_euc_to_unicode(unsigned int euc)
223b71206888 Initial import
thib
parents:
diff changeset
70 {
223b71206888 Initial import
thib
parents:
diff changeset
71 unsigned int jis, unicode;
223b71206888 Initial import
thib
parents:
diff changeset
72
223b71206888 Initial import
thib
parents:
diff changeset
73 jis = codeconv_euc_to_jis(euc);
223b71206888 Initial import
thib
parents:
diff changeset
74 unicode = codeconv_jis_to_unicode(jis);
223b71206888 Initial import
thib
parents:
diff changeset
75
223b71206888 Initial import
thib
parents:
diff changeset
76 return unicode;
223b71206888 Initial import
thib
parents:
diff changeset
77 }
223b71206888 Initial import
thib
parents:
diff changeset
78
223b71206888 Initial import
thib
parents:
diff changeset
79 static unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
80 codeconv_unicode_to_jis(unsigned int unicode)
223b71206888 Initial import
thib
parents:
diff changeset
81 {
223b71206888 Initial import
thib
parents:
diff changeset
82 int k0, k1;
223b71206888 Initial import
thib
parents:
diff changeset
83 unsigned int jis;
223b71206888 Initial import
thib
parents:
diff changeset
84
223b71206888 Initial import
thib
parents:
diff changeset
85 k0 = (unicode >> 8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
86 k1 = unicode & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
87 jis = unicode_rev_table[k0][k1];
223b71206888 Initial import
thib
parents:
diff changeset
88
223b71206888 Initial import
thib
parents:
diff changeset
89 return jis;
223b71206888 Initial import
thib
parents:
diff changeset
90 }
223b71206888 Initial import
thib
parents:
diff changeset
91
223b71206888 Initial import
thib
parents:
diff changeset
92 static unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
93 codeconv_jis_to_euc(unsigned int jis)
223b71206888 Initial import
thib
parents:
diff changeset
94 {
223b71206888 Initial import
thib
parents:
diff changeset
95 unsigned int hi, low;
223b71206888 Initial import
thib
parents:
diff changeset
96
223b71206888 Initial import
thib
parents:
diff changeset
97 hi = (jis >> 8) & 0x7f | 0x80;
223b71206888 Initial import
thib
parents:
diff changeset
98 low = jis & 0x7f | 0x80;
223b71206888 Initial import
thib
parents:
diff changeset
99
223b71206888 Initial import
thib
parents:
diff changeset
100 return (hi << 8) | low;
223b71206888 Initial import
thib
parents:
diff changeset
101 }
223b71206888 Initial import
thib
parents:
diff changeset
102
223b71206888 Initial import
thib
parents:
diff changeset
103 unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
104 codeconv_unicode_to_euc(unsigned int unicode)
223b71206888 Initial import
thib
parents:
diff changeset
105 {
223b71206888 Initial import
thib
parents:
diff changeset
106 unsigned int jis, euc;
223b71206888 Initial import
thib
parents:
diff changeset
107
223b71206888 Initial import
thib
parents:
diff changeset
108 if (unicode >= 0xff61 && unicode <= 0xff9f)
223b71206888 Initial import
thib
parents:
diff changeset
109 return unicode - 0xff61 + 0x8ea1;
223b71206888 Initial import
thib
parents:
diff changeset
110
223b71206888 Initial import
thib
parents:
diff changeset
111 jis = codeconv_unicode_to_jis(unicode);
223b71206888 Initial import
thib
parents:
diff changeset
112 if (jis == 0)
223b71206888 Initial import
thib
parents:
diff changeset
113 return 0x7878;
223b71206888 Initial import
thib
parents:
diff changeset
114 euc = codeconv_jis_to_euc(jis);
223b71206888 Initial import
thib
parents:
diff changeset
115
223b71206888 Initial import
thib
parents:
diff changeset
116 return euc;
223b71206888 Initial import
thib
parents:
diff changeset
117 }
223b71206888 Initial import
thib
parents:
diff changeset
118
223b71206888 Initial import
thib
parents:
diff changeset
119 static unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
120 codeconv_jis_to_sjis(unsigned int jis)
223b71206888 Initial import
thib
parents:
diff changeset
121 {
223b71206888 Initial import
thib
parents:
diff changeset
122 unsigned int hi, low;
223b71206888 Initial import
thib
parents:
diff changeset
123
223b71206888 Initial import
thib
parents:
diff changeset
124 hi = (jis >> 8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
125 low = jis & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
126
223b71206888 Initial import
thib
parents:
diff changeset
127 low += (hi & 0x01) ? 0x1f : 0x7d;
223b71206888 Initial import
thib
parents:
diff changeset
128 if (low >= 0x7f)
223b71206888 Initial import
thib
parents:
diff changeset
129 low++;
223b71206888 Initial import
thib
parents:
diff changeset
130 hi = ((hi - 0x21) >> 1) + 0x81;
223b71206888 Initial import
thib
parents:
diff changeset
131 if (hi > 0x9f)
223b71206888 Initial import
thib
parents:
diff changeset
132 hi += 0x40;
223b71206888 Initial import
thib
parents:
diff changeset
133
223b71206888 Initial import
thib
parents:
diff changeset
134 return (hi << 8) | low;
223b71206888 Initial import
thib
parents:
diff changeset
135 }
223b71206888 Initial import
thib
parents:
diff changeset
136
223b71206888 Initial import
thib
parents:
diff changeset
137 unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
138 codeconv_euc_to_sjis(unsigned int euc)
223b71206888 Initial import
thib
parents:
diff changeset
139 {
223b71206888 Initial import
thib
parents:
diff changeset
140 unsigned int jis, sjis;
223b71206888 Initial import
thib
parents:
diff changeset
141
223b71206888 Initial import
thib
parents:
diff changeset
142 jis = codeconv_euc_to_jis(euc);
223b71206888 Initial import
thib
parents:
diff changeset
143 sjis = codeconv_jis_to_sjis(jis);
223b71206888 Initial import
thib
parents:
diff changeset
144
223b71206888 Initial import
thib
parents:
diff changeset
145 return sjis;
223b71206888 Initial import
thib
parents:
diff changeset
146 }
223b71206888 Initial import
thib
parents:
diff changeset
147
223b71206888 Initial import
thib
parents:
diff changeset
148 static unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
149 codeconv_sjis_to_jis(unsigned int sjis)
223b71206888 Initial import
thib
parents:
diff changeset
150 {
223b71206888 Initial import
thib
parents:
diff changeset
151 unsigned int hi, low;
223b71206888 Initial import
thib
parents:
diff changeset
152
223b71206888 Initial import
thib
parents:
diff changeset
153 hi = (sjis >> 8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
154 low = sjis & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
155
223b71206888 Initial import
thib
parents:
diff changeset
156 hi -= (hi <= 0x9f) ? 0x71 : 0xb1;
223b71206888 Initial import
thib
parents:
diff changeset
157 hi = (hi << 1) + 1;
223b71206888 Initial import
thib
parents:
diff changeset
158 if (low > 0x7f)
223b71206888 Initial import
thib
parents:
diff changeset
159 low--;
223b71206888 Initial import
thib
parents:
diff changeset
160 if (low >= 0x9e) {
223b71206888 Initial import
thib
parents:
diff changeset
161 low -= 0x7d;
223b71206888 Initial import
thib
parents:
diff changeset
162 hi++;
223b71206888 Initial import
thib
parents:
diff changeset
163 } else
223b71206888 Initial import
thib
parents:
diff changeset
164 low -= 0x1f;
223b71206888 Initial import
thib
parents:
diff changeset
165
223b71206888 Initial import
thib
parents:
diff changeset
166 return (hi << 8) | low;
223b71206888 Initial import
thib
parents:
diff changeset
167 }
223b71206888 Initial import
thib
parents:
diff changeset
168
223b71206888 Initial import
thib
parents:
diff changeset
169 unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
170 codeconv_sjis_to_euc(unsigned int sjis)
223b71206888 Initial import
thib
parents:
diff changeset
171 {
223b71206888 Initial import
thib
parents:
diff changeset
172 unsigned int jis, euc;
223b71206888 Initial import
thib
parents:
diff changeset
173
223b71206888 Initial import
thib
parents:
diff changeset
174 jis = codeconv_sjis_to_jis(sjis);
223b71206888 Initial import
thib
parents:
diff changeset
175 euc = codeconv_jis_to_euc(jis);
223b71206888 Initial import
thib
parents:
diff changeset
176
223b71206888 Initial import
thib
parents:
diff changeset
177 return euc;
223b71206888 Initial import
thib
parents:
diff changeset
178 }
223b71206888 Initial import
thib
parents:
diff changeset
179
223b71206888 Initial import
thib
parents:
diff changeset
180 unsigned int
223b71206888 Initial import
thib
parents:
diff changeset
181 codeconv_euc_to_latin1(unsigned int euc)
223b71206888 Initial import
thib
parents:
diff changeset
182 {
223b71206888 Initial import
thib
parents:
diff changeset
183 int high = (euc>>8) & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
184 if (high) return 0;
223b71206888 Initial import
thib
parents:
diff changeset
185 return euc & 0xff;
223b71206888 Initial import
thib
parents:
diff changeset
186 }