From a4b253907e058aff55bc57a17198a39cbec5ee01 Mon Sep 17 00:00:00 2001 From: moi15moi Date: Thu, 27 Jul 2023 20:15:35 -0400 Subject: [PATCH 1/7] [ass_document] get_used_style - Add collect_draw_fonts parameter --- font_collector/ass_document.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/font_collector/ass_document.py b/font_collector/ass_document.py index b6ec1e2..77bfcf7 100644 --- a/font_collector/ass_document.py +++ b/font_collector/ass_document.py @@ -3,6 +3,7 @@ from .usage_data import UsageData from ass import parse_file, parse_string, Dialogue, Document from ass_tag_analyzer import ( + AssDraw, AssInvalidTagBold, AssInvalidTagFontName, AssInvalidTagItalic, @@ -58,6 +59,7 @@ def _set_used_styles( line_style: AssStyle, current_style: AssStyle, current_wrap_style: WrapStyle, + collect_draw_fonts: bool ) -> None: """ Parameters: @@ -69,6 +71,7 @@ def _set_used_styles( line_style (AssStyle): Style of the line. In general, it will be equal to original_line_style except it there is an \rXXX current_style (AssStyle): Real style of the text. It exist since \fn, \b, \i can override the line_style. current_wrap_style (WrapStyle): Since \q can override the subtitle WrapStyle, we need it. + collect_draw_fonts (bool): If true, then it will also collect the draw style, if false, it will ignore it. """ for tag in tags: @@ -127,6 +130,7 @@ def _set_used_styles( line_style, current_style, current_wrap_style, + collect_draw_fonts ) elif isinstance(tag, AssText): @@ -156,9 +160,24 @@ def _set_used_styles( current_style = AssStyle( current_style.fontname, current_style.weight, current_style.italic ) + elif collect_draw_fonts and isinstance(tag, AssDraw): + usage_data = used_styles.get(current_style, None) + if usage_data is None: + usage_data = UsageData(set(), set([line_index])) + used_styles[current_style] = usage_data + else: + usage_data.lines.add(line_index) + + # We need to make an copy of the style since current_style can be modified + current_style = AssStyle( + current_style.fontname, current_style.weight, current_style.italic + ) + - def get_used_style(self) -> Dict[AssStyle, UsageData]: + def get_used_style(self, collect_draw_fonts: bool = False) -> Dict[AssStyle, UsageData]: """ + Parameters: + collect_draw_fonts (bool): If true, then it will also collect the draw style, if false, it will ignore it. Returns: An dictionnary which contain all the used AssStyle and it's UsageData. """ @@ -211,6 +230,7 @@ def get_used_style(self) -> Dict[AssStyle, UsageData]: line_style, current_style, sub_wrap_style, + collect_draw_fonts ) return used_styles From 37ea523d127276558d7853f21463f52bf35340e6 Mon Sep 17 00:00:00 2001 From: moi15moi Date: Thu, 27 Jul 2023 20:16:15 -0400 Subject: [PATCH 2/7] [parse_arguments] Add cmd argument --collect-draw-fonts --- font_collector/parse_arguments.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/font_collector/parse_arguments.py b/font_collector/parse_arguments.py index 680476f..cb1e009 100644 --- a/font_collector/parse_arguments.py +++ b/font_collector/parse_arguments.py @@ -38,6 +38,7 @@ def parse_arguments() -> Tuple[ bool, Set[Path], bool, + bool ]: """ Returns: @@ -102,6 +103,13 @@ def parse_arguments() -> Tuple[ If specified, FontCollector won't use the system font to find the font used by an .ass file. """, ) + parser.add_argument( + "--collect-draw-fonts", + action="store_true", + help=""" + If specified, FontCollector will collect the font used by the draw. For more detail when this is usefull, see: https://github.com/libass/libass/issues/617 + """, + ) args = parser.parse_args() @@ -123,6 +131,7 @@ def parse_arguments() -> Tuple[ additional_fonts = set() use_system_fonts = args.exclude_system_fonts + collect_draw_fonts = args.collect_draw_fonts return ( ass_files_path, @@ -131,4 +140,5 @@ def parse_arguments() -> Tuple[ delete_fonts, additional_fonts, use_system_fonts, + collect_draw_fonts ) From 9ac0b5ee309b5c888cb48a673f3f54aa1f245cc1 Mon Sep 17 00:00:00 2001 From: moi15moi Date: Thu, 27 Jul 2023 20:16:41 -0400 Subject: [PATCH 3/7] [__main__] Adapt main with the new collect_draw_fonts parameter --- font_collector/__main__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/font_collector/__main__.py b/font_collector/__main__.py index 04a033a..43a5d68 100644 --- a/font_collector/__main__.py +++ b/font_collector/__main__.py @@ -21,6 +21,7 @@ def main(): delete_fonts, additional_fonts, use_system_font, + collect_draw_fonts ) = parse_arguments() font_results: List[FontResult] = [] font_collection = FontLoader(additional_fonts, use_system_font).fonts @@ -28,7 +29,7 @@ def main(): 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() + styles = subtitle.get_used_style(collect_draw_fonts) nbr_font_not_found = 0 From a9941596f8e09ec24b7d0dc50d59662c7155f7da Mon Sep 17 00:00:00 2001 From: moi15moi Date: Thu, 27 Jul 2023 20:17:34 -0400 Subject: [PATCH 4/7] Rename tests\ass\Untitled.ass to tests\ass\Style test.ass --- tests/ass/{Untitled.ass => Style test.ass} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/ass/{Untitled.ass => Style test.ass} (100%) diff --git a/tests/ass/Untitled.ass b/tests/ass/Style test.ass similarity index 100% rename from tests/ass/Untitled.ass rename to tests/ass/Style test.ass From 2efe64b66cc2d40f51d36a43ab2ac4961cc7b39f Mon Sep 17 00:00:00 2001 From: moi15moi Date: Thu, 27 Jul 2023 20:18:07 -0400 Subject: [PATCH 5/7] Add tests\ass\Collect Draw Style test.ass --- tests/ass/Collect Draw Style test.ass | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/ass/Collect Draw Style test.ass diff --git a/tests/ass/Collect Draw Style test.ass b/tests/ass/Collect Draw Style test.ass new file mode 100644 index 0000000..878d776 --- /dev/null +++ b/tests/ass/Collect Draw Style test.ass @@ -0,0 +1,24 @@ +[Script Info] +; Script generated by Aegisub 9214, Daydream Cafe Edition [Shinon] +; http://www.aegisub.org/ +Title: Default Aegisub file +ScriptType: v4.00+ +WrapStyle: 0 +ScaledBorderAndShadow: yes +YCbCr Matrix: None +PlayResX: 640 +PlayResY: 480 + +[Aegisub Project Garbage] +Video File: ?dummy:25.000000:1000000:640:480:16:145:190: +Video AR Value: 1.333333 +Active Line: 1 + +[V4+ Styles] +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding +Style: Default,Verdana,35,&H00FFFFFF,&H000000FF,&H002C2C2C,&H00000000,-1,0,0,0,100,100,0,0,1,2.5,0,2,0,0,72,1 + +[Events] +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text +Dialogue: 0,0:00:00.00,0:00:05.00,Default,,0,0,0,,{\fnJester}Test +Dialogue: 0,0:00:00.00,0:00:05.00,Default,,0,0,0,,{\p1\fscx1000\fscy1000\fnTestGmail}m 0 0 m -43 -21 l 51 -21 l 52 49 l -44 49 From bce571e4c5000c094cbd6422609260d5da33d5be Mon Sep 17 00:00:00 2001 From: moi15moi Date: Thu, 27 Jul 2023 20:19:36 -0400 Subject: [PATCH 6/7] [test_ass_document] Add test_get_style_used_with_and_without_collect_draw_fonts --- tests/test_ass_document.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/test_ass_document.py b/tests/test_ass_document.py index 008ffa6..8f726c4 100644 --- a/tests/test_ass_document.py +++ b/tests/test_ass_document.py @@ -3,12 +3,12 @@ # Get ass path used for tests dir_path = os.path.dirname(os.path.realpath(__file__)) -path_ass = os.path.join(dir_path, "ass", "Untitled.ass") - -subtitle = AssDocument.from_file(path_ass) def test_get_style_used(): + path_ass = os.path.join(dir_path, "ass", "Style test.ass") + subtitle = AssDocument.from_file(path_ass) + styles = subtitle.get_used_style() expected_results = { @@ -23,3 +23,20 @@ def test_get_style_used(): } assert styles == expected_results + +def test_get_style_used_with_and_without_collect_draw_fonts(): + path_ass = os.path.join(dir_path, "ass", "Collect Draw Style test.ass") + subtitle = AssDocument.from_file(path_ass) + + styles = subtitle.get_used_style() + expected_results = { + AssStyle("Jester", 700, False): UsageData(set("Test"), {1}), + } + assert styles == expected_results + + styles = subtitle.get_used_style(True) + expected_results = { + AssStyle("Jester", 700, False): UsageData(set("Test"), {1}), + AssStyle("testgmail", 700, False): UsageData(set(), {2}), + } + assert styles == expected_results From 711ec55cc542bf668b7d60928eff0ef48064d2c0 Mon Sep 17 00:00:00 2001 From: moi15moi Date: Thu, 27 Jul 2023 20:20:08 -0400 Subject: [PATCH 7/7] Bump version to 2.1.2 --- font_collector/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/font_collector/_version.py b/font_collector/_version.py index 58039f5..4eabd0b 100644 --- a/font_collector/_version.py +++ b/font_collector/_version.py @@ -1 +1 @@ -__version__ = "2.1.1" +__version__ = "2.1.2"