sebastiencs / icons-in-terminal

Use any fonts in the terminal without replacing or patching
MIT License
981 stars 46 forks source link

map icons to nerd fonts unicode ranges #47

Open onlinespending opened 2 years ago

onlinespending commented 2 years ago

This is a wonderful project, but think it would make sense to map the various icon packs to the unicode ranges used by the very popular and ubiquitous Nerd Fonts icon super pack. While the font fallback approach of icons-in-terminal works well for Linux, many often have to SSH into their server using a different platform as their client (Windows, iOS, etc.), where they're limited to selecting a single TrueType font.

The great thing is, by making the mapping consistent with Nerd Fonts for the glyphs that are common to both, we can have the best of both worlds.

I made a simple change to support arbitrary destination mapping, by adding a "dest-start" field to the json entries. For now, I just see if there's a dest-start value, and if so, set it to codepoint. If there's not, it will simply continue from the previous codepoint. This does require the json file entries to be organized sequentially, but this could be enhanced to be more robust.

--- a/scripts/generate_font.py
+++ b/scripts/generate_font.py
@@ -124,12 +124,15 @@ with open(sys.argv[1]) as config_file:
         if "map-names" in json_file:
             map_names = read_map_names(json_file["map-names"])

+        if "dest-start" in json_file:
+            codepoint = int(json_file["dest-start"], 16)
+
         start = codepoint
         inserted = []

         for symbol in font.glyphs("encoding"):
-            if codepoint == POWERLINE_START:
-                codepoint = POWERLINE_END
+            #if codepoint == POWERLINE_START:
+            #    codepoint = POWERLINE_END
             if (symbol.encoding in excludes or
                 symbol.encoding < start_from or
                 (until > 0 and symbol.encoding >= until)):

And here's my updated config.json file that maps the common glyphs to Nerd Fonts ranges.

[
    {
        "name": "pomicons",
        "short-name": "pom",
        "path": "./fonts/Pomicons.ttf",
        "start-from": "0xE000",
        "excludes": [ "0x20", "0xA0" ],
        "dest-start": "0xE000"
        },
    {
        "name": "weather",
        "path": "./fonts/weathericons.ttf",
        "map-names": "./fonts/weathericons-map",
        "start-from": "0xF000",
        "dest-start": "0xE300"
        },
    {
        "name": "devicons",
        "short-name": "dev",
        "path": "./fonts/devicons.ttf",
        "map-names": "./fonts/devicons-map",
        "start-from": "0xE600",
        "excludes": [ "0xFFFD" ],
        "dest-start": "0xE700"
        },
    {
        "name": "fileicons",
        "short-name": "file",
        "path": "./fonts/file-icons.ttf",
        "map-names": "fonts/file-icons-map",
        "move-vertically": -100,
        "dest-start": "0xE800"
    },
    {
        "name": "myicons",
        "path": "./fonts/myicons.ttf",
        "excludes": [ "0xFFFD" ]
    },
    {
        "name": "linea-arrows",
        "path": "./fonts/linea/linea-arrows-10.ttf",
        "map-names": "./fonts/linea/linea-arrows-10-map",
        "move-vertically": -200
    },
    {
        "name": "linea-basic",
        "path": "./fonts/linea/linea-basic-10.ttf",
        "map-names": "./fonts/linea/linea-basic-10-map",
        "move-vertically": -200
    },
    {
        "name": "linea-basic-elaboration",
        "short-name": "linea-elaboration",
        "path": "./fonts/linea/linea-basic-elaboration-10.ttf",
        "map-names": "./fonts/linea/linea-basic-elaboration-10-map",
        "move-vertically": -200
    },
    {
        "name": "linea-ecommerce",
        "path": "./fonts/linea/linea-ecommerce-10.ttf",
        "map-names": "./fonts/linea/linea-ecommerce-10-map",
        "move-vertically": -200
    },
    {
        "name": "linea-music",
        "path": "./fonts/linea/linea-music-10.ttf",
        "map-names": "./fonts/linea/linea-music-10-map",
        "move-vertically": -200
    },
    {
        "name": "linea-software",
        "path": "./fonts/linea/linea-software-10.ttf",
        "map-names": "./fonts/linea/linea-software-10-map",
        "move-vertically": -200
    },
    {
        "name": "linea-weather",
        "path": "./fonts/linea/linea-weather-10.ttf",
        "map-names": "./fonts/linea/linea-weather-10-map",
        "move-vertically": -200
    },
    {
        "name": "mfizz",
        "path": "./fonts/font-mfizz.ttf",
        "map-names": "./fonts/font-mfizz-map",
        "excludes": [ "0x20" ]
    },
    {
        "name": "firacode",
        "path": "./fonts/FiraCode-Regular-Symbol.otf",
        "map-names": "./fonts/FiraCode-Regular-Symbol-map",
        "excludes": [ "0xE0A0", "0xE0A1", "0xE0A2", "0xE0B0", "0xE0B1", "0xE0B2", "0xE0B3" ]
    },
    {
        "name": "fontawesome",
        "short-name": "fa",
        "path": "./fonts/FontAwesome.otf",
        "start-from": "0xF000",
        "map-names": "fonts/FontAwesome-map",
        "until": "0xF2E1",
        "dest-start": "0xF000"
        },
    {
        "name": "linux",
        "short-name": "linux",
        "path": "./fonts/font-linux.ttf",
        "map-names": "./fonts/font-linux-map",
        "start-from": "0xF100",
        "dest-start": "0xF300"
        },
        {
        "name": "octicons",
        "short-name": "oct",
        "path": "./fonts/octicons.ttf",
        "dest-start": "0xF400"
        },
    {
        "name": "material",
        "short-name": "md",
        "path": "./fonts/material-design-icons.ttf",
        "map-names": "./fonts/material-design-map",
        "start-from": "0xE000",
        "move-vertically": -150,
        "dest-start": "0xF500"
        }
]
gzagatti commented 10 months ago

Thanks for sharing this. This patch allowed me to display all the icons alongside my preferred font. Unfortunately, the mapping from unicode to icon names is off. About half of the icons have a wrong name. Did you have the same issue?