summaryrefslogtreecommitdiff
path: root/support/lametex/src/plaintext.pl
blob: bf6230614f6da6c2c668f302db29af81bf9ccacf (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/local/bin/perl -P
# This program converts a PostScript file written by lametex -t and converts it
# into a plain text file.
#
# Copyright 1992 Jonathan Monsarrat. Permission given to freely distribute,
# edit and use as long as this copyright statement remains intact.

# Cycle through all the input, breaking everything apart into word. Interpret
# each word, building together a line of word and accumulating information
# about the margins, line spacing, indentation, and justification.
$justify = 102;
$firstpage=1;
while(<>)
{
  split;    # Breaks this line up into tokens by whitespace.
  foreach (@_) {
     if(/^def$/) { &FLUSHLINE; &DEFINE; }
     elsif(/^NW$/) { &NEXTWORD; }
     elsif(/^NEWPARA$/) { &NEWPARA; }
     elsif(/^BASELINESKIP$/) { &FLUSHLINE; $baselineskip = $last; }
     elsif(/^NewFont$/) { if ($lastlast eq "true") { $glue = 1;} }
     elsif(/^ENDPAGE/) { &FLUSHLINE;}
     elsif(/^STARTPAGE/) {
            if($firstpage==1) { $firstpage=0; }
            else { printf("\n\014\n");} }
     elsif(/^BULLET/) { $bullet=1; }
     elsif(/^ENUMERATE/) { 
           $_ = substr($last,1,length($last)-2);  # Take off parenthesis.
           s/\\\\/"/;
           s/\\\(/\(/;
           s/\\\)/\)/;
           $enum = $_; }

    
     $lastlast=$last;
     $last=$_;
  }
}

&FLUSHLINE;   # Don't forget to print the last line!

###############################################
# Handle the "def" keyword.
sub DEFINE
{
  $lastlast = substr($lastlast,1);
  eval("\$$lastlast = $last");
}

###############################################
# Handle the next word to be printed.
sub NEXTWORD
{
  $_ = $last;
  if($last =~ /^\((\\\\)\)$/) {
     $_ = "\\";
  } else {
     $_ = substr($_,1,length($_)-2);  # Take off parenthesis
  }
  s/\\\\/\"/;
  s/\\\(/\(/;
  s/\\\)/\)/;
  $word = $_;
  if(length($word) + $linelength + 1 >
      80 - int($leftmargin/9) - int($rightmargin/9)) {
    &PRINTLINE($justify);
  }
  if($glue) {
    $glue = pop(@line);
    push(@line, $glue.$word);
    $glue = 0;
  } else {
    push(@line, $word);
  }
  $linelength += length($word)+1;
}

###############################################
# Forces a print of the world list before margin space filled
sub FLUSHLINE
{
  if($linelength > 0) {
     if($justify == 102) {
        &PRINTLINE(108);     # Don't fully justify this line.
     } else {
       &PRINTLINE($justify);
     }
  }
}

###############################################
# Make a new paragraph
sub NEWPARA
{
  &FLUSHLINE;
  if($vspace > 0) {
     for($x=0; $x < $vspace/14; $x++) {
        print "\n";
      }
     $vspace = 0;
     if($para > 0) {
        $leftmargin -= $para;
        $para = 0;
     }
  } else {
     for($x=0; $x < $parskip/14; $x++) {
        print "\n";
     }
    if($justify != 99) {
       $para=$parindent;
       $leftmargin += $para;
    }
  }
}

###############################################
# Print the current line to stdout with the given justification.
sub PRINTLINE {
   $justification = shift;

   for($x=0; $x < int($leftmargin/9) - length($enum) - 1;
	   $x++) {
     if($bullet && $x == int($leftmargin/9) - 2) {
        print "*";
        $bullet=0;
     } else {
        print " ";
     }
   }
   if($enum) {
     print $enum;
     $enum="";
   }
   print " ";
   if($justification == 114) {     # flushright
      for($x=0; $x <
           80 - int($rightmargin/9) - int($leftmargin/9) - $linelength; $x++) {
         print " ";
      }
   } elsif($justification == 99) { # centered
      for($x=0;$x <
           (80 - int($rightmargin/9) - int($leftmargin/9) - $linelength)/2;
            $x++) {
         print " ";
      }
   }

   $slack = 80 - int($leftmargin/9) - int($rightmargin/9) - $linelength;
   $spaces = 1;
   $count = 0;
   foreach (@line) {
      $count++;                      # fully justified?
      if($justification == 102 && $slack > 0 && $count > 1) {
          while($count > $spaces / ($slack+1) * ($#line + 1)) {
             print " ";
             $spaces++;
          }
      }
      print;
      print " ";
  }

  for($x=0; $x < $baselineskip/14; $x++) {
     print "\n";
  }

  if($para > 0) {
    $leftmargin -= $para;
    $para = 0;
  }

  $linelength = 0;   # Empty the current line variable.
  $#line = -1;
}