#!/usr/bin/perl
# text2xfig.pl - A Perl program for translating plain text to Xfig format
# http://www.cs.dal.ca/~vlado/srcperl/text2xfig.pl
# Copyright 2007 Vlado Keselj http://www.cs.dal.ca/~vlado

# Limitations: Probably does not work for all characters.
# $Id: text2xfig.pl,v 1.2 2007/06/19 10:05:27 vlado Exp $
$Version = sprintf "%d.%d", q$Revision: 1.2 $ =~ /(\d+)/g;

my $input = join('',<>);
print &preamble;

my $row=1, $col=1;
$_ = $input;
while ($_ ne '') {		# find sequences of non-space and put them
    if (/^\n/) { $_=$'; $row++; $col=1; }
    elsif (/^(\S+)/) { $_=$'; print &print_text($&, r=>$row, c=>$col); $col+=length($&); }
    elsif (/^( +)/) { $_=$'; $col+=length($&); }
    elsif (/^\t/) { $_=$'; $col++; $col++ while ($col%8 != 0); }
    else { die ">>>$_" }
}
exit(0); # END RUN

sub print_text {
    my $text = shift;
    my %p = @_;
    my $c = (exists($p{c}) ? $p{c} : 1); # column position
    my $x = int(0.5+($c-1)*91.2+1500);
    my $r = (exists($p{r}) ? $p{r} : 1); # row position
    my $y = ($r-1)*165+1350; #225 for 12pt, 165 for 10pt
    my $fontsize = 10;
    my $r = "4 0 0 50 -1";
    $r.= " 12"; # font family, 0=Roman 12=Courier
    $r.= " $fontsize 0.0000 4 135";
    $r.= " ".(105*length($text)); # 90*number of chars?, 105 for fixed

    $text =~ s/\\/\\\\/g;
    $r.=" $x $y $text\\001\n";
    return $r;
}

sub preamble {
    my $r = << 'EOT';
#FIG 3.2
Portrait
Center
Inches
Letter  
100.00
Single
-2
1200 2
EOT
return $r;
}

__END__
=head1 NAME

text2xfig.pl - Translate plain text to Xfig format, word-by-word

=head1 SYNOPIS

  text2xfig.pl in.txt > out.fig

=head1 DESCRIPTION

Translating plain text into Xfig format.  Each word is positioned
separately.  Initial stage.  There could be bugs.

=head1 PREREQUISITES

No prerequisites, but you need xfig to see and edit the output, and to
produce other formats.

=head1 SCRIPT CATEGORIES

Xfig
Misc

=head1 COPYRIGHT

Copyright 2007 Vlado Keselj F<http://www.cs.dal.ca/~vlado>

This script is provided "as is" without expressed or implied warranty.
This is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

The latest version can be found at F<http://www.cs.dal.ca/~vlado/srcperl/>.

=cut
