-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Changes** - Now, we distinct a font file from a font face. There are now 2 kind of font face. "Variable" is for [variable font](https://fonts.google.com/knowledge/introducing_type/introducing_variable_fonts) and "Normal" for the rest. - Know what is the language of a family name or a exact name - From a FontFace, get the best name depending of the OS language via ``ABCFontFace.get_best_family_name``/``ABCFontFace.get_best_exact_name``. The user can also query a family name from a BCP47 tag via ``ABCFontFace.get_family_name_from_lang``/``ABCFontFace.get_exact_name_from_lang`` - Users can know implement their own ass document reader. They just need to extend the class ABCAssDocument - Users can create their own strategy to find a font. They simply need to implement FontSelectionStrategy. Also, the user can choose between 2 strategy (FontSelectionStrategyLibass and FontSelectionStrategyVSFilter) - Know if a font is a font collection (TTC/OTC file) - Know what is the font type (opentype/truetype) - Create a FontCollection class. It has the same responsabilities has the old FontLoader. The new FontLoader now only load the font cache, load a batch of fonts and load the system fonts. - Add ``need_faux_bold`` attribute to ``FontResult`` - Resolve the issue #8
- Loading branch information
Showing
196 changed files
with
4,784,161 additions
and
2,314 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
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,90 +1,100 @@ | ||
import logging | ||
import sys | ||
from .ass_document import AssDocument | ||
from .font import Font | ||
from .font_loader import FontLoader | ||
from .font_result import FontResult | ||
from .helpers import Helpers | ||
import shutil | ||
from .ass.ass_document import AssDocument | ||
from .font import FontCollection, FontFile, FontLoader, FontResult, FontSelectionStrategyLibass, VariableFontFace | ||
from .mkvpropedit import Mkvpropedit | ||
from .parse_arguments import parse_arguments | ||
from typing import List | ||
from pathlib import Path | ||
from typing import List, Set | ||
|
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def main(): | ||
def main() -> None: | ||
( | ||
ass_files_path, | ||
output_directory, | ||
mkv_path, | ||
delete_fonts, | ||
additional_fonts, | ||
additional_fonts_recursive, | ||
additional_fonts_path, | ||
additional_fonts_recursive_path, | ||
use_system_font, | ||
collect_draw_fonts | ||
collect_draw_fonts, | ||
convert_variable_to_collection | ||
) = parse_arguments() | ||
font_results: List[FontResult] = [] | ||
font_collection = FontLoader(additional_fonts, use_system_font, additional_fonts_recursive).fonts | ||
additional_fonts = FontLoader.load_additional_fonts(additional_fonts_path) | ||
additional_fonts.extend(FontLoader.load_additional_fonts(additional_fonts_recursive_path, True)) | ||
font_collection = FontCollection(use_system_font=use_system_font, additional_fonts=additional_fonts) | ||
font_strategy = FontSelectionStrategyLibass() | ||
|
||
for ass_path in ass_files_path: | ||
subtitle = AssDocument.from_file(ass_path) | ||
_logger.info(f"Loaded successfully {ass_path}") | ||
styles = subtitle.get_used_style(collect_draw_fonts) | ||
used_styles = subtitle.get_used_style(collect_draw_fonts) | ||
|
||
nbr_font_not_found = 0 | ||
|
||
for style, usage_data in styles.items(): | ||
for style, usage_data in used_styles.items(): | ||
|
||
font_result = Helpers.get_used_font_by_style(font_collection, style) | ||
font_result = font_collection.get_used_font_by_style(style, font_strategy) | ||
|
||
# Did not found the font | ||
if font_result is None: | ||
nbr_font_not_found += 1 | ||
_logger.error( | ||
f"Used on lines: {' '.join(str(line) for line in usage_data.ordered_lines)}" | ||
) | ||
_logger.error(f"Could not find font '{style.fontname}'") | ||
_logger.error(f"Used on lines: {' '.join(str(line) for line in usage_data.ordered_lines)}") | ||
else: | ||
font_results.append(font_result) | ||
|
||
if font_result.mismatch_bold: | ||
if font_result.need_faux_bold: | ||
_logger.warning(f"Faux bold used for '{style.fontname}'.") | ||
elif font_result.mismatch_bold: | ||
_logger.warning(f"'{style.fontname}' does not have a bold variant.") | ||
if font_result.mismatch_italic: | ||
_logger.warning( | ||
f"'{style.fontname}' does not have an italic variant." | ||
) | ||
|
||
if font_result.mismatch_bold or font_result.mismatch_italic: | ||
_logger.warning( | ||
f"Used on lines: {' '.join(str(line) for line in usage_data.ordered_lines)}" | ||
) | ||
|
||
if ( | ||
len( | ||
missing_glyphs := font_result.font.get_missing_glyphs( | ||
usage_data.characters_used | ||
) | ||
) | ||
> 0 | ||
): | ||
_logger.warning( | ||
f"'{style.fontname}' is missing the following glyphs used: {missing_glyphs}" | ||
) | ||
_logger.warning(f"'{style.fontname}' does not have an italic variant.") | ||
|
||
if font_result.need_faux_bold or font_result.mismatch_bold or font_result.mismatch_italic: | ||
_logger.warning(f"Used on lines: {' '.join(str(line) for line in usage_data.ordered_lines)}") | ||
|
||
|
||
missing_glyphs = font_result.font_face.get_missing_glyphs(usage_data.characters_used) | ||
if len(missing_glyphs) > 0: | ||
_logger.warning(f"'{style.fontname}' is missing the following glyphs used: {missing_glyphs}") | ||
|
||
if nbr_font_not_found == 0: | ||
_logger.info(f"All fonts found") | ||
else: | ||
_logger.info(f"{nbr_font_not_found} fonts could not be found.") | ||
|
||
fonts_found: List[Font] = [font.font for font in font_results] | ||
fonts_file_found: Set[FontFile] = set() | ||
|
||
for font_result in font_results: | ||
if font_result.font_face.font_file is None: | ||
raise ValueError(f"This font_face \"{font_result.font_face}\" isn't linked to any FontFile.") | ||
|
||
if convert_variable_to_collection and isinstance(font_result.font_face, VariableFontFace): | ||
font_name = font_result.font_face.get_best_family_prefix_from_lang().value | ||
font_filename = output_directory.joinpath(f"{font_name}.ttc") | ||
generated_font_file = font_result.font_face.variable_font_to_collection(font_filename) | ||
fonts_file_found.add(generated_font_file) | ||
else: | ||
fonts_file_found.add(font_result.font_face.font_file) | ||
|
||
if mkv_path is not None: | ||
if delete_fonts: | ||
Mkvpropedit.delete_fonts_of_mkv(mkv_path) | ||
Mkvpropedit.merge_fonts_into_mkv(fonts_found, mkv_path) | ||
Mkvpropedit.merge_fonts_into_mkv(fonts_file_found, mkv_path) | ||
else: | ||
Helpers.copy_font_to_directory(fonts_found, output_directory) | ||
if not output_directory.is_dir(): | ||
output_directory.mkdir() | ||
|
||
for font in fonts_file_found: | ||
font_filename = output_directory.joinpath(font.filename.resolve().name) | ||
# Don't overwrite fonts | ||
if not font_filename.is_file(): | ||
shutil.copy(font.filename, font_filename) | ||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) | ||
main() |
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,4 @@ | ||
from .abc_ass_document import * | ||
from .ass_document import * | ||
from .ass_style import * | ||
from .usage_data import * |
Oops, something went wrong.