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.


In reply to Re^3: How to get an accurate TTFMetrics values for unicode strings? by vr
in thread How to get an accurate TTFMetrics values for unicode strings? by Lejocode

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.