Skip to content

Commit 07ffbf7

Browse files
authored
Support font files and font names for the --font flag, and update e2e tests to use builtin font (#98)
Signed-off-by: Eric Matthes <ehmatthes@gmail.com> Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent ec599fd commit 07ffbf7

23 files changed

+34
-6
lines changed

src/git_sim/__main__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import contextlib
12
import datetime
23
import os
34
import pathlib
45
import sys
56
import time
7+
from pathlib import Path
68

79
import typer
10+
import manim as m
11+
12+
from fontTools.ttLib import TTFont
813

914
import git_sim.commands
1015
from git_sim.settings import (
@@ -18,6 +23,12 @@
1823
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
1924

2025

26+
def get_font_name(font_path):
27+
"""Get the name of a font from its .ttf file."""
28+
font = TTFont(font_path)
29+
return font["name"].getName(4, 3, 1, 1033).toUnicode()
30+
31+
2132
def version_callback(value: bool) -> None:
2233
if value:
2334
print(f"git-sim version {git_sim.__version__}")
@@ -193,7 +204,15 @@ def main(
193204
settings.color_by = color_by
194205
settings.highlight_commit_messages = highlight_commit_messages
195206
settings.style = style
196-
settings.font = font
207+
208+
# If font is a path, define the context that will be used when using Manim.
209+
if Path(font).exists():
210+
font_path = Path(font)
211+
settings.font_context = m.register_font(font_path)
212+
settings.font = get_font_name(font_path)
213+
else:
214+
settings.font_context = contextlib.nullcontext()
215+
settings.font = font
197216

198217
try:
199218
if sys.platform == "linux" or sys.platform == "darwin":

src/git_sim/commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
def handle_animations(scene: Scene) -> None:
1515
from git_sim.animations import handle_animations as _handle_animations
1616

17-
return _handle_animations(scene)
17+
with settings.font_context:
18+
return _handle_animations(scene)
1819

1920

2021
def add(

src/git_sim/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Settings(BaseSettings):
4141
highlight_commit_messages: bool = False
4242
style: Union[StyleOptions, None] = StyleOptions.CLEAN
4343
font: str = "Monospace"
44+
font_context: bool = False
4445

4546
class Config:
4647
env_prefix = "git_sim_"

tests/e2e_tests/ProggyClean.ttf

40.2 KB
Binary file not shown.
-25 KB
Loading
-40.1 KB
Loading
-38 KB
Loading
-38.5 KB
Loading
-20.1 KB
Loading
-24 KB
Loading

0 commit comments

Comments
 (0)