コード例 #1
0
def get_master_data(master, glyph, axislabel):
    project = master.project

    masters = project.get_ordered_masters()
    prepare_master_environment(masters[0])
    prepare_master_environment(master)

    metapost = Metapost(project)

    glyphs = masters[0].get_glyphs()
    glyphs = glyphs.filter(models.Glyph.name == glyph)
    if not metapost.execute_single(masters[0], glyphs.first()):
        return

    instancelog = project.get_instancelog(masters[0].version)
    metaglyphs = get_glyph_info_from_log(instancelog)

    glyphs = master.get_glyphs()
    glyphs = glyphs.filter(models.Glyph.name == glyph)
    if not metapost.execute_single(master, glyphs.first()):
        return
    master_instancelog = project.get_instancelog(master.version, 'a')

    glyphsdata = get_glyph_info_from_log(master_instancelog, master=master)

    metalabel = get_metapolation_label(axislabel)

    return {'glyphs': glyphsdata,
            'metaglyphs': metaglyphs,
            'master_name': master.name,
            'master_version': '{0:03d}'.format(master.version),
            'master_id': master.id,
            'metapolation': metalabel,
            'label': axislabel,
            'versions': project.get_versions()}
コード例 #2
0
ファイル: project.py プロジェクト: kolber/metapolator
    def GET(self):
        x = web.input(project=0)

        project = models.Project.get(id=x.project)
        if not project:
            raise web.notfound()

        if x.get('glyph'):
            if not models.Glyph.exists(name=x.get('glyph'),
                                       project_id=project.id):
                raise web.notfound()
            project.currentglyph = x.glyph
            web.ctx.orm.commit()

        masters = project.get_ordered_masters()

        masters_list = []

        metapost = Metapost(project)

        for i, master in enumerate(masters):

            prepare_master_environment(master)

            glyphs = master.get_glyphs()
            glyphs = glyphs.filter(models.Glyph.name == project.currentglyph)

            if not metapost.execute_single(master, glyphs.first()):
                return web.badrequest()

            master_instancelog = project.get_instancelog(master.version, 'a')
            glyphsdata = get_glyph_info_from_log(master_instancelog,
                                                 master=master)

            metalabel = get_metapolation_label(chr(models.LABELS[i]))

            masters_list.append({'glyphs': glyphsdata,
                                 'label': chr(models.LABELS[i]),
                                 'metapolation': metalabel,
                                 'master_id': master.id})

        glyphs = masters[0].get_glyphs()

        glyphs = glyphs.filter(models.Glyph.name == project.currentglyph)

        if not metapost.execute_interpolated_single(glyphs.first()):
            return web.badrequest()

        instancelog = project.get_instancelog(masters[0].version)
        metaglyphs = get_glyph_info_from_log(instancelog)

        import operator
        masters = map(operator.attrgetter('id', 'version'),
                      models.Master.filter(project_id=project.id))

        return ujson.dumps({'masters': masters_list,
                            'versions': project.get_versions(),
                            'metaglyphs': metaglyphs,
                            'mode': project.mfparser,
                            'project_id': project.id})
コード例 #3
0
ファイル: upload.py プロジェクト: kolber/metapolator
    def POST(self):
        x = web.input(ufofile={}, project_id=0, label='', glyph='',
                      master_id=0)
        try:
            rawzipcontent = x.ufofile.file.read()
            if not rawzipcontent:
                raise web.badrequest()
            zipcontent = StringIO.StringIO(rawzipcontent)
        except (AttributeError, TypeError, ValueError):
            raise web.badrequest()

        try:
            project_id = int(x.project_id)
        except TypeError:
            project_id = 0

        if not project_id:
            projects = models.Project.all()
            count = projects.filter(models.Project.projectname.like('UntitledProject%')).count()
            project = models.Project.create(projectname='UntitledProject%s' % (count + 1),
                                            mfparser=session.get('mfparser'))
        else:
            project = models.Project.get(id=project_id)
            if not project:
                raise web.notfound()

        master_exists = False
        try:
            master = models.Master.get(id=x.master_id, project_id=project.id)
            master_exists = bool(master)
        except AttributeError:
            master = None

        try:
            fzip = zipfile.ZipFile(zipcontent)

            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:
                raise web.badrequest()

            FontNameA = ufo_dirs[0]
            name, ext = op.splitext(op.basename(FontNameA))

            if not master:
                master = project.create_master()
                master.name = name
                web.ctx.orm.commit()

            label = get_metapolation_label(x.label)
            metapolation = models.Metapolation.get(label=label, project_id=project.id)
            if not metapolation:
                metapolation = models.Metapolation.create(label=label, project_id=project.id)

            fontpath = project.get_upload_directory()
            fzip.extractall(fontpath)

            ufopath = master.get_ufo_path()
            shutil.move(op.join(fontpath, FontNameA), ufopath)

            prepare_master_environment(master)

            if not master_exists:
                currentglyph = put_font_all_glyphs(master, project.currentglyph,
                                                   preload=True)
                project.currentglyph = currentglyph
            else:
                put_font_all_glyphs(master, project.currentglyph,
                                    preload=True, force_update=True)

            asyncresult = tasks.fill_master_with_glyphs.delay(master.id,
                                                              web.ctx.user.id,
                                                              force_update=master_exists)
            master.task_id = asyncresult.task_id
            master.task_created = datetime.datetime.now()
            web.ctx.orm.commit()
        except (zipfile.BadZipfile, OSError, IOError):
            raise

        master.update_masters_ordering(x.label)
        return ujson.dumps({'project_id': project.id,
                            'glyphname': project.currentglyph,
                            'master_id': master.id,
                            'label': x.label,
                            'metapolation': label})
コード例 #4
0
    def GET(self):
        x = web.input(project=0)

        project = models.Project.get(id=x.project)
        if not project:
            raise web.notfound()

        if x.get('glyph'):
            if not models.Glyph.exists(name=x.get('glyph'),
                                       project_id=project.id):
                raise web.notfound()
            project.currentglyph = x.glyph
            web.ctx.orm.commit()

        masters = project.get_ordered_masters()

        masters_list = []

        metapost = Metapost(project)

        for i, master in enumerate(masters):

            prepare_master_environment(master)

            glyphs = master.get_glyphs()
            glyphs = glyphs.filter(models.Glyph.name == project.currentglyph)

            if not metapost.execute_single(master, glyphs.first()):
                return web.badrequest()

            master_instancelog = project.get_instancelog(master.version, 'a')
            glyphsdata = get_glyph_info_from_log(master_instancelog,
                                                 master=master)

            metalabel = get_metapolation_label(chr(models.LABELS[i]))

            masters_list.append({
                'glyphs': glyphsdata,
                'label': chr(models.LABELS[i]),
                'metapolation': metalabel,
                'master_id': master.id
            })

        glyphs = masters[0].get_glyphs()

        glyphs = glyphs.filter(models.Glyph.name == project.currentglyph)

        if not metapost.execute_interpolated_single(glyphs.first()):
            return web.badrequest()

        instancelog = project.get_instancelog(masters[0].version)
        metaglyphs = get_glyph_info_from_log(instancelog)

        import operator
        masters = map(operator.attrgetter('id', 'version'),
                      models.Master.filter(project_id=project.id))

        return ujson.dumps({
            'masters': masters_list,
            'versions': project.get_versions(),
            'metaglyphs': metaglyphs,
            'mode': project.mfparser,
            'project_id': project.id
        })
コード例 #5
0
    def POST(self):
        x = web.input(ufofile={},
                      project_id=0,
                      label='',
                      glyph='',
                      master_id=0)
        try:
            rawzipcontent = x.ufofile.file.read()
            if not rawzipcontent:
                raise web.badrequest()
            zipcontent = StringIO.StringIO(rawzipcontent)
        except (AttributeError, TypeError, ValueError):
            raise web.badrequest()

        try:
            project_id = int(x.project_id)
        except TypeError:
            project_id = 0

        if not project_id:
            projects = models.Project.all()
            count = projects.filter(
                models.Project.projectname.like('UntitledProject%')).count()
            project = models.Project.create(projectname='UntitledProject%s' %
                                            (count + 1),
                                            mfparser=session.get('mfparser'))
        else:
            project = models.Project.get(id=project_id)
            if not project:
                raise web.notfound()

        master_exists = False
        try:
            master = models.Master.get(id=x.master_id, project_id=project.id)
            master_exists = bool(master)
        except AttributeError:
            master = None

        try:
            fzip = zipfile.ZipFile(zipcontent)

            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:
                raise web.badrequest()

            FontNameA = ufo_dirs[0]
            name, ext = op.splitext(op.basename(FontNameA))

            if not master:
                master = project.create_master()
                master.name = name
                web.ctx.orm.commit()

            label = get_metapolation_label(x.label)
            metapolation = models.Metapolation.get(label=label,
                                                   project_id=project.id)
            if not metapolation:
                metapolation = models.Metapolation.create(
                    label=label, project_id=project.id)

            fontpath = project.get_upload_directory()
            fzip.extractall(fontpath)

            ufopath = master.get_ufo_path()
            shutil.move(op.join(fontpath, FontNameA), ufopath)

            prepare_master_environment(master)

            if not master_exists:
                currentglyph = put_font_all_glyphs(master,
                                                   project.currentglyph,
                                                   preload=True)
                project.currentglyph = currentglyph
            else:
                put_font_all_glyphs(master,
                                    project.currentglyph,
                                    preload=True,
                                    force_update=True)

            asyncresult = tasks.fill_master_with_glyphs.delay(
                master.id, web.ctx.user.id, force_update=master_exists)
            master.task_id = asyncresult.task_id
            master.task_created = datetime.datetime.now()
            web.ctx.orm.commit()
        except (zipfile.BadZipfile, OSError, IOError):
            raise

        master.update_masters_ordering(x.label)
        return ujson.dumps({
            'project_id': project.id,
            'glyphname': project.currentglyph,
            'master_id': master.id,
            'label': x.label,
            'metapolation': label
        })
コード例 #6
0
    def POST(self):
        postdata = web.input(project_id=0)

        project = models.Project.get(id=postdata.project_id)
        if not project:
            raise web.notfound()

        masters = project.get_ordered_masters()

        master = project.create_master()
        web.ctx.orm.commit()

        prepare_master_environment(master)

        metapost = Metapost(project)
        metapost.execute_interpolated_bulk()

        primary_master = masters[0]

        master.name = primary_master.name
        logpath = project.get_instancelog(version=primary_master.version)
        for glyph in primary_master.get_glyphs():
            json = get_glyph_info_from_log(logpath, glyph.name)
            if not json:
                error = ('could not find any contours for '
                         'instance in %s for %s') % (logpath, glyph.name)
                raise web.badrequest(ujson.dumps({'error': error}))

            zpoints = glyph.get_zpoints()

            points = []
            for i, contourpoints in enumerate(json[0]['contours']):
                if not contourpoints:
                    error = ('could not find any points in contour '
                             'for instance in %s') % logpath
                    raise web.badrequest(ujson.dumps({'error': error}))
                metapost_points = []
                for point in contourpoints:
                    if project.mfparser == 'controlpoints':
                        p = {
                            'x': self.round(point['controls'][0]['x']),
                            'y': self.round(point['controls'][0]['y'])
                        }
                        metapost_points.append(p)

                    metapost_points.append({
                        'x': self.round(point['x']),
                        'y': self.round(point['y'])
                    })

                    if project.mfparser == 'controlpoints':
                        p = {
                            'x': self.round(point['controls'][1]['x']),
                            'y': self.round(point['controls'][1]['y'])
                        }
                        metapost_points.append(p)
                if project.mfparser == 'controlpoints' and metapost_points:
                    if i != 0:
                        points_ = metapost_points[1:] + metapost_points[:1]
                        points += points_
                    else:
                        points_ = metapost_points[2:] + metapost_points[:2]
                        points += points_[::-1]
                else:
                    points += metapost_points

            if len(zpoints) != len(points):
                error = '%s zp != mp %s' % (len(zpoints), len(points))
                raise web.badrequest(ujson.dumps({'error': error}))

            newglypha = models.Glyph.create(master_id=master.id,
                                            name=glyph.name,
                                            width=glyph.width,
                                            project_id=glyph.project_id)

            i = 0
            for point in points:
                self.create_glyphpoint(newglypha, (i + 1), zpoints[i], point)
                i += 1

        project.masters = ','.join([str(master.id)] * len(masters))
        web.ctx.orm.commit()
        return ujson.dumps({'versions': project.get_versions()})
コード例 #7
0
ファイル: createmaster.py プロジェクト: kolber/metapolator
    def POST(self):
        postdata = web.input(project_id=0)

        project = models.Project.get(id=postdata.project_id)
        if not project:
            raise web.notfound()

        masters = project.get_ordered_masters()

        master = project.create_master()
        web.ctx.orm.commit()

        prepare_master_environment(master)

        metapost = Metapost(project)
        metapost.execute_interpolated_bulk()

        primary_master = masters[0]

        master.name = primary_master.name
        logpath = project.get_instancelog(version=primary_master.version)
        for glyph in primary_master.get_glyphs():
            json = get_glyph_info_from_log(logpath, glyph.name)
            if not json:
                error = ("could not find any contours for " "instance in %s for %s") % (logpath, glyph.name)
                raise web.badrequest(ujson.dumps({"error": error}))

            zpoints = glyph.get_zpoints()

            points = []
            for i, contourpoints in enumerate(json[0]["contours"]):
                if not contourpoints:
                    error = ("could not find any points in contour " "for instance in %s") % logpath
                    raise web.badrequest(ujson.dumps({"error": error}))
                metapost_points = []
                for point in contourpoints:
                    if project.mfparser == "controlpoints":
                        p = {"x": self.round(point["controls"][0]["x"]), "y": self.round(point["controls"][0]["y"])}
                        metapost_points.append(p)

                    metapost_points.append({"x": self.round(point["x"]), "y": self.round(point["y"])})

                    if project.mfparser == "controlpoints":
                        p = {"x": self.round(point["controls"][1]["x"]), "y": self.round(point["controls"][1]["y"])}
                        metapost_points.append(p)
                if project.mfparser == "controlpoints" and metapost_points:
                    if i != 0:
                        points_ = metapost_points[1:] + metapost_points[:1]
                        points += points_
                    else:
                        points_ = metapost_points[2:] + metapost_points[:2]
                        points += points_[::-1]
                else:
                    points += metapost_points

            if len(zpoints) != len(points):
                error = "%s zp != mp %s" % (len(zpoints), len(points))
                raise web.badrequest(ujson.dumps({"error": error}))

            newglypha = models.Glyph.create(
                master_id=master.id, name=glyph.name, width=glyph.width, project_id=glyph.project_id
            )

            i = 0
            for point in points:
                self.create_glyphpoint(newglypha, (i + 1), zpoints[i], point)
                i += 1

        project.masters = ",".join([str(master.id)] * len(masters))
        web.ctx.orm.commit()
        return ujson.dumps({"versions": project.get_versions()})