Skip to content

Commit

Permalink
Merge pull request #24 from moi15moi/Add-collect-draw-fonts
Browse files Browse the repository at this point in the history
Add --collect-draw-fonts
  • Loading branch information
moi15moi authored Jul 28, 2023
2 parents 1281557 + 711ec55 commit c5b3a4f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
3 changes: 2 additions & 1 deletion font_collector/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ 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

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

Expand Down
2 changes: 1 addition & 1 deletion font_collector/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.1"
__version__ = "2.1.2"
22 changes: 21 additions & 1 deletion font_collector/ass_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,6 +59,7 @@ def _set_used_styles(
line_style: AssStyle,
current_style: AssStyle,
current_wrap_style: WrapStyle,
collect_draw_fonts: bool
) -> None:
"""
Parameters:
Expand All @@ -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:
Expand Down Expand Up @@ -127,6 +130,7 @@ def _set_used_styles(
line_style,
current_style,
current_wrap_style,
collect_draw_fonts
)

elif isinstance(tag, AssText):
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions font_collector/parse_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def parse_arguments() -> Tuple[
bool,
Set[Path],
bool,
bool
]:
"""
Returns:
Expand Down Expand Up @@ -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()

Expand All @@ -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,
Expand All @@ -131,4 +140,5 @@ def parse_arguments() -> Tuple[
delete_fonts,
additional_fonts,
use_system_fonts,
collect_draw_fonts
)
24 changes: 24 additions & 0 deletions tests/ass/Collect Draw Style test.ass
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
23 changes: 20 additions & 3 deletions tests/test_ass_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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

0 comments on commit c5b3a4f

Please sign in to comment.