It's not "Unicode" issue. Arabic has different glyphs for same letter depending on context. A quick and dirty solution:

use strict; use warnings; use Font::TTFMetrics; my $pointsize = 16; my $resolution = 72; my $metrics = Font::TTFMetrics-> new( "Arial.ttf" ); my $c = $pointsize * $resolution / 72 / $metrics-> get_units_per_em; my $str = "\x{0628}\x{0644}\x{062D}\x{0629}"; printf "Wrong answer: %.0f\n", $metrics-> string_width( $str ) * $c; printf "Right answer: %.0f\n", $metrics-> string_width( render_arabic( + $str )) * $c; sub render_arabic { my $word = shift; return $word if length $word == 1; # isolated my %LUT = ( "\x{0628}" => [ "\x{FE90}", "\x{FE92}", "\x{FE91}" ], "\x{0644}" => [ "\x{FEDE}", "\x{FEE0}", "\x{FEDF}" ], "\x{062D}" => [ "\x{FEA2}", "\x{FEA4}", "\x{FEA3}" ], "\x{0629}" => [ "\x{FE94}" ], ); $word =~ s/^. /$LUT{ $& }[ 2 ]/x; $word =~ s/(?<=.).(?=.)/$LUT{ $& }[ 1 ]/xg; $word =~ s/ .$/$LUT{ $& }[ 0 ]/x; return $word }

Wrong answer: 33 Right answer: 22

In reply to Re: 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.