Skip to content

Commit

Permalink
Merge pull request #764 from ryanoasis/bugfix/Make-NerdFonts-Monospac…
Browse files Browse the repository at this point in the history
…ed-Again

font-patcher: Make Nerd Fonts Monospaced Again
  • Loading branch information
Finii authored Sep 6, 2022
2 parents 30605d4 + f67de4d commit 6564e71
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 40 deletions.
64 changes: 38 additions & 26 deletions font-patcher
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import absolute_import, print_function, unicode_literals

# Change the script version when you edit this script:
script_version = "3.0.3"
script_version = "3.0.4"

version = "2.2.1"
projectName = "Nerd Fonts"
Expand Down Expand Up @@ -320,6 +320,7 @@ class font_patcher:
parser.add_argument('-out', '--outputdir', dest='outputdir', default=".", type=str, nargs='?', help='The directory to output the patched font file to')
parser.add_argument('--glyphdir', dest='glyphdir', default=__dir__ + "/src/glyphs/", type=str, nargs='?', help='Path to glyphs to be used for patching')
parser.add_argument('--makegroups', dest='makegroups', default=False, action='store_true', help='Use alternative method to name patched fonts (experimental)')
parser.add_argument('--variable-width-glyphs', dest='nonmono', default=False, action='store_true', help='Do not adjust advance width (no "overhang")')

# progress bar arguments - https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
progressbars_group_parser = parser.add_mutually_exclusive_group(required=False)
Expand Down Expand Up @@ -380,6 +381,10 @@ class font_patcher:
if self.args.alsowindows:
self.args.windows = False

if self.args.nonmono and self.args.single:
print("Warniung: Specified contradicting --variable-width-glyphs and --use-single-width-glyph. Ignoring --variable-width-glyphs.")
self.args.nonmono = False

# this one also works but it needs to be updated every time a font is added
# it was a conditional in self.setup_font_names() before, but it was missing
# a symbol font, so it would name the font complete without being so sometimes.
Expand Down Expand Up @@ -673,7 +678,7 @@ class font_patcher:
# Supported params: overlap | careful
# Powerline dividers
SYM_ATTR_POWERLINE = {
'default': {'align': 'c', 'valign': 'c', 'stretch': 'pa', 'params': ''},
'default': {'align': 'c', 'valign': 'c', 'stretch': 'pa', 'params': {}},

# Arrow tips
0xe0b0: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {'overlap': 0.02}},
Expand Down Expand Up @@ -706,23 +711,23 @@ class font_patcher:
0xe0c3: {'align': 'r', 'valign': 'c', 'stretch': 'xy', 'params': {'overlap': 0.01}},

# Small squares
0xe0c4: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0c5: {'align': 'r', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0c4: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {}},
0xe0c5: {'align': 'r', 'valign': 'c', 'stretch': 'xy', 'params': {}},

# Bigger squares
0xe0c6: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0c7: {'align': 'r', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0c6: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {}},
0xe0c7: {'align': 'r', 'valign': 'c', 'stretch': 'xy', 'params': {}},

# Waveform
0xe0c8: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {'overlap': 0.01}},

# Hexagons
0xe0cc: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0cd: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0cc: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {}},
0xe0cd: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {}},

# Legos
0xe0ce: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0cf: {'align': 'c', 'valign': 'c', 'stretch': 'xy', 'params': ''},
0xe0ce: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {}},
0xe0cf: {'align': 'c', 'valign': 'c', 'stretch': 'xy', 'params': {}},
0xe0d1: {'align': 'l', 'valign': 'c', 'stretch': 'xy', 'params': {'overlap': 0.02}},

# Top and bottom trapezoid
Expand All @@ -732,22 +737,22 @@ class font_patcher:

SYM_ATTR_DEFAULT = {
# 'pa' == preserve aspect ratio
'default': {'align': 'c', 'valign': 'c', 'stretch': 'pa', 'params': ''}
'default': {'align': 'c', 'valign': 'c', 'stretch': 'pa', 'params': {}}
}

SYM_ATTR_FONTA = {
# 'pa' == preserve aspect ratio
'default': {'align': 'c', 'valign': 'c', 'stretch': 'pa', 'params': ''},
'default': {'align': 'c', 'valign': 'c', 'stretch': 'pa', 'params': {}},

# Don't center these arrows vertically
0xf0dc: {'align': 'c', 'valign': '', 'stretch': 'pa', 'params': ''},
0xf0dd: {'align': 'c', 'valign': '', 'stretch': 'pa', 'params': ''},
0xf0de: {'align': 'c', 'valign': '', 'stretch': 'pa', 'params': ''}
0xf0dc: {'align': 'c', 'valign': '', 'stretch': 'pa', 'params': {}},
0xf0dd: {'align': 'c', 'valign': '', 'stretch': 'pa', 'params': {}},
0xf0de: {'align': 'c', 'valign': '', 'stretch': 'pa', 'params': {}}
}

CUSTOM_ATTR = {
# 'pa' == preserve aspect ratio
'default': {'align': 'c', 'valign': '', 'stretch': '', 'params': ''}
'default': {'align': 'c', 'valign': '', 'stretch': '', 'params': {}}
}

# Most glyphs we want to maximize during the scale. However, there are some
Expand Down Expand Up @@ -1012,13 +1017,10 @@ class font_patcher:
# Currently stretching vertically for both monospace and double-width
scale_ratio_y = self.font_dim['height'] / sym_dim['height']

if 'overlap' in sym_attr['params']:
overlap = sym_attr['params']['overlap']
else:
overlap = 0
overlap = sym_attr['params'].get('overlap')

if scale_ratio_x != 1 or scale_ratio_y != 1:
if overlap != 0:
if overlap:
scale_ratio_x *= 1 + overlap
scale_ratio_y *= 1 + overlap
self.sourceFont[currentSourceFontGlyph].transform(psMat.scale(scale_ratio_x, scale_ratio_y))
Expand All @@ -1044,7 +1046,7 @@ class font_patcher:
# Right align
x_align_distance += self.font_dim['width'] - sym_dim['width']

if overlap != 0:
if overlap:
overlap_width = self.font_dim['width'] * overlap
if sym_attr['align'] == 'l':
x_align_distance -= overlap_width
Expand All @@ -1054,20 +1056,26 @@ class font_patcher:
align_matrix = psMat.translate(x_align_distance, y_align_distance)
self.sourceFont[currentSourceFontGlyph].transform(align_matrix)

# Ensure after horizontal adjustments and centering that the glyph
# does not overlap the bearings (edges)
if not overlap:
self.remove_glyph_neg_bearings(self.sourceFont[currentSourceFontGlyph])

# Needed for setting 'advance width' on each glyph so they do not overlap,
# also ensures the font is considered monospaced on Windows by setting the
# same width for all character glyphs. This needs to be done for all glyphs,
# even the ones that are empty and didn't go through the scaling operations.
# It should come after setting the glyph bearings
self.set_glyph_width_mono(self.sourceFont[currentSourceFontGlyph])

# Ensure after horizontal adjustments and centering that the glyph
# does not overlap the bearings (edges)
self.remove_glyph_neg_bearings(self.sourceFont[currentSourceFontGlyph])
# Re-remove negative bearings for target font with variable advance width
if self.args.nonmono:
self.remove_glyph_neg_bearings(self.sourceFont[currentSourceFontGlyph])

# Check if the inserted glyph is scaled correctly for monospace
if self.args.single:
(xmin, _, xmax, _) = self.sourceFont[currentSourceFontGlyph].boundingBox()
if int(xmax - xmin) > self.font_dim['width'] * (1 + overlap):
if int(xmax - xmin) > self.font_dim['width'] * (1 + (overlap or 0)):
print("\n Warning: Scaled glyph U+{:X} wider than one monospace width ({} / {} (overlap {}))".format(
currentSourceFontGlyph, int(xmax - xmin), self.font_dim['width'], overlap))

Expand Down Expand Up @@ -1114,6 +1122,10 @@ class font_patcher:
self.font_dim.width is set with self.get_sourcefont_dimensions().
"""
try:
# Fontforge handles the width change like this:
# - Keep existing left_side_bearing
# - Set width
# - Calculate and set new right_side_bearing
glyph.width = self.font_dim['width']
except:
pass
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ Full options:
usage: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -416,6 +416,8 @@ options:
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars Show percentage completion progress bars per Glyph Set
--no-progressbars Don't show percentage completion progress bars per Glyph Set
--also-windows Create two fonts, the normal and the --windows version
Expand Down
4 changes: 3 additions & 1 deletion readme_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ The list is not complete, but you can [search for a complete list here](https://
usage: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -427,6 +427,8 @@ optional arguments:
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars 显示每个Glyph Set的完成度进度条
--no-progressbars 不显示每个Glyph Set的完成度进度条
--also-windows Create two fonts, the normal and the --windows version
Expand Down
4 changes: 3 additions & 1 deletion readme_es.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ Parcha la fuente de tu preferencia para usar los [VimDevIcons ➶][vim-devicons]
uso: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -375,6 +375,8 @@ argumentos opcionales:
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars Muestra barras de progreso con porcentajes de completitud por cada Conjunto de Glifos
--no-progressbars No muestra barras de progreso con porcentajes de completitud por cada Conjunto de Glifos
--also-windows Create two fonts, the normal and the --windows version
Expand Down
4 changes: 3 additions & 1 deletion readme_fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ Générer la police de votre choix pour l'utiliser avec [VimDevIcons ➶][vim-de
usage: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -453,6 +453,8 @@ options:
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars Show percentage completion progress bars per Glyph Set
--no-progressbars Don't show percentage completion progress bars per Glyph Set
--also-windows Create two fonts, the normal and the --windows version
Expand Down
4 changes: 3 additions & 1 deletion readme_hi.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ The list is not complete, but you can [search for a complete list here](https://
प्रयोग: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -408,6 +408,8 @@ The list is not complete, but you can [search for a complete list here](https://
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars प्रति ग्लिफ़ सेट प्रतिशत पूर्णता प्रगति बार दिखाएं
--no-progressbars प्रति ग्लिफ़ सेट प्रतिशत पूर्णता प्रगति बार न दिखाएं
--also-windows Create two fonts, the normal and the --windows version
Expand Down
4 changes: 3 additions & 1 deletion readme_it.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ Modificare il font di tua scelta per utilizzare i [VimDevIcons ➶][vim-devicons
usage: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -376,6 +376,8 @@ options:
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars Show percentage completion progress bars per Glyph Set
--no-progressbars Don't show percentage completion progress bars per Glyph Set
--also-windows Create two fonts, the normal and the --windows version
Expand Down
4 changes: 3 additions & 1 deletion readme_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ The list is not complete, but you can [search for a complete list here](https://
usage: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -371,6 +371,8 @@ optional arguments:
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars グリフセットごとに進捗を百分率で表示します。
--no-progressbars グリフセットごとの進捗を表示しません。
--also-windows Create two fonts, the normal and the --windows version
Expand Down
4 changes: 3 additions & 1 deletion readme_ko.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ The list is not complete, but you can [search for a complete list here](https://
usage: font-patcher [-h] [-v] [-s] [-l] [-q] [-w] [-c] [--careful] [--removeligs]
[--postprocess [POSTPROCESS]] [--configfile [CONFIGFILE]]
[--custom [CUSTOM]] [-ext [EXTENSION]] [-out [OUTPUTDIR]]
[--glyphdir [GLYPHDIR]] [--makegroups]
[--glyphdir [GLYPHDIR]] [--makegroups] [--variable-width-glyphs]
[--progressbars | --no-progressbars] [--also-windows]
[--fontawesome] [--fontawesomeextension] [--fontlinux]
[--octicons] [--codicons] [--powersymbols] [--pomicons]
Expand Down Expand Up @@ -372,6 +372,8 @@ options:
--glyphdir [GLYPHDIR]
Path to glyphs to be used for patching
--makegroups Use alternative method to name patched fonts (experimental)
--variable-width-glyphs
Do not adjust advance width (no "overhang")
--progressbars Show percentage completion progress bars per Glyph Set
--no-progressbars Don't show percentage completion progress bars per Glyph Set
--also-windows Create two fonts, the normal and the --windows version
Expand Down
Loading

0 comments on commit 6564e71

Please sign in to comment.