def main(): parser = argparse.ArgumentParser(description="Merge Aref Ruqaa fonts.") parser.add_argument("file1", metavar="FILE", help="input font to process") parser.add_argument("file2", metavar="FILE", help="input font to process") parser.add_argument("--out-file", metavar="FILE", help="output font to write", required=True) args = parser.parse_args() configLogger(level=logging.ERROR) merger = merge.Merger() font = merger.merge([args.file1, args.file2]) # Drop incomplete Greek support. unicodes = set(font.getBestCmap().keys()) - set(range(0x0370, 0x03FF)) options = subset.Options() options.set( layout_features="*", layout_scripts=["arab", "latn", "DFLT"], name_IDs="*", name_languages="*", notdef_outline=True, glyph_names=False, recalc_average_width=True, ) subsetter = subset.Subsetter(options=options) subsetter.populate(unicodes=unicodes) subsetter.subset(font) font.save(args.out_file)
def woffCreate(self, text=None, No='2'): mergeTools = merge.Merger() woffFile = "static/font/" + No + ".woff" saveFilename = str(Path(self.cwd) / woffFile) filename = [self.fontFile, saveFilename] textLuck = [] baseUrl = 'https://www.font.cn/preview/getFont?font_id=303&format=ttf&vers=v1.0&words=' urlEnd = '&hex=1' if text is None: return collectText = {t for t in text} text = ''.join(collectText) for t in text: if t not in self.name: hexT = str(hex(ord(t))) textLuck.append(hexT[2:]) # print(len(textLuck)) if len(textLuck) != 0: mergeUrl = ','.join(textLuck) aimUrl = baseUrl + mergeUrl + urlEnd fontDown = self.downloadWoff(aimUrl, saveFilename) if fontDown != None: mergeFont = mergeTools.merge(filename) mergeFont.save(self.fontFile) # 拆分合并后的字体文件成2.woff options = subset.Options() fontMerge = subset.load_font(self.fontFile, options) subsetter = subset.Subsetter(options) subsetter.populate(text=text) subsetter.subset(fontMerge) options.flavor = 'woff' subset.save_font(fontMerge, saveFilename, options)
def main(): t = Timer() parser = ArgumentParser() parser.add_argument("-d", "--directory", default="./", help="Path to directory containing the fonts") parser.add_argument("-o", "--output", default="merged.ttf", help="Path to output file.") parser.add_argument( "-v", "--verbose", action="store_true", help="Verbose mode, printing out more info", ) args = parser.parse_args() logging.basicConfig( level=logging.INFO if args.verbose else logging.WARNING) valid_files = build_valid_filenames(directory=args.directory) if len(valid_files) <= 1: log.warning( "expecting at least two fonts to merge, but only got %d " + "font(s).", len(valid_files), ) sys.exit(-1) for idx, file in enumerate(valid_files): if not has_gsub_table(file): log.info("adding default GSUB table to %s." % file) valid_files[idx] = add_gsub_to_font(file) merger = merge.Merger() print("Merging %d Fonts..." % len(valid_files)) font = merger.merge(valid_files) # Use the line metric in the first font to replace the one in final result. metrics = read_line_metrics(ttLib.TTFont(valid_files[0])) set_line_metrics(font, metrics) font.save(args.output) font.close() print("%d fonts are merged. %d fonts are skipped. Cost %0.3f s." % (len(valid_files), len(files) - len(valid_files), t.time())) print("Please check the result at %s." % os.path.abspath(os.path.realpath(args.output)))
def mergeFiles(): merger = mg.Merger() for i, font in enumerate(fontlist): fontname = font.split('/')[-1].replace('?raw=true', '') print('Merging ' + str(len(fontlist) - i)) try: if "NotoSans" or "NotoNast" in fontname: merged = merger.merge(fontfiles=[fontname, "NotoSans-Regular.ttf"]) elif "NotoSerif" in fontname: merged = merger.merge(fontfiles=[fontname, "NotoSerif-Regular.ttf"]) else: merged = merger.merge(fontfiles=[fontname, "NotoSans-Regular.ttf"]) merged.save("./merged/" + fontname) except Exception as e: print(e) print('Merging ' + fontname + ' failed')
def _merge_fonts(fonts, output_file_path): """ Given a list of fontTools font objects, merge them and export to output_file_path. Implemenatation note: it would have been nice to pass the fonts directly to the merger, but the current fontTools implementation of Merger takes a list of file names """ tmp = tempfile.gettempdir() f_names = [] for i, f in enumerate(fonts): tmp_font_path = os.path.join(tmp, "{}.woff".format(i)) f_names.append(tmp_font_path) f.save(tmp_font_path) merger = merge.Merger(options=FONT_TOOLS_OPTIONS) merged_font = merger.merge(f_names) merged_font.save(output_file_path) logging.info("created {}".format(output_file_path))
def main(): t = Timer() parser = ArgumentParser() parser.add_argument('-f', '--files', help='Path to YAML file containing paths to the fonts') parser.add_argument('-o', '--output', default='merged.ttf', help='Path to output file.') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose mode, printing out more info') args = parser.parse_args() logging.basicConfig(level=logging.INFO if args.verbose else logging.WARNING) if not args.files: print('add -h for usage') sys.exit(2) with open(args.files, 'r') as stream: try: valid_files = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) if len(valid_files) <= 1: log.warn('expecting at least two fonts to merge, but only got %d ' + 'font(s).', len(valid_files)) sys.exit(-1) for idx, file in enumerate(valid_files): if not has_gsub_table(file): log.info('adding default GSUB table to %s.' % file) valid_files[idx] = add_gsub_to_font(file) merger = merge.Merger() print('Merging %d Fonts...' % len(valid_files)) font = merger.merge(valid_files) # Use the line metric in the first font to replace the one in final result. metrics = read_line_metrics(ttLib.TTFont(valid_files[0])) set_line_metrics(font, metrics) font.save(args.output) font.close() print('%d fonts are merged. Cost %0.3f s.' % (len(valid_files), t.time())) print('Please check the result at %s.' % os.path.abspath( os.path.realpath(args.output)))
def main(newName, files): t = Timer() parser = ArgumentParser() parser.add_argument('-d', '--directory', default='./', help='Path to directory containing the fonts') # parser.add_argument('-o', '--output', default='merged.ttf', # help='Path to output file.') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose mode, printing out more info') args = parser.parse_args() logging.basicConfig( level=logging.INFO if args.verbose else logging.WARNING) valid_files = build_valid_filenames(files=files, directory=args.directory) if len(valid_files) <= 1: log.warning( 'expecting at least two fonts to merge, but only got %d ' + 'font(s).', len(valid_files)) # sys.exit(-1) return for idx, file in enumerate(valid_files): if not has_gsub_table(file): log.info('adding default GSUB table to %s.' % file) valid_files[idx] = add_gsub_to_font(file) merger = merge.Merger() print('Merging %d Fonts...' % len(valid_files)) font = merger.merge(valid_files) # Use the line metric in the first font to replace the one in final result. metrics = read_line_metrics(ttLib.TTFont(valid_files[0])) set_line_metrics(font, metrics) # print(os.path.basename(valid_files[0])) font.save(newName) font.close() print('%d fonts are merged. %d fonts are skipped. Cost %0.3f s.' % (len(valid_files), len(files) - len(valid_files), t.time())) print('Please check the result at %s.' % os.path.abspath(os.path.realpath(newName)))
def merging(self): print(" INFO: starts merging") merger = merge.Merger() for ftpath in self.actualFonts2merge: ft, upm = self.upm(ftpath) if upm != 1000: print("Scale", os.path.basename(ftpath), "at 1000 upm") scale_font(ft, 1000 / upm) ft.save(ftpath) self.font = merger.merge(self.actualFonts2merge) if len(self.metrics) > 0: self.font = self.updateMetrics(self.font) renamed = self.renamer() cleanedNewName = (self.newName.replace(" ", "") + "-" + self.tempStyle + self.italic.replace("-", "") + ".ttf") if "ttf" in self.output: renamed.save( os.path.join(self.destination, cleanedNewName.replace("RegularItalic", "Italic"))) if "woff2" in self.output: print(" save woff2 fonts") renamed.flavor = "woff2" woofName = cleanedNewName.replace(".ttf", ".woff2") renamed.save( os.path.join(self.destination, woofName.replace("RegularItalic", "Italic"))) for suppr in self.subsettedFonts2remove: os.remove(suppr) print(" INFO: ends merging\n") if self.subset != "": customDir = os.path.join(self.scriptsFolder, "Custom_Fonts") for ftpath in os.listdir(customDir): for flavour in [".ttf", ".woff2"]: if ftpath.endswith(flavour): kept = str(self.subset)[2:-2] font = self.customSubsetting( ttLib.TTFont(os.path.join(customDir, ftpath)), kept) font.save( os.path.join(customDir, os.path.basename(ftpath)))
def main(): merge_table = { 'Historic': [ 'Avestan', 'Carian', 'Egyptian Hieroglyphs', 'Imperial Aramaic', 'Pahlavi', # Should be 'Inscriptional Pahlavi', 'Parthian', # Should be 'Inscriptional Parthian', 'Linear B', 'Lycian', 'Lydian', 'Mandaic', 'Old Persian', 'Old South Arabian', 'Old Turkic', 'Osmanya', 'Phags-Pa', 'Phoenician', 'Samaritan', 'Sumero-Akkadian Cuneiform', 'Ugaritic', ], 'South Asian': [ 'Devanagari', 'Bengali', 'Gurmukhi', 'Gujarati', 'Oriya', 'Tamil', 'Telugu', 'Kannada', 'Malayalam', 'Sinhala', 'Thaana', 'Brahmi', 'Kaithi', 'Kharoshthi', # Move to Historic? 'Lepcha', 'Limbu', 'Meetei Mayek', 'Ol Chiki', 'Saurashtra', 'Syloti Nagri', ], 'Southeast Asian': [ 'Thai', 'Lao', 'Khmer', 'Batak', 'Buginese', 'Buhid', 'Cham', 'Hanunoo', 'Javanese', 'Kayah Li', 'New Tai Lue', 'Rejang', 'Sundanese', 'Tagalog', 'Tagbanwa', 'Tai Le', 'Tai Tham', 'Tai Viet', ], '': [ # LGC, 'Armenian', 'Bamum', 'Canadian Aboriginal', 'Cherokee', 'Coptic', 'Cypriot Syllabary', 'Deseret', 'Ethiopic', 'Georgian', 'Glagolitic', 'Gothic', 'Hebrew', 'Lisu', 'NKo', 'Ogham', 'Old Italic', 'Runic', 'Shavian', 'Tifinagh', 'Vai', ], } add_ui_alternative(merge_table, 'South Asian') add_ui_alternative(merge_table, 'Southeast Asian') for merge_target in sorted(merge_table): for weight in ['Regular', 'Bold']: merger = merge.Merger() source_fonts = merge_table[merge_target] if '' not in source_fonts: source_fonts = [''] + source_fonts # The LGC font regular_sources = [ make_font_file_name(script, weight) for script in source_fonts ] regular_sources = [ font for font in regular_sources if os.path.isfile(font) ] if len(regular_sources) <= 1: continue print('Merging Noto Sans %s %s' % (merge_target, weight)) for index, fontfile in enumerate(regular_sources): if not has_gsub_table(fontfile): regular_sources[index] = add_gsub_to_font(fontfile) font = merger.merge(regular_sources) first_font = source_fonts[0] if first_font != merge_target: for name_record in font['name'].names: name = name_record.string.decode('UTF-16BE') name = name.replace(make_font_name(first_font), make_font_name(merge_target)) name = name.replace(make_puncless_font_name(first_font), make_puncless_font_name(merge_target)) name_record.string = name.encode('UTF-16BE') font.save( make_font_file_name(merge_target, weight, directory='combined/unhinted'))
def main(): parser = argparse.ArgumentParser(description="Merge Aref Ruqaa fonts.") parser.add_argument("file1", metavar="FILE", help="input font to process") parser.add_argument("file2", metavar="FILE", help="input font to process") parser.add_argument("--color", action="store_true", help="merge color tables") parser.add_argument("--family", metavar="STR", help="base family name") parser.add_argument("--suffix", metavar="STR", help="suffix to add to family name") parser.add_argument( "--out-file", metavar="FILE", help="output font to write", required=True ) args = parser.parse_args() configLogger(level=logging.ERROR) merger = merge.Merger() font = merger.merge([args.file1, args.file2]) if args.color: orig = TTFont(args.file1) font["CPAL"] = copy.deepcopy(orig["CPAL"]) font["COLR"] = copy.deepcopy(orig["COLR"]) name = font["name"] family = args.family psname = args.family.replace(" ", "") for rec in name.names: if rec.nameID in (1, 3, 4, 6): rec.string = ( str(rec) .replace(family, family + " " + args.suffix) .replace(psname, psname + args.suffix) ) unicodes = font.getBestCmap().keys() options = subset.Options() options.set( layout_features="*", layout_scripts="*", name_IDs="*", name_languages="*", notdef_outline=True, glyph_names=False, recalc_average_width=True, ) subsetter = subset.Subsetter(options=options) subsetter.populate(unicodes=unicodes) subsetter.subset(font) if "glyf" in font: from fontTools.ttLib import newTable from fontTools.ttLib.tables import ttProgram font["prep"] = prep = newTable("prep") prep.program = ttProgram.Program() prep.program.fromAssembly( ["PUSHW[]", "511", "SCANCTRL[]", "PUSHB[]", "4", "SCANTYPE[]"] ) font.save(args.out_file)
def main(): merge_table = { "Historic": [ "Avestan", "Carian", "Egyptian Hieroglyphs", "Imperial Aramaic", "Pahlavi", # Should be 'Inscriptional Pahlavi', "Parthian", # Should be 'Inscriptional Parthian', "Linear B", "Lycian", "Lydian", "Mandaic", "Old Persian", "Old South Arabian", "Old Turkic", "Osmanya", "Phags-Pa", "Phoenician", "Samaritan", "Sumero-Akkadian Cuneiform", "Ugaritic", ], "South Asian": [ "Devanagari", "Bengali", "Gurmukhi", "Gujarati", "Oriya", "Tamil", "Telugu", "Kannada", "Malayalam", "Sinhala", "Thaana", "Brahmi", "Kaithi", "Kharoshthi", # Move to Historic? "Lepcha", "Limbu", "Meetei Mayek", "Ol Chiki", "Saurashtra", "Syloti Nagri", ], "Southeast Asian": [ "Thai", "Lao", "Khmer", "Batak", "Buginese", "Buhid", "Cham", "Hanunoo", "Javanese", "Kayah Li", "New Tai Lue", "Rejang", "Sundanese", "Tagalog", "Tagbanwa", "Tai Le", "Tai Tham", "Tai Viet", ], "": [ # LGC, "Armenian", "Bamum", "Canadian Aboriginal", "Cherokee", "Coptic", "Cypriot Syllabary", "Deseret", "Ethiopic", "Georgian", "Glagolitic", "Gothic", "Hebrew", "Lisu", "NKo", "Ogham", "Old Italic", "Runic", "Shavian", "Tifinagh", "Vai", ], } add_ui_alternative(merge_table, "South Asian") add_ui_alternative(merge_table, "Southeast Asian") for merge_target in sorted(merge_table): for weight in ["Regular", "Bold"]: merger = merge.Merger() source_fonts = merge_table[merge_target] if "" not in source_fonts: source_fonts = [""] + source_fonts # The LGC font regular_sources = [ make_font_file_name(script, weight) for script in source_fonts ] regular_sources = [ font for font in regular_sources if os.path.isfile(font) ] if len(regular_sources) <= 1: continue print("Merging Noto Sans %s %s" % (merge_target, weight)) for index, fontfile in enumerate(regular_sources): if not has_gsub_table(fontfile): regular_sources[index] = add_gsub_to_font(fontfile) font = merger.merge(regular_sources) first_font = source_fonts[0] if first_font != merge_target: for name_record in font["name"].names: name = name_record.string.decode("UTF-16BE") name = name.replace(make_font_name(first_font), make_font_name(merge_target)) name = name.replace( make_puncless_font_name(first_font), make_puncless_font_name(merge_target), ) name_record.string = name.encode("UTF-16BE") font.save( make_font_file_name(merge_target, weight, directory="combined/unhinted"))