	PROGRAM HPGL
C *** LAST REVISED ON 19-AUG-1987 11:41:56.19
C *** SOURCE FILE: [DL.GRAPHICS.LONGLIB]HPGL.FOR
C
C	CREATED:  DGL 15-JAN-1986
C
C	THIS PROGRAM PERMITS REPLOTTING OF A PRINTER GRAPHICS FILE
C	PRODUCED BY THE LONGLIB GRAPHICS LIBRARY TO A VT220 TERMINAL
C	EQUIPPED WITH AN HPGL COMPATIBLE PEN PLOTTER.  THIS VERSION
C	DOES NOT USE STRIPPING OF THE PLOT IMAGE NOR ATTEMPT TO MINIMIZE
C	PEN MOTIONS.
C
C	NOTE: SINCE THE PRINTER FILE DOES NOT MAINTAIN A COPY OF
C	WHAT DATA WENT TO THE SCREENS, ANY CTERMS USED IN THE ORIGINAL
C	PROGRAM GENERATING THE FILE WILL NOT BE REPEATED BY THIS PROGRAM.
C
C	THIS PROGRAM IS FORTRAN 77 COMPATIBLE WITH THE EXCEPTIONS:
C		1. TABS (^I) ARE USED TO INDENT LINES.
C		2. A VAX DEPENDENT ROUTINE IS USED TO GET COMMAND LINE.
C		3. INTEGER*2 USED IN REGET ROUTINE.  THIS CAN BE CHANGED
C		   TO INTEGER IF THIS IS ALSO DONE IN LONGLIB PRINTER
C		   HISTORY ROUTINES LIBRARY.
C		4. USES ICHAR TO EXTRACT ASCII VALUE FROM CHARACTER VARIABLE.
C
	COMMON /TERM/ITERM
	CHARACTER*80 NAME
C
C	MACHINE DEPENDENT METHOD OF GETING THE FILE NAME FROM THE
C	RUN COMMAND LINE
C
	IERR=LIB$GET_FOREIGN(NAME,,IFLAG)
C
C	IF FILE NAME WAS NOT PRESENT ON ARGUMENT LINE THEN USE DEFAULT NAME
C
	IF (NAME.EQ.' ') NAME='FOR003.DAT'
C
C	DETERMINE TERMINAL TYPE
C
	WRITE(*,10)
10	FORMAT('$ENTER TERMINAL TYPE: (0=PLAIN VT220, 1=VT100+SELNAR) ')
	READ(*,*) ITERM
C
C	TURN PRINTER PORT ON TERMINAL
C
	CALL PLOTAUTO
C
C	CONVERT LONGLIB META FILE TO HPGL PLOT COMMANDS AND WRITE TO TERMINAL
C
	CALL HPGLPLOT(NAME)
C
C	TURN PRINTER PORT OFF ON TERMINAL
C
	CALL PLOTAOFF
	STOP
	END
C
	SUBROUTINE PLOTAOFF
C
C	DISABLE PRINTER PORT ON TERMINAL
C
	COMMON /TERM/ITERM
	CHARACTER*1 ESC,ETB
	PARAMETER (ESC=CHAR(27))
	PARAMETER (ETB=CHAR(23))
	IF (ITERM.EQ.0) THEN
C	VT220
		WRITE (*,1) ESC
1		FORMAT(X,A1,'[?4i')
	ELSE IF (ITERM.EQ.1) THEN
C	VT100+SELNAR GR100
		WRITE (*,1) ETB,ESC
2		FORMAT('+',2A1,'2')
	ENDIF
	RETURN
	END
C
C
	SUBROUTINE PLOTAUTO
C
C	ENABLE PRINTER PORT ON TERMINAL
C
	COMMON /TERM/ITERM
	CHARACTER*1 ESC
	PARAMETER (ESC=CHAR(27))
	IF (ITERM.EQ.0) THEN
C	VT220
	 	WRITE (*,1) ESC
1		FORMAT(X,A1,'[?5i')
	ELSE IF (ITERM.EQ.1) THEN
C	VT100+SELNAR GR100
		WRITE (*,2) ESC,ESC
2		FORMAT(X,A1,'1',A1,';<')
	ENDIF
	RETURN
	END
C
C
	SUBROUTINE HPGLPLOT(NAME)
C
C	MAIN ROUTINE TO REPLOT A PRINTER GRAPHICS FILE TO AN HPGL COMPATIBLE
C	DEVICE FOR THE LONGLIB GRAPHICS LIB.
C
C	INPUT:
C	NAME	(CHARACTER)	FILE NAME FOR PRINTER HISTORY FILE
C
	CHARACTER*(*) NAME
	CHARACTER*1 ANS
	INTEGER LINET(7)
	DATA LINET/-1,1,4,2,3,6,5/
	LOGICAL PENUP
	DATA PENUP/.TRUE./
C
C	INITIALIZE PLOTTER
C
	WRITE (*,23)
23	FORMAT(' IN;SP1;PU;LT-1;')
C
C	OPEN PRINTER HISTORY FILE
C
	MP=999
	OPEN(UNIT=2,FILE=NAME,FORM='UNFORMATTED',STATUS='OLD',
     $		READONLY,ERR=99)
C
C	TOP OF COMMAND READ LOOP
C
1000	CALL REGET(M1,M2,M3,MP,2)
	IF (M3.EQ.11) GOTO 110
	SY=7200./FLOAT(M1)/7.92
	SX=10000./FLOAT(M2)/11.0
	IF (M1.EQ.0.OR.M2.EQ.0) GOTO 110
  1	CONTINUE
	CALL REGET(M1,M2,M3,MP,2)
	Y1=M1*SY
	X1=M2*SX
C
C	I3 IS THE COMMAND CODE (EQUIVALENT TO 3RD ARGUMENT OF PLOT)
C
	I3=M3
C
C	IF I3=999 THEN END OF FILE ENCOUNTERED
C
	IF (M3.GE.999) GOTO 999
C
C	EXECUTE COMMAND
C
	GOTO (10,20,20,10,10,10,10,10,20,100,110) I3
10	GOTO 1
C
20	CONTINUE
C
C	CALL PLOT(X1,Y1,I3)		! EQUIV LONGLIB COMMAND
C
	IF (PENUP) THEN
		IF (I3.EQ.2) WRITE(*,3000)
3000		FORMAT(' PD;')
	ELSE
		IF (I3.EQ.3) WRITE(*,3001)
3001		FORMAT(' PU;')
	ENDIF
	IF (I3.EQ.2) PENUP=.FALSE.
	IF (I3.EQ.3) PENUP=.TRUE.
	WRITE(*,43) IFIX(X1+.5),IFIX(Y1+.5)
43	FORMAT(X,'PA',I6,',',I6,';')
	GOTO 1
C
100	CONTINUE
C
C	CALL NEWPAGE		! EQUIV LONGLIB COMMAND
C
	WRITE(*,3002)
3002	FORMAT(' PU;SP0;')
C
C	DISABLE TERMINAL PRINTER PORT
C
	CALL PLOTAOFF
C
C	PROMPT TERMINAL FOR PAGE CHANGE
C
	WRITE(*,44)
44	FORMAT('$Change paper, <return> when ready, S to stop ')
	READ (*,3500,END=99) ANS
3500	FORMAT(A1)
C
C	RE-ENABLE TERMINAL PRINTER PORT
C
	CALL PLOTAUTO
C
C	IS REPLY IS ASCII S THEN STOP
C
	IF (ICHAR(ANS).EQ.115.OR.ICHAR(ANS).EQ.83) RETURN
C
	GOTO 1
999	GOTO (1000,1001,1002,1003) I3-999
2000	GOTO 1
C
C	LINE TYPE
C
1001	CONTINUE
	IOLD=IABS(M1)
1550	IOLD=MOD(IOLD,7)+1
	IOLD=LINET(IOLD)
	WRITE (*,1555) IOLD
1555	FORMAT(X,'LT',I2',1.2;')
	GOTO 1
C
C	SET PEN COLOR
C
1002	CONTINUE
C
C	IF (M2.NE.0) CALL PLOT(FLOAT(M2),0.,0)	! EQIV LONGLIB NEW COLOR
C
	IF (M2.NE.0) THEN
		I=M2
		IF (M2.EQ.255) I=1
		IF (M2.EQ.10) I=1
		WRITE(*,3020) I
3020		FORMAT(' PU;SP',I2,';')
	END IF
	GOTO 1
C
C	DETERMINE LINE WIDTH
C
1003	I3=M1*10+IOLD
C
C	CALL NEWPEN(I3)		! EQUIV LONGLIB COMMAND
C
	GOTO 1
C
C	CLOSE FILE AND TERMINATE PLOTTING
C
110	CLOSE(2)
 99	WRITE(*,3030)
3030	FORMAT(' PU;SP0;')
	RETURN
	END
C
	SUBROUTINE REGET(M1,M2,M3,MP,ILU)
C
C	READ DATA FROM LONGLIB PRINTER HISTORY META FILE
C	
	INTEGER*2 M(128)
	MP=MP+3
	IF (MP.GT.128) THEN
		READ (ILU,ERR=99) M
		MP=3
	ENDIF
	M3=M(MP)
	M2=M(MP-1)
	M1=M(MP-2)
	IF (M3.EQ.999) GOTO 99
	RETURN
99	M3=11
	RETURN
	END
