package Gstring;


my($font);
my($bfont);
my($fg);
my($height_multiple) = 1.3;
my($height_mul2) = 0.80;

##################################################
# setup font
sub SetFont($$$)
{
    $font = shift;
    $bfont = shift;
    $fg = shift;
}

##################################################
# width of a string
sub Width($)
{
    my($str) = shift;
    my($s);
    my($width) = 0;
    my(@rows) = split('/', $str);

    Graph::LoadFont($font);

    foreach $s (@rows) {
	my($w);
	if ($s =~ /^(.*){(.*)}(.*)$/) {
	    $w = Graph::StringWidth($1);
	    Graph::LoadFont($bfont);
	    $w += Graph::StringWidth($2);
	    Graph::LoadFont($font);
	    $w += Graph::StringWidth($3);
	} else {
	    $w = Graph::StringWidth($s);
	}
	if ($w > $width) {
	    $width = $w;
	}
    }
    return $width;
}

##################################################
# height of a string
sub Height($)
{
    my($str) = shift;
    my($s);
    my($height) = 0;
    my(@rows) = split('/', $str);

    Graph::LoadFont($font);

    foreach $s (@rows) {
	my($h);
	if ($s =~ /^(.*){(.*)}(.*)$/) {
	    my($h2);
	    $h = Graph::StringHeight($1);
	    Graph::LoadFont($bfont);
	    $h2 = Graph::StringHeight($2);
	    if ($h2 > $h) { $h = $h2; }
	    Graph::LoadFont($font);
	    if ($3) {
		$h2 = Graph::StringHeight($3);
		if ($h2 > $h) { $h = $h2; }
	    }
	} else {
	    $h = Graph::StringHeight($s);
	}
	$height += $h * $height_multiple;
    }
    return $height;
}


##################################################
# display one string
sub Draw($$$)
{
    my($str) = shift;
    my($x) = shift;
    my($y) = shift;
    my($y0) = $y;

    Graph::LoadFont($font);
    Graph::SetForeground(Graph::GetColor($fg));

    my(@rows) = split('/', $str);
    foreach $s (@rows) {
	my($h) = Height($s);
	my($x1) = 0;
	if ($s =~ /^(.*){(.*)}(.*)$/) {
	    Graph::LoadFont($font);
	    Graph::DrawString($x+$x1, $y+$height_mul2*$h, $1);
	    $x1 += Width($1);
	    Graph::LoadFont($bfont);
	    Graph::DrawString($x+$x1, $y+$height_mul2*$h, $2);
	    $x1 += Width($2);
	    Graph::LoadFont($font);
	    Graph::DrawString($x+$x1, $y+$height_mul2*$h, $3);
	} else {
	    Graph::LoadFont($font);
	    Graph::DrawString($x, $y+$height_mul2*$h, $s);
	}
	$y += $h;
    }
    return $y - $y0;
}


1;
