home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / FGDEMO40.ZIP / SOURCE.COM / CHAR.C < prev    next >
Text File  |  1995-02-12  |  6KB  |  216 lines

  1. /**********************************************************************\
  2. *                                                                      *
  3. *  char.c -- FGDEMO character display and management routines          *
  4. *                                                                      *
  5. \**********************************************************************/
  6.  
  7. #include "defs.h"
  8.  
  9. char *pfont;
  10.  
  11. int spacing[] = {
  12. /* !  "  #  $  %  &  '  (  )  *  +  ,  - */
  13.    4, 7, 9, 8,13, 9, 4, 5, 5, 8, 8, 4, 6,
  14.  
  15. /* .  /  0  1  2  3  4  5  6  7  8  9  : */
  16.    4, 5, 8, 6, 8, 8, 8, 8, 8, 8, 8, 8, 4,
  17.  
  18. /* ;  <  =  >  ?  @  A  B  C  D  E  F  G */
  19.    4, 8, 8, 8, 8,13,10,10, 9,10, 9, 9,10,
  20.  
  21. /* H  I  J  K  L  M  N  O  P  Q  R  S  T */
  22.   10, 4, 7, 9, 9,12,10,10,10,10,11, 9,10,
  23.  
  24. /* U  V  W  X  Y  Z  [  \  ]  ^  _  `  a */
  25.   10,10,16,12,12,11, 5, 6, 5, 7, 9, 6, 8,
  26.  
  27. /* b  c  d  e  f  g  h  i  j  k  l  m  n */
  28.    8, 8, 8, 8, 5, 8, 8, 4, 4, 8, 4,12, 8,
  29.  
  30. /* o  p  q  r  s  t  u  v  w  x  y  z  { */
  31.    8, 8, 8, 7, 8, 5, 8,10,12,10,10, 8, 6,
  32.  
  33. /* |  }  ~ */
  34.    5, 6,12};
  35.  
  36.  
  37. /**********************************************************************\
  38. *                                                                      *
  39. *  center_pstring -- display proportional characters, centered         *
  40. *                                                                      *
  41. \**********************************************************************/
  42.  
  43. void center_pstring(char *string,int x1,int x2,int y)
  44. {
  45.    register int x;
  46.  
  47.    x = get_center(string,x1,x2);
  48.    put_pstring(string,x,y);
  49. }
  50.  
  51. /**********************************************************************\
  52. *                                                                      *
  53. *  first_nonblank -- find first character that is not a blank          *
  54. *                                                                      *
  55. \**********************************************************************/
  56.  
  57. char first_nonblank(char *string)
  58. {
  59.    register int i;
  60.    char c;
  61.  
  62.    i = 0;
  63.    for(;;)
  64.    {
  65.       c = string[i++];
  66.       if (c == 0)
  67.          return(0);
  68.       else if (c > 32)
  69.          return(c);
  70.    }
  71. }
  72.  
  73. /**********************************************************************\
  74. *                                                                      *
  75. *  get_center -- locate center for proportionally-spaced characters    *
  76. *                                                                      *
  77. \**********************************************************************/
  78.  
  79. get_center(char *string,int x1,int x2)
  80. {
  81.    return(((x1 + x2) / 2) - (length_pstring(string)/ 2));
  82. }
  83.  
  84. /**********************************************************************\
  85. *                                                                      *
  86. *  get_font -- retrieve the 14pt bitmapped font character definitions  *
  87. *                                                                      *
  88. \**********************************************************************/
  89.  
  90. void get_font()
  91. {
  92.    register int i;
  93.    register int index;
  94.    int x, y;
  95.    int yoffset;
  96.  
  97.    /* allocate dynamic memory for the fonts */
  98.  
  99.    pfont = malloc(2632);
  100.    yoffset = 18;
  101.  
  102.    /* if the allocate failed or the font file isn't present, go no farther */
  103.  
  104.    if (pfont == (char *)NULL)
  105.       abort_program("Not enough memory to load font.\n");
  106.    if (!exists("FONT14.PCX"))
  107.       abort_program("File missing: FONT14.PCX.\n");
  108.  
  109.    /* display the 14pt font on the hidden page */
  110.  
  111.    fg_setpage(hidden);
  112.    fg_showpcx("FONT14.PCX",1);
  113.  
  114.    /* retrieve the font */
  115.  
  116.    fg_setcolor(11);
  117.    index = 0;
  118.  
  119.    for (i = 0; i < 94; i++)
  120.    {
  121.       x = 1 + (i/13) * 16;
  122.       y = yoffset + (i%13) * (PTSIZE+1);
  123.       fg_move(x,y);
  124.       fg_getmap(&pfont[index*2],2,PTSIZE);  /* put in bitmap array */
  125.       index += PTSIZE;
  126.    }
  127. }
  128.  
  129. /**********************************************************************\
  130. *                                                                      *
  131. *  length_pstring -- compute the length of a proportional string       *
  132. *                                                                      *
  133. \**********************************************************************/
  134.  
  135. length_pstring(char *string)
  136. {
  137.    register int i;
  138.    register int nchar;
  139.    int length;
  140.    char ch;
  141.  
  142.    /* number of characters in the string */
  143.  
  144.    nchar = strlen(string);
  145.    if (nchar == 0) return(0);
  146.  
  147.    length = 0;
  148.  
  149.    /* total up pixel width of each character */
  150.  
  151.    for (i = 0; i < nchar; i++)
  152.    {
  153.       ch = string[i] - (char)33;
  154.       if (ch < 0)
  155.          length += 8;
  156.       else
  157.          length += spacing[ch];
  158.    }
  159.    return(length);
  160. }
  161.  
  162. /**********************************************************************\
  163. *                                                                      *
  164. *  put_pstring -- display proportionally-spaced characters             *
  165. *                                                                      *
  166. \**********************************************************************/
  167.  
  168. void put_pstring(char *string,int x,int y)
  169. {
  170.    register int i;
  171.    register int nchar;
  172.    int index;
  173.    char ch;
  174.  
  175.    /* get the length of the string */
  176.  
  177.    nchar = strlen(string);
  178.    if (nchar == 0) return;
  179.  
  180.    /* display each character as a mode-independent bitmap */
  181.  
  182.    for (i = 0; i < nchar; i++)
  183.    {
  184.       ch = string[i] - (char)33;
  185.  
  186.       /* printable character: greater than ASCII 32 */
  187.  
  188.       if (ch >= 0)
  189.       {
  190.          index = ch * (PTSIZE * 2);
  191.          fg_move(x,y);
  192.          fg_drawmap(&pfont[index],2,PTSIZE);
  193.  
  194.          /* increment x depending on the proportional space */
  195.  
  196.          x += spacing[ch];
  197.       }
  198.  
  199.       /* assume that white space will be 8 pixels wide */
  200.  
  201.       else
  202.          x += 8;
  203.    }
  204. }
  205.  
  206. /**********************************************************************\
  207. *                                                                      *
  208. *  row_offset -- return the row offset in pixels for the font size     *
  209. *                                                                      *
  210. \**********************************************************************/
  211.  
  212. row_offset(int nrows)
  213. {
  214.    return(nrows*PTSIZE);
  215. }
  216.