def POST(self): postdata = web.input(masters='', project_id=0) project = models.Project.get(id=postdata.project_id) if not project: raise web.notfound() masters = project.get_ordered_masters() self.set_masters(masters) self.initialize(project.projectname, masters[0].version, masters[1].version) self.call_metapost_all_glyphs(self.get_lft_master()) instance = models.Instance.create(project_id=project.id) for extension in ['-webfont.eot', '-webfont.ttf', '.otf']: source = project.get_basename() + extension dest_dir = op.join(working_dir(), 'instances') if not op.exists(dest_dir): os.makedirs(dest_dir) dest_file = op.join(dest_dir, '%s%s' % (instance.id, extension)) try: shutil.copy(op.join(working_dir(), 'static', source), dest_file) except: pass return simplejson.dumps({})
def get_instancelog(self, version=1, ab_source=None): if ab_source: return op.join(working_dir(), '%s-%s-%03d.log' % (self.projectname, ab_source.upper(), version)) return op.join(working_dir(), '%s.log' % (self.projectname))
def prepare_master_environment(master): for f in os.listdir(working_dir('commons', user='******')): filename = working_dir(op.join('commons', f), user='******') try: shutil.copy2(filename, master.get_fonts_directory()) except (IOError, OSError): raise writeGlobalParam(master.project)
def ufo2mf(master): dirnamef1 = working_dir(op.join(master.get_fonts_directory('a'), "glyphs")) dirnamef2 = working_dir(op.join(master.get_fonts_directory('b'), "glyphs")) dirnamep1 = working_dir(op.join(master.get_fonts_directory(), "glyphs")) if not op.exists(dirnamep1): os.makedirs(dirnamep1) charlist1 = filter(lambda f: fnextension(f) == 'glif', os.listdir(dirnamef1)) charlist2 = filter(lambda f: fnextension(f) == 'glif', os.listdir(dirnamef2)) for ch1 in charlist1: if ch1 in charlist2: fnb, ext = buildfname(ch1) glyphA = Glyph.get(idmaster=master.idmaster, fontsource='A', name=fnb) glyphB = Glyph.get(idmaster=master.idmaster, fontsource='B', name=fnb) xmltomf.xmltomf1(master, glyphA, glyphB)
def get_fonts_directory(self, ab_source=None): """ Return uploaded user font project directory. If ab_source set then return FontA or FontB directories in fonts project directory. >>> master = query(Master).filter(idmaster=1).one() >>> master.get_fonts_directory() /var/www/webpy-app/metapolator/users/1/fonts/1 >>> master.get_fonts_directory('a') /var/www/webpy-app/metapolator/users/1/fonts/1/FontNameA.UFO >>> master.get_fonts_directory('b') /var/www/webpy-app/metapolator/users/1/fonts/1/FontNameB.UFO If FontB is not set then it returns FontA for that >>> master = query(Master).filter(idmaster=2).one() >>> master.get_fonts_directory() /var/www/webpy-app/metapolator/users/1/fonts/1 >>> master.get_fonts_directory('a') /var/www/webpy-app/metapolator/users/1/fonts/1/FontNameA.UFO >>> master.get_fonts_directory('b') /var/www/webpy-app/metapolator/users/1/fonts/1/FontNameA.UFO """ fonts_directory = op.join(working_dir(), 'fonts', str(self.idmaster)) if not ab_source: return fonts_directory if ab_source.lower() == 'a': return op.join(fonts_directory, self.fontnamea) elif ab_source.lower() == 'b' and self.fontnameb: return op.join(fonts_directory, self.fontnameb) return op.join(fonts_directory, self.fontnamea)
def POST(self, name, glyphid): if not is_loggedin(): return web.notfound() master = models.Master.get(fontname=name) if not master: return web.notfound() x = web.input(pointnr='', source='', x='', y='') query = models.GlyphOutline.filter(idmaster=master.idmaster, fontsource=x.source.upper(), glyphname=glyphid, pointnr=x.pointnr) query.update(dict(x=x.x, y=x.y)) web.ctx.orm.commit() writeGlyphlist(master, glyphid) glyphA = models.Glyph.get(idmaster=master.idmaster, fontsource='A', name=glyphid) glyphB = models.Glyph.get(idmaster=master.idmaster, fontsource='A', name=glyphid) import xmltomf xmltomf.xmltomf1(master, glyphA, glyphB) makefont(working_dir(), master) M_glyphjson = get_edges_json(u'%s.log' % master.fontname, glyphid) if x.source.upper() == 'A': glyphjson = get_edges_json(u'%sA.log' % master.fontname, glyphid) else: glyphjson = get_edges_json(u'%sB.log' % master.fontname, glyphid) return simplejson.dumps({'M': M_glyphjson, 'R': glyphjson})
def makefont_single(master, cell=''): if not project_exists(master): return False cell = cell.upper() os.environ['MFINPUTS'] = master.get_fonts_directory() os.environ['MFMODE'] = session.get('mfparser', '') if cell == 'A': metafont = master.get_metafont('a') else: metafont = master.get_metafont() import subprocess process = subprocess.Popen( ["sh", "makefont.sh", metafont], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=working_dir() ) while True: line = process.stdout.readline() if not line or '<to be read again>' in line: process.kill() break
def GET(self): if not is_loggedin(): raise seeother('/login') mmaster = models.Master.all() fontlist = [f for f in glob.glob(working_dir('fonts') + "/*/*.ufo")] fontlist.sort() form = FontForm() return render.font1(fontlist, form, mmaster, cFont)
def prepare_environment_directory(force=False): filelist = ['makefont.sh', 'mf2pt1.mp', 'mf2pt1.pl', 'mf2pt1.texi', 'mtp.enc'] static_directory = op.join(working_dir(), 'static') if not op.exists(static_directory): os.makedirs(static_directory) if op.exists(op.join(working_dir(), 'makefont.sh')) and not force: return for filename in filelist: try: shutil.copy2(op.join(working_dir(user='******'), filename), op.join(working_dir())) except (OSError, IOError): raise
def get_fonts_directory(master, ab_source=None): fonts_directory = op.join(working_dir(), 'fonts', str(master.idmaster)) if ab_source.lower() == 'a': return op.join(fonts_directory, master.FontNameA) elif ab_source.lower() == 'b' and master.FontNameB: return op.join(fonts_directory, master.FontNameB) elif ab_source.lower() == 'b': return op.join(fonts_directory, master.FontNameA) return fonts_directory
def GET(self, id): if not is_loggedin(): raise seeother('/login') mmaster = models.Master.all() fontname = cFont.fontname fontna = cFont.fontna fontnb = cFont.fontnb fontlist = [f for f in glob.glob(working_dir('fonts') + "/*/*.ufo")] fontlist.sort() form = FontForm() form.fill({'Name': fontname, 'UFO_A': fontna, 'UFO_B': fontnb}) if id == 'i0': cFont.loadoption = 0 cFont.loadoptionAll = 0 model.putFont() return render.font1(fontlist, form, mmaster, cFont) if id == 'i1': cFont.loadoption = 1 cFont.loadoptionAll = 0 model.putFont() return render.font1(fontlist, form, mmaster, cFont) if id == 'i2': cFont.loadoption = 0 cFont.loadoptionAll = 1 putFontAllglyphs() return render.font1(fontlist, form, mmaster, cFont) if id == 'i3': cFont.loadoption = 1 cFont.loadoptionAll = 1 putFontAllglyphs() return render.font1(fontlist, form, mmaster, cFont) if id == 'e4': mfoption = 0 model.writexml() return render.font1(fontlist, form, mmaster, cFont) if id == 'e5': mfoption = 1 alist = list(get_activeglyph()) writeallxmlfromdb(alist) return render.font1(fontlist, form, mmaster, cFont) if id == '20000': return render.cproject() id = int(id) if id > 1000 and id < 10000: cFont.glyphName = chr(id - 1001 + 32) cFont.glyphunic = str(id - 1001) master = None if id > 0 and id < 1000: master = models.Master.get(idmaster=id) return render.font1(fontlist, form, mmaster, cFont, master)
def copyproject(): idmaster = int(cFont.idmaster) # # copy unix font ufo files # # mkdir -p fonts/3 # cp -rpu fonts/1/* fonts/3/ # idma = Master.select_maxid(session.user) idmasternew = idma[0].maxid + 1 print "idmasternew", idmasternew # import shutil new_proj_dir = working_dir(op.join('fonts', str(idmasternew))) old_proj_dir = working_dir(op.join('fonts', str(idmaster))) shutil.copytree(old_proj_dir, new_proj_dir) # # for iloop in [1, -1]: idmnew = "'"+str(iloop*idmasternew)+"'" idm = "'"+str(iloop*idmaster)+"'" strg = ("insert into master (idmaster,FontName,FontNameA,FontNameB,idglobal,vdate,user_id)" " select "+idmnew+", FontName,FontNameA,FontNameB,idglobal,now(),user_id from master where" " user_id=%s and idmaster=%s") % (session.user, idm) print strg db.query(strg) strg = ("insert into glyphoutline (idmaster,id,glyphname,PointNr,x,y,contrp,pip,vdate,user_id)" " select "+idmnew+",id,glyphname,PointNr,x,y,contrp,pip,now(),user_id from glyphoutline where" " user_id=%s and idmaster=%s") % (session.user, idm) print strg db.query(strg) strg = ("insert into groupparam (idmaster,user_id,groupname,vdate,startp,doubledash,tripledash,superleft,superright,leftp,rightp,downp,upp,dir,leftp2,rightp2,downp2,upp2,dir2,tension,tensionand,cycle,penshifted,pointshifted,angle,penwidth,overx,overbase,overcap,overasc,overdesc,ascpoint,descpoint,stemcutter,stemshift,inktrap_l,inktrap_r)" " select "+idmnew+",user_id,groupname,now(),startp,doubledash,tripledash,superleft,superright,leftp,rightp,downp,upp,dir,leftp2,rightp2,downp2,upp2,dir2,tension,tensionand,cycle,penshifted,pointshifted,angle,penwidth,overx,overbase,overcap,overasc,overdesc,ascpoint,descpoint,stemcutter,stemshift,inktrap_l,inktrap_r from groupparam where" " user_id=%s and idmaster=%s") % (session.user, idm) print strg db.query(strg) strg = ("insert into glyphparam (idmaster,id,glyphname,user_id,PointName,groupname,vdate,startp,doubledash,tripledash,superleft,superright,leftp,rightp,downp,upp,dir,leftp2,rightp2,downp2,upp2,dir2,tension,tensionand,cycle,penshifted,pointshifted,angle,penwidth,overx,overbase,overcap,overasc,overdesc,ascpoint,descpoint,stemcutter,stemshift,inktrap_l,inktrap_r)" " select "+idmnew+",id,glyphname,user_id,PointName,groupname,now(),startp,doubledash,tripledash,superleft,superright,leftp,rightp,downp,upp,dir,leftp2,rightp2,downp2,upp2,dir2,tension,tensionand,cycle,penshifted,pointshifted,angle,penwidth,overx,overbase,overcap,overasc,overdesc,ascpoint,descpoint,stemcutter,stemshift,inktrap_l,inktrap_r from glyphparam where" " user_id=%s and idmaster=%s") % (session.user, idm) print strg db.query(strg) return
def GET(self, id): master = models.Master.get(idmaster=id) if not master: return web.notfound() prepare_environment_directory() putFontAllglyphs(master) makefont(working_dir(), master) raise seeother('/fonts/')
def get_edges_json(log_filename, glyphid=None): result = {'edges': []} try: fp = open(op.join(working_dir(), log_filename)) content = fp.read() fp.close() return get_json(content, glyphid) except (IOError, OSError): pass return result
def writeGlyphlist(master, glyphid=None): ifile = open(op.join(master.get_fonts_directory(), "glyphlist.mf"), "w") dirnamep1 = working_dir(op.join(master.get_fonts_directory(), "glyphs")) charlist1 = [f for f in os.listdir(dirnamep1)] for ch1 in charlist1: fnb, ext = buildfname(ch1) if (not glyphid or str(glyphid) == fnb) and ext in ["mf"]: ifile.write("input glyphs/" + ch1 + "\n") ifile.close()
def POST(self, name, glyphid): if not is_loggedin(): raise seeother('/login') master = models.Master.get(fontname=name) if not master: return web.notfound() x = web.input(idlocal_changed=False, idglobal_changed=False, ab_source='a') form = LocalParamForm() if x.idlocal_changed: # if dropdown value in form has been changed so just refresh # data in selected form attrs = Settings.get_local_params(x.idlocal_changed, x.ab_source) return simplejson.dumps(attrs) elif x.idglobal_changed: params = models.GlobalParam.get(idglobal=x.idglobal_changed) return simplejson.dumps(params.as_dict()) if 'ab_source' in form.d and form.validates(): idlocal = form.d.idlocal fontsource = form.d.ab_source models.Master.update(idmaster=master.idmaster, values={'idlocal{0}'.format(fontsource.lower()): idlocal}) master = models.Master.get(fontname=name) values = form.d del values['ab_source'] del values['save'] del values['idlocal'] models.LocalParam.update(idlocal=idlocal, values=values) formg = GlobalParamForm() if formg.validates(): idglobal = formg.d.idglobal models.Master.update(idmaster=master.idmaster, values={'idglobal': idglobal}) master = models.Master.get(fontname=name) values = formg.d del values['idglobal'] del values['save'] models.GlobalParam.update(idglobal=idglobal, values=values) writeGlyphlist(master, glyphid) if model.writeGlobalParam(master): makefont(working_dir(), master) raise seeother('/view/{0}/{1}/settings/'.format(master.fontname, glyphid))
def POST(self, id): if not is_loggedin(): raise seeother('/login') mmaster = list(model.get_masters()) form = FontForm() form.fill() cFont.fontname = form.d.Name cFont.fontna = form.d.UFO_A cFont.fontnb = form.d.UFO_B # fontlist = [f for f in glob.glob(working_dir('fonts') + "/*/*.ufo")] fontlist.sort() return render.font1(fontlist, form, mmaster, cFont)
def writexml(master, ab_source=None): # # write two xml file for glyph in A and B Font # glyphName = cFont.glyphunic idworks = cFont.idwork idmaster = gidmast(cFont.idwork) if cFont.idwork == '0': glyphsourceA = op.join(working_dir(cFont.fontpath), cFont.fontna, "glyphs", glyphName + ".glif") glyphsource = glyphsourceA try: xmldoc = etree.parse(glyphsourceA) except IOError: return items = xmldoc.find("outline") if cFont.idwork == '1': glyphsourceB = op.join(working_dir(cFont.fontpath), cFont.fontnb, "glyphs", glyphName + ".glif") glyphsource = glyphsourceB try: xmldoc = etree.parse(glyphsourceB) except IOError: return items = xmldoc.find("outline") ids = " and idmaster="+'"'+str(idmaster)+'"' inum = 0 # db_rows=list(db.query("SELECT PointName,x,y from glyphoutline")) # we assume the number of rows from the db >= the number of the itemlist for itemlist in items: for s in itemlist: inum = inum + 1 db_row = VDBGlyphOutline.select_one(session.user, inum, glyphName, idmaster) if not db_row: continue s.attrib['pointNo'] = str(db_row.PointNr) s.attrib['x'] = str(db_row.x) s.attrib['y'] = str(db_row.y) sname = db_row.PointName if not sname: if s.get('name'): del s.attrib['name'] continue db_rowpar = VDBGlyphOutlines.select_one(session.user, inum, glyphName, idmaster) nameattr = sname if s.get('name'): s.attrib['name'] = nameattr else: s.attrib['name'] = nameattr # # first read group parameters if str(db_rowpar.groupn) not in ["None", 'NULL', '']: groupname = db_rowpar.groupn # save the groupname in an xml attribute # if s.get('groupname'): s.attrib['groupname'] = groupname else: s.sattrib('groupname', groupname) # # get the parameter list included with group parameters (lower priority) db_rowpar = VGLS.select_one(session.user, inum, glyphName, idmaster) # # read param value and write into xml # add glyphparameters here: xxmlat(s, db_rowpar.startp, 'startp', '1', 0) xxmlat(s, db_rowpar.doubledash, 'doubledash', '1', 0) xxmlat(s, db_rowpar.tripledash, 'tripledash', '1', 0) xxmlat(s, db_rowpar.superleft, 'superleft', '', 5) xxmlat(s, db_rowpar.superright, 'superright', '', 5) xxmlat(s, db_rowpar.leftp, 'leftp', '1', 0) xxmlat(s, db_rowpar.rightp, 'rightp', '1', 0) xxmlat(s, db_rowpar.downp, 'downp', '1', 0) xxmlat(s, db_rowpar.upp, 'upp', '1', 0) xxmlat(s, db_rowpar.dir, 'dir', '', 0) xxmlat(s, db_rowpar.leftp2, 'leftp2', '1', 0) xxmlat(s, db_rowpar.rightp2, 'rightp2', '1', 0) xxmlat(s, db_rowpar.downp2, 'downp2', '1', 0) xxmlat(s, db_rowpar.upp2, 'upp2', '1', 0) xxmlat(s, db_rowpar.dir2, 'dir2', '', 0) xxmlat(s, db_rowpar.tension, 'tension', '', 0) xxmlat(s, db_rowpar.tensionand, 'tensionand', '', 0) xxmlat(s, db_rowpar.cycle, 'cycle', '', 0) xxmlat(s, db_rowpar.penshifted, 'penshifted', '', 0) xxmlat(s, db_rowpar.pointshifted, 'pointshifted', '', 0) xxmlat(s, db_rowpar.angle, 'angle', '', 0) xxmlat(s, db_rowpar.penwidth, 'penwidth', '', 0) xxmlat(s, db_rowpar.overx, 'overx', '', 0) xxmlat(s, db_rowpar.overbase, 'overbase', '', 0) xxmlat(s, db_rowpar.overcap, 'overcap', '', 0) xxmlat(s, db_rowpar.overasc, 'overasc', '', 0) xxmlat(s, db_rowpar.ascpoint, 'ascpoint', '1', 0) xxmlat(s, db_rowpar.descpoint, 'descpoint', '1', 0) xxmlat(s, db_rowpar.stemcutter, 'stemcutter', '', 4) xxmlat(s, db_rowpar.stemshift, 'stemshift', '', 4) xxmlat(s, db_rowpar.inktrap_l, 'inktrap_l', '', 4) xxmlat(s, db_rowpar.inktrap_r, 'inktrap_r', '', 4) # print "glyphsource", glyphsource with codecs.open(glyphsource, "w", "utf-8") as out: xmldoc.write(out) out.close() # restore current idwork setting cFont.idwork = idworks
def POST(self): if not is_loggedin(): raise seeother('/login') x = web.input(zipfile={}) if 'name' in x and models.Master.exists(fontname=x.name): return render.create_project(error='Project with this name already exists') if 'zipfile' in x and 'name' in x and x.name: filename = op.join(working_dir(), x.zipfile.filename) try: with open(filename, 'w') as fp: fp.write(x.zipfile.file.read()) except (IOError, OSError): return render.create_project(error='Could not upload this file to disk') prepare_environment_directory() try: fzip = zipfile.ZipFile(filename) namelist = fzip.namelist() iscorrect_ufo = False ufo_dirs = [] for f in namelist: if re.search(r'.ufo[\\/]$', f): ufo_dirs.append(re.sub(r'[\\/]', '', f)) if re.search(r'.ufo[\\/]glyphs[\\/].*?.glif$', f, re.IGNORECASE): iscorrect_ufo = True if not iscorrect_ufo: return render.create_project(error='Archive does not contain any correct UFO folder') FontNameA = ufo_dirs[0] try: FontNameB = ufo_dirs[1] except IndexError: FontNameB = '' master = models.Master.create(fontname=x.name) fontpath = master.get_fonts_directory() fzip.extractall(fontpath) shutil.move(op.join(fontpath, FontNameA), op.join(fontpath, '%sA.UFO' % x.name)) if FontNameB: shutil.move(op.join(fontpath, FontNameB), op.join(fontpath, '%sB.UFO' % x.name)) FontNameB = '%sB.UFO' % x.name models.Master.update(idmaster=master.idmaster, values=dict(fontnamea='%sA.UFO' % x.name, fontnameb=FontNameB)) FontNameA = '%sA.UFO' % x.name if not FontNameB: FontNameB = '%sB.UFO' % x.name for f in os.listdir(working_dir('commons', user='******')): filename = working_dir(op.join('commons', f), user='******') try: if filename.endswith('font.mf'): shutil.copy2(filename, op.join(fontpath, mf_filename(FontNameA))) shutil.copy2(filename, op.join(fontpath, mf_filename(FontNameB))) shutil.copy2(filename, op.join(fontpath, mf_filename(x.name))) else: shutil.copy2(filename, fontpath) except (IOError, OSError): raise putFontAllglyphs(master) writeGlyphlist(master) makefont(working_dir(), master) except (zipfile.BadZipfile, OSError, IOError): if master: models.Master.delete(idmaster=master.idmaster) models.GlyphOutline.delete(idmaster=master.idmaster) models.GlyphParam.delete(idmaster=master.idmaster) fontpath = master.get_fonts_directory() shutil.rmtree(fontpath) return seeother('/fonts/') return render.create_project(error='Please fill all fields in form')
def get_directory(self, version=1): directory = op.join(working_dir(), '%s-%03d' % (self.projectname, version)) if not op.exists(directory): os.makedirs(directory) return directory