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

I'm in the midst of finishing up a Perl/Tk module which mingles a bit of GD to render things pTk Canvas can't. I need to render some text via GD::Text, but GD::Text needs a TTF font file path, rather than the family/weight/etc. that Perl/Tk uses, so I need to go fishing in the registry to extract the info. I'm wondering if there's already a CPAN module out there that does this ? I haven't found anything, and if there's nothing out there yet, I thought I'd whip up a little module that

1. Takes font family, weight, etc., (as from a pTk font object)

2. Forms a Win32 font name (e.g., capitalize the fontname pieces, append Bold and/or Italic if the weight/slant indicate it)

3. Use that result as a key to lookup and return the font file via Win32::TieRegistry

Obviously its not too complicated, but it seems like something that might be generally useful.

If anyone knows of a module that already does this, please advise. Otherwise, if anyone can suggest a namespace for this, that'd be great as well. I'm thinking it should be under Win32, not Tk or GD; maybe Win32::Font::NameToFile ?

Update:

Done. Uploaded to CPAN as Win32-Font-NameToFile-0.10.tar.gz. Also available here.

I ran some quick tests with the demo app from GD::Text and it behaves as desired (at least for my configuration). Given the vagueries of Win32 fonts, YMMV.

  • Comment on Win32 Module to return font filename from full font name/weight/slant ?

Replies are listed 'Best First'.
Re: Win32 Module to return font filename from full font name/weight/slant ?
by planetscape (Chancellor) on Jan 04, 2006 at 14:09 UTC

    Sounds like you have an excellent handle on the problem, and I doubt I have much to contribute, but there is a link that may be helpful (if you don't know about it already):


    Windows NT Workstation Resource Kit: Chapter 8 - Fonts

    I like your proposed name, "Win32::Font::NameToFile" . :-)

    HTH,

    planetscape
Re: Win32 Module to return font filename from full font name/weight/slant ?
by hsmyers (Canon) on Jan 04, 2006 at 16:26 UTC
    There are a couple of things that come to mind; first it's dangerous to assume that font file names are regular. I'm not sure myself as I look at the 600 or so fonts installed on my system(s) if I've ever seen rhyme or reason to the names. If you know otherwise then of course ignore the warning--YMMV. The other notion is that your module should have some means of telling you and the world if a particular font exists. One of the things missing from MSFT's bizarre approach to font tech is the idea of telling you that it can't find the font you are looking for; instead it cheerfully loads some other font that it thinks might be nice. Imagine trying this with symbol fonts---like say "Chess Alpha"! You might find http://www.codeproject.com/gdi/fontfile.asp useful.

    --hsm

    "Never try to teach a pig to sing...it wastes your time and it annoys the pig."
      Yes, the font naming can be a bit random, but in general the last pieces are the weight and slant (but what the difference between italic and oblique is I'm not certain, they look the same to me...). Fortunately, we have regexen, so dealing with those issues should be feasible.

      As to loading a "similar" font, I'd never knowingly do that; however, I am thinking of providing a "get_ttf_matching('string')" i/f that returns the list of fonts that start with 'string'.

      And the URL you gave is what got me rolling down this hill in the first place, but thanks for the pointer!

        It's not that you would load a similar font, under the windows regime, the OS loads a similar font if it can't find an exact match! Then it cheerfully goes on about your business as if what it did were perfectly reasonable. GRRRR! Sigh!!

        Typically the difference between italic and oblique is that an italic font is designed to slant, while oblique is just knocked up the side of it's head and made to tilt. Use italic over oblique is best from the typophile point of view.

        --hsm

        "Never try to teach a pig to sing...it wastes your time and it annoys the pig."