Exemplo n.º 1
0
 def __init__(self, otf, ufo):
     self.ufo = ufo
     stream = BytesIO()
     otf.save(stream)
     stream.seek(0)
     self.otf = TTFont(stream)
     self._postscriptNames = ufo.lib.get('public.postscriptNames')
Exemplo n.º 2
0
def write_data(td):
    """Writes CharStrings from the TopDict td into a string that is easily
    readable."""

    out = BytesIO()
    td.CharStrings.charStringsIndex.getCompiler(td.strings, None).toFile(out)
    return out.getvalue()
Exemplo n.º 3
0
def write_data(td):
    """Writes CharStrings from the TopDict td into a string that is easily
    readable."""

    out = BytesIO()
    td.CharStrings.charStringsIndex.getCompiler(td.strings, None).toFile(out)
    return out.getvalue()
Exemplo n.º 4
0
 def __init__(self, otf, ufo):
     self.ufo = ufo
     stream = BytesIO()
     otf.save(stream)
     stream.seek(0)
     self.otf = TTFont(stream)
     self._postscriptNames = ufo.lib.get('public.postscriptNames')
Exemplo n.º 5
0
 def __init__(self, otf, ufo, glyphSet=None):
     self.ufo = ufo
     self.glyphSet = glyphSet if glyphSet is not None else ufo
     stream = BytesIO()
     otf.save(stream)
     stream.seek(0)
     self.otf = TTFont(stream)
     self._postscriptNames = ufo.lib.get("public.postscriptNames")
Exemplo n.º 6
0
def write_data(td):
    """Writes CharStrings and FDSelect from the TopDict td into a string
    that is easily readable."""

    out = BytesIO()
    td.CharStrings.charStringsIndex.getCompiler(td.strings, None).toFile(out)
    try:
        fdselect = struct.pack('B', len(td.FDArray)) + array.array('B', list(td.FDSelect)).tostring()
    except AttributeError:
        fdselect = struct.pack('B', 1)
    out.write(fdselect)
    return out.getvalue()
Exemplo n.º 7
0
    def importTTX(self):
        """
        Merge TTX files from data directory "com.github.fonttools.ttx"

        **This should not be called externally.** Subclasses
        may override this method to handle the bounds creation
        in a different way if desired.
        """
        import os
        import re

        prefix = "com.github.fonttools.ttx"
        sfntVersionRE = re.compile('(^<ttFont\s+)(sfntVersion=".*"\s+)(.*>$)',
                                   flags=re.MULTILINE)
        if not hasattr(self.ufo, "data"):
            return
        if not self.ufo.data.fileNames:
            return
        for path in self.ufo.data.fileNames:
            foldername, filename = os.path.split(path)
            if (foldername == prefix and filename.endswith(".ttx")):
                ttx = self.ufo.data[path].decode('utf-8')
                # strip 'sfntVersion' attribute from ttFont element,
                # if present, to avoid overwriting the current value
                ttx = sfntVersionRE.sub(r'\1\3', ttx)
                fp = BytesIO(ttx.encode('utf-8'))
                self.otf.importXML(fp)
Exemplo n.º 8
0
    def importTTX(self):
        """
        Merge TTX files from data directory "com.github.fonttools.ttx"

        **This should not be called externally.** Subclasses
        may override this method to handle the bounds creation
        in a different way if desired.
        """
        import os
        prefix = "com.github.fonttools.ttx"
        if not hasattr(self.ufo, "data"):
            return
        if not self.ufo.data.fileNames:
            return
        for path in self.ufo.data.fileNames:
            foldername, filename = os.path.split(path)
            if (foldername == prefix and filename.endswith(".ttx")):
                fp = BytesIO(self.ufo.data[path])
                self.otf.importXML(fp)
Exemplo n.º 9
0
 def __init__(self, otf, ufo):
     self.ufo = ufo
     stream = BytesIO()
     otf.save(stream)
     stream.seek(0)
     self.otf = TTFont(stream)
Exemplo n.º 10
0
def lib_convertCFFToCFF2(cff, otFont):
    # This assumes a decompiled CFF table.
    cff2GetGlyphOrder = cff.otFont.getGlyphOrder
    topDictData = TopDictIndex(None, cff2GetGlyphOrder, None)
    topDictData.items = cff.topDictIndex.items
    cff.topDictIndex = topDictData
    topDict = topDictData[0]
    if hasattr(topDict, 'Private'):
        privateDict = topDict.Private
    else:
        privateDict = None
    opOrder = buildOrder(topDictOperators2)
    topDict.order = opOrder
    topDict.cff2GetGlyphOrder = cff2GetGlyphOrder
    if not hasattr(topDict, "FDArray"):
        fdArray = topDict.FDArray = FDArrayIndex()
        fdArray.strings = None
        fdArray.GlobalSubrs = topDict.GlobalSubrs
        topDict.GlobalSubrs.fdArray = fdArray
        charStrings = topDict.CharStrings
        if charStrings.charStringsAreIndexed:
            charStrings.charStringsIndex.fdArray = fdArray
        else:
            charStrings.fdArray = fdArray
        fontDict = FontDict()
        fontDict.setCFF2(True)
        fdArray.append(fontDict)
        fontDict.Private = privateDict
        privateOpOrder = buildOrder(privateDictOperators2)
        for entry in privateDictOperators:
            key = entry[1]
            if key not in privateOpOrder:
                if key in privateDict.rawDict:
                    # print "Removing private dict", key
                    del privateDict.rawDict[key]
                if hasattr(privateDict, key):
                    delattr(privateDict, key)
                    # print "Removing privateDict attr", key
    else:
        # clean up the PrivateDicts in the fdArray
        fdArray = topDict.FDArray
        privateOpOrder = buildOrder(privateDictOperators2)
        for fontDict in fdArray:
            fontDict.setCFF2(True)
            for key in fontDict.rawDict.keys():
                if key not in fontDict.order:
                    del fontDict.rawDict[key]
                    if hasattr(fontDict, key):
                        delattr(fontDict, key)

            privateDict = fontDict.Private
            for entry in privateDictOperators:
                key = entry[1]
                if key not in privateOpOrder:
                    if key in privateDict.rawDict:
                        # print "Removing private dict", key
                        del privateDict.rawDict[key]
                    if hasattr(privateDict, key):
                        delattr(privateDict, key)
                        # print "Removing privateDict attr", key
    # Now delete up the decrecated topDict operators from CFF 1.0
    for entry in topDictOperators:
        key = entry[1]
        if key not in opOrder:
            if key in topDict.rawDict:
                del topDict.rawDict[key]
            if hasattr(topDict, key):
                delattr(topDict, key)

    # At this point, the Subrs and Charstrings are all still T2Charstring class
    # easiest to fix this by compiling, then decompiling again
    cff.major = 2
    file = BytesIO()
    cff.compile(file, otFont, isCFF2=True)
    file.seek(0)
    cff.decompile(file, otFont, isCFF2=True)
Exemplo n.º 11
0
def lib_convertCFFToCFF2(cff, otFont):
	# This assumes a decompiled CFF table.
	cff2GetGlyphOrder = cff.otFont.getGlyphOrder
	topDictData = TopDictIndex(None, cff2GetGlyphOrder, None)
	topDictData.items = cff.topDictIndex.items
	cff.topDictIndex = topDictData
	topDict = topDictData[0]
	if hasattr(topDict, 'Private'):
		privateDict = topDict.Private
	else:
		privateDict = None
	opOrder = buildOrder(topDictOperators2)
	topDict.order = opOrder
	topDict.cff2GetGlyphOrder = cff2GetGlyphOrder
	if not hasattr(topDict, "FDArray"):
		fdArray = topDict.FDArray = FDArrayIndex()
		fdArray.strings = None
		fdArray.GlobalSubrs = topDict.GlobalSubrs
		topDict.GlobalSubrs.fdArray = fdArray
		charStrings = topDict.CharStrings
		if charStrings.charStringsAreIndexed:
			charStrings.charStringsIndex.fdArray = fdArray
		else:
			charStrings.fdArray = fdArray
		fontDict = FontDict()
		fontDict.setCFF2(True)
		fdArray.append(fontDict)
		fontDict.Private = privateDict
		privateOpOrder = buildOrder(privateDictOperators2)
		for entry in privateDictOperators:
			key = entry[1]
			if key not in privateOpOrder:
				if key in privateDict.rawDict:
					# print "Removing private dict", key
					del privateDict.rawDict[key]
				if hasattr(privateDict, key):
					delattr(privateDict, key)
					# print "Removing privateDict attr", key
	else:
		# clean up the PrivateDicts in the fdArray
		fdArray = topDict.FDArray
		privateOpOrder = buildOrder(privateDictOperators2)
		for fontDict in fdArray:
			fontDict.setCFF2(True)
			for key in fontDict.rawDict.keys():
				if key not in fontDict.order:
					del fontDict.rawDict[key]
					if hasattr(fontDict, key):
						delattr(fontDict, key)

			privateDict = fontDict.Private
			for entry in privateDictOperators:
				key = entry[1]
				if key not in privateOpOrder:
					if key in privateDict.rawDict:
						# print "Removing private dict", key
						del privateDict.rawDict[key]
					if hasattr(privateDict, key):
						delattr(privateDict, key)
						# print "Removing privateDict attr", key
	# Now delete up the decrecated topDict operators from CFF 1.0
	for entry in topDictOperators:
		key = entry[1]
		if key not in opOrder:
			if key in topDict.rawDict:
				del topDict.rawDict[key]
			if hasattr(topDict, key):
				delattr(topDict, key)

	# At this point, the Subrs and Charstrings are all still T2Charstring class
	# easiest to fix this by compiling, then decompiling again
	cff.major = 2
	file = BytesIO()
	cff.compile(file, otFont, isCFF2=True)
	file.seek(0)
	cff.decompile(file, otFont, isCFF2=True)
 def __init__(self, otf, ufo):
     self.ufo = ufo
     stream = BytesIO()
     otf.save(stream)
     stream.seek(0)
     self.otf = TTFont(stream)