From 45ccc129ce93c03153db7b790839fb52921b384c Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Mon, 25 Mar 2024 06:47:20 +0000 Subject: [PATCH] feat(dl): Try find SSAv4 fonts in System OS fonts folder Currently only Windows is supported. Feel free to make a pull request to add Linux or mac OS support. --- devine/commands/dl.py | 12 +++++++++++- devine/core/utilities.py | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/devine/commands/dl.py b/devine/commands/dl.py index b15fe0f..86d2fe4 100644 --- a/devine/commands/dl.py +++ b/devine/commands/dl.py @@ -50,7 +50,7 @@ from devine.core.titles import Movie, Song, Title_T from devine.core.titles.episode import Episode from devine.core.tracks import Audio, Subtitle, Tracks, Video from devine.core.tracks.attachment import Attachment -from devine.core.utilities import get_binary_path, is_close_match, time_elapsed_since +from devine.core.utilities import get_binary_path, get_system_fonts, is_close_match, time_elapsed_since from devine.core.utils.click_types import LANGUAGE_RANGE, QUALITY_LIST, SEASON_RANGE, ContextData, MultipleChoice from devine.core.utils.collections import merge_dict from devine.core.utils.subprocess import ffprobe @@ -607,13 +607,23 @@ class dl: font_names.append(line.removesuffix("Style: ").split(",")[1]) font_count = 0 + system_fonts = get_system_fonts() for font_name in set(font_names): family_dir = Path(config.directories.fonts, font_name) + fonts_from_system = [ + file + for name, file in system_fonts.items() + if name.startswith(font_name) + ] if family_dir.exists(): fonts = family_dir.glob("*.*tf") for font in fonts: title.tracks.add(Attachment(font, f"{font_name} ({font.stem})")) font_count += 1 + elif fonts_from_system: + for font in fonts_from_system: + title.tracks.add(Attachment(font, f"{font_name} ({font.stem})")) + font_count += 1 else: self.log.warning(f"Subtitle uses font [text2]{font_name}[/] but it could not be found...") diff --git a/devine/core/utilities.py b/devine/core/utilities.py index 3cf02bb..8faefc0 100644 --- a/devine/core/utilities.py +++ b/devine/core/utilities.py @@ -277,6 +277,27 @@ def get_extension(value: Union[str, Path, ParseResult]) -> Optional[str]: return ext +def get_system_fonts() -> dict[str, Path]: + if sys.platform == "win32": + import winreg + with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as reg: + key = winreg.OpenKey( + reg, + r"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts", + 0, + winreg.KEY_READ + ) + total_fonts = winreg.QueryInfoKey(key)[1] + return { + name.replace(" (TrueType)", ""): Path(r"C:\Windows\Fonts", filename) + for n in range(0, total_fonts) + for name, filename, _ in [winreg.EnumValue(key, n)] + } + else: + # TODO: Get System Fonts for Linux and mac OS + return {} + + class FPS(ast.NodeVisitor): def visit_BinOp(self, node: ast.BinOp) -> float: if isinstance(node.op, ast.Div):