#! /usr/bin/perl -w
#
# Testing dvi2bitmap specials
#
# Create a TeX file with strut and mark specials in it, use
# --query=bitmaps, and check that the correct stuff is reported.

sub compare_bitmaps($$\@);

$texbin='@TEX@';
if ($texbin eq 'NO_TEX') {
    print STDERR "No TeX binary available.  Can't run t8, so assume success\n";
    exit 0;
}

$prefix = 'temp-t8';
$texfile = "$prefix.tex";
$m = 2;				# magnification factor

open (TEX, ">$texfile") || die "Can't open $texfile for output";
$newpage = "\n\n\\vfill\\eject\n\n";

$nerrors = 0;

@expected = ();

print TEX <<'EOD';
%\hoffset=0pt
%\voffset=0pt
%\leftskip=0pt % default
%\topskip=0pt
%\parindent=0pt
%\parskip=0pt
\nopagenumbers


EOD

print TEX "\\special{dvi2bitmap default unit bp}\n\n";

print TEX '\vrule width 20bp height 10bp';
print TEX $newpage;
push(@expected, [20*$m, 10*$m]);


print TEX '\vrule width 20bp height 10bp \special{dvi2bitmap strut 10 4 5 6}';
print TEX $newpage;
push(@expected, [24*$m, 16*$m]);

print TEX '\vrule width 10bp height 10bp \special{dvi2bitmap strut 12 0 15 0}';
print TEX $newpage;
push(@expected, [12*$m, 15*$m]);

print TEX '\vrule width 20bp height 10bp \special{dvi2bitmap mark}';
print TEX $newpage;
push(@expected, [20*$m, 10*$m, 20*$m, 10*$m]);

print TEX "\\bye\n";

close(TEX);

system("$texbin $prefix") == 0
    || die "Command 'tex $prefix' failed";

########## test 1
#
# Basic use

$cmd = sprintf("../dvi2bitmap --output=%s-%%d --query=bitmaps --resolution=%d --verbose=quiet %s.dvi",
	       $prefix, 72*$m, $prefix);
$nerrors += compare_bitmaps("Test 1", $cmd, @expected);


########## test 2
#
# Same, but with a --magnification of 2, so that the expected values
# must be doubled 

@texpected = ();
foreach my $ev (@expected) {
    my @t = ();
    foreach my $tt (@$ev) {
        push (@t, $tt*2);
    }
    push (@texpected, \@t);
}

$cmd = sprintf("../dvi2bitmap --output=%s-%%d --query=bitmaps --resolution=%d --verbose=quiet --magnification=2 %s.dvi",
	       $prefix, 72*$m, $prefix);
$nerrors += compare_bitmaps("Test 2", $cmd, @texpected);

########## test 3
#
# Same, but with a --magnification of 5, so that the expected values
# must be multiplied by 5.  This should show up more straightforward
# rounding errors

@texpected = ();
foreach my $ev (@expected) {
    my @t = ();
    foreach my $tt (@$ev) {
        push (@t, $tt*5);
    }
    push (@texpected, \@t);
}

$cmd = sprintf("../dvi2bitmap --output=%s-%%d --query=bitmaps --resolution=%d --verbose=quiet --magnification=5 %s.dvi",
	       $prefix, 72*$m, $prefix);
$nerrors += compare_bitmaps("Test 3", $cmd, @texpected);

########## test 4
#
# Same, but with a --magnification of 3 and --scaledown of 2.  This
# seems to stress roundings.  In DviFilePosition::DviFilePosition, the
# initialised positions are offset by one to avoid a failure here (and
# are otherwise unmotivated). [there's a reference to this text there,
# so change both together]

@texpected = ();
foreach my $ev (@expected) {
    my @t = ();
    foreach my $tt (@$ev) {
        push (@t, $tt*1.5);
    }
    push (@texpected, \@t);
}

$cmd = sprintf("../dvi2bitmap --output=%s-%%d --query=bitmaps --resolution=%d --verbose=quiet --magnification=3 --scaledown=2 %s.dvi",
	       $prefix, 72*$m, $prefix);
$nerrors += compare_bitmaps("Test 4", $cmd, @texpected);

########## That's all....


exit($nerrors);


sub compare_bitmaps ($$\@) {
    my $testlabel = shift;
    my $cmd = shift;
    my $exref = shift;
    my $nerr = 0;

    my @exp = @$exref;

    open (DB, "$cmd |") || die "Can't open $cmd in a pipe";

    my $ntests = 0;
    while (<DB>) {
        chomp;
        s/^Qbitmaps \S*\s*//;
        my @a = split;

        if ($#expected < 0) {
            print STDERR "More actual than expected\n";
            $nerr++;
            last;
        }
        my @e = @{$exp[$ntests]};
        $ntests++;

        my $isok = 1;
        my $i = 0;
        while (1) {
            if (! (defined($e[$i]) && defined($a[$i]))) { # one list has ended
                $isok = !(defined($e[$i]) || defined($a[$i]));
                # false unless both undef
                last;
            }
            if ($e[$i] != $a[$i]) {
                $isok = 0;
                last;
            }
            $i++;
        }
        if (!$isok) {
            print STDERR "$testlabel, page $ntests: expected [";
            foreach $i (0..$#e) {
                printf STDERR " %.1f", $e[$i];
            }
            print STDERR " ], got [";
            foreach $i (0..$#a) {
                printf STDERR " %.1f", $a[$i];
            }
            print " ]\n";
            $nerr++;
        }
    }

    close(DB);

    return $nerr;
}