summaryrefslogtreecommitdiff
path: root/src/imConv.c
blob: c3c1974dece4d875975c499b0444dd4d8ef44c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/******************************************************************

              Copyright 1991, 1992 by Fuji Xerox Co.,Ltd.
	      Copyright 1993, 1994 by FUJITSU LIMITED

Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation, and that the name of Fuji Xerox Co.,Ltd.
, and that the name of FUJITSU LIMITED not be used in advertising or
publicity pertaining to distribution of the software without specific,
 written prior permission.
Fuji Xerox Co.,Ltd. , and FUJITSU LIMITED makes no representations about
the suitability of this software for any purpose.
It is provided "as is" without express or implied warranty.

FUJI XEROX CO.,LTD. AND FUJITSU LIMITED DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL FUJI XEROX CO.,LTD.
AND FUJITSU LIMITED BE LIABLE FOR ANY SPECIAL, INDIRECT
OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
THE USE OR PERFORMANCE OF THIS SOFTWARE.

  Auther:   Kazunori Nishihara, Fuji Xerox Co.,Ltd.
                                kaz@ssdev.ksp.fujixerox.co.jp
  Modifier: Takashi Fujiwara    FUJITSU LIMITED
                                fujiwara@a80.tech.yk.fujitsu.co.jp

******************************************************************/
/* 2000 Modifier: Ivan Pascal	The XFree86 Project.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include "Xlibint.h"
#include "Xlcint.h"
#include "Ximint.h"
#include "XlcPubI.h"

#ifdef XKB
/*
 * rather than just call _XLookupString (i.e. the pre-XKB XLookupString)
 * do this because with XKB the event may have some funky modifiers that
 * _XLookupString doesn't grok.
 */
#include "XKBlib.h"
#define	XLOOKUPSTRING lookup_string
#else
#define	XLOOKUPSTRING XLookupString
#endif

typedef unsigned int ucs4_t;

typedef int (*ucstocsConvProc)(
    XPointer,
    unsigned char *,
    ucs4_t,
    int
);

struct SubstRec {
    const char encoding_name[8];
    const char charset_name[12];
};

static const struct SubstRec SubstTable[] = {
    {"STRING", "ISO8859-1"},
    {"TIS620", "TIS620-0"},
    {"UTF-8",  "ISO10646-1"}
};
#define num_substitute (sizeof SubstTable / sizeof SubstTable[0])

/*
 * Given the name of a charset, returns the pointer to convertors
 * from UCS char to specified charset char.
 * This converter is needed for _XimGetCharCode subroutine.
 */
XPointer
_XimGetLocaleCode (
    _Xconst char*	encoding_name)
{
    XPointer cvt = _Utf8GetConvByName(encoding_name);
    if (!cvt && encoding_name) {
       int i;
       for (i = 0; i < num_substitute; i++)
           if (!strcmp(encoding_name, SubstTable[i].encoding_name))
               return _Utf8GetConvByName(SubstTable[i].charset_name);
    }
    return cvt;
}

/*
 * Returns the locale dependent representation of a keysym.
 * The locale's encoding is passed in form of pointer to UCS convertor.
 * The resulting multi-byte sequence is placed starting at buf (a buffer
 * with nbytes bytes, nbytes should be >= 8) and is NUL terminated.
 * Returns the length of the resulting multi-byte sequence, excluding the
 * terminating NUL byte. Return 0 if the keysym is not representable in the
 * locale
 */
/*ARGSUSED*/
int
_XimGetCharCode (
    XPointer            ucs_conv,
    KeySym 		keysym,
    unsigned char*	buf,
    int 		nbytes)
{
    int count = 0;
    ucstocsConvProc cvt = (ucstocsConvProc) ucs_conv;
    ucs4_t ucs4;

    if (keysym < 0x80) {
        buf[0] = (char) keysym;
        count = 1;
    } else if (cvt) {
        ucs4 = KeySymToUcs4(keysym);
        if (ucs4)
            count = (*cvt)((XPointer)NULL, buf, ucs4, nbytes);
    }

    if (count < 0)
       	count = 0;
    if (count>nbytes)
        return nbytes;
    if (count<nbytes)
        buf[count]= '\0';
    return count;
}

#ifdef XKB
static int lookup_string(
    XKeyEvent*		event,
    char*		buffer,
    int			nbytes,
    KeySym*		keysym,
    XComposeStatus*	status)
{
    int ret;
    unsigned ctrls = XkbGetXlibControls (event->display);
    XkbSetXlibControls (event->display,
			XkbLC_ForceLatin1Lookup, XkbLC_ForceLatin1Lookup);
    ret = XLookupString(event, (char *)buffer, nbytes, keysym, status);
    XkbSetXlibControls (event->display,
			XkbLC_ForceLatin1Lookup, ctrls);
    return ret;
}
#endif

#define BUF_SIZE (20)

int
_XimLookupMBText(
    Xic			 ic,
    XKeyEvent*		event,
    char*		buffer,
    int			nbytes,
    KeySym*		keysym,
    XComposeStatus*	status)
{
    int count;
    KeySym symbol;
    Status	dummy;
    Xim	im = (Xim)ic->core.im;
    XimCommonPrivateRec* private = &im->private.common;
    unsigned char look[BUF_SIZE];
    ucs4_t ucs4;

    /* force a latin-1 lookup for compatibility */
    count = XLOOKUPSTRING(event, (char *)buffer, nbytes, &symbol, status);
    if (keysym != NULL) *keysym = symbol;
    if ((nbytes == 0) || (symbol == NoSymbol)) return count;

    if (count > 1) {
	memcpy(look, (char *)buffer,count);
	look[count] = '\0';
	if ((count = im->methods->ctstombs(ic->core.im,
				(char*) look, count,
				buffer, nbytes, &dummy)) < 0) {
	    count = 0;
	}
    } else if ((count == 0) ||
	       (count == 1 && (symbol > 0x7f && symbol < 0xff00))) {

        XPointer from = (XPointer) &ucs4;
        XPointer to =   (XPointer) look;
        int from_len = 1;
        int to_len = BUF_SIZE;
        XPointer args[1];
        XlcCharSet charset;
        args[0] = (XPointer) &charset;
        ucs4 = (ucs4_t) KeySymToUcs4(symbol);
        if (!ucs4)
            return 0;

	if (_XlcConvert(private->ucstoc_conv,
                        &from, &from_len, &to, &to_len,
                        args, 1 ) != 0) {
	    count = 0;
	} else {
            from = (XPointer) look;
            to =   (XPointer) buffer;
            from_len = BUF_SIZE - to_len;
            to_len = nbytes;
            args[0] = (XPointer) charset;
	    if (_XlcConvert(private->cstomb_conv,
                            &from, &from_len, &to, &to_len,
			    args, 1 ) != 0) {
		count = 0;
	    } else {
                count = nbytes - to_len;
	    }
	}
    }
    /* FIXME:
     * we should make sure that if the character is a Latin1 character
     * and it's on the right side, and we're in a non-Latin1 locale
     * that this is a valid Latin1 character for this locale.
     */
    return count;
}

int
_XimLookupWCText(
    Xic			 ic,
    XKeyEvent*		event,
    wchar_t*		buffer,
    int			nbytes,
    KeySym*		keysym,
    XComposeStatus*	status)
{
    int count;
    KeySym symbol;
    Status	dummy;
    Xim	im = (Xim)ic->core.im;
    XimCommonPrivateRec* private = &im->private.common;
    unsigned char look[BUF_SIZE];
    ucs4_t ucs4;

    /* force a latin-1 lookup for compatibility */
    count = XLOOKUPSTRING(event, (char *)look, nbytes, &symbol, status);
    if (keysym != NULL) *keysym = symbol;
    if ((nbytes == 0) || (symbol == NoSymbol)) return count;

    if (count > 1) {
	if ((count = im->methods->ctstowcs(ic->core.im,
				(char*) look, count,
				buffer, nbytes, &dummy)) < 0) {
	    count = 0;
	}
    } else if ((count == 0) ||
	       (count == 1 && (symbol > 0x7f && symbol < 0xff00))) {

        XPointer from = (XPointer) &ucs4;
        XPointer to =   (XPointer) look;
        int from_len = 1;
        int to_len = BUF_SIZE;
        XPointer args[1];
        XlcCharSet charset;
        args[0] = (XPointer) &charset;
        ucs4 = (ucs4_t) KeySymToUcs4(symbol);
        if (!ucs4)
            return 0;

	if (_XlcConvert(private->ucstoc_conv,
                        &from, &from_len, &to, &to_len,
                        args, 1 ) != 0) {
	    count = 0;
	} else {
            from = (XPointer) look;
            to =   (XPointer) buffer;
            from_len = BUF_SIZE - to_len;
            to_len = nbytes;
            args[0] = (XPointer) charset;

	    if (_XlcConvert(private->cstowc_conv,
                            &from, &from_len, &to, &to_len,
			    args, 1 ) != 0) {
		count = 0;
	    } else {
                count = nbytes - to_len;
	    }
	}
    } else
	/* FIXME:
	 * we should make sure that if the character is a Latin1 character
	 * and it's on the right side, and we're in a non-Latin1 locale
	 * that this is a valid Latin1 character for this locale.
	 */
	buffer[0] = look[0];

    return count;
}

int
_XimLookupUTF8Text(
    Xic			 ic,
    XKeyEvent*		event,
    char*		buffer,
    int			nbytes,
    KeySym*		keysym,
    XComposeStatus*	status)
{
    int count;
    KeySym symbol;
    Status	dummy;
    Xim	im = (Xim)ic->core.im;
    XimCommonPrivateRec* private = &im->private.common;
    unsigned char look[BUF_SIZE];
    ucs4_t ucs4;

    /* force a latin-1 lookup for compatibility */
    count = XLOOKUPSTRING(event, (char *)buffer, nbytes, &symbol, status);
    if (keysym != NULL) *keysym = symbol;
    if ((nbytes == 0) || (symbol == NoSymbol)) return count;

    if (count > 1) {
	memcpy(look, (char *)buffer,count);
	look[count] = '\0';
	if ((count = im->methods->ctstoutf8(ic->core.im,
				(char*) look, count,
				buffer, nbytes, &dummy)) < 0) {
	    count = 0;
	}
    } else if ((count == 0) ||
	       (count == 1 && (symbol > 0x7f && symbol < 0xff00))) {

        XPointer from = (XPointer) &ucs4;
        int from_len = 1;
        XPointer to = (XPointer) buffer;
        int to_len = nbytes;

        ucs4 = (ucs4_t) KeySymToUcs4(symbol);
        if (!ucs4)
            return 0;

	if (_XlcConvert(private->ucstoutf8_conv,
                        &from, &from_len, &to, &to_len,
                        NULL, 0) != 0) {
	    count = 0;
	} else {
            count = nbytes - to_len;
	}
    }
    /* FIXME:
     * we should make sure that if the character is a Latin1 character
     * and it's on the right side, and we're in a non-Latin1 locale
     * that this is a valid Latin1 character for this locale.
     */
    return count;
}