tags:

views:

290

answers:

2

I've got a Python program using PIL to render text, and it works great with all kinds of fonts. But it only draws "missing glyph" rectangles with wingdings or webdings.

Here's a sample that tries to draw every unicode character:

# Run this with .ttf file path as an argument, and also an encoding if you like.
# It will make 16 PNGs with all the characters drawn.
import sys
import Image, ImageDraw, ImageFont

size = 20
per = 64

chars = 0x10000
perpage = per*per

fontfile = sys.argv[1]
encoding = sys.argv[2] if len(sys.argv) > 2 else ''

font = ImageFont.truetype(sys.argv[1], size, encoding=encoding)

for page in range(0, chars//perpage):

    im = Image.new("RGB", (size*per+30, size*per+30), '#ffffc0')
    draw = ImageDraw.Draw(im)

    for line in range(0, per):
        for col in range(0, per):
            c = page*perpage + line*per + col
            draw.text((col*size, line*size), unichr(c), font=font, fill='black')

    im.save('allchars_%03d.png' % page)

With Arial.ttf (or even better, ArialUni.ttf), I get 16 interesting PNGs. Searching for issues with PIL, some symbol fonts need to have their encoding specified. If I use Symbol.ttf, I get all missing glyphs until I specify "symb" as the encoding.

How do I get wingdings to work?

+1  A: 

I must have done something wrong before. "symb" as the encoding works for wingdings too! Sorry for the noise...

Ned Batchelder
+1  A: 

Not all of wingdings is mapped to unicode: see http://www.alanwood.net/demos/wingdings.html

Also you're only covering the Basic Multilingual Plane (wikipedia)

Douglas Leeder
This is one of those cases where we use a font simply as a set of glyphs, with no semantic content. So 'c' is a large empty box, etc.
Ned Batchelder