Skip to content

Commit 78a1f7a

Browse files
committed
Add better log when there is a mismatch between the font request and the one we got
1 parent 6a3a2b2 commit 78a1f7a

4 files changed

Lines changed: 71 additions & 6 deletions

File tree

font_collector/__main__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
FontCollection,
1010
FontFile,
1111
FontLoader,
12-
FontResult,
1312
FontSelectionStrategyLibass,
14-
VariableFontFace
13+
VariableFontFace,
14+
font_weight_to_name
1515
)
1616
from .mkvpropedit import Mkvpropedit
1717
from .parse_arguments import parse_arguments
@@ -39,7 +39,7 @@ def main() -> None:
3939
file_handler.setFormatter(_handler.formatter)
4040
_logger.addHandler(file_handler)
4141
_logger.info(f"{Path.cwd()}>{' '.join(argv)}")
42-
42+
4343
try:
4444
fonts_file_found: set[FontFile] = set()
4545
additional_fonts = FontLoader.load_additional_fonts(additional_fonts_path)
@@ -65,11 +65,11 @@ def main() -> None:
6565
_logger.error(f"Used on lines: {' '.join(str(line) for line in usage_data.ordered_lines)}")
6666
else:
6767
if font_result.need_faux_bold:
68-
_logger.warning(f"Faux bold used for '{style.fontname}'.")
68+
_logger.warning(f"Faux bold used for '{style.fontname}' (requested weight {style.weight}-{(font_weight_to_name(style.weight))}, got {font_result.font_face.weight}-{(font_weight_to_name(font_result.font_face.weight))}).")
6969
elif font_result.mismatch_bold:
70-
_logger.warning(f"'{style.fontname}' does not have a bold variant.")
70+
_logger.warning(f"Mismatched weight for '{style.fontname}' (requested weight {style.weight}-{(font_weight_to_name(style.weight))}, got {font_result.font_face.weight}-{(font_weight_to_name(font_result.font_face.weight))}).")
7171
if font_result.mismatch_italic:
72-
_logger.warning(f"'{style.fontname}' does not have an italic variant.")
72+
_logger.warning(f"Mismatched italic for '{style.fontname}' (requested {'non-' if style.italic else ''}italic, got {'non-' if font_result.font_face.is_italic else ''}italic).")
7373

7474
if font_result.need_faux_bold or font_result.mismatch_bold or font_result.mismatch_italic:
7575
_logger.warning(f"Used on lines: {' '.join(str(line) for line in usage_data.ordered_lines)}")

font_collector/font/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
from .font_type import *
1111
from .name import *
1212
from .variable_font_face import *
13+
from .weight_helper import *
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import annotations
2+
3+
__all__ = ["font_weight_to_name"]
4+
5+
def font_weight_to_name(weight: int) -> str:
6+
"""
7+
Args:
8+
weight: The weight of a font as defined in usWeightClass:. https://learn.microsoft.com/en-us/typography/opentype/spec/os2#usweightclass
9+
Returns:
10+
The name corresponding to the weight.
11+
"""
12+
if weight <= 150:
13+
return "Thin"
14+
elif 151 <= weight <= 250:
15+
return "ExtraLight"
16+
elif 251 <= weight <= 350:
17+
return "Light"
18+
elif 351 <= weight <= 450:
19+
return "Regular"
20+
elif 451 <= weight <= 550:
21+
return "Medium"
22+
elif 551 <= weight <= 650:
23+
return "SemiBold"
24+
elif 651 <= weight <= 750:
25+
return "Bold"
26+
elif 751 <= weight <= 850:
27+
return "ExtraBold"
28+
else:
29+
return "Black"

tests/font/test_weight_helper.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
from font_collector import font_weight_to_name
3+
4+
5+
@pytest.mark.parametrize("weight,expected", [
6+
(50, "Thin"),
7+
(100, "Thin"),
8+
(150, "Thin"),
9+
(151, "ExtraLight"),
10+
(200, "ExtraLight"),
11+
(250, "ExtraLight"),
12+
(251, "Light"),
13+
(300, "Light"),
14+
(350, "Light"),
15+
(351, "Regular"),
16+
(400, "Regular"),
17+
(450, "Regular"),
18+
(451, "Medium"),
19+
(500, "Medium"),
20+
(550, "Medium"),
21+
(551, "SemiBold"),
22+
(600, "SemiBold"),
23+
(650, "SemiBold"),
24+
(651, "Bold"),
25+
(700, "Bold"),
26+
(750, "Bold"),
27+
(751, "ExtraBold"),
28+
(800, "ExtraBold"),
29+
(850, "ExtraBold"),
30+
(851, "Black"),
31+
(900, "Black"),
32+
(1000, "Black"),
33+
])
34+
def test_font_weight_to_name(weight, expected):
35+
assert font_weight_to_name(weight) == expected

0 commit comments

Comments
 (0)