Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing a 'grid overlay' on a text file

Status
Not open for further replies.

Hemo

Programmer
Apr 9, 2003
190
US
Thought I'd share this snippet of code I wrote to print out a text file with a grid overlay.

I frequently use this when I am required to 'process' text files and need to know which line or what columns some text appears.

Hope it is useful for someone else as well.

Code:
#!/usr/bin/perl -w
#############################################################################
#                 Copyright (C) John Hiemenz, 2003-2004                     #
#                                                                           #
# This program is free software; you can redistribute it and/or modify it   #
# under the terms of the GNU General Public License as published by the     #
# Free Software Foundation; either version 2 of the License, or (at your    #
# option) any later version.                                                #
#                                                                           #
# This program is distributed in the hope that it will be useful, but       #
# WITHOUT ANY WARRANTY; without even the implied warranty of                #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General  #
# Public License for more details.                                          #
#                                                                           #
# You should have received a copy of the GNU General Public License along   #
# with this program; if not, write to:                                      #
#                      Free Software Foundation, Inc.                       #
#                      59 Temple Place - Suite 330                          #
#                      Boston, MA  02111-1307, USA.                         #
#                                                                           #
# Or visit this URL:  [URL unfurl="true"]http://www.gnu.org/licenses/licenses.html#TOCGPL[/URL]      #
#                                                                           #
#############################################################################
#                                                                           #
# Module:             psgrid.pl                                             #
# Version:            0.9.5                                                 #
#                                                                           #
#    Description:                                                           #
#                 A Perl filter script for encompassing text files in a     #
#                 PostScript grid overlay suitable for output to file or    #
#                 printer.  The output grid shows line and column position  #
#                 of all text, aiding developers when processing text files #
#                 and needing this information for reference.               #
#                                                                           #
#    Usage:                                                                 #
#           This filter takes input from STDIN and sends output to STDOUT   #
#           so it can be implemented in a number of ways and methods.       #
#           psgrid.pl filename | lp   (grids filename and sends to printer) #
#           psgrid.pl filename > grid.sp (sends output to file named grid.ps#
#                                                                           #
#    Modification History:                                                  #
#                                                                           #
#    mm/dd/yy     programmer     description                                #
#    12/19/03     Hemo           Created, tested. Possible limitation of    #
#                                999 lines in input file. 156 colum width   #
#                                current limitation.                        #
#                                                                           #
#############################################################################
#                                                                           #
#===========================================================================#

use strict;

# Just initializing some variables to keep 'use strict' happy
my $FORM_TITLE = "Text to PostScript Grid";
my $VERSION    = "0.9.5";

our ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime;

our @JobData = ();

###
# main()
###

&process_datastream;

&output_header;
&create_grid;

print "\n";
print "GRID\n";
&output_data;
print "showpage PR\n";

close();

###
# End of Program Execution
###

################################
#####                      #####
######### Subroutines ##########
#####                      #####
################################

sub output_header {
    print "%!PS-Adobe\n";
    print "%%Title: $FORM_TITLE\n";
    print "%%Creator: psgrid.pl\n";
    print "%%Author: John Hiemenz\n";
    print "%%Copyright 2003\n";
    print "%%LanguageLevel: 2\n";
    print "%%Version: $VERSION\n";
    print "%%Date Printed: "
      . ( $mon + 1 ) . "/"
      . ($mday) . "/"
      . ( $year + 1900 ) . "\n";
    print "%%Time Printed: " . ($hour) . ":" . ($min) . "\n";
    print "%%EndComments\n";
    print "/PS {/Pd save def} bind def\n";
    print "/PR {Pd restore} bind def\n";
    print "/SNP {pop pop} bind def\n";
    print "/LN {setlinewidth newpath moveto rlineto stroke} bind def\n";
    print "/FT {findfont exch makefont def} bind def\n";
    print "/MS {moveto show} bind def\n";
    print "/LPT [6.00 0 0 8.00 0 0] /Courier-Bold FT\n";
    print "/#copies 1 def\n";
    print "{letter} stopped pop\n";
    print "\n";
}

sub create_grid {
    my $i = 0;
    print "/GRID {\n";
    print "PS 612 792 SNP\n";
    print "LPT setfont\n";
    print "566.4 0 27.6 756 0.72 LN\n";
    print "566.4 0 27.6 29.52 0.72 LN\n";
    print "0 -727.2 27.6 756 0.72 LN\n";
    print "0 -727.2 593.28 756 0.72 LN\n";

    for ( $i = 37.92 ; $i <= 756 ; $i = $i + 16.32 ) {
        print &quot;566.4 0 27.6 $i 0.24 LN\n&quot;;
    }

    print &quot;22.32 768 moveto\n&quot;;

    for ( $i = 0 ; $i <= 15 ; $i++ ) {
        if ( $i < 10 ) {
            print &quot;( $i        ) show\n&quot;;
        }
        else {
            print &quot;($i        ) show\n&quot;;
        }
    }

    print &quot;22.32 758.4 moveto\n&quot;;

    for ( $i = 1 ; $i <= 15 ; $i++ ) {
        print &quot;( 0 2 4 6 8) show\n&quot;;
    }

    print &quot;( 0 2 4 6) show\n&quot;;

    for ( $i = 28.08 ; $i <= 589.68 ; $i = $i + 14.4 ) {
        print &quot;0 -727.2 $i 756 0.24 LN\n&quot;;
    }

    print &quot;} bind def\n&quot;;
}

sub output_data {
    my $i    = 0;
    my $x    = 11.88;
    my $yy   = 744.96;
    my $y    = $yy;
    my $step = 8.16;

    for ( $i = 1 ; $i <= $#JobData ; $i++ ) {
        print &quot;$x $y moveto\n&quot;;
        if ( $i < 10 ) {
            print &quot;(   $i &quot; . $JobData[$i] . &quot;) show\n&quot;;
        }
        elsif ( $i < 100 ) {
            print &quot;(  $i &quot; . $JobData[$i] . &quot;) show\n&quot;;
        }
        elsif ( $i >= 100 ) {
            print &quot;( $i &quot; . $JobData[$i] . &quot;) show\n&quot;;
        }
        if ( ( $i % 88 ) eq (0) ) {
            print &quot;showpage PR\n&quot;;
            print &quot;GRID\n&quot;;
            $y = $yy;
        }
        else {
            $y = $y - $step;
        }
    }
}

sub process_datastream {

    @JobData = (&quot;Ignore Element 0&quot;);

    while (<>) {
        push @JobData, $_;
    }
    chomp @JobData;
}
###
# End of File
###
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top