font metrics and character spacing trouble
Posted: 2007-08-08T20:09:29-07:00
This is a very esoteric question about character spacing.
I am trying to write a word (a number actually) character by character. I need to do this because I want each character to be colored differently. I am using perlMagick on Ubuntu linux.
I have tried several different methods to get the spacing right. So far the best I have managed is to write the text from right to left, using QueryFontMetrics to determine the right corner of each character.
The result is pretty good, there is a small fixed offset which I don't mind but the spacing between the first and second character (from the left) is always off (too large) by a significant fraction of the character size.
Any help on how to get the correct spacing will be apreciated. Pointers on how to space the characters manually or advice on how to use QueryFontMetrics better.
I have skimmed http://www.microsoft.com/typography/otspec/TTCH01.htm but didn't notice anything on spacing characters in a word.
A code snipit of what I've been trying, it writes outlined red text character by character and the same string all at once in green for comparison:
I am trying to write a word (a number actually) character by character. I need to do this because I want each character to be colored differently. I am using perlMagick on Ubuntu linux.
I have tried several different methods to get the spacing right. So far the best I have managed is to write the text from right to left, using QueryFontMetrics to determine the right corner of each character.
The result is pretty good, there is a small fixed offset which I don't mind but the spacing between the first and second character (from the left) is always off (too large) by a significant fraction of the character size.
Any help on how to get the correct spacing will be apreciated. Pointers on how to space the characters manually or advice on how to use QueryFontMetrics better.
I have skimmed http://www.microsoft.com/typography/otspec/TTCH01.htm but didn't notice anything on spacing characters in a word.
A code snipit of what I've been trying, it writes outlined red text character by character and the same string all at once in green for comparison:
Code: Select all
use Image::Magick;
use POSIX;
$image = Image::Magick->new;
$image->Set( size => "200x100" );
$image->read("xc:white");
#Set the center of the string
$x = 100;
$text = "121212";
#get a list of characters
@text = split //, $text;
($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) =
$image->QueryFontMetrics(
pointsize => 60,
text => $text,
StrokeWidth => 2,
Stroke => black
);
$line_left = int( $x - $width / 2 );
$offset = 0;
$offset_text = $text;
#Draw comparison text below.
#This text gets the correct spacing.
$image->Annotate(
font => "Helvetica",
fill => green,
pointsize => 60,
text => $text,
StrokeWidth => 2,
Stroke => black,
align => left,
x => $line_left,
y => 95,
);
#Draw the colored text separately
#for testing opurposes it's all red
#Start from the right
for($i = @text -1; $i >=0; $i--) {
$char = $text[$i];
#Get the width.
#the distance from the start of the line to the right corner of this char
($x_ppem, $y_ppem, $ascender, $descender, $width, $height, $max_advance) =
$image->QueryFontMetrics(
font => Helvetica,
pointsize => 60,
text => $offset_text,
StrokeWidth => 2,
);
$offset = ceil( $width );
$x = $line_left + $offset;
#Draw the text would usually be different colors for each character
$image->Annotate(
font => Helvetica,
# fill => get_color($char),
fill => red,
pointsize => 60,
text => $char,
StrokeWidth => 2,
Stroke => black,
align => right,
x => $x,
y => 50,
);
#Strip off the character we just wrote and return for the next
$offset_text =~ s/.$//;
}
$image->Write( filename => "numbers.gif", compression => 'None' );