     	PROGRAM TXTPLT_MAIN
C
C  PROGRAMMER:  RONG-SHENG ZHOU
C		BOSTON COLLEGE, 12/88
C
C	THIS ROUTINE IMPLEMENTS A DCL "RUNOFF" LIKE TEXT PLOTTING.
C
C	FOLLOWING COMMANDS ARE IMPLEMENTED:
C
C	FULL-NAME           SHORT-HAND            FUNCTION
C ------------------------------------------------------------------------
C
C	PAGE SETUP COMMANDS:
C     -----------------------------
C	.PAGESIZE p1,p2     .PS p1,p2         SET PAGE SIZE (HxV)
C	.SCALE p1,p2        .SC p1,p2         SET PLOT OVERALL SCALE
C	.TOPMARGIN p1       .TM p1            SET TOP PAGE MARGIN
C	.LEFTMARGIN p1      .LM p1            SET LEFT PAGE MARGIN
C
C	PAGE RUN COMMANDS:
C     ------------------------------
C	.SPACING p1         .SP p1            SET LINE SPACING
C	.HSPACE p1          .HS p1            INDENT TO THE RIGHT
C	.VSPACE p1          .VS p1            INDENT DOWN
C	.RESET p1,p2        .RS p1,p2         ABSOLUTE COORDINATE
C	.LINE p1,p2,...     .LI p1,p2,...     DRAW A LINE
C	.SIZE  p1,p1        .SZ p1,p2         SET TEXT SIZE
C	.COLOR p1           .CL p1            SET TEXT COLOR
C	.CENTER p1 p2       .CT p1 p2         BEGIN CENTERING PRINT
C	.ENDCENTER          .EC               END CENTERING
C	.FONT p1            .FT p1            SELECT NEW FONT
C	.NEWPAGE            .NP
C
     	PARAMETER (LUN= 13, XORG= 5.0, YORG= 4.0)
     	INTEGER TPEN, FONT, CENT
     	COMMON/TXTPLT_SETUP/ PWID, PHGT, TOPM, ODDM, LINE,
     +  XPOS, YPOS, SPAC, TWID, THGT, TPEN, CENT, FONT,
     +  IPAG, JPAG, XCN1, XCN2, XSCL, YSCL, YOLD
     	INTEGER CRT
     	LOGICAL YES
     	CHARACTER TXTNAM*32, DESNAM*32, TLINE*132
     	CHARACTER PLOTID*32
     	COMMON/TXTPLT_HARDCOPY/ HPAR(20)
C
C  INITIALIZE THE SESSION...
C
     	CALL IFINIT_SESSION
C
C  GET THE FILENAME FIRST...
C
111	CALL IFINIT_LINE(3)
112	CALL IFANSI_VIDEO(30)
     	WRITE(6,1000)
1000	FORMAT(' * Press PF2 for help, PF4 for DCL prompt *')
     	CALL IFANSI_VIDEO(47)
113	WRITE(6,1001)
1001	FORMAT(' Text filename [.TXT] ',$)
     	IEND = IFQIOW( NCHS, TXTNAM )
     	IF( IEND.EQ.26) GOTO 999
     	IF( NCHS.EQ.0 ) GOTO 113
     	NCHS = IFMISC_STRIP(TXTNAM)
     	CALL   IFMISC_UPPER(NCHS,TXTNAM)
C
C  DISPLAY ON-LINE HELP IF REQUESTED...
C
     	IF( IEND.EQ.27 ) THEN
     	    IF( TXTNAM(1:2).EQ.'OQ' ) THEN
     		NCHS = IFMISC_HLP( 'TXTPLT', '$ HELP', 21, 0 )
     		IF( NCHS.NE.0 ) GOTO 112
     	    ELSE IF( TXTNAM(1:2).EQ.'OS' ) THEN
     		CALL IFMISC_SPAWN( '$', 0 )
     	    ELSE
     		CALL IFMISC_OOPS( 5, 0, 0, ' ' )
     	        GOTO 112
     	    ENDIF
     	    GOTO 111
     	ENDIF
C
C  CHECK FILE EXISTANCE...
C
     	CALL IFMISC_DEFEXT( TXTNAM, '.TXT' )
     	INQUIRE( FILE=TXTNAM, EXIST=YES )
     	IF( .NOT.YES ) GOTO 222
C
C  OPEN THE FILE....
C
     	OPEN( UNIT=LUN, FILE=TXTNAM, STATUS='OLD', READONLY,
     +        FORM='FORMATTED', ERR=222 )
     	GOTO 333
222	CALL IFMISC_OOPS( 1, 0, 0, TXTNAM )
     	GOTO 112
C
C  INITIALIZE PAGE SETUP...
C
333	DESNAM = TXTNAM
     	CALL IFMISC_NAMEXT( DESNAM, '.DES' )
     	CALL TXTPLT_INIT
     	IEND = 0
	PLOTID = ' '
444	XPOS = 0.0
     	YPOS = 0.0
     	YOLD = 0.0
     	LINE = 0
     	CALL IFINIT_LINE(3)
     	CALL IFINIT_PLOT( XORG, YORG, TXTNAM )
     	CALL IFSPEN(TPEN)
     	CALL IFSEG_PIVOT( XORG, YORG )
     	CALL IFSEG_OPEN( 19 )
C
C  READ ONE LINE FROM THE FILE...
C
555	READ(LUN,556,ERR=666,END=775) TLINE
556	FORMAT(A)
     	IF( TLINE.EQ.' ' ) GOTO 555
     	NCHS = IFMISC_STRIP(TLINE)
     	IF( TLINE(1:1).EQ.'.' ) THEN
     	    NCHS = TXTPLT_COMM( TLINE(1:NCHS) )
     	    IF( NCHS.LT.0 ) GOTO 776		!ERROR COMMAND
     	    IF( NCHS.GT.0 ) THEN		!NEW PAGE
     		IPAG = IPAG + 1
     		IF( IPAG.GT.JPAG ) GOTO 776
     		GOTO 555
     	    ENDIF
     	ELSE
     	    IF( IPAG.EQ.JPAG ) CALL TXTPLT_DRAW( TLINE )
     	    LINE = LINE + 1
     	    YPOS = YPOS + SPAC + THGT
     	    IF( YPOS.GT.YOLD ) YOLD = YPOS
     	    IF( YPOS.GT.PHGT ) THEN
     		IPAG = IPAG + 1
     		IF( IPAG.GT.JPAG ) GOTO 776
     	    ENDIF
     	ENDIF
     	GOTO 555
666	CALL IFMISC_OOPS( 14, 0, 0, TXTNAM )
     	GOTO 777
775	IEND = 1
776	CALL IFSEG_OPEN(0)
     	CALL LAYOUT_AUX(0,XORG,YORG,DESNAM)
     	CALL IFINIT_PLOT_END
C
C  PAUSE FOR THIS PAGE...
C
777	CALL IFANSI_VIDEO(47)
     	WRITE(6,7000)
7000	FORMAT(' TXTPLT> Please press a desired function key:')
888	WRITE(6,7001)
7001	FORMAT(' TXTPLT> ?>Help 0>Exit 1>Hardcopy 2>Layout 3>Newfile',
     +' 4>Down_page 5>Up_page ...')
     	CALL IFANSI_CURSOR(0,8)
     	CALL IFQIOW_KEY( KEY )

     	IF( KEY.EQ.26 .OR. KEY.EQ.48 .OR. KEY.EQ.69 ) GOTO 999
     	IF( KEY.EQ.49 .OR. KEY.EQ.72 ) THEN
     	    CALL IFCOPY( HPAR(1), PLOTID )
     	    CALL IFINIT_LINE( -3 )
     	ELSE IF( KEY.EQ.-80 ) THEN
     	    CALL IFANSI_CURSOR(0,14)
     	    CALL IFANSI_LEDS(1)
     	    CALL IFFEED( HPAR(1), PLOTID, ' ' )
     	    CALL IFANSI_LEDS(0)
     	ELSE IF( KEY.EQ.50 .OR. KEY.EQ.76 ) THEN
     	    CALL LAYOUT_AUX( 2, XORG, YORG, DESNAM )
     	ELSE IF( KEY.EQ.51 .OR. KEY.EQ.78 .OR. KEY.EQ.-82 ) THEN
     	    CLOSE(UNIT=LUN)
     	    GOTO 113
     	ELSE IF( KEY.EQ.52 .OR. KEY.EQ.68 ) THEN
     	    IF( IEND.EQ.1 ) THEN
     		WRITE(6,7005)
7005		FORMAT('_oops: End of file hit.')
     	    ELSE
     		JPAG = JPAG + 1
     		IPAG = JPAG
     		GOTO 444
     	    ENDIF
     	ELSE IF( KEY.EQ.53 .OR. KEY.EQ.85 ) THEN
     	    IF( JPAG.EQ.1 ) THEN
     		WRITE(6,7006)
7006		FORMAT('_oops: 1st page being displayed.')
     	    ELSE
     		REWIND(UNIT=LUN)
     		IEND = 0
     		JPAG = JPAG - 1
     		IPAG = 1
     		GOTO 444
     	    ENDIF
     	ELSE IF( KEY.EQ.54 .OR. KEY.EQ.82 ) THEN
     	    REWIND(UNIT=LUN)
     	    IEND = 0
     	    IPAG = 1
     	    GOTO 444
     	ELSE IF( KEY.EQ.32 .OR. KEY.EQ.63 .OR. KEY.EQ.-81 ) THEN
     	    CALL IFMISC_HLP( 'TXTPLT', '$ MENU', -2, 0 )
     	ELSE IF( KEY.EQ.36 .OR. KEY.EQ.-83 ) THEN
     	    CALL IFMISC_SPAWN( '$', 0 )
     	    CALL IFINIT_LINE( -3 )
     	ELSE
     	    CALL IFMISC_OOPS( 5, 0, 0, ' ' )
    	ENDIF
     	GOTO 888
999	CLOSE(UNIT=LUN)
     	CALL IFINIT_EXIT
     	CALL EXIT
	END

     	SUBROUTINE TXTPLT_INIT
C
C	THIS ROUTINE INITIALIZES PAGE SETUP.
C
     	INTEGER TPEN, FONT, CENT
     	COMMON/TXTPLT_SETUP/ PWID, PHGT, TOPM, ODDM, LINE,
     +  XPOS, YPOS, SPAC, TWID, THGT, TPEN, CENT, FONT,
     +  IPAG, JPAG, XCN1, XCN2, XSCL, YSCL, YOLD

     	PWID = 8.0
     	PHGT = 5.0
     	XSCL = 1.0
     	YSCL = 1.0
     	TOPM = 1.5
     	ODDM = 0.5
     	SPAC = 0.15
     	TWID = 0.15
     	THGT = 0.15
     	CENT = 0
     	FONT = 1
     	XCN1 = 0.0
     	XCN2 = PWID
     	IPAG = 1
     	JPAG = 1
     	TPEN = 3

     	RETURN
     	END

     	FUNCTION TXTPLT_COMM( COMM )
C
C	THIS ROUTINE PARSES INPUT LINE FOR A POSSIBLE COMMAND.
C
     	PARAMETER (XORG= 5.0, YORG= 4.0)
     	INTEGER TPEN, FONT, CENT
     	COMMON/TXTPLT_SETUP/ PWID, PHGT, TOPM, ODDM, LINE,
     +  XPOS, YPOS, SPAC, TWID, THGT, TPEN, CENT, FONT,
     +  IPAG, JPAG, XCN1, XCN2, XSCL, YSCL, YOLD
     	CHARACTER TEXT*64, KEYW*2, PARA*32, COMM*(*)
     	DIMENSION RP(10), KP(10)

     	TEXT = COMM(2:)
     	NCHS = IFMISC_LENG(TEXT)
     	CALL   IFMISC_UPPER(NCHS,TEXT)
     	ISPC = INDEX( TEXT(1:NCHS), ' ' )
     	PARA = ' '
     	KEYW = ' '
     	IF( ISPC.GT.0 ) THEN
     	    NCHS = ISPC - 1
     	    PARA = TEXT
     	    ISPC = IFMISC_NEXT(PARA)
     	ENDIF
     	IF( NCHS.LT.2 ) GOTO 666
     	IF( NCHS.EQ.2 ) KEYW = TEXT(1:2)
     	IF( KEYW.EQ.'CT' .OR. TEXT(1:NCHS).EQ.'CENTER' ) THEN
     	    IF( PARA.NE.' ' ) THEN
     		NCHS = IFMISC_DECODE( PARA, ISPC, 2, RP, KP )
     		IF( NCHS.EQ.0 ) GOTO 555
     		IF( RP(1).LT.0.0 .OR. RP(1).GT.PWID ) GOTO 777
     		IF( RP(2).LT.0.0 .OR. RP(2).GT.PWID ) GOTO 777
     		IF( RP(2).LE.RP(1) ) GOTO 777
     		XCN1 = RP(1)
     		XCN2 = RP(2)
     	    ELSE
     		XCN1 = 0.0
     		XCN2 = PWID
     	    ENDIF
     	    CENT = 1
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'EC' .OR. TEXT(1:NCHS).EQ.'ENDCENTER' ) THEN
     	    CENT = 0
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'NP' .OR. TEXT(1:NCHS).EQ.'NEWPAGE' ) THEN
     	    NCHS = 1
     	    GOTO 9999
     	ENDIF
     	IF( KEYW.EQ.'CL' .OR. TEXT(1:NCHS).EQ.'COLOR' ) THEN
     	    IF( PARA.EQ.' ' ) GOTO 888
     	    NCHS = 0
     	    IF( PARA(1:3).EQ.'BLA' .OR. PARA(1:3).EQ.'WHI' ) NCHS = 1
     	    IF( PARA(1:3).EQ.'RED' ) NCHS = 2
     	    IF( PARA(1:3).EQ.'GRE' ) NCHS = 3
     	    IF( PARA(1:3).EQ.'BLU' ) NCHS = 4
     	    IF( PARA(1:3).EQ.'MAG' ) NCHS = 5
     	    IF( PARA(1:3).EQ.'PUR' ) NCHS = 6
     	    IF( PARA(1:3).EQ.'YEL' ) NCHS = 7
     	    IF( NCHS.EQ.0 ) GOTO 777
     	    IF( NCHS.NE.TPEN ) THEN
     		TPEN = NCHS
     		CALL IFSPEN( TPEN )
     	    ENDIF
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'FT' .OR. TEXT(1:NCHS).EQ.'FONT' ) THEN
     	    IF( PARA.EQ.' ' ) GOTO 888
     	    NCHS = 0
     	    IF( PARA.EQ.'STANDARD' ) NCHS = 1
     	    IF( PARA.EQ.'ITALIC'   ) NCHS = 2
     	    IF( PARA.EQ.'BOLDFACE' ) NCHS = 3
     	    IF( NCHS.EQ.0 ) GOTO 777
     	    IF( NCHS.NE.FONT ) THEN
     		FONT = NCHS
     		CALL IFFONT( FONT )
     	    ENDIF
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'RS' .OR. TEXT(1:NCHS).EQ.'RESET' ) THEN
     	    IF( PARA.NE.' ' ) THEN
     		NCHS = IFMISC_DECODE( PARA, ISPC, 2, RP, KP )
     		IF( NCHS.EQ.0 ) GOTO 777
     		IF( RP(1).LT.0.0 .OR. RP(1).GT.PWID-1.0 ) GOTO 777
     		IF( RP(2).LT.0.0 .OR. RP(2).GT.PHGT ) GOTO 777
     		XPOS = RP(1)
     		YPOS = RP(2)
     	    ELSE
     		XPOS = 0.0
     		YPOS = YOLD
     	    ENDIF
     	    GOTO 999
     	ENDIF

     	IF( PARA.EQ.' ' ) GOTO 888
     	ISPC = IFMISC_DECODE( PARA, ISPC, 5, RP, KP )
     	IF( ISPC.EQ.0 ) GOTO 777

     	IF( KEYW.EQ.'PS' .OR. TEXT(1:NCHS).EQ.'PAGESIZE' ) THEN
     	    IF( LINE.GT.0 ) GOTO 555
     	    IF( RP(1).LT.1.0 .OR. RP(1).GT.15.0 ) GOTO 777
     	    IF( RP(2).LT.1.0 .OR. RP(2).GT.15.0 ) GOTO 777
     	    PWID = RP(1)
     	    PHGT = RP(2)
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'SC' .OR. TEXT(1:NCHS).EQ.'SCALE' ) THEN
     	    IF( LINE.GT.0 ) GOTO 555
     	    IF( RP(1).LT.0.2 .OR. RP(1).GT.5.0 ) GOTO 777
     	    IF( RP(2).LT.0.2 .OR. RP(2).GT.5.0 ) GOTO 777
     	    XSCL = RP(1)
     	    YSCL = RP(2)
     	    CALL IFSCAL( XSCL, YSCL )
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'TM' .OR. TEXT(1:NCHS).EQ.'TOPMARGIN' ) THEN
     	    IF( LINE.GT.0 ) GOTO 555
     	    IF( RP(1).LT.0.0 .OR. RP(1).GT.4.0 ) GOTO 777
     	    TOPM = RP(1)
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'LM' .OR. TEXT(1:NCHS).EQ.'LEFTMARGIN' ) THEN
     	    IF( LINE.GT.0 ) GOTO 555
     	    IF( RP(1).LT.0.0 .OR. RP(1).GT.4.0 ) GOTO 777
     	    ODDM = RP(1)
     	    GOTO 999
     	ENDIF

     	IF( KEYW.EQ.'SP' .OR. TEXT(1:NCHS).EQ.'SPACING' ) THEN
     	    IF( RP(1).LE.0.0 .OR. RP(1).GT.1.0 ) GOTO 777
     	    SPAC = RP(1)
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'HS' .OR. TEXT(1:NCHS).EQ.'HSPACE' ) THEN
     	    IF( RP(1).LT.0.0 .OR. RP(1).GT.PWID-XPOS-1.0 ) GOTO 777
     	    XPOS = XPOS + RP(1)
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'VS' .OR. TEXT(1:NCHS).EQ.'VSPACE' ) THEN
     	    IF( RP(1).LT.0.0 .OR. RP(1).GT.PHGT-YPOS ) GOTO 777
     	    YPOS = YPOS + RP(1)
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'LI' .OR. TEXT(1:NCHS).EQ.'LINE' ) THEN
     	    IF( RP(1).LT.0.0 .OR. RP(1).GT.PWID ) GOTO 777
     	    IF( RP(2).LT.0.0 .OR. RP(2).GT.PHGT ) GOTO 777
     	    IF( RP(3).LT.0.0 .OR. RP(3).GT.PWID ) GOTO 777
     	    IF( RP(4).LT.0.0 .OR. RP(4).GT.PHGT ) GOTO 777
     	    IF( RP(5).GT.0.0 ) CALL IFSTYL( INT(RP(5)), 1 )
     	    XPS = XORG-ODDM
     	    YPS = YORG-TOPM-8.0
     	    CALL IFPLOT( RP(1)-XPS, RP(2)-YPS, 3 )
     	    CALL IFPLOT( RP(3)-XPS, RP(4)-YPS, 2 )
     	    IF( RP(5).GT.0.0 ) CALL IFSTYL( 1, 1 )
     	    GOTO 999
     	ENDIF
     	IF( KEYW.EQ.'SZ' .OR. TEXT(1:NCHS).EQ.'SIZE' ) THEN
     	    IF( RP(1).LT.0.05 .OR. RP(1).GT.5.0 ) GOTO 777
     	    IF( RP(2).LT.0.05 .OR. RP(2).GT.5.0 ) GOTO 777
     	    TWID = RP(1)
     	    THGT = RP(2)
     	    GOTO 999
     	ENDIF
C
C  OOPS...
C
666	NCHS = -1
     	WRITE(6,667) COMM(1:32)
667	FORMAT('_%Undefined command: ',A32)
     	GOTO 9999
777	NCHS = -1
     	WRITE(6,778) COMM(1:32)
778	FORMAT('_%Invalid/out_of_range parameter: ',A32)
     	GOTO 9999
888	NCHS = -1
     	WRITE(6,889) COMM(1:32)
889	FORMAT('_%Missing parameter in command: ',A32)
     	GOTO 9999
555	NCHS = -1
     	WRITE(6,556) COMM(1:32)
556	FORMAT('_%Illegal page setup from command: ',A32)

999	NCHS = 0
9999	TXTPLT_COMM = NCHS
     	RETURN
     	END

     	SUBROUTINE TXTPLT_DRAW( TEXT )
C
C	THIS ROUTINE DRAWS A LINE OF TEXT.
C
     	PARAMETER (XORG= 5.0, YORG= 4.0)
     	INTEGER TPEN, FONT, CENT
     	COMMON/TXTPLT_SETUP/ PWID, PHGT, TOPM, ODDM, LINE,
     +  XPOS, YPOS, SPAC, TWID, THGT, TPEN, CENT, FONT,
     +  IPAG, JPAG, XCN1, XCN2, XSCL, YSCL, YOLD
     	CHARACTER TEXT*(*)

     	XPS = XORG-ODDM
     	YPS = 8.0-YPOS-TOPM-YORG
     	IF( CENT.EQ.0 ) THEN
     	    CALL IFTEXT_EXT(XPOS-XPS,YPS,TEXT,TWID,THGT,0.0)
     	ELSE
     	    CALL IFTEXT_CENT(XCN1-XPS,XCN2-XPS,YPS,YPS,TEXT,TWID,THGT)
     	ENDIF

     	RETURN
     	END

