Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Module to map fontnames to X11 fonts.dir entries ?

by renodino (Curate)
on Dec 12, 2007 at 18:11 UTC ( #656682=perlquestion: print w/replies, xml ) Need Help??
renodino has asked for the wisdom of the Perl Monks concerning the following question:

I've written a module to generate bitmapped versions of fonts for use with HTML <canvas>'s. I use Imager to render the font's character set, and it needs font filenames on non-Windows platforms (rather than the font family/size/weight/etc.), which isn't exactly user friendly. So I'm looking for a module that can take in a human readable font spec, and spit out a filename(s).

I know of several modules which somehow manage to do this (e.g., Tk), but can't find the code that does it. So before I run off and build the Linux equivalent of Win32::Font::NameToFile, I'm hoping some enlightened monk might know if this is already solved on CPAN somewhere ?

FWIW: in case anyone's interested, the format I'm trying to parse is XLFD

Perl Contrarian & SQL fanboy
  • Comment on Module to map fontnames to X11 fonts.dir entries ?

Replies are listed 'Best First'.
Re: Module to map fontnames to X11 fonts.dir entries ?
by almut (Canon) on Dec 12, 2007 at 19:54 UTC

    Probably not exactly what you're looking for, but maybe a useful starting point for further investigations:  fontconfig comes with a little utility fc-match that maps logical names / property patterns to real font files installed in the system — presuming fontconfig itself is installed and properly configured. The details of the matching algorithm, valid property specs, etc. are described in the docs.  For example,

    $ fc-match Times:italic

    would output

    n021023l.pfb: "Nimbus Roman No9 L" "Regular Italic"

    or more verbosely (which includes the absolute path to the font file):

    $ fc-match --verbose sans:bold Pattern has 28 elts (size 32) family: "Arial"(s) style: "Bold"(s) stylelang: "xx"(s) slant: 0(i)(s) weight: 200(i)(s) width: 100(i)(s) size: 12(f)(s) pixelsize: 12.5(f)(s) foundry: "monotype"(s) antialias: FcTrue(s) hintstyle: 3(i)(s) hinting: FcTrue(s) verticallayout: FcFalse(s) autohint: FcTrue(s) globaladvance: FcTrue(s) file: "/usr/X11R6/lib/X11/fonts/truetype/truetype/arialbd.ttf" +(s) index: 0(i)(s) outline: FcTrue(s) scalable: FcTrue(s) dpi: 75(f)(s) rgba: 1(i)(w) scale: 1(f)(s) charset: set(s) lang: aa|af|ar|ast|ava|ay|az|az-ir|be|bg|bi|bin|br|bs|bua|ca|. +.. fontversion: 184812(i)(s) capability: "otlayout:arab"(s) fontformat: "TrueType"(s) embeddedbitmap: FcFalse(w)

    With --sort you'd get a sorted list of best matching fonts...

    I have no idea, however, how that relates to the XLFD that you mentioned. Also, I'm not aware of any Perl binding to the fontconfig library (but it shouldn't be too difficult to create one, should you prefer to make direct calls into the lib, instead of using the supplied utility — the interface is reasonably well documented).

      Many thnx.

      But that seems like a sledgehammer to swat a fly (albeit, a very pesky fly!).

      So I guess its time to start slinging some Perl. I'm surprised this problem hasn't yet been addressed, it seems like a reasonably common issue thats been around for more than a decade. So I'm merging Win32::Font::NameToFile with an XLFD handler, and maybe look into an OS X solution as well. Then we'll finally have a single solution (well, sortof).

      Perl Contrarian & SQL fanboy
Re: Module to map fontnames to X11 fonts.dir entries ?
by traveler (Parson) on Dec 13, 2007 at 01:51 UTC
    It sounds as though your program is sort or like *nix's xfd(1) or showfont(1). The source for those may help in your quest.

    HTH --traveler

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://656682]
Approved by almut
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-04-22 20:53 GMT
Find Nodes?
    Voting Booth?