How to align the first characters per line visually with varying font sizes?

The following snippet

#set text(font: "Fira Sans")
#rect(width: 5cm, height: 3cm, inset: (x: 0mm))[
  #text("B",size: 48pt)
  #linebreak()
  #lorem(8)
]

renders as
image

where the letter B (which could be at the beginning of a heading) is quite obviously not aligned with the text below. The large difference in font sizes exaggerates the space at the beginning of the line.

It seems to me that this is due how glyphs are layed out by the font itself, and none of the text(...) parameters change this issue.

Is there a way to visually align the first characters of each line, short of inserting hand-tuned negative spaces?

Hello and welcome! Are you looking for dropped capitals?

See droplet – Typst Universe if it suits your needs.

As for your issue, it is very subtle. See how it renders with a Serif font.
image
Unfortunately, I am not sure you could solve this… Calling font experts here :smiley:

Hi! No — it’s not dropped capitals that I’m looking for. The example might have been stripped down a bit too much; the top line with larger font is a regular heading, which I trimmed down to just the first character.

Yeah, for Serif fonts, the behaviour makes more sense: They’re probably not visually aligned in the way that protrusion would allow at line endings when using justification, but the default is much better. (And I’m not sure that “protrusion” at the beginning of lines would be desirable.)

This has come up before, and I also don’t think that there is a solution except manually inserting negative spacing. Note also that this doesn’t only affect Typst, but instead seems to rather be a quirk of sans serif fonts in general, as it also happens with HTML.

Thanks @Eric for the links, I didn’t manage to find any related issues before!

Somewhat related, the idea of protrusion at the beginning of lines has been mentioned in
this issue, and apparently LaTeX microtype has that feature.

There’s also a discussion regarding this issue for InDesign at https://community.adobe.com/t5/indesign-discussions/obvious-left-alignment-differences-between-headline-and-body-text/td-p/10775210.