[PYTHON IMAGE-SIG] Using BDF font files with PIL

Fredrik Lundh fredrik.lundh@image.combitech.se
Tue, 20 May 1997 10:24:49 +0200


> The PIL "features" sheet says that BDF files can be used; how is
> this done?  I don't see it in the manual.  If I just missed it
> there, please point me in the right direction & I'll read it myself.

Let's see...

1. there's a bdf2pil utility in the Scripts directory, which reads
a BDF file and produces a PIL-compatible font file.

2. the ImageFont module can be used to read such files (use either
load or load_path)

3. the setfont() method in the ImageDraw class can be used to set the
font, and the text() method is then used to draw with it.

And yes, the bdf2pil script included in 0.2b4 chokes on some BDF files
(probably platform dependent).  The attached patch for pre-0.2b5
versions was contributed by Joel Shprentz.

Cheers	/F

--------------------------------------------------------------------

*** bdf2pil.py.orig     Thu Nov  7 13:57:44 1996
--- bdf2pil.py  Thu Nov  7 16:57:38 1996
***************
*** 69,87 ****
        i = string.find(s, " ")
        props[s[:i]] = s[i+1:-1]

      # load bitmap
      bitmap = []
      while 1:
        s = f.readline()
        if not s or s[:7] == "ENDCHAR":
            break
!       bitmap.append(s[:-1])
      bitmap = string.join(bitmap, "")
-
-     [w, h, x, y] = map(string.atoi, string.split(props["BBX"]))
-     [dx, dy] = map(string.atoi, string.split(props["DWIDTH"]))
-
-     bbox = (dx, dy, x, y, x+w, y+h)

      return id, string.atoi(props["ENCODING"]), bbox, bitmap

--- 69,88 ----
        i = string.find(s, " ")
        props[s[:i]] = s[i+1:-1]

+     [w, h, x, y] = map(string.atoi, string.split(props["BBX"]))
+     [dx, dy] = map(string.atoi, string.split(props["DWIDTH"]))
+
+     bbox = (dx, dy, x, y, x+w, y+h)
+
      # load bitmap
+     maxHexChars = 2 * ((w + 7) / 8)
      bitmap = []
      while 1:
        s = f.readline()
        if not s or s[:7] == "ENDCHAR":
            break
!       bitmap.append(s[:maxHexChars])
      bitmap = string.join(bitmap, "")

      return id, string.atoi(props["ENCODING"]), bbox, bitmap

_______________
IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org
_______________