示例#1
0
 def create_fonts(self):
     print "batch creating fonts...\n"
     for font in self.fonts:
         font_path = '%s_%s.ufo' % (self.project.name, font)
         font_path = os.path.join(self.project.paths['ufos'], font_path)
         if os.path.exists(font_path) is False:
             f = NewFont(showUI=False)
             print '\t%s creating font...' % font
             f.save(destDir=font_path)
         else:
             print '\t%s already exists.' % font
     print "\n...done.\n"
示例#2
0
 def create_fonts(self):
     """Create fonts in space, if they don't exist yet."""
     print "batch creating fonts...\n"
     for font in self.fonts:
         font_path = "%s_%s.ufo" % (self.project.name, font)
         font_path = os.path.join(self.project.paths["ufos"], font_path)
         if os.path.exists(font_path) is False:
             f = NewFont(showUI=False)
             print "\t%s creating font..." % font
             f.save(destDir=font_path)
         else:
             print "\t%s already exists." % font
     print "\n...done.\n"
示例#3
0
 def generate_instance(self, instance_name, verbose=False, folder=None):
     """Generate a ``.ufo`` instance with name ``instance_name``, using data from the project's interpol lib."""
     _masters = self.masters()
     if self.libs["interpol"].has_key(instance_name):
         # get instance info
         master_1, master_2, interpol_factor = self.libs["interpol"][instance_name]
         # make file names
         master_1_filename = "%s_%s.ufo" % (self.name, master_1)
         master_2_filename = "%s_%s.ufo" % (self.name, master_2)
         # master masters
         master_1_path = os.path.join(self.paths["ufos"], master_1_filename)
         master_2_path = os.path.join(self.paths["ufos"], master_2_filename)
         # instance masters
         if master_1_path not in _masters:
             master_1_path = os.path.join(self.paths["instances"], master_1_filename)
         if master_2_path not in _masters:
             master_2_path = os.path.join(self.paths["instances"], master_2_filename)
         # generate instance
         if os.path.exists(master_1_path) and os.path.exists(master_2_path):
             if verbose:
                 print "generating %s %s (factor: %s, %s)..." % (
                     self.name,
                     instance_name,
                     interpol_factor[0],
                     interpol_factor[1],
                 ),
             instance_filename = "%s_%s.ufo" % (self.name, instance_name)
             if folder is None:
                 instance_path = os.path.join(self.paths["instances"], instance_filename)
             else:
                 instance_path = os.path.join(folder, instance_filename)
             # open/create fonts
             f1 = RFont(master_1_path, showUI=False)
             f2 = RFont(master_2_path, showUI=False)
             f3 = NewFont(showUI=False)
             # interpolate
             f3.interpolate((interpol_factor[0], interpol_factor[1]), f1, f2)
             f3.update()
             f1.close()
             f2.close()
             f3.save(instance_path)
             f3.close()
             if verbose:
                 print "done.\n"
     # instance not in lib
     else:
         if verbose:
             print "%s is not an instance.\n" % instance_name
示例#4
0
def temp_font():
    """
    Return a temporary font for importing single ``.glyfs``.

    """
    if CurrentFont() is None:
        t = NewFont()
    else:
        t = CurrentFont()
    return t
示例#5
0
def expand_font(src_font, distance, join=1, cap=1):
    # create a new empty font
    dst_font = NewFont(showUI=False)
    # expand all glyph
    for glyph_name in src_font.keys():
        # get source glyph
        src_glyph = src_font[glyph_name]
        # get dest glyph
        dst_font.newGlyph(glyph_name)
        dst_glyph = dst_font[glyph_name]
        # expand glyph into dest font
        outline_pen = make_outline(src_glyph, distance, join, cap)
        outline_pen.drawPoints(dst_glyph.getPointPen())
        # copy width from source glyph
        dst_glyph.width = src_glyph.width
        # copy components
        if len(src_glyph.components):
            for component in src_glyph.components:
                dst_glyph.appendComponent(component.baseGlyph, component.offset, component.scale)
    # done
    return dst_font
示例#6
0
def expand_font(src_font, distance, join=1, cap=1):
    '''Expand outlines for all glyphs in font.'''
    # create a new empty font
    dst_font = NewFont(showUI=False)
    # expand all glyph
    for glyph_name in src_font.keys():
        # get source glyph
        src_glyph = src_font[glyph_name]
        # get dest glyph
        dst_font.newGlyph(glyph_name)
        dst_glyph = dst_font[glyph_name]
        # expand glyph into dest font
        outline_pen = make_outline(src_glyph, distance, join, cap)
        outline_pen.drawPoints(dst_glyph.getPointPen())
        # copy width from source glyph
        dst_glyph.width = src_glyph.width
        # copy components
        if len(src_glyph.components):
            for component in src_glyph.components:
                dst_glyph.appendComponent(component.baseGlyph, component.offset, component.scale)
    # done
    return dst_font
示例#7
0
 def generate_instance(self, instance_name, verbose=False, folder=None):
     '''Generate a .ufo instance with name `instance_name`, using data from the project's interpol lib.'''
     if self.libs['interpol'].has_key(instance_name):
         # master 1
         master_1 = self.libs['interpol'][instance_name][0]
         master_1_filename = '%s_%s.ufo' % (self.name, master_1)
         master_1_path = os.path.join(self.paths['ufos'], master_1_filename)
         # master 2
         master_2 = self.libs['interpol'][instance_name][1]
         master_2_filename = '%s_%s.ufo' % (self.name, master_2)
         master_2_path = os.path.join(self.paths['ufos'], master_2_filename)
         # interpolation factor
         interpol_factor = self.libs['interpol'][instance_name][2]
         # if both masters exist, generate instance
         if os.path.exists(master_1_path) and os.path.exists(master_2_path):
             if verbose:
                 print 'generating %s %s (factor: %s, %s)...' % (self.name, instance_name,
                         interpol_factor[0], interpol_factor[1]),
             instance_filename = '%s_%s.ufo' % (self.name, instance_name)
             if folder is None:
                 instance_path = os.path.join(self.paths['instances'], instance_filename)
             else:
                 instance_path = os.path.join(folder, instance_filename)
             # open/create fonts
             f1 = RFont(master_1_path, showUI=False)
             f2 = RFont(master_2_path, showUI=False)
             f3 = NewFont(showUI=False)
             # interpolate
             f3.interpolate((interpol_factor[0], interpol_factor[1]), f2, f1)
             f3.update()
             f1.close()
             f2.close()
             f3.save(instance_path)
             f3.close()
             if verbose:
                 print 'done.\n'
     # instance not in lib
     else:
         if verbose:
             print '%s is not an instance.\n' % instance_name
示例#8
0
 def generate_vfbs(self, masters=True, instances=False, interpol=False):
     """Batch convert ufos in project to vfb format."""
     # collect files
     _ufos = []
     if masters:
         _ufos += self.masters()
     if instances:
         _ufos += self.instances()
     if interpol:
         _ufos += self.masters_interpol()
     # run
     for ufo in _ufos:
         _vfb_name = os.path.split(ufo)[1]
         _vfb_file = "%s.vfb" % _vfb_name.split(".")[0]
         _vfb_path = os.path.join(self.paths["vfbs"], _vfb_file)
         font = NewFont()
         font.readUFO(ufo, doProgress=True)
         font.save(_vfb_path)
         font.close()
示例#9
0
 def generate_vfbs(self, masters=True, instances=False, interpol=False):
     '''Batch convert ufos in project to vfb format.'''
     import os
     from robofab.world import NewFont
     # collect files
     _ufos = []
     if masters:
         _ufos += self.masters()
     if instances:
         _ufos += self.instances()
     if interpol:
         _ufos += self.masters_interpol()
     # run
     for ufo in _ufos:
         _vfb_name = os.path.split(ufo)[1]
         _vfb_file = '%s.vfb' % _vfb_name.split('.')[0]
         _vfb_path = os.path.join(self.paths['vfbs'], _vfb_file)
         font = NewFont()
         font.readUFO(ufo, doProgress=True)
         font.save(_vfb_path)
         font.close()
示例#10
0
    def makeInstance(self, axes, gname, glyph):
        nbAxes = maxNbAxes = len(axes)
        # steps = [1, 2]
        # maxNbAxes = max(steps)
        # for nbAxes in steps:

        speeds = [1 for i in range(nbAxes)]
        # LCM = self.ilcm(speeds)
        LCM = 60
        # print(LCM)
        # while ((LCM < 600 or LCM > 1200) or self.checkEqual(speeds)):
        #     speeds = [int(10/nbAxes + random.random()*60/nbAxes) for i in range(nbAxes)]
        #     LCM = self.ilcm(speeds)
        #     print(speeds, LCM, (LCM < 600 or LCM > 1200))

        alpha = 2 * math.pi / maxNbAxes
        ld = [{'Axis': l, 'PreviewValue': 0} for l in axes]
        # glyph.preview.computeDeepComponentsPreview(ld)
        origineGlyph = RGlyph()
        for atomicInstance in glyph.preview():
            atomicInstance = atomicInstance.glyph
            atomicInstance.draw(origineGlyph.getPen())
        # origineGlyph = glyph.preview.variationPreview.copy()
        origineGlyph.name = "interpo"
        db.newDrawing()
        for k in range(nbAxes):
            start = time.time()
            ld = [{
                'Axis': l,
                'PreviewValue': j == k
            } for j, l in enumerate(axes)]
            # glyph.preview.computeDeepComponentsPreview(ld)
            for g in range(LCM):

                H = 700
                W = 700
                db.newPage(W * 2.5, H)
                db.frameDuration(1 / 30)

                db.fill(1)
                db.rect(0, 0, W * 2.5, H)

                r = W / 3
                ainc = 0
                lines = []
                rands = []
                values = []

                for i in range(nbAxes):
                    path = db.BezierPath()
                    path.moveTo((H / 2, W / 2))
                    line = (r * math.sin(ainc), r * math.cos(ainc))
                    path.lineTo((H / 2 + line[0], W / 2 + line[1]))
                    dx = line[0] * .05
                    dy = line[1] * .05
                    path.moveTo((H / 2 + line[0] - dy, W / 2 + line[1] + dx))
                    path.lineTo((H / 2 + line[0] + dy, W / 2 + line[1] - dx))
                    db.stroke(.2)
                    db.strokeWidth(1.5)
                    db.fill(None)
                    db.drawPath(path)
                    ainc += alpha
                    lines.append((line, axes[i]["sourceName"]))
                    # v = getValueForAxeAtFrame(i, g, nbAxes, LCM, speeds[i])
                    # values.append(v)
                    if i == k:
                        rands.append([
                            1000 * abs(
                                math.sin(math.pi *
                                         (speeds[i] * c / LCM + speeds[i])))
                            for c in range(LCM)
                        ])
                    else:
                        rands.append([0 for c in range(LCM)])

                db.fill(1)
                db.oval(H / 2 - H * .01, W / 2 - W * .01, H * .02, W * .02)

                patharea = db.BezierPath()
                patharea.moveTo((H / 2, W / 2))
                patharea.lineTo((H / 2 + lines[0][0][0] * rands[0][g] / 1000,
                                 W / 2 + lines[0][0][1] * rands[0][g] / 1000))
                db.fill(0, 0, 0, .1)
                db.stroke(None)
                for c, (line, lineName) in enumerate(lines):
                    patharea.lineTo((H / 2 + line[0] * rands[c][g] / 1000,
                                     W / 2 + line[1] * rands[c][g] / 1000))
                patharea.lineTo((H / 2 + lines[0][0][0] * rands[0][g] / 1000,
                                 W / 2 + lines[0][0][1] * rands[0][g] / 1000))
                patharea.lineTo((H / 2, W / 2))
                db.drawPath(patharea)

                for c, (line, lineName) in enumerate(lines):
                    db.fill(0)  #1-rands[c]
                    db.stroke(.2)
                    db.strokeWidth(1)
                    db.oval(H / 2 + line[0] * rands[c][g] / 1000 - 4.5,
                            W / 2 + line[1] * rands[c][g] / 1000 - 4.5, 9, 9)
                    db.fill(.2)
                    ftxt = db.FormattedString(txt=lineName,
                                              font="GrtskZetta-Light",
                                              fontSize=14,
                                              align="center")
                    db.textBox(ftxt, (H / 2 + line[0] * 1.3 - 30,
                                      W / 2 + line[1] * 1.3 - 10, 60, 20))

                #########
                db.save()
                # ld = []
                # for j, l in enumerate(axes):
                #     ld.append({'Axis': l, 'PreviewValue':rands[j][g]/1000})

                # # d = {l:rands[j][g]/1000 for (j, l) in enumerate(axes)}

                # # glyph = interpolation(NewFont().newGlyph('temp'), ufo[gname], layersInfo = d)
                # # glyph = self.RCJKI.currentFont.get(gname)
                # glyph.preview.computeDeepComponentsPreview(ld)
                #########
                # print(glyph)
                db.translate(W * 1.3, H * .15)
                db.scale(.7 * H / 1000)
                db.stroke(.5)
                db.fill(None)
                db.rect(0, 0, 1000, 1000)
                db.fill(0)
                db.stroke(None)
                db.save()
                db.translate(0, 120)

                ratioX = ratioY = (rands[k][g]) / 1000
                resultGlyph = RGlyph()
                locations = {}
                for e in ld:
                    locations[e["Axis"]["sourceName"]] = e["PreviewValue"]
                for c in glyph.preview(locations):
                    c = c.glyph
                    c.draw(resultGlyph.getPen())
                interpoGlyph = interpolation.interpol_glyph_glyph_ratioX_ratioY_scaleX_scaleY(
                    origineGlyph, resultGlyph, ratioX, ratioY, 1, 1,
                    NewFont(showUI=False))
                db.drawGlyph(interpoGlyph)

                #         for aes in glyph.preview:
                #             # axis2layerName = {axisName:layerName for axisName, layerName in self.RCJKI.currentFont[aes['name']].lib['robocjk.atomicElement.glyphVariations'].items()}

                #             # lInfos = {axis2layerName[axisName]:v for axisName, v in aes['coord'].items()}
                # #            print(ae['coord'])
                #             for ae in aes.values():
                #                 glyph = ae[0]
                #                 print(glyph)
                #                 db.save()
                #                 self._drawGlyph(glyph)
                #                 db.restore()
                db.restore()
                db.restore()
                caption = db.FormattedString(txt='%s-axis' % (nbAxes),
                                             font="GrtskMega-Medium",
                                             fontSize=14,
                                             align="left")
                db.textBox(caption, (10, 10, W - 20, 20))

            stop = time.time()
            print(stop - start, "seconde for axis")

        pdfData = db.pdfImage()
示例#11
0
    def importButtonCallback(self, sender):

        if not len(self.selectedMasters):
            return

        if self.verbose:
            print('importing glyphs from selected sources...\n')

        # mode 0 : fonts → fonts

        if self.importMode == 0:

            for master in self.selectedMasters:
                ufoPath = self._sources[master['name']]
                srcFont = OpenFont(ufoPath, showInterface=False)
                tmpFont = NewFont(familyName=srcFont.info.familyName, styleName=srcFont.info.styleName, showInterface=False)

                glyphsFolder = os.path.join(ufoPath, 'glyphs')
                ufoName = splitall(glyphsFolder)[-2]

                if self.verbose:
                    print(f'\t{ufoName}:')

                for glyphName in self.glyphNames:
                    if glyphName not in srcFont:
                        if self.verbose:
                            print(f'\t\t{glyphName} not in font.')
                        continue

                    srcGlyph = srcFont[glyphName]
                    if srcGlyph.components:
                        for component in srcGlyph.components:
                            if not component.baseGlyph in tmpFont:
                                if self.verbose:
                                    print(f'\t\timporting {component.baseGlyph} ({glyphName})...')
                                tmpFont[component.baseGlyph] = srcFont[component.baseGlyph]
                                tmpFont[component.baseGlyph].lib[self.glyphSetPathKey] = glyphsFolder

                    if self.verbose:
                        print(f'\t\timporting {glyphName}...')
                    tmpFont[glyphName] = srcGlyph
                    tmpFont[glyphName].lib[self.glyphSetPathKey] = glyphsFolder

                tmpFont.openInterface()

                if self.verbose:
                    print()

        # mode 1 : fonts → glyphs
        
        if self.importMode == 1:
            tmpFont = CurrentFont()
            if tmpFont is None:
                tmpFont = NewFont(familyName='tempEdit')

            for i, master in enumerate(self.selectedMasters):
                ufoPath = self._sources[master['name']]
                if not os.path.exists(ufoPath):
                    if self.verbose:
                        print(f'source file does not exist: {ufoPath}')
                    continue
                
                srcFont = OpenFont(ufoPath, showInterface=False)
                glyphsFolder = os.path.join(ufoPath, 'glyphs')
                ufoName = splitall(glyphsFolder)[-2]
                glyphNameExtension = os.path.splitext(ufoName)[0]

                for glyphName in self.glyphNames:

                    tmpGlyphName = f'{glyphName}.{glyphNameExtension}'

                    if glyphName not in srcFont:
                        if self.verbose:
                            print(f'\t\tcreating {glyphName}...')
                            tmpFont.newGlyph(tmpGlyphName)

                    else:
                        srcGlyph = srcFont[glyphName]
                        for component in srcGlyph.components:
                            if component.baseGlyph not in tmpFont:
                                if component.baseGlyph not in srcFont:
                                    continue
                                if self.verbose:
                                    print(f'\t\timporting {component.baseGlyph} ({glyphName})...')
                                tmpBaseGlyph = f'{component.baseGlyph}.{glyphNameExtension}'
                                tmpFont[tmpBaseGlyph] = srcFont[component.baseGlyph]
                                tmpFont[tmpBaseGlyph].lib[self.glyphSetPathKey] = glyphsFolder

                        if self.verbose:
                            print(f'\t\timporting {glyphName}...')

                        tmpFont.newGlyph(tmpGlyphName)
                        tmpFont[tmpGlyphName].appendGlyph(srcGlyph)
                        tmpFont[tmpGlyphName].width = srcGlyph.width

                    tmpFont[tmpGlyphName].lib[self.glyphSetPathKey] = glyphsFolder

                if 'background' not in tmpFont.layerOrder:
                    tmpFont.newLayer('background')

                if self.verbose:
                    print()

        # mode 2 : fonts → layers

        else:
            
            tmpFont = CurrentFont()
            if tmpFont is None:
                tmpFont = NewFont(familyName='tempEdit')

            for i, master in enumerate(self.selectedMasters):
                ufoPath = self._sources[master['name']]
                if not os.path.exists(ufoPath):
                    if self.verbose:
                        print(f'source file does not exist: {ufoPath}')
                    continue
                
                srcFont = OpenFont(ufoPath, showInterface=False)
                glyphsFolder = os.path.join(ufoPath, 'glyphs')
                ufoName = splitall(glyphsFolder)[-2]
                layerName = os.path.splitext(ufoName)[0]
                tmpLayer = tmpFont.newLayer(layerName)

                if self.verbose:
                    print(f'\t{ufoName}:')

                if i == 0:
                    tmpFont.defaultLayer = tmpLayer
                    if 'foreground' in tmpFont.layerOrder:
                        tmpFont.removeLayer('foreground')

                for glyphName in self.glyphNames:

                    if glyphName not in srcFont:
                        if self.verbose:
                            print(f'\t\tcreating {glyphName}...')
                            tmpLayer.newGlyph(glyphName)

                    else:
                        srcGlyph = srcFont[glyphName]
                        for component in srcGlyph.components:
                            if component.baseGlyph not in tmpLayer:
                                if component.baseGlyph not in srcFont:
                                    continue
                                if self.verbose:
                                    print(f'\t\timporting {component.baseGlyph} ({glyphName})...')
                                tmpLayer[component.baseGlyph] = srcFont[component.baseGlyph]
                                tmpLayer[component.baseGlyph].lib[self.glyphSetPathKey] = glyphsFolder

                        if self.verbose:
                            print(f'\t\timporting {glyphName}...')
                        tmpLayer[glyphName] = srcGlyph
                        tmpLayer[glyphName].width = srcGlyph.width

                    tmpLayer[glyphName].lib[self.glyphSetPathKey] = glyphsFolder

                if self.verbose:
                    print()

        if self.verbose:
            print('...done.\n')