Add VT(9) font tools.

Reviewed by:	nwhitehorn
MFC_to_10_after:	re approval

Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Aleksandr Rybalko 2013-12-05 22:58:05 +00:00
parent 4874c0802c
commit ac6cebeb9c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=259019
8 changed files with 686 additions and 0 deletions

View File

@ -0,0 +1,6 @@
PROG= fontcvt
MAN1=
WARNS?= 6
.include <bsd.prog.mk>

View File

@ -0,0 +1,385 @@
/*-
* Copyright (c) 2009 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Ed Schouten under sponsorship from the
* FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/endian.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static unsigned int width, wbytes, height;
struct glyph {
TAILQ_ENTRY(glyph) g_list;
uint8_t *g_data;
unsigned int g_index;
};
static TAILQ_HEAD(, glyph) glyph_list = TAILQ_HEAD_INITIALIZER(glyph_list);
static unsigned int glyph_total, glyph_normal, glyph_bold,
glyph_unique, glyph_dupe;
struct mapping {
TAILQ_ENTRY(mapping) m_list;
unsigned int m_char;
unsigned int m_length;
struct glyph *m_glyph;
};
TAILQ_HEAD(mapping_list, mapping);
static struct mapping_list mapping_list_normal =
TAILQ_HEAD_INITIALIZER(mapping_list_normal);
static struct mapping_list mapping_list_bold =
TAILQ_HEAD_INITIALIZER(mapping_list_bold);
static unsigned int mapping_total, mapping_normal, mapping_normal_folded,
mapping_bold, mapping_bold_folded, mapping_unique, mapping_dupe;
static void
usage(void)
{
fprintf(stderr,
"usage: fontcvt width height normal.bdf bold.bdf out.fnt\n");
exit(1);
}
static int
add_mapping(struct glyph *gl, unsigned int c, int bold)
{
struct mapping *mp;
struct mapping_list *ml;
mapping_total++;
if (bold) {
int found = 0;
TAILQ_FOREACH(mp, &mapping_list_normal, m_list) {
if (mp->m_char < c)
continue;
else if (mp->m_char > c)
break;
found = 1;
/*
* No mapping is needed if it's equal to the
* normal mapping.
*/
if (mp->m_glyph == gl) {
mapping_dupe++;
return (0);
}
}
if (!found) {
fprintf(stderr,
"Character %u not in normal font!\n", c);
return (1);
}
}
mp = malloc(sizeof *mp);
mp->m_char = c;
mp->m_glyph = gl;
mp->m_length = 0;
ml = bold ? &mapping_list_bold : &mapping_list_normal;
if (TAILQ_LAST(ml, mapping_list) != NULL &&
TAILQ_LAST(ml, mapping_list)->m_char >= c) {
fprintf(stderr, "Bad ordering at character %u\n", c);
return (1);
}
TAILQ_INSERT_TAIL(ml, mp, m_list);
if (bold)
mapping_bold++;
else
mapping_normal++;
mapping_unique++;
return (0);
}
static struct glyph *
add_glyph(const uint8_t *bytes, int bold, int fallback)
{
struct glyph *gl;
glyph_total++;
if (bold)
glyph_bold++;
else
glyph_normal++;
TAILQ_FOREACH(gl, &glyph_list, g_list) {
if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
glyph_dupe++;
return (gl);
}
}
gl = malloc(sizeof *gl);
gl->g_data = malloc(wbytes * height);
memcpy(gl->g_data, bytes, wbytes * height);
if (fallback)
TAILQ_INSERT_HEAD(&glyph_list, gl, g_list);
else
TAILQ_INSERT_TAIL(&glyph_list, gl, g_list);
glyph_unique++;
return (gl);
}
static int
parse_bdf(const char *filename, int bold __unused)
{
FILE *fp;
char *ln;
size_t length;
uint8_t bytes[wbytes * height];
unsigned int curchar = 0, i, line;
struct glyph *gl;
fp = fopen(filename, "r");
if (fp == NULL) {
perror(filename);
return (1);
}
while ((ln = fgetln(fp, &length)) != NULL) {
ln[length - 1] = '\0';
if (strncmp(ln, "ENCODING ", 9) == 0) {
curchar = atoi(ln + 9);
}
if (strcmp(ln, "BITMAP") == 0) {
for (i = 0; i < height; i++) {
if ((ln = fgetln(fp, &length)) == NULL) {
fprintf(stderr, "Unexpected EOF!\n");
return (1);
}
ln[length - 1] = '\0';
sscanf(ln, "%x", &line);
if (wbytes == 1) {
bytes[i] = line;
} else if (wbytes == 2) {
bytes[i * 2 + 0] = line >> 8;
bytes[i * 2 + 1] = line;
} else {
fprintf(stderr,
"Unsupported wbytes!\n");
return (1);
}
}
/* Prevent adding two glyphs for 0xFFFD */
if (curchar == 0xFFFD) {
if (!bold)
gl = add_glyph(bytes, bold, 1);
} else if (curchar >= 0x20) {
gl = add_glyph(bytes, bold, 0);
if (add_mapping(gl, curchar, bold) != 0)
return (1);
}
}
}
return (0);
}
static void
number_glyphs(void)
{
struct glyph *gl;
unsigned int idx = 0;
TAILQ_FOREACH(gl, &glyph_list, g_list)
gl->g_index = idx++;
}
static void
write_glyphs(FILE *fp)
{
struct glyph *gl;
TAILQ_FOREACH(gl, &glyph_list, g_list)
fwrite(gl->g_data, wbytes * height, 1, fp);
}
static void
fold_mappings(int bold)
{
struct mapping_list *ml;
struct mapping *mn, *mp, *mbase;
if (bold)
ml = &mapping_list_bold;
else
ml = &mapping_list_normal;
mp = mbase = TAILQ_FIRST(ml);
for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
mn = TAILQ_NEXT(mp, m_list);
if (mn != NULL && mn->m_char == mp->m_char + 1 &&
mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
continue;
mbase->m_length = mp->m_char - mbase->m_char + 1;
mbase = mp = mn;
if (bold)
mapping_bold_folded++;
else
mapping_normal_folded++;
}
}
struct file_mapping {
uint32_t source;
uint16_t destination;
uint16_t length;
} __packed;
static void
write_mappings(FILE *fp, int bold)
{
struct mapping_list *ml;
struct mapping *mp;
struct file_mapping fm;
unsigned int i = 0, j = 0;
if (bold)
ml = &mapping_list_bold;
else
ml = &mapping_list_normal;
TAILQ_FOREACH(mp, ml, m_list) {
j++;
if (mp->m_length > 0) {
i += mp->m_length;
fm.source = htobe32(mp->m_char);
fm.destination = htobe16(mp->m_glyph->g_index);
fm.length = htobe16(mp->m_length - 1);
fwrite(&fm, sizeof fm, 1, fp);
}
}
assert(i == j);
}
struct file_header {
uint8_t magic[8];
uint8_t width;
uint8_t height;
uint16_t nglyphs;
uint16_t nmappings_normal;
uint16_t nmappings_bold;
} __packed;
static int
write_fnt(const char *filename)
{
FILE *fp;
struct file_header fh = {
.magic = "VFNT 1.0",
};
fp = fopen(filename, "wb");
if (fp == NULL) {
perror(filename);
return (1);
}
fh.width = width;
fh.height = height;
fh.nglyphs = htobe16(glyph_unique);
fh.nmappings_normal = htobe16(mapping_normal_folded);
fh.nmappings_bold = htobe16(mapping_bold_folded);
fwrite(&fh, sizeof fh, 1, fp);
write_glyphs(fp);
write_mappings(fp, 0);
write_mappings(fp, 1);
return (0);
}
int
main(int argc, char *argv[])
{
assert(sizeof(struct file_header) == 16);
assert(sizeof(struct file_mapping) == 8);
if (argc != 6)
usage();
width = atoi(argv[1]);
wbytes = howmany(width, 8);
height = atoi(argv[2]);
if (parse_bdf(argv[3], 0) != 0)
return (1);
if (parse_bdf(argv[4], 1) != 0)
return (1);
number_glyphs();
fold_mappings(0);
fold_mappings(1);
if (write_fnt(argv[5]) != 0)
return (1);
printf(
"Statistics:\n"
"- glyph_total: %5u\n"
"- glyph_normal: %5u\n"
"- glyph_bold: %5u\n"
"- glyph_unique: %5u\n"
"- glyph_dupe: %5u\n"
"- mapping_total: %5u\n"
"- mapping_normal: %5u\n"
"- mapping_normal_folded: %5u\n"
"- mapping_bold: %5u\n"
"- mapping_bold_folded: %5u\n"
"- mapping_unique: %5u\n"
"- mapping_dupe: %5u\n",
glyph_total,
glyph_normal, glyph_bold,
glyph_unique, glyph_dupe,
mapping_total,
mapping_normal, mapping_normal_folded,
mapping_bold, mapping_bold_folded,
mapping_unique, mapping_dupe);
return (0);
}

View File

@ -0,0 +1,12 @@
#!/bin/sh
for i in 6:12 8:14 8:16 10:18 10:20 11:22 12:24 14:28 16:32
do
C=`echo $i | cut -f 1 -d :`
R=`echo $i | cut -f 2 -d :`
./fontcvt \
$C $R \
~/terminus-font-4.36/ter-u${R}n.bdf \
~/terminus-font-4.36/ter-u${R}b.bdf \
terminus-u${R}.vfnt
gzip -9nf terminus-u${R}.vfnt
done

View File

@ -0,0 +1,6 @@
PROG= mkkfont
MAN1=
WARNS?= 6
.include <bsd.prog.mk>

View File

@ -0,0 +1,181 @@
/*-
* Copyright (c) 2009 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Ed Schouten under sponsorship from the
* FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/endian.h>
#include <sys/param.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct file_mapping {
uint32_t source;
uint16_t destination;
uint16_t length;
} __packed;
struct file_header {
uint8_t magic[8];
uint8_t width;
uint8_t height;
uint16_t nglyphs;
uint16_t nmappings_normal;
uint16_t nmappings_bold;
} __packed;
static int
print_glyphs(struct file_header *fh)
{
unsigned int gbytes, nglyphs, j, k, total;
uint8_t *gbuf;
gbytes = howmany(fh->width, 8) * fh->height;
nglyphs = be16toh(fh->nglyphs);
printf("\nstatic uint8_t font_bytes[%u * %u] = {", nglyphs, gbytes);
total = nglyphs * gbytes;
gbuf = malloc(total);
if (fread(gbuf, total, 1, stdin) != 1) {
perror("glyph");
return (1);
}
for (j = 0; j < total; j += 12) {
for (k = 0; k < 12 && k < total - j; k++) {
printf(k == 0 ? "\n\t" : " ");
printf("0x%02hhx,", gbuf[j + k]);
}
}
free(gbuf);
printf("\n};\n");
return (0);
}
static int
print_mappings(struct file_header *fh, int bold)
{
struct file_mapping fm;
const char *name;
unsigned int nmappings, i, col = 0;
if (bold) {
nmappings = be16toh(fh->nmappings_bold);
name = "bold";
} else {
nmappings = be16toh(fh->nmappings_normal);
name = "normal";
}
if (nmappings == 0)
return (0);
printf("\nstatic struct vt_font_map font_mapping_%s[%u] = {",
name, nmappings);
for (i = 0; i < nmappings; i++) {
if (fread(&fm, sizeof fm, 1, stdin) != 1) {
perror("mapping");
return (1);
}
printf(col == 0 ? "\n\t" : " ");
printf("{ 0x%04x, 0x%04x, 0x%02x },",
be32toh(fm.source), be16toh(fm.destination),
be16toh(fm.length));
col = (col + 1) % 2;
}
printf("\n};\n");
return (0);
}
static int
print_info(struct file_header *fh)
{
unsigned int nnormal, nbold;
printf(
"\nstruct vt_font vt_font_default = {\n"
"\t.vf_width\t\t= %u,\n"
"\t.vf_height\t\t= %u,\n"
"\t.vf_bytes\t\t= font_bytes,\n",
fh->width, fh->height);
nnormal = be16toh(fh->nmappings_normal);
nbold = be16toh(fh->nmappings_bold);
if (nnormal != 0)
printf(
"\t.vf_normal\t\t= font_mapping_normal,\n"
"\t.vf_normal_length\t= %u,\n", nnormal);
if (nbold != 0)
printf(
"\t.vf_bold\t\t= font_mapping_bold,\n"
"\t.vf_bold_length\t\t= %u,\n", nbold);
printf("\t.vf_refcount\t\t= 1,\n};\n");
return (0);
}
int
main(int argc __unused, char *argv[] __unused)
{
struct file_header fh;
if (fread(&fh, sizeof fh, 1, stdin) != 1) {
perror("file_header");
return (1);
}
if (memcmp(fh.magic, "VFNT 1.0", 8) != 0) {
fprintf(stderr, "Bad magic\n");
return (1);
}
printf("#include <dev/vt/vt.h>\n");
if (print_glyphs(&fh) != 0)
return (1);
if (print_mappings(&fh, 0) != 0)
return (1);
if (print_mappings(&fh, 1) != 0)
return (1);
if (print_info(&fh) != 0)
return (1);
return (0);
}

View File

@ -0,0 +1,5 @@
#!/bin/sh
for i in 12 14 16 20 22 24 28 32
do
zcat ../fontcvt/terminus-u${i}.vfnt.gz | ./mkkfont > terminus-u${i}.c
done

View File

@ -0,0 +1,6 @@
PROG= setfont
MAN1=
WARNS?= 6
.include <bsd.prog.mk>

View File

@ -0,0 +1,85 @@
#include <sys/consio.h>
#include <sys/endian.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct file_header {
uint8_t magic[8];
uint8_t width;
uint8_t height;
uint16_t nglyphs;
uint16_t nmappings_normal;
uint16_t nmappings_bold;
} __packed;
static vfnt_map_t *
load_mappingtable(unsigned int nmappings)
{
vfnt_map_t *t;
unsigned int i;
if (nmappings == 0)
return (NULL);
t = malloc(sizeof *t * nmappings);
if (fread(t, sizeof *t * nmappings, 1, stdin) != 1) {
perror("mappings");
exit(1);
}
for (i = 0; i < nmappings; i++) {
t[i].src = be32toh(t[i].src);
t[i].dst = be16toh(t[i].dst);
t[i].len = be16toh(t[i].len);
}
return (t);
}
int
main(int argc __unused, char *argv[] __unused)
{
struct file_header fh;
static vfnt_t vfnt;
size_t glyphsize;
if (fread(&fh, sizeof fh, 1, stdin) != 1) {
perror("file_header");
return (1);
}
if (memcmp(fh.magic, "VFNT 1.0", 8) != 0) {
fprintf(stderr, "Bad magic\n");
return (1);
}
vfnt.nnormal = be16toh(fh.nmappings_normal);
vfnt.nbold = be16toh(fh.nmappings_bold);
vfnt.nglyphs = be16toh(fh.nglyphs);
vfnt.width = fh.width;
vfnt.height = fh.height;
glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.nglyphs;
vfnt.glyphs = malloc(glyphsize);
if (fread(vfnt.glyphs, glyphsize, 1, stdin) != 1) {
perror("glyphs");
return (1);
}
vfnt.normal = load_mappingtable(vfnt.nnormal);
vfnt.bold = load_mappingtable(vfnt.nbold);
if (ioctl(STDOUT_FILENO, PIO_VFONT, &vfnt) == -1) {
perror("PIO_VFONT");
return (1);
}
return (0);
}