-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
291 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pyside6 | ||
pyside6 | ||
ass | ||
fonttools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import shutil | ||
import os | ||
|
||
def copy_fonts(font_folder, subtitle_fonts): | ||
# Create the folder if it does not exist | ||
if not os.path.exists(font_folder): | ||
os.makedirs(font_folder) | ||
|
||
# Make sure not to copy fonts twice | ||
fonts_copied = set() | ||
|
||
for font, installed_ttf in subtitle_fonts: | ||
if installed_ttf in fonts_copied: | ||
# This font was already copied | ||
continue | ||
|
||
extension = os.path.splitext(installed_ttf)[-1] | ||
font_file = os.path.join(font_folder, f'{font}{extension}') | ||
|
||
if os.path.exists(font_file): | ||
# This font already exists in the folder | ||
continue | ||
|
||
# Copy the font into the folder | ||
shutil.copyfile(installed_ttf, font_file) | ||
fonts_copied.add(installed_ttf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from fontTools import ttLib | ||
from fontTools.ttLib import TTLibError | ||
|
||
def parse_font_name(ttf, installed_fonts): | ||
# Some fonts are collections | ||
# For these fonts, we have to query each font separately | ||
fontNumber = 0 | ||
|
||
while True: | ||
try: | ||
font = ttLib.TTFont(ttf, fontNumber=fontNumber) | ||
except TTLibError as e: | ||
# Maybe this isn't a TrueType font? | ||
if 'Not a TrueType' in str(e): | ||
print(ttf, 'is not a TrueType font') | ||
break | ||
|
||
# Let's check all names. | ||
for name in font['name'].names: | ||
# https://learn.microsoft.com/en-us/typography/opentype/spec/name#name-ids | ||
# Only check names that are font names | ||
if name.nameID in [1, 4]: | ||
# For some reason, names are in UTF-16-BE | ||
try: | ||
name = name.string.decode('utf-16-be') | ||
except: | ||
name = name.string.decode('utf-8') | ||
|
||
installed_fonts[name] = ttf | ||
|
||
if font.sfntVersion != b'ttcf': | ||
# Not a collection, so let's not check the rest of the fonts | ||
break | ||
|
||
# Check the next font if this is a collection | ||
fontNumber += 1 | ||
|
||
def parse_font_names(ttfs, installed_fonts): | ||
# Read all font files and see which are installed | ||
for ttf in ttfs: | ||
parse_font_name(ttf, installed_fonts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import ass | ||
import re | ||
|
||
def find_fonts_in_subtitle(subtitle_filename): | ||
# Now let's parse the subtitles | ||
with open(subtitle_filename, 'r', encoding='utf-8-sig') as f: | ||
doc = ass.parse(f) | ||
|
||
# These are the fonts that are used in the subtitle | ||
fonts = set() | ||
|
||
# First, the fonts used in all styles... | ||
for style in doc.styles: | ||
font_name = style.fontname | ||
fonts.add(font_name) | ||
|
||
# Second, the fonts that are manually specified in each dialogue | ||
# Format: \fnFontName\ | ||
pattern = re.compile(r'\\fn([^\\}]+)(\\|})') | ||
|
||
for event in doc.events: | ||
fonts_found = pattern.findall(event.text) | ||
|
||
# Add all fonts that are found | ||
for font in fonts_found: | ||
# The first match is the font name itself | ||
# The second match is the delimiter | ||
fonts.add(font[0]) | ||
|
||
# Sort the fonts | ||
fonts = list(fonts) | ||
fonts = sorted(fonts) | ||
return fonts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
import winreg | ||
|
||
def find_installed_ttfs(): | ||
installed_font_ttfs = [] | ||
|
||
# Find all installed font TTFs | ||
# There are two places to check: | ||
# - System fonts (Windows) | ||
# - User installed fonts (AppData) | ||
for registry_hive in [winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER]: | ||
reg = winreg.ConnectRegistry(None, registry_hive) | ||
key = winreg.OpenKey(reg, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts', 0, winreg.KEY_READ) | ||
|
||
for i in range(0, winreg.QueryInfoKey(key)[1]): | ||
ttf = winreg.EnumValue(key, i)[1] | ||
|
||
if '\\' not in ttf: | ||
ttf = os.path.join(os.environ['WINDIR'], 'Fonts', ttf) | ||
|
||
installed_font_ttfs.append(ttf) | ||
|
||
return installed_font_ttfs |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters