program ThreePage;	{ Fast hack to print three landscape-columns per sheet
			  on my HP-DeskJet 510.
			  Plain AscII only, but you might end columns with lines
			  starting with ^L (anything after it is ignored!).
			  Beware of ^L's snd other Esc-likes somewhere inside lines!
			  Might work on other HP-Series printer...
			  © Copyright 1993 by Henning Peters
			  E-Mail: h08f@alf.zfn.uni-bremen.de }

{$I "include:utils/stringlib.i"}
{$I "include:utils/break.i"}

const spaces:string="                                                                                    ";	{ 84 Spaces }

var datei,drucker:text;
    buffer:array[1..3,1..75]of string;
    sp,zl,psp,pzl,page,len,pr,fill,i:integer;
    line,name:string;

procedure break;
begin
  write(drucker,'');
  close(drucker);
  close(datei);
  writeln("\a\n\n*** BREAK ***\n");
  exit(5);
end;

begin
  for sp:=1 to 3 do for zl:=1 to 75 do buffer[sp,zl]:=AllocString(85);
  line:=AllocString(280); name:=AllocString(120);
  write("\f		\e[4mPrint-3-Columns\e[0m  for HP-DeskJet 510\n\n	© Copyright and written 15.7.93 by Henning Peters\n\nFile: "); readln(name);
  if reopen(name,datei) then begin page:=1;
    if open("par:",drucker) then begin
      write("\nInitializing printer - ");
      write(drucker,"\e&l1o10d80P\e&k2G\e(s24h6v6T\e(0N");
	{ Landscape, 10 Lines/Inch, 75 Lines/Page (the "80" is correct! Strange, eh?),
	  use right Line-End, 24 Chars/Inch, 6 Pt Char-Height, Letter-Gothic-Font,
	  ECMA-94 Latin 1 Charset }
      writeln("Ok.\n");
      while not eof(datei) do begin
	write("Page ",page,": reading"); sp:=1;
	repeat
	  write('.'); zl:=1;
	  buffer[1,75][0]:='\0'; buffer[2,75][0]:='\0'; buffer[3,75][0]:='\0';
		{ Empty last lines }
	  repeat
	    if checkbreak then break;
	    readln(datei,line);
	    if line[0]='' then begin
	      fill:=zl; zl:=80;
	    end else begin
	      strncpy(buffer[sp,zl],line,80);
	      if sp<3 then begin
		len:=strlen(buffer[sp,zl]); strncat(buffer[sp,zl],spaces,84-len);
	      end;
	      inc(zl);
	    end;
	  until (zl>75) or eof(datei);
	  if (not eof(datei)) and (sp<3) and (zl=80) then
	    for pr:=fill to 75 do strcpy(buffer[sp,pr],spaces);
		{ Column aborted with ^L; this is nessecary if ie. col#2 is longer
		  than col#1 }
	  if eof(datei) or (sp=3) then begin
	    len:=strlen(buffer[sp,75]); strncat(buffer[75,zl],spaces,84-len);
	  end;	{ Fill up last line on page to place page# }
	  inc(sp); dec(zl);
	until (sp>3) or eof(datei);
	write(" printing"); dec(sp);
	if (sp=1) and (zl<75) then len:=zl else len:=75;
	for pzl:=1 to len do begin
	  if pzl mod 25=0 then write('.');
	  psp:=sp; if pzl>zl then dec(psp);
	  if checkbreak then break;
	  for pr:=1 to psp do
	    write(drucker,buffer[pr,pzl]);
	  if pzl=len then begin
	    if len<75 then begin
	      for i:=len to 74 do writeln(drucker);
	      fill:=262;
	    end else fill:=10+84*(3-psp);
		{ Fill up to very last line, rightmost row }
	    write(drucker,page:fill,''); 	{ Print page# at lower-right }
	  end else writeln(drucker);
	end;
	writeln("Ok."); inc(page);
      end; {while}
      writeln("\nDone.\n");
      close(drucker);
    end else begin
      writeln("\n\aCan't access `par:'!\n"); exit(10);
    end;
    close(datei);
  end else begin
    writeln("\n\aCan't access file `",name,"'!\n"); exit(10);
  end;
end.

