Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Converting X11 fonts for GD

by seekerOfKnowledge (Novice)
on Aug 23, 2012 at 14:29 UTC ( [id://989323]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to us an outside font with GD.

Though I do not have to use X11 fonts, GD comes with a program called "bdf2gd.pl" that can convert X11 fonts to a compatible format. So it looked like a good place to start. Unfortunately, the utility doesn't seem to work. A 99k ASCII text file comes out a 220k binary file.

The GD documentation also says their font format is the same as the old Windows' fixed-point format. So I got my copy of Windows 3.1 and tried one of its fonts. GD complains its format is not correct.

So, my question, oh wise ones, is what is the true way to use outside fonts in GD?


I humbly await your enlightenment,
seekerOfKnowledge



** UPDATE **

Thanks everyone, but I figured out I did not give enough detail and, after tracing through all the Perl modules involved (and libgd), have found the problem. The answer is, it can not be done without changing a module.


Let me explain:

First, an important detail: I wanted the GD font for the Chart-2.4.7 module, which passes it to GD. Additionally, I wanted ultimately to use a proportionally-spaced font.

To use a proportionally-spaced font, I need to modify chart to allow font specifications and not just GD font objects and I need to change all the text positioning code to take the flexible widths into account.

I did trace through the libgd code and found the on-disk format for its fixed fonts. So I can now use fixed fonts all day long. This will have to suffice, until I can take the time to modify Chart.



Thanks everyone for your quick response and help.

Replies are listed 'Best First'.
Re: Converting X11 fonts for GD
by BrowserUk (Patriarch) on Aug 23, 2012 at 14:57 UTC

    Did you miss the GD POD section entitled "Font Utilities"

    If you wish to add more built-in fonts, the directory bdf_scripts contains two contributed utilities that may help you convert X-Windows BDF-format fonts into the format that libgd uses internally. However these scripts were written for earlier versions of GD which included its own mini-gd library. These scripts will have to be adapted for use with libgd, and the libgd library itself will have to be recompiled and linked! Please do not contact me for help with these scripts: they are unsupported.

    Have you tried using TrueType fonts via stringFT()?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Converting X11 fonts for GD
by zentara (Archbishop) on Aug 23, 2012 at 15:27 UTC
    Another gotcha with GD fonts, is that it is easier to just copy the desired font to your working directory, to get the path right. Here is a simple example. Your GD must be compiled with FreeType IIRC, to enable ttf font support. But then, you can import any ttf font you like.
    #!/usr/bin/perl use GD; use strict; use warnings; my ($w, $h) = (200, 200); 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); # Generic.ttf is in script's working directory $img->stringFT($black,"./Generic.ttf",72,0,100,100,'A'); open (IMG, ">$0.png"); print IMG $img->png; close IMG;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      If the black cat hides in a coal cellar, is he really there?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        Sure it's there, you just need better infrared vision. :-)

        If you are referring to it being black text on a black background, it works on linux, black text on default white background. I don't know how Windows might color it.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://989323]
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: (10)
As of 2024-04-16 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found