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

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.

Regarding your use case, I think the reason there is no focus on this is that documents are usually compiled once and then distributed in one way or another. Therefore, the system font set on the computer producing the PDF is not the most important factor for producing the document. That system fonts are used by default is imo a concession to the fact that due to size, the amount of fonts that can be shipped with the compiler is necessarily small – but there’s an opt out using --ignore-system-fonts and override using --font-path to go in the reproducible direction.

That said, I see no particular issue with proposing support for these aliases; even if it’s not the most common use case, as long as these aliases are standardised, support shouldn’t hurt.

(Re the linked issue, my personal opinion is that Typst does not really resolve this. You write “the user having to create an unsemantic div class” about HTML, but using block(inset: ...) is basically that too: unsemantic styling of a general-purpose block level element.

2 Likes

@ensko, I’ve been distributing the source, and having it interpreted in real-time by an IDE plugin. Considering that CommonMark files are generally read in this manner, I don’t foresee it being unintuitive.

That’s brilliant to hear, for I believe that they are:

Would an equivalent issue be acceptable for adhering to the default font size? I ask although adhering to the default typeface is more important for those with dyslexia, I find that, amongst the elderly, the font size tends to be more important.

That’s sensical. However, considering that Typst appears to solely provide one way to accomplish indentation, whereas the WHATWG’s HTML5 LS (when CSS3 and ECMAScript are involved) provides so many ways, each of which can break when a user disables one of those components, Typst’s approach appears less brittle. I don’t imagine that #block directives can be disabled.

However, if you believe that a dedicated indentation directive (if that’s the name for such a thing) would be desirable, please confirm, and I’ll file an issue for that, too!

Having at least one option to recommend, which is semantic, would be wonderful. It’s the sole thing that prevents a lot of projects that I interact with from transferring their documentation from /plain to something more versatile.

I figured the setup had to be something like that. I want to add, in that case you have more options: you don’t necessarily need to create documents that, compiled using the Typst compiler directly, match the system defaults; applying extra settings before feeding the compiler the document would work as well. Should you be on the Typst Discord, the bot does something like that (adjust, margins, use a dark theme by default, etc.): typst-bot/crates/bot/src/bot.rs at main · mattfbacon/typst-bot · GitHub. It’s not very sophisticated – just prepending a few set rules.

Similarly, I have a small script to compile Markdown to PDF via Typst (that could be augmented with styles).

md-to-pdf.sh
ROOT=$(dirname $1)
IN=$(basename $1)
OUT=${2:-$ROOT/$(basename $IN .md).pdf}

typst c --root "$ROOT" --input "file=$IN" - "$OUT" << EOF
#import "@preview/cmarker:0.1.8"

#cmarker.render(read(sys.inputs.file))
EOF

If I’m not mistaken, that could be adapted roughly like this:

ROOT=$(dirname $1)
IN=$(basename $1)
OUT=${2:-$ROOT/$(basename $IN .typ).pdf}

SYSTEM_UI_FONT=...

typst c --root "$ROOT" --input "file=$IN" --input "system-ui-font=$SYSTEM_UI_FONT" - "$OUT" << EOF
#set text(font: sys.inputs.system-ui-font)

#include sys.inputs.file
EOF

You can propose it, but given the above, I’d probably add that in “user space”. It feels simpler to implement than font resolution, so a pattern on how to do this yourself sounds like enough to me.

I think that would make more sense if there was more consensus outside of Typst what this semantic element would look like. Just a few days ago I learned that Outlook Web’s Indent button surrounds content with <blockquote> elements; that was horrifying! In the medium term, Typst will get custom elements so users can define their own elements with semantic meaning, so the Typst core can stay out of that discussion instead of defining something that may become incompatible with later consensus down the line.

1 Like