Subject: [PATCH v3 1/3] gnu: Add nerd-font-patcher

From: Saku Laesvuori <[email-masked]>
Date: Thu, 14 Dec 2023 16:01:12 +0200

* gnu/packages/python-xyz.scm (nerd-font-patcher): New variable.
* gnu/packages/patches/nerd-font-patcher-convert-to-python-package.patch:
New file

Change-Id: I5f0b99b5120dfa8835727f4ea9f01c235962eed9
---
 ...nt-patcher-convert-to-python-package.patch | 85 +++++++++++++++++++
 gnu/packages/python-xyz.scm                   | 47 ++++++++++
 2 files changed, 132 insertions(+)
 create mode 100644 gnu/packages/patches/nerd-font-patcher-convert-to-python-package.patch

diff --git a/gnu/packages/patches/nerd-font-patcher-convert-to-python-package.patch b/gnu/packages/patches/nerd-font-patcher-convert-to-python-package.patch
new file mode 100644
index 0000000000..400c608f88
--- /dev/null
+++ b/gnu/packages/patches/nerd-font-patcher-convert-to-python-package.patch
@@ -0,0 +1,85 @@
+This patch
+
+* Creates a pyproject.toml file
+* Changes paths to be more sensible.
+
+It requires extra work in a snippet to work properly, because moving the
+files in a patch would effectively duplicate all the code.
+
+ font-patcher                               | 13 ++++---------
+ bin/scripts/name_parser/FontnameParser.py |  2 +-
+ pyproject.toml                             | 13 +++++++++++++
+ 3 files changed, 18 insertions(+), 10 deletions(-)
+ create mode 100644 pyproject.toml
+
+diff --git a/font-patcher b/font-patcher
+index 4cbf46c..89ae402 100755
+--- a/font-patcher
++++ b/font-patcher
+@@ -38,13 +38,9 @@ except ImportError:
+         )
+     )
+ 
+-sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])) + '/bin/scripts/name_parser/')
+-try:
+-    from FontnameParser import FontnameParser
+-    from FontnameTools import FontnameTools
+-    FontnameParserOK = True
+-except ImportError:
+-    FontnameParserOK = False
++from nerd_font_patcher.name_parser.FontnameParser import FontnameParser
++from nerd_font_patcher.name_parser.FontnameTools import FontnameTools
++FontnameParserOK = True
+ 
+ class TableHEADWriter:
+     """ Access to the HEAD table without external dependencies """
+@@ -1897,7 +1893,7 @@ def setup_arguments():
+     expert_group.add_argument('--custom',                                  dest='custom',           default=False, type=str,            help='Specify a custom symbol font, all glyphs will be copied; absolute path suggested')
+ 
+     expert_group.add_argument('--dry',                                     dest='dry_run',          default=False, action='store_true', help='Do neither patch nor store the font, to check naming')
+-    expert_group.add_argument('--glyphdir',                                dest='glyphdir',         default=__dir__ + "/src/glyphs/", type=str, help='Path to glyphs to be used for patching')
++    expert_group.add_argument('--glyphdir',                                dest='glyphdir',         default=__dir__ + "/share/fonts/nerd-font-glyphs", type=str, help='Path to glyphs to be used for patching')
+     expert_group.add_argument('--has-no-italic',                           dest='noitalic',         default=False, action='store_true', help='Font family does not have Italic (but Oblique), to help create correct RIBBI set')
+     expert_group.add_argument('-l', '--adjust-line-height',                dest='adjustLineHeight', default=False, action='store_true', help='Whether to adjust line heights (attempt to center powerline separators more evenly)')
+     expert_group.add_argument('--metrics',                                 dest='metrics',          default=None, choices=get_metrics_names(), help='Select vertical metrics source (for problematic cases)')
+@@ -2068,5 +2064,4 @@ def main():
+ 
+ 
+ if __name__ == "__main__":
+-    __dir__ = os.path.dirname(os.path.abspath(__file__))
+     main()
+diff --git a/bin/scripts/name_parser/FontnameParser.py b/bin/scripts/name_parser/FontnameParser.py
+index 5768c42..1d95fdc 100644
+--- a/bin/scripts/name_parser/FontnameParser.py
++++ b/bin/scripts/name_parser/FontnameParser.py
+@@ -2,7 +2,7 @@
+ # coding=utf8
+ 
+ import re
+-from FontnameTools import FontnameTools
++from nerd_font_patcher.name_parser.FontnameTools import FontnameTools
+ 
+ class FontnameParser:
+     """Parse a font name and generate all kinds of names"""
+diff --git a/pyproject.toml b/pyproject.toml
+new file mode 100644
+index 0000000..e039e68
+--- /dev/null
++++ b/pyproject.toml
+@@ -0,0 +1,13 @@
++[build-system]
++requires = ["setuptools"]
++build-backend = "setuptools.build_meta"
++
++[project]
++name = "font_patcher"
++version = "3.1.1"
++
++[project.scripts]
++font-patcher = "nerd_font_patcher:main"
++
++[tool.setuptools]
++packages = ["nerd_font_patcher", "nerd_font_patcher.name_parser"]
+-- 
+2.41.0
+
diff --git a/gnu/packages/python-xyz.scm b/gnu/packages/python-xyz.scm
index 86f8d81689..320fd4cb69 100644
--- a/gnu/packages/python-xyz.scm
+++ b/gnu/packages/python-xyz.scm
@@ -146,6 +146,7 @@
 ;;; Copyright © 2023 Janneke Nieuwenhuizen <[email-masked]>
 ;;; Copyright © 2023 Attila Lendvai <[email-masked]>
 ;;; Copyright © 2023 Troy Figiel <[email-masked]>
+;;; Copyright © 2023 Saku Laesvuori <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -13611,6 +13612,52 @@ (define-public python-fonttools
              python-zopfli))
       (properties (alist-delete 'hidden? (package-properties base))))))
 
+(define-public nerd-font-patcher
+  (package
+    (name "nerd-font-patcher")
+    (version "3.1.1")
+    (source
+     (origin
+       (method url-fetch/zipbomb)
+       (uri
+        (string-append
+         "https://github.com/ryanoasis/nerd-fonts/releases/download/v"
+         version "/FontPatcher.zip"))
+       (sha256
+        (base32 "1wp41ljxa5sjgxhw97cm06icw9n03nmpygr3j9qq3zs5wsxvs4bv"))
+       (snippet #~(begin
+                    (rename-file "bin/scripts" "nerd_font_patcher")
+                    (rename-file "font-patcher" "nerd_font_patcher/__init__.py")))
+       (patches
+        (search-patches "nerd-font-patcher-convert-to-python-package.patch"))))
+    (build-system pyproject-build-system)
+    (arguments
+     (list
+      #:tests? #f ;no tests available
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-before 'build 'patch-__dir__
+            (lambda _
+              (substitute* "nerd_font_patcher/__init__.py"
+                (("__dir__")
+                 (string-append "\"" #$output "\"\n")))))
+          (add-after 'install 'install-glyphs
+            (lambda _
+              (let ((font-directory (string-append #$output "/share/fonts")))
+                (mkdir-p font-directory)
+                (copy-recursively "src/glyphs" (string-append font-directory "/nerd-font-glyphs"))))))))
+    (inputs (list python fontforge))
+    (native-inputs (list python-setuptools))
+    (home-page "https://github.com/ryanoasis/nerd-fonts/#option-9-patch-your-own-font")
+    (synopsis "Script for adding Nerd Fonts glyphs into fonts")
+    (description "This package provides a python script that can be used to add the
+Nerd Fonts glyphs to any font file.")
+    (license (list license:asl2.0
+                   license:cc-by4.0
+                   license:expat
+                   license:silofl1.1
+                   license:unlicense))))
+
 (define-public python-ly
   (package
     (name "python-ly")

base-commit: 4bf4e340b74b66a0fc319aeb100fb1f948a67211
-- 
2.41.0