def convert_font(input_path, dest_path): filename = os.path.basename(input_path) print "Converting font: " + filename # the path to the output file. The file name is the fontfilename.ttx ttx_path = dest_path[:-1] + "x" try: # run ttx to generate an xml file in the output folder which represents all # its info ttx_args = ["-q", "-o", ttx_path, input_path] ttx.main(ttx_args) # now parse the xml file to change its PS name. tree = etree.parse(ttx_path) root = tree.getroot() for name in root.iter("name"): update_tag(name, get_font_info(name)) tree.write(ttx_path, xml_declaration=True, encoding="utf-8") # generate the udpated font now. ttx_args = ["-q", "-o", dest_path, ttx_path] ttx.main(ttx_args) except InvalidFontException: # In case of invalid fonts, we exit. print filename + " is not a valid font" raise except Exception as e: print "Error converting font: " + filename print e # Some fonts are too big to be handled by the ttx library. # Just copy paste them. shutil.copy(input_path, dest_path) try: # delete the temp ttx file is it exists. os.remove(ttx_path) except OSError: pass
def convert_font(input_path, dest_path): filename = os.path.basename(input_path) print 'Converting font: ' + filename # the path to the output file. The file name is the fontfilename.ttx ttx_path = dest_path[:-1] + 'x' try: # run ttx to generate an xml file in the output folder which represents all # its info ttx_args = ['-q', '-o', ttx_path, input_path] ttx.main(ttx_args) # now parse the xml file to change its PS name. tree = etree.parse(ttx_path) root = tree.getroot() for name in root.iter('name'): update_tag(name, get_font_info(name)) tree.write(ttx_path, xml_declaration=True, encoding='utf-8') # generate the udpated font now. ttx_args = ['-q', '-o', dest_path, ttx_path] ttx.main(ttx_args) except InvalidFontException: # In case of invalid fonts, we exit. print filename + ' is not a valid font' raise except Exception as e: print 'Error converting font: ' + filename print e # Some fonts are too big to be handled by the ttx library. # Just copy paste them. shutil.copy(input_path, dest_path) try: # delete the temp ttx file is it exists. os.remove(ttx_path) except OSError: pass
def TTFonts(filenames): # 转换XML转换ttf try: print("开始转换字体!!!" + filenames) ttx.main([filenames]) print("-----------------------------------") except Exception as e: print("Something went wrong converting ttx -> ttf/otf:") exit()
def test_main_system_exit(tmpdir, monkeypatch): with pytest.raises(SystemExit): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttx") outpath = tmpdir.join("TestTTF.ttf") args = ["-o", str(outpath), inpath] monkeypatch.setattr(ttx, "process", (lambda x, y: raise_exception(SystemExit))) ttx.main(args)
def test_main_system_exit(tmpdir, monkeypatch): with pytest.raises(SystemExit): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttx") outpath = tmpdir.join("TestTTF.ttf") args = ["-o", str(outpath), inpath] monkeypatch.setattr( ttx, "process", (lambda x, y: raise_exception(SystemExit)) ) ttx.main(args)
def test_main_keyboard_interrupt(tmpdir, monkeypatch, caplog): with pytest.raises(SystemExit): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttx") outpath = tmpdir.join("TestTTF.ttf") args = ["-o", str(outpath), inpath] monkeypatch.setattr(ttx, "process", (lambda x, y: raise_exception(KeyboardInterrupt))) ttx.main(args) assert "(Cancelled.)" in caplog.text
def execute_ttx(self, project_root, builddir, files): paths = [] for f in files: f = op.join(self.builddir, f) paths.append(f) self.stdout_pipe.write('$ ttx %s' % ' '.join(paths)) ttx.main(paths) for p in files: self.otf2ttf(p)
def execute_ttx(self, files): paths = [] for f in files: f = op.join(self.builddir, f) paths.append(f) self.bakery.logging_cmd('ttx %s' % ' '.join(paths)) ttx.main(paths) for p in files: self.otf2ttf(p)
def execute_ttx(self, files, pipedata): paths = [] for f in files: f = op.join(self.builddir, f) paths.append(f) self.bakery.logging_cmd('ttx {}'.format(' '.join(paths))) ttx.main(paths) for p in files: self.otf2ttf(p, pipedata)
def test_main_keyboard_interrupt(tmpdir, monkeypatch, capsys): with pytest.raises(SystemExit): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttx") outpath = tmpdir.join("TestTTF.ttf") args = ["-o", str(outpath), inpath] monkeypatch.setattr( ttx, "process", (lambda x, y: raise_exception(KeyboardInterrupt)) ) ttx.main(args) out, err = capsys.readouterr() assert "(Cancelled.)" in err
def process_ttf(name): dirname, filename, basename, extname = names(name) ttxname = basename + '.ttx' ttx.main(['-t', 'cmap', '-t', 'name', '-o', ttxname, name]) if not p.exists('modified'): os.makedirs('modified') process_ttx(ttxname) ttxnew = p.join('modified', ttxname) ttx.main(['-d', 'modified', '-b', '-m', name, ttxnew])
def test_main_ttlib_error(tmpdir, monkeypatch, caplog): with pytest.raises(SystemExit): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttx") outpath = tmpdir.join("TestTTF.ttf") args = ["-o", str(outpath), inpath] monkeypatch.setattr( ttx, "process", (lambda x, y: raise_exception(TTLibError("Test error"))), ) ttx.main(args) assert "Test error" in caplog.text
def test_main_base_exception(tmpdir, monkeypatch, capsys): with pytest.raises(SystemExit): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttx") outpath = tmpdir.join("TestTTF.ttf") args = ["-o", str(outpath), inpath] monkeypatch.setattr( ttx, "process", (lambda x, y: raise_exception(Exception("Test error"))), ) ttx.main(args) out, err = capsys.readouterr() assert "Unhandled exception has occurred" in err
def test(self): font_name = "Roboto-Regular.ttf" srcdir = tempfile.mkdtemp() print "srcdir: " + srcdir shutil.copy(font_name, srcdir) destdir = tempfile.mkdtemp() print "destdir: " + destdir self.assertTrue(build_font.main([srcdir, destdir]) is None) out_path = os.path.join(destdir, font_name) ttx.main([out_path]) ttx_path = out_path[:-1] + "x" tree = etree.parse(ttx_path) root = tree.getroot() name_tag = root.find('name') fonts = build_font.get_font_info(name_tag) shutil.rmtree(srcdir) shutil.rmtree(destdir) self.assertEqual(fonts[0].family, "Roboto1200310") self.assertEqual(fonts[0].fullname, "Roboto1200310 Regular")
if __name__ == "__main__": # Create a directory to do work in if not os.path.exists("merged_fonts"): os.makedirs("merged_fonts") # Copy all fonts to our directory so we don't corrupt the originals for file in getFilesWith([".ttf", ".otf"]): shutil.copy(file, "merged_fonts") # Change our working directory to our new one os.chdir("merged_fonts") #Convert fonts to xml and catch any exceptions for file in getFilesWith([".ttf", ".otf"]): try: ttx.main([file]) print "-----------------------------------" except Exception as e: print "Something went wrong converting ttf/otf -> ttx:" print e pass # Remove the old fonts that have been converted to xml for file in getFilesWith([".ttf", ".otf"]): os.remove(file) # Change font names changeFont(getFilesWith([".ttx"])) # Convert xml back to fonts for file in getFilesWith([".ttx"]):
def build(ttx): sys.argv = ['ttx', ttx] main()
def extract(ttf): sys.argv = ['ttx', '-z', 'extfile', ttf] main()
def test_main_getopterror_missing_directory(): with pytest.raises(SystemExit): with pytest.raises(getopt.GetoptError): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttf") args = ["-d", "bogusdir", inpath] ttx.main(args)
def test_main_default_ttx_compile_to_ttf(tmpdir): inpath = os.path.join("Tests", "ttx", "data", "TestTTF.ttx") outpath = tmpdir.join("TestTTF.ttf") args = ["-o", str(outpath), inpath] ttx.main(args) assert outpath.check(file=True)
import sys from fontTools import ttx ttx.main(sys.argv[1:])
filename = filename.split(".") filename.pop(-1) filename = ".".join(filename) # work only if its ttf try: if fileext != "ttf": print >> sys.stderr, "Given file is not a ttf file" exit() except (IndexError): print >> sys.stderr, "You should provide a ttf file as first argument" exit() # generate xml ttx.main([ filepath, ]) ttxfilepath = os.path.join(filedir, "%s.ttx" % filename) # parse xml to extract available mappings parser = make_parser() parser.setFeature(feature_namespaces, 0) dh = GetMappings() parser.setContentHandler(dh) fontttx = open(ttxfilepath) parser.parse(fontttx) # close and revert changes fontttx.close() os.remove(ttxfilepath) dh.maprange()
if __name__ == "__main__": # Create a directory to do work in if not os.path.exists("merged_fonts"): os.makedirs("merged_fonts") # Copy all fonts to our directory so we don't corrupt the originals for file in getFilesWith([".ttf",".otf"]): shutil.copy(file,"merged_fonts") # Change our working directory to our new one os.chdir("merged_fonts") #Convert fonts to xml and catch any exceptions for file in getFilesWith([".ttf",".otf"]): try: ttx.main([file]) print "-----------------------------------" except Exception as e: print "Something went wrong converting ttf/otf -> ttx:" print e pass # Remove the old fonts that have been converted to xml for file in getFilesWith([".ttf",".otf"]): os.remove(file) # Change font names changeFont(getFilesWith([".ttx"])) # Convert xml back to fonts for file in getFilesWith([".ttx"]):
fileext = filename.split(".")[-1] filename = filename.split(".") filename.pop(-1) filename = ".".join(filename) # work only if its ttf try: if fileext != "ttf": print >> sys.stderr, "Given file is not a ttf file" exit() except(IndexError): print >> sys.stderr, "You should provide a ttf file as first argument" exit() # generate xml ttx.main([filepath, ]) ttxfilepath = os.path.join(filedir, "%s.ttx" % filename) # parse xml to extract available mappings parser = make_parser() parser.setFeature(feature_namespaces, 0) dh = GetMappings() parser.setContentHandler(dh) fontttx = open(ttxfilepath) parser.parse(fontttx) # close and revert changes fontttx.close() os.remove(ttxfilepath) dh.maprange() dh.printme(filename)
#!/usr/bin/python __copyright__ = """Copyright 2015 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. """ # wrapper for the real ttx.py, which is in: # FDK/Tools/osx/Python/AFDKOPython27/lib/python2.7/site-packages/FontTools/fontTools/ttx.py # or the equivalent for win or linux os. import sys from fontTools import ttx ttx.main(sys.argv[1:])