Page 1 of 1

Inconsistency with text metrics IM 6.8.9-9

Posted: 2016-10-31T06:24:13-07:00
by Djidiouf
IM version:
ImageMagick 6.8.9-9 Q16 x86_64 2016-05-16
Debian 8.4 - x64


Issue
I've recently tried to get the size of a given text in pixel but found myself puzzled by an inconsistency.


The following script retrieves the sizes of each word passed as a variable and output a total size.

Code: Select all

text="Test de la saucisse seche ij."
IFS=', ' read -r -a array <<< "$text"  # Put each word in a list
array_lengh=$(echo ${#array[@]})

total_size=0
i=0
for element in "${array[@]}"
do
    i=$(echo $(($i + 1)))
    size=$(convert -debug annotate  xc: -font /home/server/streams/_thumbnails/_resources/BebasNeue.otf -pointsize 130 -interword-spacing 20 -kerning 0 -annotate 0 "$element" null: 2>&1 | grep "Metrics:" | awk -F ";" '{print $2}' | awk -F ": " '{print $2}')
    echo "($i) $element=$size"
    
    total_size=$(echo "$total_size+$size" | bc)
done

echo $total_size
Here is the output:
(1) Test=188.953
(2) de=96.7969
(3) la=96.5625
(4) saucisse=374.562
(5) seche=249.016
(6) ij.=81.7031
user@server:~/scripts$ echo $total_size
1087.5935

Now, it's time to get the size of all these words already concatenated together, without any space between them:

Code: Select all

convert -debug annotate xc: -font /home/server/streams/_thumbnails/_resources/BebasNeue.otf -pointsize 130 -interword-spacing 20 -kerning 0 -annotate 0 "Testdelasaucissesecheij." null: 2>&1 | grep "Metrics:" | awk -F ";" '{print $2}' | awk -F ": " '{print $2}'
Output:
1094.95

As you can see, there is a difference of 7~ pixels. It's really problematic when you want to know exactly which size a text will be in pixel. I used to do some auto-fit with caption but with a specific pointsize and that kind of inconsistency doesn't allow to forecast when a new line will occur or if a text will fit correctly in a box.

Re: Inconsistency with text metrics IM 6.8.9-9

Posted: 2016-10-31T09:37:45-07:00
by snibgo
The pixel width of a letter or word depends on what is next to it. For example consider the letters f and i. When adjacent, fi occupies less space than f plus i in many fonts.

Re: Inconsistency with text metrics IM 6.8.9-9

Posted: 2016-11-01T06:04:22-07:00
by Djidiouf
Isn't what the kerning is about? Fixing this width to a specific size and removing any variation? I set the kerning to be 0 on that purpose

If not, is there a mean to do that?

Re: Inconsistency with text metrics IM 6.8.9-9

Posted: 2016-11-01T06:20:59-07:00
by snibgo
"-kerning" increases or decreases the gap between characters. See http://www.imagemagick.org/Usage/text/#kerning. By default it is zero, so "-kerning 0" has no effect.

Re: Inconsistency with text metrics IM 6.8.9-9

Posted: 2016-11-02T07:01:51-07:00
by Djidiouf
Ok thank you.

So my understanding is:
- that behaviour is known and it's how fonts work
- kerning is relative and apply upon the gap set in the font between characters

Re: Inconsistency with text metrics IM 6.8.9-9

Posted: 2016-12-05T04:03:25-07:00
by Bonzo
The pixel width of a letter or word depends on what is next to it
From what I understand it also depends on the font as there are monospaced fonts.