metahoguet
clone your own copy | download snapshot

Snapshots | iceberg

Inside this repository

api.py
text/x-python

Download raw (2.5 KB)

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

from datetime import datetime
import subprocess
import time
from plottertools.shapes import box
from plottertools.font import Font
from plottertools.textbox import Textbox
from chiplotle.geometry.core.coordinate import Coordinate
from settings import db
import os.path
import shutil
from flask import Flask, Response, app, request, render_template

marginleft = 30 # margin left in mm
marginbottom = (271.75 / 3) # margin bottom in mm

page = {
  'left': int(marginleft * -40),
  'right': int((190 - marginleft) * 40),
  'bottom': int(marginbottom * -40),
  'top': int((271.75 - marginbottom) * 40)
}

pen = 1
speed = 10
force = 1

font=Font(path='fonts/converted/futural.fnt', resolution=1, scale=4.5)

app = Flask(__name__)

@app.route("/api/ploteps/")
def ploteps():
  epsfile = request.args.get('epsfile')
  charnum = os.path.splitext(os.path.basename(epsfile))[0]
  svgfile = './svg/{}.svg'.format(charnum)
  hpglfile = './hpgl/{}.hpgl'.format(charnum)

  shutil.copy(svgfile, './svg/snaps/{}-{}.svg'.format(charnum, int(time.time())))

  # converting eps to hpgl
  subprocess.call([
      'pstoedit',
      '-f',
      'plot-hpgl',
      epsfile,
      hpglfile
  ])

  handle = open(hpglfile) 
  dirtyhpgl = ''.join(handle.readlines()).replace('\n', '')
  cleanhpglchunks = []

  pen = 1
  pencount = 4
  penmap = {}

  for chunk in dirtyhpgl.split(';'):
    if chunk[0:2] in ['PA', 'PR', 'PU', 'PD']:
      cleanhpglchunks.append(chunk)
    elif chunk[0:2] == 'SP':
      sourcepen = int(chunk[2])
      if not sourcepen in penmap:
        pen += 1

        if pen > pencount:
          pen = 1

        penmap[sourcepen] = pen

      cleanhpglchunks.append('SP{}'.format(penmap[sourcepen]))

  cleanhpgl = ';'.join(cleanhpglchunks)

  textbox = Textbox(
      font=font,
      width = 75 * 40,
      position = Coordinate(page['right'] - (80 * 40), page['bottom'] + (12 * 40)),
      align = Textbox.alignLeft,
      lineHeight=1.40
  )

  textbox.insertText('{0} \n{1} \n\ntype.code, Maison du Livre, Bruxelles'.format(hpglfile, datetime.now().strftime("%d-%m-%Y %H:%M")))

  hpgl = 'IN;RO90;IP0,0,7600,10870;SC{0},{1},{2},{3};FS{5};SP{4};VS{6};PU;{7};PU;'.format(page['left'], page['right'], page['bottom'], page['top'], pen, force, speed, cleanhpgl)
  # hpgl += box((page['left'] + 200, page['bottom'] + 200), 200 * 40, 287 * 40)
  hpgl += textbox.hpgl()

  db.plots.insert({'hpgl': hpgl, 'plotted': False})

  return '100'

if __name__ == "__main__":
  app.run(host="localhost", port=5555, debug=True)