I am trying to calculate the length of a string in a a font. The goal is to create texts like:
DD Qui n'a pu l'obtenir ne le méritait pas.
LC Ne le méritait pas! Moi?
DD Vous!
LC Ton impudence
Téméraire vieillard, aura sa récompense
In a fixed font (as above), this is easy. For proportional fonts, I can make an acceptable approximation of the previous line length with:
use Tk;
use Tk::Font;
my $mw = MainWindow->new();
my $font = $mw->fontCreate( 'TimesNew Roman' );
sub timesspace {
(my $text)=@_;
my $retval= $font->measure( $text );
return int($retval);
}
(a reasonable estimation, because the Tk fonts are not exactly the same as the output-processor's font).
This however, requires a graphical environment (because I create a Tk window), even though al the scripts are CLI only. This leads to:
couldn't connect to display "localhost:11.0" at /usr/local/lib64/perl5/Tk/MainWindow.pm line 53.
MainWindow->new() at /usr/local/bin/xml3roff line 10.
Is there a reliable and simple way of finding the length of a string in Times/Helvetica/... without the need of such an environment?
