home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1999 March / PCShareware-3-99.iso / IMPLE / DJGPP.RAR / DJGPP2 / XLIB-SR0.ZIP / SRC / XLIBEMU / DRAWTEXT.C < prev    next >
C/C++ Source or Header  |  1994-02-10  |  5KB  |  219 lines

  1. /* $Id: drawtext.c 1.3 1994/02/11 00:47:09 ulrich Exp $ */
  2. /*
  3.  * drawtext.c
  4.  *
  5.  * X library text drawing functions.
  6.  */
  7. #include "Xlibemu.h"
  8.  
  9. GrTextOption _TextOpt = {
  10.   (GrFont *) 0,
  11.     1, 1,
  12.     0, 0,
  13.     GR_TEXT_RIGHT,
  14.     GR_ALIGN_LEFT,
  15.     GR_ALIGN_BASELINE,
  16.     GR_BYTE_TEXT
  17.   };
  18.  
  19. extern unsigned long _GXfunctionToGrOPER[];
  20.  
  21. #define GrCOLOR 0xffff
  22.  
  23. void
  24. GxPatternString (char *text, int length, int x, int y, GrTextOption *opt, GrPattern *pat)
  25. {
  26.   int x1, y1, x2, y2;
  27.   int ww, hh;
  28.   int width, height;
  29.   int fgcolor, bgcolor;
  30.   int y_orig, x_orig;
  31.   GrContext *cxt, cxt_save;
  32.   GrFont *f = opt->txo_font;
  33.  
  34.   x_orig = x;
  35.   y_orig = y;
  36.  
  37.   /* code from GrDrawString() */
  38.  
  39.   if((f == NULL) ||
  40.      (_GrGetTextSize(text,length,&width,&height,opt) == FALSE) ||
  41.      (width  == 0) ||
  42.      (height == 0))
  43.     return;
  44.   if((ww = width)  < 0) { ww = (-ww); x += (ww - 1); }
  45.   if((hh = height) < 0) { hh = (-hh); y += (hh - 1); }
  46.   switch(opt->txo_xalign) {
  47.   case GR_ALIGN_RIGHT:
  48.     x -= (ww - 1);
  49.     break;
  50.   case GR_ALIGN_CENTER:
  51.     x -= (ww >> 1);
  52.     break;
  53.   }
  54.   switch(opt->txo_yalign) {
  55.   case GR_ALIGN_BASELINE:
  56.     if(opt->txo_direct == GR_TEXT_DEFAULT) y -= f->fnt_baseline;
  57.     break;
  58.   case GR_ALIGN_BOTTOM:
  59.     y -= (hh - 1);
  60.     break;
  61.   case GR_ALIGN_CENTER:
  62.     y -= (hh >> 1);
  63.     break;
  64.   }
  65.   x2 = (x1 = x) + width  + ((width  > 0) ? (-1) : 1);
  66.   y2 = (y1 = y) + height + ((height > 0) ? (-1) : 1);
  67.  
  68.   /* end code GrDrawString() */
  69.  
  70.   GrSaveContext (&cxt_save);
  71.   cxt = GrCreateContext (width, 2 * height, NULL, NULL);
  72.   GrSetContext (cxt);
  73.   GrBitBlt (cxt, 0, 0, &cxt_save, x1, y1, x2, y2, GrWRITE);
  74.   GrBitBlt (cxt, 0, height, &cxt_save, x1, y1, x2, y2, GrWRITE);
  75.  
  76.   fgcolor = pat->gp_bmp_fgcolor;
  77.   bgcolor = pat->gp_bmp_bgcolor;
  78.  
  79.   pat->gp_bmp_fgcolor = 0;
  80.   pat->gp_bmp_bgcolor = GrNOCOLOR;
  81.   GrPatternFilledBox (0, 0, width - 1, height - 1, pat);
  82.       
  83.   GrDrawString (text, length, x_orig - x1, y_orig - y1, opt);
  84.  
  85.   pat->gp_bmp_fgcolor = GrNOCOLOR;
  86.   pat->gp_bmp_bgcolor = 0;
  87.   GrPatternFilledBox (0, height, width - 1, 2 * height - 1, pat);
  88.  
  89.   GrBitBlt (&cxt_save, x1, y1, cxt, 0, 0, width - 1, height - 1, GrWRITE);
  90.   GrBitBlt (&cxt_save, x1, y1, cxt, 0, height, width - 1, 2 * height - 1, GrOR);
  91.   GrDestroyContext (cxt);
  92.   GrSetContext (&cxt_save);
  93.   
  94.   pat->gp_bmp_fgcolor = fgcolor;
  95.   pat->gp_bmp_bgcolor = bgcolor;
  96. }
  97.  
  98. int
  99. XDrawString(
  100.     Display*        display,
  101.     Drawable        d,
  102.     GC            gc,
  103.     int            x,
  104.     int            y,
  105.     _Xconst char*    string,
  106.     int            length)
  107. {
  108.   int oper;
  109.  
  110.   if (_WDrawContext (d, gc) == 0)
  111.     return 0;
  112.   FlushGC(display, gc);
  113.   _TextOpt.txo_font = gc->values.font;
  114.   _TextOpt.txo_bgcolor.v = GrNOCOLOR;
  115.   oper = _GXfunctionToGrOPER[gc->values.function];
  116.   _TextOpt.txo_fgcolor.v = oper | gc->values.foreground;
  117.   switch (gc->values.fill_style) {
  118.   case FillSolid:
  119.     _WDrawRegion (GrDrawString, (long) string, length, x, y, (long) &_TextOpt);
  120.     break;
  121.   case FillStippled:
  122.   case FillOpaqueStippled:
  123.     GxPatternString (string, length, x, y, &_TextOpt, gc->values.stipple->pattern);
  124.     break;
  125.   }
  126.   return 1;
  127. }
  128.  
  129. int
  130. XDrawImageString(
  131.     Display*        display,
  132.     Drawable        d,
  133.     GC            gc,
  134.     int            x,
  135.     int            y,
  136.     _Xconst char*    string,
  137.     int            length)
  138. {
  139.   int oper;
  140.  
  141.   if (_WDrawContext (d, gc) == 0)
  142.     return 0;
  143.   FlushGC(display, gc);
  144.   /*
  145.    * XLib documentation says to ignore fill-style and function
  146.    */
  147.  
  148.   _TextOpt.txo_font = gc->values.font;
  149.   _TextOpt.txo_fgcolor.v = gc->values.foreground;
  150.   _TextOpt.txo_bgcolor.v = gc->values.background;
  151.   _WDrawRegion (GrDrawString, (long) string, length, x, y, (long) &_TextOpt);
  152.  
  153.   return 1;
  154. }
  155.  
  156. int
  157. XDrawString16(
  158.     Display*        display,
  159.     Drawable        d,
  160.     GC            gc,
  161.     int            x,
  162.     int            y,
  163.     _Xconst XChar2b*    string,
  164.     int            length)
  165. {
  166.   return 0;
  167. }
  168.  
  169. int
  170. XDrawText(
  171.     Display*        display,
  172.     Drawable        d,
  173.     GC            gc,
  174.     int            x,
  175.     int            y,
  176.     XTextItem*        items,
  177.     int            nitems)
  178. {
  179.   int i;
  180.   XFontStruct *fs;
  181.  
  182.   if (_WDrawContext (d, gc) == 0)
  183.     return 0;
  184.   FlushGC(display, gc);
  185.   fs = XQueryFont (display, (XID) gc->values.font);
  186.  
  187.   for (i = 0; i < nitems; i++) {
  188.  
  189.     if (items->nchars > 0 && items->chars) {
  190.       if (items->font != None) {
  191.     XSetFont (display, gc, items->font);
  192.     fs = XQueryFont (display, (XID) gc->values.font);
  193.       }
  194.       x += items->delta;
  195.       XDrawString (display, d, gc, x, y,
  196.            items->chars, items->nchars);
  197.       x += XTextWidth (fs, items->chars, items->nchars);
  198.     }
  199.  
  200.     items++;
  201.   }
  202.   return 1;
  203. }
  204.  
  205. int
  206. XDrawText16(
  207.     Display*        display,
  208.     Drawable        d,
  209.     GC            gc,
  210.     int            x,
  211.     int            y,
  212.     XTextItem16*    items,
  213.     int            nitems
  214. )
  215. {
  216.   FlushGC(display, gc);
  217.   return 0;
  218. }
  219.