Skip to content

Commit

Permalink
Merge pull request #23 from moi15moi/Replace-matplolib-by-FindSystemF…
Browse files Browse the repository at this point in the history
…ontsFilename

Replace matplolib by FindSystemFontsFilename
  • Loading branch information
moi15moi authored Jun 16, 2023
2 parents cea4426 + 35574d4 commit d44279d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 17 deletions.
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.0"
__version__ = "2.1.1"
21 changes: 7 additions & 14 deletions font_collector/font_loader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import pickle
from .font import Font
from matplotlib import font_manager
from find_system_fonts_filename import get_system_fonts_filename
from pathlib import Path
from tempfile import gettempdir
from typing import List, Set
Expand Down Expand Up @@ -60,7 +60,7 @@ def add_generated_font(font: Font):
@staticmethod
def load_system_fonts() -> Set[Font]:
system_fonts: Set[Font] = set()
fonts_paths: Set[str] = set(font_manager.findSystemFonts())
fonts_paths: Set[str] = get_system_fonts_filename()
system_font_cache_file = FontLoader.get_system_font_cache_file_path()

if os.path.exists(system_font_cache_file):
Expand All @@ -79,11 +79,7 @@ def load_system_fonts() -> Set[Font]:
# Add font that have been installed since last execution
added = fonts_paths.difference(cached_paths)
for font_path in added:
try:
system_fonts.update(Font.from_font_path(font_path))
except FileExistsError:
# matplotlib can sometimes returns file that aren't font: "C:\WINDOWS\Fonts\desktop.ini"
continue
system_fonts.update(Font.from_font_path(font_path))

# If there is a change, update the cache file
if len(added) > 0 or len(removed) > 0:
Expand All @@ -93,11 +89,7 @@ def load_system_fonts() -> Set[Font]:
else:
# Since there is no cache file, load the font
for font_path in fonts_paths:
try:
system_fonts.update(Font.from_font_path(font_path))
except FileExistsError:
# matplotlib can sometimes returns file that aren't font: "C:\WINDOWS\Fonts\desktop.ini"
continue
system_fonts.update(Font.from_font_path(font_path))

# Save the font into the cache file
with open(system_font_cache_file, "wb") as file:
Expand Down Expand Up @@ -128,8 +120,9 @@ def load_additional_fonts(additional_fonts_path: List[Path]) -> Set[Font]:
if os.path.isfile(font_path):
additional_fonts.update(Font.from_font_path(font_path))
elif os.path.isdir(font_path):
for path in font_manager.findSystemFonts(fontpaths=str(font_path)):
additional_fonts.update(Font.from_font_path(path))
for file in os.listdir(font_path):
if Path(file).suffix.lstrip(".").strip().lower() in ["ttf", "otf", "ttc", "otc"]:
additional_fonts.update(Font.from_font_path(os.path.join(font_path, file)))
else:
raise FileNotFoundError(f"The file {font_path} is not reachable")
return additional_fonts
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def find_version(*file_paths):
"ass-tag-analyzer",
"fontTools>=4.38.0",
"freetype-py",
"matplotlib>=3.6",
"FindSystemFontsFilename>=0.1.1",
],
entry_points={"console_scripts": ["fontcollector=font_collector.__main__:main"]},
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_get_used_font_by_style():

style = styles[0]
font_collection = FontLoader(
[os.path.join(dir_path, "fonts", "Raleway")], False
[os.path.join(dir_path, "fonts", "Raleway", "generated_fonts")], False
).fonts

font_result = Helpers.get_used_font_by_style(font_collection, style)
Expand Down

0 comments on commit d44279d

Please sign in to comment.