%D \module
%D   [       file=meta-imp-threesix,
%D        version=2019.00.00,
%D          title=\METAPOST\ Graphics,
%D       subtitle=Don Knuths ThreeSix font,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D The design of the font is if course by Don Knuth and can be found in facs9b.ps. I
%D wish I was clever enough to understand all the good stuff in there.

\startluacode
    local unpack = unpack
    local concat = table.concat
    local gsub, gmatch, utfbyte = string.gsub, string.gmatch, utf.byte

    local font36 = {
        ["0"] = [[00111100 01111110 11000011 11000011 11000011 11000011 01111110 00111100]],
        ["1"] = [[00011100 11111100 11101100 00001100 00001100 00001100 11111111 11111111]],
        ["2"] = [[00111110 01110011 01110011 00000011 00001110 00111000 11110001 11111111]],
        ["3"] = [[01111110 01100110 00000110 00111100 00000110 11100111 11100111 01111110]],
        ["4"] = [[00001110 00011110 00110110 01100110 11111111 11111111 00000110 00001110]],
        ["5"] = [[01111110 01111110 01000000 01111100 01000111 00000011 11000111 11111110]],
        ["6"] = [[01111110 01100110 11000000 11011100 11100110 11000011 01100011 01111110]],
        ["7"] = [[11111111 11111111 10000111 10001110 00011100 00011100 00011100 00011100]],
        ["8"] = [[01111110 01100110 01100110 00111100 11000011 11000011 11000011 01111110]],
        ["9"] = [[01111110 11000110 11000011 01100111 00111011 00000011 01100110 01111110]],
        ["A"] = [[000110000 000111000 001111100 001101100 011001110 011111110 010000110 111100111]],
        ["B"] = [[1111100 1110110 0110110 0111100 0110011 0110011 1110011 1111100]],
        ["C"] = [[01111101 11110011 11100001 11100001 11100000 11100000 11100001 01111110]],
        ["D"] = [[11111100 11100010 01100011 01100011 01100011 01100011 11100010 11111100]],
        ["E"] = [[1111111 1110001 0110101 0111100 0110100 0110001 1110001 1111111]],
        ["F"] = [[11111111 11100001 01100101 01111100 01100100 01100000 11111000 11111000]],
        ["G"] = [[01111010 11100110 11100010 11100000 11100111 11100010 11100010 01111100]],
        ["H"] = [[11100111 11100111 01000010 01111110 01000010 01000010 11100111 11100111]],
        ["I"] = [[1111111 1111111 0011000 0011000 0011000 0011000 1111111 1111111]],
        ["J"] = [[01111111 01111111 00000110 00000110 11110110 01100110 01100110 00111100]],
        ["K"] = [[11101110 11100100 01101000 01110000 01111000 01101100 11100110 11101111]],
        ["L"] = [[111111000 111111000 011000000 011000000 011000000 011000011 111000011 111111111]],
        ["M"] = [[1100000011 1110000111 0111111110 0100110010 0100110010 0100000010 1100000011 1100000011]],
        ["N"] = [[11000011 11100011 01110010 01111010 01011110 01001110 11100110 11100010]],
        ["O"] = [[01111110 11000011 11000011 11000011 11000011 11000011 11000011 01111110]],
        ["P"] = [[11111110 11100011 01100011 01111110 01100000 01100000 11111000 11111000]],
        ["Q"] = [[01111100 11000110 11000110 11000110 11000110 11001110 11000110 01111101]],
        ["R"] = [[11111100 11100110 01100110 01111100 01101000 01100100 11100010 11100111]],
        ["S"] = [[01111110 11100001 11100001 01111000 00011110 10000111 11000011 10111110]],
        ["T"] = [[1111111111 1100110011 1100110011 0000110000 0000110000 0000110000 0001111000 0001111000]],
        ["U"] = [[111101111 111101111 011000010 011000010 011000010 011000010 011000010 001111100]],
        ["V"] = [[11111000111 11111000111 01110000010 00110000100 00111000100 00011001000 00001101000 00001110000]],
        ["W"] = [[111000000111 111000000111 110000000010 011000000100 011001000100 001101101000 001101101000 0001101100000]],
        ["X"] = [[1111001110 1111000110 0001101000 0000110000 0000110000 0001011000 0110001111 0111001111]],
        ["Y"] = [[111100011 111100011 011000010 001110100 000111000 000011000 001111110 001111110]],
        ["Z"] = [[11111111 10000111 00001110 00011100 00111000 01110000 11100001 11111111]],

        -- these don't have the 36 dots property:

        [","] = [[0000 0000 0000 0000 0000 0011 0111 0111]],
        [";"] = [[0011 0011 0011 0000 0000 0011 0111 0111]],
        ["."] = [[0000 0000 0000 0000 0000 0111 0111 0111]],
        [":"] = [[0111 0111 0111 0000 0000 0111 0111 0111]],
        ["!"] = [[0111 0111 0111 0111 0111 0000 0111 0111]],
        ["?"] = [[11111 10111 00111 01110 01110 00000 01110 01110]],

        [utf.char(0x00AD)] = [[00000 00000 00000 11111 11111 00000 00000 00000]], -- hyphen
        [utf.char(0x2013)] = [[0000000 0000000 0000000 1111111 1111111 0000000 0000000 0000000]], -- endash
        [utf.char(0x2014)] = [[000000000 000000000 000000000 111111111 111111111 000000000 000000000 000000000]], -- emdash
    }

    MP.font36 = font36

    local f_code  = string.formatters["ThreeSix(%s);"]
    local replace = { ["0"] = "N;", ["1"] = "Y;", [" "] = "L;" }

    local function remap(str)
        -- permit abundant spacing (bonus)
        str = gsub(str,"%s+", " ")
        -- remap what we got to macro calls
        str = gsub(str,".",replace)
        -- return the result
        return str
    end

    function MP.registerthreesix(name)
        fonts.dropins.registerglyphs {
            name     = name,
            units    = 12,
            usecolor = true,
            preamble = "InitializeThreeSix;",
        }
        for u, v in table.sortedhash(font36) do
            local data   = remap(v)
            local ny     = 8
            local nx     = ((#v + 1) // ny) - 1
            local height = ny * 1.1 - 0.1
            local width  = nx * 1.1 - 0.1
            fonts.dropins.registerglyph {
                category = name,
                unicode  = utfbyte(u),
                width    = width,
                height   = height,
                code     = f_code(data),
            }
        end
    end
    MP.registerthreesix("fontthreesix")
\stopluacode

\startMPcalculation{simplefun}
    def InitializeThreeSix =
        save Y, N, L, S ;
        save shape, fillcolor, mypen, random, threesixpen, spread, hoffset ;
        string shape, fillcolor, mypen ; boolean random ; pen threesixpen ;
        shape       := getparameterdefault "mpsfont" "shape"  "circle" ;
        random      := hasoption           "mpsfont" "random" "true" ;
        fillcolor   := getparameterdefault "mpsfont" "color"  "" ;
        mypen       := getparameterdefault "mpsfont" "pen"    "" ;
        spread      := getparameterdefault "mpsfont" "spread" 0 ;
        hoffset     := 12 * spread / 2 ;
        threesixpen := pencircle
            if mypen = "fancy" :
                xscaled 1/20 yscaled 2/20 rotated 45
            else :
                scaled 1/20
            fi ;
        if shape == "square" :
            def S =
                unitsquare if random : randomized 1/10 fi
                shifted (nx,ny)
            enddef ;
        elseif shape = "diamond" :
            def S =
                unitdiamond if random : randomized 1/10 fi
                shifted (nx,ny)
            enddef ;
        else :
            def S =
                unitcircle if random : randomizedcontrols 1/20 fi
                shifted (nx,ny)
            enddef ;
        fi ;
        if fillcolor = "" :
            def N =
                nx := nx + dx ;
                draw S ;
            enddef ;
        else :
            def N =
                nx := nx + dx ;
                draw S withcolor "lightgray" ;
            enddef ;
        fi ;
        if fillcolor = "random" :
            def Y =
                nx := nx + dx ;
                fillup S withcolor white randomized (2/3,2/3,2/3) ;
            enddef ;
        elseif fillcolor = "" :
            def Y =
                nx := nx + dx ;
                fillup S ;
            enddef ;
        else :
            def Y =
                nx := nx + dx ;
                fillup S withcolor fillcolor ;
            enddef ;
        fi ;
        def L =
            nx := - dx ;
            ny := ny + dy ;
        enddef ;
    enddef ;

    vardef ThreeSix (text code) =
        save dx, dy, nx, ny ;
        dx :=   11/10 ;
        dy := - 11/10 ;
        nx := - dx ;
        ny :=   0 ;
        pickup threesixpen ;
        draw image (code) shifted (hoffset,-ny) ;
    enddef ;
\stopMPcalculation

% spacing=.25 plus .1 minus .05 extra .25

\definefontfeature % black and white, with some spread
  [fontthreesix]
  [default]
  [metapost=fontthreesix]

\definefontfeature % color, with some spread
  [fontthreesix-tweak]
  [default]
  [metapost={category=fontthreesix,spread=.1}]

\definefontfeature % color, with some spread
  [fontthreesix-color]
  [default]
  [metapost={category=fontthreesix,shape=diamond,color=random,pen=fancy,spread=.1}]

\definefontfeature % color, tight
  [fontthreesix-initial]
  [metapost={category=fontthreesix,color=random,shape=circle}] % units?

\definefont[DEKFontA][Serif*fontthreesix]
\definefont[DEKFontB][Serif*fontthreesix-color]
\definefont[DEKFontC][Serif*fontthreesix-initial]
\definefont[DEKFontD][Serif*fontthreesix-tweak]

\continueifinputfile{meta-imp-threesix.mkxl}

\starttext

\definefontfeature % color, with some spread
  [fontthreesix-color]
  [default]
  [metapost={category=fontthreesix,shape=diamond,color=random,pen=fancy,spread=.1,random=yes}]

\definefont[DEKFontA][Serif*fontthreesix-color @ 200pt]
\definefont[DEKFontB][Serif*fontthreesix-color @  12pt]

% Or course:

\startTEXpage[offset=1dk]
    \DEKFontA TEX
\stopTEXpage

\startTEXpage[offset=1dk]
    \DEKFontA T\kern-0.05em\lower.75\exheight\hbox{E}\kern-.05emX
\stopTEXpage

% We only have uppercase characters! But I added a few punctuation symbols so that
% we can do the following. Actually we can consider lowercase to be just smaller and
% another shape.

\startTEXpage[offset=1dk,align=flushleft,foregroundstyle=\DEKFontB]
    \WORD{\input{knuth}}
\stopTEXpage

\stoptext