2013-12-05 22:58:05 +00:00
|
|
|
/*-
|
2014-06-05 18:38:27 +00:00
|
|
|
* Copyright (c) 2009, 2014 The FreeBSD Foundation
|
2013-12-05 22:58:05 +00:00
|
|
|
* 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$");
|
|
|
|
|
2014-06-04 03:02:49 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/fnv_hash.h>
|
2013-12-05 22:58:05 +00:00
|
|
|
#include <sys/endian.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2014-06-09 20:52:35 +00:00
|
|
|
#include <err.h>
|
2013-12-05 22:58:05 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-06-03 17:53:11 +00:00
|
|
|
#include <unistd.h>
|
2013-12-05 22:58:05 +00:00
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
#define VFNT_MAPS 4
|
|
|
|
#define VFNT_MAP_NORMAL 0
|
2014-06-09 20:52:35 +00:00
|
|
|
#define VFNT_MAP_NORMAL_RH 1
|
2013-12-21 13:58:55 +00:00
|
|
|
#define VFNT_MAP_BOLD 2
|
2014-06-09 20:52:35 +00:00
|
|
|
#define VFNT_MAP_BOLD_RH 3
|
2013-12-21 13:58:55 +00:00
|
|
|
|
2014-06-03 17:53:11 +00:00
|
|
|
static unsigned int width = 8, wbytes, height = 16;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
|
|
|
struct glyph {
|
|
|
|
TAILQ_ENTRY(glyph) g_list;
|
2014-06-04 03:02:49 +00:00
|
|
|
SLIST_ENTRY(glyph) g_hash;
|
2013-12-05 22:58:05 +00:00
|
|
|
uint8_t *g_data;
|
|
|
|
unsigned int g_index;
|
|
|
|
};
|
|
|
|
|
2014-06-04 03:02:49 +00:00
|
|
|
#define FONTCVT_NHASH 4096
|
2013-12-21 13:58:55 +00:00
|
|
|
TAILQ_HEAD(glyph_list, glyph);
|
2014-06-04 03:02:49 +00:00
|
|
|
static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
|
2013-12-21 13:58:55 +00:00
|
|
|
static struct glyph_list glyphs[VFNT_MAPS] = {
|
|
|
|
TAILQ_HEAD_INITIALIZER(glyphs[0]),
|
|
|
|
TAILQ_HEAD_INITIALIZER(glyphs[1]),
|
|
|
|
TAILQ_HEAD_INITIALIZER(glyphs[2]),
|
|
|
|
TAILQ_HEAD_INITIALIZER(glyphs[3]),
|
|
|
|
};
|
|
|
|
static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
|
|
|
struct mapping {
|
|
|
|
TAILQ_ENTRY(mapping) m_list;
|
|
|
|
unsigned int m_char;
|
|
|
|
unsigned int m_length;
|
|
|
|
struct glyph *m_glyph;
|
|
|
|
};
|
|
|
|
|
|
|
|
TAILQ_HEAD(mapping_list, mapping);
|
2013-12-21 13:58:55 +00:00
|
|
|
static struct mapping_list maps[VFNT_MAPS] = {
|
|
|
|
TAILQ_HEAD_INITIALIZER(maps[0]),
|
|
|
|
TAILQ_HEAD_INITIALIZER(maps[1]),
|
|
|
|
TAILQ_HEAD_INITIALIZER(maps[2]),
|
|
|
|
TAILQ_HEAD_INITIALIZER(maps[3]),
|
|
|
|
};
|
|
|
|
static unsigned int mapping_total, map_count[4], map_folded_count[4],
|
|
|
|
mapping_unique, mapping_dupe;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
|
2014-07-21 17:18:00 +00:00
|
|
|
(void)fprintf(stderr,
|
2014-06-10 18:29:45 +00:00
|
|
|
"usage: vtfontcvt [-w width] [-h height] [-v] normal.bdf [bold.bdf] out.fnt\n");
|
2013-12-05 22:58:05 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-08-31 23:08:39 +00:00
|
|
|
static void *
|
|
|
|
xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *m;
|
|
|
|
|
|
|
|
if ((m = malloc(size)) == NULL)
|
|
|
|
errx(1, "memory allocation failure");
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:58:05 +00:00
|
|
|
static int
|
2013-12-21 13:58:55 +00:00
|
|
|
add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
|
2013-12-05 22:58:05 +00:00
|
|
|
{
|
|
|
|
struct mapping *mp;
|
|
|
|
struct mapping_list *ml;
|
|
|
|
|
|
|
|
mapping_total++;
|
|
|
|
|
2015-08-31 23:08:39 +00:00
|
|
|
mp = xmalloc(sizeof *mp);
|
2013-12-05 22:58:05 +00:00
|
|
|
mp->m_char = c;
|
|
|
|
mp->m_glyph = gl;
|
|
|
|
mp->m_length = 0;
|
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
ml = &maps[map_idx];
|
2013-12-05 22:58:05 +00:00
|
|
|
if (TAILQ_LAST(ml, mapping_list) != NULL &&
|
2014-07-21 17:43:37 +00:00
|
|
|
TAILQ_LAST(ml, mapping_list)->m_char >= c)
|
2016-09-14 18:22:12 +00:00
|
|
|
errx(1, "Bad ordering at character %u", c);
|
2013-12-05 22:58:05 +00:00
|
|
|
TAILQ_INSERT_TAIL(ml, mp, m_list);
|
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
map_count[map_idx]++;
|
2013-12-05 22:58:05 +00:00
|
|
|
mapping_unique++;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:52:35 +00:00
|
|
|
static int
|
|
|
|
dedup_mapping(unsigned int map_idx)
|
|
|
|
{
|
|
|
|
struct mapping *mp_bold, *mp_normal, *mp_temp;
|
|
|
|
unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
|
|
|
|
|
|
|
|
assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RH);
|
|
|
|
mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
|
|
|
|
TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
|
|
|
|
while (mp_normal->m_char < mp_bold->m_char)
|
|
|
|
mp_normal = TAILQ_NEXT(mp_normal, m_list);
|
2014-07-21 17:43:37 +00:00
|
|
|
if (mp_bold->m_char != mp_normal->m_char)
|
2016-09-14 18:22:12 +00:00
|
|
|
errx(1, "Character %u not in normal font!",
|
2014-06-09 20:52:35 +00:00
|
|
|
mp_bold->m_char);
|
|
|
|
if (mp_bold->m_glyph != mp_normal->m_glyph)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* No mapping is needed if it's equal to the normal mapping. */
|
|
|
|
TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
|
|
|
|
free(mp_bold);
|
|
|
|
mapping_dupe++;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:58:05 +00:00
|
|
|
static struct glyph *
|
2013-12-21 13:58:55 +00:00
|
|
|
add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
|
2013-12-05 22:58:05 +00:00
|
|
|
{
|
|
|
|
struct glyph *gl;
|
2014-06-04 03:02:49 +00:00
|
|
|
int hash;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
|
|
|
glyph_total++;
|
2013-12-21 13:58:55 +00:00
|
|
|
glyph_count[map_idx]++;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
2014-06-04 03:02:49 +00:00
|
|
|
hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
|
|
|
|
SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
|
|
|
|
if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
|
|
|
|
glyph_dupe++;
|
|
|
|
return (gl);
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 23:08:39 +00:00
|
|
|
gl = xmalloc(sizeof *gl);
|
|
|
|
gl->g_data = xmalloc(wbytes * height);
|
2013-12-05 22:58:05 +00:00
|
|
|
memcpy(gl->g_data, bytes, wbytes * height);
|
|
|
|
if (fallback)
|
2013-12-21 13:58:55 +00:00
|
|
|
TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
|
2013-12-05 22:58:05 +00:00
|
|
|
else
|
2013-12-21 13:58:55 +00:00
|
|
|
TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
|
2014-06-04 03:02:49 +00:00
|
|
|
SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
|
2013-12-05 22:58:05 +00:00
|
|
|
|
|
|
|
glyph_unique++;
|
|
|
|
return (gl);
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:38:27 +00:00
|
|
|
static int
|
|
|
|
add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
|
|
|
|
{
|
|
|
|
struct glyph *gl;
|
|
|
|
|
|
|
|
/* Prevent adding two glyphs for 0xFFFD */
|
|
|
|
if (curchar == 0xFFFD) {
|
|
|
|
if (map_idx < VFNT_MAP_BOLD)
|
|
|
|
gl = add_glyph(bytes, 0, 1);
|
|
|
|
} else if (curchar >= 0x20) {
|
|
|
|
gl = add_glyph(bytes, map_idx, 0);
|
|
|
|
if (add_mapping(gl, curchar, map_idx) != 0)
|
|
|
|
return (1);
|
|
|
|
if (bytes_r != NULL) {
|
|
|
|
gl = add_glyph(bytes_r, map_idx + 1, 0);
|
|
|
|
if (add_mapping(gl, curchar,
|
|
|
|
map_idx + 1) != 0)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-05 22:58:05 +00:00
|
|
|
static int
|
2013-12-21 13:58:55 +00:00
|
|
|
parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
|
|
|
|
unsigned int dwidth)
|
|
|
|
{
|
|
|
|
uint8_t *p;
|
|
|
|
unsigned int i, subline;
|
|
|
|
|
2014-07-21 17:43:37 +00:00
|
|
|
if (dwidth != width && dwidth != width * 2)
|
2016-09-14 18:22:12 +00:00
|
|
|
errx(1, "Bitmap with unsupported width %u!", dwidth);
|
2013-12-21 13:58:55 +00:00
|
|
|
|
|
|
|
/* Move pixel data right to simplify splitting double characters. */
|
|
|
|
line >>= (howmany(dwidth, 8) * 8) - dwidth;
|
|
|
|
|
|
|
|
for (i = dwidth / width; i > 0; i--) {
|
|
|
|
p = (i == 2) ? right : left;
|
|
|
|
|
|
|
|
subline = line & ((1 << width) - 1);
|
|
|
|
subline <<= (howmany(width, 8) * 8) - width;
|
|
|
|
|
|
|
|
if (wbytes == 1) {
|
|
|
|
*p = subline;
|
|
|
|
} else if (wbytes == 2) {
|
|
|
|
*p++ = subline >> 8;
|
|
|
|
*p = subline;
|
|
|
|
} else {
|
2016-09-14 18:22:12 +00:00
|
|
|
errx(1, "Unsupported wbytes %u!", wbytes);
|
2013-12-21 13:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
line >>= width;
|
|
|
|
}
|
2014-06-10 18:29:45 +00:00
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-06-05 18:38:27 +00:00
|
|
|
parse_bdf(FILE *fp, unsigned int map_idx)
|
2013-12-05 22:58:05 +00:00
|
|
|
{
|
|
|
|
char *ln;
|
|
|
|
size_t length;
|
2013-12-21 13:58:55 +00:00
|
|
|
uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
|
|
|
|
unsigned int curchar = 0, dwidth = 0, i, line;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
|
|
|
while ((ln = fgetln(fp, &length)) != NULL) {
|
|
|
|
ln[length - 1] = '\0';
|
|
|
|
|
|
|
|
if (strncmp(ln, "ENCODING ", 9) == 0) {
|
|
|
|
curchar = atoi(ln + 9);
|
|
|
|
}
|
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
if (strncmp(ln, "DWIDTH ", 7) == 0) {
|
|
|
|
dwidth = atoi(ln + 7);
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:38:44 +00:00
|
|
|
if (strncmp(ln, "BITMAP", 6) == 0 &&
|
|
|
|
(ln[6] == ' ' || ln[6] == '\0')) {
|
2013-12-05 22:58:05 +00:00
|
|
|
for (i = 0; i < height; i++) {
|
2014-07-21 17:43:37 +00:00
|
|
|
if ((ln = fgetln(fp, &length)) == NULL)
|
2016-09-14 18:22:12 +00:00
|
|
|
errx(1, "Unexpected EOF!");
|
2013-12-05 22:58:05 +00:00
|
|
|
ln[length - 1] = '\0';
|
|
|
|
sscanf(ln, "%x", &line);
|
2013-12-21 13:58:55 +00:00
|
|
|
if (parse_bitmap_line(bytes + i * wbytes,
|
|
|
|
bytes_r + i * wbytes, line, dwidth) != 0)
|
2013-12-05 22:58:05 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:38:27 +00:00
|
|
|
if (add_char(curchar, map_idx, bytes,
|
|
|
|
dwidth == width * 2 ? bytes_r : NULL) != 0)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-07-02 19:31:49 +00:00
|
|
|
static void
|
|
|
|
set_width(int w)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (w <= 0 || w > 128)
|
|
|
|
errx(1, "invalid width %d", w);
|
|
|
|
width = w;
|
|
|
|
wbytes = howmany(width, 8);
|
|
|
|
}
|
|
|
|
|
2014-06-05 18:38:27 +00:00
|
|
|
static int
|
|
|
|
parse_hex(FILE *fp, unsigned int map_idx)
|
|
|
|
{
|
|
|
|
char *ln, *p;
|
|
|
|
char fmt_str[8];
|
|
|
|
size_t length;
|
2015-09-01 01:35:43 +00:00
|
|
|
uint8_t *bytes = NULL, *bytes_r = NULL;
|
2014-06-05 18:38:27 +00:00
|
|
|
unsigned curchar = 0, i, line, chars_per_row, dwidth;
|
2015-09-01 01:35:43 +00:00
|
|
|
int rv = 0;
|
2014-06-05 18:38:27 +00:00
|
|
|
|
|
|
|
while ((ln = fgetln(fp, &length)) != NULL) {
|
|
|
|
ln[length - 1] = '\0';
|
|
|
|
|
|
|
|
if (strncmp(ln, "# Height: ", 10) == 0) {
|
2015-09-01 01:35:43 +00:00
|
|
|
if (bytes != NULL)
|
|
|
|
errx(1, "malformed input: Height tag after font data");
|
2014-06-05 18:38:27 +00:00
|
|
|
height = atoi(ln + 10);
|
|
|
|
} else if (strncmp(ln, "# Width: ", 9) == 0) {
|
2015-09-01 01:35:43 +00:00
|
|
|
if (bytes != NULL)
|
|
|
|
errx(1, "malformed input: Width tag after font data");
|
2014-07-02 19:31:49 +00:00
|
|
|
set_width(atoi(ln + 9));
|
2016-03-15 21:32:46 +00:00
|
|
|
} else if (sscanf(ln, "%6x:", &curchar)) {
|
2015-09-01 01:35:43 +00:00
|
|
|
if (bytes == NULL) {
|
|
|
|
bytes = xmalloc(wbytes * height);
|
|
|
|
bytes_r = xmalloc(wbytes * height);
|
|
|
|
}
|
2016-03-15 21:32:46 +00:00
|
|
|
/* ln is guaranteed to have a colon here. */
|
|
|
|
p = strchr(ln, ':') + 1;
|
2014-06-05 18:38:27 +00:00
|
|
|
chars_per_row = strlen(p) / height;
|
|
|
|
dwidth = width;
|
2014-07-02 19:31:49 +00:00
|
|
|
if (chars_per_row / 2 > (width + 7) / 8)
|
2014-06-05 18:38:27 +00:00
|
|
|
dwidth *= 2; /* Double-width character. */
|
|
|
|
snprintf(fmt_str, sizeof(fmt_str), "%%%ux",
|
|
|
|
chars_per_row);
|
2014-06-10 18:29:45 +00:00
|
|
|
|
2014-06-05 18:38:27 +00:00
|
|
|
for (i = 0; i < height; i++) {
|
|
|
|
sscanf(p, fmt_str, &line);
|
|
|
|
p += chars_per_row;
|
|
|
|
if (parse_bitmap_line(bytes + i * wbytes,
|
2015-09-01 01:35:43 +00:00
|
|
|
bytes_r + i * wbytes, line, dwidth) != 0) {
|
|
|
|
rv = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
2014-06-05 18:38:27 +00:00
|
|
|
|
|
|
|
if (add_char(curchar, map_idx, bytes,
|
2015-09-01 01:35:43 +00:00
|
|
|
dwidth == width * 2 ? bytes_r : NULL) != 0) {
|
|
|
|
rv = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 01:35:43 +00:00
|
|
|
out:
|
|
|
|
free(bytes);
|
|
|
|
free(bytes_r);
|
|
|
|
return (rv);
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 18:38:27 +00:00
|
|
|
static int
|
|
|
|
parse_file(const char *filename, unsigned int map_idx)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
size_t len;
|
2014-06-11 17:19:57 +00:00
|
|
|
int rv;
|
2014-06-05 18:38:27 +00:00
|
|
|
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
perror(filename);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
len = strlen(filename);
|
|
|
|
if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
|
2014-06-11 17:19:57 +00:00
|
|
|
rv = parse_hex(fp, map_idx);
|
|
|
|
else
|
|
|
|
rv = parse_bdf(fp, map_idx);
|
|
|
|
fclose(fp);
|
|
|
|
return (rv);
|
2014-06-05 18:38:27 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 22:58:05 +00:00
|
|
|
static void
|
|
|
|
number_glyphs(void)
|
|
|
|
{
|
|
|
|
struct glyph *gl;
|
2013-12-21 13:58:55 +00:00
|
|
|
unsigned int i, idx = 0;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
for (i = 0; i < VFNT_MAPS; i++)
|
|
|
|
TAILQ_FOREACH(gl, &glyphs[i], g_list)
|
|
|
|
gl->g_index = idx++;
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 12:59:56 +00:00
|
|
|
static int
|
2013-12-05 22:58:05 +00:00
|
|
|
write_glyphs(FILE *fp)
|
|
|
|
{
|
|
|
|
struct glyph *gl;
|
2013-12-21 13:58:55 +00:00
|
|
|
unsigned int i;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
for (i = 0; i < VFNT_MAPS; i++) {
|
|
|
|
TAILQ_FOREACH(gl, &glyphs[i], g_list)
|
2014-06-10 12:59:56 +00:00
|
|
|
if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
|
|
|
|
return (1);
|
2013-12-21 13:58:55 +00:00
|
|
|
}
|
2014-06-10 12:59:56 +00:00
|
|
|
return (0);
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-12-21 13:58:55 +00:00
|
|
|
fold_mappings(unsigned int map_idx)
|
2013-12-05 22:58:05 +00:00
|
|
|
{
|
2013-12-21 13:58:55 +00:00
|
|
|
struct mapping_list *ml = &maps[map_idx];
|
2013-12-05 22:58:05 +00:00
|
|
|
struct mapping *mn, *mp, *mbase;
|
|
|
|
|
|
|
|
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;
|
2013-12-21 13:58:55 +00:00
|
|
|
map_folded_count[map_idx]++;
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct file_mapping {
|
|
|
|
uint32_t source;
|
|
|
|
uint16_t destination;
|
|
|
|
uint16_t length;
|
|
|
|
} __packed;
|
|
|
|
|
2014-06-10 12:59:56 +00:00
|
|
|
static int
|
2013-12-21 13:58:55 +00:00
|
|
|
write_mappings(FILE *fp, unsigned int map_idx)
|
2013-12-05 22:58:05 +00:00
|
|
|
{
|
2013-12-21 13:58:55 +00:00
|
|
|
struct mapping_list *ml = &maps[map_idx];
|
2013-12-05 22:58:05 +00:00
|
|
|
struct mapping *mp;
|
|
|
|
struct file_mapping fm;
|
|
|
|
unsigned int i = 0, j = 0;
|
|
|
|
|
|
|
|
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);
|
2014-06-10 12:59:56 +00:00
|
|
|
if (fwrite(&fm, sizeof fm, 1, fp) != 1)
|
|
|
|
return (1);
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(i == j);
|
2014-06-10 12:59:56 +00:00
|
|
|
return (0);
|
2013-12-05 22:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct file_header {
|
|
|
|
uint8_t magic[8];
|
|
|
|
uint8_t width;
|
|
|
|
uint8_t height;
|
2013-12-21 13:58:55 +00:00
|
|
|
uint16_t pad;
|
|
|
|
uint32_t glyph_count;
|
|
|
|
uint32_t map_count[4];
|
2013-12-05 22:58:05 +00:00
|
|
|
} __packed;
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_fnt(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
struct file_header fh = {
|
2013-12-21 13:58:55 +00:00
|
|
|
.magic = "VFNT0002",
|
2013-12-05 22:58:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fp = fopen(filename, "wb");
|
|
|
|
if (fp == NULL) {
|
|
|
|
perror(filename);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fh.width = width;
|
|
|
|
fh.height = height;
|
2013-12-21 13:58:55 +00:00
|
|
|
fh.glyph_count = htobe32(glyph_unique);
|
|
|
|
fh.map_count[0] = htobe32(map_folded_count[0]);
|
|
|
|
fh.map_count[1] = htobe32(map_folded_count[1]);
|
|
|
|
fh.map_count[2] = htobe32(map_folded_count[2]);
|
|
|
|
fh.map_count[3] = htobe32(map_folded_count[3]);
|
2014-06-10 12:59:56 +00:00
|
|
|
if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
|
|
|
|
perror(filename);
|
2014-06-11 17:19:57 +00:00
|
|
|
fclose(fp);
|
2014-06-10 12:59:56 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2014-06-10 18:29:45 +00:00
|
|
|
|
2014-06-10 12:59:56 +00:00
|
|
|
if (write_glyphs(fp) != 0 ||
|
|
|
|
write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
|
|
|
|
write_mappings(fp, 1) != 0 ||
|
|
|
|
write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
|
|
|
|
write_mappings(fp, 3) != 0) {
|
|
|
|
perror(filename);
|
2014-06-11 17:19:57 +00:00
|
|
|
fclose(fp);
|
2014-06-10 12:59:56 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2013-12-05 22:58:05 +00:00
|
|
|
|
2014-06-11 17:19:57 +00:00
|
|
|
fclose(fp);
|
2013-12-05 22:58:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2014-06-09 20:49:13 +00:00
|
|
|
static void
|
|
|
|
print_font_info(void)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Statistics:\n"
|
2015-05-14 20:17:53 +00:00
|
|
|
"- glyph_total: %6u\n"
|
|
|
|
"- glyph_normal: %6u\n"
|
|
|
|
"- glyph_normal_right: %6u\n"
|
|
|
|
"- glyph_bold: %6u\n"
|
|
|
|
"- glyph_bold_right: %6u\n"
|
|
|
|
"- glyph_unique: %6u\n"
|
|
|
|
"- glyph_dupe: %6u\n"
|
|
|
|
"- mapping_total: %6u\n"
|
|
|
|
"- mapping_normal: %6u\n"
|
|
|
|
"- mapping_normal_folded: %6u\n"
|
|
|
|
"- mapping_normal_right: %6u\n"
|
|
|
|
"- mapping_normal_right_folded: %6u\n"
|
|
|
|
"- mapping_bold: %6u\n"
|
|
|
|
"- mapping_bold_folded: %6u\n"
|
|
|
|
"- mapping_bold_right: %6u\n"
|
|
|
|
"- mapping_bold_right_folded: %6u\n"
|
|
|
|
"- mapping_unique: %6u\n"
|
|
|
|
"- mapping_dupe: %6u\n",
|
2014-06-09 20:49:13 +00:00
|
|
|
glyph_total,
|
|
|
|
glyph_count[0],
|
|
|
|
glyph_count[1],
|
|
|
|
glyph_count[2],
|
|
|
|
glyph_count[3],
|
|
|
|
glyph_unique, glyph_dupe,
|
|
|
|
mapping_total,
|
|
|
|
map_count[0], map_folded_count[0],
|
|
|
|
map_count[1], map_folded_count[1],
|
|
|
|
map_count[2], map_folded_count[2],
|
|
|
|
map_count[3], map_folded_count[3],
|
|
|
|
mapping_unique, mapping_dupe);
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:58:05 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-09 20:49:13 +00:00
|
|
|
int ch, val, verbose = 0;
|
2013-12-05 22:58:05 +00:00
|
|
|
|
2013-12-21 13:58:55 +00:00
|
|
|
assert(sizeof(struct file_header) == 32);
|
2013-12-05 22:58:05 +00:00
|
|
|
assert(sizeof(struct file_mapping) == 8);
|
|
|
|
|
2014-06-10 18:29:45 +00:00
|
|
|
while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
|
2014-06-03 17:53:11 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
2014-06-09 20:49:13 +00:00
|
|
|
val = atoi(optarg);
|
2014-07-21 17:43:37 +00:00
|
|
|
if (val <= 0 || val > 128)
|
2014-06-09 20:49:13 +00:00
|
|
|
errx(1, "Invalid height %d", val);
|
|
|
|
height = val;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
2014-06-03 17:53:11 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2014-07-02 19:31:49 +00:00
|
|
|
set_width(atoi(optarg));
|
2014-06-03 17:53:11 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2014-06-03 17:54:18 +00:00
|
|
|
if (argc < 2 || argc > 3)
|
2013-12-05 22:58:05 +00:00
|
|
|
usage();
|
2014-06-03 17:53:11 +00:00
|
|
|
|
2013-12-05 22:58:05 +00:00
|
|
|
wbytes = howmany(width, 8);
|
|
|
|
|
2014-06-05 18:38:27 +00:00
|
|
|
if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
|
2013-12-05 22:58:05 +00:00
|
|
|
return (1);
|
2014-06-03 17:54:18 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
if (argc == 2) {
|
2014-06-05 18:38:27 +00:00
|
|
|
if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
|
2014-06-03 17:54:18 +00:00
|
|
|
return (1);
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
}
|
2013-12-05 22:58:05 +00:00
|
|
|
number_glyphs();
|
2014-06-09 20:52:35 +00:00
|
|
|
dedup_mapping(VFNT_MAP_BOLD);
|
|
|
|
dedup_mapping(VFNT_MAP_BOLD_RH);
|
2013-12-05 22:58:05 +00:00
|
|
|
fold_mappings(0);
|
|
|
|
fold_mappings(1);
|
2013-12-21 13:58:55 +00:00
|
|
|
fold_mappings(2);
|
|
|
|
fold_mappings(3);
|
2014-06-03 17:54:18 +00:00
|
|
|
if (write_fnt(argv[0]) != 0)
|
2013-12-05 22:58:05 +00:00
|
|
|
return (1);
|
2014-06-09 20:49:13 +00:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
print_font_info();
|
|
|
|
|
2013-12-05 22:58:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|