home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / TO_MF.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  13KB  |  394 lines

  1. /*
  2.    Copyright (c) 1991 - 1993 Heinz W. Werntges.  All rights reserved.
  3.    Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /** to_mf.c:    Converter to Metafont and misc. TeX formats
  25.  **
  26.  ** 91/01/19  V 1.00  HWW  Derived from HPtoGF.c
  27.  ** 91/02/10  V 1.01  HWW  "zaehler" removed
  28.  ** 91/02/15  V 1.02  HWW  stdlib.h supported
  29.  ** 91/02/18  V 1.03  HWW  some int's changed to float's
  30.  ** 91/06/09  V 1.04  HWW  New options added; some simplifications done
  31.  ** 91/10/15  V 1.05  HWW  ANSI_C
  32.  ** 91/11/20  V 1.06  HWW  Many changes for "SPn;" support
  33.  ** 92/01/13  V 1.06a HWW  debugged
  34.  ** 92/02/27  V 1.07b HWW  TeX modes added (epic, emTeX specials)
  35.  ** 92/05/17  V 1.07c HWW  Output to stdout if outfile == '-'
  36.  ** 92/05/19  V 1.07d HWW  Warning if color mode
  37.  ** 92/12/10  V 1.08a HWW  CAD (TeXcad) mode added
  38.  ** 92/12/12  V 1.08b HWW  Info line now interprets outfile=='-' as "stdout"
  39.  ** 93/04/12  V 1.08c HWW  Fix for J. Post's report re: \emline
  40.  **/
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44.  
  45. #ifdef ATARI
  46. #include <string.h>
  47. #include <ctype.h>
  48. #endif
  49.  
  50. #include "bresnham.h"
  51. #include "hp2xx.h"
  52.  
  53.  
  54.  
  55. extern  float   xmin, xmax, ymin, ymax;
  56.  
  57.  
  58.  
  59. void    to_mftex (PAR *p, FILE *td, int mode)
  60. {
  61.  
  62. PlotCmd         cmd;
  63. HPGL_Pt         pt1;
  64. float           coord2mm;
  65. FILE            *md;
  66. int             pensize, chars_out = 0, max_chars_out = 210, np = 1;
  67. char            *ftype="", *scale_cmd="", *pen_cmd="",
  68.                 *poly_start="", *poly_next="", *poly_last="", *poly_end="",
  69.                 *draw_dot="", *exit_cmd="";
  70. #ifdef ATARI
  71. int             i;
  72. FILE            *csfile;
  73. char            *csname, *pos1, *pos2, *special_cmd, *tempch;
  74. HPGL_Pt         old_pt;
  75. #endif
  76.  
  77.  
  78.   switch (mode)
  79.   {
  80.     case 0:     /* Metafont mode        */
  81.         ftype           = "METAFONT";
  82.         scale_cmd       = "mode_setup;\nbeginchar(\"Z\",%4.3fmm#,%4.3fmm#,0);\n";
  83.         pen_cmd         = "pickup pencircle scaled 0.%1dmm;\n";
  84.         poly_start      = "draw(%4.3fmm,%4.3fmm)";
  85.         poly_next       = "--(%4.3fmm,%4.3fmm)";
  86.         poly_last       = "--(%4.3fmm,%4.3fmm);\n";
  87.         poly_end        = ";\n";
  88.         draw_dot        = "drawdot(%4.3fmm,%4.3fmm);\n";
  89.         exit_cmd        = "endchar;\nend;\n";
  90.         break;
  91.     case 1:     /* TeX (em-Specials) mode       */
  92.         ftype           = "emTeX-specials";
  93.         scale_cmd       = "\\unitlength1mm\n\\begin{picture}(%4.3f,%4.3f)\n";
  94.         pen_cmd         = "\\special{em:linewidth 0.%1dmm}\n";
  95.         poly_start      = "\\put(%4.3f,%4.3f){\\special{em:moveto}}\n";
  96.         poly_next       = "\\put(%4.3f,%4.3f){\\special{em:lineto}}\n";
  97.         poly_last       = poly_next;
  98.         poly_end        = "";
  99.         draw_dot        = "\\put(%4.3f,%4.3f){\\makebox(0,0)[cc]{.}}\n";
  100.         exit_cmd        = "\\end{picture}\n";
  101.         break;
  102.     case 2:     /* TeX (epic) mode      */
  103.         ftype           = "TeX (epic)";
  104.         scale_cmd       = "\\unitlength1mm\n\\begin{picture}(%4.3f,%4.3f)\n";
  105.         pen_cmd         = "\\linethickness{0.%1dmm}\n";
  106.         poly_start      = "\\drawline(%4.3f,%4.3f)";
  107.         poly_next       = "(%4.3f,%4.3f)";
  108.         poly_last       = "(%4.3f,%4.3f)\n";
  109.         poly_end        = "\n";
  110.         draw_dot        = "\\put(%4.3f,%4.3f){\\picsquare}\n";
  111.         exit_cmd        = "\\end{picture}\n";
  112.         break;
  113.     case 3:     /* TeXcad (\emline-Macros) mode */
  114.         ftype           = "TeXcad compatible";
  115.         scale_cmd       = "\\unitlength=1mm\n\\begin{picture}(%4.3f,%4.3f)\n";
  116.         pen_cmd         = "\\special{em:linewidth 0.%1dmm}\n\\linethickness{ 0.%1dmm}\n";
  117.         poly_start      = "\\emline{%4.3f}{%4.3f}{%d}";
  118.         poly_next       = "{%4.3f}{%4.3f}{%d}%%\n";
  119.         /* %% = Fix for John Post's bug report    */
  120.         poly_last       = poly_next;
  121.         poly_end        = "";
  122.         draw_dot        = "\\put(%4.3f,%4.3f){\\makebox(0,0)[cc]{.}}\n";
  123.         exit_cmd        = "\\end{picture}\n";
  124.         break;
  125. #ifdef ATARI
  126.     case 4:      /* CS-Graphics specials for ATARI TeX */
  127.         ftype           = "CS-TeX specials";
  128.         scale_cmd       = "\\unitlength1mm\n\\begin{draw}{%4.3f}{%4.3f}{%s}\n";
  129.         special_cmd     = "\\put(0,0){\\special{CS!i %s}}\n";
  130.         pen_cmd         = "w 0.%1dmm\n";
  131.         poly_start      = "%4.3f %4.3f l ";
  132.         poly_next       = "%4.3f %4.3f\n";
  133.         poly_last       = poly_next;
  134.         poly_end        = "";
  135.         draw_dot        = "\\put(%4.3f,%4.3f){\\makebox(0,0)[cc]{.}}\n";
  136.         exit_cmd        = "\\end{draw}\n";
  137.         break;
  138. #endif
  139.   }
  140.  
  141. #ifdef ATARI
  142.   if (mode == 4)
  143.   {
  144.         csname = (char *)calloc(strlen(p->outfile)+5, sizeof(char));
  145.         tempch = (char *)calloc(strlen(p->outfile)+5, sizeof(char));
  146.         strcpy(csname, p->outfile);
  147.  
  148.         for (i=0; i<strlen(csname); i++)
  149.                 csname[i] = (isupper(csname[i]) ? tolower(csname[i]) : csname[i]);
  150.  
  151.         strcpy(tempch, csname);
  152.   }
  153. #endif
  154.  
  155.  
  156.   if (*p->outfile != '-')
  157.   {
  158.  
  159. #ifdef ATARI
  160.         if (mode == 4)
  161.         {
  162.                 pos1 = strrchr(csname, 46);
  163.                 pos2 = strrchr(csname, 92);
  164.  
  165.                 if (pos1 != NULL && pos1 > pos2)
  166.                         pos1[0] = 0;
  167.  
  168.                 strcat(csname, ".tex\0");
  169.  
  170.                 if (!strcmp(csname, tempch))
  171.                 {
  172.                         pos1 = strrchr(tempch, 46); *pos1 = 0;
  173.                         strcat(tempch, ".csg\0");
  174.                 }
  175.  
  176.  
  177.                 if ((csfile = fopen(csname, "a")) == NULL ||
  178.                     (md     = fopen(tempch, "w")) == NULL)
  179.                 {
  180.                         free(csname); free(tempch);
  181.                         perror("hp2xx mf/tex");
  182.                         exit(ERROR);
  183.                 }
  184.  
  185.         }
  186.         else
  187.         {
  188.                 csfile = stdout;
  189.  
  190. #endif
  191.                 if ((md = fopen(p->outfile, "w")) == NULL)
  192.                 {
  193.                         perror("hp2xx (mf/tex)");
  194.                         exit(ERROR);
  195.                 }
  196. #ifdef ATARI
  197.         }
  198. #endif
  199.  
  200.   }
  201.   else
  202.   {
  203.         md = stdout;
  204. #ifdef ATARI
  205.         csfile = stdout;
  206. #endif
  207.   }
  208.  
  209. #ifdef ATARI
  210.   if (!p->quiet)
  211.         if (mode == 4)
  212.                 fprintf(stderr,"\n\n- Writing %s code to \"%s\"\n", ftype,
  213.                         *p->outfile == '-' ? "stdout" : tempch);
  214.         else
  215.                 fprintf(stderr,"\n\n- Writing %s code to \"%s\"\n", ftype,
  216.                         *p->outfile == '-' ? "stdout" : p->outfile);
  217. #else
  218.   if (!p->quiet)
  219.         fprintf(stderr,"\n\n- Writing %s code to \"%s\"\n", ftype,
  220.                 *p->outfile == '-' ? "stdout" : p->outfile);
  221. #endif
  222.  
  223.   if (p->is_color)
  224.         fprintf(stderr, "\nWARNING: MF/TeX modes ignore colors!\n");
  225.  
  226. #ifdef ATARI
  227.   if (mode == 4)
  228.   {
  229.         if (!p->quiet)
  230.           fprintf(stderr, "- TEX-Input file is \"%s\"\n", csname);
  231.  
  232.         pos1 = strchr(tempch, 92);
  233.         while (pos1 != NULL)
  234.         {
  235.                 *pos1 = 47; pos1 = strchr(tempch, 92);
  236.         }
  237.  
  238.         fprintf(csfile,"%% %s code in %s, created by hp2xx\n",
  239.                 ftype, tempch);
  240.         fprintf(csfile, scale_cmd, p->width, p->height, tempch);
  241.         fprintf(csfile, special_cmd, tempch);
  242.         fprintf(md, "CS-Graphics V 1\nr\nu 1mm\n");
  243.         free(csname); free(tempch);
  244.   }
  245.   else
  246.   {
  247. #endif
  248.         fprintf(md,"%% %s code in %s, created by hp2xx\n",
  249.                 ftype, p->outfile);
  250.         fprintf(md, scale_cmd, p->width, p->height);
  251. #ifdef ATARI
  252.   }
  253. #endif
  254.  
  255.   pensize = p->pensize[p->pen];
  256.   if (pensize != 0)
  257.         if (mode == 3)
  258.                 fprintf(md, pen_cmd, pensize, pensize);
  259.         else
  260.                 fprintf(md, pen_cmd, pensize);
  261.  
  262.   /* Factor transforming the coordinate values into millimeters: */
  263.   coord2mm = p->height / (ymax-ymin);
  264.  
  265.  
  266.   while ((cmd = PlotCmd_from_tmpfile()) != EOF)
  267.         switch (cmd)
  268.         {
  269.           case NOP:
  270.                 break;
  271.  
  272.           case SET_PEN:
  273.                 if ((p->pen = fgetc(td)) == EOF)
  274.                 {
  275.                         perror("Unexpected end of temp. file: ");
  276.                         exit (ERROR);
  277.                 }
  278.                 pensize = p->pensize[p->pen];
  279.                 if (pensize != 0)
  280.                 {
  281.                         if (chars_out)  /* Finish up old polygon */
  282.                         {
  283.                                 fprintf(md, poly_end);
  284.                                 chars_out = 0;
  285.                         }
  286.                         if (mode == 3)
  287.                                 fprintf(md, pen_cmd, pensize, pensize);
  288.                         else
  289.                                 fprintf(md, pen_cmd, pensize);
  290.                 }
  291.                 break;
  292.  
  293.           case MOVE_TO:
  294.                 HPGL_Pt_from_tmpfile (&pt1);
  295.                 if (pensize == 0 || mode == 3 || mode == 4)
  296.                         break;
  297.                 if (chars_out)          /* Finish up old polygon */
  298.                         fprintf(md, poly_end);
  299.                 chars_out =  fprintf(md, poly_start,
  300.                         (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
  301.                 break;
  302.  
  303.           case DRAW_TO:
  304.                 if (mode == 3)  /* Needs special treatment: no polygons!        */
  305.                 {
  306.                         chars_out =  fprintf(md, poly_start,
  307.                           (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm, np++);
  308.                         HPGL_Pt_from_tmpfile (&pt1);
  309.                         chars_out += fprintf(md, poly_next,
  310.                           (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm, np++);
  311.                         break;
  312.                 }
  313. #ifdef ATARI
  314.                 else if (mode == 4)
  315.                 {
  316.                         old_pt = pt1;
  317.                         chars_out = fprintf(md, poly_start,
  318.                           (old_pt.x-xmin) * coord2mm, (old_pt.y-ymin) * coord2mm, np++);
  319.                         HPGL_Pt_from_tmpfile (&pt1);
  320.                         chars_out += fprintf(md, poly_next,
  321.                           (pt1.x-old_pt.x) * coord2mm, (pt1.y-old_pt.y) * coord2mm, np++);
  322.                         old_pt = pt1;
  323.                         break;
  324.                 }
  325. #endif
  326.  
  327.                 HPGL_Pt_from_tmpfile (&pt1);
  328.                 if (pensize == 0)
  329.                         break;
  330.                 if (chars_out > max_chars_out)
  331.                                         /* prevent overlong lines */
  332.                 {
  333.                         fprintf(md, poly_last,
  334.                           (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
  335.                         chars_out =  fprintf(md, poly_start,
  336.                           (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
  337.                 } else
  338.                         chars_out += fprintf(md, poly_next,
  339.                           (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
  340.                 break;
  341.  
  342.           case PLOT_AT:
  343.                 HPGL_Pt_from_tmpfile (&pt1);
  344.                 if (chars_out)          /* Finish up old polygon */
  345.                 {
  346.                         fprintf(md, poly_end);
  347.                         chars_out = 0;
  348.                 }
  349.                 if (pensize == 0)
  350.                         break;
  351.  
  352. #ifdef ATARI
  353.                 if (mode == 4)
  354.                         fprintf(csfile, draw_dot,
  355.                                 (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
  356.  
  357.                 else
  358. #endif
  359.                         fprintf(md, draw_dot,
  360.                                 (pt1.x-xmin) * coord2mm, (pt1.y-ymin) * coord2mm);
  361.                 break;
  362.  
  363.           default:
  364.                 fprintf(stderr,"Illegal cmd in temp. file!");
  365.                 exit (ERROR);
  366.         }
  367.  
  368.  
  369.   if (chars_out)                        /* Finish up old polygon */
  370.   {
  371.         fprintf(md, poly_end);
  372.         chars_out = 0;
  373.   }
  374.  
  375. #ifdef ATARI
  376.   if (mode == 4)
  377.   {
  378.     fprintf(csfile, exit_cmd);
  379.  
  380.     if (csfile != stdout)
  381.         fclose(csfile);        
  382.   }
  383.   else
  384. #endif
  385.     fprintf(md, exit_cmd);                /* Add file trailer     */
  386.  
  387.   if (md != stdout)
  388.         fclose(md);
  389.  
  390.   if (!p->quiet)
  391.         fputc ('\n', stderr);
  392.  
  393. }
  394.