0023024: Update headers of OCCT files
[occt.git] / src / Resource / Resource_ConvertUnicode.c
CommitLineData
b311480e 1/*
2 Copyright (c) 1998-1999 Matra Datavision
3 Copyright (c) 1999-2012 OPEN CASCADE SAS
4
5 The content of this file is subject to the Open CASCADE Technology Public
6 License Version 6.5 (the "License"). You may not use the content of this file
7 except in compliance with the License. Please obtain a copy of the License
8 at http://www.opencascade.org and read it completely before using this file.
9
10 The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12
13 The Original Code and all software distributed under the License is
14 distributed on an "AS IS" basis, without warranty of any kind, and the
15 Initial Developer hereby disclaims all such warranties, including without
16 limitation, any warranties of merchantability, fitness for a particular
17 purpose or non-infringement. Please see the License for the specific terms
18 and conditions governing the rights and limitations under the License.
19
20*/
21
7fd59977 22#include <stdio.h>
23#include <string.h>
24#include <sys/types.h>
25
26typedef unsigned short char16 ;
27
28#include <Resource_Shiftjis.h>
29#include <Resource_gb2312.h>
30
31#define isjis(c) (((c)>=0x21 && (c)<=0x7e))
32#define iseuc(c) (((c)>=0xa1 && (c)<=0xfe))
33#define issjis1(c) (((c)>=0x81 && (c)<=0x9f) || ((c)>=0xe0 && (c)<=0xef))
34#define issjis2(c) ((c)>=0x40 && (c)<=0xfc && (c)!=0x7f)
35#define ishankana(c) ((c)>=0xa0 && (c)<=0xdf)
36#define isshift(c) (((c)>=0x80 && (c)<=0xff))
37
38
39static void sjis_to_jis (unsigned int *ph, unsigned int *pl)
40{
41
42 if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) {
43 return ;
44 }
45
46 if (*ph <= 0x9f)
47 {
48 if (*pl < 0x9f)
49 *ph = (*ph << 1) - 0xe1;
50 else
51 *ph = (*ph << 1) - 0xe0;
52 }
53 else
54 {
55 if (*pl < 0x9f)
56 *ph = (*ph << 1) - 0x161;
57 else
58 *ph = (*ph << 1) - 0x160;
59 }
60 if (*pl < 0x7f)
61 *pl -= 0x1f;
62 else if (*pl < 0x9f)
63 *pl -= 0x20;
64 else
65 *pl -= 0x7e;
66}
67
68static void jis_to_sjis (unsigned int *ph, unsigned int *pl)
69{
70 if (*ph & 1)
71 {
72 if (*pl < 0x60)
73 *pl += 0x1f;
74 else
75 *pl += 0x20;
76 }
77 else
78 *pl += 0x7e;
79 if (*ph < 0x5f)
80 *ph = (*ph + 0xe1) >> 1;
81 else
82 *ph = (*ph + 0x161) >> 1;
83}
84
85static void euc_to_sjis (unsigned int *ph, unsigned int *pl)
86{
87 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
88 *ph = 0 ;
89 *pl = 0 ;
90 return ;
91 }
92
93 if ( ! iseuc ( *ph ) || ! iseuc ( *pl ) ) {
94 return ;
95 }
96
97
98 *ph &= 0x7F ;
99 *pl &= 0x7F ;
100
101 jis_to_sjis ( ph , pl ) ;
102
103}
104
105static void sjis_to_euc (unsigned int *ph, unsigned int *pl)
106{
107 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
108 *ph = 0 ;
109 *pl = 0 ;
110 return ;
111 }
112
113 if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) {
114 return ;
115 }
116
117 if ( *ph == 0 && *pl == 0 )
118 return ;
119
120 sjis_to_jis ( ph , pl ) ;
121
122 *ph |= 0x80 ;
123 *pl |= 0x80 ;
124
125}
126
127void Resource_sjis_to_unicode (unsigned int *ph, unsigned int *pl)
128{
129 char16 sjis ;
130 char16 uni ;
131
132 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
133 *ph = 0 ;
134 *pl = 0 ;
135 return ;
136 }
137
138 if ( ! issjis1 ( *ph ) || ! issjis2 ( *pl ) ) {
139 return ;
140 }
141
142 sjis = ((*ph) << 8) | (*pl) ;
143 uni = sjisuni [sjis] ;
144 *ph = uni >> 8 ;
145 *pl = uni & 0xFF ;
146}
147
148void Resource_unicode_to_sjis (unsigned int *ph, unsigned int *pl)
149{
150 char16 sjis ;
151 char16 uni ;
152
153 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
154 *ph = 0 ;
155 *pl = 0 ;
156 return ;
157 }
158 if ( *ph == 0 && *pl == 0 )
159 return ;
160
161 uni = ((*ph) << 8) | (*pl) ;
162 sjis = unisjis [uni] ;
163 *ph = sjis >> 8 ;
164 *pl = sjis & 0xFF ;
165}
166
167void Resource_unicode_to_euc (unsigned int *ph, unsigned int *pl)
168{
169
170 if ( *ph == 0 && *pl == 0 )
171 return ;
172
173 Resource_unicode_to_sjis ( ph , pl ) ;
174 if (issjis1(*ph)) { /* let's believe it is ANSI code if it is not sjis*/
175 sjis_to_euc ( ph , pl ) ;
176 }
177
178}
179
180void Resource_euc_to_unicode (unsigned int *ph, unsigned int *pl)
181{
182
183 if ( ! iseuc ( *ph ) || ! iseuc ( *pl ) ) {
184 return ;
185 }
186
187
188 if ( *ph == 0 && *pl == 0 )
189 return ;
190
191 euc_to_sjis ( ph , pl ) ;
192 Resource_sjis_to_unicode ( ph , pl ) ;
193
194}
195
196
197void Resource_gb_to_unicode (unsigned int *ph, unsigned int *pl)
198{
199 char16 gb ;
200 char16 uni ;
201
202
203 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
204 *ph = 0 ;
205 *pl = 0 ;
206 return ;
207 }
208
209 if ( ! isshift ( *ph ) || ! isshift ( *pl ) ) {
210 return ;
211 }
212
213 *ph = (*ph) & 0x7f ;
214 *pl = (*pl) & 0x7f ;
215
216 gb = ((*ph) << 8) | (*pl) ;
217 uni = gbuni [gb] ;
218 *ph = uni >> 8 ;
219 *pl = uni & 0xFF ;
220}
221
222void Resource_unicode_to_gb (unsigned int *ph, unsigned int *pl)
223{
224 char16 gb ;
225 char16 uni ;
226
227 if ( (*ph & 0xFFFFFF00) || (*pl & 0xFFFFFF00) ) {
228 *ph = 0 ;
229 *pl = 0 ;
230 return ;
231 }
232 if ( *ph == 0 && *pl == 0 )
233 return ;
234
235 uni = ((*ph) << 8) | (*pl) ;
236 gb = unigb [uni] ;
237 if (gb != 0) {
238 *ph = ( gb >> 8 ) | 0x80 ;
239 *pl = ( gb & 0xFF ) | 0x80 ;
240 }
241 else {
242 *ph = 0;
243 *pl = 0 ;
244 }
245}