#!/usr/bin/perl

my $fntW, $fntH, $fntX, $fntY;
my $fntAscent, $fntDescent;

my $charCode, $w, $h, $x, $y;
my $verbatim;

while (<STDIN>) {
    $verbatim = 1;
    if (/FONTBOUNDINGBOX ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/) {
        $fntW = $1;
        $fntH = $2;
        $fntX = $3;
        $fntY = $4;
    }
    elsif (/FONT_ASCENT ([0-9]+)/) {
        $fntAscent = $1;
    }
    elsif (/FONT_DESCENT ([0-9]+)/) {
        $fntDescent = $1;
    }
    elsif (/ENCODING ([0-9]+)/) {
        $charCode = $1;
    }
    elsif (/BBX ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/) {
        $w = $1;
        $h = $2;
        $x = $3;
        $y = $4;
        
        if ($y + $h > $fntAscent) {
            $y = $fntAscent - $h;
            print STDERR "Char $charCode beyond ascent; set y to $y\n";
        }
        elsif ($y < -$fntDescent) {
            $y = -$fntDescent;
            print STDERR "Char $charCode beyond descent; set y to $y\n";
        }
        $verbatim = 0;
        print "BBX $w $h $x $y\n";
    }
    
    if ($verbatim) {
        print;
    }
}
