Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
schmoove
Posts: 6
Joined: 2016-02-11T06:47:07-07:00
Authentication code: 1151

Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by schmoove »

Hello,

I am migrating from Debian 7 to Debian 8 and Imagick::queryFontMetrics is sometimes returning faulty calculations for the textWidth and the boundingBox[x1] of individual characters, likely due to an integer overflow. My setup in Debian 7 is working just fine, my Debian 8 and Ubuntu 15.10 setup looks buggy. The faulty calculations are pretty much off by 33554430, which corresponds to two added up medium unsigned integers (3 bytes in length, maximum value == 16777215).

To reproduce, take the following font (RNS.ttf):

http://www.1001fonts.com/rns-font.html

queryFontMetrics for the character 'g':

Code: Select all

    <?php
      $image = new Imagick();
      $fontDraw = new ImagickDraw();
      $fontDraw->setFont('./RNS.ttf');
      $fontDraw->setFontSize(72);
      $fontMetrics = $image->queryFontMetrics($fontDraw, 'g', FALSE);
      print_r($fontMetrics);
    ?>

Debian 8 and Ubuntu 15.10 (bogus results):
Array
(
[characterWidth] => 72
[characterHeight] => 72
[ascender] => 70
[descender] => -31
[textWidth] => 33554469.1875
[textHeight] => 103
[maxHorizontalAdvance] => 61
[boundingBox] => Array
(
[x1] => -33554430.1875
[y1] => -0.09375
[x2] => 35
[y2] => 55.171875
)

[originX] => 36
[originY] => 0
)
Debian 7 (correct results):
Array
(
[characterWidth] => 72
[characterHeight] => 72
[ascender] => 70
[descender] => -31
[textWidth] => 37.5625
[textHeight] => 103
[maxHorizontalAdvance] => 61
[boundingBox] => Array
(
[x1] => 0.4375
[y1] => -0.140625
[x2] => 35
[y2] => 54.078125
)

[originX] => 36
[originY] => 0
)
All other characters look good. Only the lowercase 'g' seems to break queryFontMetrics for RNS.ttf.

Another font example (GreatVibes-Regular.ttf), breaks for the letter 'u' in certain sizes:

https://github.com/google/fonts/tree/ma ... greatvibes

queryFontMetrics for the character 'u' in size 256:

Code: Select all

    <?php
      $image = new Imagick();
      $fontDraw = new ImagickDraw();
      $fontDraw->setFont('./GreatVibes-Regular.ttf');
      $fontDraw->setFontSize(256);
      $fontMetrics = $image->queryFontMetrics($fontDraw, 'g', FALSE);
      print_r($fontMetrics);
    ?>

Debian 8 and Ubuntu 15.10 (bogus results):
Array
(
[characterWidth] => 72
[characterHeight] => 72
[ascender] => 70
[descender] => -31
[textWidth] => 33554469.1875
[textHeight] => 103
[maxHorizontalAdvance] => 61
[boundingBox] => Array
(
[x1] => -33554430.1875
[y1] => -0.09375
[x2] => 35
[y2] => 55.171875
)

[originX] => 36
[originY] => 0
)

Size 72 looks fine for the GreatVibes font.


Here is my stack:
$ apt-cache policy libfreetype6
Debian 7 (wheezy): 2.4.9-1.1+deb7u3
Debian 8 (jessie): 2.5.2-3+deb8u1
Ubuntu 15.10: 2.5.2-4ubuntu2

I have tried installing some different freetype versions by hand, but with the same results.

$ apt-cache policy imagemagick
Debian 7 (wheezy): 8:6.7.7.10-5+deb7u3
Debian 8 (jessie): 8:6.8.9.9-5
Ubuntu 15.10: 8:6.8.9.9-5ubuntu2

I also tried with manual install of ImageMagick-6.9.2-6 with same results.

$ apt-cache policy php5-imagick
Debian 7 (wheezy): 3.1.0~rc1-1+b2
Debian 8 (jessie): 3.2.0~rc1-1
Ubuntu 15.10: 3.3.0~rc2-1

I have tried several imagick versions via PECL with same results.

I have tried looking at two font files in the above examples, opening them in fontforge. I wasn't able to find any abnormalities with the disobedient characters, but then again my fontforge knowledge is very poor.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by magick »

We're using ImageMagick 6.9.3-3, the current release. We get

Code: Select all

php rns.php
Array
(
    [characterWidth] => 72
    [characterHeight] => 72
    [ascender] => 70
    [descender] => -31
    [textWidth] => 38.5625
    [textHeight] => 103
    [maxHorizontalAdvance] => 61
    [boundingBox] => Array
        (
            [x1] => 0.4375
            [y1] => -0.09375
            [x2] => 35
            [y2] => 55.171875
        )

    [originX] => 36.28125
    [originY] => 0
)
Unfortunatley, we do not have access to a Debian system to determine why its failing for you. Can you upgrade to ImageMagick 6.9.3-3 and let us know if that resolves the issue?
schmoove
Posts: 6
Joined: 2016-02-11T06:47:07-07:00
Authentication code: 1151

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by schmoove »

Still no luck. I just installed ImageMagick-6.9.3-3 from source on Ubuntu 15.10, recompiled PECL imagick-3.3.0 and I am getting the same bogus results for RNS.ttf, querying metrics for the letter 'g':

Code: Select all

<?php
  $image = new Imagick();
  $fontDraw = new ImagickDraw();
  $fontDraw->setFont('./RNS.ttf');
  $fontDraw->setFontSize(72);
  $fontMetrics = $image->queryFontMetrics($fontDraw, 'g', FALSE);
  print_r($fontMetrics);
?>
Result:

Code: Select all

Array
(
    [characterWidth] => 72
    [characterHeight] => 72
    [ascender] => 71
    [descender] => -32
    [textWidth] => 33554469.1875
    [textHeight] => 103
    [maxHorizontalAdvance] => 61
    [boundingBox] => Array
        (
            [x1] => -33554430.1875
            [y1] => -0.09375
            [x2] => 35
            [y2] => 55.171875
        )

    [originX] => 36
    [originY] => 0
)
My phpinfo() shows me that the correct newly installed Imagick and ImageMagick versions are being used:

Code: Select all

imagick module => enabled
imagick module version => 3.3.0
imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version => ImageMagick 6.9.3-3 Q16 x86_64 2016-02-11 http://www.imagemagick.org
Imagick using ImageMagick library version => ImageMagick 6.9.3-3 Q16 x86_64 2016-02-11 http://www.imagemagick.org
ImageMagick copyright => Copyright (C) 1999-2016 ImageMagick Studio LLC
ImageMagick release date => 2016-02-11
I also tried the latest imagick beta v3.4.0RC6 with the same bogus results.

Could this perhaps be a freetype issue?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by magick »

Do you get the same results if you use the command line? Try
  • convert -debug annotate -pointsize 72 -font GreatVibes-Regular.ttf label:g null:
If the metrics are reasonable, the problem could be IMagick. If not, we still need to consider the version of the Freetype delegate library.
schmoove
Posts: 6
Joined: 2016-02-11T06:47:07-07:00
Authentication code: 1151

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by schmoove »

Still the same faulty results:

Code: Select all

$ convert -debug annotate -pointsize 72 -font ./RNS.ttf label:g null:
2016-02-12T10:09:01+01:00 0:00.000 0.000u 6.9.3 Annotate lt-convert[7535]: annotate.c/RenderFreetype/1420/Annotate
  Font ./RNS.ttf; font-encoding none; text-encoding none; pointsize 72
2016-02-12T10:09:01+01:00 0:00.000 0.000u 6.9.3 Annotate lt-convert[7535]: annotate.c/GetTypeMetrics/842/Annotate
  Metrics: text: g; width: 3.35545e+07; height: 103; ascent: 71; descent: -32; max advance: 61; bounds: -3.35544e+07,-0.09375  35,55.1719; origin: 36,0; pixels per em: 72,72; underline position: -1.5625; underline thickness: 0.78125

$ convert --version
Version: ImageMagick 6.9.3-3 Q16 x86_64 2016-02-12 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC OpenMP 
Delegates (built-in): bzlib djvu fontconfig freetype gvc jbig jng jpeg lcms lqr lzma openexr pangocairo png tiff wmf x xml zlib
I also tried the convert command from ImageMagick 6.8.9-9 with the same faulty results.

Side Note: I had to link some old libraries for ImageMagick 6.9.3-3 to compile:

Code: Select all

# ln -s /usr/lib/x86_64-linux-gnu/ImageMagick-6.8.9/bin-Q16/MagickWand-config /usr/bin/
# ln -s /usr/lib/x86_64-linux-gnu/ImageMagick-6.8.9/bin-Q16/Wand-config /usr/bin/
These libraries are probably from the Ubuntu repository, not 100% sure though:

Code: Select all

$ apt-get install libmagickwand-dev libmagickcore-dev
schmoove
Posts: 6
Joined: 2016-02-11T06:47:07-07:00
Authentication code: 1151

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by schmoove »

Just one more thought, I would actually prefer rendering fonts via pangocairo for its better OpenType support, I haven't taken the time yet to figure out how to make the switch though. That might even be a good way to check if freetype might be the culprit. If this switch would actually happen to solve the metrics issue, I would be even more than delighted :-)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by magick »

For comparison, we use ImageMagick 6.9.3-3 and Freetype 2.6.0:

Code: Select all

$ convert -debug annotate -pointsize 72 -font ./RNS.ttf label:g null:
2016-02-12T06:56:07-05:00 0:00.110 0.010u 7.0.0 Annotate convert[22115]: annotate.c/RenderFreetype/1442/Annotate
  Font ./RNS.ttf; font-encoding none; text-encoding none; pointsize 72
2016-02-12T06:56:07-05:00 0:00.110 0.010u 7.0.0 Annotate convert[22115]: annotate.c/GetTypeMetrics/860/Annotate
  Metrics: text: g; width: 38.5625; height: 103; ascent: 70; descent: -31; max advance: 61; bounds: 0.4375,-0.09375  35,55.1719; origin: 36.2812,0; pixels per em: 72,72; underline position: -1.5625; underline thickness: 0.78125
2016-02-12T06:56:07-05:00 0:00.120 0.010u 7.0.0 Annotate convert[22115]: annotate.c/RenderFreetype/1442/Annotate
  Font ./RNS.ttf; font-encoding none; text-encoding none; pointsize 72
2016-02-12T06:56:07-05:00 0:00.120 0.010u 7.0.0 Annotate convert[22115]: annotate.c/GetTypeMetrics/860/Annotate
  Metrics: text: g; width: 38.5625; height: 103; ascent: 70; descent: -31; max advance: 61; bounds: 0.4375,-0.09375  35,55.1719; origin: 36.2812,0; pixels per em: 72,72; underline position: -1.5625; underline thickness: 0.78125
2016-02-12T06:56:07-05:00 0:00.120 0.020u 7.0.0 Annotate convert[22115]: annotate.c/RenderFreetype/1442/Annotate
  Font ./RNS.ttf; font-encoding none; text-encoding none; pointsize 72
2016-02-12T06:56:07-05:00 0:00.120 0.020u 7.0.0 Annotate convert[22115]: annotate.c/GetTypeMetrics/860/Annotate
  Metrics: text: g; width: 38.5625; height: 103; ascent: 70; descent: -31; max advance: 61; bounds: 0.4375,-0.09375  35,55.1719; origin: 36.2812,0; pixels per em: 72,72; underline position: -1.5625; underline thickness: 0.78125
2016-02-12T06:56:07-05:00 0:00.120 0.020u 7.0.0 Annotate convert[22115]: annotate.c/RenderFreetype/1442/Annotate
  Font ./RNS.ttf; font-encoding none; text-encoding none; pointsize 72
Ensure you have embedded Freetype in ImageMagick with this command:
  • $ convert -list format | grep -i Freetype
    TTF* TTF r-- TrueType font (Freetype 2.6.0)
schmoove
Posts: 6
Joined: 2016-02-11T06:47:07-07:00
Authentication code: 1151

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by schmoove »

Thanks for your help so far @magick. I'll be back soon with results regarding Freetype 2.6+
schmoove
Posts: 6
Joined: 2016-02-11T06:47:07-07:00
Authentication code: 1151

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by schmoove »

No luck with Freetype 2.6.3 from source, I am still seeing the faulty results:

Code: Select all

$ convert -debug annotate -pointsize 72 -font ./RNS.ttf label:g null:
2016-02-15T20:14:49+01:00 0:00.010 0.000u 6.9.3 Annotate convert[3870]: annotate.c/RenderFreetype/1421/Annotate
  Font ./RNS.ttf; font-encoding none; text-encoding none; pointsize 72
2016-02-15T20:14:49+01:00 0:00.010 0.000u 6.9.3 Annotate convert[3870]: annotate.c/GetTypeMetrics/843/Annotate
  Metrics: text: g; width: 3.35545e+07; height: 103; ascent: 70; descent: -31; max advance: 61; bounds: -3.35544e+07,-0.09375  35,55.1719; origin: 36,0; pixels per em: 72,72; underline position: -1.5625; underline thickness: 0.78125

$ convert --version
Version: ImageMagick 6.9.3-4 Q16 x86_64 2016-02-15 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC OpenMP 
Delegates (built-in): freetype png zlib

$ convert -list format | grep -i Freetype
   TTF* r--   TrueType font (Freetype 2.6.3)
Anybody here running Debian/Ubuntu who could take a look? I am pretty much stumped from here on.
shaffe
Posts: 1
Joined: 2016-11-29T08:04:54-07:00
Authentication code: 1151

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by shaffe »

I had the same problem with the f lowercase glyph (Imagick 3.1.2 - Linux php55.linux 3.13.0-101-generic #148-Ubuntu SMP Thu Oct 20 22:08:32 UTC 2016 x86_64).
In order to use the queryFontMetrics I had to replace all f with h. The result is OK.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Imagick::queryFontMetrics faulty textWidth | Integer overflow | Debian 8 (Jessie) | Ubuntu 15.10

Post by fmw42 »

Are the fonts the exact same fonts from the very same source on each of your systems. I am not on Linux, but have downloaded the same font names from different places and they are not always the same. Some may not have good glyph metrics associated with them
Post Reply