home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xom_XChar.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  12KB  |  472 lines

  1. /* $XConsortium: omXChar.c /main/6 1996/12/05 10:41:05 swick $ */
  2. /*
  3.  * Copyright 1992, 1993 by TOSHIBA Corp.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose and without fee is hereby granted, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of TOSHIBA not be used in advertising
  10.  * or publicity pertaining to distribution of the software without specific,
  11.  * written prior permission. TOSHIBA make no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * TOSHIBA DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17.  * TOSHIBA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21.  * SOFTWARE.
  22.  *
  23.  * Author: Katsuhisa Yano    TOSHIBA Corp.
  24.  *                   mopi@osa.ilab.toshiba.co.jp
  25.  */
  26. /*
  27.  * Copyright 1995 by FUJITSU LIMITED
  28.  * This is source code modified by FUJITSU LIMITED under the Joint
  29.  * Development Agreement for the CDE/Motif PST.
  30.  *
  31.  * Modifier: Takanori Tateno   FUJITSU LIMITED
  32.  *
  33.  */
  34.  
  35. #include "Xlib_private.h"
  36. #include "XlcPublic.h"
  37. #include "XomGeneric.h"
  38. #include <stdio.h>
  39.  
  40. /* for VW/UDC start */
  41. static Bool
  42. ismatch_scopes(fontdata,value, is_shift)
  43.     FontData      fontdata;
  44.     unsigned long *value;
  45.     Bool      is_shift;
  46. {
  47.     DBUG_ENTER("ismatch_scopes")
  48.     register int side, scopes_num = fontdata->scopes_num;
  49.     FontScope scopes = fontdata->scopes;
  50.     if (!scopes_num)
  51.         DBUG_RETURN(False);
  52.  
  53.     if(fontdata->font == NULL)
  54.     DBUG_RETURN(False);
  55.  
  56.     side = fontdata->side;
  57.  
  58.     for(;scopes_num--;scopes++)
  59.         if ((scopes->start <= (*value & 0x7f7f)) &&
  60.                         ((scopes->end) >= (*value & 0x7f7f))){
  61.         if(is_shift == True) {
  62.                 if(scopes->shift){
  63.                     if(scopes->shift_direction == '+'){
  64.                         *value += scopes->shift ;
  65.                     } else if( scopes->shift_direction == '-'){
  66.                         *value -= scopes->shift ;
  67.                     }
  68.                 }
  69.             }
  70.             DBUG_RETURN(True);
  71.         }
  72.  
  73.     DBUG_RETURN(False);
  74. }
  75.  
  76. static int
  77. check_vertical_fonttype(name)
  78.     char    *name;
  79. {
  80.     DBUG_ENTER("check_vertical_fonttype")
  81.     char    *ptr;
  82.     int        type = 0;
  83.  
  84.     if(name == (char *)NULL || (int) strlen(name) <= 0)
  85.     DBUG_RETURN(False);
  86.  
  87.     /* Obtains the pointer of CHARSET_ENCODING_FIELD. */
  88.     if((ptr = strchr(name, '-')) == (char *) NULL)
  89.     DBUG_RETURN(False);
  90.     ptr++;
  91.  
  92.     /* Obtains the pointer of vertical_map font type. */
  93.     if((ptr = strchr(ptr, '.')) == (char *) NULL)
  94.     DBUG_RETURN(False);
  95.     ptr++;
  96.  
  97.     switch(*ptr) {
  98.       case '1':
  99.     type = 1;    break;
  100.       case '2':
  101.     type = 2;    break;
  102.       case '3':
  103.     type = 3;    break;
  104.     }
  105.     DBUG_RETURN(type);
  106. }
  107.  
  108. /*
  109. */
  110. #define VMAP          0
  111. #define VROTATE       1
  112. #define FONTSCOPE     2
  113.  
  114. FontData
  115. _XomGetFontDataFromFontSet(fs,str,len,len_ret,is2b,type)
  116. FontSet fs;
  117. unsigned char *str;
  118. int len;
  119. int *len_ret;
  120. int is2b;     
  121. int type;          /* VMAP , VROTATE , else */
  122. {
  123.     DBUG_ENTER("_XomGetFontDataFromFontSet")
  124.     unsigned long value;
  125.     int num,i,hit,csize;
  126.     FontData fontdata;
  127.     unsigned char *c;
  128.     int vfont_type;
  129.     
  130.     c = str;
  131.     hit = -1;
  132.     if(type == VMAP){
  133.     fontdata = fs->vmap;
  134.     num      = fs->vmap_num;
  135.     } else if(type == VROTATE){
  136.         fontdata = (FontData)fs->vrotate;
  137.     num      = fs->vrotate_num;
  138.     } else {
  139.     if(fs->font_data_count <= 0 || fs->font_data == (FontData)NULL) {
  140.         fontdata = fs->substitute;
  141.         num      = fs->substitute_num;
  142.     }else {
  143.             fontdata = fs->font_data;
  144.         num      = fs->font_data_count;
  145.     }
  146.     /* CDExc20229 fix */
  147.     if(fontdata == NULL || num == 0){
  148.         DBUG_RETURN(NULL);
  149.     }
  150.     }
  151.  
  152.  
  153.     if(is2b){
  154.         csize = 2;
  155.     } else {
  156.         csize = 1;
  157.     }
  158.  
  159.     for(;len;len--){
  160.         if(is2b){
  161.             value = (((unsigned long)*c) << 8)|(unsigned long)*(c + 1);
  162.         } else {
  163.             value = (unsigned long)*c;
  164.         }
  165.     
  166.         for (i=0;i<num;i++) {
  167.         if(type == VROTATE) {
  168.         if(fontdata[i].font) {
  169.             /* If the num_cr equal zero, all character is rotated. */
  170.             if(fontdata[i].scopes_num == 0) {
  171.             break;
  172.             } else {
  173.             /* The vertical rotate glyph is not have code shift. */
  174.             if (ismatch_scopes(&(fontdata[i]),&value,False)) {
  175.                 break;
  176.             }
  177.             }
  178.         }
  179.         } else if(type == VMAP) {
  180.         if(fontdata[i].font) {
  181.             vfont_type = check_vertical_fonttype(fontdata[i].name);
  182.             if(vfont_type == 0 || vfont_type == 1) {
  183.             break;
  184.             } else if(vfont_type == 2 || vfont_type == 3) {
  185.             if(fontdata[i].scopes_num <= 0)
  186.                 break;
  187.  
  188.             if (ismatch_scopes(&(fontdata[i]),&value,True)) {
  189.                 break;
  190.             }
  191.             }
  192.         }
  193.         } else { /* FONTSCOPE */
  194.         if(fontdata[i].font) {
  195.             if(fontdata[i].scopes_num <= 0)
  196.                         break;
  197.             if (ismatch_scopes(&(fontdata[i]),&value,True)){
  198.                 break;
  199.                     }
  200.         }
  201.         }
  202.         }
  203.         if((hit != -1) && (i != hit)){
  204.             break;
  205.         }
  206.         if(i == num){
  207.             if( type == VROTATE || type == VMAP){
  208.         /* Change 1996.01.23 start */
  209.         if(fs->font_data_count <= 0 ||
  210.            fs->font_data == (FontData)NULL)
  211.             fontdata = fs->substitute;
  212.         else
  213.             fontdata = fs->font_data;
  214.         /* Change 1996.01.23 end */
  215.         }
  216.         hit = 0;
  217.             c += csize;
  218.         break;
  219.         }
  220.         if( hit == -1 ) hit = i;
  221.         if(is2b){
  222.             *c = (unsigned char)(value >> 8);
  223.             *(c + 1) = (unsigned char)(value);
  224.         } else {
  225.             *c = (unsigned char)value;
  226.         }
  227.         c += csize;
  228.     }
  229.     *len_ret = (c - str);
  230.     DBUG_RETURN(&(fontdata[hit]));
  231. }
  232. /* for VW/UDC end   */
  233.  
  234. static FontSet
  235. _XomGetFontSetFromCharSet(oc, charset)
  236.     XOC oc;
  237.     XlcCharSet charset;
  238. {
  239.     DBUG_ENTER("_XomGetFontSetFromCharSet")
  240.     register FontSet font_set = XOC_GENERIC(oc)->font_set;
  241.     register int num = XOC_GENERIC(oc)->font_set_num;
  242.     XlcCharSet *charset_list;
  243.     int charset_count;
  244.  
  245.     for ( ; num-- > 0; font_set++) {
  246.     charset_count = font_set->charset_count;
  247.     charset_list = font_set->charset_list;
  248.     for ( ; charset_count-- > 0; charset_list++)
  249.         if (*charset_list == charset)
  250.         DBUG_RETURN(font_set);
  251.     }
  252.  
  253.     DBUG_RETURN((FontSet) NULL);
  254. }
  255.  
  256. #ifdef MUSTCOPY
  257. static void
  258. cs_to_xchar2b(from, to, length)
  259.     register char *from;
  260.     register XChar2b *to;
  261.     register length;
  262. {
  263.     DBUG_ENTER("cs_to_xchar2b")
  264.     while (length-- > 0) {
  265.     to->byte1 = *from++;
  266.     to->byte2 = *from++;
  267.     to++;
  268.     }
  269.     DBUG_VOID_RETURN;
  270. }
  271.  
  272. static void
  273. cs_to_xchar2b_gl(from, to, length)
  274.     register char *from;
  275.     register XChar2b *to;
  276.     register length;
  277. {
  278.     DBUG_ENTER("cs_to_xchar2b_gl")
  279.     while (length-- > 0) {
  280.     to->byte1 = *from++ & 0x7f;
  281.     to->byte2 = *from++ & 0x7f;
  282.     to++;
  283.     }
  284.     DBUG_VOID_RETURN;
  285. }
  286.  
  287. static void
  288. cs_to_xchar2b_gr(from, to, length)
  289.     register char *from;
  290.     register XChar2b *to;
  291.     register length;
  292. {
  293.     DBUG_ENTER("cs_to_xchar2b_gr")
  294.     while (length-- > 0) {
  295.     to->byte1 = *from++ | 0x80;
  296.     to->byte2 = *from++ | 0x80;
  297.     to++;
  298.     }
  299.     DBUG_VOID_RETURN;
  300. }
  301. #endif
  302.  
  303. static void
  304. shift_to_gl(text, length)
  305.     register char *text;
  306.     register int length;
  307. {
  308.     DBUG_ENTER("shift_to_gl")
  309.     while (length-- > 0)
  310.     *text++ &= 0x7f;
  311.     DBUG_VOID_RETURN;
  312. }
  313.  
  314. static void
  315. shift_to_gr(text, length)
  316.     register char *text;
  317.     register int length;
  318. {
  319.     DBUG_ENTER("shift_to_gr")
  320.     while (length-- > 0)
  321.     *text++ |= 0x80;
  322.     DBUG_VOID_RETURN;
  323. }
  324.  
  325. static Bool
  326. load_font(oc, font_set)
  327.     XOC oc;
  328.     FontSet font_set;
  329. {
  330.     DBUG_ENTER("load_font")
  331.     font_set->font = XLoadQueryFont(oc->core.om->core.display,
  332.             oc->core.font_info.font_name_list[font_set->id]);
  333.     if (font_set->font == NULL)
  334.     DBUG_RETURN(False);
  335.     
  336.     oc->core.font_info.font_struct_list[font_set->id] = font_set->font;
  337.     XFreeFontInfo(NULL, font_set->info, 1);
  338.     font_set->info = NULL;
  339.  
  340.     if (font_set->font->min_byte1 || font_set->font->max_byte1)
  341.     font_set->is_xchar2b = True;
  342.     else
  343.     font_set->is_xchar2b = False;
  344.  
  345.     DBUG_RETURN(True);
  346. }
  347.  
  348. int
  349. _XomConvert(oc, conv, from, from_left, to, to_left, args, num_args)
  350.     XOC oc;
  351.     XlcConv conv;
  352.     XPointer *from;
  353.     int *from_left;
  354.     XPointer *to;
  355.     int *to_left;
  356.     XPointer *args;
  357.     int num_args;
  358. {
  359.     DBUG_ENTER("_XomConvert")
  360.     XPointer cs, lc_args[1];
  361.     XlcCharSet charset;
  362.     int length, cs_left, ret;
  363.     FontSet font_set;
  364. #ifdef MUSTCOPY
  365.     XChar2b *xchar2b;
  366.     char *buf, buf_local[BUFSIZ];
  367. #endif
  368.     
  369.     cs = *to;
  370.     cs_left = *to_left;
  371.     lc_args[0] = (XPointer) &charset;
  372.  
  373.     ret = _XlcConvert(conv, from, from_left, &cs, &cs_left, lc_args, 1);
  374.     if (ret < 0)
  375.     DBUG_RETURN(-1);
  376.  
  377.     font_set = _XomGetFontSetFromCharSet(oc, charset);
  378.     if (font_set == NULL)
  379.     DBUG_RETURN(-1);
  380.  
  381.     if (font_set->font == NULL && load_font(oc, font_set) == False)
  382.     DBUG_RETURN(-1);
  383.  
  384.     length = *to_left - cs_left;
  385.  
  386. #ifdef MUSTCOPY
  387.     if (font_set->is_xchar2b) {
  388.     buf = (length > BUFSIZ) ? Xmalloc(length) : buf_local;
  389.     if (buf == NULL)
  390.         DBUG_RETURN(-1);
  391.     memcpy(buf, (char *) *to, length);
  392.  
  393.     xchar2b = (XChar2b *) *to;
  394.     length >>= 1;
  395.  
  396.     if (font_set->side == charset->side)
  397.         cs_to_xchar2b(buf, xchar2b, length);
  398.     else if (font_set->side == XlcGL)
  399.         cs_to_xchar2b_gl(buf, xchar2b, length);
  400.     else if (font_set->side == XlcGR)
  401.         cs_to_xchar2b_gr(buf, xchar2b, length);
  402.     else
  403.         cs_to_xchar2b(buf, xchar2b, length);
  404.     
  405.     if (buf != buf_local)
  406.         Xfree(buf);
  407.  
  408.     *to = (XPointer) (xchar2b + length);
  409.     *to_left -= length;
  410.     } else
  411. #endif
  412.     {
  413.     if (font_set->side != charset->side) {
  414.         if (font_set->side == XlcGL)
  415.         shift_to_gl(*to, length);
  416.         else if (font_set->side == XlcGR)
  417.         shift_to_gr(*to, length);
  418.     }
  419.  
  420.     if (font_set->is_xchar2b)
  421.         length >>= 1;
  422.     *to = cs;
  423.     *to_left -= length;
  424.     }
  425.  
  426.     *((XFontStruct **) args[0]) = font_set->font;
  427.     *((Bool *) args[1]) = font_set->is_xchar2b;
  428.     if(num_args >= 3){
  429.         *((FontSet *) args[2]) = font_set;
  430.     }
  431.  
  432.     DBUG_RETURN(ret);
  433. }
  434.  
  435. XlcConv
  436. _XomInitConverter(oc, type)
  437.     XOC oc;
  438.     XOMTextType type;
  439. {
  440.     DBUG_ENTER("_XomInitConverter")
  441.     XOCGenericPart *gen = XOC_GENERIC(oc);
  442.     XlcConv conv;
  443.     char *conv_type;
  444.     XLCd lcd;
  445.  
  446.     if (type == XOMWideChar) {
  447.     conv = gen->wcs_to_cs;
  448.     conv_type = XlcNWideChar;
  449.     } else {
  450.     conv = gen->mbs_to_cs;
  451.     conv_type = XlcNMultiByte;
  452.     }
  453.  
  454.     if (conv) {
  455.     _XlcResetConverter(conv);
  456.     DBUG_RETURN(conv);
  457.     }
  458.  
  459.     lcd = oc->core.om->core.lcd;
  460.  
  461.     conv = _XlcOpenConverter(lcd, conv_type, lcd, XlcNCharSet);
  462.     if (conv == (XlcConv) NULL)
  463.     DBUG_RETURN((XlcConv) NULL);
  464.  
  465.     if (type == XOMWideChar)
  466.     gen->wcs_to_cs = conv;
  467.     else
  468.     gen->mbs_to_cs = conv;
  469.  
  470.     DBUG_RETURN(conv);
  471. }
  472.