Inconsistency with text metrics IM 6.8.9-9

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Djidiouf
Posts: 8
Joined: 2016-10-31T06:06:48-07:00
Authentication code: 1151

Inconsistency with text metrics IM 6.8.9-9

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Inconsistency with text metrics IM 6.8.9-9

Post 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.
snibgo's IM pages: im.snibgo.com
Djidiouf
Posts: 8
Joined: 2016-10-31T06:06:48-07:00
Authentication code: 1151

Re: Inconsistency with text metrics IM 6.8.9-9

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Inconsistency with text metrics IM 6.8.9-9

Post 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.
snibgo's IM pages: im.snibgo.com
Djidiouf
Posts: 8
Joined: 2016-10-31T06:06:48-07:00
Authentication code: 1151

Re: Inconsistency with text metrics IM 6.8.9-9

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Inconsistency with text metrics IM 6.8.9-9

Post 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.
Post Reply