Wednesday, May 7, 2008

Print to png file from Octave

His last advice is very useful. I tried and it works perfectly.
Here is the link to the original post.

----------------------------------------------------------------

I ran into a problem trying to print a plot to a PNG file. I originally reported this to the octave bugs list but I finally realized it was more a matter of understanding how to use fonts with Octave 3.0 under Linux. (I don't know if this problem exists for Windows users or if so how to solve it). It took me a long time to figure out how this works so I thought I'd share the answer.

I was trying to do something like this:

x = 1:10 ;
plot (x);
title ('My plot');
print myplot.png -dpng

When Octave executed the print command, I got this error message:

gdImageStringFT: Could not find/open font while printing string My plot with font Helvetica

It turns out that Octave 3.0 uses 'Helvetica' as it's default font and if you don't happen to have a Helvetica font installed, you'll get this error message. I fixed this problem by finding the fonts that are installed on my system and explicitly specifying which font to use. Here's a detailed explanation:

First you need some fonts. You could use one of the five default fonts available in gnuplot but I found them way too limited for my needs. You probably already have some good true type fonts installed in your Linux distro. If not, you should be able to install some with your package manager.

Next you need to find the path to the fonts. Try looking in "/usr/share/fonts/truetype". If they aren't there, you can do "find . -name *.ttf" from /usr or other system directories until you find the font directories. As an example, I found two font files I wanted to use:

/usr/share/fonts/truetype/ttf-mgopen/MgOpenModernaBold.ttf
/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf

Now you need to tell gnuplot where to find the fonts. Do this by setting the GDFONTPATH environment variable. I had to list each font subdirectory like this:

export GDFONTPATH="/usr/share/fonts/truetype/ttf-mgopen:/usr/share/fonts/truetype/ttf-dejavu"

Now you can use the name of the font file in Octave like this:

title ('My plot', 'FontName', 'MgOpenModernaBold.ttf', 'FontSize', 18);

As far as I can tell, the Octave plot window didn't use the font I specified but when I printed to a PNG file, it had the right font.

If for some reason, setting GDFONTPATH doesn't work, you can specify the full path to the font. It's a yucky hack, but it works. I discovered thru trial and error that there seems to be some magical length limit on the path name. When I did this in octave:

title ('My plot', 'FontName', "/usr/share/fonts/truetype/ttf-mgopen/MgOpenModernaBold.ttf");

I got an error. So I copied the font file to a directory with a shorter name and shortened the file name as well:

title ('My plot', 'FontName', "/home/dave/Fonts/MdrnaBd.ttf");

This works just fine.

3 comments:

Thomas Soininen said...

Thanks for the font hack! This really helped me out :)

Unknown said...

Ah, Thanks!

Sondos said...

Thanks very much!