Custom font ligatures
Asked Answered
C

2

5

I'm using Visual Studio Code and I see all these cool font ligatures for double and triple equals, arrows, etc. and I can't help but wonder if there is any way to add new custom ones to a font or VS Code. I tried doing some web searches but I can't seem to find anything.

For example, when I'm in python, I want the lambda keyword to be displayed as the real lambda symbol λ. Would that be possible?

Curare answered 7/12, 2020 at 21:19 Comment(0)
I
7

Those features are designed and built into the font file itself, rather than having anything to do with any one specific code editor.

If the font you’re considering adding them to is available under an open license or one that otherwise allows modifications to the software, it would be possible to add them.

A popular example of this is Fira Code, which is a modified version of the OFL-licensed Fira Mono, but with the ligature glyphs drawn specifically for the project.

There is a script for using those glyphs automatically in other fonts and generating the feature code, for fonts where the license allows modifications: https://github.com/ToxicFrog/Ligaturizer

The README describes how you can enable or disable new ligatures. Yours should be possible, because the “λ” lambda glyph probably already exists within Fira Code—otherwise you’d have to draw a new glyph as well.

The config you should need to add to ligatures.py is:

{
    # When the text has l+a+m+b+d+a…
    'chars': ['l', 'a', 'm', 'b', 'd', 'a'],

    # Use the existing `lambda` glyph from Fira Code, rather than one of the
    # custom drawn coding ligature glyphs
    'firacode_ligature_name': 'lambda',
},
Izolaiztaccihuatl answered 7/12, 2020 at 23:24 Comment(1)
This works, but please beware that it replaces lambda with "<space><space><space><space><lambda>λ", that is, the length of the word remains the same.Aldred
A
3

@kennethormandy's answer is correct in it's way of describing how ligatures are made and how they work.

However, i wouldn't recommend using ToxicFrog/Ligaturizer to create ligatures if you do not understand how Ligaturizer work.

The principle behind Ligaturizer is to copy glyphs from FiraCode and to use said glyph where a certain string is positioned ; that's it.

Meaning that if you create a ligature to transform alpha toward α like this :

{
    'chars': ['a', 'l', 'p', 'h', 'a'],
    'firacode_ligature_name': 'alpha'
}

It would replace ALL occurrences of alpha by α :

  • alpha1 would become α1.
  • alpha_test would become α_test.
  • /!\ alphabet would become αbet. /!\

As what the original goal is to use those ligatures to replace variable names and not all occurrences, i would instead use another way instead of Ligaturizer.

Now place to my answer : I would instead recommend using lemegeton/glyphizer

As glyphizer was inspired by this exact post (and was henceforth made to answer all of the downside of using Ligaturizer for this exact problem) there's no need to go and change any configuration file as all the Greek letters are already inside those (or maybe just to remove useless ligatures).

The goal of glyphizer is to create a tool that allow the creation of simple ligatures (like Ligaturizer) but also contextual ligatures (must start with XXX, must end with YYY, must not start with AAA, must not end with BBB ; in our alpha case it would be "must not start with [a-zA-Z] and must not end with [a-zA-Z]) while using the glyphs of the original font instead of the glyphs of FiraCode.

Airboat answered 21/4, 2024 at 14:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.