def find_file(file_name, directories=None, extensions=None): # Check if this is a file online first, and if so, download # it to a temporary directory if validators.url(file_name): import urllib.request from manimlib.utils.directories import get_downloads_dir stem, name = os.path.split(file_name) folder = get_downloads_dir() path = os.path.join(folder, name) urllib.request.urlretrieve(file_name, path) return path # Check if what was passed in is already a valid path to a file if os.path.exists(file_name): return file_name # Otherwise look in local file system directories = directories or [""] extensions = extensions or [""] possible_paths = (os.path.join(directory, file_name + extension) for directory in directories for extension in extensions) for path in possible_paths: if os.path.exists(path): return path raise IOError(f"{file_name} not Found")
def register_font(font_file: typing.Union[str, Path]): """Temporarily add a font file to Pango's search path. This searches for the font_file at various places. The order it searches it described below. 1. Absolute path. 2. Downloads dir. Parameters ---------- font_file : The font file to add. Examples -------- Use ``with register_font(...)`` to add a font file to search path. .. code-block:: python with register_font("path/to/font_file.ttf"): a = Text("Hello", font="Custom Font Name") Raises ------ FileNotFoundError: If the font doesn't exists. AttributeError: If this method is used on macOS. Notes ----- This method of adding font files also works with :class:`CairoText`. .. important :: This method is available for macOS for ``ManimPango>=v0.2.3``. Using this method with previous releases will raise an :class:`AttributeError` on macOS. """ input_folder = Path(get_downloads_dir()).parent.resolve() possible_paths = [ Path(font_file), input_folder / font_file, ] for path in possible_paths: path = path.resolve() if path.exists(): file_path = path break else: error = f"Can't find {font_file}." f"Tried these : {possible_paths}" raise FileNotFoundError(error) try: ok = manimpango.register_font(str(file_path)) assert ok yield finally: manimpango.unregister_font(str(file_path))