home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / enl_BETA-0.13.src.tar.gz / enl_BETA-0.13.src.tar / enl-0.13 / text.c < prev    next >
C/C++ Source or Header  |  1997-11-16  |  5KB  |  254 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;
  107.    /* char tstr[32]; */
  108.    char mode;
  109.    
  110.    hh=h-2;
  111.    if (hh<2) return;
  112.    font_string = malloc(strlen(cfg.font)+
  113.        strlen(font_weight)+
  114.        strlen(font_slant)+33);
  115.    for (i=hh;i>2;i--)
  116.      {
  117.     sprintf(font_string,"-*-%s-%s-%s-*-*-%i-*-*-*-*-*-*-*",
  118.         cfg.font,font_weight,font_slant,i);
  119.     font=XLoadFont(disp,font_string);
  120.     if (!font) return;
  121.     xfs=XLoadQueryFont(disp,font_string);
  122.     if (!xfs) return;
  123.     XTextExtents(xfs,txt,strlen(txt),&dummy,&ascent,&descent,&dummy2);
  124.     if ((ascent+descent)<=hh) break;
  125.     XFreeFont(disp,xfs);
  126.      }
  127.    fg=XCreateGC(disp,p,0,&gcv);
  128.    bg=XCreateGC(disp,p,0,&gcv);
  129.    XSetFont(disp,fg,font);
  130.    XSetFont(disp,bg,font);
  131.    XSetForeground(disp,fg,cfg.font_fg);
  132.    XSetForeground(disp,bg,cfg.font_bg);
  133.    
  134.    if (cfg.font_style&OUTLINE) mode=1;
  135.    else if (cfg.font_style&SHADOW) mode=2;
  136.    else mode=0;
  137.    
  138.    for (x=strlen(txt);x>0;x--)
  139.      {
  140.     i=XTextWidth(xfs,txt,x);
  141.     if (i<(w-2))
  142.       {
  143.          y=1+ascent;
  144.          ww=x;
  145.          if (cfg.font_style&J_RIGHT) x=w-i-1;
  146.          else if (cfg.font_style&J_CENTER) x=(w>>1)-(i>>1);
  147.          else x=1;
  148.          if (mode==0)
  149.            {
  150.           XDrawString(disp,p,fg,px+x,py+y,txt,ww);
  151.            }
  152.          else if (mode==1)
  153.            {
  154.           XDrawString(disp,p,bg,px+x-1,py+y,txt,ww);
  155.           XDrawString(disp,p,bg,px+x+1,py+y,txt,ww);
  156.           XDrawString(disp,p,bg,px+x,py+y-1,txt,ww);
  157.           XDrawString(disp,p,bg,px+x,py+y+1,txt,ww);
  158.           XDrawString(disp,p,fg,px+x,py+y,txt,ww);
  159.            }
  160.          else if (mode==2)
  161.            {
  162.           XDrawString(disp,p,bg,px+x+1,py+y+1,txt,ww);
  163.           XDrawString(disp,p,fg,px+x,py+y,txt,ww);
  164.            }
  165.          x=-1;
  166.          break;
  167.       }
  168.      }
  169.    XFreeFont(disp,xfs);
  170.    free(font_string);
  171.    XFreeGC(disp,fg);
  172.    XFreeGC(disp,bg);
  173. }
  174.  
  175. void DrawTextMask(Pixmap p, char *txt, int px, int py, int w, int h)
  176. {
  177.    GC or;
  178.    XGCValues gcv;
  179.    int hh;
  180.    int i;
  181.    int x,y,ww;
  182.    XFontStruct *xfs;
  183.    XCharStruct dummy2;
  184.    Font font;
  185.    int dummy;
  186.    int ascent;
  187.    int descent;
  188.    char *font_string;
  189.    /* char tstr[32]; */
  190.    char mode;
  191.  
  192.    
  193.    hh=h-2;
  194.    if (hh<2) return;
  195.    font_string= malloc(strlen(cfg.font)+
  196.                 strlen(font_weight)+
  197.             strlen(font_slant)+33);
  198.    for (i=hh;i>2;i--)
  199.      {
  200.     sprintf(font_string,"-*-%s-%s-%s-*-*-%i-*-*-*-*-*-*-*",
  201.         cfg.font,font_weight,font_slant,i);
  202.     font=XLoadFont(disp,font_string);
  203.     if (!font) return;
  204.     xfs=XLoadQueryFont(disp,font_string);
  205.     if (!xfs) return;
  206.     XTextExtents(xfs,txt,strlen(txt),&dummy,&ascent,&descent,&dummy2);
  207.     if ((ascent+descent)<=hh) break;
  208.     XFreeFont(disp,xfs);
  209.      }
  210.    gcv.function=GXor;
  211.    or=XCreateGC(disp,p,GCFunction,&gcv);
  212.    XSetForeground(disp,or,1);
  213.    XSetFont(disp,or,font);
  214.    
  215.    if (cfg.font_style&OUTLINE) mode=1;
  216.    else if (cfg.font_style&SHADOW) mode=2;
  217.    else mode=0;
  218.    
  219.    for (x=strlen(txt);x>0;x--)
  220.      {
  221.     i=XTextWidth(xfs,txt,x);
  222.     if (i<(w-2))
  223.       {
  224.          y=1+ascent;
  225.          ww=x;
  226.          if (cfg.font_style&J_RIGHT) x=w-i-1;
  227.          else if (cfg.font_style&J_CENTER) x=(w>>1)-(i>>1);
  228.          else x=1;
  229.          if (mode==0)
  230.            {
  231.           XDrawString(disp,p,or,px+x,py+y,txt,ww);
  232.            }
  233.          else if (mode==1)
  234.            {
  235.           XDrawString(disp,p,or,px+x-1,py+y,txt,ww);
  236.           XDrawString(disp,p,or,px+x+1,py+y,txt,ww);
  237.           XDrawString(disp,p,or,px+x,py+y-1,txt,ww);
  238.           XDrawString(disp,p,or,px+x,py+y+1,txt,ww);
  239.           XDrawString(disp,p,or,px+x,py+y,txt,ww);
  240.            }
  241.          else if (mode==2)
  242.            {
  243.           XDrawString(disp,p,or,px+x+1,py+y+1,txt,ww);
  244.           XDrawString(disp,p,or,px+x,py+y,txt,ww);
  245.            }
  246.          x=-1;
  247.          break;
  248.       }
  249.      }
  250.    free(font_string);
  251.    XFreeFont(disp,xfs);
  252.    XFreeGC(disp,or);
  253. }
  254.