diff options
| author | 2026-01-25 18:18:02 -0600 | |
|---|---|---|
| committer | 2026-01-25 18:18:02 -0600 | |
| commit | 7e6ea44c1e0a80cc22ee6dfd79103db6a1877774 (patch) | |
| tree | 4f93a30f8e235c5ad11be57939e0486beb6e7e62 /tools | |
| parent | Add a charts module. (diff) | |
Redo cp437 conversion - dumped bit maps were not correct.
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/cp437.janet | 54 | ||||
| -rw-r--r-- | tools/make-font.janet | 47 |
2 files changed, 82 insertions, 19 deletions
diff --git a/tools/cp437.janet b/tools/cp437.janet new file mode 100644 index 0000000..ca2a674 --- /dev/null +++ b/tools/cp437.janet @@ -0,0 +1,54 @@ +(def unicode-to-cp437 + {0x263A 0x01 0x263B 0x02 0x2665 0x03 0x2666 0x04 + 0x2663 0x05 0x2660 0x06 0x2022 0x07 0x25D8 0x08 + 0x25CB 0x09 0x25D9 0x0A 0x2642 0x0B 0x2640 0x0C + 0x266A 0x0D 0x266B 0x0E 0x263C 0x0F 0x25BA 0x10 + 0x25C4 0x11 0x2195 0x12 0x203C 0x13 0x00B6 0x14 + 0x00A7 0x15 0x25AC 0x16 0x21A8 0x17 0x2191 0x18 + 0x2193 0x19 0x2192 0x1A 0x2190 0x1B 0x221F 0x1C + 0x2194 0x1D 0x25B2 0x1E 0x25BC 0x1F 0x2302 0x7F + 0x00C7 0x80 0x00FC 0x81 0x00E9 0x82 0x00E2 0x83 + 0x00E4 0x84 0x00E0 0x85 0x00E5 0x86 0x00E7 0x87 + 0x00EA 0x88 0x00EB 0x89 0x00E8 0x8A 0x00EF 0x8B + 0x00EE 0x8C 0x00EC 0x8D 0x00C4 0x8E 0x00C5 0x8F + 0x00C9 0x90 0x00E6 0x91 0x00C6 0x92 0x00F4 0x93 + 0x00F6 0x94 0x00F2 0x95 0x00FB 0x96 0x00F9 0x97 + 0x00FF 0x98 0x00D6 0x99 0x00DC 0x9A 0x00A2 0x9B + 0x00A3 0x9C 0x00A5 0x9D 0x20A7 0x9E 0x0192 0x9F + 0x00E1 0xA0 0x00ED 0xA1 0x00F3 0xA2 0x00FA 0xA3 + 0x00F1 0xA4 0x00D1 0xA5 0x00AA 0xA6 0x00BA 0xA7 + 0x00BF 0xA8 0x2310 0xA9 0x00AC 0xAA 0x00BD 0xAB + 0x00BC 0xAC 0x00A1 0xAD 0x00AB 0xAE 0x00BB 0xAF + 0x2591 0xB0 0x2592 0xB1 0x2593 0xB2 0x2502 0xB3 + 0x2524 0xB4 0x2561 0xB5 0x2562 0xB6 0x2556 0xB7 + 0x2555 0xB8 0x2563 0xB9 0x2551 0xBA 0x2557 0xBB + 0x255D 0xBC 0x255C 0xBD 0x255B 0xBE 0x2510 0xBF + 0x2514 0xC0 0x2534 0xC1 0x252C 0xC2 0x251C 0xC3 + 0x2500 0xC4 0x253C 0xC5 0x255E 0xC6 0x255F 0xC7 + 0x255A 0xC8 0x2554 0xC9 0x2569 0xCA 0x2566 0xCB + 0x2560 0xCC 0x2550 0xCD 0x256C 0xCE 0x2567 0xCF + 0x2568 0xD0 0x2564 0xD1 0x2565 0xD2 0x2559 0xD3 + 0x2558 0xD4 0x2552 0xD5 0x2553 0xD6 0x256B 0xD7 + 0x256A 0xD8 0x2518 0xD9 0x250C 0xDA 0x2588 0xDB + 0x2584 0xDC 0x258C 0xDD 0x2590 0xDE 0x2580 0xDF + 0x03B1 0xE0 0x00DF 0xE1 0x0393 0xE2 0x03C0 0xE3 + 0x03A3 0xE4 0x03C3 0xE5 0x00B5 0xE6 0x03C4 0xE7 + 0x03A6 0xE8 0x0398 0xE9 0x03A9 0xEA 0x03B4 0xEB + 0x221E 0xEC 0x03C6 0xED 0x03B5 0xEE 0x2229 0xEF + 0x2261 0xF0 0x00B1 0xF1 0x2265 0xF2 0x2264 0xF3 + 0x2320 0xF4 0x2321 0xF5 0x00F7 0xF6 0x2248 0xF7 + 0x00B0 0xF8 0x2219 0xF9 0x00B7 0xFA 0x221A 0xFB + 0x207F 0xFC 0x00B2 0xFD 0x25A0 0xFE 0x00A0 0xFF + }) + +(def cp437-to-unicode (invert unicode-to-cp437)) + +(defn convert-cp437-to-unicode + [codepoint] + (if (and (>= codepoint 20) (< codepoint 0x7F)) # ascii + codepoint + (get cp437-to-unicode codepoint 0x20))) # default to space + +# Make the table +(with [f (file/open "out.jdn" :wb)] + (xprintf f "%j" (seq [i :range [0 0xFF]] (convert-cp437-to-unicode i)))) diff --git a/tools/make-font.janet b/tools/make-font.janet index bcd7905..a5e1dc9 100644 --- a/tools/make-font.janet +++ b/tools/make-font.janet @@ -8,6 +8,11 @@ ### ourselves. ### +(def cp437-to-unicode + @[32 9786 9787 9829 9830 9827 9824 8226 9688 9675 9689 9794 9792 9834 9835 9788 9658 9668 8597 8252 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 8962 199 252 233 226 228 224 229 231 234 235 232 239 238 236 196 197 201 230 198 244 246 242 251 249 255 214 220 162 163 165 8359 402 225 237 243 250 241 209 170 186 191 8976 172 189 188 161 171 187 9617 9618 9619 9474 9508 9569 9570 9558 9557 9571 9553 9559 9565 9564 9563 9488 9492 9524 9516 9500 9472 9532 9566 9567 9562 9556 9577 9574 9568 9552 9580 9575 9576 9572 9573 9561 9560 9554 9555 9579 9578 9496 9484 9608 9604 9612 9616 9600 945 223 915 960 931 963 181 964 934 920 937 948 8734 966 949 8745 8801 177 8805 8804 8992 8993 247 8776 176 8729 183 8730 8319 178 9632]) + +(eprint (length cp437-to-unicode)) + (begin-jit :module-name "make-font" :cflags @["-Ideps/stb" "-Ideps"]) @@ -25,25 +30,27 @@ (include "stb_image_write.h") (include <string.h>) +(declare (cp437-lookup (array int)) :static :const (array ,;cp437-to-unicode)) + (cfunction load-font "Load a font from disk and create compressed font" [header-dest:cstring cname:cstring path:cstring gw:int gh:int] -> int (def fnt:stbtt-fontinfo) - (def font-buffer (malloc (<< 1 24))) - (def f (fopen path "rb")) + (def font-buffer:*char (malloc (<< 1 24))) + (def f:*FILE (fopen path "rb")) (unless f (janet-panic "no font file found")) (assert (fread font-buffer 1 (<< 1 24) f)) (fclose f) # Raster pixel data from stb truetype - (def (img-data (* char)) (malloc (* gw gh 256))) - (memset img-data 0 (* gw gh 256)) + (def img-data:*char (malloc (* gw gh 256 4))) + (memset img-data 0 (* gw gh 256 4)) # Extract CP 437 fonts into a bitmap - (if (not (stbtt-InitFont ;fnt font-buffer 0)) (janet-panic "bad font")) - (def scale:float (stbtt-ScaleForPixelHeight ;fnt gh)) + (if (not (stbtt-InitFont &fnt font-buffer 0)) (janet-panic "bad font")) + (def scale:float (stbtt-ScaleForPixelHeight &fnt gh)) (printf "scale: %f\n" scale) # Iterate glyphs to find the overall offset for characters @@ -51,12 +58,15 @@ (var y0min:int 10000) (var x1max:int -10000) (var y1max:int -10000) - (for [(var glyph:int 1) (<= glyph 0x100) (++ glyph)] + (for [(var cp437:int 0) (< cp437 0x100) (++ cp437)] + (def unicode:int (aref cp437-lookup cp437)) + (def glyph:int (stbtt-FindGlyphIndex &fnt unicode)) + (if (not glyph) (continue)) (var x0:int 0) (var y0:int 0) (var x1:int 0) (var y1:int 0) - (stbtt-GetGlyphBitmapBox ;fnt glyph scale scale ;x0 ;y0 ;x1 ;y1) + (stbtt-GetGlyphBitmapBox &fnt glyph scale scale &x0 &y0 &x1 &y1) # (printf "glyph %d (x0, y0, x1, y1) = (%d, %d, %d, %d)\n" glyph x0 y0 x1 y1) (set x0min (? (< x0 x0min) x0 x0min)) (set y0min (? (< y0 y0min) y0 y0min)) @@ -72,32 +82,31 @@ (printf "xoff, yoff = %d, %d\n" xoff yoff) # Iterate glyphs - (for [(var glyph:int 1) (<= glyph 0x100) (++ glyph)] + (for [(var cp437:int 0) (< cp437 0x100) (++ cp437)] + (def unicode:int (aref cp437-lookup cp437)) + (def glyph:int (stbtt-FindGlyphIndex &fnt unicode)) + (if (not glyph) (continue)) (var x0:int 0) (var y0:int 0) (var x1:int 0) (var y1:int 0) - (stbtt-GetGlyphBitmapBox ;fnt glyph scale scale ;x0 ;y0 ;x1 ;y1) + (stbtt-GetGlyphBitmapBox &fnt glyph scale scale &x0 &y0 &x1 &y1) (def pw:int (* 16 gw)) - (def x:int (+ x0 xoff (* gw (% (- glyph 1) 16)))) - (def y:int (+ y0 yoff (* gh (/ (- glyph 1) 16)))) + (def x:int (+ x0 xoff (* gw (% cp437 16)))) + (def y:int (+ y0 yoff (* gh (/ cp437 16)))) # (printf "rendering at (x, y) = (%d, %d)\n" x y) - (stbtt-MakeGlyphBitmap ;fnt (+ img-data x (* pw y)) gw gh pw scale scale glyph)) - - # Clean up font data - (free font-buffer) + (stbtt-MakeGlyphBitmap &fnt (+ img-data x (* pw y)) gw gh pw scale scale glyph)) # Write image for debugging - (def pngname (malloc 1000)) + (def pngname:*void (malloc 1000)) (memset pngname 0 1000) (strcat pngname header-dest) (strcat pngname ".png") (stbi-write-png pngname (* 16 gw) (* 16 gh) 1 img-data (* 16 gw)) - (free pngname) # Now extract pixels into 1 bit per pixel buffer. # Print the buffer as a C array - (def fp (fopen header-dest "wb")) + (def fp:*FILE (fopen header-dest "wb")) (fprintf fp "unsigned char %s_data[] = {\n" cname) (for [(var y:int 0) (< y (* 16 gh)) (set y (+ y gh))] (for [(var x:int 0) (< x (* 16 gw)) (set x (+ x gw))] |
