2022-10-06 09:02:14 +02:00
#!/usr/bin/env python3
2018-02-13 04:23:43 +02:00
# coding=utf8
2023-11-26 19:49:56 +02:00
# Nerd Fonts Version: 3.1.1
2024-03-17 15:21:15 +02:00
# Script Version: 1.2.0
2018-02-13 04:23:43 +02:00
# Example Usage:
2022-10-06 09:02:14 +02:00
# ./generate-glyph-info-from-set.py --font ../../src/glyphs/materialdesignicons-webfont.ttf --start f001 --end f847 --offset 4ff --prefix mdi
# ./generate-glyph-info-from-set.py --font ../../src/glyphs/materialdesign/*.ttf --start f0001 --end f1af0 --offset 0 --prefix md
2018-03-21 03:32:36 +02:00
# ./generate-glyph-info-from-set.py --font ../../src/glyphs/weathericons-regular-webfont.ttf --start f000 --end f0eb --negoffset d00 --prefix weather --nogaps
2018-02-13 04:23:43 +02:00
from __future__ import absolute_import , print_function , unicode_literals
2023-11-26 19:49:56 +02:00
version = " 3.1.1 "
2018-02-13 04:23:43 +02:00
projectName = " Nerd Fonts "
projectNameAbbreviation = " NF "
projectNameSingular = projectName [ : - 1 ]
import sys
try :
import psMat
except ImportError :
sys . exit ( projectName + " : FontForge module is probably not installed. [See: http://designwithfontforge.com/en-US/Installing_Fontforge.html] " )
import re
import os
import argparse
from argparse import RawTextHelpFormatter
import errno
import subprocess
try :
#Load the module
import fontforge
except ImportError :
sys . exit ( projectName + " : FontForge module could not be loaded. Try installing fontforge python bindings [e.g. on Linux Debian or Ubuntu: `sudo apt install fontforge python-fontforge`] " )
2022-10-17 11:33:40 +02:00
parser = argparse . ArgumentParser ( description = ' Nerd Fonts Glyph Info Generator: displays code point and glyph names from given set \n \n * Website: https://www.nerdfonts.com \n * Version: ' + version + ' \n * Development Website: https://github.com/ryanoasis/nerd-fonts \n * Changelog: https://github.com/ryanoasis/nerd-fonts/blob/-/changelog.md ' , formatter_class = RawTextHelpFormatter )
2018-02-13 04:23:43 +02:00
parser . add_argument ( ' -start ' , ' --start ' , type = str , nargs = ' ? ' , dest = ' symbolFontStart ' , help = ' The starting unicode hex codepoint ' )
parser . add_argument ( ' -end ' , ' --end ' , type = str , nargs = ' ? ' , dest = ' symbolFontEnd ' , help = ' The ending unicode hex codepoint ' )
parser . add_argument ( ' -offset ' , ' --offset ' , type = str , nargs = ' ? ' , dest = ' symbolOffset ' , help = ' The amount (in hex) to offset the range by for the source font ' )
2018-03-20 03:38:30 +02:00
parser . add_argument ( ' -negoffset ' , ' --negoffset ' , type = str , nargs = ' ? ' , dest = ' negSymbolOffset ' , help = ' The amount (in hex) to negative offset the range by for the source font ' )
2018-02-15 04:28:26 +02:00
parser . add_argument ( ' -prefix ' , ' --prefix ' , type = str , nargs = ' ? ' , dest = ' prefix ' , help = ' The prefix to use for the shell variables and css names ' )
2018-03-21 03:32:36 +02:00
parser . add_argument ( ' -nogaps ' , ' --nogaps ' , action = ' store_true ' , dest = ' nogaps ' , help = ' Generates the codepoints in a continous codepoints with the given range (no empties/gaps) ' )
2018-02-13 04:23:43 +02:00
parser . add_argument ( ' -font ' , ' --font ' , type = str , nargs = ' ? ' , dest = ' filepath ' , help = ' The file path to the font file to open ' )
args = parser . parse_args ( )
print ( args . symbolFontStart , args . symbolFontEnd )
symbolFont = fontforge . open ( args . filepath )
args . symbolFontStart = int ( " 0x " + args . symbolFontStart , 16 )
args . symbolFontEnd = int ( " 0x " + args . symbolFontEnd , 16 )
2018-02-15 04:28:26 +02:00
ctr = 0
2018-02-13 04:23:43 +02:00
2018-03-20 03:38:30 +02:00
if args . negSymbolOffset :
args . negSymbolOffset = int ( " 0x " + args . negSymbolOffset , 16 )
sign = ' - '
offset = args . negSymbolOffset
elif args . symbolOffset :
args . symbolOffset = int ( " 0x " + args . symbolOffset , 16 )
sign = ' '
offset = args . symbolOffset
2018-03-21 03:32:36 +02:00
signedOffset = int ( sign + ' 0x ' + format ( offset , ' X ' ) , 16 )
hexPosition = args . symbolFontStart + signedOffset
2024-03-17 15:21:15 +02:00
allNames = { }
suppressedEntries = [ ]
symbolFont . encoding = ' UnicodeFull '
for index in range ( args . symbolFontStart , args . symbolFontEnd + 1 ) :
if not index in symbolFont :
continue
sym_glyph = symbolFont [ index ]
2018-02-13 04:23:43 +02:00
slot = format ( sym_glyph . unicode , ' X ' )
name = sym_glyph . glyphname
2018-02-15 04:28:26 +02:00
sh_name = " i_ " + args . prefix + " _ " + name . replace ( " - " , " _ " )
2018-02-13 04:23:43 +02:00
2018-03-21 03:32:36 +02:00
if args . nogaps :
2022-10-06 09:02:14 +02:00
char = chr ( hexPosition )
2018-03-21 03:32:36 +02:00
else :
2022-10-06 09:02:14 +02:00
char = chr ( int ( ' 0x ' + slot , 16 ) + signedOffset )
2018-03-21 03:32:36 +02:00
2024-03-17 15:21:15 +02:00
entryString = " i= ' " + char + " ' " + sh_name + " =$i SLOT " + slot + ' ' + str ( index )
if name not in allNames :
print ( entryString )
else :
suppressedEntries . append ( entryString )
2018-02-15 04:28:26 +02:00
ctr + = 1
2018-03-21 03:32:36 +02:00
hexPosition + = 1
2024-03-17 15:21:15 +02:00
allNames [ name ] = 1
2018-02-13 04:23:43 +02:00
2018-02-15 04:28:26 +02:00
print ( " Done, generated " + str ( ctr ) + " glyphs " )
2018-02-13 04:23:43 +02:00
2024-03-17 15:21:15 +02:00
if len ( suppressedEntries ) > 0 :
print ( ' FOLLOGING ENTRIES SUPPRESSED to prevent double names with different codepoints: ' )
print ( ' \n ' . join ( suppressedEntries ) )