2022-10-06 09:02:14 +02:00
#!/usr/bin/env python3
2018-02-13 04:23:43 +02:00
# coding=utf8
2024-04-04 09:53:24 +02:00
# Nerd Fonts Version: 3.2.0
2024-04-03 21:09:59 +02:00
# Script Version: 1.3.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
2024-04-04 09:53:24 +02:00
version = " 3.2.0 "
2018-02-13 04:23:43 +02:00
import sys
import re
import os
import argparse
from argparse import RawTextHelpFormatter
import errno
import subprocess
2024-04-03 21:09:59 +02:00
import fontforge
2018-02-13 04:23:43 +02:00
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 )
2024-04-03 21:09:59 +02:00
args . symbolFontStart = int ( args . symbolFontStart , 16 )
args . symbolFontEnd = int ( 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 :
2024-04-03 21:09:59 +02:00
args . negSymbolOffset = int ( args . negSymbolOffset , 16 )
2018-03-20 03:38:30 +02:00
sign = ' - '
offset = args . negSymbolOffset
elif args . symbolOffset :
2024-04-03 21:09:59 +02:00
args . symbolOffset = int ( args . symbolOffset , 16 )
2018-03-20 03:38:30 +02:00
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-04-03 21:09:59 +02:00
allNames = set ( )
2024-03-17 15:21:15 +02:00
suppressedEntries = [ ]
symbolFont . encoding = ' UnicodeFull '
for index in range ( args . symbolFontStart , args . symbolFontEnd + 1 ) :
if not index in symbolFont :
continue
sym_glyph = symbolFont [ index ]
2024-04-03 21:09:59 +02:00
code = sym_glyph . unicode
2018-02-13 04:23:43 +02:00
name = sym_glyph . glyphname
2024-04-03 21:09:59 +02:00
sh_name = ' i_ {} _ {} ' . format ( 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 :
2024-04-03 21:09:59 +02:00
char = chr ( code + signedOffset )
entryString = ' i= \' {} \' {} =$i ' . format ( char , sh_name )
2018-03-21 03:32:36 +02:00
2024-04-03 21:09:59 +02:00
if index != code :
suppressedEntries . append ( entryString + ' (not main) ' )
elif name not in allNames :
2024-03-17 15:21:15 +02:00
print ( entryString )
else :
2024-04-03 21:09:59 +02:00
suppressedEntries . append ( entryString + ' (double) ' )
2024-03-17 15:21:15 +02:00
2018-02-15 04:28:26 +02:00
ctr + = 1
2018-03-21 03:32:36 +02:00
hexPosition + = 1
2024-04-03 21:09:59 +02:00
allNames . add ( name )
2018-02-13 04:23:43 +02:00
2024-04-03 21:09:59 +02:00
print ( ' Done, generated {} glyphs ' . format ( ctr ) )
2018-02-13 04:23:43 +02:00
2024-03-17 15:21:15 +02:00
if len ( suppressedEntries ) > 0 :
2024-04-03 21:09:59 +02:00
print ( ' FOLLOGING ENTRIES SUPPRESSED to prevent double names: ' )
2024-03-17 15:21:15 +02:00
print ( ' \n ' . join ( suppressedEntries ) )