Commit 62a1695c authored by Michael Murtaugh's avatar Michael Murtaugh

Bdf and pilfont style conversion

parent fd3d06e9
#
# The Python Imaging Library
# $Id$
#
# bitmap distribution font (bdf) file parser
#
# history:
# 1996-05-16 fl created (as bdf2pil)
# 1997-08-25 fl converted to FontFile driver
# 2001-05-25 fl removed bogus __init__ call
# 2002-11-20 fl robustification (from Kevin Cazabon, Dmitry Vasiliev)
# 2003-04-22 fl more robustification (from Graham Dumpleton)
#
# Copyright (c) 1997-2003 by Secret Labs AB.
# Copyright (c) 1997-2003 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#
from __future__ import print_function
from PIL import FontFile, Image
# --------------------------------------------------------------------
# parse X Bitmap Distribution Format (BDF)
# --------------------------------------------------------------------
bdf_slant = {
"R": "Roman",
"I": "Italic",
"O": "Oblique",
"RI": "Reverse Italic",
"RO": "Reverse Oblique",
"OT": "Other",
}
bdf_spacing = {"P": "Proportional", "M": "Monospaced", "C": "Cell"}
def bdf_char(f):
# skip to STARTCHAR
while True:
s = f.readline()
if not s:
return None
if s[:9] == b"STARTCHAR":
break
id = s[9:].strip().decode("ascii")
# load symbol properties
props = {}
while True:
s = f.readline()
if not s or s[:6] == b"BITMAP":
break
i = s.find(b" ")
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
# load bitmap
bitmap = []
while True:
s = f.readline()
if not s or s[:7] == b"ENDCHAR":
break
bitmap.append(s[:-1])
bitmap = b"".join(bitmap)
[x, y, l, d] = [int(p) for p in props["BBX"].split()]
[dx, dy] = [int(p) for p in props["DWIDTH"].split()]
bbox = (dx, dy), (l, -d - y, x + l, -d), (0, 0, x, y)
try:
im = Image.frombytes("1", (x, y), bitmap, "hex", "1")
except ValueError:
# deal with zero-width characters
im = Image.new("1", (x, y))
return id, int(props["ENCODING"]), bbox, im
##
# Font file plugin for the X11 BDF format.
class BdfFontFile(FontFile.FontFile):
def __init__(self, fp):
FontFile.FontFile.__init__(self)
s = fp.readline()
if s[:13] != b"STARTFONT 2.1":
raise SyntaxError("not a valid BDF file")
props = {}
comments = []
while True:
s = fp.readline()
if not s or s[:13] == b"ENDPROPERTIES":
break
i = s.find(b" ")
props[s[:i].decode("ascii")] = s[i + 1 : -1].decode("ascii")
if s[:i] in [b"COMMENT", b"COPYRIGHT"]:
if s.find(b"LogicalFontDescription") < 0:
comments.append(s[i + 1 : -1].decode("ascii"))
while True:
c = bdf_char(fp)
if not c:
break
id, ch, (xy, dst, src), im = c
if 0 <= ch < len(self.glyph):
self.glyph[ch] = xy, dst, src, im
if __name__ == "__main__":
ap = argparse.ArgumentParser("")
ap.add_argument("input", nargs="+")
args = ap.parse_args()
for n in args.input:
with open(font_file_path,'rb') as fp:
p = BdfFontFile.BdfFontFile(fp) #PcfFontFile if you're reading PCF files
# won't overwrite, creates new .pil and .pdm files in same dir
p.save(font_file_path)
......@@ -5,7 +5,9 @@ from PIL import Image
from PIL import ImageFont, ImageDraw
import sys
import argparse
from BdfFontFile import BdfFontFile
import json
from etherpad import changeset_parse, perform_changeset_curline
# from BdfFontFile import BdfFontFile
class ImageScroller(SampleBase):
......@@ -13,7 +15,7 @@ class ImageScroller(SampleBase):
super(ImageScroller, self).__init__(*args, **kwargs)
# self.parser.add_argument("-i", "--image", help="The image to display", default="../../../examples-api-use/runtext.ppm")
self.parser.add_argument("-t", "--text", help="The text to scroll on the RGB LED panel", default="Hello world!")
self.parser.add_argument("--pad", type=argparse.FileType("r"), default="../constantcodeofconduct2.etherpad")
self.parser.add_argument("--pad", type=argparse.FileType("r"), default="../constantcodeofconduct2.etherpad.json")
self.parser.add_argument("--padname", default="constantcodeofconduct2")
self.parser.add_argument("--font", default="fonts/9x18.bdf")
......@@ -26,6 +28,13 @@ class ImageScroller(SampleBase):
pad_data = data["pad:{0}".format(padname)]
last_rev = pad_data['head']
text = "\n"
self.image = Image.new("RGB", (self.matrix.width * 2, self.matrix.height), (0,0,0))
draw = ImageDraw.ImageDraw(self.image)
# font = ImageFont.load(self.args.font)
double_buffer = self.matrix.CreateFrameCanvas()
img_width, img_height = self.image.size
for r in range(0, last_rev+1):
d = data["pad:{0}:revs:{1}".format(padname, r)]
cs = changeset_parse(d['changeset'])
......@@ -38,34 +47,18 @@ class ImageScroller(SampleBase):
line = lines[linepos]
pre, post = line[:charpos], line[charpos:]
pre = pre[-40:]
post = post[:40]
sys.stdout.write("\r{0}|{1}{2}".format(pre, post, " "*64))
sys.stdout.flush()
sleep(0.25)
self.image = Image.new("RGB", (self.matrix.width * 2, self.matrix.height), (0,0,0))
draw = ImageDraw.ImageDraw(self.image)
font = ImageFont.load(self.args.font)
draw.text((10, 10), "hello", font=font)
double_buffer = self.matrix.CreateFrameCanvas()
img_width, img_height = self.image.size
# let's scroll
xpos = 0
while True:
xpos += 1
if (xpos > img_width):
xpos = 0
double_buffer.SetImage(self.image, -xpos)
# double_buffer.SetImage(self.image, -xpos + img_width)
pre = pre[-10:]
post = post[:5]
# sys.stdout.write("\r{0}|{1}{2}".format(pre, post, " "*64))
# sys.stdout.flush()
msg = "{0}|{1}".format(pre, post)
draw.rectangle((0, 0, img_width, img_height), fill=(0,0,0))
draw.text((0, 10), msg)
double_buffer.SetImage(self.image, 0)
double_buffer = self.matrix.SwapOnVSync(double_buffer)
# time.sleep(0.01)
time.sleep(0.025)
time.sleep(0.1)
# Main function
# e.g. call with
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment