commit 9f17cf2b52684efd023a37225d50b996c5a47002 Author: Sergiusz 'q3k' BazaƄski Date: Sun Aug 30 00:46:51 2015 +0200 Initial commit. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ecac4a5 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Use the source, luke :^) diff --git a/render.py b/render.py new file mode 100644 index 0000000..3a3b555 --- /dev/null +++ b/render.py @@ -0,0 +1,109 @@ +import math +import os +import StringIO +import subprocess +import tempfile +import time + +import cairo +import flask +import pango +import pangocairo + +class App(flask.Flask): + def __init__(self, *args, **kwargs): + super(App, self).__init__(*args, **kwargs) + self.last = 0 + + +app = App(__name__) + +class Renderer(object): + INCH_PER_MM = 0.039 + DPI = 300 + def __init__(self, size=(19,51)): + width, height = [int(s * self.INCH_PER_MM * self.DPI) for s in size] + surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) + context = cairo.Context(surface) + # fill it white, while we're at it + context.rectangle(0, 0, width, height) + context.set_source_rgb(1, 1, 1) + context.fill() + + context.translate(width, 0) + context.rotate(math.pi/2) + # yolo + self.width, self.height = height, width + + self.context = context + self.surface = surface + + def export_png(self, name): + with open(name, 'w') as f: + self.surface.write_to_png(f) + + def render_text(self, text, fontname, x, y): + self.context.save() + 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) + font = pango.FontDescription(fontname) + layout.set_font_description(font) + layout.set_text(text) + layout.set_alignment(pango.ALIGN_CENTER) + + if y == -1: + self.context.translate(0, (self.height - layout.get_size()[1]/pango.SCALE)/2) + + self.context.set_source_rgb(0, 0, 0) + pangocairo_context.update_layout(layout) + pangocairo_context.show_layout(layout) + + self.context.restore() + +@app.route('/') +def index(): + return flask.render_template('index.html') + +@app.route('/stuff/preview//') +def stuff_preview(size): + text = flask.request.args.get('text') + r = Renderer() + r.render_text(text, 'Sans {}'.format(size), 0, -1) + + sio = StringIO.StringIO() + r.surface.write_to_png(sio) + sio.seek(0) + return flask.send_file(sio, mimetype='image/png') + +DELAY = 5 +@app.route('/stuff/print//', methods=['POST']) +def stuff_print(size): + last = app.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') + r = Renderer() + r.render_text(text, 'Sans {}'.format(size), 0, -1) + path = '/tmp/hslabel' + f = open(path, 'w') + + r.surface.write_to_png(f) + f.flush() + f.close() + time.sleep(1) + ex = 'lpr -P DYMO_LabelWriter_450 {}'.format(path) + os.system(ex) + + f.close() + app.last = time.time() + return 'ok' + +if __name__ == '__main__': + app.run(debug=True) diff --git a/templates/.index.html.swp b/templates/.index.html.swp new file mode 100644 index 0000000..0721da1 Binary files /dev/null and b/templates/.index.html.swp differ diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0772f57 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,97 @@ + + + + + Hackerspace Printing System For Printing + + + + +
+ +

Box 'o Stuff Label For SAMLA boxes with common equipment

+
+
+

Preview

+
+
+
+

Settings

+
+ + +
+
+ + +
+ + +
+
+

API

+
$ # To get a label preview
+$ curl http://label.waw.hackerspace.pl/stuff/preview/60/?text=foobar | feh -
+$ # To print the label
+$ curl -d "" http://label.waw.hackerspace.pl/stuff/print/60/?text=foobar
+
+
+
+ + + + + +