feat(portability): add __file__-anchored WORKSPACE_ROOT discovery, cross-OS font resolver, and public HTTP onboarding quickstart (v1.2.0)
This commit is contained in:
1 parent
f8235a2037
commit
4e38541fbf
7 files changed
+227
-103
No files matched your search
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
hyperstitionstud_io.publishing.fonts — Cross-OS TrueType Font Resolver with Built-In Fallbacks.
|
||||
Searches Debian/Ubuntu, Fedora/RHEL, Arch, Alpine, macOS, and user font directories so PDF and
|
||||
PNG rendering never crashes on OS-specific font paths.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
FONT_SEARCH_DIRS = [
|
||||
Path("/usr/share/fonts/truetype/dejavu"),
|
||||
Path("/usr/share/fonts/dejavu-sans-fonts"),
|
||||
Path("/usr/share/fonts/dejavu-sans-mono-fonts"),
|
||||
Path("/usr/share/fonts/TTF"),
|
||||
Path("/usr/share/fonts/dejavu"),
|
||||
Path.home() / ".local" / "share" / "fonts",
|
||||
Path.home() / "Library" / "Fonts",
|
||||
Path("/Library/Fonts"),
|
||||
Path("/System/Library/Fonts"),
|
||||
]
|
||||
|
||||
|
||||
def resolve_font_path(filename: str, fallback_filename: str | None = None) -> Path | None:
|
||||
for d in FONT_SEARCH_DIRS:
|
||||
cand = d / filename
|
||||
if cand.is_file():
|
||||
return cand
|
||||
if fallback_filename:
|
||||
for d in FONT_SEARCH_DIRS:
|
||||
cand = d / fallback_filename
|
||||
if cand.is_file():
|
||||
return cand
|
||||
return None
|
||||
|
||||
|
||||
def load_pillow_font(filename: str, size: int) -> Any:
|
||||
from PIL import ImageFont
|
||||
|
||||
p = resolve_font_path(filename, fallback_filename="DejaVuSans.ttf")
|
||||
if p is not None:
|
||||
return ImageFont.truetype(str(p), size)
|
||||
try:
|
||||
return ImageFont.load_default(size=size)
|
||||
except TypeError:
|
||||
return ImageFont.load_default()
|
||||
@@ -54,27 +54,37 @@ def compile_markdown_to_pdf(
|
||||
Table,
|
||||
TableStyle,
|
||||
)
|
||||
from hyperstitionstud_io.publishing.fonts import resolve_font_path
|
||||
|
||||
md_p = Path(md_path).resolve()
|
||||
pdf_p = Path(pdf_path).resolve()
|
||||
pdf_p.parent.mkdir(parents=True, exist_ok=True)
|
||||
tmp_p = pdf_p.with_suffix(f".tmp.{os.getpid()}.pdf")
|
||||
|
||||
oblique_ttf = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Oblique.ttf"
|
||||
bold_oblique_ttf = "/usr/share/fonts/truetype/dejavu/DejaVuSans-BoldOblique.ttf"
|
||||
pdfmetrics.registerFont(TTFont("DejaVu", "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"))
|
||||
pdfmetrics.registerFont(TTFont("DejaVu-Bold", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"))
|
||||
pdfmetrics.registerFont(TTFont("DejaVu-Oblique", oblique_ttf if Path(oblique_ttf).is_file() else "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"))
|
||||
pdfmetrics.registerFont(TTFont("DejaVu-BoldOblique", bold_oblique_ttf if Path(bold_oblique_ttf).is_file() else "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"))
|
||||
pdfmetrics.registerFont(TTFont("DejaVuMono", "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"))
|
||||
pdfmetrics.registerFont(TTFont("DejaVuMono-Bold", "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf"))
|
||||
pdfmetrics.registerFontFamily(
|
||||
"DejaVu",
|
||||
normal="DejaVu",
|
||||
bold="DejaVu-Bold",
|
||||
italic="DejaVu-Oblique",
|
||||
boldItalic="DejaVu-BoldOblique",
|
||||
)
|
||||
f_reg = resolve_font_path("DejaVuSans.ttf")
|
||||
f_bold = resolve_font_path("DejaVuSans-Bold.ttf")
|
||||
f_ob = resolve_font_path("DejaVuSans-Oblique.ttf", fallback_filename="DejaVuSans.ttf")
|
||||
f_bob = resolve_font_path("DejaVuSans-BoldOblique.ttf", fallback_filename="DejaVuSans-Bold.ttf")
|
||||
f_mreg = resolve_font_path("DejaVuSansMono.ttf")
|
||||
f_mbold = resolve_font_path("DejaVuSansMono-Bold.ttf")
|
||||
|
||||
if f_reg and f_bold and f_mreg and f_mbold:
|
||||
pdfmetrics.registerFont(TTFont("DejaVu", str(f_reg)))
|
||||
pdfmetrics.registerFont(TTFont("DejaVu-Bold", str(f_bold)))
|
||||
pdfmetrics.registerFont(TTFont("DejaVu-Oblique", str(f_ob or f_reg)))
|
||||
pdfmetrics.registerFont(TTFont("DejaVu-BoldOblique", str(f_bob or f_bold)))
|
||||
pdfmetrics.registerFont(TTFont("DejaVuMono", str(f_mreg)))
|
||||
pdfmetrics.registerFont(TTFont("DejaVuMono-Bold", str(f_mbold)))
|
||||
pdfmetrics.registerFontFamily(
|
||||
"DejaVu",
|
||||
normal="DejaVu",
|
||||
bold="DejaVu-Bold",
|
||||
italic="DejaVu-Oblique",
|
||||
boldItalic="DejaVu-BoldOblique",
|
||||
)
|
||||
base_font, bold_font, mono_font = "DejaVu", "DejaVu-Bold", "DejaVuMono"
|
||||
else:
|
||||
base_font, bold_font, mono_font = "Helvetica", "Helvetica-Bold", "Courier"
|
||||
|
||||
styles = getSampleStyleSheet()
|
||||
st_h1 = ParagraphStyle("HS_H1", parent=styles["Heading1"], fontName="DejaVu-Bold", fontSize=15, leading=19, textColor=colors.HexColor("#111827"), spaceAfter=8)
|
||||
|
||||
Reference in new issue
Block a user