{************************************************}
{                                                }
{        PlotterDriver                           }
{        PLOTTER.INC                             }
{                                                }
{************************************************}


Overlay Procedure PlotterDriver(Var Par
                                    :Parameters);

Type
  Blocks=Array [ 0..1023 ] Of Byte;
  Buffer=Record
             Upd : Boolean;
             Ind : Byte;
             Blk : Blocks;
           End;

Var
  Buffers : Array [ 0..1 ] Of Buffer;
  Prev : Byte;

Function Block (N : Byte) : Byte;
Begin
  If (Buffers[0].Ind<>N) And (Buffers[1].Ind<>N)
    Then
      Begin
        With Buffers [ Prev ] Do
          Begin
            If Upd
              Then
                Begin
                  Seek (BitMap,Ind Shl 3);
                  BlockWrite (BitMap,Blk,8);
                End;
            Upd:=False; Ind:=N;
            Seek (BitMap,Ind Shl 3);
            BlockRead (BitMap,Blk,8);
          End;
        Block:=Prev;
        If Prev=0 Then Prev:=1 Else Prev:=0;
      End
    Else
      If Buffers[0].Ind=N Then Block:=0
         Else Block:=1;
End;


Procedure PlotterStep (X,Y :Integer; Mode :Tools);
Begin
  If Tool=Pen
    Then Write (Aux,'D')
    Else Write (Aux,'M');
  WriteLn (Aux,X,',',Y - 719);
End;

Function Open : Boolean;
Var I : Integer;
Begin
  Assign (BitMap,'BITMAP.PIC');
  (*$I-*) Reset (BitMap) (*$I+*);
  If Ioresult=0
    Then
      Begin
        Write (Aux,#10,#27,'b');
        Device:=Plotter;
        Xmin:=0; Xmax:=959;
        Ymin:=0; Ymax:=719;
        For I:=0 To 1 Do
          With Buffers [I] Do
            Begin Upd:=False; Ind:=255 End;
        Prev:=0;
        Open:=True;
      End
    Else
      Open:=False;
End;

Procedure Calculate (X,Y: Integer;
                     Var Bit: Byte;
                     Var Adr,Blk: Integer);
Begin
  Bit:=X And 7;
  Adr:=X Shr 3+Y And 7 Shl 7;
  Blk:=Y Shr 3;
End;

Function Point (X,Y : Integer) : Boolean;
Var
  Bit : Byte;
  Adr,Bl : Integer;
Begin
  If (X>=Xmin) And (X<=Xmax) And (Y>=Ymin)
               And (Y<=Ymax)
    Then
      Begin
        Calculate (X,Y,Bit,Adr,Bl);
        If Buffers[Block(Bl)].Blk[Adr]
           And Pat[Bit]<>0
          Then Point:=True
          Else Point:=False;
      End
    Else Fence;
End;

Var
  Xprev,Yprev : Integer;

Function Near (X,Y : Integer) : Boolean;
Begin
  If (Abs(X-Xprev)<2) And (Abs(Y-Yprev)<2)
    Then Near:=True
    Else Near:=False
End;

Procedure Dot (X,Y : Integer);
Var
  Bit : Byte;
  Adr,Bl : Integer;
Begin
  Control (X,Y);
  Calculate (X,Y,Bit,Adr,Bl);
  With Buffers [ Block (Bl) ] Do
    If Tool=Pen Then
      Begin
        Blk [ Adr ]:=Blk [ Adr ] Or Pat [ Bit ];
        Upd:=True;
      End;
  If Near (X,Y)
    Then PlotterStep (X,Y,Tool)
    Else
      Begin
        PlotterStep (X,Y,None);
        PlotterStep (X+1,Y,Tool);
        PlotterStep (X,Y,Tool);
      End;
  Xprev:=X; Yprev:=Y;
End;

Procedure Plot (X,Y : Integer);
Begin
  Control (X,Y);
  PlotterStep (X,Y,None);
  Position.X:=X; Position.Y:=Y;
End;

Procedure Clear;
Var
  I : Integer;
  Bl : Blocks;
Begin
  For I:=0 To 1023 Do Bl [I]:=0;
  Reset (Bitmap);
  For I:=0 To 89 Do
    Begin
      Seek (BitMap,I Shl 3);
      BlockWrite (BitMap,Bl,8);
    End;
End;

Begin
  With Par Do
    Case Command Of
      OpenF  : Result:=Open;
      PointF : Result:=Point (X,Y);
      DotF   : Dot (X,Y);
      PlotF  : Plot (X,Y);
      ClearF : Clear;
      CloseF : Close (BitMap);
    End;
End;

