Font::BDF::Reader - Module for reading in BDF files


  use Font::BDF::Reader;
  my $BDF               = Font::BDF::Reader->new( $bdf_filename );

  my @starchars         = $BDF->get_all_STARTCHAR;
  sub font_to_png {
  foreach my $starchar (@starchars) {
    my $png_data        = font_to_png( $BDF->get_font_info_by_STARTCHAR( $starchar ) );
    my $png_file        = "$bdf_base.$starchar.png";
    my $FH              = IO::File->new( ">$png_file" )
      || die "Error opening file for write: '$png_file'";
    binmode $FH;
    print $FH $png_data;


  This module supports importing data from BDF files.  A BDF file is
  an ASCII file that defines a font.  The fonts are bitmap fonts, and
  are easily converted to other formats.


This module imports data from a BDF file. Have a look at the BDF file spec at


new ( [ BDF_FILE ] )

Creates a Font::BDF object. If BDF_FILE is specified, it attempts to read in the entire file. (which can eat up a lot of memory, depending on the size of the file)


open_bdf_file ( BDF_FILE )

Opens a BDF file without doing anything. Will die() if it can't find or open BDF_FILE.


Assumes open_bdf_file() has been called. It reads in the BDF metadata, using read_bdf_metadata(), and then reads in ALL of the characters in the BDF file using read_bdf_chars(). Will die() if the BDF file has not been opened yet.


Attempts to read in the metadata block of the BDF file, which occurs before any character data. Assumes open_bdf_file() has been called.


Returns a HASHREF containing the metadata for the BDF file. Each attribute consists of a key-value pair. The value is always one scalar. Here's an example of the metadata from a font file:

          'SIZE' => '48 100 100',
          'STARTFONT' => '2.1',
          'FONT_ASCENT' => '46',
          'FONT' => '-watanabe-fixed-medium-r-normal--48-450-75-75-c-480-jisx0208.1983-0',
          'COMMENT' => undef,
          'ENDPROPERTIES' => undef,
          'STARTPROPERTIES' => '4',
          'DEFAULT_CHAR' => '41377',
          'COPYRIGHT' => '"Public Domain"',
          'CHARS' => '8890',
          'FONTBOUNDINGBOX' => '48 48 0 -2',
          'FONT_DESCENT' => '2'

Reads in ALL of the characters. For a file containing 8000 characters, each 48 by 48 pixels, approximately 16MB of memory is used. Non-asian character sets will have significantly smaller memory requirements.

This procedure also keeps track of the number of characters read, issuing a warning if the number of characters does not equal the expected number of characters as specified in the metadata section.


Reads in the next character. Returns 0 if there are no more characters.


Returns all of the STARTCHAR keys in a list ordered alphabetically. Each font has a unique STARTCHAR and ENCODING that it is indexed on.


Returns all of the ENCODING keys in a list ordered alphabetically.

get_font_info_by_STARTCHAR ( STARTCHAR )

Returns the font info for a particular STARTCHAR. Returns undef if no information exists for STARTCHAR.

The following is an example of font information returned by this routine: { 'BITMAP' => [ '000000000000', <SNIP, SNIP> '000000000000' ], 'ENCODING' => [ '8481' ], 'DWIDTH' => [ '48', '0' ], 'BBX' => [ '48', '48', '0', '-2' ], 'SWIDTH' => [ '150', '0' ], 'STARTCHAR' => '2121' }

This is basically the most direct conversion from the BDF file to a Perl hash.

get_font_info_by_ENCODING ( ENCODING )

Returns the font info for a particular ENCODING. Returns undef if no information exists for ENCODING


Clears the entire font cache.

clear_font_info_by_STARTCHAR ( STARTCHAR )

Clears the information for a font by STARTCHAR.

clear_font_info_by_ENCODING ( ENCODING )

Clears the information for a font by ENCODING.




See the script bdf2png for example usage of this module.

The specifications for the BDF format can be found here:


Desmond Lee, <<gt>


Copyright 2003 by Desmond Lee

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.