hzeller / rpi-rgb-led-matrix

Controlling up to three chains of 64x64, 32x32, 16x32 or similar RGB LED displays using Raspberry Pi GPIO
GNU General Public License v2.0
3.65k stars 1.16k forks source link

Font loading #1508

Open galaxyZ2 opened 1 year ago

galaxyZ2 commented 1 year ago

Hello Everyone!

Please allow me another question I am fighting for quite a bit. I have a flask app, and the webpage prompts the user to choose a font type.

fontname = []
font_dir = '/home/linux/rpi-rgb-led-matrix/rpi-rgb-led-matrix/fonts/'
for root, dirs, files in os.walk(font_dir):
    for file in files:
        if file.endswith(".bdf"):
            fontname.append(file) 

Then the Display form is as follows:

class DisplayForm(FlaskForm):
    message = StringField('Messages')
    color = StringField('Color')
    font = SelectField('Font', choices=[(f, f) for f in fontname])
    submit = SubmitField('Submit')

Until this point everything works as expected, the SelectField is populated with the choices. After this I have my def:

@app.route('/display', methods=['GET','POST'])
def display():
    global color, x, y, message, fontname
    #logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
    form = DisplayForm()
    if form.validate_on_submit():
        message = form.message.data
        color = form.color.data
        if not message or not color:
            return render_template('display.html', form=form, error_mesage='Please enter the values')
        x = 0
        y = 20
        message = form.message.data
        color_code = form.color.data
        fonttype = form.font.data
        r, g, b = map(int, color_code.split(","))
        color = graphics.Color(r,g,b)
        font_path = os.path.join(font_dir, fonttype)
        font_path = graphics.Font()
        font.LoadFont(font_path)
        canvas.Clear()
        graphics.DrawText(canvas, font, x, y, color, message)
        matrix.SwapOnVSync(canvas)
        #return 'Displaying message: {}'.format(message)
        return render_template('display.html', form=form, fontname=fontname, color=color, x=x, y=y)

There is no exception thrown on the server side, but the text is not drawn, my thinking is that the font is not loaded correctly.

Could you point out my issue here?

galaxyZ2 commented 1 year ago

I am not sure if in the graphics.DrawText() font needs to be mentioned or font_path, neither of them have possitive effect on the outcome.

ledvinap commented 1 year ago

font_path = graphics.Font()

galaxyZ2 commented 1 year ago

font_path = graphics.Font()

I am now trying to create a font variable as follows then load that, but to no avail. :(

 font_path = os.path.join(font_dir, fonttype)
 font = graphics.Font(font_path)
 font.LoadFont(font)
 graphics.DrawText(canvas, font, x, y, color, message)
ledvinap commented 1 year ago

font_path = os.path.join(font_dir, fonttype) # string with path font = graphics.Font(); # empty Font object font.LoadFont(font_path ) # load font file into font object graphics.DrawText(canvas, font, x, y, color, message)

galaxyZ2 commented 1 year ago

Got it working, thank you for your help! I also had bad reference in the html.