home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / utils / texi2ps / ps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-08  |  4.3 KB  |  230 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* texi2ps -- convert texinfo format files into Postscript files.
  3.  
  4.    Copyright (C) 1995 DJ Delorie (dj@delorie.com)
  5.  
  6.    texi2ps is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  8.    responsibility to anyone for the consequences of using it or for
  9.    whether it serves any particular purpose or works at all, unless he
  10.    says so in writing.  Refer to the GNU General Public License
  11.    for full details.
  12.  
  13.    Everyone is granted permission to copy, modify and redistribute
  14.    texi2ps, but only under the conditions described in the GNU
  15.    General Public License.   A copy of this license is supposed to
  16.    have been given to you along with texi2ps so you can know your
  17.    rights and responsibilities.  It should be in a file named COPYING.
  18.    Among other things, the copyright notice and this notice must be
  19.    preserved on all copies.  */
  20.  
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #include "ps.h"
  27. #include "word.h"
  28.  
  29. #define COURIER_ALL_SAME 1
  30.  
  31. #include "cou.h"
  32. #if !COURIER_ALL_SAME
  33. #include "coub.h"
  34. #include "coui.h"
  35. #include "coubi.h"
  36. #endif
  37. #include "ding.h"
  38. #include "hlv.h"
  39. #include "hlvb.h"
  40. #include "hlvi.h"
  41. #include "hlvbi.h"
  42. #include "sym.h"
  43. #include "tms.h"
  44. #include "tmsb.h"
  45. #include "tmsi.h"
  46. #include "tmsbi.h"
  47.  
  48. static float *all_metrics[] = {
  49.   fm_tms, fm_tmsb, fm_tmsi, fm_tmsbi,
  50. #if COURIER_ALL_SAME
  51.   fm_cou, fm_cou, fm_cou, fm_cou,
  52. #else
  53.   fm_cou, fm_coub, fm_coui, fm_coubi,
  54. #endif
  55.   fm_sym, fm_sym, fm_sym, fm_sym,
  56.   fm_hlv, fm_hlvb, fm_hlvi, fm_hlvbi,
  57.   fm_ding, fm_ding, fm_ding, fm_ding
  58. };
  59.  
  60. float *font_metrics=fm_tms;
  61. static int ps_col = 0;
  62.  
  63. void
  64. psprintf(char *fmt, ...)
  65. {
  66.   char tmp[1000];
  67.   int need;
  68.   va_list args = 0;
  69.   va_start(args, fmt);
  70.   vsprintf(tmp, fmt, args);
  71.   va_end(args);
  72.   need = strlen(tmp)+1;
  73.   if (ps_col+need > 78)
  74.   {
  75.     putchar('\n');
  76.     ps_col = 0;
  77.   }
  78.   else if (ps_col)
  79.     putchar(' ');
  80.   fputs(tmp, stdout);
  81.   ps_col += need;
  82. }
  83.  
  84. void
  85. pscomment(char *fmt, ...)
  86. {
  87.   char tmp[1000];
  88.   int need;
  89.  
  90.   if (ps_col)
  91.     putchar('\n');
  92.   if (fmt)
  93.   {
  94.     va_list args = 0;
  95.     va_start(args, fmt);
  96.     vsprintf(tmp, fmt, args);
  97.     fputs("% ", stdout);
  98.     fputs(tmp, stdout);
  99.     va_end(args);
  100.   }
  101.   putchar('\n');
  102.   ps_col = 0;
  103. }
  104.  
  105. static const char PS_ESC[] = "()\\";
  106.  
  107. void
  108. psputw(char *w)
  109. {
  110.   char *wp;
  111.   int need = strlen(w) + 2;
  112.   for (wp=w; *wp; wp++)
  113.     if (strchr(PS_ESC, *wp))
  114.       need++;
  115.  
  116.   if (ps_col+need > 78)
  117.   {
  118.     putchar('\n');
  119.     ps_col = 0;
  120.   }
  121.   else if (ps_col)
  122.     putchar(' ');
  123.  
  124.   putchar('(');
  125.   while (*w)
  126.   {
  127.     if (strchr(PS_ESC, *w))
  128.       putchar('\\');
  129.     putchar(*w++);
  130.   }
  131.   putchar(')');
  132.   ps_col += need;
  133. }
  134.  
  135. typedef struct PS_FSTACK {
  136.   struct PS_FSTACK *prev;
  137.   int ps_font;
  138.   int scale;
  139. } PS_FSTACK;
  140.  
  141. static PS_FSTACK *ps_fstack = 0;
  142. int ps_font=0;
  143. int ps_fontsize=10;
  144.  
  145. static int old_ps_font = 0;
  146. static int old_ps_fontsize = 0;
  147.  
  148. static void
  149. do_font()
  150. {
  151.   if (ps_font == old_ps_font && ps_fontsize == old_ps_fontsize)
  152.     return;
  153.   old_ps_font = ps_font;
  154.   old_ps_fontsize = ps_fontsize;
  155. /*  psprintf("/%s findfont %d scalefont setfont", font_names[ps_font], ps_fontsize); */
  156.   font_metrics = all_metrics[ps_font];
  157. }
  158.  
  159. void
  160. psf_setfont()
  161. {
  162.   old_ps_fontsize = -1;
  163.   do_font();
  164. }
  165.  
  166. static void
  167. push()
  168. {
  169.   PS_FSTACK *f = (PS_FSTACK *)malloc(sizeof(PS_FSTACK));
  170.   f->prev = ps_fstack;
  171.   ps_fstack = f;
  172.   f->ps_font = ps_font;
  173.   f->scale = ps_fontsize;
  174. }
  175.  
  176. void
  177. psf_pushset(int fl)
  178. {
  179.   push();
  180.   ps_font |= fl;
  181.   do_font();
  182. }
  183.  
  184. void
  185. psf_pushfont(int fl)
  186. {
  187.   push();
  188.   ps_font &= ~(PSF_bold|PSF_italic);
  189.   ps_font |= fl;
  190.   do_font();
  191. }
  192.  
  193. void
  194. psf_pushreset(int fl)
  195. {
  196.   push();
  197.   ps_font &= ~fl;
  198.   do_font();
  199. }
  200.  
  201. void
  202. psf_pushscale(int sc)
  203. {
  204.   push();
  205.   ps_fontsize = sc;
  206.   do_font();
  207. }
  208.  
  209. void
  210. psf_pop()
  211. {
  212.   PS_FSTACK *f;
  213.   if (!ps_fstack)
  214.     return;
  215.   ps_font = ps_fstack->ps_font;
  216.   ps_fontsize = ps_fstack->scale;
  217.   f = ps_fstack;
  218.   ps_fstack = ps_fstack->prev;
  219.   free(f);
  220.   do_font();
  221. }
  222.  
  223. void
  224. psdone()
  225. {
  226.   psprintf("%s %d\n%c",
  227.            "restore\n%%Trailer\n%%Pages:", /* DSC Trailer */
  228.            --current_page, '\004');        /* check_eop() incremented page */
  229. }
  230.