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