Back to index

tetex-bin  3.0
epsf.c
Go to the documentation of this file.
00001 /* 
00002  * epsf.c -- Encapsulated PostScript window server.
00003  * Copyright (C) 1998  Mathias Herberts <herberts@infini.fr>
00004  *
00005  * These functions generate an Encapsulated PostScript File
00006  * representing the graphics normally shown online. They are
00007  * selected by setting MFTERM to epsf.
00008  *
00009  * The name of the file defaults to metafont.eps but can be
00010  * changed by setting the MFEPSF environment variable.
00011  *
00012  * The file is closed when the program exits.
00013  */
00014 
00015 /* 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU General Public License as
00018  * published by the Free Software Foundation; either version 2 of
00019  * the License, or (at your option) any later version.
00020  *
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  * GNU General Public License for more details.
00025  *
00026  * You should have received a copy of the GNU General Public License
00027  * along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00029  */
00030 
00031 
00032 
00033 #define       EXTERN        extern
00034 
00035 #include "../mfd.h"
00036 
00037 #ifdef EPSFWIN
00038 
00039 #include <stdio.h>
00040 
00041 static FILE * psout = NULL;
00042 
00043 /*
00044  * Arrays epsf_{left,right,top,bottom} hold the coordinates of
00045  * open windows.
00046  *
00047  * When a new window is opened, i.e. when blankrectangle is
00048  * called, we check the arrays to see if the blank rectangle
00049  * clears an open window. If it is the case a showpage and an %%EOF is
00050  * appended To the PostScript file and a new header is output thus
00051  * making it easy to separate the pages.
00052  *
00053  * The last page of the file is not terminated by a showpage and
00054  * an %%EOF, the user should add them himself.
00055  *
00056  */
00057 
00058 static screencol epsf_left[16];
00059 static screencol epsf_right[16];
00060 static screenrow epsf_top[16];
00061 static screenrow epsf_bottom[16];
00062 
00063 static short epsf_window = 0;
00064 static unsigned int epsf_page = 1;
00065 
00066 void
00067 mf_epsf_header ()
00068 {
00069   fprintf (psout, "%%!PS-Adobe-3.0 EPSF-3.0\n");
00070   fprintf (psout, "%%%%BoundingBox: -1 -1 %d %d\n", screenwidth, screendepth);
00071   fprintf (psout, "%%%%Creator: METAFONT\n");
00072   fprintf (psout, "%%%%Page: %d %d\n\n", epsf_page, epsf_page);
00073   fprintf (psout, "1 dup scale\n");
00074   fprintf (psout, "1 setlinewidth\n\n");
00075 
00076   epsf_page++;
00077 }
00078 
00079 boolean
00080 mf_epsf_initscreen P1H(void)
00081 {
00082   if (getenv ("MFEPSF") != (char *) NULL)
00083     {
00084       psout = fopen ((char *) getenv ("MFEPSF"), "w");
00085     }
00086   else
00087     {
00088       psout = fopen ("metafont.eps", "w");
00089     }
00090    
00091    if (psout == (FILE *) NULL)
00092     {
00093       return 0;
00094     }
00095   else
00096     {
00097       mf_epsf_header ();
00098       epsf_window = 0;
00099       return 1;
00100     }
00101 }
00102 
00103 void
00104 mf_epsf_updatescreen P1H(void)
00105 {
00106   fflush (psout);
00107 }
00108 
00109 void
00110 mf_epsf_blankrectangle P4C(screencol, left,
00111                        screencol, right,
00112                        screenrow, top,
00113                        screenrow, bottom)
00114 {
00115   int i;
00116 
00117   for (i = 0; i < epsf_window; i++)
00118     {
00119       if (! ((right - 1 < epsf_left[i]) || (epsf_right[i] < left)) ) /* new window is neither left nor right of window i */
00120        if ( ! ((top > epsf_bottom[i]) || (epsf_top[i] > bottom - 1))) /* new window is neither below nor above window i */
00121          {        
00122            fprintf (psout, "\nshowpage\n%%%%EOF\n");
00123            mf_epsf_header ();
00124            epsf_window = 0;
00125            break;
00126          }
00127     }
00128   
00129   epsf_left[epsf_window] = left;
00130   epsf_right[epsf_window] = right - 1;
00131   epsf_top[epsf_window] = top;
00132   epsf_bottom[epsf_window] = bottom - 1;
00133 
00134   epsf_window++;
00135 
00136   fprintf (psout, "1 setgray %d %d %d %d rectfill 0 setgray\n", left, screendepth - 1 - bottom, right - left, bottom - top);
00137 
00138   fflush (psout);
00139 }
00140 
00141 
00142 void
00143 mf_epsf_paintrow P4C(screenrow, row,
00144                   pixelcolor, init_color,
00145                   transspec, transition_vector,
00146                   screencol, vector_size)
00147 {
00148   int col;
00149   int color;
00150 
00151   color = (init_color == 0) ? 1 : 0;
00152 
00153   do
00154     {
00155       col = *transition_vector++;
00156 
00157       if (!color)
00158        {
00159          fprintf (psout, "newpath %d %d moveto %d %d lineto stroke\n", 
00160                  col, 
00161                  screendepth - 1 - row,
00162                  *transition_vector,
00163                  screendepth - 1 - row);
00164        }
00165 
00166       color = (color == 0) ? 1 : 0;
00167     }
00168   while (--vector_size > 0);
00169 }
00170 
00171 
00172 #else /* !EPSFWIN */
00173 
00174 int epsf_dummy;
00175 
00176 #endif /* !EPSFWIN */