omXChar.c 10.9 KB
Newer Older
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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/* $Xorg: omXChar.c,v 1.3 2000/08/17 19:45:23 cpqbld Exp $ */
/*
 * Copyright 1992, 1993 by TOSHIBA Corp.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, 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 TOSHIBA not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. TOSHIBA make no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * TOSHIBA 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.
 *
 * Author: Katsuhisa Yano	TOSHIBA Corp.
 *			   	mopi@osa.ilab.toshiba.co.jp
 */
/*
 * Copyright 1995 by FUJITSU LIMITED
 * This is source code modified by FUJITSU LIMITED under the Joint
 * Development Agreement for the CDE/Motif PST.
 *
 * Modifier: Takanori Tateno   FUJITSU LIMITED
 *
 */
/*
 * Modifiers: Jeff Walls, Paul Anderson (HEWLETT-PACKARD)
 */
/* $XFree86: xc/lib/X11/omXChar.c,v 1.6tsi Exp $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "Xlibint.h"
#include "XlcPublic.h"
#include "XomGeneric.h"
#include <stdio.h>

/* for VW/UDC start */
static Bool
ismatch_scopes(
    FontData      fontdata,
    unsigned long *value,
    Bool	  is_shift)
{
    register int scopes_num = fontdata->scopes_num;
    FontScope scopes = fontdata->scopes;
    if (!scopes_num)
        return False;

    if(fontdata->font == NULL)
	return False;

    for(;scopes_num--;scopes++)
        if ((scopes->start <= (*value & 0x7f7f)) &&
                        ((scopes->end) >= (*value & 0x7f7f))){
	    if(is_shift == True) {
                if(scopes->shift){
                    if(scopes->shift_direction == '+'){
                        *value += scopes->shift ;
                    } else if( scopes->shift_direction == '-'){
                        *value -= scopes->shift ;
                    }
                }
            }
            return True;
        }

    return False;
}

static int
check_vertical_fonttype(
    char	*name)
{
    char	*ptr;
    int		type = 0;

    if(name == (char *)NULL || (int) strlen(name) <= 0)
	return False;

    /* Obtains the pointer of CHARSET_ENCODING_FIELD. */
    if((ptr = strchr(name, '-')) == (char *) NULL)
	return False;
    ptr++;

    /* Obtains the pointer of vertical_map font type. */
    if((ptr = strchr(ptr, '.')) == (char *) NULL)
	return False;
    ptr++;

    switch(*ptr) {
      case '1':
	type = 1;	break;
      case '2':
	type = 2;	break;
      case '3':
	type = 3;	break;
    }
    return type;
}

/*
*/
#define VMAP          0
#define VROTATE       1
#define FONTSCOPE     2

FontData
_XomGetFontDataFromFontSet(
    FontSet fs,
    unsigned char *str,
    int len,
    int *len_ret,
    int is2b,     
    int type)          /* VMAP , VROTATE , else */
{
    unsigned long value;
    int num,i,hit,csize;
    FontData fontdata;
    unsigned char *c;
    int vfont_type;
    
    c = str;
    hit = -1;
    if(type == VMAP){
	fontdata = fs->vmap;
	num      = fs->vmap_num;
    } else if(type == VROTATE){
        fontdata = (FontData)fs->vrotate;
	num      = fs->vrotate_num;
    } else {
	if(fs->font_data_count <= 0 || fs->font_data == (FontData)NULL) {
	    fontdata = fs->substitute;
	    num      = fs->substitute_num;
	}else {
            fontdata = fs->font_data;
	    num      = fs->font_data_count;
	}
	/* CDExc20229 fix */
	if(fontdata == NULL || num == 0){
	    return(NULL);
	}
    }


    if(is2b){
        csize = 2;
    } else {
        csize = 1;
    }

    for(;len;len--){
        if(is2b){
            value = (((unsigned long)*c) << 8)|(unsigned long)*(c + 1);
        } else {
            value = (unsigned long)*c;
        }
    
       /* ### NOTE: This routine DOES NOT WORK!
	* ###       We can work around the problem in the calling routine,
	* ###       but we really need to understand this better.  As it
	* ###       stands, the algorithm ALWAYS returns "fontdata[0]"
	* ###       for non-VW text!  This is clearly wrong.  In fact,
	* ###       given the new parse_font[name|data]() algorithms,
	* ###       we may not even need this routine to do anything
	* ###       for non-VW text (since font_set->font always contains
	* ###       the best font for this fontset).  -- jjw/pma (HP)
	*/
        for (i=0;i<num;i++) {
	    if(type == VROTATE) {
		if(fontdata[i].font) {
		    /* If the num_cr equal zero, all character is rotated. */
		    if(fontdata[i].scopes_num == 0) {
			break;
		    } else {
			/* The vertical rotate glyph is not have code shift. */
			if (ismatch_scopes(&(fontdata[i]),&value,False)) {
			    break;
			}
		    }
		}
	    } else if(type == VMAP) {
		if(fontdata[i].font) {
		    vfont_type = check_vertical_fonttype(fontdata[i].name);
		    if(vfont_type == 0 || vfont_type == 1) {
			break;
		    } else if(vfont_type == 2 || vfont_type == 3) {
			if(fontdata[i].scopes_num <= 0)
			    break;

			if (ismatch_scopes(&(fontdata[i]),&value,True)) {
			    break;
			}
		    }
		}
	    } else { /* FONTSCOPE */
		if(fontdata[i].font) {
		    if(fontdata[i].scopes_num <= 0)
                        break;
		    if (ismatch_scopes(&(fontdata[i]),&value,True)){
		        break;
                    }
		}
	    }
        }
        if((hit != -1) && (i != hit)){
            break;
        }
        if(i == num){
            if( type == VROTATE || type == VMAP){
		/* Change 1996.01.23 start */
		if(fs->font_data_count <= 0 ||
		   fs->font_data == (FontData)NULL)
		    fontdata = fs->substitute;
		else
		    fontdata = fs->font_data;
		/* Change 1996.01.23 end */
	    }
	    hit = 0;
            c += csize;
	    break;
        }
        if( hit == -1 ) hit = i;
        if(is2b){
            *c = (unsigned char)(value >> 8);
            *(c + 1) = (unsigned char)(value);
        } else {
            *c = (unsigned char)value;
        }
        c += csize;
    }
    *len_ret = (c - str);
    return(&(fontdata[hit]));
}
/* for VW/UDC end   */

static FontSet
_XomGetFontSetFromCharSet(
    XOC oc,
    XlcCharSet charset)
{
    register FontSet font_set = XOC_GENERIC(oc)->font_set;
    register int num = XOC_GENERIC(oc)->font_set_num;
    XlcCharSet *charset_list;
    int charset_count;

    for ( ; num-- > 0; font_set++) {
	charset_count = font_set->charset_count;
	charset_list = font_set->charset_list;
	for ( ; charset_count-- > 0; charset_list++)
	    if (*charset_list == charset)
		return font_set;
    }

    return (FontSet) NULL;
}

#ifdef MUSTCOPY
static void
cs_to_xchar2b(
    register char *from,
    register XChar2b *to,
    register length)
{
    while (length-- > 0) {
	to->byte1 = *from++;
	to->byte2 = *from++;
	to++;
    }
}

static void
cs_to_xchar2b_gl(
    register char *from,
    register XChar2b *to,
    register length)
{
    while (length-- > 0) {
	to->byte1 = *from++ & 0x7f;
	to->byte2 = *from++ & 0x7f;
	to++;
    }
}

static void
cs_to_xchar2b_gr(
    register char *from,
    register XChar2b *to,
    register length)
{
    while (length-- > 0) {
	to->byte1 = *from++ | 0x80;
	to->byte2 = *from++ | 0x80;
	to++;
    }
}
#endif

static void
shift_to_gl(
    register char *text,
    register int length)
{
    while (length-- > 0)
	*text++ &= 0x7f;
}

static void
shift_to_gr(
    register char *text,
    register int length)
{
    while (length-- > 0)
	*text++ |= 0x80;
}

static Bool
load_font(
    XOC oc,
    FontSet font_set)
{
    font_set->font = XLoadQueryFont(oc->core.om->core.display,
			oc->core.font_info.font_name_list[font_set->id]);
    if (font_set->font == NULL)
	return False;
    
    oc->core.font_info.font_struct_list[font_set->id] = font_set->font;
    XFreeFontInfo(NULL, font_set->info, 1);
    font_set->info = NULL;

    if (font_set->font->min_byte1 || font_set->font->max_byte1)
	font_set->is_xchar2b = True;
    else
	font_set->is_xchar2b = False;

    return True;
}

int
_XomConvert(
    XOC oc,
    XlcConv conv,
    XPointer *from,
    int *from_left,
    XPointer *to,
    int *to_left,
    XPointer *args,
    int num_args)
{
    XPointer cs, lc_args[1];
    XlcCharSet charset;
    int length, cs_left, ret;
    FontSet font_set;
#ifdef MUSTCOPY
    XChar2b *xchar2b;
    char *buf, buf_local[BUFSIZ];
#endif
    
    cs = *to;
    cs_left = *to_left;
    lc_args[0] = (XPointer) &charset;

    ret = _XlcConvert(conv, from, from_left, &cs, &cs_left, lc_args, 1);
    if (ret < 0)
	return -1;

    font_set = _XomGetFontSetFromCharSet(oc, charset);
    if (font_set == NULL)
	return -1;

    if (font_set->font == NULL && load_font(oc, font_set) == False)
	return -1;

    length = *to_left - cs_left;

#ifdef MUSTCOPY
    if (font_set->is_xchar2b) {
	buf = (length > BUFSIZ) ? Xmalloc(length) : buf_local;
	if (buf == NULL)
	    return -1;
	memcpy(buf, (char *) *to, length);

	xchar2b = (XChar2b *) *to;
	length >>= 1;

	if (font_set->side == charset->side)
	    cs_to_xchar2b(buf, xchar2b, length);
	else if (font_set->side == XlcGL)
	    cs_to_xchar2b_gl(buf, xchar2b, length);
	else if (font_set->side == XlcGR)
	    cs_to_xchar2b_gr(buf, xchar2b, length);
	else
	    cs_to_xchar2b(buf, xchar2b, length);
	
	if (buf != buf_local)
	    Xfree(buf);

	*to = (XPointer) (xchar2b + length);
	*to_left -= length;
    } else
#endif
    {
	if (font_set->side != charset->side) {
	    if (font_set->side == XlcGL)
		shift_to_gl(*to, length);
	    else if (font_set->side == XlcGR)
		shift_to_gr(*to, length);
	}

	if (font_set->is_xchar2b)
	    length >>= 1;
	*to = cs;
	*to_left -= length;
    }

    *((XFontStruct **) args[0]) = font_set->font;
    *((Bool *) args[1]) = font_set->is_xchar2b;
    if(num_args >= 3){
        *((FontSet *) args[2]) = font_set;
    }

    return ret;
}

XlcConv
_XomInitConverter(
    XOC oc,
    XOMTextType type)
{
    XOCGenericPart *gen = XOC_GENERIC(oc);
    XlcConv *convp;
    char *conv_type;
    XlcConv conv;
    XLCd lcd;

    switch (type) {
    case XOMWideChar:
	convp = &gen->wcs_to_cs;
	conv_type = XlcNWideChar;
	break;
    case XOMMultiByte:
	convp = &gen->mbs_to_cs;
	conv_type = XlcNMultiByte;
	break;
    case XOMUtf8String:
	convp = &gen->utf8_to_cs;
	conv_type = XlcNUtf8String;
	break;
    default:
	return (XlcConv) NULL;
    }

    conv = *convp;
    if (conv) {
	_XlcResetConverter(conv);
	return conv;
    }

    lcd = oc->core.om->core.lcd;

    conv = _XlcOpenConverter(lcd, conv_type, lcd, XlcNFontCharSet);
    if (conv == (XlcConv) NULL) {
        conv = _XlcOpenConverter(lcd, conv_type, lcd, XlcNCharSet);
        if (conv == (XlcConv) NULL)
	    return (XlcConv) NULL;
    }

    *convp = conv;
    return conv;
}