Skip to content

Conversation

@danielwe
Copy link

@danielwe danielwe commented Sep 7, 2025

Description

For icons with the pa stretch rule, font_patcher currently scales icons up to fit the cell in monospace patching, but only scales down for non-monospace. This makes some icons significantly smaller with non-monospaced patching than with monospaced, which is rather unexpected. Case in point: the nd-md-alpha alphabetic icons are tiny in non-monospaced fonts, but substantial in monospaced.

With this PR, non-monospaced patching never makes smaller icons than monospaced. For pa, if monospaced patching would scale up, non-monospaced scales up by the same amount, but no more.

Requirements / Checklist

  • Read the Contributing Guidelines
  • I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan.
    Issue number where discussion took place: #xxx
  • If this contains a font/glyph add its origin as background info below (e.g. URL)
  • Verified the license of any newly added font, glyph, or glyph set. License is: xxx

What does this Pull Request (PR) do?

  • Factors the logic for determining iconheight out into a separate function that takes an optional single_width parameter
  • Uses this function to correctly calculate the scale factors that would be obtained in single_width when needed
  • For pa icons that don't need downscaling, sets the scale factor to the maximum of 1 and the single width scale factor.

How should this be manually tested?

  • Patch a font in both mono and non-mono
  • Look at 󰬇 (nd-md-alpha_z) in both variants at the same font size
  • With the previous patcher, you'll see something like this, where the larger icon is with the Mono variant
    Screenshot 2025-09-07 at 01 46 35Screenshot 2025-09-07 at 01 46 54
  • With this PR, the icon will have the larger size in both variants

Any background context you can provide?

See explanation.

What are the relevant tickets (if any)?

NA.

Screenshots (if appropriate or helpful)

See above.

@Finii
Copy link
Collaborator

Finii commented Sep 11, 2025

Thanks for the interesting PR!

I remember pondering that problem some years (?) ago when I wrote the "scales down never up" comment in the code.
Am still on vacation this week (6 weeks in total 😬); but I will try to recollect the reasoning and adapt probably. Changing the scaling usually results in a outcry of people who believe their beloved icon is now too big / too small and does not match that other icon anymore esthetically (and usually hey are right), so one person's feature is another person's bug ;-)

# non monospaced fonts just scale down on 'pa', not up
scale_ratio_x = min(scale_ratio_x, 1.0)
if scale_ratio_x > 1 and not self.args.single and not '!' in stretch and not overlap:
# on 'pa', non monospaced fonts only scale up as much as they would if monospaced
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love comments ❤️

@danielwe
Copy link
Author

Changing the scaling usually results in a outcry of people who believe their beloved icon is now too big / too small

I can imagine! Consider this my outcry about the current state 😅 You're free to take it or leave it, of course---just thought I'd bundle a suggested solution together with my complaint.

I struggle to see why anyone would prefer upscaling that only applies in monospace (note again that in the screenshots I provided, the left is non-Mono and the right is Mono), but stranger things have happened.

mitchellh added a commit to ghostty-org/ghostty that referenced this pull request Sep 29, 2025
#8563)

> This PR will probably need a rebase on the final outcome of #8550 and
~#8552~ #8580, but I'm putting it out here so folks can begin taking a
look if they want.

This is a rewrite of the code that applies scaling and alignment
constraints. The main intention is to further improve how Nerd Font
icons are matched to the primary font. This PR aligns the calculations
more closely with how the Nerd Font `font-patcher` script works, except
in two cases where we can easily do something unambiguously better (one
because of what's arguably a bug in the script, and one because we do
multi-cell alignment with knowledge of the pixel-rounded cell grid).

A goal of the rewrite is to make the scaling and alignment calculations
as clear and easy to follow as possible.

I'll lead with some screenshots. First the status quo, then this PR.
<img width="505" height="357" alt="Screenshot 2025-09-07 at 17 23 51"
src="https://github.com/user-attachments/assets/8e3ff9fd-3b66-4d54-be38-d54cf3b6cc5b"
/><img width="505" height="357" alt="Screenshot 2025-09-07 at 17 20 39"
src="https://github.com/user-attachments/assets/84fbe076-2e3f-4879-b9b2-91ce86b9ef5f"
/>

Relevant specs: macOS; 1920x1080; Ghostty config:
```ini
font-family = "CommitMono"
font-size = "15"
adjust-cell-height = "+20%"
```

**Points to note**

* Icons are generally larger, making better use of the available space.
* Icons are aligned nearly a pixel lower, better matching the text. This
is because alignment is now calculated from face metrics/bearings, not
the pixel-rounded cell. (See more below.)
* Relative sizes are better matched. Note especially that tall and
narrow icons, like the git branch symbol and icons depicting sheets of
paper, look conspicuously small in the status quo. With this PR, they're
better matched to other icons.
* Look at the letter Z icon I use as prompt character for zsh. It's
_tiny_ in the status quo, but properly sized with this PR. This
demonstrates the most important and clear-cut improvement we make over
`font-patcher`. (See more below.)
* Icons wider than a single cell are now left-aligned rather than
centered across two cells. I think this is preferable and makes better
use of space in most relevant contexts.
- Consider a Neovim bufferline showing the buffer title as a filetype
icon followed by the file name. Padding on the left would be a waste of
space, but having that extra space on the right can improve legibility.
- In listings, such as in the screenshots, columns look tidier when
their left edges are straight rather than ragged.
- This is how `font-patcher` does alignment, and thus what Nerd Font
users and UI designers expect.

**Implementation details**

I won't get too deep in the weeds here; see the code and comments. In
brief:

* `size_horizontal` and `size_vertical` are combined to a single `size`,
which can be `.none, .stretch, .fit, .cover` or `.fit_cover1`. The
latter implements the `pa` rule from `font-patcher`, except it works
better for icons that are small before scaling, like the letter Z prompt
in the screenshots. In short, it preserves aspect ratio while clamping
the size such that the icon `.cover`s at least one cell and `.fit`s
within the available space. See code comments and
ryanoasis/nerd-fonts/pull/1926 for details.
* An alignment mode `.center1` is added, implementing the centering rule
from `font-patcher` that I explained/defended above. In short, we center
the icon _in the first cell_, even it's allowed to span multiple cells.
For icons wider than a single cell, the lower bound that prevents them
from protruding to the left kicks in and turns this into left-alignment.
We keep the regular `.center` rule around for use with emojis, et
cetera.
* Scaling and alignment calculations only use the unrounded face metrics
and bearings. This ensures that pixel rounding of the cell and baseline,
and `adjust-cell-{width,height}`, don't affect scaling or relative
alignment; the icons are always scaled and aligned to the _face_. (The
one place we need to use cell metrics in the calculations is when we use
`cell_width` to obtain the inter-cell padding needed to correctly center
or right-align a glyph across two cells.)
- We can do this with impunity because we're blessed with sprite glyphs
in place of the "icons" that are actually box drawing and block graphics
characters 🙌

**Guide**

The meat of the changes is 100 % in `src/font/face.zig` and
`src/font/nerd_font_codegen.py`. Changes to other files only amount to
a) adding/changing some struct fields to get numbers to where they need
to be (see `src/font/Metrics.zig`), and b) collateral updates to make
otherwise unchanged code and tests work with/take advantage of the
modified structs.

Most files should have a clear and friendly diff. The exception is the
bottom half of `src/font/face.zig`, where the diff is meaningless and
the new code should just be reviewed on its own merits. This is the part
where the `constrain` function is rewritten and refactored. Scarred by
countless hours perusing `font-patcher`, I tried hard to make the math
and logic easy to follow here. I hope I have succeeded 🤞
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants