198e93f6ed/*-
298e93f6ed * Copyright (c) 2009 The FreeBSD Foundation
398e93f6ed * All rights reserved.
498e93f6ed *
598e93f6ed * This software was developed by Ed Schouten under sponsorship from the
698e93f6ed * FreeBSD Foundation.
798e93f6ed *
898e93f6ed * Redistribution and use in source and binary forms, with or without
998e93f6ed * modification, are permitted provided that the following conditions
1098e93f6ed * are met:
1198e93f6ed * 1. Redistributions of source code must retain the above copyright
1298e93f6ed *    notice, this list of conditions and the following disclaimer.
1398e93f6ed * 2. Redistributions in binary form must reproduce the above copyright
1498e93f6ed *    notice, this list of conditions and the following disclaimer in the
1598e93f6ed *    documentation and/or other materials provided with the distribution.
1698e93f6ed *
1798e93f6ed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1898e93f6ed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1998e93f6ed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2098e93f6ed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2198e93f6ed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2298e93f6ed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2398e93f6ed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2498e93f6ed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2598e93f6ed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2698e93f6ed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2798e93f6ed * SUCH DAMAGE.
2898e93f6ed */
2998e93f6ed
3098e93f6ed#include <sys/cdefs.h>
3198e93f6ed__FBSDID("$FreeBSD$");
3298e93f6ed
3398e93f6ed#include <sys/endian.h>
3498e93f6ed#include <sys/param.h>
3598e93f6ed
3698e93f6ed#include <stdint.h>
3798e93f6ed#include <stdio.h>
3898e93f6ed#include <stdlib.h>
3998e93f6ed#include <string.h>
4098e93f6ed
4198e93f6edstruct file_mapping {
4298e93f6ed	uint32_t	source;
4398e93f6ed	uint16_t	destination;
4498e93f6ed	uint16_t	length;
4598e93f6ed} __packed;
4698e93f6ed
4798e93f6edstruct file_header {
4898e93f6ed	uint8_t		magic[8];
4998e93f6ed	uint8_t		width;
5098e93f6ed	uint8_t		height;
5146ad59cemaste	uint16_t	pad;
5246ad59cemaste	uint32_t	glyph_count;
5346ad59cemaste	uint32_t	map_count[4];
5498e93f6ed} __packed;
5598e93f6ed
5698e93f6edstatic int
5798e93f6edprint_glyphs(struct file_header *fh)
5898e93f6ed{
5946ad59cemaste	unsigned int gbytes, glyph_count, j, k, total;
6098e93f6ed	uint8_t *gbuf;
6198e93f6ed
6298e93f6ed	gbytes = howmany(fh->width, 8) * fh->height;
6346ad59cemaste	glyph_count = be32toh(fh->glyph_count);
6498e93f6ed
6546ad59cemaste	printf("\nstatic uint8_t font_bytes[%u * %u] = {", glyph_count, gbytes);
6646ad59cemaste	total = glyph_count * gbytes;
6798e93f6ed	gbuf = malloc(total);
6898e93f6ed
6998e93f6ed	if (fread(gbuf, total, 1, stdin) != 1) {
7098e93f6ed		perror("glyph");
7198e93f6ed		return (1);
7298e93f6ed	}
7398e93f6ed
7498e93f6ed	for (j = 0; j < total; j += 12) {
7598e93f6ed		for (k = 0; k < 12 && k < total - j; k++) {
7698e93f6ed			printf(k == 0 ? "\n\t" : " ");
7798e93f6ed			printf("0x%02hhx,", gbuf[j + k]);
7898e93f6ed		}
7998e93f6ed	}
8098e93f6ed
8198e93f6ed	free(gbuf);
8298e93f6ed	printf("\n};\n");
8398e93f6ed
8498e93f6ed	return (0);
8598e93f6ed}
8698e93f6ed
8746ad59cemastestatic const char *map_names[4] = {
8846ad59cemaste    "normal", "normal_right", "bold", "bold_right" };
8946ad59cemaste
9098e93f6edstatic int
9146ad59cemasteprint_mappings(struct file_header *fh, int map_index)
9298e93f6ed{
9398e93f6ed	struct file_mapping fm;
9498e93f6ed	unsigned int nmappings, i, col = 0;
9598e93f6ed
9646ad59cemaste
9746ad59cemaste	nmappings = be32toh(fh->map_count[map_index]);
9898e93f6ed
9998e93f6ed	if (nmappings == 0)
10098e93f6ed		return (0);
10198e93f6ed
10298e93f6ed	printf("\nstatic struct vt_font_map font_mapping_%s[%u] = {",
10346ad59cemaste	    map_names[map_index], nmappings);
10498e93f6ed
10598e93f6ed	for (i = 0; i < nmappings; i++) {
10698e93f6ed		if (fread(&fm, sizeof fm, 1, stdin) != 1) {
10798e93f6ed			perror("mapping");
10898e93f6ed			return (1);
10998e93f6ed		}
11098e93f6ed
11198e93f6ed		printf(col == 0 ? "\n\t" : " ");
11298e93f6ed		printf("{ 0x%04x, 0x%04x, 0x%02x },",
11398e93f6ed		    be32toh(fm.source), be16toh(fm.destination),
11498e93f6ed		    be16toh(fm.length));
11598e93f6ed		col = (col + 1) % 2;
11698e93f6ed	}
11798e93f6ed
11898e93f6ed	printf("\n};\n");
11998e93f6ed
12098e93f6ed	return (0);
12198e93f6ed}
12298e93f6ed
12398e93f6edstatic int
12498e93f6edprint_info(struct file_header *fh)
12598e93f6ed{
12646ad59cemaste	unsigned int i;
12798e93f6ed
12898e93f6ed	printf(
12998e93f6ed	    "\nstruct vt_font vt_font_default = {\n"
13098e93f6ed	    "\t.vf_width\t\t= %u,\n"
13198e93f6ed	    "\t.vf_height\t\t= %u,\n"
13298e93f6ed	    "\t.vf_bytes\t\t= font_bytes,\n",
13398e93f6ed	    fh->width, fh->height);
13498e93f6ed
13546ad59cemaste	printf("\t.vf_map\t\t\t= {\n");
13646ad59cemaste	for (i = 0; i < 4; i++) {
13746ad59cemaste		if (fh->map_count[i] > 0)
13846ad59cemaste			printf("\t\t\t\t    font_mapping_%s,\n", map_names[i]);
13946ad59cemaste		else
14046ad59cemaste			printf("\t\t\t\t    NULL,\n");
14146ad59cemaste	}
14284e903aemaste	printf("\t\t\t\t  },\n");
14346ad59cemaste	printf("\t.vf_map_count\t\t= { %u, %u, %u, %u },\n",
14446ad59cemaste	    be32toh(fh->map_count[0]),
14546ad59cemaste	    be32toh(fh->map_count[1]),
14646ad59cemaste	    be32toh(fh->map_count[2]),
14746ad59cemaste	    be32toh(fh->map_count[3]));
14898e93f6ed	printf("\t.vf_refcount\t\t= 1,\n};\n");
14998e93f6ed
15098e93f6ed	return (0);
15198e93f6ed}
15298e93f6ed
15398e93f6edint
15498e93f6edmain(int argc __unused, char *argv[] __unused)
15598e93f6ed{
15698e93f6ed	struct file_header fh;
15742f0c8aemaste	unsigned int i;
15898e93f6ed
15998e93f6ed	if (fread(&fh, sizeof fh, 1, stdin) != 1) {
16098e93f6ed		perror("file_header");
16198e93f6ed		return (1);
16298e93f6ed	}
16398e93f6ed
16446ad59cemaste	if (memcmp(fh.magic, "VFNT0002", 8) != 0) {
16598e93f6ed		fprintf(stderr, "Bad magic\n");
16698e93f6ed		return (1);
16798e93f6ed	}
16898e93f6ed
16998e93f6ed	printf("#include <dev/vt/vt.h>\n");
17098e93f6ed
17198e93f6ed	if (print_glyphs(&fh) != 0)
17298e93f6ed		return (1);
17342f0c8aemaste	for (i = 0; i < 4; i++)
17442f0c8aemaste		if (print_mappings(&fh, i) != 0)
17542f0c8aemaste			return (1);
17698e93f6ed	if (print_info(&fh) != 0)
17798e93f6ed		return (1);
17898e93f6ed
17998e93f6ed	return (0);
18098e93f6ed}
181