Initial commit
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Inject @font-face declarations into converted HTML files so browsers can render custom fonts."""
|
||||
|
||||
import os
|
||||
import glob
|
||||
|
||||
HTML_DIR = os.path.join(os.path.dirname(__file__), "..", "public", "html")
|
||||
|
||||
# Map: TTF filename -> (family name, weight, style)
|
||||
FONT_MAP = {
|
||||
"balaram_.ttf": ("Balaram", "normal", "normal"),
|
||||
"bab_____.ttf": ("Balaram", "bold", "normal"),
|
||||
"bai_____.ttf": ("Balaram", "normal", "italic"),
|
||||
"babi____.ttf": ("Balaram", "bold", "italic"),
|
||||
"SCAGRG__.TTF": ("ScaGoudy", "normal", "normal"),
|
||||
"SCAGB___.TTF": ("ScaGoudy", "bold", "normal"),
|
||||
"SCAGI___.TTF": ("ScaGoudy", "normal", "italic"),
|
||||
"SCAGBI__.TTF": ("ScaGoudy", "bold", "italic"),
|
||||
"SCATRG__.TTF": ("ScaTimes", "normal", "normal"),
|
||||
"SCATB___.TTF": ("ScaTimes", "bold", "normal"),
|
||||
"SCATI___.TTF": ("ScaTimes", "normal", "italic"),
|
||||
"SCAPRG__.TTF": ("ScaPalatino", "normal", "normal"),
|
||||
"SCAPB___.TTF": ("ScaPalatino", "bold", "normal"),
|
||||
"SCAPI___.TTF": ("ScaPalatino", "normal", "italic"),
|
||||
"SCAHRG__.TTF": ("ScaHelvetica", "normal", "normal"),
|
||||
"SCAHB___.TTF": ("ScaHelvetica", "bold", "normal"),
|
||||
"SCAORG__.TTF": ("ScaOptima", "normal", "normal"),
|
||||
"SCAOB___.TTF": ("ScaOptima", "bold", "normal"),
|
||||
"SCAOI___.TTF": ("ScaOptima", "normal", "italic"),
|
||||
"SCAKRG__.TTF": ("ScaKorinna", "normal", "normal"),
|
||||
"SCAKB___.TTF": ("ScaKorinna", "bold", "normal"),
|
||||
"SCAKI___.TTF": ("ScaKorinna", "normal", "italic"),
|
||||
"SCABRG__.TTF": ("ScaBenguit", "normal", "normal"),
|
||||
"SCABB___.TTF": ("ScaBenguit", "bold", "normal"),
|
||||
"SCACRG__.TTF": ("ScaCheltenham", "normal", "normal"),
|
||||
"SCACB___.TTF": ("ScaCheltenham", "bold", "normal"),
|
||||
"SCACI___.TTF": ("ScaCheltenham", "normal", "italic"),
|
||||
"SCASRG__.TTF": ("ScaSabon", "normal", "normal"),
|
||||
"SCASB___.TTF": ("ScaSabon", "bold", "normal"),
|
||||
"SCASI___.TTF": ("ScaSabon", "normal", "italic"),
|
||||
"SCAFQRG_.TTF": ("ScaFrizQuadrata", "normal", "normal"),
|
||||
"SCAFQB__.TTF": ("ScaFrizQuadrata", "bold", "normal"),
|
||||
"S_TMS___.TTF": ("Sanskrit-Times", "normal", "normal"),
|
||||
"S_TMS_B_.TTF": ("Sanskrit-Times", "bold", "normal"),
|
||||
"S_TMS__I.TTF": ("Sanskrit-Times", "normal", "italic"),
|
||||
"S_TMS_BI.TTF": ("Sanskrit-Times", "bold", "italic"),
|
||||
"S_HLV___.TTF": ("Sanskrit-Helvetica", "normal", "normal"),
|
||||
"S_HLV_B_.TTF": ("Sanskrit-Helvetica", "bold", "normal"),
|
||||
"S_HLV__O.TTF": ("Sanskrit-Helvetica", "normal", "italic"),
|
||||
"S_HLV_BO.TTF": ("Sanskrit-Helvetica", "bold", "italic"),
|
||||
"S_NCALN.TTF": ("Sanskrit-New Caledonia", "normal", "normal"),
|
||||
"S_NCALB.TTF": ("Sanskrit-New Caledonia", "bold", "normal"),
|
||||
"S_NCALI.TTF": ("Sanskrit-New Caledonia", "normal", "italic"),
|
||||
"S_NCALBI.TTF": ("Sanskrit-New Caledonia", "bold", "italic"),
|
||||
"RMDEVA.TTF": ("RM Devanagari", "normal", "normal"),
|
||||
"BELLCN__.TTF": ("BellCentennial", "normal", "normal"),
|
||||
"inbenr11.ttf": ("Inbenr", "normal", "normal"),
|
||||
"inbenb11.ttf": ("Inbenb", "normal", "normal"),
|
||||
"inbeni11.ttf": ("Inbeni", "normal", "normal"),
|
||||
"inbeno11.ttf": ("Inbeno", "normal", "normal"),
|
||||
"indevr20.ttf": ("Indevr", "normal", "normal"),
|
||||
}
|
||||
|
||||
def build_font_face_css():
|
||||
rules = []
|
||||
for filename, (family, weight, style) in sorted(FONT_MAP.items()):
|
||||
rules.append(
|
||||
f"@font-face {{\n"
|
||||
f" font-family: '{family}';\n"
|
||||
f" src: url('/fonts/{filename}') format('truetype');\n"
|
||||
f" font-weight: {weight};\n"
|
||||
f" font-style: {style};\n"
|
||||
f" font-display: swap;\n"
|
||||
f"}}"
|
||||
)
|
||||
return "\n".join(rules)
|
||||
|
||||
def inject_into_html(html_path, font_css):
|
||||
with open(html_path, "r", encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
|
||||
# Remove old injection if present
|
||||
if "/* RTF-VIEWER-FONTS */" in content:
|
||||
import re
|
||||
content = re.sub(
|
||||
r'<style type="text/css">\s*/\* RTF-VIEWER-FONTS \*/.*?</style>\s*',
|
||||
'',
|
||||
content,
|
||||
flags=re.DOTALL
|
||||
)
|
||||
|
||||
style_tag = f'<style type="text/css">\n/* RTF-VIEWER-FONTS */\n{font_css}\n</style>'
|
||||
|
||||
if "</head>" in content:
|
||||
content = content.replace("</head>", f"{style_tag}\n</head>")
|
||||
elif "<body" in content:
|
||||
content = content.replace("<body", f"{style_tag}\n<body")
|
||||
else:
|
||||
content = style_tag + "\n" + content
|
||||
|
||||
with open(html_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
|
||||
print(f" Injected fonts into {os.path.basename(html_path)}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
font_css = build_font_face_css()
|
||||
html_files = glob.glob(os.path.join(HTML_DIR, "*.html"))
|
||||
|
||||
if not html_files:
|
||||
print("No HTML files found in", HTML_DIR)
|
||||
exit(1)
|
||||
|
||||
print(f"Injecting @font-face rules into {len(html_files)} HTML files...")
|
||||
for html_path in sorted(html_files):
|
||||
inject_into_html(html_path, font_css)
|
||||
print("Done!")
|
||||
Reference in New Issue
Block a user