home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / voglw.zip / text.c < prev    next >
C/C++ Source or Header  |  1997-02-13  |  5KB  |  295 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef PC
  4. #include <string.h>
  5. #else
  6. #ifdef SYS5
  7. #include <string.h>
  8. #define rindex strrchr
  9. #else
  10. #include <strings.h>
  11. #endif
  12. #endif
  13. #include "vogl.h"
  14.  
  15. static    Vector    cpos;    /* Current character position */
  16. static    int    sync, sc_x, sc_y;
  17.  
  18. /*
  19.  * font
  20.  *     assigns a font.
  21.  */
  22. void
  23. font(id)
  24.     short    id;
  25. {
  26.     Token    *tok;
  27.  
  28.     if (!vdevice.initialised)
  29.         verror("font: vogl not initialised");
  30.  
  31.     if (id < 0 || id >= vdevice.maxfontnum)
  32.         verror("font: font number is out of range");
  33.     
  34.     if (vdevice.inobject) {
  35.         tok = newtokens(2);
  36.         tok[0].i = VFONT;
  37.         tok[1].i = id;
  38.         return;
  39.     }
  40.  
  41.     if (id == 1) {
  42.         if (!(*vdevice.dev.Vfont)(vdevice.dev.large)) 
  43.             verror("font: unable to open large font");
  44.     } else if (id == 0) {
  45.         if (!(*vdevice.dev.Vfont)(vdevice.dev.small))
  46.             verror("font: unable to open small font");
  47.     }
  48.  
  49.     vdevice.attr->a.fontnum = id;
  50. }
  51.  
  52. /*
  53.  * charstr
  54.  *
  55.  * Draw a string from the current character position.
  56.  *
  57.  */
  58. void
  59. charstr(str)
  60.     char     *str;
  61. {
  62.     int    cx, cy;
  63.     char    c;
  64.     Token    *tok;
  65.     int    oldclipoff = vdevice.clipoff;
  66.     int    sl = strlen(str);
  67.  
  68.     if(!vdevice.initialised) 
  69.         verror("charstr: vogl not initialized");
  70.  
  71.     if (vdevice.inobject) {
  72.         tok = newtokens(2 + strlen(str) / sizeof(Token));
  73.  
  74.         tok[0].i = DRAWSTR;
  75.         strcpy((char *)&tok[1], str);
  76.  
  77.         return;
  78.     }
  79.  
  80.     if (sync = vdevice.sync)
  81.         vdevice.sync = 0;
  82.  
  83.     cx = vdevice.cpVx;
  84.     cy = vdevice.cpVy;
  85.  
  86.     vdevice.cpVx = sc_x;
  87.     vdevice.cpVy = sc_y;
  88.  
  89.     /*   If not clipping then simply display text and return  */
  90.  
  91.     if (oldclipoff) {
  92.         (*vdevice.dev.Vstring)(str);
  93.     } else { /* Check if string is within viewport */
  94.          /* Could clip in Z maybe? */
  95.         int    left_s = vdevice.cpVx;
  96.         int    bottom_s = vdevice.cpVy - (int)vdevice.hheight;
  97.         int    top_s = bottom_s + (int)vdevice.hheight;
  98.         int    right_s = left_s + (int)((sl + 1) * vdevice.hwidth);
  99.  
  100.         if (left_s > vdevice.minVx &&
  101.             bottom_s < vdevice.maxVy &&
  102.             top_s > vdevice.minVy &&
  103.             right_s < vdevice.maxVx) {
  104.             (*vdevice.dev.Vstring)(str);
  105.         } else {
  106.             while (c = *str++) {
  107.                 if (vdevice.cpVx > vdevice.minVx &&
  108.                     vdevice.cpVx < vdevice.maxVx - (int)vdevice.hwidth) {
  109.                     (*vdevice.dev.Vchar)(c);
  110.                 }
  111.                 vdevice.cpVx += (int)vdevice.hwidth;
  112.             }
  113.         }
  114.     }
  115.  
  116.     sc_x += sl * (int)vdevice.hwidth;
  117.  
  118.     /* Put our original device graphics position back... */
  119.     vdevice.cpVx = cx;
  120.     vdevice.cpVy = cy;
  121.  
  122.     if (sync) {
  123.         vdevice.sync = 1;
  124.         (*vdevice.dev.Vsync)();
  125.     }
  126.  
  127. }
  128.  
  129. /*
  130.  * cmov
  131.  *
  132.  *    Sets the current character position.
  133.  */
  134. void
  135. cmov(x, y, z)
  136.     float    x, y, z;
  137. {
  138.     Vector    res;
  139.     Token    *tok;
  140.  
  141.     if (vdevice.inobject) {
  142.         tok = newtokens(4);
  143.  
  144.         tok[0].i = CMOV;
  145.         tok[1].f = x;
  146.         tok[2].f = y;
  147.         tok[3].f = z;
  148.  
  149.         return;
  150.     }
  151.  
  152.     cpos[V_X] = x;
  153.     cpos[V_Y] = y;
  154.     cpos[V_Z] = z;
  155.     cpos[V_W] = 1.0;
  156.  
  157.     multvector(res, cpos, vdevice.transmat->m);
  158.  
  159.     sc_x = WtoVx(res);
  160.     sc_y = WtoVy(res);
  161. }
  162.  
  163.  
  164. /*
  165.  * cmov2
  166.  *
  167.  *    Sets the current character position. Ignores the Z coord.
  168.  *    
  169.  *
  170.  */
  171. void
  172. cmov2(x, y)
  173.     float    x, y;
  174. {
  175.     cmov(x, y, 0.0);
  176. }
  177.  
  178. /*
  179.  * cmovi
  180.  *
  181.  *    Same as cmov but with integer arguments....
  182.  */
  183. void
  184. cmovi(x, y, z)
  185.     Icoord    x, y, z;
  186. {
  187.     cmov((Coord)x, (Coord)y, (Coord)z);
  188. }
  189.  
  190. /*
  191.  * cmovs
  192.  *
  193.  *    Same as cmov but with short integer arguments....
  194.  */
  195. void
  196. cmovs(x, y, z)
  197.     Scoord    x, y, z;
  198. {
  199.     cmov((Coord)x, (Coord)y, (Coord)z);
  200. }
  201.  
  202. /*
  203.  * cmov2i
  204.  *
  205.  *    Same as cmov2 but with integer arguments....
  206.  */
  207. void
  208. cmov2i(x, y)
  209.     Icoord    x, y;
  210. {
  211.     cmov((Coord)x, (Coord)y, 0.0);
  212. }
  213.  
  214. /*
  215.  * cmov2s
  216.  *
  217.  *    Same as cmov but with short integer arguments....
  218.  */
  219. void
  220. cmov2s(x, y)
  221.     Scoord    x, y;
  222. {
  223.     cmov((Coord)x, (Coord)y, 0.0);
  224. }
  225.  
  226.  
  227. /*
  228.  * strwidth
  229.  *
  230.  * Return the length of a string in pixels
  231.  *
  232.  */
  233. long
  234. strwidth(s)
  235.     char    *s;
  236. {
  237. #ifdef SUN_CC
  238.     /*
  239.      * SUN's ANSI CC compiler bitches here sating to use an explicit
  240.      * cast for strlen... it's only a warning, but this fixes it...
  241.          */
  242.     return((long)((size_t)strlen(s) * vdevice.hwidth));
  243. #else
  244.     return((long)(strlen(s) * vdevice.hwidth));
  245. #endif
  246. }
  247.  
  248. /* 
  249.  * getheight
  250.  *
  251.  * Return the maximum Height of the current font
  252.  */
  253. long 
  254. getheight()
  255. {
  256.     if (!vdevice.initialised)
  257.         verror("getheight: vogl not initialized");
  258.  
  259.     return((long)vdevice.hheight);
  260. }
  261.  
  262. #ifdef OLD_GL
  263.     /* This seem to have disappeared from GL 
  264.      * No.. wonder, it's in the C library under SYSV
  265.      */
  266. /*
  267.  * getwidth
  268.  *
  269.  * Return the maximum Width of the current font.
  270.  *
  271.  */
  272. long
  273. getwidth()
  274. {
  275.     if (!vdevice.initialised)
  276.         verror("getwidth: vogl not initialised");
  277.  
  278.  
  279.     return((long)vdevice.hwidth);
  280. }
  281. #endif
  282.  
  283. /*
  284.  * getcpos
  285.  *
  286.  * Return the current character position in screen coords 
  287.  */
  288. void
  289. getcpos(cx, cy)
  290.     Scoord    *cx, *cy;
  291. {
  292.     *cx = sc_x;
  293.     *cy = sc_y;
  294. }
  295.