summaryrefslogtreecommitdiff
path: root/dviware/kane/dvijep_p
blob: da0d36a1fc34223e5a08ec5f4e031d1ee1759463 (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189

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

/*-->dvipp8*/
/**********************************************************************/
/******************************* dvipp8 *******************************/
/**********************************************************************/

#include "dvihead.h"

/***********************************************************************
***********************************************************************/

/**********************************************************************/
/************************  Device Definitions  ************************/
/**********************************************************************/

/* All output-device-specific definitions go here.  This section must
be changed when modifying a dvi driver for use on a new device */

#undef HPJETPLUS
#define  HPJETPLUS   1			/* conditional compilation flag */

#define VERSION_NO	"2.10"		/* DVI driver version number */

#define  DEVICE_ID	"Hewlett Packard Laserjet+ laser printer"
				/* this string is printed at runtime */

#define OUTFILE_EXT	"jep"

#define  BYTE_SIZE	  8		/* output file byte size */

#undef STDRES
#define STDRES  1			/* to get standard font resolution */

#define  MAXLINE	4096		/* maximum input file line size */
					/* it is needed only in special() */

#define  MAXPAGEFONTS	16		/* silly limit on fonts/page */

#define  FIRSTPRINTERFONT 16		/* Lowest font number used */
#define  LASTPRINTERFONT  31		/* Highest font number used */

#define  XDPI		300		/* horizontal dots/inch */
#define  XPSIZE		9		/* horizontal paper size in inches */
#define  XSIZE		(((XDPI*XPSIZE+2*HOST_WORD_SIZE-1)/\
				(2*HOST_WORD_SIZE))*(2*HOST_WORD_SIZE))
					/* number of horizontal dots; */
					/* MUST BE multiple of */
					/* 2*HOST_WORD_SIZE */

#define  YDPI		300		/* vertical dots/inch */
#define  YPSIZE		11		/* vertical paper size in inches */
#define  YSIZE		4096		/* number of vertical dots */

#define XORIGIN		60		/* measured pixel coordinates of */
#define YORIGIN		15		/* page origin (should be at (0,0) */

/***********************************************************************
Define some  useful  shorthand macros  for  all required  LaserJet  Plus
commands.  With the exception of the CREATEFONT and DOWNLOADCHAR macros,
which require extra data,  NO escape sequences  appear anywhere else  in
the text.

The LaserJet Plus allows coordinates in  dots as well as the  decipoints
supported by the regular LaserJet.  Dot coordinates are more convenient,
so we use them exclusively.  The Plus puts the page origin in the upper-
left corner, while we have it in the lower-left corner, so MOVEY adjusts
the coordinate accordingly.

With 7-bit fonts, only characters 33..127 are printable, and with 8-bit fonts,
33..127 and 160..255 are printable.  Since TeX assumes characters 0..255 are
available, but 0..127 are used most often, we remap character numbers in 0..32
to 191..223, and 128..159 to 224..255, and use 8-bit fonts.  The macros
conceal the remapping.  This means that 191..255 are not available for 
downloading to: these characters are handles by raster graphics.

Whenever possible,  escape  sequences  are  combined  and  0  parameters
omitted, in order to compress the output.
***********************************************************************/

/* Begin raster graphics .. {transfer raster graphics} .. end raster graphics */
#define BEGIN_RASTER_GRAPHICS	OUTS("\033*t300R\033*r1A")

/* CREATE_FONT must be followed by a 26-byte font header (see readfont) */
#define CREATE_FONT OUTS("\033\051s26W")

#define DELETE_ALL_FONTS OUTS("\033*c1F")

#define SELECT_ROMAN8 

/* DOWNLOAD_CHARACTER is not a complete specification---it must be followed by
two-byte values of xoff, yoff, width, height, deltax */
#define DOWNLOAD_CHARACTER(nbytes) OUTF("\033\050s%dW",(nbytes)+16)

/* End raster graphics */
#define END_RASTER_GRAPHICS	OUTS("\033*rB")

/* Map characters  */
#define MAP_CHAR(c)\
  (((c) < 33) ? ((c) + 191) :\
  (((c) < 128) ? (c) :\
  (((c) < 160) ? ((c) + 96) : (c))))

#define BAD_CHAR(c) ((c) > 255)

/* Move absolute to (x,y), page origin in lower left corner */
#define MOVETO(x,y) OUTF2("\033*p%dx%dY",(x),YSIZE-(y))

/* Move absolute to (x,current y), page origin in lower left corner */
#define MOVEX(x) OUTF("\033*p%dX",(x))

/* Move absolute to (current x,y), page origin in lower left corner */
#define MOVEY(y) OUTF("\033*p%dY",YSIZE-(y))

/* Output TeX character number with mapping to HP LaserJet Plus number */
#define OUT_CHAR(c)  OUTC(MAP_CHAR(c))

/* Output a 16-bit binary number in two bytes */
#define OUT16(n) {OUTC((n)>>8); OUTC((n));}

/* Eject a page from the printer--this is the last command on each page */
#define PAGEEJECT OUTC('\f')

/* Reset printer  at job  start.  <ESC>E  is global  reset which  clears
everything but permanent fonts, which  we do not use.  <ESC>&l0E  resets
left margin to  0.  <ESC>&a0L resets  top margin to  0.  Without  these,
default margins  are supplied  which  lie inside  page.   Unfortunately,
these do not permit negative values,  so we have to further correct  the
origin with  (XORIGIN,  YORIGIN)  defined above;  these  bias  (lmargin,
tmargin) set in dviinit(). */
#define RESET_PRINTER OUTS("\033E\033&lE\033&aL")
#define RESET_PRINTER_END /* OUTS("\033E") */

/* Move relative by (delx,dely) from current point */
#define RMOVETO(delx,dely) {\
if ((delx) == 0)\
	RMOVEY(dely)\
else if ((dely) == 0)\
	RMOVEX(delx)\
else\
	OUTF2("\033*p%c%dx%c%dY",\
		((delx) > 0 ? '+' : '-'),ABS(delx),\
		((dely) < 0 ? '+' : '-'),ABS(dely));\
}

/* Move relative to (delx+currentx,currenty), page origin in lower  left
corner */
#define RMOVEX(delx) {\
if ((delx) > 0)\
	OUTF("\033*p+%dX",(delx));\
else if ((delx) < 0)\
	OUTF("\033*p%dX",(delx));\
}

/* Move relative to (currentx,dely+currenty), page origin in lower  left
corner */
#define RMOVEY(dely) {\
if ((dely) > 0)\
	OUTF("\033*p%dY",-(dely));\
else if ((dely) < 0)\
	OUTF("\033*p+%dY",-(dely));\
}

/* Round a POSITIVE floating-point value to the nearest integer */
#define ROUND(x) ((int)(x + 0.5))

/* Output a new rule at TeX position (x,y).  The device coordinates will
be changed on completion.  The rule origin is the TeX convention of  the
lower-left corner, while the LaserJet  Plus uses the upper-left  corner.
*/
#define RULE(x,y,width,height) {\
	MOVETO(x,(y)+height-1);\
	OUTF2("\033*c%da%dbP",width,height);\
}

/* Set rule of  same size as  previous one at  TeX position (x,y).   The
device coordinates will be changed on completion. */
#define RULE2(x,y) {\
	MOVETO(x,(y)+rule_height-1);\
	OUTS("\033*cP");\
}

/* Set the character in preparation for a DOWNLOAD_CHARACTER */
#define SET_CHAR_CODE(ch)\
	OUTF("\033*c%dE",MAP_CHAR(ch));


/* Set the number of copies of the current page (issue just before
PAGEEJECT) */
#define SETCOPIES(n) OUTF("\033&l%dX",MAX(1,MIN(n,99)))

/* Set the font number in preparation for a CREATE_FONT */
#define SET_FONT_ID(fontnumber) OUTF("\033*c%dd4F",(fontnumber));

#define SET_CURRENT_FONT(fontnumber) OUTF("\033\050%dX",fontnumber);


#define TRANSFER_RASTER_GRAPHICS(nbytes) OUTF("\033*b%dW",(nbytes));

#define UNDEFINED_FONT 65535		/* fonts are numbered 0,1,2,... */

/* In order to handle the MAXPAGEFONTS restriction, we keep a set of the
fonts in  use, accessing  them with  these two  functions.  If  a  later
printer model increases MAXFONTS beyond 32, we can transparently  change
to another  representation,  but  for  now,  we  use  an  efficient  bit
set implementation. */

UNSIGN32 page_members;			/* the bit set for current page */
#define ADD_MEMBER(set,member) {set |= (1 << (member));}
#define IS_MEMBER(set,member) (set & (1 << (member)))
#define EMPTY_SET(set) {set = 0L;}

    /* BIGCHAR() is used in setchar() and setstr().  BIGCHAR()
    is TRUE if the character is too big to send as a downloaded font, or
    if more than MAXPAGEFONTS have been used on this page, and the
    required font's font_number is not in the page_members set, or if
    more the font is not in the first MAXFONTS fonts. */

#define BIGCHAR(t) (((t->wp > (COORDINATE)size_limit) ||\
(t->hp > (COORDINATE)size_limit)) || (fontptr->font_number >= MAXFONTS) ||\
(t->istoobig))

#include "main.h"
void checkchldd();
#include "abortrun.h"
#include "actfact.h"
#include "alldone.h"

/*-->bopact*/
/**********************************************************************/
/******************************* bopact *******************************/
/**********************************************************************/

void
bopact()			/* beginning of page action */
{
    page_fonts = 0;		/* no fonts used on this page yet */
    EMPTY_SET(page_members);

    rule_height = -1;		/* reset last rule parameters */
    rule_width = -1;

    str_ycp = -1;		/* reset string y coordinate */
}

#include "chargf.h"
#include "charpk.h"
#include "charpxl.h"
void makefont1();
/*-->checkchldd*/
/**********************************************************************/
/**************************** checkchldd ******************************/
/**********************************************************************/
void
checkchldd(c)
register BYTE c;
{
  register struct char_entry *tcharptr;  /* temporary char_entry pointer */
  if (fontptr->font_number == UNDEFINED_FONT)
    (void)makefont1();
  tcharptr = &(fontptr->ch[c]);

  if ((BIGCHAR(tcharptr)) || (BAD_CHAR(c)))
    {		/* we shall need to send a graphics bitmap */
	tcharptr->isloaded = FALSE;
    }
  else  {
	if (tcharptr->isloaded == FALSE)
	{
		fontptr->used_chars++;
		tcharptr->isloaded = TRUE;
	}
  }
}



#include "checkpage.h"
#include "clrrow.h"
#include "dbgopen.h"

/*-->devinit*/
/**********************************************************************/
/****************************** devinit *******************************/
/**********************************************************************/

void
devinit(argc,argv)		/* initialize device */
int argc;
char *argv[];
{
    RESET_PRINTER;		/* printer reset */
    DELETE_ALL_FONTS;		/* delete all downloaded fonts */
    font_count = 0;		/* no font numbers are assigned yet */
    pf = -1;    
}

/*-->devterm*/
/**********************************************************************/
/****************************** devterm *******************************/
/**********************************************************************/

void
devterm()			/* terminate device */
{
    RESET_PRINTER_END;		/* printer reset */
}

#include "dvifile_p.h"
#include "dviinit.h"
#include "dviterm.h"

/*-->eopact*/
/**********************************************************************/
/******************************* eopact *******************************/
/**********************************************************************/

void
eopact()			/* end of page action */
{
    if (copies > 1)
        SETCOPIES(copies);
    PAGEEJECT;
}

#include "f20open.h"
#include "fatal.h"

/*-->fillrect*/
/**********************************************************************/
/****************************** fillrect ******************************/
/**********************************************************************/

void
fillrect(x,y,width,height)
COORDINATE x,y,width,height;		/* lower left corner, size */

/***********************************************************************
With the  page origin  (0,0) at  the lower-left  corner, draw  a  filled
rectangle at (x,y).

For most  TeX  uses, rules  are  uncommon, and  little  optimization  is
possible.  However, for the LaTeX Bezier option, curves are simulated by
many small rules (typically  2 x 2)  separated by positioning  commands.
By remembering  the size  of the  last rule  set, we  can test  for  the
occurrence of repeated rules of the same size, and reduce the output  by
omitting the rule  sizes.  The  last rule  parameters are  reset by  the
begin-page action in prtpage(), so they do not carry across pages.

It is not possible to use relative, instead of absolute, moves in  these
sequences, without stacking rules for the whole page, because each  rule
is separated in  the DVI file  by push, pop,  and positioning  commands,
making  for  an  uncertain  correspondence  between  internal  (xcp,ycp)
pixel page coordinates and external device coordinates.

The last string y coordinate, str_ycp,  must be reset here to force  any
later setstring() to reissue new absolute positioning commands.
***********************************************************************/

{
     str_ycp = -1;		/* invalidate string y coordinate */

    if ((height != rule_height) || (width != rule_width))
    {
	RULE(x,y,width,height);
	rule_width = width;	/* save current rule parameters */
	rule_height = height;
    }
    else			/* same size rule again */
	RULE2(x,y);
}

#include "findpost.h"
#include "fixpos.h"
#include "fontfile.h"
#include "fontsub.h"
#include "getbytes.h"
#include "getfntdf.h"
#include "getpgtab.h"
#include "inch.h"
#include "initglob.h"

/*-->loadbmap*/
/**********************************************************************/
/****************************** loadbmap ******************************/
/**********************************************************************/

void
loadbmap(c)			/* load big character as raster bitmap */
register BYTE c;
{
    register struct char_entry *tcharptr;  /* temporary char_entry pointer */
    void (*charyy)();		/* subterfuge to get around PCC-20 bug */

    if (fontptr != pfontptr)
	(void)openfont(fontptr->n);
    if (fontfp == (FILE *)NULL)	/* do nothing if no font file */
	return;

    tcharptr = &(fontptr->ch[c]);

    moveto(hh - tcharptr->xoffp,YSIZE-vv+tcharptr->yoffp);

    MOVETO(xcp,ycp);
    BEGIN_RASTER_GRAPHICS;

    /* Bug fix: PCC-20 otherwise jumps to charxx instead of *charxx */
    charyy = fontptr->charxx;
    (void)(*charyy)(c,outraster);	/* load rasters into device */

    END_RASTER_GRAPHICS;
    str_ycp = -1;		/* invalidate string y coordinate */
}

/*-->loadchar*/
/**********************************************************************/
/****************************** loadchar ******************************/
/**********************************************************************/

void
loadchar(c,pc)
register BYTE c;
BYTE pc;
{
    void (*charyy)();		/* subterfuge to get around PCC-20 bug */
    register struct char_entry *tcharptr; /* temporary char_entry pointer */
    COORDINATE nbytes;			/* number of bytes per row */
    float temp;
    INT16 ntemp;

    if ((c < FIRSTPXLCHAR) || (LASTPXLCHAR < c)) /* check character range */
	return;

    tcharptr = &(fontptr->ch[c]);

    if (!VISIBLE(tcharptr))
	return;				/* do nothing for invisible fonts */

    if (fontptr != pfontptr)
	openfont(fontptr->n);

    if (fontfp == (FILE *)NULL)		/* do nothing if no font file */
	return;

    (void)clearerr(plotfp);

    tcharptr->isloaded = TRUE;

    SET_CHAR_CODE(pc);

    /* Number of bytes in bitmap, possibly reduced because of LaserJet Plus */
    /* limits.  Large characters really need to be handled as bitmaps */
    /* instead of as downloaded characters. */

    nbytes = ((tcharptr->wp) + 7) >> 3;		/* wp div 8 */
    DOWNLOAD_CHARACTER(MIN(16,nbytes)*MIN(255,(tcharptr->hp)));

    OUTC(4);			/* 4 0 14 1 is fixed sequence */
    OUTC(0);

    OUTC(14);
    OUTC(1);

    OUTC(0);			/* orientation = 0 ==> portrait */
    OUTC(0);

    /* Apologies for the temporary variables; the Sun OS 3.2 cc could
    not compile the original expressions. */
    ntemp = MIN(-(tcharptr->xoffp),127);
    ntemp = MAX(-128,ntemp);
    OUT16(ntemp);
    ntemp = MIN(tcharptr->yoffp,127);
    ntemp = MAX(-128,ntemp);
    OUT16(ntemp);
    ntemp = MIN(128,tcharptr->wp);
    OUT16(ntemp);
    ntemp = MIN(255,tcharptr->hp);
    OUT16(ntemp);
    temp = (float)(tcharptr->tfmw);
    temp = temp*conv;
    ntemp = ROUND(temp);
    OUT16(4*ntemp);			/* delta x to nearest dot */

    /* Bug fix: PCC-20 otherwise jumps to charxx instead of *charxx */
    charyy = fontptr->charxx;
    (void)(*charyy)(c,outrow);		/* output rasters */

    if (DISKFULL(plotfp))
	(void)fatal("loadchar():  Output error -- disk storage probably full");
}

/*-->makefont1*/
/**********************************************************************/
/***************************** makefont1 *******************************/
/**********************************************************************/

void
makefont1()
{
    register UNSIGN16 the_char;	/* loop index */
    INT16 j;			/* loop index */
				/* parameters of largest cell */
    COORDINATE cell_above;	/* dots above baseline */
    COORDINATE cell_below;	/* dots below baseline */
    COORDINATE char_below;	/* dots below baseline */
    COORDINATE cell_height;	/* total height */
    COORDINATE cell_left;	/* dots left of character cell */
    COORDINATE cell_width;	/* total width */
    COORDINATE cell_baseline;	/* in 0..(cell_height-1) */

    /*******************************************************************
    Each LaserJet  Plus  font  must  be  assigned  a  unique  number  in
    0..32767, and only 32 fonts  can be active at  one time.  We keep  a
    table of font pointers for this purpose and store the  corresponding
    table index in font_number.  Each time a new font is encountered, we
    increment font_count and  store it in  that font's font_number.  TeX
    produces a unique font number as well, and TeX82 only uses values in
    the range 0..255.  The DVI file supports 32-bit font numbers, so  it
    is better to generate  our own, because  someday other programs  may
    produce DVI files too.

    If fonts are freed dynamically, the table entry must be  invalidated
    and a command sent to the printer to delete that font.  At  present,
    font freeing happens only in dviterm() at the close of processing of
    a single DVI file.  A subsequent  DVI file processed will result  in
    the invocation of devinit() which resets font_count to 0.  It is not
    possible to  avoid  downloading  some fonts  for  subsequent  files,
    because we have no control over what order they are printed in,  and
    each output file must therefore be regarded as independent.

    In order to  deal with  the limit of  MAXFONTS active  fonts in  the
    LaserJet Plus, we  have two choices.   The first and  best and  most
    difficult, is to  entirely discard  the matching of  TeX fonts  with
    LaserJet Plus fonts---new characters would simply be assigned to the
    next available entry in the current open font.  The second choice is
    less satisfactory---when  MAXFONTS fonts  have been  downloaded,  we
    delete the last  of them and  reset the isloaded  flags for all  the
    characters  in  that  font.   The  Plus  also  allows  deletion   of
    individual characters,  so  in  the  first  case,  we  could  delete
    little-used characters  to  make room  in  the font  tables  on  the
    device; we do not use that approach here.  One problem is that  font
    metrics must be assigned  which match the  largest character in  the
    font, so  individual  character deletion  and  later re-use  of  the
    vacated slot is not  in general possible.  A  second problem is  the
    bizarre behavior  of  the  LaserJet  Plus  in  response  to  a  font
    deletion--the current page  is printed, and  the all text  following
    the delete font command will appear on the next page.

    There is a  silly restriction  that only MAXPAGEFONTS  fonts can  be
    used on a  single page,  where MAXPAGEFONTS <  MAXFONTS.  We  handle
    this by keeping a count, page_fonts,  of the number of fonts in  use
    on the  current  page,  and  whenever  a  font  is  requested  whose
    font_number  (0,1,...)   reaches   or  exceeds  MAXPAGEFONTS,   then
    setchar() and setstr()  will not  download the character  as a  font
    character, but instead will set it  as a raster bitmap, in the  same
    fashion that large characters are handled.

    We handle the font deletion misfeature  by just not using it.  If  a
    document requires more than MAXFONTS fonts, then the characters  for
    those additional  fonts  are  sent as  raster  bitmaps,  instead  of
    downloaded characters.

    These two actions completely  remove any limitation  on the size  or
    number of fonts in the LaserJet Plus.

    As an optimization  to reduce  the effort of  font table  searching,
    each time a font is requested which has already been loaded, we move
    that font to the front of font_table[]; the least popular fonts will
    therefore be the  ones that  get deleted.  The  least recently  used
    priority mechanism is  widely used in  operating systems for  buffer
    management; the overhead of  list rearrangement here is  acceptable,
    because font changing  occurs relatively  infrequently, compared  to
    character setting.

	<------wp------>
    ^	................
    |	................
    |	................
    |	................
    |	................
    |	................
    |	................
    hp  ................
    |	................
    |	................
    |	................
    |	.....o..........       <-- character reference point (xcp,ycp) at "o"
    |	................ ^
    |	................ |
    |	................ |--- (hp - yoffp - 1)
    |	................ |
    v	+............... v     <-- (xcorner,ycorner) at "+"
	<--->
	  |
	  |
	 xoffp (negative if (xcp,ycp) left of bitmap)


    The LaserJet Plus  font mechanism requires  the declaration of  cell
    height, width, and baseline in the CreateFont command.   Experiments
    show that  these must  be large  enough to  incorporate the  maximum
    extent above and below the baseline  of any characters in the  font.
    The metric correspondences are as follows:

    -----			-------------
    TeX				LaserJet Plus
    -----			-------------
    xoffp			-(left offset)
    yoffp			top offset
    max(yoffp) + 1		cell baseline
    max(wp)			cell width
    max(yoffp) +
    max(hp-yoff,yoff-hp) + 1	cell height


    *******************************************************************/

    for (j = 0; j < (INT16)font_count; ++j)
	if (font_table[j] == fontptr)	/* then font already known */
	{
	    for (; j > 0; --j)		/* LRU: move find to front */
	        font_table[j] = font_table[j-1];
	    font_table[0] = fontptr;
	    return;			/* nothing more to do */
	}

    if (font_count >= MAXFONTS)	/* then no more fonts can be downloaded */
    {
	/* set up so BIGCHAR() recognizes the font as not downloadable
	and sends bitmaps instead */
	fontptr->font_number = font_count;
	for (the_char = FIRSTPXLCHAR; the_char <= LASTPXLCHAR; ++the_char)
	{
	    fontptr->ch[the_char].isloaded = FALSE;
	    fontptr->ch[the_char].istoobig = TRUE;
	}
	return;
    }

/* new font */
	fontptr->font_number = font_count;
	font_table[font_count] = fontptr;
	font_count++;			/* count the next font */
	fontptr->used_chars = 0;
	fontptr->assigned_chars = 0;

	cell_above = 0;
	cell_below = 0;
	cell_left = 0;
	cell_width = 0;

	for (the_char = FIRSTPXLCHAR; the_char <= LASTPXLCHAR; ++the_char)
	{
	    char_below = fontptr->ch[the_char].yoffp -
	        fontptr->ch[the_char].hp;
	    char_below = ABS(char_below);
	    cell_height = 1 + fontptr->ch[the_char].yoffp + char_below;
	    fontptr->ch[the_char].isloaded = FALSE;  
	    fontptr->ch[the_char].lj_char = -1;
	    fontptr->ch[the_char].lj_font = -1;
	    fontptr->ch[the_char].istoobig = (BOOLEAN)(
	        !IN(-128,-fontptr->ch[the_char].xoffp,127) ||
		!IN(-128, fontptr->ch[the_char].yoffp,127) ||
		!IN(1,fontptr->ch[the_char].wp,255) ||
		!IN(1,cell_height,255));
	}

}
/*-->makefont*/
/**********************************************************************/
/***************************** makefont *******************************/
/**********************************************************************/

void
makefont(pptr,n_slots)
struct printer_font *pptr;
int n_slots;
{
    register UNSIGN16 the_char;	/* loop index */
    register the_slot;
    INT16 j;			/* loop index */
				/* parameters of largest cell */
    COORDINATE cell_above;	/* dots above baseline */
    COORDINATE cell_below;	/* dots below baseline */
    COORDINATE char_below;	/* dots below baseline */
    COORDINATE cell_height;	/* total height */
    COORDINATE cell_left;	/* dots left of character cell */
    COORDINATE cell_width;	/* total width */
    COORDINATE cell_baseline;	/* in 0..(cell_height-1) */



	cell_above = 0;
	cell_below = 0;
	cell_left = 0;
	cell_width = 0;

	for (the_slot = 0; the_slot < n_slots; ++the_slot)
	{
	    fontptr = pptr->char_slots[the_slot].tex_font;
	    the_char = pptr->char_slots[the_slot].tex_char;
	    char_below = fontptr->ch[the_char].yoffp -
	        fontptr->ch[the_char].hp;
	    char_below = ABS(char_below);
	    cell_height = 1 + fontptr->ch[the_char].yoffp + char_below;
	    fontptr->ch[the_char].istoobig = (BOOLEAN)(
	        !IN(-128,-fontptr->ch[the_char].xoffp,127) ||
		!IN(-128, fontptr->ch[the_char].yoffp,127) ||
		!IN(1,fontptr->ch[the_char].wp,255) ||
		!IN(1,cell_height,255));
	    cell_above = MAX(cell_above,fontptr->ch[the_char].yoffp);
	    cell_below = MAX(cell_below,char_below);
	    cell_left = MAX(cell_left,-fontptr->ch[the_char].xoffp);
	    cell_width = MAX(cell_width,fontptr->ch[the_char].wp);
	}
	cell_baseline = cell_above + 1;
	cell_height = (cell_above + cell_below) + 1;

	if ((cell_width > 255) || (cell_height > 255))
	{    /* don't worry, big characters are handled specially later */
	    cell_height = MIN(cell_height,255);
	    cell_width = MIN(cell_width,255);
	}
	cell_width = MAX(1,cell_width);

	SET_FONT_ID(pf);

	CREATE_FONT;

	OUTC(0);
	OUTC(26);		/* descriptor length */

	OUTC(0);		/* always 0 */
	OUTC(1);		/* 8-bit font (for more than 95 characters) */

	OUT16(0);		/* always 0 */

	OUT16(cell_baseline);	/* baseline position (in 0..cell_height-1)*/

	OUT16(cell_width);	/* cell width */

	OUT16(cell_height);	/* cell height */

	OUTC(0);		/* orientation = 0 ==> portrait */
	OUTC(1);		/* spacing = 1 ==> proportional */

	OUT16(0);		/* symbol set (arbitrary) */

	OUT16(2);		/* pitch (arbitrary in 2..1260) */

	OUT16(cell_height);	/* font height (arbitrary in 0..10922) */

	OUT16(0);		/* always 0 */

	OUTC(0);		/* always 0 */
	OUTC(0);		/* style = 0 ==> upright */

	OUTC(0);		/* stroke weight = 0 ==> normal */
	OUTC(0);		/* typeface = 0 (arbitrary) */
}

#include "movedown.h"
#include "moveover.h"
#include "moveto.h"

/*-->newfont*/
/**********************************************************************/
/****************************** newfont *******************************/
/**********************************************************************/

void
newfont()
{
    /*******************************************************************
    Creating fonts immediately  (usually at postamble  read time, before
    any pages have been  printed), would exhaust  the LaserJet Plus font
    supply in a  document with more than  32 fonts, requiring many fonts
    to be sent as bitmaps, instead of as downloaded characters.  If only
    a few pages were  selected for  printing,  it  is unlikely that more
    than 32 would   actually  be needed.   With  character bitmaps,  the
    output file is much larger than when downloaded characters are used,
    and there is a likelihood of raising printer error code 21 (page too
    complex to process), at least on the LaserJet Plus, which has rather
    limited internal memory (about 512Kb).  The HPLJ  Series II can have
    up to  4.5Mb  of memory, and possibly   would   eliminate this error
    condition.

    Instead, we simply mark the font as currently undefined.  When it is
    first referenced, setfont() will call  makefont() to actually create
    it in the printer.
    *******************************************************************/

    fontptr->font_number = UNDEFINED_FONT;
}

#include "nosignex.h"
#include "openfont.h"
#include "option.h"

/*-->outraster*/
/**********************************************************************/
/***************************** outraster ******************************/
/**********************************************************************/

void
outraster(c,yoff)/* print single raster line in bitmap graphics mode */
BYTE c;		/* current character value */
UNSIGN16 yoff;	/* offset from top row (0,1,...,hp-1)  */
{
    UNSIGN16 bytes_per_row;	/* number of raster bytes to copy */
    register UNSIGN16 k;	/* loop index */
    register UNSIGN32 *p;	/* pointer into img_row[] */
    struct char_entry *tcharptr;/* temporary char_entry pointer */
    register BYTE the_byte;	/* unpacked raster byte */

    tcharptr = &(fontptr->ch[c]);/* assume check for valid c has been done */
    bytes_per_row = (UNSIGN16)((tcharptr->wp) + 7) >> 3; /* wp div 8 */
    p = img_row;		/* we step pointer p along img_row[] */

    TRANSFER_RASTER_GRAPHICS(bytes_per_row);

    for (k = bytes_per_row; k > 0; ++p)
    {
	the_byte = (BYTE)((*p) >> 24);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;

	the_byte = (BYTE)((*p) >> 16);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;

	the_byte = (BYTE)((*p) >> 8);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;

	the_byte = (BYTE)(*p);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;
    }
}

/*-->outrow*/
/**********************************************************************/
/******************************* outrow *******************************/
/**********************************************************************/

void
outrow(c,yoff)	/* copy img_row[] into rasters[] if allocated, else no-op */
BYTE c;		/* current character value */
UNSIGN16 yoff;	/* offset from top row (0,1,...,hp-1)  */
{
    UNSIGN16 bytes_per_row;	/* number of raster bytes to copy */
    register UNSIGN16 k;	/* loop index */
    register UNSIGN32 *p;	/* pointer into img_row[] */
    struct char_entry *tcharptr;/* temporary char_entry pointer */
    register BYTE the_byte;	/* unpacked raster byte */

    if (yoff > 255)	/* LaserJet Plus cannot handle big characters */
	return;

    tcharptr = &(fontptr->ch[c]);/* assume check for valid c has been done */
    bytes_per_row = (UNSIGN16)((tcharptr->wp) + 7) >> 3; /* wp div 8 */
    bytes_per_row = MIN(16,bytes_per_row);	/* limited to 128 bits/row */
    p = img_row;		/* we step pointer p along img_row[] */

    for (k = bytes_per_row; k > 0; ++p)
    {
	the_byte = (BYTE)((*p) >> 24);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;

	the_byte = (BYTE)((*p) >> 16);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;

	the_byte = (BYTE)((*p) >> 8);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;

	the_byte = (BYTE)(*p);
	OUTC(the_byte);
	if ((--k) <= 0)
	    break;
    }
}

#include "prtpage_p.h"
#include "readfont.h"
#include "readgf.h"
#include "readpk.h"
#include "readpost.h"
#include "readpxl.h"
#include "reldfont.h"
#include "rulepxl.h"

/*-->setchar*/
/**********************************************************************/
/****************************** setchar *******************************/
/**********************************************************************/

void
setchar(c, update_h)
register BYTE c;
register BOOLEAN update_h;
{
    register struct char_entry *tcharptr;  /* temporary char_entry pointer */

    /* ONPAGE() is used here and in setstr().  */

#define ONPAGE(t) (((hh - t->xoffp + t->pxlw) <= XSIZE) \
    && (hh >= 0)\
    && (vv <= YSIZE)\
    && (vv >= 0))

    if (DBGOPT(DBG_SET_TEXT))
    {
	(void)fprintf(stderr,"setchar('");
	if (isprint(c))
	    (void)putc(c,stderr);
	else
	    (void)fprintf(stderr,"\\%03o",(int)c);
	(void)fprintf(stderr,"'<%d>) (hh,vv) = (%ld,%ld) font name <%s>",
	    (int)c, (long)hh, (long)vv, fontptr->n);
	NEWLINE(stderr);
    }

    tcharptr = &(fontptr->ch[c]);

    moveto(hh,(COORDINATE)(YSIZE-vv));
    if (ONPAGE(tcharptr))
    {				/* character fits entirely on page */
	if (tcharptr->lj_font != pf)
	{
	    (void)setfont(tcharptr->lj_font);
	}

	if (VISIBLE(tcharptr))
	{
	    if ((BIGCHAR(tcharptr)) || (BAD_CHAR(c)))
		loadbmap(c);
	    else
	    {
		if (ycp != str_ycp)
		{
		    MOVETO(xcp,ycp);
		    str_ycp = ycp;
		}
		else
	            MOVEX(xcp);
		
		c = tcharptr->lj_char;
		OUT_CHAR(c);
	    }
	}
    }
    else if (DBGOPT(DBG_OFF_PAGE) && !quiet)
    {				/* character is off page -- discard it */
	(void)fprintf(stderr,
	    "setchar(): Char %c [10#%3d 8#%03o 16#%02x] off page.",
	    isprint(c) ? c : '?',c,c,c);
	NEWLINE(stderr);
    }

    if (update_h)
    {
	h += (INT32)tcharptr->tfmw;
	hh += (COORDINATE)tcharptr->pxlw;
	hh = fixpos(hh-lmargin,h,conv) + lmargin;
    }
}

#include "setfntnm.h"

/*-->setfont*/
/**********************************************************************/
/****************************** setfont *******************************/
/**********************************************************************/

void
setfont(font)
{
	pf = font;
	SET_CURRENT_FONT(pf);
}

#include "setrule.h"

/*-->setstr*/
/**********************************************************************/
/******************************* setstr *******************************/
/**********************************************************************/

void
setstr(c)
register BYTE c;
{
    register struct char_entry *tcharptr;  /* temporary char_entry pointer */
    register BOOLEAN inside;
    COORDINATE hh_last;		/* hh before call to fixpos() */
    register UNSIGN16 k;	/* loop index */
    UNSIGN16 nstr;		/* number of characters in str[] */
    BYTE str[MAXSTR+1];		/* string accumulator */
    BOOLEAN truncated;		/* off-page string truncation flag */

    /*******************************************************************
    Set a sequence of characters in SETC_000 .. SETC_127 with a  minimal
    number of LaserJet Plus commands.  These sequences tend to occur  in
    long clumps  in  a DVI  file,  and setting  them  together  whenever
    possible substantially decreases  the overhead and  the size of  the
    output file.  A sequence can be set as a single string if

	* TeX and LaserJet Plus coordinates of each character agree (may
	  not be true, since device coordinates are in multiples of  1/4
	  pixel; violation  of  this  requirement  can  be  detected  if
	  fixpos() changes hh, or if ycp != str_ycp), AND

	* each character is in the same font (this will always be true
	  in a sequence from a DVI file), AND
[this needs looking at in PP8 version]

	* each character fits within the page boundaries, AND

	* each character definition is already loaded, AND

	* each character is from a visible font, AND

	* each  character bitmap  extent is smaller  than the size_limit
	  (which is  used to  enable discarding  large characters  after
	  each use in order  to conserve virtual  memory storage on  the
	  output device).

    Whenever any of these conditions  does not hold, any string  already
    output is terminated, and a new one begun.

    Two output optimizations are implemented here.  First, up to  MAXSTR
    (in practice more  than enough) characters  are collected in  str[],
    and any  that  require downloading  are  handled.  Then  the  entire
    string is set at once, subject to the above limitations.  Second, by
    recording the vertical page coordinate, ycp, in the global  variable
    str_ycp (reset  in  prtpage()  at begin-page  processing),  it  is
    possible to avoid  outputting y coordinates  unnecessarily, since  a
    single line of  text will  generally result  in many  calls to  this
    function.
    *******************************************************************/

#define BEGINSTRING {inside = TRUE;\
    if (ycp != str_ycp)\
    {\
	MOVETO(xcp,ycp);\
	str_ycp = ycp;\
    }\
    else\
	MOVEX(xcp);}

#define ENDSTRING {inside = FALSE;}

#define OFF_PAGE (-1)	/* off-page coordinate value */

    tcharptr = &(fontptr->ch[c]);

    if (tcharptr->lj_font != pf)	/* output new font selection */
    {
	(void)setfont(tcharptr->lj_font);
    }

    inside = FALSE;
    truncated = FALSE;

    nstr = 0;
    while ((SETC_000 <= c) && (c <= SETC_127) && (nstr < MAXSTR))
    {			/* loop over character sequence */
	str[nstr++] = c;		/* save string character */

	c = (BYTE)nosignex(dvifp,(BYTE)1);
    }

    /* put back character which terminated the loop */
    (void)UNGETC((int)(c),dvifp);

    if (DBGOPT(DBG_SET_TEXT))
    {
	(void)fprintf(stderr,"setstr(\"");
	for (k = 0; k < nstr; ++k)
	{
	    c = str[k];
	    if (isprint(c))
	        (void)putc(c,stderr);
	    else
	        (void)fprintf(stderr,"\\%03o",(int)c);
	}
	(void)fprintf(stderr,"\") (hh,vv) = (%ld,%ld) font name <%s>",
	    (long)hh, (long)vv, fontptr->n);
	NEWLINE(stderr);
    }

    for (k = 0; k < nstr; ++k)
    {			/* loop over character sequence */
	c = str[k];
	tcharptr = &(fontptr->ch[c]);
	moveto(hh,YSIZE-vv);

	if (ONPAGE(tcharptr) && VISIBLE(tcharptr))
	{		/* character fits entirely on page and is visible */
	    /* We must check first for a big character.  The character */
	    /* may have been downloaded on a previous page, but if it is */
	    /* not in the page_members font set, we must send it again */
	    /* as a bitmap instead. */
	    if (BIGCHAR(tcharptr))	/* BAD_CHAR(c)) cannot be true here */
	    {
		if (inside)
		    ENDSTRING;		/* finish any open string */
		loadbmap(c);
	    }
	    else if (tcharptr->lj_font != pf)
	    {
		if (inside)
			ENDSTRING;
	    }
	    else
	    {
		if (!inside)
		    BEGINSTRING;
		c = tcharptr->lj_char;
		OUT_CHAR(c);
	    }
	}
	else		/* character does not fit on page -- output */
	{		/* current string and discard the character */
	    truncated = TRUE;
	    if (inside)
		ENDSTRING;
	}
	/* update horizontal positions in TFM and pixel units */
	h += (INT32)tcharptr->tfmw;
	hh += (COORDINATE)tcharptr->pxlw;
	hh_last = hh;
	hh = fixpos(hh-lmargin,h,conv) + lmargin;
	if (DBGOPT(DBG_POS_CHAR))
	{
	    (void)fprintf(stderr,
		"[%03o] xcp = %d\tycp = %d\thh = %d\thh_last = %d\n",
		c,xcp,ycp,hh,hh_last);
	}

	/* If fixpos() changed position, we need new string next time */
	/* around.   Actually, since the LaserJet Plus stores character */
	/* widths in units of 1/4 dot, we could compute coordinates at */
	/* four times the precision, but for now, we start a new string */
	/* each time we have one or more dots of error. */

	if ((hh != hh_last) && inside)
	    ENDSTRING;
    }
    if (truncated && DBGOPT(DBG_OFF_PAGE) && !quiet)
    {
	(void)fprintf(stderr,"setstr(): Text [");
	for (k = 0; k < nstr; ++k)
	    (void)fprintf(stderr,isprint(str[k]) ? "%c" : "\\%03o",str[k]);
	(void)fprintf(stderr,"] truncated at page boundaries.");
	NEWLINE(stderr);
    }
    if (inside)		/* finish last string */
	ENDSTRING;
}

#include "signex.h"
#include "skgfspec.h"
#include "skipfont.h"
#include "skpkspec.h"
#include "special.h"
#include "strchr.h"
#include "strcm2.h"
#include "strid2.h"
#include "strrchr.h"
#include "tctos.h"
#include "usage.h"
#include "warning.h"