Any reason to expect simple solution for a complex problem? With a "single line of code"? It's not just sum of metrics resulting from arbitrary sequence of Unicode code points, that you are looking for. But layout engine for complex writing systems, with ligatures, contextual substitutions, etc. A magic which happens when you type 4 characters and they "automatically" change their shape (and width, if that's what you are after), after any new character is added.
Think uniscribe, or pango. Apropos of Pango:
use strict;
use warnings;
use feature 'say';
use Pango;
my $surface = Cairo::ImageSurface-> create( 'argb32', 200, 100 );
my $cr = Cairo::Context-> create( $surface );
my $layout = Pango::Cairo::create_layout( $cr );
my $font = Pango::FontDescription-> from_string( 'Arial 16' );
#Pango::Cairo::Context-> set_resolution( $cr, 72 );
$layout-> set_font_description( $font );
$layout-> set_text( "\x{0628}\x{0644}\x{062D}\x{0629}" );
say for Pango::Layout::get_pixel_size( $layout );
Not a single line, is it? But "new", definitely. It says width for a 96 dpi default resolution, and the commented line is there because I didn't figure out how to change it.
If a "single line of code" is a priority, you can qx( pango-view ... and parse the pgm-file for width, all that fits nicely into a single line.
|