Skip to content

Add variable font support - #8240

Closed
jassielof wants to merge 4 commits into
typst:mainfrom
jassielof:feat/fvar
Closed

jassielof wants to merge 4 commits into
typst:mainfrom
jassielof:feat/fvar

Conversation

@jassielof

@jassielof jassielof commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes #185

This follows up on the discussion in Discord's Font Forge channel: https://discord.com/channels/1054443721975922748/1184433451554840636/1502363192247189617

Hi Emilia,

I took a look at your branch and we discussed this a bit more in the team. I would say that the design is overall fairly aligned with what we have envisioned. The axes parameter is fine and, contrary to some earlier discussion on the server, I don't think that a font object is in any way a blocker for this.

I think your existing code forms a valuable basis, but my main concern is that we need the retain the flexibility to ship and land this on our timeline. There are also some concerns that relate to the web app (e.g. the Serialize for FontInfo is important to our font loading subsystem).

I think most of my concerns could be addressed through review fairly well, but (a) I can't guarantee that we'll be able to land the PR, and (b) it's very possible that I will want to take over and finish the PR myself at some point. This would either happen in the form of me pushing to the PR or by me porting your commits and/or changes to a new branch (obviously you would keep author credit). If this is fine with you, I would suggest you open a PR on typst/typst and I'd initially leave a review with a couple of high-level changes I would like to see. Then, we can see further.

Attachments

Round

Round axis using Google Sans Flex: round.pdf

#let the-font = "Google Sans Flex"
#set text(
  font: the-font,
  fallback: false,
)
#show raw: set text(
  font: the-font,
  fallback: false,
)

// #text(axes: (ROND: 1), size: 100pt, lorem(1))
// #text(axes: (ROND: 100), size: 100pt, lorem(1))


#for r in range(1, 101, step: 1) {
  set align(center + horizon)
  set page(flipped: true)
  text(axes: (ROND: r, wdth: 151), size: 100pt, lorem(2), weight: "bold")
}

Optical size

Optical sizing axis using Adobe's Source Serif:
size.pdf

// #let the-font = "Google Sans Flex" // ranges from 6 to 144
#let the-font = "Source Serif 4" // It's easier to notice a difference in optical size with serif fonts, ranges from 8 to 60
#set text(
  font: the-font,
  fallback: false,
)

#let display-size = 30pt

#for opt-size in range(8, 60, step: 1) {
  set align(center + horizon)
  set page(flipped: true, width: auto, height: auto)

  let scale-ratio = display-size / (opt-size * 1pt) * 100%

  scale(scale-ratio, reflow: true)[
    #text(size: opt-size * 1pt, style: "italic")[Handgloves]
  ]

  pagebreak(weak: true)
}

Weight

Weight axis using Google Sans Flex: weight.pdf

#let the-font = "Google Sans Flex"
#set text(
  font: the-font,
  fallback: false,
)
#show raw: set text(
  font: the-font,
  fallback: false,
)

#table(
  columns: (auto, auto, 1fr, auto),
  ..for w in range(100, 1000, step: 1) {
    (
      [#w],
      context [
        - #measure(text(weight: w, lorem(10))).width
        - #measure(text(weight: w, lorem(10))).height
      ],
      text(weight: w, lorem(10)),
      context text.font,
    )
  }
)

Credits

Mainly the initial #6771 and also https://github.com/yeslogic/allsorts as I used them to learn and how to implement it in Typst.

References

@jassielof

jassielof commented May 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Development assets will need variable fonts for tests and documentation (probably).

@laurmaedje

Copy link
Copy Markdown
Member

Thanks for opening a PR!

Initially, I have two high-level comments:

  • My main architectural comment would be about the Font type. I feel like it might be cleaner to have a type-system-level separation between an uninstantiated font and an instantiated one. In particular, I would imagine keeping the Font type free of instance parameters and instead adding a FontInstance type that can be created from a font and parameters. This type would then be used consistently across all parts of the code base that operate on fonts post-instantiation. The existing API surface of Font would partially migrate to FontInstance. The creation of FontInstance from Font could be memoized like in your create_variable_font_instance and FontInstance would also use Arc internally so that it's cheap to clone.

  • I would also prefer to (a) not have font-specific stuff in typst-library/src/lib.rs and (b) less code duplication in the places where book().select(..) is called. I feel like we could have a single place somewhere in the text or shaping module where the logic of font_by_key and book().select stuff is well-abstracted and then all the consumers of this abstraction have simple call sites.

@isuffix isuffix added the text Related to the text category, which is all about text handling, shaping, etc. label May 17, 2026
@jassielof jassielof changed the title Initial variable font support Add variable font support May 23, 2026
@jassielof

jassielof commented May 23, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll add the mentioned summary of testing fonts for each axis here (as mentioned in Discord), as a separate comment, rather than cluttering the main summary more then. I can also move it to the main summary if wanted.

The fonts I assume are to be added to the development assets, which I've already submitted a PR starting with Roboto Flex.


Ideally, it would be good to test the main registered axes, then as extras, test the proprietary ones, regardless.


Testing fonts for variable axes

Registered

Weight (wght)

Testing font: Google Sans Flex

Maps directly to:

  • Strong: strong() with the axis default value
  • Text weight text(weight: value)
  • Shorthand: *content...*

Optical size (opsz)

Testing font: Google Sans Flex, although serif fonts have an easier or more notorious change such as Adobe's Source Serif Variable.

Maps directly to:

  • Text size: text(size: value)

Slant (slnt)

Testing font: Google Sans Flex

There's no clear mapping for slant yet. As mentioned by @laurmaedje in Discord too.

Italic (ital)

One could say Inter, but it comes as a separate file. So, there's Retail Variable in that case with true italic axis.

Something to consider are cases where this axis is misused, such as Basenji Variable where the italics axis is used as a slant axis, as italics should be either 0 or 1.

I emailed the designer, as for why they did it this way and basically:

So you’re correct on the theory, and I agree with your reasoning. The decision wasn’t about typographic accuracy so much as making sure the fonts behaved consistently for the widest range of users. At this point I’m not planning to change those axes, but I do appreciate you flagging it and articulating the distinction so clearly.

In terms of behavior, it maps directly to:

  • Emphasize: emph()
  • Text style: text(style: "italic")
  • And the shorthand: _content..._

Width (wdth)

Testing font: Google Sans Flex

Similar to slant, there's no clear mapping, there's text stretch but yeah.

Proprietary

These are mainly to be used via text(axes: values).

Roundness (ROND)

The main font seems to be Google Sans Flex.

Bleed (BLED)

Sixtyfour Convergence.

Bounce (BNCE)

Shantell Sans.

AR Retinal Resolution (ARRR)

AR One Sans.

Ascender height (YTAS)

Roboto Flex.

Time of the day (TOTD) and distance (DIST)

GNOMON* as suggested by @isuffix.

Casual (CASL)

Recursive.

Contrast (CTRS)

Science Gothic.

Cursive (CRSV)

Recursive.

Descender depth (YTDE)

Roboto Flex.

Edge highlight (EHLT)

Nabla.

Element expansion (ELXP)

Bitcount Grid Double Ink.

Element grid (ELGR)

Handjet.

Yeast and gravity

Cheee Variable.

Wonky

Fraunces.

Vertical Position of Paint 1 and 2

Bitcount Grid Double Ink.

And well, many more axes. In these proprietary axes, the default, max/min, and step values, as well as the axis name, are entirely defined by the foundry or the designer, and are quite arbitrary. As for the registered by the specification ones, technically they should follow the convention strictly, but there's the case of the Basenji font.

@laurmaedje

Copy link
Copy Markdown
Member

Something to consider are cases where this axis is misused, such as Basenji Variable where the italics axis is used as a slant axis, as italics should be either 0 or 1.

Is this truly a misuse? My reading of the linked part of the OpenType spec would be that any value between 0 and 1 is fine.

@jassielof

jassielof commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Well, the specification kinda wants the axes values to be "strict", and the Basenji Variable has the italic axis ranging from 0 to 15, and it's behaving like the Slant axis.

I'll attach the full email in a bit.

@laurmaedje

If you meant if the italic axis can be continuous, then yeah. My misuse point was just on the italics axis being used as a slant with out of range values (0 to 15).

@laurmaedje

Copy link
Copy Markdown
Member

Superseded by #8425.

Thanks for your work on this! In the end, I made various different design decisions while working on it, so it made more sense to open a new PR, but this was still very useful as a base. :)

@laurmaedje laurmaedje closed this Jun 4, 2026
@jassielof
jassielof deleted the feat/fvar branch June 15, 2026 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

text Related to the text category, which is all about text handling, shaping, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support variable fonts

3 participants