{************************************************}
{                                                }
{        PrinterDriver                           }
{        PRINTER.INC                             }
{                                                }
{************************************************}


Overlay Procedure PrinterDriver
                  (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;

Function Open : Boolean;
Var I : Integer;
Begin
  Assign (BitMap, 'BITMAP.PIC');
  (*$I-*) Reset (BitMap) (*$I+*);
  If Ioresult = 0
    Then
      Begin
        Device := Printer;
        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
  Y := 719 - Y;
  Bit := Y And 14 Shr 1;
  Adr := X;
  Blk := Y Shr 3 And $FFFE + Y And 1;
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;

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;
End;

Procedure Plot (X, Y : Integer);
Begin
  Control (X, Y);
  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;

Procedure Print;
Var
  I : Byte;
Procedure PrintRow (N : Byte);
Var I : Integer;
Begin
  Write (Lst, #27, 'L', #192, #3);
  For I := 0 To 959 Do
    Write (Lst, Chr (Buffers [N].Blk [I]));
End;
Begin
  For I := 0 To 44 Do
    Begin
      PrintRow (Block (I Shl 1));
      Write (Lst, #27, 'J', #1, #13);
      PrintRow (Block (I Shl 1 + 1));
      Write (Lst, #27, 'J', #23, #13);
    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 : Begin Print; Close (BitMap) End;
    End;
End;


