summaryrefslogtreecommitdiff
path: root/dviware/kane/dvifile_p.h
blob: 4070054b83e02bf01d227c2de611eb79d85ee0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

Date:		20-MAR-1989 17:20:04 GMT
From:		PHYOKANE@CS8700.UCG.IE
To:		abbottp@ASTON.AC.UK
Subject:        file dvifile_p.h
sender: 	hea"phyokane@cs8700.ucg.ie"  <phyokane@cs8700.ucg.ie>

/* -*-C-*- dvifile.h */
/*-->dvifile*/
/**********************************************************************/
/****************************** dvifile *******************************/
/**********************************************************************/

void
dvifile(argc,argv,filestr)
int   argc;			/* argument count */
char* argv[];			/* argument strings */
char* filestr;			/* DVI filename to process */

{

#define PAGENUMBER(p) ((p) < 0 ? (page_count + 1 + (p)) : (p))

    register INT16 i,j,m;	/* loop indices */
    register INT16 swap;	/* swap storage */
    register struct font_entry *p;

    INT16 m_begin,m_end,m_step;	/* loop limits and increment */
    INT16 list[3];		/* index list for sort */
    char tempstr[MAXSTR];	/* temporary string storage */
    int the_char;
    int s;
    int slot;
    int n_slots;
    int fontsize;
    int pfont_size;
    int pfont_n;
    struct font_entry *bigfont;
    struct printer_font *pptr;
    /*
    Establish the default  font file  search order.  This  is done  here
    instead of  in  initglob()  or  option(),  because  both  could  set
    fontlist[].

    By default, the search list contains  names for the PK, GF, and  PXL
    font  files;  that  order   corresponds  to  increasing  size   (and
    therefore, presumably, increasing  processing cost)  of font  raster
    information.  This search  order may  be overridden at  run time  by
    defining an alternate one in the environment variable FONTLIST;  the
    order of  the  strings  "PK",  "GF",  and  "PXL"  in  that  variable
    determines the search  order, and  letter case  is NOT  significant.
    The  substrings  may  be  separated   by  zero  or  more   arbitrary
    characters, so  the  values  "PK-GF-PXL", "PK  GF  PXL",  "PKGFPXL",
    "PK;GF;PXL", "PK/GF/PXL"  are all  acceptable,  and all  choose  the
    default search  order.   This  flexibility  in  separator  character
    choice is occasioned  by the  requirement on some  systems that  the
    environment variable have the syntax of a file name, or be a  single
    "word".  If  any  substring  is  omitted, then  that  font  will  be
    excluded from consideration.  Thus, "GF" permits the use only of  GF
    font files.

    The indexes gf_index, pk_index, and pxl_index are in -1 .. 2, and at
    least one must be non-negative.  A negative index excludes that font
    file type from the search.
    */

    /* Note that non-negative entries in list[] are UNIQUE. */
    list[0] = gf_index = (INT16)strid2(fontlist,"GF");
    list[1] = pk_index = (INT16)strid2(fontlist,"PK");
    list[2] = pxl_index = (INT16)strid2(fontlist,"PXL");

    for (i = 0; i < 3; ++i)	/* put list in non-decreasing order */
	for (j = i+1; j < 3; ++j)
	    if (list[i] > list[j])
	    {
		swap = list[i];
		list[i] = list[j];
		list[j] = swap;
	    }
    for (i = 0; i < 3; ++i)	/* assign indexes 0,1,2 */
	if (list[i] >= 0)
	{
            if (list[i] == gf_index)
		gf_index = i;
            else if (list[i] == pk_index)
		pk_index = i;
            else if (list[i] == pxl_index)
		pxl_index = i;
	}

    if ((gf_index < 0) && (pk_index < 0) && (pxl_index < 0))
	(void)fatal("dvifile():  FONTLIST does not define at least one of \
GF, PK, or PXL fonts");

    (void)dviinit(filestr);	/* initialize DVI file processing */

    (void)devinit(argc,argv);	/* initialize device output */

    (void)readpost();
    (void)FSEEK(dvifp, 14L, 0); /* position past preamble */
    (void)getbytes(dvifp, tempstr,
	(BYTE)nosignex(dvifp,(BYTE)1)); /* flush DVI comment */

    cur_page_number = 0;


#if    (HPLASERJET|HPJETPLUS|GOLDENDAWNGL100|POSTSCRIPT|IMPRESS|CANON_A2)
    /* print pages in reverse order because of laser printer */
    /* page stacking */
    if (backwards)
    {
	m_begin = 1;
	m_end = page_count;
	m_step = 1;
    }
    else	/* normal device order */
    {
	m_begin = page_count;
	m_end = 1;
	m_step = -1;
    }

#else
  /* NOT (HPLASERJET|HPJETPLUS|GOLDENDAWNGL100|POSTSCRIPT|IMPRESS|CANON_A2) */
    /* print pages in forward order for most devices */
    if (backwards)
    {
	m_begin = page_count;
	m_end = 1;
	m_step = -1;
    }
    else
    {
	m_begin = 1;
	m_end = page_count;
	m_step = 1;
    }
#endif /* (HPLASERJET|HPJETPLUS|GOLDENDAWNGL100|POSTSCRIPT|IMPRESS|CANON_A2) */

    for (i = 0; i < npage; ++i)		/* make page numbers positive */
    {					/* and order pairs non-decreasing */
	page_begin[i] = PAGENUMBER(page_begin[i]);
	page_end[i] = PAGENUMBER(page_end[i]);
	if (page_begin[i] > page_end[i])
	{
	    swap = page_begin[i];
	    page_begin[i] = page_end[i];
	    page_end[i] = swap;
	}
    }
/* First pass over pages and setup font download information */

    for (m = m_begin; ; m += m_step)
    {
	for (i = 0; i < npage; ++i)	/* search page list */
	    if ( IN(page_begin[i],m,page_end[i]) &&
	        (((m - page_begin[i]) % page_step[i]) == 0) )
	    {
		if (!quiet)		/* start progress report */
		    (void)fprintf(stderr,"[%d", m);
		cur_page_number++;/* sequential page number 1..N */
		cur_index = m-1;	/* remember index globally */
		checkpage(page_ptr[cur_index]);
		if (!quiet)		/* finish progress report */
		    (void)fprintf(stderr,"] ");
		break;
	    }
	if (m == m_end)			/* exit loop after last page */
	    break;
    }
if (!quiet)		/* finish progress report */
    (void)fprintf(stderr,"\n");

/* Initialize the printer fonts */
for (pf = FIRSTPRINTERFONT; pf <= LASTPRINTERFONT; pf++)
{
	printer_fonts[pf].slots_free = 190;
	for (s = 0; s < 190; s++)
	{
		printer_fonts[pf].char_slots[s].tex_char = -1;
		printer_fonts[pf].char_slots[s].tex_font = NULL;
	}
}
/*	Pack the TeX fonts into printer fonts

	WHILE there are unassigned TeX fonts
		select the largest TeX font
		if possible assign all of the TeX font  
			to the FIRST SMALLEST SPACE free in the printer fonts
		else
			find the FIRST LARGEST SPACE free and assign as much
			of the TeX font as will fit
*/
do {
    /* First....find the largest TeX font left....*/
    fontsize = 0;
    bigfont = NULL;
    fontptr = hfontptr;
    while ((fontptr != (struct font_entry *)NULL))
    {
	if ((fontptr->used_chars - fontptr->assigned_chars) > fontsize)
	{
		fontsize = fontptr->used_chars - fontptr->assigned_chars;
		bigfont = fontptr;
	}
	fontptr = fontptr->next;
    }
    if (bigfont == NULL) break;  /* All TeX fonts assigned */
    fontptr = bigfont;
    fontsize =  (fontsize > 190) ? 190 : fontsize;

    /* Next... find a printer font to put it in.... */
    pfont_size = 1000;
    for (pf = FIRSTPRINTERFONT; pf <= LASTPRINTERFONT; pf++)
    {
	if ((printer_fonts[pf].slots_free >= fontsize) &&
	    (printer_fonts[pf].slots_free < pfont_size))
	{		
		pfont_size = printer_fonts[pf].slots_free;
		pfont_n = pf;
	}
    }
    /* ...if there is no big slot then split this font... */
    /*    putting the first bit into the bigest free slot */
    if (pfont_size > 190)
    {	
	pfont_size = 0;	    
	for (pf = FIRSTPRINTERFONT; pf <= LASTPRINTERFONT; pf++)
	{
		if (printer_fonts[pf].slots_free > pfont_size)
		{		
			pfont_size = printer_fonts[pf].slots_free;
			pfont_n = pf;
		}
	}
    }
    if (pfont_size == 0)
	(void)fatal( "dvifile(): Need more printer fonts");
    pf = pfont_n;
/* Printer font number pf has pfont_size slots available to take characters
   from TeX font fontptr */
    (void)fprintf(stderr,
"[Assigning %d characters from %s (mag = %g) to printer font %d]\n",
	(fontsize <= pfont_size) ? fontsize : pfont_size,
	fontptr->n,(fontptr->font_mag)/1500.0,pf);
    for (the_char = FIRSTPXLCHAR; the_char <= LASTPXLCHAR; the_char++)
    {
	if (printer_fonts[pf].slots_free == 0)
		break;
	if (fontptr->ch[the_char].isloaded == FALSE)	/* Not used	*/
		continue;
	if (fontptr->ch[the_char].lj_font != 65535)/* Already assigned */
		continue;
	slot = 190 - printer_fonts[pf].slots_free--;
	printer_fonts[pf].char_slots[slot].tex_char = the_char;
	printer_fonts[pf].char_slots[slot].tex_font = fontptr;
	fontptr->ch[the_char].lj_char = slot;
	fontptr->ch[the_char].lj_font = pf;
	fontptr->assigned_chars++;
    }
    if ( printer_fonts[pf].slots_free == pfont_size ) /* Nothing happened !!!*/
    	fontptr->assigned_chars = fontptr->used_chars;/* Zero size font ??   */
} while (TRUE);
/* .... and finally download the fonts to the printer.... */
for (pf = FIRSTPRINTERFONT; pf <= LASTPRINTERFONT; pf++)
{
    pptr = &printer_fonts[pf];
    if (pptr->slots_free == 190) continue;
    slot = 0;
    n_slots = 190 - pptr->slots_free;
    (void)fprintf(stderr,"[Loading printer font %d with %d characters]\n",
			pf,n_slots);    
    (void)makefont(pptr,n_slots);
    do {
	fontptr = pptr->char_slots[slot].tex_font;   
	while ( (slot < n_slots) &&
		 (pptr->char_slots[slot].tex_font == fontptr) )
        {
		loadchar(pptr->char_slots[slot].tex_char,MAP_CHAR(slot));
		++slot;
	}
    } while ( slot < n_slots );
}

#if    (HPJETPLUS | POSTSCRIPT | IMPRESS | CANON_A2)
    pf = -1;
#endif


/* Finally pass over pages and print them */
    for (m = m_begin; ; m += m_step)
    {
	for (i = 0; i < npage; ++i)	/* search page list */
	    if ( IN(page_begin[i],m,page_end[i]) &&
	        (((m - page_begin[i]) % page_step[i]) == 0) )
	    {
		if (!quiet)		/* start progress report */
		    (void)fprintf(stderr,"[%d", m);
		cur_page_number++;/* sequential page number 1..N */
		cur_index = m-1;	/* remember index globally */
		prtpage(page_ptr[cur_index]);
		if (!quiet)		/* finish progress report */
		    (void)fprintf(stderr,"] ");
		break;
	    }
	if (m == m_end)			/* exit loop after last page */
	    break;
    }

    (void)devterm();		/* terminate device output */

    (void)dviterm();		/* terminate DVI file processing */
}