#!/usr/bin/perl # Converts raw DOTS file to BBXML format. Use to post-process the # output of bbXpmToDots. # # Author: Darin Franklin # http://pobox.com/~dfranklin/bbxml/ use strict; use FileHandle; # The input file contains the DOTS image. Each pixel is identified # with a number, 0-8. # 710700000007755171547757000775511700000707 # 700000000001070007774770077177000000007177 # 770000007707570007746710777747070077777771 # ... use Getopt::Long; my $outdir = '.'; my $dotsLabel = 'A'; my $textLabel = 'A'; my $height = 7; GetOptions( "output=s" => \$outdir, "dotsLabel=s" => \$dotsLabel, "textLabel=s" => \$textLabel, "height=i" => \$height, ); mkdir $outdir unless -e $outdir; my @labels; my ($width) = printDOTS(\@labels); printTEXT(); printMemoryConfig(\@labels, $width); my @stack; sub unreadLine { push @stack, shift; } sub readLine { return @stack ? pop @stack : <>; } # print the dots files sub printDOTS { my ($labels) = @_; my $label = $dotsLabel; my $fh; my $width; while (<>) { chomp; next unless $width = length($_); # start new file if (($. == 1) || (($. - 1) % $height == 0)) { push @$labels, $label; $fh = FileHandle->new; my $fn = "$outdir/dots.$label.xml"; print "Writing $fn\n"; $fh->open(">$fn") or die $@; print $fh qq(\n); print $fh qq( \n); $label++; } print $fh qq($_\n); # end this file if (($. % $height == 0)) { print $fh qq( \n); print $fh qq(\n); $fh->close(); } } # finish the last one # pad the last dots file my $j = ($height - $. % $height) % $height; while ($j--) { print $fh qq() . '0' x $width . qq(\n); } print $fh qq( \n); print $fh qq(\n); $fh->close(); return ($width); } # print the TEXT file setup sub printTEXT { my $label = $textLabel; my $fh = FileHandle->new; my $fn = "$outdir/text." . getLabels($label, 2) . ".xml"; print "Writing $fn\n"; $fh->open(">$fn") or die $@; print $fh qq(\n); print $fh qq( \n); print $fh qq( \n); foreach (@labels) { print $fh qq( \n); } print $fh qq( \n); $label++; print $fh qq( \n); print $fh qq( \n); foreach (reverse @labels) { print $fh qq( \n); } print $fh qq( \n); print $fh qq( \n); print $fh qq(\n); $fh->close; } # print the memoryConfig commands sub printMemoryConfig { my ($labels, $width) = @_; my $fh = FileHandle->new; my $fn = "$outdir/0_memoryConfig." . (join "", @$labels) . ".xml"; print "Writing $fn\n"; $fh->open(">$fn") or die $@; print $fh qq(\n); print $fh qq( \n); # text my $label = $textLabel; my $size = 10 + 2 * @$labels; print $fh qq( \n); $label++; print $fh qq( \n); # dots foreach (@$labels) { print $fh qq( \n); } print $fh qq( \n); print $fh qq(\n); $fh->close; } sub getLabels { my ($start, $len) = @_; my $label = ''; $label .= $start++ while ($len--); return $label; }