Skip to content

Commit

Permalink
feat: Oldstyle figures using XCharter
Browse files Browse the repository at this point in the history
  • Loading branch information
otytlandsvik committed Nov 24, 2024
1 parent 6048d83 commit 393121e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@ This template uses a number of different fonts:

- Open Sans (Noto Sans)
- JetBrains Mono (Fira Code)
- Charis SIL (Charter)
- XCharter (Charter)

The above parenthesized fonts are fallback typefaces available by default in [the web app](https://typst.app).
If you'd like to use the main fonts instead, simply upload the `.ttf`s to the web app and it will detect and apply them automatically.
If you'd like to use the main fonts instead, simply upload the `.ttf`s or `.otf`s to the web app and it will detect and apply them automatically.

XCharter is commonly packaged only for LaTeX, however to use it with typst, we need to fetch the archive from [CTAN](https://mirrors.ctan.org/fonts/xcharter.zip).
The required `.otf` font files are in the `opentype` directory of the archive.

If you're running typst locally, install the fonts in a directory of your choosing and specify it with `--font-path`.

> [!IMPORTANT]
> We use XCharter to access additional font features not available in Charter, such as smallcaps and oldstyle figures.
> If you instead use the fallback font Charter to compile your document, be aware that these are unavailable.
### Nix

If you're using the nix package manager, simply run the provided dev shell. It includes all dependencies needed to write and build the document locally, including the main fonts.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
...
}:
let
xcharter = pkgs.callPackage ./xcharter.nix { inherit pkgs; };
fontPackages = pkgs.symlinkJoin {
name = "typst-fonts";
paths = with pkgs; [
noto-fonts
open-sans
jetbrains-mono
charis-sil
xcharter
];
};
in
Expand Down
16 changes: 9 additions & 7 deletions lib.typ
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

// Common styles for main matter
#let main-matter(body) = {
set text(features: ("onum",))
set page(numbering: "1",
// Only show numbering in footer when no chapter header is present
footer: context {
Expand Down Expand Up @@ -250,8 +251,8 @@
show footnote.entry: set par(hanging-indent: 1.5em)

// Set the body font.
// Default is Charis SIL at 11pt
set text(font: ("Charis SIL", "Charter"), size: 11pt)
// Default is XCharter at 11pt
set text(font: ("XCharter", "Charter"), size: 11pt)

// Set raw text font.
// Default is JetBrains Mono at 9tp with DejaVu Sans Mono as fallback
Expand Down Expand Up @@ -309,7 +310,11 @@
let heading-number = if heading.numbering == none {
[]
} else {
text(counter(heading.where(level: 1)).display(), size: 62pt)
text(
counter(heading.where(level: 1)).display(),
features: ("lnum",),
size: 62pt,
)
}

// Reset figure numbering on every chapter start
Expand Down Expand Up @@ -355,7 +360,6 @@
show heading: set text(
font: ("Open Sans", "Noto Sans"),
weight: "bold",
features: ("sc", "si", "scit"),
hyphenate: false,
)

Expand Down Expand Up @@ -440,8 +444,6 @@
font: ("Open Sans", "Noto Sans"),
size: 8pt,
fill: uit-gray-color,
// FIXME: Seems to have no effect
// features: ("sc", "si", "scit"),
fill-line(upper(left-text), upper(right-text)),
)
}
Expand All @@ -453,6 +455,7 @@

// Configure equation numbering.
set math.equation(numbering: n => {
set text(font: ("XCharter", "Charter"))
let h1 = counter(heading).get().first()
numbering("(1.1)", h1, n)
})
Expand Down Expand Up @@ -575,7 +578,6 @@
..outrageous.presets.typst,
font-weight: ("bold", auto),
fill: (none, auto),
font: (("Charis SIL", "Charter"), ("Charis SIL", "Charter")),
vspace: (1.5em, 0.5em),

// Manually add indent and spacing
Expand Down
Binary file modified template/thesis.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions template/thesis.typ
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
),
))
// If you wish to use lining figures rather than oldstyle figures, uncomment this line.
// #set text(features: ("lnum",))
// Include as many chapters as you like.
= Introduction <chp:introduction>
Expand Down
17 changes: 17 additions & 0 deletions xcharter.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ pkgs }:

pkgs.stdenv.mkDerivation {
pname = "XCharter";
version = "1.26";

src = pkgs.fetchzip {
url = "https://mirrors.ctan.org/fonts/xcharter.zip";
hash = "sha256-2PfPmG15Q+woBzZ7BSC/6aq566/4PM40w1766+miRgg=";
};

installPhase = ''
runHook preInstall
install -Dm644 -t $out/share/fonts/opentype/ opentype/*.otf
runHook postInstall
'';
}

0 comments on commit 393121e

Please sign in to comment.