def ttcfile_build_from_namesfile( output_ttc_path, file_dir, namesfile_name=None, tool_path=_BUILD_TOOL_PATH ): """Read names of files from namesfile and pass them to build_ttc to build a .ttc file. The names file will default to one named after output_ttc and located in file_dir.""" output_ttc_path = tool_utils.resolve_path(output_ttc_path) if not namesfile_name: namesfile_name = ttc_namesfile_name(output_ttc_path) namesfile_path = path.join(file_dir, namesfile_name) if not path.isfile(namesfile_path): raise ValueError("could not find names file %s" % namesfile_path) filenames = tool_utils.read_lines(namesfile_path) with tool_utils.temp_chdir(file_dir): # resolve filenames relative to file_dir fontpath_list = [tool_utils.resolve_path(n) for n in filenames] missing = [n for n in fontpath_list if not path.isfile(n)] if missing: raise ValueError( "%d files were missing:\n %s" % (len(missing), "\n ".join(missing)) ) ttcfile_build(output_ttc_path, fontpath_list)
def remove_codepoints_from_ttc(ttc_name): otf_names = ttc_utils.ttcfile_extract(ttc_name, TEMP_DIR) with tool_utils.temp_chdir(TEMP_DIR): for index, otf_name in enumerate(otf_names): print 'Subsetting %s...' % otf_name remove_from_cmap(otf_name, otf_name, exclude=EXCLUDED_CODEPOINTS) ttc_utils.ttcfile_build(ttc_name, otf_names) for f in otf_names: os.remove(f)
def remove_codepoints_from_ttc(ttc_name): otf_names = ttc_utils.ttcfile_extract(ttc_name, TEMP_DIR, f'{PIP_USER}/otc2otf') with tool_utils.temp_chdir(TEMP_DIR): for otf_name in otf_names: print(f'Subsetting {otf_name}...') remove_from_cmap(otf_name, otf_name, exclude=EXCLUDED_CODEPOINTS) # TODO: Allow user choose output location ttc_utils.ttcfile_build(f'{SCRIPT_PATH}/output/{ttc_name}', otf_names, f'{PIP_USER}/otf2otc') for f in otf_names: os.remove(f)
def patch_cjk_ttc(ttc_srcfile, ttc_dstfile): """Take the source ttc, break it apart, remove the cjk emoji from each file, then repackage them into a new ttc.""" tmp_dir = tempfile.mkdtemp() font_names = ttc_utils.ttcfile_extract(ttc_srcfile, tmp_dir) tmp_patched_dir = path.join(tmp_dir, 'patched') os.mkdir(tmp_patched_dir) _remove_cjk_emoji(font_names, tmp_dir, tmp_patched_dir) # have ttcfile_build resolve names relative to patched dir with tool_utils.temp_chdir(tmp_patched_dir): ttc_utils.ttcfile_build(ttc_dstfile, font_names) shutil.rmtree(tmp_dir)
def ttcfile_extract(input_ttc_path, output_dir, tool_path=_EXTRACT_TOOL_PATH): """Extract .ttf/.otf fonts from a .ttc file, and return a list of the names of the extracted fonts.""" otc2otf = tool_utils.resolve_path(tool_path) if not otc2otf: raise ValueError('can not resolve %s' % tool_path) input_ttc_path = tool_utils.resolve_path(input_ttc_path) output_dir = tool_utils.ensure_dir_exists(output_dir) with tool_utils.temp_chdir(output_dir): # capture and discard standard output, the tool is noisy subprocess.check_output([otc2otf, input_ttc_path]) return ttcfile_filenames(input_ttc_path)
def ttcfile_build_from_namesfile( output_ttc_path, file_dir, namesfile_name=None, tool_path=_BUILD_TOOL_PATH): """Read names of files from namesfile and pass them to build_ttc to build a .ttc file. The names file will default to one named after output_ttc and located in file_dir.""" output_ttc_path = tool_utils.resolve_path(output_ttc_path) if not namesfile_name: namesfile_name = ttc_namesfile_name(output_ttc_path) namesfile_path = path.join(file_dir, namesfile_name) if not path.isfile(namesfile_path): raise ValueError('could not find names file %s' % namesfile_path) filenames = tool_utils.read_lines(namesfile_path) with tool_utils.temp_chdir(file_dir): # resolve filenames relative to file_dir fontpath_list = [tool_utils.resolve_path(n) for n in filenames] missing = [n for n in fontpath_list if not path.isfile(n)] if missing: raise ValueError( '%d files were missing:\n %s' % ( len(missing), '\n '.join(missing))) ttcfile_build(output_ttc_path, fontpath_list)