|
| 1 | +{ pkgs ? import <nixpkgs> { } |
| 2 | +, stdenv ? pkgs.stdenv |
| 3 | +, lib ? pkgs.lib |
| 4 | +, ghostscript ? pkgs.ghostscript |
| 5 | +, ref ? null |
| 6 | +, doPdfGeneration ? true |
| 7 | + # Checks whether the generated output matches the checked-in SSS32.ps. |
| 8 | + # When doing development you may want to shut this off to obtain the |
| 9 | + # output file that you need to check in. |
| 10 | +, doOutputDiff ? true |
| 11 | +}: |
| 12 | + |
| 13 | +let |
| 14 | + src = |
| 15 | + if isNull ref |
| 16 | + then lib.sourceFilesBySuffices ./. [ ".ps" ".inc" ] |
| 17 | + else |
| 18 | + fetchGit { |
| 19 | + url = ./.; |
| 20 | + inherit ref; |
| 21 | + }; |
| 22 | + shortId = lib.optionalString (! isNull ref) ("-" + builtins.substring 0 8 src.rev); |
| 23 | + |
| 24 | + |
| 25 | + setup = rec { |
| 26 | + # Will be broken into multiple sub-files in a later PR. |
| 27 | + fullSetup = { |
| 28 | + content = builtins.readFile "${src}/include/setup.ps.inc"; |
| 29 | + dependencies = [ ]; |
| 30 | + }; |
| 31 | + }; |
| 32 | + # Dependencies that every page has |
| 33 | + standardDependencies = [ setup.fullSetup ]; |
| 34 | + |
| 35 | + allPages = { |
| 36 | + title = { |
| 37 | + sourceHeader = "Title Page"; |
| 38 | + content = builtins.readFile "${src}/include/title.ps.inc"; |
| 39 | + dependencies = [ ]; |
| 40 | + }; |
| 41 | + license = { |
| 42 | + sourceHeader = "License Information"; |
| 43 | + drawPageContent = true; |
| 44 | + content = builtins.readFile "${src}/include/license.ps.inc"; |
| 45 | + dependencies = [ ]; |
| 46 | + }; |
| 47 | + reference = { |
| 48 | + sourceHeader = "Reference Sheet"; |
| 49 | + drawPageContent = true; |
| 50 | + content = builtins.readFile "${src}/include/reference.ps.inc"; |
| 51 | + dependencies = [ ]; |
| 52 | + }; |
| 53 | + principalTables = { |
| 54 | + sourceHeader = "Arithmetic Tables"; |
| 55 | + content = builtins.readFile "${src}/include/principal-tables.ps.inc"; |
| 56 | + dependencies = [ ]; |
| 57 | + }; |
| 58 | + |
| 59 | + additionBottom = { |
| 60 | + content = "{xor} (Addition) code dup perm drawBottomWheelPage\n"; |
| 61 | + dependencies = [ ]; |
| 62 | + }; |
| 63 | + additionTop = { |
| 64 | + content = "showTopWheelPage\n"; |
| 65 | + dependencies = [ ]; |
| 66 | + }; |
| 67 | + recovery = { |
| 68 | + content = builtins.readFile "${src}/include/volvelle-recovery.ps.inc"; |
| 69 | + dependencies = [ ]; |
| 70 | + }; |
| 71 | + fusionInner = { |
| 72 | + content = builtins.readFile "${src}/include/volvelle-fusion-1.ps.inc"; |
| 73 | + dependencies = [ ]; |
| 74 | + }; |
| 75 | + fusionOuter = { |
| 76 | + content = builtins.readFile "${src}/include/volvelle-fusion-2.ps.inc"; |
| 77 | + dependencies = [ ]; |
| 78 | + }; |
| 79 | + |
| 80 | + generationInstructions = { |
| 81 | + content = builtins.readFile "${src}/include/page7.ps.inc"; |
| 82 | + dependencies = [ ]; |
| 83 | + }; |
| 84 | + |
| 85 | + checksumTable1 = { |
| 86 | + content = builtins.readFile "${src}/include/checksum-table-1.ps.inc"; |
| 87 | + isLandscape = true; |
| 88 | + dependencies = [ ]; |
| 89 | + }; |
| 90 | + checksumTable2 = { |
| 91 | + content = builtins.readFile "${src}/include/checksum-table-2.ps.inc"; |
| 92 | + isLandscape = true; |
| 93 | + dependencies = [ ]; |
| 94 | + }; |
| 95 | + checksumWorksheet = { |
| 96 | + content = builtins.readFile "${src}/include/checksum-worksheet.ps.inc"; |
| 97 | + isLandscape = true; |
| 98 | + dependencies = [ ]; |
| 99 | + }; |
| 100 | + |
| 101 | + shareTable = a: b: c: d: { |
| 102 | + content = "${toString a} ${toString b} ${toString c} ${toString d} showShareTablePage\n"; |
| 103 | + dependencies = [ ]; |
| 104 | + }; |
| 105 | + }; |
| 106 | + |
| 107 | + fullBooklet = { |
| 108 | + name = "SSS32.ps"; |
| 109 | + pages = with allPages; [ |
| 110 | + title |
| 111 | + license |
| 112 | + reference |
| 113 | + principalTables |
| 114 | + additionBottom |
| 115 | + additionTop |
| 116 | + generationInstructions |
| 117 | + (shareTable 29 24 13 25) |
| 118 | + (shareTable 9 8 23 18) |
| 119 | + (shareTable 22 31 27 19) |
| 120 | + (shareTable 1 0 3 16) |
| 121 | + (shareTable 11 28 12 14) |
| 122 | + (shareTable 6 4 2 15) |
| 123 | + (shareTable 10 17 21 20) |
| 124 | + (shareTable 26 30 7 5) |
| 125 | + recovery |
| 126 | + fusionInner |
| 127 | + fusionOuter |
| 128 | + checksumTable1 |
| 129 | + checksumTable2 |
| 130 | + checksumWorksheet |
| 131 | + ]; |
| 132 | + }; |
| 133 | + |
| 134 | + dependencyContentRecur = content: builtins.concatMap |
| 135 | + (item: (dependencyContentRecur item.dependencies) ++ [ item.content ]) |
| 136 | + content; |
| 137 | + dependencyContent = pages: lib.lists.unique ( |
| 138 | + (map (dep: dep.content) standardDependencies) ++ |
| 139 | + (builtins.concatMap (page: dependencyContentRecur page.dependencies) pages) |
| 140 | + ); |
| 141 | + |
| 142 | + renderBooklet = booklet: |
| 143 | + let |
| 144 | + addPage = content: pageData: { |
| 145 | + content = content.content + lib.optionalString (pageData ? sourceHeader) '' |
| 146 | + %**************************************************************** |
| 147 | + %* |
| 148 | + %* ${pageData.sourceHeader} |
| 149 | + %* |
| 150 | + %**************************************************************** |
| 151 | + '' + '' |
| 152 | + %%Page: ${toString content.nextPgIdx} ${toString content.nextPgIdx} |
| 153 | + ${lib.optionalString (pageData ? isLandscape) "%%PageOrientation: Landscape\n"}%%BeginPageSetup |
| 154 | + /pgsave save def |
| 155 | + %%EndPageSetup |
| 156 | + '' + ( |
| 157 | + if pageData ? drawPageContent |
| 158 | + then |
| 159 | + if pageData ? isLandscape |
| 160 | + then "landscapePage begin ${toString content.nextCtIdx} drawPageContent\n" |
| 161 | + else "portraitPage begin ${toString content.nextCtIdx} drawPageContent\n" |
| 162 | + else |
| 163 | + if pageData ? isLandscape |
| 164 | + then "90 rotate\n" |
| 165 | + else "" |
| 166 | + ) + '' |
| 167 | + ${pageData.content} |
| 168 | + ${lib.optionalString (pageData ? drawPageContent) "end\n"}pgsave restore |
| 169 | + showpage |
| 170 | + ''; |
| 171 | + nextCtIdx = content.nextCtIdx + (if pageData ? drawPageContent then 1 else 0); |
| 172 | + nextPgIdx = content.nextPgIdx + 1; |
| 173 | + }; |
| 174 | + initialContent = { |
| 175 | + content = '' |
| 176 | + %!PS-Adobe-3.0 |
| 177 | + %%Orientation: Portrait |
| 178 | + %%Pages: ${toString (builtins.length booklet.pages)} |
| 179 | + %%EndComments |
| 180 | + %%BeginSetup |
| 181 | + ${toString (dependencyContent (booklet.pages))}%%EndSetup |
| 182 | +
|
| 183 | + %************************************************************************ |
| 184 | + %************************************************************************ |
| 185 | + %* |
| 186 | + %* Section Three: Page Rendering |
| 187 | + %* |
| 188 | + %************************************************************************ |
| 189 | + %************************************************************************ |
| 190 | +
|
| 191 | + ''; |
| 192 | + nextPgIdx = 1; |
| 193 | + nextCtIdx = 1; |
| 194 | + }; |
| 195 | + finalContent = builtins.foldl' addPage initialContent booklet.pages; |
| 196 | + in |
| 197 | + pkgs.writeTextFile { |
| 198 | + name = booklet.name; |
| 199 | + text = finalContent.content + '' |
| 200 | + %%EOF |
| 201 | + ''; |
| 202 | + }; |
| 203 | +in |
| 204 | +stdenv.mkDerivation { |
| 205 | + name = "codex32${shortId}"; |
| 206 | + |
| 207 | + buildInputs = if doPdfGeneration then [ ghostscript ] else [ ]; |
| 208 | + |
| 209 | + phases = [ "buildPhase" ]; |
| 210 | + buildPhase = '' |
| 211 | + set -e |
| 212 | +
|
| 213 | + mkdir "$out" |
| 214 | + cd "$out" |
| 215 | + cp ${renderBooklet fullBooklet} SSS32.ps |
| 216 | +
|
| 217 | + ${lib.optionalString doOutputDiff "diff -C 5 ${src}/SSS32.ps SSS32.ps"} |
| 218 | + sed -i 's/(revision \(.*\))/(revision \1${shortId})/' ./SSS32.ps |
| 219 | + ${lib.optionalString doPdfGeneration "ps2pdf -dPDFSETTINGS=/prepress SSS32.ps"} |
| 220 | + ''; |
| 221 | +} |
| 222 | + |
0 commit comments