Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Using fonts in GD on OS X.

by jimt (Chaplain)
on Oct 23, 2006 at 16:15 UTC ( [id://580090]=perlquestion: print w/replies, xml ) Need Help??

jimt has asked for the wisdom of the Perl Monks concerning the following question:

I'm stumped. How can I load up an external font and have GD use it? I just want to print out a string into an image, then save it as a png. Nothing fancy. But I haven't been able to track down an example that works.

My gd config summary tells me that I have support for PNG, JPEG, Freetype 2.x, Fontconfig, Xpm, and pthreads. My GD is up to date w/2.35, and my gd version is 2.0.33. I'm running 10.4.8 on a macbook w/a core duo. Sample script along with sample results are attached. What do I need to change to make this work? Did I miss the proper incantation to produce my string, or am I doing it properly with one of the methods down below and something external is thwarting me? Any pointers appreciated.

#!/usr/bin/perl use GD; use strict; use warnings; my ($w, $h) = (50, 50); my $img = GD::Image->new($w, $h); my $white = $img->colorAllocate(255,255,255); my $black = $img->colorAllocate(0,0,0); my %pixmap = (); my $monaco = GD::Font->load('/System/Library/Fonts/Monaco.dfont') or die "Can't load monaco!"; $img->rectangle(10,10,20,20,$black); #this gives me a bus error #$img->stringFT($black,'/System/Library/Fonts/Monaco.dfont',12,0,10,10 +,'A'); #this does nothing #$img->stringFT($black,$monaco,12,0,10,10,'A'); #this dies with "font is not of type GD::Font " #$img->string('/System/Library/Fonts/Monaco.dfont',10,10,'A',$black); #this one does nothing #$img->string($monaco,10,10,'A',$black); $img->rectangle(0,0,10,10,$black); open (IMG, ">/Users/jim/Desktop/img.png"); print IMG $img->png; close IMG;

Replies are listed 'Best First'.
Re: Using fonts in GD on OS X.
by zentara (Archbishop) on Oct 23, 2006 at 16:38 UTC
    Hi, I'm on linux, but your line
    my $monaco = GD::Font->load('/System/Library/Fonts/Monaco.dfont') or die "Can't load monaco!";
    causes my script to prematurely exit with "Success" at that line. If I comment out the line, and use one of my fonts, your script works. However, notice in the next snippet, that you may need your gd libs to be compiled with TrueType font support. I don't know what monaco is.

    Works:

    #!/usr/bin/perl use GD; use strict; use warnings; my ($w, $h) = (50, 50); my $img = GD::Image->new($w, $h); my $white = $img->colorAllocate(255,255,255); my $black = $img->colorAllocate(0,0,0); # this line causes a premature exit with "success". #my $monaco = GD::Font->load('Generic.ttf');# or die "Can't load monac +o!"; $img->rectangle(10,10,20,20,$black); $img->stringFT($black,"./Generic.ttf",12,0,10,10,'A'); open (IMG, ">$0.png"); print IMG $img->png; close IMG;

    Now here is another snippet, that may be useful:

    #!/usr/bin/perl use strict; use warnings; use GD; #Andrew Gaffney my $im = new GD::Image(61,20); my ($text, $saveto) = @ARGV || ('Hi There', $0.'.png'); my $white = $im->colorAllocate(255,255,255); my $black = $im->colorAllocate(0,0,0); my $gray = $im->colorAllocate(132,132,132); my $blue = $im->colorAllocate(206,206,255); my $leftblue = $im->colorAllocate(231,231,255); my $bottomblue = $im->colorAllocate(165,165,206); my $rightblue = $im->colorAllocate(123,123,156); my $topblue = $im->colorAllocate(214,214,255); $im->transparent($white); $im->interlaced('true'); $im->filledRectangle(0,0,60,19,$white); $im->filledRectangle(3,3,60,19,$gray); $im->filledRectangle(0,0,57,16,$blue); $im->rectangle(0,0,57,16,$white); $im->line(1,0,56,0,$topblue); $im->line(57,1,57,15,$rightblue); $im->line(1,16,56,16,$bottomblue); $im->line(0,1,0,15,$leftblue); # Dry run to determine size of outputted text #this requires libgd was compiled with TrueType Font support my (@bounds) = GD::Image->stringFT($black,"./Generic.ttf",9,0,0,0,$tex +t); # Use above dimensions to center text $im->stringFT($black,"./Generic.ttf",9,0,((57 - $bounds[2])/2),13,$tex +t); #$im->string(gdLargeFont, 2, 2, $text, $black); open IMAGE, "> $saveto" or die "Can't open $saveto\n"; binmode IMAGE; print IMAGE $im->png; close IMAGE;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Using fonts in GD on OS X.
by Cabrion (Friar) on Oct 24, 2006 at 01:45 UTC
    You have to use TrueType fonts. Those that end in .ttf will work. Mac's handle several font formats and .dfont isn't TrueType, it's an Apple thing called a Data Fork Font that appears to embed various foramts within it, but GD wouldn't have a clue about that format. You can't just use any font that a Mac can handle.
Re: Using fonts in GD on OS X.
by jimt (Chaplain) on Oct 24, 2006 at 14:03 UTC

    What a PITA. I'm documenting here so others may learn from my pain.

    There are vague claims on the internet that GD will work with Mac OS X style dfont files. For example, the instructions on compiling gd on OS X (http://www.paginar.net/matias/articles/gd_x_howto.html) claim to be able to run gdtestft with a dfont file. Theres also this useless little comment: (http://use.perl.org/comments.pl?sid=24188&cid=37044) that gave me hope. Bus error for me.

    I tried re-configuring gd an ran the gtestft script and got this error:

    dyld: Symbol not found: _FSPathMakeRef Referenced from: /usr/local/lib/libfreetype.6.dylib Expected in: flat namespace

    So I dug some more and I found this http://lists.freedesktop.org/archives/fontconfig/2006-October/002501.html which claimed that freetype-2.2.1 enables MacOS specific font format support, but requires Carbon framework functions, however freetype-config and freetype2.pc don't pass the appropriate linker flags. It has instructions for flags to add, which I did.

    Now gdtestft tossed this error:

    Symbol not found: __cg_jpeg_resync_to_restart Referenced from: /System/Library/Frameworks/ ApplicationServices.framework/Versions/A/Frameworks/ ImageIO.framework/Versions/A/ImageIO Expected in: /usr/local/lib/libJPEG.dylib

    Progres, at least. That lead me to this link: http://tolstoy.newcastle.edu.au/R/devel/05/05/0752.html, which told me that I should remove my symlink to my own intalled /usr/local/libjpeg.dylib. So I did that, re-configured and re-made gd, and now gdtestft died quietly and simply with a segfault, instead of a bus error. Sigh.

    So, I opted for workarounds, which lead me to Fondu, a set of programs to interconvert between mac font formats and pfb, ttf, otf and bdf files on unix. This is happily installable via fink or darwin ports or compilation or whatever else you want.

    I then used fondu to convert my Monaco.dfont file into a Monaco.ttf file, and GD would then happily read it and operate on it for me. And, for my purposes since I'm just using a single font, I can live with that. Using multiple font would be a nuisance.

    Supposedly there's a way to make it all work with dfonts directly, but I still haven't found the magic incantation. If someone knows it and is willing to share, then please do. Otherwise, this workaround will get me by for now.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://580090]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found