def isType1(pathOrFile): try: font = T1Font(pathOrFile) del font except T1Error: return False return True
def fontToUFO(src, dst, fileType=None): from robofab.ufoLib import UFOWriter from robofab.pens.adapterPens import SegmentToPointPen if fileType is None: fileType = guessFileType(src) if fileType is None: raise ValueError, "Can't determine input file type" ufoWriter = UFOWriter(dst) if fileType == "TTF": from fontTools.ttLib import TTFont font = TTFont(src, 0) elif fileType == "Type 1": from fontTools.t1Lib import T1Font font = T1Font(src) else: assert 0, "unknown file type: %r" % fileType inGlyphSet = font.getGlyphSet() outGlyphSet = ufoWriter.getGlyphSet() for glyphName in inGlyphSet.keys(): print "-", glyphName glyph = inGlyphSet[glyphName] def drawPoints(pen): pen = SegmentToPointPen(pen) glyph.draw(pen) outGlyphSet.writeGlyph(glyphName, glyph, drawPoints) outGlyphSet.writeContents() if fileType == "TTF": info = extractTTFFontInfo(font) elif fileType == "Type 1": info = extractT1FontInfo(font) ufoWriter.writeInfo(info)
def dump(font_path, gname): t1_font = T1Font(font_path) gs = t1_font.getGlyphSet() charstrings = t1_font["CharStrings"] g = charstrings[gname] g.decompile() operands = [] for b in g.program: if isinstance(b, int): operands.append(b) else: print("[{}] << {} >>".format( ", ".join(map(lambda v: str(v), operands)), b)) operands = []
def _readT1Font(pathOrFile): ## rewrite the fontTools read() cause it was using lots of carbon stuff not avialble in py 64bit import AppKit fileType, error = AppKit.NSWorkspace.sharedWorkspace().typeOfFile_error_(pathOrFile, None) normpath = pathOrFile.lower() readFunc = None if fileType == "com.adobe.postscript-lwfn-font": readFunc = readLWFN elif fileType == "com.apple.font-suitcase": readFunc = readLWFN elif normpath[-4:] == '.pfb': readFunc = readPFB else: readFunc = readOther font = T1Font() font.data = readFunc(pathOrFile) return font
def extractFontFromType1(pathOrFile, destination, doGlyphs=True, doInfo=True, doKerning=True, customFunctions=[]): source = T1Font(pathOrFile) destination.lib["public.glyphOrder"] = _extractType1GlyphOrder(source) if doInfo: extractType1Info(source, destination) if doGlyphs: extractType1Glyphs(source, destination) if doKerning: # kerning extraction is not supported yet. # in theory, it could be retried from an AFM. # we need to find the AFM naming rules so that we can sniff for the file. pass for function in customFunctions: function(source, destination)
def main(argv): if len(argv) != 3: print "Usage: %s <.pfb font file> <output .pdf path>" % argv[0] sys.exit(1) # Load the number of glyphs in the font. font = T1Font(argv[1]) glyph_count = len(font.getGlyphSet()) print "Glyphs in font: %d" % glyph_count # Craft the PDF header. pdf_data = "%PDF-1.1\n" pdf_data += "\n" pdf_data += "1 0 obj\n" pdf_data += "<< /Pages 2 0 R >>\n" pdf_data += "endobj\n" pdf_data += "\n" pdf_data += "2 0 obj\n" pdf_data += "<<\n" pdf_data += " /Type /Pages\n" pdf_data += " /Count 1\n" pdf_data += " /Kids [ 3 0 R ]\n" pdf_data += ">>\n" pdf_data += "endobj\n" pdf_data += "\n" pdf_data += "3 0 obj\n" pdf_data += "<<\n" pdf_data += " /Type /Page\n" pdf_data += " /MediaBox [0 0 612 792]\n" pdf_data += " /Contents 5 0 R\n" pdf_data += " /Parent 2 0 R\n" pdf_data += " /Resources <<\n" pdf_data += " /Font <<\n" pdf_data += " /CustomFont <<\n" pdf_data += " /Type /Font\n" pdf_data += " /Subtype /Type0\n" pdf_data += " /BaseFont /TestFont\n" pdf_data += " /Encoding /Identity-H\n" pdf_data += " /DescendantFonts [6 0 R]\n" pdf_data += " >>\n" pdf_data += " >>\n" pdf_data += " >>\n" pdf_data += ">>\n" pdf_data += "endobj\n" # Construct the font body object. with open(argv[1], "rb") as f: font_data = f.read() pdf_data += "4 0 obj\n" pdf_data += "<</Subtype /Type1/Length %d>>stream\n" % len(font_data) pdf_data += font_data pdf_data += "\nendstream\n" pdf_data += "endobj\n" # Construct the page contents object. pdf_data += "5 0 obj\n" pdf_data += "<< >>\n" pdf_data += "stream\n" pdf_data += "BT\n" segment_count = int(math.ceil(float(glyph_count) / GLYPHS_PER_SEGMENT)) for i in xrange(segment_count): pdf_data += " /CustomFont 12 Tf\n" if i == 0: pdf_data += " 10 780 Td\n" else: pdf_data += " 0 -10 Td\n" pdf_data += " <%s> Tj\n" % "".join( map(lambda x: struct.pack(">H", x), (x for x in xrange(i * GLYPHS_PER_SEGMENT, (i + 1) * GLYPHS_PER_SEGMENT)))).encode("hex") pdf_data += "ET\n" pdf_data += "endstream\n" pdf_data += "endobj\n" # Construct the descendant font object. pdf_data += "6 0 obj\n" pdf_data += "<<\n" pdf_data += " /FontDescriptor <<\n" pdf_data += " /Type /FontDescriptor\n" pdf_data += " /FontName /TestFont\n" pdf_data += " /Flags 5\n" pdf_data += " /FontBBox[0 0 10 10]\n" pdf_data += " /FontFile3 4 0 R\n" pdf_data += " >>\n" pdf_data += " /Type /Font\n" pdf_data += " /Subtype /Type1\n" pdf_data += " /BaseFont /TestFont\n" pdf_data += ">>\n" pdf_data += "endobj\n" # Construct the trailer. pdf_data += "trailer\n" pdf_data += "<<\n" pdf_data += " /Root 1 0 R\n" pdf_data += ">>\n" # Write the output to disk. with open(argv[2], "w+b") as f: f.write(pdf_data)