omText.c 9.28 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
/* $Xorg: omText.c,v 1.3 2000/08/17 19:45:22 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
 */
/* $XFree86: xc/lib/X11/omText.c,v 1.9 2003/04/22 13:57:45 pascal Exp $ */
/*
 * Copyright 1995 by FUJITSU LIMITED
 * This is source code modified by FUJITSU LIMITED under the Joint
 * Development Agreement for the CDE/Motif PST.
 */
/*
 * Modifiers: Jeff Walls, Paul Anderson (HEWLETT-PACKARD)
 */

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

/* For VW/UDC */

static int
is_rotate(
    XOC		oc,
    XFontStruct	*font)
{
    XOCGenericPart	*gen = XOC_GENERIC(oc);
    FontSet		font_set;
    VRotate		vrotate;
    int			font_set_count;
    int			vrotate_num;

    font_set = gen->font_set;
    font_set_count = gen->font_set_num;
    for( ; font_set_count-- ; font_set++) {
	if((font_set->vrotate_num > 0) && (font_set->vrotate)) {
	    vrotate = font_set->vrotate;
	    vrotate_num = font_set->vrotate_num;
	    for( ; vrotate_num-- ; vrotate++)
		if(vrotate->font == font)
		    return True;
	}
    }
    return False;
}

static int
is_codemap(
    XOC		oc,
    XFontStruct	*font)
{
    XOCGenericPart	*gen = XOC_GENERIC(oc);
    FontSet		font_set;
    FontData		vmap;
    int			font_set_count;
    int			vmap_num;

    font_set = gen->font_set;
    font_set_count = gen->font_set_num;
    for( ; font_set_count-- ; font_set++) {
	if(font_set->vmap_num > 0) {
	    vmap = font_set->vmap;
	    vmap_num = font_set->vmap_num;
	    for( ; vmap_num-- ; vmap++)
		if(vmap->font == font)
		    return True;
	}
    }
    return False;
}

static int
draw_vertical(
    Display	*dpy,
    Drawable	d,
    XOC		oc,
    GC		gc,
    XFontStruct	*font,
    Bool	is_xchar2b,
    int		x, int y,
    XPointer	text,
    int		length)
{
    XChar2b	*buf2b;
    char	*buf;
    int		wx = 0, wy = 0;
    int		direction = 0;
    int		font_ascent_return = 0, font_descent_return = 0;
    int		i;
    XCharStruct	overall;		

    wy = y;
    if (is_xchar2b) {
	for(i = 0, buf2b = (XChar2b *) text ; i < length ; i++, buf2b++) {
	    if(is_rotate(oc, font) == True) {
		XTextExtents16(font, buf2b, 1,
			       &direction, &font_ascent_return,
			       &font_descent_return, &overall);
		wx = x - (int)((overall.rbearing - overall.lbearing) >> 1) -
			 (int) overall.lbearing;
		wy += overall.ascent;
		XDrawString16(dpy, d, gc, wx, wy, buf2b, 1);
		wy += overall.descent;
	    } else {
		wx = x - (int)((font->max_bounds.rbearing -
				font->min_bounds.lbearing) >> 1) -
			 (int) font->min_bounds.lbearing;
		wy += font->max_bounds.ascent;
		XDrawString16(dpy, d, gc, wx, wy, buf2b, 1);
		wy += font->max_bounds.descent;
	    }
	}
    } else {
	for(i = 0, buf = (char *)text ; i < length && *buf ; i++, buf++) {
	    if(is_rotate(oc, font) == True) {
		XTextExtents(font, buf, 1,
			     &direction, &font_ascent_return,
			     &font_descent_return, &overall);
		wx = x - (int)((overall.rbearing - overall.lbearing) >> 1) -
			 (int) overall.lbearing;
		wy += overall.ascent;
		XDrawString(dpy, d, gc, wx, wy, buf, 1);
		wy += overall.descent;
	    } else {
		wx = x - (int)((font->max_bounds.rbearing -
				font->min_bounds.lbearing) >> 1) -
			 (int) font->min_bounds.lbearing;
		wy += font->max_bounds.ascent;
		XDrawString(dpy, d, gc, wx, wy, buf, 1);
		wy += font->max_bounds.descent;
	    }
	}
    }
    return wy;
}

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

static int
DrawStringWithFontSet(
    Display *dpy,
    Drawable d,
    XOC oc,
    FontSet fs,
    GC gc,
    int x, int y,
    XPointer text,
    int length)
{
    XFontStruct *font;
    Bool is_xchar2b;
    unsigned char *ptr;
    int ptr_len, char_len = 0;
    FontData fd;
    int ret = 0;

    ptr = (unsigned char *)text;
    is_xchar2b = fs->is_xchar2b;

    while (length > 0) {
        fd = _XomGetFontDataFromFontSet(fs,
			ptr,length,&ptr_len,is_xchar2b,FONTSCOPE);
	if(ptr_len <= 0)
	    break;

       /* First, see if the "Best Match" font for the FontSet was set.
	* If it was, use that font.  If it was not set, then use the
	* font defined by font_set->font_data[0] (which is what
	* _XomGetFontDataFromFontSet() always seems to return for
	* non-VW text).  Note that given the new algorithm in 
	* parse_fontname() and parse_fontdata(), fs->font will
	* *always* contain good data.   We should probably remove
	* the check for "fd->font", but we won't :-) -- jjw/pma (HP)
	*/
        if((font = fs->font) == (XFontStruct *) NULL){

	    if(fd == (FontData) NULL ||
	       (font = fd->font) == (XFontStruct *) NULL)
		break;
        }

	switch(oc->core.orientation) {
	  case XOMOrientation_LTR_TTB:
	  case XOMOrientation_RTL_TTB:
            XSetFont(dpy, gc, font->fid);

	    if (is_xchar2b) {
		char_len = ptr_len / sizeof(XChar2b);
		XDrawString16(dpy, d, gc, x, y, (XChar2b *)ptr, char_len);
		x += XTextWidth16(font, (XChar2b *)ptr, char_len);
            } else {
		char_len = ptr_len;
		XDrawString(dpy, d, gc, x, y, (char *)ptr, char_len);
		x += XTextWidth(font, (char *)ptr, char_len);
	    }
	    break;
	  case XOMOrientation_TTB_RTL:
	  case XOMOrientation_TTB_LTR:
	    if(fs->font == font) {
		fd = _XomGetFontDataFromFontSet(fs,
			ptr,length,&ptr_len,is_xchar2b,VMAP);
		if(ptr_len <= 0)
		    break;
		if(fd == (FontData) NULL ||
		   (font = fd->font) == (XFontStruct *) NULL)
		    break;

		if(is_codemap(oc, fd->font) == False) {
		    fd = _XomGetFontDataFromFontSet(fs,
			     ptr,length,&ptr_len,is_xchar2b,VROTATE);
		    if(ptr_len <= 0)
			break;
		    if(fd == (FontData) NULL ||
		       (font = fd->font) == (XFontStruct *) NULL)
			break;
		}
	    }

	    if(is_xchar2b)
		char_len = ptr_len / sizeof(XChar2b);
	    else
		char_len = ptr_len;
            XSetFont(dpy, gc, font->fid);
	    y = draw_vertical(dpy, d, oc, gc, font, is_xchar2b, x, y,
			       (char *)ptr, char_len);
	    break;

	  case XOMOrientation_Context:
	    /* never used? */
	    break;
	}

	if(char_len <= 0)
	    break;

        length -= char_len;
        ptr += ptr_len;
    }

    switch(oc->core.orientation) {
      case XOMOrientation_LTR_TTB:
      case XOMOrientation_RTL_TTB:
	ret = x;
	break;
      case XOMOrientation_TTB_RTL:
      case XOMOrientation_TTB_LTR:
	ret = y;
	break;
      case XOMOrientation_Context:
	/* not used? */
	break;
    }
    return ret;
}

/* For VW/UDC */

int
_XomGenericDrawString(
    Display *dpy,
    Drawable d,
    XOC oc,
    GC gc,
    int x, int y,
    XOMTextType type,
    XPointer text,
    int length)
{
    XlcConv conv;
    XFontStruct *font;
    Bool is_xchar2b;
/* VW/UDC */
    XPointer args[3];
    FontSet fs;
/* VW/UDC */
    XChar2b xchar2b_buf[BUFSIZ], *buf;
    int start_x = x;
    int start_y = y;
    int left = 0, buf_len = 0;
    int next = 0;

    conv = _XomInitConverter(oc, type);
    if (conv == NULL)
	return -1;
    
    args[0] = (XPointer) &font;
    args[1] = (XPointer) &is_xchar2b;
    args[2] = (XPointer) &fs;

    while (length > 0) {
	buf = xchar2b_buf;
	left = buf_len = BUFSIZ;

	if (_XomConvert(oc, conv, (XPointer *) &text, &length,
			(XPointer *) &buf, &left, args, 3) < 0)
	    break;
	buf_len -= left;

/* For VW/UDC */
	next = DrawStringWithFontSet(dpy, d, oc, fs, gc, x, y,
				     (XPointer)xchar2b_buf, buf_len);

	switch(oc->core.orientation) {
	  case XOMOrientation_LTR_TTB:
	  case XOMOrientation_RTL_TTB:
	    x = next;
	    break;
	  case XOMOrientation_TTB_RTL:
	  case XOMOrientation_TTB_LTR:
	    y = next;
	    break;
          case XOMOrientation_Context:
	    /* not used */
	    break;
	}
/* For VW/UDC */
    }

    x -= start_x;
    y -= start_y;

    return x;
}

int
_XmbGenericDrawString(Display *dpy, Drawable d, XOC oc, GC gc, int x, int y,
		      _Xconst char *text, int length)
{
    return _XomGenericDrawString(dpy, d, oc, gc, x, y, XOMMultiByte,
				 (XPointer) text, length);
}

int
_XwcGenericDrawString(Display *dpy, Drawable d, XOC oc, GC gc, int x, int y,
		      _Xconst wchar_t *text, int length)
{
    return _XomGenericDrawString(dpy, d, oc, gc, x, y, XOMWideChar,
				 (XPointer) text, length);
}

int
_Xutf8GenericDrawString(Display *dpy, Drawable d, XOC oc, GC gc, int x, int y,
			_Xconst char *text, int length)
{
    return _XomGenericDrawString(dpy, d, oc, gc, x, y, XOMUtf8String,
				 (XPointer) text, length);
}