|
| 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