Skip to content

Commit 73d3d15

Browse files
authored
Use system fonts instead of embedded fonts (#1429)
1 parent 35da020 commit 73d3d15

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

source/_extensions/post_process.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
cleanup_fontawesome_font_files,
1515
)
1616

17+
from .post_process_tasks.systemfonts import switch_to_system_fonts
18+
1719
READTHEDOCS_BUILDERS = ["readthedocs", "readthedocsdirhtml"]
1820

1921

@@ -31,6 +33,7 @@ def do(app: Sphinx, exception: Union[Exception, None]) -> None:
3133
# The two fontawesome functions must run in order
3234
cleanup_fontawesome_css(app)
3335
cleanup_fontawesome_font_files(app)
36+
switch_to_system_fonts(app)
3437

3538

3639
def setup(app: Sphinx) -> Dict[str, Any]:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import inspect
2+
import math
3+
import re
4+
from pathlib import Path
5+
6+
from sphinx.application import Sphinx
7+
8+
9+
def switch_to_system_fonts(app: Sphinx) -> None:
10+
"""
11+
Removes custom fonts and replaces their usage with system fonts.
12+
This can't be done in frc-rtd.css. frc-rtd.css is loaded in after the
13+
rtd theme's css is loaded. The custom fonts seems to start downloading
14+
before frc-rtd.css is parsed.
15+
"""
16+
17+
_name = inspect.stack()[0][3]
18+
print("Running", _name)
19+
outdir = Path(app.outdir)
20+
21+
RE_FONT = re.compile(r"(@font-face\{)|(font-family:.*?(?:;|\}))")
22+
23+
css_path = outdir / "_static" / "css"
24+
theme_css_file = css_path / "theme.css"
25+
26+
theme_text = theme_css_file.read_text(encoding="utf-8")
27+
28+
def sub_font_faces(match: re.Match):
29+
"""
30+
Delete font faces we don't want
31+
"""
32+
text = match.group()
33+
if "Roboto Slab" in text:
34+
return ""
35+
if "Lato" in text:
36+
return ""
37+
return text
38+
39+
theme_text = re.sub(r"@font-face\{.*?\}", sub_font_faces, theme_text)
40+
41+
def sub_font_usage(match: re.Match):
42+
"""
43+
Replace custom fonts with system fonts
44+
"""
45+
text, ending = match.groups()
46+
47+
if "Roboto Slab" in text:
48+
text = '-apple-system,BlinkMacSystemFont,"Segoe UI Variable Display", "Segoe UI", Roboto,Tahoma,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji", "Segoe UI Symbol";'
49+
elif "Lato" in text:
50+
text = '-apple-system,BlinkMacSystemFont,"Segoe UI Variable Text", "Segoe UI", Roboto,Tahoma,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji", "Segoe UI Symbol";'
51+
52+
return f"font-family:{text}{ending}"
53+
54+
theme_text = re.sub(r"font-family:(.*?)(;|\})", sub_font_usage, theme_text)
55+
56+
theme_css_file.write_text(theme_text, encoding="utf-8")
57+
58+
for path in css_path.glob("fonts/*"):
59+
if path.name.lower().startswith("lato"):
60+
path.unlink()
61+
if path.name.lower().startswith("roboto"):
62+
path.unlink()

0 commit comments

Comments
 (0)