~mrms/unifont2h

c29e7363afeb86e3cd55129fee37a5360247974f — Marek Maskarinec 3 months ago 93bf4cb
allow including only parts of the plane
3 files changed, 33 insertions(+), 7 deletions(-)

M README.md
M main.c
M test.c
M README.md => README.md +0 -4
@@ 7,7 7,3 @@ the `test.c` file.

* `make` - build the tool
* `make test` - build the test program

## todo

* add a way to include only parts of the BMP

M main.c => main.c +24 -2
@@ 50,15 50,24 @@ void dosmall(const char *glyph) {

int main(int argc, const char *argv[]) {
	printf(

		"#ifndef UNIFONT_H\n"
		"#define UNIFONT_H\n"
		"const uint16_t unifont[65536][16] = {\n"
		"#ifdef __\n"
		"static uint16_t __[256][16] = {\n"
	);

	uint32_t codepoint;
	char glyph[65] = {0};
	for (int i=0; scanf("%x:%s\n", &codepoint, glyph) == 2; ++i) {
		fprintf(stderr, "%x:%s\n", codepoint, glyph);
		if (i % 256 == 0)
			printf(
				"};\n"
				"#endif\n"
				"#if defined(UNIFONT_GROUP_%d) || defined(UNIFONT_GROUP_ALL)\n"
				"static uint16_t group_%d[256][16] = {\n",
				i / 256, i / 256
			);

		printf("\t{");
		if (strlen(glyph) == 32)


@@ 70,4 79,17 @@ int main(int argc, const char *argv[]) {
		memset(glyph, 0, 65);
	}
	printf("};\n#endif\n");

	printf("uint16_t *unifont[256] = {\n");
	for (int i=0; i < 256; ++i)
		printf(
			"#if defined(UNIFONT_GROUP_%d) || defined(UNIFONT_GROUP_ALL)\n"
			"&group_%d[0][0],\n"
			"#else\n"
			"NULL,\n"
			"#endif\n",
			i, i
		);

	printf("};\n#endif\n");
}

M test.c => test.c +9 -1
@@ 1,10 1,18 @@
#include <stdint.h>
#include <stdio.h>

#define UNIFONT_GROUP_0
#define UNIFONT_GROUP_1
#include "unifont.h"

static
void printchar(uint32_t c) {
	const uint16_t *bmp = unifont[c];
	const uint16_t *page = unifont[c/256];
	if (page == NULL) {
		printf("page containing character U+%x isn't included.\n", c);
		return;
	}
	const uint16_t *bmp = &unifont[c/256][c%256 * 16];

	for (int i=0; i < 16; ++i) {
		uint16_t r = bmp[i];