Image

Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How can I create a Typst document that adheres to the OS font?

+1
−0

I provide documents that must be accessible to the dyslexic and [my/hyper]opic.

With CSS3, I am able to specify system-ui, where required. However, all browser stylesheets default to that, regardless. Typst appears to provide no equivalent.

Worse, typst fonts fails to enumerate all FontConfig aliases:

  1. import subprocess
    import xml.etree.ElementTree as ET
    from pathlib import Path
    
    
    def resolve_fontconfig_files(seed: Path, visited: set[Path] | None = None) -> list[Path]:
        """Recursively resolve all FontConfig config files via <include> directives."""
        if visited is None:
            visited = set()
    
        seed = seed.resolve()
        if not seed.exists() or seed in visited:
            return []
        visited.add(seed)
    
        files: list[Path] = [seed]
    
        try:
            tree = ET.parse(seed)
        except ET.ParseError:
            return files
    
        for include in tree.getroot().iter("include"):
            text = (include.text or "").strip()
            if not text:
                continue
            target = (seed.parent / text).resolve()
            if target.is_dir():
                for child in sorted(target.glob("*.conf")):
                    files.extend(resolve_fontconfig_files(child, visited))
            else:
                files.extend(resolve_fontconfig_files(target, visited))
    
        return files
    
    
    def collect_alias_names() -> list[str]:
        """Extract declared alias names from FontConfig XML config files."""
        roots: list[Path] = [
            Path("/etc/fonts/fonts.conf"),
            Path.home() / ".config/fontconfig/fonts.conf",
            Path.home() / ".fonts.conf",
        ]
    
        names: list[str] = []
        seen: set[str] = set()
        visited: set[Path] = set()
    
        for root in roots:
            for path in resolve_fontconfig_files(root, visited):
                try:
                    tree = ET.parse(path)
                except ET.ParseError:
                    continue
                for alias_el in tree.getroot().iter("alias"):
                    family_el = alias_el.find("family")
                    if family_el is None or not (family_el.text or "").strip():
                        continue
    
                    # Skip reverse mappings (real font → generic); they only have
                    # <accept> or <default>, never <prefer>.
                    if alias_el.find("prefer") is None:
                        continue
    
                    name = family_el.text.strip()
                    if name not in seen:
                        seen.add(name)
                        names.append(name)
    
        return names
    
    
    def resolve_alias(alias: str) -> list[str]:
        """Use fc-match to resolve an alias to its ordered list of families."""
        result = subprocess.run(
            ["fc-match", "--sort", "--format=%{family}\\n", alias],
            capture_output=True,
            text=True,
        )
        families: list[str] = []
        seen: set[str] = set()
        for line in result.stdout.splitlines():
            name = line.strip()
            if name and name not in seen:
                seen.add(name)
                families.append(name)
        return families
    
    
    def enumerate_fontconfig_aliases() -> dict[str, list[str]]:
        """Return a mapping of alias name → resolved font families via fc-match."""
        return {alias: resolve_alias(alias) for alias in collect_alias_names()}
    
    
    if __name__ == "__main__":
        import pprint
        pprint.pprint(collect_alias_names())
    

  2. ['system-ui',
     'sans-serif',
     'monospace',
     'FontAwesome',
     'emoji',
     'math',
     'serif',
     'fantasy',
     'cursive']
    

Consequently, I can't even revert to kcm_fonts's typeface:

  1. #!/usr/bin/env bash
    grep -i font $HOME/.config/kdeglobals | jc --ini | jq | yq -P
    
  2. font: Monospace,10,-1,5,400,0,0,0,0,0,0,0,0,0,0,1
    menuFont: Monospace,10,-1,5,400,0,0,0,0,0,0,0,0,0,0,1
    smallestReadableFont: Monospace,10,-1,5,400,0,0,0,0,0,0,0,0,0,0,1
    toolBarFont: Monospace,10,-1,5,400,0,0,0,0,0,0,0,0,0,0,1
    activeFont: Monospace,10,-1,5,400,0,0,0,0,0,0,0,0,0,0,1
    

I don't understand how one is supposed to create accessible markup, if the document cannot dynamically adhere to the DE's typeface, and the size thereof.

Context

My primary interest in Typst is that it appears to resolve what github.com/whatwg/html/issues/8751#issuecomment-2676253060 describes. However, these deficiencies must be resolved before it's usable. Considering that accessibility of the focus of both, I hope that that is sensical.

Reposts

  1. software.codidact.com/posts/296006

  2. forum.typst.app/t/8582

  3. github.com/typst/typst/issues/8160

History

0 comment threads

Sign up to answer this question »