home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / enl_DR-0.10.tar.gz / enl_DR-0.10.tar / enl / text.c < prev    next >
C/C++ Source or Header  |  1997-06-14  |  5KB  |  245 lines

  1. #include "enlightenment.h"
  2.  
  3. void FontField(char *font, int num, char *ret)
  4. {
  5.    int count;
  6.    int i,j,k,l;
  7.    
  8.    i=0;
  9.    count=-1;
  10.    while (count<num)
  11.      {
  12.     if (font[i++]=='-') count++;
  13.      }
  14.    j=i;
  15.    while (font[j++]!='-');
  16.    l=0;
  17.    for (k=i;k<j-1;k++)
  18.      ret[l++]=font[k];
  19.    ret[l]=0;
  20. }
  21.  
  22. void InitTextInfo()
  23. {
  24.    char **fonts;
  25.    char pat[1024];
  26.    int num;
  27.    int i;
  28.    int flag;
  29.    char field[1024];
  30.    
  31.    sprintf(pat,"-*-%s-*-*-*-*-*-*-*-*-*-*-*-*",cfg.font);
  32.    fonts=XListFonts(disp,pat,10240,&num);
  33.    flag=0;
  34.     for (i=0;i<num;i++)
  35.      {
  36.     FontField(fonts[i],1,field);
  37.     if (!strcmp(cfg.font,field)) flag=1;
  38.      }
  39.    if (!flag)
  40.      {
  41.     free(cfg.font);
  42.     cfg.font=malloc(6);
  43.     strcpy(cfg.font,"fixed");
  44.      }
  45.    if (cfg.font_style&BOLD)
  46.      {
  47.     flag=0;
  48.     for (i=0;i<num;i++)
  49.       {
  50.          FontField(fonts[i],2,field);
  51.          if (!strcmp("bold",field)) flag=1;
  52.       }
  53.     if (flag) strcpy(font_weight,"bold");
  54.     else strcpy(font_weight,"*");
  55.      }
  56.    else 
  57.      {
  58.     flag=0;
  59.     for (i=0;i<num;i++)
  60.       {
  61.          FontField(fonts[i],2,field);
  62.          if (!strcmp("medium",field)) flag=1;
  63.              }
  64.     if (flag) strcpy(font_weight,"medium");
  65.     else strcpy(font_weight,"*");
  66.      }
  67.    if (cfg.font_style&ITALIC)
  68.      {
  69.     flag=0;
  70.           for (i=0;i<num;i++)
  71.       {
  72.          FontField(fonts[i],3,field);
  73.          if (!strcmp("i",field)) flag=1;
  74.       }
  75.     if (flag) strcpy(font_slant,"i");
  76.     else strcpy (font_slant,"*");
  77.      }
  78.    else 
  79.      {
  80.     flag=0;
  81.     for (i=0;i<num;i++)
  82.       {
  83.          FontField(fonts[i],3,field);
  84.          if (!strcmp("r",field)) flag=1;
  85.              }
  86.     if (flag) strcpy(font_slant,"r");
  87.     else strcpy(font_slant,"*");
  88.      }
  89.    XFreeFontNames(fonts);                      
  90. }
  91.  
  92. void DrawText(Pixmap p, char *txt, int px, int py, int w, int h)
  93. {
  94.    GC fg;
  95.    GC bg;
  96.    XGCValues gcv;
  97.    int hh;
  98.    int i;
  99.    int x,y,ww;
  100.    XFontStruct *xfs;
  101.    XCharStruct dummy2;
  102.    Font font;
  103.    int dummy;
  104.    int ascent;
  105.    int descent;
  106.    char font_string[1024];
  107.    char tstr[32];
  108.    int mode;
  109.    
  110.    hh=h-2;
  111.    if (hh<2) return;
  112.    for (i=hh;i>2;i--)
  113.      {
  114.     sprintf(font_string,"-*-%s-%s-%s-*-*-%i-*-*-*-*-*-*-*",
  115.         cfg.font,font_weight,font_slant,i);
  116.     font=XLoadFont(disp,font_string);
  117.     if (!font) return;
  118.     xfs=XLoadQueryFont(disp,font_string);
  119.     if (!xfs) return;
  120.     XTextExtents(xfs,txt,strlen(txt),&dummy,&ascent,&descent,&dummy2);
  121.     if ((ascent+descent)<=hh) break;
  122.     XFreeFont(disp,xfs);
  123.      }
  124.    fg=XCreateGC(disp,p,0,&gcv);
  125.    bg=XCreateGC(disp,p,0,&gcv);
  126.    XSetFont(disp,fg,font);
  127.    XSetFont(disp,bg,font);
  128.    XSetForeground(disp,fg,cfg.font_fg);
  129.    XSetForeground(disp,bg,cfg.font_bg);
  130.    
  131.    if (cfg.font_style&OUTLINE) mode=1;
  132.    else if (cfg.font_style&SHADOW) mode=2;
  133.    else mode=0;
  134.    
  135.    for (x=strlen(txt);x>0;x--)
  136.      {
  137.     i=XTextWidth(xfs,txt,x);
  138.     if (i<(w-2))
  139.       {
  140.          y=1+ascent;
  141.          ww=x;
  142.          if (cfg.font_style&J_RIGHT) x=w-i-1;
  143.          else if (cfg.font_style&J_CENTER) x=(w>>1)-(i>>1);
  144.          else x=1;
  145.          if (mode==0)
  146.            {
  147.           XDrawString(disp,p,fg,px+x,py+y,txt,ww);
  148.            }
  149.          else if (mode==1)
  150.            {
  151.           XDrawString(disp,p,bg,px+x-1,py+y,txt,ww);
  152.           XDrawString(disp,p,bg,px+x+1,py+y,txt,ww);
  153.           XDrawString(disp,p,bg,px+x,py+y-1,txt,ww);
  154.           XDrawString(disp,p,bg,px+x,py+y+1,txt,ww);
  155.           XDrawString(disp,p,fg,px+x,py+y,txt,ww);
  156.            }
  157.          else if (mode==2)
  158.            {
  159.           XDrawString(disp,p,bg,px+x+1,py+y+1,txt,ww);
  160.           XDrawString(disp,p,fg,px+x,py+y,txt,ww);
  161.            }
  162.          x=-1;
  163.          break;
  164.       }
  165.      }
  166.    XFreeFont(disp,xfs);
  167.    XFreeGC(disp,fg);
  168.    XFreeGC(disp,bg);
  169. }
  170.  
  171. void DrawTextMask(Pixmap p, char *txt, int px, int py, int w, int h)
  172. {
  173.    GC or;
  174.    XGCValues gcv;
  175.    int hh;
  176.    int i;
  177.    int x,y,ww;
  178.    XFontStruct *xfs;
  179.    XCharStruct dummy2;
  180.    Font font;
  181.    int dummy;
  182.    int ascent;
  183.    int descent;
  184.    char font_string[1024];
  185.    char tstr[32];
  186.    int mode;
  187.    
  188.    hh=h-2;
  189.    if (hh<2) return;
  190.    for (i=hh;i>2;i--)
  191.      {
  192.     sprintf(font_string,"-*-%s-%s-%s-*-*-%i-*-*-*-*-*-*-*",
  193.         cfg.font,font_weight,font_slant,i);
  194.     font=XLoadFont(disp,font_string);
  195.     if (!font) return;
  196.     xfs=XLoadQueryFont(disp,font_string);
  197.     if (!xfs) return;
  198.     XTextExtents(xfs,txt,strlen(txt),&dummy,&ascent,&descent,&dummy2);
  199.     if ((ascent+descent)<=hh) break;
  200.     XFreeFont(disp,xfs);
  201.      }
  202.    gcv.function=GXor;
  203.    or=XCreateGC(disp,p,GCFunction,&gcv);
  204.    XSetForeground(disp,or,1);
  205.    XSetFont(disp,or,font);
  206.    
  207.    if (cfg.font_style&OUTLINE) mode=1;
  208.    else if (cfg.font_style&SHADOW) mode=2;
  209.    else mode=0;
  210.    
  211.    for (x=strlen(txt);x>0;x--)
  212.      {
  213.     i=XTextWidth(xfs,txt,x);
  214.     if (i<(w-2))
  215.       {
  216.          y=1+ascent;
  217.          ww=x;
  218.          if (cfg.font_style&J_RIGHT) x=w-i-1;
  219.          else if (cfg.font_style&J_CENTER) x=(w>>1)-(i>>1);
  220.          else x=1;
  221.          if (mode==0)
  222.            {
  223.           XDrawString(disp,p,or,px+x,py+y,txt,ww);
  224.            }
  225.          else if (mode==1)
  226.            {
  227.           XDrawString(disp,p,or,px+x-1,py+y,txt,ww);
  228.           XDrawString(disp,p,or,px+x+1,py+y,txt,ww);
  229.           XDrawString(disp,p,or,px+x,py+y-1,txt,ww);
  230.           XDrawString(disp,p,or,px+x,py+y+1,txt,ww);
  231.           XDrawString(disp,p,or,px+x,py+y,txt,ww);
  232.            }
  233.          else if (mode==2)
  234.            {
  235.           XDrawString(disp,p,or,px+x+1,py+y+1,txt,ww);
  236.           XDrawString(disp,p,or,px+x,py+y,txt,ww);
  237.            }
  238.          x=-1;
  239.          break;
  240.       }
  241.      }
  242.    XFreeFont(disp,xfs);
  243.    XFreeGC(disp,or);
  244. }
  245.