// 8x8 Font
// Written by Sjur Julin 2023.
// License: CC BY 4.0 https://creativecommons.org/licenses/by/4.0/
// Read the 2023 article here: https://www.ignorantus.com/pages/nano_fractals/
#include <string.h>
#include "txt8x8.h"

static uint64_t font[96] = {
    0x0000000000000000, 0x3030303030003000, 0x6c24480000000000, 0x287c28287c280000, 0x103860380c781000, 0x00c6cc183066c600, 0x78cc7cd8cc7e0000, 0x3010200000000000,
    0x1820606060201800, 0x30080c0c0c083000, 0x00107c386c000000, 0x0010107c10100000, 0x0000000000301020, 0x0000007c00000000, 0x0000000030300000, 0x0c08181030206000,
    0x78cccccccc780000, 0x3070303030300000, 0xf80c78c0c0fc0000, 0xf80c380c0cf80000, 0x18385898fc180000, 0xfcc0f80c0cf80000, 0x78c0f8cccc780000, 0xfc0c183030300000,
    0x78cc78cccc780000, 0x78cccc7c0c780000, 0x0030300030300000, 0x0000303000301020, 0x183060c060301800, 0x00007c007c000000, 0x6030180c18306000, 0x78cc0c1830003000,
    0x78c4d4dcc0780000, 0x78ccccfccccc0000, 0xf8ccf8ccccf80000, 0x78ccc0c0cc780000, 0xf8ccccccccf80000, 0xfcc0f0c0c0fc0000, 0xfcc0f0c0c0c00000, 0x78ccc0dccc7c0000,
    0xccccfccccccc0000, 0x3030303030300000, 0x1818181818f00000, 0xccd8f0d8cccc0000, 0xc0c0c0c0c0fc0000, 0xc6eefed6c6c60000, 0xccecfcdccccc0000, 0x78cccccccc780000,
    0xf8ccccf8c0c00000, 0x78ccccdccc7c0000, 0xf8ccccf8cccc0000, 0x7cc070380cf80000, 0xfc30303030300000, 0xcccccccccc780000, 0xcccccccc78300000, 0xc6c6d6feeec60000,
    0xcc783078cccc0000, 0xcccc783030300000, 0xfc183060c0fc0000, 0x7860606060607800, 0x6020301018080c00, 0x3c0c0c0c0c0c3c00, 0x1028440000000000, 0x00000000000000fe,
    0x6030100000000000, 0x00780c7ccc7c0000, 0xc0f8ccccccf80000, 0x007cc0c0c07c0000, 0x0c7ccccccc7c0000, 0x0078ccfcc0780000, 0x3c60786060600000, 0x007ccccc7c0c7800,
    0xc0c0f8cccccc0000, 0x3000303030300000, 0x1800181818183000, 0xc0c0d8f0d8cc0000, 0x3030303030300000, 0x00fcd6d6d6c60000, 0x00f8cccccccc0000, 0x0078cccccc780000,
    0x00f8ccccf8c0c000, 0x007ccccc7c0c0c00, 0x00786c6060600000, 0x0078c0780cf80000, 0x6060f860603c0000, 0x00cccccccc7c0000, 0x00cccccc78300000, 0x00c6c6d6d67e0000,
    0x00cc783078cc0000, 0x00cccccc7c0c7800, 0x00fc183060fc0000, 0x182020c020201800, 0x1010100010101000, 0x3008080608083000, 0x6498000000000000, 0x0000000000000000
};

void fix8x8(void)
{
    for( int i=0; i<96; i++ ) {
        uint64_t ch = font[i], cx = 0;
        for( int j=0; j<64; j++ ) {
            cx <<= 1;
            cx |= (ch & 1);
            ch >>= 1;
        }
        font[i] = cx;
    }
}

void txt8x8x2(uint32_t *buf, int stride, char *txt, uint32_t fg, uint32_t bg)
{
    int txtlen = (int)strlen(txt);
    for( int n=0; n<txtlen; n++ ) {
        uint64_t ch = font[txt[n]-0x20];
        for( int y=0; y<8; y++ ) {
            uint32_t *dst0 = &buf[n*8*2+0*stride+y*stride*2];
            uint32_t *dst1 = &buf[n*8*2+1*stride+y*stride*2];
            for( int x=0; x<8; x++ ) {
                uint32_t v = (ch & 1) ? fg : bg;
                *dst0++ = v; *dst0++ = v;
                *dst1++ = v; *dst1++ = v;
                ch >>= 1;
            }
        }
    }
}

void txt8x8x4(uint32_t *buf, int stride, char *txt, uint32_t fg, uint32_t bg)
{
    int txtlen = (int)strlen(txt);
    for( int n=0; n<txtlen; n++ ) {
        uint64_t ch = font[txt[n]-0x20];
        for( int y=0; y<8; y++ ) {
            uint32_t *dst0 = &buf[n*8*4+0*stride+y*stride*4];
            uint32_t *dst1 = &buf[n*8*4+1*stride+y*stride*4];
            uint32_t *dst2 = &buf[n*8*4+2*stride+y*stride*4];
            uint32_t *dst3 = &buf[n*8*4+3*stride+y*stride*4];
            for( int x=0; x<8; x++ ) {
                uint32_t v = (ch & 1) ? fg : bg;
                *dst0++ = v; *dst0++ = v; *dst0++ = v; *dst0++ = v;
                *dst1++ = v; *dst1++ = v; *dst1++ = v; *dst1++ = v;
                *dst2++ = v; *dst2++ = v; *dst2++ = v; *dst2++ = v;
                *dst3++ = v; *dst3++ = v; *dst3++ = v; *dst3++ = v;
                ch >>= 1;
            }
        }
    }
}

void txt8x8x5(uint32_t *buf, int stride, char *txt, uint32_t fg, uint32_t bg)
{
    int txtlen = (int)strlen(txt);
    for( int n=0; n<txtlen; n++ ) {
        uint64_t ch = font[txt[n]-0x20];
        for( int y=0; y<8; y++ ) {
            uint32_t *dst0 = &buf[n*8*5+0*stride+y*stride*5];
            uint32_t *dst1 = &buf[n*8*5+1*stride+y*stride*5];
            uint32_t *dst2 = &buf[n*8*5+2*stride+y*stride*5];
            uint32_t *dst3 = &buf[n*8*5+3*stride+y*stride*5];
            uint32_t *dst4 = &buf[n*8*5+4*stride+y*stride*5];
            for( int x=0; x<8; x++ ) {
                uint32_t v = (ch & 1) ? fg : bg;
                *dst0++ = v; *dst0++ = v; *dst0++ = v; *dst0++ = v; *dst0++ = v;
                *dst1++ = v; *dst1++ = v; *dst1++ = v; *dst1++ = v; *dst1++ = v;
                *dst2++ = v; *dst2++ = v; *dst2++ = v; *dst2++ = v; *dst2++ = v;
                *dst3++ = v; *dst3++ = v; *dst3++ = v; *dst3++ = v; *dst3++ = v;
                *dst4++ = v; *dst4++ = v; *dst4++ = v; *dst4++ = v; *dst4++ = v;
                ch >>= 1;
            }
        }
    }
}
