summaryrefslogtreecommitdiff
path: root/dviware/epson/eps-0.2/eps/epson.c
blob: 8963e0baebd5e8a7081be9fcf057b230be587e79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
static const char rcsid[] = "$Header: c:/mctex/imagen/rcs/epson.c 1.1 91/08/05 23:40:37 ROOT_DOS Exp Locker: ROOT_DOS $";

/* Copyright (c) 1993 Neal Becker
 * All rights reserved.
 * Permission to copy for any purpose is hereby granted
 * so long as this copyright notice remains intact.
*/

/*
$Log:	epson.c $
 * Revision 1.1  91/08/05  23:40:37  ROOT_DOS
 * Initial revision
 * 
*/

#include "epson.h"


#if defined( __MSDOS__ ) && !defined( __GNUC__ )
#  include <mem.h>
#endif /* __MSDOS__ */

#include <stdlib.h>
#include "PROTO.h"

BitMapRow::BitMapRow() {
  if( ( Bits = new char[ HBytes ] ) == 0 )
    GripeOutOfMemory( HBytes, "BitMap" );
  Printed = Length = 0;
  Start = MaxShort;
}

void BitMapRow::Clear() {
  memset( Bits, 0, HBytes );
  Printed = Length = 0;
  Start = MaxShort;
}
  
BitMap::BitMap() {
  for( int i = 0; i < ( BitMapRows + ExtraRows ); i++ ) {
    if( ( Rows[ i ] = new BitMapRow ) == NULL )
      GripeOutOfMemory( sizeof( BitMapRow ), "BitMap" );
  }
}

void BitMap::Clear() {
  for( int i = 0; i < ( BitMapRows + ExtraRows ); i++ )
    Rows[ i ]->Clear();
}

#if defined __MSDOS__ && !defined( __GNUC__ )

#include <alloc.h>
void ReportMem() {
  fprintf( stderr,"%lX Bytes Left\n", (unsigned long)farcoreleft() );
}

#else /* __MSDOS__ */

void ReportMem() {}

#endif /* __MSDOS__ */

void epson::PrintBitMap( BitMap& theBitMap ) {
  for( DviPos = 0; DviPos < BitMapRows; DviPos ++ ) {
    if( theBitMap.NeedToPrint( DviPos ) )
      PrintRow( theBitMap, DviPos );
  }
  if( ferror( fp ) )
    panic( "error writing output" );
}

void epson::PrintRow( BitMap& theBitMap, int Row ) {
  SetVpos( Row );
  Output8Rows( theBitMap, Row );
};

void epson::Output8Rows( BitMap& theBitMap, int Row ) {
  int FirstBytePos = theBitMap.FindFirst( Row );
  int RealSpaces = FirstBytePos / 3;
  int TotalSpaces = RealSpaces + LeftMarginSpaces;
  int HByte = 0;			/* current byte pos */
  if( TotalSpaces > 0 ) {
    if( HasFastSpace ) {
      fputs( "\x1b" "f0", fp );
      fputc( TotalSpaces, fp );
    }
    else {
      for( int i = 0; i < TotalSpaces; i++ )
        fputc( ' ', fp );
    }
    HByte += RealSpaces * 3;
  }
  int LastBytePos = theBitMap.FindLength( Row );
  int NumBits = 8 * ( LastBytePos - HByte + 1 );
  char n1 = NumBits & 0xff;
  char n2 = (unsigned)(NumBits) >> 8;
  fputs( "\x1b" "*" "\x3", fp );
  fputc( n1, fp );
  fputc( n2, fp );
  char Bytes[ 8 ];
  for( ; HByte <= LastBytePos; HByte++ ) {
    Encode8Bytes( theBitMap, Row, HByte, Bytes );
    for( int HBit = 7; HBit >= 0; HBit-- )
      fputc( Bytes[ HBit ], fp );
  }
  Eol();
  for( int V = 0; V < 8; V++ ) {
    int TheRow = Row + ( V * 3 );
    if( TheRow >= BitMapRows ) break;
    theBitMap.Mark( TheRow );
  }
}


void epson::Encode8Bytes( BitMap& theBitMap, int Row, int HByte, char Bytes[ 8 ] ) {
  memset( Bytes, 0, 8 );
  for( int VBit = 0; VBit < 8; VBit++ ) {
    int TheRow = Row + ( VBit * 3 );
    if( TheRow >= BitMapRows ) break;
    char TheChar = theBitMap.Rows[ TheRow ]->Bits[ HByte ];
    char TheMask = BitMask[ 7 - VBit ];
    for( int HBit = 0; HBit <= 7; HBit++ )
      if( TheChar & BitMask[ HBit ] )
        Bytes[ HBit ] |= TheMask;
  }
}

int BitMap::FindLength( int Row ) {
  for( int i = 0, l = 0; i < 8; i++ ) {
    int TheRow = Row + ( i * 3 );
    if( TheRow >= BitMapRows ) break;
    l = max( l, Rows[ TheRow ]->Length );
  }
  return l;
}

int BitMap::FindFirst( int Row ) {
  for( int i = 0, l = 0xff; i < 8; i++ ) {
    int TheRow = Row + ( i * 3 );
    if( TheRow >= BitMapRows ) break;
    l = min( l, Rows[ TheRow ]->Start );
  }
  return l;
}

void epson::SetVpos( int n ) {
  while( n - PrintPos > 255 )
    Vaddv( 255 );
  Vaddv( n - PrintPos );
}

void epson::Vaddv( int n ) {
  fputs( "\x1b" "J", fp );
  fputc( n, fp );
  PrintPos += n;
}