Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 1 | { |
| 2 | lib, |
| 3 | fetchFromGitHub, |
| 4 | stdenv, |
| 5 | fontforge, |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 6 | writeText, |
| 7 | useNerdfontsOffset ? true, |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 8 | }: let |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 9 | pname = "octicons"; |
| 10 | version = "4.4.0"; |
| 11 | src = fetchFromGitHub { |
| 12 | owner = "primer"; |
| 13 | repo = "octicons"; |
| 14 | rev = "v${version}"; |
| 15 | sha256 = "sha256-xhJRe5mO2YkqQggqZ1JHBYsJKQUSJA08crDvl22GSGg="; |
| 16 | }; |
| 17 | |
| 18 | script = writeText "octicons.py" '' |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 19 | import fontforge |
| 20 | import json |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 21 | import itertools |
| 22 | |
| 23 | |
| 24 | if ${ |
| 25 | if useNerdfontsOffset |
| 26 | then "True" |
| 27 | else "False" |
| 28 | }: |
| 29 | validRange = list(itertools.chain((0x2665, 0x26a1), range(0xF400, 0xF4A8), (0xF112, 0xF67C))) |
| 30 | def codepointTransform(_): |
| 31 | return validRange.pop(0) |
| 32 | else: |
| 33 | codepointTransform = lambda cp: cp |
| 34 | |
| 35 | offset = ${ |
| 36 | if useNerdfontsOffset |
| 37 | then "0x0400" |
| 38 | else "0" |
| 39 | } |
| 40 | |
| 41 | with open("${src}/lib/font/codepoints.json") as f: |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 42 | codepoints = json.load(f) |
| 43 | |
| 44 | font = fontforge.font() |
| 45 | |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 46 | for name, codepoint in sorted(codepoints.items(), key=lambda item: item[1]): |
| 47 | transformed = codepointTransform(codepoint) |
| 48 | print(name, hex(transformed)) |
| 49 | glyph = font.createChar(transformed) |
| 50 | glyph.importOutlines(f"${src}/lib/svg/{name}.svg") |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 51 | |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 52 | font.familyname = "${pname}" |
| 53 | font.fullname = "${pname}" |
| 54 | font.fontname = "${pname}" |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 55 | |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 56 | font.generate("${pname}.ttf") |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 57 | ''; |
| 58 | in |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 59 | stdenv.mkDerivation rec { |
| 60 | inherit version src pname; |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 61 | |
| 62 | buildPhase = '' |
| 63 | fontforge \ |
| 64 | -lang py \ |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 65 | -script ${script} \; |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 66 | ''; |
| 67 | |
| 68 | installPhase = '' |
Skyler Grey | 0c7d092 | 2022-10-30 22:59:22 +0000 | [diff] [blame] | 69 | mkdir -p $out/bin $out/share/fonts/truetype |
| 70 | mv ${pname}.ttf $out/share/fonts/truetype |
| 71 | echo $out |
Skyler Grey | 95c67e0 | 2022-10-30 22:28:42 +0000 | [diff] [blame] | 72 | ''; |
| 73 | |
| 74 | nativeBuildInputs = [fontforge]; |
| 75 | |
| 76 | meta = with lib; { |
| 77 | description = "GitHub's Octicons icon pack"; |
| 78 | homepage = "https://github.com/primer/octicons"; |
| 79 | license = licenses.mit; |
| 80 | maintainers = with maintainers; [minion3665]; |
| 81 | }; |
| 82 | } |