termfont

Display characters with unicode quadrants and halfblock
git clone https://noulin.net/git/termfont.git
Log | Files | Refs

commit 3e5d0236c61874627f38c84d4004620761f8ace7
parent c6b1fb3fd8fc197e5e0ca10f5d5d30a3fdd161a7
Author: Remy Noulin <loader2x@gmail.com>
Date:   Mon, 25 Dec 2023 22:03:07 +0200

add 2 by 3 block rendering, -s option, fix bug in halfblock function

termfont.c | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 151 insertions(+), 3 deletions(-)

Diffstat:
Mtermfont.c | 154+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 151 insertions(+), 3 deletions(-)

diff --git a/termfont.c b/termfont.c @@ -358,7 +358,7 @@ void showStringHalfblockNoFullBlock(twidfont *font, char *string) { if (*(p + i)== ' ') printf("%k",0); else - printf("%k", colors[j-1]); + printf("%k", colors[font->height-1]); printf(halfBlocks[2]); printf(RST); } @@ -506,6 +506,150 @@ void showStringQuadrantColors(twidfont *font, char *string) { free(pixels); } +void showString2by3(twidfont *font, char *string) { + if (!font or !font->pixel) ret; + size_t len = lenG(string); + if (!len) ret; + + u32 totalWidth = 0; // in blocks + range(i, len) { + totalWidth += font->chars[string[i]].width; + } + + u8 *pixels = malloc(font->height * totalWidth); + u32 x = 0; // start of next character in pixels + + range(i, len) { + if (!font->chars[string[i]].width) continue; // font symbol unavailable + // copy symbol to buffer + u8 *p = font->chars[string[i]].pixels; + range(ii, font->height) { + memcpy(pixels + x + ii * totalWidth, p, font->chars[string[i]].width); + p += font->chars[string[i]].width; + } + + x+= font->chars[string[i]].width; + } + + u8 *p = pixels; + char utf[6] = init0Var; + u32 block; + rangeFromStep(j, 2, font->height, 3) { + rangeFromStep(i, 1, totalWidth, 2) { + block = 0; + if (*(p + i-1) != ' ') block |= 1; + if (*(p + i) != ' ') block |= 2; + if (*(p + i-1 + totalWidth) != ' ') block |= 4; + if (*(p + i + totalWidth) != ' ') block |= 8; + if (*(p + i-1 + 2*totalWidth) != ' ') block |= 16; + if (*(p + i + 2*totalWidth) != ' ') block |= 32; + if (block == 0) block = ' '; + elif (block == 0x3f) block = 0x2588; + elif (block < 0x15) block = block-1 + 0x1fb00; + elif (block == 0x15) block = 0x258c; + elif (block < 0x2a) block = block-2 + 0x1fb00; + elif (block == 0x2a) block = 0x2590; + else block = block-3 + 0x1fb00; + // vertical bar left 0x258c right 0x2590 + pError0(bRune2CodeUTF8(utf, block)); + printf(utf); + } + if (totalWidth & 1) { + block = 0; + if (*(p + totalWidth-1) != ' ') block |= 1; + if (*(p + totalWidth-1 + totalWidth) != ' ') block |= 4; + if (*(p + totalWidth-1 + 2*totalWidth) != ' ') block |= 16; + if (block == 0) block = ' '; + elif (block == 0x3f) block = 0x2588; + elif (block < 0x15) block = block-1 + 0x1fb00; + elif (block == 0x15) block = 0x258c; + elif (block < 0x2a) block = block-2 + 0x1fb00; + elif (block == 0x2a) block = 0x2590; + else block = block-3 + 0x1fb00; + // vertical bar left 0x258c right 0x2590 + pError0(bRune2CodeUTF8(utf, block)); + printf(utf); + } + put; + p += totalWidth * 3; + } + if (font->height % 3 == 1) { + // one more line + rangeFromStep(i, 1, totalWidth, 2) { + block = 0; + if (*(p + i-1) != ' ') block |= 1; + if (*(p + i) != ' ') block |= 2; + if (block == 0) block = ' '; + elif (block == 0x3f) block = 0x2588; + elif (block < 0x15) block = block-1 + 0x1fb00; + elif (block == 0x15) block = 0x258c; + elif (block < 0x2a) block = block-2 + 0x1fb00; + elif (block == 0x2a) block = 0x2590; + else block = block-3 + 0x1fb00; + // vertical bar left 0x258c right 0x2590 + pError0(bRune2CodeUTF8(utf, block)); + printf(utf); + } + if (totalWidth & 1) { + block = 0; + if (*(p + totalWidth-1) != ' ') block |= 1; + if (block == 0) block = ' '; + elif (block == 0x3f) block = 0x2588; + elif (block < 0x15) block = block-1 + 0x1fb00; + elif (block == 0x15) block = 0x258c; + elif (block < 0x2a) block = block-2 + 0x1fb00; + elif (block == 0x2a) block = 0x2590; + else block = block-3 + 0x1fb00; + // vertical bar left 0x258c right 0x2590 + pError0(bRune2CodeUTF8(utf, block)); + printf(utf); + } + put; + } + if (font->height % 3 == 2) { + // two more lines + rangeFromStep(i, 1, totalWidth, 2) { + block = 0; + if (*(p + i-1) != ' ') block |= 1; + if (*(p + i) != ' ') block |= 2; + if (*(p + i-1 + totalWidth) != ' ') block |= 4; + if (*(p + i + totalWidth) != ' ') block |= 8; + if (block == 0) block = ' '; + elif (block == 0x3f) block = 0x2588; + elif (block < 0x15) block = block-1 + 0x1fb00; + elif (block == 0x15) block = 0x258c; + elif (block < 0x2a) block = block-2 + 0x1fb00; + elif (block == 0x2a) block = 0x2590; + else block = block-3 + 0x1fb00; + // vertical bar left 0x258c right 0x2590 + pError0(bRune2CodeUTF8(utf, block)); + printf(utf); + } + if (totalWidth & 1) { + block = 0; + if (*(p + totalWidth-1) != ' ') block |= 1; + if (*(p + totalWidth-1 + totalWidth) != ' ') block |= 4; + if (block == 0) block = ' '; + elif (block == 0x3f) block = 0x2588; + elif (block < 0x15) block = block-1 + 0x1fb00; + elif (block == 0x15) block = 0x258c; + elif (block < 0x2a) block = block-2 + 0x1fb00; + elif (block == 0x2a) block = 0x2590; + else block = block-3 + 0x1fb00; + // vertical bar left 0x258c right 0x2590 + pError0(bRune2CodeUTF8(utf, block)); + printf(utf); + } + put; + } + + /* u32 *buffer = malloc( (font->height/2 + font->height&1) */ + /* * ( totalWidth/2 + totalWidth&1) */ + /* * sizeof(u32)); */ + /* free(buffer); */ + free(pixels); +} + void list(void) { cleanCharP(p) = expandHome(FONT_DIR); cleanSmallArrayP(dir) = readDirG(rtSmallArrayt, p); @@ -528,12 +672,13 @@ void demo(void) { cleanAllocateSmallArray(fontFile); readFileG(fontFile, l); setG(l, -6, 0); - logI("%m", l); twidfont font = init0Var; parseTwidFont(fontFile, &font); + logI("%m h=%d mod3=%d", l, font.height, font.height % 3); if (not font.pixel) showStringRaw(&font, "0123 abcd ABCD"); else { + showString2by3(&font, "0123 abcd ABCD"); showStringQuadrant(&font, "0123 abcd ABCD"); showStringHalfblock(&font, "0123 abcd ABCD"); } @@ -553,12 +698,13 @@ int main(int ARGC, char** ARGV) { //disableLibsheepyErrorLogs; if ((argc == 1) or - (argc == 4 and not eqG(argv[1], "-f") and not eqG(argv[1], "-F"))) { + (argc == 4 and not eqG(argv[1], "-s") and not eqG(argv[1], "-f") and not eqG(argv[1], "-F"))) { help: logI("Help\n" "Commands:\n" BLD CYN"list"RST" to list available fonts in "FONT_DIR"\n" BLD CYN"demo"RST" to print a short string with all available fonts\n" + BLD CYN"-s font name (without .twidf) 'Message'"RST" to show message with the selected font rendered with 2 by 3 blocks\n" BLD CYN"-f font name (without .twidf) 'Message'"RST" to show message with the selected font rendered with quadrants\n" BLD CYN"-F font name (without .twidf) 'Message'"RST" to show message with the selected font rendered with halfblocks\n" BLD CYN"color 'Message'"RST" to show message with the PressStart2P halfblock with colors\n" @@ -616,6 +762,8 @@ int main(int ARGC, char** ARGV) { if (not font.pixel) showStringRaw(&font, argv[3]); + elif (eqG(argv[1], "-s")) + showString2by3(&font, argv[3]); elif (eqG(argv[1], "-f")) showStringQuadrant(&font, argv[3]); elif (eqG(argv[1], "-F"))