11ab64890Smrg/*
21ab64890Smrg * Copyright 1992, 1993 by TOSHIBA Corp.
31ab64890Smrg *
41ab64890Smrg * Permission to use, copy, modify, and distribute this software and its
51ab64890Smrg * documentation for any purpose and without fee is hereby granted, provided
61ab64890Smrg * that the above copyright notice appear in all copies and that both that
71ab64890Smrg * copyright notice and this permission notice appear in supporting
81ab64890Smrg * documentation, and that the name of TOSHIBA not be used in advertising
91ab64890Smrg * or publicity pertaining to distribution of the software without specific,
101ab64890Smrg * written prior permission. TOSHIBA make no representations about the
111ab64890Smrg * suitability of this software for any purpose.  It is provided "as is"
121ab64890Smrg * without express or implied warranty.
131ab64890Smrg *
141ab64890Smrg * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
151ab64890Smrg * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
161ab64890Smrg * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
171ab64890Smrg * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
181ab64890Smrg * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
191ab64890Smrg * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
201ab64890Smrg * SOFTWARE.
211ab64890Smrg *
221ab64890Smrg * Author: Katsuhisa Yano	TOSHIBA Corp.
231ab64890Smrg *			   	mopi@osa.ilab.toshiba.co.jp
241ab64890Smrg */
251ab64890Smrg/*
261ab64890Smrg * Copyright 1995 by FUJITSU LIMITED
271ab64890Smrg * This is source code modified by FUJITSU LIMITED under the Joint
281ab64890Smrg * Development Agreement for the CDE/Motif PST.
291ab64890Smrg */
301ab64890Smrg/*
311ab64890Smrg * Modifiers: Jeff Walls, Paul Anderson (HEWLETT-PACKARD)
321ab64890Smrg */
331ab64890Smrg
341ab64890Smrg#ifdef HAVE_CONFIG_H
351ab64890Smrg#include <config.h>
361ab64890Smrg#endif
371ab64890Smrg#include "Xlibint.h"
381ab64890Smrg#include "XomGeneric.h"
391ab64890Smrg#include <stdio.h>
401ab64890Smrg
411ab64890Smrg/* For VW/UDC */
421ab64890Smrg
431ab64890Smrgstatic int
441ab64890Smrgis_rotate(
451ab64890Smrg    XOC		oc,
461ab64890Smrg    XFontStruct	*font)
471ab64890Smrg{
481ab64890Smrg    XOCGenericPart	*gen = XOC_GENERIC(oc);
491ab64890Smrg    FontSet		font_set;
501ab64890Smrg    VRotate		vrotate;
511ab64890Smrg    int			font_set_count;
521ab64890Smrg    int			vrotate_num;
531ab64890Smrg
541ab64890Smrg    font_set = gen->font_set;
551ab64890Smrg    font_set_count = gen->font_set_num;
561ab64890Smrg    for( ; font_set_count-- ; font_set++) {
571ab64890Smrg	if((font_set->vrotate_num > 0) && (font_set->vrotate)) {
581ab64890Smrg	    vrotate = font_set->vrotate;
591ab64890Smrg	    vrotate_num = font_set->vrotate_num;
601ab64890Smrg	    for( ; vrotate_num-- ; vrotate++)
611ab64890Smrg		if(vrotate->font == font)
621ab64890Smrg		    return True;
631ab64890Smrg	}
641ab64890Smrg    }
651ab64890Smrg    return False;
661ab64890Smrg}
671ab64890Smrg
681ab64890Smrgstatic int
691ab64890Smrgis_codemap(
701ab64890Smrg    XOC		oc,
711ab64890Smrg    XFontStruct	*font)
721ab64890Smrg{
731ab64890Smrg    XOCGenericPart	*gen = XOC_GENERIC(oc);
741ab64890Smrg    FontSet		font_set;
751ab64890Smrg    FontData		vmap;
761ab64890Smrg    int			font_set_count;
771ab64890Smrg    int			vmap_num;
781ab64890Smrg
791ab64890Smrg    font_set = gen->font_set;
801ab64890Smrg    font_set_count = gen->font_set_num;
811ab64890Smrg    for( ; font_set_count-- ; font_set++) {
821ab64890Smrg	if(font_set->vmap_num > 0) {
831ab64890Smrg	    vmap = font_set->vmap;
841ab64890Smrg	    vmap_num = font_set->vmap_num;
851ab64890Smrg	    for( ; vmap_num-- ; vmap++)
861ab64890Smrg		if(vmap->font == font)
871ab64890Smrg		    return True;
881ab64890Smrg	}
891ab64890Smrg    }
901ab64890Smrg    return False;
911ab64890Smrg}
921ab64890Smrg
931ab64890Smrgstatic int
941ab64890Smrgdraw_vertical(
951ab64890Smrg    Display	*dpy,
961ab64890Smrg    Drawable	d,
971ab64890Smrg    XOC		oc,
981ab64890Smrg    GC		gc,
991ab64890Smrg    XFontStruct	*font,
1001ab64890Smrg    Bool	is_xchar2b,
1011ab64890Smrg    int		x, int y,
1021ab64890Smrg    XPointer	text,
1031ab64890Smrg    int		length)
1041ab64890Smrg{
1051ab64890Smrg    XChar2b	*buf2b;
1061ab64890Smrg    char	*buf;
1071ab64890Smrg    int		wx = 0, wy = 0;
1081ab64890Smrg    int		direction = 0;
1091ab64890Smrg    int		font_ascent_return = 0, font_descent_return = 0;
1101ab64890Smrg    int		i;
11161b2299dSmrg    XCharStruct	overall;
1121ab64890Smrg
1131ab64890Smrg    wy = y;
1141ab64890Smrg    if (is_xchar2b) {
1151ab64890Smrg	for(i = 0, buf2b = (XChar2b *) text ; i < length ; i++, buf2b++) {
1161ab64890Smrg	    if(is_rotate(oc, font) == True) {
1171ab64890Smrg		XTextExtents16(font, buf2b, 1,
1181ab64890Smrg			       &direction, &font_ascent_return,
1191ab64890Smrg			       &font_descent_return, &overall);
1201ab64890Smrg		wx = x - (int)((overall.rbearing - overall.lbearing) >> 1) -
1211ab64890Smrg			 (int) overall.lbearing;
1221ab64890Smrg		wy += overall.ascent;
1231ab64890Smrg		XDrawString16(dpy, d, gc, wx, wy, buf2b, 1);
1241ab64890Smrg		wy += overall.descent;
1251ab64890Smrg	    } else {
1261ab64890Smrg		wx = x - (int)((font->max_bounds.rbearing -
1271ab64890Smrg				font->min_bounds.lbearing) >> 1) -
1281ab64890Smrg			 (int) font->min_bounds.lbearing;
1291ab64890Smrg		wy += font->max_bounds.ascent;
1301ab64890Smrg		XDrawString16(dpy, d, gc, wx, wy, buf2b, 1);
1311ab64890Smrg		wy += font->max_bounds.descent;
1321ab64890Smrg	    }
1331ab64890Smrg	}
1341ab64890Smrg    } else {
1351ab64890Smrg	for(i = 0, buf = (char *)text ; i < length && *buf ; i++, buf++) {
1361ab64890Smrg	    if(is_rotate(oc, font) == True) {
1371ab64890Smrg		XTextExtents(font, buf, 1,
1381ab64890Smrg			     &direction, &font_ascent_return,
1391ab64890Smrg			     &font_descent_return, &overall);
1401ab64890Smrg		wx = x - (int)((overall.rbearing - overall.lbearing) >> 1) -
1411ab64890Smrg			 (int) overall.lbearing;
1421ab64890Smrg		wy += overall.ascent;
1431ab64890Smrg		XDrawString(dpy, d, gc, wx, wy, buf, 1);
1441ab64890Smrg		wy += overall.descent;
1451ab64890Smrg	    } else {
1461ab64890Smrg		wx = x - (int)((font->max_bounds.rbearing -
1471ab64890Smrg				font->min_bounds.lbearing) >> 1) -
1481ab64890Smrg			 (int) font->min_bounds.lbearing;
1491ab64890Smrg		wy += font->max_bounds.ascent;
1501ab64890Smrg		XDrawString(dpy, d, gc, wx, wy, buf, 1);
1511ab64890Smrg		wy += font->max_bounds.descent;
1521ab64890Smrg	    }
1531ab64890Smrg	}
1541ab64890Smrg    }
1551ab64890Smrg    return wy;
1561ab64890Smrg}
1571ab64890Smrg
1581ab64890Smrg#define VMAP          0
1591ab64890Smrg#define VROTATE       1
1601ab64890Smrg#define FONTSCOPE     2
1611ab64890Smrg
1621ab64890Smrgstatic int
1631ab64890SmrgDrawStringWithFontSet(
1641ab64890Smrg    Display *dpy,
1651ab64890Smrg    Drawable d,
1661ab64890Smrg    XOC oc,
1671ab64890Smrg    FontSet fs,
1681ab64890Smrg    GC gc,
1691ab64890Smrg    int x, int y,
1701ab64890Smrg    XPointer text,
1711ab64890Smrg    int length)
1721ab64890Smrg{
1731ab64890Smrg    XFontStruct *font;
1741ab64890Smrg    Bool is_xchar2b;
1751ab64890Smrg    unsigned char *ptr;
1761ab64890Smrg    int ptr_len, char_len = 0;
1771ab64890Smrg    FontData fd;
1781ab64890Smrg    int ret = 0;
1791ab64890Smrg
1801ab64890Smrg    ptr = (unsigned char *)text;
1811ab64890Smrg    is_xchar2b = fs->is_xchar2b;
1821ab64890Smrg
1831ab64890Smrg    while (length > 0) {
1841ab64890Smrg        fd = _XomGetFontDataFromFontSet(fs,
1851ab64890Smrg			ptr,length,&ptr_len,is_xchar2b,FONTSCOPE);
1861ab64890Smrg	if(ptr_len <= 0)
1871ab64890Smrg	    break;
1881ab64890Smrg
1891ab64890Smrg       /* First, see if the "Best Match" font for the FontSet was set.
1901ab64890Smrg	* If it was, use that font.  If it was not set, then use the
1911ab64890Smrg	* font defined by font_set->font_data[0] (which is what
1921ab64890Smrg	* _XomGetFontDataFromFontSet() always seems to return for
19361b2299dSmrg	* non-VW text).  Note that given the new algorithm in
1941ab64890Smrg	* parse_fontname() and parse_fontdata(), fs->font will
1951ab64890Smrg	* *always* contain good data.   We should probably remove
1961ab64890Smrg	* the check for "fd->font", but we won't :-) -- jjw/pma (HP)
1971ab64890Smrg	*/
1981ab64890Smrg        if((font = fs->font) == (XFontStruct *) NULL){
1991ab64890Smrg
2001ab64890Smrg	    if(fd == (FontData) NULL ||
2011ab64890Smrg	       (font = fd->font) == (XFontStruct *) NULL)
2021ab64890Smrg		break;
2031ab64890Smrg        }
2041ab64890Smrg
2051ab64890Smrg	switch(oc->core.orientation) {
2061ab64890Smrg	  case XOMOrientation_LTR_TTB:
2071ab64890Smrg	  case XOMOrientation_RTL_TTB:
2081ab64890Smrg            XSetFont(dpy, gc, font->fid);
2091ab64890Smrg
2101ab64890Smrg	    if (is_xchar2b) {
2111ab64890Smrg		char_len = ptr_len / sizeof(XChar2b);
2121ab64890Smrg		XDrawString16(dpy, d, gc, x, y, (XChar2b *)ptr, char_len);
2131ab64890Smrg		x += XTextWidth16(font, (XChar2b *)ptr, char_len);
2141ab64890Smrg            } else {
2151ab64890Smrg		char_len = ptr_len;
2161ab64890Smrg		XDrawString(dpy, d, gc, x, y, (char *)ptr, char_len);
2171ab64890Smrg		x += XTextWidth(font, (char *)ptr, char_len);
2181ab64890Smrg	    }
2191ab64890Smrg	    break;
2201ab64890Smrg	  case XOMOrientation_TTB_RTL:
2211ab64890Smrg	  case XOMOrientation_TTB_LTR:
2221ab64890Smrg	    if(fs->font == font) {
2231ab64890Smrg		fd = _XomGetFontDataFromFontSet(fs,
2241ab64890Smrg			ptr,length,&ptr_len,is_xchar2b,VMAP);
2251ab64890Smrg		if(ptr_len <= 0)
2261ab64890Smrg		    break;
2271ab64890Smrg		if(fd == (FontData) NULL ||
2281ab64890Smrg		   (font = fd->font) == (XFontStruct *) NULL)
2291ab64890Smrg		    break;
2301ab64890Smrg
2311ab64890Smrg		if(is_codemap(oc, fd->font) == False) {
2321ab64890Smrg		    fd = _XomGetFontDataFromFontSet(fs,
2331ab64890Smrg			     ptr,length,&ptr_len,is_xchar2b,VROTATE);
2341ab64890Smrg		    if(ptr_len <= 0)
2351ab64890Smrg			break;
2361ab64890Smrg		    if(fd == (FontData) NULL ||
2371ab64890Smrg		       (font = fd->font) == (XFontStruct *) NULL)
2381ab64890Smrg			break;
2391ab64890Smrg		}
2401ab64890Smrg	    }
2411ab64890Smrg
2421ab64890Smrg	    if(is_xchar2b)
2431ab64890Smrg		char_len = ptr_len / sizeof(XChar2b);
2441ab64890Smrg	    else
2451ab64890Smrg		char_len = ptr_len;
2461ab64890Smrg            XSetFont(dpy, gc, font->fid);
2471ab64890Smrg	    y = draw_vertical(dpy, d, oc, gc, font, is_xchar2b, x, y,
2481ab64890Smrg			       (char *)ptr, char_len);
2491ab64890Smrg	    break;
2501ab64890Smrg
2511ab64890Smrg	  case XOMOrientation_Context:
2521ab64890Smrg	    /* never used? */
2531ab64890Smrg	    break;
2541ab64890Smrg	}
2551ab64890Smrg
2561ab64890Smrg	if(char_len <= 0)
2571ab64890Smrg	    break;
2581ab64890Smrg
2591ab64890Smrg        length -= char_len;
2601ab64890Smrg        ptr += ptr_len;
2611ab64890Smrg    }
2621ab64890Smrg
2631ab64890Smrg    switch(oc->core.orientation) {
2641ab64890Smrg      case XOMOrientation_LTR_TTB:
2651ab64890Smrg      case XOMOrientation_RTL_TTB:
2661ab64890Smrg	ret = x;
2671ab64890Smrg	break;
2681ab64890Smrg      case XOMOrientation_TTB_RTL:
2691ab64890Smrg      case XOMOrientation_TTB_LTR:
2701ab64890Smrg	ret = y;
2711ab64890Smrg	break;
2721ab64890Smrg      case XOMOrientation_Context:
2731ab64890Smrg	/* not used? */
2741ab64890Smrg	break;
2751ab64890Smrg    }
2761ab64890Smrg    return ret;
2771ab64890Smrg}
2781ab64890Smrg
2791ab64890Smrg/* For VW/UDC */
2801ab64890Smrg
2811ab64890Smrgint
2821ab64890Smrg_XomGenericDrawString(
2831ab64890Smrg    Display *dpy,
2841ab64890Smrg    Drawable d,
2851ab64890Smrg    XOC oc,
2861ab64890Smrg    GC gc,
2871ab64890Smrg    int x, int y,
2881ab64890Smrg    XOMTextType type,
2891ab64890Smrg    XPointer text,
2901ab64890Smrg    int length)
2911ab64890Smrg{
2921ab64890Smrg    XlcConv conv;
2931ab64890Smrg    XFontStruct *font;
2941ab64890Smrg    Bool is_xchar2b;
2951ab64890Smrg/* VW/UDC */
2961ab64890Smrg    XPointer args[3];
2971ab64890Smrg    FontSet fs;
2981ab64890Smrg/* VW/UDC */
2991ab64890Smrg    XChar2b xchar2b_buf[BUFSIZ], *buf;
3001ab64890Smrg    int start_x = x;
3011ab64890Smrg    int start_y = y;
3021ab64890Smrg    int left = 0, buf_len = 0;
3031ab64890Smrg    int next = 0;
3041ab64890Smrg
3051ab64890Smrg    conv = _XomInitConverter(oc, type);
3061ab64890Smrg    if (conv == NULL)
3071ab64890Smrg	return -1;
30861b2299dSmrg
3091ab64890Smrg    args[0] = (XPointer) &font;
3101ab64890Smrg    args[1] = (XPointer) &is_xchar2b;
3111ab64890Smrg    args[2] = (XPointer) &fs;
3121ab64890Smrg
3131ab64890Smrg    while (length > 0) {
3141ab64890Smrg	buf = xchar2b_buf;
3151ab64890Smrg	left = buf_len = BUFSIZ;
3161ab64890Smrg
3171ab64890Smrg	if (_XomConvert(oc, conv, (XPointer *) &text, &length,
3181ab64890Smrg			(XPointer *) &buf, &left, args, 3) < 0)
3191ab64890Smrg	    break;
3201ab64890Smrg	buf_len -= left;
3211ab64890Smrg
3221ab64890Smrg/* For VW/UDC */
3231ab64890Smrg	next = DrawStringWithFontSet(dpy, d, oc, fs, gc, x, y,
3241ab64890Smrg				     (XPointer)xchar2b_buf, buf_len);
3251ab64890Smrg
3261ab64890Smrg	switch(oc->core.orientation) {
3271ab64890Smrg	  case XOMOrientation_LTR_TTB:
3281ab64890Smrg	  case XOMOrientation_RTL_TTB:
3291ab64890Smrg	    x = next;
3301ab64890Smrg	    break;
3311ab64890Smrg	  case XOMOrientation_TTB_RTL:
3321ab64890Smrg	  case XOMOrientation_TTB_LTR:
3331ab64890Smrg	    y = next;
3341ab64890Smrg	    break;
3351ab64890Smrg          case XOMOrientation_Context:
3361ab64890Smrg	    /* not used */
3371ab64890Smrg	    break;
3381ab64890Smrg	}
3391ab64890Smrg/* For VW/UDC */
3401ab64890Smrg    }
3411ab64890Smrg
3421ab64890Smrg    x -= start_x;
3431ab64890Smrg    y -= start_y;
3441ab64890Smrg
3451ab64890Smrg    return x;
3461ab64890Smrg}
3471ab64890Smrg
3481ab64890Smrgint
3491ab64890Smrg_XmbGenericDrawString(Display *dpy, Drawable d, XOC oc, GC gc, int x, int y,
3501ab64890Smrg		      _Xconst char *text, int length)
3511ab64890Smrg{
3521ab64890Smrg    return _XomGenericDrawString(dpy, d, oc, gc, x, y, XOMMultiByte,
3531ab64890Smrg				 (XPointer) text, length);
3541ab64890Smrg}
3551ab64890Smrg
3561ab64890Smrgint
3571ab64890Smrg_XwcGenericDrawString(Display *dpy, Drawable d, XOC oc, GC gc, int x, int y,
3581ab64890Smrg		      _Xconst wchar_t *text, int length)
3591ab64890Smrg{
3601ab64890Smrg    return _XomGenericDrawString(dpy, d, oc, gc, x, y, XOMWideChar,
3611ab64890Smrg				 (XPointer) text, length);
3621ab64890Smrg}
3631ab64890Smrg
3641ab64890Smrgint
3651ab64890Smrg_Xutf8GenericDrawString(Display *dpy, Drawable d, XOC oc, GC gc, int x, int y,
3661ab64890Smrg			_Xconst char *text, int length)
3671ab64890Smrg{
3681ab64890Smrg    return _XomGenericDrawString(dpy, d, oc, gc, x, y, XOMUtf8String,
3691ab64890Smrg				 (XPointer) text, length);
3701ab64890Smrg}
371