Program HP_Laserjet_Raster;
uses FDelay,Crt,dos;
const
  FF = 255;
  Esc = #27;
var
  nul : byte;
  key : char;

  BegZero: LongInt; {Start Zero fill}
  Xp,Yp,RasBuf,ConBuf : byte;
  cont : LongInt;
  buf : char;
  bufn : byte;
  nbuf : string[3];
  bufarr : byte;
  inf  : file of char;
  outf : file of char;
  D: DirStr;
  N: NameStr;
  E: ExtStr;
(****************************************************************************)
{............................................................................}
{............................................................................}
Procedure PCL(proh:boolean);
var
  numarr,res : integer;
  cy : byte;
begin
  read(inf,buf);
  if buf='*' then
  begin
    read(inf,buf);
    if buf='b' then
    begin;
      nbuf:='';
      for cy:=1 to 3 do
      begin
        read(inf,buf);
        nbuf:=nbuf+buf;
      end;
      read(inf,buf);
      if buf='W' then
      begin
        Val(nbuf,numarr,res);
        {
          GoToXY(10,10);
          write('--------------------------------');
          Delay(500);
          GoToXY(10,10);
          write('result ',res:3,'<->',nbuf,'<->');
        }
        if res=0 then
        begin
          bufarr:=0;
          for res:=1 to numarr do
          begin
            read(inf,buf);
            bufarr:=bufarr or (Ord(buf));
          end;
          if bufarr<>0 then BegZero:=FilePos(inf);
          inc(cont);
          GoToXY(Xp,Yp);
          write(cont:6);
        end;
      end;
    end;
  end;
end;
{............................................................................}
(****************************************************************************)
BEGIN
  BegZero:=0;
  cont:=0;
  nul:=0;
  Write('Produced by ');
  TextColor(Yellow); TextBackGround(Red); Write(' P.E.co.');
  NormVideo; WriteLn('  08.04.01  Alchevsk, Ukraine.');
  Write('Homepage : ');
  TextColor(LightGreen);
  WriteLn(' http://pop.alterkch.ru , http://www.geocities.com/pop_eco');
  NormVideo;
  if ParamCount=0 then
  begin
    writeln('OrCAD PCL or WIN95/98 HP LaserJet Series II file to Alchemy patch & optimizer ');
    writeln;
    writeln('Usage: PCLFIX.EXE _input_file_');
    writeln('Output file be with the same name and *.PCL extension.');
    Halt;
  end;
  Assign(inf,ParamStr(1));
  reset(inf);
  FSplit(ParamStr(1), D, N, E);
  writeln;
  writeln('Analyze ...');
  Xp:=WhereX;
  Yp:=WhereY;
  write('0');
  while FilePos(inf) < FileSize(inf) do
  begin
    read(inf,buf);
    if buf=Esc then PCL(True);
  end;
  writeln(' strings.');
  if cont=0 then
  begin
    TextColor(LightRed);
    writeln('I understand only 300DPI OrCAD HP Printer or WIN95/98 HP LaserJet Series II !!!');
  end else
  begin
    Seek(inf,0);
    Assign(outf,N+'.pcl');
    rewrite(outf);
    buf:=Esc;
    write(outf,buf);
    buf:='E';
    write(outf,buf);
    writeln('Write...');
    for cont:=0 to BegZero do
    begin
      Read(inf,buf);
      write(outf,buf);
      GoToXY(Xp,Yp);
      write(cont:6);
    end;
    Close(outf);
  end;
  Close(inf);
  TextColor(LightGray);
  Writeln;
  Writeln;
  writeln('O''k.');
END.
