faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 11 people (36%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

Utility for creating fonts.scale files from AFM files

Jul 5th, 2000 10:02
Nathan Wallace, Hans Nowak, Snippet 223, M.-A. Lemburg


"""
Packages: interfaces;text.special_formats
"""
"""
Below you find a little utility that I've written to enable
fonts given as AFM/PFB files under X11. I needed those fonts
for StarOffice/Linux and thought the utility might be use to some
of you:
"""
#!/usr/local/bin/python
""" mkfontscales.py - Create a fonts.scale file from AFM and PFB font files
                      found in the current directory.
    1999, Marc-Andre Lemburg, mailto:mal@lemburg.com
"""
import string,os
def parse_afm(filename,
              split=string.split,join=string.join,
              atof=string.atof,lower=string.lower):
    lines = open(filename).readlines()
    foundry = 'adobe'
    foundry,family,wheight,slant,spacing = \
                'adobe',filename[:-4],'normal','r','p'                   
    for line in lines:
        # Split line
        try:
            splitline = split(line)
        except ValueError:
            continue
        if len(splitline) >= 2:
            name,args = splitline[0],splitline[1:]
        else:
            continue
        # Parse information
        if name == 'FamilyName':
            family = lower(join(args,' '))
        elif name == 'Weight':
            wheight = lower(args[0])
        elif name == 'ItalicAngle':
            angle = atof(args[0])
            if angle != 0.0:
                slant = 'i'
            else:
                slant = 'r'
        elif name == 'IsFixedPitch':
            if args[0] == 'false':
                spacing = 'p'
            else:
                spacing = 'm'
    return ('',foundry,family,wheight,slant,'normal',
            '','0','0','0','0',spacing,'0','iso8859','1')
def filter_afm(filename):
    return filename[-4:] == '.afm'
def main():
    fonts = filter(filter_afm,os.listdir('.'))
    dir = {}
    print 'Working...'
    for font in fonts:
        print ' %s' % font
        Xname = string.join(parse_afm(font),'-')
        dir[font[:-4] + '.pfb'] = Xname
    fontscale = open('fonts.scale','w')
    fontscale.write('%i\n' % len(dir))
    ids = dir.items()
    ids.sort()
    for id in ids:
        fontscale.write('%s %s\n' % id)
    fontscale.close()
if __name__ == '__main__':
    main()
"""
Usage is pretty simple: cd to the dir where you've stored the
AFM/PFB files (it has to be on the font path of your server)
and let the script run. Then tell the X11 server
to load the new setup with 'xset fp rehash'. That's it.
BTW, the CorelDraw CDs include tons of these fonts...
"""