LJ/examples/python/text.py

92 lines
3.0 KiB
Python
Raw Normal View History

2023-07-16 23:22:27 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
Static text writer
pip3 install Hershey-Fonts
v0.1.0
Font list :
'futural', 'astrology', 'cursive', 'cyrilc_1', 'cyrillic', 'futuram', 'gothgbt', 'gothgrt',
'gothiceng', 'gothicger', 'gothicita', 'gothitt', 'greek', 'greekc', 'greeks', 'japanese',
'markers', 'mathlow', 'mathupp', 'meteorology', 'music', 'rowmand', 'rowmans', 'rowmant',
'scriptc', 'scripts', 'symbolic', 'timesg', 'timesi', 'timesib', 'timesr', 'timesrb'
Licensed under GNU GPLv3
by cocoa and Sam Neurohack
'''
import time
import argparse
from HersheyFonts import HersheyFonts
import redis
argsparser = argparse.ArgumentParser(description="Text generator")
argsparser.add_argument("-s","--speed",help="Frame Per Second (30)",default=30,type=int)
argsparser.add_argument("-x","--xpos",help="Position X (0)",default=0,type=int)
argsparser.add_argument("-y","--ypos",help="Position Y (0)",default=0,type=int)
argsparser.add_argument("-v","--verbose",action="store_true",default="True",help="Verbose output (true)")
argsparser.add_argument("-t","--text",help="Text to display, default hello",default="hello",type=str)
argsparser.add_argument("-f","--font",help="Herschey font to use. (futural) One of ({})".format(", ".join(HersheyFonts().default_font_names)),default="futural",type=str)
argsparser.add_argument("-i","--ip",help="IP address of the Redis server (127.0.0.1)",default="127.0.0.1",type=str)
argsparser.add_argument("-p","--port",help="Port of the Redis server (6379)",default="6379",type=str)
argsparser.add_argument("-k","--key",help="Redis key to update, default (/pl/0/0)",default="/pl/0/0",type=str)
args = argsparser.parse_args()
fps = args.speed
verbose = args.verbose
text = args.text
fontname = args.font
looptime = 1 / fps
r=redis.StrictRedis(host=args.ip, port=args.port, db=0)
def debug(text):
if verbose:
print(text)
def startFrame():
return time.time()
def endFrame(timer):
if not looptime :
debug( "No looptime provided at init.")
return
elapsed = time.time() - timer
if( elapsed < looptime ):
delta = looptime - elapsed
time.sleep( delta )
# Useful variables init.
def rgb2int(rgb):
return int('0x%02x%02x%02x' % tuple(rgb),0)
# Useful variables init.
white = rgb2int((255,255,255))
red = rgb2int((255,0,0))
blue = rgb2int((0,0,255))
green = rgb2int((0,255,0))
color = 65280
shape = []
thefont = HersheyFonts()
#thefont.load_default_font()
thefont.load_default_font(fontname)
thefont.normalize_rendering(120)
for (x1, y1), (x2, y2) in thefont.lines_for_text(text):
shape.append((x1+args.xpos, -y1+args.ypos, color))
shape.append((x2+args.xpos ,-y2+args.ypos, color))
while True:
timer = startFrame()
if r.set(args.key,str(shape))==True:
debug("redis set("+str(args.key)+") to "+str(shape))
print(shape, flush = True);
endFrame(timer)