forked from hswaw/labelmaker
WIP: Upgrade to Python3, change dependencies
Due to old pangocairo bindings seemingly not being updated anymore I resorted to using cffi-based bindings for now. Current problems with pango and pangocairo bindings is that they are incomplete, and it seems like outside HTML parsing there are some broken things about font rendering, which I need to deal with. I might look into rebuilding this functionality using something else, maybe Pillow. Health check was disabled for now so I can focus on image rendering.
This commit is contained in:
parent
b366bb265b
commit
db76e8a64f
1 changed files with 26 additions and 24 deletions
50
render.py
50
render.py
|
@ -1,15 +1,14 @@
|
|||
import json
|
||||
|
||||
import math
|
||||
import os
|
||||
import StringIO
|
||||
import io
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
import cairo
|
||||
import cairocffi as cairo
|
||||
import flask
|
||||
import pango
|
||||
import pangocairo
|
||||
import pangocffi as pango
|
||||
import pangocairocffi as pangocairo
|
||||
|
||||
class App(flask.Flask):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -50,26 +49,27 @@ class Renderer(object):
|
|||
if y != -1:
|
||||
self.context.translate(x, y)
|
||||
|
||||
pangocairo_context = pangocairo.CairoContext(self.context)
|
||||
pangocairo_context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
|
||||
layout = pangocairo_context.create_layout()
|
||||
layout.set_width(self.width*pango.SCALE)
|
||||
layout.set_alignment(pango.ALIGN_CENTER)
|
||||
self.context.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
|
||||
layout = pangocairo.create_layout(self.context)
|
||||
layout.width = pango.units_from_double(self.width)
|
||||
layout.alignment = pango.Alignment.CENTER
|
||||
|
||||
if html:
|
||||
# Absolutely horrifying hack to fix broken text wrapping
|
||||
layout.set_markup('<span font_desc="%s">%s</span>' % (fontname, text))
|
||||
layout.apply_markup('<span font_desc="%s">%s</span>' % (fontname, text))
|
||||
else:
|
||||
font = pango.FontDescription(fontname)
|
||||
layout.set_font_description(font)
|
||||
layout.set_text(text)
|
||||
font = pango.FontDescription()
|
||||
font.family = "Sans"
|
||||
font.size = 65
|
||||
layout.font_description = font
|
||||
layout.text = text
|
||||
|
||||
if y == -1:
|
||||
self.context.translate(0, (self.height - layout.get_size()[1]/pango.SCALE)/2)
|
||||
self.context.translate(0, (self.height - pango.units_to_double(layout.get_size()[1]))/2)
|
||||
|
||||
self.context.set_source_rgb(0, 0, 0)
|
||||
pangocairo_context.update_layout(layout)
|
||||
pangocairo_context.show_layout(layout)
|
||||
pangocairo.update_layout(self.context, layout)
|
||||
pangocairo.show_layout(self.context, layout)
|
||||
|
||||
self.context.restore()
|
||||
|
||||
|
@ -99,10 +99,12 @@ def healthcheck():
|
|||
return False, line
|
||||
mark = False
|
||||
|
||||
@app.route('/health')
|
||||
def health():
|
||||
ok, details = healthcheck()
|
||||
return json.dumps({'ok': ok, 'details': details})
|
||||
# TODO: Uncomment and fix this
|
||||
|
||||
# @app.route('/health')
|
||||
# def health():
|
||||
# ok, details = healthcheck()
|
||||
# return json.dumps({'ok': ok, 'details': details})
|
||||
|
||||
@app.route('/stuff/preview/<int:size>/')
|
||||
def stuff_preview(size):
|
||||
|
@ -111,7 +113,7 @@ def stuff_preview(size):
|
|||
r = Renderer()
|
||||
r.render_text(text, 'Sans {}'.format(size), 0, -1, html)
|
||||
|
||||
sio = StringIO.StringIO()
|
||||
sio = io.BytesIO()
|
||||
r.surface.write_to_png(sio)
|
||||
sio.seek(0)
|
||||
return flask.send_file(sio, mimetype='image/png')
|
||||
|
@ -122,7 +124,7 @@ def stuff_print(size):
|
|||
if not healthcheck()[0]:
|
||||
return 'Printer is down.'
|
||||
last = app.last
|
||||
print last, time.time() - last
|
||||
print(last, time.time() - last)
|
||||
if time.time() - last < DELAY:
|
||||
return 'Please wait {} more seconds before next print.'.format(int(DELAY - (time.time() - last)))
|
||||
text = flask.request.args.get('text')
|
||||
|
|
Loading…
Reference in a new issue