Exemple #1
0
def importAllGlifFiles(font, dirName=None, doProgress=True, bar=None):
	"""import all GLIFs into a FontLab font"""
	if dirName is None:
		if font.file_name:
			dir, base = os.path.split(font.file_name)
			base = base.split(".")[0] + ".glyphs"
			dirName = os.path.join(dir, base)
		else:
			dirName = GetFolder("Please select a folder with .glif files")
	glyphSet = GlyphSet(dirName)
	glyphNames = glyphSet.keys()
	glyphNames.sort()
	barStart = 0
	closeBar = False
	if doProgress:
		if not bar:
			bar = ProgressBar("Importing Glyphs", len(glyphNames))
			closeBar = True
		else:
			barStart = bar.getCurrentTick()
	else:
		bar = None
	try:
		for i in range(len(glyphNames)):
			#if not (i % 10) and not bar.tick(barStart + i):
			#	raise KeyboardInterrupt
			glyphName = glyphNames[i]
			flGlyph = NewGlyph(font, glyphName, clear=True)
			pen = FLPointPen(flGlyph)
			glyph = GlyphPlaceholder()
			glyphSet.readGlyph(glyphName, glyph, pen)
			if hasattr(glyph, "width"):
				flGlyph.width = int(round(glyph.width))
			if hasattr(glyph, "unicodes"):
				flGlyph.unicodes = glyph.unicodes
			if hasattr(glyph, "note"):
				flGlyph.note = glyph.note  # XXX must encode
			if hasattr(glyph, "lib"):
				from cStringIO import StringIO
				from robofab.plistlib import writePlist
				lib = glyph.lib
				if lib:
					if len(lib) == 1 and "org.robofab.fontlab.customdata" in lib:
						data = lib["org.robofab.fontlab.customdata"].data
					else:
						f = StringIO()
						writePlist(glyph.lib, f)
						data = f.getvalue()
					flGlyph.customdata = data
			# XXX the next bit is only correct when font is the current font :-(
			fl.UpdateGlyph(font.FindGlyph(glyphName))
			if bar and not i % 10:
				bar.tick(barStart + i)
	except KeyboardInterrupt:
		if bar:
			bar.close()
			bar = None
	fl.UpdateFont(FontIndex(font))
	if bar and closeBar:
		bar.close()
Exemple #2
0
def writePlistAtomically(obj, path):
	"""
	Write a plist for "obj" to "path". Do this sort of atomically,
	making it harder to cause corrupt files, for example when writePlist
	encounters an error halfway during write. This also checks to see
	if text matches the text that is already in the file at path.
	If so, the file is not rewritten so that the modification date
	is preserved.
	"""
	f = StringIO()
	writePlist(obj, f)
	data = f.getvalue()
	writeFileAtomically(data, path)
def exportUFO(font,
              newFile=True,
              doInfo=True,
              doKerning=True,
              doGroups=True,
              doLib=True,
              doFeatures=True,
              doHints=False,
              doMarks=True,
              doMasks=True,
              glyphs=None,
              formatVersion=2):
    # get the UFO path
    ufoPath = os.path.splitext(font.path)[0] + ".ufo"
    if not newFile:
        if not os.path.exists(ufoPath):
            Message("Could not find the UFO file \"%s\"." %
                    os.path.basename(ufoPath))
            return
    else:
        if os.path.exists(ufoPath):
            ufoPath = _findAvailablePathName(ufoPath)
    # make sure no bogus glyph names are coming in
    if glyphs is not None:
        glyphs = [glyphName for glyphName in glyphs if font.has_key(glyphName)]
    # make the font the top font in FL
    fl.ifont = font.fontIndex
    # add the masks and marks to the glyph.lib
    if doMasks or doMarks:
        if glyphs is None:
            glyphNames = font.keys()
        else:
            glyphNames = glyphs
        for glyphName in glyphNames:
            glyph = font[glyphName]
            if doMarks:
                mark = glyph.mark
                glyph.lib[MARK_LIB_KEY] = mark
            if doMasks:
                # open a glyph window
                fl.EditGlyph(glyph.index)
                # switch to the mask layer
                fl.CallCommand(fl_cmd.ViewEditMask)
                # if the mask is empty, skip this step
                if not len(glyph):
                    # switch back to the edit layer
                    fl.CallCommand(fl_cmd.ViewEditMask)
                    continue
                # get the mask data
                pen = InstructionPointPen()
                glyph.drawPoints(pen)
                # switch back to the edit layer
                fl.CallCommand(fl_cmd.ViewEditMask)
                # write the mask data to the glyph lib
                instructions = pen.getInstructions()
                if instructions:
                    glyph.lib[MASK_LIB_KEY] = instructions
        # close all glyph windows. sometimes this actually works.
        fl.CallCommand(fl_cmd.WindowCloseAllGlyphWindows)
    # remove WWS names from the lib
    wwsStorage = {}
    if "openTypeNameWWSFamilyName" in font.lib:
        wwsStorage["openTypeNameWWSFamilyName"] = font.lib.pop(WWS_FAMILY_KEY)
    if "openTypeNameWWSSubfamilyName" in font.lib:
        wwsStorage["openTypeNameWWSSubfamilyName"] = font.lib.pop(
            WWS_SUBFAMILY_KEY)
    # write the UFO
    font.writeUFO(path=ufoPath,
                  doHints=doHints,
                  doInfo=doInfo,
                  doKerning=doKerning,
                  doGroups=doGroups,
                  doLib=doLib,
                  doFeatures=doFeatures,
                  glyphs=glyphs,
                  formatVersion=formatVersion)
    # add the WWS names to the info
    if doInfo:
        infoPath = os.path.join(ufoPath, "fontinfo.plist")
        info = readPlist(infoPath)
        newInfo = deepcopy(info)
        newInfo.update(wwsStorage)
        if info != newInfo:
            writePlist(newInfo, infoPath)
    # put the WWS names back in the lib
    font.lib.update(wwsStorage)
    # remove the masks and marks from the glyph.lib
    if doMasks or doMarks:
        if glyphs is None:
            glyphNames = font.keys()
        else:
            glyphNames = glyphs
        for glyphName in glyphNames:
            glyph = font[glyphName]
            lib = glyph.lib
            if lib.has_key(MASK_LIB_KEY):
                del lib[MASK_LIB_KEY]
            if lib.has_key(MARK_LIB_KEY):
                del lib[MARK_LIB_KEY]
Exemple #4
0
def importAllGlifFiles(font, dirName=None, doProgress=True, bar=None):
    """import all GLIFs into a FontLab font"""
    if dirName is None:
        if font.file_name:
            dir, base = os.path.split(font.file_name)
            base = base.split(".")[0] + ".glyphs"
            dirName = os.path.join(dir, base)
        else:
            dirName = GetFolder("Please select a folder with .glif files")
    glyphSet = GlyphSet(dirName)
    glyphNames = glyphSet.keys()
    glyphNames.sort()
    barStart = 0
    closeBar = False
    if doProgress:
        if not bar:
            bar = ProgressBar("Importing Glyphs", len(glyphNames))
            closeBar = True
        else:
            barStart = bar.getCurrentTick()
    else:
        bar = None
    try:
        for i in range(len(glyphNames)):
            #if not (i % 10) and not bar.tick(barStart + i):
            #	raise KeyboardInterrupt
            glyphName = glyphNames[i]
            flGlyph = NewGlyph(font, glyphName, clear=True)
            pen = FLPointPen(flGlyph)
            glyph = GlyphPlaceholder()
            glyphSet.readGlyph(glyphName, glyph, pen)
            if hasattr(glyph, "width"):
                flGlyph.width = int(round(glyph.width))
            if hasattr(glyph, "unicodes"):
                flGlyph.unicodes = glyph.unicodes
            if hasattr(glyph, "note"):
                flGlyph.note = glyph.note  # XXX must encode
            if hasattr(glyph, "lib"):
                from cStringIO import StringIO
                from robofab.plistlib import writePlist
                lib = glyph.lib
                if lib:
                    if len(lib
                           ) == 1 and "org.robofab.fontlab.customdata" in lib:
                        data = lib["org.robofab.fontlab.customdata"].data
                    else:
                        f = StringIO()
                        writePlist(glyph.lib, f)
                        data = f.getvalue()
                    flGlyph.customdata = data
            # XXX the next bit is only correct when font is the current font :-(
            fl.UpdateGlyph(font.FindGlyph(glyphName))
            if bar and not i % 10:
                bar.tick(barStart + i)
    except KeyboardInterrupt:
        if bar:
            bar.close()
            bar = None
    fl.UpdateFont(FontIndex(font))
    if bar and closeBar:
        bar.close()
def exportUFO(font, newFile=True, doInfo=True, doKerning=True, doGroups=True, doLib=True, doFeatures=True,
    doHints=False, doMarks=True, doMasks=True, glyphs=None, formatVersion=2):
    # get the UFO path
    ufoPath = os.path.splitext(font.path)[0] + ".ufo"
    if not newFile:
        if not os.path.exists(ufoPath):
            Message("Could not find the UFO file \"%s\"." % os.path.basename(ufoPath))
            return
    else:
        if os.path.exists(ufoPath):
            ufoPath = _findAvailablePathName(ufoPath)
    # make sure no bogus glyph names are coming in
    if glyphs is not None:
        glyphs = [glyphName for glyphName in glyphs if font.has_key(glyphName)]
    # make the font the top font in FL
    fl.ifont = font.fontIndex
    # add the masks and marks to the glyph.lib
    if doMasks or doMarks:
        if glyphs is None:
            glyphNames = font.keys()
        else:
            glyphNames = glyphs
        for glyphName in glyphNames:
            glyph = font[glyphName]
            if doMarks:
                mark = glyph.mark
                glyph.lib[MARK_LIB_KEY] = mark
            if doMasks:
                # open a glyph window
                fl.EditGlyph(glyph.index)
                # switch to the mask layer
                fl.CallCommand(fl_cmd.ViewEditMask)
                # if the mask is empty, skip this step
                if not len(glyph):
                    # switch back to the edit layer
                    fl.CallCommand(fl_cmd.ViewEditMask)
                    continue
                # get the mask data
                pen = InstructionPointPen()
                glyph.drawPoints(pen)
                # switch back to the edit layer
                fl.CallCommand(fl_cmd.ViewEditMask)
                # write the mask data to the glyph lib
                instructions = pen.getInstructions()
                if instructions:
                    glyph.lib[MASK_LIB_KEY] = instructions
        # close all glyph windows. sometimes this actually works.
        fl.CallCommand(fl_cmd.WindowCloseAllGlyphWindows)
    # remove WWS names from the lib
    wwsStorage = {}
    if "openTypeNameWWSFamilyName" in font.lib:
        wwsStorage["openTypeNameWWSFamilyName"] = font.lib.pop(WWS_FAMILY_KEY)
    if "openTypeNameWWSSubfamilyName" in font.lib:
        wwsStorage["openTypeNameWWSSubfamilyName"] = font.lib.pop(WWS_SUBFAMILY_KEY)
    # write the UFO
    font.writeUFO(path=ufoPath, doHints=doHints, doInfo=doInfo,
        doKerning=doKerning, doGroups=doGroups, doLib=doLib, doFeatures=doFeatures, glyphs=glyphs,
        formatVersion=formatVersion)
    # add the WWS names to the info
    if doInfo:
        infoPath = os.path.join(ufoPath, "fontinfo.plist")
        info = readPlist(infoPath)
        newInfo = deepcopy(info)
        newInfo.update(wwsStorage)
        if info != newInfo:
            writePlist(newInfo, infoPath)
    # put the WWS names back in the lib
    font.lib.update(wwsStorage)
    # remove the masks and marks from the glyph.lib
    if doMasks or doMarks:
        if glyphs is None:
            glyphNames = font.keys()
        else:
            glyphNames = glyphs
        for glyphName in glyphNames:
            glyph = font[glyphName]
            lib = glyph.lib
            if lib.has_key(MASK_LIB_KEY):
                del lib[MASK_LIB_KEY]
            if lib.has_key(MARK_LIB_KEY):
                del lib[MARK_LIB_KEY]